PxVec3.h

Go to the documentation of this file.
00001 // This code contains NVIDIA Confidential Information and is disclosed to you
00002 // under a form of NVIDIA software license agreement provided separately to you.
00003 //
00004 // Notice
00005 // NVIDIA Corporation and its licensors retain all intellectual property and
00006 // proprietary rights in and to this software and related documentation and
00007 // any modifications thereto. Any use, reproduction, disclosure, or
00008 // distribution of this software and related documentation without an express
00009 // license agreement from NVIDIA Corporation is strictly prohibited.
00010 //
00011 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
00012 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
00013 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
00014 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
00015 //
00016 // Information and code furnished is believed to be accurate and reliable.
00017 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
00018 // information or for any infringement of patents or other rights of third parties that may
00019 // result from its use. No license is granted by implication or otherwise under any patent
00020 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
00021 // This code supersedes and replaces all information previously supplied.
00022 // NVIDIA Corporation products are not authorized for use as critical
00023 // components in life support devices or systems without express written approval of
00024 // NVIDIA Corporation.
00025 //
00026 // Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
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     // Operators
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 } // namespace physx
00391 #endif
00392 
00394 #endif // #ifndef PXFOUNDATION_PXVEC3_H


Copyright © 2008-2018 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com