jleibs commited on
Commit
f266fba
1 Parent(s): d4cef29

Parameterize cube

Browse files
Files changed (1) hide show
  1. app.py +56 -11
app.py CHANGED
@@ -3,24 +3,66 @@ import rerun.blueprint as rrb
3
  import numpy as np
4
  import gradio as gr
5
  import urllib
 
 
6
 
 
7
 
8
- def html_template(rrd, width=950, height=712, app_url="https://app.rerun.io"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  encoded_url = urllib.parse.quote(rrd)
10
  return f"""<iframe width="{width}" height="{height}" src="{app_url}?url={encoded_url}" frameborder="0" allowfullscreen=""></iframe>"""
11
 
12
- def show_cube():
13
  rr.init("my data")
14
 
15
- positions = np.zeros((10, 3))
16
- positions[:,0] = np.linspace(-10,10,10)
17
-
18
- colors = np.zeros((10,3), dtype=np.uint8)
19
- colors[:,0] = np.linspace(0,255,10)
20
-
21
- rr.log("my_points", rr.Points3D(positions=positions, colors=colors, radii=0.5))
22
 
23
- blueprint = rrb.Spatial3DView(origin='my_points')
24
 
25
  rr.save("cube.rrd", default_blueprint=blueprint)
26
 
@@ -29,13 +71,16 @@ def show_cube():
29
 
30
  with gr.Blocks() as demo:
31
  with gr.Row():
 
 
 
32
  button = gr.Button("Show Cube")
33
  with gr.Row():
34
  rrd = gr.File()
35
  with gr.Row():
36
  viewer = gr.HTML()
37
 
38
- button.click(show_cube, outputs=rrd)
39
  rrd.change(html_template, js="""(rrd) => { console.log(rrd.url); return rrd.url}""", inputs=[rrd], outputs=viewer, preprocess=False)
40
 
41
 
 
3
  import numpy as np
4
  import gradio as gr
5
  import urllib
6
+ from math import cos, sin
7
+ from collections import namedtuple
8
 
9
+ ColorGrid = namedtuple("ColorGrid", ["positions", "colors"])
10
 
11
+ def build_color_grid(x_count=10, y_count=10, z_count=10, twist=0):
12
+ """
13
+ Create a cube of points with colors.
14
+
15
+ The total point cloud will have x_count * y_count * z_count points.
16
+
17
+ Parameters
18
+ ----------
19
+ x_count, y_count, z_count:
20
+ Number of points in each dimension.
21
+ twist:
22
+ Angle to twist from bottom to top of the cube
23
+
24
+ """
25
+
26
+ grid = np.mgrid[
27
+ slice(-x_count, x_count, x_count * 1j),
28
+ slice(-y_count, y_count, y_count * 1j),
29
+ slice(-z_count, z_count, z_count * 1j),
30
+ ]
31
+
32
+ angle = np.linspace(-float(twist) / 2, float(twist) / 2, z_count)
33
+ for z in range(z_count):
34
+ xv, yv, zv = grid[:, :, :, z]
35
+ rot_xv = xv * cos(angle[z]) - yv * sin(angle[z])
36
+ rot_yv = xv * sin(angle[z]) + yv * cos(angle[z])
37
+ grid[:, :, :, z] = [rot_xv, rot_yv, zv]
38
+
39
+ positions = np.vstack([xyz.ravel() for xyz in grid])
40
+
41
+ colors = np.vstack(
42
+ [
43
+ xyz.ravel()
44
+ for xyz in np.mgrid[
45
+ slice(0, 255, x_count * 1j),
46
+ slice(0, 255, y_count * 1j),
47
+ slice(0, 255, z_count * 1j),
48
+ ]
49
+ ]
50
+ )
51
+
52
+ return ColorGrid(positions.T, colors.T.astype(np.uint8))
53
+
54
+
55
+ def html_template(rrd, width=1319, height=900, app_url="https://app.rerun.io"):
56
  encoded_url = urllib.parse.quote(rrd)
57
  return f"""<iframe width="{width}" height="{height}" src="{app_url}?url={encoded_url}" frameborder="0" allowfullscreen=""></iframe>"""
58
 
59
+ def show_cube(x, y, z):
60
  rr.init("my data")
61
 
62
+ cube = build_color_grid(int(x), int(y), int(z), twist=0)
63
+ rr.log("cube", rr.Points3D(cube.positions, colors=cube.colors, radii=0.5))
 
 
 
 
 
64
 
65
+ blueprint = rrb.Spatial3DView(origin='cube')
66
 
67
  rr.save("cube.rrd", default_blueprint=blueprint)
68
 
 
71
 
72
  with gr.Blocks() as demo:
73
  with gr.Row():
74
+ x_count = gr.Slider(minimum=1, maximum=10, value=5, label="X Count")
75
+ y_count = gr.Slider(minimum=1, maximum=10, value=5, label="Y Count")
76
+ z_count = gr.Slider(minimum=1, maximum=10, value=5, label="Z Count")
77
  button = gr.Button("Show Cube")
78
  with gr.Row():
79
  rrd = gr.File()
80
  with gr.Row():
81
  viewer = gr.HTML()
82
 
83
+ button.click(show_cube, inputs=[x_count, y_count, z_count], outputs=rrd)
84
  rrd.change(html_template, js="""(rrd) => { console.log(rrd.url); return rrd.url}""", inputs=[rrd], outputs=viewer, preprocess=False)
85
 
86