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/ptr.h"
14#include "fl/xymap.h"
15#include "lib8tion/types.h"
16
17namespace fl {
18
20
21using alpha16 =
22 uint16_t; // 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);
29 static Transform16 ToBounds(const vec2<alpha16> &min,
30 const vec2<alpha16> &max, alpha16 rotation = 0);
31
32 static Transform16 From(uint16_t width, uint16_t 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 alpha16 scale_x = 0xffff;
43 alpha16 scale_y = 0xffff;
47
49};
50
51// This transform assumes the coordinates are in the range [0,1].
53 public:
54 static TransformFloatImplPtr Identity() {
55 TransformFloatImplPtr tx = TransformFloatImplPtr::New();
56 return tx;
57 }
58 TransformFloatImpl() = default;
59 float scale_x = 1.0f;
60 float scale_y = 1.0f;
61 float offset_x = 0.0f;
62 float offset_y = 0.0f;
63 float rotation = 0.0f; // rotation range is [0,1], not [0,2*PI]!
64 float scale() const;
65 void set_scale(float scale);
66 vec2f transform(const vec2f &xy) const;
67 bool is_identity() const;
68};
69
70// Future usage.
71struct Matrix3x3f {
74 return m;
75 }
77 vec2<float> out;
78 out.x = m[0][0] * xy.x + m[0][1] * xy.y + m[0][2];
79 out.y = m[1][0] * xy.x + m[1][1] * xy.y + m[1][2];
80 return out;
81 }
82 float m[3][3] = {
83 {1.0f, 0.0f, 0.0f},
84 {0.0f, 1.0f, 0.0f},
85 {0.0f, 0.0f, 1.0f},
86 };
87};
88
89// TransformFloat is a wrapper around the smart ptr. This version allows for
90// easy use and fast / well behaved copy.
92 TransformFloat() = default;
93 float scale_x() const { return mImpl->scale_x; }
94 float scale_y() const { return mImpl->scale_y; }
95 float offset_x() const { return mImpl->offset_x; }
96 float offset_y() const { return mImpl->offset_y; }
97 // rotation range is [0,1], not [0,2*PI]!
98 float rotation() const { return mImpl->rotation; }
99 float scale() const { return MIN(scale_x(), scale_y()); }
100 void set_scale(float scale) { mImpl->set_scale(scale); }
101 void set_scale_x(float scale) { mImpl->scale_x = scale; }
102 void set_scale_y(float scale) { mImpl->scale_y = scale; }
103 void set_offset_x(float offset) { mImpl->offset_x = offset; }
104 void set_offset_y(float offset) { mImpl->offset_y = offset; }
105 void set_rotation(float rotation) { mImpl->rotation = rotation; }
106
107 vec2f transform(const vec2f &xy) const {
108 // mDirty = true; // always recompile.
109 // compileIfNecessary();
110 // return mCompiled.transform(xy);
111 return mImpl->transform(xy);
112 }
113 bool is_identity() const { return mImpl->is_identity(); }
114
115 Matrix3x3f compile() const;
116 void compileIfNecessary() const {
117 // if (mDirty) {
118 // mCompiled = compile();
119 // mDirty = false;
120 // }
121 }
122
123 private:
124 TransformFloatImplPtr mImpl = TransformFloatImpl::Identity();
125 // Matrix3x3f mCompiled; // future use.
126 // mutable bool mDirty = true; // future use.
127 mutable Matrix3x3f mCompiled; // future use.
128};
129
130} // namespace fl
unsigned int xy(unsigned int x, unsigned int y)
Referent()
Definition ptr.cpp:7
static TransformFloatImplPtr Identity()
Definition transform.h:54
bool is_identity() const
TransformFloatImpl()=default
void set_scale(float scale)
vec2f transform(const vec2f &xy) const
Definition transform.cpp:15
Defines fractional types used for lib8tion functions.
#define MIN(a, b)
Definition math_macros.h:15
uint16_t alpha16
Definition transform.h:21
vec2< float > vec2f
Definition geometry.h:151
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_SMART_PTR(type)
Definition ptr.h:31
vec2< float > transform(const vec2< float > &xy) const
Definition transform.h:76
float m[3][3]
Definition transform.h:82
static Matrix3x3f Identity()
Definition transform.h:72
Transform16()=default
vec2< alpha16 > transform(const vec2< alpha16 > &xy) const
Definition transform.cpp:92
alpha16 scale_x
Definition transform.h:42
alpha16 scale_y
Definition transform.h:43
static Transform16 From(uint16_t width, uint16_t height)
Definition transform.h:32
alpha16 rotation
Definition transform.h:46
static Transform16 ToBounds(alpha16 max_value)
Definition transform.cpp:44
alpha16 offset_x
Definition transform.h:44
alpha16 offset_y
Definition transform.h:45
void set_scale(float scale)
Definition transform.h:100
void set_offset_x(float offset)
Definition transform.h:103
void set_scale_x(float scale)
Definition transform.h:101
void compileIfNecessary() const
Definition transform.h:116
void set_rotation(float rotation)
Definition transform.h:105
float offset_x() const
Definition transform.h:95
TransformFloat()=default
float scale_x() const
Definition transform.h:93
float scale() const
Definition transform.h:99
float offset_y() const
Definition transform.h:96
void set_scale_y(float scale)
Definition transform.h:102
float rotation() const
Definition transform.h:98
vec2f transform(const vec2f &xy) const
Definition transform.h:107
float scale_y() const
Definition transform.h:94
Matrix3x3f compile() const
Matrix3x3f mCompiled
Definition transform.h:127
TransformFloatImplPtr mImpl
Definition transform.h:124
bool is_identity() const
Definition transform.h:113
void set_offset_y(float offset)
Definition transform.h:104