PxVec2.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_PXVEC2_H
00031 #define PXFOUNDATION_PXVEC2_H
00032 
00037 #include "foundation/PxMath.h"
00038 
00039 #if !PX_DOXYGEN
00040 namespace physx
00041 {
00042 #endif
00043 
00049 class PxVec2
00050 {
00051   public:
00055     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2()
00056     {
00057     }
00058 
00062     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(PxZERO r) : x(0.0f), y(0.0f)
00063     {
00064         PX_UNUSED(r);
00065     }
00066 
00074     explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(float a) : x(a), y(a)
00075     {
00076     }
00077 
00084     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(float nx, float ny) : x(nx), y(ny)
00085     {
00086     }
00087 
00091     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(const PxVec2& v) : x(v.x), y(v.y)
00092     {
00093     }
00094 
00095     // Operators
00096 
00100     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator=(const PxVec2& p)
00101     {
00102         x = p.x;
00103         y = p.y;
00104         return *this;
00105     }
00106 
00110     PX_DEPRECATED PX_CUDA_CALLABLE PX_FORCE_INLINE float& operator[](int index)
00111     {
00112         PX_ASSERT(index >= 0 && index <= 1);
00113 
00114         return reinterpret_cast<float*>(this)[index];
00115     }
00116 
00120     PX_DEPRECATED PX_CUDA_CALLABLE PX_FORCE_INLINE const float& operator[](int index) const
00121     {
00122         PX_ASSERT(index >= 0 && index <= 1);
00123 
00124         return reinterpret_cast<const float*>(this)[index];
00125     }
00126 
00130     PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec2& v) const
00131     {
00132         return x == v.x && y == v.y;
00133     }
00134 
00138     PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec2& v) const
00139     {
00140         return x != v.x || y != v.y;
00141     }
00142 
00146     PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
00147     {
00148         return x == 0.0f && y == 0.0f;
00149     }
00150 
00154     PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
00155     {
00156         return PxIsFinite(x) && PxIsFinite(y);
00157     }
00158 
00162     PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
00163     {
00164         const float unitTolerance = 1e-4f;
00165         return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
00166     }
00167 
00173     PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const
00174     {
00175         return x * x + y * y;
00176     }
00177 
00181     PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
00182     {
00183         return PxSqrt(magnitudeSquared());
00184     }
00185 
00189     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator-() const
00190     {
00191         return PxVec2(-x, -y);
00192     }
00193 
00197     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator+(const PxVec2& v) const
00198     {
00199         return PxVec2(x + v.x, y + v.y);
00200     }
00201 
00205     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator-(const PxVec2& v) const
00206     {
00207         return PxVec2(x - v.x, y - v.y);
00208     }
00209 
00213     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator*(float f) const
00214     {
00215         return PxVec2(x * f, y * f);
00216     }
00217 
00221     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator/(float f) const
00222     {
00223         f = 1.0f / f; // PT: inconsistent notation with operator /=
00224         return PxVec2(x * f, y * f);
00225     }
00226 
00230     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator+=(const PxVec2& v)
00231     {
00232         x += v.x;
00233         y += v.y;
00234         return *this;
00235     }
00236 
00240     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator-=(const PxVec2& v)
00241     {
00242         x -= v.x;
00243         y -= v.y;
00244         return *this;
00245     }
00246 
00250     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator*=(float f)
00251     {
00252         x *= f;
00253         y *= f;
00254         return *this;
00255     }
00259     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator/=(float f)
00260     {
00261         f = 1.0f / f; // PT: inconsistent notation with operator /
00262         x *= f;
00263         y *= f;
00264         return *this;
00265     }
00266 
00270     PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec2& v) const
00271     {
00272         return x * v.x + y * v.y;
00273     }
00274 
00277     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 getNormalized() const
00278     {
00279         const float m = magnitudeSquared();
00280         return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec2(0, 0);
00281     }
00282 
00286     PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
00287     {
00288         const float m = magnitude();
00289         if(m > 0.0f)
00290             *this /= m;
00291         return m;
00292     }
00293 
00297     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 multiply(const PxVec2& a) const
00298     {
00299         return PxVec2(x * a.x, y * a.y);
00300     }
00301 
00305     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 minimum(const PxVec2& v) const
00306     {
00307         return PxVec2(PxMin(x, v.x), PxMin(y, v.y));
00308     }
00309 
00313     PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const
00314     {
00315         return PxMin(x, y);
00316     }
00317 
00321     PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 maximum(const PxVec2& v) const
00322     {
00323         return PxVec2(PxMax(x, v.x), PxMax(y, v.y));
00324     }
00325 
00329     PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const
00330     {
00331         return PxMax(x, y);
00332     }
00333 
00334     float x, y;
00335 };
00336 
00337 PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec2 operator*(float f, const PxVec2& v)
00338 {
00339     return PxVec2(f * v.x, f * v.y);
00340 }
00341 
00342 #if !PX_DOXYGEN
00343 } // namespace physx
00344 #endif
00345 
00347 #endif // #ifndef PXFOUNDATION_PXVEC2_H


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