d3-3d
Version:
D3.js plugin for 3d visualization written in Typescript
3 lines (2 loc) • 6.17 kB
JavaScript
(function(i,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(i=typeof globalThis<"u"?globalThis:i||self,u(i.d33d={}))})(this,(function(i){"use strict";function u(r){return`M${r[0].projected.x},${r[0].projected.y}L${r[1].projected.x},${r[1].projected.y}L${r[2].projected.x},${r[2].projected.y}L${r[3].projected.x},${r[3].projected.y}Z`}function j(r){return r.x}function C(r){return r.y}function Z(r){return r.z}class c{constructor(){this._scale=1,this._origin={x:0,y:0},this._rotateX=0,this._rotateY=0,this._rotateZ=0,this._x=j,this._y=C,this._z=Z,this._rotationCenter={x:0,y:0,z:0}}scale(t){return t?(this._scale=t,this):this._scale}origin(t){return t?(this._origin=t,this):this._origin}rotationCenter(t){return t?(this._rotationCenter=t,this):this._rotationCenter}rotateX(t){return t?(this._rotateX=t,this):this._rotateX}rotateY(t){return t?(this._rotateY=t,this):this._rotateY}rotateZ(t){return t?(this._rotateZ=t,this):this._rotateZ}x(t){return typeof t=="function"?(this._x=t,this):this._x}y(t){return typeof t=="function"?(this._y=t,this):this._y}z(t){return typeof t=="function"?(this._z=t,this):this._z}}function l(r){const t=[...r,r[0]];let n=0;for(let e=0;e<r.length;e+=1){const o=t[e].rotated,s=t[e+1].rotated;n+=(s.x-o.x)*(s.y+o.y)}return n>0}function h(r){let t=0,n=0,e=0;for(let o=r.length-1;o>=0;o-=1){const s=r[o].rotated;t+=s.x,n+=s.y,e+=s.z}return{x:t/r.length,y:n/r.length,z:e/r.length}}function w(r,t){const n=t.rotateCenter,e={x:r.x-n.x,y:r.y-n.y,z:r.z-n.z},o=X(e,t.z),s=$(o,t.y),a=D(s,t.x);return{x:a.x+n.x,y:a.y+n.y,z:a.z+n.z}}function D(r,t){const n=Math.sin(t),e=Math.cos(t);return{x:r.x,y:r.y*e-r.z*n,z:r.y*n+r.z*e}}function $(r,t){const n=Math.sin(t),e=Math.cos(t);return{x:r.z*n+r.x*e,y:r.y,z:r.z*e-r.x*n}}function X(r,t){const n=Math.sin(t),e=Math.cos(t);return{x:r.x*e-r.y*n,y:r.x*n+r.y*e,z:r.z}}function _(r,t){return{x:t.origin.x+t.scale*r.x,y:t.origin.y+t.scale*r.y}}const d=(r,t)=>r.map(n=>{const e={x:t.x(n),y:t.y(n),z:t.z(n)},o=w(e,{x:t.rotateX,y:t.rotateY,z:t.rotateZ,rotateCenter:t.rotateCenter}),s=_(o,{scale:t.scale,origin:t.origin});return{...n,rotated:o,projected:s}});function y(r,t){const n=r;return n.centroid=h(n),n.ccw=l(n),n.face=t,n}class Y extends c{data(t){return t.map(n=>{const e=d(n,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()}),o=y([e[0],e[1],e[2],e[3]],"front"),s=y([e[7],e[6],e[5],e[4]],"back"),a=y([e[4],e[5],e[1],e[0]],"left"),f=y([e[3],e[2],e[6],e[7]],"right"),x=y([e[4],e[0],e[3],e[7]],"top"),g=y([e[1],e[5],e[6],e[2]],"bottom");return e.faces=[o,s,a,f,x,g],e.centroid={x:(a.centroid.x+f.centroid.x)/2,y:(x.centroid.y+g.centroid.y)/2,z:(o.centroid.z+s.centroid.z)/2},e})}draw(t){return u(t)}}function P(){return new Y}class M extends c{constructor(){super(...arguments),this._rows=1}rows(t){return t?(this._rows=t,this):this._rows}data(t){const n=this._rows,e=d(t,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()}),o=e.length/n,s=[];let a=0;for(let f=o-1;f>0;f-=1)for(let x=n-1;x>0;x-=1){const g=x+f*n,m=g-1,p=m-n+1,N=p-1,z=[e[g],e[p],e[N],e[m]];z.plane=`plane-${a}`,z.ccw=l(z),z.centroid=h(z),s.push(z),a+=1}return s}draw(t){return u(t)}}function R(){return new M}class b extends c{data(t){return t.map(n=>{const e=d(n,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()});return e.centroid=h(e),e})}}function L(){return new b}function S(r){const t=r[r.length-1];let n=`M${t.projected.x},${t.projected.y}`;for(let e=r.length-2;e>=0;e-=1){const o=r[e].projected;n+=`L${o.x},${o.y}`}return n}class A extends c{data(t){return t.map(n=>{const e=n.length/2,o=Math.trunc(e),s=d(n,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()});return s.centroid=o===e?h([s[e-1],s[e]]):s[o].rotated,s})}draw(t){return S(t)}}function T(){return new A}class k extends c{data(t){return t.map(n=>{const e=d(n,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()});return e.ccw=l(e),e.centroid=h(e),e})}draw(t){return u(t)}}function F(){return new k}class G extends c{data(t){return t.map(n=>{const e={x:this.x()(n),y:this.y()(n),z:this.z()(n)},o=w(e,{x:this.rotateX(),y:this.rotateY(),z:this.rotateZ(),rotateCenter:this.rotationCenter()}),s=_(e,{scale:this.scale(),origin:this.origin()});return{...n,rotated:o,centroid:o,projected:s}})}}function O(){return new G}function q(r){const t=r[r.length-1];let n=`M${t.projected.x},${t.projected.y}`;for(let e=r.length-2;e>=0;e-=1){const o=r[e].projected;n+=`L${o.x},${o.y}`}return`${n}Z`}class B extends c{data(t){return t.map(n=>{const e=d(n,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()});return e.ccw=l(e),e.centroid=h(e),e})}draw(t){return q(t)}}function E(){return new B}const H=r=>`M${r[0].projected.x},${r[0].projected.y}L${r[1].projected.x},${r[1].projected.y}L${r[2].projected.x},${r[2].projected.y}Z`;class I extends c{data(t){return t.map(n=>{const e=d(n,{origin:this.origin(),rotateCenter:this.rotationCenter(),rotateX:this.rotateX(),rotateY:this.rotateY(),rotateZ:this.rotateZ(),scale:this.scale(),x:this.x(),y:this.y(),z:this.z()});return e.ccw=l(e),e.centroid=h(e),e})}draw(t){return H(t)}}function J(){return new I}function K(r,t){const n=r.centroid.z,e=t.centroid.z;return n<e?-1:n>e?1:0}i.ccw=l,i.centroid=h,i.cubes3D=P,i.gridPlanes3D=R,i.lineStrips3D=T,i.lines3D=L,i.planes3D=F,i.points3D=O,i.polygons3D=E,i.sort=K,i.triangles3D=J,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=d3-3d.js.map