RavEngine
Loading...
Searching...
No Matches
PxVec4.h
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_PXVEC4_H
31#define PXFOUNDATION_PXVEC4_H
35#include "foundation/PxMath.h"
36#include "foundation/PxVec3.h"
37#include "foundation/PxSharedAssert.h"
38
44#if !PX_DOXYGEN
45namespace physx
46{
47#endif
48
49class PxVec4
50{
51 public:
55 PX_CUDA_CALLABLE PX_INLINE PxVec4()
56 {
57 }
58
62 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec4(PxZERO r) : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
63 {
64 PX_UNUSED(r);
65 }
66
74 explicit PX_CUDA_CALLABLE PX_INLINE PxVec4(float a) : x(a), y(a), z(a), w(a)
75 {
76 }
77
86 PX_CUDA_CALLABLE PX_INLINE PxVec4(float nx, float ny, float nz, float nw) : x(nx), y(ny), z(nz), w(nw)
87 {
88 }
89
96 PX_CUDA_CALLABLE PX_INLINE PxVec4(const PxVec3& v, float nw) : x(v.x), y(v.y), z(v.z), w(nw)
97 {
98 }
99
105 explicit PX_CUDA_CALLABLE PX_INLINE PxVec4(const float v[]) : x(v[0]), y(v[1]), z(v[2]), w(v[3])
106 {
107 }
108
112 PX_CUDA_CALLABLE PX_INLINE PxVec4(const PxVec4& v) : x(v.x), y(v.y), z(v.z), w(v.w)
113 {
114 }
115
116 // Operators
117
121 PX_CUDA_CALLABLE PX_INLINE PxVec4& operator=(const PxVec4& p)
122 {
123 x = p.x;
124 y = p.y;
125 z = p.z;
126 w = p.w;
127 return *this;
128 }
129
133 PX_CUDA_CALLABLE PX_INLINE float& operator[](unsigned int index)
134 {
135 PX_SHARED_ASSERT(index <= 3);
136
137 return reinterpret_cast<float*>(this)[index];
138 }
139
143 PX_CUDA_CALLABLE PX_INLINE const float& operator[](unsigned int index) const
144 {
145 PX_SHARED_ASSERT(index <= 3);
146
147 return reinterpret_cast<const float*>(this)[index];
148 }
149
153 PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxVec4& v) const
154 {
155 return x == v.x && y == v.y && z == v.z && w == v.w;
156 }
157
161 PX_CUDA_CALLABLE PX_INLINE bool operator!=(const PxVec4& v) const
162 {
163 return x != v.x || y != v.y || z != v.z || w != v.w;
164 }
165
169 PX_CUDA_CALLABLE PX_INLINE bool isZero() const
170 {
171 return x == 0 && y == 0 && z == 0 && w == 0;
172 }
173
177 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
178 {
179 return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z) && PxIsFinite(w);
180 }
181
185 PX_CUDA_CALLABLE PX_INLINE bool isNormalized() const
186 {
187 const float unitTolerance = 1e-4f;
188 return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
189 }
190
196 PX_CUDA_CALLABLE PX_INLINE float magnitudeSquared() const
197 {
198 return x * x + y * y + z * z + w * w;
199 }
200
204 PX_CUDA_CALLABLE PX_INLINE float magnitude() const
205 {
206 return PxSqrt(magnitudeSquared());
207 }
208
212 PX_CUDA_CALLABLE PX_INLINE PxVec4 operator-() const
213 {
214 return PxVec4(-x, -y, -z, -w);
215 }
216
220 PX_CUDA_CALLABLE PX_INLINE PxVec4 operator+(const PxVec4& v) const
221 {
222 return PxVec4(x + v.x, y + v.y, z + v.z, w + v.w);
223 }
224
228 PX_CUDA_CALLABLE PX_INLINE PxVec4 operator-(const PxVec4& v) const
229 {
230 return PxVec4(x - v.x, y - v.y, z - v.z, w - v.w);
231 }
232
237 PX_CUDA_CALLABLE PX_INLINE PxVec4 operator*(float f) const
238 {
239 return PxVec4(x * f, y * f, z * f, w * f);
240 }
241
245 PX_CUDA_CALLABLE PX_INLINE PxVec4 operator/(float f) const
246 {
247 f = 1.0f / f;
248 return PxVec4(x * f, y * f, z * f, w * f);
249 }
250
254 PX_CUDA_CALLABLE PX_INLINE PxVec4& operator+=(const PxVec4& v)
255 {
256 x += v.x;
257 y += v.y;
258 z += v.z;
259 w += v.w;
260 return *this;
261 }
262
266 PX_CUDA_CALLABLE PX_INLINE PxVec4& operator-=(const PxVec4& v)
267 {
268 x -= v.x;
269 y -= v.y;
270 z -= v.z;
271 w -= v.w;
272 return *this;
273 }
274
278 PX_CUDA_CALLABLE PX_INLINE PxVec4& operator*=(float f)
279 {
280 x *= f;
281 y *= f;
282 z *= f;
283 w *= f;
284 return *this;
285 }
289 PX_CUDA_CALLABLE PX_INLINE PxVec4& operator/=(float f)
290 {
291 f = 1.0f / f;
292 x *= f;
293 y *= f;
294 z *= f;
295 w *= f;
296 return *this;
297 }
298
302 PX_CUDA_CALLABLE PX_INLINE float dot(const PxVec4& v) const
303 {
304 return x * v.x + y * v.y + z * v.z + w * v.w;
305 }
306
309 PX_CUDA_CALLABLE PX_INLINE PxVec4 getNormalized() const
310 {
311 float m = magnitudeSquared();
312 return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec4(0, 0, 0, 0);
313 }
314
318 PX_CUDA_CALLABLE PX_INLINE float normalize()
319 {
320 float m = magnitude();
321 if(m > 0.0f)
322 *this /= m;
323 return m;
324 }
325
329 PX_CUDA_CALLABLE PX_INLINE PxVec4 multiply(const PxVec4& a) const
330 {
331 return PxVec4(x * a.x, y * a.y, z * a.z, w * a.w);
332 }
333
337 PX_CUDA_CALLABLE PX_INLINE PxVec4 minimum(const PxVec4& v) const
338 {
339 return PxVec4(PxMin(x, v.x), PxMin(y, v.y), PxMin(z, v.z), PxMin(w, v.w));
340 }
341
345 PX_CUDA_CALLABLE PX_INLINE PxVec4 maximum(const PxVec4& v) const
346 {
347 return PxVec4(PxMax(x, v.x), PxMax(y, v.y), PxMax(z, v.z), PxMax(w, v.w));
348 }
349
350 PX_CUDA_CALLABLE PX_INLINE PxVec3 getXYZ() const
351 {
352 return PxVec3(x, y, z);
353 }
354
358 PX_CUDA_CALLABLE PX_INLINE void setZero()
359 {
360 x = y = z = w = 0.0f;
361 }
362
363 float x, y, z, w;
364};
365
366PX_CUDA_CALLABLE static PX_INLINE PxVec4 operator*(float f, const PxVec4& v)
367{
368 return PxVec4(f * v.x, f * v.y, f * v.z, f * v.w);
369}
370
371#if !PX_DOXYGEN
372} // namespace physx
373#endif
374
376#endif // #ifndef PXFOUNDATION_PXVEC4_H
3 Element vector class.
Definition PxVec3.h:50
Definition PxVec4.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec4(PxZERO r)
zero constructor.
Definition PxVec4.h:62
PX_CUDA_CALLABLE PX_INLINE PxVec4 & operator/=(float f)
scalar division
Definition PxVec4.h:289
PX_CUDA_CALLABLE PX_INLINE PxVec4 & operator+=(const PxVec4 &v)
vector addition
Definition PxVec4.h:254
PX_CUDA_CALLABLE PX_INLINE PxVec4 operator+(const PxVec4 &v) const
vector addition
Definition PxVec4.h:220
PX_CUDA_CALLABLE PX_INLINE PxVec4 operator-(const PxVec4 &v) const
vector difference
Definition PxVec4.h:228
PX_CUDA_CALLABLE PX_INLINE PxVec4(const PxVec4 &v)
Copy ctor.
Definition PxVec4.h:112
PX_CUDA_CALLABLE PX_INLINE float magnitudeSquared() const
returns the squared magnitude
Definition PxVec4.h:196
PX_CUDA_CALLABLE PX_INLINE bool operator!=(const PxVec4 &v) const
returns true if the two vectors are not exactly equal.
Definition PxVec4.h:161
PX_CUDA_CALLABLE PX_INLINE bool isNormalized() const
is normalized - used by API parameter validation
Definition PxVec4.h:185
PX_CUDA_CALLABLE PX_INLINE float magnitude() const
returns the magnitude
Definition PxVec4.h:204
PX_CUDA_CALLABLE PX_INLINE PxVec4 & operator=(const PxVec4 &p)
Assignment operator.
Definition PxVec4.h:121
PX_CUDA_CALLABLE PX_INLINE PxVec4(const PxVec3 &v, float nw)
Initializes from 3 scalar parameters.
Definition PxVec4.h:96
PX_CUDA_CALLABLE PX_INLINE PxVec4 maximum(const PxVec4 &v) const
element-wise maximum
Definition PxVec4.h:345
PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxVec4 &v) const
returns true if the two vectors are exactly equal.
Definition PxVec4.h:153
PX_CUDA_CALLABLE PX_INLINE PxVec4 & operator-=(const PxVec4 &v)
vector difference
Definition PxVec4.h:266
PX_CUDA_CALLABLE PX_INLINE PxVec4 minimum(const PxVec4 &v) const
element-wise minimum
Definition PxVec4.h:337
PX_CUDA_CALLABLE PX_INLINE bool isZero() const
tests for exact zero vector
Definition PxVec4.h:169
PX_CUDA_CALLABLE PX_INLINE float & operator[](unsigned int index)
element access
Definition PxVec4.h:133
PX_CUDA_CALLABLE PX_INLINE float dot(const PxVec4 &v) const
returns the scalar product of this and other.
Definition PxVec4.h:302
PX_CUDA_CALLABLE PX_INLINE PxVec4 & operator*=(float f)
scalar multiplication
Definition PxVec4.h:278
PX_CUDA_CALLABLE PX_INLINE PxVec4(const float v[])
Initializes from an array of scalar parameters.
Definition PxVec4.h:105
PX_CUDA_CALLABLE PX_INLINE PxVec4()
default constructor leaves data uninitialized.
Definition PxVec4.h:55
PX_CUDA_CALLABLE PX_INLINE PxVec4(float nx, float ny, float nz, float nw)
Initializes from 3 scalar parameters.
Definition PxVec4.h:86
PX_CUDA_CALLABLE PX_INLINE PxVec4 operator*(float f) const
scalar post-multiplication
Definition PxVec4.h:237
PX_CUDA_CALLABLE PX_INLINE void setZero()
set vector elements to zero
Definition PxVec4.h:358
PX_CUDA_CALLABLE PX_INLINE PxVec4(float a)
Assigns scalar parameter to all elements.
Definition PxVec4.h:74
PX_CUDA_CALLABLE PX_INLINE PxVec4 operator/(float f) const
scalar division
Definition PxVec4.h:245
PX_CUDA_CALLABLE PX_INLINE PxVec4 operator-() const
negation
Definition PxVec4.h:212
PX_CUDA_CALLABLE PX_INLINE PxVec4 getNormalized() const
Definition PxVec4.h:309
PX_CUDA_CALLABLE PX_INLINE PxVec4 multiply(const PxVec4 &a) const
a[i] * b[i], for all i.
Definition PxVec4.h:329
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 PxVec4.h:177
PX_CUDA_CALLABLE PX_INLINE const float & operator[](unsigned int index) const
element access
Definition PxVec4.h:143
PX_CUDA_CALLABLE PX_INLINE float normalize()
normalizes the vector in place
Definition PxVec4.h:318
#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