00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef PXFOUNDATION_PXVEC3_H
00031 #define PXFOUNDATION_PXVEC3_H
00032
00037 #include "foundation/PxMath.h"
00038
00039 #if !PX_DOXYGEN
00040 namespace physx
00041 {
00042 #endif
00043
00049 class PxVec3
00050 {
00051 public:
00055 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3()
00056 {
00057 }
00058
00062 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(PxZERO r) : x(0.0f), y(0.0f), z(0.0f)
00063 {
00064 PX_UNUSED(r);
00065 }
00066
00074 explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float a) : x(a), y(a), z(a)
00075 {
00076 }
00077
00085 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
00086 {
00087 }
00088
00092 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(const PxVec3& v) : x(v.x), y(v.y), z(v.z)
00093 {
00094 }
00095
00096
00097
00101 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator=(const PxVec3& p)
00102 {
00103 x = p.x;
00104 y = p.y;
00105 z = p.z;
00106 return *this;
00107 }
00108
00112 PX_CUDA_CALLABLE PX_FORCE_INLINE float& operator[](unsigned int index)
00113 {
00114 PX_ASSERT(index <= 2);
00115
00116 return reinterpret_cast<float*>(this)[index];
00117 }
00118
00122 PX_CUDA_CALLABLE PX_FORCE_INLINE const float& operator[](unsigned int index) const
00123 {
00124 PX_ASSERT(index <= 2);
00125
00126 return reinterpret_cast<const float*>(this)[index];
00127 }
00128
00132 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec3& v) const
00133 {
00134 return x == v.x && y == v.y && z == v.z;
00135 }
00136
00140 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec3& v) const
00141 {
00142 return x != v.x || y != v.y || z != v.z;
00143 }
00144
00148 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
00149 {
00150 return x == 0.0f && y == 0.0f && z == 0.0f;
00151 }
00152
00156 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
00157 {
00158 return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z);
00159 }
00160
00164 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
00165 {
00166 const float unitTolerance = 1e-4f;
00167 return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
00168 }
00169
00175 PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const
00176 {
00177 return x * x + y * y + z * z;
00178 }
00179
00183 PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
00184 {
00185 return PxSqrt(magnitudeSquared());
00186 }
00187
00191 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-() const
00192 {
00193 return PxVec3(-x, -y, -z);
00194 }
00195
00199 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator+(const PxVec3& v) const
00200 {
00201 return PxVec3(x + v.x, y + v.y, z + v.z);
00202 }
00203
00207 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-(const PxVec3& v) const
00208 {
00209 return PxVec3(x - v.x, y - v.y, z - v.z);
00210 }
00211
00215 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator*(float f) const
00216 {
00217 return PxVec3(x * f, y * f, z * f);
00218 }
00219
00223 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator/(float f) const
00224 {
00225 f = 1.0f / f;
00226 return PxVec3(x * f, y * f, z * f);
00227 }
00228
00232 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator+=(const PxVec3& v)
00233 {
00234 x += v.x;
00235 y += v.y;
00236 z += v.z;
00237 return *this;
00238 }
00239
00243 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator-=(const PxVec3& v)
00244 {
00245 x -= v.x;
00246 y -= v.y;
00247 z -= v.z;
00248 return *this;
00249 }
00250
00254 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator*=(float f)
00255 {
00256 x *= f;
00257 y *= f;
00258 z *= f;
00259 return *this;
00260 }
00264 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator/=(float f)
00265 {
00266 f = 1.0f / f;
00267 x *= f;
00268 y *= f;
00269 z *= f;
00270 return *this;
00271 }
00272
00276 PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3& v) const
00277 {
00278 return x * v.x + y * v.y + z * v.z;
00279 }
00280
00284 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 cross(const PxVec3& v) const
00285 {
00286 return PxVec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
00287 }
00288
00291 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 getNormalized() const
00292 {
00293 const float m = magnitudeSquared();
00294 return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec3(0, 0, 0);
00295 }
00296
00300 PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
00301 {
00302 const float m = magnitude();
00303 if(m > 0.0f)
00304 *this /= m;
00305 return m;
00306 }
00307
00312 PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeSafe()
00313 {
00314 const float mag = magnitude();
00315 if(mag < PX_NORMALIZATION_EPSILON)
00316 return 0.0f;
00317 *this *= 1.0f / mag;
00318 return mag;
00319 }
00320
00325 PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeFast()
00326 {
00327 const float mag = magnitude();
00328 PX_ASSERT(mag >= PX_NORMALIZATION_EPSILON);
00329 *this *= 1.0f / mag;
00330 return mag;
00331 }
00332
00336 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 multiply(const PxVec3& a) const
00337 {
00338 return PxVec3(x * a.x, y * a.y, z * a.z);
00339 }
00340
00344 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 minimum(const PxVec3& v) const
00345 {
00346 return PxVec3(PxMin(x, v.x), PxMin(y, v.y), PxMin(z, v.z));
00347 }
00348
00352 PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const
00353 {
00354 return PxMin(x, PxMin(y, z));
00355 }
00356
00360 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 maximum(const PxVec3& v) const
00361 {
00362 return PxVec3(PxMax(x, v.x), PxMax(y, v.y), PxMax(z, v.z));
00363 }
00364
00368 PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const
00369 {
00370 return PxMax(x, PxMax(y, z));
00371 }
00372
00376 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 abs() const
00377 {
00378 return PxVec3(PxAbs(x), PxAbs(y), PxAbs(z));
00379 }
00380
00381 float x, y, z;
00382 };
00383
00384 PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec3 operator*(float f, const PxVec3& v)
00385 {
00386 return PxVec3(f * v.x, f * v.y, f * v.z);
00387 }
00388
00389 #if !PX_DOXYGEN
00390 }
00391 #endif
00392
00394 #endif // #ifndef PXFOUNDATION_PXVEC3_H