FastLED 3.10.8
Loading...
Searching...
No Matches
transform.h
Go to the documentation of this file.
1#pragma once
2
3/*
4Efficient transform classes for floating point and alpha16 coordinate systems.
5Note that component transforms are used because it's easy to skip calculations
6for components that are not used. For example, if the rotation is 0 then no
7expensive trig functions are needed. Same with scale and offset.
8
9*/
10
11#include "fl/math/alpha.h"
12#include "fl/math/geometry.h"
13#include "fl/math/math.h"
14#include "fl/stl/shared_ptr.h" // For FASTLED_SHARED_PTR macros
15#include "fl/stl/int.h"
16#include "fl/stl/noexcept.h"
17
18namespace fl {
19
21
22// alpha16 is now defined in fl/int.h as a UNORM16 type [0, 65535] → [0.0, 1.0].
23
24// This transform assumes the coordinates are in the range [0,65535].
26 // Make a transform that maps a rectangle to the given bounds from
27 // (0,0) to (max_value,max_value), inclusive.
31
37
38 // static Transform16 From(const XYMap &map) {
39 // return Transform16::ToBounds(map.getWidth(), map.getHeight());
40 // }
41 Transform16() = default;
42
43 // Use default move constructor and assignment operator for POD data
46
47 alpha16 scale_x = 0xffff;
48 alpha16 scale_y = 0xffff;
52
54};
55
56// This transform assumes the coordinates are in the range [0,1].
58 public:
59 static TransformFloatImplPtr Identity() FL_NO_EXCEPT {
60 TransformFloatImplPtr tx = fl::make_shared<TransformFloatImpl>();
61 return tx;
62 }
64 virtual ~TransformFloatImpl() FL_NO_EXCEPT = default; // Add virtual destructor for proper cleanup
65 float scale_x = 1.0f;
66 float scale_y = 1.0f;
67 float offset_x = 0.0f;
68 float offset_y = 0.0f;
69 float rotation = 0.0f; // rotation range is [0,1], not [0,2*FL_PI]!
70 float scale() const FL_NO_EXCEPT;
74};
75
76// Future usage.
77struct Matrix3x3f {
80 Matrix3x3f &operator=(const Matrix3x3f &) = default;
82 Matrix3x3f &operator=(Matrix3x3f &&) FL_NO_EXCEPT = default;
83
86 return m;
87 }
88
90 vec2<float> out;
91 out.x = m[0][0] * xy.x + m[0][1] * xy.y + m[0][2];
92 out.y = m[1][0] * xy.x + m[1][1] * xy.y + m[1][2];
93 return out;
94 }
95 float m[3][3] = {
96 {1.0f, 0.0f, 0.0f},
97 {0.0f, 1.0f, 0.0f},
98 {0.0f, 0.0f, 1.0f},
99 };
100};
101
102// TransformFloat is a wrapper around the smart ptr. This version allows for
103// easy use and fast / well behaved copy.
105 TransformFloat() = default; // ok no noexcept: infer member exception contracts (#4773)
106 float scale_x() const FL_NO_EXCEPT { return mImpl->scale_x; }
107 float scale_y() const FL_NO_EXCEPT { return mImpl->scale_y; }
108 float offset_x() const FL_NO_EXCEPT { return mImpl->offset_x; }
109 float offset_y() const FL_NO_EXCEPT { return mImpl->offset_y; }
110 // rotation range is [0,1], not [0,2*FL_PI]!
111 float rotation() const FL_NO_EXCEPT { return mImpl->rotation; }
112 float scale() const FL_NO_EXCEPT { return fl::min(scale_x(), scale_y()); }
113 void set_scale(float scale) FL_NO_EXCEPT { mImpl->set_scale(scale); }
114 void set_scale_x(float scale) FL_NO_EXCEPT { mImpl->scale_x = scale; }
115 void set_scale_y(float scale) FL_NO_EXCEPT { mImpl->scale_y = scale; }
116 void set_offset_x(float offset) FL_NO_EXCEPT { mImpl->offset_x = offset; }
117 void set_offset_y(float offset) FL_NO_EXCEPT { mImpl->offset_y = offset; }
118 void set_rotation(float rotation) FL_NO_EXCEPT { mImpl->rotation = rotation; }
119
121 // mDirty = true; // always recompile.
122 // compileIfNecessary();
123 // return mCompiled.transform(xy);
124 return mImpl->transform(xy);
125 }
126 bool is_identity() const FL_NO_EXCEPT { return mImpl->is_identity(); }
127
130 // if (mDirty) {
131 // mCompiled = compile();
132 // mDirty = false;
133 // }
134 }
135
136 private:
137 TransformFloatImplPtr mImpl = TransformFloatImpl::Identity();
138 // Matrix3x3f mCompiled; // future use.
139 // mutable bool mDirty = true; // future use.
140 mutable Matrix3x3f mCompiled; // future use.
141};
142
143} // namespace fl
uint32_t scale_y[NUM_LAYERS]
Definition Fire2023.h:95
uint32_t scale_x[NUM_LAYERS]
Definition Fire2023.h:94
unsigned int xy(unsigned int x, unsigned int y)
Unsigned alpha types with UNORM semantics (GPU industry standard).
static TransformFloatImplPtr Identity() FL_NO_EXCEPT
Definition transform.h:59
TransformFloatImpl() FL_NO_EXCEPT=default
vec2f transform(const vec2f &xy) const FL_NO_EXCEPT
void set_scale(float scale) FL_NO_EXCEPT
float scale() const FL_NO_EXCEPT
bool is_identity() const FL_NO_EXCEPT
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
u8 u8 height
Definition blur.h:186
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71
vec2< float > vec2f
Definition geometry.h:333
u8 width
Definition blur.h:186
Base definition for an LED controller.
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:577
float m[3][3]
Definition transform.h:95
static Matrix3x3f Identity() FL_NO_EXCEPT
Definition transform.h:84
Matrix3x3f() FL_NO_EXCEPT=default
vec2< float > transform(const vec2< float > &xy) const FL_NO_EXCEPT
Definition transform.h:89
Transform16()=default
static Transform16 From(u16 width, u16 height) FL_NO_EXCEPT
Definition transform.h:32
static Transform16 ToBounds(const vec2< alpha16 > &min, const vec2< alpha16 > &max, alpha16 rotation=0) FL_NO_EXCEPT
alpha16 scale_x
Definition transform.h:47
alpha16 scale_y
Definition transform.h:48
Transform16 & operator=(Transform16 &&other) FL_NO_EXCEPT=default
alpha16 rotation
Definition transform.h:51
static Transform16 ToBounds(alpha16 max_value) FL_NO_EXCEPT
alpha16 offset_x
Definition transform.h:49
vec2< alpha16 > transform(const vec2< alpha16 > &xy) const FL_NO_EXCEPT
Transform16(Transform16 &&other) FL_NO_EXCEPT=default
alpha16 offset_y
Definition transform.h:50
void compileIfNecessary() const FL_NO_EXCEPT
Definition transform.h:129
Matrix3x3f compile() const FL_NO_EXCEPT
TransformFloat()=default
void set_offset_y(float offset) FL_NO_EXCEPT
Definition transform.h:117
float offset_x() const FL_NO_EXCEPT
Definition transform.h:108
bool is_identity() const FL_NO_EXCEPT
Definition transform.h:126
float scale_x() const FL_NO_EXCEPT
Definition transform.h:106
void set_scale_x(float scale) FL_NO_EXCEPT
Definition transform.h:114
float offset_y() const FL_NO_EXCEPT
Definition transform.h:109
void set_rotation(float rotation) FL_NO_EXCEPT
Definition transform.h:118
void set_offset_x(float offset) FL_NO_EXCEPT
Definition transform.h:116
float rotation() const FL_NO_EXCEPT
Definition transform.h:111
float scale() const FL_NO_EXCEPT
Definition transform.h:112
void set_scale(float scale) FL_NO_EXCEPT
Definition transform.h:113
Matrix3x3f mCompiled
Definition transform.h:140
TransformFloatImplPtr mImpl
Definition transform.h:137
void set_scale_y(float scale) FL_NO_EXCEPT
Definition transform.h:115
vec2f transform(const vec2f &xy) const FL_NO_EXCEPT
Definition transform.h:120
float scale_y() const FL_NO_EXCEPT
Definition transform.h:107
Unsigned 16-bit alpha / brightness — UNORM16.
Definition alpha.h:87
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190