summaryrefslogtreecommitdiff
path: root/graphics/asymptote/base
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2020-03-04 03:02:32 +0000
committerNorbert Preining <norbert@preining.info>2020-03-04 03:02:32 +0000
commit19d25b8009801aa98ea2f46b45c37c257f990491 (patch)
treec0da70fbab26feff40e87df289ae797917df8419 /graphics/asymptote/base
parentb78f5a6705512314d6fe42fd5144c5f8e5d484ce (diff)
CTAN sync 202003040302
Diffstat (limited to 'graphics/asymptote/base')
-rw-r--r--graphics/asymptote/base/graph3.asy220
-rw-r--r--graphics/asymptote/base/palette.asy9
-rw-r--r--graphics/asymptote/base/shaders/fragment.glsl20
-rw-r--r--graphics/asymptote/base/shaders/vertex.glsl2
-rw-r--r--graphics/asymptote/base/solids.asy3
-rw-r--r--graphics/asymptote/base/three.asy146
-rw-r--r--graphics/asymptote/base/three_arrows.asy44
-rw-r--r--graphics/asymptote/base/three_light.asy4
-rw-r--r--graphics/asymptote/base/three_surface.asy485
-rw-r--r--graphics/asymptote/base/three_tube.asy490
-rw-r--r--graphics/asymptote/base/tube.asy36
-rw-r--r--graphics/asymptote/base/webgl/asygl.js2
12 files changed, 729 insertions, 732 deletions
diff --git a/graphics/asymptote/base/graph3.asy b/graphics/asymptote/base/graph3.asy
index ed09830974..1fe63490c9 100644
--- a/graphics/asymptote/base/graph3.asy
+++ b/graphics/asymptote/base/graph3.asy
@@ -1547,6 +1547,33 @@ path3[] segment(triple[] v, bool[] cond, interpolate3 join=operator --)
segment.length);
}
+bool uperiodic(real[][] a) {
+ int n=a.length;
+ if(n == 0) return false;
+ int m=a[0].length;
+ real[] a0=a[0];
+ real[] a1=a[n-1];
+ for(int j=0; j < m; ++j) {
+ real norm=0;
+ for(int i=0; i < n; ++i)
+ norm=max(norm,abs(a[i][j]));
+ real epsilon=sqrtEpsilon*norm;
+ if(abs(a0[j]-a1[j]) > epsilon) return false;
+ }
+ return true;
+}
+bool vperiodic(real[][] a) {
+ int n=a.length;
+ if(n == 0) return false;
+ int m=a[0].length-1;
+ for(int i=0; i < n; ++i) {
+ real[] ai=a[i];
+ real epsilon=sqrtEpsilon*norm(ai);
+ if(abs(ai[0]-ai[m]) > epsilon) return false;
+ }
+ return true;
+}
+
bool uperiodic(triple[][] a) {
int n=a.length;
if(n == 0) return false;
@@ -1614,7 +1641,7 @@ surface surface(triple[][] f, bool[][] cond={})
if(uperiodic(f)) s.ucyclic(true);
if(vperiodic(f)) s.vcyclic(true);
}
-
+
return s;
}
@@ -1694,10 +1721,114 @@ surface bispline(real[][] z, real[][] p, real[][] q, real[][] r,
}
}
}
-
+
+ return s;
+}
+
+private real[][][] bispline0(real[][] z, real[][] p, real[][] q, real[][] r,
+ real[] x, real[] y, bool[][] cond={})
+{ // z[i][j] is the value at (x[i],y[j])
+ // p and q are the first derivatives with respect to x and y, respectively
+ // r is the second derivative ddu/dxdy
+ int n=x.length-1;
+ int m=y.length-1;
+
+ bool all=cond.length == 0;
+
+ int count;
+ if(all)
+ count=n*m;
+ else {
+ count=0;
+ for(int i=0; i < n; ++i) {
+ bool[] condi=cond[i];
+ bool[] condp=cond[i+1];
+ for(int j=0; j < m; ++j)
+ if(all || (condi[j] && condi[j+1] && condp[j] && condp[j+1]))
+ ++count;
+ }
+ }
+
+ real[][][] s=new real[count][][];
+ int k=0;
+ for(int i=0; i < n; ++i) {
+ int ip=i+1;
+ real xi=x[i];
+ real xp=x[ip];
+ real hx=(xp-xi)/3;
+ real[] zi=z[i];
+ real[] zp=z[ip];
+ real[] ri=r[i];
+ real[] rp=r[ip];
+ real[] pi=p[i];
+ real[] pp=p[ip];
+ real[] qi=q[i];
+ real[] qp=q[ip];
+ bool[] condi=all ? null : cond[i];
+ bool[] condp=all ? null : cond[i+1];
+ for(int j=0; j < m; ++j) {
+ if(all || (condi[j] && condi[j+1] && condp[j] && condp[j+1])) {
+ real yj=y[j];
+ int jp=j+1;
+ real yp=y[jp];
+ real hy=(yp-yj)/3;
+ real hxy=hx*hy;
+ real zij=zi[j];
+ real zip=zi[jp];
+ real zpj=zp[j];
+ real zpp=zp[jp];
+ real pij=hx*pi[j];
+ real ppj=hx*pp[j];
+ real qip=hy*qi[jp];
+ real qpp=hy*qp[jp];
+ real zippip=zip+hx*pi[jp];
+ real zppmppp=zpp-hx*pp[jp];
+ real zijqij=zij+hy*qi[j];
+ real zpjqpj=zpj+hy*qp[j];
+
+ s[k]=new real[][] {{zij,zijqij,zip-qip,zip},
+ {zij+pij,zijqij+pij+hxy*ri[j],
+ zippip-qip-hxy*ri[jp],zippip},
+ {zpj-ppj,zpjqpj-ppj-hxy*rp[j],
+ zppmppp-qpp+hxy*rp[jp],zppmppp},
+ {zpj,zpjqpj,zpp-qpp,zpp}};
+ ++k;
+ }
+ }
+ }
+
return s;
}
+// return the surface values described by a real matrix f, interpolated with
+// xsplinetype and ysplinetype.
+real[][][] bispline(real[][] f, real[] x, real[] y,
+ splinetype xsplinetype=null,
+ splinetype ysplinetype=xsplinetype, bool[][] cond={})
+{
+ real epsilon=sqrtEpsilon*norm(y);
+ if(xsplinetype == null)
+ xsplinetype=(abs(x[0]-x[x.length-1]) <= epsilon) ? periodic : notaknot;
+ if(ysplinetype == null)
+ ysplinetype=(abs(y[0]-y[y.length-1]) <= epsilon) ? periodic : notaknot;
+ int n=x.length; int m=y.length;
+ real[][] ft=transpose(f);
+ real[][] tp=new real[m][];
+ for(int j=0; j < m; ++j)
+ tp[j]=xsplinetype(x,ft[j]);
+ real[][] q=new real[n][];
+ for(int i=0; i < n; ++i)
+ q[i]=ysplinetype(y,f[i]);
+ real[][] qt=transpose(q);
+ real[] d1=xsplinetype(x,qt[0]);
+ real[] d2=xsplinetype(x,qt[m-1]);
+ real[][] r=new real[n][];
+ real[][] p=transpose(tp);
+ for(int i=0; i < n; ++i)
+ r[i]=clamped(d1[i],d2[i])(y,p[i]);
+ return bispline0(f,p,q,r,x,y,cond);
+}
+
// return the surface described by a real matrix f, interpolated with
// xsplinetype and ysplinetype.
surface surface(real[][] f, real[] x, real[] y,
@@ -1802,6 +1933,91 @@ surface surface(triple f(pair z), pair a, pair b, int nu=nmesh, int nv=nu,
return surface(v,active);
}
+// return the surface described by a parametric function f evaluated at u and v
+// and interpolated with usplinetype and vsplinetype.
+surface surface(triple f(pair z), real[] u, real[] v,
+ splinetype[] usplinetype, splinetype[] vsplinetype=Spline,
+ bool cond(pair z)=null)
+{
+ int nu=u.length-1;
+ int nv=v.length-1;
+ real[] ipt=sequence(u.length);
+ real[] jpt=sequence(v.length);
+ real[][] fx=new real[u.length][v.length];
+ real[][] fy=new real[u.length][v.length];
+ real[][] fz=new real[u.length][v.length];
+
+ bool[][] active;
+ bool all=cond == null;
+ if(!all) active=new bool[u.length][v.length];
+
+ for(int i=0; i <= nu; ++i) {
+ real ui=u[i];
+ real[] fxi=fx[i];
+ real[] fyi=fy[i];
+ real[] fzi=fz[i];
+ bool[] activei=all ? null : active[i];
+ for(int j=0; j <= nv; ++j) {
+ pair z=(ui,v[j]);
+ if(!all) activei[j]=cond(z);
+ triple f=f(z);
+ fxi[j]=f.x;
+ fyi[j]=f.y;
+ fzi[j]=f.z;
+ }
+ }
+
+ if(usplinetype.length == 0) {
+ usplinetype=new splinetype[] {uperiodic(fx) ? periodic : notaknot,
+ uperiodic(fy) ? periodic : notaknot,
+ uperiodic(fz) ? periodic : notaknot};
+ } else if(usplinetype.length != 3) abort("usplinetype must have length 3");
+
+ if(vsplinetype.length == 0) {
+ vsplinetype=new splinetype[] {vperiodic(fx) ? periodic : notaknot,
+ vperiodic(fy) ? periodic : notaknot,
+ vperiodic(fz) ? periodic : notaknot};
+ } else if(vsplinetype.length != 3) abort("vsplinetype must have length 3");
+
+ real[][][] sx=bispline(fx,ipt,jpt,usplinetype[0],vsplinetype[0],active);
+ real[][][] sy=bispline(fy,ipt,jpt,usplinetype[1],vsplinetype[1],active);
+ real[][][] sz=bispline(fz,ipt,jpt,usplinetype[2],vsplinetype[2],active);
+
+ surface s=surface(sx.length);
+ s.index=new int[nu][nv];
+ int k=-1;
+ for(int i=0; i < nu; ++i) {
+ int[] indexi=s.index[i];
+ for(int j=0; j < nv; ++j)
+ indexi[j]=++k;
+ }
+
+ for(int k=0; k < sx.length; ++k) {
+ triple[][] Q=new triple[4][];
+ real[][] Px=sx[k];
+ real[][] Py=sy[k];
+ real[][] Pz=sz[k];
+ for(int i=0; i < 4 ; ++i) {
+ real[] Pxi=Px[i];
+ real[] Pyi=Py[i];
+ real[] Pzi=Pz[i];
+ Q[i]=new triple[] {(Pxi[0],Pyi[0],Pzi[0]),
+ (Pxi[1],Pyi[1],Pzi[1]),
+ (Pxi[2],Pyi[2],Pzi[2]),
+ (Pxi[3],Pyi[3],Pzi[3])};
+ }
+ s.s[k]=patch(Q);
+ }
+
+ if(usplinetype[0] == periodic && usplinetype[1] == periodic &&
+ usplinetype[1] == periodic) s.ucyclic(true);
+
+ if(vsplinetype[0] == periodic && vsplinetype[1] == periodic &&
+ vsplinetype[1] == periodic) s.vcyclic(true);
+
+ return s;
+}
+
// return the surface described by a parametric function f over box(a,b),
// interpolated with usplinetype and vsplinetype.
surface surface(triple f(pair z), pair a, pair b, int nu=nmesh, int nv=nu,
diff --git a/graphics/asymptote/base/palette.asy b/graphics/asymptote/base/palette.asy
index 2cb6e7c547..e77fa946e6 100644
--- a/graphics/asymptote/base/palette.asy
+++ b/graphics/asymptote/base/palette.asy
@@ -358,10 +358,10 @@ pen[] Wheel(int NColors=32766)
if(settings.gray) return Grayscale(NColors);
int nintervals=6;
+ if(NColors <= nintervals) NColors=nintervals+1;
int n=-quotient(NColors,-nintervals);
pen[] Palette;
- if(n == 0) return Palette;
Palette=new pen[n*nintervals];
real ninv=1.0/n;
@@ -386,10 +386,10 @@ pen[] Rainbow(int NColors=32766)
int offset=1;
int nintervals=5;
+ if(NColors <= nintervals) NColors=nintervals+1;
int n=-quotient(NColors-1,-nintervals);
pen[] Palette;
- if(n == 0) return Palette;
Palette=new pen[n*nintervals+offset];
real ninv=1.0/n;
@@ -418,12 +418,13 @@ private pen[] BWRainbow(int NColors, bool two)
if(two) nintervals += 6;
+ int Nintervals=nintervals*divisor;
+ if(NColors <= Nintervals) NColors=Nintervals+1;
int num=NColors-offset;
- int n=-quotient(num,-nintervals*divisor)*divisor;
+ int n=-quotient(num,-Nintervals)*divisor;
NColors=n*nintervals+offset;
pen[] Palette;
- if(n == 0) return Palette;
Palette=new pen[NColors];
real ninv=1.0/n;
diff --git a/graphics/asymptote/base/shaders/fragment.glsl b/graphics/asymptote/base/shaders/fragment.glsl
index cdcc0b49e8..057acbc744 100644
--- a/graphics/asymptote/base/shaders/fragment.glsl
+++ b/graphics/asymptote/base/shaders/fragment.glsl
@@ -135,29 +135,25 @@ void main()
Material m;
#ifdef TRANSPARENT
m=Materials[abs(materialIndex)-1];
- if(materialIndex >= 0) {
+ emissive=m.emissive;
+ if(materialIndex >= 0)
diffuse=m.diffuse;
- emissive=m.emissive;
- } else {
+ else {
diffuse=Color;
-#if Nlights > 0
- emissive=vec4(0.0);
-#else
- emissive=Color;
+#if Nlights == 0
+ emissive += Color;
#endif
}
#else
m=Materials[int(materialIndex)];
+ emissive=m.emissive;
#ifdef COLOR
diffuse=Color;
-#if Nlights > 0
- emissive=vec4(0.0);
-#else
- emissive=Color;
+#if Nlights == 0
+ emissive += Color;
#endif
#else
diffuse=m.diffuse;
- emissive=m.emissive;
#endif
#endif
diff --git a/graphics/asymptote/base/shaders/vertex.glsl b/graphics/asymptote/base/shaders/vertex.glsl
index 9a54d3211c..4b2a54b989 100644
--- a/graphics/asymptote/base/shaders/vertex.glsl
+++ b/graphics/asymptote/base/shaders/vertex.glsl
@@ -34,7 +34,7 @@ void main()
#ifndef ORTHOGRAPHIC
ViewPosition=(viewMat*v).xyz;
#endif
- Normal=normal*normMat;
+ Normal=normalize(normal*normMat);
#endif
#ifdef COLOR
diff --git a/graphics/asymptote/base/solids.asy b/graphics/asymptote/base/solids.asy
index 2f0a13f4b5..72110d6f7a 100644
--- a/graphics/asymptote/base/solids.asy
+++ b/graphics/asymptote/base/solids.asy
@@ -374,6 +374,7 @@ void draw(picture pic=currentpicture, revolution r, int m=0, int n=nslice,
// Return a right circular cylinder of height h in the direction of axis
// based on a circle centered at c with radius r.
+// Note: unitcylinder provides a smoother and more efficient representation.
revolution cylinder(triple c=O, real r, real h, triple axis=Z)
{
triple C=c+r*perp(axis);
@@ -392,7 +393,7 @@ revolution cone(triple c=O, real r, real h, triple axis=Z, int n=nslice)
// Return an approximate sphere of radius r centered at c obtained by rotating
// an (n+1)-point approximation to a half circle about the Z axis.
-// Note: unitsphere provides a smoother and more efficient surface.
+// Note: unitsphere provides a smoother and more efficient representation.
revolution sphere(triple c=O, real r, int n=nslice)
{
return revolution(c,Arc(c,r,180-sqrtEpsilon,0,sqrtEpsilon,0,Y,n),Z);
diff --git a/graphics/asymptote/base/three.asy b/graphics/asymptote/base/three.asy
index 2b32fd1612..75f758dd35 100644
--- a/graphics/asymptote/base/three.asy
+++ b/graphics/asymptote/base/three.asy
@@ -37,8 +37,7 @@ struct render
// General parameters:
real margin; // shrink amount for rendered openGL viewport, in bp.
- real tubegranularity; // granularity for rendering tubes
- bool labelfill; // fill subdivision cracks in unlighted labels
+ bool labelfill; // fill PRC subdivision cracks in unlighted labels
bool partnames; // assign part name indices to compound objects
bool defaultnames; // assign default names to unnamed objects
@@ -52,7 +51,6 @@ struct render
bool3 merge=defaultrender.merge,
int sphere=defaultrender.sphere,
real margin=defaultrender.margin,
- real tubegranularity=defaultrender.tubegranularity,
bool labelfill=defaultrender.labelfill,
bool partnames=defaultrender.partnames,
bool defaultnames=defaultrender.defaultnames)
@@ -64,7 +62,6 @@ struct render
this.merge=merge;
this.sphere=sphere;
this.margin=margin;
- this.tubegranularity=tubegranularity;
this.labelfill=labelfill;
this.partnames=partnames;
this.defaultnames=defaultnames;
@@ -80,7 +77,6 @@ defaultrender.closed=false;
defaultrender.tessellate=false;
defaultrender.merge=false;
defaultrender.margin=0.02;
-defaultrender.tubegranularity=0.001;
defaultrender.sphere=NURBSsphere;
defaultrender.labelfill=true;
defaultrender.partnames=false;
@@ -217,9 +213,10 @@ triple project(triple u, triple v)
triple perp(triple v)
{
triple u=cross(v,Y);
- if(abs(u) > sqrtEpsilon) return unit(u);
+ real norm=sqrtEpsilon*abs(v);
+ if(abs(u) > norm) return unit(u);
u=cross(v,Z);
- return (abs(u) > sqrtEpsilon) ? unit(u) : X;
+ return (abs(u) > norm) ? unit(u) : X;
}
// Return the transformation corresponding to moving the camera from the target
@@ -1140,6 +1137,18 @@ path3[] path3(explicit path[] g, triple plane(pair)=XYplane)
return sequence(new path3(int i) {return path3(g[i],plane);},g.length);
}
+path3 interp(path3 a, path3 b, real t)
+{
+ int n=size(a);
+ return path3(sequence(new triple(int i) {
+ return interp(precontrol(a,i),precontrol(b,i),t);},n),
+ sequence(new triple(int i) {return interp(point(a,i),point(b,i),t);},n),
+ sequence(new triple(int i) {return interp(postcontrol(a,i),
+ postcontrol(b,i),t);},n),
+ sequence(new bool(int i) {return straight(a,i) && straight(b,i);},n),
+ cyclic(a) && cyclic(b));
+}
+
path3 invert(path p, triple normal, triple point,
projection P=currentprojection)
{
@@ -2158,7 +2167,8 @@ pair max(frame f, projection P)
void draw(picture pic=currentpicture, Label L="", path3 g,
align align=NoAlign, material p=currentpen, margin3 margin=NoMargin3,
- light light=nolight, string name="", render render=defaultrender)
+ light light=nolight, string name="",
+ render render=defaultrender)
{
pen q=(pen) p;
pic.add(new void(frame f, transform3 t, picture pic, projection P) {
@@ -2188,80 +2198,62 @@ draw=new void(frame f, path3 g, material p=currentpen,
projection P=currentprojection) {
pen q=(pen) p;
if(is3D()) {
- p=material(p);
real width=linewidth(q);
void drawthick(path3 g) {
- if(settings.thick) {
- if(width > 0) {
- bool prc=prc();
- void cylinder(transform3) {};
- void sphere(transform3, bool half) {};
- void disk(transform3) {};
- void pipe(path3, path3);
- if(prc) {
- cylinder=new void(transform3 t) {drawPRCcylinder(f,t,p,light);};
- sphere=new void(transform3 t, bool half)
- {drawPRCsphere(f,t,half,p,light,render);};
- disk=new void(transform3 t) {draw(f,t*unitdisk,p,light,render);};
- pipe=new void(path3 center, path3 g)
- {drawPRCtube(f,center,g,p,light);};
+ if(settings.thick && width > 0) {
+ bool prc=prc();
+ bool webgl=settings.outformat == "html";
+ real linecap=linecap(q);
+ real r=0.5*width;
+ bool open=!cyclic(g);
+ int L=length(g);
+ triple g0=point(g,0);
+ triple gL=point(g,L);
+ if(open && L > 0) {
+ if(linecap == 2) {
+ g0 -= r*dir(g,0);
+ gL += r*dir(g,L);
+ g=g0..g..gL;
+ L += 2;
}
- real linecap=linecap(q);
- real r=0.5*width;
- bool open=!cyclic(g);
- int L=length(g);
- triple g0=point(g,0);
- triple gL=point(g,L);
- if(open && L > 0) {
- if(linecap == 2) {
- g0 -= r*dir(g,0);
- gL += r*dir(g,L);
- g=g0..g..gL;
- L += 2;
- }
- }
- tube T=tube(g,width,render,cylinder,sphere,pipe);
- path3 c=T.center;
- if(L >= 0) {
- if(open) {
- int Lc=length(c);
- triple c0=point(c,0);
- triple cL=point(c,Lc);
- triple dir0=dir(g,0);
- triple dirL=dir(g,L);
- triple dirc0=dir(c,0);
- triple dircL=dir(c,Lc);
- transform3 t0=shift(g0)*align(-dir0);
- transform3 tL=shift(gL)*align(dirL);
- transform3 tc0=shift(c0)*align(-dirc0);
- transform3 tcL=shift(cL)*align(dircL);
- if(linecap == 0 || linecap == 2) {
- transform3 scale2r=scale(r,r,1);
- T.s.append(t0*scale2r*unitdisk);
- disk(tc0*scale2r);
- if(L > 0) {
- T.s.append(tL*scale2r*unitdisk);
- disk(tcL*scale2r);
- }
- } else if(linecap == 1) {
- transform3 scale3r=scale3(r);
- T.s.append(t0*scale3r*
- (dir0 != O ? unithemisphere : unitsphere));
- sphere(tc0*scale3r,half=straight(c,0));
- if(L > 0) {
- T.s.append(tL*scale3r*
- (dirL != O ? unithemisphere : unitsphere));
- sphere(tcL*scale3r,half=straight(c,Lc-1));
- }
+ }
+ tube T=tube(g,width);
+ path3 c=T.center;
+ if(L >= 0) {
+ if(open) {
+ int Lc=length(c);
+ triple c0=point(c,0);
+ triple cL=point(c,Lc);
+ triple dir0=dir(g,0);
+ triple dirL=dir(g,L);
+ triple dirc0=dir(c,0);
+ triple dircL=dir(c,Lc);
+ transform3 t0=shift(g0)*align(-dir0);
+ transform3 tL=shift(gL)*align(dirL);
+ transform3 tc0=shift(c0)*align(-dirc0);
+ transform3 tcL=shift(cL)*align(dircL);
+ if(linecap == 0 || linecap == 2) {
+ transform3 scale2r=scale(r,r,1);
+ T.s.push(t0*scale2r*unitdisk);
+ if(L > 0) {
+ T.s.push(tL*scale2r*unitdisk);
}
+ } else if(linecap == 1) {
+ transform3 scale3r=scale3(r);
+ T.s.push(t0*scale3r*(straight(c,0) ?
+ unithemisphere : unitsphere));
+ if(L > 0)
+ T.s.push(tL*scale3r*(straight(c,Lc-1) ?
+ unithemisphere : unitsphere));
}
- if(opacity(q) == 1)
- _draw(f,c,q);
}
- for(patch s : T.s.s)
- draw3D(f,s,p,light,prc=false);
- } else _draw(f,g,q);
- } else _draw(f,g,q);
+// Draw central core for better small-scale rendering.
+ if((!prc || piecewisestraight(g)) && !webgl && opacity(q) == 1)
+ _draw(f,c,p,light);
+ }
+ for(surface s : T.s)
+ draw(f,s,p,light,render);
+ } else _draw(f,g,p,light);
}
bool group=q != nullpen && (name != "" || render.defaultnames);
if(group)
@@ -2701,7 +2693,7 @@ struct scene
if(pic.bounds3.exact && noAdjust)
this.P.bboxonly=false;
-
+
f=pic.fit3(t,pic.bounds3.exact ? pic2 : null,this.P);
if(!pic.bounds3.exact) {
diff --git a/graphics/asymptote/base/three_arrows.asy b/graphics/asymptote/base/three_arrows.asy
index 87fc39c52e..b0369e6bea 100644
--- a/graphics/asymptote/base/three_arrows.asy
+++ b/graphics/asymptote/base/three_arrows.asy
@@ -105,8 +105,11 @@ struct arrowhead3
s.append(shift(-n)*t*extrude(g,width*Z));
}
if(draw)
- for(path3 g : H)
- s.append(tube(g,width).s);
+ for(path3 g : H) {
+ tube T=tube(g,width);
+ for(surface S : T.s)
+ s.append(S);
+ }
return shift(v)*s;
}
@@ -150,27 +153,26 @@ DefaultHead3.head=new surface(path3 g, position position=EndPoint,
real remainL=size;
bool first=true;
for(int i=0; i < n; ++i) {
- render(subpath(s,i,i+1),new void(path3 q, real) {
- if(remainL > 0) {
- real l=arclength(q);
- real w=remainL*aspect;
- surface segment=scale(w,w,l)*unitcylinder;
- if(first) { // add base
- first=false;
- segment.append(scale(w,w,1)*unitdisk);
- }
- for(patch p : segment.s) {
- for(int i=0; i < 4; ++i) {
- for(int j=0; j < 4; ++j) {
- real k=1-p.P[i][j].z/remainL;
- p.P[i][j]=bend((k*p.P[i][j].x,k*p.P[i][j].y,p.P[i][j].z),q,l);
- }
- }
+ path3 q=subpath(s,i,i+1);
+ if(remainL > 0) {
+ real l=arclength(q);
+ real w=remainL*aspect;
+ surface segment=scale(w,w,l)*unitcylinder;
+ if(first) { // add base
+ first=false;
+ segment.append(scale(w,w,1)*unitdisk);
+ }
+ for(patch p : segment.s) {
+ for(int i=0; i < 4; ++i) {
+ for(int j=0; j < 4; ++j) {
+ real k=1-p.P[i][j].z/remainL;
+ p.P[i][j]=bend((k*p.P[i][j].x,k*p.P[i][j].y,p.P[i][j].z),q,l);
}
- head.append(segment);
- remainL -= l;
}
- });
+ }
+ head.append(segment);
+ remainL -= l;
+ }
}
}
return head;
diff --git a/graphics/asymptote/base/three_light.asy b/graphics/asymptote/base/three_light.asy
index 6e0bc366c7..25db6ad6d2 100644
--- a/graphics/asymptote/base/three_light.asy
+++ b/graphics/asymptote/base/three_light.asy
@@ -80,9 +80,9 @@ pen operator ecast(material m)
return m.p.length > 0 ? m.diffuse() : nullpen;
}
-material emissive(material m)
+material emissive(material m, bool colors=false)
{
- return material(black+opacity(m.opacity),m.diffuse(),black,m.opacity,1);
+ return material(black+opacity(m.opacity),colors ? m.emissive() : m.diffuse()+m.emissive(),black,m.opacity,1);
}
pen color(triple normal, material m, light light, transform3 T=light.T) {
diff --git a/graphics/asymptote/base/three_surface.asy b/graphics/asymptote/base/three_surface.asy
index 4ad31d6d2d..12ac464b24 100644
--- a/graphics/asymptote/base/three_surface.asy
+++ b/graphics/asymptote/base/three_surface.asy
@@ -71,10 +71,6 @@ struct patch {
triple BuP(int j, real u) {
return bezierP(P[0][j],P[1][j],P[2][j],P[3][j],u);
}
- triple BuPP(int j, real u) {
- return bezierPP(P[0][j],P[1][j],P[2][j],P[3][j],u);
- }
- triple BuPPP(int j) {return bezierPPP(P[0][j],P[1][j],P[2][j],P[3][j]);}
path3 uequals(real u) {
triple z0=Bu(0,u);
@@ -87,10 +83,6 @@ struct patch {
triple BvP(int i, real v) {
return bezierP(P[i][0],P[i][1],P[i][2],P[i][3],v);
}
- triple BvPP(int i, real v) {
- return bezierPP(P[i][0],P[i][1],P[i][2],P[i][3],v);
- }
- triple BvPPP(int i) {return bezierPPP(P[i][0],P[i][1],P[i][2],P[i][3]);}
path3 vequals(real v) {
triple z0=Bv(0,v);
@@ -103,29 +95,52 @@ struct patch {
return bezier(Bu(0,u),Bu(1,u),Bu(2,u),Bu(3,u),v);
}
- // compute normal vectors for degenerate cases
- private triple normal0(real u, real v, real epsilon) {
- triple n=0.5*(cross(bezier(BuPP(0,u),BuPP(1,u),BuPP(2,u),BuPP(3,u),v),
- bezier(BvP(0,v),BvP(1,v),BvP(2,v),BvP(3,v),u))+
- cross(bezier(BuP(0,u),BuP(1,u),BuP(2,u),BuP(3,u),v),
- bezier(BvPP(0,v),BvPP(1,v),BvPP(2,v),BvPP(3,v),u)));
- return abs(n) > epsilon ? n :
- 0.25*cross(bezier(BuPP(0,u),BuPP(1,u),BuPP(2,u),BuPP(3,u),v),
- bezier(BvPP(0,v),BvPP(1,v),BvPP(2,v),BvPP(3,v),u))+
- 1/6*(cross(bezier(BuP(0,u),BuP(1,u),BuP(2,u),BuP(3,u),v),
- bezier(BvPPP(0),BvPPP(1),BvPPP(2),BvPPP(3),u))+
- cross(bezier(BuPPP(0),BuPPP(1),BuPPP(2),BuPPP(3),v),
- bezier(BvP(0,v),BvP(1,v),BvP(2,v),BvP(3,v),u)))+
- 1/12*(cross(bezier(BuPPP(0),BuPPP(1),BuPPP(2),BuPPP(3),v),
- bezier(BvPP(0,v),BvPP(1,v),BvPP(2,v),BvPP(3,v),u))+
- cross(bezier(BuPP(0,u),BuPP(1,u),BuPP(2,u),BuPP(3,u),v),
- bezier(BvPPP(0),BvPPP(1),BvPPP(2),BvPPP(3),u)))+
- 1/36*cross(bezier(BuPPP(0),BuPPP(1),BuPPP(2),BuPPP(3),v),
- bezier(BvPPP(0),BvPPP(1),BvPPP(2),BvPPP(3),u));
- }
-
static real fuzz=1000*realEpsilon;
+ triple normal(triple left3, triple left2, triple left1, triple middle,
+ triple right1, triple right2, triple right3) {
+ real epsilon=fuzz*change2(P);
+
+ triple lp=3.0*(left1-middle);
+ triple rp=3.0*(right1-middle);
+
+ triple n=cross(rp,lp);
+ if(abs(n) > epsilon)
+ return n;
+
+ // Return one-half of the second derivative of the Bezier curve defined
+ // by a,b,c,d at 0.
+ triple bezierPP(triple a, triple b, triple c) {
+ return 3.0*(a+c-2.0*b);
+ }
+
+ triple lpp=bezierPP(middle,left1,left2);
+ triple rpp=bezierPP(middle,right1,right2);
+
+ n=cross(rpp,lp)+cross(rp,lpp);
+ if(abs(n) > epsilon)
+ return n;
+
+ // Return one-sixth of the third derivative of the Bezier curve defined
+ // by a,b,c,d at 0.
+ triple bezierPPP(triple a, triple b, triple c, triple d) {
+ return d-a+3.0*(b-c);
+ }
+
+ triple lppp=bezierPPP(middle,left1,left2,left3);
+ triple rppp=bezierPPP(middle,right1,right2,right3);
+
+ n=cross(rpp,lpp)+cross(rppp,lp)+cross(rp,lppp);
+ if(abs(n) > epsilon)
+ return n;
+
+ n=cross(rppp,lpp)+cross(rpp,lppp);
+ if(abs(n) > epsilon)
+ return n;
+
+ return cross(rppp,lppp);
+ }
+
triple partialu(real u, real v) {
return bezier(BuP(0,u),BuP(1,u),BuP(2,u),BuP(3,u),v);
}
@@ -134,34 +149,32 @@ struct patch {
return bezier(BvP(0,v),BvP(1,v),BvP(2,v),BvP(3,v),u);
}
- triple normal(real u, real v) {
- triple n=cross(partialu(u,v),partialv(u,v));
- real epsilon=fuzz*change2(P);
- return (abs(n) > epsilon) ? n : normal0(u,v,epsilon);
- }
-
triple normal00() {
- triple n=9*cross(P[1][0]-P[0][0],P[0][1]-P[0][0]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normal0(0,0,epsilon);
+ return normal(P[0][3],P[0][2],P[0][1],P[0][0],P[1][0],P[2][0],P[3][0]);
}
triple normal10() {
- triple n=9*cross(P[3][0]-P[2][0],P[3][1]-P[3][0]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normal0(1,0,epsilon);
+ return normal(P[0][0],P[1][0],P[2][0],P[3][0],P[3][1],P[3][2],P[3][3]);
}
triple normal11() {
- triple n=9*cross(P[3][3]-P[2][3],P[3][3]-P[3][2]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normal0(1,1,epsilon);
+ return normal(P[3][0],P[3][1],P[3][2],P[3][3],P[2][3],P[1][3],P[0][3]);
}
triple normal01() {
- triple n=9*cross(P[1][3]-P[0][3],P[0][3]-P[0][2]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normal0(0,1,epsilon);
+ return normal(P[3][3],P[2][3],P[1][3],P[0][3],P[0][2],P[0][1],P[0][0]);
+ }
+
+ triple normal(real u, real v) {
+ if(u == 0) {
+ if(v == 0) return normal00();
+ if(v == 1) return normal01();
+ }
+ if(u == 1) {
+ if(v == 0) return normal10();
+ if(v == 1) return normal11();
+ }
+ return cross(partialu(u,v),partialv(u,v));
}
triple pointtriangular(real u, real v) {
@@ -171,7 +184,7 @@ struct patch {
6*u*v*w*P[2][1]+v^2*(3*(w*P[2][2]+u*P[3][2])+v*P[3][3]);
}
- triple bu(real u, real v) {
+ triple partialutriangular(real u, real v) {
// Compute one-third of the directional derivative of a Bezier triangle
// in the u direction at (u,v).
real w=1-u-v;
@@ -179,21 +192,7 @@ struct patch {
2*v*(w-u)*P[2][1]-v^2*P[2][2]+u^2*P[3][0]+2*u*v*P[3][1]+v^2*P[3][2];
}
- triple buu(real u, real v) {
- // Compute one-sixth of the second directional derivative of a Bezier
- // triangle in the u direction at (u,v).
- real w=1-u-v;
- return w*P[0][0]+(u-2*w)*P[1][0]+v*P[1][1]+(w-2*u)*P[2][0]-2*v*P[2][1]+
- u*P[3][0]+v*P[3][1];
- }
-
- triple buuu() {
- // Compute one-sixth of the third directional derivative of a Bezier
- // triangle in the u direction at (u,v).
- return -P[0][0]+3*P[1][0]-3*P[2][0]+P[3][0];
- }
-
- triple bv(real u, real v) {
+ triple partialvtriangular(real u, real v) {
// Compute one-third of the directional derivative of a Bezier triangle
// in the v direction at (u,v).
real w=1-u-v;
@@ -202,54 +201,26 @@ struct patch {
v^2*P[3][3];
}
- triple bvv(real u, real v) {
- // Compute one-sixth of the second directional derivative of a Bezier
- // triangle in the v direction at (u,v).
- real w=1-u-v;
- return w*P[0][0]+u*P[1][0]+(v-2*w)*P[1][1]-2*u*P[2][1]+(w-2*v)*P[2][2]+
- u*P[3][2]+v*P[3][3];
- }
-
- triple bvvv() {
- // Compute one-sixth of the third directional derivative of a Bezier
- // triangle in the v direction at (u,v).
- return -P[0][0]+3*P[1][1]-3*P[2][2]+P[3][3];
- }
-
- // compute normal vectors for a degenerate Bezier triangle
- private triple normaltriangular0(real u, real v, real epsilon) {
- triple n=9*(cross(buu(u,v),bv(u,v))+
- cross(bu(u,v),bvv(u,v)));
- return abs(n) > epsilon ? n :
- 9*cross(buu(u,v),bvv(u,v))+
- 3*(cross(buuu(),bv(u,v))+cross(bu(u,v),bvvv())+
- cross(buuu(),bvv(u,v))+cross(buu(u,v),bvvv()))+
- cross(buuu(),bvvv());
- }
-
- // Compute the normal of a Bezier triangle at (u,v)
- triple normaltriangular(real u, real v) {
- triple n=9*cross(bu(u,v),bv(u,v));
- real epsilon=fuzz*change2(P);
- return (abs(n) > epsilon) ? n : normal0(u,v,epsilon);
- }
-
triple normal00triangular() {
- triple n=9*cross(P[1][0]-P[0][0],P[1][1]-P[0][0]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normaltriangular0(0,0,epsilon);
+ return normal(P[3][3],P[2][2],P[1][1],P[0][0],P[1][0],P[2][0],P[3][0]);
}
triple normal10triangular() {
- triple n=9*cross(P[3][0]-P[2][0],P[3][1]-P[2][0]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normaltriangular0(1,0,epsilon);
+ return normal(P[0][0],P[1][0],P[2][0],P[3][0],P[3][1],P[3][2],P[3][3]);
}
triple normal01triangular() {
- triple n=9*cross(P[3][2]-P[2][2],P[3][3]-P[2][2]);
- real epsilon=fuzz*change2(P);
- return abs(n) > epsilon ? n : normaltriangular0(0,1,epsilon);
+ return normal(P[3][0],P[3][1],P[3][2],P[3][3],P[2][2],P[1][1],P[0][0]);
+ }
+
+ // Compute the normal vector of a Bezier triangle at (u,v)
+ triple normaltriangular(real u, real v) {
+ if(u == 0) {
+ if(v == 0) return normal00triangular();
+ if(v == 1) return normal01triangular();
+ }
+ if(u == 1 && v == 0) return normal10triangular();
+ return cross(partialutriangular(u,v),partialvtriangular(u,v));
}
pen[] colors(material m, light light=currentlight) {
@@ -751,11 +722,18 @@ path[] regularize(path p, bool checkboundary=true)
return s;
}
+typedef void drawfcn(frame f, transform3 t=identity4, material[] m,
+ light light=currentlight, render render=defaultrender);
+
struct surface {
patch[] s;
int index[][];// Position of patch corresponding to major U,V parameter in s.
bool vcyclic;
+ transform3 T=identity4;
+ drawfcn draw;
+ bool PRCprimitive=true; // True unless no PRC primitive is available.
+
bool empty() {
return s.length == 0;
}
@@ -1052,6 +1030,9 @@ surface operator * (transform3 t, surface s)
S.s[i]=t*s.s[i];
S.index=copy(s.index);
S.vcyclic=(bool) s.vcyclic;
+ S.T=t*s.T;
+ S.draw=s.draw;
+ S.PRCprimitive=s.PRCprimitive;
return S;
}
@@ -1365,16 +1346,6 @@ triple point(patch s, real u, real v)
return s.point(u,v);
}
-real PRCshininess(real shininess)
-{
- // Empirical translation table from Phong-Blinn to PRC shininess model:
- static real[] x={0.015,0.025,0.05,0.07,0.1,0.14,0.23,0.5,0.65,0.75,0.85,
- 0.875,0.9,1};
- static real[] y={0.05,0.1,0.15,0.2,0.25,0.3,0.4,0.5,0.55,0.6,0.7,0.8,0.9,1};
- static realfunction s=fspline(x,y,monotonic);
- return s(shininess);
-}
-
struct interaction
{
int type;
@@ -1393,30 +1364,34 @@ interaction LabelInteraction()
return settings.autobillboard ? Billboard : Embedded;
}
-material material(material m, light light)
+material material(material m, light light, bool colors=false)
{
- return light.on() || invisible((pen) m) ? m : emissive(m);
+ return light.on() || invisible((pen) m) ? m : emissive(m,colors);
}
-void draw3D(frame f, int type=0, patch s, triple center=O, material m,
+void draw3D(frame f, patch s, triple center=O, material m,
light light=currentlight, interaction interaction=Embedded,
- bool prc=true)
+ bool primitive=false)
{
bool straight=s.straight && s.planar;
- bool prc=prc();
if(s.colors.length > 0) {
- if(prc && light.on())
+ if(prc() && light.on())
straight=false; // PRC vertex colors (for quads only) ignore lighting
- m=mean(s.colors);
+ m.diffuse(mean(s.colors));
}
- m=material(m,light);
+ m=material(m,light,s.colors.length > 0);
- real PRCshininess;
- if(prc) PRCshininess=PRCshininess(m.shininess);
-
(s.triangular ? drawbeziertriangle : draw)
(f,s.P,center,straight,m.p,m.opacity,m.shininess,
- m.metallic,m.fresnel0,PRCshininess,s.colors,interaction.type,prc);
+ m.metallic,m.fresnel0,s.colors,interaction.type,primitive);
+}
+
+void _draw(frame f, path3 g, triple center=O, material m,
+ light light=currentlight, interaction interaction=Embedded)
+{
+ if(!prc()) m=material(m,light);
+ _draw(f,g,center,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
+ interaction.type);
}
int computeNormals(triple[] v, int[][] vi, triple[] n, int[][] ni)
@@ -1448,11 +1423,7 @@ void draw(frame f, triple[] v, int[][] vi,
if(p.length > 0)
m=mean(p);
m=material(m,light);
- real PRCshininess;
- if(prc())
- PRCshininess=PRCshininess(m.shininess);
- draw(f,v,vi,n,ni,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
- PRCshininess,p,pi);
+ draw(f,v,vi,n,ni,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,p,pi);
}
// Draw triangles on a picture.
@@ -1520,40 +1491,9 @@ void draw(picture pic=currentpicture, triple[] v, int[][] vi,
pic.addPoint(v[viij]);
}
-void drawPRCsphere(frame f, transform3 t=identity4, bool half=false,
- material m, light light=currentlight,
- render render=defaultrender)
-{
- m=material(m,light);
- drawPRCsphere(f,t,half,m.p,m.opacity,PRCshininess(m.shininess),
- render.sphere);
-}
-
-void drawPRCcylinder(frame f, transform3 t=identity4, material m,
- light light=currentlight)
-{
- m=material(m,light);
- drawPRCcylinder(f,t,m.p,m.opacity,PRCshininess(m.shininess));
-}
-
-void drawPRCdisk(frame f, transform3 t=identity4, material m,
- light light=currentlight)
-{
- m=material(m,light);
- drawPRCdisk(f,t,m.p,m.opacity,PRCshininess(m.shininess));
-}
-
-void drawPRCtube(frame f, path3 center, path3 g, material m,
- light light=currentlight)
-{
- m=material(m,light);
- drawPRCtube(f,center,g,m.p,m.opacity,PRCshininess(m.shininess));
-}
-
void tensorshade(transform t=identity(), frame f, patch s,
material m, light light=currentlight, projection P)
{
-
pen[] p;
if(s.triangular) {
p=s.colorstriangular(m,light);
@@ -1574,53 +1514,62 @@ void draw(transform t=identity(), frame f, surface s, int nu=1, int nv=1,
{
bool is3D=is3D();
if(is3D) {
- bool group=name != "" || render.defaultnames;
- if(group)
- begingroup3(f,name == "" ? "surface" : name,render);
-
- // Sort patches by mean distance from camera
- triple camera=P.camera;
- if(P.infinity) {
- triple m=min(s);
- triple M=max(s);
- camera=P.target+camerafactor*(abs(M-m)+abs(m-P.target))*unit(P.vector());
- }
-
- real[][] depth=new real[s.s.length][];
- for(int i=0; i < depth.length; ++i)
- depth[i]=new real[] {dot(P.normal,camera-s.s[i].cornermean()),i};
+ bool prc=prc();
+ if(s.draw != null && (settings.outformat == "html" ||
+ (prc && s.PRCprimitive))) {
+ for(int k=0; k < s.s.length; ++k)
+ draw3D(f,s.s[k],surfacepen[k],light,primitive=true);
+ s.draw(f,s.T,surfacepen,light,render);
+ } else {
+ bool group=name != "" || render.defaultnames;
+ if(group)
+ begingroup3(f,name == "" ? "surface" : name,render);
+
+ // Sort patches by mean distance from camera
+ triple camera=P.camera;
+ if(P.infinity) {
+ triple m=min(s);
+ triple M=max(s);
+ camera=P.target+camerafactor*(abs(M-m)+abs(m-P.target))*
+ unit(P.vector());
+ }
- depth=sort(depth);
+ real[][] depth=new real[s.s.length][];
+ for(int i=0; i < depth.length; ++i)
+ depth[i]=new real[] {dot(P.normal,camera-s.s[i].cornermean()),i};
- for(int p=depth.length-1; p >= 0; --p) {
- real[] a=depth[p];
- int k=round(a[1]);
- draw3D(f,s.s[k],surfacepen[k],light);
- }
+ depth=sort(depth);
- if(group)
- endgroup3(f);
+ for(int p=depth.length-1; p >= 0; --p) {
+ real[] a=depth[p];
+ int k=round(a[1]);
+ draw3D(f,s.s[k],surfacepen[k],light);
+ }
- pen modifiers=thin()+squarecap;
- for(int p=depth.length-1; p >= 0; --p) {
- real[] a=depth[p];
- int k=round(a[1]);
- patch S=s.s[k];
- pen meshpen=meshpen[k];
- if(!invisible(meshpen) && !S.triangular) {
- if(group)
- begingroup3(f,meshname(name),render);
- meshpen=modifiers+meshpen;
- real step=nu == 0 ? 0 : 1/nu;
- for(int i=0; i <= nu; ++i)
- draw(f,S.uequals(i*step),meshpen,meshlight,partname(i,render),
- render);
- step=nv == 0 ? 0 : 1/nv;
- for(int j=0; j <= nv; ++j)
- draw(f,S.vequals(j*step),meshpen,meshlight,partname(j,render),
- render);
- if(group)
- endgroup3(f);
+ if(group)
+ endgroup3(f);
+
+ pen modifiers=thin()+squarecap;
+ for(int p=depth.length-1; p >= 0; --p) {
+ real[] a=depth[p];
+ int k=round(a[1]);
+ patch S=s.s[k];
+ pen meshpen=meshpen[k];
+ if(!invisible(meshpen) && !S.triangular) {
+ if(group)
+ begingroup3(f,meshname(name),render);
+ meshpen=modifiers+meshpen;
+ real step=nu == 0 ? 0 : 1/nu;
+ for(int i=0; i <= nu; ++i)
+ draw(f,S.uequals(i*step),meshpen,meshlight,partname(i,render),
+ render);
+ step=nv == 0 ? 0 : 1/nv;
+ for(int j=0; j <= nv; ++j)
+ draw(f,S.vequals(j*step),meshpen,meshlight,partname(j,render),
+ render);
+ if(group)
+ endgroup3(f);
+ }
}
}
}
@@ -1881,7 +1830,7 @@ void label(frame f, Label L, triple position, align align=NoAlign,
draw3D(f3,S,position,L.p,light,interaction);
// Fill subdivision cracks
if(prc && render.labelfill && opacity(L.p) == 1 && !lighton)
- _draw(f3,S.external(),position,L.p,interaction.type);
+ _draw(f3,S.external(),position,L.p,light,interaction);
}
endgroup3(f3);
if(L.defaulttransform3)
@@ -1901,7 +1850,7 @@ void label(frame f, Label L, triple position, align align=NoAlign,
draw3D(f,S,V,L.p,light,interaction);
// Fill subdivision cracks
if(prc && render.labelfill && opacity(L.p) == 1 && !lighton)
- _draw(f,S.external(),V,L.p,interaction.type);
+ _draw(f,S.external(),V,L.p,light,interaction);
}
endgroup3(f);
}
@@ -1970,7 +1919,7 @@ void label(picture pic=currentpicture, Label L, triple position,
draw3D(f3,S,v,L.p,light,interaction);
// Fill subdivision cracks
if(prc && render.labelfill && opacity(L.p) == 1 && !lighton)
- _draw(f3,S.external(),v,L.p,interaction.type);
+ _draw(f3,S.external(),v,L.p,light,interaction);
}
endgroup3(f3);
if(L.defaulttransform3)
@@ -1990,7 +1939,7 @@ void label(picture pic=currentpicture, Label L, triple position,
draw3D(f,S,V,L.p,light,interaction);
// Fill subdivision cracks
if(prc && render.labelfill && opacity(L.p) == 1 && !lighton)
- _draw(f,S.external(),V,L.p,interaction.type);
+ _draw(f,S.external(),V,L.p,light,interaction);
}
endgroup3(f);
}
@@ -2114,14 +2063,25 @@ surface surface(Label L, surface s, real uoffset, real voffset,
}
private real a=4/3*(sqrt(2)-1);
+private real f=0.5*sqrt(3)*a^2;
+
private transform3 t1=rotate(90,O,Z);
private transform3 t2=t1*t1;
private transform3 t3=t2*t1;
private transform3 i=xscale3(-1)*zscale3(-1);
-restricted patch octant1=patch(X{Y}..{-X}Y{Z}..{-Y}Z..Z{X}..{-Z}cycle,
+// Degenerate first octant
+restricted patch octant1x=patch(X{Y}..{-X}Y{Z}..{-Y}Z..Z{X}..{-Z}cycle,
new triple[] {(1,a,a),(a,1,a),(a^2,a,1),
- (a,a^2,1)});
+ (a,a^2,1)});
+private triple[][][] P=hsplit(octant1x.P,
+ intersect((1,0){N}..{W}(0,1),(0,0)--2*dir(60))[0]);
+// Nondegenerate first octant
+surface octant1=surface(patch(P[0]),
+ patch(P[1][0][0]..controls P[1][1][0] and P[1][2][0]..
+ P[1][3][0]..controls P[1][3][1] and P[1][3][2]..
+ P[1][3][3]..controls P[1][0][2] and P[1][0][1]..
+ cycle,(f,f,1)));
restricted surface unithemisphere=surface(octant1,t1*octant1,t2*octant1,
t3*octant1);
@@ -2129,33 +2089,41 @@ restricted surface unitsphere=surface(octant1,t1*octant1,t2*octant1,t3*octant1,
i*octant1,i*t1*octant1,i*t2*octant1,
i*t3*octant1);
-restricted patch unitfrustum(real t1, real t2)
+unitsphere.draw=
+ new void(frame f, transform3 t=identity4, material[] m,
+ light light=currentlight, render render=defaultrender)
+ {
+ material m=material(m[0],light);
+ drawSphere(f,t,half=false,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
+ render.sphere);
+ };
+
+unithemisphere.draw=
+ new void(frame f, transform3 t=identity4, material[] m,
+ light light=currentlight, render render=defaultrender)
+ {
+ material m=material(m[0],light);
+ drawSphere(f,t,half=true,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
+ render.sphere);
+ };
+
+restricted patch unitfrustum1(real ta, real tb)
{
- real s1=interp(t1,t2,1/3);
- real s2=interp(t1,t2,2/3);
- return patch(interp(Z,X,t2){Y}..{-X}interp(Z,Y,t2)--interp(Z,Y,t1){X}..{-Y}
- interp(Z,X,t1)--cycle,
+ real s1=interp(ta,tb,1/3);
+ real s2=interp(ta,tb,2/3);
+ return patch(interp(Z,X,tb){Y}..{-X}interp(Z,Y,tb)--interp(Z,Y,ta){X}..{-Y}
+ interp(Z,X,ta)--cycle,
new triple[] {(s2,s2*a,1-s2),(s2*a,s2,1-s2),(s1*a,s1,1-s1),
(s1,s1*a,1-s1)});
}
-// Return a unitcone constructed from n frusta (the final one being degenerate)
-surface unitcone(int n=6)
+restricted surface unitfrustum(real ta, real tb)
{
- surface unitcone;
- unitcone.s=new patch[4*n];
- real r=1/3;
- for(int i=0; i < n; ++i) {
- patch s=unitfrustum(i < n-1 ? r^(i+1) : 0,r^i);
- unitcone.s[i]=s;
- unitcone.s[n+i]=t1*s;
- unitcone.s[2n+i]=t2*s;
- unitcone.s[3n+i]=t3*s;
- }
- return unitcone;
+ patch p=unitfrustum1(ta,tb);
+ return surface(p,t1*p,t2*p,t3*p);
}
-restricted surface unitcone=unitcone();
+restricted surface unitcone=surface(unitfrustum(0,1));
restricted surface unitsolidcone=surface(patch(unitcircle3)...unitcone.s);
// Construct an approximate cone over an arbitrary base.
@@ -2166,6 +2134,18 @@ private patch unitcylinder1=patch(X{Y}..{-X}Y--Y+Z{X}..{-Y}X+Z--cycle);
restricted surface unitcylinder=surface(unitcylinder1,t1*unitcylinder1,
t2*unitcylinder1,t3*unitcylinder1);
+drawfcn unitcylinderDraw(bool core) {
+ return new void(frame f, transform3 t=identity4, material[] m,
+ light light=currentlight, render render=defaultrender)
+ {
+ material m=material(m[0],light);
+ drawCylinder(f,t,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
+ m.opacity == 1 ? core : false);
+ };
+}
+
+unitcylinder.draw=unitcylinderDraw(false);
+
private patch unitplane=patch(new triple[] {O,X,X+Y,Y});
restricted surface unitcube=surface(reverse(unitplane),
rotate(90,O,X)*unitplane,
@@ -2176,24 +2156,23 @@ restricted surface unitcube=surface(reverse(unitplane),
restricted surface unitplane=surface(unitplane);
restricted surface unitdisk=surface(unitcircle3);
+unitdisk.draw=
+ new void(frame f, transform3 t=identity4, material[] m,
+ light light=currentlight, render render=defaultrender)
+ {
+ material m=material(m[0],light);
+ drawDisk(f,t,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0);
+ };
+
void dot(frame f, triple v, material p=currentpen,
light light=nolight, string name="",
render render=defaultrender, projection P=currentprojection)
{
+ if(name == "" && render.defaultnames) name="dot";
pen q=(pen) p;
- if(is3D()) {
- bool group=name != "" || render.defaultnames;
- if(group)
- begingroup3(f,name == "" ? "dot" : name,render);
- real size=0.5*linewidth(dotsize(q)+q);
- transform3 T=shift(v)*scale3(size);
- for(patch s : unitsphere.s)
- draw3D(f,T*s,v,p,light,prc=false);
- if(prc())
- drawPRCsphere(f,T,p,light);
- if(group)
- endgroup3(f);
- } else dot(f,project(v,P.t),q);
+ real size=0.5*linewidth(dotsize(q)+q);
+ transform3 T=shift(v)*scale3(size);
+ draw(f,T*unitsphere,p,light,name,render,P);
}
void dot(frame f, triple[] v, material p=currentpen, light light=nolight,
@@ -2250,18 +2229,7 @@ void dot(picture pic=currentpicture, triple v, material p=currentpen,
real size=0.5*linewidth(dotsize(q)+q);
pic.add(new void(frame f, transform3 t, picture pic, projection P) {
triple V=t*v;
- if(is3D()) {
- bool group=name != "" || render.defaultnames;
- if(group)
- begingroup3(f,name == "" ? "dot" : name,render);
- transform3 T=shift(V)*scale3(size);
- for(patch s : unitsphere.s)
- draw3D(f,T*s,V,p,light,prc=false);
- if(prc())
- drawPRCsphere(f,T,p,light,render);
- if(group)
- endgroup3(f);
- }
+ dot(f,V,p,light,name,render,P);
if(pic != null)
dot(pic,project(V,P.t),q);
},true);
@@ -2450,11 +2418,8 @@ void draw(picture pic=currentpicture, triple[][] P, real[] uknot, real[] vknot,
if(group)
begingroup3(f,name == "" ? "surface" : name,render);
triple[][] P=t*P;
- real PRCshininess;
- if(prc())
- PRCshininess=PRCshininess(m.shininess);
- draw(f,P,uknot,vknot,weights,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
- PRCshininess,colors);
+ draw(f,P,uknot,vknot,weights,m.p,m.opacity,m.shininess,m.metallic,
+ m.fresnel0,colors);
if(group)
endgroup3(f);
if(pic != null)
diff --git a/graphics/asymptote/base/three_tube.asy b/graphics/asymptote/base/three_tube.asy
index 25bb9385ec..86451b97fa 100644
--- a/graphics/asymptote/base/three_tube.asy
+++ b/graphics/asymptote/base/three_tube.asy
@@ -1,41 +1,15 @@
-void render(path3 s, void f(path3, real), render render=defaultrender)
-{
- real granularity=render.tubegranularity;
- void Split(triple z0, triple c0, triple c1, triple z1, real t0=0, real t1=1,
- real depth=mantissaBits) {
- if(depth > 0) {
- real S=straightness(z0,c0,c1,z1);
- if(S > 0) {
- --depth;
- if(S > max(granularity*max(abs(z0),abs(c0),abs(c1),abs(z1)))) {
- triple m0=0.5*(z0+c0);
- triple m1=0.5*(c0+c1);
- triple m2=0.5*(c1+z1);
- triple m3=0.5*(m0+m1);
- triple m4=0.5*(m1+m2);
- triple m5=0.5*(m3+m4);
- real tm=0.5*(t0+t1);
- Split(z0,m0,m3,m5,t0,tm,depth);
- Split(m5,m4,m2,z1,tm,t1,depth);
- return;
- }
- }
- }
- f(z0..controls c0 and c1..z1,t0);
- }
- Split(point(s,0),postcontrol(s,0),precontrol(s,1),point(s,1));
-}
-
-struct rmf
-{
+struct rmf {
triple p,r,t,s;
- void operator init(triple p, triple r, triple t)
- {
+ void operator init(triple p, triple r, triple t) {
this.p=p;
this.r=r;
this.t=t;
s=cross(t,r);
}
+
+ transform3 transform() {
+ return transform3(r,s,t);
+ }
}
// Rotation minimizing frame
@@ -66,344 +40,172 @@ rmf[] rmf(path3 g, real[] t)
return R;
}
-private real[][][] bispline0(real[][] z, real[][] p, real[][] q, real[][] r,
- real[] x, real[] y, bool[][] cond={})
-{ // z[i][j] is the value at (x[i],y[j])
- // p and q are the first derivatives with respect to x and y, respectively
- // r is the second derivative ddu/dxdy
- int n=x.length-1;
- int m=y.length-1;
-
- bool all=cond.length == 0;
-
- int count;
- if(all)
- count=n*m;
- else {
- count=0;
- for(int i=0; i < n; ++i) {
- bool[] condi=cond[i];
- bool[] condp=cond[i+1];
- for(int j=0; j < m; ++j)
- if(all || (condi[j] && condi[j+1] && condp[j] && condp[j+1]))
- ++count;
- }
- }
-
- real[][][] s=new real[count][][];
- int k=0;
- for(int i=0; i < n; ++i) {
- int ip=i+1;
- real xi=x[i];
- real xp=x[ip];
- real hx=(xp-xi)/3;
- real[] zi=z[i];
- real[] zp=z[ip];
- real[] ri=r[i];
- real[] rp=r[ip];
- real[] pi=p[i];
- real[] pp=p[ip];
- real[] qi=q[i];
- real[] qp=q[ip];
- bool[] condi=all ? null : cond[i];
- bool[] condp=all ? null : cond[i+1];
- for(int j=0; j < m; ++j) {
- if(all || (condi[j] && condi[j+1] && condp[j] && condp[j+1])) {
- real yj=y[j];
- int jp=j+1;
- real yp=y[jp];
- real hy=(yp-yj)/3;
- real hxy=hx*hy;
- real zij=zi[j];
- real zip=zi[jp];
- real zpj=zp[j];
- real zpp=zp[jp];
- real pij=hx*pi[j];
- real ppj=hx*pp[j];
- real qip=hy*qi[jp];
- real qpp=hy*qp[jp];
- real zippip=zip+hx*pi[jp];
- real zppmppp=zpp-hx*pp[jp];
- real zijqij=zij+hy*qi[j];
- real zpjqpj=zpj+hy*qp[j];
-
- s[k]=new real[][] {{zij,zijqij,zip-qip,zip},
- {zij+pij,zijqij+pij+hxy*ri[j],
- zippip-qip-hxy*ri[jp],zippip},
- {zpj-ppj,zpjqpj-ppj-hxy*rp[j],
- zppmppp-qpp+hxy*rp[jp],zppmppp},
- {zpj,zpjqpj,zpp-qpp,zpp}};
- ++k;
- }
+rmf[] rmf(triple z0, triple c0, triple c1, triple z1, real[] t)
+{
+ real norm=sqrtEpsilon*max(abs(z0),abs(c0),abs(c1),abs(z1));
+
+// Special case of dir for t in (0,1].
+ triple dir(real t) {
+ if(t == 1) {
+ triple dir=z1-c1;
+ if(abs(dir) > norm) return unit(dir);
+ dir=2.0*c1-c0-z1;
+ if(abs(dir) > norm) return unit(dir);
+ return unit(z1-z0+3.0*(c0-c1));
}
+ triple a=z1-z0+3.0*(c0-c1);
+ triple b=2.0*(z0+c1)-4.0*c0;
+ triple c=c0-z0;
+ triple dir=a*t*t+b*t+c;
+ if(abs(dir) > norm) return unit(dir);
+ dir=2.0*a*t+b;
+ if(abs(dir) > norm) return unit(dir);
+ return unit(a);
}
-
- return s;
-}
-// return the surface values described by a real matrix f, interpolated with
-// xsplinetype and ysplinetype.
-real[][][] bispline(real[][] f, real[] x, real[] y,
- splinetype xsplinetype=null,
- splinetype ysplinetype=xsplinetype, bool[][] cond={})
-{
- real epsilon=sqrtEpsilon*norm(y);
- if(xsplinetype == null)
- xsplinetype=(abs(x[0]-x[x.length-1]) <= epsilon) ? periodic : notaknot;
- if(ysplinetype == null)
- ysplinetype=(abs(y[0]-y[y.length-1]) <= epsilon) ? periodic : notaknot;
- int n=x.length; int m=y.length;
- real[][] ft=transpose(f);
- real[][] tp=new real[m][];
- for(int j=0; j < m; ++j)
- tp[j]=xsplinetype(x,ft[j]);
- real[][] q=new real[n][];
- for(int i=0; i < n; ++i)
- q[i]=ysplinetype(y,f[i]);
- real[][] qt=transpose(q);
- real[] d1=xsplinetype(x,qt[0]);
- real[] d2=xsplinetype(x,qt[m-1]);
- real[][] r=new real[n][];
- real[][] p=transpose(tp);
- for(int i=0; i < n; ++i)
- r[i]=clamped(d1[i],d2[i])(y,p[i]);
- return bispline0(f,p,q,r,x,y,cond);
-}
-
-bool uperiodic(real[][] a) {
- int n=a.length;
- if(n == 0) return false;
- int m=a[0].length;
- real[] a0=a[0];
- real[] a1=a[n-1];
- for(int j=0; j < m; ++j) {
- real norm=0;
- for(int i=0; i < n; ++i)
- norm=max(norm,abs(a[i][j]));
- real epsilon=sqrtEpsilon*norm;
- if(abs(a0[j]-a1[j]) > epsilon) return false;
+ rmf[] R=new rmf[t.length];
+ triple T=c0-z0;
+ if(abs(T) < norm) {
+ T=z0-2*c0+c1;
+ if(abs(T) < norm)
+ T=z1-z0+3.0*(c0-c1);
}
- return true;
-}
-bool vperiodic(real[][] a) {
- int n=a.length;
- if(n == 0) return false;
- int m=a[0].length-1;
- for(int i=0; i < n; ++i) {
- real[] ai=a[i];
- real epsilon=sqrtEpsilon*norm(ai);
- if(abs(ai[0]-ai[m]) > epsilon) return false;
+ T=unit(T);
+ triple Tp=perp(T);
+ R[0]=rmf(z0,Tp,T);
+ for(int i=1; i < t.length; ++i) {
+ rmf Ri=R[i-1];
+ real t=t[i];
+ triple p=bezier(z0,c0,c1,z1,t);
+ triple v1=p-Ri.p;
+ if(v1 != O) {
+ triple r=Ri.r;
+ triple u1=unit(v1);
+ triple ti=Ri.t;
+ triple tp=ti-2*dot(u1,ti)*u1;
+ ti=dir(t);
+ triple rp=r-2*dot(u1,r)*u1;
+ triple u2=unit(ti-tp);
+ rp=rp-2*dot(u2,rp)*u2;
+ R[i]=rmf(p,unit(rp),unit(ti));
+ } else
+ R[i]=R[i-1];
}
- return true;
+ return R;
}
-// return the surface described by a parametric function f evaluated at u and v
-// and interpolated with usplinetype and vsplinetype.
-surface surface(triple f(pair z), real[] u, real[] v,
- splinetype[] usplinetype, splinetype[] vsplinetype=Spline,
- bool cond(pair z)=null)
+surface tube(triple z0, triple c0, triple c1, triple z1, real w)
{
- int nu=u.length-1;
- int nv=v.length-1;
- real[] ipt=sequence(u.length);
- real[] jpt=sequence(v.length);
- real[][] fx=new real[u.length][v.length];
- real[][] fy=new real[u.length][v.length];
- real[][] fz=new real[u.length][v.length];
-
- bool[][] active;
- bool all=cond == null;
- if(!all) active=new bool[u.length][v.length];
-
- for(int i=0; i <= nu; ++i) {
- real ui=u[i];
- real[] fxi=fx[i];
- real[] fyi=fy[i];
- real[] fzi=fz[i];
- bool[] activei=all ? null : active[i];
- for(int j=0; j <= nv; ++j) {
- pair z=(ui,v[j]);
- if(!all) activei[j]=cond(z);
- triple f=f(z);
- fxi[j]=f.x;
- fyi[j]=f.y;
- fzi[j]=f.z;
- }
- }
-
- if(usplinetype.length == 0) {
- usplinetype=new splinetype[] {uperiodic(fx) ? periodic : notaknot,
- uperiodic(fy) ? periodic : notaknot,
- uperiodic(fz) ? periodic : notaknot};
- } else if(usplinetype.length != 3) abort("usplinetype must have length 3");
-
- if(vsplinetype.length == 0) {
- vsplinetype=new splinetype[] {vperiodic(fx) ? periodic : notaknot,
- vperiodic(fy) ? periodic : notaknot,
- vperiodic(fz) ? periodic : notaknot};
- } else if(vsplinetype.length != 3) abort("vsplinetype must have length 3");
-
- real[][][] sx=bispline(fx,ipt,jpt,usplinetype[0],vsplinetype[0],active);
- real[][][] sy=bispline(fy,ipt,jpt,usplinetype[1],vsplinetype[1],active);
- real[][][] sz=bispline(fz,ipt,jpt,usplinetype[2],vsplinetype[2],active);
-
- surface s=surface(sx.length);
- s.index=new int[nu][nv];
- int k=-1;
- for(int i=0; i < nu; ++i) {
- int[] indexi=s.index[i];
- for(int j=0; j < nv; ++j)
- indexi[j]=++k;
- }
-
- for(int k=0; k < sx.length; ++k) {
- triple[][] Q=new triple[4][];
- real[][] Px=sx[k];
- real[][] Py=sy[k];
- real[][] Pz=sz[k];
- for(int i=0; i < 4 ; ++i) {
- real[] Pxi=Px[i];
- real[] Pyi=Py[i];
- real[] Pzi=Pz[i];
- Q[i]=new triple[] {(Pxi[0],Pyi[0],Pzi[0]),
- (Pxi[1],Pyi[1],Pzi[1]),
- (Pxi[2],Pyi[2],Pzi[2]),
- (Pxi[3],Pyi[3],Pzi[3])};
+ surface s;
+ static real[] T={0,1/3,2/3,1};
+ rmf[] rmf=rmf(z0,c0,c1,z1,T);
+
+ real aw=a*w;
+ triple[] arc={(w,0,0),(w,aw,0),(aw,w,0),(0,w,0)};
+ triple[] g={z0,c0,c1,z1};
+
+ void f(transform3 R) {
+ triple[][] P=new triple[4][];
+ for(int i=0; i < 4; ++i) {
+ transform3 T=shift(g[i])*rmf[i].transform()*R;
+ P[i]=new triple[] {T*arc[0],T*arc[1],T*arc[2],T*arc[3]};
}
- s.s[k]=patch(Q);
+ s.push(patch(P,copy=false));
}
- if(usplinetype[0] == periodic && usplinetype[1] == periodic &&
- usplinetype[1] == periodic) s.ucyclic(true);
-
- if(vsplinetype[0] == periodic && vsplinetype[1] == periodic &&
- vsplinetype[1] == periodic) s.vcyclic(true);
-
+ f(identity4);
+ f(t1);
+ f(t2);
+ f(t3);
+
+ s.PRCprimitive=false;
+ s.draw=new void(frame f, transform3 t=identity4, material[] m,
+ light light=currentlight, render render=defaultrender)
+ {
+ material m=material(m[0],light);
+ drawTube(f,t*g,w,m.p,m.opacity,m.shininess,m.metallic,m.fresnel0,
+ t*min(s),t*max(s),m.opacity == 1);
+ };
return s;
}
-path3 interp(path3 a, path3 b, real t)
-{
- int n=size(a);
- return path3(sequence(new triple(int i) {
- return interp(precontrol(a,i),precontrol(b,i),t);},n),
- sequence(new triple(int i) {return interp(point(a,i),point(b,i),t);},n),
- sequence(new triple(int i) {return interp(postcontrol(a,i),
- postcontrol(b,i),t);},n),
- sequence(new bool(int i) {return straight(a,i) && straight(b,i);},n),
- cyclic(a) && cyclic(b));
+real tubethreshold=20;
+
+// Note: casting an array of surfaces to a single surface will disable
+// primitive compression.
+surface operator cast(surface[] s) {
+ surface S;
+ for(surface p : s)
+ S.append(p);
+ return S;
}
struct tube
{
- surface s;
+ surface[] s;
path3 center; // tube axis
void Null(transform3) {}
void Null(transform3, bool) {}
- void operator init(path3 p, real width, render render=defaultrender,
- void cylinder(transform3)=Null,
- void sphere(transform3, bool half)=Null,
- void pipe(path3, path3)=null) {
+ surface[] render(path3 g, real r) {
+ triple z0=point(g,0);
+ triple c0=postcontrol(g,0);
+ triple c1=precontrol(g,1);
+ triple z1=point(g,1);
+ real norm=sqrtEpsilon*max(abs(z0),abs(c0),abs(c1),abs(z1));
+ surface[] s;
+ void Split(triple z0, triple c0, triple c1, triple z1,
+ real depth=mantissaBits) {
+ if(depth > 0) {
+ pair threshold(triple z0, triple c0, triple c1) {
+ triple u=c1-z0;
+ triple v=c0-z0;
+ real x=abs(v);
+ return (x,abs(u*x^2-dot(u,v)*v));
+ }
+
+ pair a0=threshold(z0,c0,c1);
+ pair a1=threshold(z1,c1,c0);
+ real rL=r*arclength(z0,c0,c1,z1)*tubethreshold;
+ if((a0.x >= norm && rL*a0.y^2 > a0.x^8) ||
+ (a1.x >= norm && rL*a1.y^2 > a1.x^8)) {
+ triple m0=0.5*(z0+c0);
+ triple m1=0.5*(c0+c1);
+ triple m2=0.5*(c1+z1);
+ triple m3=0.5*(m0+m1);
+ triple m4=0.5*(m1+m2);
+ triple m5=0.5*(m3+m4);
+ --depth;
+ Split(z0,m0,m3,m5,depth);
+ Split(m5,m4,m2,z1,depth);
+ return;
+ }
+ }
+
+ s.push(tube(z0,c0,c1,z1,r));
+ }
+ Split(z0,c0,c1,z1);
+ return s;
+ }
+
+ void operator init(path3 p, real width) {
+ center=p;
real r=0.5*width;
void generate(path3 p) {
int n=length(p);
- if(piecewisestraight(p)) {
- for(int i=0; i < n; ++i) {
+ for(int i=0; i < n; ++i) {
+ if(straight(p,i)) {
triple v=point(p,i);
triple u=point(p,i+1)-v;
transform3 t=shift(v)*align(unit(u))*scale(r,r,abs(u));
- s.append(t*unitcylinder);
- cylinder(t);
- }
- center=center&p;
- } else {
- real[] T;
- path3 G;
- for(int i=0; i < n; ++i)
- render(subpath(p,i,i+1),
- new void(path3 g, real s) {
- G=G&g;
- T.push(i+s);
- },render);
- T.push(n);
- T.cyclic=cyclic(p);
- rmf[] rmf=rmf(p,T);
- triple f(pair t) {
- rmf R=rmf[round(t.x)];
- int n=round(t.y);
- static real[] x={1,0,-1,0};
- static real[] y={0,1,0,-1};
- return point(G,t.x)+r*(R.r*x[n]-R.s*y[n]);
- }
-
- static real[] v={0,1,2,3,0};
- static real[] circular(real[] x, real[] y) {
- static real a=8/3*(sqrt(2)-1);
- return a*periodic(x,y);
- }
-
- static splinetype[] Monotonic={monotonic,monotonic,monotonic};
- static splinetype[] Circular={circular,circular,circular};
- if(T.length > 0) {
- surface S=surface(f,sequence(T.length),v,Monotonic,Circular);
- s.append(S);
-
- // Compute center of tube:
- int n=S.index.length;
- if(T.cyclic) --n;
- triple[] pre=new triple[n+1];
- triple[] point=new triple[n+1];
- triple[] post=new triple[n+1];
-
- int[] index=S.index[0];
- triple Point;
- for(int m=0; m < 4; ++m)
- Point += S.s[index[m]].P[0][0];
- pre[0]=point[0]=0.25*Point;
-
- for(int i=0; i < n; ++i) {
- index=S.index[i];
- triple Pre,Point,Post;
- for(int m=0; m < 4; ++m) {
- triple [][] P=S.s[index[m]].P;
- Post += P[1][0];
- Pre += P[2][0];
- Point += P[3][0];
- }
- post[i]=0.25*Post;
- pre[i+1]=0.25*Pre;
- point[i+1]=0.25*Point;
-
- }
-
- index=S.index[n-1];
- triple Post;
- for(int m=0; m < 4; ++m)
- Post += S.s[index[m]].P[3][0];
- post[n]=0.25*Post;
-
- bool[] b=array(n+1,false);
- path3 Center=path3(pre,point,post,b,T.cyclic);
- center=center&Center;
-
- if(pipe != null) { // Compute path along tube
- triple[] pre=new triple[n+1];
- triple[] point=new triple[n+1];
- triple[] post=new triple[n+1];
- pre[0]=point[0]=S.s[S.index[0][0]].P[0][0];
- for(int i=0; i < n; ++i) {
- triple [][] P=S.s[S.index[i][0]].P;
- post[i]=P[1][0];
- pre[i+1]=P[2][0];
- point[i+1]=P[3][0];
- }
- post[n]=S.s[S.index[n-1][0]].P[3][0];
- pipe(Center,path3(pre,point,post,b,T.cyclic));
- }
- }
+ // Draw opaque surfaces with core for better small-scale rendering.
+ surface unittube=t*unitcylinder;
+ unittube.draw=unitcylinderDraw(core=true);
+ s.push(unittube);
+ } else
+ s.append(render(subpath(p,i,i+1),r));
}
}
@@ -416,11 +218,11 @@ struct tube
generate(subpath(p,begin,i));
triple dir=dir(p,i,-1);
transform3 T=t*align(dir);
- s.append(shift(point(p,i))*T*(dir != O ? unithemisphere : unitsphere));
- sphere(shift(point(center,length(center)))*T,
- half=straight(p,i-1) && straight(p,i));
+ s.push(shift(point(p,i))*T*(straight(p,i-1) && straight(p,i) ?
+ unithemisphere : unitsphere));
begin=i;
}
- generate(subpath(p,begin,n));
+ path3 g=subpath(p,begin,n);
+ generate(g);
}
}
diff --git a/graphics/asymptote/base/tube.asy b/graphics/asymptote/base/tube.asy
index 33e2ea3c86..cfaf458db4 100644
--- a/graphics/asymptote/base/tube.asy
+++ b/graphics/asymptote/base/tube.asy
@@ -9,6 +9,33 @@
import three;
+real tubegranularity=1e-7;
+
+void render(path3 s, real r, void f(path3, real))
+{
+ void Split(triple z0, triple c0, triple c1, triple z1, real t0=0, real t1=1,
+ real depth=mantissaBits) {
+ if(depth > 0) {
+ real S=straightness(z0,c0,c1,z1);
+ if(S > max(tubegranularity*max(abs(z0),abs(c0),abs(c1),abs(z1)))) {
+ --depth;
+ triple m0=0.5*(z0+c0);
+ triple m1=0.5*(c0+c1);
+ triple m2=0.5*(c1+z1);
+ triple m3=0.5*(m0+m1);
+ triple m4=0.5*(m1+m2);
+ triple m5=0.5*(m3+m4);
+ real tm=0.5*(t0+t1);
+ Split(z0,m0,m3,m5,t0,tm,depth);
+ Split(m5,m4,m2,z1,tm,t1,depth);
+ return;
+ }
+ }
+ f(z0..controls c0 and c1..z1,t0);
+ }
+ Split(point(s,0),postcontrol(s,0),precontrol(s,1),point(s,1));
+}
+
// A 3D version of roundedpath(path, real).
path3 roundedpath(path3 A, real r)
{
@@ -42,13 +69,8 @@ real[] sample(path3 g, real r, real relstep=0)
real[] t;
int n=length(g);
if(relstep <= 0) {
- for(int i=0; i < n; ++i) {
- real S=straightness(g,i);
- if(S < sqrtEpsilon*r)
- t.push(i);
- else
- render(subpath(g,i,i+1),new void(path3, real s) {t.push(i+s);});
- }
+ for(int i=0; i < n; ++i)
+ render(subpath(g,i,i+1),r,new void(path3, real s) {t.push(i+s);});
t.push(n);
} else {
int nb=ceil(1/relstep);
diff --git a/graphics/asymptote/base/webgl/asygl.js b/graphics/asymptote/base/webgl/asygl.js
index 46aea1a377..7c0e9edba4 100644
--- a/graphics/asymptote/base/webgl/asygl.js
+++ b/graphics/asymptote/base/webgl/asygl.js
@@ -36,4 +36,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
-let vertex="\nattribute vec3 position;\n#ifdef WIDTH\nattribute float width;\n#endif\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\nattribute float materialIndex;\n#ifdef COLOR\nattribute vec4 color;\n#endif\n\nuniform mat3 normMat;\nuniform mat4 viewMat;\nuniform mat4 projViewMat;\n\n#ifdef NORMAL\n#ifndef ORTHOGRAPHIC\nvarying vec3 ViewPosition;\n#endif\nvarying vec3 Normal;\n#endif\nvarying vec4 diffuse;\nvarying vec3 specular;\nvarying float roughness,metallic,fresnel0;\nvarying vec4 emissive;\n\nstruct Material {\n vec4 diffuse,emissive,specular;\n vec4 parameters;\n};\n\nuniform Material Materials[Nmaterials];\n\nvoid main(void)\n{\n vec4 v=vec4(position,1.0);\n gl_Position=projViewMat*v;\n#ifdef NORMAL\n#ifndef ORTHOGRAPHIC\n ViewPosition=(viewMat*v).xyz;\n#endif \n Normal=normal*normMat;\n \n Material m;\n#ifdef TRANSPARENT\n m=Materials[int(abs(materialIndex))-1];\n if(materialIndex >= 0.0) {\n diffuse=m.diffuse;\n emissive=m.emissive;\n } else {\n diffuse=color;\n#if nlights > 0\n emissive=vec4(0.0);\n#else\n emissive=color;\n#endif\n }\n#else\n m=Materials[int(materialIndex)];\n#ifdef COLOR\n diffuse=color;\n#if nlights > 0\n emissive=vec4(0.0);\n#else\n emissive=color;\n#endif\n#else\n diffuse=m.diffuse;\n emissive=m.emissive;\n#endif\n#endif\n specular=m.specular.rgb;\n vec4 parameters=m.parameters;\n roughness=1.0-parameters[0];\n metallic=parameters[1];\n fresnel0=parameters[2];\n#else\n emissive=Materials[int(materialIndex)].emissive;\n#endif\n#ifdef WIDTH\n gl_PointSize=width;\n#endif\n}\n",fragment="\n#ifdef NORMAL\n#ifndef ORTHOGRAPHIC\nvarying vec3 ViewPosition;\n#endif\nvarying vec3 Normal;\nvarying vec4 diffuse;\nvarying vec3 specular;\nvarying float roughness,metallic,fresnel0;\n\nfloat Roughness2;\nvec3 normal;\n\nstruct Light {\n vec3 direction;\n vec3 color;\n};\n\nuniform Light Lights[Nlights];\n\nfloat NDF_TRG(vec3 h)\n{\n float ndoth=max(dot(normal,h),0.0);\n float alpha2=Roughness2*Roughness2;\n float denom=ndoth*ndoth*(alpha2-1.0)+1.0;\n return denom != 0.0 ? alpha2/(denom*denom) : 0.0;\n}\n \nfloat GGX_Geom(vec3 v)\n{\n float ndotv=max(dot(v,normal),0.0);\n float ap=1.0+Roughness2;\n float k=0.125*ap*ap;\n return ndotv/((ndotv*(1.0-k))+k);\n}\n \nfloat Geom(vec3 v, vec3 l)\n{\n return GGX_Geom(v)*GGX_Geom(l);\n}\n \nfloat Fresnel(vec3 h, vec3 v, float fresnel0)\n{\n float a=1.0-max(dot(h,v),0.0);\n float b=a*a;\n return fresnel0+(1.0-fresnel0)*b*b*a;\n}\n \n// physical based shading using UE4 model.\nvec3 BRDF(vec3 viewDirection, vec3 lightDirection)\n{\n vec3 lambertian=diffuse.rgb;\n vec3 h=normalize(lightDirection+viewDirection);\n \n float omegain=max(dot(viewDirection,normal),0.0);\n float omegali=max(dot(lightDirection,normal),0.0);\n \n float D=NDF_TRG(h);\n float G=Geom(viewDirection,lightDirection);\n float F=Fresnel(h,viewDirection,fresnel0);\n \n float denom=4.0*omegain*omegali;\n float rawReflectance=denom > 0.0 ? (D*G)/denom : 0.0;\n \n vec3 dielectric=mix(lambertian,rawReflectance*specular,F);\n vec3 metal=rawReflectance*diffuse.rgb;\n \n return mix(dielectric,metal,metallic);\n}\n#endif\nvarying vec4 emissive;\n \nvoid main(void)\n{\n#if defined(NORMAL) && nlights > 0\n normal=normalize(Normal);\n normal=gl_FrontFacing ? normal : -normal;\n#ifdef ORTHOGRAPHIC\n vec3 viewDir=vec3(0.0,0.0,1.0);\n#else\n vec3 viewDir=-normalize(ViewPosition);\n#endif\n Roughness2=roughness*roughness;\n vec3 color=emissive.rgb;\n for(int i=0; i < nlights; ++i) {\n Light Li=Lights[i];\n vec3 L=Li.direction;\n float cosTheta=max(dot(normal,L),0.0);\n vec3 radiance=cosTheta*Li.color;\n color += BRDF(viewDir,L)*radiance;\n }\n gl_FragColor=vec4(color,diffuse.a);\n#else\n gl_FragColor=emissive;\n#endif\n}\n";!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var i=t();for(var a in i)("object"==typeof exports?exports:e)[a]=i[a]}}("undefined"!=typeof self?self:this,function(){return function(e){var t={};function i(a){if(t[a])return t[a].exports;var r=t[a]={i:a,l:!1,exports:{}};return e[a].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=e,i.c=t,i.d=function(e,t,a){i.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:a})},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.s=1)}([function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.setMatrixArrayType=function(e){t.ARRAY_TYPE=e},t.toRadian=function(e){return e*r},t.equals=function(e,t){return Math.abs(e-t)<=a*Math.max(1,Math.abs(e),Math.abs(t))};var a=t.EPSILON=1e-6;t.ARRAY_TYPE="undefined"!=typeof Float32Array?Float32Array:Array,t.RANDOM=Math.random;var r=Math.PI/180},function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.mat4=t.mat3=void 0;var a=n(i(2)),r=n(i(3));function n(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t.default=e,t}t.mat3=a,t.mat4=r},function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.create=function(){var e=new a.ARRAY_TYPE(9);return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=1,e[5]=0,e[6]=0,e[7]=0,e[8]=1,e},t.fromMat4=function(e,t){return e[0]=t[0],e[1]=t[1],e[2]=t[2],e[3]=t[4],e[4]=t[5],e[5]=t[6],e[6]=t[8],e[7]=t[9],e[8]=t[10],e},t.invert=function(e,t){var i=t[0],a=t[1],r=t[2],n=t[3],s=t[4],o=t[5],l=t[6],h=t[7],c=t[8],d=c*s-o*h,m=-c*n+o*l,f=h*n-s*l,u=i*d+a*m+r*f;if(!u)return null;return u=1/u,e[0]=d*u,e[1]=(-c*a+r*h)*u,e[2]=(o*a-r*s)*u,e[3]=m*u,e[4]=(c*i-r*l)*u,e[5]=(-o*i+r*n)*u,e[6]=f*u,e[7]=(-h*i+a*l)*u,e[8]=(s*i-a*n)*u,e};var a=function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t.default=e,t}(i(0))},function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.create=function(){var e=new a.ARRAY_TYPE(16);return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,e},t.identity=function(e){return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,e},t.invert=function(e,t){var i=t[0],a=t[1],r=t[2],n=t[3],s=t[4],o=t[5],l=t[6],h=t[7],c=t[8],d=t[9],m=t[10],f=t[11],u=t[12],v=t[13],p=t[14],g=t[15],x=i*o-a*s,M=i*l-r*s,w=i*h-n*s,b=a*l-r*o,A=a*h-n*o,S=r*h-n*l,R=c*v-d*u,D=c*p-m*u,P=c*g-f*u,T=d*p-m*v,y=d*g-f*v,z=m*g-f*p,I=x*z-M*y+w*T+b*P-A*D+S*R;if(!I)return null;return I=1/I,e[0]=(o*z-l*y+h*T)*I,e[1]=(r*y-a*z-n*T)*I,e[2]=(v*S-p*A+g*b)*I,e[3]=(m*A-d*S-f*b)*I,e[4]=(l*P-s*z-h*D)*I,e[5]=(i*z-r*P+n*D)*I,e[6]=(p*w-u*S-g*M)*I,e[7]=(c*S-m*w+f*M)*I,e[8]=(s*y-o*P+h*R)*I,e[9]=(a*P-i*y-n*R)*I,e[10]=(u*A-v*w+g*x)*I,e[11]=(d*w-c*A-f*x)*I,e[12]=(o*D-s*T-l*R)*I,e[13]=(i*T-a*D+r*R)*I,e[14]=(v*M-u*b-p*x)*I,e[15]=(c*b-d*M+m*x)*I,e},t.multiply=r,t.translate=function(e,t,i){var a=i[0],r=i[1],n=i[2],s=void 0,o=void 0,l=void 0,h=void 0,c=void 0,d=void 0,m=void 0,f=void 0,u=void 0,v=void 0,p=void 0,g=void 0;t===e?(e[12]=t[0]*a+t[4]*r+t[8]*n+t[12],e[13]=t[1]*a+t[5]*r+t[9]*n+t[13],e[14]=t[2]*a+t[6]*r+t[10]*n+t[14],e[15]=t[3]*a+t[7]*r+t[11]*n+t[15]):(s=t[0],o=t[1],l=t[2],h=t[3],c=t[4],d=t[5],m=t[6],f=t[7],u=t[8],v=t[9],p=t[10],g=t[11],e[0]=s,e[1]=o,e[2]=l,e[3]=h,e[4]=c,e[5]=d,e[6]=m,e[7]=f,e[8]=u,e[9]=v,e[10]=p,e[11]=g,e[12]=s*a+c*r+u*n+t[12],e[13]=o*a+d*r+v*n+t[13],e[14]=l*a+m*r+p*n+t[14],e[15]=h*a+f*r+g*n+t[15]);return e},t.rotate=function(e,t,i,r){var n=r[0],s=r[1],o=r[2],l=Math.sqrt(n*n+s*s+o*o),h=void 0,c=void 0,d=void 0,m=void 0,f=void 0,u=void 0,v=void 0,p=void 0,g=void 0,x=void 0,M=void 0,w=void 0,b=void 0,A=void 0,S=void 0,R=void 0,D=void 0,P=void 0,T=void 0,y=void 0,z=void 0,I=void 0,E=void 0,O=void 0;if(Math.abs(l)<a.EPSILON)return null;n*=l=1/l,s*=l,o*=l,h=Math.sin(i),c=Math.cos(i),d=1-c,m=t[0],f=t[1],u=t[2],v=t[3],p=t[4],g=t[5],x=t[6],M=t[7],w=t[8],b=t[9],A=t[10],S=t[11],R=n*n*d+c,D=s*n*d+o*h,P=o*n*d-s*h,T=n*s*d-o*h,y=s*s*d+c,z=o*s*d+n*h,I=n*o*d+s*h,E=s*o*d-n*h,O=o*o*d+c,e[0]=m*R+p*D+w*P,e[1]=f*R+g*D+b*P,e[2]=u*R+x*D+A*P,e[3]=v*R+M*D+S*P,e[4]=m*T+p*y+w*z,e[5]=f*T+g*y+b*z,e[6]=u*T+x*y+A*z,e[7]=v*T+M*y+S*z,e[8]=m*I+p*E+w*O,e[9]=f*I+g*E+b*O,e[10]=u*I+x*E+A*O,e[11]=v*I+M*E+S*O,t!==e&&(e[12]=t[12],e[13]=t[13],e[14]=t[14],e[15]=t[15]);return e},t.fromTranslation=function(e,t){return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=t[0],e[13]=t[1],e[14]=t[2],e[15]=1,e},t.fromRotation=function(e,t,i){var r=i[0],n=i[1],s=i[2],o=Math.sqrt(r*r+n*n+s*s),l=void 0,h=void 0,c=void 0;if(Math.abs(o)<a.EPSILON)return null;return r*=o=1/o,n*=o,s*=o,l=Math.sin(t),h=Math.cos(t),c=1-h,e[0]=r*r*c+h,e[1]=n*r*c+s*l,e[2]=s*r*c-n*l,e[3]=0,e[4]=r*n*c-s*l,e[5]=n*n*c+h,e[6]=s*n*c+r*l,e[7]=0,e[8]=r*s*c+n*l,e[9]=n*s*c-r*l,e[10]=s*s*c+h,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,e},t.frustum=function(e,t,i,a,r,n,s){var o=1/(i-t),l=1/(r-a),h=1/(n-s);return e[0]=2*n*o,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=2*n*l,e[6]=0,e[7]=0,e[8]=(i+t)*o,e[9]=(r+a)*l,e[10]=(s+n)*h,e[11]=-1,e[12]=0,e[13]=0,e[14]=s*n*2*h,e[15]=0,e},t.ortho=function(e,t,i,a,r,n,s){var o=1/(t-i),l=1/(a-r),h=1/(n-s);return e[0]=-2*o,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=-2*l,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=2*h,e[11]=0,e[12]=(t+i)*o,e[13]=(r+a)*l,e[14]=(s+n)*h,e[15]=1,e};var a=function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t.default=e,t}(i(0));function r(e,t,i){var a=t[0],r=t[1],n=t[2],s=t[3],o=t[4],l=t[5],h=t[6],c=t[7],d=t[8],m=t[9],f=t[10],u=t[11],v=t[12],p=t[13],g=t[14],x=t[15],M=i[0],w=i[1],b=i[2],A=i[3];return e[0]=M*a+w*o+b*d+A*v,e[1]=M*r+w*l+b*m+A*p,e[2]=M*n+w*h+b*f+A*g,e[3]=M*s+w*c+b*u+A*x,M=i[4],w=i[5],b=i[6],A=i[7],e[4]=M*a+w*o+b*d+A*v,e[5]=M*r+w*l+b*m+A*p,e[6]=M*n+w*h+b*f+A*g,e[7]=M*s+w*c+b*u+A*x,M=i[8],w=i[9],b=i[10],A=i[11],e[8]=M*a+w*o+b*d+A*v,e[9]=M*r+w*l+b*m+A*p,e[10]=M*n+w*h+b*f+A*g,e[11]=M*s+w*c+b*u+A*x,M=i[12],w=i[13],b=i[14],A=i[15],e[12]=M*a+w*o+b*d+A*v,e[13]=M*r+w*l+b*m+A*p,e[14]=M*n+w*h+b*f+A*g,e[15]=M*s+w*c+b*u+A*x,e}}])});let canvasWidth,canvasHeight,b,B,angle,Zoom0,viewportmargin,zoomFactor,zoomPinchFactor,zoomPinchCap,zoomStep,shiftHoldDistance,shiftWaitTime,vibrateTime,embedded,canvas,gl,alpha,offscreen,context,maxMaterials,halfCanvasWidth,halfCanvasHeight,Zoom,P=[],Materials=[],Lights=[],Centers=[],Background=[1,1,1,1],absolute=!1,viewportshift=[0,0],nlights=0,Nmaterials=2,materials=[],pixel=.75,BezierFactor=.4,FillFactor=.1,maxViewportWidth=window.innerWidth,maxViewportHeight=window.innerHeight;const windowTrim=10;let lastzoom,H,zmin,zmax,size2,ArcballFactor,positionBuffer,materialBuffer,colorBuffer,indexBuffer,resizeStep=1.2,Fuzz2=1e3*Number.EPSILON,Fuzz4=Fuzz2*Fuzz2,third=1/3,rotMat=mat4.create(),projMat=mat4.create(),viewMat=mat4.create(),projViewMat=mat4.create(),normMat=mat3.create(),viewMat3=mat3.create(),rotMats=mat4.create(),cjMatInv=mat4.create(),translMat=mat4.create(),center={x:0,y:0,z:0},shift={x:0,y:0},viewParam={xmin:0,xmax:0,ymin:0,ymax:0,zmin:0,zmax:0},remesh=!0,mouseDownOrTouchActive=!1,lastMouseX=null,lastMouseY=null,touchID=null,Positions=[],Normals=[],Colors=[],Indices=[];class Material{constructor(e,t,i,a,r,n){this.diffuse=e,this.emissive=t,this.specular=i,this.shininess=a,this.metallic=r,this.fresnel0=n}setUniform(e,t){let i=i=>gl.getUniformLocation(e,"Materials["+t+"]."+i);gl.uniform4fv(i("diffuse"),new Float32Array(this.diffuse)),gl.uniform4fv(i("emissive"),new Float32Array(this.emissive)),gl.uniform4fv(i("specular"),new Float32Array(this.specular)),gl.uniform4f(i("parameters"),this.shininess,this.metallic,this.fresnel0,0)}}let indexExt,enumPointLight=1,enumDirectionalLight=2;class Light{constructor(e,t){this.direction=e,this.color=t}setUniform(e,t){let i=i=>gl.getUniformLocation(e,"Lights["+t+"]."+i);gl.uniform3fv(i("direction"),new Float32Array(this.direction)),gl.uniform3fv(i("color"),new Float32Array(this.color))}}function initShaders(){let e=gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS);maxMaterials=Math.floor((e-14)/4),Nmaterials=Math.min(Math.max(Nmaterials,Materials.length),maxMaterials),noNormalShader=initShader(),pixelShader=initShader(["WIDTH"]),materialShader=initShader(["NORMAL"]),colorShader=initShader(["NORMAL","COLOR"]),transparentShader=initShader(["NORMAL","COLOR","TRANSPARENT"])}function setBuffers(){positionBuffer=gl.createBuffer(),materialBuffer=gl.createBuffer(),colorBuffer=gl.createBuffer(),indexBuffer=gl.createBuffer()}function noGL(){gl||alert("Could not initialize WebGL")}function saveAttributes(){let e=window.top.document.asygl[alpha];e.gl=gl,e.nlights=Lights.length,e.Nmaterials=Nmaterials,e.maxMaterials=maxMaterials,e.noNormalShader=noNormalShader,e.pixelShader=pixelShader,e.materialShader=materialShader,e.colorShader=colorShader,e.transparentShader=transparentShader}function restoreAttributes(){let e=window.top.document.asygl[alpha];gl=e.gl,nlights=e.nlights,Nmaterials=e.Nmaterials,maxMaterials=e.maxMaterials,noNormalShader=e.noNormalShader,pixelShader=e.pixelShader,materialShader=e.materialShader,colorShader=e.colorShader,transparentShader=e.transparentShader}function initGL(){if(alpha=Background[3]<1,embedded){let e=window.top.document;null==e.asygl&&(e.asygl=Array(2)),context=canvas.getContext("2d"),(offscreen=e.offscreen)||(offscreen=e.createElement("canvas"),e.offscreen=offscreen),e.asygl[alpha]&&e.asygl[alpha].gl?(restoreAttributes(),(Lights.length!=nlights||Math.min(Materials.length,maxMaterials)>Nmaterials)&&(initShaders(),saveAttributes())):((gl=offscreen.getContext("webgl",{alpha:alpha}))||noGL(),initShaders(),e.asygl[alpha]={},saveAttributes())}else(gl=canvas.getContext("webgl",{alpha:alpha}))||noGL(),initShaders();setBuffers(),indexExt=gl.getExtension("OES_element_index_uint")}function getShader(e,t,i,a=[]){let r=`#version 100\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n #define nlights ${Lights.length}\n\n const int Nlights=${Math.max(Lights.length,1)};\n\n #define Nmaterials ${Nmaterials}\n`;orthographic&&(r+="#define ORTHOGRAPHIC\n"),a.forEach(e=>r+="#define "+e+"\n");let n=e.createShader(i);return e.shaderSource(n,r+t),e.compileShader(n),e.getShaderParameter(n,e.COMPILE_STATUS)?n:(alert(e.getShaderInfoLog(n)),null)}function drawBuffer(e,t,i=e.indices){if(0==e.indices.length)return;let a=t==pixelShader,r=t!=noNormalShader&&!a;setUniforms(e,t),gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer),gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(e.vertices),gl.STATIC_DRAW),gl.vertexAttribPointer(positionAttribute,3,gl.FLOAT,!1,r?24:a?16:12,0),r&&Lights.length>0?gl.vertexAttribPointer(normalAttribute,3,gl.FLOAT,!1,24,12):a&&gl.vertexAttribPointer(widthAttribute,1,gl.FLOAT,!1,16,12),gl.bindBuffer(gl.ARRAY_BUFFER,materialBuffer),gl.bufferData(gl.ARRAY_BUFFER,new Int16Array(e.materialIndices),gl.STATIC_DRAW),gl.vertexAttribPointer(materialAttribute,1,gl.SHORT,!1,2,0),t!=colorShader&&t!=transparentShader||(gl.bindBuffer(gl.ARRAY_BUFFER,colorBuffer),gl.bufferData(gl.ARRAY_BUFFER,new Uint8Array(e.colors),gl.STATIC_DRAW),gl.vertexAttribPointer(colorAttribute,4,gl.UNSIGNED_BYTE,!0,0,0)),gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer),gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,indexExt?new Uint32Array(i):new Uint16Array(i),gl.STATIC_DRAW),gl.drawElements(r?gl.TRIANGLES:a?gl.POINTS:gl.LINES,i.length,indexExt?gl.UNSIGNED_INT:gl.UNSIGNED_SHORT,0)}class vertexBuffer{constructor(){this.clear()}clear(){this.vertices=[],this.materialIndices=[],this.colors=[],this.indices=[],this.nvertices=0,this.materials=[],this.materialTable=[]}vertex(e,t){return this.vertices.push(e[0]),this.vertices.push(e[1]),this.vertices.push(e[2]),this.vertices.push(t[0]),this.vertices.push(t[1]),this.vertices.push(t[2]),this.materialIndices.push(materialIndex),this.nvertices++}Vertex(e,t,i=[0,0,0,0]){return this.vertices.push(e[0]),this.vertices.push(e[1]),this.vertices.push(e[2]),this.vertices.push(t[0]),this.vertices.push(t[1]),this.vertices.push(t[2]),this.materialIndices.push(materialIndex),this.colors.push(i[0]),this.colors.push(i[1]),this.colors.push(i[2]),this.colors.push(i[3]),this.nvertices++}vertex1(e){return this.vertices.push(e[0]),this.vertices.push(e[1]),this.vertices.push(e[2]),this.materialIndices.push(materialIndex),this.nvertices++}vertex0(e,t){return this.vertices.push(e[0]),this.vertices.push(e[1]),this.vertices.push(e[2]),this.vertices.push(t),this.materialIndices.push(materialIndex),this.nvertices++}iVertex(e,t,i,a=[0,0,0,0]){let r=6*e;this.vertices[r]=t[0],this.vertices[r+1]=t[1],this.vertices[r+2]=t[2],this.vertices[r+3]=i[0],this.vertices[r+4]=i[1],this.vertices[r+5]=i[2],this.materialIndices[e]=materialIndex;let n=4*e;this.colors[n]=a[0],this.colors[n+1]=a[1],this.colors[n+2]=a[2],this.colors[n+3]=a[3],this.indices.push(e)}append(e){append(this.vertices,e.vertices),append(this.materialIndices,e.materialIndices),append(this.colors,e.colors),appendOffset(this.indices,e.indices,this.nvertices),this.nvertices+=e.nvertices}}let materialIndex,material0Data=new vertexBuffer,material1Data=new vertexBuffer,materialData=new vertexBuffer,colorData=new vertexBuffer,transparentData=new vertexBuffer,triangleData=new vertexBuffer;function append(e,t){let i=e.length,a=t.length;e.length+=a;for(let r=0;r<a;++r)e[i+r]=t[r]}function appendOffset(e,t,i){let a=e.length,r=t.length;e.length+=t.length;for(let n=0;n<r;++n)e[a+n]=t[n]+i}class Geometry{constructor(){this.data=new vertexBuffer,this.Onscreen=!1,this.m=[]}offscreen(e){let t=projViewMat,i=e[0],a=i[0],r=i[1],n=i[2],s=1/(t[3]*a+t[7]*r+t[11]*n+t[15]);this.x=this.X=(t[0]*a+t[4]*r+t[8]*n+t[12])*s,this.y=this.Y=(t[1]*a+t[5]*r+t[9]*n+t[13])*s;for(let i=1,a=e.length;i<a;++i){let a=e[i],r=a[0],n=a[1],s=a[2],o=1/(t[3]*r+t[7]*n+t[11]*s+t[15]),l=(t[0]*r+t[4]*n+t[8]*s+t[12])*o,h=(t[1]*r+t[5]*n+t[9]*s+t[13])*o;l<this.x?this.x=l:l>this.X&&(this.X=l),h<this.y?this.y=h:h>this.Y&&(this.Y=h)}return(this.X<-1.01||this.x>1.01||this.Y<-1.01||this.y>1.01)&&(this.Onscreen=!1,!0)}T(e){let t=this.c[0],i=this.c[1],a=this.c[2],r=e[0]-t,n=e[1]-i,s=e[2]-a;return[r*normMat[0]+n*normMat[3]+s*normMat[6]+t,r*normMat[1]+n*normMat[4]+s*normMat[7]+i,r*normMat[2]+n*normMat[5]+s*normMat[8]+a]}Tcorners(e,t){return[this.T(e),this.T([e[0],e[1],t[2]]),this.T([e[0],t[1],e[2]]),this.T([e[0],t[1],t[2]]),this.T([t[0],e[1],e[2]]),this.T([t[0],e[1],t[2]]),this.T([t[0],t[1],e[2]]),this.T(t)]}setMaterial(e,t){null==e.materialTable[this.MaterialIndex]&&(e.materials.length>=Nmaterials&&t(),e.materialTable[this.MaterialIndex]=e.materials.length,e.materials.push(Materials[this.MaterialIndex])),materialIndex=e.materialTable[this.MaterialIndex]}render(){let e;if(this.setMaterialIndex(),0==this.CenterIndex?e=corners(this.Min,this.Max):(this.c=Centers[this.CenterIndex-1],e=this.Tcorners(this.Min,this.Max)),this.offscreen(e))return void this.data.clear();let t,i=this.controlpoints;if(0==this.CenterIndex){if(!remesh&&this.Onscreen)return void this.append();t=i}else{let e=i.length;t=Array(e);for(let a=0;a<e;++a)t[a]=this.T(i[a])}let a=orthographic?1:this.Min[2]/B[2],r=pixel*Math.hypot(a*(viewParam.xmax-viewParam.xmin),a*(viewParam.ymax-viewParam.ymin))/size2;this.res2=r*r,this.Epsilon=FillFactor*r,this.data.clear(),this.Onscreen=!0,this.process(t)}}class BezierPatch extends Geometry{constructor(e,t,i,a,r,n){super(),this.controlpoints=e,this.Min=a,this.Max=r,this.color=n,this.CenterIndex=t;let s=e.length;if(n){let e=n[0][3]+n[1][3]+n[2][3];this.transparent=16==s||4==s?e+n[3][3]<1020:e<765}else this.transparent=Materials[i].diffuse[3]<1;this.MaterialIndex=i,this.vertex=this.transparent?this.data.Vertex.bind(this.data):this.data.vertex.bind(this.data),this.L2norm(this.controlpoints)}setMaterialIndex(){this.transparent?this.setMaterial(transparentData,drawTransparent):this.color?this.setMaterial(colorData,drawColor):this.setMaterial(materialData,drawMaterial)}L2norm(e){let t=e[0];this.epsilon=0;let i=e.length;for(let a=1;a<i;++a)this.epsilon=Math.max(this.epsilon,abs2([e[a][0]-t[0],e[a][1]-t[1],e[a][2]-t[2]]));this.epsilon*=Fuzz4}processTriangle(e){let t=e[0],i=e[1],a=e[2],r=unit(cross([i[0]-t[0],i[1]-t[1],i[2]-t[2]],[a[0]-t[0],a[1]-t[1],a[2]-t[2]]));this.offscreen([t,i,a])||(this.color?(this.data.indices.push(this.data.Vertex(t,r,this.color[0])),this.data.indices.push(this.data.Vertex(i,r,this.color[1])),this.data.indices.push(this.data.Vertex(a,r,this.color[2]))):(this.data.indices.push(this.vertex(t,r)),this.data.indices.push(this.vertex(i,r)),this.data.indices.push(this.vertex(a,r))),this.append())}processQuad(e){let t=e[0],i=e[1],a=e[2],r=e[3],n=cross([i[0]-t[0],i[1]-t[1],i[2]-t[2]],[a[0]-i[0],a[1]-i[1],a[2]-i[2]]),s=cross([a[0]-r[0],a[1]-r[1],a[2]-r[2]],[r[0]-t[0],r[1]-t[1],r[2]-t[2]]),o=unit([n[0]+s[0],n[1]+s[1],n[2]+s[2]]);if(!this.offscreen([t,i,a,r])){let e,n,s,l;this.color?(e=this.data.Vertex(t,o,this.color[0]),n=this.data.Vertex(i,o,this.color[1]),s=this.data.Vertex(a,o,this.color[2]),l=this.data.Vertex(r,o,this.color[3])):(e=this.vertex(t,o),n=this.vertex(i,o),s=this.vertex(a,o),l=this.vertex(r,o)),this.data.indices.push(e),this.data.indices.push(n),this.data.indices.push(s),this.data.indices.push(e),this.data.indices.push(s),this.data.indices.push(l),this.append()}}process(e){if(this.transparent&&(materialIndex=this.color?-1-materialIndex:1+materialIndex),10==e.length)return this.process3(e);if(3==e.length)return this.processTriangle(e);if(4==e.length)return this.processQuad(e);let t=e[0],i=e[3],a=e[12],r=e[15],n=this.normal(i,e[2],e[1],t,e[4],e[8],a);iszero(n)&&iszero(n=this.normal(i,e[2],e[1],t,e[13],e[14],r))&&(n=this.normal(r,e[11],e[7],i,e[4],e[8],a));let s=this.normal(t,e[4],e[8],a,e[13],e[14],r);iszero(s)&&iszero(s=this.normal(t,e[4],e[8],a,e[11],e[7],i))&&(s=this.normal(i,e[2],e[1],t,e[13],e[14],r));let o=this.normal(a,e[13],e[14],r,e[11],e[7],i);iszero(o)&&iszero(o=this.normal(a,e[13],e[14],r,e[2],e[1],t))&&(o=this.normal(t,e[4],e[8],a,e[11],e[7],i));let l=this.normal(r,e[11],e[7],i,e[2],e[1],t);if(iszero(l)&&iszero(l=this.normal(r,e[11],e[7],i,e[4],e[8],a))&&(l=this.normal(a,e[13],e[14],r,e[2],e[1],t)),this.color){let h=this.color[0],c=this.color[1],d=this.color[2],m=this.color[3],f=this.data.Vertex(t,n,h),u=this.data.Vertex(a,s,c),v=this.data.Vertex(r,o,d),p=this.data.Vertex(i,l,m);this.Render(e,f,u,v,p,t,a,r,i,!1,!1,!1,!1,h,c,d,m)}else{let h=this.vertex(t,n),c=this.vertex(a,s),d=this.vertex(r,o),m=this.vertex(i,l);this.Render(e,h,c,d,m,t,a,r,i,!1,!1,!1,!1)}this.data.indices.length>0&&this.append()}append(){this.transparent?transparentData.append(this.data):this.color?colorData.append(this.data):materialData.append(this.data)}Render(e,t,i,a,r,n,s,o,l,h,c,d,m,f,u,v,p){if(this.Distance(e)<this.res2)this.offscreen([n,s,o])||(this.data.indices.push(t),this.data.indices.push(i),this.data.indices.push(a)),this.offscreen([n,o,l])||(this.data.indices.push(t),this.data.indices.push(a),this.data.indices.push(r));else{if(this.offscreen(e))return;let g=e[0],x=e[3],M=e[12],w=e[15],b=new Split3(g,e[1],e[2],x),A=new Split3(e[4],e[5],e[6],e[7]),S=new Split3(e[8],e[9],e[10],e[11]),R=new Split3(M,e[13],e[14],w),D=new Split3(g,e[4],e[8],M),P=new Split3(b.m0,A.m0,S.m0,R.m0),T=new Split3(b.m3,A.m3,S.m3,R.m3),y=new Split3(b.m5,A.m5,S.m5,R.m5),z=new Split3(b.m4,A.m4,S.m4,R.m4),I=new Split3(b.m2,A.m2,S.m2,R.m2),E=new Split3(x,e[7],e[11],w),O=[g,b.m0,b.m3,b.m5,D.m0,P.m0,T.m0,y.m0,D.m3,P.m3,T.m3,y.m3,D.m5,P.m5,T.m5,y.m5],_=[D.m5,P.m5,T.m5,y.m5,D.m4,P.m4,T.m4,y.m4,D.m2,P.m2,T.m2,y.m2,M,R.m0,R.m3,R.m5],L=[y.m5,z.m5,I.m5,E.m5,y.m4,z.m4,I.m4,E.m4,y.m2,z.m2,I.m2,E.m2,R.m5,R.m4,R.m2,w],N=[b.m5,b.m4,b.m2,x,y.m0,z.m0,I.m0,E.m0,y.m3,z.m3,I.m3,E.m3,y.m5,z.m5,I.m5,E.m5],B=O[15],C=this.normal(O[0],O[4],O[8],O[12],O[13],O[14],O[15]);iszero(C)&&iszero(C=this.normal(O[0],O[4],O[8],O[12],O[11],O[7],O[3]))&&(C=this.normal(O[3],O[2],O[1],O[0],O[13],O[14],O[15]));let F=this.normal(_[12],_[13],_[14],_[15],_[11],_[7],_[3]);iszero(F)&&iszero(F=this.normal(_[12],_[13],_[14],_[15],_[2],_[1],_[0]))&&(F=this.normal(_[0],_[4],_[8],_[12],_[11],_[7],_[3]));let V=this.normal(L[15],L[11],L[7],L[3],L[2],L[1],L[0]);iszero(V)&&iszero(V=this.normal(L[15],L[11],L[7],L[3],L[4],L[8],L[12]))&&(V=this.normal(L[12],L[13],L[14],L[15],L[2],L[1],L[0]));let H=this.normal(N[3],N[2],N[1],N[0],N[4],N[8],N[12]);iszero(H)&&iszero(H=this.normal(N[3],N[2],N[1],N[0],N[13],N[14],N[15]))&&(H=this.normal(N[15],N[11],N[7],N[3],N[4],N[8],N[12]));let G=this.normal(L[3],L[2],L[1],B,L[4],L[8],L[12]),U=this.Epsilon,W=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])];if(!h)if(h=Straightness(g,e[4],e[8],M)<this.res2){let e=unit(this.derivative(_[0],_[1],_[2],_[3]));W=[W[0]-U*e[0],W[1]-U*e[1],W[2]-U*e[2]]}else W=O[12];let Y=[.5*(s[0]+o[0]),.5*(s[1]+o[1]),.5*(s[2]+o[2])];if(!c)if(c=Straightness(M,e[13],e[14],w)<this.res2){let e=unit(this.derivative(L[12],L[8],L[4],L[0]));Y=[Y[0]-U*e[0],Y[1]-U*e[1],Y[2]-U*e[2]]}else Y=_[15];let j=[.5*(o[0]+l[0]),.5*(o[1]+l[1]),.5*(o[2]+l[2])];if(!d)if(d=Straightness(w,e[11],e[7],x)<this.res2){let e=unit(this.derivative(N[15],L[14],L[13],_[12]));j=[j[0]-U*e[0],j[1]-U*e[1],j[2]-U*e[2]]}else j=L[3];let k=[.5*(l[0]+n[0]),.5*(l[1]+n[1]),.5*(l[2]+n[2])];if(!m)if(m=Straightness(g,e[1],e[2],x)<this.res2){let e=unit(this.derivative(O[3],O[7],O[11],O[15]));k=[k[0]-U*e[0],k[1]-U*e[1],k[2]-U*e[2]]}else k=N[0];if(f){let e=Array(4),g=Array(4),x=Array(4),M=Array(4),w=Array(4);for(let t=0;t<4;++t)e[t]=.5*(f[t]+u[t]),g[t]=.5*(u[t]+v[t]),x[t]=.5*(v[t]+p[t]),M[t]=.5*(p[t]+f[t]),w[t]=.5*(e[t]+x[t]);let b=this.data.Vertex(W,C,e),A=this.data.Vertex(Y,F,g),S=this.data.Vertex(j,V,x),R=this.data.Vertex(k,H,M),D=this.data.Vertex(B,G,w);this.Render(O,t,b,D,R,n,W,B,k,h,!1,!1,m,f,e,w,M),this.Render(_,b,i,A,D,W,s,Y,B,h,c,!1,!1,e,u,g,w),this.Render(L,D,A,a,S,B,Y,o,j,!1,c,d,!1,w,g,v,x),this.Render(N,R,D,S,r,k,B,j,l,!1,!1,d,m,M,w,x,p)}else{let e=this.vertex(W,C),f=this.vertex(Y,F),u=this.vertex(j,V),v=this.vertex(k,H),p=this.vertex(B,G);this.Render(O,t,e,p,v,n,W,B,k,h,!1,!1,m),this.Render(_,e,i,f,p,W,s,Y,B,h,c,!1,!1),this.Render(L,p,f,a,u,B,Y,o,j,!1,c,d,!1),this.Render(N,v,p,u,r,k,B,j,l,!1,!1,d,m)}}}process3(e){this.Res2=BezierFactor*BezierFactor*this.res2;let t=e[0],i=e[6],a=e[9],r=this.normal(a,e[5],e[2],t,e[1],e[3],i),n=this.normal(t,e[1],e[3],i,e[7],e[8],a),s=this.normal(i,e[7],e[8],a,e[5],e[2],t);if(this.color){let o=this.color[0],l=this.color[1],h=this.color[2],c=this.data.Vertex(t,r,o),d=this.data.Vertex(i,n,l),m=this.data.Vertex(a,s,h);this.Render3(e,c,d,m,t,i,a,!1,!1,!1,o,l,h)}else{let o=this.vertex(t,r),l=this.vertex(i,n),h=this.vertex(a,s);this.Render3(e,o,l,h,t,i,a,!1,!1,!1)}this.data.indices.length>0&&this.append()}Render3(e,t,i,a,r,n,s,o,l,h,c,d,m){if(this.Distance3(e)<this.Res2)this.offscreen([r,n,s])||(this.data.indices.push(t),this.data.indices.push(i),this.data.indices.push(a));else{if(this.offscreen(e))return;let f=e[0],u=e[1],v=e[2],p=e[3],g=e[4],x=e[5],M=e[6],w=e[7],b=e[8],A=e[9],S=[.5*(A[0]+x[0]),.5*(A[1]+x[1]),.5*(A[2]+x[2])],R=[.5*(A[0]+b[0]),.5*(A[1]+b[1]),.5*(A[2]+b[2])],D=[.5*(x[0]+v[0]),.5*(x[1]+v[1]),.5*(x[2]+v[2])],P=[.5*(b[0]+g[0]),.5*(b[1]+g[1]),.5*(b[2]+g[2])],T=[.5*(b[0]+w[0]),.5*(b[1]+w[1]),.5*(b[2]+w[2])],y=[.5*(v[0]+g[0]),.5*(v[1]+g[1]),.5*(v[2]+g[2])],z=[.5*(v[0]+f[0]),.5*(v[1]+f[1]),.5*(v[2]+f[2])],I=[.5*(g[0]+p[0]),.5*(g[1]+p[1]),.5*(g[2]+p[2])],E=[.5*(w[0]+M[0]),.5*(w[1]+M[1]),.5*(w[2]+M[2])],O=[.5*(f[0]+u[0]),.5*(f[1]+u[1]),.5*(f[2]+u[2])],_=[.5*(u[0]+p[0]),.5*(u[1]+p[1]),.5*(u[2]+p[2])],L=[.5*(p[0]+M[0]),.5*(p[1]+M[1]),.5*(p[2]+M[2])],N=[.5*(S[0]+D[0]),.5*(S[1]+D[1]),.5*(S[2]+D[2])],B=[.5*(R[0]+T[0]),.5*(R[1]+T[1]),.5*(R[2]+T[2])],C=[.5*(D[0]+z[0]),.5*(D[1]+z[1]),.5*(D[2]+z[2])],F=[.5*P[0]+.25*(g[0]+u[0]),.5*P[1]+.25*(g[1]+u[1]),.5*P[2]+.25*(g[2]+u[2])],V=[.5*(T[0]+E[0]),.5*(T[1]+E[1]),.5*(T[2]+E[2])],H=[.5*y[0]+.25*(g[0]+w[0]),.5*y[1]+.25*(g[1]+w[1]),.5*y[2]+.25*(g[2]+w[2])],G=[.25*(x[0]+g[0])+.5*I[0],.25*(x[1]+g[1])+.5*I[1],.25*(x[2]+g[2])+.5*I[2]],U=[.5*(O[0]+_[0]),.5*(O[1]+_[1]),.5*(O[2]+_[2])],W=[.5*(_[0]+L[0]),.5*(_[1]+L[1]),.5*(_[2]+L[2])],Y=[.5*(H[0]+U[0]),.5*(H[1]+U[1]),.5*(H[2]+U[2])],j=[.5*(H[0]+W[0]),.5*(H[1]+W[1]),.5*(H[2]+W[2])],k=[.5*(U[0]+W[0]),.5*(U[1]+W[1]),.5*(U[2]+W[2])],Z=[.5*(G[0]+V[0]),.5*(G[1]+V[1]),.5*(G[2]+V[2])],X=[.5*(B[0]+G[0]),.5*(B[1]+G[1]),.5*(B[2]+G[2])],q=[.5*(B[0]+V[0]),.5*(B[1]+V[1]),.5*(B[2]+V[2])],K=[.5*(N[0]+F[0]),.5*(N[1]+F[1]),.5*(N[2]+F[2])],$=[.5*(C[0]+F[0]),.5*(C[1]+F[1]),.5*(C[2]+F[2])],Q=[.5*(N[0]+C[0]),.5*(N[1]+C[1]),.5*(N[2]+C[2])],J=[f,O,z,U,[.5*(y[0]+O[0]),.5*(y[1]+O[1]),.5*(y[2]+O[2])],C,k,Y,$,Q],ee=[k,W,j,L,[.5*(I[0]+E[0]),.5*(I[1]+E[1]),.5*(I[2]+E[2])],Z,M,E,V,q],te=[Q,K,N,X,[.5*(S[0]+P[0]),.5*(S[1]+P[1]),.5*(S[2]+P[2])],S,q,B,R,A],ie=[q,X,Z,K,[.25*(D[0]+T[0]+_[0]+g[0]),.25*(D[1]+T[1]+_[1]+g[1]),.25*(D[2]+T[2]+_[2]+g[2])],j,Q,$,Y,k],ae=this.normal(k,j,Z,q,X,K,Q),re=this.normal(q,X,K,Q,$,Y,k),ne=this.normal(Q,$,Y,k,j,Z,q),se=this.Epsilon,oe=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])];if(!o)if(o=Straightness(M,w,b,A)<this.res2){let e=unit(this.sumderivative(ie[0],ie[2],ie[5],ie[9],ie[1],ie[3],ie[6]));oe=[oe[0]-se*e[0],oe[1]-se*e[1],oe[2]-se*e[2]]}else oe=q;let le=[.5*(s[0]+r[0]),.5*(s[1]+r[1]),.5*(s[2]+r[2])];if(!l)if(l=Straightness(f,v,x,A)<this.res2){let e=unit(this.sumderivative(ie[6],ie[3],ie[1],ie[0],ie[7],ie[8],ie[9]));le=[le[0]-se*e[0],le[1]-se*e[1],le[2]-se*e[2]]}else le=Q;let he=[.5*(r[0]+n[0]),.5*(r[1]+n[1]),.5*(r[2]+n[2])];if(!h)if(h=Straightness(f,u,p,M)<this.res2){let e=unit(this.sumderivative(ie[9],ie[8],ie[7],ie[6],ie[5],ie[2],ie[0]));he=[he[0]-se*e[0],he[1]-se*e[1],he[2]-se*e[2]]}else he=k;if(c){let e=Array(4),f=Array(4),u=Array(4);for(let t=0;t<4;++t)e[t]=.5*(d[t]+m[t]),f[t]=.5*(m[t]+c[t]),u[t]=.5*(c[t]+d[t]);let v=this.data.Vertex(oe,ae,e),p=this.data.Vertex(le,re,f),g=this.data.Vertex(he,ne,u);this.Render3(J,t,g,p,r,he,le,!1,l,h,c,u,f),this.Render3(ee,g,i,v,he,n,oe,o,!1,h,u,d,e),this.Render3(te,p,v,a,le,oe,s,o,l,!1,f,e,m),this.Render3(ie,v,p,g,oe,le,he,!1,!1,!1,e,f,u)}else{let e=this.vertex(oe,ae),c=this.vertex(le,re),d=this.vertex(he,ne);this.Render3(J,t,d,c,r,he,le,!1,l,h),this.Render3(ee,d,i,e,he,n,oe,o,!1,h),this.Render3(te,c,e,a,le,oe,s,o,l,!1),this.Render3(ie,e,c,d,oe,le,he,!1,!1,!1)}}}Distance(e){let t=e[0],i=e[3],a=e[12],r=e[15],n=Distance2(r,t,this.normal(i,e[2],e[1],t,e[4],e[8],a));return n=Math.max(n,Straightness(t,e[1],e[2],i)),n=Math.max(n,Straightness(t,e[4],e[8],a)),n=Math.max(n,Straightness(i,e[7],e[11],r)),n=Math.max(n,Straightness(a,e[13],e[14],r)),n=Math.max(n,Straightness(e[4],e[5],e[6],e[7])),n=Math.max(n,Straightness(e[8],e[9],e[10],e[11])),n=Math.max(n,Straightness(e[1],e[5],e[9],e[13])),Math.max(n,Straightness(e[2],e[6],e[10],e[14]))}Distance3(e){let t=e[0],i=e[4],a=e[6],r=e[9],n=abs2([(t[0]+a[0]+r[0])*third-i[0],(t[1]+a[1]+r[1])*third-i[1],(t[2]+a[2]+r[2])*third-i[2]]);return n=Math.max(n,Straightness(t,e[1],e[3],a)),n=Math.max(n,Straightness(t,e[2],e[5],r)),Math.max(n,Straightness(a,e[7],e[8],r))}derivative(e,t,i,a){let r=[t[0]-e[0],t[1]-e[1],t[2]-e[2]];if(abs2(r)>this.epsilon)return r;let n=bezierPP(e,t,i);return abs2(n)>this.epsilon?n:bezierPPP(e,t,i,a)}sumderivative(e,t,i,a,r,n,s){let o=this.derivative(e,t,i,a),l=this.derivative(e,r,n,s);return[o[0]+l[0],o[1]+l[1],o[2]+l[2]]}normal(e,t,i,a,r,n,s){let o=r[0]-a[0],l=r[1]-a[1],h=r[2]-a[2],c=i[0]-a[0],d=i[1]-a[1],m=i[2]-a[2],f=[l*m-h*d,h*c-o*m,o*d-l*c];if(abs2(f)>this.epsilon)return unit(f);let u=[c,d,m],v=[o,l,h],p=bezierPP(a,i,t),g=bezierPP(a,r,n),x=cross(g,u),M=cross(v,p);if(abs2(f=[x[0]+M[0],x[1]+M[1],x[2]+M[2]])>this.epsilon)return unit(f);let w=bezierPPP(a,i,t,e),b=bezierPPP(a,r,n,s);x=cross(g,p),M=cross(v,w);let A=cross(b,u),S=cross(b,p),R=cross(g,w),D=cross(b,w);return unit([9*x[0]+3*(M[0]+A[0]+S[0]+R[0])+D[0],9*x[1]+3*(M[1]+A[1]+S[1]+R[1])+D[1],9*x[2]+3*(M[2]+A[2]+S[2]+R[2])+D[2]])}}class BezierCurve extends Geometry{constructor(e,t,i,a,r){super(),this.controlpoints=e,this.Min=a,this.Max=r,this.CenterIndex=t,this.MaterialIndex=i}setMaterialIndex(){this.setMaterial(material1Data,drawMaterial1)}processLine(e){let t=e[0],i=e[1];this.offscreen([t,i])||(this.data.indices.push(this.data.vertex1(t)),this.data.indices.push(this.data.vertex1(i)),this.append())}process(e){if(2==e.length)return this.processLine(e);let t=this.data.vertex1(e[0]),i=this.data.vertex1(e[3]);this.Render(e,t,i),this.data.indices.length>0&&this.append()}append(){material1Data.append(this.data)}Render(e,t,i){let a=e[0],r=e[1],n=e[2],s=e[3];if(Straightness(a,r,n,s)<this.res2)this.offscreen([a,s])||(this.data.indices.push(t),this.data.indices.push(i));else{if(this.offscreen(e))return;let o=[.5*(a[0]+r[0]),.5*(a[1]+r[1]),.5*(a[2]+r[2])],l=[.5*(r[0]+n[0]),.5*(r[1]+n[1]),.5*(r[2]+n[2])],h=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])],c=[.5*(o[0]+l[0]),.5*(o[1]+l[1]),.5*(o[2]+l[2])],d=[.5*(l[0]+h[0]),.5*(l[1]+h[1]),.5*(l[2]+h[2])],m=[.5*(c[0]+d[0]),.5*(c[1]+d[1]),.5*(c[2]+d[2])],f=[a,o,c,m],u=[m,d,h,s],v=this.data.vertex1(m);this.Render(f,t,v),this.Render(u,v,i)}}}class Pixel extends Geometry{constructor(e,t,i,a,r){super(),this.controlpoint=e,this.width=t,this.CenterIndex=0,this.MaterialIndex=i,this.Min=a,this.Max=r}setMaterialIndex(){this.setMaterial(material0Data,drawMaterial0)}process(e){this.data.indices.push(this.data.vertex0(this.controlpoint,this.width)),this.append()}append(){material0Data.append(this.data)}}class Triangles extends Geometry{constructor(e,t,i){super(),this.CenterIndex=0,this.MaterialIndex=e,this.Min=t,this.Max=i,this.Positions=Positions,this.Normals=Normals,this.Colors=Colors,this.Indices=Indices,Positions=[],Normals=[],Colors=[],Indices=[],this.transparent=Materials[e].diffuse[3]<1}setMaterialIndex(){this.transparent?this.setMaterial(transparentData,drawTransparent):this.setMaterial(triangleData,drawTriangle)}process(e){materialIndex=this.Colors.length>0?-1-materialIndex:1+materialIndex;for(let e=0,t=this.Indices.length;e<t;++e){let t=this.Indices[e],i=t[0],a=this.Positions[i[0]],r=this.Positions[i[1]],n=this.Positions[i[2]];if(!this.offscreen([a,r,n])){let e=t.length>1?t[1]:i;if(e&&0!=e.length||(e=i),this.Colors.length>0){let s=t.length>2?t[2]:i;s&&0!=s.length||(s=i);let o=this.Colors[s[0]],l=this.Colors[s[1]],h=this.Colors[s[2]];this.transparent|=o[3]+l[3]+h[3]<765,this.data.iVertex(i[0],a,this.Normals[e[0]],o),this.data.iVertex(i[1],r,this.Normals[e[1]],l),this.data.iVertex(i[2],n,this.Normals[e[2]],h)}else this.data.iVertex(i[0],a,this.Normals[e[0]]),this.data.iVertex(i[1],r,this.Normals[e[1]]),this.data.iVertex(i[2],n,this.Normals[e[2]])}}this.data.nvertices=this.Positions.length,this.data.indices.length>0&&this.append()}append(){this.transparent?transparentData.append(this.data):triangleData.append(this.data)}}function home(){mat4.identity(rotMat),initProjection(),setProjection(),remesh=!0,draw()}let positionAttribute=0,normalAttribute=1,materialAttribute=2,colorAttribute=3,widthAttribute=4;function initShader(e=[]){let t=getShader(gl,vertex,gl.VERTEX_SHADER,e),i=getShader(gl,fragment,gl.FRAGMENT_SHADER,e),a=gl.createProgram();return gl.attachShader(a,t),gl.attachShader(a,i),gl.bindAttribLocation(a,positionAttribute,"position"),gl.bindAttribLocation(a,normalAttribute,"normal"),gl.bindAttribLocation(a,materialAttribute,"materialIndex"),gl.bindAttribLocation(a,colorAttribute,"color"),gl.bindAttribLocation(a,widthAttribute,"width"),gl.linkProgram(a),gl.getProgramParameter(a,gl.LINK_STATUS)||alert("Could not initialize shaders"),a}class Split3{constructor(e,t,i,a){this.m0=[.5*(e[0]+t[0]),.5*(e[1]+t[1]),.5*(e[2]+t[2])];let r=.5*(t[0]+i[0]),n=.5*(t[1]+i[1]),s=.5*(t[2]+i[2]);this.m2=[.5*(i[0]+a[0]),.5*(i[1]+a[1]),.5*(i[2]+a[2])],this.m3=[.5*(this.m0[0]+r),.5*(this.m0[1]+n),.5*(this.m0[2]+s)],this.m4=[.5*(r+this.m2[0]),.5*(n+this.m2[1]),.5*(s+this.m2[2])],this.m5=[.5*(this.m3[0]+this.m4[0]),.5*(this.m3[1]+this.m4[1]),.5*(this.m3[2]+this.m4[2])]}}function iszero(e){return 0==e[0]&&0==e[1]&&0==e[2]}function unit(e){let t=1/(Math.sqrt(e[0]*e[0]+e[1]*e[1]+e[2]*e[2])||1);return[e[0]*t,e[1]*t,e[2]*t]}function abs2(e){return e[0]*e[0]+e[1]*e[1]+e[2]*e[2]}function dot(e,t){return e[0]*t[0]+e[1]*t[1]+e[2]*t[2]}function cross(e,t){return[e[1]*t[2]-e[2]*t[1],e[2]*t[0]-e[0]*t[2],e[0]*t[1]-e[1]*t[0]]}function bezierPP(e,t,i){return[e[0]+i[0]-2*t[0],e[1]+i[1]-2*t[1],e[2]+i[2]-2*t[2]]}function bezierPPP(e,t,i,a){return[a[0]-e[0]+3*(t[0]-i[0]),a[1]-e[1]+3*(t[1]-i[1]),a[2]-e[2]+3*(t[2]-i[2])]}function Straightness(e,t,i,a){let r=[third*(a[0]-e[0]),third*(a[1]-e[1]),third*(a[2]-e[2])];return Math.max(abs2([t[0]-r[0]-e[0],t[1]-r[1]-e[1],t[2]-r[2]-e[2]]),abs2([a[0]-r[0]-i[0],a[1]-r[1]-i[1],a[2]-r[2]-i[2]]))}function Distance2(e,t,i){let a=dot([e[0]-t[0],e[1]-t[1],e[2]-t[2]],i);return a*a}function corners(e,t){return[e,[e[0],e[1],t[2]],[e[0],t[1],e[2]],[e[0],t[1],t[2]],[t[0],e[1],e[2]],[t[0],e[1],t[2]],[t[0],t[1],e[2]],t]}function COBTarget(e,t){mat4.fromTranslation(translMat,[center.x,center.y,center.z]),mat4.invert(cjMatInv,translMat),mat4.multiply(e,t,cjMatInv),mat4.multiply(e,translMat,e)}function setUniforms(e,t){let i=t==pixelShader;gl.useProgram(t),gl.enableVertexAttribArray(positionAttribute),i&&gl.enableVertexAttribArray(widthAttribute);let a=t!=noNormalShader&&!i&&Lights.length>0;if(a&&gl.enableVertexAttribArray(normalAttribute),gl.enableVertexAttribArray(materialAttribute),t.projViewMatUniform=gl.getUniformLocation(t,"projViewMat"),t.viewMatUniform=gl.getUniformLocation(t,"viewMat"),t.normMatUniform=gl.getUniformLocation(t,"normMat"),t!=colorShader&&t!=transparentShader||gl.enableVertexAttribArray(colorAttribute),a)for(let e=0;e<Lights.length;++e)Lights[e].setUniform(t,e);for(let i=0;i<e.materials.length;++i)e.materials[i].setUniform(t,i);gl.uniformMatrix4fv(t.projViewMatUniform,!1,projViewMat),gl.uniformMatrix4fv(t.viewMatUniform,!1,viewMat),gl.uniformMatrix3fv(t.normMatUniform,!1,normMat)}function handleMouseDown(e){zoomEnabled||enableZoom(),mouseDownOrTouchActive=!0,lastMouseX=e.clientX,lastMouseY=e.clientY}let pinchStart,touchStartTime,pinch=!1;function pinchDistance(e){return Math.hypot(e[0].pageX-e[1].pageX,e[0].pageY-e[1].pageY)}function handleTouchStart(e){e.preventDefault(),zoomEnabled||enableZoom();let t=e.targetTouches;swipe=rotate=pinch=!1,zooming||(1!=t.length||mouseDownOrTouchActive||(touchStartTime=(new Date).getTime(),touchId=t[0].identifier,lastMouseX=t[0].pageX,lastMouseY=t[0].pageY),2!=t.length||mouseDownOrTouchActive||(touchId=t[0].identifier,pinchStart=pinchDistance(t),pinch=!0))}function handleMouseUpOrTouchEnd(e){mouseDownOrTouchActive=!1}function rotateScene(e,t,i,a,r){if(e==i&&t==a)return;let[n,s]=arcball([e,-t],[i,-a]);mat4.fromRotation(rotMats,2*r*ArcballFactor*n/lastzoom,s),mat4.multiply(rotMat,rotMats,rotMat)}function shiftScene(e,t,i,a){let r=1/lastzoom;shift.x+=(i-e)*r*halfCanvasWidth,shift.y-=(a-t)*r*halfCanvasHeight}function panScene(e,t,i,a){orthographic?shiftScene(e,t,i,a):(center.x+=(i-e)*(viewParam.xmax-viewParam.xmin),center.y-=(a-t)*(viewParam.ymax-viewParam.ymin))}function updateViewMatrix(){COBTarget(viewMat,rotMat),mat4.translate(viewMat,viewMat,[center.x,center.y,0]),mat3.fromMat4(viewMat3,viewMat),mat3.invert(normMat,viewMat3),mat4.multiply(projViewMat,projMat,viewMat)}function capzoom(){let e=Math.sqrt(Number.MAX_VALUE),t=1/e;Zoom<=t&&(Zoom=t),Zoom>=e&&(Zoom=e),Zoom!=lastzoom&&(remesh=!0),lastzoom=Zoom}function zoomImage(e){let t=zoomStep*halfCanvasHeight*e;const i=Math.log(.1*Number.MAX_VALUE)/Math.log(zoomFactor);Math.abs(t)<i&&(Zoom*=zoomFactor**t,capzoom())}function normMouse(e){let t=e[0],i=e[1],a=Math.hypot(t,i);return a>1&&(denom=1/a,t*=denom,i*=denom),[t,i,Math.sqrt(Math.max(1-i*i-t*t,0))]}function arcball(e,t){let i=normMouse(e),a=normMouse(t),r=dot(i,a);return r>1?r=1:r<-1&&(r=-1),[Math.acos(r),unit(cross(i,a))]}function zoomScene(e,t,i,a){zoomImage(t-a)}const DRAGMODE_ROTATE=1,DRAGMODE_SHIFT=2,DRAGMODE_ZOOM=3,DRAGMODE_PAN=4;function processDrag(e,t,i,a=1){let r;switch(i){case DRAGMODE_ROTATE:r=rotateScene;break;case DRAGMODE_SHIFT:r=shiftScene;break;case DRAGMODE_ZOOM:r=zoomScene;break;case DRAGMODE_PAN:r=panScene;break;default:r=((e,t,i,a)=>{})}r((lastMouseX-halfCanvasWidth)/halfCanvasWidth,(lastMouseY-halfCanvasHeight)/halfCanvasHeight,(e-halfCanvasWidth)/halfCanvasWidth,(t-halfCanvasHeight)/halfCanvasHeight,a),lastMouseX=e,lastMouseY=t,setProjection(),draw()}let zoomEnabled=0;function enableZoom(){zoomEnabled=1,canvas.addEventListener("wheel",handleMouseWheel,!1)}function disableZoom(){zoomEnabled=0,canvas.removeEventListener("wheel",handleMouseWheel,!1)}function handleKey(e){if(zoomEnabled||enableZoom(),embedded&&zoomEnabled&&27==e.keyCode)return void disableZoom();let t=[];switch(e.key){case"x":t=[1,0,0];break;case"y":t=[0,1,0];break;case"z":t=[0,0,1];break;case"h":home();break;case"+":case"=":case">":expand();break;case"-":case"_":case"<":shrink()}t.length>0&&(mat4.rotate(rotMat,rotMat,.1,t),updateViewMatrix(),draw())}function handleMouseWheel(e){e.preventDefault(),e.deltaY<0?Zoom*=zoomFactor:Zoom/=zoomFactor,capzoom(),setProjection(),draw()}function handleMouseMove(e){if(!mouseDownOrTouchActive)return;let t;processDrag(e.clientX,e.clientY,t=e.getModifierState("Control")?DRAGMODE_SHIFT:e.getModifierState("Shift")?DRAGMODE_ZOOM:e.getModifierState("Alt")?DRAGMODE_PAN:DRAGMODE_ROTATE)}let zooming=!1,swipe=!1,rotate=!1;function handleTouchMove(e){if(e.preventDefault(),zooming)return;let t=e.targetTouches;if(!pinch&&1==t.length&&touchId==t[0].identifier){let e=t[0].pageX,i=t[0].pageY,a=e-lastMouseX,r=i-lastMouseY,n=a*a+r*r<=shiftHoldDistance*shiftHoldDistance;if(n&&!swipe&&!rotate&&(new Date).getTime()-touchStartTime>shiftWaitTime&&(navigator.vibrate&&window.navigator.vibrate(vibrateTime),swipe=!0),swipe)processDrag(e,i,DRAGMODE_SHIFT);else if(!n){rotate=!0,processDrag(t[0].pageX,t[0].pageY,DRAGMODE_ROTATE,.5)}}if(pinch&&!swipe&&2==t.length&&touchId==t[0].identifier){let e=pinchDistance(t),i=e-pinchStart;zooming=!0,(i*=zoomPinchFactor)>zoomPinchCap&&(i=zoomPinchCap),i<-zoomPinchCap&&(i=-zoomPinchCap),zoomImage(i/size2),pinchStart=e,swipe=rotate=zooming=!1,setProjection(),draw()}}let pixelShader,noNormalShader,materialShader,colorShader,transparentShader,zbuffer=[];function transformVertices(e){let t=viewMat[2],i=viewMat[6],a=viewMat[10];zbuffer.length=e.length;for(let r=0;r<e.length;++r){let n=6*r;zbuffer[r]=t*e[n]+i*e[n+1]+a*e[n+2]}}function drawMaterial0(){drawBuffer(material0Data,pixelShader),material0Data.clear()}function drawMaterial1(){drawBuffer(material1Data,noNormalShader),material1Data.clear()}function drawMaterial(){drawBuffer(materialData,materialShader),materialData.clear()}function drawColor(){drawBuffer(colorData,colorShader),colorData.clear()}function drawTriangle(){drawBuffer(triangleData,transparentShader),triangleData.clear()}function drawTransparent(){let e=transparentData.indices;if(e.length>0){transformVertices(transparentData.vertices);let t=e.length/3,i=Array(t).fill().map((e,t)=>t);i.sort(function(t,i){let a=3*t;Ia=e[a],Ib=e[a+1],Ic=e[a+2];let r=3*i;return IA=e[r],IB=e[r+1],IC=e[r+2],zbuffer[Ia]+zbuffer[Ib]+zbuffer[Ic]<zbuffer[IA]+zbuffer[IB]+zbuffer[IC]?-1:1});let a=Array(e.length);for(let r=0;r<t;++r){let t=3*i[r];a[3*r]=e[t],a[3*r+1]=e[t+1],a[3*r+2]=e[t+2]}gl.depthMask(!1),drawBuffer(transparentData,transparentShader,a),gl.depthMask(!0)}transparentData.clear()}function drawBuffers(){drawMaterial0(),drawMaterial1(),drawMaterial(),drawColor(),drawTriangle(),drawTransparent()}function draw(){embedded&&(offscreen.width=canvas.width,offscreen.height=canvas.height,setViewport()),gl.clearColor(Background[0],Background[1],Background[2],Background[3]),gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);for(let e=0;e<P.length;++e)P[e].render();drawBuffers(),embedded&&(context.clearRect(0,0,canvas.width,canvas.height),context.drawImage(offscreen,0,0)),remesh=!1}function setDimensions(e,t,i,a){let r=e/t,n=1/lastzoom,s=(i/e+viewportshift[0])*lastzoom,o=(a/t+viewportshift[1])*lastzoom;if(orthographic){let e=B[0]-b[0],t=B[1]-b[1];if(e<t*r){let e=.5*t*r*n,i=2*e*s,a=t*n*o;viewParam.xmin=-e-i,viewParam.xmax=e-i,viewParam.ymin=b[1]*n-a,viewParam.ymax=B[1]*n-a}else{let t=.5*e/(r*Zoom),i=e*n*s,a=2*t*o;viewParam.xmin=b[0]*n-i,viewParam.xmax=B[0]*n-i,viewParam.ymin=-t-a,viewParam.ymax=t-a}}else{let e=H*n,t=e*r,i=2*t*s,a=2*e*o;viewParam.xmin=-t-i,viewParam.xmax=t-i,viewParam.ymin=-e-a,viewParam.ymax=e-a}}function setProjection(){setDimensions(canvasWidth,canvasHeight,shift.x,shift.y),(orthographic?mat4.ortho:mat4.frustum)(projMat,viewParam.xmin,viewParam.xmax,viewParam.ymin,viewParam.ymax,-viewParam.zmax,-viewParam.zmin),updateViewMatrix()}function initProjection(){H=-Math.tan(.5*angle)*B[2],center.x=center.y=0,center.z=.5*(b[2]+B[2]),lastzoom=Zoom=Zoom0,viewParam.zmin=b[2],viewParam.zmax=B[2],shift.x=shift.y=0}function setViewport(){gl.viewportWidth=canvasWidth,gl.viewportHeight=canvasHeight,gl.viewport(0,0,gl.viewportWidth,gl.viewportHeight),gl.scissor(0,0,gl.viewportWidth,gl.viewportHeight)}function setCanvas(){canvas.width=canvasWidth,canvas.height=canvasHeight,embedded&&(offscreen.width=canvasWidth,offscreen.height=canvasHeight),size2=Math.hypot(canvasWidth,canvasHeight),halfCanvasWidth=.5*canvasWidth,halfCanvasHeight=.5*canvasHeight}function setsize(e,t){e>maxViewportWidth&&(e=maxViewportWidth),t>maxViewportHeight&&(t=maxViewportHeight),shift.x*=e/canvasWidth,shift.y*=t/canvasHeight,canvasWidth=e,canvasHeight=t,setCanvas(),setViewport(),home()}function expand(){setsize(canvasWidth*resizeStep+.5,canvasHeight*resizeStep+.5)}function shrink(){setsize(Math.max(canvasWidth/resizeStep+.5,1),Math.max(canvasHeight/resizeStep+.5,1))}function webGLStart(){if(canvas=document.getElementById("Asymptote"),embedded=window.top.document!=document,initGL(),absolute&&!embedded)canvasWidth*=window.devicePixelRatio,canvasHeight*=window.devicePixelRatio;else{canvas.width=Math.max(window.innerWidth-windowTrim,windowTrim),canvas.height=Math.max(window.innerHeight-windowTrim,windowTrim);let e=canvasWidth/canvasHeight;canvas.width>canvas.height*e?canvas.width=Math.min(canvas.height*e,canvas.width):canvas.height=Math.min(canvas.width/e,canvas.height),canvas.width>0&&(canvasWidth=canvas.width),canvas.height>0&&(canvasHeight=canvas.height)}setCanvas(),ArcballFactor=1+8*Math.hypot(viewportmargin[0],viewportmargin[1])/size2,viewportshift[0]/=Zoom0,viewportshift[1]/=Zoom0,gl.enable(gl.BLEND),gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA),gl.enable(gl.DEPTH_TEST),gl.enable(gl.SCISSOR_TEST),setViewport(),home(),canvas.onmousedown=handleMouseDown,document.onmouseup=handleMouseUpOrTouchEnd,document.onmousemove=handleMouseMove,canvas.onkeydown=handleKey,embedded||enableZoom(),canvas.addEventListener("touchstart",handleTouchStart,!1),canvas.addEventListener("touchend",handleMouseUpOrTouchEnd,!1),canvas.addEventListener("touchcancel",handleMouseUpOrTouchEnd,!1),canvas.addEventListener("touchleave",handleMouseUpOrTouchEnd,!1),canvas.addEventListener("touchmove",handleTouchMove,!1),document.addEventListener("keydown",handleKey,!1)}
+let vertex="\nattribute vec3 position;\n#ifdef WIDTH\nattribute float width;\n#endif\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\nattribute float materialIndex;\n#ifdef COLOR\nattribute vec4 color;\n#endif\n\nuniform mat3 normMat;\nuniform mat4 viewMat;\nuniform mat4 projViewMat;\n\n#ifdef NORMAL\n#ifndef ORTHOGRAPHIC\nvarying vec3 ViewPosition;\n#endif\nvarying vec3 Normal;\n#endif\nvarying vec4 diffuse;\nvarying vec3 specular;\nvarying float roughness,metallic,fresnel0;\nvarying vec4 emissive;\n\nstruct Material {\n vec4 diffuse,emissive,specular;\n vec4 parameters;\n};\n\nuniform Material Materials[Nmaterials];\n\nvoid main(void)\n{\n vec4 v=vec4(position,1.0);\n gl_Position=projViewMat*v;\n#ifdef NORMAL\n#ifndef ORTHOGRAPHIC\n ViewPosition=(viewMat*v).xyz;\n#endif \n Normal=normalize(normal*normMat);\n \n Material m;\n#ifdef TRANSPARENT\n m=Materials[int(abs(materialIndex))-1];\n emissive=m.emissive;\n if(materialIndex >= 0.0) {\n diffuse=m.diffuse;\n } else {\n diffuse=color;\n#if nlights == 0\n emissive += color;\n#endif\n }\n#else\n m=Materials[int(materialIndex)];\n emissive=m.emissive;\n#ifdef COLOR\n diffuse=color;\n#if nlights == 0\n emissive += color;\n#endif\n#else\n diffuse=m.diffuse;\n#endif\n#endif\n specular=m.specular.rgb;\n vec4 parameters=m.parameters;\n roughness=1.0-parameters[0];\n metallic=parameters[1];\n fresnel0=parameters[2];\n#else\n emissive=Materials[int(materialIndex)].emissive;\n#endif\n#ifdef WIDTH\n gl_PointSize=width;\n#endif\n}\n",fragment="\n#ifdef NORMAL\n#ifndef ORTHOGRAPHIC\nvarying vec3 ViewPosition;\n#endif\nvarying vec3 Normal;\nvarying vec4 diffuse;\nvarying vec3 specular;\nvarying float roughness,metallic,fresnel0;\n\nfloat Roughness2;\nvec3 normal;\n\nstruct Light {\n vec3 direction;\n vec3 color;\n};\n\nuniform Light Lights[Nlights];\n\nfloat NDF_TRG(vec3 h)\n{\n float ndoth=max(dot(normal,h),0.0);\n float alpha2=Roughness2*Roughness2;\n float denom=ndoth*ndoth*(alpha2-1.0)+1.0;\n return denom != 0.0 ? alpha2/(denom*denom) : 0.0;\n}\n \nfloat GGX_Geom(vec3 v)\n{\n float ndotv=max(dot(v,normal),0.0);\n float ap=1.0+Roughness2;\n float k=0.125*ap*ap;\n return ndotv/((ndotv*(1.0-k))+k);\n}\n \nfloat Geom(vec3 v, vec3 l)\n{\n return GGX_Geom(v)*GGX_Geom(l);\n}\n \nfloat Fresnel(vec3 h, vec3 v, float fresnel0)\n{\n float a=1.0-max(dot(h,v),0.0);\n float b=a*a;\n return fresnel0+(1.0-fresnel0)*b*b*a;\n}\n \n// physical based shading using UE4 model.\nvec3 BRDF(vec3 viewDirection, vec3 lightDirection)\n{\n vec3 lambertian=diffuse.rgb;\n vec3 h=normalize(lightDirection+viewDirection);\n \n float omegain=max(dot(viewDirection,normal),0.0);\n float omegali=max(dot(lightDirection,normal),0.0);\n \n float D=NDF_TRG(h);\n float G=Geom(viewDirection,lightDirection);\n float F=Fresnel(h,viewDirection,fresnel0);\n \n float denom=4.0*omegain*omegali;\n float rawReflectance=denom > 0.0 ? (D*G)/denom : 0.0;\n \n vec3 dielectric=mix(lambertian,rawReflectance*specular,F);\n vec3 metal=rawReflectance*diffuse.rgb;\n \n return mix(dielectric,metal,metallic);\n}\n#endif\nvarying vec4 emissive;\n \nvoid main(void)\n{\n#if defined(NORMAL) && nlights > 0\n normal=normalize(Normal);\n normal=gl_FrontFacing ? normal : -normal;\n#ifdef ORTHOGRAPHIC\n vec3 viewDir=vec3(0.0,0.0,1.0);\n#else\n vec3 viewDir=-normalize(ViewPosition);\n#endif\n Roughness2=roughness*roughness;\n vec3 color=emissive.rgb;\n for(int i=0; i < nlights; ++i) {\n Light Li=Lights[i];\n vec3 L=Li.direction;\n float cosTheta=max(dot(normal,L),0.0);\n vec3 radiance=cosTheta*Li.color;\n color += BRDF(viewDir,L)*radiance;\n }\n gl_FragColor=vec4(color,diffuse.a);\n#else\n gl_FragColor=emissive;\n#endif\n}\n";!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var i=e();for(var a in i)("object"==typeof exports?exports:t)[a]=i[a]}}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function i(a){if(e[a])return e[a].exports;var r=e[a]={i:a,l:!1,exports:{}};return t[a].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=t,i.c=e,i.d=function(t,e,a){i.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:a})},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=1)}([function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.setMatrixArrayType=function(t){e.ARRAY_TYPE=t},e.toRadian=function(t){return t*r},e.equals=function(t,e){return Math.abs(t-e)<=a*Math.max(1,Math.abs(t),Math.abs(e))};var a=e.EPSILON=1e-6;e.ARRAY_TYPE="undefined"!=typeof Float32Array?Float32Array:Array,e.RANDOM=Math.random;var r=Math.PI/180},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.mat4=e.mat3=void 0;var a=n(i(2)),r=n(i(3));function n(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e.default=t,e}e.mat3=a,e.mat4=r},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.create=function(){var t=new a.ARRAY_TYPE(9);return t[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=1,t[5]=0,t[6]=0,t[7]=0,t[8]=1,t},e.fromMat4=function(t,e){return t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[4],t[4]=e[5],t[5]=e[6],t[6]=e[8],t[7]=e[9],t[8]=e[10],t},e.invert=function(t,e){var i=e[0],a=e[1],r=e[2],n=e[3],s=e[4],o=e[5],h=e[6],l=e[7],d=e[8],c=d*s-o*l,m=-d*n+o*h,f=l*n-s*h,u=i*c+a*m+r*f;if(!u)return null;return u=1/u,t[0]=c*u,t[1]=(-d*a+r*l)*u,t[2]=(o*a-r*s)*u,t[3]=m*u,t[4]=(d*i-r*h)*u,t[5]=(-o*i+r*n)*u,t[6]=f*u,t[7]=(-l*i+a*h)*u,t[8]=(s*i-a*n)*u,t};var a=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e.default=t,e}(i(0))},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.create=function(){var t=new a.ARRAY_TYPE(16);return t[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=1,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=1,t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},e.identity=function(t){return t[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=1,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=1,t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},e.invert=function(t,e){var i=e[0],a=e[1],r=e[2],n=e[3],s=e[4],o=e[5],h=e[6],l=e[7],d=e[8],c=e[9],m=e[10],f=e[11],u=e[12],p=e[13],v=e[14],g=e[15],w=i*o-a*s,x=i*h-r*s,M=i*l-n*s,b=a*h-r*o,A=a*l-n*o,S=r*l-n*h,P=d*p-c*u,R=d*v-m*u,T=d*g-f*u,y=c*v-m*p,D=c*g-f*p,I=m*g-f*v,z=w*I-x*D+M*y+b*T-A*R+S*P;if(!z)return null;return z=1/z,t[0]=(o*I-h*D+l*y)*z,t[1]=(r*D-a*I-n*y)*z,t[2]=(p*S-v*A+g*b)*z,t[3]=(m*A-c*S-f*b)*z,t[4]=(h*T-s*I-l*R)*z,t[5]=(i*I-r*T+n*R)*z,t[6]=(v*M-u*S-g*x)*z,t[7]=(d*S-m*M+f*x)*z,t[8]=(s*D-o*T+l*P)*z,t[9]=(a*T-i*D-n*P)*z,t[10]=(u*A-p*M+g*w)*z,t[11]=(c*M-d*A-f*w)*z,t[12]=(o*R-s*y-h*P)*z,t[13]=(i*y-a*R+r*P)*z,t[14]=(p*x-u*b-v*w)*z,t[15]=(d*b-c*x+m*w)*z,t},e.multiply=r,e.translate=function(t,e,i){var a=i[0],r=i[1],n=i[2],s=void 0,o=void 0,h=void 0,l=void 0,d=void 0,c=void 0,m=void 0,f=void 0,u=void 0,p=void 0,v=void 0,g=void 0;e===t?(t[12]=e[0]*a+e[4]*r+e[8]*n+e[12],t[13]=e[1]*a+e[5]*r+e[9]*n+e[13],t[14]=e[2]*a+e[6]*r+e[10]*n+e[14],t[15]=e[3]*a+e[7]*r+e[11]*n+e[15]):(s=e[0],o=e[1],h=e[2],l=e[3],d=e[4],c=e[5],m=e[6],f=e[7],u=e[8],p=e[9],v=e[10],g=e[11],t[0]=s,t[1]=o,t[2]=h,t[3]=l,t[4]=d,t[5]=c,t[6]=m,t[7]=f,t[8]=u,t[9]=p,t[10]=v,t[11]=g,t[12]=s*a+d*r+u*n+e[12],t[13]=o*a+c*r+p*n+e[13],t[14]=h*a+m*r+v*n+e[14],t[15]=l*a+f*r+g*n+e[15]);return t},e.rotate=function(t,e,i,r){var n=r[0],s=r[1],o=r[2],h=Math.sqrt(n*n+s*s+o*o),l=void 0,d=void 0,c=void 0,m=void 0,f=void 0,u=void 0,p=void 0,v=void 0,g=void 0,w=void 0,x=void 0,M=void 0,b=void 0,A=void 0,S=void 0,P=void 0,R=void 0,T=void 0,y=void 0,D=void 0,I=void 0,z=void 0,E=void 0,O=void 0;if(Math.abs(h)<a.EPSILON)return null;n*=h=1/h,s*=h,o*=h,l=Math.sin(i),d=Math.cos(i),c=1-d,m=e[0],f=e[1],u=e[2],p=e[3],v=e[4],g=e[5],w=e[6],x=e[7],M=e[8],b=e[9],A=e[10],S=e[11],P=n*n*c+d,R=s*n*c+o*l,T=o*n*c-s*l,y=n*s*c-o*l,D=s*s*c+d,I=o*s*c+n*l,z=n*o*c+s*l,E=s*o*c-n*l,O=o*o*c+d,t[0]=m*P+v*R+M*T,t[1]=f*P+g*R+b*T,t[2]=u*P+w*R+A*T,t[3]=p*P+x*R+S*T,t[4]=m*y+v*D+M*I,t[5]=f*y+g*D+b*I,t[6]=u*y+w*D+A*I,t[7]=p*y+x*D+S*I,t[8]=m*z+v*E+M*O,t[9]=f*z+g*E+b*O,t[10]=u*z+w*E+A*O,t[11]=p*z+x*E+S*O,e!==t&&(t[12]=e[12],t[13]=e[13],t[14]=e[14],t[15]=e[15]);return t},e.fromTranslation=function(t,e){return t[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=1,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=1,t[11]=0,t[12]=e[0],t[13]=e[1],t[14]=e[2],t[15]=1,t},e.fromRotation=function(t,e,i){var r=i[0],n=i[1],s=i[2],o=Math.sqrt(r*r+n*n+s*s),h=void 0,l=void 0,d=void 0;if(Math.abs(o)<a.EPSILON)return null;return r*=o=1/o,n*=o,s*=o,h=Math.sin(e),l=Math.cos(e),d=1-l,t[0]=r*r*d+l,t[1]=n*r*d+s*h,t[2]=s*r*d-n*h,t[3]=0,t[4]=r*n*d-s*h,t[5]=n*n*d+l,t[6]=s*n*d+r*h,t[7]=0,t[8]=r*s*d+n*h,t[9]=n*s*d-r*h,t[10]=s*s*d+l,t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},e.frustum=function(t,e,i,a,r,n,s){var o=1/(i-e),h=1/(r-a),l=1/(n-s);return t[0]=2*n*o,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=2*n*h,t[6]=0,t[7]=0,t[8]=(i+e)*o,t[9]=(r+a)*h,t[10]=(s+n)*l,t[11]=-1,t[12]=0,t[13]=0,t[14]=s*n*2*l,t[15]=0,t},e.ortho=function(t,e,i,a,r,n,s){var o=1/(e-i),h=1/(a-r),l=1/(n-s);return t[0]=-2*o,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=-2*h,t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=2*l,t[11]=0,t[12]=(e+i)*o,t[13]=(r+a)*h,t[14]=(s+n)*l,t[15]=1,t};var a=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e.default=t,e}(i(0));function r(t,e,i){var a=e[0],r=e[1],n=e[2],s=e[3],o=e[4],h=e[5],l=e[6],d=e[7],c=e[8],m=e[9],f=e[10],u=e[11],p=e[12],v=e[13],g=e[14],w=e[15],x=i[0],M=i[1],b=i[2],A=i[3];return t[0]=x*a+M*o+b*c+A*p,t[1]=x*r+M*h+b*m+A*v,t[2]=x*n+M*l+b*f+A*g,t[3]=x*s+M*d+b*u+A*w,x=i[4],M=i[5],b=i[6],A=i[7],t[4]=x*a+M*o+b*c+A*p,t[5]=x*r+M*h+b*m+A*v,t[6]=x*n+M*l+b*f+A*g,t[7]=x*s+M*d+b*u+A*w,x=i[8],M=i[9],b=i[10],A=i[11],t[8]=x*a+M*o+b*c+A*p,t[9]=x*r+M*h+b*m+A*v,t[10]=x*n+M*l+b*f+A*g,t[11]=x*s+M*d+b*u+A*w,x=i[12],M=i[13],b=i[14],A=i[15],t[12]=x*a+M*o+b*c+A*p,t[13]=x*r+M*h+b*m+A*v,t[14]=x*n+M*l+b*f+A*g,t[15]=x*s+M*d+b*u+A*w,t}}])});let canvasWidth,canvasHeight,b,B,angle,Zoom0,viewportmargin,zoomFactor,zoomPinchFactor,zoomPinchCap,zoomStep,shiftHoldDistance,shiftWaitTime,vibrateTime,embedded,canvas,gl,alpha,offscreen,context,maxMaterials,halfCanvasWidth,halfCanvasHeight,Zoom,P=[],Materials=[],Lights=[],Centers=[],Background=[1,1,1,1],absolute=!1,viewportshift=[0,0],nlights=0,Nmaterials=2,materials=[],pixel=.75,FillFactor=.1,maxViewportWidth=window.innerWidth,maxViewportHeight=window.innerHeight;const windowTrim=10;let lastzoom,H,zmin,zmax,size2,ArcballFactor,positionBuffer,materialBuffer,colorBuffer,indexBuffer,resizeStep=1.2,third=1/3,rotMat=mat4.create(),projMat=mat4.create(),viewMat=mat4.create(),projViewMat=mat4.create(),normMat=mat3.create(),viewMat3=mat3.create(),cjMatInv=mat4.create(),T=mat4.create(),center={x:0,y:0,z:0},shift={x:0,y:0},viewParam={xmin:0,xmax:0,ymin:0,ymax:0,zmin:0,zmax:0},remesh=!0,wireframe=0,mouseDownOrTouchActive=!1,lastMouseX=null,lastMouseY=null,touchID=null,Positions=[],Normals=[],Colors=[],Indices=[];class Material{constructor(t,e,i,a,r,n){this.diffuse=t,this.emissive=e,this.specular=i,this.shininess=a,this.metallic=r,this.fresnel0=n}setUniform(t,e){let i=i=>gl.getUniformLocation(t,"Materials["+e+"]."+i);gl.uniform4fv(i("diffuse"),new Float32Array(this.diffuse)),gl.uniform4fv(i("emissive"),new Float32Array(this.emissive)),gl.uniform4fv(i("specular"),new Float32Array(this.specular)),gl.uniform4f(i("parameters"),this.shininess,this.metallic,this.fresnel0,0)}}let indexExt,TRIANGLES,material0Data,material1Data,materialData,colorData,transparentData,triangleData,materialIndex,enumPointLight=1,enumDirectionalLight=2;class Light{constructor(t,e){this.direction=t,this.color=e}setUniform(t,e){let i=i=>gl.getUniformLocation(t,"Lights["+e+"]."+i);gl.uniform3fv(i("direction"),new Float32Array(this.direction)),gl.uniform3fv(i("color"),new Float32Array(this.color))}}function initShaders(){let t=gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS);maxMaterials=Math.floor((t-14)/4),Nmaterials=Math.min(Math.max(Nmaterials,Materials.length),maxMaterials),pixelShader=initShader(["WIDTH"]),materialShader=initShader(["NORMAL"]),colorShader=initShader(["NORMAL","COLOR"]),transparentShader=initShader(["NORMAL","COLOR","TRANSPARENT"])}function deleteShaders(){gl.deleteProgram(transparentShader),gl.deleteProgram(colorShader),gl.deleteProgram(materialShader),gl.deleteProgram(pixelShader)}function setBuffers(){positionBuffer=gl.createBuffer(),materialBuffer=gl.createBuffer(),colorBuffer=gl.createBuffer(),indexBuffer=gl.createBuffer()}function noGL(){gl||alert("Could not initialize WebGL")}function saveAttributes(){let t=window.top.document.asygl[alpha];t.gl=gl,t.nlights=Lights.length,t.Nmaterials=Nmaterials,t.maxMaterials=maxMaterials,t.pixelShader=pixelShader,t.materialShader=materialShader,t.colorShader=colorShader,t.transparentShader=transparentShader}function restoreAttributes(){let t=window.top.document.asygl[alpha];gl=t.gl,nlights=t.nlights,Nmaterials=t.Nmaterials,maxMaterials=t.maxMaterials,pixelShader=t.pixelShader,materialShader=t.materialShader,colorShader=t.colorShader,transparentShader=t.transparentShader}function initGL(){if(alpha=Background[3]<1,embedded){let t=window.top.document;null==t.asygl&&(t.asygl=Array(2)),context=canvas.getContext("2d"),(offscreen=t.offscreen)||(offscreen=t.createElement("canvas"),t.offscreen=offscreen),t.asygl[alpha]&&t.asygl[alpha].gl?(restoreAttributes(),(Lights.length!=nlights||Math.min(Materials.length,maxMaterials)>Nmaterials)&&(initShaders(),saveAttributes())):((gl=offscreen.getContext("webgl",{alpha:alpha}))||noGL(),initShaders(),t.asygl[alpha]={},saveAttributes())}else(gl=canvas.getContext("webgl",{alpha:alpha}))||noGL(),initShaders();setBuffers(),indexExt=gl.getExtension("OES_element_index_uint"),TRIANGLES=gl.TRIANGLES,material0Data=new vertexBuffer(gl.POINTS),material1Data=new vertexBuffer(gl.LINES),materialData=new vertexBuffer,colorData=new vertexBuffer,transparentData=new vertexBuffer,triangleData=new vertexBuffer}function getShader(t,e,i,a=[]){let r=`#version 100\n#ifdef GL_FRAGMENT_PRECISION_HIGH\n precision highp float;\n#else\n precision mediump float;\n#endif\n #define nlights ${0==wireframe?Lights.length:0}\n\n const int Nlights=${Math.max(Lights.length,1)};\n\n #define Nmaterials ${Nmaterials}\n`;orthographic&&(r+="#define ORTHOGRAPHIC\n"),a.forEach(t=>r+="#define "+t+"\n");let n=t.createShader(i);return t.shaderSource(n,r+e),t.compileShader(n),t.getShaderParameter(n,t.COMPILE_STATUS)?n:(alert(t.getShaderInfoLog(n)),null)}function drawBuffer(t,e,i=t.indices){if(0==t.indices.length)return;let a=e!=pixelShader;setUniforms(t,e),gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer),gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(t.vertices),gl.STATIC_DRAW),gl.vertexAttribPointer(positionAttribute,3,gl.FLOAT,!1,a?24:16,0),a&&Lights.length>0?gl.vertexAttribPointer(normalAttribute,3,gl.FLOAT,!1,24,12):pixel&&gl.vertexAttribPointer(widthAttribute,1,gl.FLOAT,!1,16,12),gl.bindBuffer(gl.ARRAY_BUFFER,materialBuffer),gl.bufferData(gl.ARRAY_BUFFER,new Int16Array(t.materialIndices),gl.STATIC_DRAW),gl.vertexAttribPointer(materialAttribute,1,gl.SHORT,!1,2,0),e!=colorShader&&e!=transparentShader||(gl.bindBuffer(gl.ARRAY_BUFFER,colorBuffer),gl.bufferData(gl.ARRAY_BUFFER,new Uint8Array(t.colors),gl.STATIC_DRAW),gl.vertexAttribPointer(colorAttribute,4,gl.UNSIGNED_BYTE,!0,0,0)),gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer),gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,indexExt?new Uint32Array(i):new Uint16Array(i),gl.STATIC_DRAW),gl.drawElements(a?wireframe?gl.LINES:t.type:gl.POINTS,i.length,indexExt?gl.UNSIGNED_INT:gl.UNSIGNED_SHORT,0)}class vertexBuffer{constructor(t){this.type=t||TRIANGLES,this.clear()}clear(){this.vertices=[],this.materialIndices=[],this.colors=[],this.indices=[],this.nvertices=0,this.materials=[],this.materialTable=[]}vertex(t,e){return this.vertices.push(t[0]),this.vertices.push(t[1]),this.vertices.push(t[2]),this.vertices.push(e[0]),this.vertices.push(e[1]),this.vertices.push(e[2]),this.materialIndices.push(materialIndex),this.nvertices++}Vertex(t,e,i=[0,0,0,0]){return this.vertices.push(t[0]),this.vertices.push(t[1]),this.vertices.push(t[2]),this.vertices.push(e[0]),this.vertices.push(e[1]),this.vertices.push(e[2]),this.materialIndices.push(materialIndex),this.colors.push(i[0]),this.colors.push(i[1]),this.colors.push(i[2]),this.colors.push(i[3]),this.nvertices++}vertex0(t,e){return this.vertices.push(t[0]),this.vertices.push(t[1]),this.vertices.push(t[2]),this.vertices.push(e),this.materialIndices.push(materialIndex),this.nvertices++}iVertex(t,e,i,a=[0,0,0,0]){let r=6*t;this.vertices[r]=e[0],this.vertices[r+1]=e[1],this.vertices[r+2]=e[2],this.vertices[r+3]=i[0],this.vertices[r+4]=i[1],this.vertices[r+5]=i[2],this.materialIndices[t]=materialIndex;let n=4*t;this.colors[n]=a[0],this.colors[n+1]=a[1],this.colors[n+2]=a[2],this.colors[n+3]=a[3],this.indices.push(t)}append(t){append(this.vertices,t.vertices),append(this.materialIndices,t.materialIndices),append(this.colors,t.colors),appendOffset(this.indices,t.indices,this.nvertices),this.nvertices+=t.nvertices}}function append(t,e){let i=t.length,a=e.length;t.length+=a;for(let r=0;r<a;++r)t[i+r]=e[r]}function appendOffset(t,e,i){let a=t.length,r=e.length;t.length+=e.length;for(let n=0;n<r;++n)t[a+n]=e[n]+i}class Geometry{constructor(){this.data=new vertexBuffer,this.Onscreen=!1,this.m=[]}offscreen(t){let e=projViewMat,i=t[0],a=i[0],r=i[1],n=i[2],s=1/(e[3]*a+e[7]*r+e[11]*n+e[15]);this.x=this.X=(e[0]*a+e[4]*r+e[8]*n+e[12])*s,this.y=this.Y=(e[1]*a+e[5]*r+e[9]*n+e[13])*s;for(let i=1,a=t.length;i<a;++i){let a=t[i],r=a[0],n=a[1],s=a[2],o=1/(e[3]*r+e[7]*n+e[11]*s+e[15]),h=(e[0]*r+e[4]*n+e[8]*s+e[12])*o,l=(e[1]*r+e[5]*n+e[9]*s+e[13])*o;h<this.x?this.x=h:h>this.X&&(this.X=h),l<this.y?this.y=l:l>this.Y&&(this.Y=l)}return(this.X<-1.01||this.x>1.01||this.Y<-1.01||this.y>1.01)&&(this.Onscreen=!1,!0)}T(t){let e=this.c[0],i=this.c[1],a=this.c[2],r=t[0]-e,n=t[1]-i,s=t[2]-a;return[r*normMat[0]+n*normMat[3]+s*normMat[6]+e,r*normMat[1]+n*normMat[4]+s*normMat[7]+i,r*normMat[2]+n*normMat[5]+s*normMat[8]+a]}Tcorners(t,e){return[this.T(t),this.T([t[0],t[1],e[2]]),this.T([t[0],e[1],t[2]]),this.T([t[0],e[1],e[2]]),this.T([e[0],t[1],t[2]]),this.T([e[0],t[1],e[2]]),this.T([e[0],e[1],t[2]]),this.T(e)]}setMaterial(t,e){null==t.materialTable[this.MaterialIndex]&&(t.materials.length>=Nmaterials&&e(),t.materialTable[this.MaterialIndex]=t.materials.length,t.materials.push(Materials[this.MaterialIndex])),materialIndex=t.materialTable[this.MaterialIndex]}render(){let t;if(this.setMaterialIndex(),0==this.CenterIndex?t=corners(this.Min,this.Max):(this.c=Centers[this.CenterIndex-1],t=this.Tcorners(this.Min,this.Max)),this.offscreen(t))return void this.data.clear();let e,i=this.controlpoints;if(0==this.CenterIndex){if(!remesh&&this.Onscreen)return void this.append();e=i}else{let t=i.length;e=Array(t);for(let a=0;a<t;++a)e[a]=this.T(i[a])}let a=orthographic?1:this.Min[2]/B[2],r=pixel*Math.hypot(a*(viewParam.xmax-viewParam.xmin),a*(viewParam.ymax-viewParam.ymin))/size2;this.res2=r*r,this.Epsilon=FillFactor*r,this.data.clear(),this.Onscreen=!0,this.process(e)}}class BezierPatch extends Geometry{constructor(t,e,i,a,r,n){super(),this.controlpoints=t,this.Min=a,this.Max=r,this.color=n,this.CenterIndex=e;let s=t.length;if(n){let t=n[0][3]+n[1][3]+n[2][3];this.transparent=16==s||4==s?t+n[3][3]<1020:t<765}else this.transparent=Materials[i].diffuse[3]<1;this.MaterialIndex=i,this.vertex=this.transparent?this.data.Vertex.bind(this.data):this.data.vertex.bind(this.data),this.L2norm(this.controlpoints)}setMaterialIndex(){this.transparent?this.setMaterial(transparentData,drawTransparent):this.color?this.setMaterial(colorData,drawColor):this.setMaterial(materialData,drawMaterial)}L2norm(t){let e=t[0];this.epsilon=0;let i=t.length;for(let a=1;a<i;++a)this.epsilon=Math.max(this.epsilon,abs2([t[a][0]-e[0],t[a][1]-e[1],t[a][2]-e[2]]));this.epsilon*=Number.EPSILON}processTriangle(t){let e=t[0],i=t[1],a=t[2],r=unit(cross([i[0]-e[0],i[1]-e[1],i[2]-e[2]],[a[0]-e[0],a[1]-e[1],a[2]-e[2]]));if(!this.offscreen([e,i,a])){let t,n,s;this.color?(t=this.data.Vertex(e,r,this.color[0]),n=this.data.Vertex(i,r,this.color[1]),s=this.data.Vertex(a,r,this.color[2])):(t=this.vertex(e,r),n=this.vertex(i,r),s=this.vertex(a,r)),0==wireframe?(this.data.indices.push(t),this.data.indices.push(n),this.data.indices.push(s)):(this.data.indices.push(t),this.data.indices.push(n),this.data.indices.push(n),this.data.indices.push(s),this.data.indices.push(s),this.data.indices.push(t)),this.append()}}processQuad(t){let e=t[0],i=t[1],a=t[2],r=t[3],n=cross([i[0]-e[0],i[1]-e[1],i[2]-e[2]],[a[0]-i[0],a[1]-i[1],a[2]-i[2]]),s=cross([a[0]-r[0],a[1]-r[1],a[2]-r[2]],[r[0]-e[0],r[1]-e[1],r[2]-e[2]]),o=unit([n[0]+s[0],n[1]+s[1],n[2]+s[2]]);if(!this.offscreen([e,i,a,r])){let t,n,s,h;this.color?(t=this.data.Vertex(e,o,this.color[0]),n=this.data.Vertex(i,o,this.color[1]),s=this.data.Vertex(a,o,this.color[2]),h=this.data.Vertex(r,o,this.color[3])):(t=this.vertex(e,o),n=this.vertex(i,o),s=this.vertex(a,o),h=this.vertex(r,o)),0==wireframe?(this.data.indices.push(t),this.data.indices.push(n),this.data.indices.push(s),this.data.indices.push(t),this.data.indices.push(s),this.data.indices.push(h)):(this.data.indices.push(t),this.data.indices.push(n),this.data.indices.push(n),this.data.indices.push(s),this.data.indices.push(s),this.data.indices.push(h),this.data.indices.push(h),this.data.indices.push(t)),this.append()}}curve(t,e,i,a,r){new BezierCurve([t[e],t[i],t[a],t[r]],0,materialIndex,this.Min,this.Max).render()}process(t){if(this.transparent&&1!=wireframe&&(materialIndex=this.color?-1-materialIndex:1+materialIndex),10==t.length)return this.process3(t);if(3==t.length)return this.processTriangle(t);if(4==t.length)return this.processQuad(t);if(1==wireframe)return this.curve(t,0,4,8,12),this.curve(t,12,13,14,15),this.curve(t,15,11,7,3),void this.curve(t,3,2,1,0);let e=t[0],i=t[3],a=t[12],r=t[15],n=this.normal(i,t[2],t[1],e,t[4],t[8],a);abs2(n)<this.epsilon&&abs2(n=this.normal(i,t[2],t[1],e,t[13],t[14],r))<this.epsilon&&(n=this.normal(r,t[11],t[7],i,t[4],t[8],a));let s=this.normal(e,t[4],t[8],a,t[13],t[14],r);abs2(s)<this.epsilon&&abs2(s=this.normal(e,t[4],t[8],a,t[11],t[7],i))<this.epsilon&&(s=this.normal(i,t[2],t[1],e,t[13],t[14],r));let o=this.normal(a,t[13],t[14],r,t[11],t[7],i);abs2(o)<this.epsilon&&abs2(o=this.normal(a,t[13],t[14],r,t[2],t[1],e))<this.epsilon&&(o=this.normal(e,t[4],t[8],a,t[11],t[7],i));let h=this.normal(r,t[11],t[7],i,t[2],t[1],e);if(abs2(h)<this.epsilon&&abs2(h=this.normal(r,t[11],t[7],i,t[4],t[8],a))<this.epsilon&&(h=this.normal(a,t[13],t[14],r,t[2],t[1],e)),this.color){let l=this.color[0],d=this.color[1],c=this.color[2],m=this.color[3],f=this.data.Vertex(e,n,l),u=this.data.Vertex(a,s,d),p=this.data.Vertex(r,o,c),v=this.data.Vertex(i,h,m);this.Render(t,f,u,p,v,e,a,r,i,!1,!1,!1,!1,l,d,c,m)}else{let l=this.vertex(e,n),d=this.vertex(a,s),c=this.vertex(r,o),m=this.vertex(i,h);this.Render(t,l,d,c,m,e,a,r,i,!1,!1,!1,!1)}this.data.indices.length>0&&this.append()}append(){this.transparent?transparentData.append(this.data):this.color?colorData.append(this.data):materialData.append(this.data)}Render(t,e,i,a,r,n,s,o,h,l,d,c,m,f,u,p,v){let g=this.Distance(t);if(g[0]<this.res2&&g[1]<this.res2)this.offscreen([n,s,o])||(0==wireframe?(this.data.indices.push(e),this.data.indices.push(i),this.data.indices.push(a)):(this.data.indices.push(e),this.data.indices.push(i),this.data.indices.push(i),this.data.indices.push(a))),this.offscreen([n,o,h])||(0==wireframe?(this.data.indices.push(e),this.data.indices.push(a),this.data.indices.push(r)):(this.data.indices.push(a),this.data.indices.push(r),this.data.indices.push(r),this.data.indices.push(e)));else{if(this.offscreen(t))return;let w=t[0],x=t[3],M=t[12],b=t[15];if(g[0]<this.res2){let g=new Split3(w,t[1],t[2],x),A=new Split3(t[4],t[5],t[6],t[7]),S=new Split3(t[8],t[9],t[10],t[11]),P=new Split3(M,t[13],t[14],b),R=[w,g.m0,g.m3,g.m5,t[4],A.m0,A.m3,A.m5,t[8],S.m0,S.m3,S.m5,M,P.m0,P.m3,P.m5],T=[g.m5,g.m4,g.m2,x,A.m5,A.m4,A.m2,t[7],S.m5,S.m4,S.m2,t[11],P.m5,P.m4,P.m2,b],y=this.normal(R[12],R[13],R[14],R[15],R[11],R[7],R[3]);abs2(y)<=this.epsilon&&abs2(y=this.normal(R[12],R[13],R[14],R[15],R[2],R[1],R[0]))<=this.epsilon&&(y=this.normal(R[0],R[4],R[8],R[12],R[11],R[7],R[3]));let D=this.normal(T[3],T[2],T[1],T[0],T[4],T[8],T[12]);abs2(D)<=this.epsilon&&abs2(D=this.normal(T[3],T[2],T[1],T[0],T[13],T[14],T[15]))<=this.epsilon&&(D=this.normal(T[15],T[11],T[7],T[3],T[4],T[8],T[12]));let I=this.Epsilon,z=[.5*(s[0]+o[0]),.5*(s[1]+o[1]),.5*(s[2]+o[2])];if(!d)if(d=Straightness(M,t[13],t[14],b)<this.res2){let t=unit(this.differential(T[12],T[8],T[4],T[0]));z=[z[0]-I*t[0],z[1]-I*t[1],z[2]-I*t[2]]}else z=R[15];let E=[.5*(h[0]+n[0]),.5*(h[1]+n[1]),.5*(h[2]+n[2])];if(!m)if(m=Straightness(w,t[1],t[2],x)<this.res2){let t=unit(this.differential(R[3],R[7],R[11],R[15]));E=[E[0]-I*t[0],E[1]-I*t[1],E[2]-I*t[2]]}else E=T[0];if(f){let t=Array(4),g=Array(4);for(let e=0;e<4;++e)t[e]=.5*(u[e]+p[e]),g[e]=.5*(v[e]+f[e]);let w=this.data.Vertex(z,y,t),x=this.data.Vertex(E,D,g);this.Render(R,e,i,w,x,n,s,z,E,l,d,!1,m,f,u,t,g),this.Render(T,x,w,a,r,E,z,o,h,!1,d,c,m,g,t,p,v)}else{let t=this.vertex(z,y),f=this.vertex(E,D);this.Render(R,e,i,t,f,n,s,z,E,l,d,!1,m),this.Render(T,f,t,a,r,E,z,o,h,!1,d,c,m)}return}if(g[1]<this.res2){let g=new Split3(w,t[4],t[8],M),A=new Split3(t[1],t[5],t[9],t[13]),S=new Split3(t[2],t[6],t[10],t[14]),P=new Split3(x,t[7],t[11],b),R=[w,t[1],t[2],x,g.m0,A.m0,S.m0,P.m0,g.m3,A.m3,S.m3,P.m3,g.m5,A.m5,S.m5,P.m5],T=[g.m5,A.m5,S.m5,P.m5,g.m4,A.m4,S.m4,P.m4,g.m2,A.m2,S.m2,P.m2,M,t[13],t[14],b],y=this.normal(R[0],R[4],R[8],R[12],R[13],R[14],R[15]);abs2(y)<=this.epsilon&&abs2(y=this.normal(R[0],R[4],R[8],R[12],R[11],R[7],R[3]))<=this.epsilon&&(y=this.normal(R[3],R[2],R[1],R[0],R[13],R[14],R[15]));let D=this.normal(T[15],T[11],T[7],T[3],T[2],T[1],T[0]);abs2(D)<=this.epsilon&&abs2(D=this.normal(T[15],T[11],T[7],T[3],T[4],T[8],T[12]))<=this.epsilon&&(D=this.normal(T[12],T[13],T[14],T[15],T[2],T[1],T[0]));let I=this.Epsilon,z=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])];if(!l)if(l=Straightness(w,t[4],t[8],M)<this.res2){let t=unit(this.differential(T[0],T[1],T[2],T[3]));z=[z[0]-I*t[0],z[1]-I*t[1],z[2]-I*t[2]]}else z=R[12];let E=[.5*(o[0]+h[0]),.5*(o[1]+h[1]),.5*(o[2]+h[2])];if(!c)if(c=Straightness(b,t[11],t[7],x)<this.res2){let t=unit(this.differential(R[15],R[14],R[13],R[12]));E=[E[0]-I*t[0],E[1]-I*t[1],E[2]-I*t[2]]}else E=T[3];if(f){let t=Array(4),g=Array(4);for(let e=0;e<4;++e)t[e]=.5*(f[e]+u[e]),g[e]=.5*(p[e]+v[e]);let w=this.data.Vertex(z,y,t),x=this.data.Vertex(E,D,g);this.Render(R,e,w,x,r,n,z,E,h,l,!1,c,m,f,t,g,v),this.Render(T,w,i,a,x,z,s,o,E,l,d,c,!1,t,u,p,g)}else{let t=this.vertex(z,y),f=this.vertex(E,D);this.Render(R,e,t,f,r,n,z,E,h,l,!1,c,m),this.Render(T,t,i,a,f,z,s,o,E,l,d,c,!1)}return}let A=new Split3(w,t[1],t[2],x),S=new Split3(t[4],t[5],t[6],t[7]),P=new Split3(t[8],t[9],t[10],t[11]),R=new Split3(M,t[13],t[14],b),T=new Split3(w,t[4],t[8],M),y=new Split3(A.m0,S.m0,P.m0,R.m0),D=new Split3(A.m3,S.m3,P.m3,R.m3),I=new Split3(A.m5,S.m5,P.m5,R.m5),z=new Split3(A.m4,S.m4,P.m4,R.m4),E=new Split3(A.m2,S.m2,P.m2,R.m2),O=new Split3(x,t[7],t[11],b),L=[w,A.m0,A.m3,A.m5,T.m0,y.m0,D.m0,I.m0,T.m3,y.m3,D.m3,I.m3,T.m5,y.m5,D.m5,I.m5],N=[T.m5,y.m5,D.m5,I.m5,T.m4,y.m4,D.m4,I.m4,T.m2,y.m2,D.m2,I.m2,M,R.m0,R.m3,R.m5],_=[I.m5,z.m5,E.m5,O.m5,I.m4,z.m4,E.m4,O.m4,I.m2,z.m2,E.m2,O.m2,R.m5,R.m4,R.m2,b],B=[A.m5,A.m4,A.m2,x,I.m0,z.m0,E.m0,O.m0,I.m3,z.m3,E.m3,O.m3,I.m5,z.m5,E.m5,O.m5],V=L[15],C=this.normal(L[0],L[4],L[8],L[12],L[13],L[14],L[15]);abs2(C)<this.epsilon&&abs2(C=this.normal(L[0],L[4],L[8],L[12],L[11],L[7],L[3]))<this.epsilon&&(C=this.normal(L[3],L[2],L[1],L[0],L[13],L[14],L[15]));let F=this.normal(N[12],N[13],N[14],N[15],N[11],N[7],N[3]);abs2(F)<this.epsilon&&abs2(F=this.normal(N[12],N[13],N[14],N[15],N[2],N[1],N[0]))<this.epsilon&&(F=this.normal(N[0],N[4],N[8],N[12],N[11],N[7],N[3]));let H=this.normal(_[15],_[11],_[7],_[3],_[2],_[1],_[0]);abs2(H)<this.epsilon&&abs2(H=this.normal(_[15],_[11],_[7],_[3],_[4],_[8],_[12]))<this.epsilon&&(H=this.normal(_[12],_[13],_[14],_[15],_[2],_[1],_[0]));let G=this.normal(B[3],B[2],B[1],B[0],B[4],B[8],B[12]);abs2(G)<this.epsilon&&abs2(G=this.normal(B[3],B[2],B[1],B[0],B[13],B[14],B[15]))<this.epsilon&&(G=this.normal(B[15],B[11],B[7],B[3],B[4],B[8],B[12]));let U=this.normal(_[3],_[2],_[1],V,_[4],_[8],_[12]),W=this.Epsilon,Y=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])];if(!l)if(l=Straightness(w,t[4],t[8],M)<this.res2){let t=unit(this.differential(N[0],N[1],N[2],N[3]));Y=[Y[0]-W*t[0],Y[1]-W*t[1],Y[2]-W*t[2]]}else Y=L[12];let j=[.5*(s[0]+o[0]),.5*(s[1]+o[1]),.5*(s[2]+o[2])];if(!d)if(d=Straightness(M,t[13],t[14],b)<this.res2){let t=unit(this.differential(_[12],_[8],_[4],_[0]));j=[j[0]-W*t[0],j[1]-W*t[1],j[2]-W*t[2]]}else j=N[15];let k=[.5*(o[0]+h[0]),.5*(o[1]+h[1]),.5*(o[2]+h[2])];if(!c)if(c=Straightness(b,t[11],t[7],x)<this.res2){let t=unit(this.differential(B[15],B[14],B[13],B[12]));k=[k[0]-W*t[0],k[1]-W*t[1],k[2]-W*t[2]]}else k=_[3];let Z=[.5*(h[0]+n[0]),.5*(h[1]+n[1]),.5*(h[2]+n[2])];if(!m)if(m=Straightness(w,t[1],t[2],x)<this.res2){let t=unit(this.differential(L[3],L[7],L[11],L[15]));Z=[Z[0]-W*t[0],Z[1]-W*t[1],Z[2]-W*t[2]]}else Z=B[0];if(f){let t=Array(4),g=Array(4),w=Array(4),x=Array(4),M=Array(4);for(let e=0;e<4;++e)t[e]=.5*(f[e]+u[e]),g[e]=.5*(u[e]+p[e]),w[e]=.5*(p[e]+v[e]),x[e]=.5*(v[e]+f[e]),M[e]=.5*(t[e]+w[e]);let b=this.data.Vertex(Y,C,t),A=this.data.Vertex(j,F,g),S=this.data.Vertex(k,H,w),P=this.data.Vertex(Z,G,x),R=this.data.Vertex(V,U,M);this.Render(L,e,b,R,P,n,Y,V,Z,l,!1,!1,m,f,t,M,x),this.Render(N,b,i,A,R,Y,s,j,V,l,d,!1,!1,t,u,g,M),this.Render(_,R,A,a,S,V,j,o,k,!1,d,c,!1,M,g,p,w),this.Render(B,P,R,S,r,Z,V,k,h,!1,!1,c,m,x,M,w,v)}else{let t=this.vertex(Y,C),f=this.vertex(j,F),u=this.vertex(k,H),p=this.vertex(Z,G),v=this.vertex(V,U);this.Render(L,e,t,v,p,n,Y,V,Z,l,!1,!1,m),this.Render(N,t,i,f,v,Y,s,j,V,l,d,!1,!1),this.Render(_,v,f,a,u,V,j,o,k,!1,d,c,!1),this.Render(B,p,v,u,r,Z,V,k,h,!1,!1,c,m)}}}process3(t){if(1==wireframe)return this.curve(t,0,1,3,6),this.curve(t,6,7,8,9),void this.curve(t,9,5,2,0);let e=t[0],i=t[6],a=t[9],r=this.normal(a,t[5],t[2],e,t[1],t[3],i),n=this.normal(e,t[1],t[3],i,t[7],t[8],a),s=this.normal(i,t[7],t[8],a,t[5],t[2],e);if(this.color){let o=this.color[0],h=this.color[1],l=this.color[2],d=this.data.Vertex(e,r,o),c=this.data.Vertex(i,n,h),m=this.data.Vertex(a,s,l);this.Render3(t,d,c,m,e,i,a,!1,!1,!1,o,h,l)}else{let o=this.vertex(e,r),h=this.vertex(i,n),l=this.vertex(a,s);this.Render3(t,o,h,l,e,i,a,!1,!1,!1)}this.data.indices.length>0&&this.append()}Render3(t,e,i,a,r,n,s,o,h,l,d,c,m){if(this.Distance3(t)<this.res2)this.offscreen([r,n,s])||(0==wireframe?(this.data.indices.push(e),this.data.indices.push(i),this.data.indices.push(a)):(this.data.indices.push(e),this.data.indices.push(i),this.data.indices.push(i),this.data.indices.push(a),this.data.indices.push(a),this.data.indices.push(e)));else{if(this.offscreen(t))return;let f=t[0],u=t[1],p=t[2],v=t[3],g=t[4],w=t[5],x=t[6],M=t[7],b=t[8],A=t[9],S=[.5*(A[0]+w[0]),.5*(A[1]+w[1]),.5*(A[2]+w[2])],P=[.5*(A[0]+b[0]),.5*(A[1]+b[1]),.5*(A[2]+b[2])],R=[.5*(w[0]+p[0]),.5*(w[1]+p[1]),.5*(w[2]+p[2])],T=[.5*(b[0]+g[0]),.5*(b[1]+g[1]),.5*(b[2]+g[2])],y=[.5*(b[0]+M[0]),.5*(b[1]+M[1]),.5*(b[2]+M[2])],D=[.5*(p[0]+g[0]),.5*(p[1]+g[1]),.5*(p[2]+g[2])],I=[.5*(p[0]+f[0]),.5*(p[1]+f[1]),.5*(p[2]+f[2])],z=[.5*(g[0]+v[0]),.5*(g[1]+v[1]),.5*(g[2]+v[2])],E=[.5*(M[0]+x[0]),.5*(M[1]+x[1]),.5*(M[2]+x[2])],O=[.5*(f[0]+u[0]),.5*(f[1]+u[1]),.5*(f[2]+u[2])],L=[.5*(u[0]+v[0]),.5*(u[1]+v[1]),.5*(u[2]+v[2])],N=[.5*(v[0]+x[0]),.5*(v[1]+x[1]),.5*(v[2]+x[2])],_=[.5*(S[0]+R[0]),.5*(S[1]+R[1]),.5*(S[2]+R[2])],B=[.5*(P[0]+y[0]),.5*(P[1]+y[1]),.5*(P[2]+y[2])],V=[.5*(R[0]+I[0]),.5*(R[1]+I[1]),.5*(R[2]+I[2])],C=[.5*T[0]+.25*(g[0]+u[0]),.5*T[1]+.25*(g[1]+u[1]),.5*T[2]+.25*(g[2]+u[2])],F=[.5*(y[0]+E[0]),.5*(y[1]+E[1]),.5*(y[2]+E[2])],H=[.5*D[0]+.25*(g[0]+M[0]),.5*D[1]+.25*(g[1]+M[1]),.5*D[2]+.25*(g[2]+M[2])],G=[.25*(w[0]+g[0])+.5*z[0],.25*(w[1]+g[1])+.5*z[1],.25*(w[2]+g[2])+.5*z[2]],U=[.5*(O[0]+L[0]),.5*(O[1]+L[1]),.5*(O[2]+L[2])],W=[.5*(L[0]+N[0]),.5*(L[1]+N[1]),.5*(L[2]+N[2])],Y=[.5*(H[0]+U[0]),.5*(H[1]+U[1]),.5*(H[2]+U[2])],j=[.5*(H[0]+W[0]),.5*(H[1]+W[1]),.5*(H[2]+W[2])],k=[.5*(U[0]+W[0]),.5*(U[1]+W[1]),.5*(U[2]+W[2])],Z=[.5*(G[0]+F[0]),.5*(G[1]+F[1]),.5*(G[2]+F[2])],X=[.5*(B[0]+G[0]),.5*(B[1]+G[1]),.5*(B[2]+G[2])],q=[.5*(B[0]+F[0]),.5*(B[1]+F[1]),.5*(B[2]+F[2])],K=[.5*(_[0]+C[0]),.5*(_[1]+C[1]),.5*(_[2]+C[2])],$=[.5*(V[0]+C[0]),.5*(V[1]+C[1]),.5*(V[2]+C[2])],Q=[.5*(_[0]+V[0]),.5*(_[1]+V[1]),.5*(_[2]+V[2])],J=[f,O,I,U,[.5*(D[0]+O[0]),.5*(D[1]+O[1]),.5*(D[2]+O[2])],V,k,Y,$,Q],tt=[k,W,j,N,[.5*(z[0]+E[0]),.5*(z[1]+E[1]),.5*(z[2]+E[2])],Z,x,E,F,q],et=[Q,K,_,X,[.5*(S[0]+T[0]),.5*(S[1]+T[1]),.5*(S[2]+T[2])],S,q,B,P,A],it=[q,X,Z,K,[.25*(R[0]+y[0]+L[0]+g[0]),.25*(R[1]+y[1]+L[1]+g[1]),.25*(R[2]+y[2]+L[2]+g[2])],j,Q,$,Y,k],at=this.normal(k,j,Z,q,X,K,Q),rt=this.normal(q,X,K,Q,$,Y,k),nt=this.normal(Q,$,Y,k,j,Z,q),st=this.Epsilon,ot=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])];if(!o)if(o=Straightness(x,M,b,A)<this.res2){let t=unit(this.sumdifferential(it[0],it[2],it[5],it[9],it[1],it[3],it[6]));ot=[ot[0]-st*t[0],ot[1]-st*t[1],ot[2]-st*t[2]]}else ot=q;let ht=[.5*(s[0]+r[0]),.5*(s[1]+r[1]),.5*(s[2]+r[2])];if(!h)if(h=Straightness(f,p,w,A)<this.res2){let t=unit(this.sumdifferential(it[6],it[3],it[1],it[0],it[7],it[8],it[9]));ht=[ht[0]-st*t[0],ht[1]-st*t[1],ht[2]-st*t[2]]}else ht=Q;let lt=[.5*(r[0]+n[0]),.5*(r[1]+n[1]),.5*(r[2]+n[2])];if(!l)if(l=Straightness(f,u,v,x)<this.res2){let t=unit(this.sumdifferential(it[9],it[8],it[7],it[6],it[5],it[2],it[0]));lt=[lt[0]-st*t[0],lt[1]-st*t[1],lt[2]-st*t[2]]}else lt=k;if(d){let t=Array(4),f=Array(4),u=Array(4);for(let e=0;e<4;++e)t[e]=.5*(c[e]+m[e]),f[e]=.5*(m[e]+d[e]),u[e]=.5*(d[e]+c[e]);let p=this.data.Vertex(ot,at,t),v=this.data.Vertex(ht,rt,f),g=this.data.Vertex(lt,nt,u);this.Render3(J,e,g,v,r,lt,ht,!1,h,l,d,u,f),this.Render3(tt,g,i,p,lt,n,ot,o,!1,l,u,c,t),this.Render3(et,v,p,a,ht,ot,s,o,h,!1,f,t,m),this.Render3(it,p,v,g,ot,ht,lt,!1,!1,!1,t,f,u)}else{let t=this.vertex(ot,at),d=this.vertex(ht,rt),c=this.vertex(lt,nt);this.Render3(J,e,c,d,r,lt,ht,!1,h,l),this.Render3(tt,c,i,t,lt,n,ot,o,!1,l),this.Render3(et,d,t,a,ht,ot,s,o,h,!1),this.Render3(it,t,d,c,ot,ht,lt,!1,!1,!1)}}}Distance(t){let e=t[0],i=t[3],a=t[12],r=t[15],n=Flatness(e,a,i,r);n=Math.max(Straightness(e,t[4],t[8],a)),n=Math.max(n,Straightness(t[1],t[5],t[9],t[13])),n=Math.max(n,Straightness(i,t[7],t[11],r)),n=Math.max(n,Straightness(t[2],t[6],t[10],t[14]));let s=Flatness(e,i,a,r);return s=Math.max(s,Straightness(e,t[1],t[2],i)),s=Math.max(s,Straightness(t[4],t[5],t[6],t[7])),s=Math.max(s,Straightness(t[8],t[9],t[10],t[11])),[n,s=Math.max(s,Straightness(a,t[13],t[14],r))]}Distance3(t){let e=t[0],i=t[4],a=t[6],r=t[9],n=abs2([(e[0]+a[0]+r[0])*third-i[0],(e[1]+a[1]+r[1])*third-i[1],(e[2]+a[2]+r[2])*third-i[2]]);return n=Math.max(n,Straightness(e,t[1],t[3],a)),n=Math.max(n,Straightness(e,t[2],t[5],r)),Math.max(n,Straightness(a,t[7],t[8],r))}differential(t,e,i,a){let r=[3*(e[0]-t[0]),3*(e[1]-t[1]),3*(e[2]-t[2])];return abs2(r)>this.epsilon?r:abs2(r=bezierPP(t,e,i))>this.epsilon?r:bezierPPP(t,e,i,a)}sumdifferential(t,e,i,a,r,n,s){let o=this.differential(t,e,i,a),h=this.differential(t,r,n,s);return[o[0]+h[0],o[1]+h[1],o[2]+h[2]]}normal(t,e,i,a,r,n,s){let o=3*(r[0]-a[0]),h=3*(r[1]-a[1]),l=3*(r[2]-a[2]),d=3*(i[0]-a[0]),c=3*(i[1]-a[1]),m=3*(i[2]-a[2]),f=[h*m-l*c,l*d-o*m,o*c-h*d];if(abs2(f)>this.epsilon)return f;let u=[d,c,m],p=[o,h,l],v=bezierPP(a,i,e),g=bezierPP(a,r,n),w=cross(g,u),x=cross(p,v);if(abs2(f=[w[0]+x[0],w[1]+x[1],w[2]+x[2]])>this.epsilon)return f;let M=bezierPPP(a,i,e,t),b=bezierPPP(a,r,n,s);w=cross(p,M),x=cross(b,u);let A=cross(g,v);return abs2(f=[w[0]+x[0]+A[0],w[1]+x[1]+A[1],w[2]+x[2]+A[2]])>this.epsilon?f:(w=cross(b,v),x=cross(g,M),abs2(f=[w[0]+x[0],w[1]+x[1],w[2]+x[2]])>this.epsilon?f:cross(b,M))}}class BezierCurve extends Geometry{constructor(t,e,i,a,r){super(),this.controlpoints=t,this.Min=a,this.Max=r,this.CenterIndex=e,this.MaterialIndex=i}setMaterialIndex(){this.setMaterial(material1Data,drawMaterial1)}processLine(t){let e=t[0],i=t[1];if(!this.offscreen([e,i])){let t=[0,0,1];this.data.indices.push(this.data.vertex(e,t)),this.data.indices.push(this.data.vertex(i,t)),this.append()}}process(t){if(2==t.length)return this.processLine(t);let e=t[0],i=t[1],a=t[2],r=t[3],n=this.normal(bezierP(e,i),bezierPP(e,i,a)),s=this.normal(bezierP(a,r),bezierPP(r,a,i)),o=this.data.vertex(e,n),h=this.data.vertex(r,s);this.Render(t,o,h),this.data.indices.length>0&&this.append()}append(){material1Data.append(this.data)}Render(t,e,i){let a=t[0],r=t[1],n=t[2],s=t[3];if(Straightness(a,r,n,s)<this.res2)this.offscreen([a,s])||(this.data.indices.push(e),this.data.indices.push(i));else{if(this.offscreen(t))return;let o=[.5*(a[0]+r[0]),.5*(a[1]+r[1]),.5*(a[2]+r[2])],h=[.5*(r[0]+n[0]),.5*(r[1]+n[1]),.5*(r[2]+n[2])],l=[.5*(n[0]+s[0]),.5*(n[1]+s[1]),.5*(n[2]+s[2])],d=[.5*(o[0]+h[0]),.5*(o[1]+h[1]),.5*(o[2]+h[2])],c=[.5*(h[0]+l[0]),.5*(h[1]+l[1]),.5*(h[2]+l[2])],m=[.5*(d[0]+c[0]),.5*(d[1]+c[1]),.5*(d[2]+c[2])],f=[a,o,d,m],u=[m,c,l,s],p=this.normal(bezierPh(a,r,n,s),bezierPPh(a,r,n,s)),v=this.data.vertex(m,p);this.Render(f,e,v),this.Render(u,v,i)}}normal(t,e){let i=dot(t,t),a=dot(t,e);return[i*e[0]-a*t[0],i*e[1]-a*t[1],i*e[2]-a*t[2]]}}class Pixel extends Geometry{constructor(t,e,i,a,r){super(),this.controlpoint=t,this.width=e,this.CenterIndex=0,this.MaterialIndex=i,this.Min=a,this.Max=r}setMaterialIndex(){this.setMaterial(material0Data,drawMaterial0)}process(t){this.data.indices.push(this.data.vertex0(this.controlpoint,this.width)),this.append()}append(){material0Data.append(this.data)}}class Triangles extends Geometry{constructor(t,e,i){super(),this.CenterIndex=0,this.MaterialIndex=t,this.Min=e,this.Max=i,this.Positions=Positions,this.Normals=Normals,this.Colors=Colors,this.Indices=Indices,Positions=[],Normals=[],Colors=[],Indices=[],this.transparent=Materials[t].diffuse[3]<1}setMaterialIndex(){this.transparent?this.setMaterial(transparentData,drawTransparent):this.setMaterial(triangleData,drawTriangle)}process(t){materialIndex=this.Colors.length>0?-1-materialIndex:1+materialIndex;for(let t=0,e=this.Indices.length;t<e;++t){let e=this.Indices[t],i=e[0],a=this.Positions[i[0]],r=this.Positions[i[1]],n=this.Positions[i[2]];if(!this.offscreen([a,r,n])){let t=e.length>1?e[1]:i;if(t&&0!=t.length||(t=i),this.Colors.length>0){let s=e.length>2?e[2]:i;s&&0!=s.length||(s=i);let o=this.Colors[s[0]],h=this.Colors[s[1]],l=this.Colors[s[2]];this.transparent|=o[3]+h[3]+l[3]<765,0==wireframe?(this.data.iVertex(i[0],a,this.Normals[t[0]],o),this.data.iVertex(i[1],r,this.Normals[t[1]],h),this.data.iVertex(i[2],n,this.Normals[t[2]],l)):(this.data.iVertex(i[0],a,this.Normals[t[0]],o),this.data.iVertex(i[1],r,this.Normals[t[1]],h),this.data.iVertex(i[1],r,this.Normals[t[1]],h),this.data.iVertex(i[2],n,this.Normals[t[2]],l),this.data.iVertex(i[2],n,this.Normals[t[2]],l),this.data.iVertex(i[0],a,this.Normals[t[0]],o))}else 0==wireframe?(this.data.iVertex(i[0],a,this.Normals[t[0]]),this.data.iVertex(i[1],r,this.Normals[t[1]]),this.data.iVertex(i[2],n,this.Normals[t[2]])):(this.data.iVertex(i[0],a,this.Normals[t[0]]),this.data.iVertex(i[1],r,this.Normals[t[1]]),this.data.iVertex(i[1],r,this.Normals[t[1]]),this.data.iVertex(i[2],n,this.Normals[t[2]]),this.data.iVertex(i[2],n,this.Normals[t[2]]),this.data.iVertex(i[0],a,this.Normals[t[0]]))}}this.data.nvertices=this.Positions.length,this.data.indices.length>0&&this.append()}append(){this.transparent?transparentData.append(this.data):triangleData.append(this.data)}}function home(){mat4.identity(rotMat),initProjection(),setProjection(),remesh=!0,draw()}let positionAttribute=0,normalAttribute=1,materialAttribute=2,colorAttribute=3,widthAttribute=4;function initShader(t=[]){let e=getShader(gl,vertex,gl.VERTEX_SHADER,t),i=getShader(gl,fragment,gl.FRAGMENT_SHADER,t),a=gl.createProgram();return gl.attachShader(a,e),gl.attachShader(a,i),gl.bindAttribLocation(a,positionAttribute,"position"),gl.bindAttribLocation(a,normalAttribute,"normal"),gl.bindAttribLocation(a,materialAttribute,"materialIndex"),gl.bindAttribLocation(a,colorAttribute,"color"),gl.bindAttribLocation(a,widthAttribute,"width"),gl.linkProgram(a),gl.getProgramParameter(a,gl.LINK_STATUS)||alert("Could not initialize shaders"),a}class Split3{constructor(t,e,i,a){this.m0=[.5*(t[0]+e[0]),.5*(t[1]+e[1]),.5*(t[2]+e[2])];let r=.5*(e[0]+i[0]),n=.5*(e[1]+i[1]),s=.5*(e[2]+i[2]);this.m2=[.5*(i[0]+a[0]),.5*(i[1]+a[1]),.5*(i[2]+a[2])],this.m3=[.5*(this.m0[0]+r),.5*(this.m0[1]+n),.5*(this.m0[2]+s)],this.m4=[.5*(r+this.m2[0]),.5*(n+this.m2[1]),.5*(s+this.m2[2])],this.m5=[.5*(this.m3[0]+this.m4[0]),.5*(this.m3[1]+this.m4[1]),.5*(this.m3[2]+this.m4[2])]}}function unit(t){let e=1/(Math.sqrt(t[0]*t[0]+t[1]*t[1]+t[2]*t[2])||1);return[t[0]*e,t[1]*e,t[2]*e]}function abs2(t){return t[0]*t[0]+t[1]*t[1]+t[2]*t[2]}function dot(t,e){return t[0]*e[0]+t[1]*e[1]+t[2]*e[2]}function cross(t,e){return[t[1]*e[2]-t[2]*e[1],t[2]*e[0]-t[0]*e[2],t[0]*e[1]-t[1]*e[0]]}function bezierP(t,e){return[e[0]-t[0],e[1]-t[1],e[2]-t[2]]}function bezierPP(t,e,i){return[3*(t[0]+i[0])-6*e[0],3*(t[1]+i[1])-6*e[1],3*(t[2]+i[2])-6*e[2]]}function bezierPPP(t,e,i,a){return[a[0]-t[0]+3*(e[0]-i[0]),a[1]-t[1]+3*(e[1]-i[1]),a[2]-t[2]+3*(e[2]-i[2])]}function bezierPh(t,e,i,a){return[i[0]+a[0]-t[0]-e[0],i[1]+a[1]-t[1]-e[1],i[2]+a[2]-t[2]-e[2]]}function bezierPPh(t,e,i,a){return[3*t[0]-5*e[0]+i[0]+a[0],3*t[1]-5*e[1]+i[1]+a[1],3*t[2]-5*e[2]+i[2]+a[2]]}function Straightness(t,e,i,a){let r=[third*(a[0]-t[0]),third*(a[1]-t[1]),third*(a[2]-t[2])];return Math.max(abs2([e[0]-r[0]-t[0],e[1]-r[1]-t[1],e[2]-r[2]-t[2]]),abs2([a[0]-r[0]-i[0],a[1]-r[1]-i[1],a[2]-r[2]-i[2]]))}function Flatness(t,e,i,a){let r=[e[0]-t[0],e[1]-t[1],e[2]-t[2]],n=[a[0]-i[0],a[1]-i[1],a[2]-i[2]];return Math.max(abs2(cross(r,unit(n))),abs2(cross(n,unit(r))))/9}function corners(t,e){return[t,[t[0],t[1],e[2]],[t[0],e[1],t[2]],[t[0],e[1],e[2]],[e[0],t[1],t[2]],[e[0],t[1],e[2]],[e[0],e[1],t[2]],e]}function minbound(t){return[Math.min(t[0][0],t[1][0],t[2][0],t[3][0],t[4][0],t[5][0],t[6][0],t[7][0]),Math.min(t[0][1],t[1][1],t[2][1],t[3][1],t[4][1],t[5][1],t[6][1],t[7][1]),Math.min(t[0][2],t[1][2],t[2][2],t[3][2],t[4][2],t[5][2],t[6][2],t[7][2])]}function maxbound(t){return[Math.max(t[0][0],t[1][0],t[2][0],t[3][0],t[4][0],t[5][0],t[6][0],t[7][0]),Math.max(t[0][1],t[1][1],t[2][1],t[3][1],t[4][1],t[5][1],t[6][1],t[7][1]),Math.max(t[0][2],t[1][2],t[2][2],t[3][2],t[4][2],t[5][2],t[6][2],t[7][2])]}function COBTarget(t,e){mat4.fromTranslation(T,[center.x,center.y,center.z]),mat4.invert(cjMatInv,T),mat4.multiply(t,e,cjMatInv),mat4.multiply(t,T,t)}function setUniforms(t,e){let i=e==pixelShader;gl.useProgram(e),gl.enableVertexAttribArray(positionAttribute),i&&gl.enableVertexAttribArray(widthAttribute);let a=!i&&Lights.length>0;if(a&&gl.enableVertexAttribArray(normalAttribute),gl.enableVertexAttribArray(materialAttribute),e.projViewMatUniform=gl.getUniformLocation(e,"projViewMat"),e.viewMatUniform=gl.getUniformLocation(e,"viewMat"),e.normMatUniform=gl.getUniformLocation(e,"normMat"),e!=colorShader&&e!=transparentShader||gl.enableVertexAttribArray(colorAttribute),a)for(let t=0;t<Lights.length;++t)Lights[t].setUniform(e,t);for(let i=0;i<t.materials.length;++i)t.materials[i].setUniform(e,i);gl.uniformMatrix4fv(e.projViewMatUniform,!1,projViewMat),gl.uniformMatrix4fv(e.viewMatUniform,!1,viewMat),gl.uniformMatrix3fv(e.normMatUniform,!1,normMat)}function handleMouseDown(t){zoomEnabled||enableZoom(),mouseDownOrTouchActive=!0,lastMouseX=t.clientX,lastMouseY=t.clientY}let pinchStart,touchStartTime,pinch=!1;function pinchDistance(t){return Math.hypot(t[0].pageX-t[1].pageX,t[0].pageY-t[1].pageY)}function handleTouchStart(t){t.preventDefault(),zoomEnabled||enableZoom();let e=t.targetTouches;swipe=rotate=pinch=!1,zooming||(1!=e.length||mouseDownOrTouchActive||(touchStartTime=(new Date).getTime(),touchId=e[0].identifier,lastMouseX=e[0].pageX,lastMouseY=e[0].pageY),2!=e.length||mouseDownOrTouchActive||(touchId=e[0].identifier,pinchStart=pinchDistance(e),pinch=!0))}function handleMouseUpOrTouchEnd(t){mouseDownOrTouchActive=!1}function rotateScene(t,e,i,a,r){if(t==i&&e==a)return;let[n,s]=arcball([t,-e],[i,-a]);mat4.fromRotation(T,2*r*ArcballFactor*n/lastzoom,s),mat4.multiply(rotMat,T,rotMat)}function shiftScene(t,e,i,a){let r=1/lastzoom;shift.x+=(i-t)*r*halfCanvasWidth,shift.y-=(a-e)*r*halfCanvasHeight}function panScene(t,e,i,a){orthographic?shiftScene(t,e,i,a):(center.x+=(i-t)*(viewParam.xmax-viewParam.xmin),center.y-=(a-e)*(viewParam.ymax-viewParam.ymin))}function updateViewMatrix(){COBTarget(viewMat,rotMat),mat4.translate(viewMat,viewMat,[center.x,center.y,0]),mat3.fromMat4(viewMat3,viewMat),mat3.invert(normMat,viewMat3),mat4.multiply(projViewMat,projMat,viewMat)}function capzoom(){let t=Math.sqrt(Number.MAX_VALUE),e=1/t;Zoom<=e&&(Zoom=e),Zoom>=t&&(Zoom=t),Zoom!=lastzoom&&(remesh=!0),lastzoom=Zoom}function zoomImage(t){let e=zoomStep*halfCanvasHeight*t;const i=Math.log(.1*Number.MAX_VALUE)/Math.log(zoomFactor);Math.abs(e)<i&&(Zoom*=zoomFactor**e,capzoom())}function normMouse(t){let e=t[0],i=t[1],a=Math.hypot(e,i);return a>1&&(denom=1/a,e*=denom,i*=denom),[e,i,Math.sqrt(Math.max(1-i*i-e*e,0))]}function arcball(t,e){let i=normMouse(t),a=normMouse(e),r=dot(i,a);return r>1?r=1:r<-1&&(r=-1),[Math.acos(r),unit(cross(i,a))]}function zoomScene(t,e,i,a){zoomImage(e-a)}const DRAGMODE_ROTATE=1,DRAGMODE_SHIFT=2,DRAGMODE_ZOOM=3,DRAGMODE_PAN=4;function processDrag(t,e,i,a=1){let r;switch(i){case DRAGMODE_ROTATE:r=rotateScene;break;case DRAGMODE_SHIFT:r=shiftScene;break;case DRAGMODE_ZOOM:r=zoomScene;break;case DRAGMODE_PAN:r=panScene;break;default:r=((t,e,i,a)=>{})}r((lastMouseX-halfCanvasWidth)/halfCanvasWidth,(lastMouseY-halfCanvasHeight)/halfCanvasHeight,(t-halfCanvasWidth)/halfCanvasWidth,(e-halfCanvasHeight)/halfCanvasHeight,a),lastMouseX=t,lastMouseY=e,setProjection(),draw()}let zoomEnabled=0;function enableZoom(){zoomEnabled=1,canvas.addEventListener("wheel",handleMouseWheel,!1)}function disableZoom(){zoomEnabled=0,canvas.removeEventListener("wheel",handleMouseWheel,!1)}function handleKey(t){if(zoomEnabled||enableZoom(),embedded&&zoomEnabled&&27==t.keyCode)return void disableZoom();let e=[];switch(t.key){case"x":e=[1,0,0];break;case"y":e=[0,1,0];break;case"z":e=[0,0,1];break;case"h":home();break;case"m":3==++wireframe&&(wireframe=0),2!=wireframe&&(embedded||deleteShaders(),initShaders()),remesh=!0,draw();break;case"+":case"=":case">":expand();break;case"-":case"_":case"<":shrink()}e.length>0&&(mat4.rotate(rotMat,rotMat,.1,e),updateViewMatrix(),draw())}function handleMouseWheel(t){t.preventDefault(),t.deltaY<0?Zoom*=zoomFactor:Zoom/=zoomFactor,capzoom(),setProjection(),draw()}function handleMouseMove(t){if(!mouseDownOrTouchActive)return;let e;processDrag(t.clientX,t.clientY,e=t.getModifierState("Control")?DRAGMODE_SHIFT:t.getModifierState("Shift")?DRAGMODE_ZOOM:t.getModifierState("Alt")?DRAGMODE_PAN:DRAGMODE_ROTATE)}let zooming=!1,swipe=!1,rotate=!1;function handleTouchMove(t){if(t.preventDefault(),zooming)return;let e=t.targetTouches;if(!pinch&&1==e.length&&touchId==e[0].identifier){let t=e[0].pageX,i=e[0].pageY,a=t-lastMouseX,r=i-lastMouseY,n=a*a+r*r<=shiftHoldDistance*shiftHoldDistance;if(n&&!swipe&&!rotate&&(new Date).getTime()-touchStartTime>shiftWaitTime&&(navigator.vibrate&&window.navigator.vibrate(vibrateTime),swipe=!0),swipe)processDrag(t,i,DRAGMODE_SHIFT);else if(!n){rotate=!0,processDrag(e[0].pageX,e[0].pageY,DRAGMODE_ROTATE,.5)}}if(pinch&&!swipe&&2==e.length&&touchId==e[0].identifier){let t=pinchDistance(e),i=t-pinchStart;zooming=!0,(i*=zoomPinchFactor)>zoomPinchCap&&(i=zoomPinchCap),i<-zoomPinchCap&&(i=-zoomPinchCap),zoomImage(i/size2),pinchStart=t,swipe=rotate=zooming=!1,setProjection(),draw()}}let pixelShader,materialShader,colorShader,transparentShader,zbuffer=[];function transformVertices(t){let e=viewMat[2],i=viewMat[6],a=viewMat[10];zbuffer.length=t.length;for(let r=0;r<t.length;++r){let n=6*r;zbuffer[r]=e*t[n]+i*t[n+1]+a*t[n+2]}}function drawMaterial0(){drawBuffer(material0Data,pixelShader),material0Data.clear()}function drawMaterial1(){drawBuffer(material1Data,materialShader),material1Data.clear()}function drawMaterial(){drawBuffer(materialData,materialShader),materialData.clear()}function drawColor(){drawBuffer(colorData,colorShader),colorData.clear()}function drawTriangle(){drawBuffer(triangleData,transparentShader),triangleData.clear()}function drawTransparent(){let t=transparentData.indices;if(wireframe>0)return drawBuffer(transparentData,transparentShader,t),void transparentData.clear();if(t.length>0){transformVertices(transparentData.vertices);let e=t.length/3,i=Array(e).fill().map((t,e)=>e);i.sort(function(e,i){let a=3*e;Ia=t[a],Ib=t[a+1],Ic=t[a+2];let r=3*i;return IA=t[r],IB=t[r+1],IC=t[r+2],zbuffer[Ia]+zbuffer[Ib]+zbuffer[Ic]<zbuffer[IA]+zbuffer[IB]+zbuffer[IC]?-1:1});let a=Array(t.length);for(let r=0;r<e;++r){let e=3*i[r];a[3*r]=t[e],a[3*r+1]=t[e+1],a[3*r+2]=t[e+2]}gl.depthMask(!1),drawBuffer(transparentData,transparentShader,a),gl.depthMask(!0)}transparentData.clear()}function drawBuffers(){drawMaterial0(),drawMaterial1(),drawMaterial(),drawColor(),drawTriangle(),drawTransparent()}function draw(){embedded&&(offscreen.width=canvas.width,offscreen.height=canvas.height,setViewport()),gl.clearColor(Background[0],Background[1],Background[2],Background[3]),gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);for(let t=0;t<P.length;++t)P[t].render();drawBuffers(),embedded&&(context.clearRect(0,0,canvas.width,canvas.height),context.drawImage(offscreen,0,0)),0==wireframe&&(remesh=!1)}function setDimensions(t,e,i,a){let r=t/e,n=1/lastzoom,s=(i/t+viewportshift[0])*lastzoom,o=(a/e+viewportshift[1])*lastzoom;if(orthographic){let t=B[0]-b[0],e=B[1]-b[1];if(t<e*r){let t=.5*e*r*n,i=2*t*s,a=e*n*o;viewParam.xmin=-t-i,viewParam.xmax=t-i,viewParam.ymin=b[1]*n-a,viewParam.ymax=B[1]*n-a}else{let e=.5*t/(r*Zoom),i=t*n*s,a=2*e*o;viewParam.xmin=b[0]*n-i,viewParam.xmax=B[0]*n-i,viewParam.ymin=-e-a,viewParam.ymax=e-a}}else{let t=H*n,e=t*r,i=2*e*s,a=2*t*o;viewParam.xmin=-e-i,viewParam.xmax=e-i,viewParam.ymin=-t-a,viewParam.ymax=t-a}}function setProjection(){setDimensions(canvasWidth,canvasHeight,shift.x,shift.y),(orthographic?mat4.ortho:mat4.frustum)(projMat,viewParam.xmin,viewParam.xmax,viewParam.ymin,viewParam.ymax,-viewParam.zmax,-viewParam.zmin),updateViewMatrix()}function initProjection(){H=-Math.tan(.5*angle)*B[2],center.x=center.y=0,center.z=.5*(b[2]+B[2]),lastzoom=Zoom=Zoom0,viewParam.zmin=b[2],viewParam.zmax=B[2],shift.x=shift.y=0}function setViewport(){gl.viewportWidth=canvasWidth,gl.viewportHeight=canvasHeight,gl.viewport(0,0,gl.viewportWidth,gl.viewportHeight),gl.scissor(0,0,gl.viewportWidth,gl.viewportHeight)}function setCanvas(){canvas.width=canvasWidth,canvas.height=canvasHeight,embedded&&(offscreen.width=canvasWidth,offscreen.height=canvasHeight),size2=Math.hypot(canvasWidth,canvasHeight),halfCanvasWidth=.5*canvasWidth,halfCanvasHeight=.5*canvasHeight}function setsize(t,e){t>maxViewportWidth&&(t=maxViewportWidth),e>maxViewportHeight&&(e=maxViewportHeight),shift.x*=t/canvasWidth,shift.y*=e/canvasHeight,canvasWidth=t,canvasHeight=e,setCanvas(),setViewport(),home()}function expand(){setsize(canvasWidth*resizeStep+.5,canvasHeight*resizeStep+.5)}function shrink(){setsize(Math.max(canvasWidth/resizeStep+.5,1),Math.max(canvasHeight/resizeStep+.5,1))}function webGLInit(){if(canvas=document.getElementById("Asymptote"),embedded=window.top.document!=document,initGL(),absolute&&!embedded)canvasWidth*=window.devicePixelRatio,canvasHeight*=window.devicePixelRatio;else{canvas.width=Math.max(window.innerWidth-windowTrim,windowTrim),canvas.height=Math.max(window.innerHeight-windowTrim,windowTrim);let t=canvasWidth/canvasHeight;canvas.width>canvas.height*t?canvas.width=Math.min(canvas.height*t,canvas.width):canvas.height=Math.min(canvas.width/t,canvas.height),canvas.width>0&&(canvasWidth=canvas.width),canvas.height>0&&(canvasHeight=canvas.height)}setCanvas(),ArcballFactor=1+8*Math.hypot(viewportmargin[0],viewportmargin[1])/size2,viewportshift[0]/=Zoom0,viewportshift[1]/=Zoom0,gl.enable(gl.BLEND),gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA),gl.enable(gl.DEPTH_TEST),gl.enable(gl.SCISSOR_TEST),setViewport(),home(),canvas.onmousedown=handleMouseDown,document.onmouseup=handleMouseUpOrTouchEnd,document.onmousemove=handleMouseMove,canvas.onkeydown=handleKey,embedded||enableZoom(),canvas.addEventListener("touchstart",handleTouchStart,!1),canvas.addEventListener("touchend",handleMouseUpOrTouchEnd,!1),canvas.addEventListener("touchcancel",handleMouseUpOrTouchEnd,!1),canvas.addEventListener("touchleave",handleMouseUpOrTouchEnd,!1),canvas.addEventListener("touchmove",handleTouchMove,!1),document.addEventListener("keydown",handleKey,!1)}let listen=!1;class Align{constructor(t,e){if(this.center=t,e){let t=e[0],i=e[1];this.ct=Math.cos(t),this.st=Math.sin(t),this.cp=Math.cos(i),this.sp=Math.sin(i)}}T0(t){return[t[0]+this.center[0],t[1]+this.center[1],t[2]+this.center[2]]}T(t){let e=t[0],i=t[1],a=t[2],r=e*this.ct+a*this.st;return[r*this.cp-i*this.sp+this.center[0],r*this.sp+i*this.cp+this.center[1],-e*this.st+a*this.ct+this.center[2]]}}function Tcorners(t,e,i){let a=[t(e),t([e[0],e[1],i[2]]),t([e[0],i[1],e[2]]),t([e[0],i[1],i[2]]),t([i[0],e[1],e[2]]),t([i[0],e[1],i[2]]),t([i[0],i[1],e[2]]),t(i)];return[minbound(a),maxbound(a)]}function sphere(t,e,i,a,r){let n,s,o,h,l,d,c=[[[1,0,0],[1,0,.370106805057161],[.798938033457256,0,.6932530716149],[.500083269410627,0,.866169630634358],[1,.552284749830793,0],[1,.552284749830793,.370106805057161],[.798938033457256,.441241291938247,.6932530716149],[.500083269410627,.276188363341013,.866169630634358],[.552284749830793,1,0],[.552284749830793,1,.370106805057161],[.441241291938247,.798938033457256,.6932530716149],[.276188363341013,.500083269410627,.866169630634358],[0,1,0],[0,1,.370106805057161],[0,.798938033457256,.6932530716149],[0,.500083269410627,.866169630634358]],[[.500083269410627,0,.866169630634358],[.500083269410627,.276188363341013,.866169630634358],[.35297776917154,0,.951284475617087],[.276188363341013,.500083269410627,.866169630634358],[.264153721902467,.264153721902467,1],[.182177944773632,0,1],[0,.500083269410627,.866169630634358],[0,.35297776917154,.951284475617087],[0,.182177944773632,1],[0,0,1]]],m=new Align(t,r);function f(t){let e=Array(t.length);for(let i=0;i<t.length;++i){let a=t[i];e[i]=l([n*a[0],s*a[1],o*a[2]])}return e}r?(h=1,d=0,l=m.T.bind(m)):(h=-1,d=-e,l=m.T0.bind(m));let u=Tcorners(l,[-e,-e,d],[e,e,e]),p=u[0],v=u[1];for(let t=-1;t<=1;t+=2){n=t*e;for(let t=-1;t<=1;t+=2){s=t*e;for(let t=h;t<=1;t+=2){o=t*e;for(let t=0;t<2;++t)P.push(new BezierPatch(f(c[t]),i,a,p,v))}}}}let a=4/3*(Math.sqrt(2)-1);function disk(t,e,i,r,n){let s=1-2*a/3,o=[[1,0,0],[1,-a,0],[a,-1,0],[0,-1,0],[1,a,0],[s,0,0],[0,-s,0],[-a,-1,0],[a,1,0],[0,s,0],[-s,0,0],[-1,-a,0],[0,1,0],[-a,1,0],[-1,a,0],[-1,0,0]],h=new Align(t,n);let l=Tcorners(h.T.bind(h),[-e,-e,0],[e,e,0]);P.push(new BezierPatch(function(t){let i=Array(t.length);for(let a=0;a<t.length;++a){let r=t[a];i[a]=h.T([e*r[0],e*r[1],0])}return i}(o),i,r,l[0],l[1]))}function cylinder(t,e,i,r,n,s,o){let h,l,d=[[1,0,0],[1,0,1/3],[1,0,2/3],[1,0,1],[1,a,0],[1,a,1/3],[1,a,2/3],[1,a,1],[a,1,0],[a,1,1/3],[a,1,2/3],[a,1,1],[0,1,0],[0,1,1/3],[0,1,2/3],[0,1,1]],c=new Align(t,s);function m(t){let e=Array(t.length);for(let a=0;a<t.length;++a){let r=t[a];e[a]=c.T([h*r[0],l*r[1],i*r[2]])}return e}let f=Tcorners(c.T.bind(c),[-e,-e,0],[e,e,i]),u=f[0],p=f[1];for(let t=-1;t<=1;t+=2){h=t*e;for(let t=-1;t<=1;t+=2)l=t*e,P.push(new BezierPatch(m(d),r,n,u,p))}if(o){let e=c.T([0,0,i]);P.push(new BezierCurve([t,e],r,n,t,e))}}function rmf(t,e,i,a,r){class n{constructor(t,e,i){this.p=t,this.r=e,this.t=i,this.s=cross(i,e)}}let s=Number.EPSILON*Math.max(abs2(t),abs2(e),abs2(i),abs2(a));function o(r){if(1==r){let r=[a[0]-i[0],a[1]-i[1],a[2]-i[2]];return abs2(r)>s?unit(r):abs2(r=[2*i[0]-e[0]-a[0],2*i[1]-e[1]-a[1],2*i[2]-e[2]-a[2]])>s?unit(r):[a[0]-t[0]+3*(e[0]-i[0]),a[1]-t[1]+3*(e[1]-i[1]),a[2]-t[2]+3*(e[2]-i[2])]}let n=[a[0]-t[0]+3*(e[0]-i[0]),a[1]-t[1]+3*(e[1]-i[1]),a[2]-t[2]+3*(e[2]-i[2])],o=[2*(t[0]+i[0])-4*e[0],2*(t[1]+i[1])-4*e[1],2*(t[2]+i[2])-4*e[2]],h=[e[0]-t[0],e[1]-t[1],e[2]-t[2]],l=r*r,d=[n[0]*l+o[0]*r+h[0],n[1]*l+o[1]*r+h[1],n[2]*l+o[2]*r+h[2]];return abs2(d)>s?unit(d):abs2(d=[n[0]*(l=2*r)+o[0],n[1]*l+o[1],n[2]*l+o[2]])>s?unit(d):unit(n)}let h=Array(r.length),l=[e[0]-t[0],e[1]-t[1],e[2]-t[2]];abs2(l)<s&&abs2(l=[t[0]-2*e[0]+i[0],t[1]-2*e[1]+i[1],t[2]-2*e[2]+i[2]])<s&&(l=[a[0]-t[0]+3*(e[0]-i[0]),a[1]-t[1]+3*(e[1]-i[1]),a[2]-t[2]+3*(e[2]-i[2])]);let d=function(t){let e=cross(t,[0,1,0]),i=Number.EPSILON*abs2(t);return abs2(e)>i?unit(e):abs2(e=cross(t,[0,0,1]))>i?unit(e):[1,0,0]}(l=unit(l));h[0]=new n(t,d,l);for(let s=1;s<r.length;++s){let l=h[s-1],d=r[s],c=1-d,m=c*c,f=m*c,u=3*d;m*=u,c*=u*d;let p=d*d*d,v=[f*t[0]+m*e[0]+c*i[0]+p*a[0],f*t[1]+m*e[1]+c*i[1]+p*a[1],f*t[2]+m*e[2]+c*i[2]+p*a[2]],g=[v[0]-l.p[0],v[1]-l.p[1],v[2]-l.p[2]];if(0!=g[0]||0!=g[1]||0!=g[2]){let t=l.r,e=unit(g),i=l.t,a=dot(e,i),r=[i[0]-2*a*e[0],i[1]-2*a*e[1],i[2]-2*a*e[2]];i=o(d);let c=2*dot(e,t),m=[t[0]-c*e[0],t[1]-c*e[1],t[2]-c*e[2]],f=unit([i[0]-r[0],i[1]-r[1],i[2]-r[2]]),u=2*dot(f,m);m=[m[0]-u*f[0],m[1]-u*f[1],m[2]-u*f[2]],h[s]=new n(v,unit(m),unit(i))}else h[s]=h[s-1]}return h}function tube(t,e,i,r,n,s,o){let h=rmf(t[0],t[1],t[2],t[3],[0,1/3,2/3,1]),l=a*e,d=[[e,0],[e,l],[l,e],[0,e]];function c(e,a,o,l){let c=Array(16);for(let i=0;i<4;++i){let r=h[i],n=r.r[0],s=r.s[0],m=n*e+s*a,f=n*o+s*l,u=(n=r.r[1])*e+(s=r.s[1])*a,p=n*o+s*l,v=(n=r.r[2])*e+(s=r.s[2])*a,g=n*o+s*l,w=t[i],x=w[0];w1=w[1],w2=w[2];for(let t=0;t<4;++t){let e=d[t],a=e[0],r=e[1];c[4*i+t]=[m*a+f*r+x,u*a+p*r+w1,v*a+g*r+w2]}}P.push(new BezierPatch(c,i,r,n,s))}c(1,0,0,1),c(0,-1,1,0),c(-1,0,0,-1),c(0,1,-1,0),o&&P.push(new BezierCurve(t,i,r,n,s))}function webGLStart(){0==window.innerWidth||0==window.innerHeight?listen||(listen=!0,window.addEventListener("resize",webGLStart,!1)):(listen&&(window.removeEventListener("resize",webGLStart,!1),listen=!1),webGLInit())}