PxVec3.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29 
30 #ifndef PXFOUNDATION_PXVEC3_H
31 #define PXFOUNDATION_PXVEC3_H
32 
37 #include "foundation/PxMath.h"
38 
39 #if !PX_DOXYGEN
40 namespace physx
41 {
42 #endif
43 
49 class PxVec3
50 {
51  public:
56  {
57  }
58 
62  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(PxZERO r) : x(0.0f), y(0.0f), z(0.0f)
63  {
64  PX_UNUSED(r);
65  }
66 
74  explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float a) : x(a), y(a), z(a)
75  {
76  }
77 
85  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
86  {
87  }
88 
92  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(const PxVec3& v) : x(v.x), y(v.y), z(v.z)
93  {
94  }
95 
96  // Operators
97 
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 float& operator[](unsigned int index)
113  {
114  PX_SHARED_ASSERT(index <= 2);
115 
116  return reinterpret_cast<float*>(this)[index];
117  }
118 
122  PX_CUDA_CALLABLE PX_FORCE_INLINE const float& operator[](unsigned int index) const
123  {
124  PX_SHARED_ASSERT(index <= 2);
125 
126  return reinterpret_cast<const float*>(this)[index];
127  }
128 
133  {
134  return x == v.x && y == v.y && z == v.z;
135  }
136 
141  {
142  return x != v.x || y != v.y || z != v.z;
143  }
144 
149  {
150  return x == 0.0f && y == 0.0f && z == 0.0f;
151  }
152 
157  {
158  return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z);
159  }
160 
165  {
166  const float unitTolerance = 1e-4f;
167  return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
168  }
169 
176  {
177  return x * x + y * y + z * z;
178  }
179 
184  {
185  return PxSqrt(magnitudeSquared());
186  }
187 
192  {
193  return PxVec3(-x, -y, -z);
194  }
195 
200  {
201  return PxVec3(x + v.x, y + v.y, z + v.z);
202  }
203 
208  {
209  return PxVec3(x - v.x, y - v.y, z - v.z);
210  }
211 
216  {
217  return PxVec3(x * f, y * f, z * f);
218  }
219 
224  {
225  f = 1.0f / f;
226  return PxVec3(x * f, y * f, z * f);
227  }
228 
233  {
234  x += v.x;
235  y += v.y;
236  z += v.z;
237  return *this;
238  }
239 
244  {
245  x -= v.x;
246  y -= v.y;
247  z -= v.z;
248  return *this;
249  }
250 
255  {
256  x *= f;
257  y *= f;
258  z *= f;
259  return *this;
260  }
265  {
266  f = 1.0f / f;
267  x *= f;
268  y *= f;
269  z *= f;
270  return *this;
271  }
272 
277  {
278  return x * v.x + y * v.y + z * v.z;
279  }
280 
285  {
286  return PxVec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
287  }
288 
292  {
293  const float m = magnitudeSquared();
294  return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec3(0, 0, 0);
295  }
296 
301  {
302  const float m = magnitude();
303  if(m > 0.0f)
304  *this /= m;
305  return m;
306  }
307 
313  {
314  const float mag = magnitude();
315  if(mag < PX_NORMALIZATION_EPSILON)
316  return 0.0f;
317  *this *= 1.0f / mag;
318  return mag;
319  }
320 
326  {
327  const float mag = magnitude();
329  *this *= 1.0f / mag;
330  return mag;
331  }
332 
337  {
338  return PxVec3(x * a.x, y * a.y, z * a.z);
339  }
340 
345  {
346  return PxVec3(PxMin(x, v.x), PxMin(y, v.y), PxMin(z, v.z));
347  }
348 
353  {
354  return PxMin(x, PxMin(y, z));
355  }
356 
361  {
362  return PxVec3(PxMax(x, v.x), PxMax(y, v.y), PxMax(z, v.z));
363  }
364 
369  {
370  return PxMax(x, PxMax(y, z));
371  }
372 
377  {
378  return PxVec3(PxAbs(x), PxAbs(y), PxAbs(z));
379  }
380 
381  float x, y, z;
382 };
383 
385 {
386  return PxVec3(f * v.x, f * v.y, f * v.z);
387 }
388 
389 #if !PX_DOXYGEN
390 } // namespace physx
391 #endif
392 
394 #endif // #ifndef PXFOUNDATION_PXVEC3_H
Definition: GuContactBuffer.h:37
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeSafe()
normalizes the vector in place. Does nothing if vector magnitude is under PX_NORMALIZATION_EPSILON. Returns vector magnitude if >= PX_NORMALIZATION_EPSILON and 0.0f otherwise.
Definition: PxVec3.h:312
PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const
returns MAX(x, y, z);
Definition: PxVec3.h:368
PX_CUDA_CALLABLE PX_FORCE_INLINE const float & operator[](unsigned int index) const
element access
Definition: PxVec3.h:122
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:156
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 getNormalized() const
Definition: PxVec3.h:291
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator*(float f) const
scalar post-multiplication
Definition: PxVec3.h:215
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-(const PxVec3 &v) const
vector difference
Definition: PxVec3.h:207
PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3 &v) const
returns the scalar product of this and other.
Definition: PxVec3.h:276
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:351
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 minimum(const PxVec3 &v) const
element-wise minimum
Definition: PxVec3.h:344
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(const PxVec3 &v)
Copy ctor.
Definition: PxVec3.h:92
PxZERO
Definition: Px.h:76
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(PxZERO r)
zero constructor.
Definition: PxVec3.h:62
#define PX_SHARED_ASSERT(exp)
Definition: PxSharedAssert.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isFinite(float a)
platform-specific finiteness check (not INF or NAN)
Definition: PxUnixIntrinsics.h:127
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float nx, float ny, float nz)
Initializes from 3 scalar parameters.
Definition: PxVec3.h:85
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 & operator+=(const PxVec3 &v)
vector addition
Definition: PxVec3.h:232
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator+(const PxVec3 &v) const
vector addition
Definition: PxVec3.h:199
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition: PxMath.h:144
float y
Definition: PxVec3.h:381
PX_CUDA_CALLABLE PX_FORCE_INLINE float & operator[](unsigned int index)
element access
Definition: PxVec3.h:112
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:86
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
is normalized - used by API parameter validation
Definition: PxVec3.h:164
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator/(float f) const
scalar division
Definition: PxVec3.h:223
PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const
returns MIN(x, y, z);
Definition: PxVec3.h:352
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
tests for exact zero vector
Definition: PxVec3.h:148
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 & operator-=(const PxVec3 &v)
vector difference
Definition: PxVec3.h:243
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition: PxMath.h:156
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 & operator=(const PxVec3 &p)
Assignment operator.
Definition: PxVec3.h:101
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
returns the magnitude
Definition: PxVec3.h:183
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition: PxMath.h:107
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
normalizes the vector in place
Definition: PxVec3.h:300
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:70
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const
returns the squared magnitude
Definition: PxVec3.h:175
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 cross(const PxVec3 &v) const
cross product
Definition: PxVec3.h:284
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 & operator/=(float f)
scalar division
Definition: PxVec3.h:264
PX_CUDA_CALLABLE PX_INLINE void PX_UNUSED(T const &)
Definition: PxPreprocessor.h:466
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeFast()
normalizes the vector in place. Asserts if vector magnitude is under PX_NORMALIZATION_EPSILON. returns vector magnitude.
Definition: PxVec3.h:325
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 abs() const
returns absolute values of components;
Definition: PxVec3.h:376
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:292
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float a)
Assigns scalar parameter to all elements.
Definition: PxVec3.h:74
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec3 &v) const
returns true if the two vectors are exactly equal.
Definition: PxVec3.h:132
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 maximum(const PxVec3 &v) const
element-wise maximum
Definition: PxVec3.h:360
static PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator*(float f, const PxVec3 &v)
Definition: PxVec3.h:384
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec3 &v) const
returns true if the two vectors are not exactly equal.
Definition: PxVec3.h:140
#define PX_NORMALIZATION_EPSILON
Definition: PxSimpleTypes.h:95
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3()
default constructor leaves data uninitialized.
Definition: PxVec3.h:55
float z
Definition: PxVec3.h:381
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 & operator*=(float f)
scalar multiplication
Definition: PxVec3.h:254
#define PX_INLINE
Definition: PxPreprocessor.h:336
#define PX_CUDA_CALLABLE
Definition: PxPreprocessor.h:460
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 multiply(const PxVec3 &a) const
a[i] * b[i], for all i.
Definition: PxVec3.h:336
3 Element vector class.
Definition: PxVec3.h:49
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-() const
negation
Definition: PxVec3.h:191
float x
Definition: PxVec3.h:381