@jgphilpott/polytree 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{Vector2,Vector3,Mesh,Triangle,Box3,Matrix3,DoubleSide,Ray,Raycaster,BufferGeometry,BufferAttribute}from"three";var BACK,COPLANAR,CSG_Rules,EPSILON,FRONT,Plane,Polygon,Polytree,RAY_EPSILON,SPANNING,Vertex,_asyncUnionArrayID,_asyncUnionID,_matrix3,_normal1,_polygonID,_wP,_wP_EPS_ARR,_wP_EPS_ARR_COUNT,_wV1,_wV2,_wV3,calcWindingNumber_buffer,disposePolytree,edge1,edge2,h,handleIntersectingPolytrees,handleObjectForOp,handleObjectForOp_async,isUniqueTriangle,isValidTriangle,nbuf2,nbuf3,pointRounding,polyInside_WindingNumber_buffer,prepareTriangleBuffer,q,rayIntersectsTriangle,raycastIntersectAscSort,returnXYZ,s,splitPolygonArr,splitPolygonByPlane,tempBox3,tempRay,tempRayDirection,tempRaycaster,tmpm3,triangleVertex0,triangleVertex1,ttvv0,wNPI,EPS2D,constructIntersection,intersectionTestEdge2D,intersectionTestVertex2D,pointInTriangleInclusive2D,pointOnSegmentInclusive2D,pointsEqual2D,resolveCoplanarTriangleIntersection,resolveTriangleIntersection,tempVector1,tempVector2,tempVector3,triangleIntersectionCCW2D,triangleIntersectsTriangle,triangleOrientation2D,trianglesOverlap2D;tempVector1=new Vector3,tempVector2=new Vector3,tempBox3=new Box3,triangleVertex0=new Vector3,triangleVertex1=new Vector3,tempRaycaster=new Raycaster,tempRay=new Ray,tempRayDirection=new Vector3(0,0,1),EPSILON=1e-5,FRONT=1,BACK=2,SPANNING=3,_polygonID=COPLANAR=0,(Polytree=class Polytree{constructor(e,t){this.polygons=[],this.replacedPolygons=[],this.mesh,this.originalMatrixWorld,this.box=e,this.subTrees=[],this.parent=t,this.level=0,this.polygonArrays=void 0,this.addPolygonsArrayToRoot(this.polygons)}clone(){return(new this.constructor).copy(this)}copy(e){var t,r,n,o;for(this.deletePolygonsArrayFromRoot(this.polygons),this.polygons=e.polygons.map(function(e){return e.clone()}),this.addPolygonsArrayToRoot(this.polygons),this.replacedPolygons=e.replacedPolygons.map(function(e){return e.clone()}),e.mesh&&(this.mesh=e.mesh),e.originalMatrixWorld&&(this.originalMatrixWorld=e.originalMatrixWorld.clone()),this.box=e.box.clone(),this.level=e.level,t=r=0,n=e.subTrees.length;0<=n?r<n:n<r;t=0<=n?++r:--r)o=new this.constructor(void 0,this).copy(e.subTrees[t]),this.subTrees.push(o);return this}addPolygonsArrayToRoot(e){return this.parent?this.parent.addPolygonsArrayToRoot(e):(void 0===this.polygonArrays&&(this.polygonArrays=[]),this.polygonArrays.push(e))}deletePolygonsArrayFromRoot(e){return this.parent?this.parent.deletePolygonsArrayFromRoot(e):-1<(e=this.polygonArrays.indexOf(e))?this.polygonArrays.splice(e,1):void 0}isEmpty(){return 0===this.polygons.length}addPolygon(e,t){var r;return this.bounds||(this.bounds=new Box3),r=e.triangle,t&&!isUniqueTriangle(r,t)||(this.bounds.min.x=Math.min(this.bounds.min.x,r.a.x,r.b.x,r.c.x),this.bounds.min.y=Math.min(this.bounds.min.y,r.a.y,r.b.y,r.c.y),this.bounds.min.z=Math.min(this.bounds.min.z,r.a.z,r.b.z,r.c.z),this.bounds.max.x=Math.max(this.bounds.max.x,r.a.x,r.b.x,r.c.x),this.bounds.max.y=Math.max(this.bounds.max.y,r.a.y,r.b.y,r.c.y),this.bounds.max.z=Math.max(this.bounds.max.z,r.a.z,r.b.z,r.c.z),this.polygons.push(e)),this}calcBox(){return this.bounds||(this.bounds=new Box3),this.box=this.bounds.clone(),this.box.min.x-=.01,this.box.min.y-=.01,this.box.min.z-=.01,this}newPolytree(e,t){return new this.constructor(e,t)}split(e){var t,r,n,o,i,s,a,l,c,u,y,h,p,g,d,m,P;if(this.box){for(p=[],n=tempVector2.copy(this.box.max).sub(this.box.min).multiplyScalar(.5),d=i=0;i<=1;d=++i)for(m=s=0;s<=1;m=++s)for(P=a=0;a<=1;P=++a)t=new Box3,g=tempVector1.set(d,m,P),t.min.copy(this.box.min).add(g.multiply(n)),t.max.copy(t.min).add(n),t.expandByScalar(EPSILON),p.push(this.newPolytree(t,this));for(u=void 0;u=this.polygons.pop();){for(r=!1,o=l=0,y=p.length;0<=y?l<y:y<l;o=0<=y?++l:--l)p[o].box.containsPoint(u.getMidpoint())&&(p[o].polygons.push(u),r=!0);if(!r)throw console.error("ERROR: unable to find subtree for:",u.triangle),new Error("Unable to find subtree for triangle at level "+e)}for(o=c=0,h=p.length;0<=h?c<h:h<c;o=0<=h?++c:--c)p[o].level=e+1,p[o].polygons.length>Polytree.polygonsPerTree&&e<Polytree.maxLevel&&p[o].split(e+1),this.subTrees.push(p[o]);return this}}buildTree(){return this.calcBox(),this.split(0),this.processTree(),this}processTree(){var e,t,r,n,o,i;if(!this.isEmpty()){for(tempBox3.copy(this.box),e=t=0,n=this.polygons.length;0<=n?t<n:n<t;e=0<=n?++t:--t)this.box.expandByPoint(this.polygons[e].triangle.a),this.box.expandByPoint(this.polygons[e].triangle.b),this.box.expandByPoint(this.polygons[e].triangle.c);this.expandParentBox()}for(i=[],e=r=0,o=this.subTrees.length;0<=o?r<o:o<r;e=0<=o?++r:--r)i.push(this.subTrees[e].processTree());return i}expandParentBox(){if(this.parent)return this.parent.box.expandByPoint(this.box.min),this.parent.box.expandByPoint(this.box.max),this.parent.expandParentBox()}getPolygonsIntersectingPolygon(e,t=[]){var r,n,o,i,s,a,l,c,u;if(this.box.intersectsTriangle(e.triangle)&&0<this.polygons.length){if(r=this.polygons.slice(),0<this.replacedPolygons.length)for(n=o=0,l=this.replacedPolygons.length;0<=l?o<l:l<o;n=0<=l?++o:--o)r.push(this.replacedPolygons[n]);for(n=i=0,c=r.length;0<=c?i<c:c<i;n=0<=c?++i:--i)(a=r[n]).originalValid&&a.valid&&a.intersects&&triangleIntersectsTriangle(e.triangle,a.triangle)&&t.push(a)}for(n=s=0,u=this.subTrees.length;0<=u?s<u:u<s;n=0<=u?++s:--s)this.subTrees[n].getPolygonsIntersectingPolygon(e,t);return t}getRayPolygons(e,t=[]){var r,n,o,i,s;if(0<this.polygons.length)for(r=n=0,i=this.polygons.length;0<=i?n<i:i<n;r=0<=i?++n:--n)this.polygons[r].valid&&this.polygons[r].originalValid&&-1===t.indexOf(this.polygons[r])&&t.push(this.polygons[r]);for(0<this.replacedPolygons.length&&t.push(...this.replacedPolygons),r=o=0,s=this.subTrees.length;0<=s?o<s:s<o;r=0<=s?++o:--o)e.intersectsBox(this.subTrees[r].box)&&this.subTrees[r].getRayPolygons(e,t);return t}rayIntersect(e,t,r=[]){var n,o,i,s,a,l,c;if(0===e.direction.length())return[];for(n=1e100,o=i=0,l=(a=this.getRayPolygons(e)).length;0<=l?i<l:l<i;o=0<=l?++i:--i)c=void 0,"regular"===Polytree.rayIntersectTriangleType?(c=e.intersectTriangle(a[o].triangle.a,a[o].triangle.b,a[o].triangle.c,!1,tempVector1))&&(tempVector1.applyMatrix4(t),(n=tempVector1.distanceTo(e.origin))<0||1/0<n?console.warn("[rayIntersect] Failed ray distance check",e):r.push({distance:n,polygon:a[o],position:tempVector1.clone()})):(c=rayIntersectsTriangle(e,a[o].triangle,tempVector1))&&(n=(s=c.clone().sub(e.origin).length())<n?s:n)<1e100&&r.push({distance:n,polygon:a[o],position:c.clone().add(e.origin)});return r.length&&r.sort(raycastIntersectAscSort),r}getIntersectingPolygons(i=[]){return this.polygonArrays.forEach(function(e){var t,r,n,o;if(e.length){for(o=[],t=r=0,n=e.length;0<=n?r<n:n<r;t=0<=n?++r:--r)e[t].valid&&e[t].intersects?o.push(i.push(e[t])):o.push(void 0);return o}}),i}getPolygons(i=[]){return this.polygonArrays.forEach(function(e){var t,r,n,o;if(e.length){for(o=[],t=r=0,n=e.length;0<=n?r<n:n<r;t=0<=n?++r:--r)e[t].valid&&-1===i.indexOf(e[t])?o.push(i.push(e[t])):o.push(void 0);return o}}),i}invert(){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.flip()})})}getMesh(){return this.parent?this.parent.getMesh():this.mesh}replacePolygon(e,t){var r,n,o,i,s;for(Array.isArray(t)||(t=[t]),0<this.polygons.length&&-1<(o=this.polygons.indexOf(e))&&(!0===e.originalValid?this.replacedPolygons.push(e):e.setInvalid(),this.polygons.splice(o,1,...t)),s=[],r=n=0,i=this.subTrees.length;0<=i?n<i:i<n;r=0<=i?++n:--n)s.push(this.subTrees[r].replacePolygon(e,t));return s}deletePolygonsByStateRules(p,g=!0){return this.polygonArrays.forEach(function(h){if(h.length)return h.filter(function(e){return!0===e.valid&&!0===e.intersects}).forEach(function(e){for(var t,r,n,o,i,s,a,l,c=!1,u=r=0,y=p.length;0<=y?r<y:y<r;u=0<=y?++r:--r)if(p[u].array){if((a=p[u].rule).includes(e.state)&&("undecided"!==e.previousState&&a.includes(e.previousState)||"undecided"===e.previousState)){for(c=!0,l={},a.forEach(function(e){return l[e]=!1}),a.forEach(function(e){return e,!1}),l[e.state]=!0,t=n=0,i=e.previousStates.length;0<=i?n<i:i<n;t=0<=i?++n:--n){if(!a.includes(e.previousStates[t])){c=!1;break}l[e.previousStates[t]]=!0}if(c){for(s in l)if(!1===l[s]){c=!1;break}if(c)break}}}else if(e.checkAllStates(p[u].rule)){c=!0;break}if(c&&(-1<(o=h.indexOf(e))&&(e.setInvalid(),h.splice(o,1)),g))return e.delete()})})}deletePolygonsByIntersection(n,o=!0){if(void 0!==n)return this.polygonArrays.forEach(function(r){if(r.length)return r.slice().forEach(function(e){var t;if(e.valid&&e.intersects===n&&(-1<(t=r.indexOf(e))&&(e.setInvalid(),r.splice(t,1)),o))return e.delete()})})}isPolygonIntersecting(e){return!!this.box.intersectsTriangle(e.triangle)}markIntesectingPolygons(t){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.intersects=t.isPolygonIntersecting(e)})})}resetPolygons(t=!0){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.reset(t)})})}handleIntersectingPolygons(e,t){var r,n,o,i,s,a,l,c,u,y,h,p,g,d,m,P,f,b,v,V,x,S;if(0<this.polygons.length){for(r=(g=this.polygons.filter(function(e){return!0===e.valid&&!0===e.intersects&&"undecided"===e.state})).pop();r;)if("undecided"===r.state&&r.valid){if(0<(S=e.getPolygonsIntersectingPolygon(r)).length)for(s=a=0,d=S.length;0<=d?a<d:d<a;s=0<=d?++a:--a){if(x=S[s],1<(V=splitPolygonByPlane(r,x.plane)).length){for(n=l=0,m=V.length;0<=m?l<m:m<l;n=0<=m?++l:--l)(p=V[n].polygon).intersects=r.intersects,p.newPolygon=!0,g.push(p);this.replacePolygon(r,V.map(function(e){return e.polygon}));break}if(r.id!==V[0].polygon.id){V[0].polygon.intersects=r.intersects,V[0].polygon.newPolygon=!0,g.push(V[0].polygon),this.replacePolygon(r,V[0].polygon);break}"coplanar-front"!==V[0].type&&"coplanar-back"!==V[0].type||(r.setState(V[0].type),r.coplanar=!0)}r=g.pop()}for(r=(g=this.polygons.filter(function(e){return!0===e.valid&&!0===e.intersects})).pop(),o=!1;r;)if(r.valid){if(o=!1,e.box.containsPoint(r.getMidpoint()))if(!0===Polytree.useWindingNumber)o=polyInside_WindingNumber_buffer(t,r.getMidpoint(),r.coplanar);else if(h=pointRounding(tempVector2.copy(r.getMidpoint())),!0!==Polytree.usePolytreeRay&&e.mesh){if(tempRayDirection.copy(r.plane.normal),tempRaycaster.set(h,tempRayDirection),!(o=(i=tempRaycaster.intersectObject(e.mesh)).length&&0<tempRayDirection.dot(i[0].face.normal)?!0:o)&&r.coplanar)for(s=c=0,P=_wP_EPS_ARR_COUNT;0<=P?c<P:P<c;s=0<=P?++c:--c)if(tempRaycaster.ray.origin.copy(h).add(_wP_EPS_ARR[s]),(i=tempRaycaster.intersectObject(e.mesh)).length&&0<tempRayDirection.dot(i[0].face.normal)){o=!0;break}}else if(tempRay.origin.copy(h),tempRayDirection.copy(r.plane.normal),tempRay.direction.copy(r.plane.normal),!(o=(i=e.rayIntersect(tempRay,e.originalMatrixWorld)).length&&0<tempRayDirection.dot(i[0].polygon.plane.normal)?!0:o)&&r.coplanar)for(s=u=0,f=_wP_EPS_ARR_COUNT;0<=f?u<f:f<u;s=0<=f?++u:--u)if(tempRay.origin.copy(h).add(_wP_EPS_ARR[s]),tempRayDirection.copy(r.plane.normal),tempRay.direction.copy(r.plane.normal),(i=e.rayIntersect(tempRay,e.originalMatrixWorld)).length&&0<tempRayDirection.dot(i[0].polygon.plane.normal)){o=!0;break}!0===o?r.setState("inside"):r.setState("outside"),r=g.pop()}}for(v=[],n=y=0,b=this.subTrees.length;0<=b?y<b:b<y;n=0<=b?++y:--y)v.push(this.subTrees[n].handleIntersectingPolygons(e,t));return v}delete(e=!0){var t,r,n;if(0<this.polygons.length&&e&&(this.polygons.forEach(function(e){return e.delete()}),this.polygons.length=0),0<this.replacedPolygons.length&&e&&(this.replacedPolygons.forEach(function(e){return e.delete()}),this.replacedPolygons.length=0),this.polygonArrays&&(this.polygonArrays.length=0),this.subTrees.length){for(t=r=0,n=this.subTrees.length;0<=n?r<n:n<r;t=0<=n?++r:--r)this.subTrees[t].delete(e);this.subTrees.length=0}return this.mesh=void 0,this.originalMatrixWorld=void 0,this.box=void 0,this.parent=void 0,this.level=void 0}dispose(e=!0){return this.delete(e)}getPolygonCloneCallback(i,s){return this.polygonArrays.forEach(function(e){var t,r,n,o;if(e.length){for(o=[],t=r=0,n=e.length;0<=n?r<n:n<r;t=0<=n?++r:--r)e[t].valid?o.push(i(e[t].clone(),s)):o.push(void 0);return o}})}deleteReplacedPolygons(){var e,t,r,n;for(0<this.replacedPolygons.length&&(this.replacedPolygons.forEach(function(e){return e.delete()}),this.replacedPolygons.length=0),n=[],e=t=0,r=this.subTrees.length;0<=r?t<r:r<t;e=0<=r?++t:--t)n.push(this.subTrees[e].deleteReplacedPolygons());return n}markPolygonsAsOriginal(){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.originalValid=!0})})}applyMatrix(e,t,r=!0){var n,o,i,s,a;if(e.isMesh&&(e.updateMatrix(),e=e.matrix),this.box.makeEmpty(),t=t||tmpm3.getNormalMatrix(e),0<this.polygons.length)for(n=o=0,s=this.polygons.length;0<=s?o<s:s<o;n=0<=s?++o:--o)this.polygons[n].valid&&this.polygons[n].applyMatrix(e,t);for(n=i=0,a=this.subTrees.length;0<=a?i<a:a<i;n=0<=a?++i:--i)this.subTrees[n].applyMatrix(e,t,!1);if(r)return this.processTree()}setPolygonIndex(t){if(void 0!==t)return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.shared=t})})}}).prototype.isPolytree=!0,CSG_Rules={union:{a:[{array:!0,rule:["inside","coplanar-back"]},{array:!(splitPolygonArr=function(e){var t,r,n,o,i=[];if(4<e.length)for(console.warn("[splitPolygonArr] arr.length > 4",e.length),t=r=3,n=e.length;3<=n?r<=n:n<=r;t=3<=n?++r:--r)(o=[]).push(e[0].clone()),o.push(e[t-2].clone()),o.push(e[t-1].clone()),i.push(o);else e[0].pos.distanceTo(e[2].pos)<=e[1].pos.distanceTo(e[3].pos)?i.push([e[0].clone(),e[1].clone(),e[2].clone()],[e[0].clone(),e[2].clone(),e[3].clone()]):i.push([e[0].clone(),e[1].clone(),e[3].clone()],[e[1].clone(),e[2].clone(),e[3].clone()]);return i}),rule:"inside"}],b:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["inside","coplanar-front"]},{array:!(splitPolygonByPlane=function(e,t,r=[]){for(var n,o,i,s,a,l,c,u,y,h,p,g,d,m,P,f,b={polygon:e,type:"undecided"},v=0,V=[],x=a=0,S=e.vertices.length;0<=S?a<S:S<a;x=0<=S?++a:--a)v|=P=(P=t.normal.dot(e.vertices[x].pos)-t.w)<-EPSILON?BACK:EPSILON<P?FRONT:COPLANAR,V.push(P);switch(v){case COPLANAR:b.type=0<t.normal.dot(e.plane.normal)?"coplanar-front":"coplanar-back",r.push(b);break;case FRONT:b.type="front",r.push(b);break;case BACK:b.type="back",r.push(b);break;case SPANNING:for(i=[],n=[],x=l=0,g=e.vertices.length;0<=g?l<g:g<l;x=0<=g?++l:--l)f=V[h=(x+1)%e.vertices.length],o=e.vertices[x],h=e.vertices[h],(s=V[x])!==BACK&&i.push(o),s!==FRONT&&n.push(s!==BACK?o.clone():o),(s|f)===SPANNING&&(s=(t.w-t.normal.dot(o.pos))/t.normal.dot(triangleVertex0.copy(h.pos).sub(o.pos)),f=o.interpolate(h,s),i.push(f),n.push(f.clone()));if(3<=i.length)if(3<i.length)for(p=c=0,d=(y=splitPolygonArr(i)).length;0<=d?c<d:d<c;p=0<=d?++c:--c)r.push({polygon:new Polygon(y[p],e.shared),type:"front"});else r.push({polygon:new Polygon(i,e.shared),type:"front"});if(3<=n.length)if(3<n.length)for(p=u=0,m=(y=splitPolygonArr(n)).length;0<=m?u<m:m<u;p=0<=m?++u:--u)r.push({polygon:new Polygon(y[p],e.shared),type:"back"});else r.push({polygon:new Polygon(n,e.shared),type:"back"})}return 0===r.length&&r.push(b),r}),rule:"inside"}]},subtract:{a:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["inside","coplanar-front"]},{array:!(pointRounding=function(e,t=15){return e.x=+e.x.toFixed(t),e.y=+e.y.toFixed(t),e.z=+e.z.toFixed(t),e}),rule:"inside"}],b:[{array:!0,rule:["outside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["inside","coplanar-front"]},{array:!(raycastIntersectAscSort=function(e,t){return e.distance-t.distance}),rule:"outside"}]},intersect:{a:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["outside","coplanar-back"]},{array:!1,rule:"outside"}],b:[{array:!0,rule:["inside","coplanar-front"]},{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["outside","coplanar-back"]},{array:!1,rule:"outside"}]}},Polytree.union=function(e,t,r=!0){var n,o,i=new Polytree,s=new Set;return e.box.intersectsBox(t.box)?(o=n=void 0,e.mesh&&(n=e.mesh.material.side,e.mesh.material.side=DoubleSide),t.mesh&&(o=t.mesh.material.side,t.mesh.material.side=DoubleSide),e.resetPolygons(!1),t.resetPolygons(!1),e.markIntesectingPolygons(t),t.markIntesectingPolygons(e),handleIntersectingPolytrees(e,t),e.deleteReplacedPolygons(),t.deleteReplacedPolygons(),e.deletePolygonsByStateRules(CSG_Rules.union.a),t.deletePolygonsByStateRules(CSG_Rules.union.b),e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s),e.mesh&&e.mesh.material.side!==n&&(e.mesh.material.side=n),t.mesh&&t.mesh.material.side!==o&&(t.mesh.material.side=o)):(e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s)),s.clear(),s=void 0,i.markPolygonsAsOriginal(),r&&i.buildTree(),i},Polytree.subtract=function(e,t,r=!0){var n,o,i=new Polytree,s=new Set;return e.box.intersectsBox(t.box)?(o=n=void 0,e.mesh&&(n=e.mesh.material.side,e.mesh.material.side=DoubleSide),t.mesh&&(o=t.mesh.material.side,t.mesh.material.side=DoubleSide),e.resetPolygons(!1),t.resetPolygons(!1),e.markIntesectingPolygons(t),t.markIntesectingPolygons(e),handleIntersectingPolytrees(e,t),e.deleteReplacedPolygons(),t.deleteReplacedPolygons(),e.deletePolygonsByStateRules(CSG_Rules.subtract.a),t.deletePolygonsByStateRules(CSG_Rules.subtract.b),t.deletePolygonsByIntersection(!1),t.invert(),e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s),e.mesh&&e.mesh.material.side!==n&&(e.mesh.material.side=n),t.mesh&&t.mesh.material.side!==o&&(t.mesh.material.side=o)):e.getPolygonCloneCallback(i.addPolygon.bind(i),s),s.clear(),s=void 0,i.markPolygonsAsOriginal(),r&&i.buildTree(),i},CSG_Rules={union:{a:[{array:!0,rule:["inside","coplanar-back"]},{array:!(Polytree.intersect=function(e,t,r=!0){var n,o,i=new Polytree,s=new Set;return e.box.intersectsBox(t.box)&&(o=n=void 0,e.mesh&&(n=e.mesh.material.side,e.mesh.material.side=DoubleSide),t.mesh&&(o=t.mesh.material.side,t.mesh.material.side=DoubleSide),e.resetPolygons(!1),t.resetPolygons(!1),e.markIntesectingPolygons(t),t.markIntesectingPolygons(e),handleIntersectingPolytrees(e,t),e.deleteReplacedPolygons(),t.deleteReplacedPolygons(),e.deletePolygonsByStateRules(CSG_Rules.intersect.a),t.deletePolygonsByStateRules(CSG_Rules.intersect.b),e.deletePolygonsByIntersection(!1),t.deletePolygonsByIntersection(!1),e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s),e.mesh&&e.mesh.material.side!==n&&(e.mesh.material.side=n),t.mesh)&&t.mesh.material.side!==o&&(t.mesh.material.side=o),s.clear(),s=void 0,i.markPolygonsAsOriginal(),r&&i.buildTree(),i}),rule:"inside"}],b:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["inside","coplanar-front"]},{array:!1,rule:"inside"}]},subtract:{a:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["inside","coplanar-front"]},{array:!1,rule:"inside"}],b:[{array:!0,rule:["outside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["inside","coplanar-front"]},{array:!1,rule:"outside"}]},intersect:{a:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["outside","coplanar-back"]},{array:!1,rule:"outside"}],b:[{array:!0,rule:["inside","coplanar-front"]},{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["outside","coplanar-back"]},{array:!1,rule:"outside"}]}},Polytree.union=function(e,t,r=!0){var n,o,i=new Polytree,s=new Set;return e.box.intersectsBox(t.box)?(o=n=void 0,e.mesh&&(n=e.mesh.material.side,e.mesh.material.side=DoubleSide),t.mesh&&(o=t.mesh.material.side,t.mesh.material.side=DoubleSide),e.resetPolygons(!1),t.resetPolygons(!1),e.markIntesectingPolygons(t),t.markIntesectingPolygons(e),handleIntersectingPolytrees(e,t),e.deleteReplacedPolygons(),t.deleteReplacedPolygons(),e.deletePolygonsByStateRules(CSG_Rules.union.a),t.deletePolygonsByStateRules(CSG_Rules.union.b),e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s),e.mesh&&e.mesh.material.side!==n&&(e.mesh.material.side=n),t.mesh&&t.mesh.material.side!==o&&(t.mesh.material.side=o)):(e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s)),s.clear(),s=void 0,i.markPolygonsAsOriginal(),r&&i.buildTree(),i},Polytree.subtract=function(e,t,r=!0){var n,o,i=new Polytree,s=new Set;return e.box.intersectsBox(t.box)?(o=n=void 0,e.mesh&&(n=e.mesh.material.side,e.mesh.material.side=DoubleSide),t.mesh&&(o=t.mesh.material.side,t.mesh.material.side=DoubleSide),e.resetPolygons(!1),t.resetPolygons(!1),e.markIntesectingPolygons(t),t.markIntesectingPolygons(e),handleIntersectingPolytrees(e,t),e.deleteReplacedPolygons(),t.deleteReplacedPolygons(),e.deletePolygonsByStateRules(CSG_Rules.subtract.a),t.deletePolygonsByStateRules(CSG_Rules.subtract.b),t.deletePolygonsByIntersection(!1),t.invert(),e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s),e.mesh&&e.mesh.material.side!==n&&(e.mesh.material.side=n),t.mesh&&t.mesh.material.side!==o&&(t.mesh.material.side=o)):e.getPolygonCloneCallback(i.addPolygon.bind(i),s),s.clear(),s=void 0,i.markPolygonsAsOriginal(),r&&i.buildTree(),i},Polytree.intersect=function(e,t,r=!0){var n,o,i=new Polytree,s=new Set;return e.box.intersectsBox(t.box)&&(o=n=void 0,e.mesh&&(n=e.mesh.material.side,e.mesh.material.side=DoubleSide),t.mesh&&(o=t.mesh.material.side,t.mesh.material.side=DoubleSide),e.resetPolygons(!1),t.resetPolygons(!1),e.markIntesectingPolygons(t),t.markIntesectingPolygons(e),handleIntersectingPolytrees(e,t),e.deleteReplacedPolygons(),t.deleteReplacedPolygons(),e.deletePolygonsByStateRules(CSG_Rules.intersect.a),t.deletePolygonsByStateRules(CSG_Rules.intersect.b),e.deletePolygonsByIntersection(!1),t.deletePolygonsByIntersection(!1),e.getPolygonCloneCallback(i.addPolygon.bind(i),s),t.getPolygonCloneCallback(i.addPolygon.bind(i),s),e.mesh&&e.mesh.material.side!==n&&(e.mesh.material.side=n),t.mesh)&&t.mesh.material.side!==o&&(t.mesh.material.side=o),s.clear(),s=void 0,i.markPolygonsAsOriginal(),r&&i.buildTree(),i},Polytree.meshUnion=function(e,t,r){var n=void 0,o=void 0;return r&&Array.isArray(r)?(n=Polytree.fromMesh(e,0),o=Polytree.fromMesh(t,1)):(n=Polytree.fromMesh(e),o=Polytree.fromMesh(t),r=void 0!==r?r:(Array.isArray(e.material)?e.material[0]:e.material).clone()),t=Polytree.union(n,o,!1),e=Polytree.toMesh(t,r),disposePolytree(n,o,t),e},Polytree.meshSubtract=function(e,t,r){var n=void 0,o=void 0;return r&&Array.isArray(r)?(n=Polytree.fromMesh(e,0),o=Polytree.fromMesh(t,1)):(n=Polytree.fromMesh(e),o=Polytree.fromMesh(t),r=void 0!==r?r:(Array.isArray(e.material)?e.material[0]:e.material).clone()),t=Polytree.subtract(n,o,!1),e=Polytree.toMesh(t,r),disposePolytree(n,o,t),e},Polytree.meshIntersect=function(e,t,r){var n=void 0,o=void 0;return r&&Array.isArray(r)?(n=Polytree.fromMesh(e,0),o=Polytree.fromMesh(t,1)):(n=Polytree.fromMesh(e),o=Polytree.fromMesh(t),r=void 0!==r?r:(Array.isArray(e.material)?e.material[0]:e.material).clone()),t=Polytree.intersect(n,o,!1),e=Polytree.toMesh(t,r),disposePolytree(n,o,t),e},Polytree.disposePolytree=!(_asyncUnionArrayID=_asyncUnionID=0),Polytree.async={batchSize:100,union:function(r,n,o=!0){return new Promise(function(e,t){try{return e(Polytree.union(r,n,o)),disposePolytree(r,n)}catch(e){return t(e)}})},subtract:function(r,n,o=!0){return new Promise(function(e,t){try{return e(Polytree.subtract(r,n,o)),disposePolytree(r,n)}catch(e){return t(e)}})},intersect:function(r,n,o=!0){return new Promise(function(e,t){try{return e(Polytree.intersect(r,n,o)),disposePolytree(r,n)}catch(e){return t(e)}})},unionArray:function(b,v=1/0){return new Promise(function(r,n){var e,t,o,i,s,a,l,c,u,y,h,p,g,d,m,P,f;try{if(f=4<Polytree.async.batchSize&&Polytree.async.batchSize<b.length,c=void 0,u=!1,g=[],f){for(t=[],o=0;o<b.length;)t.push(b.slice(o,o+Polytree.async.batchSize)),o+=Polytree.async.batchSize;for(e=t.shift();e;)p=Polytree.async.unionArray(e,0),g.push(p),e=t.shift();u=f=!0,b.length=0}else{for(h=[],i=s=0,d=b.length;0<=d?s<d:d<s;i=0<=d?++s:--s)y=v<i?v:i,P=void 0,b[i].isMesh?P=Polytree.fromMesh(b[i],-1<v?y:void 0):(P=b[i],-1<v&&P.setPolygonIndex(y)),P.polytreeIndex=i,h.push(P);for(c=h.shift(),l=void 0,i=a=0,m=h.length;a<m;i=a+=2){if(h.length<=i+1){l=h[i];break}p=Polytree.async.union(h[i],h[i+1]),g.push(p)}l&&(p=Polytree.async.union(c,l),g.push(p),u=!0)}return Promise.allSettled(g).then(function(e){var t=[];return e.forEach(function(e){if("fulfilled"===e.status)return t.push(e.value)}),u||t.unshift(c),0<t.length?1===t.length?r(t[0]):3<t.length?Polytree.async.unionArray(t,f?0:-1).then(function(e){return r(e)}).catch(function(e){return n(e)}):Polytree.async.union(t[0],t[1]).then(function(e){return 3===t.length?Polytree.async.union(e,t[2]).then(function(e){return r(e)}).catch(function(e){return n(e)}):r(e)}).catch(function(e){return n(e)}):n("Unable to find any result polytree")})}catch(e){return n(e)}})},subtractArray:function(b,v=1/0){return new Promise(function(r,n){var e,t,o,i,s,a,l,c,u,y,h,p,g,d,m,P,f;try{if(f=4<Polytree.async.batchSize&&Polytree.async.batchSize<b.length,c=void 0,u=!1,g=[],f){for(t=[],o=0;o<b.length;)t.push(b.slice(o,o+Polytree.async.batchSize)),o+=Polytree.async.batchSize;for(e=t.shift();e;)p=Polytree.async.subtractArray(e,0),g.push(p),e=t.shift();u=f=!0,b.length=0}else{for(h=[],i=s=0,d=b.length;0<=d?s<d:d<s;i=0<=d?++s:--s)y=v<i?v:i,P=void 0,b[i].isMesh?P=Polytree.fromMesh(b[i],-1<v?y:void 0):(P=b[i],-1<v&&P.setPolygonIndex(y)),P.polytreeIndex=i,h.push(P);for(c=h.shift(),l=void 0,i=a=0,m=h.length;a<m;i=a+=2){if(h.length<=i+1){l=h[i];break}p=Polytree.async.subtract(h[i],h[i+1]),g.push(p)}l&&(p=Polytree.async.subtract(c,l),g.push(p),u=!0)}return Promise.allSettled(g).then(function(e){var t=[];return e.forEach(function(e){if("fulfilled"===e.status)return t.push(e.value)}),u||t.unshift(c),0<t.length?1===t.length?r(t[0]):3<t.length?Polytree.async.subtractArray(t,f?0:-1).then(function(e){return r(e)}).catch(function(e){return n(e)}):Polytree.async.subtract(t[0],t[1]).then(function(e){return 3===t.length?Polytree.async.subtract(e,t[2]).then(function(e){return r(e)}).catch(function(e){return n(e)}):r(e)}).catch(function(e){return n(e)}):n("Unable to find any result polytree")})}catch(e){return n(e)}})},intersectArray:function(b,v=1/0){return new Promise(function(r,n){var e,t,o,i,s,a,l,c,u,y,h,p,g,d,m,P,f;try{if(f=4<Polytree.async.batchSize&&Polytree.async.batchSize<b.length,c=void 0,u=!1,g=[],f){for(t=[],o=0;o<b.length;)t.push(b.slice(o,o+Polytree.async.batchSize)),o+=Polytree.async.batchSize;for(e=t.shift();e;)p=Polytree.async.intersectArray(e,0),g.push(p),e=t.shift();u=f=!0,b.length=0}else{for(h=[],i=s=0,d=b.length;0<=d?s<d:d<s;i=0<=d?++s:--s)y=v<i?v:i,P=void 0,b[i].isMesh?P=Polytree.fromMesh(b[i],-1<v?y:void 0):(P=b[i],-1<v&&P.setPolygonIndex(y)),P.polytreeIndex=i,h.push(P);for(c=h.shift(),l=void 0,i=a=0,m=h.length;a<m;i=a+=2){if(h.length<=i+1){l=h[i];break}p=Polytree.async.intersect(h[i],h[i+1]),g.push(p)}l&&(p=Polytree.async.intersect(c,l),g.push(p),u=!0)}return Promise.allSettled(g).then(function(e){var t=[];return e.forEach(function(e){if("fulfilled"===e.status)return t.push(e.value)}),u||t.unshift(c),0<t.length?1===t.length?r(t[0]):3<t.length?Polytree.async.intersectArray(t,f?0:-1).then(function(e){return r(e)}).catch(function(e){return n(e)}):Polytree.async.intersect(t[0],t[1]).then(function(e){return 3===t.length?Polytree.async.intersect(e,t[2]).then(function(e){return r(e)}).catch(function(e){return n(e)}):r(e)}).catch(function(e){return n(e)}):n("Unable to find any result polytree")})}catch(e){return n(e)}})},operation:function(a,l=!1,c=!0,u={objCounter:0},y=!0){return new Promise(function(r,n){var o,i,s,e,t;try{return o=s=i=void 0,a.material&&(o=a.material),t=[],a.objA&&(e=handleObjectForOp_async(a.objA,l,c,u,0),t.push(e)),a.objB&&(e=handleObjectForOp_async(a.objB,l,c,u,1),t.push(e)),Promise.allSettled(t).then(function(e){var t;switch(e.forEach(function(e){if("fulfilled"===e.status)return 0===e.value.objIndex?i=e.value:1===e.value.objIndex?s=e.value:void 0}),!0===l&&(a.objA=i.original,i=i.result,a.objB=s.original,s=s.result),t=void 0,a.op){case"union":t=Polytree.async.union(i,s,c);break;case"subtract":t=Polytree.async.subtract(i,s,c);break;case"intersect":t=Polytree.async.intersect(i,s,c)}return t.then(function(e){var t;if(y&&o?(t=Polytree.toMesh(e,o),l||disposePolytree(e),r(l?{result:t,operationTree:a}:t)):r(y&&l?{result:e,operationTree:a}:e),!l)return disposePolytree(i,s)}).catch(function(e){return n(e)})})}catch(e){return n(e)}})}},Polytree.unionArray=function(e,t=1/0){for(var r,n,o,i,s,a,l=[],c=r=0,u=e.length;0<=u?r<u:u<r;c=0<=u?++r:--r)n=t<c?t:c,a=void 0,e[c].isMesh?a=Polytree.fromMesh(e[c],n):(a=e[c]).setPolygonIndex(n),a.polytreeIndex=c,l.push(a);for(o=l.shift(),i=l.shift();o&&i;)s=Polytree.union(o,i),disposePolytree(o,i),o=s,i=l.shift();return o},Polytree.subtractArray=function(e,t=1/0){for(var r,n,o,i,s,a,l=[],c=r=0,u=e.length;0<=u?r<u:u<r;c=0<=u?++r:--r)n=t<c?t:c,a=void 0,e[c].isMesh?a=Polytree.fromMesh(e[c],n):(a=e[c]).setPolygonIndex(n),a.polytreeIndex=c,l.push(a);for(o=l.shift(),i=l.shift();o&&i;)s=Polytree.subtract(o,i),disposePolytree(o,i),o=s,i=l.shift();return o},Polytree.intersectArray=function(e,t=1/0){for(var r,n,o,i,s,a,l=[],c=r=0,u=e.length;0<=u?r<u:u<r;c=0<=u?++r:--r)n=t<c?t:c,a=void 0,e[c].isMesh?a=Polytree.fromMesh(e[c],n):(a=e[c]).setPolygonIndex(n),a.polytreeIndex=c,l.push(a);for(o=l.shift(),i=l.shift();o&&i;)s=Polytree.intersect(o,i),disposePolytree(o,i),o=s,i=l.shift();return o},Polytree.operation=function(e,t=!1,r=!0,n={objCounter:0},o=!0){var i=void 0,s=void 0,a=void 0,l=void 0;switch(e.material&&(l=e.material),e.objA&&(i=handleObjectForOp(e.objA,t,r,n),!0===t)&&(e.objA=i.original,i=i.result),e.objB&&(s=handleObjectForOp(e.objB,t,r,n),!0===t)&&(e.objB=s.original,s=s.result),e.op){case"union":a=Polytree.union(i,s,r);break;case"subtract":a=Polytree.subtract(i,s,r);break;case"intersect":a=Polytree.intersect(i,s,r)}return t||disposePolytree(i,s),o&&l?(n=Polytree.toMesh(a,l),disposePolytree(a),t?{result:n,operationTree:e}:n):o&&t?{result:a,operationTree:e}:a},handleObjectForOp=function(e,t,r,n){var o=void 0;return e.isMesh?(o=Polytree.fromMesh(e,n.objCounter++),t&&(o={result:o,original:o.clone()})):e.isPolytree?(o=e,t&&(o={result:e,original:e.clone()})):e.op&&(o=Polytree.operation(e,t,r,n,!1),t)&&(o={result:o,original:e}),o},handleObjectForOp_async=function(n,o,i,s,a){return new Promise(function(t,r){var e;try{return e=void 0,n.isMesh?(e=Polytree.fromMesh(n,s.objCounter++),(e=o?{result:e,original:e.clone()}:e).objIndex=a,t(e)):n.isPolytree?(e=n,(e=o?{result:n,original:n.clone()}:e).objIndex=a,t(e)):n.op?Polytree.async.operation(n,o,i,s,!1).then(function(e){return(e=o?{result:e,original:n}:e).objIndex=a,t(e)}):void 0}catch(e){return r(e)}})},isUniqueTriangle=function(e,t,r){var n=`{${e.a.x},${e.a.y},${e.a.z}}-{${e.b.x},${e.b.y},${e.b.z}}-{${e.c.x},${e.c.y},${e.c.z}}`;return!0!==t.has(n)&&(t.add(n),r&&r.set(e,e),!0)},nbuf3=function(e){return{top:0,array:new Float32Array(e),write:function(e){return this.array[this.top++]=e.x,this.array[this.top++]=e.y,this.array[this.top++]=e.z}}},nbuf2=function(e){return{top:0,array:new Float32Array(e),write:function(e){return this.array[this.top++]=e.x,this.array[this.top++]=e.y}}},_normal1=new Vector3,tmpm3=new Matrix3,ttvv0=new Vector3,Polytree.toGeometry=function(e){for(var t,r,n,o,i,s,a,l,c,u,y,h=e.getPolygons(),p=h.length,g=nbuf3(3*p*3),d=nbuf3(3*p*3),m=void 0,P=void 0,f=[],b=[],v=0,V=h.length;v<V;v++)for(y=(u=(a=h[v]).vertices).length,void 0===a.shared||f[a.shared]||(f[a.shared]=[]),0<y&&(void 0!==u[0].uv&&(m=m||nbuf2(2*p*3)),void 0!==u[0].color)&&(P=P||nbuf3(3*p*3)),n=i=3,l=y;3<=l?i<=l:l<=i;n=3<=l?++i:--i)(void 0===a.shared?b:f[a.shared]).push(g.top/3,g.top/3+1,g.top/3+2),g.write(u[0].pos),g.write(u[n-2].pos),g.write(u[n-1].pos),d.write(u[0].normal),d.write(u[n-2].normal),d.write(u[n-1].normal),null!=m&&(m.write(u[0].uv),m.write(u[n-2].uv),m.write(u[n-1].uv)),null!=P&&(P.write(u[0].color),P.write(u[n-2].color),P.write(u[n-1].color));if((t=new BufferGeometry).setAttribute("position",new BufferAttribute(g.array,3)),t.setAttribute("normal",new BufferAttribute(d.array,3)),m&&t.setAttribute("uv",new BufferAttribute(m.array,2)),P&&t.setAttribute("color",new BufferAttribute(P.array,3)),0<f.length){for(o=[],n=s=r=0,c=f.length;0<=c?s<c:c<s;n=0<=c?++s:--s)f[n]=f[n]||[],t.addGroup(r,f[n].length,n),r+=f[n].length,o=o.concat(f[n]);b.length&&(t.addGroup(r,b.length,f.length),o=o.concat(b)),t.setIndex(o)}return t},Polytree.toMesh=function(e,t){e=Polytree.toGeometry(e);return new Mesh(e,t)},Polytree.fromMesh=function(e,t,r=new Polytree,n=!0){var o,i,s,a,l,c,u,y,h,p,g,d,m,P,f,b,v,V,x,S,I,w,D,E;if(e.isPolytree)return e;for("regular"===Polytree.rayIntersectTriangleType&&(r.originalMatrixWorld=e.matrixWorld.clone()),e.updateWorldMatrix(!0,!0),i=e.geometry,tmpm3.getNormalMatrix(e.matrix),v=i.attributes.position,m=i.attributes.normal,w=i.attributes.uv,o=i.attributes.color,a=i.groups,f=[],l=y=0,V=(c=i.index?i.index.array:Array(v.array.length/v.itemSize|0).fill().map(function(e,t){return t})).length;y<V;l=y+=3){for(E=[],u=h=0;h<3;u=++h)I=2*(D=c[l+u]),b=new Vector3(v.array[D=3*D],v.array[1+D],v.array[2+D]),D=new Vector3(m.array[D],m.array[1+D],m.array[2+D]),b.applyMatrix4(e.matrix),D.applyMatrix3(tmpm3),S=w?{x:w.array[I],y:w.array[1+I]}:void 0,I=o?{x:o.array[I],y:o.array[1+I],z:o.array[2+I]}:void 0,E.push(new Vertex(b,D,S,I));if(void 0===t&&a&&0<a.length){for(P=void 0,g=0,p=a.length;g<p;g++)s=a[g],c[l]>=s.start&&c[l]<s.start+s.count&&((P=new Polygon(E,s.materialIndex)).originalValid=!0);P&&f.push(P)}else(P=new Polygon(E,t)).originalValid=!0,f.push(P)}for(l=d=0,x=f.length;0<=x?d<x:x<d;l=0<=x?++d:--d)isValidTriangle(f[l].triangle)?r.addPolygon(f[l]):f[l].delete();return n&&r.buildTree(),!0!==Polytree.usePolytreeRay&&(r.mesh=e),r},isValidTriangle=function(e){return!e.a.equals(e.b)&&!e.a.equals(e.c)&&!e.b.equals(e.c)},Vertex=class Vertex{constructor(e,t,r,n){this.pos=(new Vector3).copy(e),this.normal=(new Vector3).copy(t),r&&(this.uv=(new Vector2).copy(r)),n&&(this.color=(new Vector3).copy(n))}clone(){return new Vertex(this.pos.clone(),this.normal.clone(),this.uv&&this.uv.clone(),this.color&&this.color.clone())}flip(){return this.normal.negate()}delete(){return this.pos=void 0,this.normal=void 0,this.uv&&(this.uv=void 0),this.color&&(this.color=void 0)}interpolate(e,t){return new Vertex(this.pos.clone().lerp(e.pos,t),this.normal.clone().lerp(e.normal,t),this.uv&&e.uv&&this.uv.clone().lerp(e.uv,t),this.color&&e.color&&this.color.clone().lerp(e.color,t))}},(Plane=class Plane{constructor(e,t){this.normal=e,this.w=t}clone(){return new Plane(this.normal.clone(),this.w)}flip(){return this.normal.negate(),this.w=-this.w}delete(){return this.normal=void 0,this.w=void 0}equals(e){return this.normal.equals(e.normal)&&this.w===e.w}}).fromPoints=function(e,t,r){t=triangleVertex0.copy(t).sub(e).cross(triangleVertex1.copy(r).sub(e)).normalize().clone();return new Plane(t,t.dot(e))},Polygon=class Polygon{constructor(e,t){this.id=_polygonID++,this.vertices=e.map(function(e){return e.clone()}),this.shared=t,this.plane=Plane.fromPoints(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.triangle=new Triangle(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.intersects=!1,this.state="undecided",this.previousState="undecided",this.previousStates=[],this.valid=!0,this.coplanar=!1,this.originalValid=!1,this.newPolygon=!1}getMidpoint(){return this.triangle.midPoint||(this.triangle.midPoint=this.triangle.getMidpoint(new Vector3))}applyMatrix(t,r){if(r=r||tmpm3.getNormalMatrix(t),this.vertices.forEach(function(e){return e.pos.applyMatrix4(t),e.normal.applyMatrix3(r)}),this.plane.delete(),this.plane=Plane.fromPoints(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.triangle.set(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.triangle.midPoint)return this.triangle.getMidpoint(this.triangle.midPoint)}reset(e=!0){return this.intersects=!1,this.state="undecided",this.previousState="undecided",this.previousStates.length=0,this.valid=!0,this.coplanar=!1,e&&(this.originalValid=!1),this.newPolygon=!1}setState(e,t){if(this.state!==t)return this.previousState=this.state,"undecided"!==this.state&&this.previousStates.push(this.state),this.state=e}checkAllStates(e){var t,r,n;if(this.state!==e||this.previousState!==e&&"undecided"!==this.previousState)return!1;for(t=0,r=(n=this.previousStates).length;t<r;t++)if(n[t]!==e)return!1;return!0}setInvalid(){return this.valid=!1}setValid(){return this.valid=!0}clone(){var e=new Polygon(this.vertices.map(function(e){return e.clone()}),this.shared);return e.intersects=this.intersects,e.valid=this.valid,e.coplanar=this.coplanar,e.state=this.state,e.originalValid=this.originalValid,e.newPolygon=this.newPolygon,e.previousState=this.previousState,e.previousStates=this.previousStates.slice(),this.triangle.midPoint&&(e.triangle.midPoint=this.triangle.midPoint.clone()),e}flip(){var e;return this.vertices.reverse().forEach(function(e){return e.flip()}),e=this.triangle.a,this.triangle.a=this.triangle.c,this.triangle.c=e,this.plane.flip()}delete(){return this.vertices.forEach(function(e){return e.delete()}),this.vertices.length=0,this.plane&&(this.plane.delete(),this.plane=void 0),this.triangle=void 0,this.shared=void 0,this.setInvalid()}},disposePolytree=function(...e){if(Polytree.disposePolytree)return e.forEach(function(e){return e.delete()})},_wV1=new Vector3,_wV2=new Vector3,_wV3=new Vector3,_wP=new Vector3,_wP_EPS_ARR=[new Vector3(EPSILON,0,0),new Vector3(0,EPSILON,0),new Vector3(0,0,EPSILON),new Vector3(-EPSILON,0,0),new Vector3(0,-EPSILON,0),new Vector3(0,0,-EPSILON)],_wP_EPS_ARR_COUNT=_wP_EPS_ARR.length,_matrix3=new Matrix3,wNPI=4*Math.PI,returnXYZ=function(e,t){return{x:e[t],y:e[t+1],z:e[t+2]}},calcWindingNumber_buffer=function(e,t){for(var r,n,o,i,s=0,a=r=0,l=e.length;r<l;a=r+=9)_wV1.subVectors(returnXYZ(e,a),t),_wV2.subVectors(returnXYZ(e,a+3),t),_wV3.subVectors(returnXYZ(e,a+6),t),n=_wV1.length(),o=_wV2.length(),i=_wV3.length(),_matrix3.set(_wV1.x,_wV1.y,_wV1.z,_wV2.x,_wV2.y,_wV2.z,_wV3.x,_wV3.y,_wV3.z),s+=2*Math.atan2(_matrix3.determinant(),n*o*i+_wV1.dot(_wV2)*i+_wV2.dot(_wV3)*n+_wV3.dot(_wV1)*o);return s=Math.round(s/wNPI)},polyInside_WindingNumber_buffer=function(e,t,r){var n,o,i,s=!1;if(_wP.copy(t),0===calcWindingNumber_buffer(e,_wP)){if(r)for(n=o=0,i=_wP_EPS_ARR_COUNT;0<=i?o<i:i<o;n=0<=i?++o:--o)if(_wP.copy(t).add(_wP_EPS_ARR[n]),0!==calcWindingNumber_buffer(e,_wP)){s=!0;break}}else s=!0;return s},handleIntersectingPolytrees=function(e,t,r=!0){var n=void 0,o=void 0;!0===Polytree.useWindingNumber&&(r&&(n=prepareTriangleBuffer(e.getPolygons())),o=prepareTriangleBuffer(t.getPolygons())),e.handleIntersectingPolygons(t,o),r&&t.handleIntersectingPolygons(e,n),void 0!==n&&(n=void 0)},prepareTriangleBuffer=function(e){for(var t,r,n=e.length,o=new Float32Array(3*n*3),i=0,s=t=0,a=n;0<=a?t<a:a<t;s=0<=a?++t:--t)r=e[s].triangle,o[i++]=r.a.x,o[i++]=r.a.y,o[i++]=r.a.z,o[i++]=r.b.x,o[i++]=r.b.y,o[i++]=r.b.z,o[i++]=r.c.x,o[i++]=r.c.y,o[i++]=r.c.z;return o},edge1=new Vector3,edge2=new Vector3,h=new Vector3,s=new Vector3,q=new Vector3,RAY_EPSILON=1e-7,rayIntersectsTriangle=function(e,t,r=new Vector3){var n,o;return edge1.subVectors(t.b,t.a),edge2.subVectors(t.c,t.a),h.crossVectors(e.direction,edge2),n=edge1.dot(h),!(-RAY_EPSILON<n&&n<RAY_EPSILON||(n=1/n,s.subVectors(e.origin,t.a),(t=n*s.dot(h))<0)||1<t||(q.crossVectors(s,edge1),(o=n*e.direction.dot(q))<0)||1<t+o||(t=n*edge2.dot(q),!(RAY_EPSILON<t)))?r.copy(e.direction).multiplyScalar(t).add(e.origin):null},Polytree.rayIntersectsTriangle=rayIntersectsTriangle,Polytree.usePolytreeRay=!0,Polytree.useWindingNumber=!1,Polytree.rayIntersectTriangleType="MollerTrumbore",Polytree.maxLevel=16,Polytree.polygonsPerTree=100,EPS2D=1e-10,tempVector1=new Vector3,tempVector2=new Vector3,tempVector3=new Vector3,pointsEqual2D=function(e,t,r=EPS2D){return Math.abs(e.x-t.x)<=r&&Math.abs(e.y-t.y)<=r},pointOnSegmentInclusive2D=function(e,t,r,n=EPS2D){var o,i,s;return pointsEqual2D(t,r,n)?pointsEqual2D(e,t,n)||pointsEqual2D(e,r,n):!(Math.abs(triangleOrientation2D(e,t,r))>n)&&(i=Math.min(t.x,r.x)-n,o=Math.max(t.x,r.x)+n,s=Math.min(t.y,r.y)-n,t=Math.max(t.y,r.y)+n,e.x>=i)&&e.x<=o&&e.y>=s&&e.y<=t},pointInTriangleInclusive2D=function(e,t,r,n,o=EPS2D){var i,s,a,l=triangleOrientation2D(t,r,n);return o<l?(i=triangleOrientation2D(e,t,r),s=triangleOrientation2D(e,r,n),a=triangleOrientation2D(e,n,t),-o<=i&&-o<=s&&-o<=a):l<-o?(i=triangleOrientation2D(e,t,r),s=triangleOrientation2D(e,r,n),a=triangleOrientation2D(e,n,t),i<=o&&s<=o&&a<=o):pointsEqual2D(t,r,o)&&pointsEqual2D(r,n,o)?pointsEqual2D(e,t,o):pointsEqual2D(t,r,o)?pointOnSegmentInclusive2D(e,r,n,o):pointsEqual2D(r,n,o)||pointsEqual2D(n,t,o)?pointOnSegmentInclusive2D(e,t,r,o):pointOnSegmentInclusive2D(e,t,r,o)||pointOnSegmentInclusive2D(e,r,n,o)||pointOnSegmentInclusive2D(e,n,t,o)},triangleIntersectsTriangle=function(e,t,r={coplanar:!1,source:new Vector3,target:new Vector3}){var n,o,i,s,a,l,c,u,y=e.a,h=e.b,e=e.c,p=t.a,g=t.b,t=t.c;return tempVector1.copy(p).sub(t),tempVector2.copy(g).sub(t),u=(new Vector3).copy(tempVector1).cross(tempVector2),tempVector1.copy(y).sub(t),n=tempVector1.dot(u),tempVector1.copy(h).sub(t),i=tempVector1.dot(u),tempVector1.copy(e).sub(t),a=tempVector1.dot(u),!(0<n*i&&0<n*a||(tempVector1.copy(h).sub(y),tempVector2.copy(e).sub(y),c=(new Vector3).copy(tempVector1).cross(tempVector2),tempVector1.copy(p).sub(e),o=tempVector1.dot(c),tempVector1.copy(g).sub(e),s=tempVector1.dot(c),tempVector1.copy(t).sub(e),l=tempVector1.dot(c),0<o*s&&0<o*l))&&(r.normal1=c,r.normal2=u,0<n?0<i?resolveTriangleIntersection(e,y,h,p,t,g,o,l,s,r):0<a?resolveTriangleIntersection(h,e,y,p,t,g,o,l,s,r):resolveTriangleIntersection(y,h,e,p,g,t,o,s,l,r):n<0?i<0?resolveTriangleIntersection(e,y,h,p,g,t,o,s,l,r):a<0?resolveTriangleIntersection(h,e,y,p,g,t,o,s,l,r):resolveTriangleIntersection(y,h,e,p,t,g,o,l,s,r):i<0?0<=a?resolveTriangleIntersection(h,e,y,p,t,g,o,l,s,r):resolveTriangleIntersection(y,h,e,p,g,t,o,s,l,r):0<i?0<a?resolveTriangleIntersection(y,h,e,p,t,g,o,l,s,r):resolveTriangleIntersection(h,e,y,p,g,t,o,s,l,r):0<a?resolveTriangleIntersection(e,y,h,p,g,t,o,s,l,r):a<0?resolveTriangleIntersection(e,y,h,p,t,g,o,l,s,r):(r.coplanar=!0,resolveCoplanarTriangleIntersection(y,h,e,p,g,t,c,u)))},resolveTriangleIntersection=function(e,t,r,n,o,i,s,a,l,c){return!(0<s&&0<a&&0<l||s<0&&a<0&&l<0)&&(0<s?0<a?constructIntersection(e,r,t,i,n,o,c):0<l?constructIntersection(e,r,t,o,i,n,c):constructIntersection(e,t,r,n,o,i,c):s<0?a<0?constructIntersection(e,t,r,i,n,o,c):l<0?constructIntersection(e,t,r,o,i,n,c):constructIntersection(e,r,t,n,o,i,c):a<0?0<=l?constructIntersection(e,r,t,o,i,n,c):constructIntersection(e,t,r,n,o,i,c):0<a?0<l?constructIntersection(e,r,t,n,o,i,c):constructIntersection(e,t,r,o,i,n,c):0<l?constructIntersection(e,t,r,i,n,o,c):l<0?constructIntersection(e,r,t,i,n,o,c):(c.coplanar=!0,resolveCoplanarTriangleIntersection(e,t,r,n,o,i,c.normal1,c.normal2)))},resolveCoplanarTriangleIntersection=function(e,t,r,n,o,i,s,a){var l=new Vector2,c=new Vector2,u=new Vector2,y=new Vector2,h=new Vector2,p=new Vector2,g=Math.abs(s.x),d=Math.abs(s.y),s=Math.abs(s.z);return s<g&&d<=g?(l.set(e.y,e.z),c.set(t.y,t.z),u.set(r.y,r.z),y.set(n.y,n.z),h.set(o.y,o.z),p.set(i.y,i.z)):s<d&&g<=d?(l.set(e.x,e.z),c.set(t.x,t.z),u.set(r.x,r.z),y.set(n.x,n.z),h.set(o.x,o.z),p.set(i.x,i.z)):(l.set(e.x,e.y),c.set(t.x,t.y),u.set(r.x,r.y),y.set(n.x,n.y),h.set(o.x,o.y),p.set(i.x,i.y)),trianglesOverlap2D(l,c,u,y,h,p)},trianglesOverlap2D=function(e,t,r,n,o,i){var s,a,l,c,u,y,h,p,g,d,m,P,f,b,v;return pointsEqual2D(n,o,EPS2D)&&pointsEqual2D(o,i,EPS2D)?pointInTriangleInclusive2D(n,e,t,r,EPS2D):pointsEqual2D(e,t,EPS2D)&&pointsEqual2D(t,r,EPS2D)?pointInTriangleInclusive2D(e,n,o,i,EPS2D):(y=function(e,t,r){return pointsEqual2D(e,t,EPS2D)?[t,r]:(pointsEqual2D(t,r,EPS2D),[e,t])},(h=function(e,t,r){return pointsEqual2D(e,t,EPS2D)&&!pointsEqual2D(t,r,EPS2D)||pointsEqual2D(t,r,EPS2D)&&!pointsEqual2D(e,t,EPS2D)||pointsEqual2D(r,e,EPS2D)&&!pointsEqual2D(e,t,EPS2D)})(e,t,r)&&h(n,o,i)?([h,s]=y(e,t,r),[y,c]=y(n,o,i),m=Math.min(h.x,s.x),v=Math.max(h.x,s.x),P=Math.min(h.y,s.y),p=Math.max(h.y,s.y),f=Math.min(y.x,c.x),g=Math.max(y.x,c.x),b=Math.min(y.y,c.y),d=Math.max(y.y,c.y),!(v<f-EPS2D||g<m-EPS2D||p<b-EPS2D||d<P-EPS2D||Math.abs(triangleOrientation2D(h,s,y))>EPS2D||(v=Math.abs(s.x-h.x),f=Math.abs(s.y-h.y)<=v?(l=Math.min(h.x,s.x)-EPS2D,a=Math.max(h.x,s.x)+EPS2D,u=Math.min(y.x,c.x)-EPS2D,Math.max(y.x,c.x)+EPS2D):(l=Math.min(h.y,s.y)-EPS2D,a=Math.max(h.y,s.y)+EPS2D,u=Math.min(y.y,c.y)-EPS2D,Math.max(y.y,c.y)+EPS2D),a<u)||f<l)):triangleOrientation2D(e,t,r)<0?triangleOrientation2D(n,o,i)<0?triangleIntersectionCCW2D(e,r,t,n,i,o):triangleIntersectionCCW2D(e,r,t,n,o,i):triangleOrientation2D(n,o,i)<0?triangleIntersectionCCW2D(e,t,r,n,i,o):triangleIntersectionCCW2D(e,t,r,n,o,i))},triangleOrientation2D=function(e,t,r){return(e.x-r.x)*(t.y-r.y)-(e.y-r.y)*(t.x-r.x)},triangleIntersectionCCW2D=function(e,t,r,n,o,i){return 0<=triangleOrientation2D(n,o,e)?0<=triangleOrientation2D(o,i,e)?0<=triangleOrientation2D(i,n,e)||intersectionTestEdge2D(e,t,r,n,o,i):0<=triangleOrientation2D(i,n,e)?intersectionTestEdge2D(e,t,r,i,n,o):intersectionTestVertex2D(e,t,r,n,o,i):0<=triangleOrientation2D(o,i,e)?(0<=triangleOrientation2D(i,n,e)?intersectionTestEdge2D:intersectionTestVertex2D)(e,t,r,o,i,n):intersectionTestVertex2D(e,t,r,i,n,o)},intersectionTestEdge2D=function(e,t,r,n,o,i){for(var s,a,l,c,u,y,h=function(e,t,r,n){var o=Math.min(e.x,t.x),i=Math.max(e.x,t.x),s=Math.min(e.y,t.y),a=Math.max(e.y,t.y),l=Math.min(r.x,n.x),c=Math.max(r.x,n.x),u=Math.min(r.y,n.y),y=Math.max(r.y,n.y);return!(i<l||c<o||a<u||y<s)&&(i=triangleOrientation2D(e,t,r),l=triangleOrientation2D(e,t,n),c=triangleOrientation2D(r,n,e),o=triangleOrientation2D(r,n,t),(0<i&&l<0||i<0&&0<l)&&(0<c&&o<0||c<0&&0<o)||!!(Math.abs(i)<=EPS2D&&pointOnSegmentInclusive2D(r,e,t,EPS2D)||Math.abs(l)<=EPS2D&&pointOnSegmentInclusive2D(n,e,t,EPS2D)||Math.abs(c)<=EPS2D&&pointOnSegmentInclusive2D(e,r,n,EPS2D)||Math.abs(o)<=EPS2D&&pointOnSegmentInclusive2D(t,r,n,EPS2D)))},p=[[e,t],[t,r],[r,e]],g=[[n,o],[o,i],[i,n]],d=0,m=p.length;d<m;d++)for([s,a]=p[d],u=0,y=g.length;u<y;u++)if([l,c]=g[u],h(s,a,l,c))return!0;return!1},intersectionTestVertex2D=function(e,t,r,n,o,i){return!!pointInTriangleInclusive2D(n,e,t,r,EPS2D)||!!pointInTriangleInclusive2D(o,e,t,r,EPS2D)||!!(pointInTriangleInclusive2D(i,e,t,r,EPS2D)||pointInTriangleInclusive2D(e,n,o,i,EPS2D)||pointInTriangleInclusive2D(t,n,o,i,EPS2D)||pointInTriangleInclusive2D(r,n,o,i,EPS2D))},constructIntersection=function(e,t,r,n,o,i,s){var a=void 0,l=new Vector3;return tempVector1.subVectors(t,e),tempVector2.subVectors(i,e),l.copy(tempVector1).cross(tempVector2),tempVector3.subVectors(n,e),0<tempVector3.dot(l)?(tempVector1.subVectors(r,e),l.copy(tempVector1).cross(tempVector2),tempVector3.dot(l)<=0&&(tempVector2.subVectors(o,e),l.copy(tempVector1).cross(tempVector2),0<tempVector3.dot(l)?(tempVector1.subVectors(e,n),tempVector2.subVectors(e,r),a=tempVector1.dot(s.normal2)/tempVector2.dot(s.normal2),!!isFinite(a)&&(tempVector1.copy(tempVector2).multiplyScalar(a),s.source.subVectors(e,tempVector1),tempVector1.subVectors(n,e),tempVector2.subVectors(n,i),a=tempVector1.dot(s.normal1)/tempVector2.dot(s.normal1),!!isFinite(a))&&(tempVector1.copy(tempVector2).multiplyScalar(a),s.target.subVectors(n,tempVector1),!0)):(tempVector1.subVectors(n,e),tempVector2.subVectors(n,o),a=tempVector1.dot(s.normal1)/tempVector2.dot(s.normal1),!!isFinite(a)&&(tempVector1.copy(tempVector2).multiplyScalar(a),s.source.subVectors(n,tempVector1),tempVector1.subVectors(n,e),tempVector2.subVectors(n,i),a=tempVector1.dot(s.normal1)/tempVector2.dot(s.normal1),!!isFinite(a))&&(tempVector1.copy(tempVector2).multiplyScalar(a),s.target.subVectors(n,tempVector1),!0)))):(tempVector2.subVectors(o,e),l.copy(tempVector1).cross(tempVector2),!(tempVector3.dot(l)<0||(tempVector1.subVectors(r,e),l.copy(tempVector1).cross(tempVector2),tempVector3.dot(l)<0?(tempVector1.subVectors(n,e),tempVector2.subVectors(n,o),a=tempVector1.dot(s.normal1)/tempVector2.dot(s.normal1),!isFinite(a)||(tempVector1.copy(tempVector2).multiplyScalar(a),s.source.subVectors(n,tempVector1),tempVector1.subVectors(n,e),tempVector2.subVectors(n,i),a=tempVector1.dot(s.normal1)/tempVector2.dot(s.normal1),!isFinite(a))||(tempVector1.copy(tempVector2).multiplyScalar(a),s.target.subVectors(n,tempVector1),0)):(tempVector1.subVectors(e,n),tempVector2.subVectors(e,r),a=tempVector1.dot(s.normal2)/tempVector2.dot(s.normal2),!isFinite(a)||(tempVector1.copy(tempVector2).multiplyScalar(a),s.source.subVectors(e,tempVector1),tempVector1.subVectors(e,n),tempVector2.subVectors(e,t),a=tempVector1.dot(s.normal2)/tempVector2.dot(s.normal2),!isFinite(a))||(tempVector1.copy(tempVector2).multiplyScalar(a),s.target.subVectors(e,tempVector1),0)))))};export default Polytree;export{Polytree,Plane,Vertex,Polygon,rayIntersectsTriangle,triangleIntersectsTriangle,resolveTriangleIntersection,resolveCoplanarTriangleIntersection,trianglesOverlap2D,triangleOrientation2D,triangleIntersectionCCW2D,intersectionTestEdge2D,intersectionTestVertex2D,constructIntersection};
1
+ import{Vector2,Vector3,Box3,Line3,Sphere,Matrix3,Triangle,Ray,Raycaster,Mesh,DoubleSide,BufferGeometry,BufferAttribute,Plane as ThreePlane}from"three";var ASYNC_OPERATION_TIMEOUT,DEBUG_COLOR_BACK,DEBUG_COLOR_COPLANAR,DEBUG_COLOR_FRONT,DEBUG_COLOR_SPANNING,DEBUG_GEOMETRY_VALIDATION,DEBUG_INTERSECTION_VERIFICATION,DEBUG_PERFORMANCE_TIMING,DEBUG_VERBOSE_LOGGING,DEFAULT_BUFFER_SIZE,DEFAULT_COORDINATE_PRECISION,DEFAULT_MATERIAL_INDEX,GARBAGE_COLLECTION_THRESHOLD,GEOMETRY_CACHE_SIZE,INTERSECTION_CACHE_SIZE,MAX_REFINEMENT_ITERATIONS,MAX_WORKER_THREADS,MEMORY_USAGE_WARNING_LIMIT,POINT_COINCIDENCE_THRESHOLD,POLYTREE_MAX_DEPTH,POLYTREE_MAX_POLYGONS_PER_NODE,POLYTREE_MIN_NODE_SIZE,meshOperationNormalVector,meshOperationVertexVector,polygonID,calculateWindingNumberFromBuffer,checkMemoryUsage,clearGeometryCache,clearIntersectionCache,createIntersectionCacheKey,createVector2Buffer,createVector3Buffer,disposePolytreeResources,extractCoordinatesFromArray,geometryCache,geometryCacheKeys,handleIntersectingPolytrees,intersectionCache,intersectionCacheKeys,operationCounter,prepareTriangleBufferFromPolygons,roundPointCoordinates,signedVolumeOfTriangle,sortRaycastIntersectionsByDistance,splitPolygonByPlane,splitPolygonVertexArray,testPolygonInsideUsingWindingNumber,testRayTriangleIntersection,handleObjectForOperation,operationHandler,Polytree,convertToPolytree,uniteRules,subtractRules,intersectRules,Plane,Polygon,constructIntersection,intersectionTestEdge2D,intersectionTestVertex2D,isUniqueTriangle,isValidTriangle,pointInTriangleInclusive2D,pointOnSegmentInclusive2D,pointsEqual2D,resolveCoplanarTriangleIntersection,resolveTriangleIntersection,triangleIntersectionCCW2D,triangleIntersectsTriangle,triangleOrientation2D,trianglesOverlap2D,Vertex,GEOMETRIC_EPSILON=1e-8,RAY_INTERSECTION_EPSILON=1e-12,TRIANGLE_2D_EPSILON=1e-14,WINDING_NUMBER_FULL_ROTATION=4*Math.PI,POLYGON_COPLANAR=0,POLYGON_FRONT=1,POLYGON_BACK=2,POLYGON_SPANNING=3,temporaryVector3Primary=new Vector3,temporaryVector3Secondary=new Vector3,temporaryVector3Tertiary=new Vector3,temporaryVector3Quaternary=new Vector3,temporaryBoundingBox=new Box3,temporaryRaycaster=new Raycaster,temporaryRay=new Ray,defaultRayDirection=new Vector3(0,0,1),windingNumberVector1=new Vector3,windingNumberVector2=new Vector3,windingNumberVector3=new Vector3,windingNumberTestPoint=new Vector3,windingNumberEpsilonOffsets=[new Vector3(GEOMETRIC_EPSILON,0,0),new Vector3(0,GEOMETRIC_EPSILON,0),new Vector3(0,0,GEOMETRIC_EPSILON),new Vector3(-GEOMETRIC_EPSILON,0,0),new Vector3(0,-GEOMETRIC_EPSILON,0),new Vector3(0,0,-GEOMETRIC_EPSILON)],windingNumberEpsilonOffsetsCount=windingNumberEpsilonOffsets.length,windingNumberMatrix3=new Matrix3,rayTriangleEdge1=new Vector3,rayTriangleEdge2=new Vector3,rayTriangleHVector=new Vector3,rayTriangleSVector=new Vector3,rayTriangleQVector=new Vector3,temporaryTriangleVertex=new Vector3,temporaryTriangleVertexSecondary=new Vector3,temporaryMatrix3=new Matrix3,temporaryMatrixWithNormalCalc=new Matrix3;temporaryMatrixWithNormalCalc.getNormalMatrix=function(e){return this.setFromMatrix4(e).invert().transpose()},meshOperationNormalVector=new Vector3,meshOperationVertexVector=new Vector3,POLYTREE_MAX_DEPTH=1e3,POLYTREE_MIN_NODE_SIZE=1e-6,MAX_REFINEMENT_ITERATIONS=1e4,DEFAULT_COORDINATE_PRECISION=15,POINT_COINCIDENCE_THRESHOLD=1e-15,DEFAULT_BUFFER_SIZE=16384,GARBAGE_COLLECTION_THRESHOLD=POLYTREE_MAX_POLYGONS_PER_NODE=1e5,INTERSECTION_CACHE_SIZE=GEOMETRY_CACHE_SIZE=ASYNC_OPERATION_TIMEOUT=MAX_WORKER_THREADS=1/(DEFAULT_MATERIAL_INDEX=0),polygonID=0,DEBUG_INTERSECTION_VERIFICATION=DEBUG_GEOMETRY_VALIDATION=DEBUG_PERFORMANCE_TIMING=DEBUG_VERBOSE_LOGGING=!(MEMORY_USAGE_WARNING_LIMIT=.95),DEBUG_COLOR_FRONT=65280,DEBUG_COLOR_BACK=16711680,DEBUG_COLOR_COPLANAR=255,DEBUG_COLOR_SPANNING=16776960,intersectionCache=new Map,intersectionCacheKeys=[],geometryCache=new Map,geometryCacheKeys=[],operationCounter=0,clearIntersectionCache=function(){var e,r,t,o,n;if(INTERSECTION_CACHE_SIZE!==1/0&&intersectionCache.size>INTERSECTION_CACHE_SIZE){for(n=[],o=(t=intersectionCacheKeys.splice(e=0,Math.floor(INTERSECTION_CACHE_SIZE/2))).length;e<o;e++)r=t[e],n.push(intersectionCache.delete(r));return n}},clearGeometryCache=function(){var e,r,t,o,n;if(GEOMETRY_CACHE_SIZE!==1/0&&geometryCache.size>GEOMETRY_CACHE_SIZE){for(n=[],o=(t=geometryCacheKeys.splice(e=0,Math.floor(GEOMETRY_CACHE_SIZE/2))).length;e<o;e++)r=t[e],n.push(geometryCache.delete(r));return n}},createIntersectionCacheKey=function(e,r){return e.id+"_"+r.id},checkMemoryUsage=function(){var e,r,t;return"undefined"!=typeof process&&process.memoryUsage?(r=(t=process.memoryUsage()).heapUsed/1024/1024,e=t.heapTotal/1024/1024,t=t.heapUsed/t.heapTotal,DEBUG_VERBOSE_LOGGING&&console.log(`Memory usage: ${r.toFixed(2)}MB / ${e.toFixed(2)}MB (${(100*t).toFixed(1)}%)`),MEMORY_USAGE_WARNING_LIMIT<t&&(console.warn(`Memory usage is high: ${(100*t).toFixed(1)}%`),"function"==typeof("undefined"!=typeof global&&null!==global?global.gc:void 0))&&(console.log("Triggering garbage collection..."),global.gc()),t):0},createVector2Buffer=function(e=DEFAULT_BUFFER_SIZE){return{top:0,array:new Float32Array(2*e),write:function(e){return this.array[this.top++]=e.x,this.array[this.top++]=e.y}}},createVector3Buffer=function(e=DEFAULT_BUFFER_SIZE){return{top:0,array:new Float32Array(3*e),write:function(e){return this.array[this.top++]=e.x,this.array[this.top++]=e.y,this.array[this.top++]=e.z}}},sortRaycastIntersectionsByDistance=function(e,r){return e.distance-r.distance},roundPointCoordinates=function(e,r=DEFAULT_COORDINATE_PRECISION){return e.x=+e.x.toFixed(r),e.y=+e.y.toFixed(r),e.z=+e.z.toFixed(r),e},extractCoordinatesFromArray=function(e,r){return{x:e[r],y:e[r+1],z:e[r+2]}},splitPolygonByPlane=function(e,r,t=[]){for(var o,n,i,a,s,l,c,y,u,h,p,g,d,m,P,f,E,T={polygon:e,type:"undecided"},I=0,O=[],b=c=0,N=e.vertices.length;0<=N?c<N:N<c;b=0<=N?++c:--c)I|=E=(E=r.normal.dot(e.vertices[b].pos)-r.distanceFromOrigin)<-GEOMETRIC_EPSILON?POLYGON_BACK:GEOMETRIC_EPSILON<E?POLYGON_FRONT:POLYGON_COPLANAR,O.push(E);switch(I){case POLYGON_COPLANAR:T.type=0<r.normal.dot(e.plane.normal)?"coplanar-front":"coplanar-back",t.push(T);break;case POLYGON_FRONT:T.type="front",t.push(T);break;case POLYGON_BACK:T.type="back",t.push(T);break;case POLYGON_SPANNING:for(l=[],n=[],b=u=0,m=e.vertices.length;0<=m?u<m:m<u;b=0<=m?++u:--u)d=O[g=(b+1)%e.vertices.length],i=e.vertices[b],g=e.vertices[g],(y=O[b])!==POLYGON_BACK&&l.push(i),y!==POLYGON_FRONT&&n.push(y!==POLYGON_BACK?i.clone():i),(y|d)===POLYGON_SPANNING&&(y=(r.distanceFromOrigin-r.normal.dot(i.pos))/r.normal.dot(temporaryTriangleVertex.copy(g.pos).sub(i.pos)),d=i.interpolate(g,y),l.push(d),n.push(d.clone()));if(3<=l.length)if(3<l.length)for(a=h=0,P=(s=splitPolygonVertexArray(l)).length;0<=P?h<P:P<h;a=0<=P?++h:--h)t.push({polygon:new Polygon(s[a],e.shared),type:"front"});else t.push({polygon:new Polygon(l,e.shared),type:"front"});if(3<=n.length)if(3<n.length)for(a=p=0,f=(o=splitPolygonVertexArray(n)).length;0<=f?p<f:f<p;a=0<=f?++p:--p)t.push({polygon:new Polygon(o[a],e.shared),type:"back"});else t.push({polygon:new Polygon(n,e.shared),type:"back"})}return 0===t.length&&t.push(T),t},splitPolygonVertexArray=function(e){var r,t,o,n,i=[];if(4<e.length)for(console.warn("[splitPolygonVertexArray] vertexArray.length > 4",e.length),o=r=3,t=e.length;3<=t?r<=t:t<=r;o=3<=t?++r:--r)(n=[]).push(e[0].clone()),n.push(e[o-2].clone()),n.push(e[o-1].clone()),i.push(n);else e[0].pos.distanceTo(e[2].pos)<=e[1].pos.distanceTo(e[3].pos)?i.push([e[0].clone(),e[1].clone(),e[2].clone()],[e[0].clone(),e[2].clone(),e[3].clone()]):i.push([e[0].clone(),e[1].clone(),e[3].clone()],[e[1].clone(),e[2].clone(),e[3].clone()]);return i},calculateWindingNumberFromBuffer=function(e,r){for(var t,o,n,i,a=0,s=t=0,l=e.length;t<l;s=t+=9)windingNumberVector1.subVectors(extractCoordinatesFromArray(e,s),r),windingNumberVector2.subVectors(extractCoordinatesFromArray(e,s+3),r),windingNumberVector3.subVectors(extractCoordinatesFromArray(e,s+6),r),o=windingNumberVector1.length(),n=windingNumberVector2.length(),i=windingNumberVector3.length(),o<POINT_COINCIDENCE_THRESHOLD||n<POINT_COINCIDENCE_THRESHOLD||i<POINT_COINCIDENCE_THRESHOLD||(windingNumberMatrix3.set(windingNumberVector1.x,windingNumberVector1.y,windingNumberVector1.z,windingNumberVector2.x,windingNumberVector2.y,windingNumberVector2.z,windingNumberVector3.x,windingNumberVector3.y,windingNumberVector3.z),a+=2*Math.atan2(windingNumberMatrix3.determinant(),o*n*i+windingNumberVector1.dot(windingNumberVector2)*i+windingNumberVector2.dot(windingNumberVector3)*o+windingNumberVector3.dot(windingNumberVector1)*n));return a=Math.round(a/WINDING_NUMBER_FULL_ROTATION)},testPolygonInsideUsingWindingNumber=function(e,r,t){var o,n,i,a=!1;if(windingNumberTestPoint.copy(r),0===calculateWindingNumberFromBuffer(e,windingNumberTestPoint)){if(t)for(n=o=0,i=Math.min(windingNumberEpsilonOffsetsCount,MAX_REFINEMENT_ITERATIONS);0<=i?o<i:i<o;n=0<=i?++o:--o)if(windingNumberTestPoint.copy(r).add(windingNumberEpsilonOffsets[n]),0!==calculateWindingNumberFromBuffer(e,windingNumberTestPoint)){a=!0;break}}else a=!0;return a},prepareTriangleBufferFromPolygons=function(e){for(var r,t,o=e.length,n=new Float32Array(3*o*3),i=0,a=r=0,s=o;0<=s?r<s:s<r;a=0<=s?++r:--r)t=e[a].triangle,n[i++]=t.a.x,n[i++]=t.a.y,n[i++]=t.a.z,n[i++]=t.b.x,n[i++]=t.b.y,n[i++]=t.b.z,n[i++]=t.c.x,n[i++]=t.c.y,n[i++]=t.c.z;return n},testRayTriangleIntersection=function(e,r,t=new Vector3){var o,n;return rayTriangleEdge1.subVectors(r.b,r.a),rayTriangleEdge2.subVectors(r.c,r.a),rayTriangleHVector.crossVectors(e.direction,rayTriangleEdge2),o=rayTriangleEdge1.dot(rayTriangleHVector),!(-RAY_INTERSECTION_EPSILON<o&&o<RAY_INTERSECTION_EPSILON||(o=1/o,rayTriangleSVector.subVectors(e.origin,r.a),(r=o*rayTriangleSVector.dot(rayTriangleHVector))<0)||1<r||(rayTriangleQVector.crossVectors(rayTriangleSVector,rayTriangleEdge1),(n=o*e.direction.dot(rayTriangleQVector))<0)||1<r+n||(r=o*rayTriangleEdge2.dot(rayTriangleQVector),!(RAY_INTERSECTION_EPSILON<r)))?t.copy(e.direction).multiplyScalar(r).add(e.origin):null},handleIntersectingPolytrees=function(e,r,t=!0){var o,n;GARBAGE_COLLECTION_THRESHOLD<=++operationCounter&&(operationCounter=0,clearIntersectionCache(),clearGeometryCache(),checkMemoryUsage()),!(n=o=void 0)===Polytree.useWindingNumber&&(t&&(o=prepareTriangleBufferFromPolygons(e.getPolygons())),n=prepareTriangleBufferFromPolygons(r.getPolygons())),e.handleIntersectingPolygons(r,n),t&&r.handleIntersectingPolygons(e,o),void 0!==o&&(o=void 0)},signedVolumeOfTriangle=function(e,r,t){return temporaryVector3Primary.copy(r).cross(t),e.dot(temporaryVector3Primary)/6},disposePolytreeResources=function(...e){if(Polytree.disposePolytree)return e.forEach(function(e){return e.delete()})},operationHandler=function(u,h=!1,p=!0,g={objCounter:0},d=!0,m=!0){var e,r,t,o,n,i;if(m)return new Promise(function(o,t){var n,i,e,r,a,s,l,c,y;try{return i=y=n=void 0,a=null!=(l=u.objA)?l.material:void 0,s=null!=(c=u.objB)?c.material:void 0,u.material&&(i=u.material),r=[],u.objA&&(e=handleObjectForOperation(u.objA,h,p,g,0,m),r.push(e)),u.objB&&(e=handleObjectForOperation(u.objB,h,p,g,1,m),r.push(e)),Promise.allSettled(r).then(function(e){var r;switch(e.forEach(function(e){if("fulfilled"===e.status)return 0===e.value.objIndex?n=e.value:1===e.value.objIndex?y=e.value:void 0}),!0===h&&(n&&(u.objA=n.original,n=n.result),y)&&(u.objB=y.original,y=y.result),r=void 0,u.op){case"unite":r=n&&y?Polytree.async.unite(n,y,p):Promise.resolve(n||y||new Polytree);break;case"subtract":r=n&&y?Polytree.async.subtract(n,y,p):Promise.resolve(n||new Polytree);break;case"intersect":r=n&&y?Polytree.async.intersect(n,y,p):Promise.resolve(new Polytree);break;default:r=Promise.resolve(new Polytree)}return r.then(function(e){var r,t;if(e&&!e.box&&e.bounds&&e.buildTree(),d&&!h){if(r=e.getPolygons(),!e||0===r.length)return void o(void 0);if(i)t=Polytree.toMesh(e,i);else{if(r=void 0,a)r=(r=Array.isArray(a)?a[0]:a).clone();else{if(!s)return void o(void 0);r=(r=Array.isArray(s)?s[0]:s).clone()}t=Polytree.toMesh(e,r)}disposePolytreeResources(e),o(t)}else d&&h?i?(t=Polytree.toMesh(e,i),disposePolytreeResources(e),o({result:t,operationTree:u})):o({result:e,operationTree:u}):o(e);if(!h&&(n||y))return disposePolytreeResources(n,y)}).catch(function(e){return t(e)})})}catch(e){return t(e)}});switch(o=n=i=t=void 0,u.material&&(o=u.material),u.objA&&(t=handleObjectForOperation(u.objA,h,p,g,void 0,m),!0===h)&&(u.objA=t.original,t=t.result),u.objB&&(i=handleObjectForOperation(u.objB,h,p,g,void 0,m),!0===h)&&(u.objB=i.original,i=i.result),u.op){case"unite":n=t&&i?Polytree.unite(t,i,!1):t||i||new Polytree;break;case"subtract":n=t&&i?Polytree.subtract(t,i,!1):t||new Polytree;break;case"intersect":n=t&&i?Polytree.intersect(t,i,!1):new Polytree;break;default:n=new Polytree}if(n&&!n.box&&n.bounds&&n.buildTree(),h||(t||i)&&disposePolytreeResources(t,i),!d||h)return d&&h?{result:n,operationTree:u}:n;if(e=n.getPolygons(),n&&0!==e.length){if(o)r=Polytree.toMesh(n,o);else{if(e=void 0,null==(o=u.objA)||!o.material)return;e=(e=Array.isArray(u.objA.material)?u.objA.material[0]:u.objA.material).clone(),r=Polytree.toMesh(n,e)}return disposePolytreeResources(n),r}},handleObjectForOperation=function(o,n,i,a,s,l=!0){var e;return l?new Promise(function(r,t){var e;try{return e=void 0,o.isMesh?(e=Polytree.fromMesh(o,a.objCounter++),(e=n?{result:e,original:e.clone()}:e).objIndex=s,r(e)):o.isPolytree?(e=o,(e=n?{result:o,original:o.clone()}:e).objIndex=s,r(e)):o.op?Polytree.operation(o,n,i,a,!1,l).then(function(e){return(e=n?{result:e,original:o}:e).objIndex=s,r(e)}):void 0}catch(e){return t(e)}}):(e=void 0,o.isMesh?(e=Polytree.fromMesh(o,a.objCounter++),n&&(e={result:e,original:e.clone()})):o.isPolytree?(e=o,n&&(e={result:o,original:o.clone()})):o.op&&(e=Polytree.operation(o,n,i,a,!1,l),n)&&(e={result:e,original:o}),e)},(Polytree=function(){class N{constructor(e=null,r=null){this.box=e,this.polygons=[],this.replacedPolygons=[],this.parent=r,this.subTrees=[],this.level=0,this.originalMatrixWorld,this.mesh,this.polygonArrays=void 0,this.addPolygonsArrayToRoot(this.polygons)}isEmpty(){return 0===this.polygons.length}getMesh(){for(var e,r=new Set,t=this;t&&!r.has(t);){if(r.add(t),!t.parent)return null!=(e=t.mesh)?e:null;t=t.parent}return null}newPolytree(e,r){return new this.constructor(e,r)}setPolygonIndex(r){return void 0!==r&&this.polygonArrays?this.polygonArrays.forEach(function(e){if(null!=e&&e.length)return e.forEach(function(e){return e.shared=r})}):void 0}clone(){return(new this.constructor).copy(this)}copy(e){var r,t,o,n,i;if(e&&(this.deletePolygonsArrayFromRoot(this.polygons),this.polygons=e.polygons?e.polygons.map(function(e){return e.clone()}):[],this.addPolygonsArrayToRoot(this.polygons),this.replacedPolygons=e.replacedPolygons?e.replacedPolygons.map(function(e){return e.clone()}):[],e.mesh&&(this.mesh=e.mesh),e.originalMatrixWorld&&(this.originalMatrixWorld=e.originalMatrixWorld.clone()),this.box=e.box?e.box.clone():null,this.level=null!=(o=e.level)?o:0,e.subTrees))for(r=t=0,n=e.subTrees.length;0<=n?t<n:n<t;r=0<=n?++t:--t)i=new this.constructor(void 0,this).copy(e.subTrees[r]),this.subTrees.push(i);return this}unite(e,r,t=null){return N.unite(e,r,t)}subtract(e,r,t=null){return N.subtract(e,r,t)}intersect(e,r,t=null){return N.intersect(e,r,t)}addPolygonsArrayToRoot(e){return this.parent?this.parent.addPolygonsArrayToRoot(e):(void 0===this.polygonArrays&&(this.polygonArrays=[]),this.polygonArrays.push(e))}deletePolygonsArrayFromRoot(e){return this.parent?this.parent.deletePolygonsArrayFromRoot(e):-1<(e=this.polygonArrays.indexOf(e))?this.polygonArrays.splice(e,1):void 0}addPolygon(e,r){var t;return this.bounds||(this.bounds=new Box3),t=e.triangle,r&&!isUniqueTriangle(t,r)||(this.bounds.expandByPoint(t.a),this.bounds.expandByPoint(t.b),this.bounds.expandByPoint(t.c),this.polygons.push(e)),this}calcBox(){var e;return this.bounds||(this.bounds=new Box3),this.box=this.bounds.clone(),e=POLYTREE_MIN_NODE_SIZE,this.box.min.x-=e,this.box.min.y-=e,this.box.min.z-=e,this.box.max.x+=e,this.box.max.y+=e,this.box.max.z+=e,this}split(e){var r,t,o,n,i,a,s,l,c,y,u,h,p,g,d,m,P,f,E,T=[];if(this.box){for(o=temporaryVector3Secondary.copy(this.box.max).sub(this.box.min).multiplyScalar(.5),P=i=0;i<=1;P=++i)for(f=a=0;a<=1;f=++a)for(E=l=0;l<=1;E=++l)r=new Box3,m=temporaryVector3Primary.set(P,f,E),r.min.copy(this.box.min).add(m.multiply(o)),r.max.copy(r.min).add(o),r.expandByScalar(GEOMETRIC_EPSILON),T.push(this.newPolytree(r,this));for(p=void 0;p=this.polygons.pop();){for(t=!1,n=y=0,g=T.length;0<=g?y<g:g<y;n=0<=g?++y:--y)T[n].box.containsPoint(p.getMidpoint())&&(T[n].polygons.push(p),t=!0);if(!t)throw new Error(`Unable to find subtree for triangle at level ${e}.`)}for(n=h=0,d=T.length;0<=d?h<d:d<h;n=0<=d?++h:--h)T[n].level=e+1,s=T[n].polygons.length,u=T[n].box.getSize(temporaryVector3Tertiary).length(),c=Math.min(N.maxLevel,POLYTREE_MAX_DEPTH),Math.min(N.polygonsPerTree,POLYTREE_MAX_POLYGONS_PER_NODE)<s&&e<c&&POLYTREE_MIN_NODE_SIZE<u&&T[n].split(e+1),this.subTrees.push(T[n])}return this}buildTree(){return this.calcBox(),this.split(0),this.processTree(),this}processTree(){var e,r,t,o,n,i;if(!this.isEmpty()){for(temporaryBoundingBox.copy(this.box),e=r=0,o=this.polygons.length;0<=o?r<o:o<r;e=0<=o?++r:--r)this.box.expandByPoint(this.polygons[e].triangle.a),this.box.expandByPoint(this.polygons[e].triangle.b),this.box.expandByPoint(this.polygons[e].triangle.c);this.expandParentBox()}for(i=[],e=t=0,n=this.subTrees.length;0<=n?t<n:n<t;e=0<=n?++t:--t)i.push(this.subTrees[e].processTree());return i}expandParentBox(e=new Set){if(this.parent&&!e.has(this.parent))return e.add(this.parent),this.parent.box.expandByPoint(this.box.min),this.parent.box.expandByPoint(this.box.max),this.parent.expandParentBox(e)}invert(){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.flip()})})}getPolygons(i=[]){return this.polygonArrays.forEach(function(e){var r,t,o,n;if(e.length){for(n=[],r=t=0,o=e.length;0<=o?t<o:o<t;r=0<=o?++t:--t)e[r].valid&&-1===i.indexOf(e[r])?n.push(i.push(e[r])):n.push(void 0);return n}}),i}getTriangles(r=[]){return this.getPolygons().forEach(function(e){return r.push(e.triangle)}),r}getRayPolygons(e,r=[]){var t,o,n,i,a;if(0<this.polygons.length)for(t=o=0,i=this.polygons.length;0<=i?o<i:i<o;t=0<=i?++o:--o)this.polygons[t].valid&&this.polygons[t].originalValid&&-1===r.indexOf(this.polygons[t])&&r.push(this.polygons[t]);for(0<this.replacedPolygons.length&&r.push(...this.replacedPolygons),t=n=0,a=this.subTrees.length;0<=a?n<a:a<n;t=0<=a?++n:--n)e.intersectsBox(this.subTrees[t].box)&&this.subTrees[t].getRayPolygons(e,r);return r}getRayTriangles(e,r=[]){return this.getRayPolygons(e).forEach(function(e){return r.push(e.triangle)}),r}rayIntersect(e,r,t=[]){var o,n,i,a,s,l,c;if(0===e.direction.length())return[];for(o=1e100,n=i=0,l=(s=this.getRayPolygons(e)).length;0<=l?i<l:l<i;n=0<=l?++i:--i)c=void 0,"regular"===N.rayIntersectTriangleType?(c=e.intersectTriangle(s[n].triangle.a,s[n].triangle.b,s[n].triangle.c,!1,temporaryVector3Primary))&&(temporaryVector3Primary.applyMatrix4(r),(o=temporaryVector3Primary.distanceTo(e.origin))<0||1/0<o?console.warn("[rayIntersect] Failed ray distance check.",e):t.push({distance:o,polygon:s[n],position:temporaryVector3Primary.clone()})):(c=testRayTriangleIntersection(e,s[n].triangle,temporaryVector3Primary))&&(o=(a=c.clone().sub(e.origin).length())<o?a:o)<1e100&&t.push({distance:o,polygon:s[n],position:c.clone().add(e.origin)});return t.length&&t.sort(sortRaycastIntersectionsByDistance),t}getIntersectingPolygons(i=[]){return this.polygonArrays.forEach(function(e){var r,t,o,n;if(e.length){for(n=[],r=t=0,o=e.length;0<=o?t<o:o<t;r=0<=o?++t:--t)e[r].valid&&e[r].intersects?n.push(i.push(e[r])):n.push(void 0);return n}}),i}getPolygonsIntersectingPolygon(e,r=[]){var t,o,n,i,a,s,l,c,y;if(this.box.intersectsTriangle(e.triangle)&&0<this.polygons.length){if(t=this.polygons.slice(),0<this.replacedPolygons.length)for(o=n=0,l=this.replacedPolygons.length;0<=l?n<l:l<n;o=0<=l?++n:--n)t.push(this.replacedPolygons[o]);for(o=i=0,c=t.length;0<=c?i<c:c<i;o=0<=c?++i:--i)(s=t[o]).originalValid&&s.valid&&s.intersects&&triangleIntersectsTriangle(e.triangle,s.triangle)&&r.push(s)}for(o=a=0,y=this.subTrees.length;0<=y?a<y:y<a;o=0<=y?++a:--a)this.subTrees[o].getPolygonsIntersectingPolygon(e,r);return r}replacePolygon(e,r){var t,o,n,i,a;for(Array.isArray(r)||(r=[r]),0<this.polygons.length&&-1<(n=this.polygons.indexOf(e))&&(!0===e.originalValid?this.replacedPolygons.push(e):e.setInvalid(),this.polygons.splice(n,1,...r)),a=[],t=o=0,i=this.subTrees.length;0<=i?o<i:i<o;t=0<=i?++o:--o)a.push(this.subTrees[t].replacePolygon(e,r));return a}deletePolygonsByStateRules(p,g=!0){return this.polygonArrays.forEach(function(h){if(h.length)return h.filter(function(e){return!0===e.valid&&!0===e.intersects}).forEach(function(e){for(var r,t,o,n,i,a,s,l,c=!1,y=t=0,u=p.length;0<=u?t<u:u<t;y=0<=u?++t:--t)if(p[y].array){if((s=p[y].rule).includes(e.state)&&("undecided"!==e.previousState&&s.includes(e.previousState)||"undecided"===e.previousState)){for(c=!0,l={},s.forEach(function(e){return l[e]=!1}),s.forEach(function(e){return e,!1}),l[e.state]=!0,r=o=0,i=e.previousStates.length;0<=i?o<i:i<o;r=0<=i?++o:--o){if(!s.includes(e.previousStates[r])){c=!1;break}l[e.previousStates[r]]=!0}if(c){for(a in l)if(!1===l[a]){c=!1;break}if(c)break}}}else if(e.checkAllStates(p[y].rule)){c=!0;break}if(c&&(-1<(n=h.indexOf(e))&&(e.setInvalid(),h.splice(n,1)),g))return e.delete()})})}deletePolygonsByIntersection(o,n=!0){if(void 0!==o)return this.polygonArrays.forEach(function(t){if(t.length)return t.slice().forEach(function(e){var r;if(e.valid&&e.intersects===o&&(-1<(r=t.indexOf(e))&&(e.setInvalid(),t.splice(r,1)),n))return e.delete()})})}isPolygonIntersecting(e){return!!this.box.intersectsTriangle(e.triangle)}markIntesectingPolygons(r){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.intersects=r.isPolygonIntersecting(e)})})}resetPolygons(r=!0){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.reset(r)})})}handleIntersectingPolygons(e,r){var t,o,n,i,a,s,l,c,y,u,h,p,g,d,m,P,f,E,T,I,O,b;if(0<this.polygons.length){for(t=(g=this.polygons.filter(function(e){return!0===e.valid&&!0===e.intersects&&"undecided"===e.state})).pop();t;)if("undecided"===t.state&&t.valid){if(0<(b=e.getPolygonsIntersectingPolygon(t)).length)for(a=s=0,d=b.length;0<=d?s<d:d<s;a=0<=d?++s:--s){if(O=b[a],1<(I=splitPolygonByPlane(t,O.plane)).length){for(o=l=0,m=I.length;0<=m?l<m:m<l;o=0<=m?++l:--l)(p=I[o].polygon).intersects=t.intersects,p.newPolygon=!0,g.push(p);this.replacePolygon(t,I.map(function(e){return e.polygon}));break}if(t.id!==I[0].polygon.id){I[0].polygon.intersects=t.intersects,I[0].polygon.newPolygon=!0,g.push(I[0].polygon),this.replacePolygon(t,I[0].polygon);break}"coplanar-front"!==I[0].type&&"coplanar-back"!==I[0].type||(t.setState(I[0].type),t.coplanar=!0)}t=g.pop()}for(t=(g=this.polygons.filter(function(e){return!0===e.valid&&!0===e.intersects})).pop(),n=!1;t;)if(t.valid){if(n=!1,e.box.containsPoint(t.getMidpoint()))if(!0===N.useWindingNumber)n=testPolygonInsideUsingWindingNumber(r,t.getMidpoint(),t.coplanar);else if(h=roundPointCoordinates(temporaryVector3Secondary.copy(t.getMidpoint())),!0!==N.usePolytreeRay&&e.mesh){if(defaultRayDirection.copy(t.plane.normal),temporaryRaycaster.set(h,defaultRayDirection),!(n=(i=temporaryRaycaster.intersectObject(e.mesh)).length&&0<defaultRayDirection.dot(i[0].face.normal)?!0:n)&&t.coplanar)for(a=c=0,P=windingNumberEpsilonOffsetsCount;0<=P?c<P:P<c;a=0<=P?++c:--c)if(temporaryRaycaster.ray.origin.copy(h).add(windingNumberEpsilonOffsets[a]),(i=temporaryRaycaster.intersectObject(e.mesh)).length&&0<defaultRayDirection.dot(i[0].face.normal)){n=!0;break}}else if(temporaryRay.origin.copy(h),defaultRayDirection.copy(t.plane.normal),temporaryRay.direction.copy(t.plane.normal),!(n=(i=e.rayIntersect(temporaryRay,e.originalMatrixWorld)).length&&0<defaultRayDirection.dot(i[0].polygon.plane.normal)?!0:n)&&t.coplanar)for(a=y=0,f=windingNumberEpsilonOffsetsCount;0<=f?y<f:f<y;a=0<=f?++y:--y)if(temporaryRay.origin.copy(h).add(windingNumberEpsilonOffsets[a]),defaultRayDirection.copy(t.plane.normal),temporaryRay.direction.copy(t.plane.normal),(i=e.rayIntersect(temporaryRay,e.originalMatrixWorld)).length&&0<defaultRayDirection.dot(i[0].polygon.plane.normal)){n=!0;break}!0===n?t.setState("inside"):t.setState("outside"),t=g.pop()}}for(T=[],o=u=0,E=this.subTrees.length;0<=E?u<E:E<u;o=0<=E?++u:--u)T.push(this.subTrees[o].handleIntersectingPolygons(e,r));return T}applyMatrix(e,r,t=!0){var o,n,i,a,s;if(e.isMesh&&(e.updateMatrix(),e=e.matrix),this.box&&this.box.makeEmpty(),r=r||temporaryMatrixWithNormalCalc.getNormalMatrix(e),0<this.polygons.length)for(o=n=0,a=this.polygons.length;0<=a?n<a:a<n;o=0<=a?++n:--n)this.polygons[o].valid&&this.polygons[o].applyMatrix(e,r);for(o=i=0,s=this.subTrees.length;0<=s?i<s:s<i;o=0<=s?++i:--i)this.subTrees[o].applyMatrix(e,r,!1);if(t)return this.processTree()}triangleSphereIntersect(e,r){var t,o,n,i,a,s,l,c,y=new Vector3,u=new Vector3,h=new Line3,p=new ThreePlane;if(r.getPlane(p),e.intersectsPlane(p)){if(n=Math.abs(p.distanceToSphere(e)),s=e.radius*e.radius-n*n,a=p.projectPoint(e.center,y),r.containsPoint(e.center))return{depth:Math.abs(p.distanceToSphere(e)),normal:p.normal.clone(),point:a.clone()};for(o=i=0,l=(c=[[r.a,r.b],[r.b,r.c],[r.c,r.a]]).length;0<=l?i<l:l<i;o=0<=l?++i:--i)if(h.set(c[o][0],c[o][1]),h.closestPointToPoint(a,!0,u),(t=u.distanceToSquared(e.center))<s)return{depth:e.radius-Math.sqrt(t),normal:e.center.clone().sub(u).normalize(),point:u.clone()}}return!1}getSphereTriangles(e,o){for(var n,i,r,a,t=[],s=r=0,l=this.subTrees.length;0<=l?r<l:l<r;s=0<=l?++r:--r)i=this.subTrees[s],e.intersectsBox(i.box)&&(0<i.polygons.length?t.push((()=>{var e,r,t=[];for(a=e=0,r=i.polygons.length;0<=r?e<r:r<e;a=0<=r?++e:--e)(n=i.polygons[a]).valid&&(-1===o.indexOf(n.triangle)?t.push(o.push(n.triangle)):t.push(void 0));return t})()):t.push(i.getSphereTriangles(e,o)));return t}sphereIntersect(e){var r,t,o,n,i=!1,a=void 0,s=[],l=new Sphere;for(l.copy(e),this.getSphereTriangles(e,s),n=r=0,o=s.length;0<=o?r<o:o<r;n=0<=o?++r:--r)(a=this.triangleSphereIntersect(l,s[n]))&&(i=!0,l.center.add(a.normal.multiplyScalar(a.depth)));return!!i&&(t=(e=l.center.clone().sub(e.center)).length(),{normal:e.normalize(),depth:t})}fromGraphNode(e){var r;return e.updateWorldMatrix(!0,!0),r=this,e.traverse(function(e){if(!0===e.isMesh)return N.fromMesh(e,void 0,r,!1)}),this.buildTree()}deleteReplacedPolygons(){var e,r,t,o;for(0<this.replacedPolygons.length&&(this.replacedPolygons.forEach(function(e){return e.delete()}),this.replacedPolygons.length=0),o=[],e=r=0,t=this.subTrees.length;0<=t?r<t:t<r;e=0<=t?++r:--r)o.push(this.subTrees[e].deleteReplacedPolygons());return o}markPolygonsAsOriginal(){return this.polygonArrays.forEach(function(e){if(e.length)return e.forEach(function(e){return e.originalValid=!0})})}getPolygonCloneCallback(i,a){return this.polygonArrays.forEach(function(e){var r,t,o,n;if(e.length){for(n=[],r=t=0,o=e.length;0<=o?t<o:o<t;r=0<=o?++t:--t)e[r].valid?n.push(i(e[r].clone(),a)):n.push(void 0);return n}})}delete(e=!0){var r,t,o;if(0<this.polygons.length&&e&&(this.polygons.forEach(function(e){return e.delete()}),this.polygons.length=0),0<this.replacedPolygons.length&&e&&(this.replacedPolygons.forEach(function(e){return e.delete()}),this.replacedPolygons.length=0),this.polygonArrays&&(this.polygonArrays.length=0),this.subTrees.length){for(r=t=0,o=this.subTrees.length;0<=o?t<o:o<t;r=0<=o?++t:--t)this.subTrees[r].delete(e);this.subTrees.length=0}return this.mesh=void 0,this.originalMatrixWorld=void 0,this.box=void 0,this.parent=void 0,this.level=void 0}dispose(e=!0){return this.delete(e)}}return N.prototype.isPolytree=!0,N.usePolytreeRay=!0,N.disposePolytree=!0,N.useWindingNumber=!1,N.maxLevel=POLYTREE_MAX_DEPTH,N.polygonsPerTree=POLYTREE_MAX_POLYGONS_PER_NODE,N.rayIntersectTriangleType="MollerTrumbore",N.operation=operationHandler,N.rayIntersectsTriangle=testRayTriangleIntersection,N}.call(this)).toGeometry=function(e){for(var r,t,o,n,i,a,s,l,c,y,u,h,p,g,d,m=[],P=[],f=void 0,E=void 0,T=0,I=e.getPolygons(),O=0,b=I.length;O<b;O++)y=I[O],T+=Math.max(0,y.vertices.length-2);for(u=createVector3Buffer(3*T),c=createVector3Buffer(3*T),i=0,a=I.length;i<a;i++)for(d=(g=(y=I[i]).vertices).length,void 0===y.shared||m[y.shared]||(m[y.shared]=[]),0<d&&(void 0!==g[0].uv&&(f=f||createVector2Buffer(3*T)),void 0!==g[0].color)&&(E=E||createVector3Buffer(3*T)),o=s=3,h=d;3<=h?s<=h:h<=s;o=3<=h?++s:--s)(void 0===y.shared?P:m[y.shared]).push(u.top/3,u.top/3+1,u.top/3+2),u.write(g[0].pos),u.write(g[o-2].pos),u.write(g[o-1].pos),c.write(g[0].normal),c.write(g[o-2].normal),c.write(g[o-1].normal),null!=f&&(f.write(g[0].uv),f.write(g[o-2].uv),f.write(g[o-1].uv)),null!=E&&(E.write(g[0].color),E.write(g[o-2].color),E.write(g[o-1].color));if((r=new BufferGeometry).setAttribute("position",new BufferAttribute(u.array,3)),r.setAttribute("normal",new BufferAttribute(c.array,3)),f&&r.setAttribute("uv",new BufferAttribute(f.array,2)),E&&r.setAttribute("color",new BufferAttribute(E.array,3)),0<m.length){for(n=[],o=l=t=0,p=m.length;0<=p?l<p:p<l;o=0<=p?++l:--l)m[o]=m[o]||[],r.addGroup(t,m[o].length,o),t+=m[o].length,n=n.concat(m[o]);P.length&&(r.addGroup(t,P.length,m.length),n=n.concat(P)),r.setIndex(n)}return r},Polytree.toMesh=function(e,r){e=Polytree.toGeometry(e);return new Mesh(e,r)},Polytree.fromMesh=function(e,r,t=new Polytree,o=!0){var n,i,a,s,l,c,y,u,h,p,g,d,m,P,f,E,T,I,O,b,N,_,V,R;if(e.isPolytree)return e;for("regular"===Polytree.rayIntersectTriangleType&&(t.originalMatrixWorld=e.matrixWorld.clone()),e.updateWorldMatrix(!0,!0),i=e.geometry,temporaryMatrixWithNormalCalc.getNormalMatrix(e.matrix),s=i.groups,_=i.attributes.uv,n=i.attributes.color,T=i.attributes.position,m=i.attributes.normal,f=[],l=u=0,I=(c=i.index?i.index.array:Array(T.array.length/T.itemSize|0).fill().map(function(e,r){return r})).length;u<I;l=u+=3){for(R=[],y=h=0;h<3;y=++h)N=2*(V=c[l+y]),E=new Vector3(T.array[V=3*V],T.array[1+V],T.array[2+V]),V=new Vector3(m.array[V],m.array[1+V],m.array[2+V]),E.applyMatrix4(e.matrix),V.applyMatrix3(temporaryMatrixWithNormalCalc),b=_?{x:_.array[N],y:_.array[1+N]}:void 0,N=n?{x:n.array[N],y:n.array[1+N],z:n.array[2+N]}:void 0,R.push(new Vertex(E,V,b,N));if(void 0===r&&s&&0<s.length){for(P=void 0,g=0,p=s.length;g<p;g++)a=s[g],c[l]>=a.start&&c[l]<a.start+a.count&&((P=new Polygon(R,a.materialIndex)).originalValid=!0);P&&f.push(P)}else(P=new Polygon(R,r)).originalValid=!0,f.push(P)}for(l=d=0,O=f.length;0<=O?d<O:O<d;l=0<=O?++d:--d)isValidTriangle(f[l].triangle)?t.addPolygon(f[l]):f[l].delete();return o&&t.buildTree(),!0!==Polytree.usePolytreeRay&&(t.mesh=e),t},Polytree.getSurface=function(e){var r,t,o,n,i,a,s,l,c=0;if(!e)throw new Error("Input is required.");if(!(e=e.geometry||e)||!e.attributes)throw new Error("No geometry found.");if(!(n=e.attributes.position))throw new Error("Geometry has no position attribute.");for(r=o=0,i=(t=e.index?e.index.array:Array(n.array.length/n.itemSize|0).fill().map(function(e,r){return r})).length;o<i;r=o+=3)a=3*t[r+0],s=3*t[r+1],l=3*t[r+2],a=new Vector3(n.array[a],n.array[1+a],n.array[2+a]),s=new Vector3(n.array[s],n.array[1+s],n.array[2+s]),l=new Vector3(n.array[l],n.array[1+l],n.array[2+l]),c+=new Triangle(a,s,l).getArea();return c},Polytree.prototype.getSurface=function(e){return Polytree.getSurface(e)},Polytree.getVolume=function(e){var r,t,o,n,i,a,s,l=0,c=new Vector3,y=new Vector3,u=new Vector3,h=null;if(e.isMesh)h=e.geometry;else{if(!e.isBufferGeometry)throw new Error("Input must be a Three.js Mesh or BufferGeometry.");h=e}if(!(e=("function"==typeof h.toBuffer?h.toBuffer():void 0)||h).isBufferGeometry)throw new Error("Unable to convert input to BufferGeometry.");if(!(i=e.attributes.position))return 0;if(e.index)for(r=o=0,a=(t=e.index).count/3;0<=a?o<a:a<o;r=0<=a?++o:--o)c.fromBufferAttribute(i,t.array[3*r+0]),y.fromBufferAttribute(i,t.array[3*r+1]),u.fromBufferAttribute(i,t.array[3*r+2]),l+=signedVolumeOfTriangle(c,y,u);else for(r=n=0,s=i.count/3;0<=s?n<s:s<n;r=0<=s?++n:--n)c.fromBufferAttribute(i,3*r+0),y.fromBufferAttribute(i,3*r+1),u.fromBufferAttribute(i,3*r+2),l+=signedVolumeOfTriangle(c,y,u);return Math.abs(l)},Polytree.prototype.getVolume=function(e){return Polytree.getVolume(e)},convertToPolytree=function(e){return e?e.isPolytree?{polytree:e,shouldCleanup:!1}:e.isMesh?{polytree:Polytree.fromMesh(e),shouldCleanup:!0}:e.isBufferGeometry?(e=new Mesh(e,{isMaterial:!0,type:"MeshBasicMaterial"}),{polytree:Polytree.fromMesh(e),shouldCleanup:!0}):null:null},Polytree.closestPointToPoint=function(e,r,t={},o=1/0){var n,i,a,s,l,c,y,u,h,p;if(e&&r&&(e=convertToPolytree(e))){for({polytree:e,shouldCleanup:c}=e,n=o,a=i=null,s=0,l=(p=e.getTriangles()).length;s<l;s++)u=p[s],y=new Triangle(u.a,u.b,u.c),h=new Vector3,y.closestPointToPoint(r,h),(y=h.distanceTo(r))<n&&(n=y,i=h.clone(),a=u);if(i)return t.point=i,t.distance=n,t.triangle=a,c&&e.delete(),t;c&&e.delete()}return null},Polytree.distanceToPoint=function(e,r){e=Polytree.closestPointToPoint(e,r);return(null!=e?e.distance:void 0)||1/0},Polytree.intersectsSphere=function(e,r){var t,o,n,i,a,s,l;if(e&&r&&(e=convertToPolytree(e))){for({polytree:i,shouldCleanup:a}=e,o=0,n=(l=i.getTriangles()).length;o<n;o++)if(s=l[o],s=new Triangle(s.a,s.b,s.c),t=new Vector3,s.closestPointToPoint(r.center,t),t.distanceTo(r.center)<=r.radius)return a&&i.delete(),!0;a&&i.delete()}return!1},Polytree.intersectsBox=function(e,r){var t,o,n,i,a,s;if(e&&r&&(e=convertToPolytree(e))){for({polytree:n,shouldCleanup:i}=e,t=0,o=(s=n.getTriangles()).length;t<o;t++)if(a=s[t],a=new Triangle(a.a,a.b,a.c),r.intersectsTriangle(a))return i&&n.delete(),!0;i&&n.delete()}return!1},Polytree.intersectPlane=function(e,r,t=[]){var o,n,i,a,s,l,c,y,u,h,p,g,d;if(e&&r&&(e=convertToPolytree(e))){for({polytree:e,shouldCleanup:y}=e,a=0,l=(d=e.getTriangles()).length;a<l;a++){for(p=d[a],new Triangle(p.a,p.b,p.c),i=[],s=0,c=(g=[[p.a,p.b],[p.b,p.c],[p.c,p.a]]).length;s<c;s++)u=(o=g[s])[0],o=o[1],(h=r.normal.dot(u)+r.constant)*(n=r.normal.dot(o)+r.constant)<0&&(h=h/(h-n),(n=new Vector3).lerpVectors(u,o,h),i.push(n));2===i.length&&t.push(new Line3(i[0],i[1]))}y&&e.delete()}return t},Polytree.sliceIntoLayers=function(e,r,t,o,n=new Vector3(0,0,1)){var i,a,s,l,c,y,u,h,p,g,d,m,P,f,E,T,I,O;if(!(e&&0<r&&t<o))return[];if(!(e=convertToPolytree(e)))return[];for({polytree:P,shouldCleanup:e}=e,h=[],i=t;i<=o;){for(m=n.clone(),d=-i,u=[],c=0,p=(O=P.getTriangles()).length;c<p;c++){for(l=[],y=0,g=(I=[[(T=O[c]).a,T.b],[T.b,T.c],[T.c,T.a]]).length;y<g;y++)f=(a=I[y])[0],a=a[1],(E=m.dot(f)+d)*(s=m.dot(a)+d)<0&&(E=E/(E-s),(s=new Vector3).lerpVectors(f,a,E),l.push(s));2===l.length&&u.push(new Line3(l[0],l[1]))}h.push(u),i+=r}return e&&P.delete(),h},Polytree.shapecast=function(e,r,t=null){var o,n,i,a,s,l,c,y;if(!e||!r)return[];if(!(i=convertToPolytree(e)))return[];for({polytree:e,shouldCleanup:s}=i,a=[],o=0,n=(y=e.getTriangles()).length;o<n;o++)c=y[o],r(l=new Triangle(c.a,c.b,c.c),c)&&(t?null!=(i=t(l,c))&&a.push(i):a.push(c));return s&&e.delete(),a},Polytree.getTrianglesNearPoint=function(e,t,o){return e&&t&&0<o?Polytree.shapecast(e,function(e,r){return e.a.distanceTo(t)<=o||e.b.distanceTo(t)<=o||e.c.distanceTo(t)<=o}):[]},uniteRules={a:[{array:!0,rule:["inside","coplanar-back"]},{array:!(Polytree.estimateVolumeViaSampling=function(e,r=1e4,t=null){var o,n,i,a,s,l,c,y,u,h,p;if(!e)return 0;if(!(e=convertToPolytree(e)))return 0;if({polytree:c,shouldCleanup:e}=e,!t)for(t=new Box3,a=0,l=(p=c.getTriangles()).length;a<l;a++)h=p[a],t.expandByPoint(h.a),t.expandByPoint(h.b),t.expandByPoint(h.c);if(o=new Vector3,t.getSize(o),0==(n=o.x*o.y*o.z))return 0;for(s=i=0,y=r;0<=y?s<y:y<s;0<=y?++s:--s)u=new Vector3(t.min.x+Math.random()*o.x,t.min.y+Math.random()*o.y,t.min.z+Math.random()*o.z),u=new Ray(u,new Vector3(1,0,0)),c.rayIntersect(u,{elements:[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],isMatrix4:!0}).length%2==1&&i++;return r=i/r,e&&c.delete(),r*n}),rule:"inside"}],b:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["inside","coplanar-front"]},{array:!1,rule:"inside"}]},Polytree.unite=function(e,r,t=!0){var o,n,i;return t?e.isPolytree&&r.isPolytree?Polytree.async.unite(e,r,!0):(o=Polytree.fromMesh(e),n=Polytree.fromMesh(r),i=(i=Array.isArray(e.material)?e.material[0]:e.material).clone(),Polytree.async.unite(o,n,!1).then(function(e){var r=Polytree.toMesh(e,i);return disposePolytreeResources(o,n,e),r})):e.isPolytree&&r.isPolytree?(o=e,n=r,this.uniteCore(o,n,!0)):(o=Polytree.fromMesh(e),n=Polytree.fromMesh(r),i=(i=Array.isArray(e.material)?e.material[0]:e.material).clone(),t=this.uniteCore(o,n,!1),r=Polytree.toMesh(t,i),disposePolytreeResources(o,n,t),r)},subtractRules={a:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["inside","coplanar-front"]},{array:!(Polytree.uniteCore=function(e,r,t=!0){var o,n,i=new Polytree,a=new Set;return e.box.intersectsBox(r.box)?(n=o=void 0,e.mesh&&(o=e.mesh.material.side,e.mesh.material.side=DoubleSide),r.mesh&&(n=r.mesh.material.side,r.mesh.material.side=DoubleSide),e.resetPolygons(!1),r.resetPolygons(!1),e.markIntesectingPolygons(r),r.markIntesectingPolygons(e),handleIntersectingPolytrees(e,r),e.deleteReplacedPolygons(),r.deleteReplacedPolygons(),e.deletePolygonsByStateRules(uniteRules.a),r.deletePolygonsByStateRules(uniteRules.b),e.getPolygonCloneCallback(i.addPolygon.bind(i),a),r.getPolygonCloneCallback(i.addPolygon.bind(i),a),e.mesh&&e.mesh.material.side!==o&&(e.mesh.material.side=o),r.mesh&&r.mesh.material.side!==n&&(r.mesh.material.side=n)):(e.getPolygonCloneCallback(i.addPolygon.bind(i),a),r.getPolygonCloneCallback(i.addPolygon.bind(i),a)),a.clear(),a=void 0,i.markPolygonsAsOriginal(),t&&i.buildTree(),i}),rule:"inside"}],b:[{array:!0,rule:["outside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["inside","coplanar-front"]},{array:!1,rule:"outside"}]},Polytree.subtract=function(e,r,t=!0){var o,n,i;return t?e.isPolytree&&r.isPolytree?Polytree.async.subtract(e,r,!0):(o=Polytree.fromMesh(e),n=Polytree.fromMesh(r),i=(i=Array.isArray(e.material)?e.material[0]:e.material).clone(),Polytree.async.subtract(o,n,!1).then(function(e){var r=Polytree.toMesh(e,i);return disposePolytreeResources(o,n,e),r})):e.isPolytree&&r.isPolytree?(o=e,n=r,this.subtractCore(o,n,!0)):(o=Polytree.fromMesh(e),n=Polytree.fromMesh(r),i=(i=Array.isArray(e.material)?e.material[0]:e.material).clone(),t=this.subtractCore(o,n,!1),r=Polytree.toMesh(t,i),disposePolytreeResources(o,n,t),r)},intersectRules={a:[{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["outside","coplanar-back"]},{array:!(Polytree.subtractCore=function(e,r,t=!0){var o,n,i=new Polytree,a=new Set;return e.box.intersectsBox(r.box)?(n=o=void 0,e.mesh&&(o=e.mesh.material.side,e.mesh.material.side=DoubleSide),r.mesh&&(n=r.mesh.material.side,r.mesh.material.side=DoubleSide),e.resetPolygons(!1),r.resetPolygons(!1),e.markIntesectingPolygons(r),r.markIntesectingPolygons(e),handleIntersectingPolytrees(e,r),e.deleteReplacedPolygons(),r.deleteReplacedPolygons(),e.deletePolygonsByStateRules(subtractRules.a),r.deletePolygonsByStateRules(subtractRules.b),r.deletePolygonsByIntersection(!1),r.invert(),e.getPolygonCloneCallback(i.addPolygon.bind(i),a),r.getPolygonCloneCallback(i.addPolygon.bind(i),a),e.mesh&&e.mesh.material.side!==o&&(e.mesh.material.side=o),r.mesh&&r.mesh.material.side!==n&&(r.mesh.material.side=n)):e.getPolygonCloneCallback(i.addPolygon.bind(i),a),a.clear(),a=void 0,i.markPolygonsAsOriginal(),t&&i.buildTree(),i}),rule:"outside"}],b:[{array:!0,rule:["inside","coplanar-front"]},{array:!0,rule:["inside","coplanar-back"]},{array:!0,rule:["outside","coplanar-front"]},{array:!0,rule:["outside","coplanar-back"]},{array:!1,rule:"outside"}]},Polytree.intersect=function(e,r,t=!0){var o,n,i;return t?e.isPolytree&&r.isPolytree?Polytree.async.intersect(e,r,!0):(o=Polytree.fromMesh(e),n=Polytree.fromMesh(r),i=(i=Array.isArray(e.material)?e.material[0]:e.material).clone(),Polytree.async.intersect(o,n,!1).then(function(e){var r=Polytree.toMesh(e,i);return disposePolytreeResources(o,n,e),r})):e.isPolytree&&r.isPolytree?(o=e,n=r,this.intersectCore(o,n,!0)):(o=Polytree.fromMesh(e),n=Polytree.fromMesh(r),i=(i=Array.isArray(e.material)?e.material[0]:e.material).clone(),t=this.intersectCore(o,n,!1),r=Polytree.toMesh(t,i),disposePolytreeResources(o,n,t),r)},Polytree.intersectCore=function(e,r,t=!0){var o,n,i=new Polytree,a=new Set;return e.box.intersectsBox(r.box)&&(n=o=void 0,e.mesh&&(o=e.mesh.material.side,e.mesh.material.side=DoubleSide),r.mesh&&(n=r.mesh.material.side,r.mesh.material.side=DoubleSide),e.resetPolygons(!1),r.resetPolygons(!1),e.markIntesectingPolygons(r),r.markIntesectingPolygons(e),handleIntersectingPolytrees(e,r),e.deleteReplacedPolygons(),r.deleteReplacedPolygons(),e.deletePolygonsByStateRules(intersectRules.a),r.deletePolygonsByStateRules(intersectRules.b),e.deletePolygonsByIntersection(!1),r.deletePolygonsByIntersection(!1),e.getPolygonCloneCallback(i.addPolygon.bind(i),a),r.getPolygonCloneCallback(i.addPolygon.bind(i),a),e.mesh&&e.mesh.material.side!==o&&(e.mesh.material.side=o),r.mesh)&&r.mesh.material.side!==n&&(r.mesh.material.side=n),a.clear(),a=void 0,i.markPolygonsAsOriginal(),t&&i.buildTree(),i},Polytree.async={batchSize:DEFAULT_BUFFER_SIZE,maxWorkerThreads:MAX_WORKER_THREADS,unite:function(n,i,a=!0){return new Promise(function(r,t){var e,o=null;ASYNC_OPERATION_TIMEOUT!==1/0&&(o=setTimeout(function(){return t(new Error(`Union operation timed out after ${ASYNC_OPERATION_TIMEOUT}ms`))},ASYNC_OPERATION_TIMEOUT));try{return e=Polytree.uniteCore(n,i,a),disposePolytreeResources(n,i),o&&clearTimeout(o),r(e)}catch(e){return r=e,o&&clearTimeout(o),disposePolytreeResources(n,i),t(r)}})},subtract:function(n,i,a=!0){return new Promise(function(r,t){var e,o=null;ASYNC_OPERATION_TIMEOUT!==1/0&&(o=setTimeout(function(){return t(new Error(`Subtract operation timed out after ${ASYNC_OPERATION_TIMEOUT}ms`))},ASYNC_OPERATION_TIMEOUT));try{return e=Polytree.subtractCore(n,i,a),disposePolytreeResources(n,i),o&&clearTimeout(o),r(e)}catch(e){return r=e,o&&clearTimeout(o),disposePolytreeResources(n,i),t(r)}})},intersect:function(n,i,a=!0){return new Promise(function(r,t){var e,o=null;ASYNC_OPERATION_TIMEOUT!==1/0&&(o=setTimeout(function(){return t(new Error(`Intersect operation timed out after ${ASYNC_OPERATION_TIMEOUT}ms`))},ASYNC_OPERATION_TIMEOUT));try{return e=Polytree.intersectCore(n,i,a),disposePolytreeResources(n,i),o&&clearTimeout(o),r(e)}catch(e){return r=e,o&&clearTimeout(o),disposePolytreeResources(n,i),t(r)}})},uniteArray:function(e,r=1/0){return Polytree.async.processArrayWithOperation(e,r,Polytree.async.unite,Polytree.async.uniteArray,"union")},subtractArray:function(e,r=1/0){return Polytree.async.processArrayWithOperation(e,r,Polytree.async.subtract,Polytree.async.subtractArray,"subtraction")},intersectArray:function(e,r=1/0){return Polytree.async.processArrayWithOperation(e,r,Polytree.async.intersect,Polytree.async.intersectArray,"intersection")},operation:function(e,r=!1,t=!0,o={objCounter:0},n=!0){return Polytree.operation(e,r,t,o,n,!0)},processArrayWithOperation:function(O,b,N,_,V){return new Promise(function(t,o){var e,r,n,i,a,s,l,c,y,u,h,p,g,d,m,P,f,E,T,I;try{if(I=4<Polytree.async.batchSize&&Polytree.async.batchSize<O.length,h=void 0,l=!1,d=[],I){for(e=[],a=0;a<O.length;)e.push(O.slice(a,a+Polytree.async.batchSize)),a+=Polytree.async.batchSize;for(i=e.shift();i;)r=_(i,0),d.push(r),i=e.shift();l=I=!0,O.length=0}else{for(f=[],g=s=0,E=O.length;0<=E?s<E:E<s;g=0<=E?++s:--s)p=b<g?b:g,n=void 0,O[g].isMesh?n=Polytree.fromMesh(O[g],-1<b?p:void 0):(n=O[g],-1<b&&n.setPolygonIndex(p)),n.polytreeIndex=g,f.push(n);for(h=f.shift(),y=void 0,P=c=0,T=f.length;c<T;P=c+=2){if(f.length<=P+1){y=f[P];break}m=N(f[P],f[P+1]),d.push(m)}y&&(u=N(h,y),d.push(u),l=!0)}return Promise.allSettled(d).then(function(e){var r=[];return e.forEach(function(e){if("fulfilled"===e.status)return r.push(e.value)}),l||r.unshift(h),0<r.length?1===r.length?t(r[0]):3<r.length?_(r,I?0:-1).then(function(e){return t(e)}).catch(function(e){return o(e)}):N(r[0],r[1]).then(function(e){return 3===r.length?N(e,r[2]).then(function(e){return t(e)}).catch(function(e){return o(e)}):t(e)}).catch(function(e){return o(e)}):o(`Unable to find any result polytree after ${V} operation.`)})}catch(e){return o(e)}})}},Plane=class Plane{constructor(e,r){this.normal=e,this.distanceFromOrigin=r}clone(){return new Plane(this.normal.clone(),this.distanceFromOrigin)}flip(){return this.normal.negate(),this.distanceFromOrigin=-this.distanceFromOrigin}equals(e){return this.normal.equals(e.normal)&&this.distanceFromOrigin===e.distanceFromOrigin}delete(){return this.normal=void 0,this.distanceFromOrigin=void 0}static fromPoints(e,r,t){r=temporaryTriangleVertex.copy(r).sub(e).cross(temporaryTriangleVertexSecondary.copy(t).sub(e)).normalize().clone();return new Plane(r,r.dot(e))}},Polygon=class Polygon{constructor(e,r){this.id=polygonID++,this.vertices=e.map(function(e){return e.clone()}),this.shared=r,this.plane=Plane.fromPoints(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.triangle=new Triangle(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.intersects=!1,this.state="undecided",this.previousState="undecided",this.previousStates=[],this.valid=!0,this.coplanar=!1,this.originalValid=!1,this.newPolygon=!1}getMidpoint(){return this.triangle.midPoint||(this.triangle.midPoint=this.triangle.getMidpoint(new Vector3))}applyMatrix(r,t){if(t=t||temporaryMatrixWithNormalCalc.getNormalMatrix(r),this.vertices.forEach(function(e){return e.pos.applyMatrix4(r),e.normal.applyMatrix3(t)}),this.plane.delete(),this.plane=Plane.fromPoints(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.triangle.set(this.vertices[0].pos,this.vertices[1].pos,this.vertices[2].pos),this.triangle.midPoint)return this.triangle.getMidpoint(this.triangle.midPoint)}flip(){var e;return this.vertices.reverse().forEach(function(e){return e.flip()}),e=this.triangle.a,this.triangle.a=this.triangle.c,this.triangle.c=e,this.plane.flip()}reset(e=!0){return this.intersects=!1,this.state="undecided",this.previousState="undecided",this.previousStates.length=0,this.valid=!0,this.coplanar=!1,e&&(this.originalValid=!1),this.newPolygon=!1}setState(e,r){if(this.state!==r)return this.previousState=this.state,"undecided"!==this.state&&this.previousStates.push(this.state),this.state=e}checkAllStates(e){var r,t,o;if(this.state!==e||this.previousState!==e&&"undecided"!==this.previousState)return!1;for(r=0,t=(o=this.previousStates).length;r<t;r++)if(o[r]!==e)return!1;return!0}setInvalid(){return this.valid=!1}setValid(){return this.valid=!0}static createWithDefaultMaterial(e){return new Polygon(e,DEFAULT_MATERIAL_INDEX)}clone(){var e=new Polygon(this.vertices.map(function(e){return e.clone()}),this.shared);return e.intersects=this.intersects,e.valid=this.valid,e.coplanar=this.coplanar,e.state=this.state,e.originalValid=this.originalValid,e.newPolygon=this.newPolygon,e.previousState=this.previousState,e.previousStates=this.previousStates.slice(),this.triangle.midPoint&&(e.triangle.midPoint=this.triangle.midPoint.clone()),e}delete(){return this.vertices.forEach(function(e){return e.delete()}),this.vertices.length=0,this.plane&&(this.plane.delete(),this.plane=void 0),this.triangle=void 0,this.shared=void 0,this.setInvalid()}},isValidTriangle=function(e){return!e.a.equals(e.b)&&!e.a.equals(e.c)&&!e.b.equals(e.c)},isUniqueTriangle=function(e,r,t){var o=`{${e.a.x},${e.a.y},${e.a.z}}-{${e.b.x},${e.b.y},${e.b.z}}-{${e.c.x},${e.c.y},${e.c.z}}`;return!0!==r.has(o)&&(r.add(o),t&&t.set(e,e),!0)},pointsEqual2D=function(e,r,t=TRIANGLE_2D_EPSILON){return Math.abs(e.x-r.x)<=t&&Math.abs(e.y-r.y)<=t},pointOnSegmentInclusive2D=function(e,r,t,o=TRIANGLE_2D_EPSILON){var n,i,a;return pointsEqual2D(r,t,o)?pointsEqual2D(e,r,o)||pointsEqual2D(e,t,o):!(Math.abs(triangleOrientation2D(e,r,t))>o)&&(i=Math.min(r.x,t.x)-o,n=Math.max(r.x,t.x)+o,a=Math.min(r.y,t.y)-o,r=Math.max(r.y,t.y)+o,e.x>=i)&&e.x<=n&&e.y>=a&&e.y<=r},pointInTriangleInclusive2D=function(e,r,t,o,n=TRIANGLE_2D_EPSILON){var i,a,s,l=triangleOrientation2D(r,t,o);return n<l?(i=triangleOrientation2D(e,r,t),a=triangleOrientation2D(e,t,o),s=triangleOrientation2D(e,o,r),-n<=i&&-n<=a&&-n<=s):l<-n?(i=triangleOrientation2D(e,r,t),a=triangleOrientation2D(e,t,o),s=triangleOrientation2D(e,o,r),i<=n&&a<=n&&s<=n):pointsEqual2D(r,t,n)&&pointsEqual2D(t,o,n)?pointsEqual2D(e,r,n):pointsEqual2D(r,t,n)?pointOnSegmentInclusive2D(e,t,o,n):pointsEqual2D(t,o,n)||pointsEqual2D(o,r,n)?pointOnSegmentInclusive2D(e,r,t,n):pointOnSegmentInclusive2D(e,r,t,n)||pointOnSegmentInclusive2D(e,t,o,n)||pointOnSegmentInclusive2D(e,o,r,n)},triangleIntersectsTriangle=function(e,r,t={coplanar:!1,source:new Vector3,target:new Vector3}){var o,n,i,a,s,l,c,y,u=e.a,h=e.b,e=e.c,p=r.a,g=r.b,r=r.c;return temporaryVector3Primary.copy(p).sub(r),temporaryVector3Secondary.copy(g).sub(r),y=(new Vector3).copy(temporaryVector3Primary).cross(temporaryVector3Secondary),temporaryVector3Primary.copy(u).sub(r),o=temporaryVector3Primary.dot(y),temporaryVector3Primary.copy(h).sub(r),i=temporaryVector3Primary.dot(y),temporaryVector3Primary.copy(e).sub(r),s=temporaryVector3Primary.dot(y),!(0<o*i&&0<o*s||(temporaryVector3Primary.copy(h).sub(u),temporaryVector3Secondary.copy(e).sub(u),c=(new Vector3).copy(temporaryVector3Primary).cross(temporaryVector3Secondary),temporaryVector3Primary.copy(p).sub(e),n=temporaryVector3Primary.dot(c),temporaryVector3Primary.copy(g).sub(e),a=temporaryVector3Primary.dot(c),temporaryVector3Primary.copy(r).sub(e),l=temporaryVector3Primary.dot(c),0<n*a&&0<n*l))&&(t.normal1=c,t.normal2=y,0<o?0<i?resolveTriangleIntersection(e,u,h,p,r,g,n,l,a,t):0<s?resolveTriangleIntersection(h,e,u,p,r,g,n,l,a,t):resolveTriangleIntersection(u,h,e,p,g,r,n,a,l,t):o<0?i<0?resolveTriangleIntersection(e,u,h,p,g,r,n,a,l,t):s<0?resolveTriangleIntersection(h,e,u,p,g,r,n,a,l,t):resolveTriangleIntersection(u,h,e,p,r,g,n,l,a,t):i<0?0<=s?resolveTriangleIntersection(h,e,u,p,r,g,n,l,a,t):resolveTriangleIntersection(u,h,e,p,g,r,n,a,l,t):0<i?0<s?resolveTriangleIntersection(u,h,e,p,r,g,n,l,a,t):resolveTriangleIntersection(h,e,u,p,g,r,n,a,l,t):0<s?resolveTriangleIntersection(e,u,h,p,g,r,n,a,l,t):s<0?resolveTriangleIntersection(e,u,h,p,r,g,n,l,a,t):(t.coplanar=!0,resolveCoplanarTriangleIntersection(u,h,e,p,g,r,c,y)))},resolveTriangleIntersection=function(e,r,t,o,n,i,a,s,l,c){return!(0<a&&0<s&&0<l||a<0&&s<0&&l<0)&&(0<a?0<s?constructIntersection(e,t,r,i,o,n,c):0<l?constructIntersection(e,t,r,n,i,o,c):constructIntersection(e,r,t,o,n,i,c):a<0?s<0?constructIntersection(e,r,t,i,o,n,c):l<0?constructIntersection(e,r,t,n,i,o,c):constructIntersection(e,t,r,o,n,i,c):s<0?0<=l?constructIntersection(e,t,r,n,i,o,c):constructIntersection(e,r,t,o,n,i,c):0<s?0<l?constructIntersection(e,t,r,o,n,i,c):constructIntersection(e,r,t,n,i,o,c):0<l?constructIntersection(e,r,t,i,o,n,c):l<0?constructIntersection(e,t,r,i,o,n,c):(c.coplanar=!0,resolveCoplanarTriangleIntersection(e,r,t,o,n,i,c.normal1,c.normal2)))},resolveCoplanarTriangleIntersection=function(e,r,t,o,n,i,a,s){var l=new Vector2,c=new Vector2,y=new Vector2,u=new Vector2,h=new Vector2,p=new Vector2,g=Math.abs(a.x),d=Math.abs(a.y),a=Math.abs(a.z);return a<g&&d<=g?(l.set(e.y,e.z),c.set(r.y,r.z),y.set(t.y,t.z),u.set(o.y,o.z),h.set(n.y,n.z),p.set(i.y,i.z)):a<d&&g<=d?(l.set(e.x,e.z),c.set(r.x,r.z),y.set(t.x,t.z),u.set(o.x,o.z),h.set(n.x,n.z),p.set(i.x,i.z)):(l.set(e.x,e.y),c.set(r.x,r.y),y.set(t.x,t.y),u.set(o.x,o.y),h.set(n.x,n.y),p.set(i.x,i.y)),trianglesOverlap2D(l,c,y,u,h,p)},trianglesOverlap2D=function(e,r,t,o,n,i){var a,s,l,c,y,u,h,p,g,d,m,P,f,E,T;return pointsEqual2D(o,n,TRIANGLE_2D_EPSILON)&&pointsEqual2D(n,i,TRIANGLE_2D_EPSILON)?pointInTriangleInclusive2D(o,e,r,t,TRIANGLE_2D_EPSILON):pointsEqual2D(e,r,TRIANGLE_2D_EPSILON)&&pointsEqual2D(r,t,TRIANGLE_2D_EPSILON)?pointInTriangleInclusive2D(e,o,n,i,TRIANGLE_2D_EPSILON):(u=function(e,r,t){return pointsEqual2D(e,r,TRIANGLE_2D_EPSILON)?[r,t]:(pointsEqual2D(r,t,TRIANGLE_2D_EPSILON),[e,r])},(h=function(e,r,t){return pointsEqual2D(e,r,TRIANGLE_2D_EPSILON)&&!pointsEqual2D(r,t,TRIANGLE_2D_EPSILON)||pointsEqual2D(r,t,TRIANGLE_2D_EPSILON)&&!pointsEqual2D(e,r,TRIANGLE_2D_EPSILON)||pointsEqual2D(t,e,TRIANGLE_2D_EPSILON)&&!pointsEqual2D(e,r,TRIANGLE_2D_EPSILON)})(e,r,t)&&h(o,n,i)?([h,a]=u(e,r,t),[u,c]=u(o,n,i),m=Math.min(h.x,a.x),T=Math.max(h.x,a.x),P=Math.min(h.y,a.y),p=Math.max(h.y,a.y),f=Math.min(u.x,c.x),g=Math.max(u.x,c.x),E=Math.min(u.y,c.y),d=Math.max(u.y,c.y),!(T<f-TRIANGLE_2D_EPSILON||g<m-TRIANGLE_2D_EPSILON||p<E-TRIANGLE_2D_EPSILON||d<P-TRIANGLE_2D_EPSILON||Math.abs(triangleOrientation2D(h,a,u))>TRIANGLE_2D_EPSILON||(T=Math.abs(a.x-h.x),f=Math.abs(a.y-h.y)<=T?(l=Math.min(h.x,a.x)-TRIANGLE_2D_EPSILON,s=Math.max(h.x,a.x)+TRIANGLE_2D_EPSILON,y=Math.min(u.x,c.x)-TRIANGLE_2D_EPSILON,Math.max(u.x,c.x)+TRIANGLE_2D_EPSILON):(l=Math.min(h.y,a.y)-TRIANGLE_2D_EPSILON,s=Math.max(h.y,a.y)+TRIANGLE_2D_EPSILON,y=Math.min(u.y,c.y)-TRIANGLE_2D_EPSILON,Math.max(u.y,c.y)+TRIANGLE_2D_EPSILON),s<y)||f<l)):triangleOrientation2D(e,r,t)<0?triangleOrientation2D(o,n,i)<0?triangleIntersectionCCW2D(e,t,r,o,i,n):triangleIntersectionCCW2D(e,t,r,o,n,i):triangleOrientation2D(o,n,i)<0?triangleIntersectionCCW2D(e,r,t,o,i,n):triangleIntersectionCCW2D(e,r,t,o,n,i))},triangleOrientation2D=function(e,r,t){return(e.x-t.x)*(r.y-t.y)-(e.y-t.y)*(r.x-t.x)},triangleIntersectionCCW2D=function(e,r,t,o,n,i){return 0<=triangleOrientation2D(o,n,e)?0<=triangleOrientation2D(n,i,e)?0<=triangleOrientation2D(i,o,e)||intersectionTestEdge2D(e,r,t,o,n,i):0<=triangleOrientation2D(i,o,e)?intersectionTestEdge2D(e,r,t,i,o,n):intersectionTestVertex2D(e,r,t,o,n,i):0<=triangleOrientation2D(n,i,e)?(0<=triangleOrientation2D(i,o,e)?intersectionTestEdge2D:intersectionTestVertex2D)(e,r,t,n,i,o):intersectionTestVertex2D(e,r,t,i,o,n)},intersectionTestEdge2D=function(e,r,t,o,n,i){for(var a,s,l,c,y,u,h=function(e,r,t,o){var n=Math.min(e.x,r.x),i=Math.max(e.x,r.x),a=Math.min(e.y,r.y),s=Math.max(e.y,r.y),l=Math.min(t.x,o.x),c=Math.max(t.x,o.x),y=Math.min(t.y,o.y),u=Math.max(t.y,o.y);return!(i<l||c<n||s<y||u<a)&&(i=triangleOrientation2D(e,r,t),l=triangleOrientation2D(e,r,o),c=triangleOrientation2D(t,o,e),n=triangleOrientation2D(t,o,r),(0<i&&l<0||i<0&&0<l)&&(0<c&&n<0||c<0&&0<n)||!!(Math.abs(i)<=TRIANGLE_2D_EPSILON&&pointOnSegmentInclusive2D(t,e,r,TRIANGLE_2D_EPSILON)||Math.abs(l)<=TRIANGLE_2D_EPSILON&&pointOnSegmentInclusive2D(o,e,r,TRIANGLE_2D_EPSILON)||Math.abs(c)<=TRIANGLE_2D_EPSILON&&pointOnSegmentInclusive2D(e,t,o,TRIANGLE_2D_EPSILON)||Math.abs(n)<=TRIANGLE_2D_EPSILON&&pointOnSegmentInclusive2D(r,t,o,TRIANGLE_2D_EPSILON)))},p=[[e,r],[r,t],[t,e]],g=[[o,n],[n,i],[i,o]],d=0,m=p.length;d<m;d++)for([a,s]=p[d],y=0,u=g.length;y<u;y++)if([l,c]=g[y],h(a,s,l,c))return!0;return!1},intersectionTestVertex2D=function(e,r,t,o,n,i){return!!pointInTriangleInclusive2D(o,e,r,t,TRIANGLE_2D_EPSILON)||!!pointInTriangleInclusive2D(n,e,r,t,TRIANGLE_2D_EPSILON)||!!(pointInTriangleInclusive2D(i,e,r,t,TRIANGLE_2D_EPSILON)||pointInTriangleInclusive2D(e,o,n,i,TRIANGLE_2D_EPSILON)||pointInTriangleInclusive2D(r,o,n,i,TRIANGLE_2D_EPSILON)||pointInTriangleInclusive2D(t,o,n,i,TRIANGLE_2D_EPSILON))},constructIntersection=function(e,r,t,o,n,i,a){var s=void 0,l=new Vector3;return temporaryVector3Primary.subVectors(r,e),temporaryVector3Secondary.subVectors(i,e),l.copy(temporaryVector3Primary).cross(temporaryVector3Secondary),temporaryVector3Quaternary.subVectors(o,e),0<temporaryVector3Quaternary.dot(l)?(temporaryVector3Primary.subVectors(t,e),l.copy(temporaryVector3Primary).cross(temporaryVector3Secondary),temporaryVector3Quaternary.dot(l)<=0&&(temporaryVector3Secondary.subVectors(n,e),l.copy(temporaryVector3Primary).cross(temporaryVector3Secondary),0<temporaryVector3Quaternary.dot(l)?(temporaryVector3Primary.subVectors(e,o),temporaryVector3Secondary.subVectors(e,t),s=temporaryVector3Primary.dot(a.normal2)/temporaryVector3Secondary.dot(a.normal2),!!isFinite(s)&&(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.source.subVectors(e,temporaryVector3Primary),temporaryVector3Primary.subVectors(o,e),temporaryVector3Secondary.subVectors(o,i),s=temporaryVector3Primary.dot(a.normal1)/temporaryVector3Secondary.dot(a.normal1),!!isFinite(s))&&(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.target.subVectors(o,temporaryVector3Primary),!0)):(temporaryVector3Primary.subVectors(o,e),temporaryVector3Secondary.subVectors(o,n),s=temporaryVector3Primary.dot(a.normal1)/temporaryVector3Secondary.dot(a.normal1),!!isFinite(s)&&(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.source.subVectors(o,temporaryVector3Primary),temporaryVector3Primary.subVectors(o,e),temporaryVector3Secondary.subVectors(o,i),s=temporaryVector3Primary.dot(a.normal1)/temporaryVector3Secondary.dot(a.normal1),!!isFinite(s))&&(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.target.subVectors(o,temporaryVector3Primary),!0)))):(temporaryVector3Secondary.subVectors(n,e),l.copy(temporaryVector3Primary).cross(temporaryVector3Secondary),!(temporaryVector3Quaternary.dot(l)<0||(temporaryVector3Primary.subVectors(t,e),l.copy(temporaryVector3Primary).cross(temporaryVector3Secondary),temporaryVector3Quaternary.dot(l)<0?(temporaryVector3Primary.subVectors(o,e),temporaryVector3Secondary.subVectors(o,n),s=temporaryVector3Primary.dot(a.normal1)/temporaryVector3Secondary.dot(a.normal1),!isFinite(s)||(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.source.subVectors(o,temporaryVector3Primary),temporaryVector3Primary.subVectors(o,e),temporaryVector3Secondary.subVectors(o,i),s=temporaryVector3Primary.dot(a.normal1)/temporaryVector3Secondary.dot(a.normal1),!isFinite(s))||(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.target.subVectors(o,temporaryVector3Primary),0)):(temporaryVector3Primary.subVectors(e,o),temporaryVector3Secondary.subVectors(e,t),s=temporaryVector3Primary.dot(a.normal2)/temporaryVector3Secondary.dot(a.normal2),!isFinite(s)||(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.source.subVectors(e,temporaryVector3Primary),temporaryVector3Primary.subVectors(e,o),temporaryVector3Secondary.subVectors(e,r),s=temporaryVector3Primary.dot(a.normal2)/temporaryVector3Secondary.dot(a.normal2),!isFinite(s))||(temporaryVector3Primary.copy(temporaryVector3Secondary).multiplyScalar(s),a.target.subVectors(e,temporaryVector3Primary),0)))))},Vertex=class Vertex{constructor(e,r,t,o){this.pos=(new Vector3).copy(e),this.normal=(new Vector3).copy(r),t&&(this.uv=(new Vector2).copy(t)),o&&(this.color=(new Vector3).copy(o))}clone(){return new Vertex(this.pos.clone(),this.normal.clone(),this.uv&&this.uv.clone(),this.color&&this.color.clone())}flip(){return this.normal.negate()}interpolate(e,r){return new Vertex(this.pos.clone().lerp(e.pos,r),this.normal.clone().lerp(e.normal,r),this.uv&&e.uv&&this.uv.clone().lerp(e.uv,r),this.color&&e.color&&this.color.clone().lerp(e.color,r))}delete(){return this.pos=void 0,this.normal=void 0,this.uv&&(this.uv=void 0),this.color&&(this.color=void 0)}};export default Polytree;export{GEOMETRIC_EPSILON,RAY_INTERSECTION_EPSILON,TRIANGLE_2D_EPSILON,WINDING_NUMBER_FULL_ROTATION,POLYGON_COPLANAR,POLYGON_FRONT,POLYGON_BACK,POLYGON_SPANNING,temporaryVector3Primary,temporaryVector3Secondary,temporaryVector3Tertiary,temporaryVector3Quaternary,temporaryBoundingBox,temporaryRaycaster,temporaryRay,defaultRayDirection,windingNumberVector1,windingNumberVector2,windingNumberVector3,windingNumberTestPoint,windingNumberEpsilonOffsets,windingNumberEpsilonOffsetsCount,windingNumberMatrix3,rayTriangleEdge1,rayTriangleEdge2,rayTriangleHVector,rayTriangleSVector,rayTriangleQVector,temporaryTriangleVertex,temporaryTriangleVertexSecondary,temporaryMatrix3,temporaryMatrixWithNormalCalc,meshOperationNormalVector,meshOperationVertexVector,POLYTREE_MAX_DEPTH,POLYTREE_MAX_POLYGONS_PER_NODE,POLYTREE_MIN_NODE_SIZE,DEFAULT_MATERIAL_INDEX,MAX_REFINEMENT_ITERATIONS,DEFAULT_COORDINATE_PRECISION,POINT_COINCIDENCE_THRESHOLD,DEFAULT_BUFFER_SIZE,MAX_WORKER_THREADS,ASYNC_OPERATION_TIMEOUT,GARBAGE_COLLECTION_THRESHOLD,MEMORY_USAGE_WARNING_LIMIT,GEOMETRY_CACHE_SIZE,INTERSECTION_CACHE_SIZE,polygonID,DEBUG_VERBOSE_LOGGING,DEBUG_PERFORMANCE_TIMING,DEBUG_GEOMETRY_VALIDATION,DEBUG_INTERSECTION_VERIFICATION,DEBUG_COLOR_FRONT,DEBUG_COLOR_BACK,DEBUG_COLOR_COPLANAR,DEBUG_COLOR_SPANNING,Polytree,Plane,Vertex,Polygon,createVector2Buffer,createVector3Buffer,isValidTriangle,isUniqueTriangle,testRayTriangleIntersection as rayIntersectsTriangle,triangleIntersectsTriangle,resolveTriangleIntersection,resolveCoplanarTriangleIntersection,sortRaycastIntersectionsByDistance,roundPointCoordinates,extractCoordinatesFromArray,splitPolygonByPlane,splitPolygonVertexArray,prepareTriangleBufferFromPolygons,calculateWindingNumberFromBuffer,testPolygonInsideUsingWindingNumber,testRayTriangleIntersection,handleIntersectingPolytrees,disposePolytreeResources,trianglesOverlap2D,triangleOrientation2D,triangleIntersectionCCW2D,intersectionTestEdge2D,intersectionTestVertex2D,constructIntersection,signedVolumeOfTriangle};