( reconstruction: Reconstruction, extent = 10, minPercentile = 0.1, maxPercentile = 0.9, useImages = true )
| 45 | * Reference: colmap/scene/reconstruction.cc Normalize() |
| 46 | */ |
| 47 | export function computeNormalizeScale( |
| 48 | reconstruction: Reconstruction, |
| 49 | extent = 10, |
| 50 | minPercentile = 0.1, |
| 51 | maxPercentile = 0.9, |
| 52 | useImages = true |
| 53 | ): Sim3d { |
| 54 | const coordsX: number[] = []; |
| 55 | const coordsY: number[] = []; |
| 56 | const coordsZ: number[] = []; |
| 57 | |
| 58 | if (useImages) { |
| 59 | for (const image of reconstruction.images.values()) { |
| 60 | const pos = getImageWorldPosition(image); |
| 61 | coordsX.push(pos.x); |
| 62 | coordsY.push(pos.y); |
| 63 | coordsZ.push(pos.z); |
| 64 | } |
| 65 | } else if (reconstruction.points3D) { |
| 66 | for (const point3D of reconstruction.points3D.values()) { |
| 67 | coordsX.push(point3D.xyz[0]); |
| 68 | coordsY.push(point3D.xyz[1]); |
| 69 | coordsZ.push(point3D.xyz[2]); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (coordsX.length === 0) { |
| 74 | return identitySim3d(); |
| 75 | } |
| 76 | |
| 77 | const minX = percentile(coordsX, minPercentile); |
| 78 | const maxX = percentile(coordsX, maxPercentile); |
| 79 | const minY = percentile(coordsY, minPercentile); |
| 80 | const maxY = percentile(coordsY, maxPercentile); |
| 81 | const minZ = percentile(coordsZ, minPercentile); |
| 82 | const maxZ = percentile(coordsZ, maxPercentile); |
| 83 | |
| 84 | const centerX = (minX + maxX) / 2; |
| 85 | const centerY = (minY + maxY) / 2; |
| 86 | const centerZ = (minZ + maxZ) / 2; |
| 87 | |
| 88 | const diagonal = Math.sqrt( |
| 89 | Math.pow(maxX - minX, 2) + Math.pow(maxY - minY, 2) + Math.pow(maxZ - minZ, 2) |
| 90 | ); |
| 91 | |
| 92 | const scale = diagonal > 1e-6 ? extent / diagonal : 1; |
| 93 | |
| 94 | return { |
| 95 | scale, |
| 96 | rotation: new THREE.Quaternion(), |
| 97 | translation: new THREE.Vector3(-centerX * scale, -centerY * scale, -centerZ * scale), |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | function percentile(arr: number[], p: number): number { |
| 102 | const sorted = [...arr].sort((a, b) => a - b); |
no test coverage detected