PxVec3.h

Go to the documentation of this file.
00001 //
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions
00004 // are met:
00005 //  * Redistributions of source code must retain the above copyright
00006 //    notice, this list of conditions and the following disclaimer.
00007 //  * Redistributions in binary form must reproduce the above copyright
00008 //    notice, this list of conditions and the following disclaimer in the
00009 //    documentation and/or other materials provided with the distribution.
00010 //  * Neither the name of NVIDIA CORPORATION nor the names of its
00011 //    contributors may be used to endorse or promote products derived
00012 //    from this software without specific prior written permission.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
00015 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017 // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00018 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00021 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00022 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00024 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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_DEPRECATED 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_DEPRECATED 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     }
00131     PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec3& v) const
00132     {
00133         return x == v.x && y == v.y && z == v.z;
00134     }
00135 
00139     PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec3& v) const
00140     {
00141         return x != v.x || y != v.y || z != v.z;
00142     }
00143 
00147     PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
00148     {
00149         return x == 0.0f && y == 0.0f && z == 0.0f;
00150     }
00151 
00155     PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
00156     {
00157         return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z);
00158     }
00159 
00163     PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
00164     {
00165         const float unitTolerance = 1e-4f;
00166         return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
00167     }
00168 
00174     PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const
00175     {
00176         return x * x + y * y + z * z;
00177     }
00178 
00182     PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
00183     {
00184         return PxSqrt(magnitudeSquared());
00185     }
00186 
00190     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-() const
00191     {
00192         return PxVec3(-x, -y, -z);
00193     }
00194 
00198     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator+(const PxVec3& v) const
00199     {
00200         return PxVec3(x + v.x, y + v.y, z + v.z);
00201     }
00202 
00206     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-(const PxVec3& v) const
00207     {
00208         return PxVec3(x - v.x, y - v.y, z - v.z);
00209     }
00210 
00214     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator*(float f) const
00215     {
00216         return PxVec3(x * f, y * f, z * f);
00217     }
00218 
00222     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator/(float f) const
00223     {
00224         f = 1.0f / f;
00225         return PxVec3(x * f, y * f, z * f);
00226     }
00227 
00231     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator+=(const PxVec3& v)
00232     {
00233         x += v.x;
00234         y += v.y;
00235         z += v.z;
00236         return *this;
00237     }
00238 
00242     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator-=(const PxVec3& v)
00243     {
00244         x -= v.x;
00245         y -= v.y;
00246         z -= v.z;
00247         return *this;
00248     }
00249 
00253     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator*=(float f)
00254     {
00255         x *= f;
00256         y *= f;
00257         z *= f;
00258         return *this;
00259     }
00263     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator/=(float f)
00264     {
00265         f = 1.0f / f;
00266         x *= f;
00267         y *= f;
00268         z *= f;
00269         return *this;
00270     }
00271 
00275     PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3& v) const
00276     {
00277         return x * v.x + y * v.y + z * v.z;
00278     }
00279 
00283     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 cross(const PxVec3& v) const
00284     {
00285         return PxVec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
00286     }
00287 
00290     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 getNormalized() const
00291     {
00292         const float m = magnitudeSquared();
00293         return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec3(0, 0, 0);
00294     }
00295 
00299     PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
00300     {
00301         const float m = magnitude();
00302         if(m > 0.0f)
00303             *this /= m;
00304         return m;
00305     }
00306 
00311     PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeSafe()
00312     {
00313         const float mag = magnitude();
00314         if(mag < PX_NORMALIZATION_EPSILON)
00315             return 0.0f;
00316         *this *= 1.0f / mag;
00317         return mag;
00318     }
00319 
00324     PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeFast()
00325     {
00326         const float mag = magnitude();
00327         PX_ASSERT(mag >= PX_NORMALIZATION_EPSILON);
00328         *this *= 1.0f / mag;
00329         return mag;
00330     }
00331 
00335     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 multiply(const PxVec3& a) const
00336     {
00337         return PxVec3(x * a.x, y * a.y, z * a.z);
00338     }
00339 
00343     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 minimum(const PxVec3& v) const
00344     {
00345         return PxVec3(PxMin(x, v.x), PxMin(y, v.y), PxMin(z, v.z));
00346     }
00347 
00351     PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const
00352     {
00353         return PxMin(x, PxMin(y, z));
00354     }
00355 
00359     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 maximum(const PxVec3& v) const
00360     {
00361         return PxVec3(PxMax(x, v.x), PxMax(y, v.y), PxMax(z, v.z));
00362     }
00363 
00367     PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const
00368     {
00369         return PxMax(x, PxMax(y, z));
00370     }
00371 
00375     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 abs() const
00376     {
00377         return PxVec3(PxAbs(x), PxAbs(y), PxAbs(z));
00378     }
00379 
00380     float x, y, z;
00381 };
00382 
00383 PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec3 operator*(float f, const PxVec3& v)
00384 {
00385     return PxVec3(f * v.x, f * v.y, f * v.z);
00386 }
00387 
00388 #if !PX_DOXYGEN
00389 } // namespace physx
00390 #endif
00391 
00393 #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