FastLED 3.10.8
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: ") FL_NO_EXCEPT;
37} // namespace xypath_detail
38
39class XYPath {
40 public:
42 // Begin pre-baked paths.
43 // Point
44 static XYPathPtr NewPointPath(float x, float y) FL_NO_EXCEPT;
45 // Lines and curves
46 static XYPathPtr NewLinePath(float x0, float y0, float x1, float y1) FL_NO_EXCEPT;
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) FL_NO_EXCEPT;
61
62 static XYPathPtr NewCirclePath() FL_NO_EXCEPT;
63 static XYPathPtr NewCirclePath(u16 width, u16 height) FL_NO_EXCEPT;
64 static XYPathPtr NewHeartPath() FL_NO_EXCEPT;
65 static XYPathPtr NewHeartPath(u16 width, u16 height) FL_NO_EXCEPT;
66 static XYPathPtr NewArchimedeanSpiralPath(u16 width, u16 height) FL_NO_EXCEPT;
67 static XYPathPtr NewArchimedeanSpiralPath() FL_NO_EXCEPT;
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_NO_EXCEPT;
97 vec2f at(float alpha) FL_NO_EXCEPT;
99
100 // Rasterizes and draws to the leds.
101 void drawColor(const CRGB &color, float from, float to, Leds *leds,
102 int steps = -1) FL_NO_EXCEPT;
103
104 void drawGradient(const Gradient &gradient, float from, float to,
105 Leds *leds, int steps = -1) FL_NO_EXCEPT;
106
107 // Low level draw function.
108 void rasterize(float from, float to, int steps, XYRasterU8Sparse &raster,
109 AlphaFunction *optional_alpha_gen = nullptr) FL_NO_EXCEPT;
110
111 void setScale(float scale) FL_NO_EXCEPT;
112 string name() const FL_NO_EXCEPT;
113 // Overloaded to allow transform to be passed in.
114 vec2f at(float alpha, const TransformFloat &tx) FL_NO_EXCEPT;
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) FL_NO_EXCEPT;
124 bool hasDrawBounds() const FL_NO_EXCEPT;
125 TransformFloat &transform() FL_NO_EXCEPT;
126
127 void setTransform(const TransformFloat &transform) FL_NO_EXCEPT;
128
129 private:
130 int calculateSteps(float from, float to) FL_NO_EXCEPT;
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) FL_NO_EXCEPT override { return mFunction(alpha); }
146 const string name() const FL_NO_EXCEPT override { return mName; }
147 void setName(const string &name) FL_NO_EXCEPT { mName = name; }
148
150 void setDrawBounds(const fl::rect<i16> &bounds) FL_NO_EXCEPT { mDrawBounds = bounds; }
151
152 bool hasDrawBounds(fl::rect<i16> *bounds) FL_NO_EXCEPT 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
CRGB leds[1]
fl::UISlider scale("Scale", 4,.1, 4,.1)
vec2f at(float alpha) FL_NO_EXCEPT
static XYPathPtr NewGielisCurvePath(u16 width=0, u16 height=0, const fl::shared_ptr< GielisCurveParams > &params=fl::make_shared< GielisCurveParams >()) FL_NO_EXCEPT
XYPathGeneratorPtr mPath
Definition xypath.h:132
string name() const FL_NO_EXCEPT
static XYPathPtr NewCustomPath(const fl::function< vec2f(float)> &path, const rect< i16 > &drawbounds=rect< i16 >(), const TransformFloat &transform=TransformFloat(), const char *name=nullptr) FL_NO_EXCEPT
Tile2x2_u8 at_subpixel(float alpha) FL_NO_EXCEPT
void drawGradient(const Gradient &gradient, float from, float to, Leds *leds, int steps=-1) FL_NO_EXCEPT
xy_brightness at_brightness(float alpha) FL_NO_EXCEPT
Definition xypath.h:115
static XYPathPtr NewRosePath(u16 width=0, u16 height=0, const fl::shared_ptr< RosePathParams > &params=fl::make_shared< RosePathParams >()) FL_NO_EXCEPT
static XYPathPtr NewCatmullRomPath(u16 width=0, u16 height=0, const fl::shared_ptr< CatmullRomParams > &params=fl::make_shared< CatmullRomParams >()) FL_NO_EXCEPT
TransformFloat & transform() FL_NO_EXCEPT
scoped_ptr< XYRasterU8Sparse > mOptionalRaster
Definition xypath.h:139
fl::pair< vec2f, u8 > xy_brightness
Definition xypath.h:89
static XYPathPtr NewPhyllotaxisPath(u16 width=0, u16 height=0, const fl::shared_ptr< PhyllotaxisParams > &args=fl::make_shared< PhyllotaxisParams >()) FL_NO_EXCEPT
void rasterize(float from, float to, int steps, XYRasterU8Sparse &raster, AlphaFunction *optional_alpha_gen=nullptr) FL_NO_EXCEPT
XYPath(XYPathGeneratorPtr path, TransformFloat transform=TransformFloat()) FL_NO_EXCEPT
static XYPathPtr NewHeartPath() FL_NO_EXCEPT
static XYPathPtr NewPointPath(float x, float y) FL_NO_EXCEPT
void setScale(float scale) FL_NO_EXCEPT
XYPathRendererPtr mPathRenderer
Definition xypath.h:133
fl::function< u8(float)> AlphaFunction
Definition xypath.h:84
static XYPathPtr NewArchimedeanSpiralPath(u16 width, u16 height) FL_NO_EXCEPT
static XYPathPtr NewCirclePath() FL_NO_EXCEPT
void drawColor(const CRGB &color, float from, float to, Leds *leds, int steps=-1) FL_NO_EXCEPT
static XYPathPtr NewLinePath(float x0, float y0, float x1, float y1) FL_NO_EXCEPT
bool hasDrawBounds(fl::rect< i16 > *bounds) FL_NO_EXCEPT override
Definition xypath.h:152
fl::string mName
Definition xypath.h:161
const string name() const FL_NO_EXCEPT override
Definition xypath.h:146
fl::function< vec2f(float)> mFunction
Definition xypath.h:160
fl::rect< i16 > mDrawBounds
Definition xypath.h:162
void setDrawBounds(const fl::rect< i16 > &bounds) FL_NO_EXCEPT
Definition xypath.h:150
vec2f compute(float alpha) FL_NO_EXCEPT override
Definition xypath.h:145
fl::rect< i16 > drawBounds() const FL_NO_EXCEPT
Definition xypath.h:149
void setName(const string &name) FL_NO_EXCEPT
Definition xypath.h:147
XYPathFunction(fl::function< vec2f(float)> f) FL_NO_EXCEPT
Definition xypath.h:144
fl::UISlider steps("Steps", 100.0f, 1.0f, 200.0f, 1.0f)
fl::XYRaster raster(WIDTH, HEIGHT)
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
fl::string unique_missing_name(const char *prefix) FL_NO_EXCEPT
unsigned char u8
Definition stdint.h:131
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
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
Base definition for an LED controller.
Definition crgb.hpp:179
corkscrew_args args
Definition old.h:149
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:577
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38