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/lut.h"
14#include "fl/math_macros.h"
15#include "fl/memory.h"
16#include "fl/tile2x2.h"
17#include "fl/transform.h"
18#include "fl/unused.h"
19#include "fl/vector.h"
20#include "fl/warn.h"
21
22namespace fl {
23
25template <typename T> class function;
26
27// Smart pointers for the XYPath family.
41
42// FASTLED_SMART_PTR(LissajousPath);
43
44// BaseClasses.
45// Controllable parameter base class. Each subtype has a transform and
46// brightness.
48 public:
49 // Reserved for future use.
51 float brightness = 1.0f; // 0: off, 1: full brightness
52};
53
54// Base class for the actual path generator.
56 public:
57 virtual ~XYPathGenerator() = default; // Add virtual destructor for proper cleanup
58 virtual const string name() const = 0;
59 virtual vec2f compute(float alpha) = 0;
60 // No writes when returning false.
61 virtual bool hasDrawBounds(rect<i16> *bounds) {
62 FASTLED_UNUSED(bounds);
63 return false;
64 }
65};
66
68// Begin parameter classes.
70 public:
71 float x0 = -1.0f; // Start x coordinate
72 float y0 = 0.0f; // Start y coordinate
73 float x1 = 1.0f; // End x coordinate
74 float y1 = 0.0f; // End y coordinate
75};
76
78 public:
79 float c = 4.0f; // Scaling factor
80 float angle = 137.5f; // Divergence angle in degrees
81};
82
84 public:
85 u8 n = 3; // Numerator parameter (number of petals)
86 u8 d = 1; // Denominator parameter
87};
88
90 public:
91 float a = 1.0f; // Scaling parameter a
92 float b = 1.0f; // Scaling parameter b
93 float m = 3.0f; // Symmetry parameter (number of rotational symmetries)
94 float n1 = 1.0f; // Shape parameter n1
95 float n2 = 1.0f; // Shape parameter n2
96 float n3 = 100.0f; // Shape parameter n3
97};
98
100 public:
102
103 // Add a point to the path
104 void addPoint(vec2f p) { points.push_back(p); }
105
106 // Add a point with separate x,y coordinates
107 void addPoint(float x, float y) { points.push_back(vec2f(x, y)); }
108
109 // Clear all control points
110 void clear() { points.clear(); }
111
112 // Get the number of control points
113 fl::size size() const { return points.size(); }
114
115 // Vector of control points
117};
118
120// Begin implementations of common XYPaths
121
123 public:
124 PointPath(float x, float y);
125 PointPath(vec2f p);
126 vec2f compute(float alpha) override;
127 const string name() const override;
128 void set(float x, float y);
129 void set(vec2f p);
130
131 private:
133};
134
135class LinePath : public XYPathGenerator {
136 public:
137 LinePath(const LinePathParamsPtr &params = fl::make_shared<LinePathParams>());
138 LinePath(float x0, float y0, float x1, float y1);
139 vec2f compute(float alpha) override;
140 const string name() const override;
141 void set(float x0, float y0, float x1, float y1);
142 void set(const LinePathParams &p);
143
145 const LinePathParams &params() const;
146
147 private:
149};
150
152 public:
153 CirclePath();
154 vec2f compute(float alpha) override;
155 const string name() const override;
156};
157
159 public:
160 HeartPath();
161 vec2f compute(float alpha) override;
162 const string name() const override;
163};
164
166 public:
167 ArchimedeanSpiralPath(u8 turns = 3, float radius = 1.0f);
168 vec2f compute(float alpha) override;
169 const string name() const override;
170
171 void setTurns(u8 turns);
172 void setRadius(float radius);
173
174 private:
175 u8 mTurns; // Number of spiral turns
176 float mRadius; // Maximum radius of the spiral
177};
178
179class RosePath : public XYPathGenerator {
180 public:
181 // n and d determine the shape of the rose curve
182 // For n/d odd: produces n petals
183 // For n/d even: produces 2n petals
184 // For n and d coprime: produces n petals if n is odd, 2n petals if n is
185 // even
187 RosePath(u8 n = 3, u8 d = 1);
188 vec2f compute(float alpha) override;
189 const string name() const override;
190
192 const RosePathParams &params() const;
193
194 void setN(u8 n);
195 void setD(u8 d);
196
197 private:
199};
200
202 public:
203 // c is a scaling factor, angle is the divergence angle in degrees (often
204 // 137.5° - the golden angle)
207 vec2f compute(float alpha) override;
208 const string name() const override;
209
211 const PhyllotaxisParams &params() const;
212
213 private:
215};
216
218 public:
219 // Gielis superformula parameters:
220 // a, b: scaling parameters
221 // m: symmetry parameter (number of rotational symmetries)
222 // n1, n2, n3: shape parameters
225 vec2f compute(float alpha) override;
226 const string name() const override;
227
229 const GielisCurveParams &params() const;
230
231 void setA(float a);
232 void setB(float b);
233 void setM(float m);
234 void setN1(float n1);
235 void setN2(float n2);
236 void setN3(float n3);
237
238 private:
240};
241
246 public:
248
250 void addPoint(vec2f p);
251
253 void addPoint(float x, float y);
254
256 void clear();
257
259 fl::size size() const;
260
261 vec2f compute(float alpha) override;
262 const string name() const override;
263
265 const CatmullRomParams &params() const;
266
267 private:
269
270 // Helper function to interpolate between points using Catmull-Rom spline
271 vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2,
272 const vec2f &p3, float t) const;
273};
274
275// Smart pointer for CatmullRomPath
277
278} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
void setRadius(float radius)
const string name() const override
ArchimedeanSpiralPath(u8 turns=3, float radius=1.0f)
vec2f compute(float alpha) override
HeapVector< vec2f > points
void addPoint(float x, float y)
fl::size size() const
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
vec2f compute(float alpha) override
void setN3(float n3)
fl::shared_ptr< GielisCurveParams > mParams
void setN2(float n2)
GielisCurveParams & params()
void setN1(float n1)
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
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
void setN(u8 n)
void setD(u8 d)
virtual bool hasDrawBounds(rect< i16 > *bounds)
virtual const string name() const =0
virtual vec2f compute(float alpha)=0
virtual ~XYPathGenerator()=default
TransformFloat transform
static uint32_t t
Definition Luminova.h:54
unsigned char u8
Definition int.h:17
vec2< float > vec2f
Definition geometry.h:333
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
IMPORTANT!
Definition crgb.h:20
#define FASTLED_SMART_PTR(type)
Definition ptr.h:33
#define FASTLED_UNUSED(x)
Definition unused.h:4