1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef RENDER_SERVICE_CLIENT_CORE_COMMON_RS_VECTOR2_H
17 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_VECTOR2_H
18 #include <cmath>
19
20 #include "common/rs_common_def.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 template<typename T>
25 class Vector2 {
26 public:
27 union {
28 struct {
29 T x_;
30 T y_;
31 };
32 T data_[2];
33 };
34
35 Vector2();
36 Vector2(T x, T y);
37 explicit Vector2(const T* v);
38 virtual ~Vector2();
39
40 Vector2 Normalized() const;
41 T Dot(const Vector2<T>& other) const;
42 T Cross(const Vector2<T>& other) const;
43 Vector2 operator-() const;
44 Vector2 operator-(const Vector2<T>& other) const;
45 Vector2 operator+(const Vector2<T>& other) const;
46 Vector2 operator/(T scale) const;
47 Vector2 operator*(T scale) const;
48 Vector2 operator*(const Vector2<T>& other) const;
49 Vector2& operator*=(const Vector2<T>& other);
50 Vector2& operator+=(const Vector2<T>& other);
51 Vector2& operator-=(const Vector2<T>& other);
52 Vector2& operator=(const Vector2& other);
53 T operator[](int index) const;
54 T& operator[](int index);
55 bool operator==(const Vector2& other) const;
56 bool operator!=(const Vector2& other) const;
57 bool IsNearEqual(const Vector2& other, T threshold = std::numeric_limits<T>::epsilon()) const;
58
59 T* GetData();
60
61 T GetLength() const;
62 T GetSqrLength() const;
63 T Normalize();
64 bool IsInfinite() const;
65 bool IsNaN() const;
66 };
67
68 typedef Vector2<int> UIPoint;
69 typedef Vector2<float> Vector2f;
70 typedef Vector2<double> Vector2d;
71 template<typename T>
Vector2()72 Vector2<T>::Vector2()
73 {}
74
75 template<typename T>
Vector2(T x,T y)76 Vector2<T>::Vector2(T x, T y)
77 {
78 data_[0] = x;
79 data_[1] = y;
80 }
81
82 template<typename T>
Vector2(const T * v)83 Vector2<T>::Vector2(const T* v)
84 {
85 data_[0] = v[0];
86 data_[1] = v[1];
87 }
88
89 template<typename T>
~Vector2()90 Vector2<T>::~Vector2()
91 {}
92
93 template<typename T>
Normalized()94 Vector2<T> Vector2<T>::Normalized() const
95 {
96 Vector2<T> rNormalize(*this);
97 rNormalize.Normalize();
98 return rNormalize;
99 }
100
101 template<typename T>
Dot(const Vector2<T> & other)102 T Vector2<T>::Dot(const Vector2<T>& other) const
103 {
104 const T* oData = other.data_;
105 T sum = data_[0] * oData[0];
106 sum += data_[1] * oData[1];
107 return sum;
108 }
109
110 template<typename T>
Cross(const Vector2<T> & other)111 T Vector2<T>::Cross(const Vector2<T>& other) const
112 {
113 const T* oData = other.data_;
114
115 return data_[0] * oData[1] - data_[1] * oData[0];
116 }
117
118 template<typename T>
119 Vector2<T> Vector2<T>::operator-() const
120 {
121 Vector2<T> rNeg;
122 T* rData = rNeg.data_;
123 rData[0] = -data_[0];
124 rData[1] = -data_[1];
125 return rNeg;
126 }
127
128 template<typename T>
129 Vector2<T> Vector2<T>::operator-(const Vector2<T>& other) const
130 {
131 Vector2<T> rSub(*this);
132 T* rData = rSub.data_;
133 const T* oData = other.data_;
134 rData[0] -= oData[0];
135 rData[1] -= oData[1];
136 return rSub;
137 }
138
139 template<typename T>
140 Vector2<T> Vector2<T>::operator+(const Vector2<T>& other) const
141 {
142 Vector2<T> rAdd(*this);
143 return rAdd += other;
144 }
145
146 template<typename T>
147 Vector2<T> Vector2<T>::operator/(T scale) const
148 {
149 if (ROSEN_EQ(scale, 0)) {
150 return *this;
151 }
152 const T invScale = 1.0f / scale;
153 return (*this) * invScale;
154 }
155
156 template<typename T>
157 Vector2<T> Vector2<T>::operator*(T scale) const
158 {
159 Vector2<T> rMult(*this);
160 T* rData = rMult.data_;
161
162 rData[0] *= scale;
163 rData[1] *= scale;
164 return rMult;
165 }
166
167 template<typename T>
168 Vector2<T> Vector2<T>::operator*(const Vector2<T>& other) const
169 {
170 Vector2<T> rMult(*this);
171 return rMult *= other;
172 }
173
174 template<typename T>
175 Vector2<T>& Vector2<T>::operator*=(const Vector2<T>& other)
176 {
177 const T* oData = other.data_;
178 data_[0] *= oData[0];
179 data_[1] *= oData[1];
180 return *this;
181 }
182
183 template<typename T>
184 Vector2<T>& Vector2<T>::operator+=(const Vector2<T>& other)
185 {
186 data_[0] += other.data_[0];
187 data_[1] += other.data_[1];
188 return *this;
189 }
190
191 template<typename T>
192 Vector2<T>& Vector2<T>::operator-=(const Vector2<T>& other)
193 {
194 data_[0] -= other.data_[0];
195 data_[1] -= other.data_[1];
196 return *this;
197 }
198
199 template<typename T>
200 Vector2<T>& Vector2<T>::operator=(const Vector2<T>& other)
201 {
202 const T* oData = other.data_;
203 data_[0] = oData[0];
204 data_[1] = oData[1];
205 return *this;
206 }
207
208 template<typename T>
209 T Vector2<T>::operator[](int index) const
210 {
211 return data_[index];
212 }
213
214 template<typename T>
215 inline T& Vector2<T>::operator[](int index)
216 {
217 return data_[index];
218 }
219
220 template<typename T>
221 inline bool Vector2<T>::operator==(const Vector2& other) const
222 {
223 const T* oData = other.data_;
224
225 return (ROSEN_EQ<T>(data_[0], oData[0])) && (ROSEN_EQ<T>(data_[1], oData[1]));
226 }
227
228 template<typename T>
229 inline bool Vector2<T>::operator!=(const Vector2& other) const
230 {
231 const T* oData = other.data_;
232
233 return (!ROSEN_EQ<T>(data_[0], oData[0])) || (!ROSEN_EQ<T>(data_[1], oData[1]));
234 }
235
236 template<typename T>
IsNearEqual(const Vector2 & other,T threshold)237 bool Vector2<T>::IsNearEqual(const Vector2& other, T threshold) const
238 {
239 const T* otherData = other.data_;
240
241 return (ROSEN_EQ<T>(data_[0], otherData[0], threshold)) && (ROSEN_EQ<T>(data_[1], otherData[1], threshold));
242 }
243
244 template<typename T>
GetData()245 inline T* Vector2<T>::GetData()
246 {
247 return data_;
248 }
249
250 template<typename T>
GetLength()251 T Vector2<T>::GetLength() const
252 {
253 return sqrt(GetSqrLength());
254 }
255
256 template<typename T>
GetSqrLength()257 T Vector2<T>::GetSqrLength() const
258 {
259 T sum = data_[0] * data_[0];
260 sum += data_[1] * data_[1];
261 return sum;
262 }
263
264 template<typename T>
Normalize()265 T Vector2<T>::Normalize()
266 {
267 T l = GetLength();
268 if (ROSEN_EQ<T>(l, 0.0)) {
269 return 0.0f;
270 }
271
272 const T invLen = 1.0f / l;
273
274 data_[0] *= invLen;
275 data_[1] *= invLen;
276 return l;
277 }
278
279 template<typename T>
IsInfinite()280 bool Vector2<T>::IsInfinite() const
281 {
282 return std::isinf(data_[0]) || std::isinf(data_[1]);
283 }
284
285 template<typename T>
IsNaN()286 bool Vector2<T>::IsNaN() const
287 {
288 return IsNan(data_[0]) || IsNan(data_[1]);
289 }
290 } // namespace Rosen
291 } // namespace OHOS
292 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_VECTOR2_H
293