@remotion/paths 3.2.11
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/.prettierrc.js +14 -0
- package/LICENSE.md +7 -0
- package/README.md +25 -0
- package/dist/arc.d.ts +12 -0
- package/dist/arc.js +231 -0
- package/dist/bezier-functions.d.ts +11 -0
- package/dist/bezier-functions.js +138 -0
- package/dist/bezier-values.d.ts +3 -0
- package/dist/bezier-values.js +693 -0
- package/dist/bezier.d.ts +26 -0
- package/dist/bezier.js +81 -0
- package/dist/construct.d.ts +7 -0
- package/dist/construct.js +308 -0
- package/dist/evolve-path.d.ts +10 -0
- package/dist/evolve-path.js +17 -0
- package/dist/get-length.d.ts +6 -0
- package/dist/get-length.js +15 -0
- package/dist/get-part-at-length.d.ts +4 -0
- package/dist/get-part-at-length.js +20 -0
- package/dist/get-parts.d.ts +7 -0
- package/dist/get-parts.js +31 -0
- package/dist/get-point-at-length.d.ts +7 -0
- package/dist/get-point-at-length.js +24 -0
- package/dist/get-properties-at-length.d.ts +1 -0
- package/dist/get-properties-at-length.js +23 -0
- package/dist/get-reversed-path.d.ts +12 -0
- package/dist/get-reversed-path.js +159 -0
- package/dist/get-tangent-at-length.d.ts +7 -0
- package/dist/get-tangent-at-length.js +24 -0
- package/dist/helpers/arc.d.ts +12 -0
- package/dist/helpers/arc.js +226 -0
- package/dist/helpers/bezier-functions.d.ts +11 -0
- package/dist/helpers/bezier-functions.js +135 -0
- package/dist/helpers/bezier-values.d.ts +3 -0
- package/dist/helpers/bezier-values.js +694 -0
- package/dist/helpers/bezier.d.ts +26 -0
- package/dist/helpers/bezier.js +82 -0
- package/dist/helpers/construct.d.ts +7 -0
- package/dist/helpers/construct.js +309 -0
- package/dist/helpers/get-part-at-length.d.ts +4 -0
- package/dist/helpers/get-part-at-length.js +20 -0
- package/dist/helpers/linear.d.ts +7 -0
- package/dist/helpers/linear.js +30 -0
- package/dist/helpers/parse.d.ts +2 -0
- package/dist/helpers/parse.js +49 -0
- package/dist/helpers/split-curve.d.ts +47 -0
- package/dist/helpers/split-curve.js +190 -0
- package/dist/helpers/types.d.ts +22 -0
- package/dist/helpers/types.js +3 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +19 -0
- package/dist/interpolate-path.d.ts +8 -0
- package/dist/interpolate-path.js +366 -0
- package/dist/linear.d.ts +7 -0
- package/dist/linear.js +31 -0
- package/dist/normalize-path.d.ts +6 -0
- package/dist/normalize-path.js +300 -0
- package/dist/parse.d.ts +2 -0
- package/dist/parse.js +48 -0
- package/dist/reverse-path.d.ts +12 -0
- package/dist/reverse-path.js +148 -0
- package/dist/types.d.ts +26 -0
- package/dist/types.js +2 -0
- package/package.json +39 -0
- package/tsconfig.json +9 -0
package/.prettierrc.js
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2022 JonnyBurger
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
## @remotion/paths
|
|
2
|
+
|
|
3
|
+
A package providing utility functions for dealing with SVG paths. This package consists of code from [`svg-path-properties`](https://www.npmjs.com/package/svg-path-properties) and [`svg-path-reverse`](https://github.com/Pomax/svg-path-reverse#readme) with the following improvements:
|
|
4
|
+
|
|
5
|
+
- Functional style APIs
|
|
6
|
+
- First class Typescript types
|
|
7
|
+
- Documentation with examples
|
|
8
|
+
- ESM import style
|
|
9
|
+
|
|
10
|
+
No dependencies are needed, meaning this package can be used independently of Remotion.
|
|
11
|
+
|
|
12
|
+
## API
|
|
13
|
+
|
|
14
|
+
- [`getLength()`](https://remotion.dev/docs/paths/get-length)
|
|
15
|
+
- [`getParts()`](https://remotion.dev/docs/paths/get-parts)
|
|
16
|
+
- [`getPointAtLength()`](https://remotion.dev/docs/paths/get-point-at-length)
|
|
17
|
+
- [`getTangentAtLength()`](https://remotion.dev/docs/paths/get-tangent-at-length)
|
|
18
|
+
- [`reversePath()`](https://remotion.dev/docs/paths/reverse-path)
|
|
19
|
+
- [`normalizePath()`](https://remotion.dev/docs/paths/normalize-path)
|
|
20
|
+
- [`interpolatePath()`](https://remotion.dev/docs/paths/interpolate-path)
|
|
21
|
+
- [`evolvePath()`](https://remotion.dev/docs/paths/evolve-path)
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
MIT
|
package/dist/arc.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Properties } from './types';
|
|
2
|
+
export declare const makeArc: ({ x0, y0, rx, ry, xAxisRotate, LargeArcFlag, SweepFlag, x1, y1, }: {
|
|
3
|
+
x0: number;
|
|
4
|
+
y0: number;
|
|
5
|
+
rx: number;
|
|
6
|
+
ry: number;
|
|
7
|
+
xAxisRotate: number;
|
|
8
|
+
LargeArcFlag: boolean;
|
|
9
|
+
SweepFlag: boolean;
|
|
10
|
+
x1: number;
|
|
11
|
+
y1: number;
|
|
12
|
+
}) => Properties;
|
package/dist/arc.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeArc = void 0;
|
|
4
|
+
const makeArc = ({ x0, y0, rx, ry, xAxisRotate, LargeArcFlag, SweepFlag, x1, y1, }) => {
|
|
5
|
+
const lengthProperties = approximateArcLengthOfCurve(300, (t) => {
|
|
6
|
+
return pointOnEllipticalArc({
|
|
7
|
+
p0: { x: x0, y: y0 },
|
|
8
|
+
rx,
|
|
9
|
+
ry,
|
|
10
|
+
xAxisRotation: xAxisRotate,
|
|
11
|
+
largeArcFlag: LargeArcFlag,
|
|
12
|
+
sweepFlag: SweepFlag,
|
|
13
|
+
p1: { x: x1, y: y1 },
|
|
14
|
+
t,
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
const length = lengthProperties.arcLength;
|
|
18
|
+
const getTotalLength = () => {
|
|
19
|
+
return length;
|
|
20
|
+
};
|
|
21
|
+
const getPointAtLength = (fractionLength) => {
|
|
22
|
+
if (fractionLength < 0) {
|
|
23
|
+
fractionLength = 0;
|
|
24
|
+
}
|
|
25
|
+
else if (fractionLength > length) {
|
|
26
|
+
fractionLength = length;
|
|
27
|
+
}
|
|
28
|
+
const position = pointOnEllipticalArc({
|
|
29
|
+
p0: { x: x0, y: y0 },
|
|
30
|
+
rx,
|
|
31
|
+
ry,
|
|
32
|
+
xAxisRotation: xAxisRotate,
|
|
33
|
+
largeArcFlag: LargeArcFlag,
|
|
34
|
+
sweepFlag: SweepFlag,
|
|
35
|
+
p1: { x: x1, y: y1 },
|
|
36
|
+
t: fractionLength / length,
|
|
37
|
+
});
|
|
38
|
+
return { x: position.x, y: position.y };
|
|
39
|
+
};
|
|
40
|
+
const getTangentAtLength = (fractionLength) => {
|
|
41
|
+
if (fractionLength < 0) {
|
|
42
|
+
fractionLength = 0;
|
|
43
|
+
}
|
|
44
|
+
else if (fractionLength > length) {
|
|
45
|
+
fractionLength = length;
|
|
46
|
+
}
|
|
47
|
+
const point_dist = 0.05; // needs testing
|
|
48
|
+
const p1 = getPointAtLength(fractionLength);
|
|
49
|
+
let p2;
|
|
50
|
+
if (fractionLength < 0) {
|
|
51
|
+
fractionLength = 0;
|
|
52
|
+
}
|
|
53
|
+
else if (fractionLength > length) {
|
|
54
|
+
fractionLength = length;
|
|
55
|
+
}
|
|
56
|
+
if (fractionLength < length - point_dist) {
|
|
57
|
+
p2 = getPointAtLength(fractionLength + point_dist);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
p2 = getPointAtLength(fractionLength - point_dist);
|
|
61
|
+
}
|
|
62
|
+
const xDist = p2.x - p1.x;
|
|
63
|
+
const yDist = p2.y - p1.y;
|
|
64
|
+
const dist = Math.sqrt(xDist * xDist + yDist * yDist);
|
|
65
|
+
if (fractionLength < length - point_dist) {
|
|
66
|
+
return { x: -xDist / dist, y: -yDist / dist };
|
|
67
|
+
}
|
|
68
|
+
return { x: xDist / dist, y: yDist / dist };
|
|
69
|
+
};
|
|
70
|
+
const getPropertiesAtLength = (fractionLength) => {
|
|
71
|
+
const tangent = getTangentAtLength(fractionLength);
|
|
72
|
+
const point = getPointAtLength(fractionLength);
|
|
73
|
+
return { x: point.x, y: point.y, tangentX: tangent.x, tangentY: tangent.y };
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
getPointAtLength,
|
|
77
|
+
getPropertiesAtLength,
|
|
78
|
+
getTangentAtLength,
|
|
79
|
+
getTotalLength,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
exports.makeArc = makeArc;
|
|
83
|
+
const pointOnEllipticalArc = ({ p0, rx, ry, xAxisRotation, largeArcFlag, sweepFlag, p1, t, }) => {
|
|
84
|
+
// In accordance to: http://www.w3.org/TR/SVG/implnote.html#ArcOutOfRangeParameters
|
|
85
|
+
rx = Math.abs(rx);
|
|
86
|
+
ry = Math.abs(ry);
|
|
87
|
+
xAxisRotation = mod(xAxisRotation, 360);
|
|
88
|
+
const xAxisRotationRadians = toRadians(xAxisRotation);
|
|
89
|
+
// If the endpoints are identical, then this is equivalent to omitting the elliptical arc segment entirely.
|
|
90
|
+
if (p0.x === p1.x && p0.y === p1.y) {
|
|
91
|
+
return { x: p0.x, y: p0.y, ellipticalArcAngle: 0 }; // Check if angle is correct
|
|
92
|
+
}
|
|
93
|
+
// If rx = 0 or ry = 0 then this arc is treated as a straight line segment joining the endpoints.
|
|
94
|
+
if (rx === 0 || ry === 0) {
|
|
95
|
+
// return this.pointOnLine(p0, p1, t);
|
|
96
|
+
return { x: 0, y: 0, ellipticalArcAngle: 0 }; // Check if angle is correct
|
|
97
|
+
}
|
|
98
|
+
// Following "Conversion from endpoint to center parameterization"
|
|
99
|
+
// http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
|
|
100
|
+
// Step #1: Compute transformedPoint
|
|
101
|
+
const dx = (p0.x - p1.x) / 2;
|
|
102
|
+
const dy = (p0.y - p1.y) / 2;
|
|
103
|
+
const transformedPoint = {
|
|
104
|
+
x: Math.cos(xAxisRotationRadians) * dx + Math.sin(xAxisRotationRadians) * dy,
|
|
105
|
+
y: -Math.sin(xAxisRotationRadians) * dx +
|
|
106
|
+
Math.cos(xAxisRotationRadians) * dy,
|
|
107
|
+
};
|
|
108
|
+
// Ensure radii are large enough
|
|
109
|
+
const radiiCheck = transformedPoint.x ** 2 / rx ** 2 + transformedPoint.y ** 2 / ry ** 2;
|
|
110
|
+
if (radiiCheck > 1) {
|
|
111
|
+
rx *= Math.sqrt(radiiCheck);
|
|
112
|
+
ry *= Math.sqrt(radiiCheck);
|
|
113
|
+
}
|
|
114
|
+
// Step #2: Compute transformedCenter
|
|
115
|
+
const cSquareNumerator = rx ** 2 * ry ** 2 -
|
|
116
|
+
rx ** 2 * transformedPoint.y ** 2 -
|
|
117
|
+
ry ** 2 * transformedPoint.x ** 2;
|
|
118
|
+
const cSquareRootDenom = rx ** 2 * transformedPoint.y ** 2 + ry ** 2 * transformedPoint.x ** 2;
|
|
119
|
+
let cRadicand = cSquareNumerator / cSquareRootDenom;
|
|
120
|
+
// Make sure this never drops below zero because of precision
|
|
121
|
+
cRadicand = cRadicand < 0 ? 0 : cRadicand;
|
|
122
|
+
const cCoef = (largeArcFlag === sweepFlag ? -1 : 1) * Math.sqrt(cRadicand);
|
|
123
|
+
const transformedCenter = {
|
|
124
|
+
x: cCoef * ((rx * transformedPoint.y) / ry),
|
|
125
|
+
y: cCoef * (-(ry * transformedPoint.x) / rx),
|
|
126
|
+
};
|
|
127
|
+
// Step #3: Compute center
|
|
128
|
+
const center = {
|
|
129
|
+
x: Math.cos(xAxisRotationRadians) * transformedCenter.x -
|
|
130
|
+
Math.sin(xAxisRotationRadians) * transformedCenter.y +
|
|
131
|
+
(p0.x + p1.x) / 2,
|
|
132
|
+
y: Math.sin(xAxisRotationRadians) * transformedCenter.x +
|
|
133
|
+
Math.cos(xAxisRotationRadians) * transformedCenter.y +
|
|
134
|
+
(p0.y + p1.y) / 2,
|
|
135
|
+
};
|
|
136
|
+
// Step #4: Compute start/sweep angles
|
|
137
|
+
// Start angle of the elliptical arc prior to the stretch and rotate operations.
|
|
138
|
+
// Difference between the start and end angles
|
|
139
|
+
const startVector = {
|
|
140
|
+
x: (transformedPoint.x - transformedCenter.x) / rx,
|
|
141
|
+
y: (transformedPoint.y - transformedCenter.y) / ry,
|
|
142
|
+
};
|
|
143
|
+
const startAngle = angleBetween({
|
|
144
|
+
x: 1,
|
|
145
|
+
y: 0,
|
|
146
|
+
}, startVector);
|
|
147
|
+
const endVector = {
|
|
148
|
+
x: (-transformedPoint.x - transformedCenter.x) / rx,
|
|
149
|
+
y: (-transformedPoint.y - transformedCenter.y) / ry,
|
|
150
|
+
};
|
|
151
|
+
let sweepAngle = angleBetween(startVector, endVector);
|
|
152
|
+
if (!sweepFlag && sweepAngle > 0) {
|
|
153
|
+
sweepAngle -= 2 * Math.PI;
|
|
154
|
+
}
|
|
155
|
+
else if (sweepFlag && sweepAngle < 0) {
|
|
156
|
+
sweepAngle += 2 * Math.PI;
|
|
157
|
+
}
|
|
158
|
+
// We use % instead of `mod(..)` because we want it to be -360deg to 360deg(but actually in radians)
|
|
159
|
+
sweepAngle %= 2 * Math.PI;
|
|
160
|
+
// From http://www.w3.org/TR/SVG/implnote.html#ArcParameterizationAlternatives
|
|
161
|
+
const angle = startAngle + sweepAngle * t;
|
|
162
|
+
const ellipseComponentX = rx * Math.cos(angle);
|
|
163
|
+
const ellipseComponentY = ry * Math.sin(angle);
|
|
164
|
+
const point = {
|
|
165
|
+
x: Math.cos(xAxisRotationRadians) * ellipseComponentX -
|
|
166
|
+
Math.sin(xAxisRotationRadians) * ellipseComponentY +
|
|
167
|
+
center.x,
|
|
168
|
+
y: Math.sin(xAxisRotationRadians) * ellipseComponentX +
|
|
169
|
+
Math.cos(xAxisRotationRadians) * ellipseComponentY +
|
|
170
|
+
center.y,
|
|
171
|
+
ellipticalArcStartAngle: startAngle,
|
|
172
|
+
ellipticalArcEndAngle: startAngle + sweepAngle,
|
|
173
|
+
ellipticalArcAngle: angle,
|
|
174
|
+
ellipticalArcCenter: center,
|
|
175
|
+
resultantRx: rx,
|
|
176
|
+
resultantRy: ry,
|
|
177
|
+
};
|
|
178
|
+
return point;
|
|
179
|
+
};
|
|
180
|
+
const approximateArcLengthOfCurve = (resolution, pointOnCurveFunc) => {
|
|
181
|
+
// Resolution is the number of segments we use
|
|
182
|
+
resolution = resolution ? resolution : 500;
|
|
183
|
+
let resultantArcLength = 0;
|
|
184
|
+
const arcLengthMap = [];
|
|
185
|
+
const approximationLines = [];
|
|
186
|
+
let prevPoint = pointOnCurveFunc(0);
|
|
187
|
+
let nextPoint;
|
|
188
|
+
for (let i = 0; i < resolution; i++) {
|
|
189
|
+
const t = clamp(i * (1 / resolution), 0, 1);
|
|
190
|
+
nextPoint = pointOnCurveFunc(t);
|
|
191
|
+
resultantArcLength += distance(prevPoint, nextPoint);
|
|
192
|
+
approximationLines.push([prevPoint, nextPoint]);
|
|
193
|
+
arcLengthMap.push({
|
|
194
|
+
t,
|
|
195
|
+
arcLength: resultantArcLength,
|
|
196
|
+
});
|
|
197
|
+
prevPoint = nextPoint;
|
|
198
|
+
}
|
|
199
|
+
// Last stretch to the endpoint
|
|
200
|
+
nextPoint = pointOnCurveFunc(1);
|
|
201
|
+
approximationLines.push([prevPoint, nextPoint]);
|
|
202
|
+
resultantArcLength += distance(prevPoint, nextPoint);
|
|
203
|
+
arcLengthMap.push({
|
|
204
|
+
t: 1,
|
|
205
|
+
arcLength: resultantArcLength,
|
|
206
|
+
});
|
|
207
|
+
return {
|
|
208
|
+
arcLength: resultantArcLength,
|
|
209
|
+
arcLengthMap,
|
|
210
|
+
approximationLines,
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
const mod = (x, m) => {
|
|
214
|
+
return ((x % m) + m) % m;
|
|
215
|
+
};
|
|
216
|
+
const toRadians = (angle) => {
|
|
217
|
+
return angle * (Math.PI / 180);
|
|
218
|
+
};
|
|
219
|
+
const distance = (p0, p1) => {
|
|
220
|
+
return Math.sqrt((p1.x - p0.x) ** 2 + (p1.y - p0.y) ** 2);
|
|
221
|
+
};
|
|
222
|
+
const clamp = (val, min, max) => {
|
|
223
|
+
return Math.min(Math.max(val, min), max);
|
|
224
|
+
};
|
|
225
|
+
const angleBetween = (v0, v1) => {
|
|
226
|
+
const p = v0.x * v1.x + v0.y * v1.y;
|
|
227
|
+
const n = Math.sqrt((v0.x ** 2 + v0.y ** 2) * (v1.x ** 2 + v1.y ** 2));
|
|
228
|
+
const sign = v0.x * v1.y - v0.y * v1.x < 0 ? -1 : 1;
|
|
229
|
+
const angle = sign * Math.acos(p / n);
|
|
230
|
+
return angle;
|
|
231
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Point } from './types';
|
|
2
|
+
export declare const cubicPoint: (xs: number[], ys: number[], t: number) => Point;
|
|
3
|
+
export declare const cubicDerivative: (xs: number[], ys: number[], t: number) => Point;
|
|
4
|
+
export declare const getCubicArcLength: (xs: number[], ys: number[], t: number) => number;
|
|
5
|
+
export declare const quadraticPoint: (xs: number[], ys: number[], t: number) => Point;
|
|
6
|
+
export declare const getQuadraticArcLength: (xs: number[], ys: number[], t: number) => number;
|
|
7
|
+
export declare const quadraticDerivative: (xs: number[], ys: number[], t: number) => {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
};
|
|
11
|
+
export declare const t2length: (length: number, totalLength: number, func: (t: number) => number) => number;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.t2length = exports.quadraticDerivative = exports.getQuadraticArcLength = exports.quadraticPoint = exports.getCubicArcLength = exports.cubicDerivative = exports.cubicPoint = void 0;
|
|
4
|
+
const bezier_values_1 = require("./bezier-values");
|
|
5
|
+
const cubicPoint = (xs, ys, t) => {
|
|
6
|
+
const x = (1 - t) * (1 - t) * (1 - t) * xs[0] +
|
|
7
|
+
3 * (1 - t) * (1 - t) * t * xs[1] +
|
|
8
|
+
3 * (1 - t) * t * t * xs[2] +
|
|
9
|
+
t * t * t * xs[3];
|
|
10
|
+
const y = (1 - t) * (1 - t) * (1 - t) * ys[0] +
|
|
11
|
+
3 * (1 - t) * (1 - t) * t * ys[1] +
|
|
12
|
+
3 * (1 - t) * t * t * ys[2] +
|
|
13
|
+
t * t * t * ys[3];
|
|
14
|
+
return { x, y };
|
|
15
|
+
};
|
|
16
|
+
exports.cubicPoint = cubicPoint;
|
|
17
|
+
const cubicDerivative = (xs, ys, t) => {
|
|
18
|
+
const derivative = (0, exports.quadraticPoint)([3 * (xs[1] - xs[0]), 3 * (xs[2] - xs[1]), 3 * (xs[3] - xs[2])], [3 * (ys[1] - ys[0]), 3 * (ys[2] - ys[1]), 3 * (ys[3] - ys[2])], t);
|
|
19
|
+
return derivative;
|
|
20
|
+
};
|
|
21
|
+
exports.cubicDerivative = cubicDerivative;
|
|
22
|
+
const getCubicArcLength = (xs, ys, t) => {
|
|
23
|
+
let correctedT;
|
|
24
|
+
/* if (xs.length >= tValues.length) {
|
|
25
|
+
throw new Error('too high n bezier');
|
|
26
|
+
} */
|
|
27
|
+
const n = 20;
|
|
28
|
+
const z = t / 2;
|
|
29
|
+
let sum = 0;
|
|
30
|
+
for (let i = 0; i < n; i++) {
|
|
31
|
+
correctedT = z * bezier_values_1.tValues[n][i] + z;
|
|
32
|
+
sum += bezier_values_1.cValues[n][i] * bFunc(xs, ys, correctedT);
|
|
33
|
+
}
|
|
34
|
+
return z * sum;
|
|
35
|
+
};
|
|
36
|
+
exports.getCubicArcLength = getCubicArcLength;
|
|
37
|
+
const quadraticPoint = (xs, ys, t) => {
|
|
38
|
+
const x = (1 - t) * (1 - t) * xs[0] + 2 * (1 - t) * t * xs[1] + t * t * xs[2];
|
|
39
|
+
const y = (1 - t) * (1 - t) * ys[0] + 2 * (1 - t) * t * ys[1] + t * t * ys[2];
|
|
40
|
+
return { x, y };
|
|
41
|
+
};
|
|
42
|
+
exports.quadraticPoint = quadraticPoint;
|
|
43
|
+
const getQuadraticArcLength = (xs, ys, t) => {
|
|
44
|
+
if (t === undefined) {
|
|
45
|
+
t = 1;
|
|
46
|
+
}
|
|
47
|
+
const ax = xs[0] - 2 * xs[1] + xs[2];
|
|
48
|
+
const ay = ys[0] - 2 * ys[1] + ys[2];
|
|
49
|
+
const bx = 2 * xs[1] - 2 * xs[0];
|
|
50
|
+
const by = 2 * ys[1] - 2 * ys[0];
|
|
51
|
+
const A = 4 * (ax * ax + ay * ay);
|
|
52
|
+
const B = 4 * (ax * bx + ay * by);
|
|
53
|
+
const C = bx * bx + by * by;
|
|
54
|
+
if (A === 0) {
|
|
55
|
+
return t * Math.sqrt((xs[2] - xs[0]) ** 2 + (ys[2] - ys[0]) ** 2);
|
|
56
|
+
}
|
|
57
|
+
const b = B / (2 * A);
|
|
58
|
+
const c = C / A;
|
|
59
|
+
const u = t + b;
|
|
60
|
+
const k = c - b * b;
|
|
61
|
+
const uuk = u * u + k > 0 ? Math.sqrt(u * u + k) : 0;
|
|
62
|
+
const bbk = b * b + k > 0 ? Math.sqrt(b * b + k) : 0;
|
|
63
|
+
const term = b + Math.sqrt(b * b + k) === 0
|
|
64
|
+
? 0
|
|
65
|
+
: k * Math.log(Math.abs((u + uuk) / (b + bbk)));
|
|
66
|
+
return (Math.sqrt(A) / 2) * (u * uuk - b * bbk + term);
|
|
67
|
+
};
|
|
68
|
+
exports.getQuadraticArcLength = getQuadraticArcLength;
|
|
69
|
+
const quadraticDerivative = (xs, ys, t) => {
|
|
70
|
+
return {
|
|
71
|
+
x: (1 - t) * 2 * (xs[1] - xs[0]) + t * 2 * (xs[2] - xs[1]),
|
|
72
|
+
y: (1 - t) * 2 * (ys[1] - ys[0]) + t * 2 * (ys[2] - ys[1]),
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
exports.quadraticDerivative = quadraticDerivative;
|
|
76
|
+
function bFunc(xs, ys, t) {
|
|
77
|
+
const xbase = getDerivative(1, t, xs);
|
|
78
|
+
const ybase = getDerivative(1, t, ys);
|
|
79
|
+
const combined = xbase * xbase + ybase * ybase;
|
|
80
|
+
return Math.sqrt(combined);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Compute the curve derivative (hodograph) at t.
|
|
84
|
+
*/
|
|
85
|
+
const getDerivative = (derivative, t, vs) => {
|
|
86
|
+
// the derivative of any 't'-less function is zero.
|
|
87
|
+
const n = vs.length - 1;
|
|
88
|
+
let value;
|
|
89
|
+
if (n === 0) {
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
// direct values? compute!
|
|
93
|
+
if (derivative === 0) {
|
|
94
|
+
value = 0;
|
|
95
|
+
for (let k = 0; k <= n; k++) {
|
|
96
|
+
value += bezier_values_1.binomialCoefficients[n][k] * (1 - t) ** (n - k) * t ** k * vs[k];
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
// Still some derivative? go down one order, then try
|
|
101
|
+
// for the lower order curve's.
|
|
102
|
+
const _vs = new Array(n);
|
|
103
|
+
for (let k = 0; k < n; k++) {
|
|
104
|
+
_vs[k] = n * (vs[k + 1] - vs[k]);
|
|
105
|
+
}
|
|
106
|
+
return getDerivative(derivative - 1, t, _vs);
|
|
107
|
+
};
|
|
108
|
+
const t2length = (length, totalLength, func) => {
|
|
109
|
+
let error = 1;
|
|
110
|
+
let t = length / totalLength;
|
|
111
|
+
let step = (length - func(t)) / totalLength;
|
|
112
|
+
let numIterations = 0;
|
|
113
|
+
while (error > 0.001) {
|
|
114
|
+
const increasedTLength = func(t + step);
|
|
115
|
+
const increasedTError = Math.abs(length - increasedTLength) / totalLength;
|
|
116
|
+
if (increasedTError < error) {
|
|
117
|
+
error = increasedTError;
|
|
118
|
+
t += step;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const decreasedTLength = func(t - step);
|
|
122
|
+
const decreasedTError = Math.abs(length - decreasedTLength) / totalLength;
|
|
123
|
+
if (decreasedTError < error) {
|
|
124
|
+
error = decreasedTError;
|
|
125
|
+
t -= step;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
step /= 2;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
numIterations++;
|
|
132
|
+
if (numIterations > 500) {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return t;
|
|
137
|
+
};
|
|
138
|
+
exports.t2length = t2length;
|