Line data Source code
1 : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
2 :
3 : /**
4 : * Evaluates a Catmull-Rom spline at parameter t between p1 and p2
5 : * @param p0 Previous control point
6 : * @param p1 Start point of the curve segment
7 : * @param p2 End point of the curve segment
8 : * @param p3 Next control point
9 : * @param t Parameter in range [0, 1]
10 : * @returns Interpolated point on the curve
11 : */
12 4588 : export function catmullRomSpline(
13 : p0: Point,
14 : p1: Point,
15 : p2: Point,
16 : p3: Point,
17 : t: number
18 : ): Point {
19 : // Centripetal Catmull-Rom Spline (alpha = 0.5) to prevent loops and overshoots
20 4588 : const alpha = 0.5
21 :
22 4588 : const getT = (t: number, p0: Point, p1: Point) => {
23 13764 : const d = Math.hypot(p1.x - p0.x, p1.y - p0.y)
24 13764 : return t + Math.pow(d, alpha)
25 : }
26 :
27 4588 : const t0 = 0
28 4588 : const t1 = getT(t0, p0, p1)
29 4588 : const t2 = getT(t1, p1, p2)
30 4588 : const t3 = getT(t2, p2, p3)
31 :
32 : // Map normalized t to parameter range
33 4588 : const tInterp = t1 + (t2 - t1) * t
34 :
35 : // Safe interpolation for coincident points
36 4588 : const interp = (
37 : pA: Point,
38 : pB: Point,
39 : tA: number,
40 : tB: number,
41 : t: number
42 : ): Point => {
43 27528 : if (Math.abs(tB - tA) < 0.0001) return pA
44 26427 : const k = (t - tA) / (tB - tA)
45 26427 : return add(mul(pA, 1 - k), mul(pB, k))
46 : }
47 :
48 : // Barry-Goldman pyramidal interpolation
49 4588 : const A1 = interp(p0, p1, t0, t1, tInterp)
50 4588 : const A2 = interp(p1, p2, t1, t2, tInterp)
51 4588 : const A3 = interp(p2, p3, t2, t3, tInterp)
52 :
53 : const B1 = interp(A1, A2, t0, t2, tInterp)
54 : const B2 = interp(A2, A3, t1, t3, tInterp)
55 :
56 : const C = interp(B1, B2, t1, t2, tInterp)
57 :
58 4588 : return C
59 : }
60 :
61 26427 : function add(p1: Point, p2: Point): Point {
62 26427 : return { x: p1.x + p2.x, y: p1.y + p2.y }
63 : }
64 :
65 52854 : function mul(p: Point, s: number): Point {
66 52854 : return { x: p.x * s, y: p.y * s }
67 : }
68 :
69 : /**
70 : * Resamples a curve segment with a starting offset (remainder from previous segment).
71 : * Returns the resampled points and the new remainder distance.
72 : *
73 : * @param points Points defining the curve segment
74 : * @param spacing Desired spacing between points
75 : * @param startOffset Distance to travel before placing the first point (remainder)
76 : * @returns Object containing points and new remainder
77 : */
78 347 : export function resampleSegment(
79 : points: Point[],
80 : spacing: number,
81 : startOffset: number
82 : ): { points: Point[]; remainder: number } {
83 347 : if (points.length === 0) return { points: [], remainder: startOffset }
84 :
85 347 : const result: Point[] = []
86 347 : let currentDist = 0
87 347 : let nextSampleDist = startOffset
88 :
89 : // Iterate through segment points
90 347 : for (let i = 0; i < points.length - 1; i++) {
91 4588 : const p1 = points[i]
92 4588 : const p2 = points[i + 1]
93 :
94 4588 : const dx = p2.x - p1.x
95 4588 : const dy = p2.y - p1.y
96 4588 : const segmentLen = Math.hypot(dx, dy)
97 :
98 : // Handle zero-length segments
99 4588 : if (segmentLen < 0.0001) {
100 0 : while (nextSampleDist <= currentDist) {
101 0 : result.push(p1)
102 0 : nextSampleDist += spacing
103 : }
104 0 : continue
105 : }
106 :
107 : // Generate samples within the segment
108 4588 : while (nextSampleDist <= currentDist + segmentLen) {
109 1409 : const t = (nextSampleDist - currentDist) / segmentLen
110 :
111 : // Interpolate
112 1409 : const x = p1.x + t * dx
113 1409 : const y = p1.y + t * dy
114 1409 : result.push({ x, y })
115 :
116 1409 : nextSampleDist += spacing
117 : }
118 :
119 4588 : currentDist += segmentLen
120 : }
121 :
122 : // Calculate remainder distance for the next segment
123 : const remainder = nextSampleDist - currentDist
124 :
125 347 : return { points: result, remainder }
126 : }
|