RavEngine
Loading...
Searching...
No Matches
PxVec3.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_VEC3_H
30#define PX_VEC3_H
31
36#include "foundation/PxMath.h"
37
38#if !PX_DOXYGEN
39namespace physx
40{
41#endif
42
48template<class Type>
50{
51 public:
52
56 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T()
57 {
58 }
59
63 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(PxZERO) : x(Type(0.0)), y(Type(0.0)), z(Type(0.0))
64 {
65 }
66
74 explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(Type a) : x(a), y(a), z(a)
75 {
76 }
77
85 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(Type nx, Type ny, Type nz) : x(nx), y(ny), z(nz)
86 {
87 }
88
92 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(const PxVec3T& v) : x(v.x), y(v.y), z(v.z)
93 {
94 }
95
96 // Operators
97
101 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T& operator=(const PxVec3T& p)
102 {
103 x = p.x;
104 y = p.y;
105 z = p.z;
106 return *this;
107 }
108
112 PX_CUDA_CALLABLE PX_FORCE_INLINE Type& operator[](unsigned int index)
113 {
114 PX_ASSERT(index <= 2);
115 return reinterpret_cast<Type*>(this)[index];
116 }
117
121 PX_CUDA_CALLABLE PX_FORCE_INLINE const Type& operator[](unsigned int index) const
122 {
123 PX_ASSERT(index <= 2);
124 return reinterpret_cast<const Type*>(this)[index];
125 }
126
130 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec3T& v) const
131 {
132 return x == v.x && y == v.y && z == v.z;
133 }
134
138 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec3T& v) const
139 {
140 return x != v.x || y != v.y || z != v.z;
141 }
142
146 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
147 {
148 return x == Type(0.0) && y == Type(0.0) && z == Type(0.0);
149 }
150
154 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
155 {
156 return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z);
157 }
158
162 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
163 {
164 const float unitTolerance = Type(1e-4); // PT: do we need a different epsilon for float & double?
165 return isFinite() && PxAbs(magnitude() - Type(1.0)) < unitTolerance;
166 }
167
173 PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitudeSquared() const
174 {
175 return x * x + y * y + z * z;
176 }
177
181 PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitude() const
182 {
183 return PxSqrt(magnitudeSquared());
184 }
185
189 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator-() const
190 {
191 return PxVec3T(-x, -y, -z);
192 }
193
197 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator+(const PxVec3T& v) const
198 {
199 return PxVec3T(x + v.x, y + v.y, z + v.z);
200 }
201
205 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator-(const PxVec3T& v) const
206 {
207 return PxVec3T(x - v.x, y - v.y, z - v.z);
208 }
209
213 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator*(Type f) const
214 {
215 return PxVec3T(x * f, y * f, z * f);
216 }
217
221 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator/(Type f) const
222 {
223 f = Type(1.0) / f;
224 return PxVec3T(x * f, y * f, z * f);
225 }
226
230 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T& operator+=(const PxVec3T& v)
231 {
232 x += v.x;
233 y += v.y;
234 z += v.z;
235 return *this;
236 }
237
241 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T& operator-=(const PxVec3T& v)
242 {
243 x -= v.x;
244 y -= v.y;
245 z -= v.z;
246 return *this;
247 }
248
252 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T& operator*=(Type f)
253 {
254 x *= f;
255 y *= f;
256 z *= f;
257 return *this;
258 }
262 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T& operator/=(Type f)
263 {
264 f = Type(1.0) / f;
265 x *= f;
266 y *= f;
267 z *= f;
268 return *this;
269 }
270
274 PX_CUDA_CALLABLE PX_FORCE_INLINE Type dot(const PxVec3T& v) const
275 {
276 return x * v.x + y * v.y + z * v.z;
277 }
278
282 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T cross(const PxVec3T& v) const
283 {
284 return PxVec3T(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
285 }
286
288 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T getNormalized() const
289 {
290 const Type m = magnitudeSquared();
291 return m > Type(0.0) ? *this * PxRecipSqrt(m) : PxVec3T(Type(0));
292 }
293
297 PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalize()
298 {
299 const Type m = magnitude();
300 if(m > Type(0.0))
301 *this /= m;
302 return m;
303 }
304
309 PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalizeSafe()
310 {
311 const Type mag = magnitude();
312 if(mag < PX_NORMALIZATION_EPSILON) // PT: do we need a different epsilon for float & double?
313 return Type(0.0);
314 *this *= Type(1.0) / mag;
315 return mag;
316 }
317
322 PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalizeFast()
323 {
324 const Type mag = magnitude();
325 PX_ASSERT(mag >= PX_NORMALIZATION_EPSILON); // PT: do we need a different epsilon for float & double?
326 *this *= Type(1.0) / mag;
327 return mag;
328 }
329
333 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T multiply(const PxVec3T& a) const
334 {
335 return PxVec3T(x * a.x, y * a.y, z * a.z);
336 }
337
341 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T minimum(const PxVec3T& v) const
342 {
343 return PxVec3T(PxMin(x, v.x), PxMin(y, v.y), PxMin(z, v.z));
344 }
345
349 PX_CUDA_CALLABLE PX_FORCE_INLINE Type minElement() const
350 {
351 return PxMin(x, PxMin(y, z));
352 }
353
357 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T maximum(const PxVec3T& v) const
358 {
359 return PxVec3T(PxMax(x, v.x), PxMax(y, v.y), PxMax(z, v.z));
360 }
361
365 PX_CUDA_CALLABLE PX_FORCE_INLINE Type maxElement() const
366 {
367 return PxMax(x, PxMax(y, z));
368 }
369
373 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T abs() const
374 {
375 return PxVec3T(PxAbs(x), PxAbs(y), PxAbs(z));
376 }
377
378 Type x, y, z;
379};
380
381template<class Type>
382PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec3T<Type> operator*(Type f, const PxVec3T<Type>& v)
383{
384 return PxVec3T<Type>(f * v.x, f * v.y, f * v.z);
385}
386
387typedef PxVec3T<float> PxVec3;
388typedef PxVec3T<double> PxVec3d;
389
391class PxVec3Padded : public PxVec3
392{
393 public:
396 PX_FORCE_INLINE PxVec3Padded(const PxVec3& p) : PxVec3(p) {}
397 PX_FORCE_INLINE PxVec3Padded(float f) : PxVec3(f) {}
398
405 {
406 x = p.x;
407 y = p.y;
408 z = p.z;
409 return *this;
410 }
411
412 PxU32 padding;
413};
414PX_COMPILE_TIME_ASSERT(sizeof(PxVec3Padded) == 16);
415
416typedef PxVec3Padded PxVec3p;
417
418#if !PX_DOXYGEN
419} // namespace physx
420#endif
421
423#endif
A padded version of PxVec3, to safely load its data using SIMD.
Definition PxVec3.h:392
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3Padded & operator=(const PxVec3Padded &p)
Assignment operator. To fix this: error: definition of implicit copy assignment operator for 'PxVec3P...
Definition PxVec3.h:404
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitude() const
returns the magnitude
Definition PxVec3.h:181
PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalizeSafe()
normalizes the vector in place. Does nothing if vector magnitude is under PX_NORMALIZATION_EPSILON....
Definition PxVec3.h:309
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T & operator+=(const PxVec3T &v)
vector addition
Definition PxVec3.h:230
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T minimum(const PxVec3T &v) const
element-wise minimum
Definition PxVec3.h:341
PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalizeFast()
normalizes the vector in place. Asserts if vector magnitude is under PX_NORMALIZATION_EPSILON....
Definition PxVec3.h:322
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T maximum(const PxVec3T &v) const
element-wise maximum
Definition PxVec3.h:357
PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
returns true if all 3 elems of the vector are finite (not NAN or INF, etc.)
Definition PxVec3.h:154
PX_CUDA_CALLABLE PX_FORCE_INLINE Type dot(const PxVec3T &v) const
returns the scalar product of this and other.
Definition PxVec3.h:274
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T & operator/=(Type f)
scalar division
Definition PxVec3.h:262
PX_CUDA_CALLABLE PX_FORCE_INLINE const Type & operator[](unsigned int index) const
element access
Definition PxVec3.h:121
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec3T &v) const
returns true if the two vectors are not exactly equal.
Definition PxVec3.h:138
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(PxZERO)
zero constructor.
Definition PxVec3.h:63
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T getNormalized() const
Definition PxVec3.h:288
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T multiply(const PxVec3T &a) const
a[i] * b[i], for all i.
Definition PxVec3.h:333
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator+(const PxVec3T &v) const
vector addition
Definition PxVec3.h:197
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator*(Type f) const
scalar post-multiplication
Definition PxVec3.h:213
PX_CUDA_CALLABLE PX_FORCE_INLINE Type & operator[](unsigned int index)
element access
Definition PxVec3.h:112
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(Type a)
Assigns scalar parameter to all elements.
Definition PxVec3.h:74
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T()
default constructor leaves data uninitialized.
Definition PxVec3.h:56
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
tests for exact zero vector
Definition PxVec3.h:146
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator/(Type f) const
scalar division
Definition PxVec3.h:221
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec3T &v) const
returns true if the two vectors are exactly equal.
Definition PxVec3.h:130
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
is normalized - used by API parameter validation
Definition PxVec3.h:162
PX_CUDA_CALLABLE PX_FORCE_INLINE Type minElement() const
returns MIN(x, y, z);
Definition PxVec3.h:349
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator-(const PxVec3T &v) const
vector difference
Definition PxVec3.h:205
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T & operator=(const PxVec3T &p)
Assignment operator.
Definition PxVec3.h:101
PX_CUDA_CALLABLE PX_FORCE_INLINE Type magnitudeSquared() const
returns the squared magnitude
Definition PxVec3.h:173
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(Type nx, Type ny, Type nz)
Initializes from 3 scalar parameters.
Definition PxVec3.h:85
PX_CUDA_CALLABLE PX_FORCE_INLINE Type maxElement() const
returns MAX(x, y, z);
Definition PxVec3.h:365
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T abs() const
returns absolute values of components;
Definition PxVec3.h:373
PX_CUDA_CALLABLE PX_FORCE_INLINE Type normalize()
normalizes the vector in place
Definition PxVec3.h:297
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T cross(const PxVec3T &v) const
cross product
Definition PxVec3.h:282
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T(const PxVec3T &v)
Copy ctor.
Definition PxVec3.h:92
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T operator-() const
negation
Definition PxVec3.h:189
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T & operator-=(const PxVec3T &v)
vector difference
Definition PxVec3.h:241
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3T & operator*=(Type f)
scalar multiplication
Definition PxVec3.h:252
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3()
default constructor leaves data uninitialized.
Definition PxVec3.h:55
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_COMPILE_TIME_ASSERT(exp)
Definition PxPreprocessor.h:428
#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