1 line
18 KiB
Plaintext
1 line
18 KiB
Plaintext
|
|
{"version":3,"file":"SimplifyModifier.cjs","sources":["../../src/modifiers/SimplifyModifier.ts"],"sourcesContent":["import { BufferGeometry, Float32BufferAttribute, Vector3 } from 'three'\nimport * as BufferGeometryUtils from '../utils/BufferGeometryUtils'\n\nconst cb = /* @__PURE__ */ new Vector3()\nconst ab = /* @__PURE__ */ new Vector3()\n\nfunction pushIfUnique<TItem>(array: TItem[], object: TItem): void {\n if (array.indexOf(object) === -1) array.push(object)\n}\n\nfunction removeFromArray<TItem>(array: TItem[], object: TItem): void {\n const k = array.indexOf(object)\n if (k > -1) array.splice(k, 1)\n}\n\nclass Vertex {\n public position: Vector3\n private id: number\n\n public faces: Triangle[]\n public neighbors: Vertex[]\n\n public collapseCost: number\n public collapseNeighbor: null | Vertex\n\n public minCost: number = 0\n public totalCost: number = 0\n public costCount: number = 0\n\n constructor(v: Vector3, id: number) {\n this.position = v\n this.id = id // old index id\n\n this.faces = [] // faces vertex is connected\n this.neighbors = [] // neighbouring vertices aka \"adjacentVertices\"\n\n // these will be computed in computeEdgeCostAtVertex()\n this.collapseCost = 0 // cost of collapsing this vertex, the less the better. aka objdist\n this.collapseNeighbor = null // best candinate for collapsing\n }\n\n public addUniqueNeighbor(vertex: Vertex): void {\n pushIfUnique(this.neighbors, vertex)\n }\n\n public removeIfNonNeighbor(n: Vertex): void {\n const neighbors = this.neighbors\n const faces = this.faces\n\n const offset = neighbors.indexOf(n)\n if (offset === -1) return\n for (let i = 0; i < faces.length; i++) {\n if (faces[i].hasVertex(n)) return\n }\n\n neighbors.splice(offset, 1)\n }\n}\n\n// we use a triangle class to represent structure of face slightly differently\nclass Triangle {\n private a: number\n private b: number\n private c: Number\n\n public v1: Vertex\n public v2: Vertex\n public v3: Vertex\n\n public normal = new Vector3()\n\n constructor(v1: Vertex, v2: Vertex, v3: Vertex, a: number, b: number, c: number) {\n this.a = a\n this.b = b\n this.c = c\n\n this.v1 = v1\n this.v2 = v2\n this.v3 = v3\n\n this.computeNormal()\n\n v1.faces.push(this)\n v1.addUniqueNeighbor(v2)\n v1.addUniqueNeighbor(v3)\n\n v2.faces.push(this)\n v2.addUniqueNeighbor(v1)\n v2.addUniqueNeighbor(v3)\n\n v3.faces.push(this)\n v3.addUniqueNeighbor(v1)\n v3.addUniqueNeighbor(v2)\n }\n\n private computeNormal(): void {\n const vA = this.v1.position\n const vB = this.v2.position\n const vC = this.v3.position\n\n cb.subVectors(vC, vB)\n ab.subVectors(vA, vB)\n cb.cross(ab).normalize()\n\n this.normal.copy(cb)\n }\n\n public hasVertex(v: Vertex): boolean {\n return v === this.v1 || v === this.v2 || v === this.v3\n }\n\n public replaceVertex(oldv: Vertex, newv: Vertex): void {\n if (oldv === this.v1) this.v1 = newv\n else if (oldv === this.v2) this.v2 = newv\n else if (oldv === this.v3) this.v3 = newv\n\n removeFromArray(oldv.faces, this)\n newv.faces.push(this)\n\n oldv.removeIfNonNeighbor(this.v1)\n this.v1.removeIfNonNeighbor(oldv)\n\n oldv.removeIfNonNeighbor(this.v2)\n this.v2.removeIfNonNeighbor(oldv)\n\n oldv.removeIfNonNeighbor(this.v3)\n this.v3.removeIfNonNeighbor(oldv)\n\n this.v1.addUniqueNeighbor(this.v2)\n this.v1.addUniqueNeighbor(this.v3)\n\n this.v2.addUniqueNeighbor(this.v1)\n this.v2.addUniqueNeighbor(this.v3)\n\n this.v3.addUniqueNeighbor(this.v1)\n this.v3.addUniqueNeighbor(this.v2)\n\n this.computeNormal()\n }\n}\n\n/**\n *\tSimplification Geometry Modifier\n * - based on code and technique\n *\t - by Stan Melax in 1998\n *\t - Progressive Mesh type Polygon Reduction Algorithm\n * - http://www.melax.com/polychop/\n */\n\nclass SimplifyModifier {\n constructor() {}\n\n private computeEdgeCollapseCost = (u: Vertex, v: Vertex): number => {\n // if we collapse edge uv by moving u to v t
|