FastLED 3.9.15
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.
28 static Transform16 ToBounds(alpha16 max_value);
30 const vec2<alpha16> &max, alpha16 rotation = 0);
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() {
60 TransformFloatImplPtr tx = fl::make_shared<TransformFloatImpl>();
61 return tx;
62 }
64 virtual ~TransformFloatImpl() FL_NOEXCEPT = 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;
71 void set_scale(float scale);
72 vec2f transform(const vec2f &xy) const;
73 bool is_identity() const;
74};
75
76// Future usage.
77struct Matrix3x3f {
79 Matrix3x3f(const Matrix3x3f &) FL_NOEXCEPT = default;
80 Matrix3x3f &operator=(const Matrix3x3f &) = default;
82 Matrix3x3f &operator=(Matrix3x3f &&) FL_NOEXCEPT = 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.
106 float scale_x() const { return mImpl->scale_x; }
107 float scale_y() const { return mImpl->scale_y; }
108 float offset_x() const { return mImpl->offset_x; }
109 float offset_y() const { return mImpl->offset_y; }
110 // rotation range is [0,1], not [0,2*FL_PI]!
111 float rotation() const { return mImpl->rotation; }
112 float scale() const { return fl::min(scale_x(), scale_y()); }
113 void set_scale(float scale) { mImpl->set_scale(scale); }
114 void set_scale_x(float scale) { mImpl->scale_x = scale; }
115 void set_scale_y(float scale) { mImpl->scale_y = scale; }
116 void set_offset_x(float offset) { mImpl->offset_x = offset; }
117 void set_offset_y(float offset) { mImpl->offset_y = offset; }
118 void set_rotation(float rotation) { mImpl->rotation = rotation; }
119
120 vec2f transform(const vec2f &xy) const {
121 // mDirty = true; // always recompile.
122 // compileIfNecessary();
123 // return mCompiled.transform(xy);
124 return mImpl->transform(xy);
125 }
126 bool is_identity() const { return mImpl->is_identity(); }
127
129 void compileIfNecessary() const {
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()
Definition transform.h:59
TransformFloatImpl() FL_NOEXCEPT=default
bool is_identity() const
void set_scale(float scale)
vec2f transform(const vec2f &xy) const
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
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
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535
Matrix3x3f() FL_NOEXCEPT=default
vec2< float > transform(const vec2< float > &xy) const
Definition transform.h:89
float m[3][3]
Definition transform.h:95
static Matrix3x3f Identity()
Definition transform.h:84
static Transform16 From(u16 width, u16 height)
Definition transform.h:32
Transform16()=default
vec2< alpha16 > transform(const vec2< alpha16 > &xy) const
Transform16 & operator=(Transform16 &&other) FL_NOEXCEPT=default
static Transform16 ToBounds(alpha16 max_value)
alpha16 scale_x
Definition transform.h:47
alpha16 scale_y
Definition transform.h:48
alpha16 rotation
Definition transform.h:51
static Transform16 ToBounds(const vec2< alpha16 > &min, const vec2< alpha16 > &max, alpha16 rotation=0)
alpha16 offset_x
Definition transform.h:49
alpha16 offset_y
Definition transform.h:50
Transform16(Transform16 &&other) FL_NOEXCEPT=default
void set_scale(float scale)
Definition transform.h:113
void set_offset_x(float offset)
Definition transform.h:116
void set_scale_x(float scale)
Definition transform.h:114
void compileIfNecessary() const
Definition transform.h:129
void set_rotation(float rotation)
Definition transform.h:118
float offset_x() const
Definition transform.h:108
TransformFloat() FL_NOEXCEPT=default
float scale_x() const
Definition transform.h:106
float scale() const
Definition transform.h:112
float offset_y() const
Definition transform.h:109
void set_scale_y(float scale)
Definition transform.h:115
float rotation() const
Definition transform.h:111
vec2f transform(const vec2f &xy) const
Definition transform.h:120
float scale_y() const
Definition transform.h:107
Matrix3x3f compile() const
Matrix3x3f mCompiled
Definition transform.h:140
TransformFloatImplPtr mImpl
Definition transform.h:137
bool is_identity() const
Definition transform.h:126
void set_offset_y(float offset)
Definition transform.h:117
Unsigned 16-bit alpha / brightness — UNORM16.
Definition alpha.h:87
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190