Line data Source code
1 : import { CURVE_INTERPOLATIONS } from '@comfyorg/graph-model/curve'
2 : import type { CurveData, CurveInterpolation } from '@comfyorg/graph-model/curve'
3 : import type { PointTuple as CurvePoint } from '@comfyorg/shared-frontend-utils/geometry'
4 :
5 0 : export function isCurveData(value: unknown): value is CurveData {
6 0 : if (typeof value !== 'object' || value === null || Array.isArray(value))
7 0 : return false
8 0 : const v = value as Record<string, unknown>
9 0 : return (
10 : Array.isArray(v.points) &&
11 : v.points.every(
12 : (p: unknown) =>
13 : Array.isArray(p) &&
14 : p.length === 2 &&
15 : typeof p[0] === 'number' &&
16 : typeof p[1] === 'number'
17 : ) &&
18 : typeof v.interpolation === 'string' &&
19 : CURVE_INTERPOLATIONS.includes(v.interpolation as CurveInterpolation)
20 : )
21 : }
22 :
23 : /**
24 : * Piecewise linear interpolation through sorted control points.
25 : * Returns a function that evaluates y for any x in [0, 1].
26 : */
27 0 : export function createLinearInterpolator(
28 : points: CurvePoint[]
29 : ): (x: number) => number {
30 0 : if (points.length === 0) return () => 0
31 0 : if (points.length === 1) return () => points[0][1]
32 :
33 0 : const sorted = [...points].sort((a, b) => a[0] - b[0])
34 0 : const n = sorted.length
35 0 : const xs = sorted.map((p) => p[0])
36 0 : const ys = sorted.map((p) => p[1])
37 :
38 0 : return (x: number): number => {
39 0 : if (x <= xs[0]) return ys[0]
40 0 : if (x >= xs[n - 1]) return ys[n - 1]
41 :
42 0 : let lo = 0
43 0 : let hi = n - 1
44 0 : while (lo < hi - 1) {
45 0 : const mid = (lo + hi) >> 1
46 0 : if (xs[mid] <= x) lo = mid
47 0 : else hi = mid
48 : }
49 :
50 0 : const dx = xs[hi] - xs[lo]
51 0 : if (dx === 0) return ys[lo]
52 0 : const t = (x - xs[lo]) / dx
53 0 : return ys[lo] + t * (ys[hi] - ys[lo])
54 : }
55 : }
56 :
57 : /**
58 : * Factory that dispatches to the correct interpolator based on type.
59 : */
60 0 : export function createInterpolator(
61 : points: CurvePoint[],
62 : interpolation: CurveInterpolation
63 : ): (x: number) => number {
64 0 : return interpolation === 'linear'
65 : ? createLinearInterpolator(points)
66 : : createMonotoneInterpolator(points)
67 : }
68 :
69 : /**
70 : * Monotone cubic Hermite interpolation.
71 : * Produces a smooth curve that passes through all control points
72 : * without overshooting (monotone property).
73 : *
74 : * Returns a function that evaluates y for any x in [0, 1].
75 : */
76 0 : export function createMonotoneInterpolator(
77 : points: CurvePoint[]
78 : ): (x: number) => number {
79 0 : if (points.length === 0) return () => 0
80 0 : if (points.length === 1) return () => points[0][1]
81 :
82 0 : const sorted = [...points].sort((a, b) => a[0] - b[0])
83 0 : const n = sorted.length
84 0 : const xs = sorted.map((p) => p[0])
85 0 : const ys = sorted.map((p) => p[1])
86 :
87 0 : const deltas: number[] = []
88 0 : const slopes: number[] = []
89 0 : for (let i = 0; i < n - 1; i++) {
90 0 : const dx = xs[i + 1] - xs[i]
91 0 : deltas.push(dx === 0 ? 0 : (ys[i + 1] - ys[i]) / dx)
92 : }
93 :
94 0 : slopes.push(deltas[0] ?? 0)
95 0 : for (let i = 1; i < n - 1; i++) {
96 0 : if (deltas[i - 1] * deltas[i] <= 0) {
97 0 : slopes.push(0)
98 : } else {
99 0 : slopes.push((deltas[i - 1] + deltas[i]) / 2)
100 : }
101 : }
102 0 : slopes.push(deltas[n - 2] ?? 0)
103 :
104 0 : for (let i = 0; i < n - 1; i++) {
105 0 : if (deltas[i] === 0) {
106 0 : slopes[i] = 0
107 0 : slopes[i + 1] = 0
108 : } else {
109 0 : const alpha = slopes[i] / deltas[i]
110 0 : const beta = slopes[i + 1] / deltas[i]
111 0 : const s = alpha * alpha + beta * beta
112 0 : if (s > 9) {
113 0 : const t = 3 / Math.sqrt(s)
114 0 : slopes[i] = t * alpha * deltas[i]
115 0 : slopes[i + 1] = t * beta * deltas[i]
116 : }
117 : }
118 : }
119 :
120 0 : return (x: number): number => {
121 0 : if (x <= xs[0]) return ys[0]
122 0 : if (x >= xs[n - 1]) return ys[n - 1]
123 :
124 0 : let lo = 0
125 0 : let hi = n - 1
126 0 : while (lo < hi - 1) {
127 0 : const mid = (lo + hi) >> 1
128 0 : if (xs[mid] <= x) lo = mid
129 0 : else hi = mid
130 : }
131 :
132 0 : const dx = xs[hi] - xs[lo]
133 0 : if (dx === 0) return ys[lo]
134 :
135 0 : const t = (x - xs[lo]) / dx
136 0 : const t2 = t * t
137 0 : const t3 = t2 * t
138 :
139 0 : const h00 = 2 * t3 - 3 * t2 + 1
140 0 : const h10 = t3 - 2 * t2 + t
141 0 : const h01 = -2 * t3 + 3 * t2
142 0 : const h11 = t3 - t2
143 :
144 0 : return (
145 : h00 * ys[lo] +
146 : h10 * dx * slopes[lo] +
147 : h01 * ys[hi] +
148 : h11 * dx * slopes[hi]
149 : )
150 : }
151 : }
152 :
153 : export function curvesToLUT(
154 : points: CurvePoint[],
155 : interpolation: CurveInterpolation = 'monotone_cubic'
156 : ): Uint8Array {
157 : const lut = new Uint8Array(256)
158 : const interpolate = createInterpolator(points, interpolation)
159 :
160 : for (let i = 0; i < 256; i++) {
161 : const x = i / 255
162 : const y = interpolate(x)
163 : lut[i] = Math.max(0, Math.min(255, Math.round(y * 255)))
164 : }
165 :
166 : return lut
167 : }
168 :
169 0 : export function curveDataToFloatLUT(
170 : curve: CurveData,
171 : size: number = 256
172 : ): Float32Array {
173 0 : const lut = new Float32Array(size)
174 0 : const interpolate = createInterpolator(curve.points, curve.interpolation)
175 0 : for (let i = 0; i < size; i++) {
176 0 : lut[i] = interpolate(i / (size - 1))
177 : }
178 0 : return lut
179 : }
|