Flutter Engine
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 
14 #include "impeller/geometry/size.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  constexpr bool operator==(const TPoint& p) const {
42  return p.x == x && p.y == y;
43  }
44 
45  constexpr bool operator!=(const TPoint& p) const {
46  return p.x != x || p.y != y;
47  }
48 
49  template <class U>
50  inline TPoint operator+=(const TPoint<U>& p) {
51  x += static_cast<Type>(p.x);
52  y += static_cast<Type>(p.y);
53  return *this;
54  }
55 
56  template <class U>
57  inline TPoint operator+=(const TSize<U>& s) {
58  x += static_cast<Type>(s.width);
59  y += static_cast<Type>(s.height);
60  return *this;
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, class = std::enable_if_t<std::is_arithmetic_v<U>>>
92  inline TPoint operator*=(U scale) {
93  x *= static_cast<Type>(scale);
94  y *= static_cast<Type>(scale);
95  return *this;
96  }
97 
98  template <class U>
99  inline TPoint operator/=(const TPoint<U>& p) {
100  x /= static_cast<Type>(p.x);
101  y /= static_cast<Type>(p.y);
102  return *this;
103  }
104 
105  template <class U>
106  inline TPoint operator/=(const TSize<U>& s) {
107  x /= static_cast<Type>(s.width);
108  y /= static_cast<Type>(s.height);
109  return *this;
110  }
111 
112  template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
113  inline TPoint operator/=(U scale) {
114  x /= static_cast<Type>(scale);
115  y /= static_cast<Type>(scale);
116  return *this;
117  }
118 
119  constexpr TPoint operator-() const { return {-x, -y}; }
120 
121  constexpr TPoint operator+(const TPoint& p) const {
122  return {x + p.x, y + p.y};
123  }
124 
125  template <class U>
126  constexpr TPoint operator+(const TSize<U>& s) const {
127  return {x + static_cast<Type>(s.width), y + static_cast<Type>(s.height)};
128  }
129 
130  constexpr TPoint operator-(const TPoint& p) const {
131  return {x - p.x, y - p.y};
132  }
133 
134  template <class U>
135  constexpr TPoint operator-(const TSize<U>& s) const {
136  return {x - static_cast<Type>(s.width), y - static_cast<Type>(s.height)};
137  }
138 
139  template <class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
140  constexpr TPoint operator*(U scale) const {
141  return {static_cast<Type>(x * scale), static_cast<Type>(y * scale)};
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 d) const {
155  return {static_cast<Type>(x / d), static_cast<Type>(y / d)};
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  constexpr Type GetDistanceSquared(const TPoint& p) const {
168  double dx = p.x - x;
169  double dy = p.y - y;
170  return dx * dx + dy * dy;
171  }
172 
173  constexpr TPoint Min(const TPoint& p) const {
174  return {std::min<Type>(x, p.x), std::min<Type>(y, p.y)};
175  }
176 
177  constexpr TPoint Max(const TPoint& p) const {
178  return {std::max<Type>(x, p.x), std::max<Type>(y, p.y)};
179  }
180 
181  constexpr Type GetDistance(const TPoint& p) const {
182  return sqrt(GetDistanceSquared(p));
183  }
184 
185  constexpr Type GetLengthSquared() const { return GetDistanceSquared({}); }
186 
187  constexpr Type GetLength() const { return GetDistance({}); }
188 
189  constexpr TPoint Normalize() const {
190  const auto length = GetLength();
191  if (length == 0) {
192  return {};
193  }
194  return {x / length, y / length};
195  }
196 
197  constexpr TPoint Abs() const { return {std::fabs(x), std::fabs(y)}; }
198 
199  constexpr Type Cross(const TPoint& p) const { return (x * p.y) - (y * p.x); }
200 
201  constexpr Type Dot(const TPoint& p) const { return (x * p.x) + (y * p.y); }
202 
203  constexpr TPoint Reflect(const TPoint& axis) const {
204  return *this - axis * this->Dot(axis) * 2;
205  }
206 
207  constexpr bool IsZero() const { return x == 0 && y == 0; }
208 };
209 
210 // Specializations for mixed (float & integer) algebraic operations.
211 
212 template <class F, class I, class = MixedOp<F, I>>
213 constexpr TPoint<F> operator+(const TPoint<F>& p1, const TPoint<I>& p2) {
214  return {p1.x + static_cast<F>(p2.x), p1.y + static_cast<F>(p2.y)};
215 }
216 
217 template <class F, class I, class = MixedOp<F, I>>
218 constexpr TPoint<F> operator+(const TPoint<I>& p1, const TPoint<F>& p2) {
219  return p2 + p1;
220 }
221 
222 template <class F, class I, class = MixedOp<F, I>>
223 constexpr TPoint<F> operator-(const TPoint<F>& p1, const TPoint<I>& p2) {
224  return {p1.x - static_cast<F>(p2.x), p1.y - static_cast<F>(p2.y)};
225 }
226 
227 template <class F, class I, class = MixedOp<F, I>>
228 constexpr TPoint<F> operator-(const TPoint<I>& p1, const TPoint<F>& p2) {
229  return {static_cast<F>(p1.x) - p2.x, static_cast<F>(p1.y) - p2.y};
230 }
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 // RHS algebraic operations with arithmetic types.
253 
254 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
255 constexpr TPoint<T> operator*(U s, const TPoint<T>& p) {
256  return p * s;
257 }
258 
259 template <class T, class U, class = std::enable_if_t<std::is_arithmetic_v<U>>>
260 constexpr TPoint<T> operator/(U s, const TPoint<T>& p) {
261  return {static_cast<T>(s) / p.x, static_cast<T>(s) / p.y};
262 }
263 
264 // RHS algebraic operations with TSize.
265 
266 template <class T, class U>
267 constexpr TPoint<T> operator+(const TSize<U>& s, const TPoint<T>& p) {
268  return p + s;
269 }
270 
271 template <class T, class U>
272 constexpr TPoint<T> operator-(const TSize<U>& s, const TPoint<T>& p) {
273  return {static_cast<T>(s.width) - p.x, static_cast<T>(s.height) - p.y};
274 }
275 
276 template <class T, class U>
277 constexpr TPoint<T> operator*(const TSize<U>& s, const TPoint<T>& p) {
278  return p * s;
279 }
280 
281 template <class T, class U>
282 constexpr TPoint<T> operator/(const TSize<U>& s, const TPoint<T>& p) {
283  return {static_cast<T>(s.width) / p.x, static_cast<T>(s.height) / p.y};
284 }
285 
288 using Vector2 = Point;
289 
290 } // namespace impeller
291 
292 namespace std {
293 
294 template <class T>
295 inline std::ostream& operator<<(std::ostream& out,
296  const impeller::TPoint<T>& p) {
297  out << "(" << p.x << ", " << p.y << ")";
298  return out;
299 }
300 
301 } // namespace std
constexpr TPoint Reflect(const TPoint &axis) const
Definition: point.h:203
constexpr TPoint(const TPoint< U > &other)
Definition: point.h:29
TPoint operator/=(const TPoint< U > &p)
Definition: point.h:99
constexpr Type Dot(const TPoint &p) const
Definition: point.h:201
constexpr TPoint operator/(const TSize< U > &s) const
Definition: point.h:163
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:18
constexpr TPoint Normalize() const
Definition: point.h:189
constexpr bool operator==(const TPoint &p) const
Definition: point.h:41
Type width
Definition: size.h:21
constexpr Type GetLengthSquared() const
Definition: point.h:185
constexpr Type Cross(const TPoint &p) const
Definition: point.h:199
Type height
Definition: size.h:22
TPoint operator*=(const TSize< U > &s)
Definition: point.h:85
TPoint operator-=(const TPoint< U > &p)
Definition: point.h:64
constexpr TPoint operator/(const TPoint &p) const
Definition: point.h:158
Definition: ref_ptr.h:255
constexpr TPoint()=default
constexpr TPoint Abs() const
Definition: point.h:197
constexpr TPoint operator*(U scale) const
Definition: point.h:140
constexpr TPoint Max(const TPoint &p) const
Definition: point.h:177
TPoint operator*=(U scale)
Definition: point.h:92
constexpr Type GetDistance(const TPoint &p) const
Definition: point.h:181
static constexpr TPoint< Type > MakeXY(Type x, Type y)
Definition: point.h:39
constexpr TPoint operator*(const TSize< U > &s) const
Definition: point.h:149
std::ostream & operator<<(std::ostream &out, const impeller::TPoint< T > &p)
Definition: point.h:295
constexpr TPoint operator-() const
Definition: point.h:119
constexpr TPoint operator+(const TSize< U > &s) const
Definition: point.h:126
constexpr TPoint(Type x, Type y)
Definition: point.h:37
constexpr TPoint(const TSize< U > &other)
Definition: point.h:33
constexpr TPoint operator*(const TPoint &p) const
Definition: point.h:144
TPoint operator+=(const TPoint< U > &p)
Definition: point.h:50
TPoint operator/=(const TSize< U > &s)
Definition: point.h:106
int32_t width
int32_t height
size_t length
TPoint operator/=(U scale)
Definition: point.h:113
constexpr TPoint operator-(const TSize< U > &s) const
Definition: point.h:135
constexpr bool IsZero() const
Definition: point.h:207
TPoint operator-=(const TSize< U > &s)
Definition: point.h:71
TPoint operator+=(const TSize< U > &s)
Definition: point.h:57
constexpr TPoint operator-(const TPoint &p) const
Definition: point.h:130
constexpr Type GetLength() const
Definition: point.h:187
constexpr bool operator!=(const TPoint &p) const
Definition: point.h:45
constexpr TPoint operator/(U d) const
Definition: point.h:154
TPoint< Scalar > Point
Definition: point.h:286
constexpr Type GetDistanceSquared(const TPoint &p) const
Definition: point.h:167
constexpr TPoint Min(const TPoint &p) const
Definition: point.h:173
TPoint operator*=(const TPoint< U > &p)
Definition: point.h:78
constexpr TPoint operator+(const TPoint &p) const
Definition: point.h:121