Official Codebase of 3DCodeVerse
Python
Complexity: drop the unread descriptors and stored components; one count_materials
gaoypeng35858d27d36b
diff --git a/harness/codeverse3d/spatial/complexity.py b/harness/codeverse3d/spatial/complexity.pyindex fbf64b62..b1029462 100644--- a/harness/codeverse3d/spatial/complexity.py+++ b/harness/codeverse3d/spatial/complexity.py@@ -92,10 +92,7 @@ class ComplexityVector(BaseModel):hollowness: float = 0.0index: float = 0.0band: str = "trivial"- components: dict[str, float] = Field(- default_factory=dict, description="axis -> normalised 0-1 contribution input"- )- extra: dict[str, Any] = Field(default_factory=dict, description="descriptors that do not enter the index")+ extra: dict[str, Any] = Field(default_factory=dict, description="why the vector is empty (no geometry / concatenate failure)")# --------------------------------------------------------------------- silhouette@@ -246,17 +243,6 @@ def _graph_depth(scene: trimesh.Scene | None, tops: list[str]) -> int:return best-def _materials(scene: trimesh.Scene | None, fallback: int) -> int:- if scene is None:- return fallback- seen = {- id(g.visual.material)- for g in scene.geometry.values()- if hasattr(g, "visual") and hasattr(g.visual, "material")- }- return len(seen) or fallback--# --------------------------------------------------------------------- front doorsdef complexity_of_parts(parts: OrderedDict[str, trimesh.Trimesh | None] | dict[str, trimesh.Trimesh | None],@@ -273,32 +259,24 @@ def complexity_of_parts(except Exception as e: # pragma: no cover - trimesh raises many typesreturn ComplexityVector(part_count=len(solid), extra={"error": f"{type(e).__name__}: {e}"})- sil, fill, mirror = _silhouette_stats(whole)- fdens, fedges = _feature_density(whole)- hollow, watertight = _hollowness(solid)- groups, repeated = _symmetry_groups(solid)- from codeverse3d.spatial.measure import effective_top_nodes+ sil, _, _ = _silhouette_stats(whole)+ fdens, _ = _feature_density(whole)+ hollow, _ = _hollowness(solid)+ groups, _ = _symmetry_groups(solid)+ from codeverse3d.spatial.measure import count_materials, effective_top_nodestops = effective_top_nodes(scene) if scene is not None else []vec = ComplexityVector(part_count=len(solid),assembly_depth=_graph_depth(scene, [str(t) for t in tops]),tri_count=int(len(whole.faces)),- materials=_materials(scene, materials),+ materials=(count_materials(scene) if scene is not None else 0) or materials,silhouette=round(sil, 3),feature_density=round(fdens, 1),symmetry_groups=groups,hollowness=round(hollow, 3),- extra={- "silhouette_fill": round(fill, 3),- "mirror_symmetry": round(mirror, 3),- "watertight_fraction": round(watertight, 3),- "repeated_parts": repeated,- "small_sharp_edges": fedges,- },)- vec.components = {axis: round(_norm(axis, getattr(vec, axis)), 4) for axis in COMPLEXITY_WEIGHTS}- vec.index = round(sum(COMPLEXITY_WEIGHTS[a] * v for a, v in vec.components.items()), 4)+ vec.index = round(sum(w * round(_norm(a, getattr(vec, a)), 4) for a, w in COMPLEXITY_WEIGHTS.items()), 4)vec.band = band_of(vec.index)return vecdiff --git a/harness/codeverse3d/spatial/measure.py b/harness/codeverse3d/spatial/measure.pyindex 77675ba9..20cc32c5 100644--- a/harness/codeverse3d/spatial/measure.py+++ b/harness/codeverse3d/spatial/measure.py@@ -140,6 +140,10 @@ def _node_has_geometry(scene: trimesh.Scene, node: str) -> bool:return False+def count_materials(scene: trimesh.Scene) -> int:+ return len({id(g.visual.material) for g in scene.geometry.values() if hasattr(g, "visual") and hasattr(g.visual, "material")})++def effective_top_nodes(scene: trimesh.Scene) -> list[str]:"""Direct children of the scene root, descending through single wrapper nodes that carry nogeometry themselves (a three.js root Group, a URDF ``<robot>`` node) so that parts are the top nodes."""@@ -319,8 +323,7 @@ def _measure(scene: trimesh.Scene, parts: OrderedDict[str, trimesh.Trimesh | Nonrows.append(_measure_part(name, mesh))whole = merged_mesh(parts)- materials = len({id(g.visual.material) for g in scene.geometry.values()- if hasattr(g, "visual") and hasattr(g.visual, "material")})+ materials = count_materials(scene)if whole is None or len(whole.faces) == 0:findings.append("model has no triangles at all")return Measurement(diff --git a/harness/tests/spatial_tools/test_complexity.py b/harness/tests/spatial_tools/test_complexity.pyindex 5cfba6eb..d9ff1f04 100644--- a/harness/tests/spatial_tools/test_complexity.py+++ b/harness/tests/spatial_tools/test_complexity.py@@ -9,6 +9,7 @@ import pytestfrom codeverse3d.spatial.complexity import (COMPLEXITY_WEIGHTS,+ _norm,band_of,complexity_of_glb,)@@ -16,14 +17,12 @@ from codeverse3d.spatial.complexity import (def test_vector_shape_and_weights(stool_glb: Path) -> None:vec = complexity_of_glb(stool_glb)- assert set(COMPLEXITY_WEIGHTS) == set(vec.components)assert sum(COMPLEXITY_WEIGHTS.values()) == pytest.approx(1.0)assert vec.part_count == 5 and vec.tri_count > 0assert 0.0 < vec.index < 1.0- assert all(0.0 <= v <= 1.0 for v in vec.components.values())assert vec.band == band_of(vec.index)assert vec.index == pytest.approx(- sum(COMPLEXITY_WEIGHTS[a] * v for a, v in vec.components.items()), abs=5e-4+ sum(w * _norm(a, getattr(vec, a)) for a, w in COMPLEXITY_WEIGHTS.items()), abs=5e-4)labels = [band_of(x) for x in (0.0, 0.3, 0.45, 0.6, 0.9, 1.0)]assert labels == ["trivial", "simple", "moderate", "complex", "intricate", "intricate"]