Audacity 3.2.0
Point.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 Point.h
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#pragma once
12
13#include <cmath>
14#include <type_traits>
15#include <numeric>
16
17namespace graphics
18{
19
21template <typename DataType>
22struct PointType final
23{
24 DataType x {};
25 DataType y {};
26
28 {
29 x += rhs.x;
30 y += rhs.y;
31
32 return *this;
33 }
34
36 {
37 x -= rhs.x;
38 y -= rhs.y;
39
40 return *this;
41 }
42
44 {
45 x *= rhs.x;
46 y *= rhs.y;
47
48 return *this;
49 }
50
52 {
53 x /= rhs.x;
54 y /= rhs.y;
55
56 return *this;
57 }
58
59 template <typename ScaleType> PointType& operator*=(ScaleType scale) noexcept
60 {
61 x = static_cast<DataType>(x * scale);
62 y = static_cast<DataType>(y * scale);
63
64 return *this;
65 }
66
67 template <typename ScaleType> PointType& operator/=(ScaleType scale) noexcept
68 {
69 x = static_cast<DataType>(x / scale);
70 y = static_cast<DataType>(y / scale);
71
72 return *this;
73 }
74
75 PointType operator-() const noexcept
76 {
77 static_assert(std::is_signed_v<DataType>);
78
79 return { -x, -y };
80 }
81
82 bool IsZero() const noexcept
83 {
84 return std::abs(x) <= std::numeric_limits<DataType>::epsilon() &&
85 std::abs(y) <= std::numeric_limits<DataType>::epsilon();
86 }
87};
88
90template <typename To, typename From>
92{
93 return { static_cast<To>(point.x), static_cast<To>(point.y) };
94}
95
96template <typename DataType>
98{
99 return lhs.x == rhs.x && lhs.y == rhs.y;
100}
101
102template <typename DataType>
104{
105 return !(lhs == rhs);
106}
107
108template <typename DataType>
109PointType<DataType>
111{
112 return { lhs.x + rhs.x, lhs.y + rhs.y };
113}
114
115template <typename DataType>
116PointType<DataType>
118{
119 return { lhs.x - rhs.x, lhs.y - rhs.y };
120}
121
122template <typename DataType>
123PointType<DataType>
125{
126 return { lhs.x * rhs.x, lhs.y * rhs.y };
127}
128
129template <typename DataType>
130PointType<DataType>
132{
133 return { lhs.x / rhs.x, lhs.y / rhs.y };
134}
135
136template <typename DataType, typename ScaleType>
138{
139 return { static_cast<DataType>(lhs.x * rhs),
140 static_cast<DataType>(lhs.y * rhs) };
141}
142
143template <typename DataType, typename ScaleType>
145{
146 return { static_cast<DataType>(lhs * rhs.x),
147 static_cast<DataType>(lhs * rhs.y) };
148}
149
150template <typename DataType, typename ScaleType>
152{
153 return { static_cast<DataType>(lhs.x / rhs),
154 static_cast<DataType>(lhs.y / rhs) };
155}
156
158template <typename DataType>
160{
161 return lhs.x * rhs.x + lhs.y * rhs.y;
162}
163
165template <typename DataType>
166auto Norm(PointType<DataType> lhs) noexcept
167{
168 return std::sqrt(DotProduct(lhs, lhs));
169}
170
172template <typename DataType>
174{
175 return std::sqrt(DotProduct(lhs, rhs));
176}
177
179template <typename DataType>
181{
182 const auto norm = Norm(pt);
183
184 if (norm <= std::numeric_limits<DataType>::epsilon())
185 return pt;
186
187 return pt / norm;
188}
189
192} // namespace graphics
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< To > point_cast(PointType< From > point)
Casts a point to another point type.
Definition: Point.h:91
auto DotProduct(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Returns the dot (inner) product between the vectors, defined by points.
Definition: Point.h:159
PointType< DataType > operator/(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Definition: Point.h:131
auto Norm(PointType< DataType > lhs) noexcept
Returns the length of a vector matching the point.
Definition: Point.h:166
auto Distance(PointType< DataType > lhs, PointType< DataType > rhs) noexcept
Returns the distance between the two points.
Definition: Point.h:173
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
auto Normalized(PointType< DataType > pt) noexcept
Returns a normalized vector in the direction of point.
Definition: Point.h:180
__m128 norm(__m128 x, __m128 y)
__finl float_x4 __vecc sqrt(const float_x4 &a)
A point in 2D space.
Definition: Point.h:23
PointType & operator-=(PointType rhs) noexcept
Definition: Point.h:35
PointType & operator*=(PointType rhs) noexcept
Definition: Point.h:43
PointType operator-() const noexcept
Definition: Point.h:75
PointType & operator+=(PointType rhs) noexcept
Definition: Point.h:27
PointType & operator/=(PointType rhs) noexcept
Definition: Point.h:51
bool IsZero() const noexcept
Definition: Point.h:82
DataType y
Definition: Point.h:25
DataType x
Definition: Point.h:24
PointType & operator/=(ScaleType scale) noexcept
Definition: Point.h:67
PointType & operator*=(ScaleType scale) noexcept
Definition: Point.h:59