/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2026, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2026 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { Box3, Object3D, Vector3 } from "three"; import { IInfo, Info } from "@inweb/viewer-core"; import { IModelImpl } from "./ModelImpl.types"; import { IHighlighter, IHighlightStyle } from "./Highlighter.types"; import { convertUnits } from "../measurement/UnitConverter"; import { getDisplayUnit } from "../measurement/UnitFormatter"; export class ModelImpl implements IModelImpl { public id: string; public scene: Object3D; public highlighter: IHighlighter; private originalObjects: Set; private handleToObjects: Map>; constructor(scene: Object3D) { this.id = ""; this.scene = scene; this.originalObjects = new Set(); this.handleToObjects = new Map(); } dispose() { function disposeMaterial(material: any) { // if (material.alphaMap) material.alphaMap.dispose(); // if (material.anisotropyMap) material.anisotropyMap.dispose(); // if (material.aoMap) material.aoMap.dispose(); // if (material.bumpMap) material.bumpMap.dispose(); // if (material.clearcoatMap) material.clearcoatMap.dispose(); // if (material.clearcoatNormalMap) material.clearcoatNormalMap.dispose(); // if (material.clearcoatRoughnessMap) material.clearcoatRoughnessMap.dispose(); // if (material.displacementMap) material.displacementMap.dispose(); // if (material.emissiveMap) material.emissiveMap.dispose(); // if (material.gradientMap) material.gradientMap.dispose(); // if (material.envMap) material.envMap.dispose(); // if (material.iridescenceMap) material.iridescenceMap.dispose(); // if (material.lightMap) material.lightMap.dispose(); // if (material.map) material.map.dispose(); // if (material.metalnessMap) material.metalnessMap.dispose(); // if (material.normalMap) material.normalMap.dispose(); // if (material.roughnessMap) material.roughnessMap.dispose(); // if (material.sheenColorMap) material.sheenColorMap.dispose(); // if (material.sheenRoughnessMap) material.sheenRoughnessMap.dispose(); // if (material.specularMap) material.specularMap.dispose(); // if (material.thicknessMap) material.thicknessMap.dispose(); // if (material.transmissionMap) material.transmissionMap.dispose(); material.dispose(); } function disposeMaterials(material: any) { const materials = Array.isArray(material) ? material : [material]; materials.forEach((material: any) => disposeMaterial(material)); } function disposeObject(object: any) { if (object.geometry) object.geometry.dispose(); if (object.material) disposeMaterials(object.material); } this.handleToObjects.clear(); this.originalObjects.clear(); this.scene.traverse(disposeObject); this.scene.clear(); } initialize(): void { this.scene.traverse((object) => { this.originalObjects.add(object); const handle = object.userData.handle; if (!handle) return; // const fullHandle = `${this.id}:${handle}`; let objectsSet = this.handleToObjects.get(fullHandle); if (!objectsSet) { objectsSet = new Set(); this.handleToObjects.set(fullHandle, objectsSet); } objectsSet.add(object); }); } getUnits(): string { return "Meters"; } getUnitScale(): number { return convertUnits(this.getUnits(), "Meters", 1); } getUnitString(): string { return getDisplayUnit(this.getUnits()); } getPrecision(): number { return 2; } getInfo(): IInfo { // ===================== AI-CODE-START ====================== // Source: Claude Sonnet 4.5 // Date: 2025-10-02 // Reviewer: roman.mochalov@opendesign.com // Issue: CLOUD-5738 const geometries = new Set(); const materials = new Set(); const textures = new Set(); let totalObjects = 0; let totalTriangles = 0; let totalPoints = 0; let totalLines = 0; let totalEdges = 0; let geometryBytes = 0; let textureBytes = 0; this.scene.traverse((object: any) => { totalObjects++; if (object.geometry) { const geometry = object.geometry; if (!geometries.has(geometry)) { geometries.add(geometry); if (geometry.attributes) { for (const name in geometry.attributes) { const attribute = geometry.attributes[name]; if (attribute && attribute.array) { geometryBytes += attribute.array.byteLength; } } } if (geometry.index && geometry.index.array) { geometryBytes += geometry.index.array.byteLength; } } if (geometry.index) { const indexCount = geometry.index.count; if (object.isLine || object.isLineSegments) { totalLines += indexCount / 2; } else if (object.isPoints) { totalPoints += indexCount; } else { totalTriangles += indexCount / 3; } } else if (geometry.attributes && geometry.attributes.position) { const positionCount = geometry.attributes.position.count; if (object.isLine || object.isLineSegments) { totalLines += positionCount / 2; } else if (object.isPoints) { totalPoints += positionCount; } else { totalTriangles += positionCount / 3; } } if (object.isLineSegments && geometry.attributes.position) { totalEdges += geometry.attributes.position.count / 2; } } if (object.material) { const materialsArray = Array.isArray(object.material) ? object.material : [object.material]; materialsArray.forEach((material: any) => { materials.add(material); if (material.map && !textures.has(material.map)) { textures.add(material.map); textureBytes += estimateTextureSize(material.map); } const textureProps = [ "alphaMap", "aoMap", "bumpMap", "displacementMap", "emissiveMap", "envMap", "lightMap", "metalnessMap", "normalMap", "roughnessMap", "specularMap", "clearcoatMap", "clearcoatNormalMap", "clearcoatRoughnessMap", "iridescenceMap", "sheenColorMap", "sheenRoughnessMap", "thicknessMap", "transmissionMap", "anisotropyMap", "gradientMap", ]; textureProps.forEach((prop) => { const texture = material[prop]; if (texture && !textures.has(texture)) { textures.add(texture); textureBytes += estimateTextureSize(texture); } }); }); } }); function estimateTextureSize(texture: any): number { if (!texture.image) return 0; const width = texture.image.width || 0; const height = texture.image.height || 0; // Estimate bytes per pixel (RGBA = 4 bytes) const bytesPerPixel = 4; // Account for mipmaps (adds ~33% more memory) const mipmapMultiplier = texture.generateMipmaps ? 1.33 : 1; return width * height * bytesPerPixel * mipmapMultiplier; } // ===================== AI-CODE-END ====================== const info = new Info(); info.scene.objects = totalObjects; info.scene.triangles = Math.floor(totalTriangles); info.scene.points = Math.floor(totalPoints); info.scene.lines = Math.floor(totalLines); info.scene.edges = Math.floor(totalEdges); info.memory.geometries = geometries.size; info.memory.geometryBytes = geometryBytes; info.memory.optimizedGeometryBytes = 0; info.memory.textures = textures.size; info.memory.textureBytes = Math.floor(textureBytes); info.memory.materials = materials.size; info.memory.totalEstimatedGpuBytes = geometryBytes + Math.floor(textureBytes); info.optimizedScene.objects = info.scene.objects; info.optimizedScene.triangles = info.scene.triangles; info.optimizedScene.points = info.scene.points; info.optimizedScene.lines = info.scene.lines; info.optimizedScene.edges = info.scene.edges; return info; } getExtents(target: Box3): Box3 { const _box = new Box3(); function expandByObject(object: any, target: Box3) { if (!object.geometry) return; object.updateWorldMatrix(false, false); // Use object bounds for the BatchedMesh, InstancedMesh and SkinnedMesh, use geometry // bounds for others. See Box3.expandByObject() for more details. if (object.boundingBox !== undefined) { if (object.boundingBox === null) object.computeBoundingBox(); _box.copy(object.boundingBox); } else { if (object.geometry.boundingBox === null) object.geometry.computeBoundingBox(); _box.copy(object.geometry.boundingBox); } _box.applyMatrix4(object.matrixWorld); target.union(_box); } this.scene.traverseVisible((object) => expandByObject(object, target)); return target; } getObjects(): Object3D[] { return Array.from(this.originalObjects); } getVisibleObjects(): Object3D[] { const objects: Object3D[] = []; this.scene.traverseVisible((object) => object.userData.handle && objects.push(object)); return objects; } getObjectsByHandles(handles: string[]): Object3D[] { const objects: Object3D[][] = []; handles.forEach((handle) => { const objectsSet = this.handleToObjects.get(handle); objects.push(Array.from(objectsSet || [])); }); return objects.flat(); } getHandles(): string[] { return Array.from(this.handleToObjects.keys()); } getHandlesByObjects(objects: Object3D[]): string[] { const handlesSet = new Set(); objects.forEach((object) => { if (this.originalObjects.has(object)) { const fullHandle = `${this.id}:${object.userData.handle}`; handlesSet.add(fullHandle); } }); return Array.from(handlesSet); } // IModel hide(handles: string[]): this { const objects = this.getObjectsByHandles(handles); objects .filter((object) => this.originalObjects.has(object)) .forEach((object) => { object.visible = false; }); return this; } hideAll(): this { return this.isolate([]); } isolate(handles: string[]): this { const objects = this.getObjectsByHandles(handles); const visibleSet = new Set(objects); objects .filter((object) => this.originalObjects.has(object)) .forEach((object) => { object.traverseAncestors((parent) => visibleSet.add(parent)); object.traverse((child) => visibleSet.add(child)); }); this.scene.traverse((object) => (object.visible = visibleSet.has(object))); return this; } show(handles: string[]): this { const objects = this.getObjectsByHandles(handles); objects .filter((object) => this.originalObjects.has(object)) .forEach((object) => { object.visible = true; object.traverseAncestors((parent) => (parent.visible = true)); object.traverse((child) => (child.visible = true)); }); return this; } showAll(): this { this.scene.traverse((object) => (object.visible = true)); return this; } // The highlighter of the viewer has one style, the style of the default highlight. highlight(handles: string[], style?: string): this { if (this.highlighter) { const objects = this.getObjectsByHandles(handles); this.highlighter.highlightObjects(objects); } return this; } unhighlight(handles: string[]): this { if (this.highlighter) { const objects = this.getObjectsByHandles(handles); this.highlighter.unhighlightObjects(objects); } return this; } setHighlightStyle(name: string, style: IHighlightStyle): this { return this; } explode(scale = 0, coeff = 4): this { const centersCache = new Map(); const calcObjectCenter = (object: Object3D, target: Vector3): Vector3 => { const extents = new Box3().setFromObject(object); const handle = object.userData.handle; if (!handle) return extents.getCenter(target); const fullHandle = `${this.id}:${handle}`; const center = centersCache.get(fullHandle); if (center) return target.copy(center); const objects = this.getObjectsByHandles([fullHandle]); objects.forEach((x: Object3D) => extents.expandByObject(x)); extents.getCenter(target); centersCache.set(fullHandle, target.clone()); return target; }; const calcObjectOffset = (object: Object3D, target: Vector3): Vector3 => { const parent = object.parent; if (!parent || parent.userData.originalCenter === undefined) return target; return target.subVectors(object.userData.originalCenter, parent.userData.originalCenter); }; const calcObjectDepth = (object: Object3D): number => { if (object.userData.depth !== undefined) return object.userData.depth; const parent = object.parent; const depth = parent && object !== explodeRoot ? calcObjectDepth(parent) + 1 : 0; object.userData.depth = depth; object.userData.originalPosition = object.position.clone(); object.userData.originalCenter = calcObjectCenter(object, new Vector3()); object.userData.originalOffset = calcObjectOffset(object, new Vector3()); return depth; }; const explodeScale = scale / 100; const explodeRoot = this.scene; const explodeObjects = this.getObjects(); if (explodeRoot.userData.explodeDepth === undefined) { let maxDepth = 0; explodeObjects.forEach((object: Object3D) => { const depth = calcObjectDepth(object); if (depth > maxDepth) maxDepth = depth; }); explodeRoot.userData.explodeDepth = maxDepth; } const maxDepth = explodeRoot.userData.explodeDepth; const scaledExplodeDepth = explodeScale * maxDepth + 1; const explodeDepth = 0 | scaledExplodeDepth; const currentSegmentFraction = scaledExplodeDepth - explodeDepth; const explodeObject = (object: any) => { if (object.isCamera) return; object.position.copy(object.userData.originalPosition); const depth = object.userData.depth; if (depth > 0 && depth <= explodeDepth) { let objectScale = explodeScale * coeff; if (depth === explodeDepth) objectScale *= currentSegmentFraction; object.position.addScaledVector(object.userData.originalOffset, objectScale); } }; explodeObjects.forEach((object: Object3D) => { explodeObject(object); }); this.scene.updateMatrixWorld(); return this; } }