| Flutter Impeller |

point.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #pragma once
6
7 #include <algorithm>
8 #include <cmath>
9 #include <ostream>
10 #include <string>
11 #include <type_traits>
12
13 #include "impeller/geometry/scalar.h"
14 #include "impeller/geometry/size.h"
15 #include "impeller/geometry/type_traits.h"
16
17 namespace impeller {
18
19 template <class T>
20 struct TPoint {
21 using Type = T;
22
23 Type x = {};
24 Type y = {};
25
26 constexpr TPoint() = default;
27
28 template <class U>
29 explicit constexpr TPoint(const TPoint<U>& other)
30 : TPoint(static_cast<Type>(other.x), static_cast<Type>(other.y)) {}
31
32 template <class U>
33 explicit constexpr TPoint(const TSize<U>& other)
34 : TPoint(static_cast<Type>(other.width),
35 static_cast<Type>(other.height)) {}
36
37 constexpr TPoint(Type x, Type y) : x(x), y(y) {}
38
39 static constexpr TPoint<Type> MakeXY(Type x, Type y) { return {x, y}; }
40
41 template <class U>
42 static constexpr TPoint Round(const TPoint<U>& other) {
43 return TPoint{static_cast<Type>(std::round(other.x)),
44 static_cast<Type>(std::round(other.y))};
45 }
46
47 constexpr bool operator==(const TPoint& p) const {
48 return p.x == x && p.y == y;
49 }
50
51 constexpr bool operator!=(const TPoint& p) const {
52 return p.x != x || p.y != y;
53 }
54
55 template <class U>
56 inline TPoint operator+=(const TPoint<U>& p) {
57 x += static_cast<Type>(p.x);
58 y += static_cast<Type>(p.y);
59 return *this;
60 }
61
62 template <class U>
63 inline TPoint operator+=(const TSize<U>& s) {
64 x += static_cast<Type>(s.width);
65 y += static_cast<Type>(s.height);
66 return *this;
67 }
68
69 template <class U>
70 inline TPoint operator-=(const TPoint<U>& p) {
71 x -= static_cast<Type>(p.x);
72 y -= static_cast<Type>(p.y);
73 return *this;
74 }
75
76 template <class U>
77 inline TPoint operator-=(const TSize<U>& s) {
78 x -= static_cast<Type>(s.width);
79 y -= static_cast<Type>(s.height);
80 return *this;
81 }
82
83 template <class U>
84 inline TPoint operator*=(const TPoint<U>& p) {
85 x *= static_cast<Type>(p.x);
86 y *= static_cast<Type>(p.y);
87 return *this;
88 }
89
90 template <class U>
91 inline TPoint operator*=(const TSize<U>& s) {
92 x *= static_cast<Type>(s.width);
93 y *= static_cast<Type>(s.height);
94 return *this;
95 }
96
97 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
98 inline TPoint operator*=(U scale) {
99 x *= static_cast<Type>(scale);
100 y *= static_cast<Type>(scale);
101 return *this;
102 }
103
104 template <class U>
105 inline TPoint operator/=(const TPoint<U>& p) {
106 x /= static_cast<Type>(p.x);
107 y /= static_cast<Type>(p.y);
108 return *this;
109 }
110
111 template <class U>
112 inline TPoint operator/=(const TSize<U>& s) {
113 x /= static_cast<Type>(s.width);
114 y /= static_cast<Type>(s.height);
115 return *this;
116 }
117
118 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
119 inline TPoint operator/=(U scale) {
120 x /= static_cast<Type>(scale);
121 y /= static_cast<Type>(scale);
122 return *this;
123 }
124
125 constexpr TPoint operator-() const { return {-x, -y}; }
126
127 constexpr TPoint operator+(const TPoint& p) const {
128 return {x + p.x, y + p.y};
129 }
130
131 template <class U>
132 constexpr TPoint operator+(const TSize<U>& s) const {
133 return {x + static_cast<Type>(s.width), y + static_cast<Type>(s.height)};
134 }
135
136 constexpr TPoint operator-(const TPoint& p) const {
137 return {x - p.x, y - p.y};
138 }
139
140 template <class U>
141 constexpr TPoint operator-(const TSize<U>& s) const {
142 return {x - static_cast<Type>(s.width), y - static_cast<Type>(s.height)};
143 }
144
145 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
146 constexpr TPoint operator*(U scale) const {
147 return {static_cast<Type>(x * scale), static_cast<Type>(y * scale)};
148 }
149
150 constexpr TPoint operator*(const TPoint& p) const {
151 return {x * p.x, y * p.y};
152 }
153
154 template <class U>
155 constexpr TPoint operator*(const TSize<U>& s) const {
156 return {x * static_cast<Type>(s.width), y * static_cast<Type>(s.height)};
157 }
158
159 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
160 constexpr TPoint operator/(U d) const {
161 return {static_cast<Type>(x / d), static_cast<Type>(y / d)};
162 }
163
164 constexpr TPoint operator/(const TPoint& p) const {
165 return {x / p.x, y / p.y};
166 }
167
168 template <class U>
169 constexpr TPoint operator/(const TSize<U>& s) const {
170 return {x / static_cast<Type>(s.width), y / static_cast<Type>(s.height)};
171 }
172
173 constexpr Type GetDistanceSquared(const TPoint& p) const {
174 double dx = p.x - x;
175 double dy = p.y - y;
176 return dx * dx + dy * dy;
177 }
178
179 constexpr TPoint Min(const TPoint& p) const {
180 return {std::min<Type>(x, p.x), std::min<Type>(y, p.y)};
181 }
182
183 constexpr TPoint Max(const TPoint& p) const {
184 return {std::max<Type>(x, p.x), std::max<Type>(y, p.y)};
185 }
186
187 constexpr TPoint Floor() const { return {std::floor(x), std::floor(y)}; }
188
189 constexpr TPoint Ceil() const { return {std::ceil(x), std::ceil(y)}; }
190
191 constexpr TPoint Round() const { return {std::round(x), std::round(y)}; }
192
193 constexpr Type GetDistance(const TPoint& p) const {
194 return sqrt(GetDistanceSquared(p));
195 }
196
197 constexpr Type GetLengthSquared() const { return GetDistanceSquared({}); }
198
199 constexpr Type GetLength() const { return GetDistance({}); }
200
201 constexpr TPoint Normalize() const {
202 const auto length = GetLength();
203 if (length == 0) {
204 return {1, 0};
205 }
206 return {x / length, y / length};
207 }
208
209 constexpr TPoint Abs() const { return {std::fabs(x), std::fabs(y)}; }
210
211 constexpr Type Cross(const TPoint& p) const { return (x * p.y) - (y * p.x); }
212
213 constexpr Type Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); }
214
215 constexpr TPoint Reflect(const TPoint& axis) const {
216 return *this - axis * this->Dot(axis) * 2;
217 }
218
219 constexpr Radians AngleTo(const TPoint& p) const {
220 return Radians{std::atan2(this->Cross(p), this->Dot(p))};
221 }
222
223 constexpr TPoint Lerp(const TPoint& p, Scalar t) const {
224 return *this + (p - *this) * t;
225 }
226
227 constexpr bool IsZero() const { return x == 0 && y == 0; }
228 };
229
230 // Specializations for mixed (float & integer) algebraic operations.
231
232 template <class F, class I, class = MixedOp<F, I>>
233 constexpr TPoint<F> operator+(const TPoint<F>& p1, const TPoint<I>& p2) {
234 return {p1.x + static_cast<F>(p2.x), p1.y + static_cast<F>(p2.y)};
235 }
236
237 template <class F, class I, class = MixedOp<F, I>>
238 constexpr TPoint<F> operator+(const TPoint<I>& p1, const TPoint<F>& p2) {
239 return p2 + p1;
240 }
241
242 template <class F, class I, class = MixedOp<F, I>>
243 constexpr TPoint<F> operator-(const TPoint<F>& p1, const TPoint<I>& p2) {
244 return {p1.x - static_cast<F>(p2.x), p1.y - static_cast<F>(p2.y)};
245 }
246
247 template <class F, class I, class = MixedOp<F, I>>
248 constexpr TPoint<F> operator-(const TPoint<I>& p1, const TPoint<F>& p2) {
249 return {static_cast<F>(p1.x) - p2.x, static_cast<F>(p1.y) - p2.y};
250 }
251
252 template <class F, class I, class = MixedOp<F, I>>
253 constexpr TPoint<F> operator*(const TPoint<F>& p1, const TPoint<I>& p2) {
254 return {p1.x * static_cast<F>(p2.x), p1.y * static_cast<F>(p2.y)};
255 }
256
257 template <class F, class I, class = MixedOp<F, I>>
258 constexpr TPoint<F> operator*(const TPoint<I>& p1, const TPoint<F>& p2) {
259 return p2 * p1;
260 }
261
262 template <class F, class I, class = MixedOp<F, I>>
263 constexpr TPoint<F> operator/(const TPoint<F>& p1, const TPoint<I>& p2) {
264 return {p1.x / static_cast<F>(p2.x), p1.y / static_cast<F>(p2.y)};
265 }
266
267 template <class F, class I, class = MixedOp<F, I>>
268 constexpr TPoint<F> operator/(const TPoint<I>& p1, const TPoint<F>& p2) {
269 return {static_cast<F>(p1.x) / p2.x, static_cast<F>(p1.y) / p2.y};
270 }
271
272 // RHS algebraic operations with arithmetic types.
273
274 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
275 constexpr TPoint<T> operator*(U s, const TPoint<T>& p) {
276 return p * s;
277 }
278
279 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
280 constexpr TPoint<T> operator/(U s, const TPoint<T>& p) {
281 return {static_cast<T>(s) / p.x, static_cast<T>(s) / p.y};
282 }
283
284 // RHS algebraic operations with TSize.
285
286 template <class T, class U>
287 constexpr TPoint<T> operator+(const TSize<U>& s, const TPoint<T>& p) {
288 return p + s;
289 }
290
291 template <class T, class U>
292 constexpr TPoint<T> operator-(const TSize<U>& s, const TPoint<T>& p) {
293 return {static_cast<T>(s.width) - p.x, static_cast<T>(s.height) - p.y};
294 }
295
296 template <class T, class U>
297 constexpr TPoint<T> operator*(const TSize<U>& s, const TPoint<T>& p) {
298 return p * s;
299 }
300
301 template <class T, class U>
302 constexpr TPoint<T> operator/(const TSize<U>& s, const TPoint<T>& p) {
303 return {static_cast<T>(s.width) / p.x, static_cast<T>(s.height) / p.y};
304 }
305
306 using Point = TPoint<Scalar>;
307 using IPoint = TPoint<int64_t>;
308 using IPoint32 = TPoint<int32_t>;
309 using UintPoint32 = TPoint<uint32_t>;
310 using Vector2 = Point;
311
312 } // namespace impeller
313
314 namespace std {
315
316 template <class T>
317 inline std::ostream& operator<<(std::ostream& out,
318 const impeller::TPoint<T>& p) {
319 out << "(" << p.x << ", " << p.y << ")";
320 return out;
321 }
322
323 } // namespace std
impeller::TPoint::y
Type y
Definition: point.h:24
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::TPoint::operator-=
TPoint operator-=(const TSize< U > &s)
Definition: point.h:77
impeller::TPoint::Ceil
constexpr TPoint Ceil() const
Definition: point.h:189
impeller::TPoint::TPoint
constexpr TPoint(Type x, Type y)
Definition: point.h:37
impeller::TPoint::operator+=
TPoint operator+=(const TSize< U > &s)
Definition: point.h:63
impeller::TPoint::Lerp
constexpr TPoint Lerp(const TPoint &p, Scalar t) const
Definition: point.h:223
impeller::TPoint::operator*=
TPoint operator*=(U scale)
Definition: point.h:98
impeller::TPoint::Min
constexpr TPoint Min(const TPoint &p) const
Definition: point.h:179
impeller::TPoint::Round
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:42
impeller::TPoint::operator*
constexpr TPoint operator*(const TSize< U > &s) const
Definition: point.h:155
impeller::TPoint::TPoint
constexpr TPoint()=default
impeller::TPoint::operator/=
TPoint operator/=(const TPoint< U > &p)
Definition: point.h:105
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:945
impeller::TPoint::operator*=
TPoint operator*=(const TPoint< U > &p)
Definition: point.h:84
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:895
impeller::TPoint::operator/=
TPoint operator/=(const TSize< U > &s)
Definition: point.h:112
impeller::TPoint::Floor
constexpr TPoint Floor() const
Definition: point.h:187
impeller::TPoint::Dot
constexpr Type Dot(const TPoint &p) const
Definition: point.h:213
impeller::TPoint::operator/
constexpr TPoint operator/(U d) const
Definition: point.h:160
impeller::TPoint::Round
constexpr TPoint Round() const
Definition: point.h:191
impeller::TPoint::IsZero
constexpr bool IsZero() const
Definition: point.h:227
impeller::TPoint::operator-
constexpr TPoint operator-() const
Definition: point.h:125
impeller::TPoint::Reflect
constexpr TPoint Reflect(const TPoint &axis) const
Definition: point.h:215
impeller::TSize
Definition: size.h:18
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::TPoint::operator-
constexpr TPoint operator-(const TPoint &p) const
Definition: point.h:136
impeller::TPoint::AngleTo
constexpr Radians AngleTo(const TPoint &p) const
Definition: point.h:219
impeller::TPoint::Max
constexpr TPoint Max(const TPoint &p) const
Definition: point.h:183
impeller::Radians
Definition: scalar.h:35
impeller::TPoint::operator/
constexpr TPoint operator/(const TSize< U > &s) const
Definition: point.h:169
impeller::TPoint::TPoint
constexpr TPoint(const TPoint< U > &other)
Definition: point.h:29
impeller::TPoint::operator/=
TPoint operator/=(U scale)
Definition: point.h:119
impeller::TPoint::Cross
constexpr Type Cross(const TPoint &p) const
Definition: point.h:211
impeller::TSize::width
Type width
Definition: size.h:21
impeller::TPoint::x
Type x
Definition: point.h:23
impeller::operator-
constexpr Color operator-(T value, const Color &c)
Definition: color.h:889
scalar.h
impeller::TPoint::operator==
constexpr bool operator==(const TPoint &p) const
Definition: point.h:47
impeller::TPoint::operator+
constexpr TPoint operator+(const TPoint &p) const
Definition: point.h:127
impeller::TPoint::Abs
constexpr TPoint Abs() const
Definition: point.h:209
impeller::TPoint::operator*
constexpr TPoint operator*(const TPoint &p) const
Definition: point.h:150
impeller::TPoint::GetLengthSquared
constexpr Type GetLengthSquared() const
Definition: point.h:197
impeller::TPoint::operator/
constexpr TPoint operator/(const TPoint &p) const
Definition: point.h:164
impeller::TPoint::GetLength
constexpr Type GetLength() const
Definition: point.h:199
std
Definition: comparable.h:98
type_traits.h
impeller::TPoint::operator*
constexpr TPoint operator*(U scale) const
Definition: point.h:146
impeller::TPoint
Definition: point.h:20
impeller::TPoint::operator!=
constexpr bool operator!=(const TPoint &p) const
Definition: point.h:51
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:900
impeller::TSize::height
Type height
Definition: size.h:22
impeller::TPoint::GetDistance
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:193
impeller::TPoint< Type >::Type
Type Type
Definition: point.h:21
impeller::TPoint::Normalize
constexpr TPoint Normalize() const
Definition: point.h:201
impeller::TPoint::operator+=
TPoint operator+=(const TPoint< U > &p)
Definition: point.h:56
impeller::TPoint::TPoint
constexpr TPoint(const TSize< U > &other)
Definition: point.h:33
impeller::TPoint::operator-=
TPoint operator-=(const TPoint< U > &p)
Definition: point.h:70
impeller
Definition: aiks_context.cc:10
impeller::TPoint::GetDistanceSquared
constexpr Type GetDistanceSquared(const TPoint &p) const
Definition: point.h:173
impeller::TPoint::operator+
constexpr TPoint operator+(const TSize< U > &s) const
Definition: point.h:132
size.h
impeller::TPoint::MakeXY
static constexpr TPoint< Type > MakeXY(Type x, Type y)
Definition: point.h:39
impeller::TPoint::operator-
constexpr TPoint operator-(const TSize< U > &s) const
Definition: point.h:141
impeller::operator+
constexpr Color operator+(T value, const Color &c)
Definition: color.h:884
impeller::TPoint::operator*=
TPoint operator*=(const TSize< U > &s)
Definition: point.h:91