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