@ue-too/curve 0.9.5 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +472 -4
- package/b-curve.d.ts +86 -0
- package/composite-curve.d.ts +16 -0
- package/index.d.ts +96 -0
- package/index.js +2 -1590
- package/index.js.map +6 -6
- package/line.d.ts +29 -0
- package/package.json +2 -2
- package/path.d.ts +4 -0
package/index.js
CHANGED
|
@@ -1,1591 +1,3 @@
|
|
|
1
|
-
// src/b-curve.ts
|
|
2
|
-
import { PointCal } from "@ue-too/math";
|
|
3
|
-
var T = [
|
|
4
|
-
-0.06405689286260563,
|
|
5
|
-
0.06405689286260563,
|
|
6
|
-
-0.1911188674736163,
|
|
7
|
-
0.1911188674736163,
|
|
8
|
-
-0.3150426796961634,
|
|
9
|
-
0.3150426796961634,
|
|
10
|
-
-0.4337935076260451,
|
|
11
|
-
0.4337935076260451,
|
|
12
|
-
-0.5454214713888396,
|
|
13
|
-
0.5454214713888396,
|
|
14
|
-
-0.6480936519369755,
|
|
15
|
-
0.6480936519369755,
|
|
16
|
-
-0.7401241915785544,
|
|
17
|
-
0.7401241915785544,
|
|
18
|
-
-0.820001985973903,
|
|
19
|
-
0.820001985973903,
|
|
20
|
-
-0.8864155270044011,
|
|
21
|
-
0.8864155270044011,
|
|
22
|
-
-0.9382745520027328,
|
|
23
|
-
0.9382745520027328,
|
|
24
|
-
-0.9747285559713095,
|
|
25
|
-
0.9747285559713095,
|
|
26
|
-
-0.9951872199970213,
|
|
27
|
-
0.9951872199970213
|
|
28
|
-
];
|
|
29
|
-
var C = [
|
|
30
|
-
0.12793819534675216,
|
|
31
|
-
0.12793819534675216,
|
|
32
|
-
0.1258374563468283,
|
|
33
|
-
0.1258374563468283,
|
|
34
|
-
0.12167047292780339,
|
|
35
|
-
0.12167047292780339,
|
|
36
|
-
0.1155056680537256,
|
|
37
|
-
0.1155056680537256,
|
|
38
|
-
0.10744427011596563,
|
|
39
|
-
0.10744427011596563,
|
|
40
|
-
0.09761865210411388,
|
|
41
|
-
0.09761865210411388,
|
|
42
|
-
0.08619016153195327,
|
|
43
|
-
0.08619016153195327,
|
|
44
|
-
0.0733464814110803,
|
|
45
|
-
0.0733464814110803,
|
|
46
|
-
0.05929858491543678,
|
|
47
|
-
0.05929858491543678,
|
|
48
|
-
0.04427743881741981,
|
|
49
|
-
0.04427743881741981,
|
|
50
|
-
0.028531388628933663,
|
|
51
|
-
0.028531388628933663,
|
|
52
|
-
0.0123412297999872,
|
|
53
|
-
0.0123412297999872
|
|
54
|
-
];
|
|
1
|
+
import{PointCal as $}from"@ue-too/math";var v=[-0.06405689286260563,0.06405689286260563,-0.1911188674736163,0.1911188674736163,-0.3150426796961634,0.3150426796961634,-0.4337935076260451,0.4337935076260451,-0.5454214713888396,0.5454214713888396,-0.6480936519369755,0.6480936519369755,-0.7401241915785544,0.7401241915785544,-0.820001985973903,0.820001985973903,-0.8864155270044011,0.8864155270044011,-0.9382745520027328,0.9382745520027328,-0.9747285559713095,0.9747285559713095,-0.9951872199970213,0.9951872199970213],a=[0.12793819534675216,0.12793819534675216,0.1258374563468283,0.1258374563468283,0.12167047292780339,0.12167047292780339,0.1155056680537256,0.1155056680537256,0.10744427011596563,0.10744427011596563,0.09761865210411388,0.09761865210411388,0.08619016153195327,0.08619016153195327,0.0733464814110803,0.0733464814110803,0.05929858491543678,0.05929858491543678,0.04427743881741981,0.04427743881741981,0.028531388628933663,0.028531388628933663,0.0123412297999872,0.0123412297999872];class j{controlPoints;dControlPoints=[];arcLengthLUT={controlPoints:[],arcLengthLUT:[]};_fullLength;lengthCache=new Map;getCacheStats(){return{size:this.lengthCache.size,hitRate:0}}preWarmCache(z=100){let G=1/z;for(let N=0;N<=1;N+=G)this.lengthAtT(N)}clearCache(){this.lengthCache.clear()}constructor(z){this.controlPoints=z,this.dControlPoints=this.getDerivativeControlPoints(this.controlPoints),this._fullLength=this.calculateFullLength(),this.arcLengthLUT={controlPoints:[],arcLengthLUT:[]},this.clearCache()}getPointbyPercentage(z){let G=this.arcLengthLUT.controlPoints.length!=this.controlPoints.length;G=G||this.arcLengthLUT.controlPoints.reduce((O,W,_)=>{return O||!$.isEqual(W,this.controlPoints[_])},!1);let N=[];if(G)this.arcLengthLUT=this.getArcLengthLUT(1000);N=[...this.arcLengthLUT.arcLengthLUT.map((O)=>O.length)];let Q=z*this.fullLength,k=0,J=N.length-1;while(k<=J){let O=Math.floor((k+J)/2);if(N[O]==Q)return this.get((O+1)/N.length);else if(N[O]<Q)k=O+1;else J=O-1}return k>=N.length?this.get(1):this.get((k+1)/N.length)}getDerivativeControlPoints(z){let G=[];for(let N=1;N<z.length;N++)G.push($.multiplyVectorByScalar($.subVector(z[N],z[N-1]),z.length-1));return G}validateTVal(z){if(z>1||z<0)throw new c("tVal is greater than 1 or less than 0")}getControlPoints(){return this.controlPoints}setControlPoints(z){this.controlPoints=z,this.dControlPoints=this.getDerivativeControlPoints(this.controlPoints),this._fullLength=this.calculateFullLength(),this.arcLengthLUT={controlPoints:[],arcLengthLUT:[]},this.clearCache()}setControlPointAtIndex(z,G){if(z<0||z>=this.controlPoints.length)return!1;return this.controlPoints[z]=G,this.dControlPoints=this.getDerivativeControlPoints(this.controlPoints),this._fullLength=this.calculateFullLength(),this.arcLengthLUT={controlPoints:[],arcLengthLUT:[]},this.clearCache(),!0}compute(z){this.validateTVal(z);let G=this.controlPoints;while(G.length>1){let N=G.slice(1);for(let Q=0;Q<N.length;Q++)N[Q]=$.addVector($.multiplyVectorByScalar(G[Q],1-z),$.multiplyVectorByScalar(G[Q+1],z));G=N}return G[0]}get(z){if(this.validateTVal(z),this.controlPoints.length==3){let G=$.multiplyVectorByScalar(this.controlPoints[0],(1-z)*(1-z)),N=$.multiplyVectorByScalar(this.controlPoints[1],2*(1-z)*z),Q=$.multiplyVectorByScalar(this.controlPoints[2],z*z);return $.addVector($.addVector(G,N),Q)}if(this.controlPoints.length==4){let G=$.multiplyVectorByScalar(this.controlPoints[0],(1-z)*(1-z)*(1-z)),N=$.multiplyVectorByScalar(this.controlPoints[1],3*(1-z)*(1-z)*z),Q=$.multiplyVectorByScalar(this.controlPoints[2],3*(1-z)*z*z),k=$.multiplyVectorByScalar(this.controlPoints[3],z*z*z);return $.addVector($.addVector(G,N),$.addVector(Q,k))}return this.compute(z)}getLUT(z=100){let G=1/z,N=[],Q=0;N.push(this.get(Q));for(let k=0;k<z;k+=1){if(Q+=G,Q>1&&Q-G<1||k==z-1)Q=1;N.push(this.get(Q))}return N}getLUTWithTVal(z){if(z==null)z=100;let G=1/z,N=[],Q=0;N.push({point:this.get(Q),tVal:Q});for(let k=0;k<z;k+=1){if(Q+=G,Q>1&&Q-G<1||k==z-1)Q=1;N.push({point:this.get(Q),tVal:Q})}return N}get fullLength(){return this._fullLength}calculateFullLength(){return this.lengthAtT(1)}lengthAtT(z){this.validateTVal(z);let G=Math.round(z*1e6)/1e6;if(this.lengthCache.has(G))return this.lengthCache.get(G);let N=z/2,Q=v.length,k=0;for(let O=0,W;O<Q;O++)W=N*v[O]+N,k+=a[O]*$.magnitude(this.derivative(W));let J=N*k;return this.lengthCache.set(G,J),J}derivative(z){return b(z,this.dControlPoints)}derivativeNormalized(z){return $.unitVector(b(z,this.dControlPoints))}getArcLengthLUT(z=50){if(this.arcLengthLUT.controlPoints.length!==this.controlPoints.length||this.arcLengthLUT.controlPoints.some((N,Q)=>!$.isEqual(N,this.controlPoints[Q]))||this.arcLengthLUT.arcLengthLUT.length===0){this.clearCache();let N=[],Q=1/z;for(let k=0;k<=1;k+=Q)N.push({tVal:k,length:this.lengthAtT(k)});this.arcLengthLUT={controlPoints:[...this.controlPoints],arcLengthLUT:N}}return this.arcLengthLUT}splitIntoCurves(z){let G=this.split(z);return[new j(G[0]),new j(G[1])]}split(z){if(this.validateTVal(z),this.controlPoints.length==3){let F=this.controlPoints[0],H=$.subVector($.multiplyVectorByScalar(this.controlPoints[1],z),$.multiplyVectorByScalar(this.controlPoints[0],z-1)),E=$.subVector($.multiplyVectorByScalar(this.controlPoints[2],z*z),$.multiplyVectorByScalar(this.controlPoints[1],2*z*(z-1)));E=$.addVector(E,$.multiplyVectorByScalar(this.controlPoints[0],(z-1)*(z-1)));let B=$.subVector($.multiplyVectorByScalar(this.controlPoints[2],z),$.multiplyVectorByScalar(this.controlPoints[1],z-1)),q=this.controlPoints[2];return[[F,H,E],[E,B,q]]}let G=this.controlPoints[0],N=$.subVector($.multiplyVectorByScalar(this.controlPoints[1],z),$.multiplyVectorByScalar(this.controlPoints[0],z-1)),Q=$.addVector($.multiplyVectorByScalar(this.controlPoints[2],z*z),$.addVector($.multiplyVectorByScalar(this.controlPoints[1],-(2*z*(z-1))),$.multiplyVectorByScalar(this.controlPoints[0],(z-1)*(z-1)))),k=$.multiplyVectorByScalar(this.controlPoints[3],z*z*z),J=$.multiplyVectorByScalar(this.controlPoints[2],-(3*z*z*(z-1))),O=$.multiplyVectorByScalar(this.controlPoints[1],3*z*(z-1)*(z-1)),W=$.multiplyVectorByScalar(this.controlPoints[0],-((z-1)*(z-1)*(z-1))),_=$.addVector(W,$.addVector(O,$.addVector(k,J))),Z=$.addVector($.addVector($.multiplyVectorByScalar(this.controlPoints[3],z*z),$.multiplyVectorByScalar(this.controlPoints[2],-(2*z*(z-1)))),$.multiplyVectorByScalar(this.controlPoints[1],(z-1)*(z-1))),K=$.addVector($.multiplyVectorByScalar(this.controlPoints[3],z),$.multiplyVectorByScalar(this.controlPoints[2],-(z-1))),X=this.controlPoints[3];return[[G,N,Q,_],[_,Z,K,X]]}splitIn3WithControlPoints(z,G){if(G<z)console.warn("tVal2 is less than tVal, swapping them"),[z,G]=[G,z];let N=this.split(z),Q=new j(N[1]),k=x(G,z,1,0,1),J=Q.split(k);return[N[0],J[0],J[1]]}splitIn3Curves(z,G){if(G<z)console.warn("tVal2 is less than tVal, swapping them"),[z,G]=[G,z];let N=this.split(z),Q=new j(N[1]),k=x(G,z,1,0,1),J=Q.split(k);return[new j(N[0]),new j(J[0]),new j(J[1])]}splitAndTakeMidCurve(z,G){let[N,Q,k]=this.splitIn3Curves(z,G);return Q}getProjection(z){let N=Number.MAX_VALUE,Q=0,k=this.get(0),J=0,O=this.getLUTWithTVal(500);O.forEach((Z,K)=>{let X=$.distanceBetweenPoints(Z.point,z);if(X<N)N=X,k={...Z.point},Q=Z.tVal,J=K});let W=O[J].tVal,_=O[J].tVal;if(J<O.length-1)_=O[J+1].tVal;if(J>0)W=O[J-1].tVal;while(W<_&&_-W>0.00001){let Z=W+(_-W)/2,K=Z-W,X=Z+K/2,F=Z+K/2,H=N;if(X<=1&&X>=0){let E=$.distanceBetweenPoints(this.get(X),z);if(E<N)N=E,k=this.get(X),Q=X,_=X+K/2,W=X-K/2}if(F<=1&&F>=0){let E=$.distanceBetweenPoints(this.get(F),z);if(E<N)N=E,k=this.get(F),Q=F,_=F+K/2,W=F-K/2}if(H==N)break}return{projection:k,tVal:Q}}findArcs(z){let G=0,N=[];while(G<1){let Q=this.findArcStartingAt(z,G);if(Q==null||Q.arc==null)break;if(N.push(Q.arc),G=Q.arc.endT,G>=1)break}return N}findArcStartingAt(z,G){let N=1,Q=G+(N-G)/2,k={good:!1},J=0;while(!0){if(J++,Q=G+(N-G)/2,N>1||Q>1)if(k.good)return k;else return null;let O=this.get(G),W=this.get(N),_=this.get(Q),Z=this.fitArc(O,W,_);if(!Z.exists||Z.center==null||Z.radius==null)return null;let K=N-Q,X=Q-K/2,F=Q+K/2,H=this.get(X),E=this.get(F),B=$.distanceBetweenPoints(H,Z.center),q=$.distanceBetweenPoints(E,Z.center);if(Math.abs(B-Z.radius)>z||Math.abs(q-Z.radius)>z){if(k.good==!0)return k;k.good=!1,N=Q}else{if(k.good=!0,Z.startPoint!==void 0&&Z.endPoint!==void 0)k.arc={center:Z.center,radius:Z.radius,startPoint:Z.startPoint,endPoint:Z.endPoint,startT:G,endT:N};N=N+(Q-G)}}}fitArc(z,G,N){let Q=[[z.x,z.y,1],[N.x,N.y,1],[G.x,G.y,1]];if(this.determinant3by3(Q)==0)return{exists:!1};let k=[[z.x*z.x+z.y*z.y,z.y,1],[N.x*N.x+N.y*N.y,N.y,1],[G.x*G.x+G.y*G.y,G.y,1]],J=[[z.x*z.x+z.y*z.y,z.x,1],[N.x*N.x+N.y*N.y,N.x,1],[G.x*G.x+G.y*G.y,G.x,1]],O=[[z.x*z.x+z.y*z.y,z.x,z.y],[N.x*N.x+N.y*N.y,N.x,N.y],[G.x*G.x+G.y*G.y,G.x,G.y]],W=0.5*(this.determinant3by3(k)/this.determinant3by3(Q)),_=-0.5*(this.determinant3by3(J)/this.determinant3by3(Q)),Z=Math.sqrt(W*W+_*_+this.determinant3by3(O)/this.determinant3by3(Q));return{exists:!0,center:{x:W,y:_},radius:Z,startPoint:z,endPoint:G}}determinant3by3(z){let G=z[0][0],N=z[0][1],Q=z[0][2],k=z[1][0],J=z[1][1],O=z[1][2],W=z[2][0],_=z[2][1],Z=z[2][2];return G*(J*Z-O*_)-N*(k*Z-W*O)+Q*(k*_-J*W)}curvature(z){let G=b(z,this.dControlPoints),N=b(z,this.getDerivativeControlPoints(this.dControlPoints)),Q=G.x*N.y-N.x*G.y,k=Math.pow(G.x*G.x+G.y*G.y,1.5);if(k==0)return NaN;return Q/k}secondDerivative(z){return b(z,this.getDerivativeControlPoints(this.dControlPoints))}getCoefficientOfTTerms(){return this.getCoefficientOfTTermsWithControlPoints(this.controlPoints)}getDerivativeCoefficients(){return this.getCoefficientOfTTermsWithControlPoints(this.dControlPoints)}getCoefficientOfTTermsWithControlPoints(z){let G=[],N=[];if(z.length==3)N=[[1,0,0],[-2,2,0],[1,-2,1]];else if(z.length==4)N=[[1,0,0,0],[-3,3,0,0],[3,-6,3,0],[-1,3,-3,1]];else if(z.length==2)N=[[1,0],[-1,1]];else throw Error("number of control points is wrong");for(let Q=0;Q<z.length;Q++)G.push(z.reduce((k,J,O)=>{return{x:k.x+N[Q][O]*J.x,y:k.y+N[Q][O]*J.y}},{x:0,y:0}));return G}getControlPointsAlignedWithXAxis(){let z=$.unitVectorFromA2B(this.controlPoints[0],this.controlPoints[this.controlPoints.length-1]),G=$.angleFromA2B({x:1,y:0},z),N=this.controlPoints[0],Q=[{x:0,y:0}];for(let k=1;k<this.controlPoints.length;k++){let J=$.subVector(this.controlPoints[k],N),O=$.rotatePoint(J,-G);Q.push(O)}return Q}getExtrema(){let z={x:[],y:[]},G=this.getDerivativeCoefficients(),N=[0,0,0,0],Q=[0,0,0,0];G.forEach((O,W)=>{N[3-W]=O.x,Q[3-W]=O.y});let k=w(N[0],N[1],N[2],N[3]),J=w(Q[0],Q[1],Q[2],Q[3]);if(k.forEach((O)=>{if(O>=0&&O<=1)z.x.push(O)}),J.forEach((O)=>{if(O>=0&&O<=1)z.y.push(O)}),G.length>=3){N=[0,0,0,0],Q=[0,0,0,0],this.getCoefficientOfTTermsWithControlPoints(this.getDerivativeControlPoints(this.dControlPoints)).forEach((Z,K)=>{N[3-K]=Z.x,Q[3-K]=Z.y});let W=w(N[0],N[1],N[2],N[3]),_=w(Q[0],Q[1],Q[2],Q[3]);W.forEach((Z)=>{if(Z>=0&&Z<=1)z.x.push(Z)}),_.forEach((Z)=>{if(Z>=0&&Z<=1)z.y.push(Z)})}return z}translateRotateControlPoints(z,G){let N=[];for(let Q=0;Q<this.controlPoints.length;Q++)N.push($.rotatePoint($.addVector(this.controlPoints[Q],z),G));return N}getLineIntersections(z){let G=z.getTranslationRotationToAlginXAxis(),N=[],Q=this.translateRotateControlPoints(G.translation,G.rotationAngle),k=this.getCoefficientOfTTermsWithControlPoints(Q),J=[0,0,0,0];return k.forEach((W,_)=>{J[3-_]=W.y}),w(J[0],J[1],J[2],J[3]).forEach((W)=>{if(W>=0&&W<=1){if(z.pointInLine(this.get(W)))N.push(W)}}),N}getSelfIntersections(){let[z,G]=this.split(0.5),N=new j(z),Q=new j(G),k=V(N,Q);return k.forEach((J)=>{J.selfT=J.selfT*0.5,J.otherT=J.otherT*0.5+0.5}),k.shift(),k}getCircleIntersections(z,G){let N=this.getLUTWithTVal(500),Q=Number.MAX_VALUE,k=0,J=N[0].point,O=N[0].tVal;N.forEach((F,H)=>{let E=Math.abs($.distanceBetweenPoints(z,F.point));if(E<Q)Q=E,k=H});let W=N.map((F,H)=>{return{...F,distance:0}});Q=Number.MAX_VALUE;let _=0,Z=0,K=[];while(++Z<25){let F=this.findClosest(z.x,z.y,W,G,5,W[_-2]?.distance,W[_-1]?.distance);if(F<_)break;if(F>0&&F==_)break;K.push(F),_=F+2}let X=[];return K.forEach((F)=>{let H=this.refineBinary(this,z.x,z.y,W,F,G);if(H!=null)X.push({intersection:H.point,tVal:H.tVal})}),X}advanceAtTWithLength(z,G){let Q=this.lengthAtT(z)+G;if(z===0&&G<0)return{type:"beforeCurve",remainLength:-G};if(z===1&&G>0)return{type:"afterCurve",remainLength:G};if(Q>this.fullLength)return{type:"afterCurve",remainLength:Q-this.fullLength};else if(Q<0)return{type:"beforeCurve",remainLength:-Q};if(this.arcLengthLUT.arcLengthLUT.length===0)this.arcLengthLUT=this.getArcLengthLUT(1000);let k=this.arcLengthLUT.arcLengthLUT,J=0,O=k.length-1;while(J<=O){let B=Math.floor((J+O)/2),q=k[B].length;if(D(q,Q,0.01)){let M=k[B].tVal,I=this.get(M);return{type:"withinCurve",tVal:M,point:I}}else if(q<Q)J=B+1;else O=B-1}if(O<0)O=1,J=0;if(J>=k.length)O=k.length-1,J=k.length-2;let W=k[O],_=k[J],Z=_.length-W.length,K=_.tVal-W.tVal;if(Z===0){let B=this.get(W.tVal);return{type:"withinCurve",tVal:W.tVal,point:B}}let X=(Q-W.length)/Z,F=W.tVal+X*K,H=Math.max(0,Math.min(1,F)),E=this.get(H);return{type:"withinCurve",tVal:H,point:E}}advanceByDistance(z,G){let N=z,Q=G,k=0.01;if(G>this.fullLength)return{type:"afterCurve",remainLength:G-this.fullLength};else if(G<0)return{type:"beforeCurve",remainLength:-G};while(Q>0&&N<1){let J=this.get(N),O=Math.min(N+k,1),W=this.get(O),_=Math.sqrt(Math.pow(W.x-J.x,2)+Math.pow(W.y-J.y,2));if(_>=Q){let Z=Q/_;return{type:"withinCurve",tVal:N+Z*(O-N),point:this.get(N+Z*(O-N))}}Q-=_,N=O}return{type:"withinCurve",tVal:N,point:this.get(N)}}refineBinary(z,G,N,Q,k,J=0,O=0.01){let W=Q[k],_=1,Z=Number.MAX_SAFE_INTEGER;do{let K=k===0?0:k-1,X=k===Q.length-1?Q.length-1:k+1,F=Q[K].tVal,H=Q[X].tVal,E=[],B=(H-F)/4;if(B<0.001)break;E.push(Q[K]);for(let q=1;q<=3;q++){let M=z.get(F+q*B),I=Math.abs($.distanceBetweenPoints(M,{x:G,y:N})-J);if(I<Z)Z=I,W={point:M,tVal:F+q*B,distance:I},k=q;E.push({point:M,tVal:F+q*B,distance:I})}E.push(Q[X]),Q=E}while(_++<25);if(J&&Z>O)W=void 0;return W}findClosest(z,G,N,Q,k=5,J,O){let W=Number.MAX_SAFE_INTEGER,_=J||W,Z=O||W,K=-1;for(let X=0,F=N.length;X<F;X++){let H=N[X].point;if(N[X].distance=Math.abs($.distanceBetweenPoints({x:z,y:G},H)-Q),Z<k&&_>Z&&Z<N[X].distance){K=X-1;break}if(N[X].distance<W)W=N[X].distance;_=Z,Z=N[X].distance}return K}getCurveIntersections(z,G){return V(this,z,G)}get AABB(){let z=this.getExtrema(),G=[0,1],N={x:Number.MAX_VALUE,y:Number.MAX_VALUE},Q={x:-Number.MAX_VALUE,y:-Number.MAX_VALUE};return z.x.forEach((k)=>{G.push(k)}),z.y.forEach((k)=>{G.push(k)}),G.forEach((k)=>{let J=this.get(k);N.x=Math.min(N.x,J.x),N.y=Math.min(N.y,J.y),Q.x=Math.max(Q.x,J.x),Q.y=Math.max(Q.y,J.y)}),{min:N,max:Q}}normal(z){let G=this.derivative(z),N=Math.sqrt(G.x*G.x+G.y*G.y);return{tVal:z,direction:{x:-G.y/N,y:G.x/N}}}}function r(z){let G,N=0,Q=0,k=0.01,J,O=[],W=[],_=z.getExtrema().x;if(_.indexOf(0)===-1)_=[0].concat(_);if(_.indexOf(1)===-1)_.push(1);for(N=_[0],G=1;G<_.length;G++)Q=_[G],J=z.splitAndTakeMidCurve(N,Q),O.push(J),N=Q;return O.forEach((Z)=>{N=0,Q=0;while(Q<=1)for(Q=N+k;Q<=1+k;Q+=k){let K=Math.min(Q,1);if(J=Z.splitAndTakeMidCurve(N,K),!t(J)){if(Q-=k,Math.abs(N-Q)<k)return[];let X=Math.min(Q,1);J=Z.splitAndTakeMidCurve(N,X),W.push(J),N=X;break}}if(N<1)J=Z.splitAndTakeMidCurve(N,1),W.push(J)}),W}function e(z){let G=z.getControlPoints(),N=[G[0]],Q=G.length;for(let k=1;k<Q;k++){let J=G[k],O=G[k-1];N[k]={x:(Q-k)/Q*J.x+k/Q*O.x,y:(Q-k)/Q*J.y+k/Q*O.y}}return N[Q]=G[Q-1],new j(N)}function u(z,G,N){if(N!==void 0){let J=z.get(G),O=z.normal(G).direction;return{c:J,n:O,x:J.x+O.x*N,y:J.y+O.y*N}}let Q=z.getControlPoints();if(g(z)){let J=z.normal(0).direction,O=Q.map(function(W){return{x:W.x+G*J.x,y:W.y+G*J.y}});return[new j(O)]}return r(z).map(function(J){if(g(J))return u(J,G)[0];return P(J,G)})}function O0(z,G){return z.getLUTWithTVal(100).map((k)=>{let J=$.unitVector(z.derivative(k.tVal)),O={x:-J.y,y:J.x};return{x:k.point.x+O.x*G,y:k.point.y+O.y*G}})}function C(z,G,N,Q){let{x:k,y:J}=z,O=G.x,W=G.y,_=N.x,Z=N.y,K=Q.x,X=Q.y;return o(k,J,O,W,_,Z,K,X)}function o(z,G,N,Q,k,J,O,W){let _=(z*Q-G*N)*(k-O)-(z-N)*(k*W-J*O),Z=(z*Q-G*N)*(J-W)-(G-Q)*(k*W-J*O),K=(z-N)*(J-W)-(G-Q)*(k-O);if(K==0)return!1;return{x:_/K,y:Z/K}}function g(z){let G=z.getControlPoints().length-1,N=z.getControlPoints(),Q=n(N,{p1:N[0],p2:N[G]}),k=$.distanceBetweenPoints(N[0],N[G]);return Q.reduce((O,W)=>O+Math.abs(W.y),0)<k/50}function P(z,G){let N=z.getControlPoints().length-1,Q=void 0;if(typeof G==="function")Q=G;if(Q&&N===2)return P(e(z),Q);let k=z.getControlPoints();if(g(z))return p(z,z.normal(0).direction,Q?Q(0):G,Q?Q(1):G);let J=Q?Q(0):G,O=Q?Q(1):G,W=[u(z,0,10),u(z,1,10)],_=[],Z=C(W[0],W[0].c,W[1],W[1].c);if(!Z)return p(z,z.normal(0).direction,J,O);if([0,1].forEach(function(K){let X=JSON.parse(JSON.stringify(k[K*N])),F=W[K];X.x+=(K?O:J)*F.n.x,X.y+=(K?O:J)*F.n.y,_[K*N]=X}),!Q)return[0,1].forEach((K)=>{if(N===2&&!!K)return;let X=_[K*N],F=z.derivative(K),H={x:X.x+F.x,y:X.y+F.y},E=C(X,H,Z,k[K+1]);if(E)_[K+1]=E;else{let B=k[K+1],q=z.normal((K+1)/N).direction;_[K+1]={x:B.x+(K?O:J)*q.x,y:B.y+(K?O:J)*q.y}}}),new j(_);return[0,1].forEach(function(K){if(N===2&&!!K)return;let X=k[K+1],F={x:X.x-Z.x,y:X.y-Z.y},H=Q((K+1)/N),E=Math.sqrt(F.x*F.x+F.y*F.y);F.x/=E,F.y/=E,_[K+1]={x:X.x+H*F.x,y:X.y+H*F.y}}),new j(_)}function n(z,G){let N=G.p1.x,Q=G.p1.y,k=-Math.atan2(G.p2.y-Q,G.p2.x-N),J=function(O){return{x:(O.x-N)*Math.cos(k)-(O.y-Q)*Math.sin(k),y:(O.x-N)*Math.sin(k)+(O.y-Q)*Math.cos(k)}};return z.map(J)}function x(z,G,N,Q,k){let J=N-G,O=k-Q,W=z-G,_=W/J;return Q+O*_}class c extends Error{constructor(z){super(z)}}function i(z,G){if(z.min.x<=G.max.x&&G.min.x<=z.max.x&&(z.min.y<=G.max.y&&G.min.y<=z.max.y))return!0;return!1}function D(z,G,N){return Math.abs(z-G)<=(N||0.000001)}function U(z){if(z<0)return-Math.pow(-z,0.3333333333333333);return Math.pow(z,0.3333333333333333)}function A(z){return 0<=z&&z<=1}function W0(z,G,N,Q){let k=3*z-6*G+3*N,J=-3*z+3*G,O=z,W=-z+3*G-3*N+Q;if(D(W,0)){if(D(k,0)){if(D(J,0))return[];return[-O/J].filter(A)}let R=Math.sqrt(J*J-4*k*O),y=2*k;return[(R-J)/y,(-J-R)/y].filter(A)}k/=W,J/=W,O/=W;let _=(3*J-k*k)/3,Z=_/3,K=(2*k*k*k-9*k*J+27*O)/27,X=K/2,F=X*X+Z*Z*Z,H,E,B,q,M;if(F<0){let R=-_/3,y=R*R*R,m=Math.sqrt(y),L=-K/(2*m),s=L<-1?-1:L>1?1:L,T=Math.acos(s),l=U(m),h=2*l;return B=h*Math.cos(T/3)-k/3,q=h*Math.cos((T+2*Math.PI)/3)-k/3,M=h*Math.cos((T+4*Math.PI)/3)-k/3,[B,q,M].filter(A)}if(F===0)return H=X<0?U(-X):-U(X),B=2*H-k/3,q=-H-k/3,[B,q].filter(A);var I=Math.sqrt(F);return H=U(I-X),E=U(I+X),B=H-E-k/3,[B].filter(A)}function V(z,G,N=0.01){let k=[{curve1:{curve:z,startTVal:0,endTVal:1},curve2:{curve:G,startTVal:0,endTVal:1}}],J=[];while(k.length>0){let W=k.length;for(let _=0;_<W;_++){let Z=k.shift();if(Z==null)break;let K=Z.curve1.curve.AABB,X=Z.curve2.curve.AABB,F=i(K,X);if(Z.curve1.curve.fullLength<0.5&&Z.curve2.curve.fullLength<0.5){J.push({intersection:Z.curve1.curve.get(0.5),tVal1:(Z.curve1.startTVal+Z.curve1.endTVal)*0.5,tVal2:(Z.curve2.startTVal+Z.curve2.endTVal)*0.5});continue}if(F){let[H,E]=Z.curve1.curve.split(0.5),[B,q]=Z.curve2.curve.split(0.5);k.push({curve1:{curve:new j(H),startTVal:Z.curve1.startTVal,endTVal:Z.curve1.startTVal+(Z.curve1.endTVal-Z.curve1.startTVal)*0.5},curve2:{curve:new j(B),startTVal:Z.curve2.startTVal,endTVal:Z.curve2.startTVal+(Z.curve2.endTVal-Z.curve2.startTVal)*0.5}}),k.push({curve1:{curve:new j(H),startTVal:Z.curve1.startTVal,endTVal:Z.curve1.startTVal+(Z.curve1.endTVal-Z.curve1.startTVal)*0.5},curve2:{curve:new j(q),startTVal:Z.curve2.startTVal+(Z.curve2.endTVal-Z.curve2.startTVal)*0.5,endTVal:Z.curve2.endTVal}}),k.push({curve1:{curve:new j(E),startTVal:Z.curve1.startTVal+(Z.curve1.endTVal-Z.curve1.startTVal)*0.5,endTVal:Z.curve1.endTVal},curve2:{curve:new j(B),startTVal:Z.curve2.startTVal,endTVal:Z.curve2.startTVal+(Z.curve2.endTVal-Z.curve2.startTVal)*0.5}}),k.push({curve1:{curve:new j(E),startTVal:Z.curve1.startTVal+(Z.curve1.endTVal-Z.curve1.startTVal)*0.5,endTVal:Z.curve1.endTVal},curve2:{curve:new j(q),startTVal:Z.curve2.startTVal+(Z.curve2.endTVal-Z.curve2.startTVal)*0.5,endTVal:Z.curve2.endTVal}})}}}let O=[];J.sort((W,_)=>W.tVal1-_.tVal1);for(let W of J){let _=!1;for(let Z of O){let K=D(W.tVal1,Z.selfT,N),X=D(W.tVal2,Z.otherT,N);if(K&&X){_=!0;break}let F=D(W.tVal1,Z.selfT,N*10),H=D(W.tVal2,Z.otherT,N*10);if(F||H){let E=z.get(W.tVal1),B=G.get(W.tVal2),q=z.get(Z.selfT),M=G.get(Z.otherT),I=$.distanceBetweenPoints(E,q),R=$.distanceBetweenPoints(B,M);if(I<N*100&&R<N*100){_=!0;break}}}if(!_)O.push({selfT:W.tVal1,otherT:W.tVal2})}return O}function w(z,G,N,Q){if(Math.abs(z)<0.00000001){if(z=G,G=N,N=Q,Math.abs(z)<0.00000001){if(z=G,G=N,Math.abs(z)<0.00000001)return[];return[-G/z]}let W=G*G-4*z*N;if(Math.abs(W)<0.00000001)return[-G/(2*z)];else if(W>0)return[(-G+Math.sqrt(W))/(2*z),(-G-Math.sqrt(W))/(2*z)];return[]}let k=(3*z*N-G*G)/(3*z*z),J=(2*G*G*G-9*z*G*N+27*z*z*Q)/(27*z*z*z),O;if(Math.abs(k)<0.00000001)O=[f(-J)];else if(Math.abs(J)<0.00000001)O=[0].concat(k<0?[Math.sqrt(-k),-Math.sqrt(-k)]:[]);else{let W=J*J/4+k*k*k/27;if(Math.abs(W)<0.00000001)O=[-1.5*J/k,3*J/k];else if(W>0){let _=f(-J/2-Math.sqrt(W)),Z=f(-J/2+Math.sqrt(W));O=[_-k/(3*_)]}else{let _=2*Math.sqrt(-k/3),Z=Math.acos(3*J/k/_)/3,K=2*Math.PI/3;O=[_*Math.cos(Z),_*Math.cos(Z-K),_*Math.cos(Z-2*K)]}}for(let W=0;W<O.length;W++)O[W]-=G/(3*z);return O}function f(z){var G=Math.pow(Math.abs(z),0.3333333333333333);return z<0?-G:G}function b(z,G){let N=[...G];while(N.length>1){let Q=N.slice(1);for(let k=0;k<Q.length;k++)Q[k]=$.addVector($.multiplyVectorByScalar(N[k],1-z),$.multiplyVectorByScalar(N[k+1],z));N=Q}return N[0]}function t(z){if(z.getControlPoints().length===4){let k=z.getControlPoints(),J=$.subVector(k[3],k[0]),O=$.subVector(k[1],k[0]),W=$.subVector(k[2],k[0]),_=$.angleFromA2B(J,O),Z=$.angleFromA2B(J,W);if(_>0&&Z<0||_<0&&Z>0)return!1}let G=z.normal(0).direction,N=z.normal(1).direction,Q=G.x*N.x+G.y*N.y;return Math.abs(Math.acos(Q))<Math.PI/3}function p(z,G,N,Q){let k=z.getControlPoints().length-1,J=z.getControlPoints(),O=J.map((W,_)=>(1-_/k)*N+_/k*Q);return new j(J.map((W,_)=>({x:W.x+O[_]*G.x,y:W.y+O[_]*G.y})))}import{PointCal as S}from"@ue-too/math";class z0{startPoint;endPoint;constructor(z,G){this.startPoint=z,this.endPoint=G}getStartPoint(){return this.startPoint}getEndPoint(){return this.endPoint}intersectionWithAnotherLine(z){return G0(this.startPoint,this.endPoint,z.getStartPoint(),z.getEndPoint())}projectPoint(z){return N0(z,this.getStartPoint(),this.getEndPoint())}length(){return S.distanceBetweenPoints(this.startPoint,this.endPoint)}getTranslationRotationToAlginXAxis(){let z=S.subVector({x:0,y:0},this.startPoint),G=S.angleFromA2B(S.subVector(this.endPoint,this.startPoint),{x:1,y:0});return{translation:z,rotationAngle:G}}pointInLine(z){let G=S.unitVectorFromA2B(this.startPoint,this.endPoint),N=S.subVector(z,this.startPoint),Q=S.dotProduct(N,G),k=S.unitVector(N),J=S.distanceBetweenPoints(this.startPoint,this.endPoint)*0.0001;return Q<=S.distanceBetweenPoints(this.startPoint,this.endPoint)&&Q>=0&&Math.abs(k.x-G.x)<0.0001&&Math.abs(k.y-G.y)<0.0001}lerp(z){return S.linearInterpolation(this.startPoint,this.endPoint,z)}}function G0(z,G,N,Q){let k=(Q.x-N.x)*(z.y-N.y)-(Q.y-N.y)*(z.x-N.x),J=(Q.y-N.y)*(G.x-z.x)-(Q.x-N.x)*(G.y-z.y);if(J===0)return{intersects:!1};let O=k/J;if(O>=0&&O<=1)return{intersects:!0,intersection:S.linearInterpolation(z,G,O),offset:O};else return{intersects:!1}}function N0(z,G,N){let Q=S.unitVector(S.subVector(N,G)),k=S.subVector(z,G),J=S.dotProduct(k,Q);if(J<0||J>S.magnitude(S.subVector(N,G)))return{within:!1};return{within:!0,projectionPoint:S.addVector(G,S.multiplyVectorByScalar(Q,J)),offset:J/S.magnitude(S.subVector(N,G))}}import{PointCal as Y}from"@ue-too/math";class d{position;leftHandle;rightHandle;constructor(z,G,N){this.position=z,this.leftHandle=G,this.rightHandle=N}setPosition(z,G,N){let Q=Y.subVector(z,this.position);if(this.position=z,this.leftHandle.position=Y.addVector(this.leftHandle.position,Q),this.rightHandle.position=Y.addVector(this.rightHandle.position,Q),this.leftHandle.type=="VECTOR"&&G){let k=Y.subVector(G.getPosition(),this.position);if(k=Y.multiplyVectorByScalar(k,0.3333333333333333),this.leftHandle.position=Y.addVector(this.position,k),this.rightHandle.type=="ALIGNED"){let J=Y.subVector(this.rightHandle.position,this.position),O=Y.magnitude(J),W=Y.unitVectorFromA2B(this.leftHandle.position,this.position);this.rightHandle.position=Y.addVector(this.position,Y.multiplyVectorByScalar(W,O))}}if(this.rightHandle.type=="VECTOR"&&N){let k=Y.subVector(N.getPosition(),this.position);if(k=Y.multiplyVectorByScalar(k,0.3333333333333333),this.rightHandle.position=Y.addVector(this.position,k),this.leftHandle.type=="ALIGNED"){let J=Y.distanceBetweenPoints(this.leftHandle.position,this.position),O=Y.subVector(this.position,this.rightHandle.position);this.leftHandle.position=Y.addVector(this.position,Y.multiplyVectorByScalar(O,J))}}if(G!==void 0&&G.getRightHandle().type=="VECTOR"){let k=Y.subVector(this.position,G.getPosition());k=Y.multiplyVectorByScalar(k,0.3333333333333333),G.setRightHandlePosition(Y.addVector(G.getPosition(),k))}if(N!==void 0&&N.getLeftHandle().type=="VECTOR"){let k=Y.subVector(this.position,N.getPosition());k=Y.multiplyVectorByScalar(k,0.3333333333333333),N.setLeftHandlePosition(Y.addVector(N.getPosition(),k))}}getPosition(){return this.position}setLeftHandleTypeVector(z){if(this.rightHandle.type!="VECTOR")this.rightHandle.type="FREE";let G;if(z==null)G=Y.subVector(this.leftHandle.position,this.position);else G=Y.subVector(z.getPosition(),this.position),G=Y.multiplyVectorByScalar(G,0.3333333333333333);this.leftHandle.position=Y.addVector(this.position,G)}setLeftHandleTypeAligned(){if(this.leftHandle.type="ALIGNED",this.rightHandle.type=="VECTOR"){let z=Y.unitVectorFromA2B(this.rightHandle.position,this.position),G=Y.distanceBetweenPoints(this.position,this.leftHandle.position);this.leftHandle.position=Y.addVector(this.position,Y.multiplyVectorByScalar(z,G))}}setLeftHandleTypeFree(){this.leftHandle.type="FREE"}setRightHandleTypeVector(z){if(this.leftHandle.type!="VECTOR")this.leftHandle.type="FREE";let G;if(z==null)G=Y.subVector(this.rightHandle.position,this.position);else G=Y.subVector(z.getPosition(),this.position),G=Y.multiplyVectorByScalar(G,0.3333333333333333);this.rightHandle.position=Y.addVector(this.position,G)}setRightHandleTypeAligned(){if(this.rightHandle.type="ALIGNED",this.leftHandle.type=="VECTOR"){let z=Y.unitVectorFromA2B(this.leftHandle.position,this.position),G=Y.distanceBetweenPoints(this.position,this.rightHandle.position);this.rightHandle.position=Y.addVector(this.position,Y.multiplyVectorByScalar(z,G))}}setRightHandleTypeFree(){this.rightHandle.type="FREE"}setLeftHandlePosition(z){switch(this.leftHandle.type){case"ALIGNED":if(this.rightHandle.type=="VECTOR"){let N=Y.subVector(z,this.position),Q=Y.unitVectorFromA2B(this.rightHandle.position,this.position),k=Y.dotProduct(N,Q),J=Y.multiplyVectorByScalar(Q,k);this.leftHandle.position=Y.addVector(this.position,J)}else if(this.rightHandle.type=="ALIGNED"){this.leftHandle.position=z;let N=Y.distanceBetweenPoints(this.rightHandle.position,this.position),Q=Y.unitVectorFromA2B(this.leftHandle.position,this.position),k=Y.multiplyVectorByScalar(Q,N);this.rightHandle.position=Y.addVector(k,this.position)}else this.leftHandle.position=z;break;case"FREE":this.leftHandle.position=z;break;case"VECTOR":break;default:throw Error("Unknown left handle type for control point")}}setRightHandlePosition(z){switch(this.rightHandle.type){case"ALIGNED":if(this.leftHandle.type=="VECTOR"){let N=Y.subVector(z,this.position),Q=Y.unitVectorFromA2B(this.leftHandle.position,this.position),k=Y.dotProduct(N,Q),J=Y.multiplyVectorByScalar(Q,k);this.rightHandle.position=Y.addVector(this.position,J)}else if(this.rightHandle.type=="ALIGNED"){this.rightHandle.position=z;let N=Y.distanceBetweenPoints(this.leftHandle.position,this.position),Q=Y.unitVectorFromA2B(this.rightHandle.position,this.position),k=Y.multiplyVectorByScalar(Q,N);this.leftHandle.position=Y.addVector(k,this.position)}else this.rightHandle.position=z;break;case"FREE":this.rightHandle.position=z;break;case"VECTOR":break;default:throw Error("Unknown left handle type for control point")}}getLeftHandle(){return this.leftHandle}getRightHandle(){return this.rightHandle}}class Q0{controlPoints;constructor(z=[]){this.controlPoints=z}getControlPoints(){return this.controlPoints}appendControlPoint(z){let G=Y.addVector(z,{x:-100,y:0}),N=Y.addVector(z,{x:100,y:0}),J=new d(z,{position:G,type:"FREE"},{position:N,type:"FREE"});this.controlPoints.push(J)}setLeftHandlePositionOfControlPoint(z,G){if(z>=this.controlPoints.length||z<0)return;this.controlPoints[z].setLeftHandlePosition(G)}setRightHandlePositionOfControlPoint(z,G){if(z>=this.controlPoints.length||z<0)return;this.controlPoints[z].setRightHandlePosition(G)}setPositionOfControlPoint(z,G){if(z>=this.controlPoints.length||z<0)return;let N=void 0,Q=void 0;if(z+1<this.controlPoints.length)Q=this.controlPoints[z+1];if(z-1>=0)N=this.controlPoints[z-1];this.controlPoints[z].setPosition(G,N,Q)}}class k0{lines;constructor(z){this.lines=z}append(z){this.lines.push(z)}clear(){this.lines=[]}prepend(z){this.lines.unshift(z)}getLines(){return this.lines}getLength(){let z=0;return this.lines.forEach((G)=>{z+=G.length()}),z}getPercentages(){let z=this.getLength(),G=0,N=[];return this.lines.forEach((Q)=>{let J=Q.length()/z,O=G;G+=J;let W=G;N.push({start:O,end:W})}),N[N.length-1].end=1,N}getPointByPercentage(z){if(z<0||z>1)throw Error("Percentage must be between 0 and 1");let G=this.getPercentages(),N=0,Q=G.length-1;while(N<=Q){let W=Math.floor((N+Q)/2);if(z<G[W].end)Q=W-1;else if(z>G[W].end)N=W+1;else{N=W;break}}let k=this.lines[N],J=G[N],O=(z-J.start)/(J.end-J.start);return k.lerp(O)}}export{w as solveCubic,r as reduce,N0 as projectPointOntoLine,O0 as offset2,u as offset,G0 as getLineIntersection,V as getIntersectionsBetweenCurves,W0 as getCubicRoots,U as cuberoot2,f as cuberoot,b as computeWithControlPoints,D as approximately,A as accept,c as TValOutofBoundError,k0 as Path,z0 as Line,d as ControlPoint,Q0 as CompositeBCurve,j as BCurve,i as AABBIntersects};
|
|
55
2
|
|
|
56
|
-
|
|
57
|
-
controlPoints;
|
|
58
|
-
dControlPoints = [];
|
|
59
|
-
arcLengthLUT = { controlPoints: [], arcLengthLUT: [] };
|
|
60
|
-
_fullLength;
|
|
61
|
-
lengthCache = new Map;
|
|
62
|
-
getCacheStats() {
|
|
63
|
-
return {
|
|
64
|
-
size: this.lengthCache.size,
|
|
65
|
-
hitRate: 0
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
preWarmCache(steps = 100) {
|
|
69
|
-
const tSteps = 1 / steps;
|
|
70
|
-
for (let tVal = 0;tVal <= 1; tVal += tSteps) {
|
|
71
|
-
this.lengthAtT(tVal);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
clearCache() {
|
|
75
|
-
this.lengthCache.clear();
|
|
76
|
-
}
|
|
77
|
-
constructor(controlPoints) {
|
|
78
|
-
this.controlPoints = controlPoints;
|
|
79
|
-
this.dControlPoints = this.getDerivativeControlPoints(this.controlPoints);
|
|
80
|
-
this._fullLength = this.calculateFullLength();
|
|
81
|
-
this.arcLengthLUT = { controlPoints: [], arcLengthLUT: [] };
|
|
82
|
-
this.clearCache();
|
|
83
|
-
}
|
|
84
|
-
getPointbyPercentage(percentage) {
|
|
85
|
-
let controlPointsChangedSinceLastArcLengthLUT = this.arcLengthLUT.controlPoints.length != this.controlPoints.length;
|
|
86
|
-
controlPointsChangedSinceLastArcLengthLUT = controlPointsChangedSinceLastArcLengthLUT || this.arcLengthLUT.controlPoints.reduce((prevVal, curVal, index) => {
|
|
87
|
-
return prevVal || !PointCal.isEqual(curVal, this.controlPoints[index]);
|
|
88
|
-
}, false);
|
|
89
|
-
let points = [];
|
|
90
|
-
if (controlPointsChangedSinceLastArcLengthLUT) {
|
|
91
|
-
this.arcLengthLUT = this.getArcLengthLUT(1000);
|
|
92
|
-
}
|
|
93
|
-
points = [...this.arcLengthLUT.arcLengthLUT.map((item) => item.length)];
|
|
94
|
-
let targetLength = percentage * this.fullLength;
|
|
95
|
-
let low = 0;
|
|
96
|
-
let high = points.length - 1;
|
|
97
|
-
while (low <= high) {
|
|
98
|
-
let mid = Math.floor((low + high) / 2);
|
|
99
|
-
if (points[mid] == targetLength) {
|
|
100
|
-
return this.get((mid + 1) / points.length);
|
|
101
|
-
} else if (points[mid] < targetLength) {
|
|
102
|
-
low = mid + 1;
|
|
103
|
-
} else {
|
|
104
|
-
high = mid - 1;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return low >= points.length ? this.get(1) : this.get((low + 1) / points.length);
|
|
108
|
-
}
|
|
109
|
-
getDerivativeControlPoints(controlPoints) {
|
|
110
|
-
const derivativeControlPoints = [];
|
|
111
|
-
for (let index = 1;index < controlPoints.length; index++) {
|
|
112
|
-
derivativeControlPoints.push(PointCal.multiplyVectorByScalar(PointCal.subVector(controlPoints[index], controlPoints[index - 1]), controlPoints.length - 1));
|
|
113
|
-
}
|
|
114
|
-
return derivativeControlPoints;
|
|
115
|
-
}
|
|
116
|
-
validateTVal(tVal) {
|
|
117
|
-
if (tVal > 1 || tVal < 0) {
|
|
118
|
-
throw new TValOutofBoundError("tVal is greater than 1 or less than 0");
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
getControlPoints() {
|
|
122
|
-
return this.controlPoints;
|
|
123
|
-
}
|
|
124
|
-
setControlPoints(controlPoints) {
|
|
125
|
-
this.controlPoints = controlPoints;
|
|
126
|
-
this.dControlPoints = this.getDerivativeControlPoints(this.controlPoints);
|
|
127
|
-
this._fullLength = this.calculateFullLength();
|
|
128
|
-
this.arcLengthLUT = { controlPoints: [], arcLengthLUT: [] };
|
|
129
|
-
this.clearCache();
|
|
130
|
-
}
|
|
131
|
-
setControlPointAtIndex(index, newPoint) {
|
|
132
|
-
if (index < 0 || index >= this.controlPoints.length) {
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
this.controlPoints[index] = newPoint;
|
|
136
|
-
this.dControlPoints = this.getDerivativeControlPoints(this.controlPoints);
|
|
137
|
-
this._fullLength = this.calculateFullLength();
|
|
138
|
-
this.arcLengthLUT = { controlPoints: [], arcLengthLUT: [] };
|
|
139
|
-
this.clearCache();
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
compute(tVal) {
|
|
143
|
-
this.validateTVal(tVal);
|
|
144
|
-
let points = this.controlPoints;
|
|
145
|
-
while (points.length > 1) {
|
|
146
|
-
let lowerLevelPoints = points.slice(1);
|
|
147
|
-
for (let index = 0;index < lowerLevelPoints.length; index++) {
|
|
148
|
-
lowerLevelPoints[index] = PointCal.addVector(PointCal.multiplyVectorByScalar(points[index], 1 - tVal), PointCal.multiplyVectorByScalar(points[index + 1], tVal));
|
|
149
|
-
}
|
|
150
|
-
points = lowerLevelPoints;
|
|
151
|
-
}
|
|
152
|
-
return points[0];
|
|
153
|
-
}
|
|
154
|
-
get(tVal) {
|
|
155
|
-
this.validateTVal(tVal);
|
|
156
|
-
if (this.controlPoints.length == 3) {
|
|
157
|
-
let firstTerm = PointCal.multiplyVectorByScalar(this.controlPoints[0], (1 - tVal) * (1 - tVal));
|
|
158
|
-
let secondTerm = PointCal.multiplyVectorByScalar(this.controlPoints[1], 2 * (1 - tVal) * tVal);
|
|
159
|
-
let thirdTerm = PointCal.multiplyVectorByScalar(this.controlPoints[2], tVal * tVal);
|
|
160
|
-
let res = PointCal.addVector(PointCal.addVector(firstTerm, secondTerm), thirdTerm);
|
|
161
|
-
return res;
|
|
162
|
-
}
|
|
163
|
-
if (this.controlPoints.length == 4) {
|
|
164
|
-
let firstTerm = PointCal.multiplyVectorByScalar(this.controlPoints[0], (1 - tVal) * (1 - tVal) * (1 - tVal));
|
|
165
|
-
let secondTerm = PointCal.multiplyVectorByScalar(this.controlPoints[1], 3 * (1 - tVal) * (1 - tVal) * tVal);
|
|
166
|
-
let thirdTerm = PointCal.multiplyVectorByScalar(this.controlPoints[2], 3 * (1 - tVal) * tVal * tVal);
|
|
167
|
-
let forthTerm = PointCal.multiplyVectorByScalar(this.controlPoints[3], tVal * tVal * tVal);
|
|
168
|
-
let res = PointCal.addVector(PointCal.addVector(firstTerm, secondTerm), PointCal.addVector(thirdTerm, forthTerm));
|
|
169
|
-
return res;
|
|
170
|
-
}
|
|
171
|
-
return this.compute(tVal);
|
|
172
|
-
}
|
|
173
|
-
getLUT(steps = 100) {
|
|
174
|
-
const stepSpan = 1 / steps;
|
|
175
|
-
const res = [];
|
|
176
|
-
let tVal = 0;
|
|
177
|
-
res.push(this.get(tVal));
|
|
178
|
-
for (let index = 0;index < steps; index += 1) {
|
|
179
|
-
tVal += stepSpan;
|
|
180
|
-
if (tVal > 1 && tVal - stepSpan < 1 || index == steps - 1) {
|
|
181
|
-
tVal = 1;
|
|
182
|
-
}
|
|
183
|
-
res.push(this.get(tVal));
|
|
184
|
-
}
|
|
185
|
-
return res;
|
|
186
|
-
}
|
|
187
|
-
getLUTWithTVal(steps) {
|
|
188
|
-
if (steps == undefined) {
|
|
189
|
-
steps = 100;
|
|
190
|
-
}
|
|
191
|
-
const stepSpan = 1 / steps;
|
|
192
|
-
const res = [];
|
|
193
|
-
let tVal = 0;
|
|
194
|
-
res.push({ point: this.get(tVal), tVal });
|
|
195
|
-
for (let index = 0;index < steps; index += 1) {
|
|
196
|
-
tVal += stepSpan;
|
|
197
|
-
if (tVal > 1 && tVal - stepSpan < 1 || index == steps - 1) {
|
|
198
|
-
tVal = 1;
|
|
199
|
-
}
|
|
200
|
-
res.push({ point: this.get(tVal), tVal });
|
|
201
|
-
}
|
|
202
|
-
return res;
|
|
203
|
-
}
|
|
204
|
-
get fullLength() {
|
|
205
|
-
return this._fullLength;
|
|
206
|
-
}
|
|
207
|
-
calculateFullLength() {
|
|
208
|
-
return this.lengthAtT(1);
|
|
209
|
-
}
|
|
210
|
-
lengthAtT(tVal) {
|
|
211
|
-
this.validateTVal(tVal);
|
|
212
|
-
const cacheKey = Math.round(tVal * 1e6) / 1e6;
|
|
213
|
-
if (this.lengthCache.has(cacheKey)) {
|
|
214
|
-
return this.lengthCache.get(cacheKey);
|
|
215
|
-
}
|
|
216
|
-
const z = tVal / 2, len = T.length;
|
|
217
|
-
let sum = 0;
|
|
218
|
-
for (let i = 0, t;i < len; i++) {
|
|
219
|
-
t = z * T[i] + z;
|
|
220
|
-
sum += C[i] * PointCal.magnitude(this.derivative(t));
|
|
221
|
-
}
|
|
222
|
-
const result = z * sum;
|
|
223
|
-
this.lengthCache.set(cacheKey, result);
|
|
224
|
-
return result;
|
|
225
|
-
}
|
|
226
|
-
derivative(tVal) {
|
|
227
|
-
return computeWithControlPoints(tVal, this.dControlPoints);
|
|
228
|
-
}
|
|
229
|
-
derivativeNormalized(tVal) {
|
|
230
|
-
return PointCal.unitVector(computeWithControlPoints(tVal, this.dControlPoints));
|
|
231
|
-
}
|
|
232
|
-
getArcLengthLUT(steps = 50) {
|
|
233
|
-
const controlPointsChanged = this.arcLengthLUT.controlPoints.length !== this.controlPoints.length || this.arcLengthLUT.controlPoints.some((cp, index) => !PointCal.isEqual(cp, this.controlPoints[index]));
|
|
234
|
-
if (controlPointsChanged || this.arcLengthLUT.arcLengthLUT.length === 0) {
|
|
235
|
-
this.clearCache();
|
|
236
|
-
let res = [];
|
|
237
|
-
let tSteps = 1 / steps;
|
|
238
|
-
for (let tVal = 0;tVal <= 1; tVal += tSteps) {
|
|
239
|
-
res.push({ tVal, length: this.lengthAtT(tVal) });
|
|
240
|
-
}
|
|
241
|
-
this.arcLengthLUT = { controlPoints: [...this.controlPoints], arcLengthLUT: res };
|
|
242
|
-
}
|
|
243
|
-
return this.arcLengthLUT;
|
|
244
|
-
}
|
|
245
|
-
splitIntoCurves(tVal) {
|
|
246
|
-
const res = this.split(tVal);
|
|
247
|
-
return [new BCurve(res[0]), new BCurve(res[1])];
|
|
248
|
-
}
|
|
249
|
-
split(tVal) {
|
|
250
|
-
this.validateTVal(tVal);
|
|
251
|
-
if (this.controlPoints.length == 3) {
|
|
252
|
-
let newControlPoint12 = this.controlPoints[0];
|
|
253
|
-
let newControlPoint22 = PointCal.subVector(PointCal.multiplyVectorByScalar(this.controlPoints[1], tVal), PointCal.multiplyVectorByScalar(this.controlPoints[0], tVal - 1));
|
|
254
|
-
let newControlPoint32 = PointCal.subVector(PointCal.multiplyVectorByScalar(this.controlPoints[2], tVal * tVal), PointCal.multiplyVectorByScalar(this.controlPoints[1], 2 * tVal * (tVal - 1)));
|
|
255
|
-
newControlPoint32 = PointCal.addVector(newControlPoint32, PointCal.multiplyVectorByScalar(this.controlPoints[0], (tVal - 1) * (tVal - 1)));
|
|
256
|
-
let newControlPoint42 = PointCal.subVector(PointCal.multiplyVectorByScalar(this.controlPoints[2], tVal), PointCal.multiplyVectorByScalar(this.controlPoints[1], tVal - 1));
|
|
257
|
-
let newControlPoint52 = this.controlPoints[2];
|
|
258
|
-
return [[newControlPoint12, newControlPoint22, newControlPoint32], [newControlPoint32, newControlPoint42, newControlPoint52]];
|
|
259
|
-
}
|
|
260
|
-
let newControlPoint1 = this.controlPoints[0];
|
|
261
|
-
let newControlPoint2 = PointCal.subVector(PointCal.multiplyVectorByScalar(this.controlPoints[1], tVal), PointCal.multiplyVectorByScalar(this.controlPoints[0], tVal - 1));
|
|
262
|
-
let newControlPoint3 = PointCal.addVector(PointCal.multiplyVectorByScalar(this.controlPoints[2], tVal * tVal), PointCal.addVector(PointCal.multiplyVectorByScalar(this.controlPoints[1], -(2 * tVal * (tVal - 1))), PointCal.multiplyVectorByScalar(this.controlPoints[0], (tVal - 1) * (tVal - 1))));
|
|
263
|
-
let term1 = PointCal.multiplyVectorByScalar(this.controlPoints[3], tVal * tVal * tVal);
|
|
264
|
-
let term2 = PointCal.multiplyVectorByScalar(this.controlPoints[2], -(3 * tVal * tVal * (tVal - 1)));
|
|
265
|
-
let term3 = PointCal.multiplyVectorByScalar(this.controlPoints[1], 3 * tVal * (tVal - 1) * (tVal - 1));
|
|
266
|
-
let term4 = PointCal.multiplyVectorByScalar(this.controlPoints[0], -((tVal - 1) * (tVal - 1) * (tVal - 1)));
|
|
267
|
-
let newControlPoint4 = PointCal.addVector(term4, PointCal.addVector(term3, PointCal.addVector(term1, term2)));
|
|
268
|
-
let newControlPoint5 = PointCal.addVector(PointCal.addVector(PointCal.multiplyVectorByScalar(this.controlPoints[3], tVal * tVal), PointCal.multiplyVectorByScalar(this.controlPoints[2], -(2 * tVal * (tVal - 1)))), PointCal.multiplyVectorByScalar(this.controlPoints[1], (tVal - 1) * (tVal - 1)));
|
|
269
|
-
let newControlPoint6 = PointCal.addVector(PointCal.multiplyVectorByScalar(this.controlPoints[3], tVal), PointCal.multiplyVectorByScalar(this.controlPoints[2], -(tVal - 1)));
|
|
270
|
-
let newControlPoint7 = this.controlPoints[3];
|
|
271
|
-
return [[newControlPoint1, newControlPoint2, newControlPoint3, newControlPoint4], [newControlPoint4, newControlPoint5, newControlPoint6, newControlPoint7]];
|
|
272
|
-
}
|
|
273
|
-
splitIn3WithControlPoints(tVal, tVal2) {
|
|
274
|
-
if (tVal2 < tVal) {
|
|
275
|
-
console.warn("tVal2 is less than tVal, swapping them");
|
|
276
|
-
[tVal, tVal2] = [tVal2, tVal];
|
|
277
|
-
}
|
|
278
|
-
const firstSplit = this.split(tVal);
|
|
279
|
-
const secondHalf = new BCurve(firstSplit[1]);
|
|
280
|
-
const mappedTVal2 = map(tVal2, tVal, 1, 0, 1);
|
|
281
|
-
const secondSplit = secondHalf.split(mappedTVal2);
|
|
282
|
-
return [firstSplit[0], secondSplit[0], secondSplit[1]];
|
|
283
|
-
}
|
|
284
|
-
splitIn3Curves(tVal, tVal2) {
|
|
285
|
-
if (tVal2 < tVal) {
|
|
286
|
-
console.warn("tVal2 is less than tVal, swapping them");
|
|
287
|
-
[tVal, tVal2] = [tVal2, tVal];
|
|
288
|
-
}
|
|
289
|
-
const firstSplit = this.split(tVal);
|
|
290
|
-
const secondHalf = new BCurve(firstSplit[1]);
|
|
291
|
-
const mappedTVal2 = map(tVal2, tVal, 1, 0, 1);
|
|
292
|
-
const secondSplit = secondHalf.split(mappedTVal2);
|
|
293
|
-
return [new BCurve(firstSplit[0]), new BCurve(secondSplit[0]), new BCurve(secondSplit[1])];
|
|
294
|
-
}
|
|
295
|
-
splitAndTakeMidCurve(tVal, tVal2) {
|
|
296
|
-
const [firstSplit, secondSplit, thirdSplit] = this.splitIn3Curves(tVal, tVal2);
|
|
297
|
-
return secondSplit;
|
|
298
|
-
}
|
|
299
|
-
getProjection(point) {
|
|
300
|
-
const threshold = 0.00001;
|
|
301
|
-
let distance = Number.MAX_VALUE;
|
|
302
|
-
let preliminaryProjectionTVal = 0;
|
|
303
|
-
let preliminaryProjectionPoint = this.get(0);
|
|
304
|
-
let preliminaryProjectionIndex = 0;
|
|
305
|
-
const LUT = this.getLUTWithTVal(500);
|
|
306
|
-
LUT.forEach((curvePoint, index) => {
|
|
307
|
-
const curDistance = PointCal.distanceBetweenPoints(curvePoint.point, point);
|
|
308
|
-
if (curDistance < distance) {
|
|
309
|
-
distance = curDistance;
|
|
310
|
-
preliminaryProjectionPoint = { ...curvePoint.point };
|
|
311
|
-
preliminaryProjectionTVal = curvePoint.tVal;
|
|
312
|
-
preliminaryProjectionIndex = index;
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
let low = LUT[preliminaryProjectionIndex].tVal;
|
|
316
|
-
let high = LUT[preliminaryProjectionIndex].tVal;
|
|
317
|
-
if (preliminaryProjectionIndex < LUT.length - 1) {
|
|
318
|
-
high = LUT[preliminaryProjectionIndex + 1].tVal;
|
|
319
|
-
}
|
|
320
|
-
if (preliminaryProjectionIndex > 0) {
|
|
321
|
-
low = LUT[preliminaryProjectionIndex - 1].tVal;
|
|
322
|
-
}
|
|
323
|
-
while (low < high && high - low > threshold) {
|
|
324
|
-
let mid = low + (high - low) / 2;
|
|
325
|
-
let halfSpan = mid - low;
|
|
326
|
-
let lowMidMid = mid + halfSpan / 2;
|
|
327
|
-
let highMidMid = mid + halfSpan / 2;
|
|
328
|
-
let prevDist = distance;
|
|
329
|
-
if (lowMidMid <= 1 && lowMidMid >= 0) {
|
|
330
|
-
let curDist = PointCal.distanceBetweenPoints(this.get(lowMidMid), point);
|
|
331
|
-
if (curDist < distance) {
|
|
332
|
-
distance = curDist;
|
|
333
|
-
preliminaryProjectionPoint = this.get(lowMidMid);
|
|
334
|
-
preliminaryProjectionTVal = lowMidMid;
|
|
335
|
-
high = lowMidMid + halfSpan / 2;
|
|
336
|
-
low = lowMidMid - halfSpan / 2;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
if (highMidMid <= 1 && highMidMid >= 0) {
|
|
340
|
-
let curDist = PointCal.distanceBetweenPoints(this.get(highMidMid), point);
|
|
341
|
-
if (curDist < distance) {
|
|
342
|
-
distance = curDist;
|
|
343
|
-
preliminaryProjectionPoint = this.get(highMidMid);
|
|
344
|
-
preliminaryProjectionTVal = highMidMid;
|
|
345
|
-
high = highMidMid + halfSpan / 2;
|
|
346
|
-
low = highMidMid - halfSpan / 2;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
if (prevDist == distance) {
|
|
350
|
-
break;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
return { projection: preliminaryProjectionPoint, tVal: preliminaryProjectionTVal };
|
|
354
|
-
}
|
|
355
|
-
findArcs(errorThreshold) {
|
|
356
|
-
let low = 0;
|
|
357
|
-
const res = [];
|
|
358
|
-
while (low < 1) {
|
|
359
|
-
let loopRes = this.findArcStartingAt(errorThreshold, low);
|
|
360
|
-
if (loopRes == null || loopRes.arc == undefined) {
|
|
361
|
-
break;
|
|
362
|
-
}
|
|
363
|
-
res.push(loopRes.arc);
|
|
364
|
-
low = loopRes.arc.endT;
|
|
365
|
-
if (low >= 1) {
|
|
366
|
-
break;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
return res;
|
|
370
|
-
}
|
|
371
|
-
findArcStartingAt(errorThreshold, low) {
|
|
372
|
-
let high = 1;
|
|
373
|
-
let mid = low + (high - low) / 2;
|
|
374
|
-
let prevArc = { good: false };
|
|
375
|
-
let count = 0;
|
|
376
|
-
while (true) {
|
|
377
|
-
count++;
|
|
378
|
-
mid = low + (high - low) / 2;
|
|
379
|
-
if (high > 1 || mid > 1) {
|
|
380
|
-
if (prevArc.good) {
|
|
381
|
-
return prevArc;
|
|
382
|
-
} else {
|
|
383
|
-
return null;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
const lowPoint = this.get(low);
|
|
387
|
-
const highPoint = this.get(high);
|
|
388
|
-
const midPoint = this.get(mid);
|
|
389
|
-
const fitArcRes = this.fitArc(lowPoint, highPoint, midPoint);
|
|
390
|
-
if (!fitArcRes.exists || fitArcRes.center == null || fitArcRes.radius == null) {
|
|
391
|
-
return null;
|
|
392
|
-
}
|
|
393
|
-
const n = high - mid;
|
|
394
|
-
const e1 = mid - n / 2;
|
|
395
|
-
const e2 = mid + n / 2;
|
|
396
|
-
const checkPoint1 = this.get(e1);
|
|
397
|
-
const checkPoint2 = this.get(e2);
|
|
398
|
-
const checkRadius = PointCal.distanceBetweenPoints(checkPoint1, fitArcRes.center);
|
|
399
|
-
const checkRadius2 = PointCal.distanceBetweenPoints(checkPoint2, fitArcRes.center);
|
|
400
|
-
if (Math.abs(checkRadius - fitArcRes.radius) > errorThreshold || Math.abs(checkRadius2 - fitArcRes.radius) > errorThreshold) {
|
|
401
|
-
if (prevArc.good == true) {
|
|
402
|
-
return prevArc;
|
|
403
|
-
}
|
|
404
|
-
prevArc.good = false;
|
|
405
|
-
high = mid;
|
|
406
|
-
} else {
|
|
407
|
-
prevArc.good = true;
|
|
408
|
-
if (fitArcRes.startPoint !== undefined && fitArcRes.endPoint !== undefined) {
|
|
409
|
-
prevArc.arc = { center: fitArcRes.center, radius: fitArcRes.radius, startPoint: fitArcRes.startPoint, endPoint: fitArcRes.endPoint, startT: low, endT: high };
|
|
410
|
-
}
|
|
411
|
-
high = high + (mid - low);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
fitArc(startPoint, endPoint, midPoint) {
|
|
416
|
-
const M11 = [[startPoint.x, startPoint.y, 1], [midPoint.x, midPoint.y, 1], [endPoint.x, endPoint.y, 1]];
|
|
417
|
-
if (this.determinant3by3(M11) == 0) {
|
|
418
|
-
return { exists: false };
|
|
419
|
-
}
|
|
420
|
-
const M12 = [
|
|
421
|
-
[startPoint.x * startPoint.x + startPoint.y * startPoint.y, startPoint.y, 1],
|
|
422
|
-
[midPoint.x * midPoint.x + midPoint.y * midPoint.y, midPoint.y, 1],
|
|
423
|
-
[endPoint.x * endPoint.x + endPoint.y * endPoint.y, endPoint.y, 1]
|
|
424
|
-
];
|
|
425
|
-
const M13 = [
|
|
426
|
-
[startPoint.x * startPoint.x + startPoint.y * startPoint.y, startPoint.x, 1],
|
|
427
|
-
[midPoint.x * midPoint.x + midPoint.y * midPoint.y, midPoint.x, 1],
|
|
428
|
-
[endPoint.x * endPoint.x + endPoint.y * endPoint.y, endPoint.x, 1]
|
|
429
|
-
];
|
|
430
|
-
const M14 = [
|
|
431
|
-
[startPoint.x * startPoint.x + startPoint.y * startPoint.y, startPoint.x, startPoint.y],
|
|
432
|
-
[midPoint.x * midPoint.x + midPoint.y * midPoint.y, midPoint.x, midPoint.y],
|
|
433
|
-
[endPoint.x * endPoint.x + endPoint.y * endPoint.y, endPoint.x, endPoint.y]
|
|
434
|
-
];
|
|
435
|
-
const centerX = 1 / 2 * (this.determinant3by3(M12) / this.determinant3by3(M11));
|
|
436
|
-
const centerY = -1 / 2 * (this.determinant3by3(M13) / this.determinant3by3(M11));
|
|
437
|
-
const radius = Math.sqrt(centerX * centerX + centerY * centerY + this.determinant3by3(M14) / this.determinant3by3(M11));
|
|
438
|
-
return { exists: true, center: { x: centerX, y: centerY }, radius, startPoint, endPoint };
|
|
439
|
-
}
|
|
440
|
-
determinant3by3(matrix) {
|
|
441
|
-
const a = matrix[0][0];
|
|
442
|
-
const b = matrix[0][1];
|
|
443
|
-
const c = matrix[0][2];
|
|
444
|
-
const d = matrix[1][0];
|
|
445
|
-
const e = matrix[1][1];
|
|
446
|
-
const f = matrix[1][2];
|
|
447
|
-
const g = matrix[2][0];
|
|
448
|
-
const h = matrix[2][1];
|
|
449
|
-
const i = matrix[2][2];
|
|
450
|
-
return a * (e * i - f * h) - b * (d * i - g * f) + c * (d * h - e * g);
|
|
451
|
-
}
|
|
452
|
-
curvature(tVal) {
|
|
453
|
-
const derivative = computeWithControlPoints(tVal, this.dControlPoints);
|
|
454
|
-
const secondDerivative = computeWithControlPoints(tVal, this.getDerivativeControlPoints(this.dControlPoints));
|
|
455
|
-
const numerator = derivative.x * secondDerivative.y - secondDerivative.x * derivative.y;
|
|
456
|
-
const denominator = Math.pow(derivative.x * derivative.x + derivative.y * derivative.y, 3 / 2);
|
|
457
|
-
if (denominator == 0)
|
|
458
|
-
return NaN;
|
|
459
|
-
return numerator / denominator;
|
|
460
|
-
}
|
|
461
|
-
secondDerivative(tVal) {
|
|
462
|
-
return computeWithControlPoints(tVal, this.getDerivativeControlPoints(this.dControlPoints));
|
|
463
|
-
}
|
|
464
|
-
getCoefficientOfTTerms() {
|
|
465
|
-
return this.getCoefficientOfTTermsWithControlPoints(this.controlPoints);
|
|
466
|
-
}
|
|
467
|
-
getDerivativeCoefficients() {
|
|
468
|
-
return this.getCoefficientOfTTermsWithControlPoints(this.dControlPoints);
|
|
469
|
-
}
|
|
470
|
-
getCoefficientOfTTermsWithControlPoints(controlPoints) {
|
|
471
|
-
const terms = [];
|
|
472
|
-
let matrix = [];
|
|
473
|
-
if (controlPoints.length == 3) {
|
|
474
|
-
matrix = [[1, 0, 0], [-2, 2, 0], [1, -2, 1]];
|
|
475
|
-
} else if (controlPoints.length == 4) {
|
|
476
|
-
matrix = [[1, 0, 0, 0], [-3, 3, 0, 0], [3, -6, 3, 0], [-1, 3, -3, 1]];
|
|
477
|
-
} else if (controlPoints.length == 2) {
|
|
478
|
-
matrix = [[1, 0], [-1, 1]];
|
|
479
|
-
} else {
|
|
480
|
-
throw new Error("number of control points is wrong");
|
|
481
|
-
}
|
|
482
|
-
for (let index = 0;index < controlPoints.length; index++) {
|
|
483
|
-
terms.push(controlPoints.reduce((prevVal, curVal, jindex) => {
|
|
484
|
-
return { x: prevVal.x + matrix[index][jindex] * curVal.x, y: prevVal.y + matrix[index][jindex] * curVal.y };
|
|
485
|
-
}, { x: 0, y: 0 }));
|
|
486
|
-
}
|
|
487
|
-
return terms;
|
|
488
|
-
}
|
|
489
|
-
getControlPointsAlignedWithXAxis() {
|
|
490
|
-
const alignedAxis = PointCal.unitVectorFromA2B(this.controlPoints[0], this.controlPoints[this.controlPoints.length - 1]);
|
|
491
|
-
const angle = PointCal.angleFromA2B({ x: 1, y: 0 }, alignedAxis);
|
|
492
|
-
const startingPoint = this.controlPoints[0];
|
|
493
|
-
const res = [{ x: 0, y: 0 }];
|
|
494
|
-
for (let index = 1;index < this.controlPoints.length; index++) {
|
|
495
|
-
const vector = PointCal.subVector(this.controlPoints[index], startingPoint);
|
|
496
|
-
const rotatedVector = PointCal.rotatePoint(vector, -angle);
|
|
497
|
-
res.push(rotatedVector);
|
|
498
|
-
}
|
|
499
|
-
return res;
|
|
500
|
-
}
|
|
501
|
-
getExtrema() {
|
|
502
|
-
const res = { x: [], y: [] };
|
|
503
|
-
const derivativeCoefficients = this.getDerivativeCoefficients();
|
|
504
|
-
let xCoefficients = [0, 0, 0, 0];
|
|
505
|
-
let yCoefficients = [0, 0, 0, 0];
|
|
506
|
-
derivativeCoefficients.forEach((coefficient, index) => {
|
|
507
|
-
xCoefficients[3 - index] = coefficient.x;
|
|
508
|
-
yCoefficients[3 - index] = coefficient.y;
|
|
509
|
-
});
|
|
510
|
-
const xRoots = solveCubic(xCoefficients[0], xCoefficients[1], xCoefficients[2], xCoefficients[3]);
|
|
511
|
-
const yRoots = solveCubic(yCoefficients[0], yCoefficients[1], yCoefficients[2], yCoefficients[3]);
|
|
512
|
-
xRoots.forEach((root) => {
|
|
513
|
-
if (root >= 0 && root <= 1) {
|
|
514
|
-
res.x.push(root);
|
|
515
|
-
}
|
|
516
|
-
});
|
|
517
|
-
yRoots.forEach((root) => {
|
|
518
|
-
if (root >= 0 && root <= 1) {
|
|
519
|
-
res.y.push(root);
|
|
520
|
-
}
|
|
521
|
-
});
|
|
522
|
-
if (derivativeCoefficients.length >= 3) {
|
|
523
|
-
xCoefficients = [0, 0, 0, 0];
|
|
524
|
-
yCoefficients = [0, 0, 0, 0];
|
|
525
|
-
const secondDerivativeCoefficients = this.getCoefficientOfTTermsWithControlPoints(this.getDerivativeControlPoints(this.dControlPoints));
|
|
526
|
-
secondDerivativeCoefficients.forEach((coefficient, index) => {
|
|
527
|
-
xCoefficients[3 - index] = coefficient.x;
|
|
528
|
-
yCoefficients[3 - index] = coefficient.y;
|
|
529
|
-
});
|
|
530
|
-
const secondXRoots = solveCubic(xCoefficients[0], xCoefficients[1], xCoefficients[2], xCoefficients[3]);
|
|
531
|
-
const secondYRoots = solveCubic(yCoefficients[0], yCoefficients[1], yCoefficients[2], yCoefficients[3]);
|
|
532
|
-
secondXRoots.forEach((root) => {
|
|
533
|
-
if (root >= 0 && root <= 1) {
|
|
534
|
-
res.x.push(root);
|
|
535
|
-
}
|
|
536
|
-
});
|
|
537
|
-
secondYRoots.forEach((root) => {
|
|
538
|
-
if (root >= 0 && root <= 1) {
|
|
539
|
-
res.y.push(root);
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
return res;
|
|
544
|
-
}
|
|
545
|
-
translateRotateControlPoints(translation, rotationAngle) {
|
|
546
|
-
const res = [];
|
|
547
|
-
for (let index = 0;index < this.controlPoints.length; index++) {
|
|
548
|
-
res.push(PointCal.rotatePoint(PointCal.addVector(this.controlPoints[index], translation), rotationAngle));
|
|
549
|
-
}
|
|
550
|
-
return res;
|
|
551
|
-
}
|
|
552
|
-
getLineIntersections(line) {
|
|
553
|
-
const translationRotation = line.getTranslationRotationToAlginXAxis();
|
|
554
|
-
const res = [];
|
|
555
|
-
const alignedControlPoints = this.translateRotateControlPoints(translationRotation.translation, translationRotation.rotationAngle);
|
|
556
|
-
const coefficients = this.getCoefficientOfTTermsWithControlPoints(alignedControlPoints);
|
|
557
|
-
let yCoefficients = [0, 0, 0, 0];
|
|
558
|
-
coefficients.forEach((coefficient, index) => {
|
|
559
|
-
yCoefficients[3 - index] = coefficient.y;
|
|
560
|
-
});
|
|
561
|
-
const yRoots = solveCubic(yCoefficients[0], yCoefficients[1], yCoefficients[2], yCoefficients[3]);
|
|
562
|
-
yRoots.forEach((root) => {
|
|
563
|
-
if (root >= 0 && root <= 1) {
|
|
564
|
-
if (line.pointInLine(this.get(root))) {
|
|
565
|
-
res.push(root);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
});
|
|
569
|
-
return res;
|
|
570
|
-
}
|
|
571
|
-
getSelfIntersections() {
|
|
572
|
-
const [subCurveControlPoints1, subCurveControlPoints2] = this.split(0.5);
|
|
573
|
-
const subCurve1 = new BCurve(subCurveControlPoints1);
|
|
574
|
-
const subCurve2 = new BCurve(subCurveControlPoints2);
|
|
575
|
-
let initialRes = getIntersectionsBetweenCurves(subCurve1, subCurve2);
|
|
576
|
-
initialRes.forEach((intersection) => {
|
|
577
|
-
intersection.selfT = intersection.selfT * 0.5;
|
|
578
|
-
intersection.otherT = intersection.otherT * 0.5 + 0.5;
|
|
579
|
-
});
|
|
580
|
-
initialRes.shift();
|
|
581
|
-
return initialRes;
|
|
582
|
-
}
|
|
583
|
-
getCircleIntersections(circleCenter, circleRadius) {
|
|
584
|
-
const LUT = this.getLUTWithTVal(500);
|
|
585
|
-
let distanceError = Number.MAX_VALUE;
|
|
586
|
-
let preliminaryIntersectionIndex = 0;
|
|
587
|
-
let preliminaryIntersectionPoint = LUT[0].point;
|
|
588
|
-
let preliminaryIntersectionTVal = LUT[0].tVal;
|
|
589
|
-
LUT.forEach((curvePoint, index) => {
|
|
590
|
-
let curDistanceError = Math.abs(PointCal.distanceBetweenPoints(circleCenter, curvePoint.point));
|
|
591
|
-
if (curDistanceError < distanceError) {
|
|
592
|
-
distanceError = curDistanceError;
|
|
593
|
-
preliminaryIntersectionIndex = index;
|
|
594
|
-
}
|
|
595
|
-
});
|
|
596
|
-
const LUTD = LUT.map((curvePoint, index) => {
|
|
597
|
-
return { ...curvePoint, distance: 0 };
|
|
598
|
-
});
|
|
599
|
-
distanceError = Number.MAX_VALUE;
|
|
600
|
-
let start = 0;
|
|
601
|
-
let count = 0;
|
|
602
|
-
let indices = [];
|
|
603
|
-
while (++count < 25) {
|
|
604
|
-
let i = this.findClosest(circleCenter.x, circleCenter.y, LUTD, circleRadius, 5, LUTD[start - 2]?.distance, LUTD[start - 1]?.distance);
|
|
605
|
-
if (i < start)
|
|
606
|
-
break;
|
|
607
|
-
if (i > 0 && i == start)
|
|
608
|
-
break;
|
|
609
|
-
indices.push(i);
|
|
610
|
-
start = i + 2;
|
|
611
|
-
}
|
|
612
|
-
const finalList = [];
|
|
613
|
-
indices.forEach((index) => {
|
|
614
|
-
let res = this.refineBinary(this, circleCenter.x, circleCenter.y, LUTD, index, circleRadius);
|
|
615
|
-
if (res != null) {
|
|
616
|
-
finalList.push({ intersection: res.point, tVal: res.tVal });
|
|
617
|
-
}
|
|
618
|
-
});
|
|
619
|
-
return finalList;
|
|
620
|
-
}
|
|
621
|
-
advanceAtTWithLength(tVal, length) {
|
|
622
|
-
const currentLength = this.lengthAtT(tVal);
|
|
623
|
-
const targetLength = currentLength + length;
|
|
624
|
-
if (tVal === 0 && length < 0) {
|
|
625
|
-
return { type: "beforeCurve", remainLength: -length };
|
|
626
|
-
}
|
|
627
|
-
if (tVal === 1 && length > 0) {
|
|
628
|
-
return { type: "afterCurve", remainLength: length };
|
|
629
|
-
}
|
|
630
|
-
if (targetLength > this.fullLength) {
|
|
631
|
-
return { type: "afterCurve", remainLength: targetLength - this.fullLength };
|
|
632
|
-
} else if (targetLength < 0) {
|
|
633
|
-
return { type: "beforeCurve", remainLength: -targetLength };
|
|
634
|
-
}
|
|
635
|
-
if (this.arcLengthLUT.arcLengthLUT.length === 0) {
|
|
636
|
-
this.arcLengthLUT = this.getArcLengthLUT(1000);
|
|
637
|
-
}
|
|
638
|
-
const points = this.arcLengthLUT.arcLengthLUT;
|
|
639
|
-
let low = 0;
|
|
640
|
-
let high = points.length - 1;
|
|
641
|
-
while (low <= high) {
|
|
642
|
-
const mid = Math.floor((low + high) / 2);
|
|
643
|
-
const midLength = points[mid].length;
|
|
644
|
-
if (approximately(midLength, targetLength, 0.01)) {
|
|
645
|
-
const resultTVal = points[mid].tVal;
|
|
646
|
-
const point2 = this.get(resultTVal);
|
|
647
|
-
return { type: "withinCurve", tVal: resultTVal, point: point2 };
|
|
648
|
-
} else if (midLength < targetLength) {
|
|
649
|
-
low = mid + 1;
|
|
650
|
-
} else {
|
|
651
|
-
high = mid - 1;
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
if (high < 0) {
|
|
655
|
-
high = 1;
|
|
656
|
-
low = 0;
|
|
657
|
-
}
|
|
658
|
-
if (low >= points.length) {
|
|
659
|
-
high = points.length - 1;
|
|
660
|
-
low = points.length - 2;
|
|
661
|
-
}
|
|
662
|
-
const p1 = points[high];
|
|
663
|
-
const p2 = points[low];
|
|
664
|
-
const lengthRange = p2.length - p1.length;
|
|
665
|
-
const tRange = p2.tVal - p1.tVal;
|
|
666
|
-
if (lengthRange === 0) {
|
|
667
|
-
const point2 = this.get(p1.tVal);
|
|
668
|
-
return { type: "withinCurve", tVal: p1.tVal, point: point2 };
|
|
669
|
-
}
|
|
670
|
-
const ratio = (targetLength - p1.length) / lengthRange;
|
|
671
|
-
const interpolatedT = p1.tVal + ratio * tRange;
|
|
672
|
-
const clampedT = Math.max(0, Math.min(1, interpolatedT));
|
|
673
|
-
const point = this.get(clampedT);
|
|
674
|
-
return { type: "withinCurve", tVal: clampedT, point };
|
|
675
|
-
}
|
|
676
|
-
advanceByDistance(startT, distance) {
|
|
677
|
-
let currentT = startT;
|
|
678
|
-
let remainingDistance = distance;
|
|
679
|
-
const stepSize = 0.01;
|
|
680
|
-
if (distance > this.fullLength) {
|
|
681
|
-
return { type: "afterCurve", remainLength: distance - this.fullLength };
|
|
682
|
-
} else if (distance < 0) {
|
|
683
|
-
return { type: "beforeCurve", remainLength: -distance };
|
|
684
|
-
}
|
|
685
|
-
while (remainingDistance > 0 && currentT < 1) {
|
|
686
|
-
const currentPoint = this.get(currentT);
|
|
687
|
-
const nextT = Math.min(currentT + stepSize, 1);
|
|
688
|
-
const nextPoint = this.get(nextT);
|
|
689
|
-
const segmentLength = Math.sqrt(Math.pow(nextPoint.x - currentPoint.x, 2) + Math.pow(nextPoint.y - currentPoint.y, 2));
|
|
690
|
-
if (segmentLength >= remainingDistance) {
|
|
691
|
-
const ratio = remainingDistance / segmentLength;
|
|
692
|
-
return { type: "withinCurve", tVal: currentT + ratio * (nextT - currentT), point: this.get(currentT + ratio * (nextT - currentT)) };
|
|
693
|
-
}
|
|
694
|
-
remainingDistance -= segmentLength;
|
|
695
|
-
currentT = nextT;
|
|
696
|
-
}
|
|
697
|
-
return { type: "withinCurve", tVal: currentT, point: this.get(currentT) };
|
|
698
|
-
}
|
|
699
|
-
refineBinary(curve, x, y, LUT, i, targetDistance = 0, epsilon = 0.01) {
|
|
700
|
-
let q = LUT[i], count = 1, distance = Number.MAX_SAFE_INTEGER;
|
|
701
|
-
do {
|
|
702
|
-
let i1 = i === 0 ? 0 : i - 1, i2 = i === LUT.length - 1 ? LUT.length - 1 : i + 1, t1 = LUT[i1].tVal, t2 = LUT[i2].tVal, lut = [], step = (t2 - t1) / 4;
|
|
703
|
-
if (step < 0.001)
|
|
704
|
-
break;
|
|
705
|
-
lut.push(LUT[i1]);
|
|
706
|
-
for (let j = 1;j <= 3; j++) {
|
|
707
|
-
let n = curve.get(t1 + j * step);
|
|
708
|
-
let nDistance = Math.abs(PointCal.distanceBetweenPoints(n, { x, y }) - targetDistance);
|
|
709
|
-
if (nDistance < distance) {
|
|
710
|
-
distance = nDistance;
|
|
711
|
-
q = { point: n, tVal: t1 + j * step, distance: nDistance };
|
|
712
|
-
i = j;
|
|
713
|
-
}
|
|
714
|
-
lut.push({ point: n, tVal: t1 + j * step, distance: nDistance });
|
|
715
|
-
}
|
|
716
|
-
lut.push(LUT[i2]);
|
|
717
|
-
LUT = lut;
|
|
718
|
-
} while (count++ < 25);
|
|
719
|
-
if (targetDistance && distance > epsilon) {
|
|
720
|
-
q = undefined;
|
|
721
|
-
}
|
|
722
|
-
return q;
|
|
723
|
-
}
|
|
724
|
-
findClosest(x, y, LUT, circleRadius, distanceEpsilon = 5, pd2, pd1) {
|
|
725
|
-
let distance = Number.MAX_SAFE_INTEGER, prevDistance2 = pd2 || distance, prevDistance1 = pd1 || distance, i = -1;
|
|
726
|
-
for (let index = 0, e = LUT.length;index < e; index++) {
|
|
727
|
-
let p = LUT[index].point;
|
|
728
|
-
LUT[index].distance = Math.abs(PointCal.distanceBetweenPoints({ x, y }, p) - circleRadius);
|
|
729
|
-
if (prevDistance1 < distanceEpsilon && prevDistance2 > prevDistance1 && prevDistance1 < LUT[index].distance) {
|
|
730
|
-
i = index - 1;
|
|
731
|
-
break;
|
|
732
|
-
}
|
|
733
|
-
if (LUT[index].distance < distance) {
|
|
734
|
-
distance = LUT[index].distance;
|
|
735
|
-
}
|
|
736
|
-
prevDistance2 = prevDistance1;
|
|
737
|
-
prevDistance1 = LUT[index].distance;
|
|
738
|
-
}
|
|
739
|
-
return i;
|
|
740
|
-
}
|
|
741
|
-
getCurveIntersections(curve, deduplicationTolerance) {
|
|
742
|
-
return getIntersectionsBetweenCurves(this, curve, deduplicationTolerance);
|
|
743
|
-
}
|
|
744
|
-
get AABB() {
|
|
745
|
-
const extrema = this.getExtrema();
|
|
746
|
-
const tVals = [0, 1];
|
|
747
|
-
let min = { x: Number.MAX_VALUE, y: Number.MAX_VALUE };
|
|
748
|
-
let max = { x: -Number.MAX_VALUE, y: -Number.MAX_VALUE };
|
|
749
|
-
extrema.x.forEach((tVal) => {
|
|
750
|
-
tVals.push(tVal);
|
|
751
|
-
});
|
|
752
|
-
extrema.y.forEach((tVal) => {
|
|
753
|
-
tVals.push(tVal);
|
|
754
|
-
});
|
|
755
|
-
tVals.forEach((tVal) => {
|
|
756
|
-
const curPoint = this.get(tVal);
|
|
757
|
-
min.x = Math.min(min.x, curPoint.x);
|
|
758
|
-
min.y = Math.min(min.y, curPoint.y);
|
|
759
|
-
max.x = Math.max(max.x, curPoint.x);
|
|
760
|
-
max.y = Math.max(max.y, curPoint.y);
|
|
761
|
-
});
|
|
762
|
-
return { min, max };
|
|
763
|
-
}
|
|
764
|
-
normal(tVal) {
|
|
765
|
-
const d = this.derivative(tVal);
|
|
766
|
-
const q = Math.sqrt(d.x * d.x + d.y * d.y);
|
|
767
|
-
return { tVal, direction: { x: -d.y / q, y: d.x / q } };
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
function reduce(curve) {
|
|
771
|
-
let i, t1 = 0, t2 = 0, step = 0.01, segment, pass1 = [], pass2 = [];
|
|
772
|
-
let extrema = curve.getExtrema().x;
|
|
773
|
-
if (extrema.indexOf(0) === -1) {
|
|
774
|
-
extrema = [0].concat(extrema);
|
|
775
|
-
}
|
|
776
|
-
if (extrema.indexOf(1) === -1) {
|
|
777
|
-
extrema.push(1);
|
|
778
|
-
}
|
|
779
|
-
for (t1 = extrema[0], i = 1;i < extrema.length; i++) {
|
|
780
|
-
t2 = extrema[i];
|
|
781
|
-
segment = curve.splitAndTakeMidCurve(t1, t2);
|
|
782
|
-
pass1.push(segment);
|
|
783
|
-
t1 = t2;
|
|
784
|
-
}
|
|
785
|
-
pass1.forEach((p1) => {
|
|
786
|
-
t1 = 0;
|
|
787
|
-
t2 = 0;
|
|
788
|
-
while (t2 <= 1) {
|
|
789
|
-
for (t2 = t1 + step;t2 <= 1 + step; t2 += step) {
|
|
790
|
-
const clampedT2 = Math.min(t2, 1);
|
|
791
|
-
segment = p1.splitAndTakeMidCurve(t1, clampedT2);
|
|
792
|
-
if (!curveIsSimple(segment)) {
|
|
793
|
-
t2 -= step;
|
|
794
|
-
if (Math.abs(t1 - t2) < step) {
|
|
795
|
-
return [];
|
|
796
|
-
}
|
|
797
|
-
const finalT2 = Math.min(t2, 1);
|
|
798
|
-
segment = p1.splitAndTakeMidCurve(t1, finalT2);
|
|
799
|
-
pass2.push(segment);
|
|
800
|
-
t1 = finalT2;
|
|
801
|
-
break;
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
if (t1 < 1) {
|
|
806
|
-
segment = p1.splitAndTakeMidCurve(t1, 1);
|
|
807
|
-
pass2.push(segment);
|
|
808
|
-
}
|
|
809
|
-
});
|
|
810
|
-
return pass2;
|
|
811
|
-
}
|
|
812
|
-
function raiseCurveOrder(curve) {
|
|
813
|
-
const p = curve.getControlPoints(), np = [p[0]], k = p.length;
|
|
814
|
-
for (let i = 1;i < k; i++) {
|
|
815
|
-
const pi = p[i];
|
|
816
|
-
const pim = p[i - 1];
|
|
817
|
-
np[i] = {
|
|
818
|
-
x: (k - i) / k * pi.x + i / k * pim.x,
|
|
819
|
-
y: (k - i) / k * pi.y + i / k * pim.y
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
np[k] = p[k - 1];
|
|
823
|
-
return new BCurve(np);
|
|
824
|
-
}
|
|
825
|
-
function offset(curve, t, d) {
|
|
826
|
-
if (d !== undefined) {
|
|
827
|
-
const c = curve.get(t), n = curve.normal(t).direction;
|
|
828
|
-
const ret = {
|
|
829
|
-
c,
|
|
830
|
-
n,
|
|
831
|
-
x: c.x + n.x * d,
|
|
832
|
-
y: c.y + n.y * d
|
|
833
|
-
};
|
|
834
|
-
return ret;
|
|
835
|
-
}
|
|
836
|
-
const points = curve.getControlPoints();
|
|
837
|
-
const linear = curveIsLinear(curve);
|
|
838
|
-
if (linear) {
|
|
839
|
-
const nv = curve.normal(0).direction, coords = points.map(function(p) {
|
|
840
|
-
const ret = {
|
|
841
|
-
x: p.x + t * nv.x,
|
|
842
|
-
y: p.y + t * nv.y
|
|
843
|
-
};
|
|
844
|
-
return ret;
|
|
845
|
-
});
|
|
846
|
-
return [new BCurve(coords)];
|
|
847
|
-
}
|
|
848
|
-
return reduce(curve).map(function(s) {
|
|
849
|
-
if (curveIsLinear(s)) {
|
|
850
|
-
return offset(s, t)[0];
|
|
851
|
-
}
|
|
852
|
-
return scaleCurve(s, t);
|
|
853
|
-
});
|
|
854
|
-
}
|
|
855
|
-
function offset2(curve, d) {
|
|
856
|
-
const lut = curve.getLUTWithTVal(100);
|
|
857
|
-
const res = lut.map((item) => {
|
|
858
|
-
const derivative = PointCal.unitVector(curve.derivative(item.tVal));
|
|
859
|
-
const normal = { x: -derivative.y, y: derivative.x };
|
|
860
|
-
const offsetPoint = { x: item.point.x + normal.x * d, y: item.point.y + normal.y * d };
|
|
861
|
-
return offsetPoint;
|
|
862
|
-
});
|
|
863
|
-
return res;
|
|
864
|
-
}
|
|
865
|
-
function lli4(p1, p2, p3, p4) {
|
|
866
|
-
const { x: x1, y: y1 } = p1, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y, x4 = p4.x, y4 = p4.y;
|
|
867
|
-
return lli8(x1, y1, x2, y2, x3, y3, x4, y4);
|
|
868
|
-
}
|
|
869
|
-
function lli8(x1, y1, x2, y2, x3, y3, x4, y4) {
|
|
870
|
-
const nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4);
|
|
871
|
-
const ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4);
|
|
872
|
-
const d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
|
873
|
-
if (d == 0) {
|
|
874
|
-
return false;
|
|
875
|
-
}
|
|
876
|
-
return { x: nx / d, y: ny / d };
|
|
877
|
-
}
|
|
878
|
-
function curveIsLinear(curve) {
|
|
879
|
-
const order = curve.getControlPoints().length - 1;
|
|
880
|
-
const points = curve.getControlPoints();
|
|
881
|
-
const alignedPoints = alignPointsToLine(points, { p1: points[0], p2: points[order] });
|
|
882
|
-
const baseLength = PointCal.distanceBetweenPoints(points[0], points[order]);
|
|
883
|
-
const linear = alignedPoints.reduce((t, p) => t + Math.abs(p.y), 0) < baseLength / 50;
|
|
884
|
-
return linear;
|
|
885
|
-
}
|
|
886
|
-
function scaleCurve(curve, d) {
|
|
887
|
-
const order = curve.getControlPoints().length - 1;
|
|
888
|
-
let distanceFn = undefined;
|
|
889
|
-
if (typeof d === "function") {
|
|
890
|
-
distanceFn = d;
|
|
891
|
-
}
|
|
892
|
-
if (distanceFn && order === 2) {
|
|
893
|
-
return scaleCurve(raiseCurveOrder(curve), distanceFn);
|
|
894
|
-
}
|
|
895
|
-
const points = curve.getControlPoints();
|
|
896
|
-
if (curveIsLinear(curve)) {
|
|
897
|
-
return translate(curve, curve.normal(0).direction, distanceFn ? distanceFn(0) : d, distanceFn ? distanceFn(1) : d);
|
|
898
|
-
}
|
|
899
|
-
const r1 = distanceFn ? distanceFn(0) : d;
|
|
900
|
-
const r2 = distanceFn ? distanceFn(1) : d;
|
|
901
|
-
const v = [offset(curve, 0, 10), offset(curve, 1, 10)];
|
|
902
|
-
const np = [];
|
|
903
|
-
const o = lli4(v[0], v[0].c, v[1], v[1].c);
|
|
904
|
-
if (!o) {
|
|
905
|
-
return translate(curve, curve.normal(0).direction, r1, r2);
|
|
906
|
-
}
|
|
907
|
-
[0, 1].forEach(function(t) {
|
|
908
|
-
const p = JSON.parse(JSON.stringify(points[t * order]));
|
|
909
|
-
const vt = v[t];
|
|
910
|
-
p.x += (t ? r2 : r1) * vt.n.x;
|
|
911
|
-
p.y += (t ? r2 : r1) * vt.n.y;
|
|
912
|
-
np[t * order] = p;
|
|
913
|
-
});
|
|
914
|
-
if (!distanceFn) {
|
|
915
|
-
[0, 1].forEach((t) => {
|
|
916
|
-
if (order === 2 && !!t)
|
|
917
|
-
return;
|
|
918
|
-
const p = np[t * order];
|
|
919
|
-
const derivativeAtT = curve.derivative(t);
|
|
920
|
-
const p2 = { x: p.x + derivativeAtT.x, y: p.y + derivativeAtT.y };
|
|
921
|
-
const intersection = lli4(p, p2, o, points[t + 1]);
|
|
922
|
-
if (intersection) {
|
|
923
|
-
np[t + 1] = intersection;
|
|
924
|
-
} else {
|
|
925
|
-
const originalPoint = points[t + 1];
|
|
926
|
-
const normal = curve.normal((t + 1) / order).direction;
|
|
927
|
-
np[t + 1] = {
|
|
928
|
-
x: originalPoint.x + (t ? r2 : r1) * normal.x,
|
|
929
|
-
y: originalPoint.y + (t ? r2 : r1) * normal.y
|
|
930
|
-
};
|
|
931
|
-
}
|
|
932
|
-
});
|
|
933
|
-
return new BCurve(np);
|
|
934
|
-
}
|
|
935
|
-
[0, 1].forEach(function(t) {
|
|
936
|
-
if (order === 2 && !!t)
|
|
937
|
-
return;
|
|
938
|
-
const p = points[t + 1];
|
|
939
|
-
const ov = {
|
|
940
|
-
x: p.x - o.x,
|
|
941
|
-
y: p.y - o.y
|
|
942
|
-
};
|
|
943
|
-
let rc = distanceFn((t + 1) / order);
|
|
944
|
-
const m = Math.sqrt(ov.x * ov.x + ov.y * ov.y);
|
|
945
|
-
ov.x /= m;
|
|
946
|
-
ov.y /= m;
|
|
947
|
-
np[t + 1] = {
|
|
948
|
-
x: p.x + rc * ov.x,
|
|
949
|
-
y: p.y + rc * ov.y
|
|
950
|
-
};
|
|
951
|
-
});
|
|
952
|
-
return new BCurve(np);
|
|
953
|
-
}
|
|
954
|
-
function alignPointsToLine(points, line) {
|
|
955
|
-
const tx = line.p1.x, ty = line.p1.y, a = -Math.atan2(line.p2.y - ty, line.p2.x - tx), d = function(v) {
|
|
956
|
-
return {
|
|
957
|
-
x: (v.x - tx) * Math.cos(a) - (v.y - ty) * Math.sin(a),
|
|
958
|
-
y: (v.x - tx) * Math.sin(a) + (v.y - ty) * Math.cos(a)
|
|
959
|
-
};
|
|
960
|
-
};
|
|
961
|
-
return points.map(d);
|
|
962
|
-
}
|
|
963
|
-
function map(v, ds, de, ts, te) {
|
|
964
|
-
const d1 = de - ds, d2 = te - ts, v2 = v - ds, r = v2 / d1;
|
|
965
|
-
return ts + d2 * r;
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
class TValOutofBoundError extends Error {
|
|
969
|
-
constructor(message) {
|
|
970
|
-
super(message);
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
function AABBIntersects(AABB1, AABB2) {
|
|
974
|
-
if (AABB1.min.x <= AABB2.max.x && AABB2.min.x <= AABB1.max.x && (AABB1.min.y <= AABB2.max.y && AABB2.min.y <= AABB1.max.y)) {
|
|
975
|
-
return true;
|
|
976
|
-
}
|
|
977
|
-
return false;
|
|
978
|
-
}
|
|
979
|
-
function approximately(a, b, precision) {
|
|
980
|
-
const epsilon = 0.000001;
|
|
981
|
-
return Math.abs(a - b) <= (precision || epsilon);
|
|
982
|
-
}
|
|
983
|
-
function cuberoot2(v) {
|
|
984
|
-
if (v < 0)
|
|
985
|
-
return -Math.pow(-v, 1 / 3);
|
|
986
|
-
return Math.pow(v, 1 / 3);
|
|
987
|
-
}
|
|
988
|
-
function accept(t) {
|
|
989
|
-
return 0 <= t && t <= 1;
|
|
990
|
-
}
|
|
991
|
-
function getCubicRoots(pa, pb, pc, pd) {
|
|
992
|
-
let a = 3 * pa - 6 * pb + 3 * pc, b = -3 * pa + 3 * pb, c = pa, d = -pa + 3 * pb - 3 * pc + pd;
|
|
993
|
-
if (approximately(d, 0)) {
|
|
994
|
-
if (approximately(a, 0)) {
|
|
995
|
-
if (approximately(b, 0)) {
|
|
996
|
-
return [];
|
|
997
|
-
}
|
|
998
|
-
return [-c / b].filter(accept);
|
|
999
|
-
}
|
|
1000
|
-
let q3 = Math.sqrt(b * b - 4 * a * c), a2 = 2 * a;
|
|
1001
|
-
return [(q3 - b) / a2, (-b - q3) / a2].filter(accept);
|
|
1002
|
-
}
|
|
1003
|
-
a /= d;
|
|
1004
|
-
b /= d;
|
|
1005
|
-
c /= d;
|
|
1006
|
-
let p = (3 * b - a * a) / 3, p3 = p / 3, q = (2 * a * a * a - 9 * a * b + 27 * c) / 27, q2 = q / 2, discriminant = q2 * q2 + p3 * p3 * p3;
|
|
1007
|
-
let u1, v1, root1, root2, root3;
|
|
1008
|
-
if (discriminant < 0) {
|
|
1009
|
-
let mp3 = -p / 3, mp33 = mp3 * mp3 * mp3, r = Math.sqrt(mp33), t = -q / (2 * r), cosphi = t < -1 ? -1 : t > 1 ? 1 : t, phi = Math.acos(cosphi), crtr = cuberoot2(r), t1 = 2 * crtr;
|
|
1010
|
-
root1 = t1 * Math.cos(phi / 3) - a / 3;
|
|
1011
|
-
root2 = t1 * Math.cos((phi + 2 * Math.PI) / 3) - a / 3;
|
|
1012
|
-
root3 = t1 * Math.cos((phi + 4 * Math.PI) / 3) - a / 3;
|
|
1013
|
-
return [root1, root2, root3].filter(accept);
|
|
1014
|
-
}
|
|
1015
|
-
if (discriminant === 0) {
|
|
1016
|
-
u1 = q2 < 0 ? cuberoot2(-q2) : -cuberoot2(q2);
|
|
1017
|
-
root1 = 2 * u1 - a / 3;
|
|
1018
|
-
root2 = -u1 - a / 3;
|
|
1019
|
-
return [root1, root2].filter(accept);
|
|
1020
|
-
}
|
|
1021
|
-
var sd = Math.sqrt(discriminant);
|
|
1022
|
-
u1 = cuberoot2(sd - q2);
|
|
1023
|
-
v1 = cuberoot2(sd + q2);
|
|
1024
|
-
root1 = u1 - v1 - a / 3;
|
|
1025
|
-
return [root1].filter(accept);
|
|
1026
|
-
}
|
|
1027
|
-
function getIntersectionsBetweenCurves(curve, curve2, deduplicationTolerance = 0.01) {
|
|
1028
|
-
const threshold = 0.5;
|
|
1029
|
-
let pairs = [{ curve1: { curve, startTVal: 0, endTVal: 1 }, curve2: { curve: curve2, startTVal: 0, endTVal: 1 } }];
|
|
1030
|
-
const finalRes = [];
|
|
1031
|
-
while (pairs.length > 0) {
|
|
1032
|
-
let curLength = pairs.length;
|
|
1033
|
-
for (let index = 0;index < curLength; index++) {
|
|
1034
|
-
let pair = pairs.shift();
|
|
1035
|
-
if (pair == undefined) {
|
|
1036
|
-
break;
|
|
1037
|
-
}
|
|
1038
|
-
let aabb1 = pair.curve1.curve.AABB;
|
|
1039
|
-
let aabb2 = pair.curve2.curve.AABB;
|
|
1040
|
-
let intersects = AABBIntersects(aabb1, aabb2);
|
|
1041
|
-
if (pair.curve1.curve.fullLength < threshold && pair.curve2.curve.fullLength < threshold) {
|
|
1042
|
-
finalRes.push({ intersection: pair.curve1.curve.get(0.5), tVal1: (pair.curve1.startTVal + pair.curve1.endTVal) * 0.5, tVal2: (pair.curve2.startTVal + pair.curve2.endTVal) * 0.5 });
|
|
1043
|
-
continue;
|
|
1044
|
-
}
|
|
1045
|
-
if (intersects) {
|
|
1046
|
-
let [subCurveControlPoints1, subCurveControlPoints2] = pair.curve1.curve.split(0.5);
|
|
1047
|
-
let [subCurveControlPoints3, subCurveControlPoints4] = pair.curve2.curve.split(0.5);
|
|
1048
|
-
pairs.push({
|
|
1049
|
-
curve1: {
|
|
1050
|
-
curve: new BCurve(subCurveControlPoints1),
|
|
1051
|
-
startTVal: pair.curve1.startTVal,
|
|
1052
|
-
endTVal: pair.curve1.startTVal + (pair.curve1.endTVal - pair.curve1.startTVal) * 0.5
|
|
1053
|
-
},
|
|
1054
|
-
curve2: {
|
|
1055
|
-
curve: new BCurve(subCurveControlPoints3),
|
|
1056
|
-
startTVal: pair.curve2.startTVal,
|
|
1057
|
-
endTVal: pair.curve2.startTVal + (pair.curve2.endTVal - pair.curve2.startTVal) * 0.5
|
|
1058
|
-
}
|
|
1059
|
-
});
|
|
1060
|
-
pairs.push({
|
|
1061
|
-
curve1: {
|
|
1062
|
-
curve: new BCurve(subCurveControlPoints1),
|
|
1063
|
-
startTVal: pair.curve1.startTVal,
|
|
1064
|
-
endTVal: pair.curve1.startTVal + (pair.curve1.endTVal - pair.curve1.startTVal) * 0.5
|
|
1065
|
-
},
|
|
1066
|
-
curve2: {
|
|
1067
|
-
curve: new BCurve(subCurveControlPoints4),
|
|
1068
|
-
startTVal: pair.curve2.startTVal + (pair.curve2.endTVal - pair.curve2.startTVal) * 0.5,
|
|
1069
|
-
endTVal: pair.curve2.endTVal
|
|
1070
|
-
}
|
|
1071
|
-
});
|
|
1072
|
-
pairs.push({
|
|
1073
|
-
curve1: {
|
|
1074
|
-
curve: new BCurve(subCurveControlPoints2),
|
|
1075
|
-
startTVal: pair.curve1.startTVal + (pair.curve1.endTVal - pair.curve1.startTVal) * 0.5,
|
|
1076
|
-
endTVal: pair.curve1.endTVal
|
|
1077
|
-
},
|
|
1078
|
-
curve2: {
|
|
1079
|
-
curve: new BCurve(subCurveControlPoints3),
|
|
1080
|
-
startTVal: pair.curve2.startTVal,
|
|
1081
|
-
endTVal: pair.curve2.startTVal + (pair.curve2.endTVal - pair.curve2.startTVal) * 0.5
|
|
1082
|
-
}
|
|
1083
|
-
});
|
|
1084
|
-
pairs.push({
|
|
1085
|
-
curve1: {
|
|
1086
|
-
curve: new BCurve(subCurveControlPoints2),
|
|
1087
|
-
startTVal: pair.curve1.startTVal + (pair.curve1.endTVal - pair.curve1.startTVal) * 0.5,
|
|
1088
|
-
endTVal: pair.curve1.endTVal
|
|
1089
|
-
},
|
|
1090
|
-
curve2: {
|
|
1091
|
-
curve: new BCurve(subCurveControlPoints4),
|
|
1092
|
-
startTVal: pair.curve2.startTVal + (pair.curve2.endTVal - pair.curve2.startTVal) * 0.5,
|
|
1093
|
-
endTVal: pair.curve2.endTVal
|
|
1094
|
-
}
|
|
1095
|
-
});
|
|
1096
|
-
}
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
const tVals = [];
|
|
1100
|
-
finalRes.sort((a, b) => a.tVal1 - b.tVal1);
|
|
1101
|
-
for (const intersection of finalRes) {
|
|
1102
|
-
let isDuplicate = false;
|
|
1103
|
-
for (const existing of tVals) {
|
|
1104
|
-
const selfTClose = approximately(intersection.tVal1, existing.selfT, deduplicationTolerance);
|
|
1105
|
-
const otherTClose = approximately(intersection.tVal2, existing.otherT, deduplicationTolerance);
|
|
1106
|
-
if (selfTClose && otherTClose) {
|
|
1107
|
-
isDuplicate = true;
|
|
1108
|
-
break;
|
|
1109
|
-
}
|
|
1110
|
-
const selfTVeryClose = approximately(intersection.tVal1, existing.selfT, deduplicationTolerance * 10);
|
|
1111
|
-
const otherTVeryClose = approximately(intersection.tVal2, existing.otherT, deduplicationTolerance * 10);
|
|
1112
|
-
if (selfTVeryClose || otherTVeryClose) {
|
|
1113
|
-
const point1 = curve.get(intersection.tVal1);
|
|
1114
|
-
const point2 = curve2.get(intersection.tVal2);
|
|
1115
|
-
const existingPoint1 = curve.get(existing.selfT);
|
|
1116
|
-
const existingPoint2 = curve2.get(existing.otherT);
|
|
1117
|
-
const distance1 = PointCal.distanceBetweenPoints(point1, existingPoint1);
|
|
1118
|
-
const distance2 = PointCal.distanceBetweenPoints(point2, existingPoint2);
|
|
1119
|
-
if (distance1 < deduplicationTolerance * 100 && distance2 < deduplicationTolerance * 100) {
|
|
1120
|
-
isDuplicate = true;
|
|
1121
|
-
break;
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
if (!isDuplicate) {
|
|
1126
|
-
tVals.push({ selfT: intersection.tVal1, otherT: intersection.tVal2 });
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
return tVals;
|
|
1130
|
-
}
|
|
1131
|
-
function solveCubic(a, b, c, d) {
|
|
1132
|
-
if (Math.abs(a) < 0.00000001) {
|
|
1133
|
-
a = b;
|
|
1134
|
-
b = c;
|
|
1135
|
-
c = d;
|
|
1136
|
-
if (Math.abs(a) < 0.00000001) {
|
|
1137
|
-
a = b;
|
|
1138
|
-
b = c;
|
|
1139
|
-
if (Math.abs(a) < 0.00000001)
|
|
1140
|
-
return [];
|
|
1141
|
-
return [-b / a];
|
|
1142
|
-
}
|
|
1143
|
-
let D = b * b - 4 * a * c;
|
|
1144
|
-
if (Math.abs(D) < 0.00000001)
|
|
1145
|
-
return [-b / (2 * a)];
|
|
1146
|
-
else if (D > 0)
|
|
1147
|
-
return [(-b + Math.sqrt(D)) / (2 * a), (-b - Math.sqrt(D)) / (2 * a)];
|
|
1148
|
-
return [];
|
|
1149
|
-
}
|
|
1150
|
-
let p = (3 * a * c - b * b) / (3 * a * a);
|
|
1151
|
-
let q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
|
|
1152
|
-
let roots;
|
|
1153
|
-
if (Math.abs(p) < 0.00000001) {
|
|
1154
|
-
roots = [cuberoot(-q)];
|
|
1155
|
-
} else if (Math.abs(q) < 0.00000001) {
|
|
1156
|
-
roots = [0].concat(p < 0 ? [Math.sqrt(-p), -Math.sqrt(-p)] : []);
|
|
1157
|
-
} else {
|
|
1158
|
-
let D = q * q / 4 + p * p * p / 27;
|
|
1159
|
-
if (Math.abs(D) < 0.00000001) {
|
|
1160
|
-
roots = [-1.5 * q / p, 3 * q / p];
|
|
1161
|
-
} else if (D > 0) {
|
|
1162
|
-
let u = cuberoot(-q / 2 - Math.sqrt(D));
|
|
1163
|
-
let v = cuberoot(-q / 2 + Math.sqrt(D));
|
|
1164
|
-
roots = [u - p / (3 * u)];
|
|
1165
|
-
} else {
|
|
1166
|
-
let u = 2 * Math.sqrt(-p / 3);
|
|
1167
|
-
let t = Math.acos(3 * q / p / u) / 3;
|
|
1168
|
-
let k = 2 * Math.PI / 3;
|
|
1169
|
-
roots = [u * Math.cos(t), u * Math.cos(t - k), u * Math.cos(t - 2 * k)];
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
for (let i = 0;i < roots.length; i++)
|
|
1173
|
-
roots[i] -= b / (3 * a);
|
|
1174
|
-
return roots;
|
|
1175
|
-
}
|
|
1176
|
-
function cuberoot(x) {
|
|
1177
|
-
var y = Math.pow(Math.abs(x), 1 / 3);
|
|
1178
|
-
return x < 0 ? -y : y;
|
|
1179
|
-
}
|
|
1180
|
-
function computeWithControlPoints(tVal, controlPoints) {
|
|
1181
|
-
let points = [...controlPoints];
|
|
1182
|
-
while (points.length > 1) {
|
|
1183
|
-
let lowerLevelPoints = points.slice(1);
|
|
1184
|
-
for (let index = 0;index < lowerLevelPoints.length; index++) {
|
|
1185
|
-
lowerLevelPoints[index] = PointCal.addVector(PointCal.multiplyVectorByScalar(points[index], 1 - tVal), PointCal.multiplyVectorByScalar(points[index + 1], tVal));
|
|
1186
|
-
}
|
|
1187
|
-
points = lowerLevelPoints;
|
|
1188
|
-
}
|
|
1189
|
-
return points[0];
|
|
1190
|
-
}
|
|
1191
|
-
function curveIsSimple(curve) {
|
|
1192
|
-
if (curve.getControlPoints().length === 4) {
|
|
1193
|
-
const points = curve.getControlPoints();
|
|
1194
|
-
const p0ToP3Vector = PointCal.subVector(points[3], points[0]);
|
|
1195
|
-
const p0ToP1Vector = PointCal.subVector(points[1], points[0]);
|
|
1196
|
-
const p0ToP2Vector = PointCal.subVector(points[2], points[0]);
|
|
1197
|
-
const a1 = PointCal.angleFromA2B(p0ToP3Vector, p0ToP1Vector);
|
|
1198
|
-
const a2 = PointCal.angleFromA2B(p0ToP3Vector, p0ToP2Vector);
|
|
1199
|
-
if (a1 > 0 && a2 < 0 || a1 < 0 && a2 > 0)
|
|
1200
|
-
return false;
|
|
1201
|
-
}
|
|
1202
|
-
const n1 = curve.normal(0).direction;
|
|
1203
|
-
const n2 = curve.normal(1).direction;
|
|
1204
|
-
let s = n1.x * n2.x + n1.y * n2.y;
|
|
1205
|
-
return Math.abs(Math.acos(s)) < Math.PI / 3;
|
|
1206
|
-
}
|
|
1207
|
-
function translate(curve, vector, d1, d2) {
|
|
1208
|
-
const order = curve.getControlPoints().length - 1;
|
|
1209
|
-
const points = curve.getControlPoints();
|
|
1210
|
-
const d = points.map((_, i) => (1 - i / order) * d1 + i / order * d2);
|
|
1211
|
-
return new BCurve(points.map((p, i) => ({ x: p.x + d[i] * vector.x, y: p.y + d[i] * vector.y })));
|
|
1212
|
-
}
|
|
1213
|
-
// src/line.ts
|
|
1214
|
-
import { PointCal as PointCal2 } from "@ue-too/math";
|
|
1215
|
-
|
|
1216
|
-
class Line {
|
|
1217
|
-
startPoint;
|
|
1218
|
-
endPoint;
|
|
1219
|
-
constructor(startPoint, endPoint) {
|
|
1220
|
-
this.startPoint = startPoint;
|
|
1221
|
-
this.endPoint = endPoint;
|
|
1222
|
-
}
|
|
1223
|
-
getStartPoint() {
|
|
1224
|
-
return this.startPoint;
|
|
1225
|
-
}
|
|
1226
|
-
getEndPoint() {
|
|
1227
|
-
return this.endPoint;
|
|
1228
|
-
}
|
|
1229
|
-
intersectionWithAnotherLine(lineToIntersect) {
|
|
1230
|
-
return getLineIntersection(this.startPoint, this.endPoint, lineToIntersect.getStartPoint(), lineToIntersect.getEndPoint());
|
|
1231
|
-
}
|
|
1232
|
-
projectPoint(point) {
|
|
1233
|
-
return projectPointOntoLine(point, this.getStartPoint(), this.getEndPoint());
|
|
1234
|
-
}
|
|
1235
|
-
length() {
|
|
1236
|
-
return PointCal2.distanceBetweenPoints(this.startPoint, this.endPoint);
|
|
1237
|
-
}
|
|
1238
|
-
getTranslationRotationToAlginXAxis() {
|
|
1239
|
-
const translation = PointCal2.subVector({ x: 0, y: 0 }, this.startPoint);
|
|
1240
|
-
const rotationAngle = PointCal2.angleFromA2B(PointCal2.subVector(this.endPoint, this.startPoint), { x: 1, y: 0 });
|
|
1241
|
-
return { translation, rotationAngle };
|
|
1242
|
-
}
|
|
1243
|
-
pointInLine(point) {
|
|
1244
|
-
const baseVector = PointCal2.unitVectorFromA2B(this.startPoint, this.endPoint);
|
|
1245
|
-
const start2PointVector = PointCal2.subVector(point, this.startPoint);
|
|
1246
|
-
const length = PointCal2.dotProduct(start2PointVector, baseVector);
|
|
1247
|
-
const start2PointUnitVector = PointCal2.unitVector(start2PointVector);
|
|
1248
|
-
const errorThreshold = PointCal2.distanceBetweenPoints(this.startPoint, this.endPoint) * 0.0001;
|
|
1249
|
-
return length <= PointCal2.distanceBetweenPoints(this.startPoint, this.endPoint) && length >= 0 && Math.abs(start2PointUnitVector.x - baseVector.x) < 0.0001 && Math.abs(start2PointUnitVector.y - baseVector.y) < 0.0001;
|
|
1250
|
-
}
|
|
1251
|
-
lerp(ratio) {
|
|
1252
|
-
return PointCal2.linearInterpolation(this.startPoint, this.endPoint, ratio);
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
function getLineIntersection(startPoint, endPoint, startPoint2, endPoint2) {
|
|
1256
|
-
const numerator = (endPoint2.x - startPoint2.x) * (startPoint.y - startPoint2.y) - (endPoint2.y - startPoint2.y) * (startPoint.x - startPoint2.x);
|
|
1257
|
-
const denominator = (endPoint2.y - startPoint2.y) * (endPoint.x - startPoint.x) - (endPoint2.x - startPoint2.x) * (endPoint.y - startPoint.y);
|
|
1258
|
-
if (denominator === 0) {
|
|
1259
|
-
return { intersects: false };
|
|
1260
|
-
}
|
|
1261
|
-
const t = numerator / denominator;
|
|
1262
|
-
if (t >= 0 && t <= 1) {
|
|
1263
|
-
return {
|
|
1264
|
-
intersects: true,
|
|
1265
|
-
intersection: PointCal2.linearInterpolation(startPoint, endPoint, t),
|
|
1266
|
-
offset: t
|
|
1267
|
-
};
|
|
1268
|
-
} else {
|
|
1269
|
-
return {
|
|
1270
|
-
intersects: false
|
|
1271
|
-
};
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
function projectPointOntoLine(point, lineStartPoint, lineEndPoint) {
|
|
1275
|
-
const baseVector = PointCal2.unitVector(PointCal2.subVector(lineEndPoint, lineStartPoint));
|
|
1276
|
-
const vectorToPoint = PointCal2.subVector(point, lineStartPoint);
|
|
1277
|
-
const res = PointCal2.dotProduct(vectorToPoint, baseVector);
|
|
1278
|
-
if (res < 0 || res > PointCal2.magnitude(PointCal2.subVector(lineEndPoint, lineStartPoint))) {
|
|
1279
|
-
return {
|
|
1280
|
-
within: false
|
|
1281
|
-
};
|
|
1282
|
-
}
|
|
1283
|
-
return {
|
|
1284
|
-
within: true,
|
|
1285
|
-
projectionPoint: PointCal2.addVector(lineStartPoint, PointCal2.multiplyVectorByScalar(baseVector, res)),
|
|
1286
|
-
offset: res / PointCal2.magnitude(PointCal2.subVector(lineEndPoint, lineStartPoint))
|
|
1287
|
-
};
|
|
1288
|
-
}
|
|
1289
|
-
// src/composite-curve.ts
|
|
1290
|
-
import { PointCal as PointCal3 } from "@ue-too/math";
|
|
1291
|
-
|
|
1292
|
-
class ControlPoint {
|
|
1293
|
-
position;
|
|
1294
|
-
leftHandle;
|
|
1295
|
-
rightHandle;
|
|
1296
|
-
constructor(position, leftHandle, rightHandle) {
|
|
1297
|
-
this.position = position;
|
|
1298
|
-
this.leftHandle = leftHandle;
|
|
1299
|
-
this.rightHandle = rightHandle;
|
|
1300
|
-
}
|
|
1301
|
-
setPosition(destinationPosition, prevControlPoint, nextControlPoint) {
|
|
1302
|
-
let diff = PointCal3.subVector(destinationPosition, this.position);
|
|
1303
|
-
this.position = destinationPosition;
|
|
1304
|
-
this.leftHandle.position = PointCal3.addVector(this.leftHandle.position, diff);
|
|
1305
|
-
this.rightHandle.position = PointCal3.addVector(this.rightHandle.position, diff);
|
|
1306
|
-
if (this.leftHandle.type == "VECTOR" && prevControlPoint) {
|
|
1307
|
-
let relativeVector = PointCal3.subVector(prevControlPoint.getPosition(), this.position);
|
|
1308
|
-
relativeVector = PointCal3.multiplyVectorByScalar(relativeVector, 1 / 3);
|
|
1309
|
-
this.leftHandle.position = PointCal3.addVector(this.position, relativeVector);
|
|
1310
|
-
if (this.rightHandle.type == "ALIGNED") {
|
|
1311
|
-
let relativeVector2 = PointCal3.subVector(this.rightHandle.position, this.position);
|
|
1312
|
-
let mag = PointCal3.magnitude(relativeVector2);
|
|
1313
|
-
let direction = PointCal3.unitVectorFromA2B(this.leftHandle.position, this.position);
|
|
1314
|
-
this.rightHandle.position = PointCal3.addVector(this.position, PointCal3.multiplyVectorByScalar(direction, mag));
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
if (this.rightHandle.type == "VECTOR" && nextControlPoint) {
|
|
1318
|
-
let relativeVector = PointCal3.subVector(nextControlPoint.getPosition(), this.position);
|
|
1319
|
-
relativeVector = PointCal3.multiplyVectorByScalar(relativeVector, 1 / 3);
|
|
1320
|
-
this.rightHandle.position = PointCal3.addVector(this.position, relativeVector);
|
|
1321
|
-
if (this.leftHandle.type == "ALIGNED") {
|
|
1322
|
-
let mag = PointCal3.distanceBetweenPoints(this.leftHandle.position, this.position);
|
|
1323
|
-
let direction = PointCal3.subVector(this.position, this.rightHandle.position);
|
|
1324
|
-
this.leftHandle.position = PointCal3.addVector(this.position, PointCal3.multiplyVectorByScalar(direction, mag));
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
if (prevControlPoint !== undefined && prevControlPoint.getRightHandle().type == "VECTOR") {
|
|
1328
|
-
let relativeVector = PointCal3.subVector(this.position, prevControlPoint.getPosition());
|
|
1329
|
-
relativeVector = PointCal3.multiplyVectorByScalar(relativeVector, 1 / 3);
|
|
1330
|
-
prevControlPoint.setRightHandlePosition(PointCal3.addVector(prevControlPoint.getPosition(), relativeVector));
|
|
1331
|
-
}
|
|
1332
|
-
if (nextControlPoint !== undefined && nextControlPoint.getLeftHandle().type == "VECTOR") {
|
|
1333
|
-
let relativeVector = PointCal3.subVector(this.position, nextControlPoint.getPosition());
|
|
1334
|
-
relativeVector = PointCal3.multiplyVectorByScalar(relativeVector, 1 / 3);
|
|
1335
|
-
nextControlPoint.setLeftHandlePosition(PointCal3.addVector(nextControlPoint.getPosition(), relativeVector));
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
getPosition() {
|
|
1339
|
-
return this.position;
|
|
1340
|
-
}
|
|
1341
|
-
setLeftHandleTypeVector(prevControlPoint) {
|
|
1342
|
-
if (this.rightHandle.type != "VECTOR") {
|
|
1343
|
-
this.rightHandle.type = "FREE";
|
|
1344
|
-
}
|
|
1345
|
-
let relativeVector;
|
|
1346
|
-
if (prevControlPoint == undefined) {
|
|
1347
|
-
relativeVector = PointCal3.subVector(this.leftHandle.position, this.position);
|
|
1348
|
-
} else {
|
|
1349
|
-
relativeVector = PointCal3.subVector(prevControlPoint.getPosition(), this.position);
|
|
1350
|
-
relativeVector = PointCal3.multiplyVectorByScalar(relativeVector, 1 / 3);
|
|
1351
|
-
}
|
|
1352
|
-
this.leftHandle.position = PointCal3.addVector(this.position, relativeVector);
|
|
1353
|
-
}
|
|
1354
|
-
setLeftHandleTypeAligned() {
|
|
1355
|
-
this.leftHandle.type = "ALIGNED";
|
|
1356
|
-
if (this.rightHandle.type == "VECTOR") {
|
|
1357
|
-
let direction = PointCal3.unitVectorFromA2B(this.rightHandle.position, this.position);
|
|
1358
|
-
let mag = PointCal3.distanceBetweenPoints(this.position, this.leftHandle.position);
|
|
1359
|
-
this.leftHandle.position = PointCal3.addVector(this.position, PointCal3.multiplyVectorByScalar(direction, mag));
|
|
1360
|
-
}
|
|
1361
|
-
}
|
|
1362
|
-
setLeftHandleTypeFree() {
|
|
1363
|
-
this.leftHandle.type = "FREE";
|
|
1364
|
-
}
|
|
1365
|
-
setRightHandleTypeVector(nextControlPoint) {
|
|
1366
|
-
if (this.leftHandle.type != "VECTOR") {
|
|
1367
|
-
this.leftHandle.type = "FREE";
|
|
1368
|
-
}
|
|
1369
|
-
let relativeVector;
|
|
1370
|
-
if (nextControlPoint == undefined) {
|
|
1371
|
-
relativeVector = PointCal3.subVector(this.rightHandle.position, this.position);
|
|
1372
|
-
} else {
|
|
1373
|
-
relativeVector = PointCal3.subVector(nextControlPoint.getPosition(), this.position);
|
|
1374
|
-
relativeVector = PointCal3.multiplyVectorByScalar(relativeVector, 1 / 3);
|
|
1375
|
-
}
|
|
1376
|
-
this.rightHandle.position = PointCal3.addVector(this.position, relativeVector);
|
|
1377
|
-
}
|
|
1378
|
-
setRightHandleTypeAligned() {
|
|
1379
|
-
this.rightHandle.type = "ALIGNED";
|
|
1380
|
-
if (this.leftHandle.type == "VECTOR") {
|
|
1381
|
-
let direciton = PointCal3.unitVectorFromA2B(this.leftHandle.position, this.position);
|
|
1382
|
-
let mag = PointCal3.distanceBetweenPoints(this.position, this.rightHandle.position);
|
|
1383
|
-
this.rightHandle.position = PointCal3.addVector(this.position, PointCal3.multiplyVectorByScalar(direciton, mag));
|
|
1384
|
-
}
|
|
1385
|
-
}
|
|
1386
|
-
setRightHandleTypeFree() {
|
|
1387
|
-
this.rightHandle.type = "FREE";
|
|
1388
|
-
}
|
|
1389
|
-
setLeftHandlePosition(destPos) {
|
|
1390
|
-
let leftHandleType = this.leftHandle.type;
|
|
1391
|
-
switch (leftHandleType) {
|
|
1392
|
-
case "ALIGNED":
|
|
1393
|
-
if (this.rightHandle.type == "VECTOR") {
|
|
1394
|
-
let diff = PointCal3.subVector(destPos, this.position);
|
|
1395
|
-
let rightHandleDiff = PointCal3.unitVectorFromA2B(this.rightHandle.position, this.position);
|
|
1396
|
-
let resMag = PointCal3.dotProduct(diff, rightHandleDiff);
|
|
1397
|
-
let res = PointCal3.multiplyVectorByScalar(rightHandleDiff, resMag);
|
|
1398
|
-
this.leftHandle.position = PointCal3.addVector(this.position, res);
|
|
1399
|
-
} else if (this.rightHandle.type == "ALIGNED") {
|
|
1400
|
-
this.leftHandle.position = destPos;
|
|
1401
|
-
let mag = PointCal3.distanceBetweenPoints(this.rightHandle.position, this.position);
|
|
1402
|
-
let direction = PointCal3.unitVectorFromA2B(this.leftHandle.position, this.position);
|
|
1403
|
-
let res = PointCal3.multiplyVectorByScalar(direction, mag);
|
|
1404
|
-
this.rightHandle.position = PointCal3.addVector(res, this.position);
|
|
1405
|
-
} else {
|
|
1406
|
-
this.leftHandle.position = destPos;
|
|
1407
|
-
}
|
|
1408
|
-
break;
|
|
1409
|
-
case "FREE":
|
|
1410
|
-
this.leftHandle.position = destPos;
|
|
1411
|
-
break;
|
|
1412
|
-
case "VECTOR":
|
|
1413
|
-
break;
|
|
1414
|
-
default:
|
|
1415
|
-
throw new Error(`Unknown left handle type for control point`);
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
setRightHandlePosition(destPos) {
|
|
1419
|
-
let rightHandleType = this.rightHandle.type;
|
|
1420
|
-
switch (rightHandleType) {
|
|
1421
|
-
case "ALIGNED":
|
|
1422
|
-
if (this.leftHandle.type == "VECTOR") {
|
|
1423
|
-
let diff = PointCal3.subVector(destPos, this.position);
|
|
1424
|
-
let leftHandleDiff = PointCal3.unitVectorFromA2B(this.leftHandle.position, this.position);
|
|
1425
|
-
let resMag = PointCal3.dotProduct(diff, leftHandleDiff);
|
|
1426
|
-
let res = PointCal3.multiplyVectorByScalar(leftHandleDiff, resMag);
|
|
1427
|
-
this.rightHandle.position = PointCal3.addVector(this.position, res);
|
|
1428
|
-
} else if (this.rightHandle.type == "ALIGNED") {
|
|
1429
|
-
this.rightHandle.position = destPos;
|
|
1430
|
-
let mag = PointCal3.distanceBetweenPoints(this.leftHandle.position, this.position);
|
|
1431
|
-
let direction = PointCal3.unitVectorFromA2B(this.rightHandle.position, this.position);
|
|
1432
|
-
let res = PointCal3.multiplyVectorByScalar(direction, mag);
|
|
1433
|
-
this.leftHandle.position = PointCal3.addVector(res, this.position);
|
|
1434
|
-
} else {
|
|
1435
|
-
this.rightHandle.position = destPos;
|
|
1436
|
-
}
|
|
1437
|
-
break;
|
|
1438
|
-
case "FREE":
|
|
1439
|
-
this.rightHandle.position = destPos;
|
|
1440
|
-
break;
|
|
1441
|
-
case "VECTOR":
|
|
1442
|
-
break;
|
|
1443
|
-
default:
|
|
1444
|
-
throw new Error(`Unknown left handle type for control point`);
|
|
1445
|
-
}
|
|
1446
|
-
}
|
|
1447
|
-
getLeftHandle() {
|
|
1448
|
-
return this.leftHandle;
|
|
1449
|
-
}
|
|
1450
|
-
getRightHandle() {
|
|
1451
|
-
return this.rightHandle;
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
class CompositeBCurve {
|
|
1456
|
-
controlPoints;
|
|
1457
|
-
constructor(controlPoints = []) {
|
|
1458
|
-
this.controlPoints = controlPoints;
|
|
1459
|
-
}
|
|
1460
|
-
getControlPoints() {
|
|
1461
|
-
return this.controlPoints;
|
|
1462
|
-
}
|
|
1463
|
-
appendControlPoint(position) {
|
|
1464
|
-
let leftHandlePosition = PointCal3.addVector(position, { x: -100, y: 0 });
|
|
1465
|
-
let rightHandlePosition = PointCal3.addVector(position, { x: 100, y: 0 });
|
|
1466
|
-
let leftHandlePoint = {
|
|
1467
|
-
position: leftHandlePosition,
|
|
1468
|
-
type: "FREE"
|
|
1469
|
-
};
|
|
1470
|
-
let rightHandlePoint = {
|
|
1471
|
-
position: rightHandlePosition,
|
|
1472
|
-
type: "FREE"
|
|
1473
|
-
};
|
|
1474
|
-
let newControlPoint = new ControlPoint(position, leftHandlePoint, rightHandlePoint);
|
|
1475
|
-
this.controlPoints.push(newControlPoint);
|
|
1476
|
-
}
|
|
1477
|
-
setLeftHandlePositionOfControlPoint(controlPointIndex, destPos) {
|
|
1478
|
-
if (controlPointIndex >= this.controlPoints.length || controlPointIndex < 0) {
|
|
1479
|
-
return;
|
|
1480
|
-
}
|
|
1481
|
-
this.controlPoints[controlPointIndex].setLeftHandlePosition(destPos);
|
|
1482
|
-
}
|
|
1483
|
-
setRightHandlePositionOfControlPoint(controlPointIndex, destPos) {
|
|
1484
|
-
if (controlPointIndex >= this.controlPoints.length || controlPointIndex < 0) {
|
|
1485
|
-
return;
|
|
1486
|
-
}
|
|
1487
|
-
this.controlPoints[controlPointIndex].setRightHandlePosition(destPos);
|
|
1488
|
-
}
|
|
1489
|
-
setPositionOfControlPoint(controlPointIndex, destPos) {
|
|
1490
|
-
if (controlPointIndex >= this.controlPoints.length || controlPointIndex < 0) {
|
|
1491
|
-
return;
|
|
1492
|
-
}
|
|
1493
|
-
let prevControlPoint = undefined;
|
|
1494
|
-
let nextControlPoint = undefined;
|
|
1495
|
-
if (controlPointIndex + 1 < this.controlPoints.length) {
|
|
1496
|
-
nextControlPoint = this.controlPoints[controlPointIndex + 1];
|
|
1497
|
-
}
|
|
1498
|
-
if (controlPointIndex - 1 >= 0) {
|
|
1499
|
-
prevControlPoint = this.controlPoints[controlPointIndex - 1];
|
|
1500
|
-
}
|
|
1501
|
-
this.controlPoints[controlPointIndex].setPosition(destPos, prevControlPoint, nextControlPoint);
|
|
1502
|
-
}
|
|
1503
|
-
}
|
|
1504
|
-
// src/path.ts
|
|
1505
|
-
class Path {
|
|
1506
|
-
lines;
|
|
1507
|
-
constructor(lines) {
|
|
1508
|
-
this.lines = lines;
|
|
1509
|
-
}
|
|
1510
|
-
append(line) {
|
|
1511
|
-
this.lines.push(line);
|
|
1512
|
-
}
|
|
1513
|
-
clear() {
|
|
1514
|
-
this.lines = [];
|
|
1515
|
-
}
|
|
1516
|
-
prepend(line) {
|
|
1517
|
-
this.lines.unshift(line);
|
|
1518
|
-
}
|
|
1519
|
-
getLines() {
|
|
1520
|
-
return this.lines;
|
|
1521
|
-
}
|
|
1522
|
-
getLength() {
|
|
1523
|
-
let res = 0;
|
|
1524
|
-
this.lines.forEach((line) => {
|
|
1525
|
-
res += line.length();
|
|
1526
|
-
});
|
|
1527
|
-
return res;
|
|
1528
|
-
}
|
|
1529
|
-
getPercentages() {
|
|
1530
|
-
const length = this.getLength();
|
|
1531
|
-
let currentCurvePercentage = 0;
|
|
1532
|
-
const res = [];
|
|
1533
|
-
this.lines.forEach((line) => {
|
|
1534
|
-
const lineLength = line.length();
|
|
1535
|
-
const linePercentage = lineLength / length;
|
|
1536
|
-
let start = currentCurvePercentage;
|
|
1537
|
-
currentCurvePercentage += linePercentage;
|
|
1538
|
-
let end = currentCurvePercentage;
|
|
1539
|
-
res.push({ start, end });
|
|
1540
|
-
});
|
|
1541
|
-
res[res.length - 1].end = 1;
|
|
1542
|
-
return res;
|
|
1543
|
-
}
|
|
1544
|
-
getPointByPercentage(percentage) {
|
|
1545
|
-
if (percentage < 0 || percentage > 1) {
|
|
1546
|
-
throw new Error("Percentage must be between 0 and 1");
|
|
1547
|
-
}
|
|
1548
|
-
const percentages = this.getPercentages();
|
|
1549
|
-
let left = 0;
|
|
1550
|
-
let right = percentages.length - 1;
|
|
1551
|
-
while (left <= right) {
|
|
1552
|
-
const mid = Math.floor((left + right) / 2);
|
|
1553
|
-
if (percentage < percentages[mid].end) {
|
|
1554
|
-
right = mid - 1;
|
|
1555
|
-
} else if (percentage > percentages[mid].end) {
|
|
1556
|
-
left = mid + 1;
|
|
1557
|
-
} else {
|
|
1558
|
-
left = mid;
|
|
1559
|
-
break;
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
const line = this.lines[left];
|
|
1563
|
-
const linePercentage = percentages[left];
|
|
1564
|
-
const ratio = (percentage - linePercentage.start) / (linePercentage.end - linePercentage.start);
|
|
1565
|
-
return line.lerp(ratio);
|
|
1566
|
-
}
|
|
1567
|
-
}
|
|
1568
|
-
export {
|
|
1569
|
-
solveCubic,
|
|
1570
|
-
reduce,
|
|
1571
|
-
projectPointOntoLine,
|
|
1572
|
-
offset2,
|
|
1573
|
-
offset,
|
|
1574
|
-
getLineIntersection,
|
|
1575
|
-
getIntersectionsBetweenCurves,
|
|
1576
|
-
getCubicRoots,
|
|
1577
|
-
cuberoot2,
|
|
1578
|
-
cuberoot,
|
|
1579
|
-
computeWithControlPoints,
|
|
1580
|
-
approximately,
|
|
1581
|
-
accept,
|
|
1582
|
-
TValOutofBoundError,
|
|
1583
|
-
Path,
|
|
1584
|
-
Line,
|
|
1585
|
-
ControlPoint,
|
|
1586
|
-
CompositeBCurve,
|
|
1587
|
-
BCurve,
|
|
1588
|
-
AABBIntersects
|
|
1589
|
-
};
|
|
1590
|
-
|
|
1591
|
-
//# debugId=EABF32F5EF0F7EC864756E2164756E21
|
|
3
|
+
//# debugId=B487176DCF176FA664756E2164756E21
|