Skip to content

Official Codebase of 3DCodeVerse

Python

Ray engine setting: inside tests use trimesh's ray-triangle intersector unless C3D_RAY_ENGINE=embree

wang Qin6d1a94be7f5a
diff --git a/harness/codeverse3d/config.py b/harness/codeverse3d/config.py
index e546109a..b1f6896f 100644
--- a/harness/codeverse3d/config.py
+++ b/harness/codeverse3d/config.py
@@ -246,6 +246,12 @@ class Settings(BaseSettings):
default_judge: str = Field(default_factory=lambda: Backends().judge)
default_captioner: str = Field(default_factory=lambda: Backends().captioner)
default_candidates: int = Field(default=1, description="best-of-N baseline candidates (tracks read it)")
+ ray_engine: Literal["numpy", "embree"] = Field(
+ default="numpy",
+ description="ray engine of the deterministic inside tests (connectivity penetration, pose sweep): trimesh's "
+ "own ray-triangle intersector, or Embree via embreex when installed. numpy by default: embreex 4 can "
+ "segfault on large multi-part GLBs, which kills every parallel run of the process. C3D_RAY_ENGINE.",
+ )
model_timeout_s: float = Field(
default=1200.0,
diff --git a/harness/codeverse3d/spatial/joints_collide.py b/harness/codeverse3d/spatial/joints_collide.py
index 2622676f..ffcffe58 100644
--- a/harness/codeverse3d/spatial/joints_collide.py
+++ b/harness/codeverse3d/spatial/joints_collide.py
@@ -6,6 +6,7 @@ import fcl as _fcl
import numpy as np
import trimesh
from trimesh.ray import ray_util
+from trimesh.ray.ray_triangle import RayMeshIntersector
_MAX_SAMPLE_POINTS = 1500
_RAY_DIR = np.array([0.4395064455, 0.617598629942, 0.652231566745])
@@ -71,7 +72,17 @@ def inside_island(island: trimesh.Trimesh, points: np.ndarray) -> np.ndarray:
"""Deterministic parity containment (fixed ray direction, both directions must agree)."""
if len(points) == 0:
return np.zeros(0, dtype=bool)
- return np.asarray(ray_util.contains_points(island.ray, points, check_direction=_RAY_DIR), dtype=bool)
+ return np.asarray(ray_util.contains_points(_intersector(island), points, check_direction=_RAY_DIR), dtype=bool)
+
+
+def _intersector(mesh: trimesh.Trimesh) -> object:
+ """The ray engine of the parity test: trimesh's own ray-triangle intersector, or Embree (``mesh.ray`` when
+ embreex is installed) only if ``Settings.ray_engine`` asks for it: a segfault in embreex cannot be caught."""
+ from codeverse3d.config import get_settings
+
+ if get_settings().ray_engine == "embree":
+ return mesh.ray
+ return RayMeshIntersector(mesh)
class LinkBody:
diff --git a/harness/tests/spatial_tools/test_connectivity.py b/harness/tests/spatial_tools/test_connectivity.py
index 3eb788cb..afcb36bb 100644
--- a/harness/tests/spatial_tools/test_connectivity.py
+++ b/harness/tests/spatial_tools/test_connectivity.py
@@ -212,3 +212,17 @@ def test_planned_edges_are_measured_regardless_of_the_aabb_prefilter(solid_stool
assert d["planned_unresolved"] == ["Ghost"]
assert sorted(d["parts"]) == ["Leg_0", "Leg_1", "Leg_2", "Leg_3", "Seat"]
assert len(d["contacts"]) == 4 and all(c[1] == "Seat" or c[0] == "Seat" for c in d["contacts"])
+
+def test_inside_test_uses_trimesh_rays_unless_embree_is_asked_for(switch) -> None:
+ """The default ray engine is trimesh's own intersector, Embree only by ``C3D_RAY_ENGINE=embree``."""
+ from trimesh.ray.ray_triangle import RayMeshIntersector
+
+ from codeverse3d.spatial import joints_collide
+
+ box = trimesh.creation.box(extents=(1.0, 1.0, 1.0))
+ points = np.array([[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]])
+ switch("C3D_RAY_ENGINE", None)
+ assert isinstance(joints_collide._intersector(box), RayMeshIntersector)
+ assert joints_collide.inside_island(box, points).tolist() == [True, False]
+ switch("C3D_RAY_ENGINE", "embree")
+ assert joints_collide._intersector(box) is box.ray