Update JSModeler.

This commit is contained in:
kovacsv 2016-04-12 19:54:08 +02:00
parent 52ccdb0acc
commit bb8dac1880
13 changed files with 1201 additions and 1111 deletions

View File

@ -25,7 +25,7 @@
<script type="text/javascript" src="../jsmodeler/three.min.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.viewer.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.ext.three.js"></script>
<script type="text/javascript" src="include/online3dembedder.js"></script>
<script type="text/javascript">
function OnResize ()

View File

@ -35,11 +35,11 @@ LoadOnline3DModels = function ()
}
var currentMeshIndex = 0;
var environment = new JSM.AsyncEnvironment ({
var environment = {
onStart : function (/*taskCount, meshes*/) {
viewer.EnableDraw (false);
},
onProcess : function (currentTask, meshes) {
onProgress : function (currentTask, meshes) {
while (currentMeshIndex < meshes.length) {
viewer.AddMesh (meshes[currentMeshIndex]);
currentMeshIndex = currentMeshIndex + 1;
@ -53,7 +53,7 @@ LoadOnline3DModels = function ()
viewer.EnableDraw (true);
viewer.Draw ();
}
});
};
var textureLoaded = function () {
viewer.Draw ();

View File

@ -8,7 +8,7 @@
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script type="text/javascript" src="../jsmodeler/three.min.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.viewer.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.ext.three.js"></script>
<script type="text/javascript" src="include/online3dembedder.js"></script>
<script type="text/javascript">
window.addEventListener ('load', LoadOnline3DModels, true);

View File

@ -0,0 +1,29 @@
/* JSModeler 0.41 - http://www.github.com/kovacsv/JSModeler */ 'use strict';JSM.ConvertBodyToThreeMeshes=function(a,b,e){var c={textureLoadedCallback:null,hasConvexPolygons:!1};void 0!==e&&null!==e&&(c.textureLoadedCallback=JSM.ValueOrDefault(e.textureLoadedCallback,c.textureLoadedCallback),c.hasConvexPolygons=JSM.ValueOrDefault(e.hasConvexPolygons,c.hasConvexPolygons));var f=[],d=null,g=null;JSM.ExplodeBody(a,b,{hasConvexPolygons:c.hasConvexPolygons,onPointGeometryStart:function(a){g=new THREE.PointsMaterial({color:a.diffuse,size:a.pointSize});d=new THREE.Geometry},
onPointGeometryEnd:function(){var a=new THREE.Points(d,g);f.push(a)},onPoint:function(a){d.vertices.push(new THREE.Vector3(a.x,a.y,a.z))},onLineGeometryStart:function(a){d=new THREE.Geometry;g=new THREE.LineBasicMaterial({color:a.diffuse})},onLineGeometryEnd:function(){var a=new THREE.LineSegments(d,g);f.push(a)},onLine:function(a,b){d.vertices.push(new THREE.Vector3(a.x,a.y,a.z));d.vertices.push(new THREE.Vector3(b.x,b.y,b.z))},onGeometryStart:function(a){var b=null!==a.texture,e=1!==a.opacity,f=
a.diffuse,F=a.specular,u=a.shininess;0===u&&(F=0,u=1);g=new THREE.MeshPhongMaterial({color:f,specular:F,shininess:u});a.singleSided||(g.side=THREE.DoubleSide);e&&(g.opacity=a.opacity,g.transparent=!0);if(b){var n=g;a=a.texture;(new THREE.TextureLoader).load(a,function(a){a.image=JSM.ResizeImageToPowerOfTwoSides(a.image);a.wrapS=THREE.RepeatWrapping;a.wrapT=THREE.RepeatWrapping;n.map=a;n.needsUpdate=!0;null!==c.textureLoadedCallback&&c.textureLoadedCallback()})}d=new THREE.Geometry},onGeometryEnd:function(){d.computeFaceNormals();
var a=new THREE.Mesh(d,g);f.push(a)},onTriangle:function(a,b,c,e,g,f,n,E,l){var p=d.vertices.length;d.vertices.push(new THREE.Vector3(a.x,a.y,a.z));d.vertices.push(new THREE.Vector3(b.x,b.y,b.z));d.vertices.push(new THREE.Vector3(c.x,c.y,c.z));a=new THREE.Face3(p+0,p+1,p+2);d.faces.push(a);null!==e&&(null!==g&&null!==f)&&(a=[],a.push(new THREE.Vector3(e.x,e.y,e.z)),a.push(new THREE.Vector3(g.x,g.y,g.z)),a.push(new THREE.Vector3(f.x,f.y,f.z)),d.faces[d.faces.length-1].vertexNormals=a);null!==n&&(null!==
E&&null!==l)&&(e=[],e.push(new THREE.Vector2(n.x,-n.y)),e.push(new THREE.Vector2(E.x,-E.y)),e.push(new THREE.Vector2(l.x,-l.y)),d.faceVertexUvs[0].push(e))}});return f};JSM.ConvertModelToThreeMeshes=function(a,b,e){var c=[],f,d,g;for(f=0;f<a.BodyCount();f++){d=a.GetBody(f);g=JSM.ConvertBodyToThreeMeshes(d,b,e);for(d=0;d<g.length;d++)c.push(g[d])}return c};
JSM.ConvertJSONDataToThreeMeshes=function(a,b,e){function c(a,e,c,d){function g(a,c,d,f,k,p){function n(a,b,c,e,d){var f=new THREE.Vector2(a,b);if(!JSM.IsZero(d)){var g=Math.sin(d*JSM.DegRad);d=Math.cos(d*JSM.DegRad);f.x=d*a-g*b;f.y=g*a+d*b}f.x=c[0]+f.x*e[0];f.y=c[1]+f.y*e[1];return f}var q=a.parameters,h=k[a.material];a=h.texture;k=h.offset;var l=h.scale,v=h.rotation,w=new THREE.Color,t=new THREE.Color,x=h.shininess||0;w.setRGB(h.diffuse[0],h.diffuse[1],h.diffuse[2]);t.setRGB(h.specular[0],h.specular[1],
h.specular[2]);if(void 0!==a&&null!==a){w.setRGB(1,1,1);t.setRGB(1,1,1);if(void 0===k||null===k)k=[0,0];if(void 0===l||null===l)l=[1,1];if(void 0===v||null===v)v=0}0===x&&(t.setRGB(0,0,0),x=1);var y=new THREE.MeshPhongMaterial({color:w.getHex(),specular:t.getHex(),shininess:x,side:THREE.DoubleSide});1!==h.opacity&&(y.opacity=h.opacity,y.transparent=!0);void 0!==a&&null!==a&&(new THREE.TextureLoader).load(a,function(a){a.image=JSM.ResizeImageToPowerOfTwoSides(a.image);a.wrapS=THREE.RepeatWrapping;
a.wrapT=THREE.RepeatWrapping;y.map=a;y.needsUpdate=!0;void 0!==b&&null!==b&&b()});var h=new THREE.Geometry,r,z,A,s,B,C,D,u,m;for(m=0;m<q.length;m+=9)r=3*q[m+0],z=3*q[m+1],A=3*q[m+2],s=3*q[m+3],B=3*q[m+4],C=3*q[m+5],w=2*q[m+6],t=2*q[m+7],x=2*q[m+8],D=h.vertices.length,u=h.faces.length,h.vertices.push(new THREE.Vector3(c[r+0],c[r+1],c[r+2])),h.vertices.push(new THREE.Vector3(c[z+0],c[z+1],c[z+2])),h.vertices.push(new THREE.Vector3(c[A+0],c[A+1],c[A+2])),h.faces.push(new THREE.Face3(D+0,D+1,D+2)),r=
[],r.push(new THREE.Vector3(d[s+0],d[s+1],d[s+2])),r.push(new THREE.Vector3(d[B+0],d[B+1],d[B+2])),r.push(new THREE.Vector3(d[C+0],d[C+1],d[C+2])),h.faces[u].vertexNormals=r,void 0!==a&&null!==a&&(s=[],s.push(n(f[w+0],f[w+1],k,l,v)),s.push(n(f[t+0],f[t+1],k,l,v)),s.push(n(f[x+0],f[x+1],k,l,v)),h.faceVertexUvs[0].push(s));c=new THREE.Mesh(h,y);c.originalJsonIndex=e;p.push(c)}var f=a.vertices;if(void 0!==f){var k=a.normals;if(void 0!==k){var l=a.uvs;if(void 0!==l){a=a.triangles;var p;for(p=0;p<a.length;p++)g(a[p],
f,k,l,c,d)}}}}var f=[],d=a.materials;if(void 0===d)return f;var g=a.meshes;if(void 0===g)return f;var k=0;JSM.AsyncRunTask(function(){c(g[k],k,d,f);k+=1;return!0},e,g.length,0,f);return f};JSM.ThreeViewer=function(){this.enableDraw=this.drawLoop=this.settings=this.navigation=this.cameraMove=this.runAfterRender=this.runBeforeRender=this.directionalLight=this.ambientLight=this.renderer=this.camera=this.scene=this.canvas=null};
JSM.ThreeViewer.prototype.Start=function(a,b){if(!JSM.IsWebGLEnabled()||!this.InitSettings(b)||!this.InitThree(a)||!this.InitCamera(b)||!this.InitLights())return!1;this.drawLoop=!1;this.enableDraw=!0;this.DrawIfNeeded();return!0};
JSM.ThreeViewer.prototype.InitSettings=function(a){this.settings={cameraEyePosition:new JSM.Coord(1,1,1),cameraCenterPosition:new JSM.Coord(0,0,0),cameraUpVector:new JSM.Coord(0,0,1),lightAmbientColor:[0.5,0.5,0.5],lightDiffuseColor:[0.5,0.5,0.5]};void 0!==a&&(void 0!==a.cameraEyePosition&&(this.settings.cameraEyePosition=JSM.CoordFromArray(a.cameraEyePosition)),void 0!==a.cameraCenterPosition&&(this.settings.cameraCenterPosition=JSM.CoordFromArray(a.cameraCenterPosition)),void 0!==a.cameraUpVector&&
(this.settings.cameraUpVector=JSM.CoordFromArray(a.cameraUpVector)),void 0!==a.lightAmbientColor&&(this.settings.lightAmbientColor=a.lightAmbientColor),void 0!==a.lightDiffuseColor&&(this.settings.lightDiffuseColor=a.lightDiffuseColor));return!0};
JSM.ThreeViewer.prototype.InitThree=function(a){this.canvas=a;if(!this.canvas||!this.canvas.getContext)return!1;this.scene=new THREE.Scene;if(!this.scene)return!1;this.renderer=new THREE.WebGLRenderer({canvas:this.canvas,antialias:!0});if(!this.renderer)return!1;this.renderer.setClearColor(new THREE.Color(16777215));this.renderer.setSize(this.canvas.width,this.canvas.height);return!0};
JSM.ThreeViewer.prototype.InitCamera=function(a){this.cameraMove=new JSM.Camera(JSM.CoordFromArray(a.cameraEyePosition),JSM.CoordFromArray(a.cameraCenterPosition),JSM.CoordFromArray(a.cameraUpVector),a.fieldOfView,a.nearClippingPlane,a.farClippingPlane);if(!this.cameraMove)return!1;this.navigation=new JSM.Navigation;if(!this.navigation.Init(this.canvas,this.cameraMove,this.DrawIfNeeded.bind(this),this.Resize.bind(this)))return!1;this.camera=new THREE.PerspectiveCamera(this.cameraMove.fieldOfView,
this.canvas.width/this.canvas.height,this.cameraMove.nearClippingPlane,this.cameraMove.farClippingPlane);if(!this.camera)return!1;this.scene.add(this.camera);return!0};
JSM.ThreeViewer.prototype.InitLights=function(){var a=new THREE.Color,b=new THREE.Color;a.setRGB(this.settings.lightAmbientColor[0],this.settings.lightAmbientColor[1],this.settings.lightAmbientColor[2]);b.setRGB(this.settings.lightDiffuseColor[0],this.settings.lightDiffuseColor[1],this.settings.lightDiffuseColor[2]);this.ambientLight=new THREE.AmbientLight(a.getHex());if(!this.ambientLight)return!1;this.scene.add(this.ambientLight);this.directionalLight=new THREE.DirectionalLight(b.getHex());if(!this.directionalLight)return!1;
a=(new THREE.Vector3).subVectors(this.cameraMove.eye,this.cameraMove.center);this.directionalLight.position.set(a.x,a.y,a.z);this.scene.add(this.directionalLight);return!0};JSM.ThreeViewer.prototype.SetRunBeforeRender=function(a){this.runBeforeRender=a};JSM.ThreeViewer.prototype.SetRunAfterRender=function(a){this.runAfterRender=a};JSM.ThreeViewer.prototype.SetClearColor=function(a){this.renderer.setClearColor(new THREE.Color(a));this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.AddMesh=function(a){this.scene.add(a);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.AddMeshes=function(a){var b;for(b=0;b<a.length;b++)this.scene.add(a[b]);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.MeshCount=function(){var a=0,b=this;this.scene.traverse(function(e){b.IsRelevantObject(e)&&(a+=1)});return a};JSM.ThreeViewer.prototype.VertexCount=function(){var a=0,b=this;this.scene.traverse(function(e){b.IsRelevantObject(e)&&(a+=e.geometry.vertices.length)});return a};
JSM.ThreeViewer.prototype.FaceCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=b.geometry.faces.length)});return a};JSM.ThreeViewer.prototype.GetMesh=function(a){var b=null,e=0,c;for(c=0;c<this.scene.children.length;c++)if(b=this.scene.children[c],this.IsRelevantObject(b)){if(e==a)return b;e+=1}return null};JSM.ThreeViewer.prototype.RemoveMesh=function(a){a.geometry.dispose();this.scene.remove(a);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.RemoveMeshes=function(){var a,b;for(b=0;b<this.scene.children.length;b++)a=this.scene.children[b],this.IsRelevantObject(a)&&(a.geometry.dispose(),this.scene.remove(a),b--);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.RemoveLastMesh=function(){var a=null,b=this;this.scene.traverse(function(e){b.IsRelevantObject(e)&&(a=e)});null!==a&&this.scene.remove(a);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.SetCamera=function(a,b,e){this.navigation.SetCamera(a,b,e);this.navigation.SetOrbitCenter(b.Clone());this.DrawIfNeeded()};JSM.ThreeViewer.prototype.Resize=function(){this.camera.aspect=this.canvas.width/this.canvas.height;this.camera.updateProjectionMatrix();this.renderer.setSize(this.canvas.width,this.canvas.height);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.FitInWindow=function(){if(0!==this.MeshCount()){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.DrawIfNeeded()}};JSM.ThreeViewer.prototype.AdjustClippingPlanes=function(a){this.GetBoundingSphere().GetRadius()<a?(this.camera.near=0.1,this.camera.far=1E3):(this.camera.near=10,this.camera.far=1E6);this.camera.updateProjectionMatrix();this.Draw()};JSM.ThreeViewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
JSM.ThreeViewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),e,c,f=this;this.scene.traverse(function(d){if(f.IsRelevantObject(d)){e=d.geometry;var g;for(g=0;g<e.vertices.length;g++)c=e.vertices[g].clone(),c.add(d.position),a.x=JSM.Minimum(a.x,c.x),a.y=JSM.Minimum(a.y,c.y),a.z=JSM.Minimum(a.z,c.z),b.x=JSM.Maximum(b.x,c.x),b.y=JSM.Maximum(b.y,c.y),b.z=JSM.Maximum(b.z,c.z)}});return new JSM.Box(a,b)};
JSM.ThreeViewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,e,c,f,d=this;this.scene.traverse(function(g){if(d.IsRelevantObject(g)){e=g.geometry;var k;for(k=0;k<e.vertices.length;k++)c=e.vertices[k].clone(),c.add(g.position),f=a.DistanceTo(new JSM.Coord(c.x,c.y,c.z)),JSM.IsGreater(f,b)&&(b=f)}});return new JSM.Sphere(a,b)};
JSM.ThreeViewer.prototype.GetObjectsUnderPosition=function(a,b){var e=this.camera.position,c=new THREE.Vector3(2*(a/this.canvas.width)-1,2*-(b/this.canvas.height)+1,0.5);c.unproject(this.camera);c.sub(e);c.normalize();return(new THREE.Raycaster(e,c)).intersectObjects(this.scene.children)};JSM.ThreeViewer.prototype.GetObjectsUnderMouse=function(){return this.GetObjectsUnderPosition(this.navigation.mouse.curr.x,this.navigation.mouse.curr.y)};
JSM.ThreeViewer.prototype.GetObjectsUnderTouch=function(){return this.GetObjectsUnderPosition(this.navigation.touch.curr.x,this.navigation.touch.curr.y)};JSM.ThreeViewer.prototype.ProjectVector=function(a,b,e){var c=this.canvas.width/2,f=this.canvas.height/2;a=new THREE.Vector3(a,b,e);a.project(this.camera);a.x=a.x*c+c;a.y=-(a.y*f)+f;return a};JSM.ThreeViewer.prototype.EnableDraw=function(a){this.enableDraw=a};
JSM.ThreeViewer.prototype.Draw=function(){if(this.enableDraw){null!==this.runBeforeRender&&this.runBeforeRender();this.camera.position.set(this.cameraMove.eye.x,this.cameraMove.eye.y,this.cameraMove.eye.z);this.camera.up.set(this.cameraMove.up.x,this.cameraMove.up.y,this.cameraMove.up.z);this.camera.lookAt(new THREE.Vector3(this.cameraMove.center.x,this.cameraMove.center.y,this.cameraMove.center.z));var a=(new THREE.Vector3).subVectors(this.cameraMove.eye,this.cameraMove.center);this.directionalLight.position.set(a.x,
a.y,a.z);this.renderer.render(this.scene,this.camera);null!==this.runAfterRender&&this.runAfterRender();this.drawLoop&&requestAnimationFrame(this.Draw.bind(this))}};JSM.ThreeViewer.prototype.DrawIfNeeded=function(){this.drawLoop||this.Draw()};JSM.ThreeViewer.prototype.StartDrawLoop=function(){this.drawLoop=!0;this.Draw()};JSM.ThreeViewer.prototype.IsRelevantObject=function(a){return a instanceof THREE.Mesh||a instanceof THREE.LineSegments||a instanceof THREE.Points};

View File

@ -1,54 +1,57 @@
/* JSModeler 0.38 - http://www.github.com/kovacsv/JSModeler */ 'use strict';var JSM=function(){this.mainVersion=0;this.subVersion=38};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.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.Assert=function(a,b){if(!a){var c="Assertion failed.";void 0!==b&&null!==b&&(c+=" "+b);alert(c)}};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.BubbleSort=function(a,b){if(!(2>a.length||void 0===b||null===b)){var c,d;for(c=0;c<a.length-1;c++)for(d=0;d<a.length-c-1;d++)if(!b(a[d],a[d+1])){var e=a,f=d+1,g=e[d];e[d]=e[f];e[f]=g}}};JSM.ShiftArray=function(a,b){var c;for(c=0;c<b;c++)a.push(a.shift())};JSM.AsyncEnvironment=function(a){this.parameters=a};JSM.AsyncEnvironment.prototype.OnStart=function(a,b){if(void 0!==this.parameters&&void 0!==this.parameters.onStart)this.parameters.onStart(a,b)};
JSM.AsyncEnvironment.prototype.OnProcess=function(a,b){if(void 0!==this.parameters&&void 0!==this.parameters.onProcess)this.parameters.onProcess(a,b)};JSM.AsyncEnvironment.prototype.OnFinish=function(a){if(void 0!==this.parameters&&void 0!==this.parameters.onFinish)this.parameters.onFinish(a)};
JSM.AsyncRunTask=function(a,b,c,d,e){function f(g){var h=a();b.OnProcess(g,e);h&&g<c-1?setTimeout(function(){f(g+1)},d):setTimeout(function(){b.OnFinish(e)},d)}if(void 0===b||null===b){var g,h;for(g=0;g<c&&!(h=a(),!h);g++);}else b.OnStart(c,e),f(0)};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.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.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,a.z-b.z)};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.Length=function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.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};
/* 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,t,s){var w=JSM.MatrixDeterminant3x3(f,g,h,k,m,n,q,t,s),u=JSM.MatrixDeterminant3x3(e,g,h,l,m,n,p,t,s);h=JSM.MatrixDeterminant3x3(e,f,h,l,k,n,p,q,s);e=JSM.MatrixDeterminant3x3(e,f,g,l,k,m,p,q,t);return w*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.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.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],t=a[11],s=a[13],w=a[14],u=a[15],r=[];r[0]=c*a[0]+d*a[4]+e*a[8]+f*a[12];r[1]=c*g+d*k+e*p+f*s;r[2]=c*h+d*m+e*q+f*w;r[3]=c*l+d*n+e*t+f*u;return r};
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],t=a[12],s=a[13],w=a[14],u=a[15],r=b[0],v=b[1],C=b[2],D=b[3],y=b[4],x=b[5],F=b[6],K=b[7],L=b[8],H=b[9],z=b[10],B=b[11],N=b[12],O=b[13],J=b[14],I=b[15],E=[];E[0]=c*r+d*y+e*L+f*N;E[1]=c*v+d*x+e*H+f*O;E[2]=c*C+d*F+e*z+f*J;E[3]=c*D+d*K+e*B+f*I;E[4]=g*r+h*y+l*L+k*N;E[5]=g*v+h*x+l*H+k*O;E[6]=g*C+h*F+l*z+k*J;E[7]=g*D+h*K+l*B+k*I;E[8]=m*r+n*y+p*L+q*N;E[9]=m*v+n*x+p*H+q*O;E[10]=m*C+n*F+
p*z+q*J;E[11]=m*D+n*K+p*B+q*I;E[12]=t*r+s*y+w*L+u*N;E[13]=t*v+s*x+w*H+u*O;E[14]=t*C+s*F+w*z+u*J;E[15]=t*D+s*K+w*B+u*I;return E};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],t=a[13],s=a[14];a=a[15];return(b*g-c*f)*(n*a-p*s)-(b*h-d*f)*(m*a-p*t)+(b*l-e*f)*(m*s-n*t)+(c*h-d*g)*(k*a-p*q)-(c*l-e*g)*(k*s-n*q)+(d*l-e*h)*(k*t-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],t=a[13],s=a[14];a=a[15];var w=b*g-c*f,u=b*h-d*f,r=b*l-e*f,v=c*h-d*g,C=c*l-e*g,D=d*l-e*h,y=k*t-m*q,x=k*s-n*q,F=k*a-p*q,K=m*s-n*t,L=m*a-p*t,H=n*a-p*s,z=w*H-u*L+r*K+v*F-C*x+D*y;if(JSM.IsZero(z))return null;var B=[];B[0]=(g*H-h*L+l*K)/z;B[1]=(d*L-c*H-e*K)/z;B[2]=(t*D-s*C+a*v)/z;B[3]=(n*C-m*D-p*v)/z;B[4]=(h*F-f*H-l*x)/z;B[5]=(b*H-d*F+e*x)/z;B[6]=(s*r-q*D-a*u)/z;B[7]=(k*D-n*r+p*
u)/z;B[8]=(f*L-g*F+l*y)/z;B[9]=(c*F-b*L-e*y)/z;B[10]=(q*C-t*r+a*w)/z;B[11]=(m*r-k*C-p*w)/z;B[12]=(g*x-f*K-h*y)/z;B[13]=(b*K-c*x+d*y)/z;B[14]=(t*u-q*v-s*w)/z;B[15]=(k*v-m*u+n*w)/z;return B};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.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.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.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.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.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,t=g.x,s=g.y,w=h.x,u=h.y,r=(w-t)*(n-s)-(u-s)*(m-t),v=(p-m)*(n-s)-(q-n)*(m-t),t=(u-s)*(p-m)-(w-t)*(q-n);if(JSM.IsZero(t)){if(JSM.IsZero(r)&&JSM.IsZero(v)){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=r/t;v/=t;if(JSM.IsLower(g,
0)||JSM.IsGreater(g,1)||JSM.IsLower(v,0)||JSM.IsGreater(v,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};
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.GetSectorSegmentation=function(a,b,c){var d=JSM.CoordSub(a.end,a.beg),e=a.beg.DistanceTo(a.end)/b,f=0,g,h;for(g=0;g<=b;g++)h=a.beg.Clone().Offset(d,f),c.push(h),f+=e};JSM.CoordLinePosition2D={CoordOnLine:0,CoordAtLineLeft:1,CoordAtLineRight: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){var b=this.start,c=this.direction;a=c.x*(a.y-b.y)-c.y*(a.x-b.x);return JSM.IsPositive(a)?JSM.CoordLinePosition2D.CoordAtLineLeft:JSM.IsNegative(a)?JSM.CoordLinePosition2D.CoordAtLineRight:JSM.CoordLinePosition2D.CoordOnLine};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.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=
@ -68,15 +71,18 @@ JSM.Plane.prototype.LinePosition=function(a,b){var c=a.direction.Clone().Normali
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.Project=function(a,b,c,d,e,f,g,h,l){b=JSM.MatrixView(b,c,d);g=JSM.MatrixPerspective(e,f,g,h);e=new JSM.Coord(0,0,0);h=[];f=[];h[0]=a.x;h[1]=a.y;h[2]=a.z;h[3]=1;f=JSM.MatrixVectorMultiply(g,JSM.MatrixVectorMultiply(b,h));a=f[3];JSM.IsZero(a)?l=null:(f[0]=0.5*(f[0]/a)+0.5,f[1]=0.5*(f[1]/a)+0.5,f[2]=0.5*(f[2]/a)+0.5,f[0]=f[0]*l[2]+l[0],f[1]=f[1]*l[3]+l[1],e.x=f[0],e.y=f[1],e.z=f[2],l=e);return l};
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,n,m=[];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]],n=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],n=-1==n.tri1||-1==n.tri2||c[n.tri1]!=
c[n.tri2],f&&m.push([e.vertices[0],e.vertices[1],b]),k&&m.push([e.vertices[1],e.vertices[2],b]),n&&m.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,n=d,f=e.triangles[n],f.valid&&(h(e,n,f.edges[0]),h(e,n,f.edges[1]),h(e,n,f.edges[2]),f.valid=!1)));for(d=0;d<m.length;d++)c=m[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=
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 a<this.vertices.length-1?a+1:0};JSM.Polygon2D.prototype.GetPrevVertex=function(a){var b=this.vertices.length;return 0<a?a-1:b-1};JSM.Polygon2D.prototype.ShiftVertices=function(a){JSM.ShiftArray(this.vertices,a);this.ClearCache()};
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};
@ -87,16 +93,17 @@ g=this.vertices[(e+1)%c];h=new JSM.Sector2D(f,g);h=h.CoordPosition(a);if(h==JSM.
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.Clear=function(){this.vertices=[];this.ClearCache()};JSM.Polygon2D.prototype.ClearCache=function(){this.cache={signedArea:null,orientation:null,vertexOrientations:{},complexity: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.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 a<this.vertices.length-1?a+1:0};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.GetPrevVertex=function(a){var b=this.vertices.length;return 0<a?a-1:b-1};
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)};
@ -104,15 +111,27 @@ JSM.ContourPolygon.prototype.VertexCount=function(){var a=0,b;for(b=0;b<this.con
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.CutPolygonWithPlane=function(a,b,c,d,e){function f(a,b,c,d,e){1==b?c.push(a):-1==b?d.push(a):e.push(a)}function g(a,b,c,d){function e(a,b){if(!(2>b.length)){for(var c=a.GetVertex(b[0]),d=a.GetVertex(b[1]),d=JSM.CoordSub(d,c),f=JSM.GetPlaneFromCoordAndDirection(c,d),g,d=[],c=0;c<b.length;c++)g=a.GetVertex(b[c]),d.push(f.CoordSignedDistance(g));for(c=0;c<b.length-1;c++)for(f=0;f<b.length-c-1;f++)if(JSM.IsGreater(d[f],d[f+1])){g=d;var h=f+1,k=g[f];g[f]=g[h];g[h]=k;g=b;h=f+1;k=g[f];g[f]=g[h];g[h]=
k}}}function f(a,b,c,d,e,g){function h(a,b){a[a[b]]=-1;a[b]=-1}var k=[];(function(a,b,c){var d;for(d=0;d<a.VertexCount();d++)c.push(-1);for(d=0;d<b.length;d+=2){a=c;var e=b,f=d;a[e[f]]=e[f+1];a[e[f+1]]=e[f]}})(a,b,k);var l=0;g&&(l=b.length-1);for(var n=b[l],m=n,p=!1,q=0,s=new JSM.Polygon,t;;){p||(q=c[m],0!==q&&(p=!0));if(0<s.VertexCount()&&m==n)if(1==q?d.push(s):-1==q&&e.push(s),s=new JSM.Polygon,0<l&&l<b.length){m=n=b[l];continue}else break;t=a.GetVertex(m);s.AddVertex(t.x,t.y,t.z);-1!=k[m]?(l=g?
l-2:l+2,m=k[m],h(k,m)):m=m<a.VertexCount()-1?m+1:0}}var g=function(a){var b=[],c,d,e,f;for(c=0;c<a.length;c++)if(d=a[c],0===d){d=0<c?c-1:a.length-1;e=c<a.length-1?c+1:0;f=a[d];e=a[e];if(0!==e&&0===f){for(;0===a[d];)d=0<d?d-1:a.length-1;f=a[d]}(-1==f&&1==e||1==f&&-1==e)&&b.push(c)}return b}(b);0===g.length||0!==g.length%2||(e(a,g),f(a,g,b,c,d,!1),f(a,g,b,c,d,!0))}var h=new JSM.Polygon,l=[];b=function(a,b,c,d){function e(a,c,d,f,g){function h(a,c,d,e,f,g){var l=d[d.length-1];0!==l&&(0!==g&&l!=g)&&(e=
a.GetVertex(e),a=a.GetVertex(f),a=new JSM.Line(a,JSM.CoordSub(a,e)),f=new JSM.Coord(0,0,0),b.LinePosition(a,f)==JSM.LinePlanePosition.LineIntersectsPlane&&(c.AddVertex(f.x,f.y,f.z),d.push(0)))}function l(a,b,c,d,e){a=a.GetVertex(d);b.AddVertex(a.x,a.y,a.z);c.push(e)}var k=0===c,n=c===a.VertexCount(),p;n?(c=0,p=a.VertexCount()-1):p=c-1;f=f[c];k||h(a,d,g,p,c,f);n||l(a,d,g,c,f);return f}var f=[],g=!1,h=!1,l,r,v;for(l=0;l<a.VertexCount();l++)r=a.GetVertex(l),r=b.CoordPosition(r),v=0,r==JSM.CoordPlanePosition.CoordInFrontOfPlane?
(v=1,h=!0):r==JSM.CoordPlanePosition.CoordAtBackOfPlane&&(v=-1,g=!0),f.push(v);if(g&&h)for(l=0;l<=a.VertexCount();l++)e(a,l,c,f,d);else{if(g)return-1;if(h)return 1}return 0}(a,b,h,l);0===h.VertexCount()&&0===l.length?f(a,b,c,d,e):g(h,l,c,d);return 0===c.length+d.length+e.length?!1:!0};
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,l,k;for(g=0;g<b.VertexCount();g++)for(h=0;h<c.VertexCount();h++)if(l=b.GetVertex(g),k=c.GetVertex(h),e(a,b,l,k,d)&&!f(l,d)&&!f(k,d))return d.entryPositions.push(l.Clone()),d.entryPositions.push(k.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 l=0,k=0;void 0!==f&&null!==f&&(l=f[h][0],k=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,l,k)})(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=
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};
@ -135,69 +154,80 @@ JSM.ClipPolygonWithBSPTree=function(a,b,c,d,e,f){function g(a,b,c){if(null!==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.BodyPolygon=function(a){this.vertices=a;this.curved=this.material=-1};JSM.BodyPolygon.prototype.AddVertexIndex=function(a){this.vertices.push(a)};JSM.BodyPolygon.prototype.GetVertexIndex=function(a){return this.vertices[a]};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.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.Body=function(){this.vertices=[];this.polygons=[];this.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))};JSM.Body.prototype.AddVertex=function(a){this.vertices.push(a);return this.vertices.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.VertexCount=function(){return this.vertices.length};JSM.Body.prototype.GetPolygon=function(a){return this.polygons[a]};JSM.Body.prototype.PolygonCount=function(){return this.polygons.length};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.GetTextureProjectionType=function(){return this.projection};JSM.Body.prototype.SetTextureProjectionType=function(a){this.projection=a};JSM.Body.prototype.GetTextureProjectionCoords=function(){return this.coords};JSM.Body.prototype.SetTextureProjectionCoords=function(a){this.coords=a};
JSM.Body.prototype.SetPlanarTextureProjection=function(a,b,c){this.SetTextureProjectionType("Planar");this.SetTextureProjectionCoords(new JSM.CoordSystem(a,b,JSM.VectorCross(b,c),new JSM.Coord(0,0,0)))};JSM.Body.prototype.SetCubicTextureProjection=function(a,b,c,d){this.SetTextureProjectionType("Cubic");this.SetTextureProjectionCoords(new JSM.CoordSystem(a,b,c,d))};
JSM.Body.prototype.SetCylindricalTextureProjection=function(a,b,c,d){this.SetTextureProjectionType("Cylindrical");this.SetTextureProjectionCoords(new JSM.CoordSystem(a,c.Clone().SetLength(b),JSM.VectorCross(d,c).SetLength(b),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);null!==this.coords&&(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.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());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.polygons=[];this.coords=this.projection=null};JSM.Model=function(){this.bodies=[]};
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.Light=function(a){var b={ambient:8355711,diffuse:8355711,specular:0,direction:new JSM.Vector(1,0,0)};JSM.CopyObjectProperties(a,this,!0);JSM.CopyObjectProperties(b,this,!1)};
JSM.Material=function(a){JSM.CopyObjectProperties(a,this,!0);JSM.CopyObjectProperties({ambient:52224,diffuse:52224,specular:0,shininess:0,opacity:1,reflection:0,texture:null,textureWidth:1,textureHeight:1},this,!1)};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(){this.verts=[];this.edges=[];this.pgons=[]};JSM.GetPolyEdgeStartVertex=function(a,b){return a.reverse?b.edges[a.index].vert2:b.edges[a.index].vert1};JSM.GetPolyEdgeEndVertex=function(a,b){return a.reverse?b.edges[a.index].vert1:b.edges[a.index].vert2};JSM.GetAnotherPgonOfEdge=function(a,b){return-1!=a.pgon1&&a.pgon1!=b?a.pgon1:-1!=a.pgon2&&a.pgon2!=b?a.pgon2:-1};
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.CalculateAdjacencyInfo=function(a){var b=new JSM.AdjacencyInfo,c,d,e;for(c=0;c<a.VertexCount();c++)d=new JSM.VertInfo,b.verts.push(d);var f,g,h,l,k;for(c=0;c<a.PolygonCount();c++){f=a.GetPolygon(c);e=new JSM.PgonInfo;g=f.VertexIndexCount();for(d=0;d<g;d++){h=f.GetVertexIndex(d);l=f.GetVertexIndex(d<g-1?d+1:0);k=h;var m=l;l=c;for(var n=new JSM.PolyEdgeInfo,p=void 0,q=void 0,p=0;p<b.edges.length;p++)q=b.edges[p],q.vert1===k&&q.vert2===m?(n.index=p,n.reverse=!1):q.vert1===m&&q.vert2===k&&(n.index=
p,n.reverse=!0);-1===n.index?(p=new JSM.EdgeInfo,p.vert1=k,p.vert2=m,p.pgon1=l,p.pgon2=-1,b.edges.push(p),n.index=b.edges.length-1,n.reverse=!1):(k=b.edges[n.index],-1===k.pgon1?k.pgon1=l:k.pgon1!==l&&-1===k.pgon2&&(k.pgon2=l));k=n;e.verts.push(h);e.pedges.push(k);b.verts[h].edges.push(k.index);b.verts[h].pgons.push(c)}b.pgons.push(e)}return b};JSM.IsSolidBody=function(a){a=JSM.CalculateAdjacencyInfo(a);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=JSM.CalculateAdjacencyInfo(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=JSM.GetAnotherPgonOfEdge(k,e),-1!=k&&g.push(k)};JSM.AddVertexToBody=function(a,b,c,d){a.AddVertex(new JSM.BodyVertex(new JSM.Coord(b,c,d)))};JSM.AddPolygonToBody=function(a,b){a.AddPolygon(new JSM.BodyPolygon(b))};
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.MakeBodyInsideOut=function(a){var b,c,d,e,f;for(b=0;b<a.PolygonCount();b++){d=a.GetPolygon(b);f=d.vertices.slice(0);e=f.length;d.vertices=[];for(c=0;c<e;c++)d.vertices.push(f[e-c-1])}};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.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.TriangulateWithCentroids=function(a){var b=new JSM.Body,c,d;for(c=0;c<a.VertexCount();c++)d=a.GetVertex(c).position,b.AddVertex(new JSM.BodyVertex(new JSM.Coord(d.x,d.y,d.z)));var e,f,g,h,l;for(c=0;c<a.PolygonCount();c++){d=JSM.CalculatePolygonCentroid(a,c);l=b.VertexCount();b.AddVertex(new JSM.BodyVertex(new JSM.Coord(d.x,d.y,d.z)));f=a.GetPolygon(c);g=f.VertexIndexCount();for(d=0;d<g;d++)e=f.GetVertexIndex(d),h=f.GetVertexIndex(d<g-1?d+1:0),e=new JSM.BodyPolygon([e,h,l]),e.material=f.material,
e.curved=f.curved,b.AddPolygon(e)}return b};
JSM.TriangulatePolygons=function(a){var b=new JSM.Body,c,d,e;for(c=0;c<a.VertexCount();c++)e=a.GetVertexPosition(c),b.AddVertex(new JSM.BodyVertex(new JSM.Coord(e.x,e.y,e.z)));var f,g;for(c=0;c<a.PolygonCount();c++){f=new JSM.Polygon;g=a.GetPolygon(c);for(d=0;d<g.VertexIndexCount();d++)e=a.GetVertexPosition(g.GetVertexIndex(d)),f.AddVertex(e.x,e.y,e.z);e=JSM.TriangulatePolygon(f);if(null!==e)for(d=0;d<e.length;d++)f=e[d],f=new JSM.BodyPolygon([g.GetVertexIndex(f[0]),g.GetVertexIndex(f[1]),g.GetVertexIndex(f[2])]),
f.InheritAttributes(g),b.AddPolygon(f)}return b};JSM.GenerateRandomMaterials=function(a,b,c){var d,e;for(d=0;d<a.PolygonCount();d++)e=void 0!==c&&c?JSM.SeededRandomInt(0,16777215,d+1):JSM.RandomInt(0,16777215),e=b.AddMaterial(new JSM.Material({ambient:e,diffuse:e})),a.GetPolygon(d).SetMaterialIndex(e)};
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.MergeCoplanarPolygons=function(a){function b(a,b,c,d,e){function f(a,b,c){var d,e,g={};for(d=0;d<a.length;d++)g[a[d]]=!0;var h=-1,l,k,n;for(d=0;d<a.length;d++){l=b.pgons[a[d]];for(e=0;e<l.pedges.length;e++)if(k=l.pedges[e],n=b.edges[k.index],!g[n.pgon1]||!g[n.pgon2])k.reverse?(k=n.vert2,n=n.vert1):(k=n.vert1,n=n.vert2),c[k]=n,-1==h&&(h=k)}return h}function g(a,b,c,d){var e=[],f,h,l;for(f=0;f<c.length;f++)h=c[f],l=d[h],void 0===l&&(l=a.GetVertexPosition(h),l=b.AddVertex(new JSM.BodyVertex(l.Clone())),
d[h]=l),e.push(l);b.AddPolygon(new JSM.BodyPolygon(e))}if(0!==c.length){var h=[];if(1==c.length)h=a.GetPolygon(c[0]).GetVertexIndices();else{var w=[];b=f(c,b,w);if(-1==b)return;c=b;do{if(void 0===c)break;h.push(c);c=w[c]}while(c!=b)}g(a,e,h,d)}}var c=new JSM.Body,d=JSM.CalculateAdjacencyInfo(a),e=function(a,b){function c(a,b,d,e){if(e[a])return null;var f=[],g;JSM.TraversePgonsAlongEdges(a,b,function(b){g=d[a].AngleTo(d[b]);return JSM.IsEqual(g,0)?(f.push(b),e[b]=!0):!1});return f}var d=JSM.CalculateBodyPolygonNormals(a),
e=[],f={},g;for(g=0;g<b.pgons.length;g++)h=c(g,b,d,f),null!==h&&e.push(h);return e}(a,d),f={},g,h;for(g=0;g<e.length;g++)h=e[g],b(a,d,h,f,c);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.GetTextureProjectionCoords(),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.GetTextureProjectionCoords(),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.GetTextureProjectionCoords(),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],
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.GetTextureProjectionType();"Planar"===c?b=JSM.CalculateBodyPlanarTextureCoords(a):"Cubic"===c?b=JSM.CalculateBodyCubicTextureCoords(a):"Cylindrical"===c&&(b=JSM.CalculateBodyCylindricalTextureCoords(a));return b};
JSM.CutBodyByPlane=function(a,b){function c(a,b,c){function d(a){q.push(new JSM.Coord(p.x,p.y,p.z));s.push(a)}function e(c,d){t=JSM.CoordSub(a[d],a[c]).Normalize();J=new JSM.Line(a[c],t);I=b.LineIntersection(J);q.push(new JSM.Coord(I.x,I.y,I.z));s.push(-1)}var f=void 0!==c&&null!==c,g=a.length,h=[],l=[],k=!1,n,m,p;for(n=0;n<g;n++)p=a[n],m=b.CoordPosition(p),l.push(m!==JSM.CoordPlanePosition.CoordAtBackOfPlane),0<n&&l[n-1]!==l[n]&&(k=!0);if(!k){if(!1===l[0])return h;for(n=0;n<g;n++)p=a[n],h.push(new JSM.Coord(p.x,
p.y,p.z)),f&&c.push(n);return h}var q=[],s=[],t,J,I;for(n=0;n<g;n++)k=n-1,m=n,0===n&&(k=g-1),p=a[m],l[m]?(l[k]||e(k,m),d(m)):l[k]&&e(k,m);for(n=0;n<q.length;n++)g=q[n],l=h[h.length-1],0===n||!l.IsEqual(g)?(h.push(new JSM.Coord(g.x,g.y,g.z)),f&&(g=s[n],c.push(g))):f&&(g=s[n],l=s[n-1],-1!==g?c[c.length-1]=g:-1!==l&&(c[c.length-1]=l));return h}function d(a){var b=-1,c;for(c=s;c<e.VertexCount();c++)if(a.IsEqual(e.GetVertexPosition(c))){b=c;break}-1===b&&(b=e.AddVertex(new JSM.BodyVertex(new JSM.Coord(a.x,
a.y,a.z))));return b}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 t;for(k=0;k<a.VertexCount();k++)h[k]&&(t=a.GetVertexPosition(k),l[k]=e.AddVertex(new JSM.BodyVertex(new JSM.Coord(t.x,t.y,t.z))));var s=e.VertexCount();for(k=
0;k<a.PolygonCount();k++)if(n=a.GetPolygon(k),p=f[k],q=g[k],0!==q.length){h=[];for(m=0;m<q.length;m++)-1!==q[m]?h.push(l[n.GetVertexIndex(q[m])]):(t=p[m],h.push(d(t)));m=new JSM.BodyPolygon(h);m.InheritAttributes(n);e.AddPolygon(m)}return e};
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*t;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 n=e(a);for(b=0;b<d;b++)for(c=0;c<d;c++)f=n+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,t=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.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=JSM.CalculateAdjacencyInfo(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(JSM.GetPolyEdgeStartVertex(n,h)),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};
[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=
@ -207,45 +237,48 @@ 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,
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,r;for(e=0;e<d.length;e++){h=d[e];u=new JSM.BodyPolygon([]);r=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]],r.AddVertexIndex(2*b[l[0]]+2*l[1]);g.AddPolygon(u);g.AddPolygon(r)}}}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,r,v;for(f=0;f<a.ContourCount();f++){g=a.GetContour(f);v=null;void 0!==c&&null!==c&&(v=JSM.CalculatePolygonCurveGroups(g,c));h=g.VertexCount();b.push(e);for(g=0;g<h;g++)l=2*e+2*g,r=l+2,g==h-1&&(r=2*e),l=new JSM.BodyPolygon([l,
r,r+1,l+1]),null!==v&&l.SetCurveGroup(v[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.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,t;for(k=0;k<h;k++)0===k||k===h-1?q=Math.PI/2:(m=k-1,n=k,p=k+1,t=JSM.CoordSub(a[p],a[n]),q=JSM.CoordSub(a[m],a[n]),q=t.AngleTo(q)/2,JSM.CoordOrientation(a[m],a[n],a[p],b)==JSM.Orientation.Clockwise&&(q=Math.PI-q)),l.push(q);t=new JSM.Vector(0,0,1);m=[];var s,w;for(k=0;k<h;k++)n=k,k===h-1?w=JSM.CoordSub(a[n-1],a[n]):(p=(k+1)%h,w=JSM.CoordSub(a[n],a[p])),q=l[n],p=d/Math.sin(q),s=a[n].Clone(),s.Offset(w,p),s.Rotate(t,
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,g=[],h=[];JSM.GetSectorSegmentation(a,c,g);JSM.GetSectorSegmentation(b,c,h);b=[];a=[];JSM.GetRuledMesh(g,h,d,b,a);for(d=0;d<b.length;d++)f.AddVertex(new JSM.BodyVertex(b[d]));for(d=0;d<a.length;d++)g=a[d],g=new JSM.BodyPolygon(g),e&&g.SetCurveGroup(0),f.AddPolygon(g);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.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,h=[],l=[];JSM.GetSectorSegmentation(a,c,h);JSM.GetSectorSegmentation(b,c,l);b=[];a=[];JSM.GetRuledMesh(h,l,d,b,a);for(h=0;h<b.length;h++)g.AddVertex(new JSM.BodyVertex(b[h]));for(var k,h=0;h<a.length;h++)k=a[h],k=new JSM.BodyPolygon(k),e&&k.SetCurveGroup(0),g.AddPolygon(k);l=g.VertexCount();for(h=0;h<b.length;h++)k=b[h],k=new JSM.Coord(k.x,k.y,k.z),k.z-=f,g.AddVertex(new JSM.BodyVertex(k));for(h=0;h<a.length;h++){k=a[h];
b=[];for(f=k.length-1;0<=f;f--)b.push(k[f]+l);k=new JSM.BodyPolygon(b);e&&k.SetCurveGroup(0);g.AddPolygon(k)}for(h=0;h<d;h++)e=h+l,f=e+1,a=e-l,b=a+1,k=new JSM.BodyPolygon([e,f,b,a]),g.AddPolygon(k);for(h=0;h<d;h++)e=h+c*(d+1)+l,f=e+1,a=e-l,b=a+1,k=new JSM.BodyPolygon([e,a,b,f]),g.AddPolygon(k);for(h=0;h<c;h++)e=h*(d+1)+l,f=e+d+1,a=e-l,b=a+d+1,k=new JSM.BodyPolygon([e,a,b,f]),g.AddPolygon(k);for(h=0;h<c;h++)e=(h+1)*d+h+l,f=e+d+1,a=e-l,b=a+d+1,k=new JSM.BodyPolygon([e,f,b,a]),g.AddPolygon(k);g.SetCubicTextureProjection(new JSM.Coord(0,
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,t;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,t=p+1,h&&(f=m*d+n,p=f+d,q=f+1,t=p+1,n===d-1&&(q=m*d,t=(m+1)*d)),f=new JSM.BodyPolygon([f,q,t,p]),1==k?f.SetCurveGroup(m):2==k&&
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.ExplodeBodyToTriangles=function(a,b,c){if(void 0===c||null===c)return!1;var d=JSM.CalculateBodyVertexNormals(a),e,f,g=!1;if(void 0!==b&&null!==b)for(e=0;e<b.Count();e++)if(null!==b.GetMaterial(e).texture){g=!0;break}var h,l;if(g){h=JSM.CalculateBodyTextureCoords(a);for(e=0;e<h.length;e++)if(f=a.GetPolygon(e),f.HasMaterialIndex()){l=b.GetMaterial(f.GetMaterialIndex());for(f=0;f<h[e].length;f++)h[e][f].x/=l.textureWidth,h[e][f].y/=-l.textureHeight}}var k=!1;void 0!==c.hasConvexPolygons&&null!==
c.hasConvexPolygons&&(k=c.hasConvexPolygons);(function(a,b,c,d,e){function f(h,l){var r=b.GetMaterial(l);if(void 0!==e.onGeometryStart&&null!==e.onGeometryStart)e.onGeometryStart(r);var s;for(s=0;s<h.length;s++){var u=h[s],w=a.GetPolygon(u),v=w.VertexIndexCount();if(!(3>v)){var H=void 0,z=void 0,B=void 0,N=void 0,O=void 0,J=void 0,I=void 0,E=void 0,Q=void 0,G=void 0;if(3==v||k)for(G=0;G<v-2;G++){if(H=a.GetVertex(w.GetVertexIndex(0)).position,z=a.GetVertex(w.GetVertexIndex((G+1)%v)).position,B=a.GetVertex(w.GetVertexIndex((G+
2)%v)).position,N=c[u][0],O=c[u][(G+1)%v],J=c[u][(G+2)%v],Q=E=I=null,g&&(I=d[u][0],E=d[u][(G+1)%v],Q=d[u][(G+2)%v]),void 0!==e.onTriangle&&null!==e.onTriangle)e.onTriangle(H,z,B,N,O,J,I,E,Q)}else{H=new JSM.Polygon;z=void 0;for(G=0;G<v;G++)z=a.GetVertex(w.vertices[G]),H.AddVertex(z.position.x,z.position.y,z.position.z);G=JSM.CalculateBodyPolygonNormal(a,u);v=JSM.TriangulatePolygon(H,G);if(null!==v)for(var M=void 0,G=0;G<v.length;G++)if(M=v[G],H=a.GetVertex(w.GetVertexIndex(M[0])).position,z=a.GetVertex(w.GetVertexIndex(M[1])).position,
B=a.GetVertex(w.GetVertexIndex(M[2])).position,N=c[u][M[0]],O=c[u][M[1]],J=c[u][M[2]],Q=E=I=null,g&&(I=d[u][M[0]],E=d[u][M[1]],Q=d[u][M[2]]),void 0!==e.onTriangle&&null!==e.onTriangle)e.onTriangle(H,z,B,N,O,J,I,E,Q)}}}if(void 0!==e.onGeometryEnd&&null!==e.onGeometryEnd)e.onGeometryEnd(r)}var h,l=[],r=[];if(void 0===b||null===b)b=new JSM.Materials;for(h=0;h<b.Count();h++)l[h]=[];var v;for(h=0;h<a.PolygonCount();h++)v=a.GetPolygon(h),v.HasMaterialIndex()?(v=v.GetMaterialIndex(),l[v].push(h)):r.push(h);
for(h=0;h<l.length;h++)v=l[h],0!==v.length&&f(v,h);0!==r.length&&f(r,-1)})(a,b,d,h,c);return!0};
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){var c=l.edges[b],e=0;-1!=c.pgon1&&-1!=c.pgon2&&a.GetPolygon(c.pgon1).HasCurveGroup()&&a.GetPolygon(c.pgon2).HasCurveGroup()&&a.GetPolygon(c.pgon1).GetCurveGroup()==a.GetPolygon(c.pgon2).GetCurveGroup()&&(e=2);d("edge "+
(c.vert1+1)+", "+(c.vert2+1)+", -1, -1, "+e+" ! "+(b+1))}function g(e,f){var g=-1;b&&(g=a.GetPolygon(e).GetMaterialIndex()+2,g!=f&&d('set material "material'+g+'"'));var h=l.pgons[e],k=0;a.GetPolygon(e).HasCurveGroup()&&(k=2);c("pgon "+h.pedges.length+", 0, "+k+", ");var k="",m,u;for(m=0;m<h.pedges.length;m++)u=h.pedges[m],k=u.reverse?k+-(u.index+1):k+(u.index+1),m<h.pedges.length-1&&(k+=", ");c(k);c(" ! "+(e+1));d("");return g}var h="";d("base");var l=JSM.CalculateAdjacencyInfo(a),k;for(k=0;k<l.verts.length;k++)e(k);
for(k=0;k<l.edges.length;k++)f(k);var m=-1;for(k=0;k<l.pgons.length;k++)m=g(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.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};
@ -253,14 +286,13 @@ JSM.TriangleBody.prototype.AddUV=function(a,b){this.uvs.push(new JSM.Coord2D(a,b
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,f){function g(a,b,c,d,e){var f=new JSM.Vector(0,0,0),h=0;c=a.GetTriangle(c);b=e[b];var l,k;for(e=0;e<b.length;e++)l=b[e],k=a.GetTriangle(l),c.curve==k.curve&&(f=JSM.CoordAdd(f,d[l]),h+=1);f.MultiplyScalar(1/h);f.Normalize();return a.AddNormal(f.x,f.y,f.z)}var p=b.triangles[e];if(void 0===p.mat||0>p.mat)p.mat=a.GetDefaultMaterialIndex();if(void 0===p.n0||void 0===p.n1||void 0===p.n2)void 0===p.curve||0>p.curve?(c=d[e],c=b.AddNormal(c.x,
c.y,c.z),p.n0=c,p.n1=c,p.n2=c,p.curve=-1):(p.n0=g(b,p.v0,c,d,f),p.n1=g(b,p.v1,c,d,f),p.n2=g(b,p.v2,c,d,f));if(void 0===p.u0||void 0===p.u1||void 0===p.u2)p.u0=b.AddDefaultUV(),p.u1=b.AddDefaultUV(),p.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<
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){if(void 0===a||null===a)a={};var b={};JSM.CopyObjectProperties(a,b,!0);this.materials.push(b);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.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=
@ -270,8 +302,8 @@ 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){var h=b.material,l=c[h];void 0===l&&(a.triangles.push({material:h,parameters:[]}),l=a.triangles.length-1,c[h]=l);a=a.triangles[l].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 t,s;for(t=0;t<b.triangles.length;t++)s=b.triangles[t],d(a,s,c)}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.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;
@ -287,36 +319,38 @@ JSM.BinaryReader.prototype.ReadUnsignedInteger32=function(){var a=this.dataView.
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){var c,d,e,f,g,h,l,k,m,n,p,q,t,s,w,u,r,v,C,D,y,x,F,K,L,H,z,B,N,O,J,I,E,Q,G,M,T,S,U,Y;function A(a,c){if(void 0!==b.onLog&&null!==b.onLog)b.onLog(a,c)}function R(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 X(a){var b=[],c;for(c=0;3>c;c++)b[c]=a.ReadFloat32();return b}function P(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={};c=15677;d=45055;e=40960;f=40976;g=40992;h=41008;l=41024;k=41025;m=41040;n=16;p=17;q=18;t=19;s=41472;w=41728;u=41816;r=41818;v=41812;C=41814;D=41820;y=48;x=49;F=16384;K=16640;L=17920;H=18176;z=16656;B=16704;N=16672;O=16736;J=16688;I=16720;E=45056;Q=45058;G=45072;M=45075;T=45088;S=45089;U=45090;Y=45104;(function(a){function V(a,b,c){var d=[0,0,0];b=a.GetPosition()+c-6;var e=!1;P(a,b,function(b,c){b==p?e||(d[0]=a.ReadUnsignedCharacter()/255,d[1]=a.ReadUnsignedCharacter()/255,d[2]=a.ReadUnsignedCharacter()/
255):b==q?(d[0]=a.ReadUnsignedCharacter()/255,d[1]=a.ReadUnsignedCharacter()/255,d[2]=a.ReadUnsignedCharacter()/255,e=!0):b==n?e||(d[0]=a.ReadFloat32(),d[1]=a.ReadFloat32(),d[2]=a.ReadFloat32()):b==t?(d[0]=a.ReadFloat32(),d[1]=a.ReadFloat32(),d[2]=a.ReadFloat32(),e=!0):a.Skip(c-6)});return d}function W(a,b,c){var d=0;b=a.GetPosition()+c-6;P(a,b,function(b,c){b==y?d=a.ReadUnsignedInteger16()/100:b==x?d=a.ReadFloat32():a.Skip(c-6)});return d}function Z(a,b,c,d){d.texture=null;d.offset=[0,0];d.scale=
[1,1];d.rotation=0;b=a.GetPosition()+c-6;P(a,b,function(b,c){b==w?d.texture=R(a):b==u?d.offset[0]=a.ReadFloat32():b==r?d.offset[1]=a.ReadFloat32():b==v?d.scale[0]=a.ReadFloat32():b==C?d.scale[1]=a.ReadFloat32():b==D?d.rotation=a.ReadFloat32():a.Skip(c-6)})}function $(a,c,d){A("Read material chunk ("+c.toString(16)+", "+d+")",2);var n={},p=a.GetPosition()+d-6;P(a,p,function(b,p){b==e?(A("Read material name chunk ("+c.toString(16)+", "+d+")",3),n.name=R(a)):b==f?(A("Read material ambient chunk ("+c.toString(16)+
", "+d+")",3),n.ambient=V(a,b,p)):b==g?(A("Read material diffuse chunk ("+c.toString(16)+", "+d+")",3),n.diffuse=V(a,b,p)):b==h?(A("Read material specular chunk ("+c.toString(16)+", "+d+")",3),n.specular=V(a,b,p)):b==l?(A("Read material shininess chunk ("+c.toString(16)+", "+d+")",3),n.shininess=W(a,b,p)):b==k?(A("Read material shininess strength chunk ("+c.toString(16)+", "+d+")",3),n.shininessStrength=W(a,b,p)):b==m?(A("Read material transparency chunk ("+c.toString(16)+", "+d+")",3),n.transparency=
W(a,b,p)):b==s?(A("Read material texture map chunk ("+c.toString(16)+", "+d+")",3),Z(a,b,p,n)):(A("Skip chunk ("+b.toString(16)+", "+p+")",3),a.Skip(p-6))});if(void 0!==b.onMaterial&&null!==b.onMaterial)b.onMaterial(n)}function aa(a,c,d){A("Read faces chunk ("+c.toString(16)+", "+d+")",4);c=a.GetPosition()+d-6;var e=a.ReadUnsignedInteger16(),f,g,h,l;for(d=0;d<e;d++)if(f=a.ReadUnsignedInteger16(),g=a.ReadUnsignedInteger16(),h=a.ReadUnsignedInteger16(),l=a.ReadUnsignedInteger16(),void 0!==b.onFace&&
null!==b.onFace)b.onFace(f,g,h,l);P(a,c,function(c,d){if(c==J){A("Read face materials chunk ("+c.toString(16)+", "+d+")",5);var f=R(a),g=a.ReadUnsignedInteger16(),h,l;for(h=0;h<g;h++)if(l=a.ReadUnsignedInteger16(),void 0!==b.onFaceMaterial&&null!==b.onFaceMaterial)b.onFaceMaterial(l,f)}else if(c==I){A("Read face smoothing groups chunk ("+c.toString(16)+", "+d+")",5);for(f=0;f<e;f++)if(g=a.ReadUnsignedInteger32(),void 0!==b.onFaceSmoothingGroup&&null!==b.onFaceSmoothingGroup)b.onFaceSmoothingGroup(f,
g)}else A("Skip chunk ("+c.toString(16)+", "+d+")",5),a.Skip(d-6)})}function ba(a,c,d,e){A("Read mesh chunk ("+c+", "+d.toString(16)+", "+e+")",3);if(void 0!==b.onMesh&&null!==b.onMesh)b.onMesh(c);c=a.GetPosition()+e-6;P(a,c,function(c,d){if(c==z){A("Read vertices chunk ("+c.toString(16)+", "+d+")",4);var e=a.ReadUnsignedInteger16(),f,g,h,l;for(f=0;f<e;f++)if(g=a.ReadFloat32(),h=a.ReadFloat32(),l=a.ReadFloat32(),void 0!==b.onVertex&&null!==b.onVertex)b.onVertex(g,h,l)}else if(c==B){A("Read texture vertices chunk ("+
c.toString(16)+", "+d+")",4);e=a.ReadUnsignedInteger16();for(f=0;f<e;f++)if(g=a.ReadFloat32(),h=a.ReadFloat32(),void 0!==b.onTextureVertex&&null!==b.onTextureVertex)b.onTextureVertex(g,h)}else if(c==N)aa(a,c,d);else if(c==O){A("Read transformation chunk ("+c.toString(16)+", "+d+")",4);e=[];for(f=0;4>f;f++){for(g=0;3>g;g++)e.push(a.ReadFloat32());3>f?e.push(0):e.push(1)}if(void 0!==b.onTransformation&&null!==b.onTransformation)b.onTransformation(e)}else A("Skip chunk ("+c.toString(16)+", "+d+")",4),
a.Skip(d-6)})}function ca(a,b,c){A("Read object chunk ("+b.toString(16)+", "+c+")",2);b=a.GetPosition()+c-6;var d=R(a);P(a,b,function(b,c){b==K?ba(a,d,b,c):(b==L?A("Skip light chunk ("+d+", "+b.toString(16)+", "+c+")",3):b==H?A("Skip camera chunk ("+d+", "+b.toString(16)+", "+c+")",3):A("Skip chunk ("+b.toString(16)+", "+c+")",3),a.Skip(c-6))})}function da(a,b,c){A("Read editor chunk ("+b.toString(16)+", "+c+")",1);b=a.GetPosition()+c-6;P(a,b,function(b,c){b==d?$(a,b,c):b==F?ca(a,b,c):(A("Skip chunk ("+
b.toString(16)+", "+c+")",2),a.Skip(c-6))})}function ea(a,c,d){function e(a,b){var c=[];a.Skip(10);var d,f,g,h=a.ReadInteger32();for(d=0;d<h;d++)a.ReadInteger32(),f=a.ReadUnsignedInteger16(),0!==f&&a.ReadFloat32(),b==S?(g=a.ReadFloat32(),f=X(a),f[3]=g):f=X(a),c.push(f);return c}A("Read object node chunk ("+c.toString(16)+", "+d+")",2);var f={name:"",nodeId:-1,flags:-1,userId:-1,pivot:[0,0,0],positions:[],rotations:[],scales:[]};c=a.GetPosition()+d-6;P(a,c,function(b,c){b==G?(f.name=R(a),f.flags=a.ReadUnsignedInteger32(),
f.userId=a.ReadUnsignedInteger16()):b==M?f.pivot=X(a):b==T?f.positions=e(a,T):b==S?f.rotations=e(a,S):b==U?f.scales=e(a,U):b==Y?f.nodeId=a.ReadUnsignedInteger16():(A("Skip chunk ("+b.toString(16)+", "+c+")",3),a.Skip(c-6))});if(void 0!==b.onObjectNode&&null!==b.onObjectNode)b.onObjectNode(f)}function fa(a,b,c){A("Read keyframe chunk ("+b.toString(16)+", "+c+")",1);b=a.GetPosition()+c-6;P(a,b,function(b,c){b==Q?ea(a,b,c):(A("Skip chunk ("+b.toString(16)+", "+c+")",2),a.Skip(c-6))})}function ga(a,b,
d){A("Read main chunk ("+b.toString(16)+", "+d+")",0);b=a.GetPosition()+d-6;P(a,b,function(b,d){b==c?da(a,b,d):b==E?fa(a,b,d):(A("Skip chunk ("+b.toString(16)+", "+d+")",1),a.Skip(d-6))})}var ha=a.GetByteLength();P(a,ha,function(b,c){19789==b?ga(a,b,c):(A("Skip chunk ("+b.toString(16)+", "+c+")",0),a.Skip(c-6))})})(new JSM.BinaryReader(a,!0))};
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 l(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=l(b.nodes[d],b),c=JSM.MatrixMultiply(c,d)));return a.matrix=c}var k=a.meshData;k=void 0===k||null===k?null:k.transformation;if(null!==k){var n=null,n=null!==b?l(b,c):k;c=JSM.MatrixClone(n);n=JSM.MatrixClone(k);k=JSM.MatrixInvert(n);null!==k&&(h(a,n,k),b=void 0===b||null===b?[0,0,0]:b.pivot,f(c,[-b[0],-b[1],-b[2]]),b=JSM.MatrixMultiply(k,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,l;for(g=0;g<a.TriangleCount();g++)h=a.GetTriangle(g),b&&(h.u0=h.v0,h.u1=h.v1,h.u2=h.v2),l=f.faceToMaterial[g],void 0!==l&&(l=c[l],void 0!==l&&(h.mat=l)),l=f.faceToSmoothingGroup[g],void 0!==l&&0<l&&(h.curve=l)}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,w,u,r,v,C;for(g=0;g<b.BodyCount();g++)if(w=b.GetBody(g),u=w.meshData,0===u.objectNodes.length)e(w,null,c,a);else{v=a.nodes[u.objectNodes[0]];
for(h=1;h<u.objectNodes.length;h++)r=a.nodes[u.objectNodes[h]],C=f(b,w,g+1,h+1),C.meshData=w.meshData,e(C,r,c,a),g+=1;e(w,v,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){function h(a,
b){var c=a.indexOf(b)+b.length;return a.substr(c,a.length-c).trim()}if(0!==a.length&&"#"!=a[0]){var n=a.split(/\s+/);if(!(0===n.length||"#"==n[0][0]))if("g"==n[0]){if(!(2>n.length)){var p="";for(a=1;a<n.length;a++)p+=n[a],a<n.length-1&&(p+=" ");if(void 0!==b.onMesh&&null!==b.onMesh)b.onMesh(p)}}else if("v"==n[0])4>n.length||d(parseFloat(n[1]),parseFloat(n[2]),parseFloat(n[3]));else if("vn"==n[0])4>n.length||e(parseFloat(n[1]),parseFloat(n[2]),parseFloat(n[3]));else if("vt"==n[0])3>n.length||f(parseFloat(n[1]),
parseFloat(n[2]));else if("f"==n[0]){if(!(4>n.length)){var p=[],q=[],t=[],s;for(a=1;a<n.length;a++)s=n[a].split("/"),p.push(parseInt(s[0],10)-1),1<s.length&&0<s[1].length&&t.push(parseInt(s[1],10)-1),2<s.length&&0<s[2].length&&q.push(parseInt(s[2],10)-1);if(void 0!==b.onFace&&null!==b.onFace)b.onFace(p,q,t)}}else if("usemtl"==n[0]){if(!(2>n.length)&&void 0!==b.onUseMaterial&&null!==b.onUseMaterial)b.onUseMaterial(n[1])}else if("newmtl"==n[0]){if(!(2>n.length)&&void 0!==b.onNewMaterial&&null!==b.onNewMaterial)b.onNewMaterial(n[1])}else if("Ka"==
n[0]||"Kd"==n[0]||"Ks"==n[0])4>n.length||c(n[0],parseFloat(n[1]),parseFloat(n[2]),parseFloat(n[3]));else if("Ns"==n[0]||"Tr"==n[0]||"d"==n[0]){if(!(2>n.length)&&void 0!==b.onMaterialParameter&&null!==b.onMaterialParameter)b.onMaterialParameter(n[0],n[1])}else if("map_Kd"==n[0]){if(!(2>n.length)&&(n=h(a,"map_Kd"),void 0!==b.onMaterialTexture&&null!==b.onMaterialTexture))b.onMaterialTexture(n)}else"mtllib"==n[0]&&!(2>n.length)&&(n=h(a,"mtllib"),n=g(n.trim()),null!==n&&l(n))}}function l(a){a=a.split("\n");
var b,c;for(b=0;b<a.length;b++)c=a[b].trim(),h(c)}if(void 0===b||null===b)b={};l(a)};
(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){function d(a,b,c,e,f){if(void 0!==c){var g=e[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)),e[c]=g}}var e,f,g,h,l,k,m;for(e=0;e<a.BodyCount();e++){g=a.GetBody(e);l={};k={};m={};for(f=0;f<g.TriangleCount();f++)h=g.GetTriangle(f),h.v0=d(g,b,h.v0,l,0),h.v1=d(g,b,h.v1,l,0),h.v2=d(g,b,h.v2,l,0),h.n0=d(g,c,h.n0,k,1),h.n1=d(g,c,h.n1,k,1),h.n2=d(g,c,h.n2,k,1),h.u0=d(g,n,h.u0,m,2),h.u1=d(g,n,h.u1,m,2),h.u2=d(g,n,h.u2,m,2)}})(d,k,m,n);d.Finalize();return JSM.ConvertTriangleModelToJsonData(d)};
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};
@ -329,7 +363,130 @@ JSM.ImportFileList.prototype.IsFile=function(){return this.isFile};JSM.ImportFil
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)};
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"===
@ -410,11 +567,11 @@ JSM.GenerateTruncatedIcosidodecahedron=function(){var a=new JSM.Body,b=(1+Math.s
[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,t=d*b+e+1/b,s=d+e/b-b,w=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,
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,-t);JSM.AddVertexToBody(a,+p,-q,+t);JSM.AddVertexToBody(a,-p,+q,+t);JSM.AddVertexToBody(a,-p,-q,-t);JSM.AddVertexToBody(a,+q,-t,+p);JSM.AddVertexToBody(a,-q,+t,+p);JSM.AddVertexToBody(a,+q,+t,-p);JSM.AddVertexToBody(a,-q,-t,-p);JSM.AddVertexToBody(a,-t,+p,+q);JSM.AddVertexToBody(a,+t,+p,-q);JSM.AddVertexToBody(a,+t,-p,+q);JSM.AddVertexToBody(a,-t,-p,-q);JSM.AddVertexToBody(a,+s,+w,-b);JSM.AddVertexToBody(a,+s,-w,+b);JSM.AddVertexToBody(a,-s,+w,+b);JSM.AddVertexToBody(a,-s,-w,-b);JSM.AddVertexToBody(a,
+w,-b,+s);JSM.AddVertexToBody(a,-w,+b,+s);JSM.AddVertexToBody(a,+w,+b,-s);JSM.AddVertexToBody(a,-w,-b,-s);JSM.AddVertexToBody(a,-b,+s,+w);JSM.AddVertexToBody(a,+b,+s,-w);JSM.AddVertexToBody(a,+b,-s,+w);JSM.AddVertexToBody(a,-b,-s,-w);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]);
+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]);
@ -436,130 +593,21 @@ JSM.GenerateCumulatedIcosahedron=function(a){var b=new JSM.Body,c=(1+Math.sqrt(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,t=l.legoBottomSmallCylinderWidth,s=l.legoBottomLargeCylinderWidth,w=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,r=JSM.GeneratePrismShell(u,k,n-c,c,!0);l.Merge(r);for(r=0;4>r;r++)u[r].z=n-c;r=JSM.GeneratePrism(u,k,c,!0,null);l.Merge(r);if(d)for(r=0;r<a;r++)for(u=0;u<b;u++)d=new JSM.Coord(m*r+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(r=0;r<e-1;r++)d=p?new JSM.Coord(m/2,m*(r+1),(n-c)/2):new JSM.Coord(m*(r+1),m/2,(n-c)/2),k=JSM.GenerateCylinder(t/
2,n-c,f,!0,g),h(k,d),l.Merge(k)}else if(1<a&&1<b)for(r=0;r<a-1;r++)for(u=0;u<b-1;u++)d=new JSM.Coord(m*(r+1),m*(u+1),(n-c)/2),k=JSM.GenerateCylinderShell(s/2,n-c,w,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.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 t(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 w(p,q){var r=new JSM.Coord(0,0,0),u=s(p,g,h,l,k,m,n),w=s(q,a,b,c,d,e,f);r.x=w*Math.cos(q)*u*Math.cos(p);r.y=w*Math.sin(q)*u*Math.cos(p);r.z=u*Math.sin(p);return r}p=JSM.GenerateSphere(1,
p,q);var u,r;for(q=0;q<p.VertexCount();q++)u=p.GetVertex(q),r=u.position,r=t(r),r=w(r[1],r[2]),u.SetPosition(r);return p};
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 l=JSM.CoordSub(c,b).Normalize(),l=JSM.GetPlaneFromCoordAndDirection(b,l),k,n,m,p,r,v;for(k=0;k<a.PolygonCount();k++){r=JSM.Inf;v=-JSM.Inf;m=a.GetPolygon(k);for(n=0;n<m.VertexIndexCount();n++)p=a.GetVertexPosition(m.GetVertexIndex(n)),p=l.CoordDistance(p),JSM.IsLower(p,r)&&(r=p),JSM.IsGreater(p,v)&&(v=p);d.push(r);e.push(v);
n=a.GetPolygon(k);m=new JSM.Coord(0,0,0);v=r=void 0;for(r=0;r<n.VertexIndexCount();r++)v=a.GetVertexPosition(n.GetVertexIndex(r)),m=JSM.CoordAdd(m,v);m.MultiplyScalar(1/n.VertexIndexCount());n=m;m=l.CoordDistance(n);f.push(n);g.push(m);m=JSM.CalculateBodyPolygonNormal(a,k);r=JSM.CoordSub(n,b).Normalize();r=JSM.VectorDot(m,r);JSM.IsGreaterOrEqual(r,0)&&m.MultiplyScalar(-1);n=JSM.GetPlaneFromCoordAndDirection(n,m);h.push(n)}})();(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 n=l[f+1];if(null!==k[g][n])g=k[g][n];else{var m;if(m=JSM.IsLowerOrEqual(d[g],e[n])){var p=n;m=h[g];for(var C=h[p],D=void 0,y=void 0,x=!0,F=a.GetPolygon(g),D=0;D<F.VertexIndexCount();D++)if(y=a.GetVertexPosition(F.GetVertexIndex(D)),C.CoordPosition(y)===JSM.CoordPlanePosition.CoordInFrontOfPlane){x=
!1;break}if(x)m=!1;else{C=!0;p=a.GetPolygon(p);for(D=0;D<p.VertexIndexCount();D++)if(y=a.GetVertexPosition(p.GetVertexIndex(D)),m.CoordPosition(y)===JSM.CoordPlanePosition.CoordAtBackOfPlane){C=!1;break}m=C?!1:!0}}g=m?k[g][n]=!0:k[g][n]=!1}g&&(g=l,n=f+1,m=g[f],g[f]=g[n],g[n]=m)}})();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,t,s,w);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,t=c.nearClippingPlane,s=c.farClippingPlane,w=[0,0,e,l],u,r,v,C;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]),r=h(u),v=u.GetMaterialIndex(),C=b.GetMaterial(v).diffuse,f.DrawPolygon(r,C,!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,l;for(h=0;h<d.VertexCount();h++)l=d.GetVertex(h),g(e,l);r=e;u=a.GetPolygon(c.userData.originalPolygon);v=u.GetMaterialIndex();C=b.GetMaterial(v).diffuse;
f.DrawPolygon(r,C,!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),r=h(u),r.GetOrientation()==JSM.Orientation.CounterClockwise&&(v=u.GetMaterialIndex(),C=b.GetMaterial(v).diffuse,f.DrawPolygon(r,C,!0))}else if("Wireframe"==d){var D,y,x,F=[];for(c=0;c<a.PolygonCount();c++){y=D=null;u=a.GetPolygon(c);l=u.VertexIndexCount();for(d=0;d<=l;d++)x=u.GetVertexIndex(d%l),e=a.GetVertexPosition(x),r=JSM.Project(e,k,m,n,p*JSM.DegRad,
q,t,s,w),null!==D&&(null!==y&&void 0===F[[y,x]])&&(f.DrawLine(D,r),F[[y,x]]=!0,F[[x,y]]=!0),y=x,D=r}}return!0};
JSM.CatmullClarkSubdivisionOneIteration=function(a){var b=new JSM.Body,c=JSM.CalculateAdjacencyInfo(a);(function(){var d,e;for(d=0;d<c.verts.length;d++)e=a.GetVertex(d).position,b.AddVertex(new JSM.BodyVertex(new JSM.Coord(e.x,e.y,e.z)))})();var d=[];(function(){var e,g,h,l,k;for(e=0;e<c.pgons.length;e++){h=c.pgons[e];k=new JSM.Coord(0,0,0);for(g=0;g<h.verts.length;g++)l=a.GetVertex(h.verts[g]).position,k=JSM.CoordAdd(k,l);k.MultiplyScalar(1/h.verts.length);d.push(b.AddVertex(new JSM.BodyVertex(k)))}})();
var e=[];(function(){var f,g,h,l,k;for(f=0;f<c.edges.length;f++){h=c.edges[f];g=a.GetVertex(h.vert1).position.Clone().MultiplyScalar(0.25);l=a.GetVertex(h.vert2).position.Clone().MultiplyScalar(0.25);l=JSM.CoordAdd(g,l);for(g=0;2>g;g++)k=0===g?h.pgon1:h.pgon2,-1===k&&(k=0===g?h.pgon2:h.pgon1),k=b.GetVertex(d[k]).position.Clone(),l=JSM.CoordAdd(l,k.MultiplyScalar(0.25));e.push(b.AddVertex(new JSM.BodyVertex(l)))}})();(function(){var e=[],g,h,l,k;for(l=0;l<c.edges.length;l++)g=c.edges[l],h=JSM.MidCoord(a.GetVertex(g.vert1).position,
a.GetVertex(g.vert2).position),e.push(h);var m,n,p;for(l=0;l<c.verts.length;l++){m=c.verts[l];p=new JSM.Coord(0,0,0);g=new JSM.Coord(0,0,0);n=1/m.pgons.length;for(k=0;k<m.pgons.length;k++)h=b.GetVertex(d[m.pgons[k]]).position.Clone(),p=JSM.CoordAdd(p,h.MultiplyScalar(n));n=1/m.edges.length;for(k=0;k<m.edges.length;k++)h=e[m.edges[k]].Clone(),g=JSM.CoordAdd(g,h.MultiplyScalar(n));m=m.edges.length;k=b.GetVertex(l).position;k.x=(p.x+2*g.x+(m-3)*k.x)/m;k.y=(p.y+2*g.y+(m-3)*k.y)/m;k.z=(p.z+2*g.z+(m-3)*
k.z)/m}})();(function(){var f,g,h,l,k,m,n,p;for(m=0;m<c.pgons.length;m++){p=c.pgons[m];f=p.verts.length;for(n=0;n<f;n++)g=p.pedges[n],h=p.pedges[(n+1)%f],l=d[m],g=e[g.index],k=JSM.GetPolyEdgeStartVertex(h,c),h=e[h.index],l=new JSM.BodyPolygon([l,g,k,h]),h=a.GetPolygon(m),l.material=h.material,l.curved=h.curved,b.AddPolygon(l)}})();return b};JSM.CatmullClarkSubdivision=function(a,b){var c=a,d;for(d=0;d<b;d++)c=JSM.CatmullClarkSubdivisionOneIteration(c);return c};
JSM.SvgToModel=function(a,b,c,d){function e(a,b){function c(a,b,d,e,f){var g=a.createSVGPoint();g.x=e;g.y=f;a=g;d=d.getCTM();void 0!==d&&null!==d&&(a=g.matrixTransform(d));d=new JSM.Coord2D(a.x,a.y);e=new JSM.Coord2D(e,f);b=b.GetLastContour();f=b.VertexCount();if(0<f&&b.GetVertex(f-1).IsEqualWithEps(d,0.1))return e;b.AddVertex(a.x,a.y);return e}function d(a,b,e,f,g,h){var l=document.createElementNS("http://www.w3.org/2000/svg","path"),k="M "+f.x+" "+f.y+" ",n,m,p,r,s;for(n=0;n<g.length;n++)if(m=g[n],
m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS||m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL)p=m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS?"C":"c",k+=p+" "+m.x1+" "+m.y1+" "+m.x2+" "+m.y2+" "+m.x+" "+m.y+" ";else if(m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS||m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL)p=m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS?"Q":"q",k+=p+" "+m.x1+" "+m.y1+" "+m.x+" "+m.y+" ";else if(m.pathSegType==SVGPathSeg.PATHSEG_ARC_ABS||
m.pathSegType==SVGPathSeg.PATHSEG_ARC_REL)p=m.pathSegType==SVGPathSeg.PATHSEG_ARC_ABS?"A":"a",r=m.largeArcFlag?1:0,s=m.sweepFlag?1:0,k+=p+" "+m.r1+" "+m.r2+" "+m.angle+" "+r+" "+s+" "+m.x+" "+m.y+" ";else if(m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS||m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL)p=m.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS?"S":"s",k+=p+" "+m.x2+" "+m.y2+" "+m.x+" "+m.y+" ";l.setAttributeNS(null,"d",k);k=l.getTotalLength();g=0;0<e&&(g=
parseInt(k/e,10));3>g&&(g=3);e=k/g;for(k=1;k<=g;k++)f=l.getPointAtLength(k*e),f=c(a,h,b,f.x,f.y);return f}function e(a){a=a.GetLastContour();var b=a.VertexCount();if(0!==b){var c=a.GetVertex(0),b=a.GetVertex(b-1);c.IsEqualWithEps(b,0.1)&&a.vertices.pop()}}function f(a){0<a.GetLastContour().VertexCount()&&(e(a),a.AddContour())}var g=new JSM.ContourPolygon2D;g.AddContour();var h=document.createElementNS("http://www.w3.org/2000/svg","svg"),l;if(a instanceof SVGPathElement){var k=new JSM.Coord2D(0,0),
m=new JSM.Coord2D(0,0),y=b;a.hasAttribute("segmentlength")&&(y=parseFloat(a.getAttribute("segmentlength")));var x,F;for(l=0;l<a.pathSegList.numberOfItems;l++)if(x=a.pathSegList.getItem(l),x.pathSegType!=SVGPathSeg.PATHSEG_CLOSEPATH)if(x.pathSegType==SVGPathSeg.PATHSEG_MOVETO_ABS)f(g),k=c(h,g,a,x.x,x.y),m=k.Clone();else if(x.pathSegType==SVGPathSeg.PATHSEG_MOVETO_REL)f(g),k=c(h,g,a,m.x+x.x,m.y+x.y),m=k.Clone();else if(x.pathSegType==SVGPathSeg.PATHSEG_LINETO_ABS)k=c(h,g,a,x.x,x.y);else if(x.pathSegType==
SVGPathSeg.PATHSEG_LINETO_REL)k=c(h,g,a,k.x+x.x,k.y+x.y);else if(x.pathSegType==SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS)k=c(h,g,a,x.x,k.y);else if(x.pathSegType==SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS)k=c(h,g,a,k.x,x.y);else if(x.pathSegType==SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL)k=c(h,g,a,k.x+x.x,k.y);else if(x.pathSegType==SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL)k=c(h,g,a,k.x,k.y+x.y);else if(x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS||x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL||
x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS||x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL||x.pathSegType==SVGPathSeg.PATHSEG_ARC_ABS||x.pathSegType==SVGPathSeg.PATHSEG_ARC_REL||x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS||x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL){F=[];if(x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS||x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL){for(;l<a.pathSegList.numberOfItems;l++){x=a.pathSegList.getItem(l);
if(!(x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS||x.pathSegType==SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL))break;F.push(x)}l-=1}else F.push(x);k=d(h,a,y,k,F,g)}e(g)}else if(a instanceof SVGRectElement)c(h,g,a,a.x.baseVal.value,a.y.baseVal.value),c(h,g,a,a.x.baseVal.value+a.width.baseVal.value,a.y.baseVal.value),c(h,g,a,a.x.baseVal.value+a.width.baseVal.value,a.y.baseVal.value+a.height.baseVal.value),c(h,g,a,a.x.baseVal.value,a.y.baseVal.value+a.height.baseVal.value);else if(a instanceof
SVGPolygonElement)for(l=0;l<a.points.numberOfItems;l++)k=a.points.getItem(l),c(h,g,a,k.x,k.y);g.color=function(a){for(var b="";null!==a&&void 0!==a&&0===b.length;)b=a.getAttribute("fill"),null===b&&(b=a.style.fill),a=a.parentElement;a=0;if(0===b.length)return a;if("#"==b[0])a=JSM.HexColorToRGBColor(b.substring(1));else{var c=b.indexOf("("),d=b.indexOf(")");if(-1==c||-1==d)return a;b=b.substring(c+1,d).split(", ");if(3!=b.length)return a;a=JSM.RGBComponentsToHexColor(b[0],b[1],b[2])}return a}(a);g.originalElem=
a;return g}function f(a,b,c){function d(a,b){var c=[],e,f;if(b==JSM.Orientation.Clockwise)for(e=0;e<a.VertexCount();e++)f=a.GetVertex(e),c.push(new JSM.Coord(f.x,0,-f.y));else for(e=a.VertexCount()-1;0<=e;e--)f=a.GetVertex(e),c.push(new JSM.Coord(f.x,0,-f.y));return c}function e(a,b,c){a.push(null);var d;if(c==JSM.Orientation.CounterClockwise)for(c=0;c<b.VertexCount();c++)d=b.GetVertex(c),a.push(new JSM.Coord(d.x,0,-d.y));else for(c=b.VertexCount()-1;0<=c;c--)d=b.GetVertex(c),a.push(new JSM.Coord(d.x,
0,-d.y))}var f=[],g=new JSM.Vector(0,-1,0);void 0!==a.originalElem&&a.originalElem.hasAttribute("modelheight")&&(b=parseFloat(a.originalElem.getAttribute("modelheight")));var h,k;k=a.ContourCount();if(1==k)k=a.GetContour(0).GetOrientation(),h=d(a.GetContour(0),k),h=JSM.GeneratePrism(h,g,b,!0,c),f.push(h);else if(1<k){k=a.GetContour(0).GetOrientation();var l=d(a.GetContour(0),k),m=!1,y;for(y=1;y<a.ContourCount();y++)h=a.GetContour(y).GetOrientation(),h==k?(h=d(a.GetContour(y),k),h=JSM.GeneratePrism(h,
g,b,!0,c),f.push(h)):(e(l,a.GetContour(y),h),m=!0);h=m?JSM.GeneratePrismWithHole(l,g,b,!0,c):JSM.GeneratePrism(l,g,b,!0,c);f.push(h)}a=new JSM.Material({ambient:a.color,diffuse:a.color});return[f,a]}var g=new JSM.Model,h=new JSM.Materials;c=function(a,b){function c(a,b,d){a=a.getElementsByTagName(b);for(b=0;b<a.length;b++)d.push(a[b])}var d=[],f=[];c(a,"path",f);c(a,"rect",f);c(a,"polygon",f);var g=b;a.hasAttribute("segmentlength")&&(g=parseFloat(a.getAttribute("segmentlength")));var h,k;for(h=0;h<
f.length;h++)k=e(f[h],g),d.push(k);return d}(a,c);a.hasAttribute("modelheight")&&(b=parseFloat(a.getAttribute("modelheight")));var l,k,m;for(a=0;a<c.length;a++){l=f(c[a],b,d);k=l[0];l=l[1];h.AddMaterial(l);for(l=0;l<k.length;l++)m=k[l],m.SetPolygonsMaterialIndex(h.Count()-1),g.AddBody(m)}return[g,h]};
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=[],t=[];f(h.GetNodes(),g,p,q,t,[]);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(t,g,b,
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,k=a[0],m=b[0],n=(a[1]-a[0])/c,p=(b[1]-b[0])/d;(function(){var a,b,e,f;for(a=0;a<=d;a++)for(b=0;b<=c;b++)e=k+b*n,f=m+a*p,e=g(a,b,e,f,h),l.AddVertex(new JSM.BodyVertex(e))})();(function(){var a,b,g,h,k,m;for(b=0;b<d;b++)for(a=0;a<c;a++)g=b*(c+1)+a,h=g+1,k=g+c+1,m=k+1,e?(h=new JSM.BodyPolygon([g,h,m]),f&&h.SetCurveGroup(0),l.AddPolygon(h),h=new JSM.BodyPolygon([g,m,k])):h=new JSM.BodyPolygon([g,h,m,k]),f&&h.SetCurveGroup(0),l.AddPolygon(h)})();
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)};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.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.viewportWidth=a.width;b.viewportHeight=a.height;b.viewport(0,0,b.viewportWidth,b.viewportHeight);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.RenderMaterial=function(a,b,c,d,e,f,g){this.ambient=a;this.diffuse=b;this.specular=c;this.shininess=d;this.texture=e;this.textureWidth=f;this.textureHeight=g;this.textureImage=this.textureBuffer=null;this.textureLoaded=!1};JSM.RenderMaterial.prototype.HasTexture=function(){return null!==this.texture&&this.textureLoaded};
JSM.RenderGeometry=function(){this.transformation=new JSM.Transformation;this.uvBuffer=this.normalBuffer=this.vertexBuffer=this.uvArray=this.normalArray=this.vertexArray=this.material=null};JSM.RenderGeometry.prototype.SetMaterial=function(a){this.material=a};JSM.RenderGeometry.prototype.GetMaterial=function(){return this.material};JSM.RenderGeometry.prototype.SetVertexArray=function(a){this.vertexArray=new Float32Array(a)};
JSM.RenderGeometry.prototype.SetNormalArray=function(a){this.normalArray=new Float32Array(a)};JSM.RenderGeometry.prototype.SetUVArray=function(a){this.uvArray=new Float32Array(a)};JSM.RenderGeometry.prototype.GetTransformation=function(){return this.transformation};JSM.RenderGeometry.prototype.GetTransformationMatrix=function(){return this.transformation.matrix};JSM.RenderGeometry.prototype.SetTransformation=function(a){this.transformation=a};JSM.RenderGeometry.prototype.GetVertexBuffer=function(){return this.vertexBuffer};
JSM.RenderGeometry.prototype.GetNormalBuffer=function(){return this.normalBuffer};JSM.RenderGeometry.prototype.GetUVBuffer=function(){return this.uvBuffer};JSM.RenderGeometry.prototype.VertexCount=function(){return parseInt(this.vertexArray.length/3,10)};JSM.RenderGeometry.prototype.GetVertex=function(a){return new JSM.Coord(this.vertexArray[3*a],this.vertexArray[3*a+1],this.vertexArray[3*a+2])};JSM.RenderGeometry.prototype.GetTransformedVertex=function(a){a=this.GetVertex(a);return this.transformation.Apply(a)};
JSM.Renderer=function(){this.geometries=this.light=this.camera=this.texShader=this.shader=this.context=this.canvas=null};JSM.Renderer.prototype.Init=function(a,b,c){return!JSM.IsWebGLEnabled()||!this.InitContext(a)||!this.InitView(b,c)||!this.InitShaders()||!this.InitBuffers()?!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);if(null===this.context)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);return!0};
JSM.Renderer.prototype.InitShaders=function(){function a(a){var b="";a&&(b="#define USETEXTURE");return[b,"uniform highp vec3 uPolygonAmbientColor;\nuniform highp vec3 uPolygonDiffuseColor;\nuniform highp vec3 uPolygonSpecularColor;\nuniform highp float uPolygonShininess;\nuniform highp vec3 uLightAmbientColor;\nuniform highp vec3 uLightDiffuseColor;\nuniform highp vec3 uLightSpecularColor;\nvarying highp vec3 vVertex;\nvarying highp vec3 vNormal;\nvarying highp vec3 vLight;\n#ifdef USETEXTURE\nvarying highp vec2 vUV;\nuniform sampler2D uSampler;\n#endif\nvoid main (void) {\n\thighp vec3 N = normalize (vNormal);\n\tif (!gl_FrontFacing) {\n\t\tN = -N;\n\t}\n\thighp vec3 L = normalize (-vLight);\n\thighp vec3 E = normalize (-vVertex);\n\thighp vec3 R = normalize (-reflect (L, N));\n\thighp vec3 ambientComponent = uPolygonAmbientColor * uLightAmbientColor;\n\thighp vec3 diffuseComponent = uPolygonDiffuseColor * uLightDiffuseColor * max (dot (N, L), 0.0);\n\thighp vec3 specularComponent = uPolygonSpecularColor * uLightSpecularColor * pow (max (dot (R, E), 0.0), uPolygonShininess);\n#ifdef USETEXTURE\n\thighp vec3 textureColor = texture2D (uSampler, vec2 (vUV.s, vUV.t)).xyz;\n\tambientComponent = textureColor * ambientComponent;\n\tdiffuseComponent = textureColor * diffuseComponent;\n\tspecularComponent = textureColor * specularComponent;\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, 1.0);\n}"].join("\n")}function b(a){var b=
"";a&&(b="#define USETEXTURE");return[b,"attribute highp vec3 aVertexPosition;\nattribute highp vec3 aVertexNormal;\nuniform highp mat4 uViewMatrix;\nuniform highp mat4 uModelViewMatrix;\nuniform highp mat4 uProjectionMatrix;\nuniform highp vec3 uLightDirection;\nvarying highp vec3 vVertex;\nvarying highp vec3 vNormal;\nvarying highp vec3 vLight;\n#ifdef USETEXTURE\nattribute highp vec2 aVertexUV;\nvarying highp vec2 vUV;\n#endif\nvoid main (void) {\n\tvVertex = vec3 (uModelViewMatrix * vec4 (aVertexPosition, 1.0));\n\tvNormal = normalize (vec3 (uModelViewMatrix * vec4 (aVertexNormal, 0.0)));\n\tvLight = normalize (vec3 (uViewMatrix * vec4 (uLightDirection, 0.0)));\n#ifdef USETEXTURE\n\tvUV = aVertexUV;\n#endif\n\tgl_Position = uProjectionMatrix * vec4 (vVertex, 1.0);\n}"].join("\n")}
function c(a,b){b.vertexPositionAttribute=a.getAttribLocation(b,"aVertexPosition");b.vertexNormalAttribute=a.getAttribLocation(b,"aVertexNormal");b.lightAmbientColorUniform=a.getUniformLocation(b,"uLightAmbientColor");b.lightDiffuseColorUniform=a.getUniformLocation(b,"uLightDiffuseColor");b.lightSpecularColorUniform=a.getUniformLocation(b,"uLightSpecularColor");b.lightDirectionUniform=a.getUniformLocation(b,"uLightDirection");b.vMatrixUniform=a.getUniformLocation(b,"uViewMatrix");b.mvMatrixUniform=
a.getUniformLocation(b,"uModelViewMatrix");b.pMatrixUniform=a.getUniformLocation(b,"uProjectionMatrix");b.polygonAmbientColorUniform=a.getUniformLocation(b,"uPolygonAmbientColor");b.polygonDiffuseColorUniform=a.getUniformLocation(b,"uPolygonDiffuseColor");b.polygonSpecularColorUniform=a.getUniformLocation(b,"uPolygonSpecularColor");b.polygonShininessUniform=a.getUniformLocation(b,"uPolygonShininess")}this.shader=function(d){var e=a(!1),f=b(!1),e=JSM.WebGLInitShaderProgram(d,f,e,null);if(null===e)return null;
d.useProgram(e);c(d,e);return e}(this.context);if(null===this.shader)return!1;this.texShader=function(d){var e=a(!0),f=b(!0),e=JSM.WebGLInitShaderProgram(d,f,e,null);if(null===e)return null;d.useProgram(e);c(d,e);e.vertexUVAttribute=d.getAttribLocation(e,"aVertexUV");e.samplerUniform=d.getUniformLocation(e,"uSampler");return e}(this.context);return null===this.texShader?!1:!0};JSM.Renderer.prototype.InitBuffers=function(){this.geometries=[];return!0};
JSM.Renderer.prototype.InitView=function(a,b){this.camera=JSM.ValueOrDefault(a,new JSM.Camera);if(!this.camera)return!1;this.light=JSM.ValueOrDefault(b,new JSM.Light);return!this.light?!1:!0};JSM.Renderer.prototype.SetClearColor=function(a,b,c){this.context.clearColor(a,b,c,1)};
JSM.Renderer.prototype.AddGeometries=function(a){function b(a,b,c){null!==a.texture&&(a.textureBuffer=b.createTexture(),a.textureImage=new Image,a.textureImage.src=a.texture,a.textureImage.onload=function(){b.bindTexture(b.TEXTURE_2D,a.textureBuffer);b.texParameteri(b.TEXTURE_2D,b.TEXTURE_MAG_FILTER,b.LINEAR);b.texParameteri(b.TEXTURE_2D,b.TEXTURE_MIN_FILTER,b.LINEAR_MIPMAP_LINEAR);b.texImage2D(b.TEXTURE_2D,0,b.RGBA,b.RGBA,b.UNSIGNED_BYTE,a.textureImage);b.generateMipmap(b.TEXTURE_2D);b.bindTexture(b.TEXTURE_2D,
null);a.textureLoaded=!0;void 0!==c&&null!==c&&c()})}function c(a,b){a.vertexBuffer=b.createBuffer();b.bindBuffer(b.ARRAY_BUFFER,a.vertexBuffer);b.bufferData(b.ARRAY_BUFFER,a.vertexArray,b.STATIC_DRAW);a.vertexBuffer.itemSize=3;a.vertexBuffer.numItems=parseInt(a.vertexArray.length/3,10);a.normalBuffer=b.createBuffer();b.bindBuffer(b.ARRAY_BUFFER,a.normalBuffer);b.bufferData(b.ARRAY_BUFFER,a.normalArray,b.STATIC_DRAW);a.normalBuffer.itemSize=3;a.normalBuffer.numItems=parseInt(a.normalArray.length/
3,10);null!==a.uvArray&&(a.uvBuffer=b.createBuffer(),b.bindBuffer(b.ARRAY_BUFFER,a.uvBuffer),b.bufferData(b.ARRAY_BUFFER,a.uvArray,b.STATIC_DRAW),a.uvBuffer.itemSize=2,a.uvBuffer.numItems=parseInt(a.uvArray.length/2,10))}var d,e;for(d=0;d<a.length;d++)e=a[d],b(e.material,this.context,this.Render.bind(this)),c(e,this.context),this.geometries.push(e)};JSM.Renderer.prototype.RemoveGeometries=function(){this.geometries=[]};
JSM.Renderer.prototype.Resize=function(){this.context.viewportWidth=this.canvas.width;this.context.viewportHeight=this.canvas.height;this.context.viewport(0,0,this.context.viewportWidth,this.context.viewportHeight)};
JSM.Renderer.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.context.viewportWidth/this.context.viewportHeight,this.camera.nearClippingPlane,this.camera.farClippingPlane),b=JSM.MatrixView(this.camera.eye,this.camera.center,this.camera.up),c=JSM.MatrixIdentity(),d=JSM.HexColorToNormalizedRGBComponents(this.light.ambient),e=JSM.HexColorToNormalizedRGBComponents(this.light.diffuse),
f=JSM.HexColorToNormalizedRGBComponents(this.light.specular);this.light.direction=JSM.CoordSub(this.camera.center,this.camera.eye).Normalize();var g,h,l,k,m,n;for(g=0;g<this.geometries.length;g++)m=this.geometries[g],c=m.GetMaterial().HasTexture()?this.texShader:this.shader,n!=c&&(n=c,this.context.useProgram(n),this.context.uniformMatrix4fv(n.pMatrixUniform,!1,a),this.context.uniformMatrix4fv(n.vMatrixUniform,!1,b),this.context.uniform3f(n.lightDirectionUniform,this.light.direction.x,this.light.direction.y,
this.light.direction.z),this.context.uniform3f(n.lightAmbientColorUniform,d[0],d[1],d[2]),this.context.uniform3f(n.lightDiffuseColorUniform,e[0],e[1],e[2]),this.context.uniform3f(n.lightSpecularColorUniform,f[0],f[1],f[2])),c=m.material.ambient,h=m.material.diffuse,l=m.material.specular,k=m.material.shininess,this.context.uniform3f(n.polygonAmbientColorUniform,c[0],c[1],c[2]),this.context.uniform3f(n.polygonDiffuseColorUniform,h[0],h[1],h[2]),this.context.uniform3f(n.polygonSpecularColorUniform,l[0],
l[1],l[2]),this.context.uniform1f(n.polygonShininessUniform,k),n==this.texShader&&(c=m.GetUVBuffer(),this.context.activeTexture(this.context.TEXTURE0),this.context.bindTexture(this.context.TEXTURE_2D,m.material.textureBuffer),this.context.bindBuffer(this.context.ARRAY_BUFFER,c),this.context.vertexAttribPointer(n.vertexUVAttribute,c.itemSize,this.context.FLOAT,!1,0,0),this.context.enableVertexAttribArray(n.vertexUVAttribute),this.context.uniform1i(n.samplerUniform,0)),c=JSM.MatrixMultiply(m.GetTransformationMatrix(),
b),this.context.uniformMatrix4fv(n.mvMatrixUniform,!1,c),c=m.GetVertexBuffer(),this.context.bindBuffer(this.context.ARRAY_BUFFER,c),this.context.enableVertexAttribArray(n.vertexPositionAttribute),this.context.vertexAttribPointer(n.vertexPositionAttribute,c.itemSize,this.context.FLOAT,!1,0,0),m=m.GetNormalBuffer(),this.context.bindBuffer(this.context.ARRAY_BUFFER,m),this.context.enableVertexAttribArray(n.vertexNormalAttribute),this.context.vertexAttribPointer(n.vertexNormalAttribute,m.itemSize,this.context.FLOAT,
!1,0,0),this.context.drawArrays(this.context.TRIANGLES,0,c.numItems)};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.viewportWidth=this.canvas.width;this.context.viewportHeight=this.canvas.height;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.viewportWidth=this.canvas.width;this.context.viewportHeight=this.canvas.height};
JSM.PointCloudRenderer.prototype.Render=function(){this.context.viewport(0,0,this.context.viewportWidth,this.context.viewportHeight);this.context.clear(this.context.COLOR_BUFFER_BIT|this.context.DEPTH_BUFFER_BIT);var a=JSM.MatrixPerspective(this.camera.fieldOfView*JSM.DegRad,this.context.viewportWidth/this.context.viewportHeight,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.ConvertBodyToRenderGeometries=function(a,b){var c=[],d=null,e=null,f=null,g=null;JSM.ExplodeBodyToTriangles(a,b,{hasConvexPolygons:!1,onGeometryStart:function(a){e=[];f=[];g=[];var b=JSM.HexColorToNormalizedRGBComponents(a.ambient),k=JSM.HexColorToNormalizedRGBComponents(a.diffuse),m=JSM.HexColorToNormalizedRGBComponents(a.specular);a=new JSM.RenderMaterial(b,k,m,a.shininess,a.texture,a.textureWidth,a.textureHeight);d=new JSM.RenderGeometry;d.SetMaterial(a);c.push(d)},onGeometryEnd:function(a){d.SetVertexArray(e);
d.SetNormalArray(f);null!==a.texture&&d.SetUVArray(g)},onTriangle:function(a,b,c,d,n,p,q,t,s){e.push(a.x,a.y,a.z);e.push(b.x,b.y,b.z);e.push(c.x,c.y,c.z);f.push(d.x,d.y,d.z);f.push(n.x,n.y,n.z);f.push(p.x,p.y,p.z);null!==q&&(null!==t&&null!==s)&&(g.push(q.x,q.y),g.push(t.x,t.y),g.push(s.x,s.y))}});return c};JSM.ConvertModelToRenderGeometries=function(a,b){var c=[],d,e;for(d=0;d<a.BodyCount();d++){e=a.GetBody(d);var f=JSM.ConvertBodyToRenderGeometries(e,b,c);for(e=0;e<f.length;e++)c.push(f[e])}return c};
JSM.JSONFileLoader=function(a){this.onReady=a};JSM.JSONFileLoader.prototype.Load=function(a){var b=this,c=new XMLHttpRequest;c.overrideMimeType("application/json");c.open("GET",a,!0);c.onreadystatechange=function(){4==c.readyState&&b.OnReady(c.responseText)};c.send(null)};JSM.JSONFileLoader.prototype.OnReady=function(a){null!==this.onReady&&(a=JSON.parse(a),this.onReady(a))};
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);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);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.Navigation=function(){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.cameraNearDistanceLimit=0.1;this.cameraFarDistanceLimit=1E3;this.orbitCenter=this.camera.center.Clone();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){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.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&&c<this.cameraNearDistanceLimit||!d&&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.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.navigation=this.renderer=null};
JSM.Viewer.prototype.Init=function(a,b,c){return!this.InitRenderer(a,b,c)||!this.InitNavigation()?!1:!0};JSM.Viewer.prototype.InitRenderer=function(a,b,c){this.renderer=new JSM.Renderer;return!this.renderer.Init(a,b,c)?!1:!0};JSM.Viewer.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.Viewer.prototype.SetClearColor=function(a,b,c){this.renderer.SetClearColor(a,b,c);this.Draw()};JSM.Viewer.prototype.AddGeometries=function(a){this.renderer.AddGeometries(a);this.Draw()};JSM.Viewer.prototype.RemoveGeometries=function(){this.renderer.RemoveGeometries();this.Draw()};JSM.Viewer.prototype.FitInWindow=function(){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.Draw()};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),c,d,e,f;for(c=0;c<this.renderer.geometries.length;c++){e=this.renderer.geometries[c];for(d=0;d<e.VertexCount();d+=1)f=e.GetTransformedVertex(d),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.Viewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,c,d,e,f;for(c=0;c<this.renderer.geometries.length;c++){e=this.renderer.geometries[c];for(d=0;d<e.VertexCount();d+=1)f=e.GetTransformedVertex(d),f=a.DistanceTo(f),JSM.IsGreater(f,b)&&(b=f)}return new JSM.Sphere(a,b)};JSM.Viewer.prototype.Resize=function(){this.renderer.Resize();this.Draw()};JSM.Viewer.prototype.Draw=function(){this.renderer.Render()};
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.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)};

View File

@ -1,30 +0,0 @@
/* JSModeler 0.38 - http://www.github.com/kovacsv/JSModeler */ 'use strict';JSM.ResizeImageToPowerOfTwoSides=function(a){if(JSM.IsPowerOfTwo(a.width)&&!JSM.IsPowerOfTwo(a.height))return a;var b=JSM.NextPowerOfTwo(a.width),d=JSM.NextPowerOfTwo(a.height),c=document.createElement("canvas");c.width=b;c.height=d;c=c.getContext("2d");c.drawImage(a,0,0,b,d);return c.getImageData(0,0,b,d)};
JSM.ConvertBodyToThreeMeshes=function(a,b,d){var c={textureLoadedCallback:null,hasConvexPolygons:!1,doubleSided:!0};void 0!==d&&null!==d&&(c.textureLoadedCallback=JSM.ValueOrDefault(d.textureLoadedCallback,c.textureLoadedCallback),c.hasConvexPolygons=JSM.ValueOrDefault(d.hasConvexPolygons,c.hasConvexPolygons),c.doubleSided=JSM.ValueOrDefault(d.doubleSided,c.doubleSided));var f=[],e=null;JSM.ExplodeBodyToTriangles(a,b,{hasConvexPolygons:c.hasConvexPolygons,onGeometryStart:function(){e=new THREE.Geometry},
onGeometryEnd:function(a){var b=null!==a.texture,d=1!==a.opacity,v=a.ambient,G=a.diffuse,k=a.specular,l=a.shininess;0===l&&(k=0,l=1);v=new THREE.MeshPhongMaterial({ambient:v,color:G,specular:k,shininess:l});c.doubleSided&&(v.side=THREE.DoubleSide);d&&(v.opacity=a.opacity,v.transparent=!0);if(b){var m=THREE.ImageUtils.loadTexture(a.texture,new THREE.UVMapping,function(){m.image=JSM.ResizeImageToPowerOfTwoSides(m.image);null!==c.textureLoadedCallback&&c.textureLoadedCallback()});m.wrapS=THREE.RepeatWrapping;
m.wrapT=THREE.RepeatWrapping;v.map=m}e.computeFaceNormals();a=new THREE.Mesh(e,v);f.push(a)},onTriangle:function(a,b,c,d,f,k,l,m,z){var n=e.vertices.length;e.vertices.push(new THREE.Vector3(a.x,a.y,a.z));e.vertices.push(new THREE.Vector3(b.x,b.y,b.z));e.vertices.push(new THREE.Vector3(c.x,c.y,c.z));a=new THREE.Face3(n+0,n+1,n+2);e.faces.push(a);null!==d&&(null!==f&&null!==k)&&(a=[],a.push(new THREE.Vector3(d.x,d.y,d.z)),a.push(new THREE.Vector3(f.x,f.y,f.z)),a.push(new THREE.Vector3(k.x,k.y,k.z)),
e.faces[e.faces.length-1].vertexNormals=a);null!==l&&(null!==m&&null!==z)&&(d=[],d.push(new THREE.Vector2(l.x,-l.y)),d.push(new THREE.Vector2(m.x,-m.y)),d.push(new THREE.Vector2(z.x,-z.y)),e.faceVertexUvs[0].push(d))}});return f};JSM.ConvertModelToThreeMeshes=function(a,b,d){var c=[],f=[],e,h;for(e=0;e<a.BodyCount();e++){f=a.GetBody(e);f=JSM.ConvertBodyToThreeMeshes(f,b,d);for(h=0;h<f.length;h++)c.push(f[h])}return c};
JSM.ConvertJSONDataToThreeMeshes=function(a,b,d){function c(a,c){function d(a){function h(a,b,c,d,e){c=new THREE.Vector2(a,b);JSM.IsZero(e)||(d=Math.sin(e*JSM.DegRad),e=Math.cos(e*JSM.DegRad),c.x=e*a-d*b,c.y=d*a+e*b);c.x=n[0]+c.x*u[0];c.y=n[1]+c.y*u[1];return c}var p=a.parameters,g=e[a.material];a=g.texture;var n=g.offset,u=g.scale,A=g.rotation,w=new THREE.Color,x=new THREE.Color,y=new THREE.Color,B=g.shininess;w.setRGB(g.ambient[0],g.ambient[1],g.ambient[2]);x.setRGB(g.diffuse[0],g.diffuse[1],g.diffuse[2]);
y.setRGB(g.specular[0],g.specular[1],g.specular[2]);if(void 0!==a&&null!==a){w.setRGB(1,1,1);x.setRGB(1,1,1);y.setRGB(1,1,1);if(void 0===n||null===n)n=[0,0];if(void 0===u||null===u)u=[1,1];if(void 0===A||null===A)A=0}0===B&&(y.setRGB(0,0,0),B=1);w=new THREE.MeshPhongMaterial({ambient:x.getHex(),color:x.getHex(),specular:y.getHex(),shininess:B,side:THREE.DoubleSide});1!==g.opacity&&(w.opacity=g.opacity,w.transparent=!0);void 0!==a&&null!==a&&(g=THREE.ImageUtils.loadTexture(a,new THREE.UVMapping,function(a){a.image=
JSM.ResizeImageToPowerOfTwoSides(a.image);void 0!==b&&null!==b&&b()}),g.wrapS=THREE.RepeatWrapping,g.wrapT=THREE.RepeatWrapping,w.map=g);var g=new THREE.Geometry,s,r,C,t,D,E,F,z,q;for(q=0;q<p.length;q+=9)s=3*p[q+0],r=3*p[q+1],C=3*p[q+2],t=3*p[q+3],D=3*p[q+4],E=3*p[q+5],x=2*p[q+6],y=2*p[q+7],B=2*p[q+8],F=g.vertices.length,z=g.faces.length,g.vertices.push(new THREE.Vector3(k[s+0],k[s+1],k[s+2])),g.vertices.push(new THREE.Vector3(k[r+0],k[r+1],k[r+2])),g.vertices.push(new THREE.Vector3(k[C+0],k[C+1],
k[C+2])),g.faces.push(new THREE.Face3(F+0,F+1,F+2)),s=[],s.push(new THREE.Vector3(l[t+0],l[t+1],l[t+2])),s.push(new THREE.Vector3(l[D+0],l[D+1],l[D+2])),s.push(new THREE.Vector3(l[E+0],l[E+1],l[E+2])),g.faces[z].vertexNormals=s,void 0!==a&&null!==a&&(t=[],t.push(h(m[x+0],m[x+1],n,u,A)),t.push(h(m[y+0],m[y+1],n,u,A)),t.push(h(m[B+0],m[B+1],n,u,A)),g.faceVertexUvs[0].push(t));p=new THREE.Mesh(g,w);p.originalJsonIndex=c;f.push(p)}var k=a.vertices;if(void 0!==k){var l=a.normals;if(void 0!==l){var m=a.uvs;
if(void 0!==m){var h=a.triangles,n;for(n=0;n<h.length;n++)d(h[n])}}}}var f=[],e=a.materials;if(void 0===e)return f;var h=a.meshes;if(void 0===h)return f;var r=0;JSM.AsyncRunTask(function(){c(h[r],r);r+=1;return!0},d,h.length,0,f);return f};JSM.JSONFileConverter=function(a,b){this.onReady=a;this.onTextureLoaded=b};JSM.JSONFileConverter.prototype.Convert=function(a){(new JSM.JSONFileLoader(this.OnReady.bind(this))).Load(a)};
JSM.JSONFileConverter.prototype.OnReady=function(a){null!==this.onReady&&(a=JSM.ConvertJSONDataToThreeMeshes(a,this.onTextureLoaded),this.onReady(a))};JSM.ThreeViewer=function(){this.enableDraw=this.drawLoop=this.settings=this.navigation=this.cameraMove=this.runAfterRender=this.runBeforeRender=this.directionalLight=this.ambientLight=this.renderer=this.camera=this.scene=this.canvas=null};
JSM.ThreeViewer.prototype.Start=function(a,b){if(!JSM.IsWebGLEnabled()||!this.InitSettings(b)||!this.InitThree(a)||!this.InitCamera(b)||!this.InitLights())return!1;this.drawLoop=!1;this.enableDraw=!0;this.DrawIfNeeded();return!0};
JSM.ThreeViewer.prototype.InitSettings=function(a){this.settings={cameraEyePosition:new JSM.Coord(1,1,1),cameraCenterPosition:new JSM.Coord(0,0,0),cameraUpVector:new JSM.Coord(0,0,1),lightAmbientColor:[0.5,0.5,0.5],lightDiffuseColor:[0.5,0.5,0.5]};void 0!==a&&(void 0!==a.cameraEyePosition&&(this.settings.cameraEyePosition=JSM.CoordFromArray(a.cameraEyePosition)),void 0!==a.cameraCenterPosition&&(this.settings.cameraCenterPosition=JSM.CoordFromArray(a.cameraCenterPosition)),void 0!==a.cameraUpVector&&
(this.settings.cameraUpVector=JSM.CoordFromArray(a.cameraUpVector)),void 0!==a.lightAmbientColor&&(this.settings.lightAmbientColor=a.lightAmbientColor),void 0!==a.lightDiffuseColor&&(this.settings.lightDiffuseColor=a.lightDiffuseColor));return!0};
JSM.ThreeViewer.prototype.InitThree=function(a){this.canvas=a;if(!this.canvas||!this.canvas.getContext)return!1;this.scene=new THREE.Scene;if(!this.scene)return!1;this.renderer=new THREE.WebGLRenderer({canvas:this.canvas,antialias:!0});if(!this.renderer)return!1;this.renderer.setClearColor(new THREE.Color(16777215));this.renderer.setSize(this.canvas.width,this.canvas.height);return!0};
JSM.ThreeViewer.prototype.InitCamera=function(a){this.cameraMove=new JSM.Camera(JSM.CoordFromArray(a.cameraEyePosition),JSM.CoordFromArray(a.cameraCenterPosition),JSM.CoordFromArray(a.cameraUpVector),a.fieldOfView,a.nearClippingPlane,a.farClippingPlane);if(!this.cameraMove)return!1;this.navigation=new JSM.Navigation;if(!this.navigation.Init(this.canvas,this.cameraMove,this.DrawIfNeeded.bind(this),this.Resize.bind(this)))return!1;this.camera=new THREE.PerspectiveCamera(this.cameraMove.fieldOfView,
this.canvas.width/this.canvas.height,this.cameraMove.nearClippingPlane,this.cameraMove.farClippingPlane);if(!this.camera)return!1;this.scene.add(this.camera);return!0};
JSM.ThreeViewer.prototype.InitLights=function(){var a=new THREE.Color,b=new THREE.Color;a.setRGB(this.settings.lightAmbientColor[0],this.settings.lightAmbientColor[1],this.settings.lightAmbientColor[2]);b.setRGB(this.settings.lightDiffuseColor[0],this.settings.lightDiffuseColor[1],this.settings.lightDiffuseColor[2]);this.ambientLight=new THREE.AmbientLight(a.getHex());if(!this.ambientLight)return!1;this.scene.add(this.ambientLight);this.directionalLight=new THREE.DirectionalLight(b.getHex());if(!this.directionalLight)return!1;
a=(new THREE.Vector3).subVectors(this.cameraMove.eye,this.cameraMove.center);this.directionalLight.position.set(a.x,a.y,a.z);this.scene.add(this.directionalLight);return!0};JSM.ThreeViewer.prototype.SetRunBeforeRender=function(a){this.runBeforeRender=a};JSM.ThreeViewer.prototype.SetRunAfterRender=function(a){this.runAfterRender=a};JSM.ThreeViewer.prototype.SetClearColor=function(a){this.renderer.setClearColor(new THREE.Color(a));this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.AddMesh=function(a){this.scene.add(a);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.AddMeshes=function(a){var b;for(b=0;b<a.length;b++)this.scene.add(a[b]);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.MeshCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=1)});return a};JSM.ThreeViewer.prototype.VertexCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=b.geometry.vertices.length)});return a};
JSM.ThreeViewer.prototype.FaceCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=b.geometry.faces.length)});return a};JSM.ThreeViewer.prototype.GetMesh=function(a){var b=null,d=0,c;for(c=0;c<this.scene.children.length;c++)if(b=this.scene.children[c],b instanceof THREE.Mesh){if(d==a)return b;d+=1}return null};JSM.ThreeViewer.prototype.RemoveMesh=function(a){a.geometry.dispose();this.scene.remove(a);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.RemoveMeshes=function(){var a,b;for(b=0;b<this.scene.children.length;b++)a=this.scene.children[b],a instanceof THREE.Mesh&&(a.geometry.dispose(),this.scene.remove(a),b--);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.RemoveLastMesh=function(){var a=null;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a=b)});null!==a&&this.scene.remove(a);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.SetCamera=function(a,b,d){this.navigation.SetCamera(a,b,d);this.navigation.SetOrbitCenter(b.Clone());this.DrawIfNeeded()};JSM.ThreeViewer.prototype.Resize=function(){this.camera.aspect=this.canvas.width/this.canvas.height;this.camera.updateProjectionMatrix();this.renderer.setSize(this.canvas.width,this.canvas.height);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.FitInWindow=function(){if(0!==this.MeshCount()){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.DrawIfNeeded()}};JSM.ThreeViewer.prototype.AdjustClippingPlanes=function(a){this.GetBoundingSphere().GetRadius()<a?(this.camera.near=0.1,this.camera.far=1E3):(this.camera.near=10,this.camera.far=1E6);this.camera.updateProjectionMatrix();this.Draw()};JSM.ThreeViewer.prototype.GetCenter=function(){return this.GetBoundingBox().GetCenter()};
JSM.ThreeViewer.prototype.GetBoundingBox=function(){var a=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),b=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),d,c;this.scene.traverse(function(f){if(f instanceof THREE.Mesh){d=f.geometry;var e;for(e=0;e<d.vertices.length;e++)c=d.vertices[e].clone(),c.add(f.position),a.x=JSM.Minimum(a.x,c.x),a.y=JSM.Minimum(a.y,c.y),a.z=JSM.Minimum(a.z,c.z),b.x=JSM.Maximum(b.x,c.x),b.y=JSM.Maximum(b.y,c.y),b.z=JSM.Maximum(b.z,c.z)}});return new JSM.Box(a,b)};
JSM.ThreeViewer.prototype.GetBoundingSphere=function(){var a=this.GetCenter(),b=0,d,c,f;this.scene.traverse(function(e){if(e instanceof THREE.Mesh){d=e.geometry;var h;for(h=0;h<d.vertices.length;h++)c=d.vertices[h].clone(),c.add(e.position),f=a.DistanceTo(new JSM.Coord(c.x,c.y,c.z)),JSM.IsGreater(f,b)&&(b=f)}});return new JSM.Sphere(a,b)};
JSM.ThreeViewer.prototype.GetObjectsUnderPosition=function(a,b){var d=2*(a/this.canvas.width)-1,c=2*-(b/this.canvas.height)+1,f=new THREE.Projector,e=this.camera.position,d=new THREE.Vector3(d,c,0.5);f.unprojectVector(d,this.camera);d.sub(e);d.normalize();return(new THREE.Raycaster(e,d)).intersectObjects(this.scene.children)};JSM.ThreeViewer.prototype.GetObjectsUnderMouse=function(){return this.GetObjectsUnderPosition(this.navigation.mouse.curr.x,this.navigation.mouse.curr.y)};
JSM.ThreeViewer.prototype.GetObjectsUnderTouch=function(){return this.GetObjectsUnderPosition(this.navigation.touch.curr.x,this.navigation.touch.curr.y)};JSM.ThreeViewer.prototype.ProjectVector=function(a,b,d){var c=this.canvas.width/2,f=this.canvas.height/2,e=new THREE.Projector;a=new THREE.Vector3(a,b,d);e.projectVector(a,this.camera);a.x=a.x*c+c;a.y=-(a.y*f)+f;return a};JSM.ThreeViewer.prototype.EnableDraw=function(a){this.enableDraw=a};
JSM.ThreeViewer.prototype.Draw=function(){if(this.enableDraw){null!==this.runBeforeRender&&this.runBeforeRender();this.camera.position.set(this.cameraMove.eye.x,this.cameraMove.eye.y,this.cameraMove.eye.z);this.camera.up.set(this.cameraMove.up.x,this.cameraMove.up.y,this.cameraMove.up.z);this.camera.lookAt(new THREE.Vector3(this.cameraMove.center.x,this.cameraMove.center.y,this.cameraMove.center.z));var a=(new THREE.Vector3).subVectors(this.cameraMove.eye,this.cameraMove.center);this.directionalLight.position.set(a.x,
a.y,a.z);this.renderer.render(this.scene,this.camera);null!==this.runAfterRender&&this.runAfterRender();this.drawLoop&&requestAnimationFrame(this.Draw.bind(this))}};JSM.ThreeViewer.prototype.DrawIfNeeded=function(){this.drawLoop||this.Draw()};JSM.ThreeViewer.prototype.StartDrawLoop=function(){this.drawLoop=!0;this.Draw()};

