FastLED 3.9.15
Loading...
Searching...
No Matches

◆ compute()

vec2f fl::CatmullRomPath::compute ( float alpha)
overridevirtual

Implements fl::XYPathGenerator.

Definition at line 275 of file xypath_impls.cpp.

275 {
276 const auto &points = params().points;
277
278 // Need at least 2 points to define a path
279 if (points.size() < 2) {
280 // Return origin if not enough points
281 return vec2f(0.0f, 0.0f);
282 }
283
284 // If only 2 points, do linear interpolation
285 if (points.size() == 2) {
286 return vec2f(points[0].x + alpha * (points[1].x - points[0].x),
287 points[0].y + alpha * (points[1].y - points[0].y));
288 }
289
290 // For Catmull-Rom, we need 4 points to interpolate between the middle two
291 // Scale alpha to the number of segments
292 float scaledAlpha = alpha * (points.size() - 1);
293
294 // Determine which segment we're in
295 int segment = static_cast<int>(scaledAlpha);
296
297 // Clamp to valid range
298 if (segment >= static_cast<int>(points.size()) - 1) {
299 segment = points.size() - 2;
300 scaledAlpha = static_cast<float>(segment) + 1.0f;
301 }
302
303 // Get local alpha within this segment [0,1]
304 float t = scaledAlpha - static_cast<float>(segment);
305
306 // Get the four points needed for interpolation
307 vec2f p0, p1, p2, p3;
308
309 // Handle boundary cases
310 if (segment == 0) {
311 // For the first segment, duplicate the first point
312 p0 = points[0];
313 p1 = points[0];
314 p2 = points[1];
315 p3 = (points.size() > 2) ? points[2] : points[1];
316 } else if (segment == static_cast<int>(points.size()) - 2) {
317 // For the last segment, duplicate the last point
318 p0 = (segment > 0) ? points[segment - 1] : points[0];
319 p1 = points[segment];
320 p2 = points[segment + 1];
321 p3 = points[segment + 1];
322 } else {
323 // Normal case - we have points before and after
324 p0 = points[segment - 1];
325 p1 = points[segment];
326 p2 = points[segment + 1];
327 p3 = points[segment + 2];
328 }
329
330 // Perform Catmull-Rom interpolation
331 auto out = interpolate(p0, p1, p2, p3, t);
332 return out;
333}
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
HeapVector< vec2f > points
CatmullRomParams & params()
vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2, const vec2f &p3, float t) const
vec2< float > vec2f
Definition geometry.h:151

References interpolate(), params(), fl::CatmullRomParams::points, x, and y.

+ Here is the call graph for this function: