d3-3d
Version:
D3.js plugin for 3d visualization written in Typescript
2 lines • 13.2 kB
JavaScript
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.d33d={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){return e.x}function n(e){return e.y}function r(e){return e.z}function i(e,t){return o(a(t),e,t.rotateCenter)}function a(e){let t=Math.sin(e.x),n=Math.cos(e.x),r=Math.sin(e.y),i=Math.cos(e.y),a=Math.sin(e.z),o=Math.cos(e.z);return[i*o,-i*a,r,t*r*o+n*a,n*o-t*r*a,-t*i,t*a-n*r*o,n*r*a+t*o,n*i]}function o(e,t,n){let r=t.x-n.x,i=t.y-n.y,a=t.z-n.z;return{x:e[0]*r+e[1]*i+e[2]*a+n.x,y:e[3]*r+e[4]*i+e[5]*a+n.y,z:e[6]*r+e[7]*i+e[8]*a+n.z}}function s(e,t){let n=t.rotateCenter,r=1/(1-(e.z-n.z)/t.distance)-1;return{x:t.origin.x+t.scale*(e.x+(e.x-n.x)*r),y:t.origin.y+t.scale*(e.y+(e.y-n.y)*r)}}function c(e,t){let n=a({x:t.rotateX,y:t.rotateY,z:t.rotateZ,rotateCenter:t.rotateCenter});return e.map(e=>{let r={x:t.x(e),y:t.y(e),z:t.z(e)},i=o(n,r,t.rotateCenter),a=s(i,{scale:t.scale,origin:t.origin,rotateCenter:t.rotateCenter,distance:t.perspective??1/0});return{...e,rotated:i,projected:a}})}function l(e,t,n,r){let i=a({x:r.rotateX,y:r.rotateY,z:r.rotateZ,rotateCenter:r.rotateCenter}),o=r.rotateCenter,s=r.perspective??1/0,c=(e.x-r.origin.x)/r.scale-o.x,l=(e.y-r.origin.y)/r.scale-o.y,u=i[0]*c+i[3]*l+o.x,d=i[1]*c+i[4]*l+o.y,f=i[2]*c+i[5]*l+o.z,p=i[6]-(i[0]*c+i[3]*l)/s,m=i[7]-(i[1]*c+i[4]*l)/s,h=i[8]-(i[2]*c+i[5]*l)/s,g=n,_=g.x*p+g.y*m+g.z*h;if(Math.abs(_)<1e-9)return null;let v=(g.x*(t.x-u)+g.y*(t.y-d)+g.z*(t.z-f))/_;return v>=s?null:{x:u+v*p,y:d+v*m,z:f+v*h}}function u(e,t){let{origin:n,rotateCenter:r,scale:i}=e,o=a({x:e.rotateX,y:e.rotateY,z:e.rotateZ,rotateCenter:r}),s=r.x-(o[0]*r.x+o[1]*r.y+o[2]*r.z),c=r.y-(o[3]*r.x+o[4]*r.y+o[5]*r.z),l=r.z-(o[6]*r.x+o[7]*r.y+o[8]*r.z),u=[i*o[0],i*o[3],o[6],0,i*o[1],i*o[4],o[7],0,i*o[2],i*o[5],o[8],0,n.x+i*s,n.y+i*c,l,1],d=e.perspective??1/0;for(let e=0;e<16;e+=4){let a=u[e+3],o=u[e+2],s=a-(o-r.z*a)/d;if(u[e]+=(n.x+i*r.x)*(s-a),u[e+1]+=(n.y+i*r.y)*(s-a),u[e+3]=s,t){let{width:n,height:i,depth:c}=t;u[e]=2*u[e]/n-s,u[e+1]=s-2*u[e+1]/i,u[e+2]=c?(r.z*a-o+c*c*a/d)/c:0}}return new Float32Array(u)}function d(e,t,n=new Float32Array(e.length)){for(let r=0;r<e.length;r+=3){let i=e[r],a=e[r+1],o=e[r+2],s=t[3]*i+t[7]*a+t[11]*o+t[15];n[r]=(t[0]*i+t[4]*a+t[8]*o+t[12])/s,n[r+1]=(t[1]*i+t[5]*a+t[9]*o+t[13])/s,n[r+2]=t[2]*i+t[6]*a+t[10]*o+t[14]}return n}var f=class e{constructor(){this.view={scale:1,origin:{x:0,y:0},perspective:1/0,rotateX:0,rotateY:0,rotateZ:0,rotateCenter:{x:0,y:0,z:0}},this._x=t,this._y=n,this._z=r}scale(e){return e===void 0?this.view.scale:(this.view.scale=e,this)}origin(e){return e===void 0?this.view.origin:(this.view.origin=e,this)}perspective(e){return e===void 0?this.view.perspective:(this.view.perspective=e,this)}rotationCenter(e){return e===void 0?this.view.rotateCenter:(this.view.rotateCenter=e,this)}rotateX(e){return e===void 0?this.view.rotateX:(this.view.rotateX=e,this)}rotateY(e){return e===void 0?this.view.rotateY:(this.view.rotateY=e,this)}rotateZ(e){return e===void 0?this.view.rotateZ:(this.view.rotateZ=e,this)}x(e){return typeof e==`function`?(this._x=e,this):this._x}y(e){return typeof e==`function`?(this._y=e,this):this._y}z(e){return typeof e==`function`?(this._z=e,this):this._z}camera(t){if(!(t instanceof e))throw TypeError(`camera() expects camera3D() or a shape`);return this.view=t.view,this}unproject(e,t,n){return l(e,t,n,this.transformOptions())}matrix(e){return u(this.transformOptions(),e)}viewDirection(){let e=a({x:this.view.rotateX,y:this.view.rotateY,z:this.view.rotateZ,rotateCenter:this.view.rotateCenter});return{x:e[6],y:e[7],z:e[8]}}transformOptions(){return{...this.view,x:this.x(),y:this.y(),z:this.z()}}transformShape(e){let t=c(e,this.transformOptions());for(let n of Object.keys(e))n in t||Reflect.set(t,n,Reflect.get(e,n));return t}};function p(){return new f}function m(e){return h(e)>0}function h(e){let t=[...e,e[0]],n=0;for(let r=0;r<e.length;r+=1){let e=t[r].projected,i=t[r+1].projected;n+=(i.x-e.x)*(i.y+e.y)}return n}function g(e){let t=0,n=0,r=0;for(let i=e.length-1;i>=0;--i){let a=e[i].rotated;t+=a.x,n+=a.y,r+=a.z}return{x:t/e.length,y:n/e.length,z:r/e.length}}function _(e){return`${v(e.projected.x)},${v(e.projected.y)}`}function v(e){return Math.round(e*1e3)/1e3}function y(e){return`M${_(e[0])}L${_(e[1])}L${_(e[2])}L${_(e[3])}Z`}function b(e){let t=`M${_(e[e.length-1])}`;for(let n=e.length-2;n>=0;--n)t+=`L${_(e[n])}`;return t}let x=[`x`,`y`,`z`],S={x:[`y`,`z`],y:[`z`,`x`],z:[`x`,`y`]},C=[[0,0],[0,1],[1,0],[1,1]];var w=class extends f{data(e){let t=this.mirrored(e);return x.flatMap(n=>[this.wall(e,n,0,t),this.wall(e,n,1,t)])}draw(e){return y(e)+e.grid.map(e=>b(e)).join(``)}axes(e){return this.edges(e).filter(e=>e.chosen)}edges(e){let t=this.data(e),n=Math.max(...t.map(e=>Math.abs(h(e))))*1e-9,r=(e,r)=>h(t[x.indexOf(e)*2+r])>n,[i]=this.apply([{x:D(e.x),y:D(e.y),z:D(e.z)}]);return x.flatMap(t=>{let[n,a]=S[t],o=C.map(n=>this.edge(e,t,n,i.projected)),s=o.filter(({sides:[e,t]})=>r(n,e)!==r(a,t)),c=(s.length?s:o).reduce((e,t)=>A(t)>A(e)?t:e);return c.chosen=!0,o})}wall(e,t,n,r){let[i,a]=S[t],o=E(e[t])[n],[s,c]=E(e[i]),[l,u]=E(e[a]),d=[T(t,o,i,s,a,l),T(t,o,i,c,a,l),T(t,o,i,c,a,u),T(t,o,i,s,a,u)],f=this.apply(n===1===r?d:d.reverse());return f.axis=t,f.side=n,f.ccw=m(f),f.centroid=g(f),f.grid=[...O(e[i]).map(e=>this.apply([T(t,o,i,e,a,l),T(t,o,i,e,a,u)])),...O(e[a]).map(e=>this.apply([T(t,o,i,s,a,e),T(t,o,i,c,a,e)]))],f}edge(e,t,n,r){let[i,a]=S[t],[o,s]=n,c=E(e[i])[o],l=E(e[a])[s],[u,d]=this.apply(E(e[t]).map(e=>T(t,e,i,c,a,l))),f=this.apply(e[t].ticks.map(e=>T(t,e,i,c,a,l)));return f.axis=t,f.normal=k(u.projected,d.projected,r),f.sides=n,f.chosen=!1,f}mirrored(e){let t=[this.x(),this.y(),this.z()],n=e=>t.map(t=>t(e)),r={x:E(e.x)[0],y:E(e.y)[0],z:E(e.z)[0]},i=n(r),[a,o,s]=x.map(t=>n({...r,[t]:E(e[t])[1]}).map((e,t)=>e-i[t]));return a[0]*(o[1]*s[2]-o[2]*s[1])-a[1]*(o[0]*s[2]-o[2]*s[0])+a[2]*(o[0]*s[1]-o[1]*s[0])<0}apply(e){return c(e,this.transformOptions())}};function T(e,t,n,r,i,a){return{[e]:t,[n]:r,[i]:a}}function E(e){return[e.domain[0],e.domain[e.domain.length-1]]}function D(e){let[t,n]=E(e);return(t+n)/2}function O(e){let[t,n]=E(e),r=Math.min(t,n),i=Math.max(t,n);return e.ticks.filter(e=>e>r&&e<i)}function k(e,t,n){let r=t.x-e.x,i=t.y-e.y,a=(e.x+t.x)/2-n.x,o=(e.y+t.y)/2-n.y,s=(a*r+o*i)/(r*r+i*i||1),c=a-s*r,l=o-s*i,u=Math.hypot(c,l)||1;return{x:c/u,y:l/u}}function A(e){return e.normal.y-e.normal.x/2}function j(){return new w}function M(e,t){let n=e;return n.centroid=g(n),n.ccw=m(n),n.face=t,n}var N=class extends f{data(e){return e.map(e=>{let t=this.transformShape(e),n=M([t[0],t[1],t[2],t[3]],`front`),r=M([t[7],t[6],t[5],t[4]],`back`),i=M([t[4],t[5],t[1],t[0]],`left`),a=M([t[3],t[2],t[6],t[7]],`right`),o=M([t[4],t[0],t[3],t[7]],`top`),s=M([t[1],t[5],t[6],t[2]],`bottom`);return t.faces=[n,r,i,a,o,s],t.centroid={x:(i.centroid.x+a.centroid.x)/2,y:(o.centroid.y+s.centroid.y)/2,z:(n.centroid.z+r.centroid.z)/2},t})}draw(e){return y(e)}};function ee(){return new N}var te=class extends f{constructor(...e){super(...e),this._rows=1}rows(e){return e?(this._rows=e,this):this._rows}data(e){let t=this._rows,n=c(e,this.transformOptions()),r=n.length/t,i=[],a=0;for(let e=r-1;e>0;--e)for(let r=t-1;r>0;--r){let o=r+e*t,s=o-1,c=s-t+1,l=c-1,u=[n[o],n[c],n[l],n[s]];u.plane=`plane-${a}`,u.ccw=m(u),u.centroid=g(u),i.push(u),a+=1}return i}draw(e){return y(e)}};function P(){return new te}function F(e){return`${b(e)}Z`}let I=Math.PI/2,L=Math.PI/4;function R(e,t,n,r,a,o){let s=i(e,r),c=V(t,r),l=V(n,r);return`matrix(${c.x*a} ${c.y*a} ${l.x*a} ${l.y*a} ${o.x+s.x*a} ${o.y+s.y*a})`}function z(e){let t=Math.min(e.r??0,e.w/2,e.d/2),n=e.z,r=n+e.h;if(t<=0)return{wall:G(e),top:K(e)};let i=U(e,t),a=H(e,t);return{wall:[...i.map(e=>({x:e.x,y:e.y,z:n})),...[...i].reverse().map(e=>({x:e.x,y:e.y,z:r}))],top:a.map(e=>({x:e.x,y:e.y,z:r}))}}function B(e,t=.09,n=4/3){let r=1/0,i=1/0,a=-1/0,o=-1/0;for(let t of e)t.x<r&&(r=t.x),t.x>a&&(a=t.x),t.y<i&&(i=t.y),t.y>o&&(o=t.y);let s=t*Math.max(a-r,o-i),c=a-r+2*s,l=o-i+2*s,u=0,d=0;if(c/l<n){let e=l*n;u=(e-c)/2,c=e}else{let e=c/n;d=(e-l)/2,l=e}return{viewBox:`${r-s-u} ${i-s-d} ${c} ${l}`,width:c,height:l}}function V(e,t){return i(e,{...t,rotateCenter:{x:0,y:0,z:0}})}function H(e,t){let{x:n,y:r,w:i,d:a}=e;return[...W(n+i-t,r+t,t,-I,0),...W(n+i-t,r+a-t,t,0,I),...W(n+t,r+a-t,t,I,Math.PI),...W(n+t,r+t,t,Math.PI,3*I)]}function U(e,t){let{x:n,y:r,w:i,d:a}=e;return[...W(n+i-t,r+t,t,-L,0),...W(n+i-t,r+a-t,t,0,I),...W(n+t,r+a-t,t,I,3*L)]}function W(e,t,n,r,i){let a=[];for(let o=0;o<=10;o++){let s=r+(i-r)*o/10;a.push({x:e+n*Math.cos(s),y:t+n*Math.sin(s)})}return a}function G(e){let{x:t,y:n,z:r,w:i,d:a,h:o}=e,s=r+o;return[{x:t,y:n+a,z:r},{x:t+i,y:n+a,z:r},{x:t+i,y:n,z:r},{x:t+i,y:n,z:s},{x:t+i,y:n+a,z:s},{x:t,y:n+a,z:s}]}function K(e){let{x:t,y:n,z:r,w:i,d:a,h:o}=e,s=r+o;return[{x:t,y:n,z:s},{x:t+i,y:n,z:s},{x:t+i,y:n+a,z:s},{x:t,y:n+a,z:s}]}var q=class extends f{data(e){return e.map(e=>this.box(e))}plane(e){return{matrix:R(e.origin,e.u,e.v,this.angles(),this.scale(),this.origin())}}path(e){return b(this.apply(e))}project(e){return this.apply(e).map(e=>e.projected)}frame(e,t,n){return B(this.apply(e).map(e=>e.projected),t,n)}box(e){let t={...e,x:this.x()(e),y:this.y()(e),z:this.z()(e)},n=z(t),r=t.z+t.h;return{centroid:this.apply([J(t)])[0].rotated,wall:F(this.apply(n.wall)),top:F(this.apply(n.top)),topPlane:this.plane({origin:{x:t.x,y:t.y,z:r},u:{x:1,y:0,z:0},v:{x:0,y:1,z:0}}),frontPlane:this.plane({origin:{x:t.x,y:t.y+t.d,z:r},u:{x:1,y:0,z:0},v:{x:0,y:0,z:-1}}),sidePlane:this.plane({origin:{x:t.x+t.w,y:t.y+t.d,z:r},u:{x:0,y:-1,z:0},v:{x:0,y:0,z:-1}})}}angles(){return{x:this.rotateX(),y:this.rotateY(),z:this.rotateZ(),rotateCenter:this.rotationCenter()}}apply(e){return c(e,{...this.transformOptions(),perspective:1/0,x:e=>e.x,y:e=>e.y,z:e=>e.z})}};function J(e){return{x:e.x+e.w/2,y:e.y+e.d/2,z:e.z+e.h/2}}function Y(){return new q}var ne=class extends f{data(e){return e.map(e=>{let t=this.transformShape(e);return t.centroid=g(t),t})}draw(e){return b(e)}};function X(){return new ne}function Z(e){return b(e)}var re=class extends f{data(e){return e.map(e=>{let t=e.length/2,n=Math.trunc(t),r=this.transformShape(e);return r.centroid=n===t?g([r[t-1],r[t]]):r[n].rotated,r})}draw(e){return Z(e)}};function ie(){return new re}var ae=class extends f{data(e){return e.map(e=>{let t=this.transformShape(e);return t.ccw=m(t),t.centroid=g(t),t})}draw(e){return y(e)}};function oe(){return new ae}var se=class extends f{data(e){return c(e,this.transformOptions()).map(e=>{let t=e;return t.centroid=t.rotated,t})}};function ce(){return new se}var le=class extends f{data(e){return e.map(e=>{let t=this.transformShape(e);return t.ccw=m(t),t.centroid=g(t),t})}draw(e){return F(e)}};function ue(){return new le}function de(e){return`M${_(e[0])}L${_(e[1])}L${_(e[2])}Z`}var fe=class extends f{data(e){return e.map(e=>{let t=this.transformShape(e);return t.ccw=m(t),t.centroid=g(t),t})}draw(e){return de(e)}};function pe(){return new fe}function me(e,t){let n=e.centroid.z,r=t.centroid.z;return n<r?-1:+(n>r)}function Q(e){let t=0,n=0,r=0;e.forEach((i,a)=>{let o=e[(a+1)%e.length];t+=(o.y-i.y)*(i.z+o.z),n+=(o.z-i.z)*(i.x+o.x),r+=(o.x-i.x)*(i.y+o.y)});let i=Math.hypot(t,n,r);return i===0?{x:0,y:0,z:0}:{x:t/i,y:n/i,z:r/i}}function he(e){let{x:t,y:n,z:r}=e,i=e.width/2,a=e.depth/2,o=(e.topWidth??e.width)/2,s=(e.topDepth??e.depth)/2,c=n-e.height;return[{x:t-o,y:c,z:r+s},{x:t-i,y:n,z:r+a},{x:t+i,y:n,z:r+a},{x:t+o,y:c,z:r+s},{x:t-o,y:c,z:r-s},{x:t-i,y:n,z:r-a},{x:t+i,y:n,z:r-a},{x:t+o,y:c,z:r-s}]}function $(e,t){let n=(e,n)=>{let r=n/t*Math.PI*2;return{x:e.x*Math.cos(r),y:e.y,z:e.x*Math.sin(r)}},r=[];for(let i=0;i<t;i+=1)for(let t=0;t<e.length-1;t+=1){let a=n(e[t],i),o=n(e[t],i+1),s=n(e[t+1],i+1),c=n(e[t+1],i);e[t+1].x!==0&&r.push([a,c,s]),e[t].x!==0&&r.push([a,s,o])}return r}function ge(e,t){let n=e.radius,r=Math.ceil(t/2),i=[{x:0,y:n}];for(let e=1;e<r;e+=1){let t=e/r*Math.PI;i.push({x:n*Math.sin(t),y:n*Math.cos(t)})}return i.push({x:0,y:-n}),$(i,t).map(t=>t.map(t=>({x:t.x+e.x,y:t.y+e.y,z:t.z+e.z})))}function _e(e,t){let n=Math.max(1,t-1),r=[];for(let t=0;t<e.length-1;t+=n)r.push(e.slice(t,t+n+1));return r}function ve(e,t){let n=e.length-1;if(n<0)return[];let r=t=>e[Math.max(0,Math.min(n,t))],i=[];for(let e=0;e<n*t;e+=1){let n=Math.floor(e/t),a=e%t/t,o=r(n-1),s=r(n),c=r(n+1),l=r(n+2),u=e=>.5*(2*s[e]+(c[e]-o[e])*a+(2*o[e]-5*s[e]+4*c[e]-l[e])*a**2+(3*s[e]-o[e]-3*c[e]+l[e])*a**3);i.push({x:u(`x`),y:u(`y`),z:u(`z`)})}let a=e[n];return i.push({x:a.x,y:a.y,z:a.z}),i}function ye(e,t){return{domain:e.domain(),ticks:e.ticks(t)}}function be(e,t){return{x:t.origin.x+t.scale*e.x,y:t.origin.y+t.scale*e.y}}e.axes3D=j,e.axisSpec=ye,e.box=he,e.camera3D=p,e.catmullRom=ve,e.ccw=m,e.centroid=g,e.cubes3D=ee,e.gridPlanes3D=P,e.iso3D=Y,e.lathe=$,e.lineStrips3D=ie,e.lines3D=X,e.normal=Q,e.orthographic=be,e.perspective=s,e.planes3D=oe,e.points3D=ce,e.polygons3D=ue,e.projectPositions=d,e.sort=me,e.sphere=ge,e.strips=_e,e.transform=c,e.triangles3D=pe});
//# sourceMappingURL=d3-3d.js.map