@jgphilpott/polytree 0.0.7 → 0.0.8

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.
@@ -0,0 +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};