Flutter Engine
 
Loading...
Searching...
No Matches
point.h File Reference
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <ostream>
#include <string>
#include <type_traits>
#include "impeller/geometry/scalar.h"
#include "impeller/geometry/size.h"
#include "impeller/geometry/type_traits.h"

Go to the source code of this file.

Classes

struct  impeller::TPoint< T >
 

Namespaces

namespace  impeller
 
namespace  std
 

Macros

#define ONLY_ON_FLOAT_M(Modifiers, Return)
 
#define ONLY_ON_FLOAT(Return)   DL_ONLY_ON_FLOAT_M(, Return)
 

Typedefs

using impeller::Point = TPoint< Scalar >
 
using impeller::IPoint = TPoint< int64_t >
 
using impeller::IPoint32 = TPoint< int32_t >
 
using impeller::UintPoint32 = TPoint< uint32_t >
 
using impeller::Vector2 = Point
 
using impeller::Quad = std::array< Point, 4 >
 

Functions

template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator+ (const TPoint< F > &p1, const TPoint< I > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator+ (const TPoint< I > &p1, const TPoint< F > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator- (const TPoint< F > &p1, const TPoint< I > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator- (const TPoint< I > &p1, const TPoint< F > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator* (const TPoint< F > &p1, const TPoint< I > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator* (const TPoint< I > &p1, const TPoint< F > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator/ (const TPoint< F > &p1, const TPoint< I > &p2)
 
template<class F , class I , class = MixedOp<F, I>>
constexpr TPoint< F > impeller::operator/ (const TPoint< I > &p1, const TPoint< F > &p2)
 
template<class T , class U , class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TPoint< T > impeller::operator* (U s, const TPoint< T > &p)
 
template<class T , class U , class = std::enable_if_t<std::is_arithmetic_v<U>>>
constexpr TPoint< T > impeller::operator/ (U s, const TPoint< T > &p)
 
template<class T , class U >
constexpr TPoint< T > impeller::operator+ (const TSize< U > &s, const TPoint< T > &p)
 
template<class T , class U >
constexpr TPoint< T > impeller::operator- (const TSize< U > &s, const TPoint< T > &p)
 
template<class T , class U >
constexpr TPoint< T > impeller::operator* (const TSize< U > &s, const TPoint< T > &p)
 
template<class T , class U >
constexpr TPoint< T > impeller::operator/ (const TSize< U > &s, const TPoint< T > &p)
 
template<class T >
constexpr TPoint< T > impeller::operator- (const TPoint< T > &p, T v)
 
template<class T >
std::ostream & std::operator<< (std::ostream &out, const impeller::TPoint< T > &p)
 

Variables

static constexpr impeller::Vector2 impeller::kQuadrantAxes [4]
 

Macro Definition Documentation

◆ ONLY_ON_FLOAT

#define ONLY_ON_FLOAT (   Return)    DL_ONLY_ON_FLOAT_M(, Return)

Definition at line 24 of file point.h.

◆ ONLY_ON_FLOAT_M

#define ONLY_ON_FLOAT_M (   Modifiers,
  Return 
)
Value:
template <typename U = T> \
Modifiers std::enable_if_t<std::is_floating_point_v<U>, Return>

Definition at line 21 of file point.h.

27 {
28 using Type = T;
29
30 Type x = {};
31 Type y = {};
32
33 constexpr TPoint() = default;
34
35 template <class U>
36 explicit constexpr TPoint(const TPoint<U>& other)
37 : TPoint(static_cast<Type>(other.x), static_cast<Type>(other.y)) {}
38
39 template <class U>
40 explicit constexpr TPoint(const TSize<U>& other)
41 : TPoint(static_cast<Type>(other.width),
42 static_cast<Type>(other.height)) {}
43
44 constexpr TPoint(Type x, Type y) : x(x), y(y) {}
45
46 static constexpr TPoint<Type> MakeXY(Type x, Type y) { return {x, y}; }
47
48 template <class U>
49 static constexpr TPoint Round(const TPoint<U>& other) {
50 return TPoint{static_cast<Type>(std::round(other.x)),
51 static_cast<Type>(std::round(other.y))};
52 }
53
54 constexpr bool operator==(const TPoint& p) const {
55 return p.x == x && p.y == y;
56 }
57
58 constexpr bool operator!=(const TPoint& p) const {
59 return p.x != x || p.y != y;
60 }
61
62 template <class U>
63 inline TPoint operator+=(const TPoint<U>& p) {
64 x += static_cast<Type>(p.x);
65 y += static_cast<Type>(p.y);
66 return *this;
67 }
68
69 template <class U>
70 inline TPoint operator+=(const TSize<U>& s) {
71 x += static_cast<Type>(s.width);
72 y += static_cast<Type>(s.height);
73 return *this;
74 }
75
76 template <class U>
77 inline TPoint operator-=(const TPoint<U>& p) {
78 x -= static_cast<Type>(p.x);
79 y -= static_cast<Type>(p.y);
80 return *this;
81 }
82
83 template <class U>
84 inline TPoint operator-=(const TSize<U>& s) {
85 x -= static_cast<Type>(s.width);
86 y -= static_cast<Type>(s.height);
87 return *this;
88 }
89
90 template <class U>
91 inline TPoint operator*=(const TPoint<U>& p) {
92 x *= static_cast<Type>(p.x);
93 y *= static_cast<Type>(p.y);
94 return *this;
95 }
96
97 template <class U>
98 inline TPoint operator*=(const TSize<U>& s) {
99 x *= static_cast<Type>(s.width);
100 y *= static_cast<Type>(s.height);
101 return *this;
102 }
103
104 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
105 inline TPoint operator*=(U scale) {
106 x *= static_cast<Type>(scale);
107 y *= static_cast<Type>(scale);
108 return *this;
109 }
110
111 template <class U>
112 inline TPoint operator/=(const TPoint<U>& p) {
113 x /= static_cast<Type>(p.x);
114 y /= static_cast<Type>(p.y);
115 return *this;
116 }
117
118 template <class U>
119 inline TPoint operator/=(const TSize<U>& s) {
120 x /= static_cast<Type>(s.width);
121 y /= static_cast<Type>(s.height);
122 return *this;
123 }
124
125 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
126 inline TPoint operator/=(U scale) {
127 x /= static_cast<Type>(scale);
128 y /= static_cast<Type>(scale);
129 return *this;
130 }
131
132 constexpr TPoint operator-() const { return {-x, -y}; }
133
134 constexpr TPoint operator+(const TPoint& p) const {
135 return {x + p.x, y + p.y};
136 }
137
138 template <class U>
139 constexpr TPoint operator+(const TSize<U>& s) const {
140 return {x + static_cast<Type>(s.width), y + static_cast<Type>(s.height)};
141 }
142
143 constexpr TPoint operator-(const TPoint& p) const {
144 return {x - p.x, y - p.y};
145 }
146
147 template <class U>
148 constexpr TPoint operator-(const TSize<U>& s) const {
149 return {x - static_cast<Type>(s.width), y - static_cast<Type>(s.height)};
150 }
151
152 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
153 constexpr TPoint operator*(U scale) const {
154 return {static_cast<Type>(x * scale), static_cast<Type>(y * scale)};
155 }
156
157 constexpr TPoint operator*(const TPoint& p) const {
158 return {x * p.x, y * p.y};
159 }
160
161 template <class U>
162 constexpr TPoint operator*(const TSize<U>& s) const {
163 return {x * static_cast<Type>(s.width), y * static_cast<Type>(s.height)};
164 }
165
166 template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
167 constexpr TPoint operator/(U d) const {
168 return {static_cast<Type>(x / d), static_cast<Type>(y / d)};
169 }
170
171 constexpr TPoint operator/(const TPoint& p) const {
172 return {x / p.x, y / p.y};
173 }
174
175 template <class U>
176 constexpr TPoint operator/(const TSize<U>& s) const {
177 return {x / static_cast<Type>(s.width), y / static_cast<Type>(s.height)};
178 }
179
180 constexpr Type GetDistanceSquared(const TPoint& p) const {
181 double dx = p.x - x;
182 double dy = p.y - y;
183 return dx * dx + dy * dy;
184 }
185
186 constexpr TPoint Min(const TPoint& p) const {
187 return {std::min<Type>(x, p.x), std::min<Type>(y, p.y)};
188 }
189
190 constexpr TPoint Max(const TPoint& p) const {
191 return {std::max<Type>(x, p.x), std::max<Type>(y, p.y)};
192 }
193
194 constexpr TPoint Floor() const { return {std::floor(x), std::floor(y)}; }
195
196 constexpr TPoint Ceil() const { return {std::ceil(x), std::ceil(y)}; }
197
198 constexpr TPoint Round() const { return {std::round(x), std::round(y)}; }
199
200 constexpr Type GetDistance(const TPoint& p) const {
201 return sqrt(GetDistanceSquared(p));
202 }
203
204 constexpr Type GetLengthSquared() const { return GetDistanceSquared({}); }
205
206 constexpr Type GetLength() const { return GetDistance({}); }
207
208 constexpr TPoint Normalize() const {
209 const auto length = GetLength();
210 if (length == 0) {
211 return {1, 0};
212 }
213 return {x / length, y / length};
214 }
215
216 constexpr TPoint Abs() const { return {std::fabs(x), std::fabs(y)}; }
217
218 constexpr Type Cross(const TPoint& p) const { return (x * p.y) - (y * p.x); }
219
220 constexpr Type Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); }
221
222 constexpr TPoint Reflect(const TPoint& axis) const {
223 return *this - axis * this->Dot(axis) * 2;
224 }
225
226 constexpr TPoint Rotate(const Radians& angle) const {
227 const auto cos_a = std::cosf(angle.radians);
228 const auto sin_a = std::sinf(angle.radians);
229 return {x * cos_a - y * sin_a, x * sin_a + y * cos_a};
230 }
231
232 constexpr Radians AngleTo(const TPoint& p) const {
233 return Radians{std::atan2(this->Cross(p), this->Dot(p))};
234 }
235
236 constexpr TPoint Lerp(const TPoint& p, Scalar t) const {
237 return *this + (p - *this) * t;
238 }
239
240 constexpr bool IsZero() const { return x == 0 && y == 0; }
241
242 ONLY_ON_FLOAT_M(constexpr, bool)
243 IsFinite() const { return std::isfinite(x) && std::isfinite(y); }
244};
245
246// Specializations for mixed (float & integer) algebraic operations.
247
248template <class F, class I, class = MixedOp<F, I>>
249constexpr TPoint<F> operator+(const TPoint<F>& p1, const TPoint<I>& p2) {
250 return {p1.x + static_cast<F>(p2.x), p1.y + static_cast<F>(p2.y)};
251}
252
253template <class F, class I, class = MixedOp<F, I>>
254constexpr TPoint<F> operator+(const TPoint<I>& p1, const TPoint<F>& p2) {
255 return p2 + p1;
256}
257
258template <class F, class I, class = MixedOp<F, I>>
259constexpr TPoint<F> operator-(const TPoint<F>& p1, const TPoint<I>& p2) {
260 return {p1.x - static_cast<F>(p2.x), p1.y - static_cast<F>(p2.y)};
261}
262
263template <class F, class I, class = MixedOp<F, I>>
264constexpr TPoint<F> operator-(const TPoint<I>& p1, const TPoint<F>& p2) {
265 return {static_cast<F>(p1.x) - p2.x, static_cast<F>(p1.y) - p2.y};
266}
267
268template <class F, class I, class = MixedOp<F, I>>
269constexpr TPoint<F> operator*(const TPoint<F>& p1, const TPoint<I>& p2) {
270 return {p1.x * static_cast<F>(p2.x), p1.y * static_cast<F>(p2.y)};
271}
272
273template <class F, class I, class = MixedOp<F, I>>
274constexpr TPoint<F> operator*(const TPoint<I>& p1, const TPoint<F>& p2) {
275 return p2 * p1;
276}
277
278template <class F, class I, class = MixedOp<F, I>>
279constexpr TPoint<F> operator/(const TPoint<F>& p1, const TPoint<I>& p2) {
280 return {p1.x / static_cast<F>(p2.x), p1.y / static_cast<F>(p2.y)};
281}
282
283template <class F, class I, class = MixedOp<F, I>>
284constexpr TPoint<F> operator/(const TPoint<I>& p1, const TPoint<F>& p2) {
285 return {static_cast<F>(p1.x) / p2.x, static_cast<F>(p1.y) / p2.y};
286}
287
288// RHS algebraic operations with arithmetic types.
289
290template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
291constexpr TPoint<T> operator*(U s, const TPoint<T>& p) {
292 return p * s;
293}
294
295template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
296constexpr TPoint<T> operator/(U s, const TPoint<T>& p) {
297 return {static_cast<T>(s) / p.x, static_cast<T>(s) / p.y};
298}
299
300// RHS algebraic operations with TSize.
301
302template <class T, class U>
303constexpr TPoint<T> operator+(const TSize<U>& s, const TPoint<T>& p) {
304 return p + s;
305}
306
307template <class T, class U>
308constexpr TPoint<T> operator-(const TSize<U>& s, const TPoint<T>& p) {
309 return {static_cast<T>(s.width) - p.x, static_cast<T>(s.height) - p.y};
310}
311
312template <class T, class U>
313constexpr TPoint<T> operator*(const TSize<U>& s, const TPoint<T>& p) {
314 return p * s;
315}
316
317template <class T, class U>
318constexpr TPoint<T> operator/(const TSize<U>& s, const TPoint<T>& p) {
319 return {static_cast<T>(s.width) / p.x, static_cast<T>(s.height) / p.y};
320}
321
322template <class T>
323constexpr TPoint<T> operator-(const TPoint<T>& p, T v) {
324 return {p.x - v, p.y - v};
325}
326
327using Point = TPoint<Scalar>;
328using IPoint = TPoint<int64_t>;
329using IPoint32 = TPoint<int32_t>;
330using UintPoint32 = TPoint<uint32_t>;
331using Vector2 = Point;
332using Quad = std::array<Point, 4>;
333
334[[maybe_unused]]
335static constexpr impeller::Vector2 kQuadrantAxes[4] = {
336 {1.0f, 0.0f},
337 {0.0f, 1.0f},
338 {-1.0f, 0.0f},
339 {0.0f, -1.0f},
340};
341
342#undef ONLY_ON_FLOAT
343#undef ONLY_ON_FLOAT_M
344
345} // namespace impeller
346
347namespace std {
348
349template <class T>
350inline std::ostream& operator<<(std::ostream& out,
351 const impeller::TPoint<T>& p) {
352 out << "(" << p.x << ", " << p.y << ")";
353 return out;
354}
355
356} // namespace std
357
358#endif // FLUTTER_IMPELLER_GEOMETRY_POINT_H_
int32_t x
bool operator==(const FlutterPoint &a, const FlutterPoint &b)
auto & d
Definition main.cc:28
#define ONLY_ON_FLOAT_M(Modifiers, Return)
Definition point.h:21
size_t length
double y
bool operator!=(const T &value, const UniqueObject< T, Traits > &object)
Point Vector2
Definition point.h:331
constexpr Color operator-(T value, const Color &c)
Definition color.h:903
constexpr Color operator/(T value, const Color &c)
Definition color.h:914
constexpr Color operator+(T value, const Color &c)
Definition color.h:898
TPoint< Scalar > Point
Definition point.h:327
TPoint< int32_t > IPoint32
Definition point.h:329
TPoint< int64_t > IPoint
Definition point.h:328
static constexpr impeller::Vector2 kQuadrantAxes[4]
Definition point.h:335
constexpr Color operator*(T value, const Color &c)
Definition color.h:909
TPoint< uint32_t > UintPoint32
Definition point.h:330
std::array< Point, 4 > Quad
Definition point.h:332
Definition ref_ptr.h:261
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition arc.h:141
int32_t height
int32_t width