Skip to content

Commit 471b505

Browse files
committed
extra unit tests and fixed formatting in textures
1 parent 5cf6b25 commit 471b505

File tree

2 files changed

+93
-22
lines changed

2 files changed

+93
-22
lines changed

genesis/options/textures.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,27 @@ def __init__(self, **data):
140140

141141
elif self.image_array is not None:
142142
if not isinstance(self.image_array, np.ndarray):
143-
gs.raise_exception("`image_array` needs to be an numpy array.")
144-
if self.image_array.dtype != np.uint8:
145-
if self.image_array.dtype in (np.float32, np.float64):
146-
if self.image_array.max() <= 1.0:
147-
self.image_array = (self.image_array * 255.0).round()
148-
self.image_array = np.clip(self.image_array, 0.0, 255.0).astype(np.uint8)
149-
elif self.image_array.dtype == np.bool_:
150-
self.image_array = self.image_array.astype(np.uint8) * 255
151-
152-
elif np.issubdtype(self.image_array.dtype, np.integer):
153-
self.image_array = np.clip(self.image_array, 0, 255).astype(np.uint8)
154-
else:
155-
gs.raise_exception(
156-
f"Unsupported image dtype {self.image_array.dtype}. Only uint8 or float32/64 are supported."
157-
)
143+
gs.raise_exception("`image_array` needs to be a numpy array.")
144+
if self.image_array.dtype == np.uint8:
145+
pass
146+
elif np.issubdtype(self.image_array.dtype, np.floating):
147+
if self.image_array.max() <= 1.0:
148+
self.image_array = (self.image_array * 255.0).round()
149+
self.image_array = np.clip(self.image_array, 0.0, 255.0).astype(np.uint8)
150+
elif self.image_array.dtype == np.bool_:
151+
self.image_array = self.image_array.astype(np.uint8) * 255
152+
elif np.issubdtype(self.image_array.dtype, np.integer):
153+
self.image_array = np.clip(self.image_array, 0, 255).astype(np.uint8)
154+
else:
155+
gs.raise_exception(
156+
f"Unsupported image dtype {self.image_array.dtype}. "
157+
"Only uint8, integer, floating-point, or bool types are supported."
158+
)
159+
158160
if self.image_array.ndim == 2:
159161
self.image_array = np.stack([self.image_array] * 3, axis=-1)
160-
161162
elif self.image_array.shape[2] == 1:
162163
self.image_array = np.repeat(self.image_array, 3, axis=2)
163-
164164
elif self.image_array.shape[2] == 2:
165165
L = self.image_array[..., 0]
166166
A = self.image_array[..., 1]

tests/test_mesh.py

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,9 @@ def test_usd_parse(usd_filename):
296296

297297

298298
@pytest.mark.required
299-
def test_urdf_with_existing_glb():
299+
def test_urdf_with_existing_glb(tmp_path, show_viewer):
300300
assets = Path(gs.utils.get_assets_dir())
301301
glb_path = assets / "usd" / "sneaker_airforce.glb"
302-
303-
tmp_path = Path(__file__).parent
304302
urdf_path = tmp_path / "model.urdf"
305303
urdf_path.write_text(
306304
f"""<robot name="shoe">
@@ -309,8 +307,81 @@ def test_urdf_with_existing_glb():
309307
<geometry><mesh filename="{glb_path}"/></geometry>
310308
</visual>
311309
</link>
312-
</robot>"""
310+
</robot>
311+
"""
312+
)
313+
scene = gs.Scene(
314+
show_viewer=show_viewer,
315+
show_fps=False,
316+
)
317+
scene.build()
318+
scene.step()
319+
320+
321+
@pytest.mark.required
322+
def test_urdf_with_float32_grayscale_glb(tmp_path, show_viewer):
323+
glb_path = tmp_path / "gray.glb"
324+
img = np.random.rand(16, 16).astype(np.float32)
325+
vertices = np.array(
326+
[[-0.5, -0.5, 0.0], [0.5, -0.5, 0.0], [0.5, 0.5, 0.0], [-0.5, 0.5, 0.0]],
327+
dtype=np.float32,
328+
)
329+
faces = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.uint32)
330+
mesh = trimesh.Trimesh(vertices, faces, process=False)
331+
mesh.visual = trimesh.visual.texture.TextureVisuals(
332+
uv=np.array([[0, 0], [1, 0], [1, 1], [0, 1]], dtype=np.float32),
333+
material=trimesh.visual.material.SimpleMaterial(image=img),
334+
)
335+
trimesh.Scene([mesh]).export(glb_path)
336+
urdf_path = tmp_path / "model_gray.urdf"
337+
urdf_path.write_text(
338+
f"""<robot name="gray">
339+
<link name="base">
340+
<visual>
341+
<geometry><mesh filename="{glb_path}"/></geometry>
342+
</visual>
343+
</link>
344+
</robot>
345+
"""
346+
)
347+
scene = gs.Scene(
348+
show_viewer=show_viewer,
349+
show_fps=False,
350+
)
351+
scene.build()
352+
scene.step()
353+
354+
355+
@pytest.mark.required
356+
def test_urdf_with_float64_two_channel_glb(tmp_path, show_viewer):
357+
glb_path = tmp_path / "rg.glb"
358+
img = np.random.rand(16, 16, 2).astype(np.float64)
359+
vertices = np.array(
360+
[[-0.5, -0.5, 0.0], [0.5, -0.5, 0.0], [0.5, 0.5, 0.0], [-0.5, 0.5, 0.0]],
361+
dtype=np.float32,
362+
)
363+
faces = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.uint32)
364+
mesh = trimesh.Trimesh(vertices, faces, process=False)
365+
mesh.visual = trimesh.visual.texture.TextureVisuals(
366+
uv=np.array([[0, 0], [1, 0], [1, 1], [0, 1]], dtype=np.float32),
367+
material=trimesh.visual.material.SimpleMaterial(image=img),
368+
)
369+
trimesh.Scene([mesh]).export(glb_path)
370+
371+
urdf_path = tmp_path / "model_rg.urdf"
372+
urdf_path.write_text(
373+
f"""<robot name="rg">
374+
<link name="base">
375+
<visual>
376+
<geometry><mesh filename="{glb_path}"/></geometry>
377+
</visual>
378+
</link>
379+
</robot>
380+
"""
381+
)
382+
scene = gs.Scene(
383+
show_viewer=show_viewer,
384+
show_fps=False,
313385
)
314-
scene = gs.Scene(show_viewer=False)
315386
scene.build()
316387
scene.step()

0 commit comments

Comments
 (0)