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 (uint16_t).
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/ptr.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.
47class XYPathParams : public Referent {
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.
55class XYPathGenerator : public Referent {
56 public:
57 virtual const Str name() const = 0;
58 virtual vec2f compute(float alpha) = 0;
59 // No writes when returning false.
60 virtual bool hasDrawBounds(rect<int> *bounds) {
61 FASTLED_UNUSED(bounds);
62 return false;
63 }
64};
65
67// Begin parameter classes.
69 public:
70 float x0 = -1.0f; // Start x coordinate
71 float y0 = 0.0f; // Start y coordinate
72 float x1 = 1.0f; // End x coordinate
73 float y1 = 0.0f; // End y coordinate
74};
75
77 public:
78 float c = 4.0f; // Scaling factor
79 float angle = 137.5f; // Divergence angle in degrees
80};
81
83 public:
84 uint8_t n = 3; // Numerator parameter (number of petals)
85 uint8_t d = 1; // Denominator parameter
86};
87
89 public:
90 float a = 1.0f; // Scaling parameter a
91 float b = 1.0f; // Scaling parameter b
92 float m = 3.0f; // Symmetry parameter (number of rotational symmetries)
93 float n1 = 1.0f; // Shape parameter n1
94 float n2 = 1.0f; // Shape parameter n2
95 float n3 = 100.0f; // Shape parameter n3
96};
97
99 public:
101
102 // Add a point to the path
103 void addPoint(vec2f p) { points.push_back(p); }
104
105 // Add a point with separate x,y coordinates
106 void addPoint(float x, float y) { points.push_back(vec2f(x, y)); }
107
108 // Clear all control points
109 void clear() { points.clear(); }
110
111 // Get the number of control points
112 size_t size() const { return points.size(); }
113
114 // Vector of control points
116};
117
119// Begin implementations of common XYPaths
120
122 public:
123 PointPath(float x, float y);
125 vec2f compute(float alpha) override;
126 const Str name() const override;
127 void set(float x, float y);
128 void set(vec2f p);
129
130 private:
132};
133
134class LinePath : public XYPathGenerator {
135 public:
136 LinePath(const LinePathParamsPtr &params = NewPtr<LinePathParams>());
137 LinePath(float x0, float y0, float x1, float y1);
138 vec2f compute(float alpha) override;
139 const Str name() const override;
140 void set(float x0, float y0, float x1, float y1);
141 void set(const LinePathParams &p);
142
144 const LinePathParams &params() const;
145
146 private:
148};
149
151 public:
152 CirclePath();
153 vec2f compute(float alpha) override;
154 const Str name() const override;
155};
156
158 public:
159 HeartPath();
160 vec2f compute(float alpha) override;
161 const Str name() const override;
162};
163
165 public:
166 ArchimedeanSpiralPath(uint8_t turns = 3, float radius = 1.0f);
167 vec2f compute(float alpha) override;
168 const Str name() const override;
169
170 void setTurns(uint8_t turns);
171 void setRadius(float radius);
172
173 private:
174 uint8_t mTurns; // Number of spiral turns
175 float mRadius; // Maximum radius of the spiral
176};
177
178class RosePath : public XYPathGenerator {
179 public:
180 // n and d determine the shape of the rose curve
181 // For n/d odd: produces n petals
182 // For n/d even: produces 2n petals
183 // For n and d coprime: produces n petals if n is odd, 2n petals if n is
184 // even
186 RosePath(uint8_t n = 3, uint8_t d = 1);
187 vec2f compute(float alpha) override;
188 const Str name() const override;
189
191 const RosePathParams &params() const;
192
193 void setN(uint8_t n);
194 void setD(uint8_t d);
195
196 private:
198};
199
201 public:
202 // c is a scaling factor, angle is the divergence angle in degrees (often
203 // 137.5° - the golden angle)
206 vec2f compute(float alpha) override;
207 const Str name() const override;
208
210 const PhyllotaxisParams &params() const;
211
212 private:
214};
215
217 public:
218 // Gielis superformula parameters:
219 // a, b: scaling parameters
220 // m: symmetry parameter (number of rotational symmetries)
221 // n1, n2, n3: shape parameters
224 vec2f compute(float alpha) override;
225 const Str name() const override;
226
228 const GielisCurveParams &params() const;
229
230 void setA(float a);
231 void setB(float b);
232 void setM(float m);
233 void setN1(float n1);
234 void setN2(float n2);
235 void setN3(float n3);
236
237 private:
239};
240
245 public:
247
249 void addPoint(vec2f p);
250
252 void addPoint(float x, float y);
253
255 void clear();
256
258 size_t size() const;
259
260 vec2f compute(float alpha) override;
261 const Str name() const override;
262
264 const CatmullRomParams &params() const;
265
266 private:
268
269 // Helper function to interpolate between points using Catmull-Rom spline
270 vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2,
271 const vec2f &p3, float t) const;
272};
273
274// Smart pointer for CatmullRomPath
276
277} // namespace fl
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
const Str name() const override
void setRadius(float radius)
ArchimedeanSpiralPath(uint8_t turns=3, float radius=1.0f)
vec2f compute(float alpha) override
void setTurns(uint8_t turns)
HeapVector< vec2f > points
void addPoint(float x, float y)
size_t size() const
void addPoint(vec2f p)
CatmullRomParams & params()
void clear()
Clear all control points.
vec2f interpolate(const vec2f &p0, const vec2f &p1, const vec2f &p2, const vec2f &p3, float t) const
CatmullRomPath(const Ptr< CatmullRomParams > &p=NewPtr< CatmullRomParams >())
vec2f compute(float alpha) override
Ptr< CatmullRomParams > mParams
const Str name() const override
size_t size() const
Get the number of control points.
void addPoint(vec2f p)
Add a point in [0,1]² to the path.
Catmull–Rom spline through arbitrary points.
const Str name() const override
vec2f compute(float alpha) override
void setN3(float n3)
Ptr< GielisCurveParams > mParams
void setN2(float n2)
GielisCurveParams & params()
void setN1(float n1)
const Str name() const override
vec2f compute(float alpha) override
GielisCurvePath(const Ptr< GielisCurveParams > &p=NewPtr< GielisCurveParams >())
const Str name() const override
vec2f compute(float alpha) override
void set(float x0, float y0, float x1, float y1)
vec2f compute(float alpha) override
const Str name() const override
LinePath(const LinePathParamsPtr &params=NewPtr< LinePathParams >())
LinePathParams & params()
Ptr< LinePathParams > mParams
PhyllotaxisPath(const Ptr< PhyllotaxisParams > &p=NewPtr< PhyllotaxisParams >())
PhyllotaxisParams & params()
vec2f compute(float alpha) override
Ptr< PhyllotaxisParams > mParams
const Str name() const override
vec2f compute(float alpha) override
const Str name() const override
void set(float x, float y)
PointPath(float x, float y)
Definition ptr.h:118
Referent()
Definition ptr.cpp:7
RosePath(const Ptr< RosePathParams > &p=NewPtr< RosePathParams >())
const Str name() const override
RosePathParams & params()
void setD(uint8_t d)
void setN(uint8_t n)
vec2f compute(float alpha) override
Ptr< RosePathParams > mParams
Definition str.h:388
virtual const Str name() const =0
virtual vec2f compute(float alpha)=0
virtual bool hasDrawBounds(rect< int > *bounds)
TransformFloat transform
vec2< float > vec2f
Definition geometry.h:151
Ptr< T > NewPtr(Args... args)
Definition ptr.h:451
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
static FASTLED_NAMESPACE_BEGIN uint8_t const p[]
Definition noise.cpp:30
#define FASTLED_SMART_PTR(type)
Definition ptr.h:31
#define FASTLED_UNUSED(x)
Definition unused.h:3