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