summit/frontend/node_modules/three-stdlib/math/ConvexHull.cjs.map

1 line
35 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"ConvexHull.cjs","sources":["../../src/math/ConvexHull.js"],"sourcesContent":["import { Line3, Plane, Triangle, Vector3 } from 'three'\n\n/**\n * Ported from: https://github.com/maurizzzio/quickhull3d/ by Mauricio Poppe (https://github.com/maurizzzio)\n */\n\nconst Visible = 0\nconst Deleted = 1\n\nconst _v1 = /* @__PURE__ */ new Vector3()\nconst _line3 = /* @__PURE__ */ new Line3()\nconst _plane = /* @__PURE__ */ new Plane()\nconst _closestPoint = /* @__PURE__ */ new Vector3()\nconst _triangle = /* @__PURE__ */ new Triangle()\n\nclass ConvexHull {\n constructor() {\n this.tolerance = -1\n\n this.faces = [] // the generated faces of the convex hull\n this.newFaces = [] // this array holds the faces that are generated within a single iteration\n\n // the vertex lists work as follows:\n //\n // let 'a' and 'b' be 'Face' instances\n // let 'v' be points wrapped as instance of 'Vertex'\n //\n // [v, v, ..., v, v, v, ...]\n // ^ ^\n // | |\n // a.outside b.outside\n //\n this.assigned = new VertexList()\n this.unassigned = new VertexList()\n\n this.vertices = [] // vertices of the hull (internal representation of given geometry data)\n }\n\n setFromPoints(points) {\n // The algorithm needs at least four points.\n\n if (points.length >= 4) {\n this.makeEmpty()\n\n for (let i = 0, l = points.length; i < l; i++) {\n this.vertices.push(new VertexNode(points[i]))\n }\n\n this.compute()\n }\n\n return this\n }\n\n setFromObject(object) {\n const points = []\n\n object.updateMatrixWorld(true)\n\n object.traverse(function (node) {\n const geometry = node.geometry\n\n if (geometry !== undefined) {\n const attribute = geometry.attributes.position\n\n if (attribute !== undefined) {\n for (let i = 0, l = attribute.count; i < l; i++) {\n const point = new Vector3()\n\n point.fromBufferAttribute(attribute, i).applyMatrix4(node.matrixWorld)\n\n points.push(point)\n }\n }\n }\n })\n\n return this.setFromPoints(points)\n }\n\n containsPoint(point) {\n const faces = this.faces\n\n for (let i = 0, l = faces.length; i < l; i++) {\n const face = faces[i]\n\n // compute signed distance and check on what half space the point lies\n\n if (face.distanceToPoint(point) > this.tolerance) return false\n }\n\n return true\n }\n\n intersectRay(ray, target) {\n // based on \"Fast Ray-Convex Polyhedron Intersection\" by Eric Haines, GRAPHICS GEMS II\n\n const faces = this.faces\n\n let tNear = -Infinity\n let tFar = Infinity\n\n for (let i = 0, l = faces.length; i < l; i++) {\n const face = faces[i]\n\n // interpret faces as planes for the further computation\n\n const vN = face.distanceToPoint(ray.origin)\n const vD = face.normal.dot(ray.direction)\n\n // if the origin is on the positive side of a plane (so the plane can \"see\" the origin) and\n // the ray is turned away or parallel to the plane, there is no intersection\n\n if (vN > 0 && vD >= 0) return null\n\n // compute the distance from the rays origin to the intersection with the plane\n\n const t = vD !== 0 ? -vN / vD : 0\n\n // only proceed if the distance is positive. a negative distance means the intersection point\n // lies \"behind\" the origin\n\n if (t <= 0) continue\n\n // now categorized plane as front-facing or back-facing\n\n if (vD > 0) {\n // plane faces away from the ray, so this plane is a back-face\n\n tFar = Math.min(t, tFar)\n } else {\n // front-face\n\n tNear = Math.max(t, tNear)\n }\n\n if (tNear > tFar) {\n // if tNear ever is greater than tFar, the ray must miss the convex hull\n\n return null\n }\n }\n\n // evaluate intersection point\n\n // always try tNear first since its the closer intersection point\n\n if (tNear !== -Infinity) {