1633
jsmodeler/three.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -42,7 +42,7 @@ def BuildWikiExtension (rootFolder, resultFolder):
shutil.copytree (os.path.join (rootFolder, 'wikiextension'), targetFolder)
shutil.copy (os.path.join (rootFolder, 'jsmodeler', 'three.min.js'), os.path.join (targetFolder, 'three.min.js'))
shutil.copy (os.path.join (rootFolder, 'jsmodeler', 'jsmodeler.js'), os.path.join (targetFolder, 'jsmodeler.js'))
shutil.copy (os.path.join (rootFolder, 'jsmodeler', 'jsmodeler.viewer.js'), os.path.join (targetFolder, 'jsmodeler.viewer.js'))
shutil.copy (os.path.join (rootFolder, 'jsmodeler', 'jsmodeler.ext.three.js'), os.path.join (targetFolder, 'jsmodeler.ext.three.js'))
shutil.copy (os.path.join (rootFolder, 'embeddable', 'include', 'online3dembedder.js'), os.path.join (targetFolder, 'online3dembedder.js'))
def Main ():

View File

@ -14,7 +14,7 @@ FloatingDialog.prototype.Open = function (parameters)
buttonDiv.onclick = function () {
button.callback (dialog);
};
buttonsDiv.appendChild (buttonDiv);
parent.appendChild (buttonDiv);
}
if (this.dialogDiv !== null) {

View File

@ -29,7 +29,7 @@ ImporterApp.prototype.Init = function ()
var myThis = this;
var top = document.getElementById ('top');
var importerButtons = new ImporterButtons (top);
importerButtons.AddLogo ('Online 3D Viewer <span class="version">v 0.3</span>', function () { myThis.WelcomeDialog (); });
importerButtons.AddLogo ('Online 3D Viewer <span class="version">v 0.4</span>', function () { myThis.WelcomeDialog (); });
importerButtons.AddButton ('images/openfile.png', 'Open File', function () { myThis.OpenFile (); });
importerButtons.AddButton ('images/fitinwindow.png', 'Fit In Window', function () { myThis.FitInWindow (); });
importerButtons.AddButton ('images/fixup.png', 'Enable/Disable Fixed Up Vector', function () { myThis.SetFixUp (); });
@ -171,7 +171,7 @@ ImporterApp.prototype.GenerateMenu = function ()
infoTable.AddRow ('Triangle count', triangleCount);
}
function AddMaterial (importerMenu, material)
function AddMaterial (importerMenu, materialsGroup, material)
{
importerMenu.AddSubItem (materialsGroup, material.name, {
openCloseButton : {
@ -192,7 +192,7 @@ ImporterApp.prototype.GenerateMenu = function ()
});
}
function AddMesh (importerApp, importerMenu, mesh, meshIndex)
function AddMesh (importerApp, importerMenu, meshesGroup, mesh, meshIndex)
{
importerMenu.AddSubItem (meshesGroup, mesh.name, {
openCloseButton : {
@ -271,14 +271,14 @@ ImporterApp.prototype.GenerateMenu = function ()
var material;
for (i = 0; i < jsonData.materials.length; i++) {
material = jsonData.materials[i];
AddMaterial (importerMenu, material);
AddMaterial (importerMenu, materialsGroup, material);
}
var meshesGroup = AddDefaultGroup (importerMenu, 'Meshes');
var mesh;
for (i = 0; i < jsonData.meshes.length; i++) {
mesh = jsonData.meshes[i];
AddMesh (this, importerMenu, mesh, i);
AddMesh (this, importerMenu, meshesGroup, mesh, i);
}
};
@ -309,18 +309,18 @@ ImporterApp.prototype.Generate = function (progressBar)
function ShowMeshes (importerApp, progressBar, merge)
{
importerApp.inGenerate = true;
var environment = new JSM.AsyncEnvironment ({
var environment = {
onStart : function (taskCount) {
progressBar.Init (taskCount);
},
onProcess : function (currentTask) {
onProgress : function (currentTask) {
progressBar.Step (currentTask + 1);
},
onFinish : function () {
importerApp.GenerateMenu ();
importerApp.inGenerate = false;
}
});
};
if (merge) {
var jsonData = importerApp.viewer.GetJsonData ();

View File

@ -46,26 +46,26 @@ ImporterViewer.prototype.ShowAllMeshes = function (inEnvironment)
var myThis = this;
var currentMeshIndex = 0;
var environment = new JSM.AsyncEnvironment ({
var environment = {
onStart : function (taskCount/*, meshes*/) {
inEnvironment.OnStart (taskCount);
inEnvironment.onStart (taskCount);
myThis.viewer.EnableDraw (false);
},
onProcess : function (currentTask, meshes) {
onProgress : function (currentTask, meshes) {
while (currentMeshIndex < meshes.length) {
myThis.viewer.AddMesh (meshes[currentMeshIndex]);
currentMeshIndex = currentMeshIndex + 1;
}
inEnvironment.OnProcess (currentTask);
inEnvironment.onProgress (currentTask);
},
onFinish : function (meshes) {
myThis.AdjustClippingPlanes (50.0);
myThis.FitInWindow ();
myThis.viewer.EnableDraw (true);
myThis.viewer.Draw ();
inEnvironment.OnFinish (meshes);
inEnvironment.onFinish (meshes);
}
});
};
JSM.ConvertJSONDataToThreeMeshes (this.jsonData, this.Draw.bind (this), environment);
};

View File

@ -11,7 +11,7 @@
<script type="text/javascript" src="../jsmodeler/three.min.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.viewer.js"></script>
<script type="text/javascript" src="../jsmodeler/jsmodeler.ext.three.js"></script>
<script type="text/javascript" src="include/floatingdialog.js"></script>
<script type="text/javascript" src="include/importerviewer.js"></script>