text: /* JSModeler 0.41 - http://www.github.com/kovacsv/JSModeler */ 'use strict';var JSM=function(){this.mainVersion=0;this.subVersion=41};JSM.RandomNumber=function(a,b){return Math.random()*(b-a)+a};JSM.RandomInt=function(a,b){return Math.floor(Math.random()*(b-a+1)+a)};JSM.SeededRandomInt=function(a,b,c){return Math.floor((9301*c+49297)%233280/233280*(b-a+1)+a)};JSM.ValueOrDefault=function(a,b){return void 0===a||null===a?b:a};JSM.PrevIndex=function(a,b){return 0<a?a-1:b-1};JSM.NextIndex=function(a,b){return a<b-1?a+1:0};
  JSM.CopyObjectProperties=function(a,b,c){if(!(void 0===a||null===a||void 0===b||null===b))for(var d in a)if(a.hasOwnProperty(d)&&(c||void 0===b[d]||null===b[d]))b[d]=a[d]};JSM.Message=function(a){console.log("JSModeler: "+a)};JSM.Timer=function(){this.stop=this.start=0};JSM.Timer.prototype.Start=function(){this.start=(new Date).getTime()};JSM.Timer.prototype.Stop=function(){this.end=(new Date).getTime()};JSM.Timer.prototype.Result=function(){return this.end-this.start};
  JSM.FPSCounter=function(){this.current=this.frames=this.start=null};JSM.FPSCounter.prototype.Get=function(a){var b=(new Date).getTime();null===this.start&&(this.start=b,this.current=this.frames=0);if(null===a||void 0===a)a=1E3;this.frames+=1;var c=b-this.start;c>=a&&(this.current=1E3*(this.frames/c),this.start=b,this.frames=0);return parseInt(this.current,10)};JSM.SwapArrayValues=function(a,b,c){var d=a[b];a[b]=a[c];a[c]=d};
  JSM.BubbleSort=function(a,b,c){if(2>a.length||void 0===b||null===b)return!1;if(void 0===c||null===c)c=function(b,c){JSM.SwapArrayValues(a,b,c)};var d,e;for(d=0;d<a.length-1;d++)for(e=0;e<a.length-d-1;e++)b(a[e],a[e+1])||c(e,e+1);return!0};JSM.ShiftArray=function(a,b){var c;for(c=0;c<b;c++)a.push(a.shift())};
  JSM.AsyncRunTask=function(a,b,c,d,e){function f(b,e,l){var k=a();if(void 0!==l.onProgress&&null!==l.onProgress)l.onProgress(b,e);k&&b<c-1?setTimeout(function(){f(b+1,e,l)},d):setTimeout(function(){if(void 0!==l.onFinish&&null!==l.onFinish)l.onFinish(e)},d)}if(void 0===b||null===b)for(b=0;b<c&&!(e=a(),!e);b++);else{if(void 0!==b.onStart&&null!==b.onStart)b.onStart(c,e);f(0,e,b)}};
  JSM.IsWebGLEnabled=function(){if(!window.WebGLRenderingContext)return!1;try{var a=document.createElement("canvas");if(!a.getContext("experimental-webgl")&&!a.getContext("webgl"))return!1}catch(b){return!1}return!0};JSM.IsFileApiEnabled=function(){return!window.File||!window.FileReader||!window.FileList||!window.Blob||!window.URL?!1:!0};
  JSM.LoadJsonFile=function(a,b){var c=new XMLHttpRequest;c.overrideMimeType("application/json");c.open("GET",a,!0);c.onreadystatechange=function(){if(4==c.readyState){var a=JSON.parse(c.responseText);b(a)}};c.send(null)};JSM.Eps=1E-8;JSM.Inf=9999999999;JSM.RadDeg=57.29577951308232;JSM.DegRad=0.017453292519943;JSM.IsZero=function(a){return Math.abs(a)<JSM.Eps};JSM.IsPositive=function(a){return a>JSM.Eps};JSM.IsNegative=function(a){return a<-JSM.Eps};JSM.IsLower=function(a,b){return b-a>JSM.Eps};
  JSM.IsGreater=function(a,b){return a-b>JSM.Eps};JSM.IsEqual=function(a,b){return Math.abs(b-a)<JSM.Eps};JSM.IsEqualWithEps=function(a,b,c){return Math.abs(b-a)<c};JSM.IsLowerOrEqual=function(a,b){return JSM.IsLower(a,b)||JSM.IsEqual(a,b)};JSM.IsGreaterOrEqual=function(a,b){return JSM.IsGreater(a,b)||JSM.IsEqual(a,b)};JSM.Minimum=function(a,b){return JSM.IsLower(a,b)?a:b};JSM.Maximum=function(a,b){return JSM.IsGreater(a,b)?a:b};
  JSM.ArcSin=function(a){return JSM.IsGreaterOrEqual(a,1)?Math.PI/2:JSM.IsLowerOrEqual(a,-1)?-Math.PI/2:Math.asin(a)};JSM.ArcCos=function(a){return JSM.IsGreaterOrEqual(a,1)?0:JSM.IsLowerOrEqual(a,-1)?Math.PI:Math.acos(a)};JSM.Coord2D=function(a,b){this.x=a;this.y=b};JSM.Coord2D.prototype.Set=function(a,b){this.x=a;this.y=b};JSM.Coord2D.prototype.IsEqual=function(a){return JSM.IsEqual(this.x,a.x)&&JSM.IsEqual(this.y,a.y)};
  JSM.Coord2D.prototype.IsEqualWithEps=function(a,b){return JSM.IsEqualWithEps(this.x,a.x,b)&&JSM.IsEqualWithEps(this.y,a.y,b)};JSM.Coord2D.prototype.DistanceTo=function(a){return Math.sqrt((a.x-this.x)*(a.x-this.x)+(a.y-this.y)*(a.y-this.y))};JSM.Coord2D.prototype.AngleTo=function(a){var b=this.Clone().Normalize();a=a.Clone().Normalize();if(b.IsEqual(a))return 0;b=JSM.VectorDot2D(b,a);return JSM.ArcCos(b)};JSM.Coord2D.prototype.Length=function(){return Math.sqrt(this.x*this.x+this.y*this.y)};
  JSM.Coord2D.prototype.MultiplyScalar=function(a){this.x*=a;this.y*=a;return this};JSM.Coord2D.prototype.Normalize=function(){var a=this.Length();JSM.IsPositive(a)&&this.MultiplyScalar(1/a);return this};JSM.Coord2D.prototype.SetLength=function(a){var b=this.Length();JSM.IsPositive(b)&&this.MultiplyScalar(a/b);return this};JSM.Coord2D.prototype.Offset=function(a,b){var c=a.Clone().Normalize();this.x+=c.x*b;this.y+=c.y*b;return this};
  JSM.Coord2D.prototype.Rotate=function(a,b){var c=this.x-b.x,d=this.y-b.y,e=Math.cos(a),f=Math.sin(a);this.x=c*e-d*f+b.x;this.y=c*f+d*e+b.y;return this};JSM.Coord2D.prototype.ToString=function(){return"("+this.x+", "+this.y+")"};JSM.Coord2D.prototype.Clone=function(){return new JSM.Coord2D(this.x,this.y)};JSM.Vector2D=JSM.Coord2D;JSM.CoordFromArray2D=function(a){return new JSM.Coord2D(a[0],a[1])};JSM.CoordToArray2D=function(a){return[a.x,a.y]};
  JSM.CoordAdd2D=function(a,b){return new JSM.Coord2D(a.x+b.x,a.y+b.y)};JSM.CoordSub2D=function(a,b){return new JSM.Coord2D(a.x-b.x,a.y-b.y)};JSM.VectorDot2D=function(a,b){return a.x*b.x+a.y*b.y};JSM.Coord=function(a,b,c){this.x=a;this.y=b;this.z=c};JSM.Coord.prototype.Set=function(a,b,c){this.x=a;this.y=b;this.z=c};JSM.Coord.prototype.IsEqual=function(a){return JSM.IsEqual(this.x,a.x)&&JSM.IsEqual(this.y,a.y)&&JSM.IsEqual(this.z,a.z)};
  JSM.Coord.prototype.IsEqualWithEps=function(a,b){return JSM.IsEqualWithEps(this.x,a.x,b)&&JSM.IsEqualWithEps(this.y,a.y,b)&&JSM.IsEqualWithEps(this.z,a.z,b)};JSM.Coord.prototype.DistanceTo=function(a){return Math.sqrt((a.x-this.x)*(a.x-this.x)+(a.y-this.y)*(a.y-this.y)+(a.z-this.z)*(a.z-this.z))};JSM.Coord.prototype.AngleTo=function(a){var b=this.Clone().Normalize();a=a.Clone().Normalize();if(b.IsEqual(a))return 0;b=JSM.VectorDot(b,a);return JSM.ArcCos(b)};
  JSM.Coord.prototype.IsCollinearWith=function(a){a=this.AngleTo(a);return JSM.IsEqual(a,0)||JSM.IsEqual(a,Math.PI)};JSM.Coord.prototype.IsPerpendicularWith=function(a){a=this.AngleTo(a);return JSM.IsEqual(a,Math.PI/2)};JSM.Coord.prototype.Length=function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)};JSM.Coord.prototype.Add=function(a){this.x+=a.x;this.y+=a.y;this.z+=a.z};JSM.Coord.prototype.Sub=function(a){this.x-=a.x;this.y-=a.y;this.z-=a.z};
  JSM.Coord.prototype.MultiplyScalar=function(a){this.x*=a;this.y*=a;this.z*=a;return this};JSM.Coord.prototype.Normalize=function(){var a=this.Length();JSM.IsPositive(a)&&this.MultiplyScalar(1/a);return this};JSM.Coord.prototype.SetLength=function(a){var b=this.Length();JSM.IsPositive(b)&&this.MultiplyScalar(a/b);return this};JSM.Coord.prototype.Offset=function(a,b){var c=a.Clone().Normalize();this.x+=c.x*b;this.y+=c.y*b;this.z+=c.z*b;return this};
  JSM.Coord.prototype.Rotate=function(a,b,c){var d=a.Clone().Normalize();a=d.x;var e=d.y,d=d.z,f=this.x-c.x,g=this.y-c.y,h=this.z-c.z,l=Math.sin(b);b=Math.cos(b);this.x=-a*(-a*f-e*g-d*h)*(1-b)+f*b+(-d*g+e*h)*l;this.y=-e*(-a*f-e*g-d*h)*(1-b)+g*b+(d*f-a*h)*l;this.z=-d*(-a*f-e*g-d*h)*(1-b)+h*b+(-e*f+a*g)*l;this.x+=c.x;this.y+=c.y;this.z+=c.z;return this};
  JSM.Coord.prototype.ToCoord2D=function(a){var b=new JSM.Coord(0,0,0),c=new JSM.Vector(0,0,1),d=JSM.VectorCross(a,c);a=a.AngleTo(c);b=this.Clone().Rotate(d,a,b);return new JSM.Coord2D(b.x,b.y)};JSM.Coord.prototype.ToString=function(){return"("+this.x+", "+this.y+", "+this.z+")"};JSM.Coord.prototype.Clone=function(){return new JSM.Coord(this.x,this.y,this.z)};JSM.Vector=JSM.Coord;JSM.CoordFromArray=function(a){return new JSM.Coord(a[0],a[1],a[2])};JSM.CoordToArray=function(a){return[a.x,a.y,a.z]};
  JSM.CoordAdd=function(a,b){return new JSM.Coord(a.x+b.x,a.y+b.y,a.z+b.z)};JSM.CoordSub=function(a,b){return new JSM.Coord(a.x-b.x,a.y-b.y,a.z-b.z)};JSM.VectorDot=function(a,b){return a.x*b.x+a.y*b.y+a.z*b.z};JSM.VectorCross=function(a,b){var c=new JSM.Vector(0,0,0);c.x=a.y*b.z-a.z*b.y;c.y=a.z*b.x-a.x*b.z;c.z=a.x*b.y-a.y*b.x;return c};JSM.MatrixDeterminant2x2=function(a,b,c,d){return a*d-b*c};
  JSM.MatrixDeterminant3x3=function(a,b,c,d,e,f,g,h,l){var k=JSM.MatrixDeterminant2x2(e,f,h,l);f=JSM.MatrixDeterminant2x2(d,f,g,l);d=JSM.MatrixDeterminant2x2(d,e,g,h);return a*k-b*f+c*d};JSM.MatrixDeterminant4x4=function(a,b,c,d,e,f,g,h,l,k,m,n,p,q,r,s){var v=JSM.MatrixDeterminant3x3(f,g,h,k,m,n,q,r,s),u=JSM.MatrixDeterminant3x3(e,g,h,l,m,n,p,r,s);h=JSM.MatrixDeterminant3x3(e,f,h,l,k,n,p,q,s);e=JSM.MatrixDeterminant3x3(e,f,g,l,k,m,p,q,r);return v*a-u*b+h*c-e*d};
  JSM.Orientation={Invalid:0,CounterClockwise:1,Clockwise:2};JSM.MidCoord2D=function(a,b){return new JSM.Coord2D((a.x+b.x)/2,(a.y+b.y)/2)};JSM.CoordOrientation2D=function(a,b,c){var d=a.x;a=a.y;var e=b.x;b=b.y;var f=c.x;c=c.y;d=d*b+a*f+e*c-b*f-a*e-d*c;return JSM.IsPositive(d)?JSM.Orientation.CounterClockwise:JSM.IsNegative(d)?JSM.Orientation.Clockwise:JSM.Orientation.Invalid};
  JSM.CoordSignedDistance2D=function(a,b,c){var d=JSM.CoordSub2D(b,a);a=a.DistanceTo(b);c=d.AngleTo(c);JSM.IsPositive(c)&&(a=-a);return a};JSM.PolarToCartesian=function(a,b){var c=new JSM.Coord2D(0,0);c.x=a*Math.cos(b);c.y=a*Math.sin(b);return c};JSM.GetArcLengthFromAngle=function(a,b){return b*a};JSM.GetAngleFromArcLength=function(a,b){return JSM.IsEqual(a,0)?0:b/a};JSM.MidCoord=function(a,b){return new JSM.Coord((a.x+b.x)/2,(a.y+b.y)/2,(a.z+b.z)/2)};
  JSM.CoordSignedDistance=function(a,b,c){var d=JSM.CoordSub(b,a);a=a.DistanceTo(b);c=d.AngleTo(c);JSM.IsPositive(c)&&(a=-a);return a};JSM.GetVectorsFullAngle=function(a,b,c){var d=a.AngleTo(b),e=new JSM.Coord(0,0,0);JSM.CoordOrientation(a,e,b,c)==JSM.Orientation.Clockwise&&(d=2*Math.PI-d);return d};
  JSM.CoordOrientation=function(a,b,c,d){a=a.ToCoord2D(d);b=b.ToCoord2D(d);c=c.ToCoord2D(d);c=JSM.CoordOrientation2D(a,b,c);a=new JSM.Vector(0,0,1);d=d.AngleTo(a);JSM.IsEqual(d,Math.PI)&&(c==JSM.Orientation.CounterClockwise?c=JSM.Orientation.Clockwise:c==JSM.Orientation.Clockwise&&(c=JSM.Orientation.CounterClockwise));return c};JSM.SphericalToCartesian=function(a,b,c){var d=new JSM.Coord(0,0,0);d.x=a*Math.sin(b)*Math.cos(c);d.y=a*Math.sin(b)*Math.sin(c);d.z=a*Math.cos(b);return d};
  JSM.CylindricalToCartesian=function(a,b,c){var d=new JSM.Coord(0,0,0);d.x=a*Math.cos(c);d.y=a*Math.sin(c);d.z=b;return d};JSM.GetArcLength=function(a,b,c){return a.AngleTo(b)*c};JSM.GetFullArcLength=function(a,b,c,d){return JSM.GetVectorsFullAngle(a,b,d)*c};JSM.CalculateCentroid=function(a){var b=a.length,c=new JSM.Coord(0,0,0);if(1<=b){var d;for(d=0;d<b;d++)c=JSM.CoordAdd(c,a[d]);c.MultiplyScalar(1/b)}return c};
  JSM.CalculateTriangleNormal=function(a,b,c){b=JSM.CoordSub(b,a);a=JSM.CoordSub(c,a);c=new JSM.Vector(0,0,0);c.x=b.y*a.z-b.z*a.y;c.y=b.z*a.x-b.x*a.z;c.z=b.x*a.y-b.y*a.x;c.Normalize();return c};JSM.CalculateNormal=function(a){var b=a.length,c=new JSM.Vector(0,0,0);if(3<=b){var d,e,f;for(d=0;d<b;d++)e=d%b,f=(d+1)%b,e=a[e],f=a[f],c.x+=(e.y-f.y)*(e.z+f.z),c.y+=(e.z-f.z)*(e.x+f.x),c.z+=(e.x-f.x)*(e.y+f.y)}c.Normalize();return c};
  JSM.BarycentricInterpolation=function(a,b,c,d,e,f,g){function h(a,b,c){var d=(a+b+c)/2;a=d*(d-a)*(d-b)*(d-c);return 0>a?0:Math.sqrt(a)}var l=a.DistanceTo(b),k=b.DistanceTo(c),m=c.DistanceTo(a);a=a.DistanceTo(g);b=b.DistanceTo(g);g=c.DistanceTo(g);c=h(l,k,m);if(JSM.IsZero(c))return d;l=h(l,a,b);k=h(k,b,g);m=h(m,a,g);d=d.Clone().MultiplyScalar(k);e=e.Clone().MultiplyScalar(m);f=f.Clone().MultiplyScalar(l);f=JSM.CoordAdd(JSM.CoordAdd(d,e),f);f.MultiplyScalar(1/c);return f};
  JSM.MatrixIdentity=function(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]};JSM.MatrixClone=function(a){var b=[];b[0]=a[0];b[1]=a[1];b[2]=a[2];b[3]=a[3];b[4]=a[4];b[5]=a[5];b[6]=a[6];b[7]=a[7];b[8]=a[8];b[9]=a[9];b[10]=a[10];b[11]=a[11];b[12]=a[12];b[13]=a[13];b[14]=a[14];b[15]=a[15];return b};
  JSM.MatrixTranspose=function(a){var b=[];b[0]=a[0];b[1]=a[4];b[2]=a[8];b[3]=a[12];b[4]=a[1];b[5]=a[5];b[6]=a[9];b[7]=a[13];b[8]=a[2];b[9]=a[6];b[10]=a[10];b[11]=a[14];b[12]=a[3];b[13]=a[7];b[14]=a[11];b[15]=a[15];return b};JSM.MatrixVectorMultiply=function(a,b){var c=b[0],d=b[1],e=b[2],f=b[3],g=a[1],h=a[2],l=a[3],k=a[5],m=a[6],n=a[7],p=a[9],q=a[10],r=a[11],s=a[13],v=a[14],u=a[15],t=[];t[0]=c*a[0]+d*a[4]+e*a[8]+f*a[12];t[1]=c*g+d*k+e*p+f*s;t[2]=c*h+d*m+e*q+f*v;t[3]=c*l+d*n+e*r+f*u;return t};
  JSM.MatrixMultiply=function(a,b){var c=a[0],d=a[1],e=a[2],f=a[3],g=a[4],h=a[5],l=a[6],k=a[7],m=a[8],n=a[9],p=a[10],q=a[11],r=a[12],s=a[13],v=a[14],u=a[15],t=b[0],w=b[1],A=b[2],B=b[3],x=b[4],y=b[5],z=b[6],E=b[7],H=b[8],I=b[9],D=b[10],C=b[11],K=b[12],L=b[13],F=b[14],J=b[15],G=[];G[0]=c*t+d*x+e*H+f*K;G[1]=c*w+d*y+e*I+f*L;G[2]=c*A+d*z+e*D+f*F;G[3]=c*B+d*E+e*C+f*J;G[4]=g*t+h*x+l*H+k*K;G[5]=g*w+h*y+l*I+k*L;G[6]=g*A+h*z+l*D+k*F;G[7]=g*B+h*E+l*C+k*J;G[8]=m*t+n*x+p*H+q*K;G[9]=m*w+n*y+p*I+q*L;G[10]=m*A+n*z+
  p*D+q*F;G[11]=m*B+n*E+p*C+q*J;G[12]=r*t+s*x+v*H+u*K;G[13]=r*w+s*y+v*I+u*L;G[14]=r*A+s*z+v*D+u*F;G[15]=r*B+s*E+v*C+u*J;return G};JSM.MatrixDeterminant=function(a){var b=a[0],c=a[1],d=a[2],e=a[3],f=a[4],g=a[5],h=a[6],l=a[7],k=a[8],m=a[9],n=a[10],p=a[11],q=a[12],r=a[13],s=a[14];a=a[15];return(b*g-c*f)*(n*a-p*s)-(b*h-d*f)*(m*a-p*r)+(b*l-e*f)*(m*s-n*r)+(c*h-d*g)*(k*a-p*q)-(c*l-e*g)*(k*s-n*q)+(d*l-e*h)*(k*r-m*q)};
  JSM.MatrixInvert=function(a){var b=a[0],c=a[1],d=a[2],e=a[3],f=a[4],g=a[5],h=a[6],l=a[7],k=a[8],m=a[9],n=a[10],p=a[11],q=a[12],r=a[13],s=a[14];a=a[15];var v=b*g-c*f,u=b*h-d*f,t=b*l-e*f,w=c*h-d*g,A=c*l-e*g,B=d*l-e*h,x=k*r-m*q,y=k*s-n*q,z=k*a-p*q,E=m*s-n*r,H=m*a-p*r,I=n*a-p*s,D=v*I-u*H+t*E+w*z-A*y+B*x;if(JSM.IsZero(D))return null;var C=[];C[0]=(g*I-h*H+l*E)/D;C[1]=(d*H-c*I-e*E)/D;C[2]=(r*B-s*A+a*w)/D;C[3]=(n*A-m*B-p*w)/D;C[4]=(h*z-f*I-l*y)/D;C[5]=(b*I-d*z+e*y)/D;C[6]=(s*t-q*B-a*u)/D;C[7]=(k*B-n*t+p*
  u)/D;C[8]=(f*H-g*z+l*x)/D;C[9]=(c*z-b*H-e*x)/D;C[10]=(q*A-r*t+a*v)/D;C[11]=(m*t-k*A-p*v)/D;C[12]=(g*y-f*E-h*x)/D;C[13]=(b*E-c*y+d*x)/D;C[14]=(r*u-q*w-s*v)/D;C[15]=(k*w-m*u+n*v)/D;return C};JSM.MatrixTranslation=function(a,b,c){var d=[1,0,0,0,0,1,0,0,0,0,1,0];d[12]=a;d[13]=b;d[14]=c;d[15]=1;return d};
  JSM.MatrixRotation=function(a,b,c){var d=a.Clone().Normalize();a=d.x;var e=d.y,d=d.z,f=a*a,g=e*e,h=d*d,l=Math.sin(b);b=Math.cos(b);var k=[];if(void 0===c||null===c)k[0]=f+(g+h)*b,k[1]=a*e*(1-b)+d*l,k[2]=a*d*(1-b)-e*l,k[3]=0,k[4]=a*e*(1-b)-d*l,k[5]=g+(f+h)*b,k[6]=e*d*(1-b)+a*l,k[7]=0,k[8]=a*d*(1-b)+e*l,k[9]=e*d*(1-b)-a*l,k[10]=h+(f+g)*b,k[11]=0,k[12]=0,k[13]=0,k[14]=0;else{var m=c.x,n=c.y;c=c.z;k[0]=f+(g+h)*b;k[1]=a*e*(1-b)+d*l;k[2]=a*d*(1-b)-e*l;k[3]=0;k[4]=a*e*(1-b)-d*l;k[5]=g+(f+h)*b;k[6]=e*d*(1-
  b)+a*l;k[7]=0;k[8]=a*d*(1-b)+e*l;k[9]=e*d*(1-b)-a*l;k[10]=h+(f+g)*b;k[11]=0;k[12]=(m*(g+h)-a*(n*e+c*d))*(1-b)+(n*d-c*e)*l;k[13]=(n*(f+h)-e*(m*a+c*d))*(1-b)+(c*a-m*d)*l;k[14]=(c*(f+g)-d*(m*a+n*e))*(1-b)+(m*e-n*a)*l}k[15]=1;return k};
  JSM.MatrixRotationQuaternion=function(a){var b=a[0],c=a[1],d=a[2],e=a[3],f=b+b,g=c+c,h=d+d;a=b*f;var l=b*g,b=b*h,k=c*g,c=c*h,d=d*h,f=e*f,g=e*g,e=e*h,h=[];h[0]=1-(k+d);h[1]=l+e;h[2]=b-g;h[3]=0;h[4]=l-e;h[5]=1-(a+d);h[6]=c+f;h[7]=0;h[8]=b+g;h[9]=c-f;h[10]=1-(a+k);h[11]=0;h[12]=0;h[13]=0;h[14]=0;h[15]=1;return h};JSM.MatrixRotationX=function(a){var b=Math.sin(a);a=Math.cos(a);var c=[1,0,0,0,0];c[5]=a;c[6]=b;c[7]=0;c[8]=0;c[9]=-b;c[10]=a;c[11]=0;c[12]=0;c[13]=0;c[14]=0;c[15]=1;return c};
  JSM.MatrixRotationY=function(a){var b=Math.sin(a);a=Math.cos(a);var c=[];c[0]=a;c[1]=0;c[2]=-b;c[3]=0;c[4]=0;c[5]=1;c[6]=0;c[7]=0;c[8]=b;c[9]=0;c[10]=a;c[11]=0;c[12]=0;c[13]=0;c[14]=0;c[15]=1;return c};JSM.MatrixRotationZ=function(a){var b=Math.sin(a);a=Math.cos(a);var c=[];c[0]=a;c[1]=b;c[2]=0;c[3]=0;c[4]=-b;c[5]=a;c[6]=0;c[7]=0;c[8]=0;c[9]=0;c[10]=1;c[11]=0;c[12]=0;c[13]=0;c[14]=0;c[15]=1;return c};
  JSM.ApplyTransformation=function(a,b){var c=[];c[0]=b.x;c[1]=b.y;c[2]=b.z;c[3]=1;c=JSM.MatrixVectorMultiply(a,c);return new JSM.Coord(c[0],c[1],c[2])};JSM.ApplyRotation=function(a,b){var c=[];c[0]=b.x;c[1]=b.y;c[2]=b.z;c[3]=0;c=JSM.MatrixVectorMultiply(a,c);return new JSM.Coord(c[0],c[1],c[2])};JSM.CoordSystem=function(a,b,c,d){this.origo=a;this.e1=b;this.e2=c;this.e3=d};JSM.CoordSystem.prototype.Set=function(a,b,c,d){this.origo=a;this.e1=b;this.e2=c;this.e3=d};
  JSM.CoordSystem.prototype.ToDirectionVectors=function(){this.e1=JSM.CoordSub(this.e1,this.origo);this.e2=JSM.CoordSub(this.e2,this.origo);this.e3=JSM.CoordSub(this.e3,this.origo);return this};JSM.CoordSystem.prototype.ToAbsoluteCoords=function(){this.e1=JSM.CoordAdd(this.e1,this.origo);this.e2=JSM.CoordAdd(this.e2,this.origo);this.e3=JSM.CoordAdd(this.e3,this.origo);return this};
  JSM.CoordSystem.prototype.Clone=function(){return new JSM.CoordSystem(this.origo.Clone(),this.e1.Clone(),this.e2.Clone(),this.e3.Clone())};JSM.CoordSectorPosition2D={CoordInsideOfSector:0,CoordOnSectorEndCoord:1,CoordOutsideOfSector:2};JSM.SectorSectorPosition2D={SectorsDontIntersect:0,SectorsIntersectCoincident:1,SectorsIntersectEndPoint:2,SectorsIntersectOnePoint:3};JSM.CoordSectorPosition={CoordInsideOfSector:0,CoordOnSectorEndCoord:1,CoordOutsideOfSector:2};
  JSM.Sector2D=function(a,b){this.beg=a;this.end=b};JSM.Sector2D.prototype.Set=function(a,b){this.beg=a;this.end=b};JSM.Sector2D.prototype.GetLength=function(){return this.beg.DistanceTo(this.end)};
  JSM.Sector2D.prototype.CoordPosition=function(a){var b=a.x,c=a.y,d=this.beg.x,e=this.beg.y,f=this.end.x,g=this.end.y,h=this.GetLength();if(JSM.IsZero(h))return a.IsEqual(this.beg)?JSM.CoordSectorPosition2D.CoordOnSectorEndCoord:JSM.CoordSectorPosition2D.CoordOutsideOfSector;a=((b-d)*(f-d)+(c-e)*(g-e))/(h*h);if(JSM.IsLower(a,0)||JSM.IsGreater(a,1))return JSM.CoordSectorPosition2D.CoordOutsideOfSector;e+=a*(g-e);return!JSM.IsEqual(d+a*(f-d),b)||!JSM.IsEqual(e,c)?JSM.CoordSectorPosition2D.CoordOutsideOfSector:
  JSM.IsEqual(a,0)||JSM.IsEqual(a,1)?JSM.CoordSectorPosition2D.CoordOnSectorEndCoord:JSM.CoordSectorPosition2D.CoordInsideOfSector};
  JSM.Sector2D.prototype.SectorPosition=function(a,b){function c(a,b,c){return!c.IsEqual(a)&&!c.IsEqual(b)&&JSM.IsLowerOrEqual(c.x,Math.max(a.x,b.x))&&JSM.IsLowerOrEqual(c.y,Math.max(a.y,b.y))&&JSM.IsGreaterOrEqual(c.x,Math.min(a.x,b.x))&&JSM.IsGreaterOrEqual(c.y,Math.min(a.y,b.y))?!0:!1}var d=void 0!==b&&null!==b,e=this.beg,f=this.end,g=a.beg,h=a.end,l=e.IsEqual(g)||e.IsEqual(h),k=f.IsEqual(g)||f.IsEqual(h);if(l&&k)return JSM.SectorSectorPosition2D.SectorsIntersectCoincident;var m=e.x,n=e.y,p=f.x,
  q=f.y,r=g.x,s=g.y,v=h.x,u=h.y,t=(v-r)*(n-s)-(u-s)*(m-r),w=(p-m)*(n-s)-(q-n)*(m-r),r=(u-s)*(p-m)-(v-r)*(q-n);if(JSM.IsZero(r)){if(JSM.IsZero(t)&&JSM.IsZero(w)){if(c(e,f,g)||c(e,f,h)||c(g,h,e)||c(g,h,f))return JSM.SectorSectorPosition2D.SectorsIntersectCoincident;if(l)return d&&(b.x=e.x,b.y=e.y),JSM.SectorSectorPosition2D.SectorsIntersectEndPoint;if(k)return d&&(b.x=f.x,b.y=f.y),JSM.SectorSectorPosition2D.SectorsIntersectEndPoint}return JSM.SectorSectorPosition2D.SectorsDontIntersect}g=t/r;w/=r;if(JSM.IsLower(g,
  0)||JSM.IsGreater(g,1)||JSM.IsLower(w,0)||JSM.IsGreater(w,1))return JSM.SectorSectorPosition2D.SectorsDontIntersect;if(l)return d&&(b.x=e.x,b.y=e.y),JSM.SectorSectorPosition2D.SectorsIntersectEndPoint;if(k)return d&&(b.x=f.x,b.y=f.y),JSM.SectorSectorPosition2D.SectorsIntersectEndPoint;d&&(b.x=m+g*(p-m),b.y=n+g*(q-n));return JSM.SectorSectorPosition2D.SectorsIntersectOnePoint};
  JSM.Sector2D.prototype.ProjectCoord=function(a){var b=a.x,c=a.y;a=this.beg;var d=this.end,e=a.x,f=a.y,g=d.x,h=d.y,l=(g-e)*(g-e)+(h-f)*(h-f);if(JSM.IsZero(l))return a.Clone();b=((g-e)*(b-e)+(h-f)*(c-f))/l;if(JSM.IsLower(b,0))return a.Clone();if(JSM.IsGreater(b,1))return d.Clone();b=JSM.CoordSub2D(d,a).MultiplyScalar(b);return JSM.CoordAdd2D(a,b)};JSM.Sector2D.prototype.Clone=function(){return new JSM.Sector2D(this.beg.Clone(),this.end.Clone())};JSM.Sector=function(a,b){this.beg=a;this.end=b};
  JSM.Sector.prototype.Set=function(a,b){this.beg=a;this.end=b};JSM.Sector.prototype.GetLength=function(){return this.beg.DistanceTo(this.end)};
  JSM.Sector.prototype.CoordPosition=function(a){var b=a.x,c=a.y,d=a.z,e=this.beg,f=JSM.CoordSub(this.end,this.beg),g=e.x,h=e.y,l=e.z,k=e.x+f.x,m=e.y+f.y,n=e.z+f.z,p=(k-g)*(k-g)+(m-h)*(m-h)+(n-l)*(n-l);if(JSM.IsZero(p))return e.IsEqual(a)?JSM.CoordSectorPosition.CoordOnSectorEndCoord:JSM.CoordSectorPosition.CoordOutsideOfSector;b=((k-g)*(b-g)+(m-h)*(c-h)+(n-l)*(d-l))/p;f=f.Clone().MultiplyScalar(b);e=JSM.CoordAdd(e,f);a=a.DistanceTo(e);return JSM.IsZero(a)?JSM.IsLower(b,0)||JSM.IsGreater(b,1)?JSM.CoordSectorPosition.CoordOutsideOfSector:
  JSM.IsEqual(b,0)||JSM.IsEqual(b,1)?JSM.CoordSectorPosition.CoordOnSectorEndCoord:JSM.CoordSectorPosition.CoordInsideOfSector:JSM.CoordSectorPosition.CoordOutsideOfSector};JSM.Sector.prototype.Clone=function(){return new JSM.Sector(this.beg.Clone(),this.end.Clone())};JSM.GetSectorSegmentation2D=function(a,b){var c=JSM.CoordSub2D(a.end,a.beg),d=a.beg.DistanceTo(a.end)/b,e=0,f=[],g,h;for(g=0;g<=b;g++)h=a.beg.Clone().Offset(c,e),f.push(h),e+=d;return f};
  JSM.GetSectorSegmentation=function(a,b){var c=JSM.CoordSub(a.end,a.beg),d=a.beg.DistanceTo(a.end)/b,e=0,f=[],g,h;for(g=0;g<=b;g++)h=a.beg.Clone().Offset(c,e),f.push(h),e+=d;return f};JSM.CoordLinePosition2D={CoordOnLine:0,CoordAtLineLeft:1,CoordAtLineRight:2};JSM.LineLinePosition2D={LinesDontIntersect:0,LinesIntersectsOnePoint:1,LinesIntersectsCoincident:2};JSM.CoordLinePosition={CoordOnLine:0,CoordOutsideOfLine:1};JSM.LineLinePosition={LinesDontIntersect:0,LinesIntersectsOnePoint:1,LinesIntersectsCoincident:2};
  JSM.Line2D=function(a,b){this.start=a;this.direction=b};JSM.Line2D.prototype.Set=function(a,b){this.start=a;this.direction=b};JSM.Line2D.prototype.CoordPosition=function(a){a=this.CoordSignedDistance(a);return JSM.IsPositive(a)?JSM.CoordLinePosition2D.CoordAtLineLeft:JSM.IsNegative(a)?JSM.CoordLinePosition2D.CoordAtLineRight:JSM.CoordLinePosition2D.CoordOnLine};JSM.Line2D.prototype.CoordSignedDistance=function(a){var b=this.start,c=this.direction;return c.x*(a.y-b.y)-c.y*(a.x-b.x)};
  JSM.Line2D.prototype.LinePosition=function(a,b){var c=this.start.x,d=this.start.y,e=this.start.x+this.direction.x,f=this.start.y+this.direction.y,g=a.start.x,h=a.start.y,l=a.start.x+a.direction.x,k=a.start.y+a.direction.y,m=(l-g)*(d-h)-(k-h)*(c-g),n=(e-c)*(d-h)-(f-d)*(c-g),g=(k-h)*(e-c)-(l-g)*(f-d);if(JSM.IsZero(g))return JSM.IsZero(m)&&JSM.IsZero(n)?JSM.LineLinePosition2D.LinesIntersectsCoincident:JSM.LineLinePosition2D.LinesDontIntersect;m/=g;null!==b&&(b.x=c+m*(e-c),b.y=d+m*(f-d));return JSM.LineLinePosition2D.LinesIntersectsOnePoint};
  JSM.Line2D.prototype.Clone=function(){return new JSM.Line2D(this.start.Clone(),this.direction.Clone())};JSM.Line=function(a,b){this.start=a;this.direction=b};JSM.Line.prototype.Set=function(a,b){this.start=a;this.direction=b};
  JSM.Line.prototype.CoordPosition=function(a,b){var c=a.x,d=a.y,e=a.z,f=this.start,g=this.direction,h=f.x,l=f.y,k=f.z,m=f.x+g.x,n=f.y+g.y,p=f.z+g.z,q=(m-h)*(m-h)+(n-l)*(n-l)+(p-k)*(p-k);if(JSM.IsZero(q))return void 0!==b&&b.Set(f.x,f.y,f.z),f.IsEqual(a)?JSM.CoordLinePosition.CoordOnLine:JSM.CoordLinePosition.CoordOutsideOfLine;c=g.Clone().MultiplyScalar(((m-h)*(c-h)+(n-l)*(d-l)+(p-k)*(e-k))/q);f=JSM.CoordAdd(f,c);void 0!==b&&b.Set(f.x,f.y,f.z);f=a.DistanceTo(f);return JSM.IsZero(f)?JSM.CoordLinePosition.CoordOnLine:
  JSM.CoordLinePosition.CoordOutsideOfLine};JSM.Line.prototype.ProjectCoord=function(a){var b=a.x,c=a.y,d=a.z;a=this.start;var e=this.direction,f=a.x,g=a.y,h=a.z,l=a.x+e.x,k=a.y+e.y,m=a.z+e.z,n=(l-f)*(l-f)+(k-g)*(k-g)+(m-h)*(m-h);if(JSM.IsZero(n))return a.Clone();b=e.Clone().MultiplyScalar(((l-f)*(b-f)+(k-g)*(c-g)+(m-h)*(d-h))/n);return JSM.CoordAdd(a,b)};
  JSM.Line.prototype.ClosestPoint=function(a,b,c){function d(a,b,c,d,e){return(a[b].x-a[c].x)*(a[d].x-a[e].x)+(a[b].y-a[c].y)*(a[d].y-a[e].y)+(a[b].z-a[c].z)*(a[d].z-a[e].z)}var e=this.direction.Clone().Normalize(),f=this.start,g=JSM.CoordAdd(f,e),h=a.direction.Clone().Normalize();a=a.start;var l=JSM.CoordAdd(a,h),k=[f,g,a,l],m=d(k,1,0,1,0),n=d(k,0,2,1,0),g=d(k,0,2,3,2),l=d(k,3,2,1,0),k=d(k,3,2,3,2),m=m*k-l*l;if(JSM.IsEqual(m,0))return!1;n=(g*l-n*k)/m;g=(g+n*l)/k;void 0!==b&&(e.MultiplyScalar(n),e=
  JSM.CoordAdd(f,e),b.Set(e.x,e.y,e.z));void 0!==c&&(h.MultiplyScalar(g),b=JSM.CoordAdd(a,h),c.Set(b.x,b.y,b.z));return!0};JSM.Line.prototype.LinePosition=function(a,b){var c=new JSM.Coord(0,0,0),d=new JSM.Coord(0,0,0);return!this.ClosestPoint(a,c,d)?JSM.LineLinePosition.LinesIntersectsCoincident:c.IsEqual(d)?(void 0!==b&&b.Set(c.x,c.y,c.z),JSM.LineLinePosition.LinesIntersectsOnePoint):JSM.LineLinePosition.LinesDontIntersect};
  JSM.Line.prototype.Clone=function(){return new JSM.Line(this.start.Clone(),this.direction.Clone())};JSM.Box2D=function(a,b){this.min=a;this.max=b};JSM.Box2D.prototype.Set=function(a,b){this.min=a;this.max=b};JSM.Box2D.prototype.GetCenter=function(){return JSM.MidCoord2D(this.min,this.max)};JSM.Box2D.prototype.Clone=function(){return new JSM.Box2D(this.min.Clone(),this.max.Clone())};JSM.Box=function(a,b){this.min=a;this.max=b};JSM.Box.prototype.Set=function(a,b){this.min=a;this.max=b};
  JSM.Box.prototype.GetCenter=function(){return JSM.MidCoord(this.min,this.max)};JSM.Box.prototype.GetSize=function(){return JSM.CoordSub(this.max,this.min)};JSM.Box.prototype.IsCoordInside=function(a){return JSM.IsLower(a.x,this.min.x)||JSM.IsLower(a.y,this.min.y)||JSM.IsLower(a.z,this.min.z)||JSM.IsGreater(a.x,this.max.x)||JSM.IsGreater(a.y,this.max.y)||JSM.IsGreater(a.z,this.max.z)?!1:!0};JSM.Box.prototype.Clone=function(){return new JSM.Box(this.min.Clone(),this.max.Clone())};
  JSM.BoxUnion=function(a,b){var c=new JSM.Coord(JSM.Minimum(a.min.x,b.min.x),JSM.Minimum(a.min.y,b.min.y),JSM.Minimum(a.min.z,b.min.z)),d=new JSM.Coord(JSM.Maximum(a.max.x,b.max.x),JSM.Maximum(a.max.y,b.max.y),JSM.Maximum(a.max.z,b.max.z));return new JSM.Box(c,d)};JSM.Sphere=function(a,b){this.center=a;this.radius=b};JSM.Sphere.prototype.Set=function(a,b){this.center=a;this.radius=b};JSM.Sphere.prototype.GetCenter=function(){return this.center};JSM.Sphere.prototype.GetRadius=function(){return this.radius};
  JSM.Sphere.prototype.Clone=function(){return new JSM.Sphere(this.center.Clone(),this.radius)};JSM.Transformation=function(){this.matrix=JSM.MatrixIdentity()};JSM.Transformation.prototype.GetMatrix=function(){return this.matrix};JSM.Transformation.prototype.SetMatrix=function(a){this.matrix=a};JSM.Transformation.prototype.Append=function(a){this.matrix=JSM.MatrixMultiply(this.matrix,a.matrix)};JSM.Transformation.prototype.Apply=function(a){return JSM.ApplyTransformation(this.matrix,a)};
  JSM.Transformation.prototype.Clone=function(){var a=new JSM.Transformation;a.matrix=JSM.MatrixClone(this.matrix);return a};JSM.IdentityTransformation=function(){var a=new JSM.Transformation;a.matrix=JSM.MatrixIdentity();return a};JSM.TranslationTransformation=function(a){var b=new JSM.Transformation;b.matrix=JSM.MatrixTranslation(a.x,a.y,a.z);return b};JSM.OffsetTransformation=function(a,b){var c=a.Clone().Normalize().Clone().MultiplyScalar(b);return JSM.TranslationTransformation(c)};
  JSM.RotationTransformation=function(a,b,c){var d=new JSM.Transformation;d.matrix=JSM.MatrixRotation(a,b,c);return d};JSM.RotationXTransformation=function(a,b){var c=new JSM.Transformation;void 0===b||null===b?c.matrix=JSM.MatrixRotationX(a):(c.Append(JSM.TranslationTransformation(new JSM.Vector(-b.x,-b.y,-b.z))),c.Append(JSM.RotationXTransformation(a)),c.Append(JSM.TranslationTransformation(new JSM.Vector(b.x,b.y,b.z))));return c};
  JSM.RotationYTransformation=function(a,b){var c=new JSM.Transformation;void 0===b||null===b?c.matrix=JSM.MatrixRotationY(a):(c.Append(JSM.TranslationTransformation(new JSM.Vector(-b.x,-b.y,-b.z))),c.Append(JSM.RotationYTransformation(a)),c.Append(JSM.TranslationTransformation(new JSM.Vector(b.x,b.y,b.z))));return c};
  JSM.RotationZTransformation=function(a,b){var c=new JSM.Transformation;void 0===b||null===b?c.matrix=JSM.MatrixRotationZ(a):(c.Append(JSM.TranslationTransformation(new JSM.Vector(-b.x,-b.y,-b.z))),c.Append(JSM.RotationZTransformation(a)),c.Append(JSM.TranslationTransformation(new JSM.Vector(b.x,b.y,b.z))));return c};
  JSM.RotationXYZTransformation=function(a,b,c,d){var e=new JSM.Transformation;e.Append(JSM.RotationXTransformation(a,d));e.Append(JSM.RotationYTransformation(b,d));e.Append(JSM.RotationZTransformation(c,d));return e};JSM.CoordPlanePosition={CoordOnPlane:0,CoordInFrontOfPlane:1,CoordAtBackOfPlane:2};JSM.LinePlanePosition={LineParallelToPlane:0,LineIntersectsPlane:1};JSM.Plane=function(a,b,c,d){this.a=a;this.b=b;this.c=c;this.d=d};
  JSM.Plane.prototype.Set=function(a,b,c,d){this.a=a;this.b=b;this.c=c;this.d=d};JSM.Plane.prototype.GetNormal=function(){return new JSM.Vector(this.a,this.b,this.c)};JSM.Plane.prototype.CoordSignedDistance=function(a){var b=this.a,c=this.b,d=this.c;return(b*a.x+c*a.y+d*a.z+this.d)/Math.sqrt(b*b+c*c+d*d)};JSM.Plane.prototype.CoordDistance=function(a){a=this.CoordSignedDistance(a);return Math.abs(a)};
  JSM.Plane.prototype.ProjectCoord=function(a){var b=a.x,c=a.y,d=a.z,e=this.a,f=this.b,g=this.c,h=this.d,l=this.CoordDistance(a);JSM.IsGreater(e*b+f*c+g*d+h,0)&&(l=-l);b=this.GetNormal().Normalize();return a.Clone().Offset(b,l)};JSM.Plane.prototype.CoordPosition=function(a){a=this.a*a.x+this.b*a.y+this.c*a.z+this.d;return JSM.IsPositive(a)?JSM.CoordPlanePosition.CoordInFrontOfPlane:JSM.IsNegative(a)?JSM.CoordPlanePosition.CoordAtBackOfPlane:JSM.CoordPlanePosition.CoordOnPlane};
  JSM.Plane.prototype.LinePosition=function(a,b){var c=a.direction.Clone().Normalize(),d=a.start.x,e=a.start.y,f=a.start.z,g=this.a,h=this.b,l=this.c,k=this.d,m=g*(d-(a.start.x+c.x))+h*(e-(a.start.y+c.y))+l*(f-(a.start.z+c.z));if(JSM.IsZero(m))return JSM.LinePlanePosition.LineParallelToPlane;void 0!==b&&(c.MultiplyScalar((g*d+h*e+l*f+k)/m),c=JSM.CoordAdd(a.start,c),b.Set(c.x,c.y,c.z));return JSM.LinePlanePosition.LineIntersectsPlane};
  JSM.Plane.prototype.LineIntersection=function(a){var b=a.direction.Clone().Normalize(),c=a.start.x,d=a.start.y,e=a.start.z,f=this.a,g=this.b,h=this.c,l=this.d,k=f*(c-(a.start.x+b.x))+g*(d-(a.start.y+b.y))+h*(e-(a.start.z+b.z));if(JSM.IsZero(k))return null;b.MultiplyScalar((f*c+g*d+h*e+l)/k);return JSM.CoordAdd(a.start,b)};JSM.Plane.prototype.Clone=function(){return new JSM.Plane(this.a,this.b,this.c,this.d)};
  JSM.GetPlaneFromCoordAndDirection=function(a,b){var c=new JSM.Plane,d=b.Clone().Normalize(),e=d.x,f=d.y,d=d.z;c.Set(e,f,d,-(e*a.x+f*a.y+d*a.z));return c};JSM.GetPlaneFromThreeCoords=function(a,b,c){var d=new JSM.Plane,e=(b.y-a.y)*(c.z-a.z)-(c.y-a.y)*(b.z-a.z),f=(b.z-a.z)*(c.x-a.x)-(c.z-a.z)*(b.x-a.x);b=(b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);d.Set(e,f,b,-(e*a.x+f*a.y+b*a.z));return d};
  JSM.CoordPlaneSignedDirectionalDistance=function(a,b,c){var d=b.Clone().Normalize();b=a.x;var e=a.y,f=a.z,g=c.a,h=c.b,l=c.c;c=c.d;var k=g*(b-(a.x+d.x))+h*(e-(a.y+d.y))+l*(f-(a.z+d.z));if(JSM.IsZero(k))return 0;d.MultiplyScalar((g*b+h*e+l*f+c)/k);d=JSM.CoordAdd(a,d);a=a.DistanceTo(d);JSM.IsNegative(g*b+h*e+l*f+c)&&(a=-a);return a};JSM.CoordPlaneDirectionalDistance=function(a,b,c){return Math.abs(JSM.CoordPlaneSignedDirectionalDistance(a,b,c))};
  JSM.MatrixView=function(a,b,c){if(a.IsEqual(b))return JSM.MatrixIdentity();var d=[];b=JSM.CoordSub(a,b).Normalize();c=JSM.VectorCross(c,b).Normalize();var e=JSM.VectorCross(b,c).Normalize();d[0]=c.x;d[1]=e.x;d[2]=b.x;d[3]=0;d[4]=c.y;d[5]=e.y;d[6]=b.y;d[7]=0;d[8]=c.z;d[9]=e.z;d[10]=b.z;d[11]=0;d[12]=-JSM.VectorDot(c,a);d[13]=-JSM.VectorDot(e,a);d[14]=-JSM.VectorDot(b,a);d[15]=1;return d};
  JSM.MatrixPerspective=function(a,b,c,d){var e=[];a=1/Math.tan(a/2);var f=1/(c-d);e[0]=a/b;e[1]=0;e[2]=0;e[3]=0;e[4]=0;e[5]=a;e[6]=0;e[7]=0;e[8]=0;e[9]=0;e[10]=(d+c)*f;e[11]=-1;e[12]=0;e[13]=0;e[14]=2*d*c*f;e[15]=0;return e};
  JSM.Project=function(a,b,c,d,e,f,g,h,l){a=[a.x,a.y,a.z,1];b=JSM.MatrixView(b,c,d);e=JSM.MatrixPerspective(e,f,g,h);e=JSM.MatrixMultiply(b,e);a=JSM.MatrixVectorMultiply(e,a);e=a[3];if(JSM.IsZero(e))return null;f=new JSM.Coord(0,0,0);f.x=(0.5*(a[0]/e)+0.5)*l[2]+l[0];f.y=(0.5*(a[1]/e)+0.5)*l[3]+l[1];f.z=0.5*(a[2]/e)+0.5;return f};
  JSM.Unproject=function(a,b,c,d,e,f,g,h,l){a=[2*((a.x-l[0])/l[2])-1,2*((a.y-l[1])/l[3])-1,2*a.z-1,1];b=JSM.MatrixView(b,c,d);e=JSM.MatrixPerspective(e,f,g,h);e=JSM.MatrixMultiply(b,e);e=JSM.MatrixInvert(e);a=JSM.MatrixVectorMultiply(e,a);if(JSM.IsZero(a[3]))return null;e=new JSM.Coord(0,0,0);e.x=a[0]/a[3];e.y=a[1]/a[3];e.z=a[2]/a[3];return e};
  JSM.ConvexHull2D=function(a){function b(a,b){var c=a.length,d=0,e;for(e=1;e<c;e++)b==d?d=e:JSM.CoordOrientation2D(a[b],a[d],a[e])==JSM.Orientation.Clockwise&&(d=e);return d}var c=[];if(3>a.length)return c;var d=function(a){var b=a.length,c=JSM.Inf,d=-1,e,f;for(e=0;e<b;e++)f=a[e].x,JSM.IsLower(f,c)&&(c=f,d=e);return d}(a),e=d,f;do c.push(e),e=f=b(a,e);while(f!=d);return c};
  JSM.ConvexHull3D=function(a){function b(){this.position=null}function c(){this.tri2=this.tri1=this.vert2=this.vert1=null}function d(){this.valid=this.edges=this.vertices=null}function e(a,c){var d=new b;d.position=c;a.vertices.push(d);return a.vertices.length-1}function f(a,b,d,e){var f=-1,g,h;for(g=0;g<a.edges.length;g++)if(h=a.edges[g],h.vert1==d&&h.vert2==e||h.vert1==e&&h.vert2==d){f=g;break}-1==f&&(f=new c,f.vert1=d,f.vert2=e,f.tri1=-1,f.tri2=-1,a.edges.push(f),f=a.edges.length-1);a=a.edges[f];
  a.tri1!=b&&a.tri2!=b&&(-1==a.tri1?a.tri1=b:-1==a.tri2&&(a.tri2=b));return f}function g(a,b,c,e){var g=a.triangles.length,h=f(a,g,b,c),l=f(a,g,c,e),g=f(a,g,e,b),k=new d;k.vertices=[b,c,e];k.edges=[h,l,g];k.valid=!0;a.triangles.push(k);return a.triangles.length-1}function h(a,b,c){a=a.edges[c];a.tri1==b?a.tri1=-1:a.tri2==b&&(a.tri2=-1)}function l(a,b,c,d,e){c=a.vertices[c].position;d=a.vertices[d].position;e=a.vertices[e].position;a=JSM.CoordSub(a.vertices[b].position,e);b=JSM.CoordSub(c,e);c=JSM.CoordSub(d,
  e);return JSM.VectorDot(a,JSM.VectorCross(b,c))/6}function k(a,b){var c=[],d,e;for(d=0;d<a.triangles.length;d++)e=a.triangles[d],e.valid?!JSM.IsLower(l(a,e.vertices[0],e.vertices[2],e.vertices[1],b),0)?c.push(!0):c.push(!1):c.push(!1);var f,k,m,n=[];for(d=0;d<c.length;d++)c[d]&&(e=a.triangles[d],e.valid&&(f=a.edges[e.edges[0]],k=a.edges[e.edges[1]],m=a.edges[e.edges[2]],f=-1==f.tri1||-1==f.tri2||c[f.tri1]!=c[f.tri2],k=-1==k.tri1||-1==k.tri2||c[k.tri1]!=c[k.tri2],m=-1==m.tri1||-1==m.tri2||c[m.tri1]!=
  c[m.tri2],f&&n.push([e.vertices[0],e.vertices[1],b]),k&&n.push([e.vertices[1],e.vertices[2],b]),m&&n.push([e.vertices[2],e.vertices[0],b])));for(d=0;d<c.length;d++)c[d]&&(e=a.triangles[d],e.valid&&(e=a,m=d,f=e.triangles[m],f.valid&&(h(e,m,f.edges[0]),h(e,m,f.edges[1]),h(e,m,f.edges[2]),f.valid=!1)));for(d=0;d<n.length;d++)c=n[d],g(a,c[0],c[1],c[2])}var m=[],n=a.length;if(4>n)return m;var p=new function(){this.vertices=[];this.edges=[];this.triangles=[]},q;for(q=0;q<n;q++)e(p,a[q]);(function(a){var b=
  -1,b=!JSM.IsLower(l(a,0,1,2,3),0)?g(a,0,1,2):g(a,0,2,1),b=a.triangles[b];g(a,b.vertices[0],b.vertices[2],3);g(a,b.vertices[2],b.vertices[1],3);g(a,b.vertices[1],b.vertices[0],3)})(p);for(q=4;q<n;q++)k(p,q);for(q=0;q<p.triangles.length;q++)a=p.triangles[q],a.valid&&m.push(a.vertices);return m};JSM.Complexity={Invalid:0,Convex:1,Concave:2,Complex:3};JSM.CoordPolygonPosition2D={OnVertex:0,OnEdge:1,Inside:2,Outside:3};
  JSM.SectorPolygonPosition2D={IntersectionOnePoint:0,IntersectionCoincident:1,IntersectionOnVertex:2,NoIntersection:3};JSM.Polygon2D=function(){this.cache=this.vertices=null;this.Clear()};JSM.Polygon2D.prototype.AddVertex=function(a,b){this.AddVertexCoord(new JSM.Coord2D(a,b))};JSM.Polygon2D.prototype.AddVertexCoord=function(a){this.vertices.push(a);this.ClearCache()};JSM.Polygon2D.prototype.GetVertex=function(a){return this.vertices[a]};JSM.Polygon2D.prototype.VertexCount=function(){return this.vertices.length};
  JSM.Polygon2D.prototype.EnumerateVertices=function(a,b,c){var d=this.vertices.length;for(c(a);a!=b;)a=(a+1)%d,c(a)};JSM.Polygon2D.prototype.GetNextVertex=function(a){return JSM.NextIndex(a,this.vertices.length)};JSM.Polygon2D.prototype.GetPrevVertex=function(a){return JSM.PrevIndex(a,this.vertices.length)};JSM.Polygon2D.prototype.ShiftVertices=function(a){JSM.ShiftArray(this.vertices,a);this.ClearCache()};
  JSM.Polygon2D.prototype.GetSignedArea=function(){if(null!==this.cache.signedArea)return this.cache.signedArea;var a=this.vertices.length,b=0;if(3<=a){var c,d,e;for(c=0;c<a;c++)d=this.vertices[c],e=this.vertices[(c+1)%a],b+=d.x*e.y-e.x*d.y;b*=0.5}return this.cache.signedArea=b};JSM.Polygon2D.prototype.GetArea=function(){var a=this.GetSignedArea();return Math.abs(a)};
  JSM.Polygon2D.prototype.GetOrientation=function(){if(null!==this.cache.orientation)return this.cache.orientation;var a=JSM.Orientation.Invalid;if(3<=this.vertices.length){var b=this.GetSignedArea();JSM.IsPositive(b)?a=JSM.Orientation.CounterClockwise:JSM.IsNegative(b)&&(a=JSM.Orientation.Clockwise)}return this.cache.orientation=a};
  JSM.Polygon2D.prototype.GetComplexity=function(){if(null!==this.cache.complexity)return this.cache.complexity;var a=this.vertices.length;if(3>a)return JSM.Complexity.Invalid;var b=JSM.Complexity.Invalid;if(this.GetOrientation()!=JSM.Orientation.Invalid){var b=JSM.Complexity.Convex,c;for(c=0;c<a;c++)if(this.IsConcaveVertex(c)){b=JSM.Complexity.Concave;break}}return this.cache.complexity=b};
  JSM.Polygon2D.prototype.GetVertexOrientation=function(a){if(void 0!==this.cache.vertexOrientations[a])return this.cache.vertexOrientations[a];var b=this.vertices[this.GetPrevVertex(a)],c=this.vertices[a],d=this.vertices[this.GetNextVertex(a)],b=JSM.CoordOrientation2D(b,c,d);return this.cache.vertexOrientations[a]=b};JSM.Polygon2D.prototype.IsConvexVertex=function(a){var b=this.GetOrientation();a=this.GetVertexOrientation(a);return a==JSM.Orientation.Invalid?!1:a==b};
  JSM.Polygon2D.prototype.IsConcaveVertex=function(a){var b=this.GetOrientation();a=this.GetVertexOrientation(a);return a==JSM.Orientation.Invalid?!1:a!=b};
  JSM.Polygon2D.prototype.CoordPosition=function(a){function b(a,b,c){var d=b.y-a.y,e=c.y-a.y,f=JSM.IsNegative(d),g=JSM.IsPositive(d),d=JSM.IsNegative(e),e=JSM.IsPositive(e);if(f&&d||g&&e)return 0;f=!f&&!g;d=!d&&!e;if(f&&d)return 0;e=new JSM.Coord2D(b.x,a.y);JSM.IsEqual(b.y,a.y)||(g=Math.abs((b.y-a.y)/(c.y-b.y)),e.x=b.x+(c.x-b.x)*g);return JSM.IsLower(e.x,a.x)?0:JSM.IsGreater(e.x,a.x)&&(f||d)?(a=JSM.IsGreater(c.y,b.y),f&&a||d&&!a?1:0):1}var c=this.vertices.length,d=0,e,f,g,h;for(e=0;e<c;e++){f=this.vertices[e];
  g=this.vertices[(e+1)%c];h=new JSM.Sector2D(f,g);h=h.CoordPosition(a);if(h==JSM.CoordSectorPosition2D.CoordInsideOfSector)return JSM.CoordPolygonPosition2D.OnEdge;if(h==JSM.CoordSectorPosition2D.CoordOnSectorEndCoord)return JSM.CoordPolygonPosition2D.OnVertex;d+=b(a,f,g)}return 0!==d%2?JSM.CoordPolygonPosition2D.Inside:JSM.CoordPolygonPosition2D.Outside};
  JSM.Polygon2D.prototype.SectorPosition=function(a,b,c){var d=JSM.SectorPolygonPosition2D.NoIntersection,e=this.vertices.length;if(3>e)return d;var f,g,h,l,k;for(f=0;f<e;f++)if(g=f,h=(f+1)%e,l=this.vertices[g],k=this.vertices[h],!(g==b||h==b||g==c||h==c)){g=new JSM.Sector2D(l,k);g=a.SectorPosition(g);if(g==JSM.SectorSectorPosition2D.SectorsIntersectOnePoint)return JSM.SectorPolygonPosition2D.IntersectionOnePoint;if(g==JSM.SectorSectorPosition2D.SectorsIntersectCoincident)return JSM.SectorPolygonPosition2D.IntersectionCoincident;
  g==JSM.SectorSectorPosition2D.SectorsIntersectEndPoint&&(d=JSM.SectorPolygonPosition2D.IntersectionOnVertex)}return d};
  JSM.Polygon2D.prototype.IsDiagonal=function(a,b){return a==b||(this.GetPrevVertex(a)==b||this.GetNextVertex(a)==b)||this.vertices[a].IsEqual(this.vertices[b])||function(a,b,e){var f=a.GetVertex(b),g=a.GetVertex(e),f=new JSM.Sector2D(f,g);return a.SectorPosition(f,b,e)!=JSM.SectorPolygonPosition2D.NoIntersection?!0:!1}(this,a,b)||!function(a,b,e){b=a.GetVertex(b);e=a.GetVertex(e);e=new JSM.Coord2D((b.x+e.x)/2,(b.y+e.y)/2);return a.CoordPosition(e)==JSM.CoordPolygonPosition2D.Inside}(this,a,b)?!1:!0};
  JSM.Polygon2D.prototype.ToArray=function(){var a=[],b,c;for(b=0;b<this.vertices.length;b++)c=this.vertices[b],a.push(c.Clone());return a};JSM.Polygon2D.prototype.FromArray=function(a){this.Clear();var b,c;for(b=0;b<a.length;b++)c=a[b],this.AddVertex(c.x,c.y)};
  JSM.Polygon2D.prototype.GetBoundingBox=function(){if(null!==this.cache.boundingBox)return this.cache.boundingBox;var a=new JSM.Box2D(new JSM.Coord2D(JSM.Inf,JSM.Inf),new JSM.Coord2D(-JSM.Inf,-JSM.Inf)),b,c;for(b=0;b<this.vertices.length;b++)c=this.vertices[b],a.min.x=JSM.Minimum(a.min.x,c.x),a.min.y=JSM.Minimum(a.min.y,c.y),a.max.x=JSM.Maximum(a.max.x,c.x),a.max.y=JSM.Maximum(a.max.y,c.y);return this.cache.boundingBox=a};JSM.Polygon2D.prototype.Clear=function(){this.vertices=[];this.ClearCache()};
  JSM.Polygon2D.prototype.ClearCache=function(){this.cache={signedArea:null,orientation:null,vertexOrientations:{},complexity:null,boundingBox:null}};JSM.Polygon2D.prototype.Clone=function(){var a=new JSM.Polygon2D,b,c;for(b=0;b<this.vertices.length;b++)c=this.vertices[b],a.AddVertexCoord(c.Clone());return a};JSM.ContourPolygon2D=function(){this.contours=null;this.Clear()};JSM.ContourPolygon2D.prototype.AddVertex=function(a,b){this.lastContour.AddVertex(a,b)};
  JSM.ContourPolygon2D.prototype.AddVertexCoord=function(a){this.lastContour.AddVertexCoord(a)};JSM.ContourPolygon2D.prototype.AddContourVertex=function(a,b,c){return this.contours[a].AddVertex(b,c)};JSM.ContourPolygon2D.prototype.AddContourVertexCoord=function(a,b){return this.contours[a].AddVertexCoord(b)};JSM.ContourPolygon2D.prototype.VertexCount=function(){var a=0,b;for(b=0;b<this.contours.length;b++)a+=this.contours[b].VertexCount();return a};
  JSM.ContourPolygon2D.prototype.ContourVertexCount=function(a){return this.contours[a].VertexCount()};JSM.ContourPolygon2D.prototype.AddContour=function(a){this.lastContour=void 0===a||null===a?new JSM.Polygon2D:a;this.contours.push(this.lastContour)};JSM.ContourPolygon2D.prototype.GetLastContour=function(){return this.lastContour};JSM.ContourPolygon2D.prototype.GetContourVertex=function(a,b){return this.contours[a].GetVertex(b)};JSM.ContourPolygon2D.prototype.GetContour=function(a){return this.contours[a]};
  JSM.ContourPolygon2D.prototype.ContourCount=function(){return this.contours.length};JSM.ContourPolygon2D.prototype.GetSignedArea=function(){var a=0,b;for(b=0;b<this.contours.length;b++)a+=this.contours[b].GetSignedArea();return a};JSM.ContourPolygon2D.prototype.GetArea=function(){var a=this.GetSignedArea();return Math.abs(a)};
  JSM.ContourPolygon2D.prototype.GetOrientation=function(){if(null===this.lastContour)return JSM.Orientation.Invalid;var a=this.contours[0].GetOrientation();if(1==this.contours.length)return a;if(a==JSM.Orientation.Invalid)return JSM.Orientation.Invalid;var b,c;for(b=1;b<this.contours.length;b++)if(c=this.contours[b].GetOrientation(),c==JSM.Orientation.Invalid||a==c)return JSM.Orientation.Invalid;return a};
  JSM.ContourPolygon2D.prototype.GetComplexity=function(){if(null===this.lastContour)return JSM.Complexity.Invalid;if(1==this.contours.length)return this.contours[0].GetComplexity();var a,b;for(a=1;a<this.contours.length;a++)if(b=this.contours[a].GetComplexity(),b==JSM.Complexity.Invalid)return JSM.Complexity.Invalid;return JSM.Complexity.Complex};
  JSM.ContourPolygon2D.prototype.ToArray=function(){var a=[],b,c,d,e;for(b=0;b<this.contours.length;b++){d=this.contours[b];for(c=0;c<d.VertexCount();c++)e=d.GetVertex(c),a.push(e.Clone());b<this.contours.length-1&&a.push(null)}return a};JSM.ContourPolygon2D.prototype.FromArray=function(a){this.Clear();this.AddContour();var b,c;for(b=0;b<a.length;b++)c=a[b],null===c?this.AddContour():this.AddVertex(c.x,c.y)};JSM.ContourPolygon2D.prototype.Clear=function(){this.contours=[];this.lastContour=null};
  JSM.ContourPolygon2D.prototype.Clone=function(){var a=new JSM.ContourPolygon2D,b,c;for(b=0;b<this.contours.length;b++)c=this.contours[b],a.AddContour(c.Clone());return a};JSM.Polygon=function(){this.cache=this.vertices=null;this.Clear()};JSM.Polygon.prototype.AddVertex=function(a,b,c){this.AddVertexCoord(new JSM.Coord(a,b,c))};JSM.Polygon.prototype.AddVertexCoord=function(a){this.vertices.push(a);this.ClearCache()};JSM.Polygon.prototype.GetVertex=function(a){return this.vertices[a]};
  JSM.Polygon.prototype.VertexCount=function(){return this.vertices.length};JSM.Polygon.prototype.GetNextVertex=function(a){return JSM.NextIndex(a,this.vertices.length)};JSM.Polygon.prototype.GetPrevVertex=function(a){return JSM.PrevIndex(a,this.vertices.length)};JSM.Polygon.prototype.GetVertexAngle=function(a){var b=this.vertices[this.GetPrevVertex(a)],c=this.vertices[a];a=this.vertices[this.GetNextVertex(a)];b=JSM.CoordSub(b,c);c=JSM.CoordSub(a,c);return b.AngleTo(c)};
  JSM.Polygon.prototype.GetNormal=function(){if(null!==this.cache.normal)return this.cache.normal;var a=JSM.CalculateNormal(this.vertices);return this.cache.normal=a};JSM.Polygon.prototype.ToPolygon2D=function(){var a=this.GetNormal(),b=new JSM.Polygon2D,c,d;for(c=0;c<this.vertices.length;c++)d=this.vertices[c].ToCoord2D(a),b.AddVertex(d.x,d.y);return b};JSM.Polygon.prototype.ToArray=function(){var a=[],b,c;for(b=0;b<this.vertices.length;b++)c=this.vertices[b],a.push(c.Clone());return a};
  JSM.Polygon.prototype.FromArray=function(a){this.Clear();var b,c;for(b=0;b<a.length;b++)c=a[b],this.AddVertex(c.x,c.y,c.z)};JSM.Polygon.prototype.Clear=function(){this.vertices=[];this.ClearCache()};JSM.Polygon.prototype.ClearCache=function(){this.cache={normal:null}};JSM.Polygon.prototype.Clone=function(){var a=new JSM.Polygon,b,c;for(b=0;b<this.vertices.length;b++)c=this.vertices[b],a.AddVertexCoord(c.Clone());return a};JSM.ContourPolygon=function(){this.contours=null;this.Clear()};
  JSM.ContourPolygon.prototype.AddVertex=function(a,b,c){this.lastContour.AddVertex(a,b,c)};JSM.ContourPolygon.prototype.AddVertexCoord=function(a){this.lastContour.AddVertexCoord(a)};JSM.ContourPolygon.prototype.AddContourVertex=function(a,b,c,d){return this.contours[a].AddVertex(b,c,d)};JSM.ContourPolygon.prototype.AddContourVertexCoord=function(a,b){return this.contours[a].AddVertexCoord(b)};
  JSM.ContourPolygon.prototype.VertexCount=function(){var a=0,b;for(b=0;b<this.contours.length;b++)a+=this.contours[b].VertexCount();return a};JSM.ContourPolygon.prototype.ContourVertexCount=function(a){return this.contours[a].VertexCount()};JSM.ContourPolygon.prototype.AddContour=function(a){this.lastContour=void 0===a||null===a?new JSM.Polygon:a;this.contours.push(this.lastContour)};JSM.ContourPolygon.prototype.GetLastContour=function(){return this.lastContour};
  JSM.ContourPolygon.prototype.GetContourVertex=function(a,b){return this.contours[a].GetVertex(b)};JSM.ContourPolygon.prototype.GetContour=function(a){return this.contours[a]};JSM.ContourPolygon.prototype.ContourCount=function(){return this.contours.length};
  JSM.ContourPolygon.prototype.ToContourPolygon2D=function(){var a=this.contours[0].GetNormal(),b=new JSM.ContourPolygon2D,c,d,e,f;for(c=0;c<this.contours.length;c++){b.AddContour();e=this.contours[c];for(d=0;d<e.VertexCount();d++)f=e.GetVertex(d),b.AddVertexCoord(f.ToCoord2D(a))}return b};JSM.ContourPolygon.prototype.ToArray=function(){var a=[],b,c,d,e;for(b=0;b<this.contours.length;b++){d=this.contours[b];for(c=0;c<d.VertexCount();c++)e=d.GetVertex(c),a.push(e.Clone());b<this.contours.length-1&&a.push(null)}return a};
  JSM.ContourPolygon.prototype.FromArray=function(a){this.Clear();this.AddContour();var b,c;for(b=0;b<a.length;b++)c=a[b],null===c?this.AddContour():this.AddVertex(c.x,c.y,c.z)};JSM.ContourPolygon.prototype.Clear=function(){this.contours=[];this.lastContour=null};JSM.ContourPolygon.prototype.Clone=function(){var a=new JSM.ContourPolygon,b,c;for(b=0;b<this.contours.length;b++)c=this.contours[b],a.AddContour(c.Clone());return a};
  JSM.OffsetPolygonContour=function(a,b){var c=a.VertexCount(),d=a.GetNormal(),e,f,g,h,l,k=new JSM.Polygon,m;for(m=0;m<c;m++)e=a.GetPrevVertex(m),f=m,g=a.GetNextVertex(m),e=a.GetVertex(e),f=a.GetVertex(f),h=a.GetVertex(g),l=JSM.CoordSub(e,f),g=JSM.CoordSub(h,f),l=l.AngleTo(g)/2,JSM.CoordOrientation(e,f,h,d)==JSM.Orientation.Clockwise&&(l=Math.PI-l),e=b/Math.sin(l),h=f.Clone(),h.Offset(g,e),h.Rotate(d,l,f),k.AddVertex(h.x,h.y,h.z);return k};JSM.CutVertexType={Left:1,Right:2,Cut:3};
  JSM.PolygonCutter=function(a){this.geometryInterface=a;this.Reset()};JSM.PolygonCutter.prototype.Cut=function(a,b,c,d){this.Reset();var e=this.CalculateOriginalPolygonData(a);return null!==e?(a=a.Clone(),e==JSM.CutVertexType.Left?b.push(a):e==JSM.CutVertexType.Right?c.push(a):d.push(a),!0):!this.CalculateCutPolygonData()||!this.CalculateEntryVertices()||!this.CalculateCuttedPolygons(b,c)?!1:!0};
  JSM.PolygonCutter.prototype.Reset=function(){this.entryVertexTypes=this.entryVertices=this.cutVertexIndices=this.cutPolygonVertexDistances=this.cutPolygonVertexTypes=this.cutPolygon=this.originalPolygonVertexTypes=this.originalPolygon=null};
  JSM.PolygonCutter.prototype.CalculateOriginalPolygonData=function(a){this.originalPolygon=a;this.originalPolygonVertexTypes=[];var b=!1,c=!1,d,e;for(d=0;d<this.originalPolygon.VertexCount();d++)e=a.GetVertex(d),e=this.geometryInterface.getVertexSide(e),e==JSM.CutVertexType.Left?b=!0:e==JSM.CutVertexType.Right&&(c=!0),this.originalPolygonVertexTypes.push(e);return b&&c?null:b?JSM.CutVertexType.Left:c?JSM.CutVertexType.Right:JSM.CutVertexType.Cut};
  JSM.PolygonCutter.prototype.CalculateCutPolygonData=function(){function a(a,b){if(0===a.length)return!1;var c=a[a.length-1];return c==JSM.CutVertexType.Cut||b==JSM.CutVertexType.Cut?!1:c!=b}function b(a,b,c){a.cutPolygon.AddVertexCoord(b);a.cutPolygonVertexTypes.push(c);c==JSM.CutVertexType.Cut&&a.cutVertexIndices.push(a.cutPolygonVertexTypes.length-1)}function c(a,c){var d=a.originalPolygon.GetPrevVertex(c),d=a.originalPolygon.GetVertex(d),e=a.originalPolygon.GetVertex(c),d=a.geometryInterface.getIntersectionVertex(d,
  e);if(null===d)return!1;b(a,d,JSM.CutVertexType.Cut);return!0}function d(a,c,d){var e=a.originalPolygon.GetVertex(c).Clone();b(a,e,d);var f=a.originalPolygon.VertexCount(),g=a.originalPolygonVertexTypes[JSM.PrevIndex(c,f)];c=a.originalPolygonVertexTypes[JSM.NextIndex(c,f)];d==JSM.CutVertexType.Cut&&g==c&&b(a,e,d);return!0}this.cutPolygon=this.geometryInterface.createPolygon();this.cutPolygonVertexTypes=[];this.cutVertexIndices=[];var e=this.originalPolygon.VertexCount(),f,g,h,l;for(f=0;f<=e;f++)g=
  f===e,h=f,g&&(h=0),l=this.originalPolygonVertexTypes[h],a(this.cutPolygonVertexTypes,l)&&c(this,h),g||d(this,h,l);this.cutPolygonVertexDistances=this.geometryInterface.getVertexDistances(this.cutPolygon);return!function(a,b,c){if(2>b.length)return!1;JSM.BubbleSort(b,function(a,b){return JSM.IsLower(c[a],c[b])},function(a,c){JSM.SwapArrayValues(b,a,c)});return!0}(this.cutPolygon,this.cutVertexIndices,this.cutPolygonVertexDistances)?!1:!0};
  JSM.PolygonCutter.prototype.CalculateEntryVertices=function(){function a(a,b,c){if(a[c]!=JSM.CutVertexType.Cut)return 0;var d=JSM.PrevIndex(c,a.length),l=JSM.NextIndex(c,a.length),k=a[d];a=a[l];c=b[c];d=b[d];b=b[l];if(k==JSM.CutVertexType.Right){if(a==JSM.CutVertexType.Left||a==JSM.CutVertexType.Cut&&JSM.IsLowerOrEqual(c,b))return 1}else if(k==JSM.CutVertexType.Left){if(a==JSM.CutVertexType.Right||a==JSM.CutVertexType.Cut&&JSM.IsGreaterOrEqual(c,b))return-1}else if(k==JSM.CutVertexType.Cut)if(a==
  JSM.CutVertexType.Left){if(JSM.IsLowerOrEqual(c,d))return 1}else if(a==JSM.CutVertexType.Right&&JSM.IsGreaterOrEqual(c,d))return-1;return 0}this.entryVertices=[];this.entryVertexTypes=[];var b,c,d;for(b=0;b<this.cutVertexIndices.length;b++)c=this.cutVertexIndices[b],d=a(this.cutPolygonVertexTypes,this.cutPolygonVertexDistances,c),0!==d&&(this.entryVertices.push(c),this.entryVertexTypes.push(d));return 0===this.entryVertices.length||0!==this.entryVertices.length%2?!1:!0};
  JSM.PolygonCutter.prototype.CalculateCuttedPolygons=function(a,b){function c(a,b,c,g){function h(a,b,c){return-1!=c[a]?(b=c[a],c[c[a]]=-1,c[a]=-1,b):JSM.NextIndex(a,b.VertexCount())}function l(a,b,c,d,e){c=a.entryVertices[c];if(-1!==b[c]){var f=a.geometryInterface.createPolygon();f.AddVertexCoord(a.cutPolygon.GetVertex(c).Clone());for(var g=h(c,a.cutPolygon,b),l=null;g!=c;){null===l&&a.cutPolygonVertexTypes[g]!==JSM.CutVertexType.Cut&&(l=a.cutPolygonVertexTypes[g]);var k=f,m=a.cutPolygon.GetVertex(g).Clone(),
  B=k.VertexCount();0<B&&k.GetVertex(B-1).IsEqual(m)||k.AddVertexCoord(m);g=h(g,a.cutPolygon,b)}2<f.VertexCount()&&(l==JSM.CutVertexType.Left?d.push(f):l==JSM.CutVertexType.Right&&e.push(f))}}var k=function(a,b,c){var d=[],e;for(e=0;e<a.VertexCount();e++)d.push(-1);var f;for(e=0;e<b.length;e++)if(-1==d[b[e]]){a:{a=void 0;for(a=e+1;a<b.length;a++)if(-1==d[b[a]]&&c[e]!=c[a]){f=a;break a}f=-1}if(-1==f)return null;a=d;var g=b,h=e;a[g[h]]=g[f];a[g[f]]=g[h]}return d}(a.cutPolygon,a.entryVertices,a.entryVertexTypes);
  if(null===k)return!1;for(var m=g?a.entryVertices.length-1:0;0<=m&&m<a.entryVertices.length;)l(a,k,m,b,c),m=g?m-1:m+1;return!0}return!c(this,a,b,!1)||!c(this,a,b,!0)?!1:!0};
  JSM.CutPolygon2DWithLine=function(a,b,c,d,e){return(new JSM.PolygonCutter({createPolygon:function(){return new JSM.Polygon2D},getVertexSide:function(a){a=b.CoordPosition(a);var c=JSM.CutVertexType.Cut;a==JSM.CoordLinePosition2D.CoordAtLineLeft?c=JSM.CutVertexType.Left:a==JSM.CoordLinePosition2D.CoordAtLineRight&&(c=JSM.CutVertexType.Right);return c},getIntersectionVertex:function(a,c){var d=new JSM.Line2D(c,JSM.CoordSub2D(c,a)),e=new JSM.Coord2D(0,0);return b.LinePosition(d,e)!=JSM.LineLinePosition2D.LinesIntersectsOnePoint?
  null:e},getVertexDistances:function(a){for(var c=new JSM.Coord2D(0,0),d=b.start.Clone(),c=b.direction.Clone().Rotate(-Math.PI/2,c),d=new JSM.Line2D(d,c),e,k=[],c=0;c<a.VertexCount();c++)e=a.GetVertex(c),k.push(d.CoordSignedDistance(e));return k}})).Cut(a,c,d,e)};
  JSM.CutPolygonWithPlane=function(a,b,c,d,e){return(new JSM.PolygonCutter({createPolygon:function(){return new JSM.Polygon},getVertexSide:function(a){a=b.CoordPosition(a);var c=JSM.CutVertexType.Cut;a==JSM.CoordPlanePosition.CoordInFrontOfPlane?c=JSM.CutVertexType.Left:a==JSM.CoordPlanePosition.CoordAtBackOfPlane&&(c=JSM.CutVertexType.Right);return c},getIntersectionVertex:function(a,c){var d=new JSM.Line(c,JSM.CoordSub(c,a)),e=new JSM.Coord(0,0,0);return b.LinePosition(d,e)!=JSM.LinePlanePosition.LineIntersectsPlane?
  null:e},getVertexDistances:function(a){for(var c=a.GetNormal(),d=new JSM.Vector(b.a,b.b,b.c),c=JSM.VectorCross(d,c),d=a.GetVertex(0),c=JSM.GetPlaneFromCoordAndDirection(d,c),e,k=[],d=0;d<a.VertexCount();d++)e=a.GetVertex(d),k.push(c.CoordSignedDistance(e));return k}})).Cut(a,c,d,e)};
  JSM.SegmentPolygon2D=function(a,b,c){function d(a,b,c,d,e,f,g){e=e.Clone();var h,k,l,A;for(h=1;h<c;h++){e.Offset(f,d);l=new JSM.Line2D(e,g);A=[];for(k=0;k<a.length;k++){var B=b,x=A,y=[],z=[];if(JSM.CutPolygon2DWithLine(a[k],l,y,z,[])){for(var E=void 0,E=0;E<y.length;E++)B.push(y[E]);for(E=0;E<z.length;E++)x.push(z[E])}}a=A}for(k=0;k<a.length;k++)b.push(a[k])}var e=a.GetBoundingBox(),f=(e.max.x-e.min.x)/b,g=(e.max.y-e.min.y)/c;a=[a];var h=new JSM.Coord2D(e.min.x,e.min.y),e=new JSM.Coord2D(e.min.x,
  e.max.y),l=[],k=[];d(a,l,b,f,h,new JSM.Vector2D(1,0),new JSM.Vector2D(0,1));d(l,k,c,g,e,new JSM.Vector2D(0,-1),new JSM.Vector2D(1,0));return k};
  JSM.ConvertContourPolygonToPolygon2D=function(a,b){function c(a,b,c,d,e){a.AddVertexCoord(b);void 0!==c&&null!==c&&c.push([d,e])}function d(a,b,d,e,f){f=function(a,b,c,d){function e(a,b,c,d,f){function g(a,b,c){b=new JSM.Sector2D(b,c);a=a.SectorPosition(b,-1,-1);return a==JSM.SectorPolygonPosition2D.IntersectionOnePoint||a==JSM.SectorPolygonPosition2D.IntersectionCoincident?!0:!1}if(g(b,c,d))return!1;var h;for(b=1;b<a.ContourCount();b++)if(void 0===f.addedHoles[b]&&(h=a.GetContour(b),g(h,c,d)))return!1;
  return!0}function f(a,b){var c;for(c=0;c<b.entryPositions.length;c++)if(a.IsEqual(b.entryPositions[c]))return!0;return!1}c=a.GetContour(c);var g,h,k,l;for(g=0;g<b.VertexCount();g++)for(h=0;h<c.VertexCount();h++)if(k=b.GetVertex(g),l=c.GetVertex(h),e(a,b,k,l,d)&&!f(k,d)&&!f(l,d))return d.entryPositions.push(k.Clone()),d.entryPositions.push(l.Clone()),{beg:g,end:h};return null}(a,b,d,f);if(null===f)return!1;(function(a,b,d,e,f){var g=b.GetContour(d),h=e.beg;b=a.GetVertex(h).Clone();a.ShiftVertices(h+
  1);var k=0,l=0;void 0!==f&&null!==f&&(k=f[h][0],l=f[h][1],JSM.ShiftArray(f,h+1));e=e.end;h=g.GetPrevVertex(e);g.EnumerateVertices(e,h,function(b){c(a,g.GetVertex(b).Clone(),f,d,b)});c(a,g.GetVertex(e).Clone(),f,d,e);c(a,b,f,k,l)})(b,a,d,f,e);return!0}var e=a.ContourCount(),f=a.GetContour(0),g=new JSM.Polygon2D,h,l;for(h=0;h<f.VertexCount();h++)l=f.GetVertex(h),c(g,l.Clone(),b,0,h);if(1==e)return g;f=[];for(h=1;h<e;h++)f.push(h);for(e={addedHoles:{},holeTryouts:{},entryPositions:[]};0<f.length;)if(h=
  f.shift(),d(a,g,h,b,e))e.addedHoles[h]=!0;else{void 0===e.holeTryouts[h]&&(e.holeTryouts[h]=0);e.holeTryouts[h]+=1;if(10<e.holeTryouts[h])return null;f.push(h)}return g};JSM.TriangulateConvexPolygon=function(a){var b=[],c;for(c=1;c<a.VertexCount()-1;c++)b.push([0,c,c+1]);return b};
  JSM.TriangulateConcavePolygon2D=function(a){function b(a){var b=a.VertexCount(),c,d;for(c=0;c<b;c++)for(d=0;d<b;d++)if(c!=d&&a.IsDiagonal(c,d))return{beg:c,end:d};return null}function c(a,b){function c(a,b,d){b.polygon.AddVertexCoord(a.polygon.GetVertex(d));b.map.push(a.map[d])}var d={polygon:new JSM.Polygon2D,map:[]},e={polygon:new JSM.Polygon2D,map:[]},f,p;f=b.beg;p=a.polygon.GetPrevVertex(b.end);c(a,d,b.end);a.polygon.EnumerateVertices(f,p,function(b){c(a,d,b)});f=b.end;p=a.polygon.GetPrevVertex(b.beg);
  c(a,e,b.beg);a.polygon.EnumerateVertices(f,p,function(b){c(a,e,b)});return{resultData1:d,resultData2:e}}var d=[],e=function(a){var b=[],c;for(c=0;c<a;c++)b[c]=c;return b}(a.VertexCount());d.push({polygon:a,map:e});a=[];for(var f;0<d.length;)if(e=d.pop(),f=e.polygon.VertexCount(),!(3>f))if(3==f)a.push(e.map);else{f=b(e.polygon);if(null===f)return null;e=c(e,f);d.push(e.resultData1);d.push(e.resultData2)}return a};
  JSM.TriangulatePolygon2D=function(a){if(null===a)return null;var b=a.VertexCount();if(3>b)return null;if(3==b)return[[0,1,2]];b=a.GetComplexity();return b===JSM.Complexity.Invalid?null:b==JSM.Complexity.Convex?JSM.TriangulateConvexPolygon(a):JSM.TriangulateConcavePolygon2D(a)};JSM.TriangulatePolygon=function(a){a=a.ToPolygon2D();return JSM.TriangulatePolygon2D(a)};
  JSM.TraverseOctreeNodes=function(a,b){function c(a,b){if(b(a)&&null!==a.children){var f,g;for(f=0;f<a.children.length;f++)g=a.children[f],c(g,b)}}c(a.root,b)};
  JSM.CreateOctreeChildNodes=function(a,b){function c(a,b,c,d,l){var k=a.GetSize().Clone();k.MultiplyScalar(0.5);a=new JSM.Coord(a.min.x+c*k.x,a.min.y+d*k.y,a.min.z+l*k.z);k=JSM.CoordAdd(a,k);k=new JSM.Box(a,k);return b(k)}var d=a.GetSize();return JSM.IsZero(d.x)&&JSM.IsZero(d.y)&&JSM.IsZero(d.z)?null:[c(a,b,0,0,0),c(a,b,1,0,0),c(a,b,1,1,0),c(a,b,0,1,0),c(a,b,0,0,1),c(a,b,1,0,1),c(a,b,1,1,1),c(a,b,0,1,1)]};
  JSM.Octree=function(a,b){this.coords=[];this.root=this.CreateNewNode(null,a);this.maxCoordNumInNodes=b;if(void 0===this.maxCoordNumInNodes||null===this.maxCoordNumInNodes||0===this.maxCoordNumInNodes)this.maxCoordNumInNodes=50};JSM.Octree.prototype.AddCoord=function(a){return this.AddCoordToNode(a,this.root)};JSM.Octree.prototype.FindCoord=function(a){var b=this.FindNodeForCoord(a,this.root);return null===b?-1:this.FindCoordInNode(a,b)};
  JSM.Octree.prototype.FindCoordInNode=function(a,b){var c,d;for(c=0;c<b.coords.length;c++)if(d=b.coords[c],a.IsEqual(this.coords[d]))return d;return-1};JSM.Octree.prototype.AddCoordToNode=function(a,b){var c=this.FindNodeForCoord(a,b);if(null===c)return-1;var d=this.FindCoordInNode(a,c);if(-1!=d)return d;if(c.coords.length>=this.maxCoordNumInNodes&&this.SplitNode(c))return this.AddCoordToNode(a,c);d=this.coords.length;this.coords.push(a);c.coords.push(d);return d};
  JSM.Octree.prototype.FindNodeForCoord=function(a,b){if(null===b.children)return b;var c=b.box.GetCenter(),d=a.x>c.x,e=a.y>c.y,c=a.z>c.z;return!d&&!e&&!c?this.FindNodeForCoord(a,b.children[0]):d&&!e&&!c?this.FindNodeForCoord(a,b.children[1]):d&&e&&!c?this.FindNodeForCoord(a,b.children[2]):!d&&e&&!c?this.FindNodeForCoord(a,b.children[3]):!d&&!e&&c?this.FindNodeForCoord(a,b.children[4]):d&&!e&&c?this.FindNodeForCoord(a,b.children[5]):d&&e&&c?this.FindNodeForCoord(a,b.children[6]):!d&&e&&c?this.FindNodeForCoord(a,
  b.children[7]):null};JSM.Octree.prototype.SplitNode=function(a){var b=this,c=JSM.CreateOctreeChildNodes(a.box,function(c){return b.CreateNewNode(a,c)});if(null===c)return!1;a.children=c;c=a.coords;a.coords=[];var d,e;for(d=0;d<c.length;d++)e=this.FindNodeForCoord(this.coords[c[d]],a),e.coords.push(c[d]);return!0};JSM.Octree.prototype.CreateNewNode=function(a,b){return{parent:a,box:b,coords:[],children:null}};JSM.TriangleOctree=function(a){this.root=this.CreateNewNode(null,a)};
  JSM.TriangleOctree.prototype.AddTriangle=function(a,b,c,d){return this.AddTriangleToNode(a,b,c,this.root,d)};
  JSM.TriangleOctree.prototype.AddTriangleToNode=function(a,b,c,d,e){if(!d.box.IsCoordInside(a)||!d.box.IsCoordInside(b)||!d.box.IsCoordInside(c))return!1;if(null===d.children){var f=this;d.children=JSM.CreateOctreeChildNodes(d.box,function(a){return f.CreateNewNode(d,a)})}if(null!==d.children){var g,h;for(g=0;g<d.children.length;g++)if(h=d.children[g],this.AddTriangleToNode(a,b,c,h,e))return!0}d.triangles.push({v0:a,v1:b,v2:c,userData:e});return!0};
  JSM.TriangleOctree.prototype.CreateNewNode=function(a,b){return{parent:a,box:b,triangles:[],children:null}};JSM.BSPTree=function(){this.root=null};JSM.BSPTree.prototype.AddPolygon=function(a,b){null===this.root&&(this.root=this.CreateNewNode());return this.AddPolygonToNode(this.root,a,b)};JSM.BSPTree.prototype.Traverse=function(a){this.TraverseNode(this.root,a)};JSM.BSPTree.prototype.TraverseNode=function(a,b){null!==a&&(b(a),this.TraverseNode(a.inside,b),this.TraverseNode(a.outside,b))};
  JSM.BSPTree.prototype.GetNodes=function(){var a=[];this.Traverse(function(b){a.push(b)});return a};JSM.BSPTree.prototype.NodeCount=function(){var a=0;this.Traverse(function(){a+=1});return a};
  JSM.BSPTree.prototype.AddPolygonToNode=function(a,b,c){if(3>b.VertexCount())return!1;var d;if(null===a.polygon){d=b.GetNormal();var e=JSM.GetPlaneFromCoordAndDirection(b.GetVertex(0),d);a.polygon=b;void 0!==c&&(a.userData=c);a.plane=e}else{d=[];var f=[],e=[];JSM.CutPolygonWithPlane(b,a.plane,f,d,e)&&(0<d.length&&this.AddInsidePolygonsToNode(a,d,c),0<f.length&&this.AddOutsidePolygonsToNode(a,f,c),0<e.length&&(d=b.GetNormal(),0<JSM.VectorDot(d,a.plane.GetNormal())?this.AddInsidePolygonsToNode(a,e,c):
  this.AddOutsidePolygonsToNode(a,e,c)))}return!0};JSM.BSPTree.prototype.AddInsidePolygonsToNode=function(a,b,c){null===a.inside&&(a.inside=this.CreateNewNode(),a.inside.parent=a);var d;for(d=0;d<b.length;d++)this.AddPolygonToNode(a.inside,b[d],c)};JSM.BSPTree.prototype.AddOutsidePolygonsToNode=function(a,b,c){null===a.outside&&(a.outside=this.CreateNewNode(),a.outside.parent=a);var d;for(d=0;d<b.length;d++)this.AddPolygonToNode(a.outside,b[d],c)};
  JSM.BSPTree.prototype.CreateNewNode=function(){return{polygon:null,userData:null,plane:null,parent:null,inside:null,outside:null}};
  JSM.ClipPolygonWithBSPTree=function(a,b,c,d,e,f){function g(a,b,c){if(null!==b){var d=[],e=[],f=[];JSM.CutPolygonWithPlane(a,b.plane,e,d,f)&&(0<d.length&&l(b,d,c),0<e.length&&k(b,e,c),0<f.length&&(a=a.GetNormal(),0<JSM.VectorDot(a,b.plane.GetNormal())?l(b,f,!0):k(b,f,!0)))}}function h(a,b){var c;for(c=0;c<a.length;c++)b.push(a[c])}function l(a,b,c){if(null!==a.inside){a=a.inside;var e;for(e=0;e<b.length;e++)g(b[e],a,c)}else h(b,c?f:d)}function k(a,b,d){if(null!==a.outside){a=a.outside;var f;for(f=
  0;f<b.length;f++)g(b[f],a,d)}else h(b,d?e:c)}g(a,b.root,!1);return!0};JSM.TraverseBSPTreeForEyePosition=function(a,b,c){function d(a){if(null!==a){var f=a.plane.CoordPosition(b);f==JSM.CoordPlanePosition.CoordInFrontOfPlane?(d(a.inside),c(a),d(a.outside)):(f==JSM.CoordPlanePosition.CoordAtBackOfPlane?(d(a.outside),c(a)):d(a.outside),d(a.inside))}}d(a.root)};JSM.GetGaussianCParameter=function(a,b,c,d){return Math.sqrt(-(Math.pow(a-c,2)/(2*Math.log(d/Math.abs(b)))))};
  JSM.GetGaussianValue=function(a,b,c,d){return b*Math.exp(-(Math.pow(a-c,2)/(2*Math.pow(d,2))))};JSM.GenerateCirclePoints=function(a,b,c){var d=[],e=2*Math.PI,f=2*Math.PI/b,g,h;for(g=0;g<b;g++)h=JSM.CylindricalToCartesian(a,0,e),void 0!==c&&null!==c&&(h=JSM.CoordAdd(h,c)),d.push(h),e+=f;return d};
  JSM.GetRuledMesh=function(a,b,c,d,e){if(a.length===b.length){var f=a.length-1,g=[],h=[],l;for(l=0;l<=f;l++)g.push(JSM.CoordSub(b[l],a[l])),h.push(a[l].DistanceTo(b[l]));var k,m;for(l=0;l<=f;l++){k=h[l]/c;for(b=0;b<=c;b++)m=a[l].Clone().Offset(g[l],k*b),d.push(m)}for(l=0;l<f;l++)for(b=0;b<c;b++)a=l*(c+1)+b,d=a+1,g=a+c+1,h=g+1,a=[a,g,h,d],e.push(a)}};JSM.Ray=function(a,b,c){this.origin=a;this.direction=b.Normalize();this.length=c};
  JSM.Ray.prototype.Set=function(a,b,c){this.origin=a;this.direction=b.Normalize();this.length=c};JSM.Ray.prototype.GetOrigin=function(){return this.origin};JSM.Ray.prototype.GetDirection=function(){return this.direction};JSM.Ray.prototype.IsLengthReached=function(a){return void 0===this.length||null===this.length?!1:JSM.IsGreater(a,this.length)};JSM.Ray.prototype.Clone=function(){return new JSM.Ray(this.origin.Clone(),this.direction.Clone(),this.length)};JSM.BodyVertex=function(a){this.position=a};
  JSM.BodyVertex.prototype.GetPosition=function(){return this.position};JSM.BodyVertex.prototype.SetPosition=function(a){this.position=a};JSM.BodyVertex.prototype.Clone=function(){return new JSM.BodyVertex(this.position.Clone())};JSM.BodyPoint=function(a){this.vertex=a;this.material=-1};JSM.BodyPoint.prototype.GetVertexIndex=function(){return this.vertex};JSM.BodyPoint.prototype.SetVertexIndex=function(a){this.vertex=a};JSM.BodyPoint.prototype.HasMaterialIndex=function(){return-1!==this.material};
  JSM.BodyPoint.prototype.GetMaterialIndex=function(){return this.material};JSM.BodyPoint.prototype.SetMaterialIndex=function(a){this.material=a};JSM.BodyPoint.prototype.InheritAttributes=function(a){this.material=a.material};JSM.BodyPoint.prototype.Clone=function(){var a=new JSM.BodyPoint(this.vertex);a.material=this.material;return a};JSM.BodyLine=function(a,b){this.beg=a;this.end=b;this.material=-1};JSM.BodyLine.prototype.GetBegVertexIndex=function(){return this.beg};
  JSM.BodyLine.prototype.SetBegVertexIndex=function(a){this.beg=a};JSM.BodyLine.prototype.GetEndVertexIndex=function(){return this.end};JSM.BodyLine.prototype.SetEndVertexIndex=function(a){this.end=a};JSM.BodyLine.prototype.HasMaterialIndex=function(){return-1!==this.material};JSM.BodyLine.prototype.GetMaterialIndex=function(){return this.material};JSM.BodyLine.prototype.SetMaterialIndex=function(a){this.material=a};JSM.BodyLine.prototype.InheritAttributes=function(a){this.material=a.material};
  JSM.BodyLine.prototype.Clone=function(){var a=new JSM.BodyLine(this.beg,this.end);a.material=this.material;return a};JSM.BodyPolygon=function(a){this.vertices=a;this.curved=this.material=-1};JSM.BodyPolygon.prototype.AddVertexIndex=function(a){this.vertices.push(a)};JSM.BodyPolygon.prototype.InsertVertexIndex=function(a,b){this.vertices.splice(b,0,a)};JSM.BodyPolygon.prototype.GetVertexIndex=function(a){return this.vertices[a]};
  JSM.BodyPolygon.prototype.SetVertexIndex=function(a,b){this.vertices[a]=b};JSM.BodyPolygon.prototype.GetVertexIndices=function(){return this.vertices};JSM.BodyPolygon.prototype.SetVertexIndices=function(a){this.vertices=a};JSM.BodyPolygon.prototype.VertexIndexCount=function(){return this.vertices.length};JSM.BodyPolygon.prototype.HasMaterialIndex=function(){return-1!==this.material};JSM.BodyPolygon.prototype.GetMaterialIndex=function(){return this.material};
  JSM.BodyPolygon.prototype.SetMaterialIndex=function(a){this.material=a};JSM.BodyPolygon.prototype.HasCurveGroup=function(){return-1!==this.curved};JSM.BodyPolygon.prototype.GetCurveGroup=function(){return this.curved};JSM.BodyPolygon.prototype.SetCurveGroup=function(a){this.curved=a};JSM.BodyPolygon.prototype.ReverseVertexIndices=function(){this.vertices.reverse()};JSM.BodyPolygon.prototype.InheritAttributes=function(a){this.material=a.material;this.curved=a.curved};
  JSM.BodyPolygon.prototype.Clone=function(){var a=new JSM.BodyPolygon([]),b;for(b=0;b<this.vertices.length;b++)a.vertices.push(this.vertices[b]);a.material=this.material;a.curved=this.curved;return a};JSM.TextureProjectionType={Planar:0,Cubic:1,Cylindrical:2};JSM.BodyTextureProjection=function(){this.coords=this.type=null;this.SetCubic(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1))};JSM.BodyTextureProjection.prototype.GetType=function(){return this.type};
  JSM.BodyTextureProjection.prototype.GetCoords=function(){return this.coords};JSM.BodyTextureProjection.prototype.SetType=function(a){this.type=a};JSM.BodyTextureProjection.prototype.SetCoords=function(a){this.coords=a};JSM.BodyTextureProjection.prototype.SetPlanar=function(a,b,c){this.type=JSM.TextureProjectionType.Planar;this.coords=new JSM.CoordSystem(a,b,c,new JSM.Coord(0,0,0))};
  JSM.BodyTextureProjection.prototype.SetCubic=function(a,b,c,d){this.type=JSM.TextureProjectionType.Cubic;this.coords=new JSM.CoordSystem(a,b,c,d)};JSM.BodyTextureProjection.prototype.SetCylindrical=function(a,b,c,d){this.type=JSM.TextureProjectionType.Cylindrical;this.coords=new JSM.CoordSystem(a,c.Clone().SetLength(b),JSM.VectorCross(d,c).SetLength(b),d)};
  JSM.BodyTextureProjection.prototype.Transform=function(a){this.coords.ToAbsoluteCoords();this.coords.origo=a.Apply(this.coords.origo);this.coords.e1=a.Apply(this.coords.e1);this.coords.e2=a.Apply(this.coords.e2);this.coords.e3=a.Apply(this.coords.e3);this.coords.ToDirectionVectors()};JSM.BodyTextureProjection.prototype.Clone=function(){var a=new JSM.BodyTextureProjection;a.SetType(this.type);a.SetCoords(this.coords.Clone());return a};JSM.Body=function(){this.Clear()};
  JSM.Body.prototype.AddVertex=function(a){this.vertices.push(a);return this.vertices.length-1};JSM.Body.prototype.AddPoint=function(a){this.points.push(a);return this.points.length-1};JSM.Body.prototype.AddLine=function(a){this.lines.push(a);return this.lines.length-1};JSM.Body.prototype.AddPolygon=function(a){this.polygons.push(a);return this.polygons.length-1};JSM.Body.prototype.GetVertex=function(a){return this.vertices[a]};JSM.Body.prototype.GetVertexPosition=function(a){return this.vertices[a].position};
  JSM.Body.prototype.SetVertexPosition=function(a,b){this.vertices[a].position=b};JSM.Body.prototype.GetPoint=function(a){return this.points[a]};JSM.Body.prototype.GetLine=function(a){return this.lines[a]};JSM.Body.prototype.GetPolygon=function(a){return this.polygons[a]};JSM.Body.prototype.SetPointsMaterialIndex=function(a){var b;for(b=0;b<this.points.length;b++)this.points[b].SetMaterialIndex(a)};JSM.Body.prototype.SetLinesMaterialIndex=function(a){var b;for(b=0;b<this.lines.length;b++)this.lines[b].SetMaterialIndex(a)};
  JSM.Body.prototype.SetPolygonsMaterialIndex=function(a){var b;for(b=0;b<this.polygons.length;b++)this.polygons[b].SetMaterialIndex(a)};JSM.Body.prototype.SetPolygonsCurveGroup=function(a){var b;for(b=0;b<this.polygons.length;b++)this.polygons[b].SetCurveGroup(a)};
  JSM.Body.prototype.RemoveVertex=function(a){var b=[],c=[],d=[],e,f,g,h;for(e=0;e<this.points.length;e++)f=this.points[e],f.GetVertexIndex()==a?b.push(e):f.GetVertexIndex()>=a&&f.SetVertexIndex(f.GetVertexIndex()-1);for(e=0;e<this.lines.length;e++)f=this.lines[e],f.GetBegVertexIndex()==a||f.GetEndVertexIndex()==a?c.push(e):(f.GetBegVertexIndex()>=a&&f.SetBegVertexIndex(f.GetBegVertexIndex()-1),f.GetEndVertexIndex()>=a&&f.SetEndVertexIndex(f.GetEndVertexIndex()-1));for(e=0;e<this.polygons.length;e++){g=
  this.polygons[e];for(f=0;f<g.VertexIndexCount();f++)if(h=g.GetVertexIndex(f),g.GetVertexIndex(f)==a){d.push(e);break}else h>=a&&g.SetVertexIndex(f,h-1)}for(e=0;e<b.length;e++)this.RemovePoint(b[e]-e);for(e=0;e<c.length;e++)this.RemoveLine(c[e]-e);for(e=0;e<d.length;e++)this.RemovePolygon(d[e]-e);this.vertices.splice(a,1)};JSM.Body.prototype.RemovePoint=function(a){this.points.splice(a,1)};JSM.Body.prototype.RemoveLine=function(a){this.lines.splice(a,1)};
  JSM.Body.prototype.RemovePolygon=function(a){this.polygons.splice(a,1)};JSM.Body.prototype.VertexCount=function(){return this.vertices.length};JSM.Body.prototype.PointCount=function(){return this.points.length};JSM.Body.prototype.LineCount=function(){return this.lines.length};JSM.Body.prototype.PolygonCount=function(){return this.polygons.length};JSM.Body.prototype.GetTextureProjection=function(){return this.projection};JSM.Body.prototype.SetTextureProjection=function(a){this.projection=a};
  JSM.Body.prototype.SetPlanarTextureProjection=function(a,b,c){this.projection.SetPlanar(a,b,c)};JSM.Body.prototype.SetCubicTextureProjection=function(a,b,c,d){this.projection.SetCubic(a,b,c,d)};JSM.Body.prototype.SetCylindricalTextureProjection=function(a,b,c,d){this.projection.SetCylindrical(a,b,c,d)};JSM.Body.prototype.Transform=function(a){var b;for(b=0;b<this.vertices.length;b++)this.vertices[b].position=a.Apply(this.vertices[b].position);this.projection.Transform(a)};
  JSM.Body.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),c,d;for(c=0;c<this.vertices.length;c++)d=this.vertices[c].position,a.x=JSM.Minimum(a.x,d.x),a.y=JSM.Minimum(a.y,d.y),a.z=JSM.Minimum(a.z,d.z),b.x=JSM.Maximum(b.x,d.x),b.y=JSM.Maximum(b.y,d.y),b.z=JSM.Maximum(b.z,d.z);return new JSM.Box(a,b)};JSM.Body.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
  JSM.Body.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,c,d;for(c=0;c<this.vertices.length;c++)d=a.DistanceTo(this.vertices[c].position),JSM.IsGreater(d,b)&&(b=d);return new JSM.Sphere(a,b)};JSM.Body.prototype.OffsetToOrigo=function(){var a=this.GetCenter().Clone();a.MultiplyScalar(-1);var b;for(b=0;b<this.vertices.length;b++)this.vertices[b].position=JSM.CoordAdd(this.vertices[b].position,a)};
  JSM.Body.prototype.Merge=function(a){var b=this.vertices.length,c,d;for(c=0;c<a.VertexCount();c++)this.vertices.push(a.GetVertex(c).Clone());for(c=0;c<a.PointCount();c++)d=a.GetPoint(c).Clone(),d.SetVertexIndex(d.GetVertexIndex()+b),this.points.push(d);for(c=0;c<a.LineCount();c++)d=a.GetLine(c).Clone(),d.SetBegVertexIndex(d.GetBegVertexIndex()+b),d.SetEndVertexIndex(d.GetEndVertexIndex()+b),this.lines.push(d);var e;for(c=0;c<a.PolygonCount();c++){e=a.GetPolygon(c).Clone();for(d=0;d<e.VertexIndexCount();d++)e.vertices[d]+=
  b;this.polygons.push(e)}};JSM.Body.prototype.Clear=function(){this.vertices=[];this.points=[];this.lines=[];this.polygons=[];this.projection=new JSM.BodyTextureProjection};
  JSM.Body.prototype.Clone=function(){var a=new JSM.Body,b;for(b=0;b<this.vertices.length;b++)a.AddVertex(this.vertices[b].Clone());for(b=0;b<this.points.length;b++)a.AddPoint(this.points[b].Clone());for(b=0;b<this.lines.length;b++)a.AddLine(this.lines[b].Clone());for(b=0;b<this.polygons.length;b++)a.AddPolygon(this.polygons[b].Clone());a.SetTextureProjection(this.projection.Clone());return a};JSM.Model=function(){this.bodies=[]};
  JSM.Model.prototype.AddBody=function(a){this.bodies.push(a);return this.bodies.length-1};JSM.Model.prototype.GetBody=function(a){return this.bodies[a]};JSM.Model.prototype.BodyCount=function(){return this.bodies.length};JSM.Model.prototype.VertexCount=function(){var a=0,b;for(b=0;b<this.bodies.length;b++)a+=this.bodies[b].VertexCount();return a};JSM.Model.prototype.PolygonCount=function(){var a=0,b;for(b=0;b<this.bodies.length;b++)a+=this.bodies[b].PolygonCount();return a};
  JSM.HexColorToRGBComponents=function(a){for(var b=a.toString(16);6>b.length;)b="0"+b;a=parseInt(b.substr(0,2),16);var c=parseInt(b.substr(2,2),16),b=parseInt(b.substr(4,2),16);return[a,c,b]};JSM.HexColorToNormalizedRGBComponents=function(a){a=JSM.HexColorToRGBComponents(a);return[a[0]/255,a[1]/255,a[2]/255]};JSM.HexColorToRGBColor=function(a){return parseInt("0x"+a,16)};
  JSM.RGBComponentsToHexColor=function(a,b,c){function d(a){for(a=parseInt(a,10).toString(16);2>a.length;)a="0"+a;return a}a=d(a);b=d(b);c=d(c);return parseInt("0x"+a+b+c,16)};JSM.Material=function(a){this.diffuse=this.ambient=52224;this.shininess=this.specular=0;this.opacity=1;this.reflection=0;this.singleSided=!1;this.pointSize=0.1;this.texture=null;this.textureHeight=this.textureWidth=1;JSM.CopyObjectProperties(a,this,!0)};JSM.Materials=function(){this.materials=[];this.defaultMaterial=new JSM.Material};
  JSM.Materials.prototype.GetMaterial=function(a){return 0>a||a>=this.materials.length?this.defaultMaterial:this.materials[a]};JSM.Materials.prototype.AddMaterial=function(a){this.materials.push(a);return this.materials.length-1};JSM.Materials.prototype.GetDefaultMaterial=function(){return this.defaultMaterial};JSM.Materials.prototype.Count=function(){return this.materials.length};JSM.VertInfo=function(){this.edges=[];this.pgons=[]};
  JSM.EdgeInfo=function(){this.pgon2=this.pgon1=this.vert2=this.vert1=-1};JSM.PolyEdgeInfo=function(){this.index=-1;this.reverse=!1};JSM.PgonInfo=function(){this.verts=[];this.pedges=[]};JSM.AdjacencyInfo=function(a){this.pgons=this.edges=this.verts=null;this.Calculate(a)};
  JSM.AdjacencyInfo.prototype.Calculate=function(a){function b(a){var b=new JSM.VertInfo;a.verts.push(b)}function c(a,b,c){function d(a,b,c,e,f){var g=new JSM.PolyEdgeInfo,h,k;for(h=0;h<a.edges.length;h++)k=a.edges[h],k.vert1===c&&k.vert2===e?(g.index=h,g.reverse=!1):k.vert1===e&&k.vert2===c&&(g.index=h,g.reverse=!0);-1===g.index?(h=new JSM.EdgeInfo,h.vert1=c,h.vert2=e,h.pgon1=f,h.pgon2=-1,a.edges.push(h),g.index=a.edges.length-1,g.reverse=!1):(h=a.edges[g.index],-1===h.pgon1?h.pgon1=f:h.pgon1!==f&&
  -1===h.pgon2&&(h.pgon2=f));(function(a,b,c,d,e,f){function g(a,b,c){-1==a.edges.indexOf(c)&&a.edges.push(c);-1==a.pgons.indexOf(b)&&a.pgons.push(b)}f.verts.push(c);f.pedges.push(e);g(a.verts[c],b,e.index);g(a.verts[d],b,e.index)})(a,f,c,e,g,b)}b=b.GetPolygon(c);var l=new JSM.PgonInfo,k,m,n,p=b.VertexIndexCount();for(k=0;k<p;k++)m=b.GetVertexIndex(k),n=b.GetVertexIndex(k<p-1?k+1:0),d(a,l,m,n,c);a.pgons.push(l)}this.verts=[];this.edges=[];this.pgons=[];var d;for(d=0;d<a.VertexCount();d++)b(this);for(d=
  0;d<a.PolygonCount();d++)c(this,a,d)};JSM.AdjacencyInfo.prototype.IsContourVertex=function(a){var b,c;for(b=0;b<a.edges.length;b++)if(c=a.edges[b],this.IsContourEdge(this.edges[c]))return!0;return!1};JSM.AdjacencyInfo.prototype.IsContourEdge=function(a){return 1==this.GetEdgePolygonCount(a)};JSM.AdjacencyInfo.prototype.GetEdgePolygonCount=function(a){var b=0;-1!=a.pgon1&&(b+=1);-1!=a.pgon2&&(b+=2);return b};
  JSM.AdjacencyInfo.prototype.GetAnotherPgonOfEdge=function(a,b){return-1!=a.pgon1&&a.pgon1!=b?a.pgon1:-1!=a.pgon2&&a.pgon2!=b?a.pgon2:-1};JSM.AdjacencyInfo.prototype.GetPolyEdgeStartVertex=function(a){return a.reverse?this.edges[a.index].vert2:this.edges[a.index].vert1};JSM.AdjacencyInfo.prototype.GetPolyEdgeEndVertex=function(a){return a.reverse?this.edges[a.index].vert1:this.edges[a.index].vert2};
  JSM.CalculateBodyVertexToPolygon=function(a){var b=[],c,d;for(c=0;c<a.VertexCount();c++)b.push([]);var e;for(c=0;c<a.PolygonCount();c++){e=a.GetPolygon(c);for(d=0;d<e.VertexIndexCount();d++)b[e.GetVertexIndex(d)].push(c)}return b};JSM.IsSolidBody=function(a){a=new JSM.AdjacencyInfo(a);if(0===a.edges.length)return!1;var b,c;for(b=0;b<a.edges.length;b++)if(c=a.edges[b],-1===c.pgon1||-1===c.pgon2)return!1;return!0};
  JSM.CheckSolidBody=function(a){a=new JSM.AdjacencyInfo(a);var b,c,d,e,f,g,h,l;for(b=0;b<a.edges.length;b++){d=a.edges[b];if(-1===d.pgon1||-1===d.pgon2)return!1;g=a.pgons[d.pgon1];f=!1;for(c=0;c<g.pedges.length;c++)if(e=g.pedges[c],e.index==b){h=e.reverse;f=!0;break}if(!f)return!1;d=a.pgons[d.pgon2];f=!1;for(c=0;c<d.pedges.length;c++)if(e=d.pedges[c],e.index==b){l=e.reverse;f=!0;break}if(!f||h==l)return!1}return!0};
  JSM.TraversePgonsAlongEdges=function(a,b,c){var d={};a=[a];for(var e;0<a.length;)if(e=a.pop(),!d[e]&&(d[e]=!0,c(e)))for(var f=b,g=a,h=f.pgons[e],l=void 0,k=void 0,k=void 0,l=0;l<h.pedges.length;l++)k=f.edges[h.pedges[l].index],k=f.GetAnotherPgonOfEdge(k,e),-1!=k&&g.push(k)};JSM.AddVertexToBody=function(a,b,c,d){return a.AddVertex(new JSM.BodyVertex(new JSM.Coord(b,c,d)))};JSM.AddPointToBody=function(a,b){return a.AddPoint(new JSM.BodyPoint(b))};
  JSM.AddLineToBody=function(a,b,c){return a.AddLine(new JSM.BodyLine(b,c))};JSM.AddPolygonToBody=function(a,b){return a.AddPolygon(new JSM.BodyPolygon(b))};
  JSM.CheckBody=function(a){var b=a.VertexCount(),c,d,e;for(c=0;c<a.PointCount();c++)if(d=a.GetPoint(c),0>d.GetVertexIndex()||d.GetVertexIndex()>=b)return!1;for(c=0;c<a.LineCount();c++)if(d=a.GetLine(c),0>d.GetBegVertexIndex()||d.GetBegVertexIndex()>=b||0>d.GetEndVertexIndex()||d.GetEndVertexIndex()>=b)return!1;for(c=0;c<a.PolygonCount();c++){e=a.GetPolygon(c);if(3>e.VertexIndexCount())return!1;for(d=0;d<e.VertexIndexCount();d++)if(0>e.GetVertexIndex(d)||e.GetVertexIndex(d)>=b)return!1}return!0};
  JSM.CalculateBodyPolygonNormal=function(a,b){var c=a.GetPolygon(b),d=c.VertexIndexCount(),e=new JSM.Vector(0,0,0);if(3<=d){var f,g,h;for(f=0;f<d;f++)g=f,h=(f+1)%d,g=a.GetVertexPosition(c.GetVertexIndex(g)),h=a.GetVertexPosition(c.GetVertexIndex(h)),e.x+=(g.y-h.y)*(g.z+h.z),e.y+=(g.z-h.z)*(g.x+h.x),e.z+=(g.x-h.x)*(g.y+h.y)}e.Normalize();return e};JSM.CalculateBodyPolygonNormals=function(a){var b=[],c;for(c=0;c<a.PolygonCount();c++)b.push(JSM.CalculateBodyPolygonNormal(a,c));return b};
  JSM.CalculateBodyVertexNormals=function(a){var b=[],c=JSM.CalculateBodyPolygonNormals(a),d=null,e,f,g,h,l,k,m,n;for(e=0;e<a.PolygonCount();e++)if(h=a.GetPolygon(e),b[e]=[],h.HasCurveGroup()){null===d&&(d=JSM.CalculateBodyVertexToPolygon(a));for(f=0;f<h.VertexIndexCount();f++){l=new JSM.Vector(0,0,0);k=0;m=d[h.GetVertexIndex(f)];for(g=0;g<m.length;g++)n=a.GetPolygon(m[g]),n.GetCurveGroup()===h.GetCurveGroup()&&(l=JSM.CoordAdd(l,c[m[g]]),k++);l.MultiplyScalar(1/k);l.Normalize();b[e].push(l)}}else{g=
  c[e];for(f=0;f<h.VertexIndexCount();f++)b[e].push(new JSM.Vector(g.x,g.y,g.z))}return b};JSM.CalculatePolygonCurveGroups=function(a,b){var c=[],d=a.VertexCount(),e,f;for(e=0;e<d;e++)c.push(0);for(e=0;e<d;e++)f=c[a.GetPrevVertex(e)],a.GetVertexAngle(e)>b?c[e]=f:c[e]=f+1;f=c[0];d=c[d-1];if(0===f&&f!=d)for(e=0;c[e]==f;e++)c[e]=d;return c};
  JSM.CalculatePolygonCentroid=function(a,b){var c=a.GetPolygon(b),d=c.VertexIndexCount(),e=new JSM.Coord(0,0,0),f;for(f=0;f<d;f++)e=JSM.CoordAdd(e,a.GetVertexPosition(c.GetVertexIndex(f)));e.MultiplyScalar(1/d);return e};JSM.MakeBodyInsideOut=function(a){var b,c;for(b=0;b<a.PolygonCount();b++)c=a.GetPolygon(b),c.ReverseVertexIndices()};
  JSM.SoftMoveBodyVertex=function(a,b,c,d,e){b=a.GetVertexPosition(b).Clone();var f=JSM.GetGaussianCParameter(c,e,0,1E-5),g,h;for(g=0;g<a.VertexCount();g++)h=b.DistanceTo(a.GetVertex(g).position),JSM.IsGreater(h,c)||(h=JSM.GetGaussianValue(h,e,0,f),a.GetVertexPosition(g).Offset(d,h))};
  JSM.TriangulateWithCentroids=function(a){var b=a.PolygonCount(),c,d,e,f,g,h,l;for(c=0;c<b;c++){d=JSM.CalculatePolygonCentroid(a,c);e=a.AddVertex(new JSM.BodyVertex(d));f=a.GetPolygon(c);g=f.VertexIndexCount();for(d=0;d<g;d++)h=f.GetVertexIndex(d),l=f.GetVertexIndex(d<g-1?d+1:0),h=new JSM.BodyPolygon([h,l,e]),h.InheritAttributes(f),a.AddPolygon(h)}for(c=0;c<b;c++)a.RemovePolygon(0)};
  JSM.TriangulatePolygons=function(a){var b=a.PolygonCount(),c,d,e,f,g;for(c=0;c<b;c++){e=a.GetPolygon(c);f=new JSM.Polygon;for(d=0;d<e.VertexIndexCount();d++)g=a.GetVertexPosition(e.GetVertexIndex(d)),f.AddVertex(g.x,g.y,g.z);f=JSM.TriangulatePolygon(f);if(null!==f)for(d=0;d<f.length;d++)g=f[d],g=new JSM.BodyPolygon([e.GetVertexIndex(g[0]),e.GetVertexIndex(g[1]),e.GetVertexIndex(g[2])]),g.InheritAttributes(e),a.AddPolygon(g)}for(c=0;c<b;c++)a.RemovePolygon(0)};
  JSM.GenerateRandomMaterials=function(a,b,c){function d(a,b){var c=0;return c=void 0!==a&&a?JSM.SeededRandomInt(0,16777215,b+1):JSM.RandomInt(0,16777215)}var e,f,g=0;for(e=0;e<a.LineCount();e++)f=d(c,g++),f=b.AddMaterial(new JSM.Material({ambient:f,diffuse:f})),a.GetLine(e).SetMaterialIndex(f);for(e=0;e<a.PointCount();e++)f=d(c,g++),f=b.AddMaterial(new JSM.Material({ambient:f,diffuse:f})),a.GetPoint(e).SetMaterialIndex(f);for(e=0;e<a.PolygonCount();e++)f=d(c,g++),f=b.AddMaterial(new JSM.Material({ambient:f,
  diffuse:f})),a.GetPolygon(e).SetMaterialIndex(f)};JSM.AddBodyToBSPTree=function(a,b,c){var d,e,f;for(d=0;d<a.PolygonCount();d++){f={id:c,originalPolygon:d,material:-1};e=a;var g=f,h=e.GetPolygon(d);g.material=h.GetMaterialIndex();for(var g=new JSM.Polygon,l=void 0,k=void 0,l=0;l<h.VertexIndexCount();l++)k=e.GetVertexPosition(h.GetVertexIndex(l)),g.AddVertex(k.x,k.y,k.z);e=g;b.AddPolygon(e,f)}};
  JSM.CalculatePlanarTextureCoord=function(a,b){var c=new JSM.Coord2D(0,0),d=b.e1.Clone().Normalize(),e=b.e2.Clone().Normalize(),f=JSM.VectorCross(b.e1,b.e2),f=JSM.GetPlaneFromCoordAndDirection(b.origo,f),e=JSM.GetPlaneFromCoordAndDirection(b.origo,e),d=JSM.GetPlaneFromCoordAndDirection(b.origo,d),f=f.ProjectCoord(a);c.x=d.CoordSignedDistance(f);c.y=e.CoordSignedDistance(f);return c};
  JSM.CalculateCubicTextureCoord=function(a,b,c){var d=new JSM.Coord2D(0,0),e=c.e1.Clone().Normalize(),f=c.e2.Clone().Normalize(),g=c.e3.Clone().Normalize(),h=-1,l=0,k,m,n;for(k=0;3>k;k++)0===k?m=e:1===k?m=f:2===k&&(m=g),n=Math.abs(JSM.VectorDot(b,m)),JSM.IsGreater(n,l)&&(h=k,l=n);if(-1===h)return d;b=null;0===h?b=new JSM.CoordSystem(c.origo,f,g,new JSM.Coord(0,0,0)):1===h?b=new JSM.CoordSystem(c.origo,e,g,new JSM.Coord(0,0,0)):2===h&&(b=new JSM.CoordSystem(c.origo,e,f,new JSM.Coord(0,0,0)));return null===
  b?d:JSM.CalculatePlanarTextureCoord(a,b)};JSM.CalculateCylindricalTextureCoord=function(a,b,c){var d=new JSM.Coord2D(0,0),e=c.e3.Clone().Normalize();if(e.IsCollinearWith(b))return d=JSM.CalculateCubicTextureCoord(a,b,c),[d,0];var f=(new JSM.Line(c.origo,e)).ProjectCoord(a);b=JSM.CoordSignedDistance(c.origo,f,e);var g=c.e1.Clone().Normalize();a=JSM.CoordSub(a,f);e=JSM.GetVectorsFullAngle(a,g,e);c=c.e1.Length();d.x=e*c;d.y=b;return[d,e]};
  JSM.CalculatePolygonPlanarTextureCoords=function(a,b){var c=[],d=a.GetPolygon(b),e=a.GetTextureProjection().GetCoords(),f,g;for(f=0;f<d.VertexIndexCount();f++)g=a.GetVertexPosition(d.GetVertexIndex(f)),c.push(JSM.CalculatePlanarTextureCoord(g,e));return c};
  JSM.CalculatePolygonCubicTextureCoords=function(a,b,c){var d=[];b=a.GetPolygon(b);var e=a.GetTextureProjection().GetCoords(),f,g;for(f=0;f<b.VertexIndexCount();f++)g=a.GetVertexPosition(b.GetVertexIndex(f)),d.push(JSM.CalculateCubicTextureCoord(g,c,e));return d};
  JSM.CalculatePolygonCylindricalTextureCoords=function(a,b,c){var d=[],e=[],f=a.GetPolygon(b),g=a.GetTextureProjection().GetCoords(),h;for(b=0;b<f.VertexIndexCount();b++)h=a.GetVertexPosition(f.GetVertexIndex(b)),h=JSM.CalculateCylindricalTextureCoord(h,c,g),d.push(h[0]),e.push(h[1]);if(g.e3.Clone().Normalize().IsCollinearWith(c))return d;c=!1;for(b=0;b<e.length;b++){for(a=b+1;a<e.length;a++)if(JSM.IsGreater(Math.abs(e[b]-e[a]),Math.PI)){c=!0;break}if(c)break}if(c){g=g.e1.Length();for(b=0;b<e.length;b++)JSM.IsLower(e[b],
  Math.PI)&&(d[b].x=g*(e[b]+2*Math.PI))}return d};JSM.CalculateBodyPlanarTextureCoords=function(a){var b=[],c;for(c=0;c<a.PolygonCount();c++)b.push(JSM.CalculatePolygonPlanarTextureCoords(a,c));return b};JSM.CalculateBodyCubicTextureCoords=function(a){var b=[],c=JSM.CalculateBodyPolygonNormals(a),d,e;for(d=0;d<a.PolygonCount();d++)e=c[d],b.push(JSM.CalculatePolygonCubicTextureCoords(a,d,e));return b};
  JSM.CalculateBodyCylindricalTextureCoords=function(a){var b=[],c=JSM.CalculateBodyPolygonNormals(a),d,e;for(d=0;d<a.PolygonCount();d++)e=c[d],b.push(JSM.CalculatePolygonCylindricalTextureCoords(a,d,e));return b};
  JSM.CalculateBodyTextureCoords=function(a){var b=[],c=a.GetTextureProjection().GetType();c===JSM.TextureProjectionType.Planar?b=JSM.CalculateBodyPlanarTextureCoords(a):c===JSM.TextureProjectionType.Cubic?b=JSM.CalculateBodyCubicTextureCoords(a):c===JSM.TextureProjectionType.Cylindrical&&(b=JSM.CalculateBodyCylindricalTextureCoords(a));return b};
  JSM.CutBodyByPlane=function(a,b){function c(a,b,c){function d(a,b,c,e){c.push(new JSM.Coord(b.x,b.y,b.z));e.push(a)}function e(c,d,f,g){d=JSM.CoordSub(a[d],a[c]).Normalize();c=new JSM.Line(a[c],d);c=b.LineIntersection(c);f.push(new JSM.Coord(c.x,c.y,c.z));g.push(-1)}var f=void 0!==c&&null!==c,g=a.length,h=[],k=[],l=!1,m,n,q;for(m=0;m<g;m++)q=a[m],n=b.CoordPosition(q),k.push(n!==JSM.CoordPlanePosition.CoordAtBackOfPlane),0<m&&k[m-1]!==k[m]&&(l=!0);if(!l){if(!1===k[0])return h;for(m=0;m<g;m++)q=a[m],
  h.push(new JSM.Coord(q.x,q.y,q.z)),f&&c.push(m);return h}l=[];n=[];var p,r;for(m=0;m<g;m++)p=m-1,r=m,0===m&&(p=g-1),q=a[r],k[r]?(k[p]||e(p,r,l,n),d(r,q,l,n)):k[p]&&e(p,r,l,n);for(m=0;m<l.length;m++)g=l[m],k=h[h.length-1],0===m||!k.IsEqual(g)?(h.push(new JSM.Coord(g.x,g.y,g.z)),f&&(g=n[m],c.push(g))):f&&(g=n[m],k=n[m-1],-1!==g?c[c.length-1]=g:-1!==k&&(c[c.length-1]=k));return h}function d(a,b,c){for(var d=-1;c<a.VertexCount();c++)if(b.IsEqual(a.GetVertexPosition(c))){d=c;break}-1===d&&(d=a.AddVertex(new JSM.BodyVertex(new JSM.Coord(b.x,
  b.y,b.z))));return d}var e=new JSM.Body,f=[],g=[],h=[],l=[],k,m,n,p,q;for(k=0;k<a.PolygonCount();k++){n=a.GetPolygon(k);p=[];for(m=0;m<n.VertexIndexCount();m++)q=a.GetVertexPosition(n.GetVertexIndex(m)),p.push(new JSM.Coord(q.x,q.y,q.z));q=[];p=c(p,b,q);for(m=0;m<q.length;m++)-1!==q[m]&&(h[n.GetVertexIndex(q[m])]=!0);f.push(p);g.push(q)}var r;for(k=0;k<a.VertexCount();k++)h[k]&&(r=a.GetVertexPosition(k),l[k]=e.AddVertex(new JSM.BodyVertex(new JSM.Coord(r.x,r.y,r.z))));var h=e.VertexCount(),s;for(k=
  0;k<a.PolygonCount();k++)if(n=a.GetPolygon(k),p=f[k],q=g[k],0!==q.length){s=[];for(m=0;m<q.length;m++)-1!==q[m]?s.push(l[n.GetVertexIndex(q[m])]):(r=p[m],s.push(d(e,r,h)));m=new JSM.BodyPolygon(s);m.InheritAttributes(n);e.AddPolygon(m)}return e};
  JSM.GenerateRectangle=function(a,b){var c=new JSM.Body,d=a/2,e=b/2;c.AddVertex(new JSM.BodyVertex(new JSM.Coord(-d,-e,0)));c.AddVertex(new JSM.BodyVertex(new JSM.Coord(d,-e,0)));c.AddVertex(new JSM.BodyVertex(new JSM.Coord(d,e,0)));c.AddVertex(new JSM.BodyVertex(new JSM.Coord(-d,e,0)));c.AddPolygon(new JSM.BodyPolygon([0,1,2,3]));c.SetCubicTextureProjection(new JSM.Coord(-d,-e,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return c};
  JSM.GenerateCuboid=function(a,b,c){var d=new JSM.Body;a/=2;b/=2;c/=2;d.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,-b,-c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,-b,-c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,-b,c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,-b,c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,b,-c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,b,-c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,b,c)));d.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,
  b,c)));d.AddPolygon(new JSM.BodyPolygon([0,1,2,3]));d.AddPolygon(new JSM.BodyPolygon([1,5,6,2]));d.AddPolygon(new JSM.BodyPolygon([5,4,7,6]));d.AddPolygon(new JSM.BodyPolygon([4,0,3,7]));d.AddPolygon(new JSM.BodyPolygon([0,4,5,1]));d.AddPolygon(new JSM.BodyPolygon([3,2,6,7]));d.SetCubicTextureProjection(new JSM.Coord(-a,-b,-c),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return d};
  JSM.GenerateCuboidSides=function(a,b,c,d){var e=new JSM.Body;a/=2;b/=2;c/=2;e.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,-b,-c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,-b,-c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,-b,c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,-b,c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,b,-c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,b,-c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(a,b,c)));e.AddVertex(new JSM.BodyVertex(new JSM.Coord(-a,
  b,c)));d[0]&&e.AddPolygon(new JSM.BodyPolygon([0,1,2,3]));d[1]&&e.AddPolygon(new JSM.BodyPolygon([1,5,6,2]));d[2]&&e.AddPolygon(new JSM.BodyPolygon([5,4,7,6]));d[3]&&e.AddPolygon(new JSM.BodyPolygon([4,0,3,7]));d[4]&&e.AddPolygon(new JSM.BodyPolygon([0,4,5,1]));d[5]&&e.AddPolygon(new JSM.BodyPolygon([3,2,6,7]));e.SetCubicTextureProjection(new JSM.Coord(-a,-b,-c),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return e};
  JSM.GenerateSegmentedRectangle=function(a,b,c,d){var e=new JSM.Body,f=a/2,g=b/2,h=a/c,l=b/d;(function(){var a,b,n;for(a=0;a<=d;a++)for(b=0;b<=c;b++)n=new JSM.Coord(b*h-f,a*l-g,0),e.AddVertex(new JSM.BodyVertex(n))})();(function(){var a,b,f,g,h,l;for(b=0;b<d;b++)for(a=0;a<c;a++)f=b*(c+1)+a,g=f+1,h=f+c+1,l=h+1,e.AddPolygon(new JSM.BodyPolygon([f,g,l,h]))})();return e};
  JSM.GenerateSegmentedCuboid=function(a,b,c,d){function e(a){var b=0;0<a&&a<=d&&(b=(d+1)*(d+1)+(a-1)*4*d);return b}function f(a){var b=[],c=e(a);if(0===a||a===d){for(a=0;a<=d;a++)b.push(c+a);for(a=1;a<=d;a++)b.push(c+(a+1)*d+a);for(a=d-1;0<=a;a--)b.push(c+(d+1)*d+a);for(a=d-1;0<a;a--)b.push(c+a*(d+1))}else if(0<a&&a<d){for(a=0;a<=d;a++)b.push(c+a);for(a=1;a<d;a++)b.push(c+d+2*a);for(a=d;0<=a;a--)b.push(c+3*d+a-1);for(a=d-1;0<a;a--)b.push(c+d+2*a-1)}return b}function g(a){var b,c,e=a*r;if(0===a||a===
  d)for(a=0;a<=d;a++)for(b=0;b<=d;b++)c=new JSM.Coord(b*p-k,a*q-m,e-n),l.AddVertex(new JSM.BodyVertex(c));else if(0<a&&a<d)for(a=0;a<=d;a++)for(b=0;b<=d;b++)if(0===a||a===d||0===b||b===d)c=new JSM.Coord(b*p-k,a*q-m,e-n),l.AddVertex(new JSM.BodyVertex(c))}function h(a){var b,c,f,g,h,k;if(0===a||a===d){var m=e(a);for(b=0;b<d;b++)for(c=0;c<d;c++)f=m+b*(d+1)+c,g=f+1,h=f+d+1,k=h+1,0===a?l.AddPolygon(new JSM.BodyPolygon([f,h,k,g])):l.AddPolygon(new JSM.BodyPolygon([f,g,k,h]))}if(0<a&&a<=d){c=s[a-1];a=s[a];
  for(b=0;b<4*d;b++)f=c[b],h=a[b],b<4*d-1?(g=c[b+1],k=a[b+1]):(g=c[0],k=a[0]),l.AddPolygon(new JSM.BodyPolygon([f,g,k,h]))}}var l=new JSM.Body,k=a/2,m=b/2,n=c/2,p=a/d,q=b/d,r=c/d;for(a=0;a<=d;a++)g(a);var s=[];for(a=0;a<=d;a++)s.push(f(a));for(a=0;a<=d;a++)h(a);return l};
  JSM.GenerateCircle=function(a,b){var c=new JSM.Body,d=JSM.GenerateCirclePoints(a,b),e;for(e=0;e<d.length;e++)c.AddVertex(new JSM.BodyVertex(d[e]));d=new JSM.BodyPolygon([]);for(e=0;e<b;e++)d.AddVertexIndex(e);c.AddPolygon(d);c.SetCylindricalTextureProjection(new JSM.Coord(0,0,0),a,new JSM.Coord(1,0,0),new JSM.Coord(0,0,1));return c};
  JSM.GenerateSphere=function(a,b,c){var d=new JSM.Body,e=2*b,f=d.AddVertex(new JSM.BodyVertex(JSM.SphericalToCartesian(a,0,0))),g=Math.PI/b,h=g,l,k,m;for(l=1;l<b;l++){for(k=m=0;k<e;k++)d.AddVertex(new JSM.BodyVertex(JSM.SphericalToCartesian(a,h,m))),m+=g;h+=g}a=d.AddVertex(new JSM.BodyVertex(JSM.SphericalToCartesian(-a,0,0)));var n,p;for(l=1;l<=b;l++)if(1===l){g=1;for(k=0;k<e;k++)h=g+k,m=h+1,k===e-1&&(m=g),h=new JSM.BodyPolygon([h,m,f]),c&&h.SetCurveGroup(0),d.AddPolygon(h)}else if(l<b){g=(l-1)*e+
  1;for(k=0;k<e;k++)h=g+k,m=h+1,n=h-e,p=n+1,k===e-1&&(m=g,p=g-e),h=new JSM.BodyPolygon([h,m,p,n]),c&&h.SetCurveGroup(0),d.AddPolygon(h)}else if(l===b){g=(l-2)*e+1;for(k=0;k<e;k++)h=g+k,m=h+1,k===e-1&&(m=g),h=new JSM.BodyPolygon([h,a,m]),c&&h.SetCurveGroup(0),d.AddPolygon(h)}d.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return d};
  JSM.GenerateTriangulatedSphere=function(a,b,c){var d;d=new JSM.Body;var e=(1+Math.sqrt(5))/2;JSM.AddVertexToBody(d,0,1,+e);JSM.AddVertexToBody(d,0,1,-e);JSM.AddVertexToBody(d,0,-1,+e);JSM.AddVertexToBody(d,0,-1,-e);JSM.AddVertexToBody(d,1,+e,0);JSM.AddVertexToBody(d,1,-e,0);JSM.AddVertexToBody(d,-1,+e,0);JSM.AddVertexToBody(d,-1,-e,0);JSM.AddVertexToBody(d,+e,0,1);JSM.AddVertexToBody(d,-e,0,1);JSM.AddVertexToBody(d,+e,0,-1);JSM.AddVertexToBody(d,-e,0,-1);JSM.AddPolygonToBody(d,[0,2,8]);JSM.AddPolygonToBody(d,
  [0,4,6]);JSM.AddPolygonToBody(d,[0,6,9]);JSM.AddPolygonToBody(d,[0,8,4]);JSM.AddPolygonToBody(d,[0,9,2]);JSM.AddPolygonToBody(d,[1,3,11]);JSM.AddPolygonToBody(d,[1,4,10]);JSM.AddPolygonToBody(d,[1,6,4]);JSM.AddPolygonToBody(d,[1,10,3]);JSM.AddPolygonToBody(d,[1,11,6]);JSM.AddPolygonToBody(d,[2,5,8]);JSM.AddPolygonToBody(d,[2,7,5]);JSM.AddPolygonToBody(d,[2,9,7]);JSM.AddPolygonToBody(d,[3,5,7]);JSM.AddPolygonToBody(d,[3,7,11]);JSM.AddPolygonToBody(d,[3,10,5]);JSM.AddPolygonToBody(d,[4,8,10]);JSM.AddPolygonToBody(d,
  [6,11,9]);JSM.AddPolygonToBody(d,[5,10,8]);JSM.AddPolygonToBody(d,[7,9,11]);for(var e=d.GetVertexPosition(0).Length(),f=a/e,g,h,e=0;e<d.VertexCount();e++)h=d.GetVertex(e),h.position.MultiplyScalar(f);for(var l,k,m,n,f=0;f<b;f++){g=d;d=new JSM.Body;h=new JSM.AdjacencyInfo(g);for(e=0;e<h.verts.length;e++)l=g.GetVertexPosition(e),JSM.AddVertexToBody(d,l.x,l.y,l.z);l=[];for(e=0;e<h.edges.length;e++)k=h.edges[e],k=JSM.MidCoord(g.GetVertexPosition(k.vert1),g.GetVertexPosition(k.vert2)),l.push(d.AddVertex(new JSM.BodyVertex(k.SetLength(a))));
  for(e=0;e<h.pgons.length;e++){k=h.pgons[e];m=[];for(g=0;g<k.pedges.length;g++)n=k.pedges[g],m.push(h.GetPolyEdgeStartVertex(n)),m.push(l[n.index]);JSM.AddPolygonToBody(d,[m[0],m[1],m[5]]);JSM.AddPolygonToBody(d,[m[1],m[2],m[3]]);JSM.AddPolygonToBody(d,[m[3],m[4],m[5]]);JSM.AddPolygonToBody(d,[m[1],m[3],m[5]])}}if(c)for(e=0;e<d.PolygonCount();e++)d.GetPolygon(e).SetCurveGroup(0);d.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return d};
  JSM.GenerateCylinder=function(a,b,c,d,e){var f=new JSM.Body,g=2*Math.PI,h=2*Math.PI/c,l;for(l=0;l<c;l++)f.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(a,b/2,g))),f.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(a,-b/2,g))),g-=h;for(l=0;l<c;l++)g=2*l,h=g+2,l===c-1&&(h=0),g=new JSM.BodyPolygon([g,h,h+1,g+1]),e&&g.SetCurveGroup(0),f.AddPolygon(g);if(d){d=new JSM.BodyPolygon([]);e=new JSM.BodyPolygon([]);for(l=0;l<c;l++)d.AddVertexIndex(2*(c-l-1)),e.AddVertexIndex(2*l+1);f.AddPolygon(d);
  f.AddPolygon(e)}f.SetCylindricalTextureProjection(new JSM.Coord(0,0,-(b/2)),a,new JSM.Coord(1,0,0),new JSM.Coord(0,0,1));return f};
  JSM.GeneratePie=function(a,b,c,d,e,f){var g=new JSM.Body,h=c,l=c/(d-1);g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(0,b/2,0)));g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(0,-b/2,0)));for(c=0;c<d;c++)g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(a,b/2,h))),g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(a,-b/2,h))),h-=l;for(c=0;c<=d;c++)h=2*c,l=h+2,c===d&&(l=0),h=new JSM.BodyPolygon([h,l,l+1,h+1]),f&&(0<c&&c<d)&&h.SetCurveGroup(0),g.AddPolygon(h);if(e){e=
  new JSM.BodyPolygon([]);f=new JSM.BodyPolygon([]);for(c=0;c<=d;c++)e.AddVertexIndex(2*(d-c)),f.AddVertexIndex(2*c+1);g.AddPolygon(e);g.AddPolygon(f)}g.SetCylindricalTextureProjection(new JSM.Coord(0,0,-(b/2)),a,new JSM.Coord(1,0,0),new JSM.Coord(0,0,1));return g};
  JSM.GenerateCone=function(a,b,c,d,e,f){var g=new JSM.Body,h=JSM.IsZero(a),l=JSM.IsZero(b),k=2*Math.PI,m=2*Math.PI/d;h&&g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(0,c/2,0)));var n;for(n=0;n<d;n++)h||g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(a,c/2,k))),l||g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(b,-c/2,k))),k-=m;l&&g.AddVertex(new JSM.BodyVertex(JSM.CylindricalToCartesian(0,-c/2,0)));for(n=0;n<d;n++)h?(k=n+1,m=k+1,n===d-1&&(m=1),k=new JSM.BodyPolygon([0,
  m,k])):l?(k=n,m=k+1,n===d-1&&(m=0),k=new JSM.BodyPolygon([k,m,d])):(k=2*n,m=k+2,n===d-1&&(m=0),k=new JSM.BodyPolygon([k,m,m+1,k+1])),f&&k.SetCurveGroup(0),g.AddPolygon(k);if(e)if(h){f=new JSM.BodyPolygon([]);for(n=0;n<d;n++)f.AddVertexIndex(n+1);g.AddPolygon(f)}else if(l){e=new JSM.BodyPolygon([]);for(n=0;n<d;n++)e.AddVertexIndex(d-n-1);g.AddPolygon(e)}else{e=new JSM.BodyPolygon([]);f=new JSM.BodyPolygon([]);for(n=0;n<d;n++)e.AddVertexIndex(2*(d-n-1)),f.AddVertexIndex(2*n+1);g.AddPolygon(e);g.AddPolygon(f)}g.SetCylindricalTextureProjection(new JSM.Coord(0,
  0,-(c/2)),(a+b)/2,new JSM.Coord(1,0,0),new JSM.Coord(0,0,1));return g};
  JSM.GeneratePrism=function(a,b,c,d,e){var f=new JSM.Body,g=new JSM.Polygon;g.FromArray(a);a=g.VertexCount();var h=null;void 0!==e&&null!==e&&(h=JSM.CalculatePolygonCurveGroups(g,e));for(e=0;e<a;e++)f.AddVertex(new JSM.BodyVertex(g.GetVertex(e).Clone())),f.AddVertex(new JSM.BodyVertex(g.GetVertex(e).Clone().Clone().Offset(b,c)));var l;for(e=0;e<a;e++)c=2*e,l=c+2,e===a-1&&(l=0),c=new JSM.BodyPolygon([c,l,l+1,c+1]),null!==h&&c.SetCurveGroup(h[e]),f.AddPolygon(c);if(d){d=new JSM.BodyPolygon([]);h=new JSM.BodyPolygon([]);
  for(e=0;e<a;e++)d.AddVertexIndex(2*e+1),h.AddVertexIndex(2*(a-e-1));f.AddPolygon(d);f.AddPolygon(h)}a=g.GetVertex(0).Clone();g=g.GetVertex(1).Clone();g=JSM.CoordSub(g,a).Normalize();b=b.Clone().Normalize();g=JSM.VectorCross(b,g);d=JSM.VectorCross(g,b);f.SetCubicTextureProjection(a,d,g,b);return f};
  JSM.GeneratePrismWithHole=function(a,b,c,d,e){function f(a,b){var c=[],d=a.ToContourPolygon2D(),d=JSM.ConvertContourPolygonToPolygon2D(d,c);if(null!==d&&(d=JSM.TriangulatePolygon2D(d),null!==d)){var e,f,h,l,u,t;for(e=0;e<d.length;e++){h=d[e];u=new JSM.BodyPolygon([]);t=new JSM.BodyPolygon([]);for(f=0;3>f;f++)l=c[h[f]],u.AddVertexIndex(2*b[l[0]]+2*l[1]+1),l=c[h[2-f]],t.AddVertexIndex(2*b[l[0]]+2*l[1]);g.AddPolygon(u);g.AddPolygon(t)}}}var g=new JSM.Body,h=[],l=new JSM.ContourPolygon;l.FromArray(a);
  (function(a,b,c,d){var e,f,g,h,l;for(e=0;e<a.ContourCount();e++){g=a.GetContour(e);for(f=0;f<g.VertexCount();f++)h=g.GetVertex(f).Clone(),l=g.GetVertex(f).Clone().Offset(b,c),d.AddVertex(new JSM.BodyVertex(h)),d.AddVertex(new JSM.BodyVertex(l))}})(l,b,c,g);(function(a,b,c,d){var e=0,f,g,h,l,t,w;for(f=0;f<a.ContourCount();f++){g=a.GetContour(f);w=null;void 0!==c&&null!==c&&(w=JSM.CalculatePolygonCurveGroups(g,c));h=g.VertexCount();b.push(e);for(g=0;g<h;g++)l=2*e+2*g,t=l+2,g==h-1&&(t=2*e),l=new JSM.BodyPolygon([l,
  t,t+1,l+1]),null!==w&&l.SetCurveGroup(w[g]),d.AddPolygon(l);e+=h}})(l,h,e,g);d&&f(l,h);c=JSM.CoordSub(a[1],a[0]).Normalize();a=new JSM.Coord(a[0].x,a[0].y,a[0].z);b=b.Clone().Normalize();c=JSM.VectorCross(b,c);d=JSM.VectorCross(c,b);g.SetCubicTextureProjection(a,d,c,b);return g};
  JSM.GeneratePrismShell=function(a,b,c,d,e){var f=new JSM.Body,g=a.length,h;for(h=0;h<g;h++)f.AddVertex(new JSM.BodyVertex(a[h]));h=new JSM.Polygon;h.vertices=a;d=JSM.OffsetPolygonContour(h,d).vertices;for(h=0;h<g;h++)f.AddVertex(new JSM.BodyVertex(d[h]));var l;for(h=0;h<g;h++)l=a[h].Clone().Offset(b,c),f.AddVertex(new JSM.BodyVertex(l));for(h=0;h<g;h++)l=d[h].Clone().Offset(b,c),f.AddVertex(new JSM.BodyVertex(l));var k;for(h=0;h<g;h++)c=h,d=c+1,l=c+2*g,k=l+1,h===g-1&&(d=0,k=2*g),f.AddPolygon(new JSM.BodyPolygon([c,
  d,k,l])),f.AddPolygon(new JSM.BodyPolygon([c+g,l+g,k+g,d+g]));if(e)for(h=0;h<g;h++)c=h,d=c+1,l=h+g,k=l+1,h===g-1&&(d=0,k=g),f.AddPolygon(new JSM.BodyPolygon([c,l,k,d])),f.AddPolygon(new JSM.BodyPolygon([c+2*g,d+2*g,k+2*g,l+2*g]));e=JSM.CoordSub(a[1],a[0]).Normalize();a=new JSM.Coord(a[0].x,a[0].y,a[0].z);b=b.Clone().Normalize();e=JSM.VectorCross(b,e);g=JSM.VectorCross(e,b);f.SetCubicTextureProjection(a,g,e,b);return f};
  JSM.GenerateCylinderShell=function(a,b,c,d,e,f){for(var g=new JSM.Vector(0,0,1),h=-b/2,l=[],k=2*Math.PI/d,m,n=0,n=0;n<d;n++)m=n*k,m=JSM.PolarToCartesian(a,m),l.push(new JSM.Coord(m.x,m.y,h));a=JSM.GeneratePrismShell(l,g,b,c,e);if(f)for(f=0;f<d;f++)a.GetPolygon(2*f).SetCurveGroup(0),a.GetPolygon(2*f+1).SetCurveGroup(0);return a};
  JSM.GenerateLineShell=function(a,b,c,d,e,f){var g=new JSM.Body,h=a.length,l=[],k,m,n,p,q,r;for(k=0;k<h;k++)0===k||k===h-1?q=Math.PI/2:(m=k-1,n=k,p=k+1,r=JSM.CoordSub(a[p],a[n]),q=JSM.CoordSub(a[m],a[n]),q=r.AngleTo(q)/2,JSM.CoordOrientation(a[m],a[n],a[p],b)==JSM.Orientation.Clockwise&&(q=Math.PI-q)),l.push(q);r=new JSM.Vector(0,0,1);m=[];var s,v;for(k=0;k<h;k++)n=k,k===h-1?v=JSM.CoordSub(a[n-1],a[n]):(p=(k+1)%h,v=JSM.CoordSub(a[n],a[p])),q=l[n],p=d/Math.sin(q),s=a[n].Clone(),s.Offset(v,p),s.Rotate(r,
  -(Math.PI-q),a[n]),m.push(s);for(k=0;k<h;k++)g.AddVertex(new JSM.BodyVertex(a[k]));for(k=0;k<h;k++)g.AddVertex(new JSM.BodyVertex(m[k]));for(k=0;k<h;k++)n=a[k].Clone().Offset(b,c),g.AddVertex(new JSM.BodyVertex(n));for(k=0;k<h;k++)n=m[k].Clone().Offset(b,c),g.AddVertex(new JSM.BodyVertex(n));for(k=0;k<h-1;k++)n=k,p=n+1,c=n+2*h,d=c+1,g.AddPolygon(new JSM.BodyPolygon([n,p,d,c])),g.AddPolygon(new JSM.BodyPolygon([n+h,c+h,d+h,p+h]));e&&(n=0,g.AddPolygon(new JSM.BodyPolygon([n,n+2*h,n+3*h,n+h])),n=h-1,
  g.AddPolygon(new JSM.BodyPolygon([n,n+h,n+3*h,n+2*h])));if(f)for(k=0;k<h-1;k++)n=k,p=n+1,c=k+h,d=c+1,g.AddPolygon(new JSM.BodyPolygon([n,c,d,p])),g.AddPolygon(new JSM.BodyPolygon([n+2*h,p+2*h,d+2*h,c+2*h]));e=JSM.CoordSub(a[1],a[0]).Normalize();a=new JSM.Coord(a[0].x,a[0].y,a[0].z);b=b.Clone().Normalize();e=JSM.VectorCross(b,e);f=JSM.VectorCross(e,b);g.SetCubicTextureProjection(a,f,e,b);return g};
  JSM.GenerateTorus=function(a,b,c,d,e){var f=new JSM.Body,g=0,h=2*Math.PI/d,l=[],k,m;for(k=0;k<d;k++)m=JSM.PolarToCartesian(b,g),m=new JSM.Coord(m.x+a,0,m.y),l.push(m),g+=h;b=new JSM.Coord(0,0,1);g=new JSM.Coord(0,0,0);h=2*Math.PI/c;for(k=0;k<c;k++)for(a=0;a<d;a++)m=l[a].Clone().Rotate(b,k*h,g),f.AddVertex(new JSM.BodyVertex(m));for(k=0;k<c;k++){new JSM.BodyPolygon([]);for(a=0;a<d;a++)h=k*d+a,b=h+d,l=h+1,g=b+1,a===d-1&&(l=k*d,g=(k+1)*d),k===c-1&&(b=a,g=a+1,a===d-1&&(g=0)),h=new JSM.BodyPolygon([h,
  b,g,l]),e&&h.SetCurveGroup(0),f.AddPolygon(h)}f.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return f};
  JSM.GeneratePolyTorus=function(a,b,c,d){var e=new JSM.Body,f=a.length,g=2*Math.PI/f,h=[],l;for(l=0;l<f;l++)g=a[l],g=new JSM.Coord(g.x+b,0,g.y),h.push(g);b=new JSM.Coord(0,0,1);var k=new JSM.Coord(0,0,0),g=2*Math.PI/c,m;for(l=0;l<c;l++)for(a=0;a<f;a++)m=h[a].Clone().Rotate(b,l*g,k),e.AddVertex(new JSM.BodyVertex(m));for(l=0;l<c;l++){new JSM.BodyPolygon([]);for(a=0;a<f;a++)h=l*f+a,b=h+f,g=h+1,k=b+1,a===f-1&&(g=l*f,k=(l+1)*f),l===c-1&&(b=a,k=a+1,a===f-1&&(k=0)),h=new JSM.BodyPolygon([h,b,k,g]),d&&h.SetCurveGroup(a),
  e.AddPolygon(h)}e.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return e};
  JSM.GenerateRuledFromSectors=function(a,b,c,d,e){var f=new JSM.Body;a=JSM.GetSectorSegmentation(a,c);var g=JSM.GetSectorSegmentation(b,c);c=[];b=[];JSM.GetRuledMesh(a,g,d,c,b);for(d=0;d<c.length;d++)f.AddVertex(new JSM.BodyVertex(c[d]));for(d=0;d<b.length;d++)a=b[d],a=new JSM.BodyPolygon(a),e&&a.SetCurveGroup(0),f.AddPolygon(a);f.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return f};
  JSM.GenerateGrid=function(a,b,c,d,e){var f=new JSM.Sector(new JSM.Coord(0,0,0),new JSM.Coord(a,0,0));a=new JSM.Sector(new JSM.Coord(0,b,0),new JSM.Coord(a,b,0));return JSM.GenerateRuledFromSectors(f,a,c,d,e)};JSM.GenerateSquareGrid=function(a,b,c){return JSM.GenerateGrid(a,a,b,b,c)};
  JSM.GenerateRuledFromSectorsWithHeight=function(a,b,c,d,e,f){var g=new JSM.Body;a=JSM.GetSectorSegmentation(a,c);b=JSM.GetSectorSegmentation(b,c);var h=[],l=[];JSM.GetRuledMesh(a,b,d,h,l);for(a=0;a<h.length;a++)g.AddVertex(new JSM.BodyVertex(h[a]));var k;for(a=0;a<l.length;a++)k=l[a],k=new JSM.BodyPolygon(k),e&&k.SetCurveGroup(0),g.AddPolygon(k);b=g.VertexCount();for(a=0;a<h.length;a++)k=h[a],k=new JSM.Coord(k.x,k.y,k.z),k.z-=f,g.AddVertex(new JSM.BodyVertex(k));for(a=0;a<l.length;a++){k=l[a];h=[];
  for(f=k.length-1;0<=f;f--)h.push(k[f]+b);k=new JSM.BodyPolygon(h);e&&k.SetCurveGroup(0);g.AddPolygon(k)}for(a=0;a<d;a++)e=a+b,f=e+1,l=e-b,h=l+1,k=new JSM.BodyPolygon([e,f,h,l]),g.AddPolygon(k);for(a=0;a<d;a++)e=a+c*(d+1)+b,f=e+1,l=e-b,h=l+1,k=new JSM.BodyPolygon([e,l,h,f]),g.AddPolygon(k);for(a=0;a<c;a++)e=a*(d+1)+b,f=e+d+1,l=e-b,h=l+d+1,k=new JSM.BodyPolygon([e,l,h,f]),g.AddPolygon(k);for(a=0;a<c;a++)e=(a+1)*d+a+b,f=e+d+1,l=e-b,h=l+d+1,k=new JSM.BodyPolygon([e,f,h,l]),g.AddPolygon(k);g.SetCubicTextureProjection(new JSM.Coord(0,
  0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return g};JSM.GenerateRuledFromCoords=function(a,b,c,d){var e=new JSM.Body,f=[],g=[];JSM.GetRuledMesh(a,b,c,f,g);for(a=0;a<f.length;a++)e.AddVertex(new JSM.BodyVertex(f[a]));for(a=0;a<g.length;a++)f=g[a],f=new JSM.BodyPolygon(f),d&&f.SetCurveGroup(0),e.AddPolygon(f);e.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return e};
  JSM.GenerateRevolved=function(a,b,c,d,e,f){var g=new JSM.Body,h=JSM.IsEqual(c,2*Math.PI),l=a.length,k=c/d;c=JSM.CoordSub(b.end,b.beg);var m,n,p;for(m=0;m<l;m++)for(n=0;n<=d;n++)h&&n===d||(p=a[m].Clone().Rotate(c,n*k,b.beg),g.AddVertex(new JSM.BodyVertex(p)));k=0;"CurveSegments"==f?k=1:"CurveAll"==f&&(k=2);var q,r;for(m=0;m<l-1;m++)for(n=0;n<d;n++)f=m*(d+1)+n,p=f+d+1,q=f+1,r=p+1,h&&(f=m*d+n,p=f+d,q=f+1,r=p+1,n===d-1&&(q=m*d,r=(m+1)*d)),f=new JSM.BodyPolygon([f,q,r,p]),1==k?f.SetCurveGroup(m):2==k&&
  f.SetCurveGroup(0),g.AddPolygon(f);if(h&&e){e=new JSM.BodyPolygon([]);h=new JSM.BodyPolygon([]);for(m=0;m<d;m++)e.AddVertexIndex(d*(l-1)+m),h.AddVertexIndex(d-m-1);g.AddPolygon(e);g.AddPolygon(h)}d=c.Clone().Normalize();h=new JSM.Line(b.beg,d);for(m=e=0;m<l;m++)n=h.ProjectCoord(a[m]),e+=n.DistanceTo(a[m]);e/=l;b=new JSM.Coord(b.beg.x,b.beg.y,b.beg.z);l=(new JSM.Line(b,c)).ProjectCoord(a[0]);a=JSM.CoordSub(a[0],l).Normalize();g.SetCylindricalTextureProjection(b,e,a,d);return g};
  JSM.GenerateTube=function(a,b){var c=new JSM.Body,d=a.length,e=a[0].length,f,g;for(g=0;g<e;g++)for(f=0;f<d;f++)c.AddVertex(new JSM.BodyVertex(a[f][g]));var h,l;for(g=0;g<d-1;g++)for(f=0;f<e;f++)h=g+d*f,l=h+d,f===e-1&&(l=g),c.AddPolygon(new JSM.BodyPolygon([h,l,l+1,h+1]));if(b){g=new JSM.BodyPolygon([]);h=new JSM.BodyPolygon([]);for(f=0;f<e;f++)g.AddVertexIndex(d*f+d-1);for(f=e-1;0<=f;f--)h.AddVertexIndex(d*f);c.AddPolygon(g);c.AddPolygon(h)}c.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,
  0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return c};JSM.GenerateFunctionSurface=function(a,b,c,d,e){var f=new JSM.Sector(new JSM.Coord(b.x,b.y,0),new JSM.Coord(c.x,b.y,0));b=new JSM.Sector(new JSM.Coord(b.x,c.y,0),new JSM.Coord(c.x,c.y,0));d=JSM.GenerateRuledFromSectors(f,b,d,d,e);for(e=0;e<d.VertexCount();e++)f=d.GetVertexPosition(e),f.z=a(f.x,f.y);d.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return d};
  JSM.GenerateFunctionSurfaceSolid=function(a,b,c,d,e,f){var g=new JSM.Sector(new JSM.Coord(c.x,b.y,0),new JSM.Coord(b.x,b.y,0));b=new JSM.Sector(new JSM.Coord(c.x,c.y,0),new JSM.Coord(b.x,c.y,0));e=JSM.GenerateRuledFromSectorsWithHeight(g,b,d,d,e,f);g=(d+1)*(d+1);for(d=0;d<g;d++)f=e.GetVertexPosition(d),f.z=a(f.x,f.y);e.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return e};
  JSM.Camera=function(a,b,c,d,e,f){this.eye=JSM.ValueOrDefault(a,new JSM.Coord(1,1,1));this.center=JSM.ValueOrDefault(b,new JSM.Coord(0,0,0));this.up=JSM.ValueOrDefault(c,new JSM.Vector(0,0,1));this.fieldOfView=JSM.ValueOrDefault(d,45);this.nearClippingPlane=JSM.ValueOrDefault(e,0.1);this.farClippingPlane=JSM.ValueOrDefault(f,1E3)};
  JSM.Camera.prototype.Set=function(a,b,c,d,e,f){this.eye=a;this.center=b;this.up=c;this.fieldOfView=JSM.ValueOrDefault(d,45);this.nearClippingPlane=JSM.ValueOrDefault(e,0.1);this.farClippingPlane=JSM.ValueOrDefault(f,1E3)};JSM.Camera.prototype.Clone=function(){var a=new JSM.Camera;a.eye=this.eye;a.center=this.center;a.up=this.up;a.fieldOfView=this.fieldOfView;a.nearClippingPlane=this.nearClippingPlane;a.farClippingPlane=this.farClippingPlane;return a};
  JSM.ExplodeBody=function(a,b,c){function d(a,b,c,d){var l;for(l=0;l<a.Count();l++)b.push([]);a=d.itemCount();var k;for(l=0;l<a;l++)k=d.getMaterial(l),-1!==k?b[k].push(l):c.push(l)}if(void 0===c||null===c)return!1;if(void 0===b||null===b)b=new JSM.Materials;(function(a,b,c){function h(c,d,g){if(0!==c.length){d=b.GetMaterial(d);if(void 0!==g.onPointGeometryStart&&null!==g.onPointGeometryStart)g.onPointGeometryStart(d);if(void 0!==g.onPoint&&null!==g.onPoint){var h,l;for(h=0;h<c.length;h++)l=a.GetPoint(c[h]),
  l=a.GetVertexPosition(l.GetVertexIndex()),g.onPoint(l)}if(void 0!==g.onPointGeometryEnd&&null!==g.onPointGeometryEnd)g.onPointGeometryEnd(d)}}if(0!==a.PointCount()){var l=[],k=[];d(b,l,k,{itemCount:function(){return a.PointCount()},getMaterial:function(b){return a.GetPoint(b).GetMaterialIndex()}});var m;for(m=0;m<l.length;m++)h(l[m],m,c);h(k,-1,c)}})(a,b,c);(function(a,b,c){function h(c,d,g){if(0!==c.length){d=b.GetMaterial(d);if(void 0!==g.onLineGeometryStart&&null!==g.onLineGeometryStart)g.onLineGeometryStart(d);
  if(void 0!==g.onLine&&null!==g.onLine){var h,l,k;for(h=0;h<c.length;h++)l=a.GetLine(c[h]),k=a.GetVertexPosition(l.GetBegVertexIndex()),l=a.GetVertexPosition(l.GetEndVertexIndex()),g.onLine(k,l)}if(void 0!==g.onLineGeometryEnd&&null!==g.onLineGeometryEnd)g.onLineGeometryEnd(d)}}if(0!==a.LineCount()){var l=[],k=[];d(b,l,k,{itemCount:function(){return a.LineCount()},getMaterial:function(b){return a.GetLine(b).GetMaterialIndex()}});var m;for(m=0;m<l.length;m++)h(l[m],m,c);h(k,-1,c)}})(a,b,c);(function(a,
  b,c){function h(a,b){var c=JSM.CalculateBodyVertexNormals(a),d,e;e=!1;if(void 0!==b&&null!==b)for(d=0;d<b.Count();d++)if(null!==b.GetMaterial(d).texture){e=!0;break}var f=null,g;if(e){f=JSM.CalculateBodyTextureCoords(a);for(d=0;d<f.length;d++)if(e=a.GetPolygon(d),e.HasMaterialIndex()){g=b.GetMaterial(e.GetMaterialIndex());for(e=0;e<f[d].length;e++)f[d][e].x/=g.textureWidth,f[d][e].y/=-g.textureHeight}}return{vertexNormals:c,textureCoords:f}}function l(c,d,g,h){if(0!==c.length){d=b.GetMaterial(d);
  if(void 0!==h.onGeometryStart&&null!==h.onGeometryStart)h.onGeometryStart(d);var l;for(l=0;l<c.length;l++){var k=c[l],m=g,n=h,p=a.GetPolygon(k),x=p.VertexIndexCount();if(3>x)JSM.Message("Invalid polygon found.");else{var y=void 0,z=void 0,E=void 0,H=void 0,I=void 0,D=void 0,C=void 0,K=void 0,L=void 0,y=!1;void 0!==n.hasConvexPolygons&&null!==n.hasConvexPolygons&&(y=n.hasConvexPolygons);var F=void 0;if(3==x||y)for(F=0;F<x-2;F++){if(y=a.GetVertexPosition(p.GetVertexIndex(0)),z=a.GetVertexPosition(p.GetVertexIndex((F+
  1)%x)),E=a.GetVertexPosition(p.GetVertexIndex((F+2)%x)),H=m.vertexNormals[k][0],I=m.vertexNormals[k][(F+1)%x],D=m.vertexNormals[k][(F+2)%x],L=K=C=null,null!==m.textureCoords&&(C=m.textureCoords[k][0],K=m.textureCoords[k][(F+1)%x],L=m.textureCoords[k][(F+2)%x]),void 0!==n.onTriangle&&null!==n.onTriangle)n.onTriangle(y,z,E,H,I,D,C,K,L)}else{y=new JSM.Polygon;z=void 0;for(F=0;F<x;F++)z=a.GetVertexPosition(p.vertices[F]),y.AddVertex(z.x,z.y,z.z);F=JSM.CalculateBodyPolygonNormal(a,k);x=JSM.TriangulatePolygon(y,
  F);if(null!==x)for(var J=void 0,F=0;F<x.length;F++){if(J=x[F],y=a.GetVertexPosition(p.GetVertexIndex(J[0])),z=a.GetVertexPosition(p.GetVertexIndex(J[1])),E=a.GetVertexPosition(p.GetVertexIndex(J[2])),H=m.vertexNormals[k][J[0]],I=m.vertexNormals[k][J[1]],D=m.vertexNormals[k][J[2]],L=K=C=null,null!==m.textureCoords&&(C=m.textureCoords[k][J[0]],K=m.textureCoords[k][J[1]],L=m.textureCoords[k][J[2]]),void 0!==n.onTriangle&&null!==n.onTriangle)n.onTriangle(y,z,E,H,I,D,C,K,L)}else JSM.Message("Triangulation failed.")}}}if(void 0!==
  h.onGeometryEnd&&null!==h.onGeometryEnd)h.onGeometryEnd(d)}}if(0!==a.PolygonCount()){var k=[],m=[];d(b,k,m,{itemCount:function(){return a.PolygonCount()},getMaterial:function(b){return a.GetPolygon(b).GetMaterialIndex()}});var n=h(a,b),p;for(p=0;p<k.length;p++)l(k[p],p,n,c);l(m,-1,n,c)}})(a,b,c);return!0};
  JSM.ExportBodyContentToStl=function(a,b,c){function d(a){g+=a+"\n"}function e(a,b,c,e){d("\tfacet normal "+a.x+" "+a.y+" "+a.z);d("\t\touter loop");d("\t\t\tvertex "+b.x+" "+b.y+" "+b.z);d("\t\t\tvertex "+c.x+" "+c.y+" "+c.z);d("\t\t\tvertex "+e.x+" "+e.y+" "+e.z);d("\t\tendloop");d("\tendfacet")}function f(b){var d=a.GetPolygon(b),f=d.VertexIndexCount();if(!(3>f)){var g,n,p,q=null;if(3===f)q=JSM.CalculateBodyPolygonNormal(a,b),g=a.GetVertex(d.GetVertexIndex(0)).position,n=a.GetVertex(d.GetVertexIndex(1)).position,
  p=a.GetVertex(d.GetVertexIndex(2)).position,e(q,g,n,p);else if(g=!0,void 0!==c&&c&&(g=!1),q=JSM.CalculateBodyPolygonNormal(a,b),g){g=new JSM.Polygon;for(b=0;b<f;b++)n=a.GetVertex(d.vertices[b]),g.AddVertex(n.position.x,n.position.y,n.position.z);f=JSM.TriangulatePolygon(g,q);if(null!==f)for(b=0;b<f.length;b++)p=f[b],g=a.GetVertex(d.GetVertexIndex(p[0])).position,n=a.GetVertex(d.GetVertexIndex(p[1])).position,p=a.GetVertex(d.GetVertexIndex(p[2])).position,e(q,g,n,p)}else for(b=0;b<f-2;b++)g=a.GetVertex(d.GetVertexIndex(0)).position,
  n=a.GetVertex(d.GetVertexIndex((b+1)%f)).position,p=a.GetVertex(d.GetVertexIndex((b+2)%f)).position,e(q,g,n,p)}}var g="";for(b=0;b<a.PolygonCount();b++)f(b);return g};JSM.ExportBodyToStl=function(a,b,c){a=""+("solid "+b+"\n")+JSM.ExportBodyContentToStl(a,b,c);return a+="endsolid "+b+"\n"};JSM.ExportModelToStl=function(a,b,c){var d;d=""+("solid "+b+"\n");var e,f;for(e=0;e<a.BodyCount();e++)f=a.GetBody(e),d+=JSM.ExportBodyContentToStl(f,b+(e+1).toString(),c);return d+("endsolid "+b+"\n")};
  JSM.ExportBodyContentToObj=function(a,b,c){function d(b){b=a.GetVertex(b).position;g+="v "+b.x+" "+b.y+" "+b.z+"\n"}function e(b){b=JSM.CalculateBodyPolygonNormal(a,b);g+="vn "+b.x+" "+b.y+" "+b.z+"\n"}function f(d){var e=a.GetPolygon(d);g+="f ";var f;for(f=0;f<e.VertexIndexCount();f++){var h=b+e.GetVertexIndex(f)+1+"//"+(c+d+1)+" ";g+=h}g+="\n"}var g="",h;for(h=0;h<a.VertexCount();h++)d(h);for(h=0;h<a.PolygonCount();h++)e(h);for(h=0;h<a.PolygonCount();h++)f(h);return g};
  JSM.ExportBodyToObj=function(a){return JSM.ExportBodyContentToObj(a,0,0)};JSM.ExportModelToObj=function(a){var b="",c=0,d=0,e,f;for(e=0;e<a.BodyCount();e++)f=a.GetBody(e),b+=JSM.ExportBodyContentToObj(f,c,d),c+=f.VertexCount(),d+=f.PolygonCount();return b};
  JSM.ExportMaterialsToGdl=function(a){function b(a,b){var d=JSM.HexColorToRGBComponents(a.diffuse);c+='define material "material'+b+'" 2, '+(d[0]/255+","+d[1]/255+","+d[2]/255)+" ! "+b+"\n"}var c="",d=!1;void 0!==a&&null!==a&&(d=!0);if(d){b(a.GetDefaultMaterial(),1);for(d=0;d<a.Count();d++)b(a.GetMaterial(d),d+2)}return c};
  JSM.ExportBodyGeometryToGdl=function(a,b){function c(a){if(200<a.length){var b=0,c,d;for(c=0;c<a.length;c++)d=a[c],h+=d,b++,200<b&&","==d&&(h+="\n",b=0)}else h+=a}function d(a){c(a+"\n")}function e(b){var c=a.GetVertex(b).position;d("vert "+c.x+", "+c.y+", "+c.z+" ! "+(b+1))}function f(b,c){var e=b.edges[c],f=0;-1!=e.pgon1&&-1!=e.pgon2&&a.GetPolygon(e.pgon1).HasCurveGroup()&&a.GetPolygon(e.pgon2).HasCurveGroup()&&a.GetPolygon(e.pgon1).GetCurveGroup()==a.GetPolygon(e.pgon2).GetCurveGroup()&&(f=2);
  d("edge "+(e.vert1+1)+", "+(e.vert2+1)+", -1, -1, "+f+" ! "+(c+1))}function g(e,f,g){var h=-1;b&&(h=a.GetPolygon(f).GetMaterialIndex()+2,h!=g&&d('set material "material'+h+'"'));e=e.pgons[f];g=0;a.GetPolygon(f).HasCurveGroup()&&(g=2);c("pgon "+e.pedges.length+", 0, "+g+", ");g="";var k,l;for(k=0;k<e.pedges.length;k++)l=e.pedges[k],g=l.reverse?g+-(l.index+1):g+(l.index+1),k<e.pedges.length-1&&(g+=", ");c(g);c(" ! "+(f+1));d("");return h}var h="";d("base");var l=new JSM.AdjacencyInfo(a),k;for(k=0;k<
  l.verts.length;k++)e(k);for(k=0;k<l.edges.length;k++)f(l,k);var m=-1;for(k=0;k<l.pgons.length;k++)m=g(l,k,m);d("body -1");return h};JSM.ExportBodyToGdl=function(a,b){var c="",d=!1;void 0!==b&&null!==b&&(c+=JSM.ExportMaterialsToGdl(b),d=!0);return c+=JSM.ExportBodyGeometryToGdl(a,d)};JSM.ExportModelToGdl=function(a,b){var c="",d=!1;void 0!==b&&null!==b&&(c+=JSM.ExportMaterialsToGdl(b),d=!0);var e,f;for(e=0;e<a.BodyCount();e++)f=a.GetBody(e),c+=JSM.ExportBodyGeometryToGdl(f,d);return c};
  JSM.TriangleBody=function(a){this.name=a;this.vertices=[];this.normals=[];this.uvs=[];this.triangles=[];this.defaultUVIndex=-1};JSM.TriangleBody.prototype.SetName=function(a){this.name=a};JSM.TriangleBody.prototype.GetName=function(){return this.name};JSM.TriangleBody.prototype.AddVertex=function(a,b,c){this.vertices.push(new JSM.Coord(a,b,c));return this.vertices.length-1};JSM.TriangleBody.prototype.GetVertex=function(a){return this.vertices[a]};
  JSM.TriangleBody.prototype.SetVertex=function(a,b,c,d){this.vertices[a]=new JSM.Coord(b,c,d)};JSM.TriangleBody.prototype.VertexCount=function(){return this.vertices.length};JSM.TriangleBody.prototype.AddNormal=function(a,b,c){this.normals.push(new JSM.Vector(a,b,c));return this.normals.length-1};JSM.TriangleBody.prototype.GetNormal=function(a){return this.normals[a]};
  JSM.TriangleBody.prototype.GetTriangleNormal=function(a,b){var c=null,d=this.triangles[a];if(-1==d.curve)c=this.GetNormal(d.n0);else var c=this.GetVertex(d.v0),e=this.GetVertex(d.v1),f=this.GetVertex(d.v2),g=this.GetNormal(d.n0),h=this.GetNormal(d.n1),d=this.GetNormal(d.n2),c=JSM.BarycentricInterpolation(c,e,f,g,h,d,b);return c};JSM.TriangleBody.prototype.NormalCount=function(){return this.normals.length};
  JSM.TriangleBody.prototype.AddUV=function(a,b){this.uvs.push(new JSM.Coord2D(a,b));return this.uvs.length-1};JSM.TriangleBody.prototype.AddDefaultUV=function(){return-1!=this.defaultUVIndex?this.defaultUVIndex:this.defaultUVIndex=this.AddUV(0,0)};JSM.TriangleBody.prototype.GetUV=function(a){return this.uvs[a]};JSM.TriangleBody.prototype.UVCount=function(){return this.uvs.length};
  JSM.TriangleBody.prototype.AddTriangle=function(a,b,c,d,e,f,g,h,l,k,m){this.triangles.push({v0:a,v1:b,v2:c,n0:d,n1:e,n2:f,u0:g,u1:h,u2:l,mat:k,curve:m});return this.triangles.length-1};JSM.TriangleBody.prototype.GetTriangle=function(a){return this.triangles[a]};JSM.TriangleBody.prototype.TriangleCount=function(){return this.triangles.length};
  JSM.TriangleBody.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),c,d;for(c=0;c<this.vertices.length;c++)d=this.vertices[c],a.x=JSM.Minimum(a.x,d.x),a.y=JSM.Minimum(a.y,d.y),a.z=JSM.Minimum(a.z,d.z),b.x=JSM.Maximum(b.x,d.x),b.y=JSM.Maximum(b.y,d.y),b.z=JSM.Maximum(b.z,d.z);return new JSM.Box(a,b)};JSM.TriangleBody.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
  JSM.TriangleBody.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,c,d;for(c=0;c<this.vertices.length;c++)d=a.DistanceTo(this.vertices[c]),JSM.IsGreater(d,b)&&(b=d);return new JSM.Sphere(a,b)};
  JSM.TriangleBody.prototype.Finalize=function(a){function b(b,c,d,e){function f(a,b,c,d,e){var g=new JSM.Vector(0,0,0),h=0;c=a.GetTriangle(c);b=e[b];var k,l;for(e=0;e<b.length;e++)k=b[e],l=a.GetTriangle(k),c.curve==l.curve&&(g=JSM.CoordAdd(g,d[k]),h+=1);g.MultiplyScalar(1/h);g.Normalize();return a.AddNormal(g.x,g.y,g.z)}var g=b.triangles[c];if(void 0===g.mat||0>g.mat)g.mat=a.GetDefaultMaterialIndex();if(void 0===g.n0||void 0===g.n1||void 0===g.n2)void 0===g.curve||0>g.curve?(c=d[c],c=b.AddNormal(c.x,
  c.y,c.z),g.n0=c,g.n1=c,g.n2=c,g.curve=-1):(g.n0=f(b,g.v0,c,d,e),g.n1=f(b,g.v1,c,d,e),g.n2=f(b,g.v2,c,d,e));if(void 0===g.u0||void 0===g.u1||void 0===g.u2)g.u0=b.AddDefaultUV(),g.u1=b.AddDefaultUV(),g.u2=b.AddDefaultUV()}var c=[],d={},e;for(e=0;e<this.vertices.length;e++)d[e]=[];var f,g;for(e=0;e<this.triangles.length;e++)f=this.triangles[e],g=JSM.CalculateTriangleNormal(this.vertices[f.v0],this.vertices[f.v1],this.vertices[f.v2]),c.push(g),d[f.v0].push(e),d[f.v1].push(e),d[f.v2].push(e);for(e=0;e<
  this.triangles.length;e++)b(this,e,c,d)};
  JSM.TriangleBody.prototype.Clone=function(){var a=new JSM.TriangleBody(this.name),b,c;for(b=0;b<this.vertices.length;b++)a.vertices.push(this.vertices[b].Clone());for(b=0;b<this.normals.length;b++)a.normals.push(this.normals[b].Clone());for(b=0;b<this.uvs.length;b++)a.uvs.push(this.uvs[b].Clone());for(b=0;b<this.triangles.length;b++)c=this.triangles[b],a.triangles.push({v0:c.v0,v1:c.v1,v2:c.v2,n0:c.n0,n1:c.n1,n2:c.n2,u0:c.u0,u1:c.u1,u2:c.u2,mat:c.mat,curve:c.curve});return a};
  JSM.ConvertTriangleBodyToOctree=function(a){var b=new JSM.TriangleOctree(a.GetBoundingBox()),c,d,e,f;for(c=0;c<a.TriangleCount();c++)d=a.GetTriangle(c),e=a.GetVertex(d.v0),f=a.GetVertex(d.v1),d=a.GetVertex(d.v2),b.AddTriangle(e,f,d,{triangleIndex:c});return b};JSM.TriangleModel=function(){this.materials=[];this.bodies=[];this.defaultMaterial=-1};JSM.TriangleModel.prototype.AddMaterial=function(a){this.materials.push(a);return this.materials.length-1};JSM.TriangleModel.prototype.GetMaterial=function(a){return this.materials[a]};
  JSM.TriangleModel.prototype.AddDefaultMaterial=function(){-1==this.defaultMaterial&&(this.defaultMaterial=this.AddMaterial({}));return this.defaultMaterial};JSM.TriangleModel.prototype.GetDefaultMaterialIndex=function(){return this.AddDefaultMaterial()};JSM.TriangleModel.prototype.MaterialCount=function(){return this.materials.length};JSM.TriangleModel.prototype.AddBody=function(a){this.bodies.push(a);return this.bodies.length-1};
  JSM.TriangleModel.prototype.AddBodyToIndex=function(a,b){this.bodies.splice(b,0,a);return b};JSM.TriangleModel.prototype.GetBody=function(a){return this.bodies[a]};JSM.TriangleModel.prototype.VertexCount=function(){var a=0,b,c;for(b=0;b<this.bodies.length;b++)c=this.bodies[b],a+=c.VertexCount();return a};JSM.TriangleModel.prototype.TriangleCount=function(){var a=0,b,c;for(b=0;b<this.bodies.length;b++)c=this.bodies[b],a+=c.TriangleCount();return a};JSM.TriangleModel.prototype.BodyCount=function(){return this.bodies.length};
  JSM.TriangleModel.prototype.FinalizeMaterials=function(){var a={name:"Default",ambient:[0.5,0.5,0.5],diffuse:[0.5,0.5,0.5],specular:[0.1,0.1,0.1],shininess:0,opacity:1,reflection:0,texture:null,offset:null,scale:null,rotation:null},b,c;for(b=0;b<this.materials.length;b++)c=this.materials[b],JSM.CopyObjectProperties(a,c,!1)};JSM.TriangleModel.prototype.FinalizeBodies=function(){var a,b;for(a=0;a<this.bodies.length;a++)b=this.bodies[a],b.Finalize(this)};
  JSM.TriangleModel.prototype.Finalize=function(){this.FinalizeBodies();this.FinalizeMaterials()};
  JSM.ConvertBodyToTriangleBody=function(a){function b(a,b,c,d,e){c=a.AddTriangle(c,d,e);a=a.GetTriangle(c);b.HasMaterialIndex()&&(a.mat=b.GetMaterialIndex());b.HasCurveGroup()&&(a.curve=b.GetCurveGroup())}var c=new JSM.TriangleBody,d,e,f;for(d=0;d<a.VertexCount();d++)f=a.GetVertexPosition(d),c.AddVertex(f.x,f.y,f.z);var g,h,l,k;for(d=0;d<a.PolygonCount();d++)if(g=a.GetPolygon(d),h=g.VertexIndexCount(),!(3>h))if(3==h)f=g.GetVertexIndex(0),h=g.GetVertexIndex(1),k=g.GetVertexIndex(2),b(c,g,f,h,k);else{l=
  new JSM.Polygon;for(e=0;e<h;e++)f=a.GetVertexPosition(g.GetVertexIndex(e)),l.AddVertex(f.x,f.y,f.z);e=JSM.CalculateBodyPolygonNormal(a,d);l=JSM.TriangulatePolygon(l,e);if(null!==l)for(e=0;e<l.length;e++)k=l[e],f=g.GetVertexIndex(k[0]),h=g.GetVertexIndex(k[1]),k=g.GetVertexIndex(k[2]),b(c,g,f,h,k)}return c};
  JSM.ConvertModelToTriangleModel=function(a,b){var c=new JSM.TriangleModel,d,e;if(void 0!==b&&null!==b)for(d=0;d<b.Count();d++)e=b.GetMaterial(d),c.AddMaterial({name:"Material"+d,ambient:JSM.HexColorToNormalizedRGBComponents(e.ambient),diffuse:JSM.HexColorToNormalizedRGBComponents(e.diffuse),specular:JSM.HexColorToNormalizedRGBComponents(e.specular),shininess:e.shininess,opacity:e.opacity,reflection:e.reflection});for(d=0;d<a.BodyCount();d++)e=a.GetBody(d),e=JSM.ConvertBodyToTriangleBody(e),c.AddBody(e);
  c.Finalize();return c};
  JSM.ConvertTriangleModelToJsonData=function(a){function b(a,b,c){var d=[],e=a.MaterialCount(),f;for(a=0;a<b.VertexCount();a++)f=b.GetVertex(a),c.vertices.push(f.x,f.y,f.z);for(a=0;a<b.NormalCount();a++)f=b.GetNormal(a),c.normals.push(f.x,f.y,f.z);for(a=0;a<b.UVCount();a++)f=b.GetUV(a),c.uvs.push(f.x,f.y);for(a=0;a<e;a++)d.push([]);for(a=0;a<b.TriangleCount();a++)f=b.GetTriangle(a),void 0===f.mat||(0>f.mat||f.mat>=e)||d[f.mat].push(a);var p,q;for(a=0;a<d.length;a++)if(p=d[a],0!==p.length){q={material:a,
  parameters:[]};for(e=0;e<p.length;e++)f=b.GetTriangle(p[e]),q.parameters.push(f.v0,f.v1,f.v2,f.n0,f.n1,f.n2,f.u0,f.u1,f.u2);c.triangles.push(q)}}var c={version:1,materials:[],meshes:[]};(function(a,b){var c,d,e;for(c=0;c<a.MaterialCount();c++)d=a.GetMaterial(c),e={name:JSM.ValueOrDefault(d.name,""),ambient:d.ambient,diffuse:d.diffuse,specular:d.specular,shininess:d.shininess,opacity:d.opacity},void 0!==d.texture&&null!==d.texture&&(e.texture=JSM.ValueOrDefault(d.texture,null),e.offset=d.offset,e.scale=
  d.scale,e.rotation=d.rotation),b.push(e)})(a,c.materials);var d,e,f;for(d=0;d<a.BodyCount();d++)e=a.GetBody(d),0!==e.TriangleCount()&&(f={name:e.GetName(),vertices:[],normals:[],uvs:[],triangles:[]},b(a,e,f),c.meshes.push(f));return c};
  JSM.MergeJsonDataMeshes=function(a){function b(a,b,c){function d(a,b,c,e,f,g){var h=b.material,k=c[h];void 0===k&&(a.triangles.push({material:h,parameters:[]}),k=a.triangles.length-1,c[h]=k);a=a.triangles[k].parameters;for(c=0;c<b.parameters.length;c+=9)a.push(b.parameters[c]+e,b.parameters[c+1]+e,b.parameters[c+2]+e,b.parameters[c+3]+f,b.parameters[c+4]+f,b.parameters[c+5]+f,b.parameters[c+6]+g,b.parameters[c+7]+g,b.parameters[c+8]+g)}var e=a.vertices.length/3,f=a.normals.length/3,g=a.uvs.length/
  2;(function(a,b){var c;for(c=0;c<b.vertices.length;c++)a.vertices.push(b.vertices[c]);for(c=0;c<b.normals.length;c++)a.normals.push(b.normals[c]);for(c=0;c<b.uvs.length;c++)a.uvs.push(b.uvs[c])})(a,b);var r,s;for(r=0;r<b.triangles.length;r++)s=b.triangles[r],d(a,s,c,e,f,g)}var c={version:a.version,materials:a.materials,meshes:[]},d={name:"Merged",vertices:[],normals:[],uvs:[],triangles:[]},e={},f,g;for(f=0;f<a.meshes.length;f++)g=a.meshes[f],b(d,g,e);c.meshes.push(d);return c};
  JSM.RayTriangleIntersection=function(a,b,c,d){var e=a.GetOrigin(),f=a.GetDirection();c=JSM.CoordSub(c,b);d=JSM.CoordSub(d,b);var g=JSM.VectorCross(f,d),h=JSM.VectorDot(c,g);if(JSM.IsZero(h)||!JSM.IsPositive(h))return null;var h=1/h,l=JSM.CoordSub(e,b);b=JSM.VectorDot(l,g)*h;if(JSM.IsLower(b,0)||JSM.IsGreater(b,1))return null;c=JSM.VectorCross(l,c);g=JSM.VectorDot(f,c)*h;if(JSM.IsLower(g,0)||JSM.IsGreater(b+g,1))return null;d=JSM.VectorDot(d,c)*h;if(!JSM.IsPositive(d)||a.IsLengthReached(d))return null;
  a=f.Clone().MultiplyScalar(d);return{position:JSM.CoordAdd(e,a),distance:d}};
  JSM.RayBoxIntersection=function(a,b,c){var d=a.GetOrigin(),e=a.GetDirection(),f=JSM.CoordToArray(d),e=JSM.CoordToArray(e);b=JSM.CoordToArray(b);c=JSM.CoordToArray(c);var g=[0,0,0],h=[0,0,0],l=!0,k;for(k=0;3>k;k++)JSM.IsLower(f[k],b[k])?(g[k]=-1,h[k]=b[k],l=!1):JSM.IsGreater(f[k],c[k])?(g[k]=1,h[k]=c[k],l=!1):g[k]=0;k=null;if(l)return k={position:d,distance:0};l=[0,0,0];for(k=0;3>k;k++)0!==g[k]&&!JSM.IsZero(e[k])?l[k]=(h[k]-f[k])/e[k]:l[k]=-1;g=0;for(k=1;3>k;k++)JSM.IsLower(l[g],l[k])&&(g=k);if(JSM.IsNegative(l[g]))return null;
  var m=[0,0,0];for(k=0;3>k;k++)if(g!=k){if(m[k]=f[k]+l[g]*e[k],JSM.IsLower(m[k],b[k])||JSM.IsGreater(m[k],c[k]))return null}else m[k]=h[k];f=JSM.CoordFromArray(m);d=d.DistanceTo(f);return a.IsLengthReached(d)?null:k={position:f,distance:d}};
  JSM.RayOctreeIntersection=function(a,b,c){var d=null,e=!1,f=null!==c&&void 0!==c;JSM.TraverseOctreeNodes(b,function(b){if(!f&&e||!JSM.RayBoxIntersection(a,b.box.min,b.box.max))return!1;var c;for(c=0;c<b.triangles.length;c++){var l=b.triangles[c],k=JSM.RayTriangleIntersection(a,l.v0,l.v1,l.v2);if(null!==k){e=!0;if(!f)return!1;if(null===d||k.distance<d.distance)d=k,d.userData=l.userData}}return!0});f&&null!==d&&(c.position=d.position,c.distance=d.distance,c.userData=d.userData);return e};
  JSM.RayTriangleBodyIntersection=function(a,b,c){var d=null,e=!1,f=null!==c&&void 0!==c,g,h,l,k;for(g=0;g<b.TriangleCount();g++)if(h=b.GetTriangle(g),l=b.GetVertex(h.v0),k=b.GetVertex(h.v1),h=b.GetVertex(h.v2),l=JSM.RayTriangleIntersection(a,l,k,h),null!==l){e=!0;if(!f)break;if(null===d||l.distance<d.distance)d=l,d.triangleIndex=g}f&&null!==d&&(c.position=d.position,c.distance=d.distance,c.triangleIndex=d.triangleIndex);return e};
  JSM.RayTriangleModelIntersection=function(a,b,c){var d=null,e=!1,f=null!==c&&void 0!==c,g,h,l;for(g=0;g<b.BodyCount();g++)if(h=b.GetBody(g),l=f?{}:null,JSM.RayTriangleBodyIntersection(a,h,l)){e=!0;if(!f)break;if(null===d||l.distance<d.distance)d=l,d.bodyIndex=g}f&&null!==d&&(c.position=d.position,c.distance=d.distance,c.triangleIndex=d.triangleIndex,c.bodyIndex=d.bodyIndex);return e};
  JSM.RayTriangleModelIntersectionWithOctree=function(a,b,c){var d=null,e=!1,f=null!==c&&void 0!==c,g,h,l;for(g=0;g<b.BodyCount();g++)if(h=b.GetBody(g),l=f?{}:null,20<h.TriangleCount()?(void 0===h.octree&&(h.octree=JSM.ConvertTriangleBodyToOctree(h)),h=JSM.RayOctreeIntersection(a,h.octree,l)):h=JSM.RayTriangleBodyIntersection(a,h,l),h){e=!0;if(!f)break;if(null===d||l.distance<d.distance)d=l,void 0!==l.userData&&(d.triangleIndex=l.userData.triangleIndex),d.bodyIndex=g}f&&null!==d&&(c.position=d.position,
  c.distance=d.distance,c.triangleIndex=d.triangleIndex,c.bodyIndex=d.bodyIndex);return e};JSM.BinaryReader=function(a,b){this.arrayBuffer=a;this.dataView=new DataView(a);this.isLittleEndian=b;this.position=0};JSM.BinaryReader.prototype.GetPosition=function(){return this.position};JSM.BinaryReader.prototype.GetByteLength=function(){return this.arrayBuffer.byteLength};JSM.BinaryReader.prototype.Skip=function(a){this.position+=a};JSM.BinaryReader.prototype.End=function(){return this.position>=this.arrayBuffer.byteLength};
  JSM.BinaryReader.prototype.ReadBoolean=function(){var a=this.dataView.getInt8(this.position);this.position+=1;return a?!0:!1};JSM.BinaryReader.prototype.ReadCharacter=function(){var a=this.dataView.getInt8(this.position);this.position+=1;return a};JSM.BinaryReader.prototype.ReadUnsignedCharacter=function(){var a=this.dataView.getUint8(this.position);this.position+=1;return a};
  JSM.BinaryReader.prototype.ReadInteger16=function(){var a=this.dataView.getInt16(this.position,this.isLittleEndian);this.position+=2;return a};JSM.BinaryReader.prototype.ReadUnsignedInteger16=function(){var a=this.dataView.getUint16(this.position,this.isLittleEndian);this.position+=2;return a};JSM.BinaryReader.prototype.ReadInteger32=function(){var a=this.dataView.getInt32(this.position,this.isLittleEndian);this.position+=4;return a};
  JSM.BinaryReader.prototype.ReadUnsignedInteger32=function(){var a=this.dataView.getUint32(this.position,this.isLittleEndian);this.position+=4;return a};JSM.BinaryReader.prototype.ReadFloat32=function(){var a=this.dataView.getFloat32(this.position,this.isLittleEndian);this.position+=4;return a};JSM.BinaryReader.prototype.ReadDouble64=function(){var a=this.dataView.getFloat64(this.position,this.isLittleEndian);this.position+=8;return a};
  JSM.GetArrayBufferFromURL=function(a,b){var c=new XMLHttpRequest;c.open("GET",a,!0);c.responseType="arraybuffer";c.onload=function(){var a=c.response;a&&b(a)};c.send(null)};JSM.GetArrayBufferFromFile=function(a,b){var c=new FileReader;c.onloadend=function(a){a.target.readyState==FileReader.DONE&&b(a.target.result)};c.readAsArrayBuffer(a)};JSM.GetStringBufferFromURL=function(a,b){var c=new XMLHttpRequest;c.open("GET",a,!0);c.responseType="text";c.onload=function(){var a=c.response;a&&b(a)};c.send(null)};
  JSM.GetStringBufferFromFile=function(a,b){var c=new FileReader;c.onloadend=function(a){a.target.readyState==FileReader.DONE&&b(a.target.result)};c.readAsText(a)};JSM.LoadMultipleBuffersInternal=function(a,b,c,d){if(b>=a.length)d(c);else{var e=a[b],f=null,f=e.isFile?e.isArrayBuffer?JSM.GetArrayBufferFromFile:JSM.GetStringBufferFromFile:e.isArrayBuffer?JSM.GetArrayBufferFromURL:JSM.GetStringBufferFromURL;f(e.originalObject,function(e){c.push(e);JSM.LoadMultipleBuffersInternal(a,b+1,c,d)})}};
  JSM.LoadMultipleBuffers=function(a,b){JSM.LoadMultipleBuffersInternal(a,0,[],function(a){b(a)})};
  JSM.Read3dsFile=function(a,b){function c(a,c){if(void 0!==b.onLog&&null!==b.onLog)b.onLog(a,c)}function d(a){for(var b="",c=0,d=0;64>d;){c=a.ReadCharacter();if(0===c)break;b+=String.fromCharCode(c);d+=1}return b}function e(a){var b=[],c;for(c=0;3>c;c++)b[c]=a.ReadFloat32();return b}function f(a,b,c){for(;a.GetPosition()<=b-6;){var d=a,e=c,f=d.ReadUnsignedInteger16(),d=d.ReadUnsignedInteger32();e(f,d)}}if(void 0===b||null===b)b={};(function(a,h){function l(a,b,c){var d=[0,0,0];b=a.GetPosition()+c-
  6;var e=!1;f(a,b,function(b,c){b==h.MAT_COLOR?e||(d[0]=a.ReadUnsignedCharacter()/255,d[1]=a.ReadUnsignedCharacter()/255,d[2]=a.ReadUnsignedCharacter()/255):b==h.MAT_LIN_COLOR?(d[0]=a.ReadUnsignedCharacter()/255,d[1]=a.ReadUnsignedCharacter()/255,d[2]=a.ReadUnsignedCharacter()/255,e=!0):b==h.MAT_COLOR_F?e||(d[0]=a.ReadFloat32(),d[1]=a.ReadFloat32(),d[2]=a.ReadFloat32()):b==h.MAT_LIN_COLOR_F?(d[0]=a.ReadFloat32(),d[1]=a.ReadFloat32(),d[2]=a.ReadFloat32(),e=!0):a.Skip(c-6)});return d}function k(a,b,
  c){var d=0;b=a.GetPosition()+c-6;f(a,b,function(b,c){b==h.PERCENTAGE?d=a.ReadUnsignedInteger16()/100:b==h.PERCENTAGE_F?d=a.ReadFloat32():a.Skip(c-6)});return d}function m(a,b,c,e){e.texture=null;e.offset=[0,0];e.scale=[1,1];e.rotation=0;b=a.GetPosition()+c-6;f(a,b,function(b,c){b==h.MAT_TEXMAP_NAME?e.texture=d(a):b==h.MAT_TEXMAP_UOFFSET?e.offset[0]=a.ReadFloat32():b==h.MAT_TEXMAP_VOFFSET?e.offset[1]=a.ReadFloat32():b==h.MAT_TEXMAP_USCALE?e.scale[0]=a.ReadFloat32():b==h.MAT_TEXMAP_VSCALE?e.scale[1]=
  a.ReadFloat32():b==h.MAT_TEXMAP_ROTATION?e.rotation=a.ReadFloat32():a.Skip(c-6)})}function n(a,e,g){c("Read material chunk ("+e.toString(16)+", "+g+")",2);var n={},p=a.GetPosition()+g-6;f(a,p,function(b,f){b==h.MAT_NAME?(c("Read material name chunk ("+e.toString(16)+", "+g+")",3),n.name=d(a)):b==h.MAT_AMBIENT?(c("Read material ambient chunk ("+e.toString(16)+", "+g+")",3),n.ambient=l(a,b,f)):b==h.MAT_DIFFUSE?(c("Read material diffuse chunk ("+e.toString(16)+", "+g+")",3),n.diffuse=l(a,b,f)):b==h.MAT_SPECULAR?
  (c("Read material specular chunk ("+e.toString(16)+", "+g+")",3),n.specular=l(a,b,f)):b==h.MAT_SHININESS?(c("Read material shininess chunk ("+e.toString(16)+", "+g+")",3),n.shininess=k(a,b,f)):b==h.MAT_SHININESS_STRENGTH?(c("Read material shininess strength chunk ("+e.toString(16)+", "+g+")",3),n.shininessStrength=k(a,b,f)):b==h.MAT_TRANSPARENCY?(c("Read material transparency chunk ("+e.toString(16)+", "+g+")",3),n.transparency=k(a,b,f)):b==h.MAT_TEXMAP?(c("Read material texture map chunk ("+e.toString(16)+
  ", "+g+")",3),m(a,b,f,n)):(c("Skip chunk ("+b.toString(16)+", "+f+")",3),a.Skip(f-6))});if(void 0!==b.onMaterial&&null!==b.onMaterial)b.onMaterial(n)}function p(a,e,g){c("Read faces chunk ("+e.toString(16)+", "+g+")",4);e=a.GetPosition()+g-6;var k=a.ReadUnsignedInteger16(),l,m,n,p;for(g=0;g<k;g++)if(l=a.ReadUnsignedInteger16(),m=a.ReadUnsignedInteger16(),n=a.ReadUnsignedInteger16(),p=a.ReadUnsignedInteger16(),void 0!==b.onFace&&null!==b.onFace)b.onFace(l,m,n,p);f(a,e,function(e,f){if(e==h.TRI_MATERIAL){c("Read face materials chunk ("+
  e.toString(16)+", "+f+")",5);var g=d(a),l=a.ReadUnsignedInteger16(),m,n;for(m=0;m<l;m++)if(n=a.ReadUnsignedInteger16(),void 0!==b.onFaceMaterial&&null!==b.onFaceMaterial)b.onFaceMaterial(n,g)}else if(e==h.TRI_SMOOTH){c("Read face smoothing groups chunk ("+e.toString(16)+", "+f+")",5);for(g=0;g<k;g++)if(l=a.ReadUnsignedInteger32(),void 0!==b.onFaceSmoothingGroup&&null!==b.onFaceSmoothingGroup)b.onFaceSmoothingGroup(g,l)}else c("Skip chunk ("+e.toString(16)+", "+f+")",5),a.Skip(f-6)})}function q(a,
  d,e,g){c("Read mesh chunk ("+d+", "+e.toString(16)+", "+g+")",3);if(void 0!==b.onMesh&&null!==b.onMesh)b.onMesh(d);d=a.GetPosition()+g-6;f(a,d,function(d,e){if(d==h.TRI_VERTEX){c("Read vertices chunk ("+d.toString(16)+", "+e+")",4);var f=a.ReadUnsignedInteger16(),g,k,l,m;for(g=0;g<f;g++)if(k=a.ReadFloat32(),l=a.ReadFloat32(),m=a.ReadFloat32(),void 0!==b.onVertex&&null!==b.onVertex)b.onVertex(k,l,m)}else if(d==h.TRI_TEXVERTEX){c("Read texture vertices chunk ("+d.toString(16)+", "+e+")",4);f=a.ReadUnsignedInteger16();
  for(g=0;g<f;g++)if(k=a.ReadFloat32(),l=a.ReadFloat32(),void 0!==b.onTextureVertex&&null!==b.onTextureVertex)b.onTextureVertex(k,l)}else if(d==h.TRI_FACE)p(a,d,e);else if(d==h.TRI_TRANSFORMATION){c("Read transformation chunk ("+d.toString(16)+", "+e+")",4);f=[];for(g=0;4>g;g++){for(k=0;3>k;k++)f.push(a.ReadFloat32());3>g?f.push(0):f.push(1)}if(void 0!==b.onTransformation&&null!==b.onTransformation)b.onTransformation(f)}else c("Skip chunk ("+d.toString(16)+", "+e+")",4),a.Skip(e-6)})}function r(a,b,
  e){c("Read object chunk ("+b.toString(16)+", "+e+")",2);b=a.GetPosition()+e-6;var g=d(a);f(a,b,function(b,d){b==h.OBJ_TRIMESH?q(a,g,b,d):(b==h.OBJ_LIGHT?c("Skip light chunk ("+g+", "+b.toString(16)+", "+d+")",3):b==h.OBJ_CAMERA?c("Skip camera chunk ("+g+", "+b.toString(16)+", "+d+")",3):c("Skip chunk ("+b.toString(16)+", "+d+")",3),a.Skip(d-6))})}function s(a,b,d){c("Read editor chunk ("+b.toString(16)+", "+d+")",1);b=a.GetPosition()+d-6;f(a,b,function(b,d){b==h.EDIT_MATERIAL?n(a,b,d):b==h.EDIT_OBJECT?
  r(a,b,d):(c("Skip chunk ("+b.toString(16)+", "+d+")",2),a.Skip(d-6))})}function v(a,g,k){function l(a,b){var c=[];a.Skip(10);var d,f,g,k=a.ReadInteger32();for(d=0;d<k;d++)a.ReadInteger32(),f=a.ReadUnsignedInteger16(),0!==f&&a.ReadFloat32(),b==h.OBJECT_ROTATION?(g=a.ReadFloat32(),f=e(a),f[3]=g):f=e(a),c.push(f);return c}c("Read object node chunk ("+g.toString(16)+", "+k+")",2);var m={name:"",nodeId:-1,flags:-1,userId:-1,pivot:[0,0,0],positions:[],rotations:[],scales:[]};g=a.GetPosition()+k-6;f(a,g,
  function(b,f){b==h.OBJECT_HIERARCHY?(m.name=d(a),m.flags=a.ReadUnsignedInteger32(),m.userId=a.ReadUnsignedInteger16()):b==h.OBJECT_PIVOT?m.pivot=e(a):b==h.OBJECT_POSITION?m.positions=l(a,h.OBJECT_POSITION):b==h.OBJECT_ROTATION?m.rotations=l(a,h.OBJECT_ROTATION):b==h.OBJECT_SCALE?m.scales=l(a,h.OBJECT_SCALE):b==h.OBJECT_ID?m.nodeId=a.ReadUnsignedInteger16():(c("Skip chunk ("+b.toString(16)+", "+f+")",3),a.Skip(f-6))});if(void 0!==b.onObjectNode&&null!==b.onObjectNode)b.onObjectNode(m)}function u(a,
  b,d){c("Read keyframe chunk ("+b.toString(16)+", "+d+")",1);b=a.GetPosition()+d-6;f(a,b,function(b,d){b==h.OBJECT_NODE?v(a,b,d):(c("Skip chunk ("+b.toString(16)+", "+d+")",2),a.Skip(d-6))})}function t(a,b,d){c("Read main chunk ("+b.toString(16)+", "+d+")",0);b=a.GetPosition()+d-6;f(a,b,function(b,d){b==h.EDIT3DS?s(a,b,d):b==h.KF3DS?u(a,b,d):(c("Skip chunk ("+b.toString(16)+", "+d+")",1),a.Skip(d-6))})}var w=a.GetByteLength();f(a,w,function(b,d){b==h.MAIN3DS?t(a,b,d):(c("Skip chunk ("+b.toString(16)+
  ", "+d+")",0),a.Skip(d-6))})})(new JSM.BinaryReader(a,!0),{MAIN3DS:19789,EDIT3DS:15677,EDIT_MATERIAL:45055,MAT_NAME:40960,MAT_AMBIENT:40976,MAT_DIFFUSE:40992,MAT_SPECULAR:41008,MAT_SHININESS:41024,MAT_SHININESS_STRENGTH:41025,MAT_TRANSPARENCY:41040,MAT_COLOR_F:16,MAT_COLOR:17,MAT_LIN_COLOR:18,MAT_LIN_COLOR_F:19,MAT_TEXMAP:41472,MAT_TEXMAP_NAME:41728,MAT_TEXMAP_UOFFSET:41816,MAT_TEXMAP_VOFFSET:41818,MAT_TEXMAP_USCALE:41812,MAT_TEXMAP_VSCALE:41814,MAT_TEXMAP_ROTATION:41820,PERCENTAGE:48,PERCENTAGE_F:49,
  EDIT_OBJECT:16384,OBJ_TRIMESH:16640,OBJ_LIGHT:17920,OBJ_CAMERA:18176,TRI_VERTEX:16656,TRI_TEXVERTEX:16704,TRI_FACE:16672,TRI_TRANSFORMATION:16736,TRI_MATERIAL:16688,TRI_SMOOTH:16720,KF3DS:45056,OBJECT_NODE:45058,OBJECT_HIERARCHY:45072,OBJECT_PIVOT:45075,OBJECT_POSITION:45088,OBJECT_ROTATION:45089,OBJECT_SCALE:45090,OBJECT_ID:45104})};
  JSM.Convert3dsToJsonData=function(a,b){function c(a){return void 0!==b.onFileRequested&&null!==b.onFileRequested?b.onFileRequested(a):null}if(void 0===b||null===b)b={};var d=new JSM.TriangleModel,e=null,f={},g={},h={nodes:[],nodeIdToIndex:{}};JSM.Read3dsFile(a,{onMaterial:function(a){if(void 0===f[a.name]){var b=d.AddMaterial({name:a.name,ambient:a.ambient,diffuse:a.diffuse,specular:a.specular,shininess:void 0===a.shininess||null===a.shininess||void 0===a.shininessStrength||null===a.shininessStrength?
  0:a.shininess*a.shininessStrength,opacity:void 0===a.transparency||null===a.transparency?1:1-a.transparency}),e=d.GetMaterial(b);if(void 0!==a.texture&&null!==a.texture){var g=c(a.texture);null!==g&&(g=new window.Blob([g]),g=window.URL.createObjectURL(g),e.texture=g,e.offset=a.offset,e.scale=a.scale,e.rotation=-a.rotation)}f[a.name]=b}},onMesh:function(a){if(void 0===g[a]){var b=d.AddBody(new JSM.TriangleBody(a));e=d.GetBody(b);e.meshData={faceToMaterial:{},faceToSmoothingGroup:{},objectNodes:[],
  transformation:null};g[a]=b}},onTransformation:function(a){null!==e&&(e.meshData.transformation=a)},onObjectNode:function(a){var b=h.nodes.length;h.nodes.push(a);h.nodeIdToIndex[a.nodeId]=b;a=g[a.name];void 0!==a&&d.GetBody(a).meshData.objectNodes.push(b)},onVertex:function(a,b,c){null!==e&&e.AddVertex(a,b,c)},onTextureVertex:function(a,b){null!==e&&e.AddUV(a,b)},onFace:function(a,b,c){null!==e&&e.AddTriangle(a,b,c)},onFaceMaterial:function(a,b){null!==e&&(e.meshData.faceToMaterial[a]=b)},onFaceSmoothingGroup:function(a,
  b){null!==e&&(e.meshData.faceToSmoothingGroup[a]=b)},onFileRequested:c});(function(a,b,c){function d(a,b,c){function e(a,b){var c=b[0],d=b[1],f=b[2],g;for(g=0;4>g;g++)a[0+g]*=c,a[4+g]*=d,a[8+g]*=f;return a}function f(a,b){var c=b[0],d=b[1],e=b[2],g;for(g=0;3>g;g++)a[12+g]+=a[0+g]*c+a[4+g]*d+a[8+g]*e;return a}function g(a,b){var c,d;for(c=0;c<a.VertexCount();c++)d=a.GetVertex(c),d=JSM.ApplyTransformation(b,d),a.SetVertex(c,d.x,d.y,d.z)}function h(a,b,c){var d=JSM.MatrixDeterminant(b);JSM.IsNegative(d)&&
  (b=JSM.MatrixClone(b),e(b,[-1,1,1]),c=JSM.MatrixMultiply(c,b),g(a,c))}function k(a,b){if(void 0!==a.matrix)return a.matrix;var c=JSM.MatrixIdentity(),d;d=0===a.positions.length?[0,0,0]:a.positions[0];c=f(c,d);if(0===a.rotations.length)d=[0,0,0,0];else{d=a.rotations[0];var g=[0,0,0,1],h=Math.sqrt(d[0]*d[0]+d[1]*d[1]+d[2]*d[2]);JSM.IsPositive(h)&&(g=-0.5*d[3],h=Math.sin(g)/h,g=[h*d[0],h*d[1],h*d[2],Math.cos(g)]);d=g}d=JSM.MatrixRotationQuaternion(d);c=JSM.MatrixMultiply(d,c);d=0===a.scales.length?[0,
  0,0,0]:a.scales[0];c=e(c,d);65535!=a.userId&&(d=b.nodeIdToIndex[a.userId],void 0!==d&&(d=k(b.nodes[d],b),c=JSM.MatrixMultiply(c,d)));return a.matrix=c}var l=a.meshData;l=void 0===l||null===l?null:l.transformation;if(null!==l){var m=null,m=null!==b?k(b,c):l;c=JSM.MatrixClone(m);m=JSM.MatrixClone(l);l=JSM.MatrixInvert(m);null!==l&&(h(a,m,l),b=void 0===b||null===b?[0,0,0]:b.pivot,f(c,[-b[0],-b[1],-b[2]]),b=JSM.MatrixMultiply(l,c),g(a,b))}}function e(a,b,c,f){d(a,b,f);b=a.UVCount()==a.VertexCount();f=
  a.meshData;var g,h,k;for(g=0;g<a.TriangleCount();g++)h=a.GetTriangle(g),b&&(h.u0=h.v0,h.u1=h.v1,h.u2=h.v2),k=f.faceToMaterial[g],void 0!==k&&(k=c[k],void 0!==k&&(h.mat=k)),k=f.faceToSmoothingGroup[g],void 0!==k&&0<k&&(h.curve=k)}function f(a,b,c,d){b=b.Clone();b.SetName(b.GetName()+" ("+d+")");c<a.BodyCount()?a.AddBodyToIndex(b,c):a.AddBody(b);return b}var g,h,v,u,t,w,A;for(g=0;g<b.BodyCount();g++)if(v=b.GetBody(g),u=v.meshData,0===u.objectNodes.length)e(v,null,c,a);else{w=a.nodes[u.objectNodes[0]];
  for(h=1;h<u.objectNodes.length;h++)t=a.nodes[u.objectNodes[h]],A=f(b,v,g+1,h+1),A.meshData=v.meshData,e(A,t,c,a),g+=1;e(v,w,c,a)}})(h,d,f);d.Finalize();return JSM.ConvertTriangleModelToJsonData(d)};
  JSM.ReadObjFile=function(a,b){function c(a,c,d,e){if(void 0!==b.onMaterialComponent&&null!==b.onMaterialComponent)b.onMaterialComponent(a,c,d,e)}function d(a,c,d){if(void 0!==b.onVertex&&null!==b.onVertex)b.onVertex(a,c,d)}function e(a,c,d){if(void 0!==b.onNormal&&null!==b.onNormal)b.onNormal(a,c,d)}function f(a,c){if(void 0!==b.onTexCoord&&null!==b.onTexCoord)b.onTexCoord(a,c)}function g(a){return void 0!==b.onFileRequested&&null!==b.onFileRequested?b.onFileRequested(a):null}function h(a,h){function n(a,
  b){return 0<a?a-1:b+a}function p(a,b){var c=a.indexOf(b)+b.length;return a.substr(c,a.length-c).trim()}if(0!==a.length&&"#"!=a[0]){var q=a.split(/\s+/);if(!(0===q.length||"#"==q[0][0])){var r;if("g"==q[0]){if(!(2>q.length)){var s="";for(r=1;r<q.length;r++)s+=q[r],r<q.length-1&&(s+=" ");if(void 0!==b.onMesh&&null!==b.onMesh)b.onMesh(s)}}else if("v"==q[0])4>q.length||(h.vertexCount+=1,d(parseFloat(q[1]),parseFloat(q[2]),parseFloat(q[3])));else if("vn"==q[0])4>q.length||(h.normalCount+=1,e(parseFloat(q[1]),
  parseFloat(q[2]),parseFloat(q[3])));else if("vt"==q[0])3>q.length||(h.uvCount+=1,f(parseFloat(q[1]),parseFloat(q[2])));else if("f"==q[0]){if(!(4>q.length)){var s=[],v=[],u=[],t;for(r=1;r<q.length;r++)t=q[r].split("/"),s.push(n(parseInt(t[0],10),h.vertexCount)),1<t.length&&0<t[1].length&&u.push(n(parseInt(t[1],10),h.uvCount)),2<t.length&&0<t[2].length&&v.push(n(parseInt(t[2],10),h.normalCount));if(void 0!==b.onFace&&null!==b.onFace)b.onFace(s,v,u)}}else if("usemtl"==q[0]){if(!(2>q.length)&&void 0!==
  b.onUseMaterial&&null!==b.onUseMaterial)b.onUseMaterial(q[1])}else if("newmtl"==q[0]){if(!(2>q.length)&&void 0!==b.onNewMaterial&&null!==b.onNewMaterial)b.onNewMaterial(q[1])}else if("Ka"==q[0]||"Kd"==q[0]||"Ks"==q[0])4>q.length||c(q[0],parseFloat(q[1]),parseFloat(q[2]),parseFloat(q[3]));else if("Ns"==q[0]||"Tr"==q[0]||"d"==q[0]){if(!(2>q.length)&&void 0!==b.onMaterialParameter&&null!==b.onMaterialParameter)b.onMaterialParameter(q[0],q[1])}else if("map_Kd"==q[0]){if(!(2>q.length)&&(q=p(a,"map_Kd"),
  void 0!==b.onMaterialTexture&&null!==b.onMaterialTexture))b.onMaterialTexture(q)}else"mtllib"==q[0]&&!(2>q.length)&&(q=p(a,"mtllib"),q=g(q.trim()),null!==q&&l(q))}}}function l(a,b){var c=a.split("\n"),d,e;for(d=0;d<c.length;d++)e=c[d].trim(),h(e,b)}if(void 0===b||null===b)b={};l(a,{vertexCount:0,normalCount:0,uvCount:0})};
  JSM.ConvertObjToJsonData=function(a,b){function c(a){return void 0!==b.onFileRequested&&null!==b.onFileRequested?b.onFileRequested(a):null}if(void 0===b||null===b)b={};var d=new JSM.TriangleModel,e=d.AddBody(new JSM.TriangleBody("Default")),f=d.GetBody(e),g={},h=null,l=null,k=[],m=[],n=[];JSM.ReadObjFile(a,{onNewMaterial:function(a){var b=d.AddMaterial({name:a});h=d.GetMaterial(b);g[a]=b},onMaterialComponent:function(a,b,c,d){null!==h&&("Ka"==a?h.ambient=[b,c,d]:"Kd"==a?h.diffuse=[b,c,d]:"Ks"==a&&
  (h.specular=[b,c,d]))},onMaterialParameter:function(a,b){if(null!==h)if("Ns"==a)h.shininess=0,JSM.IsPositive(b)&&(h.shininess=(Math.log2(parseFloat(b))-1)/10);else if("Tr"==a||"d"==a)h.opacity=parseFloat(b)},onMaterialTexture:function(a){null!==h&&(a=c(a),null!==a&&(a=new window.Blob([a]),a=window.URL.createObjectURL(a),h.texture=a))},onUseMaterial:function(a){a=g[a];void 0!==a&&(l=a)},onMesh:function(a){a=d.AddBody(new JSM.TriangleBody(a));f=d.GetBody(a)},onVertex:function(a,b,c){k.push(new JSM.Coord(a,
  b,c))},onNormal:function(a,b,c){m.push(new JSM.Coord(a,b,c))},onTexCoord:function(a,b){n.push(new JSM.Coord2D(a,b))},onFace:function(a,b,c){var d,e,g=b.length==a.length,h=c.length==a.length,k=a.length;for(d=0;d<k-2;d++)e=f.AddTriangle(a[0],a[(d+1)%k],a[(d+2)%k]),e=f.GetTriangle(e),g&&(e.n0=b[0],e.n1=b[(d+1)%k],e.n2=b[(d+2)%k]),h&&(e.u0=c[0],e.u1=c[(d+1)%k],e.u2=c[(d+2)%k]),null!==l&&(e.mat=l)},onFileRequested:c});(function(a,b,c,d){function e(a,b,c,d,f){if(void 0!==c){var g=d[c];if(void 0!==g)return g;
  if(!(0>c||c>=b.length))return b=b[c],0===f?g=a.AddVertex(b.x,b.y,b.z):1===f?g=a.AddNormal(b.x,b.y,b.z):2===f&&(g=a.AddUV(b.x,b.y)),d[c]=g}}var f,g,h,k,l,m,n;for(f=0;f<a.BodyCount();f++){h=a.GetBody(f);l={};m={};n={};for(g=0;g<h.TriangleCount();g++)k=h.GetTriangle(g),k.v0=e(h,b,k.v0,l,0),k.v1=e(h,b,k.v1,l,0),k.v2=e(h,b,k.v2,l,0),k.n0=e(h,c,k.n0,m,1),k.n1=e(h,c,k.n1,m,1),k.n2=e(h,c,k.n2,m,1),k.u0=e(h,d,k.u0,n,2),k.u1=e(h,d,k.u1,n,2),k.u2=e(h,d,k.u2,n,2)}})(d,k,m,n);d.Finalize();return JSM.ConvertTriangleModelToJsonData(d)};
  JSM.ReadBinaryStlFile=function(a,b){function c(a){var b=[],c;for(c=0;3>c;c++)b[c]=a.ReadFloat32();return b}if(void 0===b||null===b)b={};var d=new JSM.BinaryReader(a,!0);d.Skip(80);var e=d.ReadUnsignedInteger32(),f,g,h,l,k;for(f=0;f<e;f++)if(k=c(d),g=c(d),h=c(d),l=c(d),d.Skip(2),void 0!==b.onFace&&null!==b.onFace)b.onFace(g,h,l,k)};
  JSM.ReadAsciiStlFile=function(a,b){function c(a,c){var d=a[c].trim();if(0===d.length)return c+1;d=d.split(/\s+/);if(0===d.length)return c+1;if("solid"!=d[0]&&"facet"==d[0]&&"normal"==d[1]){if(5>d.length)return-1;var d=[parseFloat(d[2]),parseFloat(d[3]),parseFloat(d[4])],e=[],k,m;for(k=c+1;k<a.length&&3>e.length;k++)if(m=a[k].trim(),0!==m.length&&(m=m.split(/\s+/),0!==m.length&&"vertex"==m[0]))if(4>m.length)break;else m=[parseFloat(m[1]),parseFloat(m[2]),parseFloat(m[3])],e.push(m);k+=1;if(3!=e.length)return-1;
  if(void 0!==b.onFace&&null!==b.onFace)b.onFace(e[0],e[1],e[2],d);return k}return c+1}if(void 0===b||null===b)b={};for(var d=0,e=a.split("\n");d<e.length&&-1!=d;)d=c(e,d)};JSM.IsBinaryStlFile=function(a){var b=a.byteLength;if(84>b)return!1;a=new JSM.BinaryReader(a,!0);a.Skip(80);a=a.ReadUnsignedInteger32();return b!=50*a+84?!1:!0};
  JSM.ConvertStlToJsonData=function(a,b){var c=new JSM.TriangleModel,d=c.AddBody(new JSM.TriangleBody("Default")),e=c.GetBody(d);null!==a?JSM.ReadBinaryStlFile(a,{onFace:function(a,b,c,d){a=e.AddVertex(a[0],a[1],a[2]);b=e.AddVertex(b[0],b[1],b[2]);c=e.AddVertex(c[0],c[1],c[2]);d=(new JSM.Vector(d[0],d[1],d[2])).Normalize();d=e.AddNormal(d.x,d.y,d.z);e.AddTriangle(a,b,c,d,d,d)}}):null!==b&&JSM.ReadAsciiStlFile(b,{onFace:function(a,b,c,d){a=e.AddVertex(a[0],a[1],a[2]);b=e.AddVertex(b[0],b[1],b[2]);c=
  e.AddVertex(c[0],c[1],c[2]);d=(new JSM.Vector(d[0],d[1],d[2])).Normalize();d=e.AddNormal(d.x,d.y,d.z);e.AddTriangle(a,b,c,d,d,d)}});c.Finalize();return JSM.ConvertTriangleModelToJsonData(c)};JSM.ImportFileList=function(){this.isFile=this.descriptors=this.originalList=null};
  JSM.ImportFileList.prototype.InitFromFiles=function(a){this.originalList=a;this.descriptors=[];var b,c;for(b=0;b<a.length;b++)c=a[b],c={originalObject:c,originalFileName:c.name,fileName:c.name.toUpperCase(),extension:this.GetFileExtension(c.name)},this.descriptors.push(c);this.isFile=!0};
  JSM.ImportFileList.prototype.InitFromURLs=function(a){this.originalList=a;this.descriptors=[];var b,c,d;for(b=0;b<a.length;b++)c=a[b],d=this.GetFileName(c),c={originalObject:c,originalFileName:d,fileName:d.toUpperCase(),extension:this.GetFileExtension(d)},this.descriptors.push(c);this.isFile=!1};JSM.ImportFileList.prototype.GetOriginalList=function(){return this.originalList};
  JSM.ImportFileList.prototype.GetInputList=function(){var a=[],b,c,d;for(b=0;b<this.descriptors.length;b++){c=this.descriptors[b];d=c.originalObject;var e=this.IsFile();c=".OBJ"==c.extension||".MTL"==c.extension?!1:!0;d={originalObject:d,isFile:e,isArrayBuffer:c};a.push(d)}return a};JSM.ImportFileList.prototype.GetFileName=function(a){var b=a.split("/");1==b.length&&(b=a.split("\\"));return 0===b.length?"":b[b.length-1]};JSM.ImportFileList.prototype.GetFileDescriptor=function(a){return this.descriptors[a]};
  JSM.ImportFileList.prototype.IsFile=function(){return this.isFile};JSM.ImportFileList.prototype.GetMainFileIndex=function(){var a,b;for(a=0;a<this.descriptors.length;a++)if(b=this.descriptors[a],this.IsSupportedExtension(b.extension))return a;return-1};JSM.ImportFileList.prototype.GetFileIndexByName=function(a){var b,c,d;for(b=0;b<this.descriptors.length;b++)if(c=this.descriptors[b],d=this.GetFileName(a),c.fileName==d.toUpperCase())return b;return-1};
  JSM.ImportFileList.prototype.IsSupportedExtension=function(a){return".3DS"==a||".OBJ"==a||".STL"==a?!0:!1};JSM.ImportFileList.prototype.GetFileExtension=function(a){var b=a.lastIndexOf(".");if(-1==b)return"";a=a.substr(b);return a=a.toUpperCase()};
  JSM.ConvertImportFileListToJsonData=function(a,b){function c(){if(void 0!==b.onError&&null!==b.onError)b.onError()}function d(a,c){if(void 0!==b.onReady&&null!==b.onReady)b.onReady(a,c)}function e(a,b,c,d){var e=a.GetFileIndexByName(c);a=a.GetFileName(c);if(-1==e)return d.missing.push(a),null;-1==d.requested.indexOf(a)&&d.requested.push(a);return b[e]}var f=a.GetMainFileIndex();if(-1===f)c();else{var g=a.GetFileDescriptor(f),h={main:g.originalFileName,requested:[],missing:[]},l=a.GetInputList();try{".3DS"==
  g.extension?JSM.LoadMultipleBuffers(l,function(b){var c=JSM.Convert3dsToJsonData(b[f],{onFileRequested:function(c){return e(a,b,c,h)}});d(h,c)}):".OBJ"==g.extension?JSM.LoadMultipleBuffers(l,function(b){var c=JSM.ConvertObjToJsonData(b[f],{onFileRequested:function(c){return e(a,b,c,h)}});d(h,c)}):".STL"==g.extension&&JSM.LoadMultipleBuffers(l,function(a){a=a[f];if(JSM.IsBinaryStlFile(a))a=JSM.ConvertStlToJsonData(a,null),d(h,a);else{for(a=0;a<l.length;a++)l[a].isArrayBuffer=!1;JSM.LoadMultipleBuffers(l,
  function(a){a=JSM.ConvertStlToJsonData(null,a[f]);d(h,a)})}})}catch(k){c()}}};JSM.ConvertFileListToJsonData=function(a,b){var c=new JSM.ImportFileList;c.InitFromFiles(a);JSM.ConvertImportFileListToJsonData(c,b)};JSM.ConvertURLListToJsonData=function(a,b){var c=new JSM.ImportFileList;c.InitFromURLs(a);JSM.ConvertImportFileListToJsonData(c,b)};JSM.IsPowerOfTwo=function(a){return a&0===a-1};JSM.NextPowerOfTwo=function(a){if(JSM.IsPowerOfTwo(a))return a;for(var b=1;b<a;)b*=2;return b};
  JSM.ResizeImageToPowerOfTwoSides=function(a){if(JSM.IsPowerOfTwo(a.width)&&!JSM.IsPowerOfTwo(a.height))return a;var b=JSM.NextPowerOfTwo(a.width),c=JSM.NextPowerOfTwo(a.height),d=document.createElement("canvas");d.width=b;d.height=c;d=d.getContext("2d");d.drawImage(a,0,0,b,c);return d.getImageData(0,0,b,c)};
  JSM.WebGLInitContext=function(a){if(null===a||void 0===a.getContext)return null;var b=a.getContext("webgl")||a.getContext("experimental-webgl");if(null===b)return null;b.viewport(0,0,a.width,a.height);b.clearColor(1,1,1,1);return b};
  JSM.WebGLInitShaderProgram=function(a,b,c,d){function e(a,b,c,d){c=a.createShader(c);a.shaderSource(c,b);a.compileShader(c);return!a.getShaderParameter(c,a.COMPILE_STATUS)?(void 0!==d&&null!==d&&d(a.getShaderInfoLog(c)),null):c}b=function(a,b,c,d){b=e(a,b,a.FRAGMENT_SHADER,d);c=e(a,c,a.VERTEX_SHADER,d);if(null===b||null===c)return null;d=a.createProgram();a.attachShader(d,c);a.attachShader(d,b);a.linkProgram(d);return!a.getProgramParameter(d,a.LINK_STATUS)?null:d}(a,c,b,d);if(null===b)return null;
  a.useProgram(b);return b};JSM.WebGLGetFloatTextureBufferSize=function(a){return JSM.NextPowerOfTwo(Math.ceil(Math.sqrt(a.length/3)))};
  JSM.WebGLCreateFloatTextureBuffer=function(a,b,c){var d=null;if(null!==b){for(;b.length<3*c*c;)b.push(0);d=new Float32Array(b)}b=a.createTexture();a.bindTexture(a.TEXTURE_2D,b);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,a.NEAREST);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,a.NEAREST);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE);a.texImage2D(a.TEXTURE_2D,0,a.RGB,c,c,0,a.RGB,a.FLOAT,d);a.bindTexture(a.TEXTURE_2D,
  null);return b};JSM.RenderAmbientLight=function(a){this.color=JSM.HexColorToNormalizedRGBComponents(a)};JSM.RenderDirectionalLight=function(a,b,c){this.diffuse=JSM.HexColorToNormalizedRGBComponents(a);this.specular=JSM.HexColorToNormalizedRGBComponents(b);this.direction=c.Clone()};JSM.RenderMaterialFlags={Point:1,Line:2,Triangle:4,Textured:8,Transparent:16};
  JSM.RenderMaterial=function(a,b){this.type=a;this.ambient=[0,0.8,0];this.diffuse=[0,0.8,0];this.specular=[0,0,0];this.shininess=0;this.opacity=1;this.reflection=0;this.singleSided=!1;this.pointSize=0.1;this.texture=null;JSM.CopyObjectProperties(b,this,!0)};JSM.RenderMaterial.prototype.SetType=function(a){this.type=a};JSM.RenderMaterial.prototype.SetBuffers=function(a,b){this.textureBuffer=a;this.textureImage=b};
  JSM.RenderMesh=function(a){this.material=a;this.uvBuffer=this.normalBuffer=this.vertexBuffer=this.uvArray=this.normalArray=this.vertexArray=null};JSM.RenderMesh.prototype.SetMaterial=function(a){this.material=a};JSM.RenderMesh.prototype.GetMaterial=function(){return this.material};JSM.RenderMesh.prototype.SetVertexArray=function(a){this.vertexArray=new Float32Array(a)};JSM.RenderMesh.prototype.SetNormalArray=function(a){this.normalArray=new Float32Array(a)};
  JSM.RenderMesh.prototype.SetUVArray=function(a){this.uvArray=new Float32Array(a)};JSM.RenderMesh.prototype.HasVertexArray=function(){return null!==this.vertexArray};JSM.RenderMesh.prototype.HasNormalArray=function(){return null!==this.normalArray};JSM.RenderMesh.prototype.HasUVArray=function(){return null!==this.uvArray};JSM.RenderMesh.prototype.GetVertexArray=function(){return this.vertexArray};JSM.RenderMesh.prototype.GetNormalArray=function(){return this.normalArray};
  JSM.RenderMesh.prototype.GetUVArray=function(){return this.uvArray};JSM.RenderMesh.prototype.SetBuffers=function(a,b,c){this.vertexBuffer=a;this.normalBuffer=b;this.uvBuffer=c};JSM.RenderMesh.prototype.GetVertexBuffer=function(){return this.vertexBuffer};JSM.RenderMesh.prototype.GetNormalBuffer=function(){return this.normalBuffer};JSM.RenderMesh.prototype.GetUVBuffer=function(){return this.uvBuffer};JSM.RenderMesh.prototype.VertexCount=function(){return parseInt(this.vertexArray.length/3,10)};
  JSM.RenderMesh.prototype.NormalCount=function(){return parseInt(this.normalArray.length/3,10)};JSM.RenderMesh.prototype.UVCount=function(){return parseInt(this.uvArray.length/2,10)};JSM.RenderMesh.prototype.GetVertex=function(a){return new JSM.Coord(this.vertexArray[3*a],this.vertexArray[3*a+1],this.vertexArray[3*a+2])};JSM.RenderMesh.prototype.GetTransformedVertex=function(a,b){var c=this.GetVertex(a);return b.Apply(c)};
  JSM.RenderBody=function(){this.transformation=new JSM.Transformation;this.meshes={}};JSM.RenderBody.prototype.AddMesh=function(a){void 0===this.meshes[a.material.type]&&(this.meshes[a.material.type]=[]);this.meshes[a.material.type].push(a)};JSM.RenderBody.prototype.EnumerateMeshes=function(a){for(var b in this.meshes)this.meshes.hasOwnProperty(b)&&this.EnumerateTypedMeshes(b,a)};JSM.RenderBody.prototype.HasTypedMeshes=function(a){return void 0!==this.meshes[a]};
  JSM.RenderBody.prototype.EnumerateTypedMeshes=function(a,b){if(this.HasTypedMeshes(a)){var c=this.meshes[a],d,e;for(d=0;d<c.length;d++)e=c[d],b(e)}};JSM.RenderBody.prototype.EnumerateMeshesWithFlag=function(a,b){for(var c in this.meshes)this.meshes.hasOwnProperty(c)&&c&a&&this.EnumerateTypedMeshes(c,b)};JSM.RenderBody.prototype.GetTransformation=function(){return this.transformation};JSM.RenderBody.prototype.GetTransformationMatrix=function(){return this.transformation.matrix};
  JSM.RenderBody.prototype.SetTransformation=function(a){this.transformation=a};JSM.ShaderType={Point:0,Line:1,Triangle:2,TexturedTriangle:3};JSM.ShaderProgram=function(a){this.context=a;this.cullEnabled=this.currentType=this.currentShader=this.shaders=this.globalParams=null};JSM.ShaderProgram.prototype.Init=function(){return!this.InitGlobalParams()||!this.InitShaders()?!1:!0};JSM.ShaderProgram.prototype.GetMaxLightCount=function(){return this.globalParams.maxLightCount};
  JSM.ShaderProgram.prototype.InitGlobalParams=function(){this.globalParams={noDirectionalLight:new JSM.RenderDirectionalLight(0,0,new JSM.Vector(0,0,0)),maxLightCount:4};return!0};
  JSM.ShaderProgram.prototype.InitShaders=function(){function a(a,b){var c=null;if(a==JSM.ShaderType.Point||a==JSM.ShaderType.Line)c=["#define MAX_LIGHTS "+b.maxLightCount,"struct Light\n{\n\tmediump vec3 diffuseColor;\n};\nstruct Material\n{\n\tmediump vec3 ambientColor;\n\tmediump vec3 diffuseColor;\n};\nuniform mediump vec3 uAmbientLightColor;\nuniform Light uLights[MAX_LIGHTS];\nuniform Material uMaterial;\nvoid main (void) {\n\tmediump vec3 ambientComponent = uMaterial.ambientColor * uAmbientLightColor;\n\tmediump vec3 diffuseComponent;\n\tfor (int i = 0; i < MAX_LIGHTS; i++) {\n\t\tdiffuseComponent += uMaterial.diffuseColor * uLights[i].diffuseColor;\n\t}\n\tgl_FragColor = vec4 (ambientComponent + diffuseComponent, 1.0);\n}"].join("\n");else if(a==
  JSM.ShaderType.Triangle||a==JSM.ShaderType.TexturedTriangle)c=["#define "+(a==JSM.ShaderType.Triangle?"NOTEXTURE":"USETEXTURE"),"#define MAX_LIGHTS "+b.maxLightCount,"struct Light\n{\n\tmediump vec3 diffuseColor;\n\tmediump vec3 specularColor;\n\tmediump vec3 direction;\n};\nstruct Material\n{\n\tmediump vec3 ambientColor;\n\tmediump vec3 diffuseColor;\n\tmediump vec3 specularColor;\n\tmediump float shininess;\n\tmediump float opacity;\n};\nuniform mediump vec3 uAmbientLightColor;\nuniform Light uLights[MAX_LIGHTS];\nuniform Material uMaterial;\nvarying mediump vec3 vVertex;\nvarying mediump vec3 vNormal;\n#ifdef USETEXTURE\nvarying mediump vec2 vUV;\nuniform sampler2D uSampler;\n#endif\nvoid main (void) {\n\tmediump vec3 N = normalize (vNormal);\n\tif (!gl_FrontFacing) {\n\t\tN = -N;\n\t}\n\tmediump vec3 ambientComponent = uMaterial.ambientColor * uAmbientLightColor;\n\tmediump vec3 diffuseComponent;\n\tmediump vec3 specularComponent;\n\tmediump vec3 E = normalize (-vVertex);\n\tfor (int i = 0; i < MAX_LIGHTS; i++) {\n\t\tmediump vec3 L = normalize (-uLights[i].direction);\n\t\tmediump vec3 R = normalize (-reflect (L, N));\n\t\tdiffuseComponent += uMaterial.diffuseColor * uLights[i].diffuseColor * max (dot (N, L), 0.0);\n\t\tspecularComponent += uMaterial.specularColor * uLights[i].specularColor * pow (max (dot (R, E), 0.0), uMaterial.shininess);\n\t}\n#ifdef USETEXTURE\n\tmediump vec3 textureColor = texture2D (uSampler, vec2 (vUV.s, vUV.t)).xyz;\n\tambientComponent = ambientComponent * textureColor;\n\tdiffuseComponent = diffuseComponent * textureColor;\n\tspecularComponent = specularComponent * textureColor;\n#endif\n\tambientComponent = clamp (ambientComponent, 0.0, 1.0);\n\tdiffuseComponent = clamp (diffuseComponent, 0.0, 1.0);\n\tspecularComponent = clamp (specularComponent, 0.0, 1.0);\n\tgl_FragColor = vec4 (ambientComponent + diffuseComponent + specularComponent, uMaterial.opacity);\n}"].join("\n");
  return c}function b(a){var b=null;if(a==JSM.ShaderType.Triangle||a==JSM.ShaderType.TexturedTriangle)b=["#define "+(a==JSM.ShaderType.Triangle?"NOTEXTURE":"USETEXTURE"),"attribute mediump vec3 aVertexPosition;\nattribute mediump vec3 aVertexNormal;\nuniform mediump mat4 uViewMatrix;\nuniform mediump mat4 uProjectionMatrix;\nuniform mediump mat4 uTransformationMatrix;\nvarying mediump vec3 vVertex;\nvarying mediump vec3 vNormal;\n#ifdef USETEXTURE\nattribute mediump vec2 aVertexUV;\nvarying mediump vec2 vUV;\n#endif\nvoid main (void) {\n\tmat4 modelViewMatrix = uViewMatrix * uTransformationMatrix;\n\tvVertex = vec3 (modelViewMatrix * vec4 (aVertexPosition, 1.0));\n\tvNormal = normalize (vec3 (modelViewMatrix * vec4 (aVertexNormal, 0.0)));\n#ifdef USETEXTURE\n\tvUV = aVertexUV;\n#endif\n\tgl_Position = uProjectionMatrix * vec4 (vVertex, 1.0);\n}"].join("\n");
  else if(a==JSM.ShaderType.Point||a==JSM.ShaderType.Line)b=["#define "+(a==JSM.ShaderType.Point?"POINT":"LINE"),"attribute mediump vec3 aVertexPosition;\nuniform mediump mat4 uViewMatrix;\nuniform mediump mat4 uProjectionMatrix;\nuniform mediump mat4 uTransformationMatrix;\n#ifdef POINT\nuniform mediump float uPointSize;\n#endif\nvarying mediump vec3 vVertex;\nvoid main (void) {\n\tmat4 modelViewMatrix = uViewMatrix * uTransformationMatrix;\n\tvVertex = vec3 (modelViewMatrix * vec4 (aVertexPosition, 1.0));\n#ifdef POINT\n\tconst mediump float scale = 200.0;\n\tgl_PointSize = uPointSize * (scale / length (vVertex));\n#endif\n\tgl_Position = uProjectionMatrix * vec4 (vVertex, 1.0);\n}"].join("\n");
  return b}function c(a,b,c,d){if(d==JSM.ShaderType.Triangle||d==JSM.ShaderType.TexturedTriangle){b.vertexPositionAttribute=a.getAttribLocation(b,"aVertexPosition");b.vertexNormalAttribute=a.getAttribLocation(b,"aVertexNormal");b.ambientLightColorUniform=a.getUniformLocation(b,"uAmbientLightColor");b.lightUniforms=[];var l;for(l=0;l<c.maxLightCount;l++)b.lightUniforms.push({diffuseColor:a.getUniformLocation(b,"uLights["+l+"].diffuseColor"),specularColor:a.getUniformLocation(b,"uLights["+l+"].specularColor"),
  direction:a.getUniformLocation(b,"uLights["+l+"].direction")});b.materialUniforms={ambientColor:a.getUniformLocation(b,"uMaterial.ambientColor"),diffuseColor:a.getUniformLocation(b,"uMaterial.diffuseColor"),specularColor:a.getUniformLocation(b,"uMaterial.specularColor"),shininess:a.getUniformLocation(b,"uMaterial.shininess"),opacity:a.getUniformLocation(b,"uMaterial.opacity")};b.vMatrixUniform=a.getUniformLocation(b,"uViewMatrix");b.pMatrixUniform=a.getUniformLocation(b,"uProjectionMatrix");b.tMatrixUniform=
  a.getUniformLocation(b,"uTransformationMatrix");d==JSM.ShaderType.TexturedTriangle&&(b.vertexUVAttribute=a.getAttribLocation(b,"aVertexUV"),b.samplerUniform=a.getUniformLocation(b,"uSampler"))}else if(d==JSM.ShaderType.Point||d==JSM.ShaderType.Line){b.vertexPositionAttribute=a.getAttribLocation(b,"aVertexPosition");b.ambientLightColorUniform=a.getUniformLocation(b,"uAmbientLightColor");b.lightUniforms=[];for(l=0;l<c.maxLightCount;l++)b.lightUniforms.push({diffuseColor:a.getUniformLocation(b,"uLights["+
  l+"].diffuseColor")});b.materialUniforms={ambientColor:a.getUniformLocation(b,"uMaterial.ambientColor"),diffuseColor:a.getUniformLocation(b,"uMaterial.diffuseColor")};b.vMatrixUniform=a.getUniformLocation(b,"uViewMatrix");b.pMatrixUniform=a.getUniformLocation(b,"uProjectionMatrix");b.tMatrixUniform=a.getUniformLocation(b,"uTransformationMatrix");d==JSM.ShaderType.Point&&(b.pointSizeUniform=a.getUniformLocation(b,"uPointSize"))}}function d(d,f,g,h){var l=b(h),k=a(h,g);if(null===l||null===k)return!1;
  l=JSM.WebGLInitShaderProgram(d,l,k,function(a){JSM.Message(a)});if(null===l)return!1;d.useProgram(l);c(d,l,g,h);f[h]=l;return!0}this.shaders={};if(!d(this.context,this.shaders,this.globalParams,JSM.ShaderType.Point)||!d(this.context,this.shaders,this.globalParams,JSM.ShaderType.Line)||!d(this.context,this.shaders,this.globalParams,JSM.ShaderType.Triangle)||!d(this.context,this.shaders,this.globalParams,JSM.ShaderType.TexturedTriangle))return!1;this.context.enable(this.context.DEPTH_TEST);this.context.depthFunc(this.context.LEQUAL);
  this.context.enable(this.context.BLEND);this.context.blendEquation(this.context.FUNC_ADD);this.context.blendFunc(this.context.SRC_ALPHA,this.context.ONE_MINUS_SRC_ALPHA);this.context.disable(this.context.CULL_FACE);this.cullEnabled=!1;return!0};
  JSM.ShaderProgram.prototype.CompileMaterial=function(a,b){if(null!==a.texture){var c=this.context,d=c.createTexture(),e=new Image;e.src=a.texture;e.onload=function(){var a=JSM.ResizeImageToPowerOfTwoSides(e);c.bindTexture(c.TEXTURE_2D,d);c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MAG_FILTER,c.LINEAR);c.texParameteri(c.TEXTURE_2D,c.TEXTURE_MIN_FILTER,c.LINEAR_MIPMAP_LINEAR);c.texImage2D(c.TEXTURE_2D,0,c.RGBA,c.RGBA,c.UNSIGNED_BYTE,a);c.generateMipmap(c.TEXTURE_2D);c.bindTexture(c.TEXTURE_2D,null);void 0!==
  b&&null!==b&&b()};a.SetBuffers(d,e)}};
  JSM.ShaderProgram.prototype.CompileMesh=function(a){var b=this.context,c=b.createBuffer();b.bindBuffer(b.ARRAY_BUFFER,c);b.bufferData(b.ARRAY_BUFFER,a.GetVertexArray(),b.STATIC_DRAW);c.itemSize=3;c.numItems=a.VertexCount();var d=null;a.HasNormalArray()&&(d=b.createBuffer(),b.bindBuffer(b.ARRAY_BUFFER,d),b.bufferData(b.ARRAY_BUFFER,a.GetNormalArray(),b.STATIC_DRAW),d.itemSize=3,d.numItems=a.NormalCount());var e=null;a.HasUVArray()&&(e=b.createBuffer(),b.bindBuffer(b.ARRAY_BUFFER,e),b.bufferData(b.ARRAY_BUFFER,
  a.GetUVArray(),b.STATIC_DRAW),e.itemSize=2,e.numItems=a.UVCount());a.SetBuffers(c,d,e)};JSM.ShaderProgram.prototype.GetShader=function(a){return this.shaders[a]};JSM.ShaderProgram.prototype.UseShader=function(a){this.currentShader=this.GetShader(a);this.currentType=a;this.context.useProgram(this.currentShader)};
  JSM.ShaderProgram.prototype.SetParameters=function(a,b,c,d){var e=this.context,f=this.currentShader,g,h;if(this.currentType==JSM.ShaderType.Triangle||this.currentType==JSM.ShaderType.TexturedTriangle){e.uniform3f(f.ambientLightColorUniform,a.color[0],a.color[1],a.color[2]);for(a=0;a<this.globalParams.maxLightCount;a++)g=a<b.length?b[a]:this.globalParams.noDirectionalLight,h=JSM.ApplyRotation(c,g.direction),e.uniform3f(f.lightUniforms[a].diffuseColor,g.diffuse[0],g.diffuse[1],g.diffuse[2]),e.uniform3f(f.lightUniforms[a].specularColor,
  g.specular[0],g.specular[1],g.specular[2]),e.uniform3f(f.lightUniforms[a].direction,h.x,h.y,h.z);e.uniformMatrix4fv(f.pMatrixUniform,!1,d);e.uniformMatrix4fv(f.vMatrixUniform,!1,c)}else if(this.currentType==JSM.ShaderType.Point||this.currentType==JSM.ShaderType.Line){e.uniform3f(f.ambientLightColorUniform,a.color[0],a.color[1],a.color[2]);for(a=0;a<this.globalParams.maxLightCount;a++)g=a<b.length?b[a]:this.globalParams.noDirectionalLight,e.uniform3f(f.lightUniforms[a].diffuseColor,g.diffuse[0],g.diffuse[1],
  g.diffuse[2]);e.uniformMatrix4fv(f.pMatrixUniform,!1,d);e.uniformMatrix4fv(f.vMatrixUniform,!1,c)}};JSM.ShaderProgram.prototype.SetCullEnabled=function(a){a&&!this.cullEnabled?(this.context.enable(this.context.CULL_FACE),this.cullEnabled=!0):!a&&this.cullEnabled&&(this.context.disable(this.context.CULL_FACE),this.cullEnabled=!1)};
  JSM.ShaderProgram.prototype.DrawArrays=function(a,b,c,d,e){var f=this.context,g=this.currentShader;this.SetCullEnabled(a.singleSided);if(this.currentType==JSM.ShaderType.Triangle||this.currentType==JSM.ShaderType.TexturedTriangle)f.uniform3f(g.materialUniforms.ambientColor,a.ambient[0],a.ambient[1],a.ambient[2]),f.uniform3f(g.materialUniforms.diffuseColor,a.diffuse[0],a.diffuse[1],a.diffuse[2]),f.uniform3f(g.materialUniforms.specularColor,a.specular[0],a.specular[1],a.specular[2]),f.uniform1f(g.materialUniforms.shininess,
  a.shininess),f.uniform1f(g.materialUniforms.opacity,a.opacity),f.uniformMatrix4fv(g.tMatrixUniform,!1,b),f.bindBuffer(f.ARRAY_BUFFER,c),f.enableVertexAttribArray(g.vertexPositionAttribute),f.vertexAttribPointer(g.vertexPositionAttribute,c.itemSize,f.FLOAT,!1,0,0),f.bindBuffer(f.ARRAY_BUFFER,d),f.enableVertexAttribArray(g.vertexNormalAttribute),f.vertexAttribPointer(g.vertexNormalAttribute,d.itemSize,f.FLOAT,!1,0,0),this.currentType==JSM.ShaderType.TexturedTriangle&&(f.activeTexture(f.TEXTURE0),f.bindTexture(f.TEXTURE_2D,
  a.textureBuffer),f.bindBuffer(f.ARRAY_BUFFER,e),f.enableVertexAttribArray(g.vertexUVAttribute),f.vertexAttribPointer(g.vertexUVAttribute,e.itemSize,f.FLOAT,!1,0,0),f.uniform1i(g.samplerUniform,0)),f.drawArrays(f.TRIANGLES,0,c.numItems);else if(this.currentType==JSM.ShaderType.Point||this.currentType==JSM.ShaderType.Line)f.uniform3f(g.materialUniforms.ambientColor,a.ambient[0],a.ambient[1],a.ambient[2]),f.uniform3f(g.materialUniforms.diffuseColor,a.diffuse[0],a.diffuse[1],a.diffuse[2]),f.uniformMatrix4fv(g.tMatrixUniform,
  !1,b),f.bindBuffer(f.ARRAY_BUFFER,c),f.enableVertexAttribArray(g.vertexPositionAttribute),f.vertexAttribPointer(g.vertexPositionAttribute,c.itemSize,f.FLOAT,!1,0,0),this.currentType==JSM.ShaderType.Point?(f.uniform1f(g.pointSizeUniform,a.pointSize),f.drawArrays(f.POINTS,0,c.numItems)):this.currentType==JSM.ShaderType.Line&&f.drawArrays(f.LINES,0,c.numItems)};JSM.Renderer=function(){this.bodies=this.directionalLights=this.ambientLight=this.shader=this.context=this.canvas=null};
  JSM.Renderer.prototype.Init=function(a){return!JSM.IsWebGLEnabled()||!this.InitContext(a)||!this.InitView()||!this.InitShaders()||!this.InitLights()||!this.InitBodies()?!1:!0};JSM.Renderer.prototype.InitContext=function(a){this.canvas=a;if(null===this.canvas||void 0===this.canvas.getContext)return!1;this.context=this.canvas.getContext("webgl")||this.canvas.getContext("experimental-webgl");if(null===this.context)return!1;this.context=JSM.WebGLInitContext(a);return null===this.context?!1:!0};
  JSM.Renderer.prototype.InitShaders=function(){this.shader=new JSM.ShaderProgram(this.context);return this.shader.Init()};JSM.Renderer.prototype.InitLights=function(){this.ambientLight=new JSM.RenderAmbientLight(0);this.directionalLights=[];return!0};JSM.Renderer.prototype.InitBodies=function(){this.bodies=[];return!0};JSM.Renderer.prototype.InitView=function(){this.directionalLights=[];return!0};JSM.Renderer.prototype.SetClearColor=function(a,b,c){this.context.clearColor(a,b,c,1)};
  JSM.Renderer.prototype.SetAmbientLight=function(a){this.ambientLight=a};JSM.Renderer.prototype.AddLight=function(a){var b=this.shader.GetMaxLightCount();if(this.directionalLights.length>=b)return-1;this.directionalLights.push(a);return this.directionalLights.length-1};JSM.Renderer.prototype.RemoveLight=function(a){a=this.directionalLights.indexOf(a);-1!=a&&this.directionalLights.splice(a,1)};JSM.Renderer.prototype.RemoveLights=function(){this.directionalLights=[]};
  JSM.Renderer.prototype.GetLight=function(a){return this.directionalLights[a]};JSM.Renderer.prototype.AddBody=function(a,b){var c=this.shader;a.EnumerateMeshes(function(a){c.CompileMaterial(a.GetMaterial(),b);c.CompileMesh(a)});this.bodies.push(a)};JSM.Renderer.prototype.AddBodies=function(a,b){var c,d;for(c=0;c<a.length;c++)d=a[c],this.AddBody(d,b)};JSM.Renderer.prototype.EnumerateBodies=function(a){var b;for(b=0;b<this.bodies.length;b++)a(this.bodies[b])};
  JSM.Renderer.prototype.RemoveBody=function(a){a=this.bodies.indexOf(a);-1!=a&&this.bodies.splice(a,1)};JSM.Renderer.prototype.RemoveBodies=function(){this.bodies=[]};JSM.Renderer.prototype.GetBody=function(a){return this.bodies[a]};JSM.Renderer.prototype.Resize=function(){this.context.viewport(0,0,this.canvas.width,this.canvas.height)};
  JSM.Renderer.prototype.FindObjects=function(a,b,c){b=new JSM.Coord(b,this.canvas.height-c,0.5);b=JSM.Unproject(b,a.eye,a.center,a.up,a.fieldOfView*JSM.DegRad,this.canvas.width/this.canvas.height,a.nearClippingPlane,a.farClippingPlane,[0,0,this.canvas.width,this.canvas.height]);var d=new JSM.Ray(a.eye,JSM.CoordSub(b,a.eye),null),e=[];this.EnumerateBodies(function(a){var b=a.GetTransformation();a.EnumerateMeshesWithFlag(JSM.RenderMaterialFlags.Triangle,function(c){var l=c.VertexCount(),k,m,n,p;for(k=
  0;k<l;k+=3)m=c.GetTransformedVertex(k+0,b),n=c.GetTransformedVertex(k+1,b),p=c.GetTransformedVertex(k+2,b),m=JSM.RayTriangleIntersection(d,m,n,p),null!==m&&e.push({renderBody:a,renderMesh:c,triangleIndex:parseInt(k/3,10),intersection:m})})});e.sort(function(a,b){return a.intersection.distance-b.intersection.distance});return e};
  JSM.Renderer.prototype.Render=function(a){function b(a,b,c,g){function h(a){if(a&JSM.RenderMaterialFlags.Triangle){if(a&JSM.RenderMaterialFlags.Textured)return JSM.ShaderType.TexturedTriangle;if(!(a&JSM.RenderMaterialFlags.Textured))return JSM.ShaderType.Triangle}else{if(a&JSM.RenderMaterialFlags.Line)return JSM.ShaderType.Line;if(a&JSM.RenderMaterialFlags.Point)return JSM.ShaderType.Point}return null}var l=null;a.EnumerateBodies(function(k){if(k.HasTypedMeshes(b)){var m=k.GetTransformationMatrix();
  k.EnumerateTypedMeshes(b,function(k){null===l&&(l=h(b),a.shader.UseShader(l),a.shader.SetParameters(a.ambientLight,a.directionalLights,c,g));var p=k.GetMaterial(),q=k.GetVertexBuffer(),r=k.GetNormalBuffer();k=k.GetUVBuffer();a.shader.DrawArrays(p,m,q,r,k)})}})}this.context.clear(this.context.COLOR_BUFFER_BIT|this.context.DEPTH_BUFFER_BIT);var c=JSM.MatrixView(a.eye,a.center,a.up);a=JSM.MatrixPerspective(a.fieldOfView*JSM.DegRad,this.canvas.width/this.canvas.height,a.nearClippingPlane,a.farClippingPlane);
  b(this,JSM.RenderMaterialFlags.Triangle,c,a);b(this,JSM.RenderMaterialFlags.Triangle+JSM.RenderMaterialFlags.Textured,c,a);b(this,JSM.RenderMaterialFlags.Line,c,a);b(this,JSM.RenderMaterialFlags.Point,c,a);b(this,JSM.RenderMaterialFlags.Triangle+JSM.RenderMaterialFlags.Transparent,c,a);b(this,JSM.RenderMaterialFlags.Triangle+JSM.RenderMaterialFlags.Transparent+JSM.RenderMaterialFlags.Textured,c,a)};
  JSM.PointCloudRenderer=function(){this.pointSize=this.points=this.camera=this.shader=this.context=this.canvas=null};JSM.PointCloudRenderer.prototype.Init=function(a,b){return!JSM.IsWebGLEnabled()||!this.InitContext(a)||!this.InitShaders()||!this.InitBuffers()||!this.InitView(b)?!1:!0};
  JSM.PointCloudRenderer.prototype.InitContext=function(a){this.canvas=a;if(null===this.canvas||void 0===this.canvas.getContext)return!1;this.context=this.canvas.getContext("experimental-webgl");if(null===this.context)return!1;this.context.clearColor(1,1,1,1);this.context.enable(this.context.DEPTH_TEST);return!0};
  JSM.PointCloudRenderer.prototype.InitShaders=function(){function a(a,b,c){c=a.createShader(c);a.shaderSource(c,b);a.compileShader(c);return!a.getShaderParameter(c,a.COMPILE_STATUS)?null:c}var b=a(this.context,"varying highp vec3 vColor;\nvoid main (void) {\n\tgl_FragColor = vec4 (vColor, 1.0);\n}",this.context.FRAGMENT_SHADER),c=a(this.context,"attribute highp vec3 aVertexPosition;\nattribute highp vec3 aVertexColor;\nuniform highp mat4 uViewMatrix;\nuniform highp mat4 uProjectionMatrix;\nuniform highp float uPointSize;\nvarying highp vec3 vColor;\nvoid main (void) {\n\tvColor = aVertexColor;\n\tgl_PointSize = uPointSize;\n\tgl_Position = uProjectionMatrix * uViewMatrix * vec4 (aVertexPosition, 1.0);\n}",
  this.context.VERTEX_SHADER);if(null===b||null===c)return!1;this.shader=this.context.createProgram();this.context.attachShader(this.shader,c);this.context.attachShader(this.shader,b);this.context.linkProgram(this.shader);if(!this.context.getProgramParameter(this.shader,this.context.LINK_STATUS))return!1;this.context.useProgram(this.shader);this.shader.vertexPositionAttribute=this.context.getAttribLocation(this.shader,"aVertexPosition");this.context.enableVertexAttribArray(this.shader.vertexPositionAttribute);
  this.shader.vertexColorAttribute=this.context.getAttribLocation(this.shader,"aVertexColor");this.context.enableVertexAttribArray(this.shader.vertexColorAttribute);this.shader.pMatrixUniform=this.context.getUniformLocation(this.shader,"uProjectionMatrix");this.shader.vMatrixUniform=this.context.getUniformLocation(this.shader,"uViewMatrix");this.shader.pointSizeUniform=this.context.getUniformLocation(this.shader,"uPointSize");return!0};
  JSM.PointCloudRenderer.prototype.InitBuffers=function(){this.points=[];this.pointSize=1;return!0};JSM.PointCloudRenderer.prototype.InitView=function(a){this.camera=JSM.ValueOrDefault(a,new JSM.Camera);return!this.camera?!1:!0};JSM.PointCloudRenderer.prototype.SetClearColor=function(a,b,c){this.context.clearColor(a,b,c,1)};JSM.PointCloudRenderer.prototype.SetPointSize=function(a){this.pointSize=a};
  JSM.PointCloudRenderer.prototype.AddPoints=function(a,b){var c=this.context.createBuffer(),d=new Float32Array(a);this.context.bindBuffer(this.context.ARRAY_BUFFER,c);this.context.bufferData(this.context.ARRAY_BUFFER,d,this.context.STATIC_DRAW);c.itemSize=3;c.numItems=parseInt(d.length/3,10);var e=this.context.createBuffer(),f=new Float32Array(b);this.context.bindBuffer(this.context.ARRAY_BUFFER,e);this.context.bufferData(this.context.ARRAY_BUFFER,f,this.context.STATIC_DRAW);e.itemSize=3;e.numItems=
  parseInt(f.length/3,10);this.points.push({pointArray:d,pointBuffer:c,colorBuffer:e})};JSM.PointCloudRenderer.prototype.RemovePoints=function(){this.points=[]};JSM.PointCloudRenderer.prototype.Resize=function(){this.context.viewport(0,0,this.canvas.width,this.canvas.height)};
  JSM.PointCloudRenderer.prototype.Render=function(){this.context.clear(this.context.COLOR_BUFFER_BIT|this.context.DEPTH_BUFFER_BIT);var a=JSM.MatrixPerspective(this.camera.fieldOfView*JSM.DegRad,this.canvas.width/this.canvas.height,this.camera.nearClippingPlane,this.camera.farClippingPlane);this.context.uniformMatrix4fv(this.shader.pMatrixUniform,!1,a);a=JSM.MatrixView(this.camera.eye,this.camera.center,this.camera.up);this.context.uniformMatrix4fv(this.shader.vMatrixUniform,!1,a);this.context.uniform1f(this.shader.pointSizeUniform,
  this.pointSize);for(var b,c,a=0;a<this.points.length;a++)b=this.points[a].pointBuffer,c=this.points[a].colorBuffer,this.context.bindBuffer(this.context.ARRAY_BUFFER,b),this.context.vertexAttribPointer(this.shader.vertexPositionAttribute,b.itemSize,this.context.FLOAT,!1,0,0),this.context.bindBuffer(this.context.ARRAY_BUFFER,c),this.context.vertexAttribPointer(this.shader.vertexColorAttribute,c.itemSize,this.context.FLOAT,!1,0,0),this.context.drawArrays(this.context.POINTS,0,b.numItems)};
  JSM.ConvertBodyToRenderBody=function(a,b,c){function d(a,b){var c=JSM.HexColorToNormalizedRGBComponents(a.ambient),d=JSM.HexColorToNormalizedRGBComponents(a.diffuse),e=JSM.HexColorToNormalizedRGBComponents(a.specular);return new JSM.RenderMaterial(b,{ambient:c,diffuse:d,specular:e,shininess:a.shininess,opacity:a.opacity,singleSided:a.singleSided,pointSize:a.pointSize,texture:a.texture})}var e=!1;void 0!==c&&null!==c&&(void 0!==c.hasConvexPolygons&&null!==c.hasConvexPolygons)&&(e=c.hasConvexPolygons);
  var f=new JSM.RenderBody,g=null,h=null,l=null;JSM.ExplodeBody(a,b,{hasConvexPolygons:e,onPointGeometryStart:function(){g=[];l=h=null},onPointGeometryEnd:function(a){a=d(a,JSM.RenderMaterialFlags.Point);a=new JSM.RenderMesh(a);a.SetVertexArray(g);f.AddMesh(a)},onPoint:function(a){g.push(a.x,a.y,a.z)},onLineGeometryStart:function(){g=[];l=h=null},onLineGeometryEnd:function(a){a=d(a,JSM.RenderMaterialFlags.Line);a=new JSM.RenderMesh(a);a.SetVertexArray(g);f.AddMesh(a)},onLine:function(a,b){g.push(a.x,
  a.y,a.z);g.push(b.x,b.y,b.z)},onGeometryStart:function(){g=[];h=[];l=[]},onGeometryEnd:function(a){var b=JSM.RenderMaterialFlags.Triangle;null!==a.texture&&(b+=JSM.RenderMaterialFlags.Textured);1>a.opacity&&(b+=JSM.RenderMaterialFlags.Transparent);b=d(a,b);b=new JSM.RenderMesh(b);b.SetVertexArray(g);b.SetNormalArray(h);null!==a.texture&&b.SetUVArray(l);f.AddMesh(b)},onTriangle:function(a,b,c,d,e,f,s,v,u){g.push(a.x,a.y,a.z);g.push(b.x,b.y,b.z);g.push(c.x,c.y,c.z);h.push(d.x,d.y,d.z);h.push(e.x,e.y,
  e.z);h.push(f.x,f.y,f.z);null!==s&&(null!==v&&null!==u)&&(l.push(s.x,s.y),l.push(v.x,v.y),l.push(u.x,u.y))}});return f};JSM.ConvertModelToRenderBodies=function(a,b,c){var d=[],e,f;for(e=0;e<a.BodyCount();e++)f=a.GetBody(e),f=JSM.ConvertBodyToRenderBody(f,b,c),d.push(f);return d};
  JSM.ConvertJSONDataToRenderBodies=function(a,b){function c(a,b){function c(a,b,d){function e(a,b,c,d,f){var g=new JSM.Vector2D(a,b);if(!JSM.IsZero(f)){var h=Math.sin(f*JSM.DegRad);f=Math.cos(f*JSM.DegRad);g.x=f*a-h*b;g.y=h*a+f*b}g.x=c[0]+g.x*d[0];g.y=c[1]+g.y*d[1];return g}function f(a,b,c,d,e){var g,h,k;for(g=0;3>g;g++){h=c[d+g];for(k=0;k<e;k++)a.push(b[h*e+k])}}var g=d[b.material];d=new JSM.RenderMaterial(JSM.RenderMaterialFlags.Triangle,{ambient:g.diffuse||[1,1,1],diffuse:g.diffuse||[1,1,1],specular:g.specular||
  [1,1,1],shininess:g.shininess||0,opacity:g.opacity||1});var h=void 0!==g.texture&&null!==g.texture;h&&(d.SetType(JSM.RenderMaterialFlags.Triangle+JSM.RenderMaterialFlags.Textured),d.texture=g.texture,d.ambient=[1,1,1],d.diffuse=[1,1,1]);d=new JSM.RenderMesh(d);var k=[],l=[],m=[],n;for(n=0;n<b.parameters.length;n+=9)f(k,a.vertices,b.parameters,n,3),f(l,a.normals,b.parameters,n+3,3),f(m,a.uvs,b.parameters,n+6,2);if(h){a=g.offset||[0,0];b=g.scale||[1,1];g=g.rotation||0;for(n=0;n<m.length;n+=2)h=e(m[n+
  0],m[n+1],a,b,g),m[n+0]=h.x,m[n+1]=-h.y}d.SetVertexArray(k);d.SetNormalArray(l);d.SetUVArray(m);return d}var d=new JSM.RenderBody,e,f;for(e=0;e<a.triangles.length;e++)f=a.triangles[e],f=c(a,f,b),d.AddMesh(f);return d}var d=[],e=a.materials;if(void 0===e)return d;var f=a.meshes;if(void 0===f)return d;var g=0;JSM.AsyncRunTask(function(){var a=c(f[g],e);d.push(a);g+=1;return!0},b,f.length,0,d);return d};
  JSM.Mouse=function(){this.down=!1;this.button=0;this.alt=this.ctrl=this.shift=!1;this.prev=new JSM.Coord2D(0,0);this.curr=new JSM.Coord2D(0,0);this.diff=new JSM.Coord2D(0,0)};JSM.Mouse.prototype.Down=function(a,b){var c=a||window.event;this.down=!0;this.button=a.which;this.shift=a.shiftKey;this.ctrl=a.ctrlKey;this.alt=a.altKey;this.SetCurrent(c,b);this.prev=this.curr.Clone()};
  JSM.Mouse.prototype.Move=function(a,b){var c=a||window.event;this.shift=a.shiftKey;this.ctrl=a.ctrlKey;this.alt=a.altKey;this.SetCurrent(c,b);this.diff=JSM.CoordSub2D(this.curr,this.prev);this.prev=this.curr.Clone()};JSM.Mouse.prototype.Up=function(a,b){var c=a||window.event;this.down=!1;this.SetCurrent(c,b)};JSM.Mouse.prototype.Out=function(a,b){var c=a||window.event;this.down=!1;this.SetCurrent(c,b)};
  JSM.Mouse.prototype.SetCurrent=function(a,b){var c=a.clientX,d=a.clientY;void 0!==b&&(void 0!==b.offsetLeft&&void 0!==b.offsetTop)&&(c-=b.offsetLeft,d-=b.offsetTop);void 0!==window.pageXOffset&&void 0!==window.pageYOffset&&(c+=window.pageXOffset,d+=window.pageYOffset);this.curr=new JSM.Coord2D(c,d)};JSM.Touch=function(){this.down=!1;this.fingers=0;this.prev=new JSM.Coord2D;this.curr=new JSM.Coord2D;this.diff=new JSM.Coord2D};
  JSM.Touch.prototype.Start=function(a,b){0!==a.touches.length&&(this.down=!0,this.fingers=a.touches.length,this.SetCurrent(a,b),this.prev=this.curr.Clone())};JSM.Touch.prototype.Move=function(a,b){0!==a.touches.length&&(this.fingers=a.touches.length,this.SetCurrent(a,b),this.diff=JSM.CoordSub2D(this.curr,this.prev),this.prev=this.curr.Clone())};JSM.Touch.prototype.End=function(a,b){0!==a.touches.length&&(this.down=!1,this.SetCurrent(a,b))};
  JSM.Touch.prototype.SetCurrent=function(a,b){function c(a){var c=a.pageX;a=a.pageY;void 0!==b&&(void 0!==b.offsetLeft&&void 0!==b.offsetTop)&&(c-=b.offsetLeft,a-=b.offsetTop);void 0!==window.pageXOffset&&void 0!==window.pageYOffset&&(c+=window.pageXOffset,a+=window.pageYOffset);return new JSM.Coord2D(c,a)}if(1==a.touches.length||3==a.touches.length)this.curr=c(a.touches[0]);else if(2==a.touches.length){var d=c(a.touches[0]).DistanceTo(c(a.touches[1]));this.curr=new JSM.Coord2D(d,d)}};
  JSM.OrderPolygons=function(a,b,c){var d=[],e=[],f=[],g=[],h=[],l=[],k=[],m=a.PolygonCount(),n,p;for(n=0;n<m;n++){l.push(n);k.push([]);for(p=0;p<m;p++)k[n].push(null)}(function(){var k=JSM.CoordSub(c,b).Normalize(),k=JSM.GetPlaneFromCoordAndDirection(b,k),l,m,n,p,t,w;for(l=0;l<a.PolygonCount();l++){t=JSM.Inf;w=-JSM.Inf;n=a.GetPolygon(l);for(m=0;m<n.VertexIndexCount();m++)p=a.GetVertexPosition(n.GetVertexIndex(m)),p=k.CoordDistance(p),JSM.IsLower(p,t)&&(t=p),JSM.IsGreater(p,w)&&(w=p);d.push(t);e.push(w);
  m=a.GetPolygon(l);n=new JSM.Coord(0,0,0);w=t=void 0;for(t=0;t<m.VertexIndexCount();t++)w=a.GetVertexPosition(m.GetVertexIndex(t)),n=JSM.CoordAdd(n,w);n.MultiplyScalar(1/m.VertexIndexCount());m=n;n=k.CoordDistance(m);f.push(m);g.push(n);n=JSM.CalculateBodyPolygonNormal(a,l);t=JSM.CoordSub(m,b).Normalize();t=JSM.VectorDot(n,t);JSM.IsGreaterOrEqual(t,0)&&n.MultiplyScalar(-1);m=JSM.GetPlaneFromCoordAndDirection(m,n);h.push(m)}})();(function(){var a=l.length,b,c;for(b=0;b<a-1;b++)for(c=0;c<a-b-1;c++){var d;
  d=l[c];var f=l[c+1];d=JSM.IsLower(e[d],e[f])?!0:JSM.IsEqual(e[d],e[f])&&JSM.IsLower(g[d],g[f])?!0:!1;if(d){d=l;var f=c+1,h=d[c];d[c]=d[f];d[f]=h}}})();(function(){var b=l.length,c,f;for(c=0;c<b-1;c++)for(f=0;f<b-c-1;f++){var g;g=l[f];var m=l[f+1];if(null!==k[g][m])g=k[g][m];else{var n;if(n=JSM.IsLowerOrEqual(d[g],e[m])){var p=m;n=h[g];for(var A=h[p],B=void 0,x=void 0,y=!0,z=a.GetPolygon(g),B=0;B<z.VertexIndexCount();B++)if(x=a.GetVertexPosition(z.GetVertexIndex(B)),A.CoordPosition(x)===JSM.CoordPlanePosition.CoordInFrontOfPlane){y=
  !1;break}if(y)n=!1;else{A=!0;p=a.GetPolygon(p);for(B=0;B<p.VertexIndexCount();B++)if(x=a.GetVertexPosition(p.GetVertexIndex(B)),n.CoordPosition(x)===JSM.CoordPlanePosition.CoordAtBackOfPlane){A=!1;break}n=A?!1:!0}}g=n?k[g][m]=!0:k[g][m]=!1}g&&(g=l,m=f+1,n=g[f],g[f]=g[m],g[m]=n)}})();return l};JSM.CanvasDrawer=function(a){this.canvas=a;this.context=this.canvas.getContext("2d")};JSM.CanvasDrawer.prototype.GetWidth=function(){return this.canvas.width};JSM.CanvasDrawer.prototype.GetHeight=function(){return this.canvas.height};
  JSM.CanvasDrawer.prototype.Clear=function(){this.context.clearRect(0,0,this.canvas.width,this.canvas.height);this.context.fillStyle="#ffffff";this.context.fillRect(0,0,this.canvas.width,this.canvas.height)};JSM.CanvasDrawer.prototype.DrawLine=function(a,b){this.context.beginPath();this.context.moveTo(a.x,this.canvas.height-a.y);this.context.lineTo(b.x,this.canvas.height-b.y);this.context.stroke()};
  JSM.CanvasDrawer.prototype.DrawPolygon=function(a,b,c){var d=this.context;b=JSM.HexColorToRGBComponents(b);d.fillStyle="rgb("+b[0]+","+b[1]+","+b[2]+")";this.context.beginPath();for(d=0;d<a.VertexCount();d++)b=a.GetVertex(d),0===d?this.context.moveTo(b.x,this.canvas.height-b.y):this.context.lineTo(b.x,this.canvas.height-b.y);this.context.closePath();this.context.fill();if(c)for(d=0;d<a.VertexCount();d++)b=a.GetVertex(d),c=a.GetVertex(d<a.VertexCount()-1?d+1:0),this.DrawLine(b,c)};
  JSM.SVGDrawer=function(a){this.svgObject=a;this.svgNameSpace="http://www.w3.org/2000/svg"};JSM.SVGDrawer.prototype.GetWidth=function(){return this.svgObject.getAttribute("width")};JSM.SVGDrawer.prototype.GetHeight=function(){return this.svgObject.getAttribute("height")};JSM.SVGDrawer.prototype.Clear=function(){for(;this.svgObject.lastChild;)this.svgObject.removeChild(this.svgObject.lastChild)};
  JSM.SVGDrawer.prototype.DrawLine=function(a,b){var c=document.createElementNS(this.svgNameSpace,"line"),d=this.GetHeight();c.setAttributeNS(null,"stroke","black");c.setAttributeNS(null,"x1",a.x);c.setAttributeNS(null,"y1",d-a.y);c.setAttributeNS(null,"x2",b.x);c.setAttributeNS(null,"y2",d-b.y);this.svgObject.appendChild(c)};
  JSM.SVGDrawer.prototype.DrawPolygon=function(a,b){var c="",d=this.GetHeight(),e,f;for(e=0;e<a.VertexCount();e++)f=a.GetVertex(e),c=c+f.x+", "+(d-f.y),e<a.VertexCount()-1&&(c+=", ");d=document.createElementNS(this.svgNameSpace,"polygon");d.setAttributeNS(null,"points",c);d.setAttributeNS(null,"fill",function(a){a=JSM.HexColorToRGBComponents(a);return"rgb("+a[0]+","+a[1]+","+a[2]+")"}(b));d.setAttributeNS(null,"fill-opacity","1.0");d.setAttributeNS(null,"stroke","black");this.svgObject.appendChild(d)};
  JSM.DrawProjectedBody=function(a,b,c,d,e,f){function g(a,b){var c=JSM.Project(b,k,m,n,p*JSM.DegRad,q,r,s,v);a.AddVertex(c.x,c.y)}function h(b){var c=new JSM.Polygon2D,d,e;for(d=0;d<b.VertexIndexCount();d++)e=a.GetVertexPosition(b.GetVertexIndex(d)),g(c,e);return c}e&&f.Clear();e=f.GetWidth();var l=f.GetHeight(),k=c.eye,m=c.center,n=c.up,p=c.fieldOfView,q=e/l,r=c.nearClippingPlane,s=c.farClippingPlane,v=[0,0,e,l],u,t,w,A;if("HiddenLinePainter"==d){d=JSM.OrderPolygons(a,k,m);if(void 0===b||null===b)b=
  new JSM.Materials;for(c=0;c<d.length;c++)u=a.GetPolygon(d[c]),t=h(u),w=u.GetMaterialIndex(),A=b.GetMaterial(w).diffuse,f.DrawPolygon(t,A,!0)}else if("HiddenLineBSPTree"==d){if(void 0===b||null===b)b=new JSM.Materials;d=new JSM.BSPTree;JSM.AddBodyToBSPTree(a,d);JSM.TraverseBSPTreeForEyePosition(d,c.eye,function(c){var d=c.polygon,e=new JSM.Polygon2D,h,k;for(h=0;h<d.VertexCount();h++)k=d.GetVertex(h),g(e,k);t=e;u=a.GetPolygon(c.userData.originalPolygon);w=u.GetMaterialIndex();A=b.GetMaterial(w).diffuse;
  f.DrawPolygon(t,A,!0)})}else if("HiddenLineFrontFacing"==d){if(void 0===b||null===b)b=new JSM.Materials;for(c=0;c<a.PolygonCount();c++)u=a.GetPolygon(c),t=h(u),t.GetOrientation()==JSM.Orientation.CounterClockwise&&(w=u.GetMaterialIndex(),A=b.GetMaterial(w).diffuse,f.DrawPolygon(t,A,!0))}else if("Wireframe"==d){var B,x,y,z=[];for(c=0;c<a.PolygonCount();c++){x=B=null;u=a.GetPolygon(c);l=u.VertexIndexCount();for(d=0;d<=l;d++)y=u.GetVertexIndex(d%l),e=a.GetVertexPosition(y),t=JSM.Project(e,k,m,n,p*JSM.DegRad,
  q,r,s,v),null!==B&&(null!==x&&void 0===z[[x,y]])&&(f.DrawLine(B,t),z[[x,y]]=!0,z[[y,x]]=!0),x=y,B=t}}return!0};JSM.Navigation=function(){this.fullscreen=this.orbitCenter=this.cameraFarDistanceLimit=this.cameraNearDistanceLimit=this.cameraEnableZoom=this.cameraEnablePan=this.cameraEnableOrbit=this.cameraFixUp=this.touch=this.mouse=this.resizeCallback=this.drawCallback=this.camera=this.canvas=null};
  JSM.Navigation.prototype.Init=function(a,b,c,d){this.canvas=a;this.camera=b;this.drawCallback=c;this.resizeCallback=d;this.mouse=new JSM.Mouse;this.touch=new JSM.Touch;this.cameraEnableZoom=this.cameraEnablePan=this.cameraEnableOrbit=this.cameraFixUp=!0;this.orbitCenter=this.camera.center.Clone();this.fullscreen=!1;var e=this;document.addEventListener&&(document.addEventListener("mousemove",function(a){e.OnMouseMove(a)}),document.addEventListener("mouseup",function(a){e.OnMouseUp(a)}));this.canvas.addEventListener&&
  (this.canvas.addEventListener("mousedown",function(a){e.OnMouseDown(a)},!1),this.canvas.addEventListener("DOMMouseScroll",function(a){e.OnMouseWheel(a)},!1),this.canvas.addEventListener("mousewheel",function(a){e.OnMouseWheel(a)},!1),this.canvas.addEventListener("touchstart",function(a){e.OnTouchStart(a)},!1),this.canvas.addEventListener("touchmove",function(a){e.OnTouchMove(a)},!1),this.canvas.addEventListener("touchend",function(a){e.OnTouchEnd(a)},!1),this.canvas.addEventListener("contextmenu",
  function(a){e.OnContextMenu(a)},!1));window.addEventListener&&window.addEventListener("resize",function(a){e.OnResize(a)},!1);return!0};JSM.Navigation.prototype.SetCamera=function(a,b,c){this.camera.Set(a,b,c);this.orbitCenter=this.camera.center.Clone()};JSM.Navigation.prototype.EnableFixUp=function(a){this.cameraFixUp=a};JSM.Navigation.prototype.EnableOrbit=function(a){this.cameraEnableOrbit=a};JSM.Navigation.prototype.EnablePan=function(a){this.cameraEnablePan=a};
  JSM.Navigation.prototype.EnableZoom=function(a){this.cameraEnableZoom=a};JSM.Navigation.prototype.SetNearDistanceLimit=function(a){this.cameraNearDistanceLimit=a};JSM.Navigation.prototype.SetFarDistanceLimit=function(a){this.cameraFarDistanceLimit=a};JSM.Navigation.prototype.SetOrbitCenter=function(a){this.orbitCenter=a};
  JSM.Navigation.prototype.FitInWindow=function(a,b){if(!JSM.IsZero(b)){var c=JSM.CoordSub(this.camera.center,a);this.camera.center=a;this.camera.eye=JSM.CoordSub(this.camera.eye,c);var c=JSM.CoordSub(this.camera.eye,this.camera.center).Normalize(),d=this.camera.fieldOfView/2;this.canvas.width<this.canvas.height&&(d=d*this.canvas.width/this.canvas.height);d=b/Math.sin(d*JSM.DegRad);this.camera.eye=this.camera.center.Clone().Offset(c,d);this.orbitCenter=this.camera.center.Clone()}};
  JSM.Navigation.prototype.SetFullscreen=function(a){this.fullscreen=a;this.ResizeCallback()};
  JSM.Navigation.prototype.Orbit=function(a,b){var c=a*JSM.DegRad,d=b*JSM.DegRad,e=JSM.CoordSub(this.camera.center,this.camera.eye).Normalize(),f=JSM.VectorCross(e,this.camera.up).Normalize(),g=!this.orbitCenter.IsEqual(this.camera.center);this.cameraFixUp?(e=e.AngleTo(this.camera.up)+d,JSM.IsGreater(e,0)&&JSM.IsLower(e,Math.PI)&&(this.camera.eye.Rotate(f,-d,this.orbitCenter),g&&this.camera.center.Rotate(f,-d,this.orbitCenter)),this.camera.eye.Rotate(this.camera.up,-c,this.orbitCenter),g&&this.camera.center.Rotate(this.camera.up,
  -c,this.orbitCenter)):(e=JSM.VectorCross(f,e).Normalize(),this.camera.eye.Rotate(f,-d,this.orbitCenter),this.camera.eye.Rotate(e,-c,this.orbitCenter),g&&(this.camera.center.Rotate(f,-d,this.orbitCenter),this.camera.center.Rotate(e,-c,this.orbitCenter)),this.camera.up=e)};
  JSM.Navigation.prototype.Pan=function(a,b){var c=JSM.CoordSub(this.camera.center,this.camera.eye).Normalize(),d=JSM.VectorCross(c,this.camera.up).Normalize(),c=JSM.VectorCross(d,c).Normalize();this.camera.eye.Offset(d,-a);this.camera.center.Offset(d,-a);this.camera.eye.Offset(c,b);this.camera.center.Offset(c,b)};
  JSM.Navigation.prototype.Zoom=function(a){var b=JSM.CoordSub(this.camera.center,this.camera.eye),c=b.Length(),d=0<a;if(d&&null!==this.cameraNearDistanceLimit&&c<this.cameraNearDistanceLimit||!d&&null!==this.cameraFarDistanceLimit&&c>this.cameraFarDistanceLimit)return 0;this.camera.eye.Offset(b,c*a)};JSM.Navigation.prototype.DrawCallback=function(){void 0!==this.drawCallback&&null!==this.drawCallback&&this.drawCallback()};
  JSM.Navigation.prototype.ResizeCallback=function(){void 0!==this.resizeCallback&&null!==this.resizeCallback&&(this.fullscreen&&(this.canvas.width=window.innerWidth,this.canvas.height=window.innerHeight),this.resizeCallback())};JSM.Navigation.prototype.OnMouseDown=function(a){a.preventDefault();this.mouse.Down(a,this.canvas)};
  JSM.Navigation.prototype.OnMouseMove=function(a){a.preventDefault();this.mouse.Move(a,this.canvas);if(this.mouse.down){a=0;if(1==this.mouse.button){if(!this.cameraEnableOrbit)return;a=0.5;this.Orbit(this.mouse.diff.x*a,this.mouse.diff.y*a)}else if(3==this.mouse.button){if(!this.cameraEnablePan)return;a=0.001*this.camera.eye.DistanceTo(this.camera.center);this.Pan(this.mouse.diff.x*a,this.mouse.diff.y*a)}this.DrawCallback()}};
  JSM.Navigation.prototype.OnMouseUp=function(a){a.preventDefault();this.mouse.Up(a,this.canvas)};JSM.Navigation.prototype.OnMouseOut=function(a){a.preventDefault();this.mouse.Out(a,this.canvas)};JSM.Navigation.prototype.OnMouseWheel=function(a){a.preventDefault();if(this.cameraEnableZoom){var b=a;null===b&&(b=window.event);a=0;b.detail?a=-b.detail:b.wheelDelta&&(a=b.wheelDelta/40);b=0.1;0>a&&(b*=-1);this.Zoom(b);this.DrawCallback()}};
  JSM.Navigation.prototype.OnTouchStart=function(a){a.preventDefault();this.touch.Start(a,this.canvas)};
  JSM.Navigation.prototype.OnTouchMove=function(a){a.preventDefault();this.touch.Move(a,this.canvas);if(this.touch.down){a=0;if(1==this.touch.fingers){if(!this.cameraEnableOrbit)return;a=0.5;this.Orbit(this.touch.diff.x*a,this.touch.diff.y*a)}else if(2==this.touch.fingers){if(!this.cameraEnableZoom)return;this.Zoom(0.005*this.touch.diff.x)}else if(3==this.touch.fingers){if(!this.cameraEnablePan)return;a=0.001*this.camera.eye.DistanceTo(this.camera.center);this.Pan(this.touch.diff.x*a,this.touch.diff.y*
  a)}this.DrawCallback()}};JSM.Navigation.prototype.OnTouchEnd=function(a){a.preventDefault();this.touch.End(a,this.canvas)};JSM.Navigation.prototype.OnContextMenu=function(a){a.preventDefault()};JSM.Navigation.prototype.OnResize=function(a){a.preventDefault();this.ResizeCallback()};JSM.SoftwareViewer=function(){this.navigation=this.drawMode=this.drawer=this.bodies=this.camera=this.canvas=null};JSM.SoftwareViewer.prototype.Start=function(a,b){return!this.InitCanvas(a)||!this.InitCamera(b)?!1:!0};
  JSM.SoftwareViewer.prototype.InitCanvas=function(a){this.bodies=[];this.canvas=a;if(!this.canvas)return!1;this.canvas instanceof HTMLCanvasElement?this.drawer=new JSM.CanvasDrawer(this.canvas):this.canvas instanceof SVGSVGElement&&(this.drawer=new JSM.SVGDrawer(this.canvas));if(!this.drawer)return!1;this.drawMode="Wireframe";return!0};
  JSM.SoftwareViewer.prototype.InitCamera=function(a){this.camera=JSM.ValueOrDefault(a,new JSM.Camera);if(!this.camera)return!1;this.navigation=new JSM.Navigation;return!this.navigation.Init(this.canvas,this.camera,this.Draw.bind(this),this.Resize.bind(this))?!1:!0};JSM.SoftwareViewer.prototype.AddBody=function(a,b){this.bodies.push([a,b])};JSM.SoftwareViewer.prototype.RemoveBodies=function(){this.bodies=[]};
  JSM.SoftwareViewer.prototype.FitInWindow=function(){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.Draw()};JSM.SoftwareViewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
  JSM.SoftwareViewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),c,d,e,f;for(c=0;c<this.bodies.length;c++){e=this.bodies[c][0];for(d=0;d<e.VertexCount();d++)f=e.GetVertex(d),a.x=JSM.Minimum(a.x,f.position.x),a.y=JSM.Minimum(a.y,f.position.y),a.z=JSM.Minimum(a.z,f.position.z),b.x=JSM.Maximum(b.x,f.position.x),b.y=JSM.Maximum(b.y,f.position.y),b.z=JSM.Maximum(b.z,f.position.z)}return new JSM.Box(a,b)};
  JSM.SoftwareViewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,c,d,e,f;for(c=0;c<this.bodies.length;c++){e=this.bodies[c][0];for(d=0;d<e.VertexCount();d++)f=e.GetVertex(d),f=a.DistanceTo(f.position),JSM.IsGreater(f,b)&&(b=f)}return new JSM.Sphere(a,b)};JSM.SoftwareViewer.prototype.Resize=function(){this.Draw()};
  JSM.SoftwareViewer.prototype.Draw=function(){var a,b;this.drawer.Clear();for(a=0;a<this.bodies.length;a++)b=this.bodies[a],JSM.DrawProjectedBody(b[0],b[1],this.camera,this.drawMode,!1,this.drawer);return!0};JSM.SpriteViewer=function(){this.navigation=this.projected=this.points=this.callbacks=this.camera=this.canvas=null};JSM.SpriteViewer.prototype.Start=function(a,b,c){return!this.InitCanvas(a)||!this.InitCamera(b)||!this.InitCallbacks(c)?!1:!0};
  JSM.SpriteViewer.prototype.InitCanvas=function(a){this.points=[];this.canvas=a;return!this.canvas?!1:!0};JSM.SpriteViewer.prototype.InitCamera=function(a){this.camera=JSM.ValueOrDefault(a,new JSM.Camera);if(!this.camera)return!1;this.navigation=new JSM.Navigation;return!this.navigation.Init(this.canvas,this.camera,this.Draw.bind(this))?!1:!0};
  JSM.SpriteViewer.prototype.InitCallbacks=function(a){this.callbacks={onPointDraw:null};void 0!==a&&(void 0!==a.onDrawStart&&(this.callbacks.onDrawStart=a.onDrawStart),void 0!==a.onPointDraw&&(this.callbacks.onPointDraw=a.onPointDraw),void 0!==a.onDrawEnd&&(this.callbacks.onDrawEnd=a.onDrawEnd));return!0};JSM.SpriteViewer.prototype.AddPoint=function(a){this.points.push(a)};JSM.SpriteViewer.prototype.RemovePoints=function(){this.points=[]};JSM.SpriteViewer.prototype.Resize=function(){this.Draw()};
  JSM.SpriteViewer.prototype.NearestPointUnderPosition=function(a,b,c){b=new JSM.Coord2D(b,c);c=-1;var d=JSM.Inf,e,f,g;for(e=0;e<this.projected.length;e++)f=this.projected[e],g=b.DistanceTo(new JSM.Coord2D(f.position.x,f.position.y)),JSM.IsLower(g,a)&&JSM.IsLower(g,d)&&(c=f.originalIndex,d=g);return c};JSM.SpriteViewer.prototype.NearestPointUnderMouse=function(a){return this.NearestPointUnderPosition(a,this.navigation.mouse.curr.x,this.navigation.mouse.curr.y)};
  JSM.SpriteViewer.prototype.NearestPointUnderTouch=function(a){return this.NearestPointUnderPosition(a,this.navigation.touch.curr.x,this.navigation.touch.curr.y)};JSM.SpriteViewer.prototype.FitInWindow=function(){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.Draw()};JSM.SpriteViewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
  JSM.SpriteViewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),c,d;for(c=0;c<this.points.length;c++)d=this.points[c],a.x=JSM.Minimum(a.x,d.x),a.y=JSM.Minimum(a.y,d.y),a.z=JSM.Minimum(a.z,d.z),b.x=JSM.Maximum(b.x,d.x),b.y=JSM.Maximum(b.y,d.y),b.z=JSM.Maximum(b.z,d.z);return new JSM.Box(a,b)};
  JSM.SpriteViewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,c,d;for(c=0;c<this.points.length;c++)d=this.points[c],d=a.DistanceTo(d),JSM.IsGreater(d,b)&&(b=d);return new JSM.Sphere(a,b)};
  JSM.SpriteViewer.prototype.Draw=function(){if(null!==this.callbacks.onDrawStart)this.callbacks.onDrawStart(this.canvas);var a=this.canvas.width/this.canvas.height,b=[0,0,this.canvas.width,this.canvas.height];this.projected=[];var c,d;for(c=0;c<this.points.length;c++)d=this.points[c],d=JSM.Project(d,this.camera.eye,this.camera.center,this.camera.up,this.camera.fieldOfView*JSM.DegRad,a,this.camera.nearClippingPlane,this.camera.farClippingPlane,b),d.y=this.canvas.height-d.y,null!==d&&this.projected.push({position:d,
  originalIndex:c});this.projected.sort(function(a,b){return a.position.z>b.position.z?-1:a.position.z<b.position.z?1:0});for(c=0;c<this.projected.length;c++)if(null!==this.callbacks.onPointDraw)this.callbacks.onPointDraw(this.canvas,this.projected[c].originalIndex,this.projected[c].position);if(null!==this.callbacks.onDrawEnd)this.callbacks.onDrawEnd(this.canvas);return!0};JSM.Viewer=function(){this.cameraLight=this.navigation=this.renderer=this.camera=null};
  JSM.Viewer.prototype.Init=function(a,b){return!this.InitRenderer(a)||!this.InitNavigation(b)||!this.InitLights()?!1:!0};JSM.Viewer.prototype.Reset=function(){this.RemoveBodies();this.RemoveLights();this.SetAmbientLight(new JSM.RenderAmbientLight(8355711));this.EnableCameraLight()};JSM.Viewer.prototype.InitRenderer=function(a){this.renderer=new JSM.Renderer;return!this.renderer.Init(a)?!1:!0};
  JSM.Viewer.prototype.InitNavigation=function(a){this.camera=JSM.ValueOrDefault(a,new JSM.Camera);if(!this.camera)return!1;this.navigation=new JSM.Navigation;return!this.navigation.Init(this.renderer.canvas,this.camera,this.Draw.bind(this),this.Resize.bind(this))?!1:!0};JSM.Viewer.prototype.InitLights=function(){this.SetAmbientLight(new JSM.RenderAmbientLight(8355711));this.EnableCameraLight();return!0};JSM.Viewer.prototype.SetClearColor=function(a,b,c){this.renderer.SetClearColor(a,b,c)};
  JSM.Viewer.prototype.EnableCameraLight=function(){null===this.cameraLight&&(this.cameraLight=new JSM.RenderDirectionalLight(8355711,16777215,new JSM.Vector(1,0,0)),this.AddLight(this.cameraLight))};JSM.Viewer.prototype.DisableCameraLight=function(){null!==this.cameraLight&&(this.RemoveLight(this.cameraLight),this.cameraLight=null)};JSM.Viewer.prototype.GetCameraLight=function(){return this.cameraLight};JSM.Viewer.prototype.SetAmbientLight=function(a){this.renderer.SetAmbientLight(a)};
  JSM.Viewer.prototype.AddLight=function(a){this.renderer.AddLight(a)};JSM.Viewer.prototype.RemoveLight=function(a){this.renderer.RemoveLight(a)};JSM.Viewer.prototype.RemoveLights=function(){this.renderer.RemoveLights();this.cameraLight=null};JSM.Viewer.prototype.AddBody=function(a){this.renderer.AddBody(a,this.Draw.bind(this))};JSM.Viewer.prototype.AddBodies=function(a){this.renderer.AddBodies(a,this.Draw.bind(this))};JSM.Viewer.prototype.RemoveBody=function(a){this.renderer.RemoveBody(a)};
  JSM.Viewer.prototype.RemoveBodies=function(){this.renderer.RemoveBodies()};JSM.Viewer.prototype.FitInWindow=function(){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius())};JSM.Viewer.prototype.SetFullscreen=function(a){this.navigation.SetFullscreen(a)};JSM.Viewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
  JSM.Viewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf);this.renderer.EnumerateBodies(function(c){var d=c.GetTransformation();c.EnumerateMeshes(function(c){var f,g;for(f=0;f<c.VertexCount();f++)g=c.GetTransformedVertex(f,d),a.x=JSM.Minimum(a.x,g.x),a.y=JSM.Minimum(a.y,g.y),a.z=JSM.Minimum(a.z,g.z),b.x=JSM.Maximum(b.x,g.x),b.y=JSM.Maximum(b.y,g.y),b.z=JSM.Maximum(b.z,g.z)})});return new JSM.Box(a,b)};
  JSM.Viewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0;this.renderer.EnumerateBodies(function(c){var d=c.GetTransformation();c.EnumerateMeshes(function(c){var f,g;for(f=0;f<c.VertexCount();f++)g=c.GetTransformedVertex(f,d),g=a.DistanceTo(g),JSM.IsGreater(g,b)&&(b=g)})});return new JSM.Sphere(a,b)};JSM.Viewer.prototype.FindObjects=function(a,b){return this.renderer.FindObjects(this.camera,a,b)};JSM.Viewer.prototype.Resize=function(){this.renderer.Resize();this.Draw()};
  JSM.Viewer.prototype.Draw=function(){var a=this.camera,b=this.GetCameraLight();null!==b&&(b.direction=JSM.CoordSub(a.center,a.eye).Normalize());this.renderer.Render(a)};JSM.PointCloudViewer=function(){this.navigation=this.renderer=this.canvas=null};JSM.PointCloudViewer.prototype.Init=function(a,b){return!this.InitRenderer(a,b)||!this.InitNavigation()?!1:!0};JSM.PointCloudViewer.prototype.InitRenderer=function(a,b){this.renderer=new JSM.PointCloudRenderer;return!this.renderer.Init(a,b)?!1:!0};
  JSM.PointCloudViewer.prototype.InitNavigation=function(){this.navigation=new JSM.Navigation;return!this.navigation.Init(this.renderer.canvas,this.renderer.camera,this.Draw.bind(this),this.Resize.bind(this))?!1:!0};JSM.PointCloudViewer.prototype.SetClearColor=function(a,b,c){this.renderer.SetClearColor(a,b,c)};JSM.PointCloudViewer.prototype.SetPointSize=function(a){this.renderer.SetPointSize(a)};JSM.PointCloudViewer.prototype.AddPoints=function(a,b){this.renderer.AddPoints(a,b)};
  JSM.PointCloudViewer.prototype.RemovePoints=function(){this.renderer.RemovePoints()};JSM.PointCloudViewer.prototype.FitInWindow=function(){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.Draw()};JSM.PointCloudViewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
  JSM.PointCloudViewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),c,d,e,f;for(c=0;c<this.renderer.points.length;c++){e=this.renderer.points[c].pointArray;for(d=0;d<e.length;d+=3)f=new JSM.Coord(e[d],e[d+1],e[d+2]),a.x=JSM.Minimum(a.x,f.x),a.y=JSM.Minimum(a.y,f.y),a.z=JSM.Minimum(a.z,f.z),b.x=JSM.Maximum(b.x,f.x),b.y=JSM.Maximum(b.y,f.y),b.z=JSM.Maximum(b.z,f.z)}return new JSM.Box(a,b)};
  JSM.PointCloudViewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,c,d,e,f;for(c=0;c<this.renderer.points.length;c++){e=this.renderer.points[c].pointArray;for(d=0;d<e.length;d+=3)f=new JSM.Coord(e[d],e[d+1],e[d+2]),f=a.DistanceTo(f),JSM.IsGreater(f,b)&&(b=f)}return new JSM.Sphere(a,b)};JSM.PointCloudViewer.prototype.Resize=function(){this.renderer.Resize();this.Draw()};JSM.PointCloudViewer.prototype.Draw=function(){this.renderer.Render()};
  JSM.GenerateSolidWithRadius=function(a,b){var c=new JSM.Body,d=!0;"Tetrahedron"===a?c=JSM.GenerateTetrahedron():"Hexahedron"===a?c=JSM.GenerateHexahedron():"Octahedron"===a?c=JSM.GenerateOctahedron():"Dodecahedron"===a?c=JSM.GenerateDodecahedron():"Icosahedron"===a?c=JSM.GenerateIcosahedron():"TruncatedTetrahedron"===a?c=JSM.GenerateTruncatedTetrahedron():"Cuboctahedron"===a?c=JSM.GenerateCuboctahedron():"TruncatedCube"===a?c=JSM.GenerateTruncatedCube():"TruncatedOctahedron"===a?c=JSM.GenerateTruncatedOctahedron():
  "Rhombicuboctahedron"===a?c=JSM.GenerateRhombicuboctahedron():"TruncatedCuboctahedron"===a?c=JSM.GenerateTruncatedCuboctahedron():"SnubCube"===a?c=JSM.GenerateSnubCube():"Icosidodecahedron"===a?c=JSM.GenerateIcosidodecahedron():"TruncatedDodecahedron"===a?c=JSM.GenerateTruncatedDodecahedron():"TruncatedIcosahedron"===a?c=JSM.GenerateTruncatedIcosahedron():"Rhombicosidodecahedron"===a?c=JSM.GenerateRhombicosidodecahedron():"TruncatedIcosidodecahedron"===a?c=JSM.GenerateTruncatedIcosidodecahedron():
  "SnubDodecahedron"===a?c=JSM.GenerateSnubDodecahedron():"TetrakisHexahedron"===a?(c=JSM.GenerateTetrakisHexahedron(),d=!1):"RhombicDodecahedron"===a?(c=JSM.GenerateRhombicDodecahedron(),d=!1):"PentakisDodecahedron"===a?(c=JSM.GeneratePentakisDodecahedron(),d=!1):"SmallStellatedDodecahedron"===a?(c=JSM.GenerateSmallStellatedDodecahedron(),d=!1):"GreatDodecahedron"===a?(c=JSM.GenerateGreatDodecahedron(),d=!1):"SmallTriambicIcosahedron"===a?(c=JSM.GenerateSmallTriambicIcosahedron(),d=!1):"GreatStellatedDodecahedron"===
  a?(c=JSM.GenerateGreatStellatedDodecahedron(),d=!1):"SmallTriakisOctahedron"===a?(c=JSM.GenerateSmallTriakisOctahedron(),d=!1):"StellaOctangula"===a?(c=JSM.GenerateStellaOctangula(),d=!1):"TriakisTetrahedron"===a&&(c=JSM.GenerateTriakisTetrahedron(),d=!1);if(0<c.VertexCount()){var e=0;if(d)e=c.GetVertexPosition(0).Length();else for(var f,d=0;d<c.VertexCount();d++)f=c.GetVertexPosition(d).Length(),JSM.IsGreater(f,e)&&(e=f);e=b/e;for(d=0;d<c.VertexCount();d++)f=c.GetVertex(d),f.position.MultiplyScalar(e)}return c};
  JSM.GenerateTetrahedron=function(){var a=new JSM.Body;a.AddVertex(new JSM.BodyVertex(new JSM.Coord(1,1,1)));a.AddVertex(new JSM.BodyVertex(new JSM.Coord(-1,-1,1)));a.AddVertex(new JSM.BodyVertex(new JSM.Coord(-1,1,-1)));a.AddVertex(new JSM.BodyVertex(new JSM.Coord(1,-1,-1)));a.AddPolygon(new JSM.BodyPolygon([0,1,3]));a.AddPolygon(new JSM.BodyPolygon([0,2,1]));a.AddPolygon(new JSM.BodyPolygon([0,3,2]));a.AddPolygon(new JSM.BodyPolygon([1,2,3]));return a};
  JSM.GenerateHexahedron=function(){var a=new JSM.Body;JSM.AddVertexToBody(a,1,1,1);JSM.AddVertexToBody(a,1,1,-1);JSM.AddVertexToBody(a,1,-1,1);JSM.AddVertexToBody(a,1,-1,-1);JSM.AddVertexToBody(a,-1,1,1);JSM.AddVertexToBody(a,-1,1,-1);JSM.AddVertexToBody(a,-1,-1,1);JSM.AddVertexToBody(a,-1,-1,-1);JSM.AddPolygonToBody(a,[0,1,5,4]);JSM.AddPolygonToBody(a,[0,2,3,1]);JSM.AddPolygonToBody(a,[0,4,6,2]);JSM.AddPolygonToBody(a,[1,3,7,5]);JSM.AddPolygonToBody(a,[2,6,7,3]);JSM.AddPolygonToBody(a,[4,5,7,6]);
  return a};JSM.GenerateOctahedron=function(){var a=new JSM.Body;JSM.AddVertexToBody(a,1,0,0);JSM.AddVertexToBody(a,-1,0,0);JSM.AddVertexToBody(a,0,1,0);JSM.AddVertexToBody(a,0,-1,0);JSM.AddVertexToBody(a,0,0,1);JSM.AddVertexToBody(a,0,0,-1);JSM.AddPolygonToBody(a,[0,2,4]);JSM.AddPolygonToBody(a,[0,3,5]);JSM.AddPolygonToBody(a,[0,4,3]);JSM.AddPolygonToBody(a,[0,5,2]);JSM.AddPolygonToBody(a,[1,2,5]);JSM.AddPolygonToBody(a,[1,3,4]);JSM.AddPolygonToBody(a,[1,4,2]);JSM.AddPolygonToBody(a,[1,5,3]);return a};
  JSM.GenerateDodecahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=1/b;JSM.AddVertexToBody(a,1,1,1);JSM.AddVertexToBody(a,1,1,-1);JSM.AddVertexToBody(a,1,-1,1);JSM.AddVertexToBody(a,-1,1,1);JSM.AddVertexToBody(a,1,-1,-1);JSM.AddVertexToBody(a,-1,1,-1);JSM.AddVertexToBody(a,-1,-1,1);JSM.AddVertexToBody(a,-1,-1,-1);JSM.AddVertexToBody(a,0,+c,+b);JSM.AddVertexToBody(a,0,+c,-b);JSM.AddVertexToBody(a,0,-c,+b);JSM.AddVertexToBody(a,0,-c,-b);JSM.AddVertexToBody(a,+c,+b,0);JSM.AddVertexToBody(a,
  +c,-b,0);JSM.AddVertexToBody(a,-c,+b,0);JSM.AddVertexToBody(a,-c,-b,0);JSM.AddVertexToBody(a,+b,0,+c);JSM.AddVertexToBody(a,-b,0,+c);JSM.AddVertexToBody(a,+b,0,-c);JSM.AddVertexToBody(a,-b,0,-c);JSM.AddPolygonToBody(a,[0,8,10,2,16]);JSM.AddPolygonToBody(a,[0,16,18,1,12]);JSM.AddPolygonToBody(a,[0,12,14,3,8]);JSM.AddPolygonToBody(a,[1,9,5,14,12]);JSM.AddPolygonToBody(a,[1,18,4,11,9]);JSM.AddPolygonToBody(a,[2,10,6,15,13]);JSM.AddPolygonToBody(a,[2,13,4,18,16]);JSM.AddPolygonToBody(a,[3,14,5,19,17]);
  JSM.AddPolygonToBody(a,[3,17,6,10,8]);JSM.AddPolygonToBody(a,[4,13,15,7,11]);JSM.AddPolygonToBody(a,[5,9,11,7,19]);JSM.AddPolygonToBody(a,[6,17,19,7,15]);return a};
  JSM.GenerateIcosahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2;JSM.AddVertexToBody(a,0,1,+b);JSM.AddVertexToBody(a,0,1,-b);JSM.AddVertexToBody(a,0,-1,+b);JSM.AddVertexToBody(a,0,-1,-b);JSM.AddVertexToBody(a,1,+b,0);JSM.AddVertexToBody(a,1,-b,0);JSM.AddVertexToBody(a,-1,+b,0);JSM.AddVertexToBody(a,-1,-b,0);JSM.AddVertexToBody(a,+b,0,1);JSM.AddVertexToBody(a,+b,0,-1);JSM.AddVertexToBody(a,-b,0,1);JSM.AddVertexToBody(a,-b,0,-1);JSM.AddPolygonToBody(a,[0,2,8]);JSM.AddPolygonToBody(a,[0,4,
  6]);JSM.AddPolygonToBody(a,[0,6,10]);JSM.AddPolygonToBody(a,[0,8,4]);JSM.AddPolygonToBody(a,[0,10,2]);JSM.AddPolygonToBody(a,[1,3,11]);JSM.AddPolygonToBody(a,[1,4,9]);JSM.AddPolygonToBody(a,[1,6,4]);JSM.AddPolygonToBody(a,[1,9,3]);JSM.AddPolygonToBody(a,[1,11,6]);JSM.AddPolygonToBody(a,[2,5,8]);JSM.AddPolygonToBody(a,[2,7,5]);JSM.AddPolygonToBody(a,[2,10,7]);JSM.AddPolygonToBody(a,[3,5,7]);JSM.AddPolygonToBody(a,[3,7,11]);JSM.AddPolygonToBody(a,[3,9,5]);JSM.AddPolygonToBody(a,[4,8,9]);JSM.AddPolygonToBody(a,
  [5,9,8]);JSM.AddPolygonToBody(a,[6,11,10]);JSM.AddPolygonToBody(a,[7,10,11]);return a};
  JSM.GenerateTruncatedTetrahedron=function(){var a=new JSM.Body;JSM.AddVertexToBody(a,1,1,3);JSM.AddVertexToBody(a,1,-1,-3);JSM.AddVertexToBody(a,-1,-1,3);JSM.AddVertexToBody(a,-1,1,-3);JSM.AddVertexToBody(a,1,3,1);JSM.AddVertexToBody(a,1,-3,-1);JSM.AddVertexToBody(a,-1,-3,1);JSM.AddVertexToBody(a,-1,3,-1);JSM.AddVertexToBody(a,3,1,1);JSM.AddVertexToBody(a,3,-1,-1);JSM.AddVertexToBody(a,-3,-1,1);JSM.AddVertexToBody(a,-3,1,-1);JSM.AddPolygonToBody(a,[0,8,4]);JSM.AddPolygonToBody(a,[1,9,5]);JSM.AddPolygonToBody(a,
  [2,10,6]);JSM.AddPolygonToBody(a,[3,11,7]);JSM.AddPolygonToBody(a,[0,2,6,5,9,8]);JSM.AddPolygonToBody(a,[0,4,7,11,10,2]);JSM.AddPolygonToBody(a,[1,3,7,4,8,9]);JSM.AddPolygonToBody(a,[1,5,6,10,11,3]);return a};
  JSM.GenerateCuboctahedron=function(){var a=new JSM.Body;JSM.AddVertexToBody(a,1,1,0);JSM.AddVertexToBody(a,1,-1,0);JSM.AddVertexToBody(a,-1,-1,0);JSM.AddVertexToBody(a,-1,1,0);JSM.AddVertexToBody(a,1,0,1);JSM.AddVertexToBody(a,1,0,-1);JSM.AddVertexToBody(a,-1,0,1);JSM.AddVertexToBody(a,-1,0,-1);JSM.AddVertexToBody(a,0,1,1);JSM.AddVertexToBody(a,0,-1,-1);JSM.AddVertexToBody(a,0,-1,1);JSM.AddVertexToBody(a,0,1,-1);JSM.AddPolygonToBody(a,[0,5,11]);JSM.AddPolygonToBody(a,[0,8,4]);JSM.AddPolygonToBody(a,
  [1,4,10]);JSM.AddPolygonToBody(a,[1,9,5]);JSM.AddPolygonToBody(a,[2,7,9]);JSM.AddPolygonToBody(a,[2,10,6]);JSM.AddPolygonToBody(a,[3,6,8]);JSM.AddPolygonToBody(a,[3,11,7]);JSM.AddPolygonToBody(a,[0,4,1,5]);JSM.AddPolygonToBody(a,[0,11,3,8]);JSM.AddPolygonToBody(a,[1,10,2,9]);JSM.AddPolygonToBody(a,[2,6,3,7]);JSM.AddPolygonToBody(a,[4,8,6,10]);JSM.AddPolygonToBody(a,[5,9,7,11]);return a};
  JSM.GenerateTruncatedCube=function(){var a=new JSM.Body,b=Math.sqrt(2)-1;JSM.AddVertexToBody(a,1,1,+b);JSM.AddVertexToBody(a,1,1,-b);JSM.AddVertexToBody(a,1,-1,+b);JSM.AddVertexToBody(a,-1,1,+b);JSM.AddVertexToBody(a,1,-1,-b);JSM.AddVertexToBody(a,-1,1,-b);JSM.AddVertexToBody(a,-1,-1,+b);JSM.AddVertexToBody(a,-1,-1,-b);JSM.AddVertexToBody(a,1,+b,1);JSM.AddVertexToBody(a,1,+b,-1);JSM.AddVertexToBody(a,1,-b,1);JSM.AddVertexToBody(a,-1,+b,1);JSM.AddVertexToBody(a,1,-b,-1);JSM.AddVertexToBody(a,-1,+b,
  -1);JSM.AddVertexToBody(a,-1,-b,1);JSM.AddVertexToBody(a,-1,-b,-1);JSM.AddVertexToBody(a,+b,1,1);JSM.AddVertexToBody(a,+b,1,-1);JSM.AddVertexToBody(a,+b,-1,1);JSM.AddVertexToBody(a,-b,1,1);JSM.AddVertexToBody(a,+b,-1,-1);JSM.AddVertexToBody(a,-b,1,-1);JSM.AddVertexToBody(a,-b,-1,1);JSM.AddVertexToBody(a,-b,-1,-1);JSM.AddPolygonToBody(a,[0,16,8]);JSM.AddPolygonToBody(a,[1,9,17]);JSM.AddPolygonToBody(a,[2,10,18]);JSM.AddPolygonToBody(a,[3,11,19]);JSM.AddPolygonToBody(a,[4,20,12]);JSM.AddPolygonToBody(a,
  [5,21,13]);JSM.AddPolygonToBody(a,[6,22,14]);JSM.AddPolygonToBody(a,[7,15,23]);JSM.AddPolygonToBody(a,[0,1,17,21,5,3,19,16]);JSM.AddPolygonToBody(a,[0,8,10,2,4,12,9,1]);JSM.AddPolygonToBody(a,[2,18,22,6,7,23,20,4]);JSM.AddPolygonToBody(a,[3,5,13,15,7,6,14,11]);JSM.AddPolygonToBody(a,[8,16,19,11,14,22,18,10]);JSM.AddPolygonToBody(a,[9,12,20,23,15,13,21,17]);return a};
  JSM.GenerateTruncatedOctahedron=function(){var a=new JSM.Body;JSM.AddVertexToBody(a,0,1,2);JSM.AddVertexToBody(a,0,1,-2);JSM.AddVertexToBody(a,0,-1,2);JSM.AddVertexToBody(a,0,-1,-2);JSM.AddVertexToBody(a,0,2,1);JSM.AddVertexToBody(a,0,-2,1);JSM.AddVertexToBody(a,0,2,-1);JSM.AddVertexToBody(a,0,-2,-1);JSM.AddVertexToBody(a,1,0,2);JSM.AddVertexToBody(a,1,0,-2);JSM.AddVertexToBody(a,-1,0,2);JSM.AddVertexToBody(a,-1,0,-2);JSM.AddVertexToBody(a,1,2,0);JSM.AddVertexToBody(a,1,-2,0);JSM.AddVertexToBody(a,
  -1,2,0);JSM.AddVertexToBody(a,-1,-2,0);JSM.AddVertexToBody(a,2,0,1);JSM.AddVertexToBody(a,-2,0,1);JSM.AddVertexToBody(a,2,0,-1);JSM.AddVertexToBody(a,-2,0,-1);JSM.AddVertexToBody(a,2,1,0);JSM.AddVertexToBody(a,-2,1,0);JSM.AddVertexToBody(a,2,-1,0);JSM.AddVertexToBody(a,-2,-1,0);JSM.AddPolygonToBody(a,[0,10,2,8]);JSM.AddPolygonToBody(a,[1,9,3,11]);JSM.AddPolygonToBody(a,[4,12,6,14]);JSM.AddPolygonToBody(a,[5,15,7,13]);JSM.AddPolygonToBody(a,[16,22,18,20]);JSM.AddPolygonToBody(a,[17,21,19,23]);JSM.AddPolygonToBody(a,
  [0,4,14,21,17,10]);JSM.AddPolygonToBody(a,[0,8,16,20,12,4]);JSM.AddPolygonToBody(a,[1,6,12,20,18,9]);JSM.AddPolygonToBody(a,[1,11,19,21,14,6]);JSM.AddPolygonToBody(a,[2,5,13,22,16,8]);JSM.AddPolygonToBody(a,[2,10,17,23,15,5]);JSM.AddPolygonToBody(a,[3,7,15,23,19,11]);JSM.AddPolygonToBody(a,[3,9,18,22,13,7]);return a};
  JSM.GenerateRhombicuboctahedron=function(){var a=new JSM.Body,b=1+Math.sqrt(2);JSM.AddVertexToBody(a,1,1,+b);JSM.AddVertexToBody(a,1,1,-b);JSM.AddVertexToBody(a,1,-1,+b);JSM.AddVertexToBody(a,-1,1,+b);JSM.AddVertexToBody(a,1,-1,-b);JSM.AddVertexToBody(a,-1,1,-b);JSM.AddVertexToBody(a,-1,-1,+b);JSM.AddVertexToBody(a,-1,-1,-b);JSM.AddVertexToBody(a,1,+b,1);JSM.AddVertexToBody(a,1,+b,-1);JSM.AddVertexToBody(a,1,-b,1);JSM.AddVertexToBody(a,-1,+b,1);JSM.AddVertexToBody(a,1,-b,-1);JSM.AddVertexToBody(a,
  -1,+b,-1);JSM.AddVertexToBody(a,-1,-b,1);JSM.AddVertexToBody(a,-1,-b,-1);JSM.AddVertexToBody(a,+b,1,1);JSM.AddVertexToBody(a,+b,1,-1);JSM.AddVertexToBody(a,+b,-1,1);JSM.AddVertexToBody(a,-b,1,1);JSM.AddVertexToBody(a,+b,-1,-1);JSM.AddVertexToBody(a,-b,1,-1);JSM.AddVertexToBody(a,-b,-1,1);JSM.AddVertexToBody(a,-b,-1,-1);JSM.AddPolygonToBody(a,[0,16,8]);JSM.AddPolygonToBody(a,[1,9,17]);JSM.AddPolygonToBody(a,[2,10,18]);JSM.AddPolygonToBody(a,[3,11,19]);JSM.AddPolygonToBody(a,[4,20,12]);JSM.AddPolygonToBody(a,
  [5,21,13]);JSM.AddPolygonToBody(a,[6,22,14]);JSM.AddPolygonToBody(a,[7,15,23]);JSM.AddPolygonToBody(a,[0,2,18,16]);JSM.AddPolygonToBody(a,[0,3,6,2]);JSM.AddPolygonToBody(a,[0,8,11,3]);JSM.AddPolygonToBody(a,[1,4,7,5]);JSM.AddPolygonToBody(a,[1,5,13,9]);JSM.AddPolygonToBody(a,[1,17,20,4]);JSM.AddPolygonToBody(a,[2,6,14,10]);JSM.AddPolygonToBody(a,[3,19,22,6]);JSM.AddPolygonToBody(a,[4,12,15,7]);JSM.AddPolygonToBody(a,[5,7,23,21]);JSM.AddPolygonToBody(a,[8,9,13,11]);JSM.AddPolygonToBody(a,[8,16,17,
  9]);JSM.AddPolygonToBody(a,[10,12,20,18]);JSM.AddPolygonToBody(a,[10,14,15,12]);JSM.AddPolygonToBody(a,[11,13,21,19]);JSM.AddPolygonToBody(a,[14,22,23,15]);JSM.AddPolygonToBody(a,[16,18,20,17]);JSM.AddPolygonToBody(a,[19,21,23,22]);return a};
  JSM.GenerateTruncatedCuboctahedron=function(){var a=new JSM.Body,b=1+Math.sqrt(2),c=1+2*Math.sqrt(2);JSM.AddVertexToBody(a,1,+b,+c);JSM.AddVertexToBody(a,1,+b,-c);JSM.AddVertexToBody(a,1,-b,+c);JSM.AddVertexToBody(a,-1,+b,+c);JSM.AddVertexToBody(a,1,-b,-c);JSM.AddVertexToBody(a,-1,+b,-c);JSM.AddVertexToBody(a,-1,-b,+c);JSM.AddVertexToBody(a,-1,-b,-c);JSM.AddVertexToBody(a,1,+c,+b);JSM.AddVertexToBody(a,1,-c,+b);JSM.AddVertexToBody(a,1,+c,-b);JSM.AddVertexToBody(a,-1,+c,+b);JSM.AddVertexToBody(a,1,
  -c,-b);JSM.AddVertexToBody(a,-1,-c,+b);JSM.AddVertexToBody(a,-1,+c,-b);JSM.AddVertexToBody(a,-1,-c,-b);JSM.AddVertexToBody(a,+b,1,+c);JSM.AddVertexToBody(a,+b,1,-c);JSM.AddVertexToBody(a,-b,1,+c);JSM.AddVertexToBody(a,+b,-1,+c);JSM.AddVertexToBody(a,-b,1,-c);JSM.AddVertexToBody(a,+b,-1,-c);JSM.AddVertexToBody(a,-b,-1,+c);JSM.AddVertexToBody(a,-b,-1,-c);JSM.AddVertexToBody(a,+b,+c,1);JSM.AddVertexToBody(a,+b,-c,1);JSM.AddVertexToBody(a,-b,+c,1);JSM.AddVertexToBody(a,+b,+c,-1);JSM.AddVertexToBody(a,
  -b,-c,1);JSM.AddVertexToBody(a,+b,-c,-1);JSM.AddVertexToBody(a,-b,+c,-1);JSM.AddVertexToBody(a,-b,-c,-1);JSM.AddVertexToBody(a,+c,1,+b);JSM.AddVertexToBody(a,-c,1,+b);JSM.AddVertexToBody(a,+c,1,-b);JSM.AddVertexToBody(a,+c,-1,+b);JSM.AddVertexToBody(a,-c,1,-b);JSM.AddVertexToBody(a,-c,-1,+b);JSM.AddVertexToBody(a,+c,-1,-b);JSM.AddVertexToBody(a,-c,-1,-b);JSM.AddVertexToBody(a,+c,+b,1);JSM.AddVertexToBody(a,-c,+b,1);JSM.AddVertexToBody(a,+c,-b,1);JSM.AddVertexToBody(a,+c,+b,-1);JSM.AddVertexToBody(a,
  -c,-b,1);JSM.AddVertexToBody(a,-c,+b,-1);JSM.AddVertexToBody(a,+c,-b,-1);JSM.AddVertexToBody(a,-c,-b,-1);JSM.AddPolygonToBody(a,[0,8,11,3]);JSM.AddPolygonToBody(a,[1,5,14,10]);JSM.AddPolygonToBody(a,[2,6,13,9]);JSM.AddPolygonToBody(a,[4,12,15,7]);JSM.AddPolygonToBody(a,[16,19,35,32]);JSM.AddPolygonToBody(a,[17,34,38,21]);JSM.AddPolygonToBody(a,[18,33,37,22]);JSM.AddPolygonToBody(a,[23,39,36,20]);JSM.AddPolygonToBody(a,[24,40,43,27]);JSM.AddPolygonToBody(a,[25,29,46,42]);JSM.AddPolygonToBody(a,[26,
  30,45,41]);JSM.AddPolygonToBody(a,[28,44,47,31]);JSM.AddPolygonToBody(a,[0,16,32,40,24,8]);JSM.AddPolygonToBody(a,[1,10,27,43,34,17]);JSM.AddPolygonToBody(a,[2,9,25,42,35,19]);JSM.AddPolygonToBody(a,[3,11,26,41,33,18]);JSM.AddPolygonToBody(a,[4,21,38,46,29,12]);JSM.AddPolygonToBody(a,[5,20,36,45,30,14]);JSM.AddPolygonToBody(a,[6,22,37,44,28,13]);JSM.AddPolygonToBody(a,[7,15,31,47,39,23]);JSM.AddPolygonToBody(a,[0,3,18,22,6,2,19,16]);JSM.AddPolygonToBody(a,[1,17,21,4,7,23,20,5]);JSM.AddPolygonToBody(a,
  [8,24,27,10,14,30,26,11]);JSM.AddPolygonToBody(a,[9,13,28,31,15,12,29,25]);JSM.AddPolygonToBody(a,[32,35,42,46,38,34,43,40]);JSM.AddPolygonToBody(a,[33,41,45,36,39,47,44,37]);return a};
  JSM.GenerateSnubCube=function(){var a=new JSM.Body,b=1/3*(Math.pow(17+3*Math.sqrt(33),1/3)-Math.pow(-17+3*Math.sqrt(33),1/3)-1),c=1/b;JSM.AddVertexToBody(a,1,+b,-c);JSM.AddVertexToBody(a,1,-b,+c);JSM.AddVertexToBody(a,-1,+b,+c);JSM.AddVertexToBody(a,-1,-b,-c);JSM.AddVertexToBody(a,+b,-c,1);JSM.AddVertexToBody(a,-b,+c,1);JSM.AddVertexToBody(a,+b,+c,-1);JSM.AddVertexToBody(a,-b,-c,-1);JSM.AddVertexToBody(a,-c,1,+b);JSM.AddVertexToBody(a,+c,1,-b);JSM.AddVertexToBody(a,+c,-1,+b);JSM.AddVertexToBody(a,
  -c,-1,-b);JSM.AddVertexToBody(a,1,+c,+b);JSM.AddVertexToBody(a,1,-c,-b);JSM.AddVertexToBody(a,-1,+c,-b);JSM.AddVertexToBody(a,-1,-c,+b);JSM.AddVertexToBody(a,+b,1,+c);JSM.AddVertexToBody(a,-b,1,-c);JSM.AddVertexToBody(a,-b,-1,+c);JSM.AddVertexToBody(a,+b,-1,-c);JSM.AddVertexToBody(a,+c,+b,1);JSM.AddVertexToBody(a,-c,-b,1);JSM.AddVertexToBody(a,+c,-b,-1);JSM.AddVertexToBody(a,-c,+b,-1);JSM.AddPolygonToBody(a,[0,6,9]);JSM.AddPolygonToBody(a,[0,9,22]);JSM.AddPolygonToBody(a,[0,17,6]);JSM.AddPolygonToBody(a,
  [0,22,19]);JSM.AddPolygonToBody(a,[1,4,10]);JSM.AddPolygonToBody(a,[1,10,20]);JSM.AddPolygonToBody(a,[1,18,4]);JSM.AddPolygonToBody(a,[1,20,16]);JSM.AddPolygonToBody(a,[2,5,8]);JSM.AddPolygonToBody(a,[2,8,21]);JSM.AddPolygonToBody(a,[2,16,5]);JSM.AddPolygonToBody(a,[2,21,18]);JSM.AddPolygonToBody(a,[3,7,11]);JSM.AddPolygonToBody(a,[3,11,23]);JSM.AddPolygonToBody(a,[3,19,7]);JSM.AddPolygonToBody(a,[3,23,17]);JSM.AddPolygonToBody(a,[4,13,10]);JSM.AddPolygonToBody(a,[4,18,15]);JSM.AddPolygonToBody(a,
  [5,14,8]);JSM.AddPolygonToBody(a,[5,16,12]);JSM.AddPolygonToBody(a,[6,12,9]);JSM.AddPolygonToBody(a,[6,17,14]);JSM.AddPolygonToBody(a,[7,15,11]);JSM.AddPolygonToBody(a,[7,19,13]);JSM.AddPolygonToBody(a,[8,14,23]);JSM.AddPolygonToBody(a,[9,12,20]);JSM.AddPolygonToBody(a,[10,13,22]);JSM.AddPolygonToBody(a,[11,15,21]);JSM.AddPolygonToBody(a,[12,16,20]);JSM.AddPolygonToBody(a,[13,19,22]);JSM.AddPolygonToBody(a,[14,17,23]);JSM.AddPolygonToBody(a,[15,18,21]);JSM.AddPolygonToBody(a,[0,19,3,17]);JSM.AddPolygonToBody(a,
  [1,16,2,18]);JSM.AddPolygonToBody(a,[4,15,7,13]);JSM.AddPolygonToBody(a,[5,12,6,14]);JSM.AddPolygonToBody(a,[8,23,11,21]);JSM.AddPolygonToBody(a,[9,20,10,22]);return a};
  JSM.GenerateIcosidodecahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=b/2,d=(1+b)/2;JSM.AddVertexToBody(a,0,0,+b);JSM.AddVertexToBody(a,0,0,-b);JSM.AddVertexToBody(a,0,+b,0);JSM.AddVertexToBody(a,0,-b,0);JSM.AddVertexToBody(a,+b,0,0);JSM.AddVertexToBody(a,-b,0,0);JSM.AddVertexToBody(a,0.5,+c,+d);JSM.AddVertexToBody(a,0.5,+c,-d);JSM.AddVertexToBody(a,0.5,-c,+d);JSM.AddVertexToBody(a,-0.5,+c,+d);JSM.AddVertexToBody(a,0.5,-c,-d);JSM.AddVertexToBody(a,-0.5,+c,-d);JSM.AddVertexToBody(a,-0.5,
  -c,+d);JSM.AddVertexToBody(a,-0.5,-c,-d);JSM.AddVertexToBody(a,+c,+d,0.5);JSM.AddVertexToBody(a,+c,-d,0.5);JSM.AddVertexToBody(a,-c,+d,0.5);JSM.AddVertexToBody(a,+c,+d,-0.5);JSM.AddVertexToBody(a,-c,-d,0.5);JSM.AddVertexToBody(a,+c,-d,-0.5);JSM.AddVertexToBody(a,-c,+d,-0.5);JSM.AddVertexToBody(a,-c,-d,-0.5);JSM.AddVertexToBody(a,+d,0.5,+c);JSM.AddVertexToBody(a,-d,0.5,+c);JSM.AddVertexToBody(a,+d,0.5,-c);JSM.AddVertexToBody(a,+d,-0.5,+c);JSM.AddVertexToBody(a,-d,0.5,-c);JSM.AddVertexToBody(a,-d,-0.5,
  +c);JSM.AddVertexToBody(a,+d,-0.5,-c);JSM.AddVertexToBody(a,-d,-0.5,-c);JSM.AddPolygonToBody(a,[0,6,9]);JSM.AddPolygonToBody(a,[0,12,8]);JSM.AddPolygonToBody(a,[1,10,13]);JSM.AddPolygonToBody(a,[1,11,7]);JSM.AddPolygonToBody(a,[2,14,17]);JSM.AddPolygonToBody(a,[2,20,16]);JSM.AddPolygonToBody(a,[3,18,21]);JSM.AddPolygonToBody(a,[3,19,15]);JSM.AddPolygonToBody(a,[4,22,25]);JSM.AddPolygonToBody(a,[4,28,24]);JSM.AddPolygonToBody(a,[5,26,29]);JSM.AddPolygonToBody(a,[5,27,23]);JSM.AddPolygonToBody(a,[6,
  22,14]);JSM.AddPolygonToBody(a,[7,17,24]);JSM.AddPolygonToBody(a,[8,15,25]);JSM.AddPolygonToBody(a,[9,16,23]);JSM.AddPolygonToBody(a,[10,28,19]);JSM.AddPolygonToBody(a,[11,26,20]);JSM.AddPolygonToBody(a,[12,27,18]);JSM.AddPolygonToBody(a,[13,21,29]);JSM.AddPolygonToBody(a,[0,8,25,22,6]);JSM.AddPolygonToBody(a,[0,9,23,27,12]);JSM.AddPolygonToBody(a,[1,7,24,28,10]);JSM.AddPolygonToBody(a,[1,13,29,26,11]);JSM.AddPolygonToBody(a,[2,16,9,6,14]);JSM.AddPolygonToBody(a,[2,17,7,11,20]);JSM.AddPolygonToBody(a,
  [3,15,8,12,18]);JSM.AddPolygonToBody(a,[3,21,13,10,19]);JSM.AddPolygonToBody(a,[4,24,17,14,22]);JSM.AddPolygonToBody(a,[4,25,15,19,28]);JSM.AddPolygonToBody(a,[5,23,16,20,26]);JSM.AddPolygonToBody(a,[5,29,21,18,27]);return a};
  JSM.GenerateTruncatedDodecahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=1/b,d=2+b,e=2*b,f=Math.pow(b,2);JSM.AddVertexToBody(a,0,+c,+d);JSM.AddVertexToBody(a,0,+c,-d);JSM.AddVertexToBody(a,0,-c,+d);JSM.AddVertexToBody(a,0,-c,-d);JSM.AddVertexToBody(a,+d,0,+c);JSM.AddVertexToBody(a,-d,0,+c);JSM.AddVertexToBody(a,+d,0,-c);JSM.AddVertexToBody(a,-d,0,-c);JSM.AddVertexToBody(a,+c,+d,0);JSM.AddVertexToBody(a,+c,-d,0);JSM.AddVertexToBody(a,-c,+d,0);JSM.AddVertexToBody(a,-c,-d,0);JSM.AddVertexToBody(a,
  +c,+b,+e);JSM.AddVertexToBody(a,+c,+b,-e);JSM.AddVertexToBody(a,+c,-b,+e);JSM.AddVertexToBody(a,-c,+b,+e);JSM.AddVertexToBody(a,+c,-b,-e);JSM.AddVertexToBody(a,-c,+b,-e);JSM.AddVertexToBody(a,-c,-b,+e);JSM.AddVertexToBody(a,-c,-b,-e);JSM.AddVertexToBody(a,+e,+c,+b);JSM.AddVertexToBody(a,+e,+c,-b);JSM.AddVertexToBody(a,+e,-c,+b);JSM.AddVertexToBody(a,-e,+c,+b);JSM.AddVertexToBody(a,+e,-c,-b);JSM.AddVertexToBody(a,-e,+c,-b);JSM.AddVertexToBody(a,-e,-c,+b);JSM.AddVertexToBody(a,-e,-c,-b);JSM.AddVertexToBody(a,
  +b,+e,+c);JSM.AddVertexToBody(a,+b,+e,-c);JSM.AddVertexToBody(a,+b,-e,+c);JSM.AddVertexToBody(a,-b,+e,+c);JSM.AddVertexToBody(a,+b,-e,-c);JSM.AddVertexToBody(a,-b,+e,-c);JSM.AddVertexToBody(a,-b,-e,+c);JSM.AddVertexToBody(a,-b,-e,-c);JSM.AddVertexToBody(a,+b,2,+f);JSM.AddVertexToBody(a,+b,2,-f);JSM.AddVertexToBody(a,+b,-2,+f);JSM.AddVertexToBody(a,-b,2,+f);JSM.AddVertexToBody(a,+b,-2,-f);JSM.AddVertexToBody(a,-b,2,-f);JSM.AddVertexToBody(a,-b,-2,+f);JSM.AddVertexToBody(a,-b,-2,-f);JSM.AddVertexToBody(a,
  +f,+b,2);JSM.AddVertexToBody(a,+f,+b,-2);JSM.AddVertexToBody(a,+f,-b,2);JSM.AddVertexToBody(a,-f,+b,2);JSM.AddVertexToBody(a,+f,-b,-2);JSM.AddVertexToBody(a,-f,+b,-2);JSM.AddVertexToBody(a,-f,-b,2);JSM.AddVertexToBody(a,-f,-b,-2);JSM.AddVertexToBody(a,2,+f,+b);JSM.AddVertexToBody(a,2,+f,-b);JSM.AddVertexToBody(a,2,-f,+b);JSM.AddVertexToBody(a,-2,+f,+b);JSM.AddVertexToBody(a,2,-f,-b);JSM.AddVertexToBody(a,-2,+f,-b);JSM.AddVertexToBody(a,-2,-f,+b);JSM.AddVertexToBody(a,-2,-f,-b);JSM.AddPolygonToBody(a,
  [0,12,15]);JSM.AddPolygonToBody(a,[1,17,13]);JSM.AddPolygonToBody(a,[2,18,14]);JSM.AddPolygonToBody(a,[3,16,19]);JSM.AddPolygonToBody(a,[4,20,22]);JSM.AddPolygonToBody(a,[5,26,23]);JSM.AddPolygonToBody(a,[6,24,21]);JSM.AddPolygonToBody(a,[7,25,27]);JSM.AddPolygonToBody(a,[8,28,29]);JSM.AddPolygonToBody(a,[9,32,30]);JSM.AddPolygonToBody(a,[10,33,31]);JSM.AddPolygonToBody(a,[11,34,35]);JSM.AddPolygonToBody(a,[36,44,52]);JSM.AddPolygonToBody(a,[37,53,45]);JSM.AddPolygonToBody(a,[38,54,46]);JSM.AddPolygonToBody(a,
  [39,55,47]);JSM.AddPolygonToBody(a,[40,48,56]);JSM.AddPolygonToBody(a,[41,49,57]);JSM.AddPolygonToBody(a,[42,50,58]);JSM.AddPolygonToBody(a,[43,59,51]);JSM.AddPolygonToBody(a,[0,2,14,38,46,22,20,44,36,12]);JSM.AddPolygonToBody(a,[0,15,39,47,23,26,50,42,18,2]);JSM.AddPolygonToBody(a,[1,3,19,43,51,27,25,49,41,17]);JSM.AddPolygonToBody(a,[1,13,37,45,21,24,48,40,16,3]);JSM.AddPolygonToBody(a,[4,6,21,45,53,29,28,52,44,20]);JSM.AddPolygonToBody(a,[4,22,46,54,30,32,56,48,24,6]);JSM.AddPolygonToBody(a,[5,
  7,27,51,59,35,34,58,50,26]);JSM.AddPolygonToBody(a,[5,23,47,55,31,33,57,49,25,7]);JSM.AddPolygonToBody(a,[8,10,31,55,39,15,12,36,52,28]);JSM.AddPolygonToBody(a,[8,29,53,37,13,17,41,57,33,10]);JSM.AddPolygonToBody(a,[9,11,35,59,43,19,16,40,56,32]);JSM.AddPolygonToBody(a,[9,30,54,38,14,18,42,58,34,11]);return a};
  JSM.GenerateTruncatedIcosahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=3*b,d=1+2*b,e=2+b,f=2*b;JSM.AddVertexToBody(a,0,1,+c);JSM.AddVertexToBody(a,0,1,-c);JSM.AddVertexToBody(a,0,-1,+c);JSM.AddVertexToBody(a,0,-1,-c);JSM.AddVertexToBody(a,1,+c,0);JSM.AddVertexToBody(a,1,-c,0);JSM.AddVertexToBody(a,-1,+c,0);JSM.AddVertexToBody(a,-1,-c,0);JSM.AddVertexToBody(a,+c,0,1);JSM.AddVertexToBody(a,-c,0,1);JSM.AddVertexToBody(a,+c,0,-1);JSM.AddVertexToBody(a,-c,0,-1);JSM.AddVertexToBody(a,2,+d,
  +b);JSM.AddVertexToBody(a,2,+d,-b);JSM.AddVertexToBody(a,2,-d,+b);JSM.AddVertexToBody(a,-2,+d,+b);JSM.AddVertexToBody(a,2,-d,-b);JSM.AddVertexToBody(a,-2,+d,-b);JSM.AddVertexToBody(a,-2,-d,+b);JSM.AddVertexToBody(a,-2,-d,-b);JSM.AddVertexToBody(a,+d,+b,2);JSM.AddVertexToBody(a,+d,-b,2);JSM.AddVertexToBody(a,-d,+b,2);JSM.AddVertexToBody(a,+d,+b,-2);JSM.AddVertexToBody(a,-d,-b,2);JSM.AddVertexToBody(a,+d,-b,-2);JSM.AddVertexToBody(a,-d,+b,-2);JSM.AddVertexToBody(a,-d,-b,-2);JSM.AddVertexToBody(a,+b,
  2,+d);JSM.AddVertexToBody(a,-b,2,+d);JSM.AddVertexToBody(a,+b,2,-d);JSM.AddVertexToBody(a,+b,-2,+d);JSM.AddVertexToBody(a,-b,2,-d);JSM.AddVertexToBody(a,-b,-2,+d);JSM.AddVertexToBody(a,+b,-2,-d);JSM.AddVertexToBody(a,-b,-2,-d);JSM.AddVertexToBody(a,1,+e,+f);JSM.AddVertexToBody(a,1,+e,-f);JSM.AddVertexToBody(a,1,-e,+f);JSM.AddVertexToBody(a,-1,+e,+f);JSM.AddVertexToBody(a,1,-e,-f);JSM.AddVertexToBody(a,-1,+e,-f);JSM.AddVertexToBody(a,-1,-e,+f);JSM.AddVertexToBody(a,-1,-e,-f);JSM.AddVertexToBody(a,
  +e,+f,1);JSM.AddVertexToBody(a,+e,-f,1);JSM.AddVertexToBody(a,-e,+f,1);JSM.AddVertexToBody(a,+e,+f,-1);JSM.AddVertexToBody(a,-e,-f,1);JSM.AddVertexToBody(a,+e,-f,-1);JSM.AddVertexToBody(a,-e,+f,-1);JSM.AddVertexToBody(a,-e,-f,-1);JSM.AddVertexToBody(a,+f,1,+e);JSM.AddVertexToBody(a,-f,1,+e);JSM.AddVertexToBody(a,+f,1,-e);JSM.AddVertexToBody(a,+f,-1,+e);JSM.AddVertexToBody(a,-f,1,-e);JSM.AddVertexToBody(a,-f,-1,+e);JSM.AddVertexToBody(a,+f,-1,-e);JSM.AddVertexToBody(a,-f,-1,-e);JSM.AddPolygonToBody(a,
  [0,28,36,39,29]);JSM.AddPolygonToBody(a,[1,32,41,37,30]);JSM.AddPolygonToBody(a,[2,33,42,38,31]);JSM.AddPolygonToBody(a,[3,34,40,43,35]);JSM.AddPolygonToBody(a,[4,12,44,47,13]);JSM.AddPolygonToBody(a,[5,16,49,45,14]);JSM.AddPolygonToBody(a,[6,17,50,46,15]);JSM.AddPolygonToBody(a,[7,18,48,51,19]);JSM.AddPolygonToBody(a,[8,20,52,55,21]);JSM.AddPolygonToBody(a,[9,24,57,53,22]);JSM.AddPolygonToBody(a,[10,25,58,54,23]);JSM.AddPolygonToBody(a,[11,26,56,59,27]);JSM.AddPolygonToBody(a,[0,2,31,55,52,28]);
  JSM.AddPolygonToBody(a,[0,29,53,57,33,2]);JSM.AddPolygonToBody(a,[1,3,35,59,56,32]);JSM.AddPolygonToBody(a,[1,30,54,58,34,3]);JSM.AddPolygonToBody(a,[4,6,15,39,36,12]);JSM.AddPolygonToBody(a,[4,13,37,41,17,6]);JSM.AddPolygonToBody(a,[5,7,19,43,40,16]);JSM.AddPolygonToBody(a,[5,14,38,42,18,7]);JSM.AddPolygonToBody(a,[8,10,23,47,44,20]);JSM.AddPolygonToBody(a,[8,21,45,49,25,10]);JSM.AddPolygonToBody(a,[9,11,27,51,48,24]);JSM.AddPolygonToBody(a,[9,22,46,50,26,11]);JSM.AddPolygonToBody(a,[12,36,28,52,
  20,44]);JSM.AddPolygonToBody(a,[13,47,23,54,30,37]);JSM.AddPolygonToBody(a,[14,45,21,55,31,38]);JSM.AddPolygonToBody(a,[15,46,22,53,29,39]);JSM.AddPolygonToBody(a,[16,40,34,58,25,49]);JSM.AddPolygonToBody(a,[17,41,32,56,26,50]);JSM.AddPolygonToBody(a,[18,42,33,57,24,48]);JSM.AddPolygonToBody(a,[19,51,27,59,35,43]);return a};
  JSM.GenerateRhombicosidodecahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=Math.pow(b,2),d=Math.pow(b,3),e=2*b,f=2+b;JSM.AddVertexToBody(a,1,1,+d);JSM.AddVertexToBody(a,1,1,-d);JSM.AddVertexToBody(a,1,-1,+d);JSM.AddVertexToBody(a,-1,1,+d);JSM.AddVertexToBody(a,1,-1,-d);JSM.AddVertexToBody(a,-1,1,-d);JSM.AddVertexToBody(a,-1,-1,+d);JSM.AddVertexToBody(a,-1,-1,-d);JSM.AddVertexToBody(a,+d,1,1);JSM.AddVertexToBody(a,+d,1,-1);JSM.AddVertexToBody(a,+d,-1,1);JSM.AddVertexToBody(a,-d,1,1);JSM.AddVertexToBody(a,
  +d,-1,-1);JSM.AddVertexToBody(a,-d,1,-1);JSM.AddVertexToBody(a,-d,-1,1);JSM.AddVertexToBody(a,-d,-1,-1);JSM.AddVertexToBody(a,1,+d,1);JSM.AddVertexToBody(a,1,+d,-1);JSM.AddVertexToBody(a,1,-d,1);JSM.AddVertexToBody(a,-1,+d,1);JSM.AddVertexToBody(a,1,-d,-1);JSM.AddVertexToBody(a,-1,+d,-1);JSM.AddVertexToBody(a,-1,-d,1);JSM.AddVertexToBody(a,-1,-d,-1);JSM.AddVertexToBody(a,+c,+b,+e);JSM.AddVertexToBody(a,+c,+b,-e);JSM.AddVertexToBody(a,+c,-b,+e);JSM.AddVertexToBody(a,-c,+b,+e);JSM.AddVertexToBody(a,
  +c,-b,-e);JSM.AddVertexToBody(a,-c,+b,-e);JSM.AddVertexToBody(a,-c,-b,+e);JSM.AddVertexToBody(a,-c,-b,-e);JSM.AddVertexToBody(a,+e,+c,+b);JSM.AddVertexToBody(a,+e,+c,-b);JSM.AddVertexToBody(a,+e,-c,+b);JSM.AddVertexToBody(a,-e,+c,+b);JSM.AddVertexToBody(a,+e,-c,-b);JSM.AddVertexToBody(a,-e,+c,-b);JSM.AddVertexToBody(a,-e,-c,+b);JSM.AddVertexToBody(a,-e,-c,-b);JSM.AddVertexToBody(a,+b,+e,+c);JSM.AddVertexToBody(a,+b,+e,-c);JSM.AddVertexToBody(a,+b,-e,+c);JSM.AddVertexToBody(a,-b,+e,+c);JSM.AddVertexToBody(a,
  +b,-e,-c);JSM.AddVertexToBody(a,-b,+e,-c);JSM.AddVertexToBody(a,-b,-e,+c);JSM.AddVertexToBody(a,-b,-e,-c);JSM.AddVertexToBody(a,+f,0,+c);JSM.AddVertexToBody(a,+f,0,-c);JSM.AddVertexToBody(a,-f,0,+c);JSM.AddVertexToBody(a,-f,0,-c);JSM.AddVertexToBody(a,+c,+f,0);JSM.AddVertexToBody(a,-c,+f,0);JSM.AddVertexToBody(a,+c,-f,0);JSM.AddVertexToBody(a,-c,-f,0);JSM.AddVertexToBody(a,0,+c,+f);JSM.AddVertexToBody(a,0,-c,+f);JSM.AddVertexToBody(a,0,+c,-f);JSM.AddVertexToBody(a,0,-c,-f);JSM.AddPolygonToBody(a,
  [0,56,3]);JSM.AddPolygonToBody(a,[1,5,58]);JSM.AddPolygonToBody(a,[2,6,57]);JSM.AddPolygonToBody(a,[4,59,7]);JSM.AddPolygonToBody(a,[8,48,10]);JSM.AddPolygonToBody(a,[9,12,49]);JSM.AddPolygonToBody(a,[11,14,50]);JSM.AddPolygonToBody(a,[13,51,15]);JSM.AddPolygonToBody(a,[16,52,17]);JSM.AddPolygonToBody(a,[18,20,54]);JSM.AddPolygonToBody(a,[19,21,53]);JSM.AddPolygonToBody(a,[22,55,23]);JSM.AddPolygonToBody(a,[24,32,40]);JSM.AddPolygonToBody(a,[25,41,33]);JSM.AddPolygonToBody(a,[26,42,34]);JSM.AddPolygonToBody(a,
  [27,43,35]);JSM.AddPolygonToBody(a,[28,36,44]);JSM.AddPolygonToBody(a,[29,37,45]);JSM.AddPolygonToBody(a,[30,38,46]);JSM.AddPolygonToBody(a,[31,47,39]);JSM.AddPolygonToBody(a,[0,3,6,2]);JSM.AddPolygonToBody(a,[0,24,40,56]);JSM.AddPolygonToBody(a,[1,4,7,5]);JSM.AddPolygonToBody(a,[1,58,41,25]);JSM.AddPolygonToBody(a,[2,57,42,26]);JSM.AddPolygonToBody(a,[3,56,43,27]);JSM.AddPolygonToBody(a,[4,28,44,59]);JSM.AddPolygonToBody(a,[5,29,45,58]);JSM.AddPolygonToBody(a,[6,30,46,57]);JSM.AddPolygonToBody(a,
  [7,59,47,31]);JSM.AddPolygonToBody(a,[8,10,12,9]);JSM.AddPolygonToBody(a,[8,32,24,48]);JSM.AddPolygonToBody(a,[9,49,25,33]);JSM.AddPolygonToBody(a,[10,48,26,34]);JSM.AddPolygonToBody(a,[11,13,15,14]);JSM.AddPolygonToBody(a,[11,50,27,35]);JSM.AddPolygonToBody(a,[12,36,28,49]);JSM.AddPolygonToBody(a,[13,37,29,51]);JSM.AddPolygonToBody(a,[14,38,30,50]);JSM.AddPolygonToBody(a,[15,51,31,39]);JSM.AddPolygonToBody(a,[16,17,21,19]);JSM.AddPolygonToBody(a,[16,40,32,52]);JSM.AddPolygonToBody(a,[17,52,33,41]);
  JSM.AddPolygonToBody(a,[18,22,23,20]);JSM.AddPolygonToBody(a,[18,54,34,42]);JSM.AddPolygonToBody(a,[19,53,35,43]);JSM.AddPolygonToBody(a,[20,44,36,54]);JSM.AddPolygonToBody(a,[21,45,37,53]);JSM.AddPolygonToBody(a,[22,46,38,55]);JSM.AddPolygonToBody(a,[23,55,39,47]);JSM.AddPolygonToBody(a,[0,2,26,48,24]);JSM.AddPolygonToBody(a,[1,25,49,28,4]);JSM.AddPolygonToBody(a,[3,27,50,30,6]);JSM.AddPolygonToBody(a,[5,7,31,51,29]);JSM.AddPolygonToBody(a,[8,9,33,52,32]);JSM.AddPolygonToBody(a,[10,34,54,36,12]);
  JSM.AddPolygonToBody(a,[11,35,53,37,13]);JSM.AddPolygonToBody(a,[14,15,39,55,38]);JSM.AddPolygonToBody(a,[16,19,43,56,40]);JSM.AddPolygonToBody(a,[17,41,58,45,21]);JSM.AddPolygonToBody(a,[18,42,57,46,22]);JSM.AddPolygonToBody(a,[20,23,47,59,44]);return a};
  JSM.GenerateTruncatedIcosidodecahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=1/b,d=3+b,e=2/b,f=1+2*b,g=Math.pow(b,2),h=-1+3*b,l=-1+2*b,k=2+b,m=2*b;JSM.AddVertexToBody(a,+c,+c,+d);JSM.AddVertexToBody(a,+c,+c,-d);JSM.AddVertexToBody(a,+c,-c,+d);JSM.AddVertexToBody(a,-c,+c,+d);JSM.AddVertexToBody(a,+c,-c,-d);JSM.AddVertexToBody(a,-c,+c,-d);JSM.AddVertexToBody(a,-c,-c,+d);JSM.AddVertexToBody(a,-c,-c,-d);JSM.AddVertexToBody(a,+c,+d,+c);JSM.AddVertexToBody(a,+c,-d,+c);JSM.AddVertexToBody(a,
  -c,+d,+c);JSM.AddVertexToBody(a,+c,+d,-c);JSM.AddVertexToBody(a,-c,-d,+c);JSM.AddVertexToBody(a,+c,-d,-c);JSM.AddVertexToBody(a,-c,+d,-c);JSM.AddVertexToBody(a,-c,-d,-c);JSM.AddVertexToBody(a,+d,+c,+c);JSM.AddVertexToBody(a,-d,+c,+c);JSM.AddVertexToBody(a,+d,+c,-c);JSM.AddVertexToBody(a,+d,-c,+c);JSM.AddVertexToBody(a,-d,+c,-c);JSM.AddVertexToBody(a,-d,-c,+c);JSM.AddVertexToBody(a,+d,-c,-c);JSM.AddVertexToBody(a,-d,-c,-c);JSM.AddVertexToBody(a,+e,+b,+f);JSM.AddVertexToBody(a,+e,+b,-f);JSM.AddVertexToBody(a,
  +e,-b,+f);JSM.AddVertexToBody(a,-e,+b,+f);JSM.AddVertexToBody(a,+e,-b,-f);JSM.AddVertexToBody(a,-e,+b,-f);JSM.AddVertexToBody(a,-e,-b,+f);JSM.AddVertexToBody(a,-e,-b,-f);JSM.AddVertexToBody(a,+b,+f,+e);JSM.AddVertexToBody(a,+b,-f,+e);JSM.AddVertexToBody(a,-b,+f,+e);JSM.AddVertexToBody(a,+b,+f,-e);JSM.AddVertexToBody(a,-b,-f,+e);JSM.AddVertexToBody(a,+b,-f,-e);JSM.AddVertexToBody(a,-b,+f,-e);JSM.AddVertexToBody(a,-b,-f,-e);JSM.AddVertexToBody(a,+f,+e,+b);JSM.AddVertexToBody(a,-f,+e,+b);JSM.AddVertexToBody(a,
  +f,+e,-b);JSM.AddVertexToBody(a,+f,-e,+b);JSM.AddVertexToBody(a,-f,+e,-b);JSM.AddVertexToBody(a,-f,-e,+b);JSM.AddVertexToBody(a,+f,-e,-b);JSM.AddVertexToBody(a,-f,-e,-b);JSM.AddVertexToBody(a,+c,+g,+h);JSM.AddVertexToBody(a,+c,+g,-h);JSM.AddVertexToBody(a,+c,-g,+h);JSM.AddVertexToBody(a,-c,+g,+h);JSM.AddVertexToBody(a,+c,-g,-h);JSM.AddVertexToBody(a,-c,+g,-h);JSM.AddVertexToBody(a,-c,-g,+h);JSM.AddVertexToBody(a,-c,-g,-h);JSM.AddVertexToBody(a,+g,+h,+c);JSM.AddVertexToBody(a,+g,-h,+c);JSM.AddVertexToBody(a,
  -g,+h,+c);JSM.AddVertexToBody(a,+g,+h,-c);JSM.AddVertexToBody(a,-g,-h,+c);JSM.AddVertexToBody(a,+g,-h,-c);JSM.AddVertexToBody(a,-g,+h,-c);JSM.AddVertexToBody(a,-g,-h,-c);JSM.AddVertexToBody(a,+h,+c,+g);JSM.AddVertexToBody(a,-h,+c,+g);JSM.AddVertexToBody(a,+h,+c,-g);JSM.AddVertexToBody(a,+h,-c,+g);JSM.AddVertexToBody(a,-h,+c,-g);JSM.AddVertexToBody(a,-h,-c,+g);JSM.AddVertexToBody(a,+h,-c,-g);JSM.AddVertexToBody(a,-h,-c,-g);JSM.AddVertexToBody(a,+l,2,+k);JSM.AddVertexToBody(a,+l,2,-k);JSM.AddVertexToBody(a,
  +l,-2,+k);JSM.AddVertexToBody(a,-l,2,+k);JSM.AddVertexToBody(a,+l,-2,-k);JSM.AddVertexToBody(a,-l,2,-k);JSM.AddVertexToBody(a,-l,-2,+k);JSM.AddVertexToBody(a,-l,-2,-k);JSM.AddVertexToBody(a,2,+k,+l);JSM.AddVertexToBody(a,2,-k,+l);JSM.AddVertexToBody(a,-2,+k,+l);JSM.AddVertexToBody(a,2,+k,-l);JSM.AddVertexToBody(a,-2,-k,+l);JSM.AddVertexToBody(a,2,-k,-l);JSM.AddVertexToBody(a,-2,+k,-l);JSM.AddVertexToBody(a,-2,-k,-l);JSM.AddVertexToBody(a,+k,+l,2);JSM.AddVertexToBody(a,-k,+l,2);JSM.AddVertexToBody(a,
  +k,+l,-2);JSM.AddVertexToBody(a,+k,-l,2);JSM.AddVertexToBody(a,-k,+l,-2);JSM.AddVertexToBody(a,-k,-l,2);JSM.AddVertexToBody(a,+k,-l,-2);JSM.AddVertexToBody(a,-k,-l,-2);JSM.AddVertexToBody(a,+b,3,+m);JSM.AddVertexToBody(a,+b,3,-m);JSM.AddVertexToBody(a,+b,-3,+m);JSM.AddVertexToBody(a,-b,3,+m);JSM.AddVertexToBody(a,+b,-3,-m);JSM.AddVertexToBody(a,-b,3,-m);JSM.AddVertexToBody(a,-b,-3,+m);JSM.AddVertexToBody(a,-b,-3,-m);JSM.AddVertexToBody(a,3,+m,+b);JSM.AddVertexToBody(a,3,-m,+b);JSM.AddVertexToBody(a,
  -3,+m,+b);JSM.AddVertexToBody(a,3,+m,-b);JSM.AddVertexToBody(a,-3,-m,+b);JSM.AddVertexToBody(a,3,-m,-b);JSM.AddVertexToBody(a,-3,+m,-b);JSM.AddVertexToBody(a,-3,-m,-b);JSM.AddVertexToBody(a,+m,+b,3);JSM.AddVertexToBody(a,-m,+b,3);JSM.AddVertexToBody(a,+m,+b,-3);JSM.AddVertexToBody(a,+m,-b,3);JSM.AddVertexToBody(a,-m,+b,-3);JSM.AddVertexToBody(a,-m,-b,3);JSM.AddVertexToBody(a,+m,-b,-3);JSM.AddVertexToBody(a,-m,-b,-3);JSM.AddPolygonToBody(a,[0,3,6,2]);JSM.AddPolygonToBody(a,[1,4,7,5]);JSM.AddPolygonToBody(a,
  [8,11,14,10]);JSM.AddPolygonToBody(a,[9,12,15,13]);JSM.AddPolygonToBody(a,[16,19,22,18]);JSM.AddPolygonToBody(a,[17,20,23,21]);JSM.AddPolygonToBody(a,[24,72,96,48]);JSM.AddPolygonToBody(a,[25,49,97,73]);JSM.AddPolygonToBody(a,[26,50,98,74]);JSM.AddPolygonToBody(a,[27,51,99,75]);JSM.AddPolygonToBody(a,[28,76,100,52]);JSM.AddPolygonToBody(a,[29,77,101,53]);JSM.AddPolygonToBody(a,[30,78,102,54]);JSM.AddPolygonToBody(a,[31,55,103,79]);JSM.AddPolygonToBody(a,[32,80,104,56]);JSM.AddPolygonToBody(a,[33,
  57,105,81]);JSM.AddPolygonToBody(a,[34,58,106,82]);JSM.AddPolygonToBody(a,[35,59,107,83]);JSM.AddPolygonToBody(a,[36,84,108,60]);JSM.AddPolygonToBody(a,[37,85,109,61]);JSM.AddPolygonToBody(a,[38,86,110,62]);JSM.AddPolygonToBody(a,[39,63,111,87]);JSM.AddPolygonToBody(a,[40,88,112,64]);JSM.AddPolygonToBody(a,[41,65,113,89]);JSM.AddPolygonToBody(a,[42,66,114,90]);JSM.AddPolygonToBody(a,[43,67,115,91]);JSM.AddPolygonToBody(a,[44,92,116,68]);JSM.AddPolygonToBody(a,[45,93,117,69]);JSM.AddPolygonToBody(a,
  [46,94,118,70]);JSM.AddPolygonToBody(a,[47,71,119,95]);JSM.AddPolygonToBody(a,[0,24,48,51,27,3]);JSM.AddPolygonToBody(a,[1,5,29,53,49,25]);JSM.AddPolygonToBody(a,[2,6,30,54,50,26]);JSM.AddPolygonToBody(a,[4,28,52,55,31,7]);JSM.AddPolygonToBody(a,[8,32,56,59,35,11]);JSM.AddPolygonToBody(a,[9,13,37,61,57,33]);JSM.AddPolygonToBody(a,[10,14,38,62,58,34]);JSM.AddPolygonToBody(a,[12,36,60,63,39,15]);JSM.AddPolygonToBody(a,[16,40,64,67,43,19]);JSM.AddPolygonToBody(a,[17,21,45,69,65,41]);JSM.AddPolygonToBody(a,
  [18,22,46,70,66,42]);JSM.AddPolygonToBody(a,[20,44,68,71,47,23]);JSM.AddPolygonToBody(a,[72,112,88,104,80,96]);JSM.AddPolygonToBody(a,[73,97,83,107,90,114]);JSM.AddPolygonToBody(a,[74,98,81,105,91,115]);JSM.AddPolygonToBody(a,[75,99,82,106,89,113]);JSM.AddPolygonToBody(a,[76,118,94,109,85,100]);JSM.AddPolygonToBody(a,[78,117,93,108,84,102]);JSM.AddPolygonToBody(a,[79,103,87,111,95,119]);JSM.AddPolygonToBody(a,[86,101,77,116,92,110]);JSM.AddPolygonToBody(a,[0,2,26,74,115,67,64,112,72,24]);JSM.AddPolygonToBody(a,
  [1,25,73,114,66,70,118,76,28,4]);JSM.AddPolygonToBody(a,[3,27,75,113,65,69,117,78,30,6]);JSM.AddPolygonToBody(a,[5,7,31,79,119,71,68,116,77,29]);JSM.AddPolygonToBody(a,[8,10,34,82,99,51,48,96,80,32]);JSM.AddPolygonToBody(a,[9,33,81,98,50,54,102,84,36,12]);JSM.AddPolygonToBody(a,[11,35,83,97,49,53,101,86,38,14]);JSM.AddPolygonToBody(a,[13,15,39,87,103,55,52,100,85,37]);JSM.AddPolygonToBody(a,[16,18,42,90,107,59,56,104,88,40]);JSM.AddPolygonToBody(a,[17,41,89,106,58,62,110,92,44,20]);JSM.AddPolygonToBody(a,
  [19,43,91,105,57,61,109,94,46,22]);JSM.AddPolygonToBody(a,[21,23,47,95,111,63,60,108,93,45]);return a};
  JSM.GenerateSnubDodecahedron=function(){var a=new JSM.Body,b=(1+Math.sqrt(5))/2,c=Math.pow(b/2+0.5*Math.sqrt(b-5/27),1/3)+Math.pow(b/2-0.5*Math.sqrt(b-5/27),1/3),d=c-1/c,e=c*b+Math.pow(b,2)+b/c,c=2*d,f=2*e,g=d+e/b+b,h=-(d*b)+e+1/b,l=d/b+e*b-1,k=-(d/b)+e*b+1,m=-d+e/b-b,n=d*b+e-1/b,p=-(d/b)+e*b-1,q=d-e/b-b,r=d*b+e+1/b,s=d+e/b-b,v=d*b-e+1/b,b=d/b+e*b+1;JSM.AddVertexToBody(a,+c,2,-f);JSM.AddVertexToBody(a,+c,-2,+f);JSM.AddVertexToBody(a,-c,2,+f);JSM.AddVertexToBody(a,-c,-2,-f);JSM.AddVertexToBody(a,2,
  -f,+c);JSM.AddVertexToBody(a,-2,+f,+c);JSM.AddVertexToBody(a,2,+f,-c);JSM.AddVertexToBody(a,-2,-f,-c);JSM.AddVertexToBody(a,-f,+c,2);JSM.AddVertexToBody(a,+f,+c,-2);JSM.AddVertexToBody(a,+f,-c,2);JSM.AddVertexToBody(a,-f,-c,-2);JSM.AddVertexToBody(a,+g,+h,-l);JSM.AddVertexToBody(a,+g,-h,+l);JSM.AddVertexToBody(a,-g,+h,+l);JSM.AddVertexToBody(a,-g,-h,-l);JSM.AddVertexToBody(a,+h,-l,+g);JSM.AddVertexToBody(a,-h,+l,+g);JSM.AddVertexToBody(a,+h,+l,-g);JSM.AddVertexToBody(a,-h,-l,-g);JSM.AddVertexToBody(a,
  -l,+g,+h);JSM.AddVertexToBody(a,+l,+g,-h);JSM.AddVertexToBody(a,+l,-g,+h);JSM.AddVertexToBody(a,-l,-g,-h);JSM.AddVertexToBody(a,+k,+m,-n);JSM.AddVertexToBody(a,+k,-m,+n);JSM.AddVertexToBody(a,-k,+m,+n);JSM.AddVertexToBody(a,-k,-m,-n);JSM.AddVertexToBody(a,+m,-n,+k);JSM.AddVertexToBody(a,-m,+n,+k);JSM.AddVertexToBody(a,+m,+n,-k);JSM.AddVertexToBody(a,-m,-n,-k);JSM.AddVertexToBody(a,-n,+k,+m);JSM.AddVertexToBody(a,+n,+k,-m);JSM.AddVertexToBody(a,+n,-k,+m);JSM.AddVertexToBody(a,-n,-k,-m);JSM.AddVertexToBody(a,
  +p,+q,-r);JSM.AddVertexToBody(a,+p,-q,+r);JSM.AddVertexToBody(a,-p,+q,+r);JSM.AddVertexToBody(a,-p,-q,-r);JSM.AddVertexToBody(a,+q,-r,+p);JSM.AddVertexToBody(a,-q,+r,+p);JSM.AddVertexToBody(a,+q,+r,-p);JSM.AddVertexToBody(a,-q,-r,-p);JSM.AddVertexToBody(a,-r,+p,+q);JSM.AddVertexToBody(a,+r,+p,-q);JSM.AddVertexToBody(a,+r,-p,+q);JSM.AddVertexToBody(a,-r,-p,-q);JSM.AddVertexToBody(a,+s,+v,-b);JSM.AddVertexToBody(a,+s,-v,+b);JSM.AddVertexToBody(a,-s,+v,+b);JSM.AddVertexToBody(a,-s,-v,-b);JSM.AddVertexToBody(a,
  +v,-b,+s);JSM.AddVertexToBody(a,-v,+b,+s);JSM.AddVertexToBody(a,+v,+b,-s);JSM.AddVertexToBody(a,-v,-b,-s);JSM.AddVertexToBody(a,-b,+s,+v);JSM.AddVertexToBody(a,+b,+s,-v);JSM.AddVertexToBody(a,+b,-s,+v);JSM.AddVertexToBody(a,-b,-s,-v);JSM.AddPolygonToBody(a,[0,3,51]);JSM.AddPolygonToBody(a,[0,30,12]);JSM.AddPolygonToBody(a,[0,48,3]);JSM.AddPolygonToBody(a,[0,51,30]);JSM.AddPolygonToBody(a,[1,2,50]);JSM.AddPolygonToBody(a,[1,28,13]);JSM.AddPolygonToBody(a,[1,49,2]);JSM.AddPolygonToBody(a,[1,50,28]);
  JSM.AddPolygonToBody(a,[2,29,14]);JSM.AddPolygonToBody(a,[2,49,29]);JSM.AddPolygonToBody(a,[3,31,15]);JSM.AddPolygonToBody(a,[3,48,31]);JSM.AddPolygonToBody(a,[4,7,55]);JSM.AddPolygonToBody(a,[4,34,16]);JSM.AddPolygonToBody(a,[4,52,7]);JSM.AddPolygonToBody(a,[4,55,34]);JSM.AddPolygonToBody(a,[5,6,54]);JSM.AddPolygonToBody(a,[5,32,17]);JSM.AddPolygonToBody(a,[5,53,6]);JSM.AddPolygonToBody(a,[5,54,32]);JSM.AddPolygonToBody(a,[6,33,18]);JSM.AddPolygonToBody(a,[6,53,33]);JSM.AddPolygonToBody(a,[7,35,
  19]);JSM.AddPolygonToBody(a,[7,52,35]);JSM.AddPolygonToBody(a,[8,11,59]);JSM.AddPolygonToBody(a,[8,26,20]);JSM.AddPolygonToBody(a,[8,56,11]);JSM.AddPolygonToBody(a,[8,59,26]);JSM.AddPolygonToBody(a,[9,10,58]);JSM.AddPolygonToBody(a,[9,24,21]);JSM.AddPolygonToBody(a,[9,57,10]);JSM.AddPolygonToBody(a,[9,58,24]);JSM.AddPolygonToBody(a,[10,25,22]);JSM.AddPolygonToBody(a,[10,57,25]);JSM.AddPolygonToBody(a,[11,27,23]);JSM.AddPolygonToBody(a,[11,56,27]);JSM.AddPolygonToBody(a,[12,18,21]);JSM.AddPolygonToBody(a,
  [12,21,24]);JSM.AddPolygonToBody(a,[12,30,18]);JSM.AddPolygonToBody(a,[13,16,22]);JSM.AddPolygonToBody(a,[13,22,25]);JSM.AddPolygonToBody(a,[13,28,16]);JSM.AddPolygonToBody(a,[14,17,20]);JSM.AddPolygonToBody(a,[14,20,26]);JSM.AddPolygonToBody(a,[14,29,17]);JSM.AddPolygonToBody(a,[15,19,23]);JSM.AddPolygonToBody(a,[15,23,27]);JSM.AddPolygonToBody(a,[15,31,19]);JSM.AddPolygonToBody(a,[16,34,22]);JSM.AddPolygonToBody(a,[17,32,20]);JSM.AddPolygonToBody(a,[18,33,21]);JSM.AddPolygonToBody(a,[19,35,23]);
  JSM.AddPolygonToBody(a,[24,58,36]);JSM.AddPolygonToBody(a,[25,57,37]);JSM.AddPolygonToBody(a,[26,59,38]);JSM.AddPolygonToBody(a,[27,56,39]);JSM.AddPolygonToBody(a,[28,50,40]);JSM.AddPolygonToBody(a,[29,49,41]);JSM.AddPolygonToBody(a,[30,51,42]);JSM.AddPolygonToBody(a,[31,48,43]);JSM.AddPolygonToBody(a,[32,54,44]);JSM.AddPolygonToBody(a,[33,53,45]);JSM.AddPolygonToBody(a,[34,55,46]);JSM.AddPolygonToBody(a,[35,52,47]);JSM.AddPolygonToBody(a,[36,43,48]);JSM.AddPolygonToBody(a,[36,46,43]);JSM.AddPolygonToBody(a,
  [36,58,46]);JSM.AddPolygonToBody(a,[37,41,49]);JSM.AddPolygonToBody(a,[37,45,41]);JSM.AddPolygonToBody(a,[37,57,45]);JSM.AddPolygonToBody(a,[38,40,50]);JSM.AddPolygonToBody(a,[38,47,40]);JSM.AddPolygonToBody(a,[38,59,47]);JSM.AddPolygonToBody(a,[39,42,51]);JSM.AddPolygonToBody(a,[39,44,42]);JSM.AddPolygonToBody(a,[39,56,44]);JSM.AddPolygonToBody(a,[40,47,52]);JSM.AddPolygonToBody(a,[41,45,53]);JSM.AddPolygonToBody(a,[42,44,54]);JSM.AddPolygonToBody(a,[43,46,55]);JSM.AddPolygonToBody(a,[0,12,24,36,
  48]);JSM.AddPolygonToBody(a,[1,13,25,37,49]);JSM.AddPolygonToBody(a,[2,14,26,38,50]);JSM.AddPolygonToBody(a,[3,15,27,39,51]);JSM.AddPolygonToBody(a,[4,16,28,40,52]);JSM.AddPolygonToBody(a,[5,17,29,41,53]);JSM.AddPolygonToBody(a,[6,18,30,42,54]);JSM.AddPolygonToBody(a,[7,19,31,43,55]);JSM.AddPolygonToBody(a,[8,20,32,44,56]);JSM.AddPolygonToBody(a,[9,21,33,45,57]);JSM.AddPolygonToBody(a,[10,22,34,46,58]);JSM.AddPolygonToBody(a,[11,23,35,47,59]);return a};
  JSM.AddCumulatedPolygonToBody=function(a,b,c){var d=new JSM.Coord(0,0,0),e=new JSM.Vector(0,0,0),f=[],g;for(g=0;g<b.length;g++)f.push(a.GetVertexPosition(b[g]));g=JSM.CalculateCentroid(f);f=JSM.CalculateNormal(f);d.Set(g.x,g.y,g.z);e.Set(f.x,f.y,f.z);d.Offset(e,c);c=a.VertexCount();JSM.AddVertexToBody(a,d.x,d.y,d.z);d=b.length;for(e=0;e<d;e++)f=b[e],g=b[e<d-1?e+1:0],JSM.AddPolygonToBody(a,[f,g,c])};
  JSM.GenerateCumulatedTetrahedron=function(a){var b=new JSM.Body;b.AddVertex(new JSM.BodyVertex(new JSM.Coord(1,1,1)));b.AddVertex(new JSM.BodyVertex(new JSM.Coord(-1,-1,1)));b.AddVertex(new JSM.BodyVertex(new JSM.Coord(-1,1,-1)));b.AddVertex(new JSM.BodyVertex(new JSM.Coord(1,-1,-1)));a*=2*Math.sqrt(2);JSM.AddCumulatedPolygonToBody(b,[0,1,3],a);JSM.AddCumulatedPolygonToBody(b,[0,2,1],a);JSM.AddCumulatedPolygonToBody(b,[0,3,2],a);JSM.AddCumulatedPolygonToBody(b,[1,2,3],a);return b};
  JSM.GenerateCumulatedHexahedron=function(a){var b=new JSM.Body;JSM.AddVertexToBody(b,1,1,1);JSM.AddVertexToBody(b,1,1,-1);JSM.AddVertexToBody(b,1,-1,1);JSM.AddVertexToBody(b,-1,1,1);JSM.AddVertexToBody(b,1,-1,-1);JSM.AddVertexToBody(b,-1,1,-1);JSM.AddVertexToBody(b,-1,-1,1);JSM.AddVertexToBody(b,-1,-1,-1);a*=2;JSM.AddCumulatedPolygonToBody(b,[0,1,5,3],a);JSM.AddCumulatedPolygonToBody(b,[0,2,4,1],a);JSM.AddCumulatedPolygonToBody(b,[0,3,6,2],a);JSM.AddCumulatedPolygonToBody(b,[1,4,7,5],a);JSM.AddCumulatedPolygonToBody(b,
  [2,6,7,4],a);JSM.AddCumulatedPolygonToBody(b,[3,5,7,6],a);return b};
  JSM.GenerateCumulatedOctahedron=function(a){var b=new JSM.Body;JSM.AddVertexToBody(b,1,0,0);JSM.AddVertexToBody(b,-1,0,0);JSM.AddVertexToBody(b,0,1,0);JSM.AddVertexToBody(b,0,-1,0);JSM.AddVertexToBody(b,0,0,1);JSM.AddVertexToBody(b,0,0,-1);a*=Math.sqrt(2);JSM.AddCumulatedPolygonToBody(b,[0,2,4],a);JSM.AddCumulatedPolygonToBody(b,[0,3,5],a);JSM.AddCumulatedPolygonToBody(b,[0,4,3],a);JSM.AddCumulatedPolygonToBody(b,[0,5,2],a);JSM.AddCumulatedPolygonToBody(b,[1,2,5],a);JSM.AddCumulatedPolygonToBody(b,
  [1,3,4],a);JSM.AddCumulatedPolygonToBody(b,[1,4,2],a);JSM.AddCumulatedPolygonToBody(b,[1,5,3],a);return b};
  JSM.GenerateCumulatedDodecahedron=function(a){var b=new JSM.Body,c=(1+Math.sqrt(5))/2,d=1/c;JSM.AddVertexToBody(b,1,1,1);JSM.AddVertexToBody(b,1,1,-1);JSM.AddVertexToBody(b,1,-1,1);JSM.AddVertexToBody(b,-1,1,1);JSM.AddVertexToBody(b,1,-1,-1);JSM.AddVertexToBody(b,-1,1,-1);JSM.AddVertexToBody(b,-1,-1,1);JSM.AddVertexToBody(b,-1,-1,-1);JSM.AddVertexToBody(b,0,+d,+c);JSM.AddVertexToBody(b,0,+d,-c);JSM.AddVertexToBody(b,0,-d,+c);JSM.AddVertexToBody(b,0,-d,-c);JSM.AddVertexToBody(b,+d,+c,0);JSM.AddVertexToBody(b,
  +d,-c,0);JSM.AddVertexToBody(b,-d,+c,0);JSM.AddVertexToBody(b,-d,-c,0);JSM.AddVertexToBody(b,+c,0,+d);JSM.AddVertexToBody(b,-c,0,+d);JSM.AddVertexToBody(b,+c,0,-d);JSM.AddVertexToBody(b,-c,0,-d);a*=Math.sqrt(5)-1;JSM.AddCumulatedPolygonToBody(b,[0,8,10,2,16],a);JSM.AddCumulatedPolygonToBody(b,[0,16,18,1,12],a);JSM.AddCumulatedPolygonToBody(b,[0,12,14,3,8],a);JSM.AddCumulatedPolygonToBody(b,[1,9,5,14,12],a);JSM.AddCumulatedPolygonToBody(b,[1,18,4,11,9],a);JSM.AddCumulatedPolygonToBody(b,[2,10,6,15,
  13],a);JSM.AddCumulatedPolygonToBody(b,[2,13,4,18,16],a);JSM.AddCumulatedPolygonToBody(b,[3,14,5,19,17],a);JSM.AddCumulatedPolygonToBody(b,[3,17,6,10,8],a);JSM.AddCumulatedPolygonToBody(b,[4,13,15,7,11],a);JSM.AddCumulatedPolygonToBody(b,[5,9,11,7,19],a);JSM.AddCumulatedPolygonToBody(b,[6,17,19,7,15],a);return b};
  JSM.GenerateCumulatedIcosahedron=function(a){var b=new JSM.Body,c=(1+Math.sqrt(5))/2;JSM.AddVertexToBody(b,0,1,+c);JSM.AddVertexToBody(b,0,1,-c);JSM.AddVertexToBody(b,0,-1,+c);JSM.AddVertexToBody(b,0,-1,-c);JSM.AddVertexToBody(b,1,+c,0);JSM.AddVertexToBody(b,1,-c,0);JSM.AddVertexToBody(b,-1,+c,0);JSM.AddVertexToBody(b,-1,-c,0);JSM.AddVertexToBody(b,+c,0,1);JSM.AddVertexToBody(b,-c,0,1);JSM.AddVertexToBody(b,+c,0,-1);JSM.AddVertexToBody(b,-c,0,-1);a*=2;JSM.AddCumulatedPolygonToBody(b,[0,2,8],a);JSM.AddCumulatedPolygonToBody(b,
  [0,4,6],a);JSM.AddCumulatedPolygonToBody(b,[0,6,9],a);JSM.AddCumulatedPolygonToBody(b,[0,8,4],a);JSM.AddCumulatedPolygonToBody(b,[0,9,2],a);JSM.AddCumulatedPolygonToBody(b,[1,3,11],a);JSM.AddCumulatedPolygonToBody(b,[1,4,10],a);JSM.AddCumulatedPolygonToBody(b,[1,6,4],a);JSM.AddCumulatedPolygonToBody(b,[1,10,3],a);JSM.AddCumulatedPolygonToBody(b,[1,11,6],a);JSM.AddCumulatedPolygonToBody(b,[2,5,8],a);JSM.AddCumulatedPolygonToBody(b,[2,7,5],a);JSM.AddCumulatedPolygonToBody(b,[2,9,7],a);JSM.AddCumulatedPolygonToBody(b,
  [3,5,7],a);JSM.AddCumulatedPolygonToBody(b,[3,7,11],a);JSM.AddCumulatedPolygonToBody(b,[3,10,5],a);JSM.AddCumulatedPolygonToBody(b,[4,8,10],a);JSM.AddCumulatedPolygonToBody(b,[6,11,9],a);JSM.AddCumulatedPolygonToBody(b,[5,10,8],a);JSM.AddCumulatedPolygonToBody(b,[7,9,11],a);return b};JSM.GenerateTetrakisHexahedron=function(){return JSM.GenerateCumulatedHexahedron(0.25)};JSM.GenerateRhombicDodecahedron=function(){return JSM.GenerateCumulatedHexahedron(0.5)};
  JSM.GeneratePentakisDodecahedron=function(){var a=Math.sqrt((65+22*Math.sqrt(5))/5)/19;return JSM.GenerateCumulatedDodecahedron(a)};JSM.GenerateSmallStellatedDodecahedron=function(){var a=Math.sqrt((5+2*Math.sqrt(5))/5);return JSM.GenerateCumulatedDodecahedron(a)};JSM.GenerateGreatDodecahedron=function(){var a=Math.sqrt(3)*(Math.sqrt(5)-3)/6;return JSM.GenerateCumulatedIcosahedron(a)};JSM.GenerateSmallTriambicIcosahedron=function(){var a=Math.sqrt(15)/15;return JSM.GenerateCumulatedIcosahedron(a)};
  JSM.GenerateGreatStellatedDodecahedron=function(){var a=Math.sqrt(3)*(3+Math.sqrt(5))/6;return JSM.GenerateCumulatedIcosahedron(a)};JSM.GenerateSmallTriakisOctahedron=function(){var a=Math.sqrt(3)-2*Math.sqrt(6)/3;return JSM.GenerateCumulatedOctahedron(a)};JSM.GenerateStellaOctangula=function(){var a=Math.sqrt(6)/3;return JSM.GenerateCumulatedOctahedron(a)};JSM.GenerateTriakisTetrahedron=function(){var a=Math.sqrt(6)/15;return JSM.GenerateCumulatedTetrahedron(a)};
  JSM.LegoDimensions=function(){this.legoWidth=0.78;this.legoSmallHeight=0.32;this.legoLargeHeight=0.96;this.legoWallWidth=0.16;this.legoCylinderWidth=0.5;this.legoCylinderHeight=0.17;this.legoBottomSmallCylinderWidth=0.3;this.legoBottomLargeCylinderWidth=0.6;this.legoBottomLargeCylinderWallWidth=0.1};
  JSM.GenerateLegoBrick=function(a,b,c,d,e,f,g){function h(a,b){var c,d;for(c=0;c<a.VertexCount();c++)d=a.GetVertex(c),d.position=JSM.CoordAdd(d.position,b)}var l=new JSM.LegoDimensions,k=new JSM.Vector(0,0,1),m=l.legoWidth,n=l.legoLargeHeight;c||(n=l.legoSmallHeight);c=l.legoWallWidth;var p=l.legoCylinderWidth,q=l.legoCylinderHeight,r=l.legoBottomSmallCylinderWidth,s=l.legoBottomLargeCylinderWidth,v=l.legoBottomLargeCylinderWallWidth,u=[];u.push(new JSM.Coord(0,0,0));u.push(new JSM.Coord(m*a,0,0));
  u.push(new JSM.Coord(m*a,m*b,0));u.push(new JSM.Coord(0,m*b,0));var l=new JSM.Body,t=JSM.GeneratePrismShell(u,k,n-c,c,!0);l.Merge(t);for(t=0;4>t;t++)u[t].z=n-c;t=JSM.GeneratePrism(u,k,c,!0,null);l.Merge(t);if(d)for(t=0;t<a;t++)for(u=0;u<b;u++)d=new JSM.Coord(m*t+m/2,m*u+m/2,n+q/2),k=JSM.GenerateCylinder(p/2,q,f,!0,g),h(k,d),l.Merge(k);if(e)if(1===a&&1<b||1===b&&1<a){e=b;p=!0;a>b&&(e=a,p=!1);for(t=0;t<e-1;t++)d=p?new JSM.Coord(m/2,m*(t+1),(n-c)/2):new JSM.Coord(m*(t+1),m/2,(n-c)/2),k=JSM.GenerateCylinder(r/
  2,n-c,f,!0,g),h(k,d),l.Merge(k)}else if(1<a&&1<b)for(t=0;t<a-1;t++)for(u=0;u<b-1;u++)d=new JSM.Coord(m*(t+1),m*(u+1),(n-c)/2),k=JSM.GenerateCylinderShell(s/2,n-c,v,f,!0,g),h(k,d),l.Merge(k);l.SetCubicTextureProjection(new JSM.Coord(0,0,0),new JSM.Coord(1,0,0),new JSM.Coord(0,1,0),new JSM.Coord(0,0,1));return l};
  JSM.GenerateConvexHullBody=function(a){var b=new JSM.Body,c=JSM.ConvexHull3D(a),d={},e,f,g,h;for(e=0;e<c.length;e++){g=c[e];for(f=0;f<g.length;f++)h=g[f],h in d||(d[h]=b.VertexCount(),b.AddVertex(new JSM.BodyVertex(a[h])))}for(e=0;e<c.length;e++){g=c[e];a=[];for(f=0;f<g.length;f++)h=g[f],a.push(d[h]);b.AddPolygon(new JSM.BodyPolygon(a))}return b};
  JSM.GenerateSuperShape=function(a,b,c,d,e,f,g,h,l,k,m,n,p,q){function r(a){var b=Math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z),c=Math.asin(a.z/b);a=Math.atan2(a.y,a.x);return[b,c,a]}function s(a,b,c,d,e,f,g){b=Math.abs(Math.cos(d*a/4)/b);a=Math.abs(Math.sin(d*a/4)/c);return Math.pow(Math.pow(b,f)+Math.pow(a,g),-1/e)}function v(q,p){var r=new JSM.Coord(0,0,0),t=s(q,g,h,l,k,m,n),u=s(p,a,b,c,d,e,f);r.x=u*Math.cos(p)*t*Math.cos(q);r.y=u*Math.sin(p)*t*Math.cos(q);r.z=t*Math.sin(q);return r}p=JSM.GenerateSphere(1,
  p,q);var u,t;for(q=0;q<p.VertexCount();q++)u=p.GetVertex(q),t=u.position,t=r(t),t=v(t[1],t[2]),u.SetPosition(t);return p};
  JSM.CatmullClarkSubdivisionOneIteration=function(a){var b=new JSM.Body,c=new JSM.AdjacencyInfo(a),d=[],e=[];(function(a,b,c){var d,e;for(d=0;d<c.verts.length;d++)e=a.GetVertexPosition(d),b.AddVertex(new JSM.BodyVertex(e.Clone()))})(a,b,c);(function(a,b,c,d){var e,m,n,p,q;for(e=0;e<c.pgons.length;e++){n=c.pgons[e];q=new JSM.Coord(0,0,0);for(m=0;m<n.verts.length;m++)p=a.GetVertexPosition(n.verts[m]),q=JSM.CoordAdd(q,p);q.MultiplyScalar(1/n.verts.length);d.push(b.AddVertex(new JSM.BodyVertex(q)))}})(a,
  b,c,d);(function(a,b,c,d,e){var m,n,p,q,r;for(m=0;m<c.edges.length;m++){p=c.edges[m];n=a.GetVertexPosition(p.vert1);q=a.GetVertexPosition(p.vert2);if(c.IsContourEdge(p))q=JSM.MidCoord(n,q);else{q=JSM.CoordAdd(n,q);for(n=0;2>n;n++)r=0===n?p.pgon1:p.pgon2,r=b.GetVertexPosition(d[r]),q=JSM.CoordAdd(q,r);q.MultiplyScalar(0.25)}e.push(b.AddVertex(new JSM.BodyVertex(q)))}})(a,b,c,d,e);(function(a,b,c,d){function e(a,b){b.x=a.x;b.y=a.y;b.z=a.z}function m(a,b,c,d){d.x=(a.x+2*b.x+(c-3)*d.x)/c;d.y=(a.y+2*b.y+
  (c-3)*d.y)/c;d.z=(a.z+2*b.z+(c-3)*d.z)/c}var n=[],p,q;for(q=0;q<c.edges.length;q++)p=c.edges[q],p=JSM.MidCoord(a.GetVertexPosition(p.vert1),a.GetVertexPosition(p.vert2)),n.push(p);var r,s,v,u;for(q=0;q<c.verts.length;q++)if(r=c.verts[q],s=b.GetVertexPosition(q),c.IsContourVertex(r)){v=0;u=new JSM.Coord(0,0,0);for(a=0;a<r.edges.length;a++)p=r.edges[a],c.IsContourEdge(c.edges[p])&&(p=n[r.edges[a]],u.Add(p),v++);u.Add(s);v++;u.MultiplyScalar(1/v);e(u,s)}else{v=new JSM.Coord(0,0,0);u=new JSM.Coord(0,
  0,0);for(a=0;a<r.pgons.length;a++)p=r.pgons[a],p=b.GetVertexPosition(d[p]),v.Add(p);v.MultiplyScalar(1/r.pgons.length);for(a=0;a<r.edges.length;a++)p=r.edges[a],p=n[p],u.Add(p);u.MultiplyScalar(1/r.edges.length);m(v,u,r.edges.length,s)}})(a,b,c,d);(function(a,b,c,d,e){var m,n,p,q,r,s,v,u;for(s=0;s<c.pgons.length;s++){u=c.pgons[s];m=u.verts.length;for(v=0;v<m;v++)n=u.pedges[v],p=u.pedges[(v+1)%m],q=d[s],n=e[n.index],r=c.GetPolyEdgeStartVertex(p),p=e[p.index],q=new JSM.BodyPolygon([q,n,r,p]),p=a.GetPolygon(s),
  q.material=p.material,q.curved=p.curved,b.AddPolygon(q)}})(a,b,c,d,e);return b};JSM.CatmullClarkSubdivision=function(a,b){var c=a,d;for(d=0;d<b;d++)c=JSM.CatmullClarkSubdivisionOneIteration(c);return c};
  JSM.BooleanOperation=function(a,b,c){function d(a,b,c,d){function e(a,c){return b.AddVertex(new JSM.BodyVertex(a))}var f=new JSM.BodyPolygon([]),g;if(d)for(d=a.VertexCount()-1;0<=d;d--)g=e(a.GetVertex(d),c),f.AddVertexIndex(g);else for(d=0;d<a.VertexCount();d++)g=e(a.GetVertex(d),c),f.AddVertexIndex(g);void 0!==a.userData&&f.SetMaterialIndex(a.userData.material);b.AddPolygon(f)}function e(a,b,c,e){var f;for(f=0;f<a.length;f++)d(a[f],b,c,e)}function f(a,b,c,d,e,f){function g(a,b){var c;for(c=0;c<a.length;c++)a[c].userData=
  b}var h,k;for(h=0;h<a.length;h++)k=a[h],JSM.ClipPolygonWithBSPTree(k.polygon,b,c,d,e,f),g(c,k.userData),g(d,k.userData),g(e,k.userData),g(f,k.userData)}var g=new JSM.BSPTree,h=new JSM.BSPTree;JSM.AddBodyToBSPTree(b,g,"a");JSM.AddBodyToBSPTree(c,h,"b");var l=[],k=[],m=[],n=[];f(g.GetNodes(),h,l,k,m,n);var p=[],q=[],r=[];f(h.GetNodes(),g,p,q,r,[]);g=new JSM.Body;b=new JSM.Octree(JSM.BoxUnion(b.GetBoundingBox(),c.GetBoundingBox()));"Union"==a?(e(l,g,b,!1),e(m,g,b,!1),e(n,g,b,!1),e(p,g,b,!1),e(r,g,b,
  !1)):"Difference"==a?(e(l,g,b,!1),e(m,g,b,!1),e(q,g,b,!0)):"Intersection"==a&&(e(k,g,b,!1),e(n,g,b,!1),e(q,g,b,!1));return g};JSM.BernsteinPolynomial=function(a,b,c){var d=1,e=JSM.Minimum(a,b-a),f;for(f=0;f<e;f++)d*=b-f,d/=f+1;return d*Math.pow(c,a)*Math.pow(1-c,b-a)};JSM.GenerateBezierCurve=function(a,b){var c=[],d=a.length-1,e=1/b,f,g,h,l,k,m;for(f=0;f<=b;f++){h=f*e;m=new JSM.Coord2D(0,0);for(g=0;g<=d;g++)l=a[g],k=JSM.BernsteinPolynomial(g,d,h),m.x+=l.x*k,m.y+=l.y*k;c.push(m)}return c};
  JSM.GenerateSurface=function(a,b,c,d,e,f,g,h){var l=new JSM.Body;(function(a,b,e,f,l){var r,s,v,u;for(r=0;r<=d;r++)for(s=0;s<=c;s++)v=b+s*f,u=e+r*l,v=g(r,s,v,u,h),a.AddVertex(new JSM.BodyVertex(v))})(l,a[0],b[0],(a[1]-a[0])/c,(b[1]-b[0])/d);(function(a){var b,g,h,l,r,s;for(g=0;g<d;g++)for(b=0;b<c;b++)h=g*(c+1)+b,l=h+1,r=h+c+1,s=r+1,e?(l=new JSM.BodyPolygon([h,l,s]),f&&l.SetCurveGroup(0),a.AddPolygon(l),l=new JSM.BodyPolygon([h,s,r])):l=new JSM.BodyPolygon([h,l,s,r]),f&&l.SetCurveGroup(0),a.AddPolygon(l)})(l);
  return l};JSM.SurfaceControlPoints=function(a,b){this.n=a;this.m=b;this.points=[];var c,d;for(c=0;c<=this.n;c++){this.points.push([]);for(d=0;d<=this.m;d++)this.points[c].push(new JSM.Coord(0,0,0))}};JSM.SurfaceControlPoints.prototype.GetNValue=function(){return this.n};JSM.SurfaceControlPoints.prototype.GetMValue=function(){return this.m};JSM.SurfaceControlPoints.prototype.GetControlPoint=function(a,b){return this.points[a][b]};
  JSM.SurfaceControlPoints.prototype.InitPlanar=function(a,b){var c=a/this.n,d=b/this.m,e,f,g;for(e=0;e<=this.n;e++)for(f=0;f<=this.m;f++)g=this.points[e][f],g.x=e*c,g.y=f*d};
  JSM.GenerateBezierSurface=function(a,b,c,d){return JSM.GenerateSurface([0,1],[0,1],b,c,!1,d,function(a,b,c,d,l){var k,m,n,p=l.GetNValue(),q=l.GetMValue();k=new JSM.Coord(0,0,0);for(a=0;a<=p;a++){m=new JSM.Coord(0,0,0);for(b=0;b<=q;b++)n=JSM.BernsteinPolynomial(a,p,c)*JSM.BernsteinPolynomial(b,q,d),n=l.GetControlPoint(a,b).Clone().MultiplyScalar(n),m=JSM.CoordAdd(m,n);k=JSM.CoordAdd(k,m)}return k},a)};
  