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/lut.h"
12#include "fl/math_macros.h"
13#include "fl/memory.h"
14#include "fl/xymap.h"
15#include "lib8tion/types.h"
16
17namespace fl {
18
20
21using alpha16 =
22 u16; // fixed point representation of 0->1 in the range [0, 65535]
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
32 static Transform16 From(u16 width, u16 height) {
33 vec2<alpha16> min = vec2<alpha16>(0, 0);
34 vec2<alpha16> max = vec2<alpha16>(width, height);
35 return Transform16::ToBounds(min, max);
36 }
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
44 Transform16(Transform16 &&other) noexcept = default;
45 Transform16 &operator=(Transform16 &&other) noexcept = default;
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 }
63 TransformFloatImpl() = default;
64 virtual ~TransformFloatImpl() = 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*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 {
78 Matrix3x3f() = default;
79 Matrix3x3f(const Matrix3x3f &) = default;
80 Matrix3x3f &operator=(const Matrix3x3f &) = default;
81 Matrix3x3f(Matrix3x3f &&) noexcept = default;
82 Matrix3x3f &operator=(Matrix3x3f &&) 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.
105 TransformFloat() = default;
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*PI]!
111 float rotation() const { return mImpl->rotation; }
112 float scale() const { return 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
unsigned int xy(unsigned int x, unsigned int y)
static TransformFloatImplPtr Identity()
Definition transform.h:59
bool is_identity() const
TransformFloatImpl()=default
void set_scale(float scale)
vec2f transform(const vec2f &xy) const
virtual ~TransformFloatImpl()=default
UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
Defines fractional types used for lib8tion functions.
#define MIN(a, b)
Definition math_macros.h:41
u16 alpha16
Definition transform.h:21
vec2< float > vec2f
Definition geometry.h:333
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
IMPORTANT!
Definition crgb.h:20
#define FASTLED_SMART_PTR(type)
Definition ptr.h:33
Matrix3x3f(Matrix3x3f &&) noexcept=default
vec2< float > transform(const vec2< float > &xy) const
Definition transform.h:89
Matrix3x3f(const Matrix3x3f &)=default
float m[3][3]
Definition transform.h:95
Matrix3x3f()=default
static Matrix3x3f Identity()
Definition transform.h:84
Matrix3x3f & operator=(const Matrix3x3f &)=default
static Transform16 From(u16 width, u16 height)
Definition transform.h:32
Transform16()=default
vec2< alpha16 > transform(const vec2< alpha16 > &xy) const
static Transform16 ToBounds(alpha16 max_value)
alpha16 scale_x
Definition transform.h:47
Transform16 & operator=(Transform16 &&other) noexcept=default
alpha16 scale_y
Definition transform.h:48
Transform16(Transform16 &&other) noexcept=default
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
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()=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
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190