-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D5.min.js
executable file
·1 lines (1 loc) · 10.4 KB
/
3D5.min.js
1
"use strict";function _possibleConstructorReturn(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function _toConsumableArray(t){if(Array.isArray(t)){for(var e=0,n=Array(t.length);t.length>e;e++)n[e]=t[e];return n}return Array.from(t)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function D3mod(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments[2],r=[e,n].sort(),i=_slicedToArray(r,2);e=i[0],n=i[1];var a=n-e;return e>t?t+a:t>n?t-a:t}function D3inherit(t,e){for(var n in e)t[n]=e[n];return t}var _createClass=function(){function t(t,e){for(var n=0;e.length>n;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),_slicedToArray=function(){function t(t,e){var n=[],r=!0,i=!1,a=void 0;try{for(var s,o=t[Symbol.iterator]();!(r=(s=o.next()).done)&&(n.push(s.value),!e||n.length!==e);r=!0);}catch(u){i=!0,a=u}finally{try{!r&&o["return"]&&o["return"]()}finally{if(i)throw a}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),P2=2*Math.PI,D3Map=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:5,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document.getElementsByTagName("canvas")[0].getContext("2d");_classCallCheck(this,t),this.surface=[0,0,e],this.camera=[0,0,0],this.size=[n.canvas.width,n.canvas.height],this.orientation=[0,0,0],this.children=[],this.Vertex=t.Vertex,this.Cube=t.Cube,this.Line=t.Line,this.RENDER_MODES=t.RENDER_MODES,this.MAPPINGS=t.MAPPINGS,this.SPLITTERS=t.SPLITTERS}return _createClass(t,[{key:"rotate",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-this.orientation[0],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return this.orientation[0]=D3mod(this.orientation[0]+t,0,P2),this.orientation[1]=D3mod(this.orientation[1]+e,0,P2),this.orientation[2]=D3mod(this.orientation[2]+n,0,P2),this}},{key:"translate",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-this.camera[0],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return this.camera[0]+=t,this.camera[1]+=e,this.camera[2]+=n,this.surface[2]+=n,this}},{key:"display",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:this.surface[2];return D3inherit(this.surface,[t,e,n]),this}},{key:"reset",value:function(){return this.orientation=[0,0,0],this.camera=[0,0,0],this}},{key:"add",value:function(){for(var t=this,e=arguments.length,n=Array(e),r=0;e>r;r++)n[r]=arguments[r];return n.every(function(t){return"number"==typeof t})?this.add(new(Function.prototype.bind.apply(this.Vertex,[null].concat(n,[this])))):n.every(function(t){return t instanceof Array})?n.forEach(function(e){return t.add.apply(t,_toConsumableArray(e))}):this.children.push(n.shift())}},{key:"segmentConnect",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.children,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document.getElementsByTagName("canvas")[0].getContext("2d");if(!(e instanceof Array&&e.every(function(e){return e instanceof t.Vertex})))throw"ENOVERTEX";for(var r=0;e.length>r;r++)if(r&&e[r].coords[2]>0)n.lineTo.apply(n,_toConsumableArray(e[r].coord2d));else if(!r&&e[r].coords[2]>=0)n.moveTo.apply(n,_toConsumableArray(e[r].coord2d));else try{var i=e[r].clip(e[r?r-1:r+1]).coord2d;if(Number.isNaN(i[0])||Number.isNaN(i[1])||!Number.isFinite(i[0])||!Number.isFinite(i[1]))break;r?n.lineTo.apply(n,_toConsumableArray(i)):n.moveTo.apply(n,_toConsumableArray(i))}catch(a){continue}return this}},{key:"v0",get:function(){return new D3Vertex(0,0,this.surface[2],this)}},{key:"n",get:function(){var t=[new this.Vertex(-this.size[0]/2,-this.size[1]/2,this.surface[2],this),new this.Vertex(-this.size[0]/2,this.size[1]/2,this.surface[2],this),new this.Vertex(this.size[0]/2,-this.size[1]/2,this.surface[2],this)],e=t[2].sub(t[0]),n=t[1].sub(t[0]),r=e.cross(n);return r}},{key:"field",get:function(){return 2*Math.atan(1/this.surface[2])}}],[{key:"parse",value:function(e,n,r){var i=new t(n,r);e=e.split("").filter(function(t){return!i.SPLITTERS.Ignore.includes(t)}).join("");var a=e.split(i.SPLITTERS.Group).map(function(t){return t.split(i.SPLITTERS.Data).map(function(t,e){return e?1*t:t})}),s=!0,o=!1,u=void 0;try{for(var h,c=a[Symbol.iterator]();!(s=(h=c.next()).done);s=!0){var l=h.value;switch(l.shift()){case i.MAPPINGS.Vertex:i.add.apply(i,_toConsumableArray(l));break;case i.MAPPINGS.Line:i.add(new i.Line(new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i)));break;case i.MAPPINGS.Cube:i.add(new i.Cube([new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i),new i.Vertex(l.shift(),l.shift(),l.shift(),i)]));break;default:throw"EBADPARSE"}}}catch(f){o=!0,u=f}finally{try{!s&&c.return&&c.return()}finally{if(o)throw u}}return i}}]),t}(),D3Renderable=function(){function t(){_classCallCheck(this,t),this.trans=[0,0,0],this.children=[],this.middlew=function(){}}return _createClass(t,[{key:"render",value:function(t){return this.middlew(t)}}]),t}(),D3Vertex=function(){function t(e,n,r,i){if(_classCallCheck(this,t),e instanceof t){var a=[e.x,e.y,e.z,e.map];e=a[0],n=a[1],r=a[2],i=a[3]}this.x=e,this.y=n,this.z=r,this.map=i,this._id=this.map.Vertex.idcnt++}return _createClass(t,[{key:"clip",value:function(e){if(!(e instanceof t))throw"ENOVERTEX";var n=this.map.v0.sub(e).dot(this.map.n)/this.sub(e).dot(this.map.n),r=this.sub(e);if(0>n||n>1||!this.map.n.dot(r))throw"ENOINTERSCT";return e.add(r.mult(n))}},{key:"add",value:function(e){if(!(e instanceof t))throw"ENOVERTEX";var n=new t(this);return n.x+=e.x,n.y+=e.y,n.z+=e.z,n}},{key:"sub",value:function(e){if(!(e instanceof t))throw"ENOVERTEX";var n=new t(this);return n.x-=e.x,n.y-=e.y,n.z-=e.z,n}},{key:"dot",value:function(e){if(!(e instanceof t))throw"ENOVERTEX";return this.x*e.x+this.y*e.y+this.z*e.z}},{key:"mult",value:function(e){if("string"==typeof e&&/^[0-9]+$/.test(e))e=1*e;else if("number"==typeof e)e=[e,e,e];else if(!(e instanceof Array))throw"EINVALNUM";var n=new t(this);return n.x*=e[0],n.y*=e[1],n.z*=e[2],n}},{key:"cross",value:function(e){if(!(p1 instanceof t))throw"ENOVERTEX";var n=new t(0,0,0,this.map);return n.x=this.y*e.z-this.z*e.y,n.y=this.x*e.z-this.z*e.x,n.z=this.x*e.y-this.y*e.x,n}},{key:"coords",get:function(){var t=Math.cos(this.map.orientation[0]),e=Math.cos(this.map.orientation[1]),n=Math.cos(this.map.orientation[2]),r=Math.sin(this.map.orientation[0]),i=Math.sin(this.map.orientation[1]),a=Math.sin(this.map.orientation[2]),s=this.x-this.map.camera[0],o=this.y-this.map.camera[1],u=this.z-this.map.camera[2],h=a*o,c=a*s,l=n*s,f=n*o,m=i*u,g=e*u;return[e*(h+l)-m,r*(g+i*(h+l))+t*(f-c),t*(g+i*(h+l))-r*(f-c)]}},{key:"scoords",get:function(){return new(Function.prototype.bind.apply(t,[null].concat(_toConsumableArray(this.coords),[this.map])))}},{key:"coord2d",get:function(){var t=this.coords,e=this.map.surface[2]/t[2];return[e*t[0]+this.map.surface[0],e*t[1]+this.map.surface[1]]}}]),t}(),D3Line=function(t){function e(t,n){_classCallCheck(this,e);var r=_possibleConstructorReturn(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return r.children=[t,n],r.middlew=function(t){return t.stroke()},r}return _inherits(e,t),_createClass(e,[{key:"render",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new D3Map,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document.getElementsByTagName("canvas")[0].getContext("2d");n.save();var r=this.children.map(function(n){return new e.Vertex(n.x+t.trans[0],n.y+t.trans[1],n.z+t.trans[2],e)});n.beginPath(),e.segmentConnect(r,n),this.middlew(n),n.closePath(),n.restore()}}],[{key:"make",value:function(t,n,r){return new e(new(Function.prototype.bind.apply(D3Vertex,[null].concat(_toConsumableArray(t),[r]))),new(Function.prototype.bind.apply(D3Vertex,[null].concat(_toConsumableArray(n),[r]))))}}]),e}(D3Renderable),D3Cube=function(t){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:e.matrix;_classCallCheck(this,e);var r=_possibleConstructorReturn(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return D3inherit(r.children,t),r.matrix=[],D3inherit(r.matrix,n),r}return _inherits(e,t),_createClass(e,[{key:"render",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new D3Map,e=this,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document.getElementsByTagName("canvas")[0].getContext("2d"),r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.RENDER_MODES.BOTH;n.save();var i=this.children.map(function(n){return new t.Vertex(n.x+e.trans[0],n.y+e.trans[1],n.z+e.trans[2],t)}),a=!0,s=!1,o=void 0;try{for(var u,h=this.matrix[Symbol.iterator]();!(a=(u=h.next()).done);a=!0){var c=u.value;n.beginPath(),t.segmentConnect(c.map(function(t){return i[t]}),n),this.middlew(),n.closePath(),(r&t.RENDER_MODES.STROKE)==t.RENDER_MODES.STROKE&&n.stroke(),(r&t.RENDER_MODES.FILL)==t.RENDER_MODES.FILL&&n.fill()}}catch(l){s=!0,o=l}finally{try{!a&&h.return&&h.return()}finally{if(s)throw o}}return n.restore(),this}}]),e}(D3Renderable);D3Cube.matrix=[[0,1,2,3],[4,5,6,7],[0,1,5,4],[3,2,6,7],[0,4,7,3],[1,5,6,2]],D3Vertex.idcnt=0,D3Map.Vertex=D3Vertex,D3Map.Cube=D3Cube,D3Map.Line=D3Line,D3Map.RENDER_MODES={FILL:1,STROKE:2,BOTH:3},D3Map.MAPPINGS={Cube:"c",Line:"l",Vertex:"p"},D3Map.SPLITTERS={Group:"|",Data:",",Ignore:[" ","\n"," ","(",")","[","]","{","}","<",">"]};