FastLED 3.9.15
Loading...
Searching...
No Matches
xypath.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/function.h"
14#include "fl/leds.h"
15#include "fl/pair.h"
16#include "fl/ptr.h"
17#include "fl/tile2x2.h"
18#include "fl/transform.h"
19#include "fl/xypath_impls.h"
20
21namespace fl {
22
23class Gradient;
24
26template <typename T> class function;
27
28// Smart pointers for the XYPath family.
33
34namespace xypath_detail {
35fl::Str unique_missing_name(const fl::Str &prefix = "XYCustomPath: ");
36} // namespace xypath_detail
37
38class XYPath : public Referent {
39 public:
41 // Begin pre-baked paths.
42 // Point
43 static XYPathPtr NewPointPath(float x, float y);
44 // Lines and curves
45 static XYPathPtr NewLinePath(float x0, float y0, float x1, float y1);
46 static XYPathPtr
48 // Cutmull allows for a path to be defined by a set of points. The path will
49 // be a smooth curve through the points.
50 static XYPathPtr NewCatmullRomPath(
51 uint16_t width = 0, uint16_t height = 0,
53
54 // Custom path using just a function.
55 static XYPathPtr
56 NewCustomPath(const fl::function<vec2f(float)> &path,
57 const rect<int> &drawbounds = rect<int>(),
60
61 static XYPathPtr NewCirclePath();
62 static XYPathPtr NewCirclePath(uint16_t width, uint16_t height);
63 static XYPathPtr NewHeartPath();
64 static XYPathPtr NewHeartPath(uint16_t width, uint16_t height);
65 static XYPathPtr NewArchimedeanSpiralPath(uint16_t width, uint16_t height);
66 static XYPathPtr NewArchimedeanSpiralPath();
67
68 static XYPathPtr
69 NewRosePath(uint16_t width = 0, uint16_t height = 0,
71
72 static XYPathPtr NewPhyllotaxisPath(
73 uint16_t width = 0, uint16_t height = 0,
75
76 static XYPathPtr NewGielisCurvePath(
77 uint16_t width = 0, uint16_t height = 0,
79 // END pre-baked paths.
80
81 // Takes in a float at time [0, 1] and returns alpha values
82 // for that point in time.
83 using AlphaFunction = fl::function<uint8_t(float)>;
84
85 // Future work: we don't actually want just the point, but also
86 // it's intensity at that value. Otherwise a seperate class has to
87 // made to also control the intensity and that sucks.
89
91 // Create a new Catmull-Rom spline path with custom parameters
92 XYPath(XYPathGeneratorPtr path,
94
95 virtual ~XYPath();
96 vec2f at(float alpha);
97 Tile2x2_u8 at_subpixel(float alpha);
98
99 // Rasterizes and draws to the leds.
100 void drawColor(const CRGB &color, float from, float to, Leds *leds,
101 int steps = -1);
102
103 void drawGradient(const Gradient &gradient, float from, float to,
104 Leds *leds, int steps = -1);
105
106 // Low level draw function.
107 void rasterize(float from, float to, int steps, XYRasterU8Sparse &raster,
108 AlphaFunction *optional_alpha_gen = nullptr);
109
110 void setScale(float scale);
111 Str name() const;
112 // Overloaded to allow transform to be passed in.
113 vec2f at(float alpha, const TransformFloat &tx);
115 vec2f p = at(alpha);
116 return xy_brightness(p, 0xff); // Full brightness for now.
117 }
118 // Needed for drawing to the screen. When this called the rendering will
119 // be centered on the width and height such that 0,0 -> maps to .5,.5,
120 // which is convenient for drawing since each float pixel can be truncated
121 // to an integer type.
122 void setDrawBounds(uint16_t width, uint16_t height);
123 bool hasDrawBounds() const;
125
127
128 private:
129 int calculateSteps(float from, float to);
130
131 XYPathGeneratorPtr mPath;
132 XYPathRendererPtr mPathRenderer;
133
134 // By default the XYPath will use a shared raster. This is a problem on
135 // multi threaded apps. Since there isn't an easy way to check for multi
136 // threading, give the api user the ability to turn this off and use a local
137 // raster.
139};
140
142 public:
144 vec2f compute(float alpha) override { return mFunction(alpha); }
145 const Str name() const override { return mName; }
146 void setName(const Str &name) { mName = name; }
147
149 void setDrawBounds(const fl::rect<int> &bounds) { mDrawBounds = bounds; }
150
151 bool hasDrawBounds(fl::rect<int> *bounds) override {
152 if (bounds) {
153 *bounds = mDrawBounds;
154 }
155 return true;
156 }
157
158 private:
160 fl::Str mName = "XYPathFunction Unnamed";
162};
163
164} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)
XYRaster raster(WIDTH, HEIGHT)
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
Referent()
Definition ptr.cpp:7
friend class Ptr
Definition ptr.h:421
Definition str.h:388
fl::function< uint8_t(float)> AlphaFunction
Definition xypath.h:83
XYPathGeneratorPtr mPath
Definition xypath.h:131
static XYPathPtr NewGielisCurvePath(uint16_t width=0, uint16_t height=0, const Ptr< GielisCurveParams > &params=NewPtr< GielisCurveParams >())
Definition xypath.cpp:207
Str name() const
Definition xypath.cpp:43
int calculateSteps(float from, float to)
Definition xypath.cpp:277
void drawColor(const CRGB &color, float from, float to, Leds *leds, int steps=-1)
Definition xypath.cpp:259
static XYPathPtr NewCustomPath(const fl::function< vec2f(float)> &path, const rect< int > &drawbounds=rect< int >(), const TransformFloat &transform=TransformFloat(), const Str &name=xypath_detail::unique_missing_name())
Definition xypath.cpp:227
static XYPathPtr NewHeartPath()
Definition xypath.cpp:162
fl::pair< vec2f, uint8_t > xy_brightness
Definition xypath.h:88
static XYPathPtr NewArchimedeanSpiralPath()
Definition xypath.cpp:181
xy_brightness at_brightness(float alpha)
Definition xypath.h:114
Tile2x2_u8 at_subpixel(float alpha)
Definition xypath.cpp:44
virtual ~XYPath()
Definition xypath.cpp:63
void setScale(float scale)
Definition xypath.cpp:41
vec2f at(float alpha)
Definition xypath.cpp:54
scoped_ptr< XYRasterU8Sparse > mOptionalRaster
Definition xypath.h:138
static XYPathPtr NewCatmullRomPath(uint16_t width=0, uint16_t height=0, const Ptr< CatmullRomParams > &params=NewPtr< CatmullRomParams >())
Definition xypath.cpp:217
static XYPathPtr NewPointPath(float x, float y)
Definition xypath.cpp:129
bool hasDrawBounds() const
Definition xypath.cpp:282
void setDrawBounds(uint16_t width, uint16_t height)
Definition xypath.cpp:37
void drawGradient(const Gradient &gradient, float from, float to, Leds *leds, int steps=-1)
Definition xypath.cpp:268
void setTransform(const TransformFloat &transform)
Definition xypath.cpp:255
static XYPathPtr NewLinePath(float x0, float y0, float x1, float y1)
Definition xypath.cpp:134
XYPathRendererPtr mPathRenderer
Definition xypath.h:132
static XYPathPtr NewPhyllotaxisPath(uint16_t width=0, uint16_t height=0, const Ptr< PhyllotaxisParams > &args=NewPtr< PhyllotaxisParams >())
Definition xypath.cpp:197
XYPath(XYPathGeneratorPtr path, TransformFloat transform=TransformFloat())
Definition xypath.cpp:58
static XYPathPtr NewRosePath(uint16_t width=0, uint16_t height=0, const Ptr< RosePathParams > &params=NewPtr< RosePathParams >())
Definition xypath.cpp:187
void rasterize(float from, float to, int steps, XYRasterU8Sparse &raster, AlphaFunction *optional_alpha_gen=nullptr)
Definition xypath.cpp:48
static XYPathPtr NewCirclePath()
Definition xypath.cpp:150
TransformFloat & transform()
Definition xypath.cpp:56
void setDrawBounds(const fl::rect< int > &bounds)
Definition xypath.h:149
void setName(const Str &name)
Definition xypath.h:146
fl::function< vec2f(float)> mFunction
Definition xypath.h:159
vec2f compute(float alpha) override
Definition xypath.h:144
fl::rect< int > mDrawBounds
Definition xypath.h:161
const Str name() const override
Definition xypath.h:145
XYPathFunction(fl::function< vec2f(float)> f)
Definition xypath.h:143
bool hasDrawBounds(fl::rect< int > *bounds) override
Definition xypath.h:151
fl::rect< int > drawBounds() const
Definition xypath.h:148
fl::Str unique_missing_name(const fl::Str &prefix)
Definition xypath.cpp:23
Pair< Key, Value > pair
Definition pair.h:13
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
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55