| |
| |
|
|
| |
| |
| |
| class Quadric { |
| matrix: number[]; |
|
|
| |
| |
| |
| constructor() { |
| |
| this.matrix = Array(16).fill(0); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| add(other: Quadric): Quadric { |
| for (let i = 0; i < 16; i++) { |
| this.matrix[i] += other.matrix[i]; |
| } |
| return this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static fromPlane(a: number, b: number, c: number, d: number): Quadric { |
| const q = new Quadric(); |
| |
| q.matrix[0] = a * a; |
| q.matrix[1] = a * b; |
| q.matrix[2] = a * c; |
| q.matrix[3] = a * d; |
| q.matrix[4] = b * a; |
| q.matrix[5] = b * b; |
| q.matrix[6] = b * c; |
| q.matrix[7] = b * d; |
| q.matrix[8] = c * a; |
| q.matrix[9] = c * b; |
| q.matrix[10] = c * c; |
| q.matrix[11] = c * d; |
| q.matrix[12] = d * a; |
| q.matrix[13] = d * b; |
| q.matrix[14] = d * c; |
| q.matrix[15] = d * d; |
| return q; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| evaluate(point: [number, number, number]): number { |
| const [x, y, z] = point; |
| const m = this.matrix; |
| return ( |
| x * (m[0] * x + m[1] * y + m[2] * z + m[3]) + |
| y * (m[4] * x + m[5] * y + m[6] * z + m[7]) + |
| z * (m[8] * x + m[9] * y + m[10] * z + m[11]) + |
| (m[12] * x + m[13] * y + m[14] * z + m[15]) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| findOptimalPoint( |
| initialGuess: [number, number, number] |
| ): [number, number, number] { |
| const m = this.matrix; |
| |
| const a = [ |
| [m[0], m[1], m[2]], |
| [m[4], m[5], m[6]], |
| [m[8], m[9], m[10]], |
| ]; |
| const b = [-m[3], -m[7], -m[11]]; |
|
|
| try { |
| const result = solveLinearSystem(a, b); |
| return result || initialGuess; |
| } catch (e) { |
| return initialGuess; |
| } |
| } |
| } |
|
|
| |
| |
| |
| class Edge { |
| v1: number; |
| v2: number; |
| error: number; |
|
|
| |
| |
| |
| |
| |
| |
| |
| constructor(v1: number, v2: number, error: number) { |
| this.v1 = v1; |
| this.v2 = v2; |
| this.error = error; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function solveLinearSystem( |
| A: number[][], |
| b: number[] |
| ): [number, number, number] | null { |
| const det = determinant3x3(A); |
| if (Math.abs(det) < 1e-10) { |
| return null; |
| } |
|
|
| const x = |
| determinant3x3([ |
| [b[0], A[0][1], A[0][2]], |
| [b[1], A[1][1], A[1][2]], |
| [b[2], A[2][1], A[2][2]], |
| ]) / det; |
|
|
| const y = |
| determinant3x3([ |
| [A[0][0], b[0], A[0][2]], |
| [A[1][0], b[1], A[1][2]], |
| [A[2][0], b[2], A[2][2]], |
| ]) / det; |
|
|
| const z = |
| determinant3x3([ |
| [A[0][0], A[0][1], b[0]], |
| [A[1][0], A[1][1], b[1]], |
| [A[2][0], A[2][1], b[2]], |
| ]) / det; |
|
|
| return [x, y, z]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function determinant3x3(matrix: number[][]): number { |
| return ( |
| matrix[0][0] * |
| (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) - |
| matrix[0][1] * |
| (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) + |
| matrix[0][2] * |
| (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function computePlaneEquation( |
| v1: [number, number, number], |
| v2: [number, number, number], |
| v3: [number, number, number] |
| ): [number, number, number, number] | null { |
| |
| const ux = v2[0] - v1[0], |
| uy = v2[1] - v1[1], |
| uz = v2[2] - v1[2]; |
| const vx = v3[0] - v1[0], |
| vy = v3[1] - v1[1], |
| vz = v3[2] - v1[2]; |
|
|
| const nx = uy * vz - uz * vy; |
| const ny = uz * vx - ux * vz; |
| const nz = ux * vy - uy * vx; |
|
|
| |
| const length = Math.sqrt(nx * nx + ny * ny + nz * nz); |
| if (length < 1e-10) { |
| return null; |
| } |
|
|
| const a = nx / length; |
| const b = ny / length; |
| const c = nz / length; |
| const d = -(a * v1[0] + b * v1[1] + c * v1[2]); |
|
|
| return [a, b, c, d]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function simplifyMesh( |
| vertices: [number, number, number][], |
| indices: number[], |
| colors: [number, number, number][], |
| targetVertexCount: number |
| ): { |
| vertices: [number, number, number][]; |
| indices: number[]; |
| colors: [number, number, number][]; |
| } { |
| console.log( |
| `Starting QEM simplification. Target vertex count: ${targetVertexCount}` |
| ); |
|
|
| |
| const vertexQuadrics = vertices.map(() => new Quadric()); |
|
|
| |
| for (let i = 0; i < indices.length; i += 4) { |
| if (indices[i] === -1) { |
| continue; |
| } |
|
|
| const v1 = vertices[indices[i]]; |
| const v2 = vertices[indices[i + 1]]; |
| const v3 = vertices[indices[i + 2]]; |
|
|
| const plane = computePlaneEquation(v1, v2, v3); |
| if (!plane) { |
| continue; |
| } |
|
|
| const faceQuadric = Quadric.fromPlane(...plane); |
|
|
| |
| vertexQuadrics[indices[i]].add(faceQuadric); |
| vertexQuadrics[indices[i + 1]].add(faceQuadric); |
| vertexQuadrics[indices[i + 2]].add(faceQuadric); |
| } |
|
|
| |
| const edges = []; |
| const seen = new Set(); |
|
|
| for (let i = 0; i < indices.length; i += 4) { |
| if (indices[i] === -1) { |
| continue; |
| } |
|
|
| for (let j = 0; j < 3; j++) { |
| const v1 = indices[i + j]; |
| const v2 = indices[i + ((j + 1) % 3)]; |
|
|
| const edgeKey = `${Math.min(v1, v2)},${Math.max(v1, v2)}`; |
| if (seen.has(edgeKey)) { |
| continue; |
| } |
| seen.add(edgeKey); |
|
|
| |
| if (!colorsMatch(colors[v1], colors[v2])) { |
| continue; |
| } |
|
|
| const combinedQuadric = new Quadric(); |
| combinedQuadric.add(vertexQuadrics[v1]); |
| combinedQuadric.add(vertexQuadrics[v2]); |
|
|
| |
| const optimalPos = combinedQuadric.findOptimalPoint([ |
| (vertices[v1][0] + vertices[v2][0]) / 2, |
| (vertices[v1][1] + vertices[v2][1]) / 2, |
| (vertices[v1][2] + vertices[v2][2]) / 2, |
| ]); |
|
|
| |
| |
| |
| |
| |
|
|
| |
|
|
| const error = combinedQuadric.evaluate(optimalPos); |
| edges.push(new Edge(v1, v2, error)); |
| } |
| } |
|
|
| |
| edges.sort((a, b) => a.error - b.error); |
|
|
| |
| const alive = new Array(vertices.length).fill(true); |
| const mapping = new Array(vertices.length).fill(-1); |
| const newVertices = []; |
| const newColors = []; |
| let currentVertex = 0; |
|
|
| |
| for (const edge of edges) { |
| if (newVertices.length >= targetVertexCount) { |
| break; |
| } |
| if (!alive[edge.v1] || !alive[edge.v2]) { |
| continue; |
| } |
|
|
| |
| const combinedQuadric = new Quadric(); |
| combinedQuadric.add(vertexQuadrics[edge.v1]); |
| combinedQuadric.add(vertexQuadrics[edge.v2]); |
|
|
| const optimalPos = combinedQuadric.findOptimalPoint([ |
| (vertices[edge.v1][0] + vertices[edge.v2][0]) / 2, |
| (vertices[edge.v1][1] + vertices[edge.v2][1]) / 2, |
| (vertices[edge.v1][2] + vertices[edge.v2][2]) / 2, |
| ]); |
|
|
| |
| newVertices.push(optimalPos); |
| newColors.push(colors[edge.v1]); |
| mapping[edge.v1] = currentVertex; |
| mapping[edge.v2] = currentVertex; |
| alive[edge.v1] = false; |
| alive[edge.v2] = false; |
| currentVertex++; |
| } |
|
|
| |
| for (let i = 0; i < vertices.length; i++) { |
| if (alive[i]) { |
| newVertices.push(vertices[i]); |
| newColors.push(colors[i]); |
| mapping[i] = currentVertex++; |
| } |
| } |
|
|
| |
| const newIndices = []; |
| const seenFaces = new Set(); |
|
|
| for (let i = 0; i < indices.length; i += 4) { |
| if (indices[i] === -1) { |
| continue; |
| } |
|
|
| const v1 = mapping[indices[i]]; |
| const v2 = mapping[indices[i + 1]]; |
| const v3 = mapping[indices[i + 2]]; |
|
|
| |
| if (v1 === v2 || v2 === v3 || v3 === v1) { |
| continue; |
| } |
|
|
| |
| const faceKey = [v1, v2, v3].sort().join(","); |
| if (seenFaces.has(faceKey)) { |
| continue; |
| } |
| seenFaces.add(faceKey); |
|
|
| newIndices.push(v1, v2, v3, -1); |
| } |
|
|
| console.log( |
| `QEM simplification complete. New vertex count: ${newVertices.length}` |
| ); |
| return { vertices: newVertices, indices: newIndices, colors: newColors }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function colorsMatch( |
| c1: [number, number, number], |
| c2: [number, number, number], |
| epsilon = 0.001 |
| ): boolean { |
| return ( |
| Math.abs(c1[0] - c2[0]) < epsilon && |
| Math.abs(c1[1] - c2[1]) < epsilon && |
| Math.abs(c1[2] - c2[2]) < epsilon |
| ); |
| } |
|
|