"""Segment dict construction for the visualization bundle.""" from __future__ import annotations import numpy as np from config.lighting import ( LIGHTING_TRANSFER_VERSION, REFLECTION_MAP_MAX, REFLECTION_MAP_MIN, SHADE_MAP_ENCODING, SHADE_MAP_MAX, SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX, SHOWROOM_SHADE_MAP_MIN, ) from config.runtime import ( ENABLE_FAST_SHADOW_TRANSFER, ENABLE_MULTI_PLANE_SURFACES, ENABLE_PLANE_FIT, ENABLE_SAM_REFINEMENT, ENABLE_SHADING_TRANSFER, ) from models.segmentation import segmenter_metadata_name from state import class_ids, class_name_for_id from enums import SurfaceType, TextureMappingMode from utils import encode_b64 def build_floor_segment( *, surface_indices: np.ndarray, occlusion_indices: np.ndarray, homography, plane: dict | None, surface_mapping: dict | None, use_surface_uv: bool, shade_map: np.ndarray | None, showroom_shade_map: np.ndarray | None, reflection_map: np.ndarray | None, floor_mask: np.ndarray, surface_mask: np.ndarray, replaced_material_mask: np.ndarray, occlusion_mask: np.ndarray, floor_direction: dict, sam_metadata: dict, edge_metadata: dict, mapping_metadata: dict, shade_statistics: dict | None, lighting_source: str | None, intrinsic_lighting_used: bool, models: dict, depth, geometry: dict, ) -> dict: return { "id": 0, "className": SurfaceType.FLOOR, "mask": encode_b64(surface_indices), "occlusionMask": encode_b64(occlusion_indices) if len(occlusion_indices) else None, "homography": homography, "floorTransform": plane.get("floorTransform") if plane else None, "plane": plane, # Pass raw numpy arrays for lazy assets — packaging writes them directly # to disk, avoiding the costly base64 encode/decode round-trip. "surfaceUv": ( surface_mapping["uv"].astype(np.float32) if use_surface_uv else None ), "surfacePlaneIds": ( surface_mapping["planeIds"].astype(np.uint8) if use_surface_uv else None ), "surfacePlanes": surface_mapping["planes"] if surface_mapping is not None else None, "shadeMap": shade_map, "showroomShadeMap": showroom_shade_map, "reflectionMap": reflection_map, "metadata": { "segmenter": segmenter_metadata_name(), "segmentationModel": models["segmentation"]["name"], "floorPixels": int(floor_mask.sum()), "surfacePixels": int(surface_mask.sum()), "replacedSoftFloorPixels": int(replaced_material_mask.sum()), "occlusionPixels": int(occlusion_mask.sum()), "floorDirection": floor_direction, **sam_metadata, **edge_metadata, **mapping_metadata, "depthEnabled": depth is not None, "shadingEnabled": shade_map is not None, "planeFitEnabled": ENABLE_PLANE_FIT, "multiPlaneSurfacesEnabled": ENABLE_MULTI_PLANE_SURFACES, "multiPlaneSurfaceCount": len(surface_mapping["planes"]) if surface_mapping is not None else 0, "multiPlaneNormalsReliable": ( bool(surface_mapping.get("normalsReliable")) if surface_mapping is not None else False ), "multiPlaneFitModes": ( [p.get("fitMode") for p in surface_mapping["planes"]] if surface_mapping is not None else [] ), "shadingTransferEnabled": ENABLE_SHADING_TRANSFER or ENABLE_FAST_SHADOW_TRANSFER, "fastShadowTransferEnabled": ENABLE_FAST_SHADOW_TRANSFER, "lightingSource": lighting_source, "lightingTransferVersion": LIGHTING_TRANSFER_VERSION, "shadeMapEncoding": SHADE_MAP_ENCODING, "shadeMapStatistics": shade_statistics, "shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX], "shadeMapSize": ( [int(shade_map.shape[1]), int(shade_map.shape[0])] if shade_map is not None else None ), "showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX], "renderPresetDefaults": { "preset": "realistic", "shadingStrength": 0.30, "reflectionTransfer": True, "edgeFeatherPixels": 2, }, "reflectionTransferEnabled": reflection_map is not None, "reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX], "geometryConfidence": plane.get("geometryConfidence", 0.0) if plane else 0.0, "metricGeometry": bool(plane and plane.get("floorTransform")), "geometryProvider": ( plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider") ), "geometryModel": models["geometry"]["name"], }, } def build_wall_segment( *, wall_id: int, wall_index: int, wall_indices: np.ndarray, homography, plane: dict, lighting_maps: dict | None, segmentation_model: str, ) -> dict: """Bundle dict for one wall. All heavy inputs are prepared by services.wall.pipeline, which also sets metadata["wallComponentCount"] once every wall is built.""" shade_map = lighting_maps.get("shadeMap") if lighting_maps else None showroom_shade_map = lighting_maps.get("showroomShadeMap") if lighting_maps else None reflection_map = lighting_maps.get("reflectionMap") if lighting_maps else None mapping_mode = ( TextureMappingMode.WALL_PLANE if plane.get("surfaceTransform") else TextureMappingMode.WALL_CONTOUR_HOMOGRAPHY ) return { "id": wall_id, "className": SurfaceType.WALL, "surfaceType": SurfaceType.WALL, "mask": encode_b64(wall_indices), # Wall masks already exclude (dilated) doors, windows and furniture, so # a separate occlusion mask would only duplicate that cut-out. "occlusionMask": None, "homography": homography, "floorTransform": plane.get("floorTransform"), "surfaceTransform": plane.get("surfaceTransform"), "plane": plane, # A wall is one plane: its exact pixel -> metres mapping is the 3x3 # surfaceTransform, so no per-pixel UV arrays are shipped. "surfaceUv": None, "surfacePlaneIds": None, "surfacePlanes": None, "shadeMap": shade_map, "showroomShadeMap": showroom_shade_map, "reflectionMap": reflection_map, "metadata": { "segmenter": segmenter_metadata_name(), "segmentationModel": segmentation_model, "wallPixels": int(len(wall_indices)), "wallComponent": wall_index, "shadingEnabled": shade_map is not None, "reflectionTransferEnabled": reflection_map is not None, "textureMappingMode": mapping_mode, "geometryConfidence": plane.get("geometryConfidence", 0.0), "geometryProvider": plane.get("geometryProvider"), "surfaceWidthMeters": plane.get("surfaceWidthMeters"), "surfaceHeightMeters": plane.get("surfaceHeightMeters"), "lightingTransferVersion": LIGHTING_TRANSFER_VERSION, "shadeMapEncoding": SHADE_MAP_ENCODING, "shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX], "shadeMapSize": ( [int(shade_map.shape[1]), int(shade_map.shape[0])] if shade_map is not None else None ), "showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX], "reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX], }, } def build_fallback_segments( seg_map: np.ndarray, depth, models: dict, ) -> list[dict]: segments = [] flat_seg = seg_map.ravel() for seg_id, class_id in enumerate(np.unique(flat_seg)): indices = np.where(flat_seg == class_id)[0].astype(np.uint32) if len(indices) < 1000: continue segments.append({ "id": int(seg_id), "className": class_name_for_id(int(class_id)), "mask": encode_b64(indices), "homography": None, "plane": None, "shadeMap": None, "showroomShadeMap": None, "reflectionMap": None, "metadata": { "segmenter": segmenter_metadata_name(), "segmentationModel": models["segmentation"]["name"], "depthEnabled": depth is not None, "shadingEnabled": False, "planeFitEnabled": ENABLE_PLANE_FIT, "samRefinementEnabled": ENABLE_SAM_REFINEMENT, "shadingTransferEnabled": ENABLE_SHADING_TRANSFER, "reflectionTransferEnabled": False, }, }) return segments