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 (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/stl/function.h"
14#include "fl/gfx/leds.h" // IWYU pragma: keep
15#include "fl/stl/pair.h"
16#include "fl/stl/shared_ptr.h" // For FASTLED_SHARED_PTR macros
17#include "fl/gfx/tile2x2.h" // IWYU pragma: keep
18#include "fl/math/transform.h"
19#include "fl/gfx/xypath_impls.h"
20#include "fl/stl/noexcept.h"
21
22namespace fl {
23
24class Gradient;
25
27template <typename T> class function;
28
29// Smart pointers for the XYPath family.
34
35namespace xypath_detail {
36fl::string unique_missing_name(const char *prefix = "XYCustomPath: ");
37} // namespace xypath_detail
38
39class XYPath {
40 public:
42 // Begin pre-baked paths.
43 // Point
44 static XYPathPtr NewPointPath(float x, float y);
45 // Lines and curves
46 static XYPathPtr NewLinePath(float x0, float y0, float x1, float y1);
47 static XYPathPtr
49 // Cutmull allows for a path to be defined by a set of points. The path will
50 // be a smooth curve through the points.
51 static XYPathPtr NewCatmullRomPath(
52 u16 width = 0, u16 height = 0,
54
55 // Custom path using just a function.
56 static XYPathPtr
57 NewCustomPath(const fl::function<vec2f(float)> &path,
58 const rect<i16> &drawbounds = rect<i16>(),
60 const char *name = nullptr);
61
62 static XYPathPtr NewCirclePath();
63 static XYPathPtr NewCirclePath(u16 width, u16 height);
64 static XYPathPtr NewHeartPath();
65 static XYPathPtr NewHeartPath(u16 width, u16 height);
66 static XYPathPtr NewArchimedeanSpiralPath(u16 width, u16 height);
67 static XYPathPtr NewArchimedeanSpiralPath();
68
69 static XYPathPtr
70 NewRosePath(u16 width = 0, u16 height = 0,
72
73 static XYPathPtr NewPhyllotaxisPath(
74 u16 width = 0, u16 height = 0,
76
77 static XYPathPtr NewGielisCurvePath(
78 u16 width = 0, u16 height = 0,
80 // END pre-baked paths.
81
82 // Takes in a float at time [0, 1] and returns alpha values
83 // for that point in time.
84 using AlphaFunction = fl::function<u8(float)>;
85
86 // Future work: we don't actually want just the point, but also
87 // it's intensity at that value. Otherwise a seperate class has to
88 // made to also control the intensity and that sucks.
90
92 // Create a new Catmull-Rom spline path with custom parameters
93 XYPath(XYPathGeneratorPtr path,
95
96 virtual ~XYPath() FL_NOEXCEPT;
97 vec2f at(float alpha);
98 Tile2x2_u8 at_subpixel(float alpha);
99
100 // Rasterizes and draws to the leds.
101 void drawColor(const CRGB &color, float from, float to, Leds *leds,
102 int steps = -1);
103
104 void drawGradient(const Gradient &gradient, float from, float to,
105 Leds *leds, int steps = -1);
106
107 // Low level draw function.
108 void rasterize(float from, float to, int steps, XYRasterU8Sparse &raster,
109 AlphaFunction *optional_alpha_gen = nullptr);
110
111 void setScale(float scale);
112 string name() const;
113 // Overloaded to allow transform to be passed in.
114 vec2f at(float alpha, const TransformFloat &tx);
116 vec2f p = at(alpha);
117 return xy_brightness(p, 0xff); // Full brightness for now.
118 }
119 // Needed for drawing to the screen. When this called the rendering will
120 // be centered on the width and height such that 0,0 -> maps to .5,.5,
121 // which is convenient for drawing since each float pixel can be truncated
122 // to an integer type.
123 void setDrawBounds(u16 width, u16 height);
124 bool hasDrawBounds() const;
126
128
129 private:
130 int calculateSteps(float from, float to);
131
132 XYPathGeneratorPtr mPath;
133 XYPathRendererPtr mPathRenderer;
134
135 // By default the XYPath will use a shared raster. This is a problem on
136 // multi threaded apps. Since there isn't an easy way to check for multi
137 // threading, give the api user the ability to turn this off and use a local
138 // raster.
140};
141
143 public:
145 vec2f compute(float alpha) override { return mFunction(alpha); }
146 const string name() const override { return mName; }
147 void setName(const string &name) { mName = name; }
148
150 void setDrawBounds(const fl::rect<i16> &bounds) { mDrawBounds = bounds; }
151
152 bool hasDrawBounds(fl::rect<i16> *bounds) override {
153 if (bounds) {
154 *bounds = mDrawBounds;
155 }
156 return true;
157 }
158
159 private:
161 fl::string mName = "XYPathFunction Unnamed";
163};
164
165} // namespace fl
fl::CRGB leds[NUM_LEDS]
fl::UISlider scale("Scale", 4,.1, 4,.1)
XYPathGeneratorPtr mPath
Definition xypath.h:132
static XYPathPtr NewCustomPath(const fl::function< vec2f(float)> &path, const rect< i16 > &drawbounds=rect< i16 >(), const TransformFloat &transform=TransformFloat(), const char *name=nullptr)
int calculateSteps(float from, float to)
static XYPathPtr NewPhyllotaxisPath(u16 width=0, u16 height=0, const fl::shared_ptr< PhyllotaxisParams > &args=fl::make_shared< PhyllotaxisParams >())
void drawColor(const CRGB &color, float from, float to, Leds *leds, int steps=-1)
static XYPathPtr NewHeartPath()
static XYPathPtr NewArchimedeanSpiralPath()
xy_brightness at_brightness(float alpha)
Definition xypath.h:115
Tile2x2_u8 at_subpixel(float alpha)
void setScale(float scale)
vec2f at(float alpha)
scoped_ptr< XYRasterU8Sparse > mOptionalRaster
Definition xypath.h:139
fl::pair< vec2f, u8 > xy_brightness
Definition xypath.h:89
static XYPathPtr NewPointPath(float x, float y)
bool hasDrawBounds() const
static XYPathPtr NewGielisCurvePath(u16 width=0, u16 height=0, const fl::shared_ptr< GielisCurveParams > &params=fl::make_shared< GielisCurveParams >())
void drawGradient(const Gradient &gradient, float from, float to, Leds *leds, int steps=-1)
void setTransform(const TransformFloat &transform)
string name() const
static XYPathPtr NewCatmullRomPath(u16 width=0, u16 height=0, const fl::shared_ptr< CatmullRomParams > &params=fl::make_shared< CatmullRomParams >())
virtual ~XYPath() FL_NOEXCEPT
static XYPathPtr NewLinePath(float x0, float y0, float x1, float y1)
XYPathRendererPtr mPathRenderer
Definition xypath.h:133
fl::function< u8(float)> AlphaFunction
Definition xypath.h:84
static XYPathPtr NewRosePath(u16 width=0, u16 height=0, const fl::shared_ptr< RosePathParams > &params=fl::make_shared< RosePathParams >())
XYPath(XYPathGeneratorPtr path, TransformFloat transform=TransformFloat())
void setDrawBounds(u16 width, u16 height)
void rasterize(float from, float to, int steps, XYRasterU8Sparse &raster, AlphaFunction *optional_alpha_gen=nullptr)
static XYPathPtr NewCirclePath()
TransformFloat & transform()
fl::string mName
Definition xypath.h:161
fl::function< vec2f(float)> mFunction
Definition xypath.h:160
vec2f compute(float alpha) override
Definition xypath.h:145
bool hasDrawBounds(fl::rect< i16 > *bounds) override
Definition xypath.h:152
fl::rect< i16 > mDrawBounds
Definition xypath.h:162
XYPathFunction(fl::function< vec2f(float)> f)
Definition xypath.h:144
fl::rect< i16 > drawBounds() const
Definition xypath.h:149
const string name() const override
Definition xypath.h:146
void setName(const string &name)
Definition xypath.h:147
void setDrawBounds(const fl::rect< i16 > &bounds)
Definition xypath.h:150
fl::UISlider steps("Steps", 100.0f, 1.0f, 200.0f, 1.0f)
fl::XYRaster raster(WIDTH, HEIGHT)
fl::string unique_missing_name(const char *prefix)
unsigned char u8
Definition stdint.h:131
unique_ptr< T, Deleter > scoped_ptr
Definition scoped_ptr.h:11
u8 u8 height
Definition blur.h:186
vec2< float > vec2f
Definition geometry.h:333
u8 width
Definition blur.h:186
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
Base definition for an LED controller.
Definition crgb.hpp:179
corkscrew_args args
Definition old.h:149
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38