utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
scale.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <cmath>
26#include <concepts>
27#include <numbers>
28#include <ostream>
29
30#include "utility/math/vector.hpp"
31
32namespace utility::graphic
33{
34
39 template<typename Type>
41
54 template<CanBeScaleComponent ScaleComponentType> class Scale:
55 public math::Vector<ScaleComponentType, 3>
56 {
57 public:
61 Scale(void)
62 : math::Vector<ScaleComponentType, 3>(
63 { ScaleComponentType { 1.0f }, ScaleComponentType { 1.0f },
64 ScaleComponentType { 1.0f } })
65 {
66 }
67
74 explicit Scale(ScaleComponentType value)
75 : math::Vector<ScaleComponentType, 3>({ value, value, value })
76 {
77 }
78
83 explicit Scale(
85 : utility::math::Vector<ScaleComponentType, 3>(vector)
86 {
87 }
88
95 Scale(ScaleComponentType x, ScaleComponentType y, ScaleComponentType z)
96 : math::Vector<ScaleComponentType, 3>({ x, y, z })
97 {
98 }
99
106 Scale(std::initializer_list<ScaleComponentType> values)
107 : math::Vector<ScaleComponentType, 3>(
108 { ScaleComponentType { 1.0f }, ScaleComponentType { 1.0f },
109 ScaleComponentType { 1.0f } })
110 {
111 if (values.size() != 3) {
112 throw std::invalid_argument(
113 "Scale requires exactly three components");
114 }
115 const auto it = values.begin();
116 this->x = *it;
117 this->y = *(it + 1);
118 this->z = *(it + 2);
119 }
120
125 Scale(const std::initializer_list<ScaleComponentType> &value)
126 : math::Vector<ScaleComponentType, 3>(value)
127 {
128 }
129
134 Scale(const Scale &other) = default;
135
140 Scale(Scale &&other) noexcept = default;
141
147 Scale &operator=(const Scale &other) = default;
148
154 Scale &operator=(Scale &&other) noexcept = default;
155
159 ~Scale(void) = default;
160
166 Scale &setX(const ScaleComponentType value) noexcept
167 {
168 this->x = value;
169 return *this;
170 }
171
176 ScaleComponentType getX(void) const noexcept
177 {
178 return this->x;
179 }
180
186 Scale &setY(const ScaleComponentType value) noexcept
187 {
188 this->y = value;
189 return *this;
190 }
191
196 ScaleComponentType getY(void) const noexcept
197 {
198 return this->y;
199 }
200
206 Scale &setZ(const ScaleComponentType value) noexcept
207 {
208 this->z = value;
209 return *this;
210 }
211
216 ScaleComponentType getZ(void) const noexcept
217 {
218 return this->z;
219 }
220
227 Scale &scaleBy(const Scale &factor) noexcept
228 {
229 this->x *= factor.x;
230 this->y *= factor.y;
231 this->z *= factor.z;
232 return *this;
233 }
234
241 Scale scaled(const Scale &factor) const noexcept
242 {
243 Scale result(*this);
244 result.scaleBy(factor);
245 return result;
246 }
247
253 Scale &setUniform(ScaleComponentType value) noexcept
254 {
255 this->x = value;
256 this->y = value;
257 this->z = value;
258 return *this;
259 }
260
266 bool isUniform(ScaleComponentType epsilon = ScaleComponentType {
267 1e-6 }) const noexcept
268 {
269 return std::abs(this->x - this->y) <= epsilon
270 && std::abs(this->y - this->z) <= epsilon;
271 }
272
277 bool hasNegativeComponent(void) const noexcept
278 {
279 return this->x < ScaleComponentType {}
280 || this->y < ScaleComponentType {}
281 || this->z < ScaleComponentType {};
282 }
283
289 bool operator==(const Scale &other) const noexcept
290 {
291 if (this->x != other.x) {
292 return false;
293 }
294 if (this->y != other.y) {
295 return false;
296 }
297 if (this->z != other.z) {
298 return false;
299 }
300 return true;
301 }
302
308 bool operator!=(const Scale &other) const noexcept
309 {
310 return !(*this == other);
311 }
312
318 bool operator<(const Scale &other) const noexcept
319 {
320 if (this->x != other.x) {
321 return this->x < other.x;
322 }
323 if (this->y != other.y) {
324 return this->y < other.y;
325 }
326 if (this->z != other.z) {
327 return this->z < other.z;
328 }
329 return false;
330 }
331 };
332
336 using ScaleF = Scale<float>;
337
341 using ScaleD = Scale<double>;
342
349 std::ostream &operator<<(std::ostream &stream, const ScaleF &scale);
350
357 std::ostream &operator<<(std::ostream &stream, const ScaleD &scale);
358} // namespace utility::graphic
Scale in 3D space represented as a vector of three ScaleComponentType components (x,...
Definition scale.hpp:56
Scale(const Scale &other)=default
Copy constructor.
Scale(std::initializer_list< ScaleComponentType > values)
Construct from initializer list of three ScaleComponentType values.
Definition scale.hpp:106
Scale & scaleBy(const Scale &factor) noexcept
Scale this object by another scale (component-wise multiplication).
Definition scale.hpp:227
Scale(ScaleComponentType value)
Construct by filling all components with the same ScaleComponentType value.
Definition scale.hpp:74
Scale & operator=(const Scale &other)=default
Copy assignment operator.
ScaleComponentType getZ(void) const noexcept
Get the Z component of the scale.
Definition scale.hpp:216
Scale(void)
Default constructor initializing scale to (1, 1, 1).
Definition scale.hpp:61
Scale(Scale &&other) noexcept=default
Move constructor.
Scale(ScaleComponentType x, ScaleComponentType y, ScaleComponentType z)
Construct from three ScaleComponentType values (x, y, z).
Definition scale.hpp:95
Scale & setUniform(ScaleComponentType value) noexcept
Set all scale components to the same value.
Definition scale.hpp:253
Scale & setX(const ScaleComponentType value) noexcept
Set the X component of the scale.
Definition scale.hpp:166
Scale & operator=(Scale &&other) noexcept=default
Move assignment operator.
~Scale(void)=default
Default destructor for Scale.
Scale & setZ(const ScaleComponentType value) noexcept
Set the Z component of the scale.
Definition scale.hpp:206
Scale(const std::initializer_list< ScaleComponentType > &value)
Construct from a GLM vector.
Definition scale.hpp:125
Scale scaled(const Scale &factor) const noexcept
Return a scaled copy of this object (component-wise multiplication).
Definition scale.hpp:241
ScaleComponentType getY(void) const noexcept
Get the Y component of the scale.
Definition scale.hpp:196
Scale & setY(const ScaleComponentType value) noexcept
Set the Y component of the scale.
Definition scale.hpp:186
bool operator==(const Scale &other) const noexcept
Equality comparison.
Definition scale.hpp:289
Scale(const utility::math::Vector< ScaleComponentType, 3 > &vector)
Construct from a vector.
Definition scale.hpp:83
bool operator<(const Scale &other) const noexcept
Less-than comparison for ordering.
Definition scale.hpp:318
bool operator!=(const Scale &other) const noexcept
Inequality comparison.
Definition scale.hpp:308
ScaleComponentType getX(void) const noexcept
Get the X component of the scale.
Definition scale.hpp:176
bool isUniform(ScaleComponentType epsilon=ScaleComponentType { 1e-6 }) const noexcept
Check whether all scale components are approximately equal.
Definition scale.hpp:266
bool hasNegativeComponent(void) const noexcept
Check whether any component is negative.
Definition scale.hpp:277
3D vector class inheriting from glm::vec3.
Definition vector.hpp:83
Vector(void)
Default constructor initializing vector values to zero.
Definition vector.hpp:88
Concept to constrain scale component type.
Definition scale.hpp:40
Concept to constrain vector component type.
Definition vector.hpp:72
The utility namespace contains classes and functions for the utility project.
Definition cache.hpp:34