| 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_COLLISION_NXCONVEXMESHDESC |
| 32 | #define PX_COLLISION_NXCONVEXMESHDESC |
| 33 | /** \addtogroup cooking |
| 34 | @{ |
| 35 | */ |
| 36 | |
| 37 | #include "foundation/PxVec3.h" |
| 38 | #include "foundation/PxFlags.h" |
| 39 | #include "common/PxCoreUtilityTypes.h" |
| 40 | #include "geometry/PxConvexMesh.h" |
| 41 | |
| 42 | #if !PX_DOXYGEN |
| 43 | namespace physx |
| 44 | { |
| 45 | #endif |
| 46 | |
| 47 | /** |
| 48 | \brief Flags which describe the format and behavior of a convex mesh. |
| 49 | */ |
| 50 | struct PxConvexFlag |
| 51 | { |
| 52 | enum Enum |
| 53 | { |
| 54 | /** |
| 55 | Denotes the use of 16-bit vertex indices in PxConvexMeshDesc::triangles or PxConvexMeshDesc::polygons. |
| 56 | (otherwise, 32-bit indices are assumed) |
| 57 | @see #PxConvexMeshDesc.indices |
| 58 | */ |
| 59 | e16_BIT_INDICES = (1<<0), |
| 60 | |
| 61 | /** |
| 62 | Automatically recomputes the hull from the vertices. If this flag is not set, you must provide the entire geometry manually. |
| 63 | |
| 64 | \note There are two different algorithms for hull computation, please see PxConvexMeshCookingType. |
| 65 | |
| 66 | @see PxConvexMeshCookingType |
| 67 | */ |
| 68 | eCOMPUTE_CONVEX = (1<<1), |
| 69 | |
| 70 | /** |
| 71 | \brief Checks and removes almost zero-area triangles during convex hull computation. |
| 72 | The rejected area size is specified in PxCookingParams::areaTestEpsilon |
| 73 | |
| 74 | \note This flag is only used in combination with eCOMPUTE_CONVEX. |
| 75 | |
| 76 | @see PxCookingParams PxCookingParams::areaTestEpsilon |
| 77 | */ |
| 78 | eCHECK_ZERO_AREA_TRIANGLES = (1<<2), |
| 79 | |
| 80 | /** |
| 81 | \brief Quantizes the input vertices using the k-means clustering |
| 82 | |
| 83 | \note The input vertices are quantized to PxConvexMeshDesc::quantizedCount |
| 84 | see http://en.wikipedia.org/wiki/K-means_clustering |
| 85 | |
| 86 | */ |
| 87 | eQUANTIZE_INPUT = (1 << 3), |
| 88 | |
| 89 | /** |
| 90 | \brief Disables the convex mesh validation to speed-up hull creation. Please use separate validation |
| 91 | function in checked/debug builds. Creating a convex mesh with invalid input data without prior validation |
| 92 | may result in undefined behavior. |
| 93 | |
| 94 | @see PxCooking::validateConvexMesh |
| 95 | */ |
| 96 | eDISABLE_MESH_VALIDATION = (1 << 4), |
| 97 | |
| 98 | /** |
| 99 | \brief Enables plane shifting vertex limit algorithm. |
| 100 | |
| 101 | Plane shifting is an alternative algorithm for the case when the computed hull has more vertices |
| 102 | than the specified vertex limit. |
| 103 | |
| 104 | The default algorithm computes the full hull, and an OBB around the input vertices. This OBB is then sliced |
| 105 | with the hull planes until the vertex limit is reached.The default algorithm requires the vertex limit |
| 106 | to be set to at least 8, and typically produces results that are much better quality than are produced |
| 107 | by plane shifting. |
| 108 | |
| 109 | When plane shifting is enabled, the hull computation stops when vertex limit is reached. The hull planes |
| 110 | are then shifted to contain all input vertices, and the new plane intersection points are then used to |
| 111 | generate the final hull with the given vertex limit.Plane shifting may produce sharp edges to vertices |
| 112 | very far away from the input cloud, and does not guarantee that all input vertices are inside the resulting |
| 113 | hull.However, it can be used with a vertex limit as low as 4. |
| 114 | */ |
| 115 | ePLANE_SHIFTING = (1 << 5), |
| 116 | |
| 117 | /** |
| 118 | \brief Inertia tensor computation is faster using SIMD code, but the precision is lower, which may result |
| 119 | in incorrect inertia for very thin hulls. |
| 120 | */ |
| 121 | eFAST_INERTIA_COMPUTATION = (1 << 6), |
| 122 | |
| 123 | /** |
| 124 | \brief Convex hulls are created with respect to GPU simulation limitations. Vertex limit is set to 64 and |
| 125 | vertex limit per face is internally set to 32. |
| 126 | \note Can be used only with eCOMPUTE_CONVEX flag. |
| 127 | */ |
| 128 | eGPU_COMPATIBLE = (1 << 7), |
| 129 | |
| 130 | /** |
| 131 | \brief Convex hull input vertices are shifted to be around origin to provide better computation stability. |
| 132 | It is recommended to provide input vertices around the origin, otherwise use this flag to improve |
| 133 | numerical stability. |
| 134 | \note Is used only with eCOMPUTE_CONVEX flag. |
| 135 | */ |
| 136 | eSHIFT_VERTICES = (1 << 8) |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | \brief collection of set bits defined in PxConvexFlag. |
| 142 | |
| 143 | @see PxConvexFlag |
| 144 | */ |
| 145 | typedef PxFlags<PxConvexFlag::Enum,PxU16> PxConvexFlags; |
| 146 | PX_FLAGS_OPERATORS(PxConvexFlag::Enum,PxU16) |
| 147 | |
| 148 | /** |
| 149 | \brief Descriptor class for #PxConvexMesh. |
| 150 | \note The number of vertices and the number of convex polygons in a cooked convex mesh is limited to 256. |
| 151 | |
| 152 | @see PxConvexMesh PxConvexMeshGeometry PxShape PxPhysics.createConvexMesh() |
| 153 | |
| 154 | */ |
| 155 | class PxConvexMeshDesc |
| 156 | { |
| 157 | public: |
| 158 | |
| 159 | /** |
| 160 | \brief Vertex positions data in PxBoundedData format. |
| 161 | |
| 162 | <b>Default:</b> NULL |
| 163 | */ |
| 164 | PxBoundedData points; |
| 165 | |
| 166 | /** |
| 167 | \brief Polygons data in PxBoundedData format. |
| 168 | <p>Pointer to first polygon. </p> |
| 169 | |
| 170 | <b>Default:</b> NULL |
| 171 | |
| 172 | @see PxHullPolygon |
| 173 | */ |
| 174 | PxBoundedData polygons; |
| 175 | |
| 176 | /** |
| 177 | \brief Polygon indices data in PxBoundedData format. |
| 178 | <p>Pointer to first index.</p> |
| 179 | |
| 180 | <b>Default:</b> NULL |
| 181 | |
| 182 | <p>This is declared as a void pointer because it is actually either an PxU16 or a PxU32 pointer.</p> |
| 183 | |
| 184 | @see PxHullPolygon PxConvexFlag::e16_BIT_INDICES |
| 185 | */ |
| 186 | PxBoundedData indices; |
| 187 | |
| 188 | /** |
| 189 | \brief Flags bits, combined from values of the enum ::PxConvexFlag |
| 190 | |
| 191 | <b>Default:</b> 0 |
| 192 | */ |
| 193 | PxConvexFlags flags; |
| 194 | |
| 195 | /** |
| 196 | \brief Limits the number of vertices of the result convex mesh. Hard maximum limit is 256 |
| 197 | and minimum limit is 4 if PxConvexFlag::ePLANE_SHIFTING is used, otherwise the minimum |
| 198 | limit is 8. |
| 199 | |
| 200 | \note Vertex limit is only used when PxConvexFlag::eCOMPUTE_CONVEX is specified. |
| 201 | \note The please see PxConvexFlag::ePLANE_SHIFTING for algorithm explanation |
| 202 | |
| 203 | @see PxConvexFlag::ePLANE_SHIFTING |
| 204 | |
| 205 | <b>Range:</b> [4, 255]<br> |
| 206 | <b>Default:</b> 255 |
| 207 | */ |
| 208 | PxU16 vertexLimit; |
| 209 | |
| 210 | /** |
| 211 | \brief Maximum number of vertices after quantization. The quantization is done during the vertex cleaning phase. |
| 212 | The quantization is applied when PxConvexFlag::eQUANTIZE_INPUT is specified. |
| 213 | |
| 214 | @see PxConvexFlag::eQUANTIZE_INPUT |
| 215 | |
| 216 | <b>Range:</b> [4, 65535]<br> |
| 217 | <b>Default:</b> 255 |
| 218 | */ |
| 219 | PxU16 quantizedCount; |
| 220 | |
| 221 | /** |
| 222 | \brief constructor sets to default. |
| 223 | */ |
| 224 | PX_INLINE PxConvexMeshDesc(); |
| 225 | /** |
| 226 | \brief (re)sets the structure to the default. |
| 227 | */ |
| 228 | PX_INLINE void setToDefault(); |
| 229 | /** |
| 230 | \brief Returns true if the descriptor is valid. |
| 231 | |
| 232 | \return True if the current settings are valid |
| 233 | */ |
| 234 | PX_INLINE bool isValid() const; |
| 235 | }; |
| 236 | |
| 237 | PX_INLINE PxConvexMeshDesc::PxConvexMeshDesc() //constructor sets to default |
| 238 | : vertexLimit(255), quantizedCount(255) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | PX_INLINE void PxConvexMeshDesc::setToDefault() |
| 243 | { |
| 244 | *this = PxConvexMeshDesc(); |
| 245 | } |
| 246 | |
| 247 | PX_INLINE bool PxConvexMeshDesc::isValid() const |
| 248 | { |
| 249 | // Check geometry |
| 250 | if(points.count < 3 || //at least 1 trig's worth of points |
| 251 | (points.count > 0xffff && flags & PxConvexFlag::e16_BIT_INDICES)) |
| 252 | return false; |
| 253 | if(!points.data) |
| 254 | return false; |
| 255 | if(points.stride < sizeof(PxVec3)) //should be at least one point's worth of data |
| 256 | return false; |
| 257 | if (quantizedCount < 4) |
| 258 | return false; |
| 259 | |
| 260 | // Check topology |
| 261 | if(polygons.data) |
| 262 | { |
| 263 | if(polygons.count < 4) // we require 2 neighbors for each vertex - 4 polygons at least |
| 264 | return false; |
| 265 | |
| 266 | if(!indices.data) // indices must be provided together with polygons |
| 267 | return false; |
| 268 | |
| 269 | PxU32 limit = (flags & PxConvexFlag::e16_BIT_INDICES) ? sizeof(PxU16) : sizeof(PxU32); |
| 270 | if(indices.stride < limit) |
| 271 | return false; |
| 272 | |
| 273 | limit = sizeof(PxHullPolygon); |
| 274 | if(polygons.stride < limit) |
| 275 | return false; |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | // We can compute the hull from the vertices |
| 280 | if(!(flags & PxConvexFlag::eCOMPUTE_CONVEX)) |
| 281 | return false; // If the mesh is convex and we're not allowed to compute the hull, |
| 282 | // you have to provide it completely (geometry & topology). |
| 283 | } |
| 284 | |
| 285 | if((flags & PxConvexFlag::ePLANE_SHIFTING) && vertexLimit < 4) |
| 286 | { |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | if (!(flags & PxConvexFlag::ePLANE_SHIFTING) && vertexLimit < 8) |
| 291 | { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | if(vertexLimit > 256) |
| 296 | { |
| 297 | return false; |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | #if !PX_DOXYGEN |
| 303 | } // namespace physx |
| 304 | #endif |
| 305 | |
| 306 | /** @} */ |
| 307 | #endif |
| 308 | |