FastLED 3.9.15
Loading...
Searching...
No Matches
xypath_impls.h
Go to the documentation of this file.
1
2#pragma once
3
4// This is a drawing/graphics related class.
5//
6// XYPath represents a parameterized (x,y) path. The input will always be
7// an alpha value between 0->1 (float) or 0->0xffff (u16).
8// A look up table can be used to optimize path calculations when steps > 0.
9//
10// We provide common paths discovered throughout human history, for use in
11// your animations.
12
13#include "fl/math/lut.h" // IWYU pragma: keep
14#include "fl/math/math.h" // IWYU pragma: keep
15#include "fl/stl/shared_ptr.h" // For FASTLED_SHARED_PTR macros
16#include "fl/gfx/tile2x2.h" // IWYU pragma: keep
17#include "fl/math/transform.h"
19#include "fl/stl/vector.h"
20#include "fl/log/log.h" // IWYU pragma: keep
21#include "fl/stl/noexcept.h"
22
23namespace fl {
24
25class XYRasterU8Sparse; // IWYU pragma: keep
26template <typename T> class function; // IWYU pragma: keep
27
28// Smart pointers for the XYPath family.
42
43// FASTLED_SHARED_PTR(LissajousPath);
44
45// BaseClasses.
46// Controllable parameter base class. Each subtype has a transform and
47// brightness.
49 public:
50 // Reserved for future use.
52 float brightness = 1.0f; // 0: off, 1: full brightness
53};
54
55// Base class for the actual path generator.
57 public:
58 virtual ~XYPathGenerator() FL_NOEXCEPT = default; // Add virtual destructor for proper cleanup
59 virtual const string name() const = 0;
60 virtual vec2f compute(float alpha) = 0;
61 // No writes when returning false.
62 virtual bool hasDrawBounds(rect<i16> *bounds) {
63 FASTLED_UNUSED(bounds);
64 return false;
65 }
66};
67
69// Begin parameter classes.
71 public:
72 float x0 = -1.0f; // Start x coordinate
73 float y0 = 0.0f; // Start y coordinate
74 float x1 = 1.0f; // End x coordinate
75 float y1 = 0.0f; // End y coordinate
76};
77
79 public:
80 float c = 4.0f; // Scaling factor
81 float angle = 137.5f; // Divergence angle in degrees
82};
83
85 public:
86 u8 n = 3; // Numerator parameter (number of petals)
87 u8 d = 1; // Denominator parameter
88};
89
91 public:
92 float a = 1.0f; // Scaling parameter a
93 float b = 1.0f; // Scaling parameter b
94 float m = 3.0f; // Symmetry parameter (number of rotational symmetries)
95 float n1 = 1.0f; // Shape parameter n1
96 float n2 = 1.0f; // Shape parameter n2
97 float n3 = 100.0f; // Shape parameter n3
98};
99
101 public:
103
104 // Add a point to the path
105 void addPoint(vec2f p) { points.push_back(p); }
106
107 // Add a point with separate x,y coordinates
108 void addPoint(float x, float y) { points.push_back(vec2f(x, y)); }
109
110 // Clear all control points
111 void clear() { points.clear(); }
112
113 // Get the number of control points
114 fl::size size() const { return points.size(); }
115
116 // Vector of control points
118};
119
121// Begin implementations of common XYPaths
122
124 public:
125 PointPath(float x, float y);
126 PointPath(vec2f p);
127 vec2f compute(float alpha) override;
128 const string name() const override;
129 void set(float x, float y);
130 void set(vec2f p);
131
132 private:
134};
135
136class LinePath : public XYPathGenerator {
137 public:
138 LinePath(const LinePathParamsPtr &params = fl::make_shared<LinePathParams>());
139 LinePath(float x0, float y0, float x1, float y1);
140 vec2f compute(float alpha) override;
141 const string name() const override;
142 void set(float x0, float y0, float x1, float y1);
143 void set(const LinePathParams &p);
144
146 const LinePathParams &params() const;
147
148 private:
150};
151
153 public:
155 vec2f compute(float alpha) override;
156 const string name() const override;
157};
158
160 public:
162 vec2f compute(float alpha) override;
163 const string name() const override;
164};
165
167 public:
168 ArchimedeanSpiralPath(u8 turns = 3, float radius = 1.0f);
169 vec2f compute(float alpha) override;
170 const string name() const override;
171
172 void setTurns(u8 turns);
173 void setRadius(float radius);
174
175 private:
176 u8 mTurns; // Number of spiral turns
177 float mRadius; // Maximum radius of the spiral
178};
179
180class RosePath : public XYPathGenerator {
181 public:
182 // n and d determine the shape of the rose curve
183 // For n/d odd: produces n petals
184 // For n/d even: produces 2n petals
185 // For n and d coprime: produces n petals if n is odd, 2n petals if n is
186 // even
188 RosePath(u8 n = 3, u8 d = 1);
189 vec2f compute(float alpha) override;
190 const string name() const override;
191
193 const RosePathParams &params() const;
194
195 void setN(u8 n);
196 void setD(u8 d);
197
198 private:
200};
201
203 public:
204 // c is a scaling factor, angle is the divergence angle in degrees (often
205 // 137.5° - the golden angle)
208 vec2f compute(float alpha) override;
209 const string name() const override;
210
212 const PhyllotaxisParams &params() const;
213
214 private:
216};
217
219 public:
220 // Gielis superformula parameters:
221 // a, b: scaling parameters
222 // m: symmetry parameter (number of rotational symmetries)
223 // n1, n2, n3: shape parameters
226 vec2f compute(float alpha) override;
227 const string name() const override;
228
230 const GielisCurveParams &params() const;
231
232 void setA(float a);
233 void setB(float b);
234 void setM(float m);
235 void setN1(float n1);
236 void setN2(float n2);
237 void setN3(float n3);
238
239 private:
241};
242
247 public:
249
251 void addPoint(vec2f p);
252
254 void addPoint(float x, float y);
255
257 void clear();
258
260 fl::size size() const;
261
262 vec2f compute(float alpha) override;
263 const string name() const override;
264
266 const CatmullRomParams &params() const;
267
268 private:
270
271 // Helper function to interpolate between points using Catmull-Rom spline
272 vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2,
273 const vec2f &p3, float t) const;
274};
275
276// Smart pointer for CatmullRomPath
278
279} // namespace fl
const string name() const override
ArchimedeanSpiralPath(u8 turns=3, float radius=1.0f)
vec2f compute(float alpha) override
CatmullRomParams() FL_NOEXCEPT
void addPoint(float x, float y)
fl::size size() const
fl::vector< vec2f > points
void addPoint(vec2f p)
fl::size size() const
Get the number of control points.
CatmullRomParams & params()
fl::shared_ptr< CatmullRomParams > mParams
void clear()
Clear all control points.
vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2, const vec2f &p3, float t) const
vec2f compute(float alpha) override
CatmullRomPath(const fl::shared_ptr< CatmullRomParams > &p=fl::make_shared< CatmullRomParams >())
const string name() const override
void addPoint(vec2f p)
Add a point in [0,1]² to the path.
Catmull–Rom spline through arbitrary points.
const string name() const override
CirclePath() FL_NOEXCEPT
vec2f compute(float alpha) override
fl::shared_ptr< GielisCurveParams > mParams
GielisCurveParams & params()
vec2f compute(float alpha) override
GielisCurvePath(const fl::shared_ptr< GielisCurveParams > &p=fl::make_shared< GielisCurveParams >())
const string name() const override
vec2f compute(float alpha) override
HeartPath() FL_NOEXCEPT
const string name() const override
void set(float x0, float y0, float x1, float y1)
vec2f compute(float alpha) override
fl::shared_ptr< LinePathParams > mParams
const string name() const override
LinePath(const LinePathParamsPtr &params=fl::make_shared< LinePathParams >())
LinePathParams & params()
PhyllotaxisParams & params()
fl::shared_ptr< PhyllotaxisParams > mParams
vec2f compute(float alpha) override
const string name() const override
PhyllotaxisPath(const fl::shared_ptr< PhyllotaxisParams > &p=fl::make_shared< PhyllotaxisParams >())
const string name() const override
vec2f compute(float alpha) override
void set(float x, float y)
PointPath(float x, float y)
fl::shared_ptr< RosePathParams > mParams
const string name() const override
RosePath(const fl::shared_ptr< RosePathParams > &p=fl::make_shared< RosePathParams >())
RosePathParams & params()
vec2f compute(float alpha) override
virtual bool hasDrawBounds(rect< i16 > *bounds)
virtual const string name() const =0
virtual vec2f compute(float alpha)=0
virtual ~XYPathGenerator() FL_NOEXCEPT=default
TransformFloat transform
Centralized logging categories for FastLED hardware interfaces and subsystems.
unsigned char u8
Definition stdint.h:131
vec2< float > vec2f
Definition geometry.h:333
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_UNUSED(x)
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535