PxVec4.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_PXVEC4_H
00031 #define PXFOUNDATION_PXVEC4_H
00032 
00035 #include "foundation/PxMath.h"
00036 #include "foundation/PxVec3.h"
00037 #include "foundation/PxAssert.h"
00038 
00044 #if !PX_DOXYGEN
00045 namespace physx
00046 {
00047 #endif
00048 
00049 class PxVec4
00050 {
00051   public:
00055     PX_CUDA_CALLABLE PX_INLINE PxVec4()
00056     {
00057     }
00058 
00062     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec4(PxZERO r) : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
00063     {
00064         PX_UNUSED(r);
00065     }
00066 
00074     explicit PX_CUDA_CALLABLE PX_INLINE PxVec4(float a) : x(a), y(a), z(a), w(a)
00075     {
00076     }
00077 
00086     PX_CUDA_CALLABLE PX_INLINE PxVec4(float nx, float ny, float nz, float nw) : x(nx), y(ny), z(nz), w(nw)
00087     {
00088     }
00089 
00096     PX_CUDA_CALLABLE PX_INLINE PxVec4(const PxVec3& v, float nw) : x(v.x), y(v.y), z(v.z), w(nw)
00097     {
00098     }
00099 
00105     explicit PX_CUDA_CALLABLE PX_INLINE PxVec4(const float v[]) : x(v[0]), y(v[1]), z(v[2]), w(v[3])
00106     {
00107     }
00108 
00112     PX_CUDA_CALLABLE PX_INLINE PxVec4(const PxVec4& v) : x(v.x), y(v.y), z(v.z), w(v.w)
00113     {
00114     }
00115 
00116     // Operators
00117 
00121     PX_CUDA_CALLABLE PX_INLINE PxVec4& operator=(const PxVec4& p)
00122     {
00123         x = p.x;
00124         y = p.y;
00125         z = p.z;
00126         w = p.w;
00127         return *this;
00128     }
00129 
00133     PX_DEPRECATED PX_CUDA_CALLABLE PX_INLINE float& operator[](unsigned int index)
00134     {
00135         PX_ASSERT(index <= 3);
00136 
00137         return reinterpret_cast<float*>(this)[index];
00138     }
00139 
00143     PX_DEPRECATED PX_CUDA_CALLABLE PX_INLINE const float& operator[](unsigned int index) const
00144     {
00145         PX_ASSERT(index <= 3);
00146 
00147         return reinterpret_cast<const float*>(this)[index];
00148     }
00149 
00153     PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxVec4& v) const
00154     {
00155         return x == v.x && y == v.y && z == v.z && w == v.w;
00156     }
00157 
00161     PX_CUDA_CALLABLE PX_INLINE bool operator!=(const PxVec4& v) const
00162     {
00163         return x != v.x || y != v.y || z != v.z || w != v.w;
00164     }
00165 
00169     PX_CUDA_CALLABLE PX_INLINE bool isZero() const
00170     {
00171         return x == 0 && y == 0 && z == 0 && w == 0;
00172     }
00173 
00177     PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
00178     {
00179         return PxIsFinite(x) && PxIsFinite(y) && PxIsFinite(z) && PxIsFinite(w);
00180     }
00181 
00185     PX_CUDA_CALLABLE PX_INLINE bool isNormalized() const
00186     {
00187         const float unitTolerance = 1e-4f;
00188         return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
00189     }
00190 
00196     PX_CUDA_CALLABLE PX_INLINE float magnitudeSquared() const
00197     {
00198         return x * x + y * y + z * z + w * w;
00199     }
00200 
00204     PX_CUDA_CALLABLE PX_INLINE float magnitude() const
00205     {
00206         return PxSqrt(magnitudeSquared());
00207     }
00208 
00212     PX_CUDA_CALLABLE PX_INLINE PxVec4 operator-() const
00213     {
00214         return PxVec4(-x, -y, -z, -w);
00215     }
00216 
00220     PX_CUDA_CALLABLE PX_INLINE PxVec4 operator+(const PxVec4& v) const
00221     {
00222         return PxVec4(x + v.x, y + v.y, z + v.z, w + v.w);
00223     }
00224 
00228     PX_CUDA_CALLABLE PX_INLINE PxVec4 operator-(const PxVec4& v) const
00229     {
00230         return PxVec4(x - v.x, y - v.y, z - v.z, w - v.w);
00231     }
00232 
00237     PX_CUDA_CALLABLE PX_INLINE PxVec4 operator*(float f) const
00238     {
00239         return PxVec4(x * f, y * f, z * f, w * f);
00240     }
00241 
00245     PX_CUDA_CALLABLE PX_INLINE PxVec4 operator/(float f) const
00246     {
00247         f = 1.0f / f;
00248         return PxVec4(x * f, y * f, z * f, w * f);
00249     }
00250 
00254     PX_CUDA_CALLABLE PX_INLINE PxVec4& operator+=(const PxVec4& v)
00255     {
00256         x += v.x;
00257         y += v.y;
00258         z += v.z;
00259         w += v.w;
00260         return *this;
00261     }
00262 
00266     PX_CUDA_CALLABLE PX_INLINE PxVec4& operator-=(const PxVec4& v)
00267     {
00268         x -= v.x;
00269         y -= v.y;
00270         z -= v.z;
00271         w -= v.w;
00272         return *this;
00273     }
00274 
00278     PX_CUDA_CALLABLE PX_INLINE PxVec4& operator*=(float f)
00279     {
00280         x *= f;
00281         y *= f;
00282         z *= f;
00283         w *= f;
00284         return *this;
00285     }
00289     PX_CUDA_CALLABLE PX_INLINE PxVec4& operator/=(float f)
00290     {
00291         f = 1.0f / f;
00292         x *= f;
00293         y *= f;
00294         z *= f;
00295         w *= f;
00296         return *this;
00297     }
00298 
00302     PX_CUDA_CALLABLE PX_INLINE float dot(const PxVec4& v) const
00303     {
00304         return x * v.x + y * v.y + z * v.z + w * v.w;
00305     }
00306 
00309     PX_CUDA_CALLABLE PX_INLINE PxVec4 getNormalized() const
00310     {
00311         float m = magnitudeSquared();
00312         return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec4(0, 0, 0, 0);
00313     }
00314 
00318     PX_CUDA_CALLABLE PX_INLINE float normalize()
00319     {
00320         float m = magnitude();
00321         if(m > 0.0f)
00322             *this /= m;
00323         return m;
00324     }
00325 
00329     PX_CUDA_CALLABLE PX_INLINE PxVec4 multiply(const PxVec4& a) const
00330     {
00331         return PxVec4(x * a.x, y * a.y, z * a.z, w * a.w);
00332     }
00333 
00337     PX_CUDA_CALLABLE PX_INLINE PxVec4 minimum(const PxVec4& v) const
00338     {
00339         return PxVec4(PxMin(x, v.x), PxMin(y, v.y), PxMin(z, v.z), PxMin(w, v.w));
00340     }
00341 
00345     PX_CUDA_CALLABLE PX_INLINE PxVec4 maximum(const PxVec4& v) const
00346     {
00347         return PxVec4(PxMax(x, v.x), PxMax(y, v.y), PxMax(z, v.z), PxMax(w, v.w));
00348     }
00349 
00350     PX_CUDA_CALLABLE PX_INLINE PxVec3 getXYZ() const
00351     {
00352         return PxVec3(x, y, z);
00353     }
00354 
00358     PX_CUDA_CALLABLE PX_INLINE void setZero()
00359     {
00360         x = y = z = w = 0.0f;
00361     }
00362 
00363     float x, y, z, w;
00364 };
00365 
00366 PX_CUDA_CALLABLE static PX_INLINE PxVec4 operator*(float f, const PxVec4& v)
00367 {
00368     return PxVec4(f * v.x, f * v.y, f * v.z, f * v.w);
00369 }
00370 
00371 #if !PX_DOXYGEN
00372 } // namespace physx
00373 #endif
00374 
00376 #endif // #ifndef PXFOUNDATION_PXVEC4_H


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