Add 3D reconstruction module (trimesh + manifold3d)"
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484"""3D model generation from structured floor plan data.
Converts a FloorPlan schema into a 3D mesh scene with:- Extruded walls with proper thickness- Door and window openings (boolean-subtracted from walls)- Floor slabs and ceilings per room- PBR materials for each element type- Export to GLB/glTF (for Three.js/web) and OBJ
Uses trimesh + manifold3d for watertight boolean geometry."""
from __future__ import annotations
import mathfrom typing import Optional
import numpy as npimport trimeshfrom shapely.geometry import LineString, Polygonfrom trimesh.visual.material import PBRMaterial
from .schema import FloorPlan, Wall, Opening, OpeningType, Roomfrom .geometry import ( wall_to_polygon, interpolate_along_centerline, normal_at_distance, compute_centerline_length, detect_rooms_from_walls,)
# ββ Architectural constants (meters) ββββββββββββββββββββββββββ
WALL_HEIGHT = 2.8 # Standard residential ceiling heightFLOOR_THICKNESS = 0.12 # Floor slab thicknessCEILING_THICKNESS = 0.08 # Ceiling thickness
DOOR_HEIGHT = 2.1 # Standard door heightDOOR_DEFAULT_WIDTH = 0.9 # Standard single door width
WINDOW_SILL_HEIGHT = 0.9 # Window sill from floorWINDOW_HEAD_HEIGHT = 2.1 # Window top from floorWINDOW_DEFAULT_WIDTH = 1.2 # Standard window width
BOOL_MARGIN = 0.06 # Extra margin for clean boolean cuts
# ββ Materials (PBR) βββββββββββββββββββββββββββββββββββββββββββ
def _wall_material() -> PBRMaterial: return PBRMaterial( baseColorFactor=[0.92, 0.89, 0.85, 1.0], # Warm white roughnessFactor=0.85, metallicFactor=0.0, name="wall", )
def _floor_material() -> PBRMaterial: return PBRMaterial( baseColorFactor=[0.76, 0.69, 0.57, 1.0], # Light wood roughnessFactor=0.7, metallicFactor=0.0, name="floor", )
def _ceiling_material() -> PBRMaterial: return PBRMaterial( baseColorFactor=[0.97, 0.97, 0.97, 1.0], # Near-white roughnessFactor=0.9, metallicFactor=0.0, name="ceiling", )
def _door_frame_material() -> PBRMaterial: return PBRMaterial( baseColorFactor=[0.55, 0.35, 0.20, 1.0], # Dark wood roughnessFactor=0.6, metallicFactor=0.0, name="door_frame", )
def _window_frame_material() -> PBRMaterial: return PBRMaterial( baseColorFactor=[0.85, 0.85, 0.85, 1.0], # Light grey aluminum roughnessFactor=0.3, metallicFactor=0.5, name="window_frame", )
def _glass_material() -> PBRMaterial: return PBRMaterial( baseColorFactor=[0.75, 0.85, 0.95, 0.3], # Translucent blue roughnessFactor=0.1, metallicFactor=0.1, name="glass", )
# ββ Wall mesh construction ββββββββββββββββββββββββββββββββββββ
def _wall_segment_to_mesh(wall: Wall, height: float = WALL_HEIGHT) -> trimesh.Trimesh: """Extrude a single wall's 2D polygon into a 3D wall mesh.""" poly = wall_to_polygon(wall) if not poly.is_valid: poly = poly.buffer(0) mesh = trimesh.creation.extrude_polygon(poly, height=height) return mesh
def _make_opening_cutter( wall: Wall, opening: Opening, z_bottom: float, z_top: float,) -> trimesh.Trimesh: """Create a box mesh aligned to a wall at an opening position, for boolean subtraction. The box is oriented along the wall direction, spanning the full wall thickness (with margin) to ensure a clean cut. """ coords = wall.centerline_coords half_len = opening.length / 2.0 mid_dist = opening.start + half_len # Get the center point and direction at the opening's midpoint center_xy = interpolate_along_centerline(coords, mid_dist) normal = normal_at_distance(coords, mid_dist) # Wall direction is perpendicular to normal (rotate normal 90Β° CW) wall_dir = (normal[1], -normal[0]) angle = math.atan2(wall_dir[1], wall_dir[0]) # Create rotation around Z axis T = trimesh.transformations.rotation_matrix(angle, [0, 0, 1]) # Position at center of opening T[:3, 3] = [ center_xy[0], center_xy[1], (z_bottom + z_top) / 2.0, ] cutter = trimesh.creation.box( extents=[ opening.length + BOOL_MARGIN, wall.thickness + BOOL_MARGIN * 4, # Extra depth for clean cut (z_top - z_bottom) + BOOL_MARGIN, ], transform=T, ) return cutter
def _make_door_frame( wall: Wall, opening: Opening, height: float = DOOR_HEIGHT,) -> trimesh.Trimesh: """Create a simple door frame mesh (thin border around the opening).""" coords = wall.centerline_coords mid_dist = opening.start + opening.length / 2.0 center_xy = interpolate_along_centerline(coords, mid_dist) normal = normal_at_distance(coords, mid_dist) wall_dir = (normal[1], -normal[0]) angle = math.atan2(wall_dir[1], wall_dir[0]) frame_width = 0.05 # 5cm frame T = trimesh.transformations.rotation_matrix(angle, [0, 0, 1]) T[:3, 3] = [center_xy[0], center_xy[1], height / 2.0] # Outer box outer = trimesh.creation.box( extents=[opening.length + frame_width * 2, wall.thickness, height], transform=T, ) # Inner cutout inner = trimesh.creation.box( extents=[opening.length, wall.thickness + 0.02, height - frame_width], transform=T.copy(), ) try: frame = trimesh.boolean.difference([outer, inner], engine="manifold") return frame except Exception: return outer # Fallback: just the outer box
def _make_window_glass( wall: Wall, opening: Opening, sill_height: float = WINDOW_SILL_HEIGHT, head_height: float = WINDOW_HEAD_HEIGHT,) -> trimesh.Trimesh: """Create a thin glass pane at a window opening.""" coords = wall.centerline_coords mid_dist = opening.start + opening.length / 2.0 center_xy = interpolate_along_centerline(coords, mid_dist) normal = normal_at_distance(coords, mid_dist) wall_dir = (normal[1], -normal[0]) angle = math.atan2(wall_dir[1], wall_dir[0]) glass_thickness = 0.01 # 1cm T = trimesh.transformations.rotation_matrix(angle, [0, 0, 1]) T[:3, 3] = [ center_xy[0], center_xy[1], (sill_height + head_height) / 2.0, ] glass = trimesh.creation.box( extents=[opening.length - 0.04, glass_thickness, head_height - sill_height - 0.04], transform=T, ) return glass
# ββ Floor and ceiling slabs βββββββββββββββββββββββββββββββββββ
def _floor_slab(room_polygon: Polygon, thickness: float = FLOOR_THICKNESS) -> trimesh.Trimesh: """Create a floor slab from a room's 2D polygon.""" if not room_polygon.is_valid: room_polygon = room_polygon.buffer(0) mesh = trimesh.creation.extrude_polygon(room_polygon, height=thickness) mesh.apply_translation([0, 0, -thickness]) return mesh
def _ceiling_slab( room_polygon: Polygon, wall_height: float = WALL_HEIGHT, thickness: float = CEILING_THICKNESS,) -> trimesh.Trimesh: """Create a ceiling slab from a room's 2D polygon.""" if not room_polygon.is_valid: room_polygon = room_polygon.buffer(0) mesh = trimesh.creation.extrude_polygon(room_polygon, height=thickness) mesh.apply_translation([0, 0, wall_height]) return mesh
# ββ Main 3D generation pipeline ββββββββββββββββββββββββββββββ
def generate_3d_model( floorplan: FloorPlan, room_polygons: Optional[list[Polygon]] = None, wall_height: float = WALL_HEIGHT, include_floors: bool = True, include_ceilings: bool = True, include_door_frames: bool = True, include_window_glass: bool = True, pixels_per_meter: Optional[float] = None,) -> trimesh.Scene: """Convert a FloorPlan into a 3D trimesh Scene. Args: floorplan: The structured floor plan data room_polygons: Pre-computed room polygons (from geometry engine). If None, will be detected automatically. wall_height: Height of walls in meters include_floors: Whether to generate floor slabs include_ceilings: Whether to generate ceiling slabs include_door_frames: Whether to generate door frame meshes include_window_glass: Whether to generate glass pane meshes pixels_per_meter: If set, all coordinates will be divided by this value to convert from pixel space to meters Returns: trimesh.Scene with named geometry nodes """ scene = trimesh.Scene() if not floorplan.walls: return scene # ββ Scale from pixels to meters if needed ββ fp = floorplan if pixels_per_meter and pixels_per_meter != 1.0: fp = _scale_floorplan(floorplan, 1.0 / pixels_per_meter) # ββ Step 1: Extrude each wall independently ββ wall_meshes = [] for wall in fp.walls: try: mesh = _wall_segment_to_mesh(wall, height=wall_height) if mesh.is_volume: wall_meshes.append(mesh) else: # Try to repair trimesh.repair.fix_winding(mesh) trimesh.repair.fix_normals(mesh) mesh.fill_holes() wall_meshes.append(mesh) except Exception as e: print(f"Warning: could not extrude wall {wall.id}: {e}") continue if not wall_meshes: return scene # ββ Step 2: Union all wall meshes ββ try: if len(wall_meshes) == 1: walls_combined = wall_meshes[0] else: walls_combined = trimesh.boolean.union(wall_meshes, engine="manifold") except Exception as e: print(f"Warning: wall union failed ({e}), concatenating instead") walls_combined = trimesh.util.concatenate(wall_meshes) # ββ Step 3: Cut door/window openings ββ opening_cutters = [] door_frames = [] window_glasses = [] for wall in fp.walls: for opening in wall.openings: if opening.type == OpeningType.DOOR: z_bottom = 0.0 z_top = opening.head_height or DOOR_HEIGHT cutter = _make_opening_cutter(wall, opening, z_bottom, z_top) opening_cutters.append(cutter) if include_door_frames: try: frame = _make_door_frame(wall, opening, height=z_top) door_frames.append(frame) except Exception: pass elif opening.type == OpeningType.WINDOW: sill = opening.sill_height or WINDOW_SILL_HEIGHT head = opening.head_height or WINDOW_HEAD_HEIGHT cutter = _make_opening_cutter(wall, opening, sill, head) opening_cutters.append(cutter) if include_window_glass: try: glass = _make_window_glass(wall, opening, sill, head) window_glasses.append(glass) except Exception: pass # Apply boolean subtraction for openings if opening_cutters: try: all_cutters = trimesh.boolean.union(opening_cutters, engine="manifold") walls_combined = trimesh.boolean.difference( [walls_combined, all_cutters], engine="manifold" ) except Exception as e: print(f"Warning: opening boolean failed ({e}), cutting individually") for cutter in opening_cutters: try: walls_combined = trimesh.boolean.difference( [walls_combined, cutter], engine="manifold" ) except Exception: continue # Apply wall material walls_combined.visual = trimesh.visual.TextureVisuals(material=_wall_material()) scene.add_geometry(walls_combined, node_name="walls", geom_name="walls") # ββ Step 4: Add door frames ββ for i, frame in enumerate(door_frames): frame.visual = trimesh.visual.TextureVisuals(material=_door_frame_material()) scene.add_geometry(frame, node_name=f"door_frame_{i}", geom_name=f"door_frame_{i}") # ββ Step 5: Add window glass ββ for i, glass in enumerate(window_glasses): glass.visual = trimesh.visual.TextureVisuals(material=_glass_material()) scene.add_geometry(glass, node_name=f"window_glass_{i}", geom_name=f"window_glass_{i}") # ββ Step 6: Detect rooms and add floors/ceilings ββ if room_polygons is None: room_polygons = detect_rooms_from_walls(fp.walls) else: # Scale room polygons if we scaled the floor plan if pixels_per_meter and pixels_per_meter != 1.0: from shapely import affinity scale_factor = 1.0 / pixels_per_meter room_polygons = [ affinity.scale(rp, xfact=scale_factor, yfact=scale_factor, origin=(0, 0)) for rp in room_polygons ] for i, rpoly in enumerate(room_polygons): if not rpoly.is_valid: rpoly = rpoly.buffer(0) if rpoly.area < 0.5: # Skip tiny fragments continue if include_floors: try: floor = _floor_slab(rpoly) floor.visual = trimesh.visual.TextureVisuals(material=_floor_material()) scene.add_geometry(floor, node_name=f"floor_{i}", geom_name=f"floor_{i}") except Exception as e: print(f"Warning: floor slab {i} failed: {e}") if include_ceilings: try: ceiling = _ceiling_slab(rpoly, wall_height=wall_height) ceiling.visual = trimesh.visual.TextureVisuals(material=_ceiling_material()) scene.add_geometry(ceiling, node_name=f"ceiling_{i}", geom_name=f"ceiling_{i}") except Exception as e: print(f"Warning: ceiling slab {i} failed: {e}") return scene
def _scale_floorplan(fp: FloorPlan, scale: float) -> FloorPlan: """Create a scaled copy of a floor plan (all coordinates multiplied by scale).""" from .schema import Point2D, Wall, Opening, Room new_walls = [] for wall in fp.walls: new_centerline = [ Point2D(x=pt.x * scale, y=pt.y * scale) for pt in wall.centerline ] new_openings = [ Opening( id=o.id, type=o.type, start=o.start * scale, length=o.length * scale, swing=o.swing, sill_height=o.sill_height, head_height=o.head_height, ) for o in wall.openings ] new_walls.append(Wall( id=wall.id, centerline=new_centerline, thickness=wall.thickness * scale, openings=new_openings, )) # Don't copy rooms β they'll be recomputed from scaled walls return FloorPlan( scale=fp.scale, origin=Point2D(x=fp.origin.x * scale, y=fp.origin.y * scale), walls=new_walls, rooms=[], # Will be recomputed )
# ββ Export helpers ββββββββββββββββββββββββββββββββββββββββββββ
def export_glb(scene: trimesh.Scene, path: str) -> int: """Export scene to GLB (binary glTF). Returns file size in bytes.""" glb_bytes = scene.export(file_type="glb") with open(path, "wb") as f: f.write(glb_bytes) return len(glb_bytes)
def export_obj(scene: trimesh.Scene, path: str) -> None: """Export scene to OBJ (Wavefront). Merges all geometry.""" combined = trimesh.util.concatenate(list(scene.geometry.values())) combined.export(path)
def export_gltf(scene: trimesh.Scene, directory: str) -> dict: """Export scene to glTF (JSON + binary buffer). Returns dict of filenames.""" import os os.makedirs(directory, exist_ok=True) gltf_dict = scene.export(file_type="gltf") for filename, data in gltf_dict.items(): filepath = os.path.join(directory, filename) mode = "wb" if isinstance(data, bytes) else "w" with open(filepath, mode) as f: f.write(data) return {k: os.path.join(directory, k) for k in gltf_dict}floorplan/reconstruction.pyAdded+484