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