// Visor 3D de la nube de puntos (three.js, MIT). Ejes OpenCV -> three: (x, -y, -z). import * as THREE from 'three'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; export class PointViewer { constructor(container) { this.container = container; this.renderer = null; } ensure() { if (this.renderer) return true; try { this.renderer = new THREE.WebGLRenderer({ antialias: false, preserveDrawingBuffer: true }); } catch (e) { this.container.textContent = 'Tu navegador no admite WebGL: el visor 3D no está disponible (las medidas siguen funcionando).'; return false; } this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1)); this.container.appendChild(this.renderer.domElement); this.scene = new THREE.Scene(); this.scene.background = new THREE.Color(0x111318); this.camera = new THREE.PerspectiveCamera(50, 1, 0.01, 1000); this.controls = new OrbitControls(this.camera, this.renderer.domElement); this.controls.enableDamping = true; this.lines = new THREE.Group(); this.scene.add(this.lines); const loop = () => { this.raf = requestAnimationFrame(loop); this.controls.update(); this.renderer.render(this.scene, this.camera); }; loop(); new ResizeObserver(() => this.resize()).observe(this.container); this.resize(); return true; } resize() { if (!this.renderer) return; const w = this.container.clientWidth || 640, h = Math.max(240, Math.round(w * 0.62)); this.renderer.setSize(w, h, false); this.renderer.domElement.style.width = '100%'; this.camera.aspect = w / h; this.camera.updateProjectionMatrix(); } setCloud(geo, rgba, factor, fovY, medianDepth) { if (!this.ensure()) return; if (this.points) { this.scene.remove(this.points); this.points.geometry.dispose(); this.points.material.dispose(); } const n = geo.valid.reduce((a, b) => a + b, 0); const pos = new Float32Array(n * 3), col = new Float32Array(n * 3); let k = 0; for (let p = 0; p < geo.valid.length; p++) { if (!geo.valid[p]) continue; pos[3 * k] = geo.points[3 * p] * factor; pos[3 * k + 1] = -geo.points[3 * p + 1] * factor; pos[3 * k + 2] = -geo.points[3 * p + 2] * factor; col[3 * k] = rgba[4 * p] / 255; col[3 * k + 1] = rgba[4 * p + 1] / 255; col[3 * k + 2] = rgba[4 * p + 2] / 255; k++; } const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.BufferAttribute(pos, 3)); g.setAttribute('color', new THREE.BufferAttribute(col, 3)); const size = Math.max(0.002, ((medianDepth * factor) / geo.fxPx) * 2.2); // ~2 veces la separación entre píxeles a la profundidad mediana this.points = new THREE.Points(g, new THREE.PointsMaterial({ size, vertexColors: true, sizeAttenuation: true })); this.scene.add(this.points); // vista inicial oblicua (desde arriba a la derecha de la cámara original) para que se aprecie el relieve this.camera.fov = fovY; this.camera.updateProjectionMatrix(); const d = medianDepth * factor; this.camera.position.set(0.45 * d, 0.3 * d, 0.1 * d); this.controls.target.set(0, 0, -d); this.controls.update(); this.count = n; } setMeasurements(list, factor) { if (!this.renderer) return; this.lines.clear(); for (const m of list) { if (!m.result) continue; const a = m.result.a3d, b = m.result.b3d; const g = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(a[0], -a[1], -a[2]), new THREE.Vector3(b[0], -b[1], -b[2])]); this.lines.add(new THREE.Line(g, new THREE.LineBasicMaterial({ color: 0x00e5ff }))); } } }