UNPKG

earclip

Version:

Triangle mesh designed to be fast, efficient, and sphere capable.

4 lines (2 loc) 9.07 kB
class L{i;x;y;prev;next;z=0;prevZ=null;nextZ=null;steiner=!1;constructor(G,K,J,Q=null){if(this.i=G,this.x=K,this.y=J,Q===null)this.prev=this,this.next=this;else this.next=Q.next,this.prev=Q,Q.next.prev=this,Q.next=this}}function h(G,K=[],J=2){let Q=K.length>0,U=Q?K[0]*J:G.length,_=b(G,0,U,J,!0),W=[];if(_===null||_.next===_.prev)return W;let F=1/0,$=1/0,w=-1/0,B=-1/0,E=0,O,C;if(Q)_=m(G,K,_,J);if(G.length>80*J){F=w=G[0],$=B=G[1];for(let R=J;R<U;R+=J){if(O=G[R],C=G[R+1],O<F)F=O;if(C<$)$=C;if(O>w)w=O;if(C>B)B=C}E=Math.max(w-F,B-$),E=E!==0?1/E:0}return f(_,W,J,F,$,E,0),W}function b(G,K,J,Q,U){let _=null;if(U===KG(G,K,J,Q)>0)for(let W=K;W<J;W+=Q)_=new L(W/Q|0,G[W],G[W+1],_);else for(let W=J-Q;W>=K;W-=Q)_=new L(W/Q|0,G[W],G[W+1],_);if(_!==null&&z(_,_.next))X(_),_=_.next;return _}function j(G,K){if(K===void 0)K=G;let J=G,Q;do if(Q=!1,!J.steiner&&(z(J,J.next)||M(J.prev,J,J.next)===0)){if(X(J),J=K=J.prev,J===J.next)break;Q=!0}else J=J.next;while(Q||J!==K);return K}function f(G,K,J,Q,U,_,W){if(G===null)return;if(W===0&&_!==0)d(G,Q,U,_);let F=G,$,w;while(G.prev!==G.next){if($=G.prev,w=G.next,_!==0?v(G,Q,U,_):c(G)){K.push($.i,G.i,w.i),X(G),G=w.next,F=w.next;continue}if(G=w,G===F){if(W===0)f(j(G),K,J,Q,U,_,1);else if(W===1)G=p(j(G),K),f(G,K,J,Q,U,_,2);else if(W===2)r(G,K,J,Q,U,_);break}}}function c(G){let K=G.prev,J=G,Q=G.next;if(M(K,J,Q)>=0)return!1;let U=K.x,_=J.x,W=Q.x,F=K.y,$=J.y,w=Q.y,B=U<_?U<W?U:W:_<W?_:W,E=F<$?F<w?F:w:$<w?$:w,O=U>_?U>W?U:W:_>W?_:W,C=F>$?F>w?F:w:$>w?$:w,R=Q.next;while(R!==K){if(R.x>=B&&R.x<=O&&R.y>=E&&R.y<=C&&H(U,F,_,$,W,w,R.x,R.y)&&M(R.prev,R,R.next)>=0)return!1;R=R.next}return!0}function v(G,K,J,Q){let U=G.prev,_=G,W=G.next;if(M(U,_,W)>=0)return!1;let F=U.x,$=_.x,w=W.x,B=U.y,E=_.y,O=W.y,C=F<$?F<w?F:w:$<w?$:w,R=B<E?B<O?B:O:E<O?E:O,A=F>$?F>w?F:w:$>w?$:w,V=B>E?B>O?B:O:E>O?E:O,P=S(C,R,K,J,Q),u=S(A,V,K,J,Q),D=G.prevZ,Z=G.nextZ;while(D!==null&&D.z>=P&&Z!==null&&Z.z<=u){if(D.x>=C&&D.x<=A&&D.y>=R&&D.y<=V&&D!==U&&D!==W&&H(F,B,$,E,w,O,D.x,D.y)&&M(D.prev,D,D.next)>=0)return!1;if(D=D.prevZ,Z.x>=C&&Z.x<=A&&Z.y>=R&&Z.y<=V&&Z!==U&&Z!==W&&H(F,B,$,E,w,O,Z.x,Z.y)&&M(Z.prev,Z,Z.next)>=0)return!1;Z=Z.nextZ}while(D!==null&&D.z>=P){if(D.x>=C&&D.x<=A&&D.y>=R&&D.y<=V&&D!==U&&D!==W&&H(F,B,$,E,w,O,D.x,D.y)&&M(D.prev,D,D.next)>=0)return!1;D=D.prevZ}while(Z!==null&&Z.z<=u){if(Z.x>=C&&Z.x<=A&&Z.y>=R&&Z.y<=V&&Z!==U&&Z!==W&&H(F,B,$,E,w,O,Z.x,Z.y)&&M(Z.prev,Z,Z.next)>=0)return!1;Z=Z.nextZ}return!0}function p(G,K){let J=G;do{let Q=J.prev,U=J.next.next;if(!z(Q,U)&&y(Q,J,J.next,U)&&Y(Q,U)&&Y(U,Q))K.push(Q.i,J.i,U.i),X(J),X(J.next),J=G=U;J=J.next}while(J!==G);return j(J)}function r(G,K,J,Q,U,_){let W=G;do{let F=W.next.next;while(F!==W.prev){if(W.i!==F.i&&i(W,F)){let $=x(W,F);W=j(W,W.next),$=j($,$.next),f(W,K,J,Q,U,_,0),f($,K,J,Q,U,_,0);return}F=F.next}W=W.next}while(W!==G)}function m(G,K,J,Q){let U=[];for(let _=0,W=K.length;_<W;_++){let F=K[_]*Q,$=_<W-1?K[_+1]*Q:G.length,w=b(G,F,$,Q,!1);if(w===w.next)w.steiner=!0;U.push(e(w))}U.sort(s);for(let _=0;_<U.length;_++)J=o(U[_],J);return J}function s(G,K){return G.x-K.x}function o(G,K){let J=n(G,K);if(J===null)return K;let Q=x(J,G);return j(Q,Q.next),j(J,J.next)}function n(G,K){let J=K,Q=G.x,U=G.y,_=-1/0,W;do{if(U<=J.y&&U>=J.next.y&&J.next.y!==J.y){let E=J.x+(U-J.y)*(J.next.x-J.x)/(J.next.y-J.y);if(E<=Q&&E>_){if(_=E,W=J.x<J.next.x?J:J.next,E===Q)return W}}J=J.next}while(J!==K);if(W===void 0)return null;let F=W,$=W.x,w=W.y,B=1/0;J=W;do{if(Q>=J.x&&J.x>=$&&Q!==J.x&&H(U<w?Q:_,U,$,w,U<w?_:Q,U,J.x,J.y)){let E=Math.abs(U-J.y)/(Q-J.x);if(Y(J,G)&&(E<B||E===B&&(J.x>W.x||J.x===W.x&&t(W,J))))W=J,B=E}J=J.next}while(J!==F);return W}function t(G,K){return M(G.prev,G,K.prev)<0&&M(K.next,G,G.next)<0}function d(G,K,J,Q){let U=G;do{if(U.z===0)U.z=S(U.x,U.y,K,J,Q);U.prevZ=U.prev,U.nextZ=U.next,U=U.next}while(U!==G);if(U.prevZ!==null)U.prevZ.nextZ=null;U.prevZ=null,a(U)}function a(G){let K,J,Q,U,_,W,F,$,w=1;do{J=G,G=null,_=null,W=0;while(J!==null){W++,Q=J,F=0;for(K=0;K<w;K++)if(F++,Q=Q.nextZ,Q===null)break;$=w;while(F>0||$>0&&Q!==null){if(F!==0&&($===0||Q===null||J===null||J.z<=Q.z))U=J,J=J?.nextZ??null,F--;else U=Q,Q=Q?.nextZ??null,$--;if(_!==null)_.nextZ=U;else G=U;if(U!==null)U.prevZ=_;_=U}J=Q}if(_!==null)_.nextZ=null;w*=2}while(W>1);return G}function S(G,K,J,Q,U){return G=(G-J)*U|0,K=(K-Q)*U|0,G=(G|G<<8)&16711935,G=(G|G<<4)&252645135,G=(G|G<<2)&858993459,G=(G|G<<1)&1431655765,K=(K|K<<8)&16711935,K=(K|K<<4)&252645135,K=(K|K<<2)&858993459,K=(K|K<<1)&1431655765,G|K<<1}function e(G){let K=G,J=G;do{if(K.x<J.x||K.x===J.x&&K.y<J.y)J=K;K=K.next}while(K!==G);return J}function H(G,K,J,Q,U,_,W,F){return(U-W)*(K-F)-(G-W)*(_-F)>=0&&(G-W)*(Q-F)-(J-W)*(K-F)>=0&&(J-W)*(_-F)-(U-W)*(Q-F)>=0}function i(G,K){return G.next.i!==K.i&&G.prev.i!==K.i&&!GG(G,K)&&(Y(G,K)&&Y(K,G)&&JG(G,K)&&(M(G.prev,G,K.prev)!==0||M(G,K.prev,K)!==0)||z(G,K)&&M(G.prev,G,G.next)>0&&M(K.prev,K,K.next)>0)}function M(G,K,J){return(K.y-G.y)*(J.x-K.x)-(K.x-G.x)*(J.y-K.y)}function z(G,K){return G.x===K.x&&G.y===K.y}function y(G,K,J,Q){let U=N(M(G,K,J)),_=N(M(G,K,Q)),W=N(M(J,Q,G)),F=N(M(J,Q,K));if(U!==_&&W!==F)return!0;if(U===0&&I(G,J,K))return!0;if(_===0&&I(G,Q,K))return!0;if(W===0&&I(J,G,Q))return!0;if(F===0&&I(J,K,Q))return!0;return!1}function N(G){return G>0?1:G<0?-1:0}function I(G,K,J){return K.x<=Math.max(G.x,J.x)&&K.x>=Math.min(G.x,J.x)&&K.y<=Math.max(G.y,J.y)&&K.y>=Math.min(G.y,J.y)}function GG(G,K){let J=G;do{if(J.i!==G.i&&J.next?.i!==G.i&&J.i!==K.i&&J.next?.i!==K.i&&y(J,J.next,G,K))return!0;J=J.next}while(J!==G);return!1}function Y(G,K){return M(G.prev,G,G.next)<0?M(G,K,G.next)>=0&&M(G,G.prev,K)>=0:M(G,K,G.prev)<0||M(G,G.next,K)<0}function JG(G,K){let J=(G.x+K.x)/2,Q=(G.y+K.y)/2,U=G,_=!1;do{if(U.y>Q!==U.next.y>Q&&U.next.y!==U.y&&J<(U.next.x-U.x)*(Q-U.y)/(U.next.y-U.y)+U.x)_=!_;U=U.next}while(U!==G);return _}function x(G,K){let J=new L(G.i,G.x,G.y),Q=new L(K.i,K.x,K.y),U=G.next,_=K.prev;return G.next=K,K.prev=G,J.next=U,U.prev=J,Q.next=J,J.prev=Q,_.next=Q,Q.prev=_,Q}function X(G){if(G.next.prev=G.prev,G.prev.next=G.next,G.prevZ!==null)G.prevZ.nextZ=G.nextZ;if(G.nextZ!==null)G.nextZ.prevZ=G.prevZ}function KG(G,K,J,Q){let U=0;for(let _=K,W=J-Q;_<J;_+=Q)U+=(G[W]-G[_])*(G[_+1]+G[W+1]),W=_;return U}function wG(G,K=1/0,J=0){let{vertices:Q,holeIndices:U,dim:_}=WG(G),W=h(Q,U,_);if(K!==1/0)QG(Q,W,K,_);return{vertices:Q,indices:W.map((F)=>F+J)}}function QG(G,K,J,Q){let U,_,W;for(let F=0;F<Q;F++)for(let $=0;$<K.length;$+=3){U=K[$],_=K[$+1],W=K[$+2];let w=UG(U,_,W,G,K,Q,F,J);if(w!==void 0)K[$]=w[0],K[$+1]=w[1],K[$+2]=w[2],$-=3}}function UG(G,K,J,Q,U,_,W,F){let $=Q[G*_+W],w=Q[K*_+W],B=Q[J*_+W];if($<w&&$<B){let E=$+F-k($,F);if(E>$&&E<=w&&E<=B&&w!==E)return q(E,G,K,J,$,w,B,Q,U,_,W,F)}else if($>w&&$>B){let E=k($,F);if(E===0)E=F;let O=$-E;if(O<$&&O>=w&&O>=B&&w!==O)return g(O,G,K,J,$,w,B,Q,U,_,W,F)}if(w<$&&w<B){let E=w+F-k(w,F);if(E>w&&E<=B&&E<=$&&($!==E||B!==E))return q(E,K,J,G,w,B,$,Q,U,_,W,F)}else if(w>$&&w>B){let E=k(w,F);if(E===0)E=F;let O=w-E;if(O<w&&O>=B&&O>=$&&($!==O||B!==O))return g(O,K,J,G,w,B,$,Q,U,_,W,F)}if(B<$&&B<w){let E=B+F-k(B,F);if(E>B&&E<=$&&E<=w&&($!==E||w!==E))return q(E,J,G,K,B,$,w,Q,U,_,W,F)}else if(B>$&&B>w){let E=k(B,F);if(E===0)E=F;let O=B-E;if(O<B&&O>=$&&O>=w&&($!==O||w!==O))return g(O,J,G,K,B,$,w,Q,U,_,W,F)}}function T(G,K,J,Q,U,_,W,F){let $=_.length/W,w=(U-Q)/(G-Q),B,E;for(let O=0;O<W;O++)if(B=_[K*W+O],E=_[J*W+O],O!==F)_.push(B+(E-B)/w);else _.push(G);return $}function q(G,K,J,Q,U,_,W,F,$,w,B,E){let O=T(G,K,J,U,_,F,w,B),C=T(G,K,Q,U,W,F,w,B);if($.push(K,O,C),G+=E,_<W){while(G<_)$.push(C,O),C=T(G,K,Q,U,W,F,w,B),$.push(C,C,O),O=T(G,K,J,U,_,F,w,B),$.push(O),G+=E;return $.push(C,O,J),[C,J,Q]}else{while(G<W)$.push(C,O),C=T(G,K,Q,U,W,F,w,B),$.push(C,C,O),O=T(G,K,J,U,_,F,w,B),$.push(O),G+=E;return $.push(C,O,Q),[Q,O,J]}}function g(G,K,J,Q,U,_,W,F,$,w,B,E){let O=T(G,K,J,U,_,F,w,B),C=T(G,K,Q,U,W,F,w,B);if($.push(K,O,C),G-=E,_>W){while(G>_)$.push(C,O),C=T(G,K,Q,U,W,F,w,B),$.push(C,C,O),O=T(G,K,J,U,_,F,w,B),$.push(O),G-=E;return $.push(C,O,J),[C,J,Q]}else{while(G>W)$.push(C,O),C=T(G,K,Q,U,W,F,w,B),$.push(C,C,O),O=T(G,K,J,U,_,F,w,B),$.push(O),G-=E;return $.push(C,O,Q),[Q,O,J]}}function k(G,K){return(G%K+K)%K}function WG(G){let K=[],J=[],Q=0,U=2;if(G.length!==0&&G[0].length!==0){let _=Array.isArray(G[0][0]);U=!_?"z"in G[0][0]?3:2:G[0][0].length;for(let W=0,F=G.length;W<F;W++){let $=G[W];for(let w of $)if(_)for(let B=0;B<U;B++)K.push(w[B]);else if(K.push(w.x,w.y),w.z!==void 0)K.push(w.z);if(W>0)Q+=G[W-1].length,J.push(Q)}}return{vertices:K,holeIndices:J,dim:U}}function FG(G,K=[],J=2,Q=[]){let U=K.length>0,_=U?K[0]*J:G.length,W=Math.abs(l(G,0,_,J));if(U)for(let $=0,w=K.length;$<w;$++){let B=K[$]*J,E=$<w-1?K[$+1]*J:G.length;W-=Math.abs(l(G,B,E,J))}let F=0;for(let $=0;$<Q.length;$+=3){let w=Q[$]*J,B=Q[$+1]*J,E=Q[$+2]*J;F+=Math.abs((G[w]-G[E])*(G[B+1]-G[w+1])-(G[w]-G[B])*(G[E+1]-G[w+1]))}return W===0&&F===0?0:Math.abs((F-W)/W)}function l(G,K,J,Q){let U=0;for(let _=K,W=J-Q;_<J;_+=Q)U+=(G[W]-G[_])*(G[_+1]+G[W+1]),W=_;return U}export{QG as tesselate,WG as flatten,h as earcut,wG as earclip,FG as deviation}; //# debugId=88FEF9EC658AED6E64756E2164756E21