• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static constexpr uint32_t V2SIZE = 2;
28     static constexpr size_t DATA_SIZE = sizeof(T) * V2SIZE;
29     union {
30         struct {
31             T x_;
32             T y_;
33         };
34         T data_[2];
35     };
36 
37     Vector2();
38     Vector2(T x, T y);
39     explicit Vector2(const T* v);
40     virtual ~Vector2();
41 
42     Vector2 Normalized() const;
43     T Dot(const Vector2<T>& other) const;
44     T Cross(const Vector2<T>& other) const;
45     Vector2 operator-() const;
46     Vector2 operator-(const Vector2<T>& other) const;
47     Vector2 operator+(const Vector2<T>& other) const;
48     Vector2 operator/(T scale) const;
49     Vector2 operator*(T scale) const;
50     Vector2 operator*(const Vector2<T>& other) const;
51     Vector2& operator*=(const Vector2<T>& other);
52     Vector2& operator+=(const Vector2<T>& other);
53     Vector2& operator-=(const Vector2<T>& other);
54     Vector2& operator=(const Vector2& other);
55     T operator[](int index) const;
56     T& operator[](int index);
57     bool operator==(const Vector2& other) const;
58     bool operator!=(const Vector2& other) const;
59     bool IsNearEqual(const Vector2& other, T threshold = std::numeric_limits<T>::epsilon()) const;
60 
61     T* GetData();
62 
63     T GetLength() const;
64     T GetSqrLength() const;
65     T Normalize();
66     bool IsInfinite() const;
67     bool IsNaN() const;
68     bool IsValid() const;
69 };
70 
71 typedef Vector2<int> UIPoint;
72 typedef Vector2<float> Vector2f;
73 typedef Vector2<double> Vector2d;
74 template<typename T>
Vector2()75 Vector2<T>::Vector2()
76 {
77     data_[0] = 0;
78     data_[1] = 0;
79 }
80 
81 template<typename T>
Vector2(T x,T y)82 Vector2<T>::Vector2(T x, T y)
83 {
84     data_[0] = x;
85     data_[1] = y;
86 }
87 
88 template<typename T>
Vector2(const T * v)89 Vector2<T>::Vector2(const T* v)
90 {
91     data_[0] = v[0];
92     data_[1] = v[1];
93 }
94 
95 template<typename T>
~Vector2()96 Vector2<T>::~Vector2()
97 {}
98 
99 template<typename T>
Normalized()100 Vector2<T> Vector2<T>::Normalized() const
101 {
102     Vector2<T> rNormalize(*this);
103     rNormalize.Normalize();
104     return rNormalize;
105 }
106 
107 template<typename T>
Dot(const Vector2<T> & other)108 T Vector2<T>::Dot(const Vector2<T>& other) const
109 {
110     const T* oData = other.data_;
111     T sum = data_[0] * oData[0];
112     sum += data_[1] * oData[1];
113     return sum;
114 }
115 
116 template<typename T>
Cross(const Vector2<T> & other)117 T Vector2<T>::Cross(const Vector2<T>& other) const
118 {
119     const T* oData = other.data_;
120 
121     return data_[0] * oData[1] - data_[1] * oData[0];
122 }
123 
124 template<typename T>
125 Vector2<T> Vector2<T>::operator-() const
126 {
127     Vector2<T> rNeg;
128     T* rData = rNeg.data_;
129     rData[0] = -data_[0];
130     rData[1] = -data_[1];
131     return rNeg;
132 }
133 
134 template<typename T>
135 Vector2<T> Vector2<T>::operator-(const Vector2<T>& other) const
136 {
137     Vector2<T> rSub(*this);
138     T* rData = rSub.data_;
139     const T* oData = other.data_;
140     rData[0] -= oData[0];
141     rData[1] -= oData[1];
142     return rSub;
143 }
144 
145 template<typename T>
146 Vector2<T> Vector2<T>::operator+(const Vector2<T>& other) const
147 {
148     Vector2<T> rAdd(*this);
149     return rAdd += other;
150 }
151 
152 template<typename T>
153 Vector2<T> Vector2<T>::operator/(T scale) const
154 {
155     if (ROSEN_EQ<T>(scale, 0)) {
156         return *this;
157     }
158     const T invScale = 1.0f / scale;
159     return (*this) * invScale;
160 }
161 
162 template<typename T>
163 Vector2<T> Vector2<T>::operator*(T scale) const
164 {
165     Vector2<T> rMult(*this);
166     T* rData = rMult.data_;
167 
168     rData[0] *= scale;
169     rData[1] *= scale;
170     return rMult;
171 }
172 
173 template<typename T>
174 Vector2<T> Vector2<T>::operator*(const Vector2<T>& other) const
175 {
176     Vector2<T> rMult(*this);
177     return rMult *= other;
178 }
179 
180 template<typename T>
181 Vector2<T>& Vector2<T>::operator*=(const Vector2<T>& other)
182 {
183     const T* oData = other.data_;
184     data_[0] *= oData[0];
185     data_[1] *= oData[1];
186     return *this;
187 }
188 
189 template<typename T>
190 Vector2<T>& Vector2<T>::operator+=(const Vector2<T>& other)
191 {
192     data_[0] += other.data_[0];
193     data_[1] += other.data_[1];
194     return *this;
195 }
196 
197 template<typename T>
198 Vector2<T>& Vector2<T>::operator-=(const Vector2<T>& other)
199 {
200     data_[0] -= other.data_[0];
201     data_[1] -= other.data_[1];
202     return *this;
203 }
204 
205 template<typename T>
206 Vector2<T>& Vector2<T>::operator=(const Vector2<T>& other)
207 {
208     const T* oData = other.data_;
209     data_[0] = oData[0];
210     data_[1] = oData[1];
211     return *this;
212 }
213 
214 template<typename T>
215 T Vector2<T>::operator[](int index) const
216 {
217     return data_[index];
218 }
219 
220 template<typename T>
221 inline T& Vector2<T>::operator[](int index)
222 {
223     return data_[index];
224 }
225 
226 template<typename T>
227 inline bool Vector2<T>::operator==(const Vector2& other) const
228 {
229     const T* oData = other.data_;
230 
231     return (ROSEN_EQ<T>(data_[0], oData[0])) && (ROSEN_EQ<T>(data_[1], oData[1]));
232 }
233 
234 template<typename T>
235 inline bool Vector2<T>::operator!=(const Vector2& other) const
236 {
237     const T* oData = other.data_;
238 
239     return (!ROSEN_EQ<T>(data_[0], oData[0])) || (!ROSEN_EQ<T>(data_[1], oData[1]));
240 }
241 
242 template<typename T>
IsNearEqual(const Vector2 & other,T threshold)243 bool Vector2<T>::IsNearEqual(const Vector2& other, T threshold) const
244 {
245     const T* otherData = other.data_;
246 
247     return (ROSEN_EQ<T>(data_[0], otherData[0], threshold)) && (ROSEN_EQ<T>(data_[1], otherData[1], threshold));
248 }
249 
250 template<typename T>
GetData()251 inline T* Vector2<T>::GetData()
252 {
253     return data_;
254 }
255 
256 template<typename T>
GetLength()257 T Vector2<T>::GetLength() const
258 {
259     return sqrt(GetSqrLength());
260 }
261 
262 template<typename T>
GetSqrLength()263 T Vector2<T>::GetSqrLength() const
264 {
265     T sum = data_[0] * data_[0];
266     sum += data_[1] * data_[1];
267     return sum;
268 }
269 
270 template<typename T>
Normalize()271 T Vector2<T>::Normalize()
272 {
273     T l = GetLength();
274     if (ROSEN_EQ<T>(l, 0.0)) {
275         return 0.0f;
276     }
277 
278     const T invLen = 1.0f / l;
279 
280     data_[0] *= invLen;
281     data_[1] *= invLen;
282     return l;
283 }
284 
285 template<typename T>
IsInfinite()286 bool Vector2<T>::IsInfinite() const
287 {
288     return std::isinf(data_[0]) || std::isinf(data_[1]);
289 }
290 
291 template<typename T>
IsNaN()292 bool Vector2<T>::IsNaN() const
293 {
294     return std::isnan(data_[0]) || std::isnan(data_[1]);
295 }
296 
297 template<typename T>
IsValid()298 bool Vector2<T>::IsValid() const
299 {
300     return !IsInfinite() && !IsNaN();
301 }
302 
303 } // namespace Rosen
304 } // namespace OHOS
305 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_VECTOR2_H
306