PxGeometryHelpers.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 
00031 #ifndef PX_PHYSICS_GEOMETRYHELPERS
00032 #define PX_PHYSICS_GEOMETRYHELPERS
00033 
00037 #include "common/PxPhysXCommonConfig.h"
00038 #include "PxGeometry.h"
00039 #include "PxBoxGeometry.h"
00040 #include "PxSphereGeometry.h"
00041 #include "PxCapsuleGeometry.h"
00042 #include "PxPlaneGeometry.h"
00043 #include "PxConvexMeshGeometry.h"
00044 #include "PxTriangleMeshGeometry.h"
00045 #include "PxHeightFieldGeometry.h"
00046 #include "foundation/PxPlane.h"
00047 #include "foundation/PxTransform.h"
00048 #include "foundation/PxUnionCast.h"
00049 
00050 #if !PX_DOXYGEN
00051 namespace physx
00052 {
00053 #endif
00054 
00064 PX_ALIGN_PREFIX(4)
00065 class PxGeometryHolder
00066 {
00067 public:
00068     PX_FORCE_INLINE PxGeometryType::Enum getType() const
00069     {
00070         return any().getType();
00071     }
00072 
00073     PX_FORCE_INLINE PxGeometry& any()
00074     { 
00075         return *PxUnionCast<PxGeometry*>(&bytes.geometry); 
00076     }
00077 
00078     PX_FORCE_INLINE const PxGeometry& any() const 
00079     { 
00080         return *PxUnionCast<const PxGeometry*>(&bytes.geometry); 
00081     }
00082 
00083     PX_FORCE_INLINE PxSphereGeometry& sphere()
00084     {
00085         return get<PxSphereGeometry, PxGeometryType::eSPHERE>();
00086     }
00087 
00088     PX_FORCE_INLINE const PxSphereGeometry& sphere() const
00089     {
00090         return get<const PxSphereGeometry, PxGeometryType::eSPHERE>();
00091     }
00092 
00093     PX_FORCE_INLINE PxPlaneGeometry& plane()
00094     {
00095         return get<PxPlaneGeometry, PxGeometryType::ePLANE>();
00096     }
00097 
00098     PX_FORCE_INLINE const PxPlaneGeometry& plane() const
00099     {
00100         return get<const PxPlaneGeometry, PxGeometryType::ePLANE>();
00101     }
00102 
00103     PX_FORCE_INLINE PxCapsuleGeometry& capsule()
00104     {
00105         return get<PxCapsuleGeometry, PxGeometryType::eCAPSULE>();
00106     }
00107 
00108     PX_FORCE_INLINE const PxCapsuleGeometry& capsule() const
00109     {
00110         return get<const PxCapsuleGeometry, PxGeometryType::eCAPSULE>();
00111     }
00112 
00113     PX_FORCE_INLINE PxBoxGeometry& box()
00114     {
00115         return get<PxBoxGeometry, PxGeometryType::eBOX>();
00116     }
00117 
00118     PX_FORCE_INLINE const PxBoxGeometry& box() const
00119     {
00120         return get<const PxBoxGeometry, PxGeometryType::eBOX>();
00121     }
00122 
00123     PX_FORCE_INLINE PxConvexMeshGeometry& convexMesh()
00124     {
00125         return get<PxConvexMeshGeometry, PxGeometryType::eCONVEXMESH>();
00126     }
00127 
00128     PX_FORCE_INLINE const PxConvexMeshGeometry& convexMesh() const
00129     {
00130         return get<const PxConvexMeshGeometry, PxGeometryType::eCONVEXMESH>();
00131     }
00132 
00133     PX_FORCE_INLINE PxTriangleMeshGeometry& triangleMesh()
00134     {
00135         return get<PxTriangleMeshGeometry, PxGeometryType::eTRIANGLEMESH>();
00136     }
00137 
00138     PX_FORCE_INLINE const PxTriangleMeshGeometry& triangleMesh() const
00139     {
00140         return get<const PxTriangleMeshGeometry, PxGeometryType::eTRIANGLEMESH>();
00141     }
00142 
00143     PX_FORCE_INLINE PxHeightFieldGeometry& heightField()
00144     {
00145         return get<PxHeightFieldGeometry, PxGeometryType::eHEIGHTFIELD>();
00146     }
00147 
00148     PX_FORCE_INLINE const PxHeightFieldGeometry& heightField() const
00149     {
00150         return get<const PxHeightFieldGeometry, PxGeometryType::eHEIGHTFIELD>();
00151     }
00152 
00153     PX_FORCE_INLINE void storeAny(const PxGeometry& geometry)
00154     {
00155         PX_ASSERT_WITH_MESSAGE( (geometry.getType() >= PxGeometryType::eSPHERE) &&
00156                                 (geometry.getType() < PxGeometryType::eGEOMETRY_COUNT),
00157                                 "Unexpected GeometryType in PxGeometryHolder::storeAny");
00158 
00159         switch(geometry.getType())
00160         {
00161         case PxGeometryType::eSPHERE:       put<PxSphereGeometry>(geometry); break;
00162         case PxGeometryType::ePLANE:        put<PxPlaneGeometry>(geometry); break;
00163         case PxGeometryType::eCAPSULE:      put<PxCapsuleGeometry>(geometry); break;
00164         case PxGeometryType::eBOX:          put<PxBoxGeometry>(geometry); break;
00165         case PxGeometryType::eCONVEXMESH:   put<PxConvexMeshGeometry>(geometry); break;
00166         case PxGeometryType::eTRIANGLEMESH: put<PxTriangleMeshGeometry>(geometry); break;
00167         case PxGeometryType::eHEIGHTFIELD:  put<PxHeightFieldGeometry>(geometry); break;
00168         case PxGeometryType::eGEOMETRY_COUNT:
00169         case PxGeometryType::eINVALID:      break;
00170         }
00171     }
00172 
00173     PX_FORCE_INLINE PxGeometryHolder()                          {}
00174     PX_FORCE_INLINE PxGeometryHolder(const PxGeometry& geometry){ storeAny(geometry);   }
00175 
00176     private:
00177         template<typename T> void put(const PxGeometry& geometry)
00178         {
00179             static_cast<T&>(any()) = static_cast<const T&>(geometry);
00180         }
00181 
00182         template<typename T, PxGeometryType::Enum type> T& get()
00183         {
00184             PX_ASSERT(getType() == type);
00185             return static_cast<T&>(any());
00186         }
00187 
00188         template<typename T, PxGeometryType::Enum type> T& get() const
00189         {
00190             PX_ASSERT(getType() == type);
00191             return static_cast<T&>(any());
00192         }
00193 
00194     union {
00195         PxU8    geometry[sizeof(PxGeometry)];
00196         PxU8    box[sizeof(PxBoxGeometry)];
00197         PxU8    sphere[sizeof(PxSphereGeometry)];
00198         PxU8    capsule[sizeof(PxCapsuleGeometry)];
00199         PxU8    plane[sizeof(PxPlaneGeometry)];
00200         PxU8    convex[sizeof(PxConvexMeshGeometry)];
00201         PxU8    mesh[sizeof(PxTriangleMeshGeometry)];
00202         PxU8    heightfield[sizeof(PxHeightFieldGeometry)];
00203     } bytes;
00204 }
00205 PX_ALIGN_SUFFIX(4);
00206 
00207 
00208 
00209 
00210 #if !PX_DOXYGEN
00211 } // namespace physx
00212 #endif
00213 
00215 #endif


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