FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
comp_type.hpp
1#pragma once
2
3#include "bsp.hpp"
4
5namespace Type {
6
14 public:
18 CycleValue& operator=(const CycleValue&) = default;
19
26 static float Calculate(float value) {
27 value = std::fmodf(value, M_2PI);
28 if (value < 0) {
29 value += M_2PI;
30 }
31 return value;
32 }
33
39 CycleValue(const float& value) : value_(Calculate(value)) {}
40
46 CycleValue(const double& value)
47 : value_(Calculate(static_cast<float>(value))) {}
48
54 CycleValue(const CycleValue& value) : value_(value.value_) {
55 while (value_ >= M_2PI) {
56 value_ -= M_2PI;
57 }
58
59 while (value_ < 0) {
60 value_ += M_2PI;
61 }
62 }
63
67 CycleValue() : value_(0.0f) {}
68
75 CycleValue operator+(const float& value) {
76 return CycleValue(value + value_);
77 }
78
85 CycleValue operator+(const double& value) {
86 return CycleValue(static_cast<float>(value) + value_);
87 }
88
96 return CycleValue(value.value_ + value_);
97 }
98
105 CycleValue operator+=(const float& value) {
106 value_ = Calculate(value + value_);
107 return *this;
108 }
109
116 CycleValue operator+=(const double& value) {
117 value_ = Calculate(static_cast<float>(value) + value_);
118 return *this;
119 }
120
128 value_ = Calculate(value.value_ + value_);
129 return *this;
130 }
131
138 float operator-(const float& raw_value) {
139 float value = Calculate(raw_value);
140 float ans = value_ - value;
141 while (ans >= M_PI) {
142 ans -= M_2PI;
143 }
144
145 while (ans < -M_PI) {
146 ans += M_2PI;
147 }
148
149 return ans;
150 }
151
158 float operator-(const double& raw_value) {
159 float value = Calculate(static_cast<float>(raw_value));
160 float ans = value_ - value;
161 while (ans >= M_PI) {
162 ans -= M_2PI;
163 }
164
165 while (ans < -M_PI) {
166 ans += M_2PI;
167 }
168
169 return ans;
170 }
171
178 float operator-(const CycleValue& value) {
179 float ans = value_ - value.value_;
180 while (ans >= M_PI) {
181 ans -= M_2PI;
182 }
183
184 while (ans < -M_PI) {
185 ans += M_2PI;
186 }
187
188 return ans;
189 }
190
196 CycleValue operator-() { return CycleValue(M_2PI - value_); }
197
203 operator float() { return this->value_; }
204
211 CycleValue& operator=(const float& value) {
212 value_ = Calculate(value);
213 return *this;
214 }
215
222 CycleValue& operator=(const double& value) {
223 value_ = Calculate(static_cast<float>(value));
224 return *this;
225 }
226
232 float Value() { return value_; }
233
234 private:
235 float value_; /* The normalized cyclic value in [0, 2π). */
236};
237
241typedef struct {
242 CycleValue yaw;
243 CycleValue pit;
244 CycleValue rol;
245} Eulr;
246
250typedef struct {
251 float q0;
252 float q1;
253 float q2;
254 float q3;
255} Quaternion;
256
260typedef struct {
261 float x;
262 float y;
263} Vector2;
264
268typedef struct {
269 float x;
270 float y;
271 float z;
272} Vector3;
273
274}; // namespace Type
A class to represent and manipulate cyclic values (e.g., angles).
Definition comp_type.hpp:13
CycleValue & operator=(const float &value)
Assigns a float value.
CycleValue(const float &value)
Constructs a CycleValue object from a float.
Definition comp_type.hpp:39
float operator-(const CycleValue &value)
Computes the difference between this value and another CycleValue.
float Value()
Returns the current value.
CycleValue operator+=(const double &value)
In-place addition of a double value.
static float Calculate(float value)
Normalizes a value to the range [0, 2π).
Definition comp_type.hpp:26
CycleValue operator+(const double &value)
Adds a double value and returns the resulting CycleValue.
Definition comp_type.hpp:85
float operator-(const double &raw_value)
Computes the difference between this value and a double value.
CycleValue operator-()
Negates the current value.
CycleValue operator+=(const float &value)
In-place addition of a float value.
CycleValue operator+(const float &value)
Adds a float value and returns the resulting CycleValue.
Definition comp_type.hpp:75
CycleValue()
Default constructor. Initializes the value to 0.
Definition comp_type.hpp:67
CycleValue & operator=(const double &value)
Assigns a double value.
CycleValue & operator=(const CycleValue &)=default
Default copy assignment operator.
CycleValue operator+=(const CycleValue &value)
In-place addition of another CycleValue.
CycleValue(const double &value)
Constructs a CycleValue object from a double.
Definition comp_type.hpp:46
CycleValue(const CycleValue &value)
Copy constructor. Ensures the copied value remains within [0, 2π).
Definition comp_type.hpp:54
CycleValue operator+(const CycleValue &value)
Adds another CycleValue and returns the result.
Definition comp_type.hpp:95
float operator-(const float &raw_value)
Computes the difference between this value and a float value.
Represents Euler angles with cyclic values for yaw, pitch, and roll.
Represents a quaternion (w, x, y, z components).
Represents a 2D vector.
Represents a 3D vector.