RavEngine
Loading...
Searching...
No Matches
PxVec2.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_VEC2_H
30#define PX_VEC2_H
31
36#include "foundation/PxMath.h"
37
38#if !PX_DOXYGEN
39namespace physx
40{
41#endif
42
48template<class Type>
50{
51 public:
55 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T()
56 {
57 }
58
62 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(PxZERO) : x(Type(0.0)), y(Type(0.0))
63 {
64 }
65
73 explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(Type a) : x(a), y(a)
74 {
75 }
76
83 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(Type nx, Type ny) : x(nx), y(ny)
84 {
85 }
86
90 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(const PxVec2T& v) : x(v.x), y(v.y)
91 {
92 }
93
94 // Operators
95
99 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T& operator=(const PxVec2T& p)
100 {
101 x = p.x;
102 y = p.y;
103 return *this;
104 }
105
109 PX_CUDA_CALLABLE PX_FORCE_INLINE Type& operator[](unsigned int index)
110 {
111 PX_ASSERT(index <= 1);
112 return reinterpret_cast<Type*>(this)[index];
113 }
114
118 PX_CUDA_CALLABLE PX_FORCE_INLINE const Type& operator[](unsigned int index) const
119 {
120 PX_ASSERT(index <= 1);
121 return reinterpret_cast<const Type*>(this)[index];
122 }
123
127 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec2T& v) const
128 {
129 return x == v.x && y == v.y;
130 }
131
135 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec2T& v) const
136 {
137 return x != v.x || y != v.y;
138 }
139
143 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
144 {
145 return x == Type(0.0) && y == Type(0.0);
146 }
147
151 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
152 {
153 return PxIsFinite(x) && PxIsFinite(y);
154 }
155
159 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
160 {
161 const Type unitTolerance = Type(1e-4);
162 return isFinite() && PxAbs(magnitude() - Type(1.0)) < unitTolerance;
163 }
164
170 PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitudeSquared() const
171 {
172 return x * x + y * y;
173 }
174
178 PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitude() const
179 {
180 return PxSqrt(magnitudeSquared());
181 }
182
186 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator-() const
187 {
188 return PxVec2T(-x, -y);
189 }
190
194 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator+(const PxVec2T& v) const
195 {
196 return PxVec2T(x + v.x, y + v.y);
197 }
198
202 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator-(const PxVec2T& v) const
203 {
204 return PxVec2T(x - v.x, y - v.y);
205 }
206
210 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator*(Type f) const
211 {
212 return PxVec2T(x * f, y * f);
213 }
214
218 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator/(Type f) const
219 {
220 f = Type(1.0) / f;
221 return PxVec2T(x * f, y * f);
222 }
223
227 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T& operator+=(const PxVec2T& v)
228 {
229 x += v.x;
230 y += v.y;
231 return *this;
232 }
233
237 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T& operator-=(const PxVec2T& v)
238 {
239 x -= v.x;
240 y -= v.y;
241 return *this;
242 }
243
247 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T& operator*=(Type f)
248 {
249 x *= f;
250 y *= f;
251 return *this;
252 }
253
257 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T& operator/=(Type f)
258 {
259 f = Type(1.0) / f;
260 x *= f;
261 y *= f;
262 return *this;
263 }
264
268 PX_CUDA_CALLABLE PX_FORCE_INLINE Type dot(const PxVec2T& v) const
269 {
270 return x * v.x + y * v.y;
271 }
272
274 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T getNormalized() const
275 {
276 const Type m = magnitudeSquared();
277 return m > Type(0.0) ? *this * PxRecipSqrt(m) : PxVec2T(Type(0));
278 }
279
283 PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalize()
284 {
285 const Type m = magnitude();
286 if(m > Type(0.0))
287 *this /= m;
288 return m;
289 }
290
294 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T multiply(const PxVec2T& a) const
295 {
296 return PxVec2T(x * a.x, y * a.y);
297 }
298
302 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T minimum(const PxVec2T& v) const
303 {
304 return PxVec2T(PxMin(x, v.x), PxMin(y, v.y));
305 }
306
310 PX_CUDA_CALLABLE PX_FORCE_INLINE Type minElement() const
311 {
312 return PxMin(x, y);
313 }
314
318 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T maximum(const PxVec2T& v) const
319 {
320 return PxVec2T(PxMax(x, v.x), PxMax(y, v.y));
321 }
322
326 PX_CUDA_CALLABLE PX_FORCE_INLINE Type maxElement() const
327 {
328 return PxMax(x, y);
329 }
330
331 Type x, y;
332};
333
334template<class Type>
335PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec2T<Type> operator*(Type f, const PxVec2T<Type>& v)
336{
337 return PxVec2T<Type>(f * v.x, f * v.y);
338}
339
340typedef PxVec2T<float> PxVec2;
341typedef PxVec2T<double> PxVec2d;
342
343#if !PX_DOXYGEN
344} // namespace physx
345#endif
346
348#endif
349
2 Element vector class.
Definition PxVec2.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitude() const
returns the magnitude
Definition PxVec2.h:178
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
is normalized - used by API parameter validation
Definition PxVec2.h:159
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T multiply(const PxVec2T &a) const
a[i] * b[i], for all i.
Definition PxVec2.h:294
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(const PxVec2T &v)
Copy ctor.
Definition PxVec2.h:90
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T getNormalized() const
Definition PxVec2.h:274
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator/(Type f) const
scalar division
Definition PxVec2.h:218
PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalize()
normalizes the vector in place
Definition PxVec2.h:283
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T & operator-=(const PxVec2T &v)
vector difference
Definition PxVec2.h:237
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(Type a)
Assigns scalar parameter to all elements.
Definition PxVec2.h:73
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator-(const PxVec2T &v) const
vector difference
Definition PxVec2.h:202
PX_CUDA_CALLABLE PX_FORCE_INLINE Type maxElement() const
returns MAX(x, y);
Definition PxVec2.h:326
PX_CUDA_CALLABLE PX_FORCE_INLINE Type & operator[](unsigned int index)
element access
Definition PxVec2.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T & operator=(const PxVec2T &p)
Assignment operator.
Definition PxVec2.h:99
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
tests for exact zero vector
Definition PxVec2.h:143
PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitudeSquared() const
returns the squared magnitude
Definition PxVec2.h:170
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T maximum(const PxVec2T &v) const
element-wise maximum
Definition PxVec2.h:318
PX_CUDA_CALLABLE PX_FORCE_INLINE Type dot(const PxVec2T &v) const
returns the scalar product of this and other.
Definition PxVec2.h:268
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec2T &v) const
returns true if the two vectors are exactly equal.
Definition PxVec2.h:127
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator+(const PxVec2T &v) const
vector addition
Definition PxVec2.h:194
PX_CUDA_CALLABLE PX_FORCE_INLINE const Type & operator[](unsigned int index) const
element access
Definition PxVec2.h:118
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator*(Type f) const
scalar post-multiplication
Definition PxVec2.h:210
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T & operator*=(Type f)
scalar multiplication
Definition PxVec2.h:247
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec2T &v) const
returns true if the two vectors are not exactly equal.
Definition PxVec2.h:135
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T & operator/=(Type f)
scalar division
Definition PxVec2.h:257
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T minimum(const PxVec2T &v) const
element-wise minimum
Definition PxVec2.h:302
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(PxZERO)
zero constructor.
Definition PxVec2.h:62
PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
returns true if all 2 elems of the vector are finite (not NAN or INF, etc.)
Definition PxVec2.h:151
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T operator-() const
negation
Definition PxVec2.h:186
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T & operator+=(const PxVec2T &v)
vector addition
Definition PxVec2.h:227
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T(Type nx, Type ny)
Initializes from 2 scalar parameters.
Definition PxVec2.h:83
PX_CUDA_CALLABLE PX_FORCE_INLINE Type minElement() const
returns MIN(x, y);
Definition PxVec2.h:310
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2T()
default constructor leaves data uninitialized.
Definition PxVec2.h:55
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition PxMath.h:109
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition PxMath.h:158
PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
returns true if the passed number is a finite floating point number as opposed to INF,...
Definition PxMath.h:326
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition PxMath.h:146
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition PxMath.h:72
PxZERO
Definition Px.h:93
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
The return value is the lesser of the two specified values.
Definition PxMath.h:88