1 "use strict";window.ThreeBSP=(
function(){var f,b=0.00001,d=0,c=1,e=2,a=3;f=
function(m){var h,p,l,j,k,g,o,n=[],q;
if(m instanceof THREE.Geometry){this.matrix=
new THREE.Matrix4}
else{
if(m instanceof THREE.Mesh){m.updateMatrix();this.matrix=m.matrix.clone();m=m.geometry}
else{
if(m instanceof f.Node){this.tree=m;this.matrix=
new THREE.Matrix4;
return this}
else{
throw"ThreeBSP: Given geometry is unsupported"}}}
for(h=0,p=m.faces.length;h<p;h++){l=m.faces[h];k=null;o=
new f.Polygon;
if(l instanceof THREE.Face3){j=m.vertices[l.a];g=k?
new THREE.Vector2(k[0].x,k[0].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[0],g);j.applyMatrix4(this.matrix);o.vertices.push(j);j=m.vertices[l.b];g=k?
new THREE.Vector2(k[1].x,k[1].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[1],g);j.applyMatrix4(this.matrix);o.vertices.push(j);j=m.vertices[l.c];g=k?
new THREE.Vector2(k[2].x,k[2].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[2],g);j.applyMatrix4(this.matrix);o.vertices.push(j)}
else{
if(typeof THREE.Face4){j=m.vertices[l.a];g=k?
new THREE.Vector2(k[0].x,k[0].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[0],g);j.applyMatrix4(this.matrix);o.vertices.push(j);j=m.vertices[l.b];g=k?
new THREE.Vector2(k[1].x,k[1].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[1],g);j.applyMatrix4(this.matrix);o.vertices.push(j);j=m.vertices[l.c];g=k?
new THREE.Vector2(k[2].x,k[2].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[2],g);j.applyMatrix4(this.matrix);o.vertices.push(j);j=m.vertices[l.d];g=k?
new THREE.Vector2(k[3].x,k[3].y):null;j=
new f.Vertex(j.x,j.y,j.z,l.vertexNormals[3],g);j.applyMatrix4(this.matrix);o.vertices.push(j)}
else{
throw"Invalid face type at index "+h}}o.calculateProperties();n.push(o)}this.tree=
new f.Node(n)};f.prototype.subtract=
function(h){var i=this.tree.clone(),g=h.tree.clone();i.invert();i.clipTo(g);g.clipTo(i);g.invert();g.clipTo(i);g.invert();i.build(g.allPolygons());i.invert();i=
new f(i);i.matrix=this.matrix;
return i};f.prototype.union=
function(h){var i=this.tree.clone(),g=h.tree.clone();i.clipTo(g);g.clipTo(i);g.invert();g.clipTo(i);g.invert();i.build(g.allPolygons());i=
new f(i);i.matrix=this.matrix;
return i};f.prototype.intersect=
function(h){var i=this.tree.clone(),g=h.tree.clone();i.invert();g.clipTo(i);g.invert();i.clipTo(g);g.clipTo(i);i.build(g.allPolygons());i.invert();i=
new f(i);i.matrix=this.matrix;
return i};f.prototype.toGeometry=
function(){var m,k,u=
new THREE.Matrix4().getInverse(this.matrix),r=
new THREE.Geometry(),v=this.tree.allPolygons(),h=v.length,t,g,w={},s,q,p,n,o,l;
for(m=0;m<h;m++){t=v[m];g=t.vertices.length;
for(k=2;k<g;k++){l=[];n=t.vertices[0];l.push(
new THREE.Vector2(n.uv.x,n.uv.y));n=
new THREE.Vector3(n.x,n.y,n.z);n.applyMatrix4(u);
if(typeof w[n.x+
","+n.y+
","+n.z]!==
"undefined"){s=w[n.x+
","+n.y+
","+n.z]}
else{r.vertices.push(n);s=w[n.x+
","+n.y+
","+n.z]=r.vertices.length-1}n=t.vertices[k-1];l.push(
new THREE.Vector2(n.uv.x,n.uv.y));n=
new THREE.Vector3(n.x,n.y,n.z);n.applyMatrix4(u);
if(typeof w[n.x+
","+n.y+
","+n.z]!==
"undefined"){q=w[n.x+
","+n.y+
","+n.z]}
else{r.vertices.push(n);q=w[n.x+
","+n.y+
","+n.z]=r.vertices.length-1}n=t.vertices[k];l.push(
new THREE.Vector2(n.uv.x,n.uv.y));n=
new THREE.Vector3(n.x,n.y,n.z);n.applyMatrix4(u);
if(typeof w[n.x+
","+n.y+
","+n.z]!==
"undefined"){p=w[n.x+
","+n.y+
","+n.z]}
else{r.vertices.push(n);p=w[n.x+
","+n.y+
","+n.z]=r.vertices.length-1}o=
new THREE.Face3(s,q,p,
new THREE.Vector3(t.normal.x,t.normal.y,t.normal.z));r.faces.push(o);r.faceVertexUvs[0].push(l)}}
return r};f.prototype.toMesh=
function(g){var h=this.toGeometry(),i=
new THREE.Mesh(h,g);i.position.setFromMatrixPosition(this.matrix);i.rotation.setFromRotationMatrix(this.matrix);
return i};f.Polygon=
function(h,i,g){
if(!(h instanceof Array)){h=[]}this.vertices=h;
if(h.length>0){this.calculateProperties()}
else{this.normal=this.w=undefined}};f.Polygon.prototype.calculateProperties=
function(){var h=this.vertices[0],g=this.vertices[1],i=this.vertices[2];this.normal=g.clone().subtract(h).cross(i.clone().subtract(h)).normalize();this.w=this.normal.clone().dot(h);
return this};f.Polygon.prototype.clone=
function(){var j,g,h=
new f.Polygon;
for(j=0,g=this.vertices.length;j<g;j++){h.vertices.push(this.vertices[j].clone())}h.calculateProperties();
return h};f.Polygon.prototype.flip=
function(){var h,g=[];this.normal.multiplyScalar(-1);this.w*=-1;
for(h=this.vertices.length-1;h>=0;h--){g.push(this.vertices[h])}this.vertices=g;
return this};f.Polygon.prototype.classifyVertex=
function(g){var h=this.normal.dot(g)-this.w;
if(h<-b){
return e}
else{
if(h>b){
return c}
else{
return d}}};f.Polygon.prototype.classifySide=
function(k){var j,l,m,n=0,h=0,g=k.vertices.length;
for(j=0;j<g;j++){l=k.vertices[j];m=this.classifyVertex(l);
if(m===c){n++}
else{
if(m===e){h++}}}
if(n>0&&h===0){
return c}
else{
if(n===0&&h>0){
return e}
else{
if(n===0&&h===0){
return d}
else{
return a}}}};f.Polygon.prototype.splitPolygon=
function(y,w,h,l,q){var r=this.classifySide(y);
if(r===d){(this.normal.dot(y.normal)>0?w:h).push(y)}
else{
if(r===c){l.push(y)}
else{
if(r===e){q.push(y)}
else{var s,p,o,k,g,n,m,A,z,u=[],x=[];
for(p=0,s=y.vertices.length;p<s;p++){o=(p+1)%s;n=y.vertices[p];m=y.vertices[o];k=this.classifyVertex(n);g=this.classifyVertex(m);
if(k!=e){u.push(n)}
if(k!=c){x.push(n)}
if((k|g)===a){A=(this.w-this.normal.dot(n))/this.normal.dot(m.clone().subtract(n));z=n.interpolate(m,A);u.push(z);x.push(z)}}
if(u.length>=3){l.push(
new f.Polygon(u).calculateProperties())}
if(x.length>=3){q.push(
new f.Polygon(x).calculateProperties())}}}}};f.Vertex=
function(g,k,j,i,h){this.x=g;this.y=k;this.z=j;this.normal=i||
new THREE.Vector3;this.uv=h||
new THREE.Vector2};f.Vertex.prototype.clone=
function(){
return new f.Vertex(this.x,this.y,this.z,this.normal.clone(),this.uv.clone())};f.Vertex.prototype.add=
function(g){this.x+=g.x;this.y+=g.y;this.z+=g.z;
return this};f.Vertex.prototype.subtract=
function(g){this.x-=g.x;this.y-=g.y;this.z-=g.z;
return this};f.Vertex.prototype.multiplyScalar=
function(g){this.x*=g;this.y*=g;this.z*=g;
return this};f.Vertex.prototype.cross=
function(h){var g=this.x,j=this.y,i=this.z;this.x=j*h.z-i*h.y;this.y=i*h.x-g*h.z;this.z=g*h.y-j*h.x;
return this};f.Vertex.prototype.normalize=
function(){var g=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);this.x/=g;this.y/=g;this.z/=g;
return this};f.Vertex.prototype.dot=
function(g){
return this.x*g.x+this.y*g.y+this.z*g.z};f.Vertex.prototype.lerp=
function(g,h){this.add(g.clone().subtract(
this).multiplyScalar(h));this.normal.add(g.normal.clone().sub(this.normal).multiplyScalar(h));this.uv.add(g.uv.clone().sub(this.uv).multiplyScalar(h));
return this};f.Vertex.prototype.interpolate=
function(g,h){
return this.clone().lerp(g,h)};f.Vertex.prototype.applyMatrix4=
function(h){var g=this.x,k=this.y,j=this.z;var i=h.elements;this.x=i[0]*g+i[4]*k+i[8]*j+i[12];this.y=i[1]*g+i[5]*k+i[9]*j+i[13];this.z=i[2]*g+i[6]*k+i[10]*j+i[14];
return this};f.Node=
function(g){var j,l,k=[],h=[];this.polygons=[];this.front=this.back=undefined;
if(!(g instanceof Array)||g.length===0){
return}this.divider=g[0].clone();
for(j=0,l=g.length;j<l;j++){this.divider.splitPolygon(g[j],this.polygons,this.polygons,k,h)}
if(k.length>0){this.front=
new f.Node(k)}
if(h.length>0){this.back=
new f.Node(h)}};f.Node.isConvex=
function(g){var k,h;
for(k=0;k<g.length;k++){
for(h=0;h<g.length;h++){
if(k!==h&&g[k].classifySide(g[h])!==e){
return false}}}
return true};f.Node.prototype.build=
function(g){var j,l,k=[],h=[];
if(!this.divider){this.divider=g[0].clone()}
for(j=0,l=g.length;j<l;j++){this.divider.splitPolygon(g[j],this.polygons,this.polygons,k,h)}
if(k.length>0){
if(!this.front){this.front=
new f.Node()}this.front.build(k)}
if(h.length>0){
if(!this.back){this.back=
new f.Node()}this.back.build(h)}};f.Node.prototype.allPolygons=
function(){var g=this.polygons.slice();
if(this.front){g=g.concat(this.front.allPolygons())}
if(this.back){g=g.concat(this.back.allPolygons())}
return g};f.Node.prototype.clone=
function(){var g=
new f.Node();g.divider=this.divider.clone();g.polygons=this.polygons.map(
function(h){
return h.clone()});g.front=this.front&&this.front.clone();g.back=this.back&&this.back.clone();
return g};f.Node.prototype.invert=
function(){var h,j,g;
for(h=0,j=this.polygons.length;h<j;h++){this.polygons[h].flip()}this.divider.flip();
if(this.front){this.front.invert()}
if(this.back){this.back.invert()}g=this.front;this.front=this.back;this.back=g;
return this};f.Node.prototype.clipPolygons=
function(g){var j,l,k,h;
if(!this.divider){
return g.slice()}k=[],h=[];
for(j=0,l=g.length;j<l;j++){this.divider.splitPolygon(g[j],k,h,k,h)}
if(this.front){k=this.front.clipPolygons(k)}
if(this.back){h=this.back.clipPolygons(h)}
else{h=[]}
return k.concat(h)};f.Node.prototype.clipTo=
function(g){this.polygons=g.clipPolygons(this.polygons);
if(this.front){this.front.clipTo(g)}
if(this.back){this.back.clipTo(g)}};
return f})();