1 /*
2 * Copyright (c) 2021-2022 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);
49 Vector2& operator+=(const Vector2<T>& other);
50 Vector2& operator=(const Vector2& other);
51 T operator[](int index) const;
52 T& operator[](int index);
53 bool operator==(const Vector2& other) const;
54 bool operator!=(const Vector2& other) const;
55 T* GetData();
56
57 T GetLength() const;
58 T GetSqrLength() const;
59 T Normalize();
60 bool IsInfinite() const;
61 bool IsNaN() const;
62 };
63
64 typedef Vector2<int> UIPoint;
65 typedef Vector2<float> Vector2f;
66 typedef Vector2<double> Vector2d;
67 template<typename T>
Vector2()68 Vector2<T>::Vector2()
69 {}
70
71 template<typename T>
Vector2(T x,T y)72 Vector2<T>::Vector2(T x, T y)
73 {
74 data_[0] = x;
75 data_[1] = y;
76 }
77
78 template<typename T>
Vector2(const T * v)79 Vector2<T>::Vector2(const T* v)
80 {
81 data_[0] = v[0];
82 data_[1] = v[1];
83 }
84
85 template<typename T>
~Vector2()86 Vector2<T>::~Vector2()
87 {}
88
89 template<typename T>
Normalized()90 Vector2<T> Vector2<T>::Normalized() const
91 {
92 Vector2<T> rNormalize(*this);
93 rNormalize.Normalize();
94 return rNormalize;
95 }
96
97 template<typename T>
Dot(const Vector2<T> & other)98 T Vector2<T>::Dot(const Vector2<T>& other) const
99 {
100 const T* oData = other.data_;
101 T sum = data_[0] * oData[0];
102 sum += data_[1] * oData[1];
103 return sum;
104 }
105
106 template<typename T>
Cross(const Vector2<T> & other)107 T Vector2<T>::Cross(const Vector2<T>& other) const
108 {
109 const T* oData = other.data_;
110
111 return data_[0] * oData[1] - data_[1] * oData[0];
112 }
113
114 template<typename T>
115 Vector2<T> Vector2<T>::operator-() const
116 {
117 Vector2<T> rNeg;
118 T* rData = rNeg.data_;
119 rData[0] = -data_[0];
120 rData[1] = -data_[1];
121 return rNeg;
122 }
123
124 template<typename T>
125 Vector2<T> Vector2<T>::operator-(const Vector2<T>& other) const
126 {
127 Vector2<T> rSub(*this);
128 T* rData = rSub.data_;
129 const T* oData = other.data_;
130 rData[0] -= oData[0];
131 rData[1] -= oData[1];
132 return rSub;
133 }
134
135 template<typename T>
136 Vector2<T> Vector2<T>::operator+(const Vector2<T>& other) const
137 {
138 Vector2<T> rAdd(*this);
139 T* rData = rAdd.data_;
140 const T* oData = other.data_;
141 rData[0] += oData[0];
142 rData[1] += oData[1];
143 return rAdd;
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)
169 {
170 const T* oData = other.data_;
171 data_[0] *= oData[0];
172 data_[1] *= oData[1];
173 return *this;
174 }
175
176 template<typename T>
177 Vector2<T>& Vector2<T>::operator+=(const Vector2<T>& other)
178 {
179 data_[0] += other.data_[0];
180 data_[1] += other.data_[1];
181 return *this;
182 }
183
184 template<typename T>
185 Vector2<T>& Vector2<T>::operator=(const Vector2<T>& other)
186 {
187 const T* oData = other.data_;
188 data_[0] = oData[0];
189 data_[1] = oData[1];
190 return *this;
191 }
192
193 template<typename T>
194 T Vector2<T>::operator[](int index) const
195 {
196 return data_[index];
197 }
198
199 template<typename T>
200 inline T& Vector2<T>::operator[](int index)
201 {
202 return data_[index];
203 }
204
205 template<typename T>
206 inline bool Vector2<T>::operator==(const Vector2& other) const
207 {
208 const T* oData = other.data_;
209
210 return (ROSEN_EQ<T>(data_[0], oData[0])) && (ROSEN_EQ<T>(data_[1], oData[1]));
211 }
212
213 template<typename T>
214 inline bool Vector2<T>::operator!=(const Vector2& other) const
215 {
216 const T* oData = other.data_;
217
218 return (!ROSEN_EQ<T>(data_[0], oData[0])) || (!ROSEN_EQ<T>(data_[1], oData[1]));
219 }
220
221 template<typename T>
GetData()222 inline T* Vector2<T>::GetData()
223 {
224 return data_;
225 }
226
227 template<typename T>
GetLength()228 T Vector2<T>::GetLength() const
229 {
230 return sqrt(GetSqrLength());
231 }
232
233 template<typename T>
GetSqrLength()234 T Vector2<T>::GetSqrLength() const
235 {
236 T sum = data_[0] * data_[0];
237 sum += data_[1] * data_[1];
238 return sum;
239 }
240
241 template<typename T>
Normalize()242 T Vector2<T>::Normalize()
243 {
244 T l = GetLength();
245 if (ROSEN_EQ<T>(l, 0.0)) {
246 return 0.0f;
247 }
248
249 const T invLen = 1.0f / l;
250
251 data_[0] *= invLen;
252 data_[1] *= invLen;
253 return l;
254 }
255
256 template<typename T>
IsInfinite()257 bool Vector2<T>::IsInfinite() const
258 {
259 return std::isinf(data_[0]) || std::isinf(data_[1]);
260 }
261
262 template<typename T>
IsNaN()263 bool Vector2<T>::IsNaN() const
264 {
265 return IsNan(data_[0]) || IsNan(data_[1]);
266 }
267 } // namespace Rosen
268 } // namespace OHOS
269 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_VECTOR2_H
270