Audacity 3.2.0
Size.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 Size.h
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#pragma once
12
13#include <cmath>
14#include <type_traits>
15
16namespace graphics
17{
18namespace details
19{
20template <typename DataType>
21constexpr DataType GetPositiveSizeValue(DataType value) noexcept
22{
23 if constexpr (std::is_unsigned_v<DataType>)
24 return value;
25 else
26 return value >= DataType {} ? value : -value;
27}
28} // namespace details
29
31template <typename DataType>
32struct SizeType final
33{
35 DataType width {};
37 DataType height {};
38
40 {
41 width = GetPositiveSizeValue(width + rhs.width);
42 height = GetPositiveSizeValue(height + rhs.height);
43
44 return *this;
45 }
46
48 {
49 width = GetPositiveSizeValue(width - rhs.width);
50 height = GetPositiveSizeValue(height - rhs.height);
51
52 return *this;
53 }
54
56 {
57 width = GetPositiveSizeValue(width * rhs.width);
58 height = GetPositiveSizeValue(height * rhs.height);
59
60 return *this;
61 }
62
64 {
65 width = GetPositiveSizeValue(width / rhs.width);
66 height = GetPositiveSizeValue(height / rhs.height);
67
68 return *this;
69 }
70
71 template <typename ScaleType> SizeType& operator*=(ScaleType scale) noexcept
72 {
73 width = static_cast<DataType>(GetPositiveSizeValue(width * scale));
74 height = static_cast<DataType>(GetPositiveSizeValue(height * scale));
75
76 return *this;
77 }
78
79 template <typename ScaleType> SizeType& operator/=(ScaleType scale) noexcept
80 {
81 width = static_cast<DataType>(GetPositiveSizeValue(width / scale));
82 height = static_cast<DataType>(GetPositiveSizeValue(height / scale));
83
84 return *this;
85 }
86
87 SizeType& operator-() noexcept
88 {
89 static_assert(std::is_signed_v<DataType>);
90
91 width = -width;
92 height = -height;
93
94 return *this;
95 }
96
98 bool IsZero() const noexcept
99 {
100 return std::abs(width) <= std::numeric_limits<DataType>::epsilon() &&
101 std::abs(height) <= std::numeric_limits<DataType>::epsilon();
102 }
103};
104
106template <typename To, typename From>
108{
109 return { static_cast<To>(point.width), static_cast<To>(point.height) };
110}
111
112template <typename DataType>
114{
115 return lhs.width == rhs.width && lhs.height == rhs.height;
116}
117
118template <typename DataType>
120{
121 return !(lhs == rhs);
122}
123
124template <typename DataType>
125SizeType<DataType>
127{
128 return { lhs.width + rhs.width, lhs.height + rhs.height };
129}
130
131template <typename DataType>
132SizeType<DataType>
134{
135 return { lhs.width - rhs.width, lhs.height - rhs.height };
136}
137
138template <typename DataType>
139SizeType<DataType>
141{
142 return { lhs.width * rhs.width, lhs.height * rhs.height };
143}
144
145template <typename DataType>
146SizeType<DataType>
148{
149 return { lhs.width / rhs.width, lhs.height / rhs.height };
150}
151
152template <typename DataType, typename ScaleType>
154{
155 return { static_cast<DataType>(lhs.width * rhs),
156 static_cast<DataType>(lhs.height * rhs) };
157}
158
159template <typename DataType, typename ScaleType>
161{
162 return { static_cast<DataType>(lhs * rhs.width),
163 static_cast<DataType>(lhs * rhs.height) };
164}
165
166template <typename DataType, typename ScaleType>
168{
169 return { static_cast<DataType>(lhs.width / rhs),
170 static_cast<DataType>(lhs.height / rhs) };
171}
172
175} // namespace graphics
constexpr DataType GetPositiveSizeValue(DataType value) noexcept
Definition: Size.h:21
Definition: Color.h:18
bool operator==(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:97
PointType< DataType > operator*(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:124
PointType< DataType > operator+(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:110
PointType< DataType > operator/(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:131
SizeType< To > size_cast(SizeType< From > point)
Casts size to another data type.
Definition: Size.h:107
bool operator!=(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:103
PointType< DataType > operator-(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:117
A class that represents size in 2D space.
Definition: Size.h:33
SizeType & operator/=(ScaleType scale) noexcept
Definition: Size.h:79
SizeType & operator/=(SizeType rhs) noexcept
Definition: Size.h:63
SizeType & operator+=(SizeType rhs) noexcept
Definition: Size.h:39
SizeType & operator*=(SizeType rhs) noexcept
Definition: Size.h:55
DataType height
Height.
Definition: Size.h:37
DataType width
Width.
Definition: Size.h:35
SizeType & operator-=(SizeType rhs) noexcept
Definition: Size.h:47
SizeType & operator-() noexcept
Definition: Size.h:87
bool IsZero() const noexcept
Return true if both width and height are zero.
Definition: Size.h:98
SizeType & operator*=(ScaleType scale) noexcept
Definition: Size.h:71