|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from conftest import skip_if_headless |
| 4 | + |
| 5 | +from crazyflow import Sim |
| 6 | +from crazyflow.sim.visualize import draw_capsule, draw_line, draw_points |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.unit |
| 10 | +@skip_if_headless |
| 11 | +def test_draw_capsule(device: str): |
| 12 | + """Test drawing a capsule and verify it changes the rendering.""" |
| 13 | + sim = Sim(device=device) |
| 14 | + # Render without drawing |
| 15 | + sim.render(mode="rgb_array", width=120, height=90) # Warm up the renderer |
| 16 | + img_before = sim.render(mode="rgb_array", width=120, height=90) |
| 17 | + # Draw a capsule and render |
| 18 | + p1 = np.array([0.0, 0.0, 0.5]) |
| 19 | + p2 = np.array([0.5, 0.5, 1.0]) |
| 20 | + rgba = np.array([1.0, 0.0, 0.0, 1.0]) |
| 21 | + draw_capsule(sim, p1, p2, radius=0.05, rgba=rgba) |
| 22 | + img_after = sim.render(mode="rgb_array", width=120, height=90) |
| 23 | + # Verify that the image changed |
| 24 | + assert not np.array_equal(img_before, img_after), "Drawing capsule should change the rendering" |
| 25 | + sim.close() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.unit |
| 29 | +@skip_if_headless |
| 30 | +def test_draw_capsule_cylinder(device: str): |
| 31 | + """Test drawing a cylinder.""" |
| 32 | + sim = Sim(device=device) |
| 33 | + sim.render(mode="rgb_array", width=120, height=90) # Warm up the renderer |
| 34 | + img_before = sim.render(mode="rgb_array", width=120, height=90) |
| 35 | + # Draw a cylinder instead of capsule |
| 36 | + p1 = np.array([0.0, 0.0, 0.5]) |
| 37 | + p2 = np.array([0.0, 0.0, 1.0]) |
| 38 | + rgba = np.array([0.0, 1.0, 0.0, 1.0]) |
| 39 | + draw_capsule(sim, p1, p2, radius=0.05, rgba=rgba, cylinder=True) |
| 40 | + img_after = sim.render(mode="rgb_array", width=120, height=90) |
| 41 | + assert not np.array_equal(img_before, img_after), "Drawing cylinder should change the rendering" |
| 42 | + sim.close() |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.unit |
| 46 | +@skip_if_headless |
| 47 | +def test_draw_line(device: str): |
| 48 | + """Test drawing a line and verify it changes the rendering.""" |
| 49 | + sim = Sim(device=device) |
| 50 | + sim.render(mode="rgb_array", width=120, height=90) # Warm up the renderer |
| 51 | + img_before = sim.render(mode="rgb_array", width=120, height=90) |
| 52 | + # Draw a line with multiple points |
| 53 | + points = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2.0]]) |
| 54 | + rgba = np.array([0.0, 0.0, 1.0, 1.0]) |
| 55 | + draw_line(sim, points, rgba=rgba, start_size=5.0, end_size=2.0) |
| 56 | + img_after = sim.render(mode="rgb_array", width=120, height=90) |
| 57 | + assert not np.array_equal(img_before, img_after), "Drawing line should change the rendering" |
| 58 | + sim.close() |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.unit |
| 62 | +@skip_if_headless |
| 63 | +def test_draw_points(device: str): |
| 64 | + """Test drawing points and verify it changes the rendering.""" |
| 65 | + sim = Sim(device=device) |
| 66 | + sim.render(mode="rgb_array", width=120, height=90) # Warm up the renderer |
| 67 | + img_before = sim.render(mode="rgb_array", width=120, height=90) |
| 68 | + # Draw multiple points |
| 69 | + points = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.5], [0.0, 0.0, 1.0]]) |
| 70 | + rgba = np.array([1.0, 1.0, 0.0, 1.0]) |
| 71 | + draw_points(sim, points, rgba=rgba, size=0.02) |
| 72 | + img_after = sim.render(mode="rgb_array", width=120, height=90) |
| 73 | + assert not np.array_equal(img_before, img_after), "Drawing points should change the rendering" |
| 74 | + sim.close() |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.unit |
| 78 | +@skip_if_headless |
| 79 | +def test_draw_combined(device: str): |
| 80 | + """Test drawing multiple visualization elements together.""" |
| 81 | + sim = Sim(device=device) |
| 82 | + p1 = np.array([0.0, 0.0, 0.5]) |
| 83 | + p2 = np.array([0.2, 0.0, 0.8]) |
| 84 | + draw_capsule(sim, p1, p2, radius=0.03, rgba=np.array([1.0, 0.0, 0.0, 1.0])) |
| 85 | + line_points = np.array([[0.3, 0.0, 0.5], [0.3, 0.2, 0.7], [0.5, 0.2, 0.9]]) |
| 86 | + draw_line(sim, line_points, rgba=np.array([0.0, 1.0, 0.0, 1.0])) |
| 87 | + points = np.array([[0.6, 0.0, 0.6], [0.7, 0.1, 0.7], [0.8, 0.0, 0.8]]) |
| 88 | + draw_points(sim, points, rgba=np.array([0.0, 0.0, 1.0, 1.0]), size=0.025) |
| 89 | + sim.render(mode="rgb_array", width=120, height=90) |
| 90 | + sim.close() |
0 commit comments