File size: 2,824 Bytes
72945c4
08fa0b1
f266fba
08fa0b1
 
 
 
 
 
 
 
d4cef29
f266fba
d4cef29
08fa0b1
f266fba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08fa0b1
 
 
72945c4
08fa0b1
 
 
 
 
72945c4
08fa0b1
d4cef29
08fa0b1
 
 
 
 
 
d4cef29
 
 
 
08fa0b1
 
 
 
 
 
 
 
 
 
 
 
d4cef29
08fa0b1
 
d4cef29
08fa0b1
 
 
d4cef29
08fa0b1
d4cef29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
from math import cos, sin, pi
from collections import namedtuple
import time

import gradio as gr
from gradio_rerun import Rerun

import rerun as rr
import rerun.blueprint as rrb


ColorGrid = namedtuple("ColorGrid", ["positions", "colors"])


def build_color_grid(x_count=10, y_count=10, z_count=10, twist=0):
    """
    Create a cube of points with colors.

    The total point cloud will have x_count * y_count * z_count points.

    Parameters
    ----------
    x_count, y_count, z_count:
        Number of points in each dimension.
    twist:
        Angle to twist from bottom to top of the cube

    """

    grid = np.mgrid[
        slice(-x_count, x_count, x_count * 1j),
        slice(-y_count, y_count, y_count * 1j),
        slice(-z_count, z_count, z_count * 1j),
    ]

    angle = np.linspace(-float(twist) / 2, float(twist) / 2, z_count)
    for z in range(z_count):
        xv, yv, zv = grid[:, :, :, z]
        rot_xv = xv * cos(angle[z]) - yv * sin(angle[z])
        rot_yv = xv * sin(angle[z]) + yv * cos(angle[z])
        grid[:, :, :, z] = [rot_xv, rot_yv, zv]

    positions = np.vstack([xyz.ravel() for xyz in grid])

    colors = np.vstack(
        [
            xyz.ravel()
            for xyz in np.mgrid[
                slice(0, 255, x_count * 1j),
                slice(0, 255, y_count * 1j),
                slice(0, 255, z_count * 1j),
            ]
        ]
    )

    return ColorGrid(positions.T, colors.T.astype(np.uint8))


@rr.thread_local_stream("rerun_example_cube")
def show_cube(x, y, z, twist):
    stream = rr.binary_stream()

    blueprint = rrb.Blueprint(
        rrb.Spatial3DView(origin="cube"),
        collapse_panels=True,
    )
    rr.send_blueprint(blueprint)

    yield stream.read()

    for step in range(twist):
        rr.set_time_sequence("steps", step)
        cube = build_color_grid(int(x), int(y), int(z), twist=pi * step / 180)
        rr.log("cube", rr.Points3D(cube.positions, colors=cube.colors, radii=0.5))
        yield stream.read()
        time.sleep(0.01)


with gr.Blocks() as demo:
    with gr.Row():
        x_count = gr.Number(
            minimum=1, maximum=20, value=10, precision=0, label="X Count"
        )
        y_count = gr.Number(
            minimum=1, maximum=20, value=10, precision=0, label="Y Count"
        )
        z_count = gr.Number(
            minimum=1, maximum=20, value=10, precision=0, label="Z Count"
        )
        twist_steps = gr.Number(
            minimum=0, maximum=180, value=90, precision=0, label="Twist (degrees)"
        )
        button = gr.Button("Show Cube")
    with gr.Row():
        viewer = Rerun(streaming=True)

    button.click(
        show_cube, inputs=[x_count, y_count, z_count, twist_steps], outputs=viewer
    )

demo.queue(default_concurrency_limit=10)
demo.launch()