Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions examples/boxes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Demo scene using boxes — an exploded Rubik's cube."""

import time

import slamd
import numpy as np
from scipy.spatial.transform import Rotation


def make_transform(pos, rot=None):
T = np.eye(4, dtype=np.float32)
T[:3, 3] = pos
if rot is not None:
T[:3, :3] = rot
return T


vis = slamd.Visualizer("Boxes")
scene = vis.scene("scene")

# Rubik's cube face colors (classic scheme)
face_colors = {
(0, 0, 1): [1.0, 0.0, 0.0], # front = red
(0, 0, -1): [1.0, 0.5, 0.0], # back = orange
(0, 1, 0): [1.0, 1.0, 1.0], # top = white
(0, -1, 0): [1.0, 1.0, 0.0], # bottom = yellow
(1, 0, 0): [0.0, 0.0, 0.8], # right = blue
(-1, 0, 0): [0.0, 0.6, 0.0], # left = green
}

cube_size = 0.45
gap = 0.55
explode = 0.3 # how far apart the layers spread

rng = np.random.default_rng(42)

for x in range(-1, 2):
for y in range(-1, 2):
for z in range(-1, 2):
pos = np.array([x, y, z], dtype=np.float32) * (gap + explode)

# Pick color: outermost face determines it
color = np.array([0.12, 0.12, 0.12], dtype=np.float32) # interior = dark
for axis, face_col in face_colors.items():
ax = np.array(axis)
coord = np.array([x, y, z])
if np.dot(coord, ax) == 1:
color = np.array(face_col, dtype=np.float32)
break

# Slight random tumble for the exploded feel
tumble = (
Rotation.from_rotvec(rng.normal(0, 0.15, size=3))
.as_matrix()
.astype(np.float32)
)

# Stretch cubes along their explosion direction
direction = np.array([x, y, z], dtype=np.float32)
dist = np.linalg.norm(direction)
if dist > 0:
stretch = 1.0 + 0.4 * dist
direction /= dist
dims = np.full(3, cube_size, dtype=np.float32)
dims += np.abs(direction) * cube_size * (stretch - 1.0)
else:
dims = np.full(3, cube_size, dtype=np.float32)

scene.set_object(
f"/rubik/{x}_{y}_{z}",
slamd.geom.Box(dims, color),
)
scene.set_transform(
f"/rubik/{x}_{y}_{z}",
make_transform(pos, tumble),
)

time.sleep(10)
4 changes: 3 additions & 1 deletion examples/bunch_of_triads.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import slamd
import numpy as np

Expand Down Expand Up @@ -32,7 +34,7 @@ def main():

scene.set_object("/triad_x", slamd.geom.Triad(last_pose_mat, 10))

vis.hang_forever()
time.sleep(10)


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions examples/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import slamd
import numpy as np
import time


def make_galaxy(n: int, rng: np.random.Generator) -> tuple[np.ndarray, np.ndarray]:
Expand Down Expand Up @@ -74,6 +75,7 @@ def main():

cloud = slamd.geom.PointCloud(positions, colors, 0.08, 0.7)
scene.set_object("/galaxy", cloud)
time.sleep(10)


if __name__ == "__main__":
Expand Down
4 changes: 4 additions & 0 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import slamd
import time

if __name__ == "__main__":
vis = slamd.Visualizer("Hello world")

scene = vis.scene("scene")

scene.set_object("/origin", slamd.geom.Triad())

# let the visualizer connect and sync state
time.sleep(0.1)
2 changes: 2 additions & 0 deletions examples/large_scale_stress_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import slamd
import numpy as np
import time


def main():
Expand Down Expand Up @@ -31,6 +32,7 @@ def main():
"/point_cloud",
slamd.geom.PointCloud(points, colors, triad_size * 0.2),
)
time.sleep(10)


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion examples/lots_of_paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import slamd
import numpy as np
import time


def main():
Expand All @@ -17,7 +18,8 @@ def main():
pth = f"/box_bruh_stuff_long_ass_path_what_is_this_{i}"

scene.set_transform(pth, transform)
scene.set_object(pth, slamd.geom.Box())
scene.set_object(pth, slamd.geom.Box(np.array([1.0, 1.0, 1.0], dtype=np.float32), np.array([0.8, 0.2, 0.0], dtype=np.float32)))
time.sleep(10)


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions examples/planes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import slamd
import numpy as np
import time


def main():
Expand All @@ -24,6 +25,7 @@ def main():
scene.set_object(
f"/plane_{i}", slamd.geom.Plane(normal, pos, color, radius, alpha)
)
time.sleep(10)


if __name__ == "__main__":
Expand Down
186 changes: 186 additions & 0 deletions examples/poly_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
"""Stress test for polyline rendering at sharp angles."""

import time

import slamd
import numpy as np

vis = slamd.Visualizer("Polyline stress test")
scene = vis.scene("scene")

x_offset = 0.0

# 1. Zigzag with increasing sharpness
points = []
for i in range(20):
x = i * 0.5
y = (1.0 if i % 2 == 0 else -1.0) * min(i * 0.3, 3.0)
points.append([x + x_offset, y, 0.0])

