11
11
12
12
#include " impeller/geometry/scalar.h"
13
13
#include " impeller/geometry/size.h"
14
+ #include " impeller/geometry/type_traits.h"
14
15
15
16
namespace impeller {
16
17
@@ -27,6 +28,11 @@ struct TPoint {
27
28
explicit constexpr TPoint (const TPoint<U>& other)
28
29
: TPoint(static_cast <Type>(other.x), static_cast<Type>(other.y)) {}
29
30
31
+ template <class U >
32
+ explicit constexpr TPoint (const TSize<U>& other)
33
+ : TPoint(static_cast <Type>(other.width),
34
+ static_cast<Type>(other.height)) {}
35
+
30
36
constexpr TPoint (Type x, Type y) : x(x), y(y) {}
31
37
32
38
static constexpr TPoint<Type> MakeXY (Type x, Type y) { return {x, y}; }
@@ -45,16 +51,18 @@ struct TPoint {
45
51
return {x + p.x , y + p.y };
46
52
}
47
53
48
- constexpr TPoint operator +(const TSize<Type>& s) const {
49
- return {x + s.width , y + s.height };
54
+ template <class U >
55
+ constexpr TPoint operator +(const TSize<U>& s) const {
56
+ return {x + static_cast <Type>(s.width ), y + static_cast <Type>(s.height )};
50
57
}
51
58
52
59
constexpr TPoint operator -(const TPoint& p) const {
53
60
return {x - p.x , y - p.y };
54
61
}
55
62
56
- constexpr TPoint operator -(const TSize<Type>& s) const {
57
- return {x - s.width , y - s.height };
63
+ template <class U >
64
+ constexpr TPoint operator -(const TSize<U>& s) const {
65
+ return {x - static_cast <Type>(s.width ), y - static_cast <Type>(s.height )};
58
66
}
59
67
60
68
constexpr TPoint operator *(Scalar scale) const {
@@ -65,8 +73,9 @@ struct TPoint {
65
73
return {x * p.x , y * p.y };
66
74
}
67
75
68
- constexpr TPoint operator *(const TSize<Type>& s) const {
69
- return {x * s.width , y * s.height };
76
+ template <class U >
77
+ constexpr TPoint operator *(const TSize<U>& s) const {
78
+ return {x * static_cast <Type>(s.width ), y * static_cast <Type>(s.height )};
70
79
}
71
80
72
81
constexpr TPoint operator /(Scalar d) const { return {x / d, y / d}; }
@@ -75,8 +84,9 @@ struct TPoint {
75
84
return {x / p.x , y / p.y };
76
85
}
77
86
78
- constexpr TPoint operator /(const TSize<Type>& s) const {
79
- return {x / s.width , y / s.height };
87
+ template <class U >
88
+ constexpr TPoint operator /(const TSize<U>& s) const {
89
+ return {x / static_cast <Type>(s.width ), y / static_cast <Type>(s.height )};
80
90
}
81
91
82
92
constexpr Type GetDistanceSquared (const TPoint& p) const {
@@ -112,6 +122,70 @@ struct TPoint {
112
122
constexpr bool IsZero () const { return x == 0 && y == 0 ; }
113
123
};
114
124
125
+ // Specializations for mixed (float & integer) algebraic operations.
126
+
127
+ template <class F , class I , class = MixedOp<F, I>>
128
+ constexpr TPoint<F> operator +(const TPoint<F>& p1, const TPoint<I>& p2) {
129
+ return {p1.x + static_cast <F>(p2.x ), p1.y + static_cast <F>(p2.y )};
130
+ }
131
+
132
+ template <class F , class I , class = MixedOp<F, I>>
133
+ constexpr TPoint<F> operator +(const TPoint<I>& p1, const TPoint<F>& p2) {
134
+ return p2 + p1;
135
+ }
136
+
137
+ template <class F , class I , class = MixedOp<F, I>>
138
+ constexpr TPoint<F> operator -(const TPoint<F>& p1, const TPoint<I>& p2) {
139
+ return {p1.x - static_cast <F>(p2.x ), p1.y - static_cast <F>(p2.y )};
140
+ }
141
+
142
+ template <class F , class I , class = MixedOp<F, I>>
143
+ constexpr TPoint<F> operator -(const TPoint<I>& p1, const TPoint<F>& p2) {
144
+ return {static_cast <F>(p1.x ) - p2.x , static_cast <F>(p1.y ) - p2.y };
145
+ }
146
+
147
+ template <class F , class I , class = MixedOp<F, I>>
148
+ constexpr TPoint<F> operator *(const TPoint<F>& p1, const TPoint<I>& p2) {
149
+ return {p1.x * static_cast <F>(p2.x ), p1.y * static_cast <F>(p2.y )};
150
+ }
151
+
152
+ template <class F , class I , class = MixedOp<F, I>>
153
+ constexpr TPoint<F> operator *(const TPoint<I>& p1, const TPoint<F>& p2) {
154
+ return p2 * p1;
155
+ }
156
+
157
+ template <class F , class I , class = MixedOp<F, I>>
158
+ constexpr TPoint<F> operator /(const TPoint<F>& p1, const TPoint<I>& p2) {
159
+ return {p1.x / static_cast <F>(p2.x ), p1.y / static_cast <F>(p2.y )};
160
+ }
161
+
162
+ template <class F , class I , class = MixedOp<F, I>>
163
+ constexpr TPoint<F> operator /(const TPoint<I>& p1, const TPoint<F>& p2) {
164
+ return {static_cast <F>(p1.x ) / p2.x , static_cast <F>(p1.y ) / p2.y };
165
+ }
166
+
167
+ // RHS algebraic operations with TSize.
168
+
169
+ template <class T , class U >
170
+ constexpr TPoint<T> operator +(const TSize<U>& s, const TPoint<T>& p) {
171
+ return p + s;
172
+ }
173
+
174
+ template <class T , class U >
175
+ constexpr TPoint<T> operator -(const TSize<U>& s, const TPoint<T>& p) {
176
+ return {static_cast <T>(s.width ) - p.x , static_cast <T>(s.height ) - p.y };
177
+ }
178
+
179
+ template <class T , class U >
180
+ constexpr TPoint<T> operator *(const TSize<U>& s, const TPoint<T>& p) {
181
+ return p * s;
182
+ }
183
+
184
+ template <class T , class U >
185
+ constexpr TPoint<T> operator /(const TSize<U>& s, const TPoint<T>& p) {
186
+ return {static_cast <T>(s.width ) / p.x , static_cast <T>(s.height ) / p.y };
187
+ }
188
+
115
189
using Point = TPoint<Scalar>;
116
190
using IPoint = TPoint<int64_t >;
117
191
0 commit comments