| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | |
| 31 | #ifndef PX_PHYSICS_GEOMETRYHELPERS |
| 32 | #define PX_PHYSICS_GEOMETRYHELPERS |
| 33 | /** \addtogroup geomutils |
| 34 | @{ |
| 35 | */ |
| 36 | |
| 37 | #include "foundation/PxPlane.h" |
| 38 | #include "foundation/PxTransform.h" |
| 39 | #include "foundation/PxUnionCast.h" |
| 40 | #include "common/PxPhysXCommonConfig.h" |
| 41 | #include "geometry/PxGeometry.h" |
| 42 | #include "geometry/PxBoxGeometry.h" |
| 43 | #include "geometry/PxSphereGeometry.h" |
| 44 | #include "geometry/PxCapsuleGeometry.h" |
| 45 | #include "geometry/PxPlaneGeometry.h" |
| 46 | #include "geometry/PxConvexMeshGeometry.h" |
| 47 | #include "geometry/PxHeightFieldGeometry.h" |
| 48 | |
| 49 | #if !PX_DOXYGEN |
| 50 | namespace physx |
| 51 | { |
| 52 | #endif |
| 53 | |
| 54 | /** |
| 55 | \brief Geometry holder class |
| 56 | |
| 57 | This class contains enough space to hold a value of any PxGeometry subtype. |
| 58 | |
| 59 | Its principal use is as a convenience class to allow geometries to be returned polymorphically |
| 60 | from functions. See PxShape::getGeometry(); |
| 61 | */ |
| 62 | |
| 63 | PX_ALIGN_PREFIX(4) |
| 64 | class PxGeometryHolder |
| 65 | { |
| 66 | public: |
| 67 | PX_FORCE_INLINE PxGeometryType::Enum getType() const |
| 68 | { |
| 69 | return any().getType(); |
| 70 | } |
| 71 | |
| 72 | PX_FORCE_INLINE PxGeometry& any() |
| 73 | { |
| 74 | return *PxUnionCast<PxGeometry*>(b: &bytes.geometry); |
| 75 | } |
| 76 | |
| 77 | PX_FORCE_INLINE const PxGeometry& any() const |
| 78 | { |
| 79 | return *PxUnionCast<const PxGeometry*>(b: &bytes.geometry); |
| 80 | } |
| 81 | |
| 82 | PX_FORCE_INLINE PxSphereGeometry& sphere() |
| 83 | { |
| 84 | return get<PxSphereGeometry, PxGeometryType::eSPHERE>(); |
| 85 | } |
| 86 | |
| 87 | PX_FORCE_INLINE const PxSphereGeometry& sphere() const |
| 88 | { |
| 89 | return get<const PxSphereGeometry, PxGeometryType::eSPHERE>(); |
| 90 | } |
| 91 | |
| 92 | PX_FORCE_INLINE PxPlaneGeometry& plane() |
| 93 | { |
| 94 | return get<PxPlaneGeometry, PxGeometryType::ePLANE>(); |
| 95 | } |
| 96 | |
| 97 | PX_FORCE_INLINE const PxPlaneGeometry& plane() const |
| 98 | { |
| 99 | return get<const PxPlaneGeometry, PxGeometryType::ePLANE>(); |
| 100 | } |
| 101 | |
| 102 | PX_FORCE_INLINE PxCapsuleGeometry& capsule() |
| 103 | { |
| 104 | return get<PxCapsuleGeometry, PxGeometryType::eCAPSULE>(); |
| 105 | } |
| 106 | |
| 107 | PX_FORCE_INLINE const PxCapsuleGeometry& capsule() const |
| 108 | { |
| 109 | return get<const PxCapsuleGeometry, PxGeometryType::eCAPSULE>(); |
| 110 | } |
| 111 | |
| 112 | PX_FORCE_INLINE PxBoxGeometry& box() |
| 113 | { |
| 114 | return get<PxBoxGeometry, PxGeometryType::eBOX>(); |
| 115 | } |
| 116 | |
| 117 | PX_FORCE_INLINE const PxBoxGeometry& box() const |
| 118 | { |
| 119 | return get<const PxBoxGeometry, PxGeometryType::eBOX>(); |
| 120 | } |
| 121 | |
| 122 | PX_FORCE_INLINE PxConvexMeshGeometry& convexMesh() |
| 123 | { |
| 124 | return get<PxConvexMeshGeometry, PxGeometryType::eCONVEXMESH>(); |
| 125 | } |
| 126 | |
| 127 | PX_FORCE_INLINE const PxConvexMeshGeometry& convexMesh() const |
| 128 | { |
| 129 | return get<const PxConvexMeshGeometry, PxGeometryType::eCONVEXMESH>(); |
| 130 | } |
| 131 | |
| 132 | PX_FORCE_INLINE PxTriangleMeshGeometry& triangleMesh() |
| 133 | { |
| 134 | return get<PxTriangleMeshGeometry, PxGeometryType::eTRIANGLEMESH>(); |
| 135 | } |
| 136 | |
| 137 | PX_FORCE_INLINE const PxTriangleMeshGeometry& triangleMesh() const |
| 138 | { |
| 139 | return get<const PxTriangleMeshGeometry, PxGeometryType::eTRIANGLEMESH>(); |
| 140 | } |
| 141 | |
| 142 | PX_FORCE_INLINE PxHeightFieldGeometry& heightField() |
| 143 | { |
| 144 | return get<PxHeightFieldGeometry, PxGeometryType::eHEIGHTFIELD>(); |
| 145 | } |
| 146 | |
| 147 | PX_FORCE_INLINE const PxHeightFieldGeometry& heightField() const |
| 148 | { |
| 149 | return get<const PxHeightFieldGeometry, PxGeometryType::eHEIGHTFIELD>(); |
| 150 | } |
| 151 | |
| 152 | PX_FORCE_INLINE void storeAny(const PxGeometry& geometry) |
| 153 | { |
| 154 | PX_ASSERT_WITH_MESSAGE( (geometry.getType() >= PxGeometryType::eSPHERE) && |
| 155 | (geometry.getType() < PxGeometryType::eGEOMETRY_COUNT), |
| 156 | "Unexpected GeometryType in PxGeometryHolder::storeAny" ); |
| 157 | |
| 158 | switch(geometry.getType()) |
| 159 | { |
| 160 | case PxGeometryType::eSPHERE: put<PxSphereGeometry>(geometry); break; |
| 161 | case PxGeometryType::ePLANE: put<PxPlaneGeometry>(geometry); break; |
| 162 | case PxGeometryType::eCAPSULE: put<PxCapsuleGeometry>(geometry); break; |
| 163 | case PxGeometryType::eBOX: put<PxBoxGeometry>(geometry); break; |
| 164 | case PxGeometryType::eCONVEXMESH: put<PxConvexMeshGeometry>(geometry); break; |
| 165 | case PxGeometryType::eTRIANGLEMESH: put<PxTriangleMeshGeometry>(geometry); break; |
| 166 | case PxGeometryType::eHEIGHTFIELD: put<PxHeightFieldGeometry>(geometry); break; |
| 167 | case PxGeometryType::eGEOMETRY_COUNT: |
| 168 | case PxGeometryType::eINVALID: break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | PX_FORCE_INLINE PxGeometryHolder() {} |
| 173 | PX_FORCE_INLINE PxGeometryHolder(const PxGeometry& geometry){ storeAny(geometry); } |
| 174 | |
| 175 | private: |
| 176 | template<typename T> void put(const PxGeometry& geometry) |
| 177 | { |
| 178 | static_cast<T&>(any()) = static_cast<const T&>(geometry); |
| 179 | } |
| 180 | |
| 181 | template<typename T, PxGeometryType::Enum type> T& get() |
| 182 | { |
| 183 | PX_ASSERT(getType() == type); |
| 184 | return static_cast<T&>(any()); |
| 185 | } |
| 186 | |
| 187 | template<typename T, PxGeometryType::Enum type> T& get() const |
| 188 | { |
| 189 | PX_ASSERT(getType() == type); |
| 190 | return static_cast<T&>(any()); |
| 191 | } |
| 192 | |
| 193 | union { |
| 194 | PxU8 geometry[sizeof(PxGeometry)]; |
| 195 | PxU8 box[sizeof(PxBoxGeometry)]; |
| 196 | PxU8 sphere[sizeof(PxSphereGeometry)]; |
| 197 | PxU8 capsule[sizeof(PxCapsuleGeometry)]; |
| 198 | PxU8 plane[sizeof(PxPlaneGeometry)]; |
| 199 | PxU8 convex[sizeof(PxConvexMeshGeometry)]; |
| 200 | PxU8 mesh[sizeof(PxTriangleMeshGeometry)]; |
| 201 | PxU8 heightfield[sizeof(PxHeightFieldGeometry)]; |
| 202 | } bytes; |
| 203 | } |
| 204 | PX_ALIGN_SUFFIX(4); |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | #if !PX_DOXYGEN |
| 210 | } // namespace physx |
| 211 | #endif |
| 212 | |
| 213 | /** @} */ |
| 214 | #endif |
| 215 | |