scene.set_object(
"/zigzag",
slamd.geom.PolyLine(
np.array(points, dtype=np.float32),
0.15,
np.array([1.0, 0.4, 0.1], dtype=np.float32),
0.4,
),
)

x_offset += 12.0

# 2. 90-degree corners (staircase)
staircase = []
for i in range(8):
staircase.append([x_offset + i, 0.0, float(i)])
staircase.append([x_offset + i, 0.0, float(i + 1)])

scene.set_object(
"/staircase",
slamd.geom.PolyLine(
np.array(staircase, dtype=np.float32),
0.12,
np.array([0.4, 1.0, 0.5], dtype=np.float32),
0.4,
),
)

x_offset += 10.0

# 3. Hairpin / tight U-bend
hairpin = np.array(
[
[0, 0, 0],
[0, 2, 0],
[0.2, 2.3, 0],
[0.4, 2.3, 0],
[0.6, 2, 0],
[0.6, 0, 0],
],
dtype=np.float32,
)
hairpin[:, 0] += x_offset
scene.set_object(
"/hairpin",
slamd.geom.PolyLine(
hairpin,
0.15,
np.array([1.0, 0.85, 0.1], dtype=np.float32),
0.4,
),
)

x_offset += 3.0

# 4. Spiral tightening (decreasing radius)
t = np.linspace(0, 6 * np.pi, 200, dtype=np.float32)
r = 3.0 * np.exp(-0.1 * t)
spiral = np.column_stack([r * np.cos(t) + x_offset + 3, r * np.sin(t), np.zeros_like(t)])
scene.set_object(
"/spiral",
slamd.geom.PolyLine(
spiral.astype(np.float32),
0.08,
np.array([0.6, 0.4, 1.0], dtype=np.float32),
0.4,
),
)

x_offset += 10.0

# 5. Very short segments with sharp angles (worst case for overlap)
short_zigzag = []
for i in range(15):
x = i * 0.2
y = (0.3 if i % 2 == 0 else -0.3)
short_zigzag.append([x + x_offset, y, 0.0])

scene.set_object(
"/short_zigzag",
slamd.geom.PolyLine(
np.array(short_zigzag, dtype=np.float32),
0.1,
np.array([1.0, 0.3, 0.7], dtype=np.float32),
0.4,
),
)

x_offset += 5.0

# 6. Near-180 degree reversal
reversal = np.array(
[
[0, 0, 0],
[2, 0, 0],
[2.05, 0.1, 0],
[0, 0.2, 0],
],
dtype=np.float32,
)
reversal[:, 0] += x_offset
scene.set_object(
"/reversal",
slamd.geom.PolyLine(
reversal,
0.1,
np.array([0.2, 0.8, 0.8], dtype=np.float32),
0.4,
),
)

x_offset += 5.0

# 7. Square (four 90-degree corners in sequence)
square = np.array(
[
[0, 0, 0],
[2, 0, 0],
[2, 2, 0],
[0, 2, 0],
[0, 0, 0],
],
dtype=np.float32,
)
square[:, 0] += x_offset
scene.set_object(
"/square",
slamd.geom.PolyLine(
square,
0.12,
np.array([1.0, 1.0, 1.0], dtype=np.float32),
0.4,
),
)

x_offset += 5.0

# 8. Helix (smooth reference — should look perfect)
t = np.linspace(0, 8 * np.pi, 300, dtype=np.float32)
helix = np.column_stack([np.cos(t) + x_offset, np.sin(t), t * 0.1])
scene.set_object(
"/helix",
slamd.geom.PolyLine(
helix.astype(np.float32),
0.08,
np.array([0.2, 0.7, 1.0], dtype=np.float32),
0.4,
),
)

x_offset += 5.0

# 9. Random walk through uniformly sampled points in a box
rng = np.random.default_rng(42)
random_points = rng.uniform(-2.0, 2.0, size=(30, 3)).astype(np.float32)
random_points[:, 0] += x_offset + 2.0
scene.set_object(
"/random_box",
slamd.geom.PolyLine(
random_points,
0.08,
np.array([1.0, 0.6, 0.2], dtype=np.float32),
0.4,
),
)

time.sleep(10)
2 changes: 2 additions & 0 deletions examples/spheres.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import slamd
import numpy as np
import time


def main():
Expand Down Expand Up @@ -39,6 +40,7 @@ def main():

spheres = slamd.geom.Spheres(positions, colors, radii, 0.3)
scene.set_object("/spheres", spheres)
time.sleep(10)


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions examples/torus_planar_graphs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import slamd
import numpy as np
from itertools import combinations
import time


TAU = 2 * np.pi
Expand Down Expand Up @@ -139,6 +140,7 @@ def main():
add_kn_on_torus(scene, 5, R, r, offset=(-spacing, 0, 0), path_prefix="/k5")
add_kn_on_torus(scene, 6, R, r, offset=(0, 0, 0), path_prefix="/k6")
add_kn_on_torus(scene, 7, R, r, offset=(spacing, 0, 0), path_prefix="/k7")
time.sleep(10)


if __name__ == "__main__":
Expand Down
Loading
Loading