|
6 | 6 |
|
7 | 7 | from fury import actor, window |
8 | 8 |
|
| 9 | +# --- Shared real inputs for visibility/snapshot tests ----------------------- |
| 10 | + |
| 11 | +CENTERS = np.array([[0.0, 0.0, 0.0]], dtype=np.float32) |
| 12 | +LINES = [np.array([[0, 0, 0], [1, 1, 1], [2, 0, 0]], dtype=np.float32)] |
| 13 | + |
| 14 | + |
| 15 | +def gradient_image(n=64): |
| 16 | + """A non-uniform 2D image so the rendered frame is never flat.""" |
| 17 | + ramp = np.linspace(0.0, 1.0, n, dtype=np.float32) |
| 18 | + return np.outer(ramp, ramp) |
| 19 | + |
| 20 | + |
| 21 | +def scalar_volume(n=32): |
| 22 | + """A non-uniform 3D scalar volume (deterministic).""" |
| 23 | + x, y, z = np.mgrid[0:n, 0:n, 0:n] |
| 24 | + return ((x + y + z) % 17).astype(np.float32) |
| 25 | + |
| 26 | + |
| 27 | +def roi_volume(n=20): |
| 28 | + """A binary volume with a solid central cube (for contour actors).""" |
| 29 | + data = np.zeros((n, n, n), dtype=np.float32) |
| 30 | + data[5:15, 5:15, 5:15] = 1.0 |
| 31 | + return data |
| 32 | + |
| 33 | + |
| 34 | +def label_volume(n=20): |
| 35 | + """A labeled volume with one nonzero region.""" |
| 36 | + data = np.zeros((n, n, n), dtype=int) |
| 37 | + data[5:15, 5:15, 5:15] = 1 |
| 38 | + return data |
| 39 | + |
| 40 | + |
| 41 | +def peak_dirs(n=8): |
| 42 | + """A (X, Y, Z, 3, 3) field of unit peak directions.""" |
| 43 | + dirs = np.zeros((n, n, n, 3, 3), dtype=np.float32) |
| 44 | + dirs[..., 0, :] = (1.0, 0.0, 0.0) |
| 45 | + dirs[..., 1, :] = (0.0, 1.0, 0.0) |
| 46 | + dirs[..., 2, :] = (0.0, 0.0, 1.0) |
| 47 | + return dirs |
| 48 | + |
| 49 | + |
| 50 | +def vector_field_data(n=8): |
| 51 | + """A (X, Y, Z, 3) field of unit vectors.""" |
| 52 | + field = np.zeros((n, n, n, 3), dtype=np.float32) |
| 53 | + field[...] = (1.0, 0.0, 0.0) |
| 54 | + return field |
| 55 | + |
| 56 | + |
| 57 | +def sph_coeffs(n=4, ncoeff=15): |
| 58 | + """SH coefficients with only the l=0 term -> isotropic visible glyphs.""" |
| 59 | + coeffs = np.zeros((n, n, n, ncoeff), dtype=np.float32) |
| 60 | + coeffs[..., 0] = 1.0 |
| 61 | + return coeffs |
| 62 | + |
| 63 | + |
| 64 | +def triangle_mesh(): |
| 65 | + """A single-triangle (vertices, faces) pair for surface actors.""" |
| 66 | + verts = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0]], dtype=np.float32) |
| 67 | + faces = np.array([[0, 1, 2]], dtype=np.int32) |
| 68 | + return verts, faces |
| 69 | + |
| 70 | + |
| 71 | +# The full set of renderable actors keyed by id. Each value is a factory that |
| 72 | +# builds a fresh actor (with real inputs) on every call. |
| 73 | +ACTOR_FACTORIES = { |
| 74 | + # Planar / polyhedron / curved mesh primitives (centers only). |
| 75 | + "square": lambda: actor.square(CENTERS), |
| 76 | + "triangle": lambda: actor.triangle(CENTERS), |
| 77 | + "star": lambda: actor.star(CENTERS), |
| 78 | + "disk": lambda: actor.disk(CENTERS), |
| 79 | + "ring": lambda: actor.ring(CENTERS), |
| 80 | + "box": lambda: actor.box(CENTERS), |
| 81 | + "tetrahedron": lambda: actor.tetrahedron(CENTERS), |
| 82 | + "icosahedron": lambda: actor.icosahedron(CENTERS), |
| 83 | + "triangularprism": lambda: actor.triangularprism(CENTERS), |
| 84 | + "pentagonalprism": lambda: actor.pentagonalprism(CENTERS), |
| 85 | + "octagonalprism": lambda: actor.octagonalprism(CENTERS), |
| 86 | + "rhombicuboctahedron": lambda: actor.rhombicuboctahedron(CENTERS), |
| 87 | + "frustum": lambda: actor.frustum(CENTERS), |
| 88 | + "superquadric": lambda: actor.superquadric(CENTERS), |
| 89 | + "cylinder": lambda: actor.cylinder(CENTERS), |
| 90 | + "cone": lambda: actor.cone(CENTERS), |
| 91 | + "arrow": lambda: actor.arrow(CENTERS), |
| 92 | + "sphere": lambda: actor.sphere(CENTERS), |
| 93 | + "ellipsoid": lambda: actor.ellipsoid(CENTERS), |
| 94 | + # Points / billboards. |
| 95 | + "point": lambda: actor.point(CENTERS), |
| 96 | + "marker": lambda: actor.marker(CENTERS), |
| 97 | + "billboard": lambda: actor.billboard(CENTERS), |
| 98 | + "billboard_sphere": lambda: actor.billboard_sphere(CENTERS), |
| 99 | + # Axes (no required args). |
| 100 | + "axes": lambda: actor.axes(), |
| 101 | + # Text / image. |
| 102 | + "text": lambda: actor.text("FURY"), |
| 103 | + "image": lambda: actor.image(gradient_image()), |
| 104 | + # Line / streamline family. |
| 105 | + "line": lambda: actor.line(LINES), |
| 106 | + "streamlines": lambda: actor.streamlines(LINES), |
| 107 | + "streamtube": lambda: actor.streamtube(LINES), |
| 108 | + "line_projection": lambda: actor.line_projection(LINES), |
| 109 | + # Surface. |
| 110 | + "surface": lambda: actor.surface(*triangle_mesh()), |
| 111 | + # Volume / field / glyph slicers (return single actors or groups; both are |
| 112 | + # hidden by setting ``.visible`` on the returned object). |
| 113 | + "data_slicer": lambda: actor.data_slicer(scalar_volume()), |
| 114 | + "volume_slicer": lambda: actor.volume_slicer(scalar_volume()), |
| 115 | + "peaks_slicer": lambda: actor.peaks_slicer(peak_dirs()), |
| 116 | + "vector_field": lambda: actor.vector_field(vector_field_data()), |
| 117 | + "vector_field_slicer": lambda: actor.vector_field_slicer(vector_field_data()), |
| 118 | + "sph_glyph": lambda: actor.sph_glyph(sph_coeffs()), |
| 119 | + # Contours (need a solid block to produce geometry). |
| 120 | + "contour_from_volume": lambda: actor.contour_from_volume(roi_volume()), |
| 121 | + "contour_from_roi": lambda: actor.contour_from_roi(roi_volume()), |
| 122 | + "contour_from_label": lambda: actor.contour_from_label(label_volume()), |
| 123 | +} |
| 124 | + |
9 | 125 |
|
10 | 126 | def random_png(width, height): |
11 | 127 | """ |
@@ -55,43 +171,42 @@ def validate_actors(actor_type="actor_name", prim_count=1, **kwargs): |
55 | 171 | if actor_type == "line": |
56 | 172 | return |
57 | 173 |
|
58 | | - fname = f"{actor_type}_test.png" |
59 | | - window.snapshot(scene=scene, fname=fname) |
60 | | - |
61 | | - img = Image.open(fname) |
62 | | - img_array = np.array(img) |
63 | | - |
64 | | - mean_r, mean_g, mean_b, _mean_a = np.mean( |
65 | | - img_array.reshape(-1, img_array.shape[2]), axis=0 |
66 | | - ) |
| 174 | + # Default material: the actor renders and red dominates the image. |
| 175 | + arr = window.snapshot(scene=scene, fname=None, return_array=True) |
| 176 | + report = window.analyze_snapshot(arr, find_objects=True) |
| 177 | + assert report.objects >= 1 |
67 | 178 |
|
| 179 | + mean_r, mean_g, mean_b, _mean_a = np.mean(arr.reshape(-1, arr.shape[2]), axis=0) |
68 | 180 | assert mean_r > mean_b and mean_r > mean_g |
| 181 | + assert 0 < mean_r < 255 |
69 | 182 |
|
70 | | - middle_pixel = img_array[img_array.shape[0] // 2, img_array.shape[1] // 2] |
71 | | - r, g, b, a = middle_pixel |
72 | | - assert r > g and r > b |
73 | | - assert g == b |
| 183 | + # Hidden: nothing renders. |
| 184 | + get_actor.visible = False |
| 185 | + report = window.analyze_snapshot( |
| 186 | + window.snapshot(scene=scene, fname=None, return_array=True), find_objects=True |
| 187 | + ) |
| 188 | + assert report.objects == 0 |
74 | 189 | scene.remove(get_actor) |
75 | 190 |
|
| 191 | + # Basic (flat) material: exact red is present, then absent once hidden. |
76 | 192 | typ_actor_1 = getattr(actor, actor_type) |
77 | 193 | get_actor_1 = typ_actor_1(**{**kwargs, "material": "basic"}) |
78 | 194 | scene.add(get_actor_1) |
79 | | - fname_1 = f"{actor_type}_test_1.png" |
80 | | - window.snapshot(scene=scene, fname=fname_1) |
81 | | - img = Image.open(fname_1) |
82 | | - img_array = np.array(img) |
83 | 195 |
|
84 | | - mean_r, mean_g, mean_b, _mean_a = np.mean( |
85 | | - img_array.reshape(-1, img_array.shape[2]), axis=0 |
| 196 | + report = window.analyze_snapshot( |
| 197 | + window.snapshot(scene=scene, fname=None, return_array=True), |
| 198 | + colors=(255, 0, 0), |
| 199 | + find_objects=True, |
86 | 200 | ) |
87 | | - |
88 | | - assert mean_r > mean_b and mean_r > mean_g |
89 | | - assert 0 < mean_r < 255 |
90 | | - assert mean_g == 0 and mean_b == 0 |
91 | | - |
92 | | - middle_pixel = img_array[img_array.shape[0] // 2, img_array.shape[1] // 2] |
93 | | - r, g, b, a = middle_pixel |
94 | | - assert r > g and r > b |
95 | | - assert g == 0 and b == 0 |
96 | | - assert r == 255 |
| 201 | + assert report.objects >= 1 |
| 202 | + assert report.colors_found == [True] |
| 203 | + |
| 204 | + get_actor_1.visible = False |
| 205 | + report = window.analyze_snapshot( |
| 206 | + window.snapshot(scene=scene, fname=None, return_array=True), |
| 207 | + colors=(255, 0, 0), |
| 208 | + find_objects=True, |
| 209 | + ) |
| 210 | + assert report.objects == 0 |
| 211 | + assert report.colors_found == [False] |
97 | 212 | scene.remove(get_actor_1) |
0 commit comments