Skip to content

Open-source Revit MCP server: let Claude and other AI assistants read and edit BIM models, with human approval on every change.

Scaffold 2, Task 2.2: Create detailed geometry clash detection

sammohammad5781b28de792c78
diff --git a/packages/model-checker/src/model_checker/clash_detection.py b/packages/model-checker/src/model_checker/clash_detection.py
index a9b2d91..56bb252 100644
--- a/packages/model-checker/src/model_checker/clash_detection.py
+++ b/packages/model-checker/src/model_checker/clash_detection.py
@@ -128,6 +128,101 @@ class ClashResult:
}
+class PreciseGeometry:
+ """Represents precise geometry for detailed clash detection."""
+
+ def __init__(self, geometry_data: dict[str, Any]) -> None:
+ """Initialize precise geometry.
+
+ Args:
+ geometry_data: Dictionary containing geometry information
+ """
+ self.geometry_data = geometry_data
+ self.vertices: list[tuple[float, float, float]] = []
+ self.faces: list[list[int]] = []
+ self._parse_geometry()
+
+ def _parse_geometry(self) -> None:
+ """Parse geometry data into vertices and faces."""
+ if "vertices" in self.geometry_data:
+ self.vertices = [
+ (v["x"], v["y"], v["z"]) for v in self.geometry_data["vertices"]
+ ]
+ if "faces" in self.geometry_data:
+ self.faces = self.geometry_data["faces"]
+
+ def get_bounding_box(self) -> BoundingBox:
+ """Get bounding box from precise geometry.
+
+ Returns:
+ BoundingBox containing all vertices
+ """
+ if not self.vertices:
+ return BoundingBox(0, 0, 0, 0, 0, 0)
+
+ xs = [v[0] for v in self.vertices]
+ ys = [v[1] for v in self.vertices]
+ zs = [v[2] for v in self.vertices]
+
+ return BoundingBox(
+ min_x=min(xs),
+ min_y=min(ys),
+ min_z=min(zs),
+ max_x=max(xs),
+ max_y=max(ys),
+ max_z=max(zs),
+ )
+
+ def intersects_with(self, other: PreciseGeometry) -> bool:
+ """Check if this geometry intersects with another.
+
+ Uses simplified geometric intersection test.
+ First checks bounding boxes, then performs vertex-in-bbox tests.
+
+ Args:
+ other: Another precise geometry
+
+ Returns:
+ True if geometries intersect
+ """
+ # Quick rejection using bounding boxes
+ bbox1 = self.get_bounding_box()
+ bbox2 = other.get_bounding_box()
+
+ if not bbox1.intersects(bbox2):
+ return False
+
+ # Check if any vertex from one geometry is inside the other's bbox
+ for vertex in self.vertices:
+ if self._point_in_bbox(vertex, bbox2):
+ return True
+
+ for vertex in other.vertices:
+ if self._point_in_bbox(vertex, bbox1):
+ return True
+
+ return False
+
+ def _point_in_bbox(
+ self, point: tuple[float, float, float], bbox: BoundingBox
+ ) -> bool:
+ """Check if a point is inside a bounding box.
+
+ Args:
+ point: 3D point coordinates
+ bbox: Bounding box to check
+
+ Returns:
+ True if point is inside bbox
+ """
+ x, y, z = point
+ return (
+ bbox.min_x < x < bbox.max_x
+ and bbox.min_y < y < bbox.max_y
+ and bbox.min_z < z < bbox.max_z
+ )
+
+
class ClashDetector:
"""Detector for geometric clashes between elements."""
@@ -275,3 +370,93 @@ class ClashDetector:
def clear_clashes(self) -> None:
"""Clear all stored clashes."""
self.clashes = []
+
+ def check_precise_clash(
+ self,
+ element_id_1: str | int,
+ geometry_1: PreciseGeometry,
+ element_id_2: str | int,
+ geometry_2: PreciseGeometry,
+ ) -> ClashResult | None:
+ """Check for precise geometric clash between two elements.
+
+ Uses detailed geometry analysis beyond bounding box intersection.
+
+ Args:
+ element_id_1: ID of first element
+ geometry_1: Precise geometry of first element
+ element_id_2: ID of second element
+ geometry_2: Precise geometry of second element
+
+ Returns:
+ ClashResult if clash detected, None otherwise
+ """
+ if geometry_1.intersects_with(geometry_2):
+ # Estimate intersection volume from bounding box overlap
+ bbox_1 = geometry_1.get_bounding_box()
+ bbox_2 = geometry_2.get_bounding_box()
+ intersection_volume = bbox_1.get_intersection_volume(bbox_2)
+
+ center_1 = bbox_1.get_center()
+ center_2 = bbox_2.get_center()
+
+ location = {
+ "x": (center_1[0] + center_2[0]) / 2,
+ "y": (center_1[1] + center_2[1]) / 2,
+ "z": (center_1[2] + center_2[2]) / 2,
+ }
+
+ severity = self._calculate_severity(intersection_volume)
+
+ clash = ClashResult(
+ element_id_1=element_id_1,
+ element_id_2=element_id_2,
+ clash_type="precise_geometry",
+ intersection_volume=intersection_volume,
+ location=location,
+ severity=severity,
+ )
+
+ self.clashes.append(clash)
+ return clash
+
+ return None
+
+ def check_precise_multiple_elements(
+ self,
+ elements: list[dict[str, Any]],
+ ) -> list[ClashResult]:
+ """Check for precise clashes between multiple elements.
+
+ Args:
+ elements: List of element dicts with 'id' and 'geometry' keys
+
+ Returns:
+ List of detected clashes
+ """
+ clashes = []
+ n = len(elements)
+
+ for i in range(n):
+ elem_1 = elements[i]
+ geom_1 = elem_1["geometry"]
+ if isinstance(geom_1, dict):
+ geom_1 = PreciseGeometry(geom_1)
+
+ for j in range(i + 1, n):
+ elem_2 = elements[j]
+ geom_2 = elem_2["geometry"]
+ if isinstance(geom_2, dict):
+ geom_2 = PreciseGeometry(geom_2)
+
+ clash = self.check_precise_clash(
+ elem_1["id"],
+ geom_1,
+ elem_2["id"],
+ geom_2,
+ )
+
+ if clash:
+ clashes.append(clash)
+
+ return clashes
diff --git a/packages/model-checker/tests/test_precise_clash.py b/packages/model-checker/tests/test_precise_clash.py
new file mode 100644
index 0000000..84595e5
--- /dev/null
+++ b/packages/model-checker/tests/test_precise_clash.py
@@ -0,0 +1,332 @@
+"""Test precise geometry clash detection."""
+
+import pytest
+
+from model_checker.clash_detection import ClashDetector, PreciseGeometry
+
+
+def test_create_precise_geometry():
+ """Test creating precise geometry from data."""
+ geometry_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 0.0},
+ {"x": 0.0, "y": 1000.0, "z": 0.0},
+ ],
+ "faces": [[0, 1, 2, 3]],
+ }
+
+ geom = PreciseGeometry(geometry_data)
+
+ assert len(geom.vertices) == 4
+ assert len(geom.faces) == 1
+ assert geom.vertices[0] == (0.0, 0.0, 0.0)
+ assert geom.vertices[2] == (1000.0, 1000.0, 0.0)
+
+
+def test_precise_geometry_bounding_box():
+ """Test getting bounding box from precise geometry."""
+ geometry_data = {
+ "vertices": [
+ {"x": 100.0, "y": 200.0, "z": 300.0},
+ {"x": 500.0, "y": 600.0, "z": 700.0},
+ {"x": 150.0, "y": 250.0, "z": 350.0},
+ ],
+ }
+
+ geom = PreciseGeometry(geometry_data)
+ bbox = geom.get_bounding_box()
+
+ assert bbox.min_x == 100.0
+ assert bbox.min_y == 200.0
+ assert bbox.min_z == 300.0
+ assert bbox.max_x == 500.0
+ assert bbox.max_y == 600.0
+ assert bbox.max_z == 700.0
+
+
+def test_precise_geometry_empty():
+ """Test precise geometry with no vertices."""
+ geometry_data = {}
+ geom = PreciseGeometry(geometry_data)
+
+ bbox = geom.get_bounding_box()
+ assert bbox.min_x == 0
+ assert bbox.max_x == 0
+
+
+def test_point_in_bbox():
+ """Test point-in-bounding-box check."""
+ geometry_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ ],
+ }
+
+ geom = PreciseGeometry(geometry_data)
+ bbox = geom.get_bounding_box()
+
+ # Point inside
+ assert geom._point_in_bbox((500.0, 500.0, 500.0), bbox) is True
+
+ # Point outside
+ assert geom._point_in_bbox((1500.0, 1500.0, 1500.0), bbox) is False
+
+ # Point on boundary (should be False with strict < check)
+ assert geom._point_in_bbox((0.0, 0.0, 0.0), bbox) is False
+
+
+def test_precise_geometry_intersects():
+ """Test precise geometry intersection detection."""
+ # Geometry 1: cube from 0,0,0 to 1000,1000,1000
+ geom1_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 0.0},
+ {"x": 0.0, "y": 1000.0, "z": 0.0},
+ {"x": 0.0, "y": 0.0, "z": 1000.0},
+ {"x": 1000.0, "y": 0.0, "z": 1000.0},
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ {"x": 0.0, "y": 1000.0, "z": 1000.0},
+ ],
+ }
+
+ # Geometry 2: cube from 500,500,500 to 1500,1500,1500 (overlapping)
+ geom2_data = {
+ "vertices": [
+ {"x": 500.0, "y": 500.0, "z": 500.0},
+ {"x": 1500.0, "y": 500.0, "z": 500.0},
+ {"x": 1500.0, "y": 1500.0, "z": 500.0},
+ {"x": 500.0, "y": 1500.0, "z": 500.0},
+ {"x": 500.0, "y": 500.0, "z": 1500.0},
+ {"x": 1500.0, "y": 500.0, "z": 1500.0},
+ {"x": 1500.0, "y": 1500.0, "z": 1500.0},
+ {"x": 500.0, "y": 1500.0, "z": 1500.0},
+ ],
+ }
+
+ geom1 = PreciseGeometry(geom1_data)
+ geom2 = PreciseGeometry(geom2_data)
+
+ assert geom1.intersects_with(geom2) is True
+ assert geom2.intersects_with(geom1) is True
+
+
+def test_precise_geometry_no_intersection():
+ """Test precise geometry with no intersection."""
+ # Geometry 1: cube from 0,0,0 to 1000,1000,1000
+ geom1_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ ],
+ }
+
+ # Geometry 2: cube from 2000,2000,2000 to 3000,3000,3000 (separated)
+ geom2_data = {
+ "vertices": [
+ {"x": 2000.0, "y": 2000.0, "z": 2000.0},
+ {"x": 3000.0, "y": 3000.0, "z": 3000.0},
+ ],
+ }
+
+ geom1 = PreciseGeometry(geom1_data)
+ geom2 = PreciseGeometry(geom2_data)
+
+ assert geom1.intersects_with(geom2) is False
+ assert geom2.intersects_with(geom1) is False
+
+
+def test_check_precise_clash():
+ """Test precise clash detection."""
+ detector = ClashDetector()
+
+ # Overlapping geometries
+ geom1_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ ],
+ }
+
+ geom2_data = {
+ "vertices": [
+ {"x": 500.0, "y": 500.0, "z": 500.0},
+ {"x": 1500.0, "y": 1500.0, "z": 1500.0},
+ ],
+ }
+
+ geom1 = PreciseGeometry(geom1_data)
+ geom2 = PreciseGeometry(geom2_data)
+
+ clash = detector.check_precise_clash("wall_1", geom1, "beam_1", geom2)
+
+ assert clash is not None
+ assert clash.element_id_1 == "wall_1"
+ assert clash.element_id_2 == "beam_1"
+ assert clash.clash_type == "precise_geometry"
+ assert clash.intersection_volume > 0
+ assert clash.location is not None
+
+
+def test_check_precise_clash_no_intersection():
+ """Test precise clash with no intersection."""
+ detector = ClashDetector()
+
+ geom1_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ ],
+ }
+
+ geom2_data = {
+ "vertices": [
+ {"x": 2000.0, "y": 2000.0, "z": 2000.0},
+ {"x": 3000.0, "y": 3000.0, "z": 3000.0},
+ ],
+ }
+
+ geom1 = PreciseGeometry(geom1_data)
+ geom2 = PreciseGeometry(geom2_data)
+
+ clash = detector.check_precise_clash("elem_1", geom1, "elem_2", geom2)
+
+ assert clash is None
+
+
+def test_check_precise_multiple_elements():
+ """Test precise clash detection for multiple elements."""
+ detector = ClashDetector()
+
+ elements = [
+ {
+ "id": "wall_1",
+ "geometry": {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 3000.0},
+ ],
+ },
+ },
+ {
+ "id": "beam_1",
+ "geometry": {
+ "vertices": [
+ {"x": 500.0, "y": 500.0, "z": 1000.0},
+ {"x": 1500.0, "y": 1500.0, "z": 2000.0},
+ ],
+ },
+ },
+ {
+ "id": "column_1",
+ "geometry": {
+ "vertices": [
+ {"x": 5000.0, "y": 5000.0, "z": 0.0},
+ {"x": 5500.0, "y": 5500.0, "z": 3000.0},
+ ],
+ },
+ },
+ ]
+
+ clashes = detector.check_precise_multiple_elements(elements)
+
+ # Should find 1 clash (wall_1 and beam_1)
+ assert len(clashes) == 1
+ assert clashes[0].element_id_1 == "wall_1"
+ assert clashes[0].element_id_2 == "beam_1"
+ assert clashes[0].clash_type == "precise_geometry"
+
+
+def test_precise_clash_severity():
+ """Test severity calculation for precise clashes."""
+ detector = ClashDetector()
+
+ # Large intersection
+ geom1_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 2000.0, "y": 2000.0, "z": 2000.0},
+ ],
+ }
+
+ geom2_data = {
+ "vertices": [
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ {"x": 3000.0, "y": 3000.0, "z": 3000.0},
+ ],
+ }
+
+ geom1 = PreciseGeometry(geom1_data)
+ geom2 = PreciseGeometry(geom2_data)
+
+ clash = detector.check_precise_clash("e1", geom1, "e2", geom2)
+
+ # 1000³ mm³ = 1 m³ = critical
+ assert clash.severity == "critical"
+
+
+def test_mixed_precise_and_bbox_clashes():
+ """Test detector can handle both precise and bounding box clashes."""
+ from model_checker.clash_detection import BoundingBox
+
+ detector = ClashDetector()
+
+ # Add bbox clash
+ bbox1 = BoundingBox(0, 0, 0, 1000, 1000, 1000)
+ bbox2 = BoundingBox(500, 500, 500, 1500, 1500, 1500)
+ detector.check_bounding_box_clash("e1", bbox1, "e2", bbox2)
+
+ # Add precise clash (overlapping cubes)
+ geom1_data = {
+ "vertices": [
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 1000.0, "z": 1000.0},
+ ]
+ }
+ geom2_data = {
+ "vertices": [
+ {"x": 500.0, "y": 500.0, "z": 500.0},
+ {"x": 1500.0, "y": 1500.0, "z": 1500.0},
+ ]
+ }
+ geom1 = PreciseGeometry(geom1_data)
+ geom2 = PreciseGeometry(geom2_data)
+ detector.check_precise_clash("e3", geom1, "e4", geom2)
+
+ assert detector.get_total_clashes() == 2
+ assert len(detector.clashes) == 2
+
+
+def test_precise_geometry_complex_shape():
+ """Test precise geometry with complex shape."""
+ # L-shaped geometry
+ geom_data = {
+ "vertices": [
+ # Bottom horizontal part
+ {"x": 0.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 0.0, "z": 0.0},
+ {"x": 1000.0, "y": 200.0, "z": 0.0},
+ {"x": 0.0, "y": 200.0, "z": 0.0},
+ # Vertical part
+ {"x": 0.0, "y": 200.0, "z": 0.0},
+ {"x": 200.0, "y": 200.0, "z": 0.0},
+ {"x": 200.0, "y": 800.0, "z": 0.0},
+ {"x": 0.0, "y": 800.0, "z": 0.0},
+ ],
+ "faces": [[0, 1, 2, 3], [4, 5, 6, 7]],
+ }
+
+ geom = PreciseGeometry(geom_data)
+
+ assert len(geom.vertices) == 8
+ assert len(geom.faces) == 2
+
+ bbox = geom.get_bounding_box()
+ assert bbox.min_x == 0.0
+ assert bbox.max_x == 1000.0
+ assert bbox.min_y == 0.0
+ assert bbox.max_y == 800.0