| 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 #ifndef FLUTTER_IMPELLER_GEOMETRY_POINT_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_POINT_H_
7
8 #include <algorithm>
9 #include <cmath>
10 #include <cstdint>
11 #include <ostream>
12 #include <string>
13 #include <type_traits>
14
15 #include "fml/logging.h"
16 #include "impeller/geometry/scalar.h"
17 #include "impeller/geometry/size.h"
18 #include "impeller/geometry/type_traits.h"
19
20 namespace impeller {
21
22 #define ONLY_ON_FLOAT_M(Modifiers, Return) \
23 template <typename U = T> \
24 Modifiers std::enable_if_t<std::is_floating_point_v<U>, Return>
25 #define ONLY_ON_FLOAT(Return) DL_ONLY_ON_FLOAT_M(, Return)
26
27 template <class T>
28 struct TPoint {
29 using Type = T;
30
31 Type x = {};
32 Type y = {};
33
34 constexpr TPoint() = default;
35
36 template <class U>
37 explicit constexpr TPoint(const TPoint<U>& other)
38 : TPoint(static_cast<Type>(other.x), static_cast<Type>(other.y)) {}
39
40 template <class U>
41 explicit constexpr TPoint(const TSize<U>& other)
42 : TPoint(static_cast<Type>(other.width),
43 static_cast<Type>(other.height)) {}
44
45 constexpr TPoint(Type x, Type y) : x(x), y(y) {}
46
47 static constexpr TPoint<Type> MakeXY(Type x, Type y) { return {x, y}; }
48
49 template <class U>
50 static constexpr TPoint Round(const TPoint<U>& other) {
51 return TPoint{static_cast<Type>(std::round(other.x)),
52 static_cast<Type>(std::round(other.y))};
53 }
54
55 constexpr bool operator==(const TPoint& p) const {
56 return p.x == x && p.y == y;
57 }
58
59 constexpr bool operator!=(const TPoint& p) const {
60 return p.x != x || p.y != y;
61 }
62
63 template <class U>
64 inline TPoint operator+=(const TPoint<U>& p) {
65 x += static_cast<Type>(p.x);
66 y += static_cast<Type>(p.y);
67 return *this;
68 }
69
70 template <class U>
71 inline TPoint operator+=(const TSize<U>& s) {
72 x += static_cast<Type>(s.width);
73 y += static_cast<Type>(s.height);
74 return *this;
75 }
76
77 template <class U>
78 inline TPoint operator-=(const TPoint<U>& p) {
79 x -= static_cast<Type>(p.x);
80 y -= static_cast<Type>(p.y);
81 return *this;
82 }
83
84 template <class U>
85 inline TPoint operator-=(const TSize<U>& s) {
86 x -= static_cast<Type>(s.width);
87 y -= static_cast<Type>(s.height);
88 return *this;
89 }
90
91 template <class U>
92 inline TPoint operator*=(const TPoint<U>& p) {
93 x *= static_cast<Type>(p.x);
94 y *= static_cast<Type>(p.y);
95 return *this;
96 }
97
98 template <class U>
99 inline TPoint operator*=(const TSize<U>& s) {
100 x *= static_cast<Type>(s.width);
101 y *= static_cast<Type>(s.height);
102 return *this;
103 }
104
105 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
106 inline TPoint operator*=(U scale) {
107 x *= static_cast<Type>(scale);
108 y *= static_cast<Type>(scale);
109 return *this;
110 }
111
112 template <class U>
113 inline TPoint operator/=(const TPoint<U>& p) {
114 x /= static_cast<Type>(p.x);
115 y /= static_cast<Type>(p.y);
116 return *this;
117 }
118
119 template <class U>
120 inline TPoint operator/=(const TSize<U>& s) {
121 x /= static_cast<Type>(s.width);
122 y /= static_cast<Type>(s.height);
123 return *this;
124 }
125
126 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
127 inline TPoint operator/=(U scale) {
128 x /= static_cast<Type>(scale);
129 y /= static_cast<Type>(scale);
130 return *this;
131 }
132
133 constexpr TPoint operator-() const { return {-x, -y}; }
134
135 constexpr TPoint operator+(const TPoint& p) const {
136 return {x + p.x, y + p.y};
137 }
138
139 template <class U>
140 constexpr TPoint operator+(const TSize<U>& s) const {
141 return {x + static_cast<Type>(s.width), y + static_cast<Type>(s.height)};
142 }
143
144 constexpr TPoint operator-(const TPoint& p) const {
145 return {x - p.x, y - p.y};
146 }
147
148 template <class U>
149 constexpr TPoint operator-(const TSize<U>& s) const {
150 return {x - static_cast<Type>(s.width), y - static_cast<Type>(s.height)};
151 }
152
153 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
154 constexpr TPoint operator*(U scale) const {
155 return {static_cast<Type>(x * scale), static_cast<Type>(y * scale)};
156 }
157
158 constexpr TPoint operator*(const TPoint& p) const {
159 return {x * p.x, y * p.y};
160 }
161
162 template <class U>
163 constexpr TPoint operator*(const TSize<U>& s) const {
164 return {x * static_cast<Type>(s.width), y * static_cast<Type>(s.height)};
165 }
166
167 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
168 constexpr TPoint operator/(U d) const {
169 return {static_cast<Type>(x / d), static_cast<Type>(y / d)};
170 }
171
172 constexpr TPoint operator/(const TPoint& p) const {
173 return {x / p.x, y / p.y};
174 }
175
176 template <class U>
177 constexpr TPoint operator/(const TSize<U>& s) const {
178 return {x / static_cast<Type>(s.width), y / static_cast<Type>(s.height)};
179 }
180
181 constexpr Type GetDistanceSquared(const TPoint& p) const {
182 double dx = p.x - x;
183 double dy = p.y - y;
184 return dx * dx + dy * dy;
185 }
186
187 constexpr TPoint Min(const TPoint& p) const {
188 return {std::min<Type>(x, p.x), std::min<Type>(y, p.y)};
189 }
190
191 constexpr TPoint Max(const TPoint& p) const {
192 return {std::max<Type>(x, p.x), std::max<Type>(y, p.y)};
193 }
194
195 constexpr TPoint Floor() const { return {std::floor(x), std::floor(y)}; }
196
197 constexpr TPoint Ceil() const { return {std::ceil(x), std::ceil(y)}; }
198
199 constexpr TPoint Round() const { return {std::round(x), std::round(y)}; }
200
201 constexpr Type GetDistance(const TPoint& p) const {
202 return sqrt(GetDistanceSquared(p));
203 }
204
205 constexpr Type GetLengthSquared() const {
206 return static_cast<double>(x) * x + static_cast<double>(y) * y;
207 }
208
209 constexpr Type GetLength() const { return std::sqrt(GetLengthSquared()); }
210
211 /// Returns the distance (squared) from this point to the closest point on
212 /// the line segment p0 -> p1.
213 ///
214 /// If the projection of this point onto the line defined by the two points
215 /// is between them, the distance (squared) to that point is returned.
216 /// Otherwise, we return the distance (squared) to the endpoint that is
217 /// closer to the projected point.
218 Type GetDistanceToSegmentSquared(TPoint p0, TPoint p1) const {
219 // Compute relative vectors to one endpoint of the segment (p0)
220 TPoint u = p1 - p0;
221 TPoint v = *this - p0;
222
223 // Compute the projection of (this point) onto p0->p1.
224 Scalar dot = u.Dot(v);
225 if (dot <= 0) {
226 // The projection lands outside the segment on the p0 side.
227 // The result is the (square of the) distance to p0 (length of v).
228 return v.GetLengthSquared();
229 }
230
231 // The dot product is the product of the length of the two vectors
232 // ||u|| and ||v|| and the cosine of the angle between them. The length
233 // of the v vector times the cosine is the same as the length of
234 // the projection of the v vector onto the u vector (consider a right
235 // triangle [(0,0), v, v_projected], the length of v multipled by the
236 // cosine is the length of v_projected).
237 //
238 // Thus the dot product is also the product of the u vector and the
239 // projected shadow of the v vector onto the u vector.
240 //
241 // So, if the dot product is larger than the square of the length of
242 // the u vector, then the v vector was projected onto the line beyond
243 // the end of the u vector and so we can use the distance formula to
244 // that endpoint as our result.
245 Scalar uLengthSquared = u.GetLengthSquared();
246 if (dot >= uLengthSquared) {
247 // The projection lands outside the segment on the p1 side.
248 // The result is the (square of the) distance to p1.
249 return GetDistanceSquared(p1);
250 }
251
252 // We must now compute the distance from this point to its projection
253 // on to the segment.
254 //
255 // We compute the cross product of the two vectors u and v which
256 // gives us the area of the parallelogram [(0,0), u, u+v, v]. That
257 // parallelogram area is also the product of the length of one of its
258 // sides and the height perpendicular to that side. We have the length
259 // of one side which is the length of the segment itself (squared) as
260 // uLengthSquared, so if we divide the parallelogram area (squared)
261 // by uLengthSquared then we will get its height (squared) relative to u.
262 //
263 // That height is also the distance from this point to the line segment.
264 Scalar cross = u.Cross(v);
265 // The cross product may currently be signed, but we will square it later.
266
267 // To get our height (squared), we want to compute:
268 // result^2 == h^2 == (cross * cross / uLengthSquared)
269 //
270 // We reorder the equation slightly to avoid infinities:
271 return (cross / uLengthSquared) * cross;
272 }
273
274 /// Returns the distance from this point to the closest point on the line
275 /// segment p0 -> p1.
276 ///
277 /// If the projection of this point onto the line defined by the two points
278 /// is between them, the distance to that point is returned. Otherwise,
279 /// we return the distance to the endpoint that is closer to the projected
280 /// point.
281 constexpr Type GetDistanceToSegment(TPoint p0, TPoint p1) const {
282 return std::sqrt(GetDistanceToSegmentSquared(p0, p1));
283 }
284
285 constexpr TPoint Normalize() const {
286 const auto length = GetLength();
287 if (length == 0) {
288 return {1, 0};
289 }
290 return {x / length, y / length};
291 }
292
293 constexpr TPoint Abs() const { return {std::fabs(x), std::fabs(y)}; }
294
295 constexpr Type Cross(const TPoint& p) const { return (x * p.y) - (y * p.x); }
296
297 /// Return the cross product representing the sign (turning direction) and
298 /// magnitude (sin of the angle) of the angle from p1 to p2 as viewed from
299 /// p0.
300 ///
301 /// Equivalent to ((p1 - p0).Cross(p2 - p0)).
302 static constexpr Type Cross(const TPoint& p0,
303 const TPoint& p1,
304 const TPoint& p2) {
305 return (p1 - p0).Cross(p2 - p0);
306 }
307
308 constexpr Type Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); }
309
310 constexpr TPoint Reflect(const TPoint& axis) const {
311 return *this - axis * this->Dot(axis) * 2;
312 }
313
314 constexpr TPoint Rotate(const Radians& angle) const {
315 const auto cos_a = std::cosf(angle.radians);
316 const auto sin_a = std::sinf(angle.radians);
317 return {x * cos_a - y * sin_a, x * sin_a + y * cos_a};
318 }
319
320 /// Return the perpendicular vector turning to the right (Clockwise)
321 /// in the logical coordinate system where X increases to the right and Y
322 /// increases downward.
323 constexpr TPoint PerpendicularRight() const { return {-y, x}; }
324
325 /// Return the perpendicular vector turning to the left (Counterclockwise)
326 /// in the logical coordinate system where X increases to the right and Y
327 /// increases downward.
328 constexpr TPoint PerpendicularLeft() const { return {y, -x}; }
329
330 constexpr Radians AngleTo(const TPoint& p) const {
331 return Radians{std::atan2(this->Cross(p), this->Dot(p))};
332 }
333
334 constexpr TPoint Lerp(const TPoint& p, Scalar t) const {
335 return *this + (p - *this) * t;
336 }
337
338 constexpr bool IsZero() const { return x == 0 && y == 0; }
339
340 ONLY_ON_FLOAT_M(constexpr, bool)
341 IsFinite() const { return std::isfinite(x) && std::isfinite(y); }
342 };
343
344 // Specializations for mixed (float & integer) algebraic operations.
345
346 template <class F, class I, class = MixedOp<F, I>>
347 constexpr TPoint<F> operator+(const TPoint<F>& p1, const TPoint<I>& p2) {
348 return {p1.x + static_cast<F>(p2.x), p1.y + static_cast<F>(p2.y)};
349 }
350
351 template <class F, class I, class = MixedOp<F, I>>
352 constexpr TPoint<F> operator+(const TPoint<I>& p1, const TPoint<F>& p2) {
353 return p2 + p1;
354 }
355
356 template <class F, class I, class = MixedOp<F, I>>
357 constexpr TPoint<F> operator-(const TPoint<F>& p1, const TPoint<I>& p2) {
358 return {p1.x - static_cast<F>(p2.x), p1.y - static_cast<F>(p2.y)};
359 }
360
361 template <class F, class I, class = MixedOp<F, I>>
362 constexpr TPoint<F> operator-(const TPoint<I>& p1, const TPoint<F>& p2) {
363 return {static_cast<F>(p1.x) - p2.x, static_cast<F>(p1.y) - p2.y};
364 }
365
366 template <class F, class I, class = MixedOp<F, I>>
367 constexpr TPoint<F> operator*(const TPoint<F>& p1, const TPoint<I>& p2) {
368 return {p1.x * static_cast<F>(p2.x), p1.y * static_cast<F>(p2.y)};
369 }
370
371 template <class F, class I, class = MixedOp<F, I>>
372 constexpr TPoint<F> operator*(const TPoint<I>& p1, const TPoint<F>& p2) {
373 return p2 * p1;
374 }
375
376 template <class F, class I, class = MixedOp<F, I>>
377 constexpr TPoint<F> operator/(const TPoint<F>& p1, const TPoint<I>& p2) {
378 return {p1.x / static_cast<F>(p2.x), p1.y / static_cast<F>(p2.y)};
379 }
380
381 template <class F, class I, class = MixedOp<F, I>>
382 constexpr TPoint<F> operator/(const TPoint<I>& p1, const TPoint<F>& p2) {
383 return {static_cast<F>(p1.x) / p2.x, static_cast<F>(p1.y) / p2.y};
384 }
385
386 // RHS algebraic operations with arithmetic types.
387
388 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
389 constexpr TPoint<T> operator*(U s, const TPoint<T>& p) {
390 return p * s;
391 }
392
393 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
394 constexpr TPoint<T> operator/(U s, const TPoint<T>& p) {
395 return {static_cast<T>(s) / p.x, static_cast<T>(s) / p.y};
396 }
397
398 // RHS algebraic operations with TSize.
399
400 template <class T, class U>
401 constexpr TPoint<T> operator+(const TSize<U>& s, const TPoint<T>& p) {
402 return p + s;
403 }
404
405 template <class T, class U>
406 constexpr TPoint<T> operator-(const TSize<U>& s, const TPoint<T>& p) {
407 return {static_cast<T>(s.width) - p.x, static_cast<T>(s.height) - p.y};
408 }
409
410 template <class T, class U>
411 constexpr TPoint<T> operator*(const TSize<U>& s, const TPoint<T>& p) {
412 return p * s;
413 }
414
415 template <class T, class U>
416 constexpr TPoint<T> operator/(const TSize<U>& s, const TPoint<T>& p) {
417 return {static_cast<T>(s.width) / p.x, static_cast<T>(s.height) / p.y};
418 }
419
420 template <class T>
421 constexpr TPoint<T> operator-(const TPoint<T>& p, T v) {
422 return {p.x - v, p.y - v};
423 }
424
425 using Point = TPoint<Scalar>;
426 using IPoint = TPoint<int64_t>;
427 using IPoint32 = TPoint<int32_t>;
428 using UintPoint32 = TPoint<uint32_t>;
429 using Vector2 = Point;
430 using Quad = std::array<Point, 4>;
431
432 [[maybe_unused]]
433 static constexpr impeller::Vector2 kQuadrantAxes[4] = {
434 {1.0f, 0.0f},
435 {0.0f, 1.0f},
436 {-1.0f, 0.0f},
437 {0.0f, -1.0f},
438 };
439
440 #undef ONLY_ON_FLOAT
441 #undef ONLY_ON_FLOAT_M
442
443 } // namespace impeller
444
445 namespace std {
446
447 template <class T>
448 inline std::ostream& operator<<(std::ostream& out,
449 const impeller::TPoint<T>& p) {
450 out << "(" << p.x << ", " << p.y << ")";
451 return out;
452 }
453
454 } // namespace std
455
456 #endif // FLUTTER_IMPELLER_GEOMETRY_POINT_H_
impeller
Definition: allocation.cc:12
impeller::operator-
constexpr Color operator-(T value, const Color &c)
Definition: color.h:903
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::operator/
constexpr Color operator/(T value, const Color &c)
Definition: color.h:914
impeller::operator+
constexpr Color operator+(T value, const Color &c)
Definition: color.h:898
impeller::Point
TPoint< Scalar > Point
Definition: point.h:425
impeller::kQuadrantAxes
static constexpr impeller::Vector2 kQuadrantAxes[4]
Definition: point.h:433
impeller::operator*
constexpr Color operator*(T value, const Color &c)
Definition: color.h:909
impeller::Quad
std::array< Point, 4 > Quad
Definition: point.h:430
std
Definition: comparable.h:95
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
ONLY_ON_FLOAT_M
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition: point.h:22
p2
Point p2
Definition: round_superellipse_param.cc:317
p1
Point p1
Definition: round_superellipse_param.cc:313
scalar.h
size.h
impeller::Radians
Definition: scalar.h:44
impeller::Radians::radians
Scalar radians
Definition: scalar.h:45
impeller::TPoint
Definition: point.h:28
impeller::TPoint::Abs
constexpr TPoint Abs() const
Definition: point.h:293
impeller::TPoint::Round
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:50
impeller::TPoint::Max
constexpr TPoint Max(const TPoint &p) const
Definition: point.h:191
impeller::TPoint::operator/=
TPoint operator/=(U scale)
Definition: point.h:127
impeller::TPoint::operator-=
TPoint operator-=(const TSize< U > &s)
Definition: point.h:85
impeller::TPoint::GetDistanceToSegmentSquared
Type GetDistanceToSegmentSquared(TPoint p0, TPoint p1) const
Definition: point.h:218
impeller::TPoint::GetLength
constexpr Type GetLength() const
Definition: point.h:209
impeller::TPoint::operator/
constexpr TPoint operator/(const TSize< U > &s) const
Definition: point.h:177
impeller::TPoint::GetDistanceToSegment
constexpr Type GetDistanceToSegment(TPoint p0, TPoint p1) const
Definition: point.h:281
impeller::TPoint::Ceil
constexpr TPoint Ceil() const
Definition: point.h:197
impeller::TPoint::operator!=
constexpr bool operator!=(const TPoint &p) const
Definition: point.h:59
impeller::TPoint::MakeXY
static constexpr TPoint< Type > MakeXY(Type x, Type y)
Definition: point.h:47
impeller::TPoint::TPoint
constexpr TPoint(Type x, Type y)
Definition: point.h:45
impeller::TPoint::Normalize
constexpr TPoint Normalize() const
Definition: point.h:285
impeller::TPoint::Cross
constexpr Type Cross(const TPoint &p) const
Definition: point.h:295
impeller::TPoint::IsZero
constexpr bool IsZero() const
Definition: point.h:338
impeller::TPoint::GetLengthSquared
constexpr Type GetLengthSquared() const
Definition: point.h:205
impeller::TPoint::PerpendicularLeft
constexpr TPoint PerpendicularLeft() const
Definition: point.h:328
impeller::TPoint::GetDistance
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:201
impeller::TPoint::Lerp
constexpr TPoint Lerp(const TPoint &p, Scalar t) const
Definition: point.h:334
impeller::TPoint::operator+=
TPoint operator+=(const TSize< U > &s)
Definition: point.h:71
impeller::TPoint::operator-
constexpr TPoint operator-(const TPoint &p) const
Definition: point.h:144
impeller::TPoint::PerpendicularRight
constexpr TPoint PerpendicularRight() const
Definition: point.h:323
impeller::TPoint::Cross
static constexpr Type Cross(const TPoint &p0, const TPoint &p1, const TPoint &p2)
Definition: point.h:302
impeller::TPoint::operator/
constexpr TPoint operator/(U d) const
Definition: point.h:168
impeller::TPoint::Type
T Type
Definition: point.h:29
impeller::TPoint::Floor
constexpr TPoint Floor() const
Definition: point.h:195
impeller::TPoint::operator*
constexpr TPoint operator*(const TSize< U > &s) const
Definition: point.h:163
impeller::TPoint::TPoint
constexpr TPoint()=default
impeller::TPoint::operator==
constexpr bool operator==(const TPoint &p) const
Definition: point.h:55
impeller::TPoint::operator/
constexpr TPoint operator/(const TPoint &p) const
Definition: point.h:172
impeller::TPoint::operator+
constexpr TPoint operator+(const TSize< U > &s) const
Definition: point.h:140
impeller::TPoint::operator/=
TPoint operator/=(const TPoint< U > &p)
Definition: point.h:113
impeller::TPoint::Round
constexpr TPoint Round() const
Definition: point.h:199
impeller::TPoint::IsFinite
IsFinite() const
Definition: point.h:341
impeller::TPoint::operator+=
TPoint operator+=(const TPoint< U > &p)
Definition: point.h:64
impeller::TPoint::TPoint
constexpr TPoint(const TPoint< U > &other)
Definition: point.h:37
impeller::TPoint::operator-
constexpr TPoint operator-(const TSize< U > &s) const
Definition: point.h:149
impeller::TPoint::Rotate
constexpr TPoint Rotate(const Radians &angle) const
Definition: point.h:314
impeller::TPoint::operator+
constexpr TPoint operator+(const TPoint &p) const
Definition: point.h:135
impeller::TPoint::y
Type y
Definition: point.h:32
impeller::TPoint::x
Type x
Definition: point.h:31
impeller::TPoint::operator*=
TPoint operator*=(const TPoint< U > &p)
Definition: point.h:92
impeller::TPoint::Reflect
constexpr TPoint Reflect(const TPoint &axis) const
Definition: point.h:310
impeller::TPoint::Min
constexpr TPoint Min(const TPoint &p) const
Definition: point.h:187
impeller::TPoint::operator/=
TPoint operator/=(const TSize< U > &s)
Definition: point.h:120
impeller::TPoint::operator-=
TPoint operator-=(const TPoint< U > &p)
Definition: point.h:78
impeller::TPoint::operator-
constexpr TPoint operator-() const
Definition: point.h:133
impeller::TPoint::TPoint
constexpr TPoint(const TSize< U > &other)
Definition: point.h:41
impeller::TPoint::operator*=
TPoint operator*=(U scale)
Definition: point.h:106
impeller::TPoint::Dot
constexpr Type Dot(const TPoint &p) const
Definition: point.h:308
impeller::TPoint::GetDistanceSquared
constexpr Type GetDistanceSquared(const TPoint &p) const
Definition: point.h:181
impeller::TPoint::AngleTo
constexpr Radians AngleTo(const TPoint &p) const
Definition: point.h:330
impeller::TPoint::operator*=
TPoint operator*=(const TSize< U > &s)
Definition: point.h:99
impeller::TPoint::operator*
constexpr TPoint operator*(const TPoint &p) const
Definition: point.h:158
impeller::TPoint::operator*
constexpr TPoint operator*(U scale) const
Definition: point.h:154
impeller::TSize
Definition: size.h:25
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28
type_traits.h