1 | /* |
2 | * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved. |
3 | * |
4 | * NVIDIA CORPORATION and its licensors retain all intellectual property |
5 | * and proprietary rights in and to this software, related documentation |
6 | * and any modifications thereto. Any use, reproduction, disclosure or |
7 | * distribution of this software and related documentation without an express |
8 | * license agreement from NVIDIA CORPORATION is strictly prohibited. |
9 | */ |
10 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
11 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
12 | |
13 | |
14 | #ifndef PX_COLLISION_NXCONVEXMESHDESC |
15 | #define PX_COLLISION_NXCONVEXMESHDESC |
16 | /** \addtogroup cooking |
17 | @{ |
18 | */ |
19 | |
20 | #include "foundation/PxVec3.h" |
21 | #include "foundation/PxFlags.h" |
22 | #include "common/PxCoreUtilityTypes.h" |
23 | #include "geometry/PxConvexMesh.h" |
24 | |
25 | #ifndef PX_DOXYGEN |
26 | namespace physx |
27 | { |
28 | #endif |
29 | |
30 | /** |
31 | \brief Flags which describe the format and behavior of a convex mesh. |
32 | */ |
33 | struct PxConvexFlag |
34 | { |
35 | enum Enum |
36 | { |
37 | /** |
38 | \brief Used to flip the normals if the winding order is reversed. |
39 | |
40 | The PhysX libraries assume that the face normal of a triangle with vertices [a,b,c] can be computed as: |
41 | edge1 = b-a |
42 | edge2 = c-a |
43 | face_normal = edge1 x edge2. |
44 | |
45 | Note: this is the same as counterclockwise winding in a right handed graphics coordinate system. |
46 | |
47 | If that does not match the winding order for your triangles, raise this flag. |
48 | |
49 | Deprecated as triangles will not be accepted in future releases as input for convex mesh descriptor. |
50 | */ |
51 | PX_DEPRECATED eFLIPNORMALS = (1<<0), |
52 | |
53 | /** |
54 | Denotes the use of 16-bit vertex indices in PxConvexMeshDesc::triangles or PxConvexMeshDesc::polygons. |
55 | (otherwise, 32-bit indices are assumed) |
56 | @see #PxConvexMeshDesc.triangles |
57 | */ |
58 | e16_BIT_INDICES = (1<<1), |
59 | |
60 | /** |
61 | Automatically recomputes the hull from the vertices. If this flag is not set, you must provide the entire geometry manually. |
62 | */ |
63 | eCOMPUTE_CONVEX = (1<<2), |
64 | |
65 | /** |
66 | \brief Inflates the convex object according to skin width. If the convex hull computation fails, use this flag to increase robustness. |
67 | |
68 | \note This flag is only used in combination with eCOMPUTE_CONVEX. |
69 | |
70 | @see PxCookingParams |
71 | */ |
72 | eINFLATE_CONVEX = (1<<3), |
73 | |
74 | /** |
75 | \brief Checks and removes almost zero-area triangles during convex hull computation. |
76 | The rejected area size is specified in PxCookingParams::areaTestEpsilon |
77 | |
78 | \note This flag is only used in combination with eCOMPUTE_CONVEX. |
79 | |
80 | \note If this flag is used in combination with eINFLATE_CONVEX, the newly added triangles |
81 | by the inflation algorithm are not checked (size of the triangles depends on PxCooking::skinWidth). |
82 | |
83 | @see PxCookingParams PxCookingParams::areaTestEpsilon |
84 | */ |
85 | eCHECK_ZERO_AREA_TRIANGLES = (1<<4) |
86 | }; |
87 | }; |
88 | |
89 | /** |
90 | \brief collection of set bits defined in PxConvexFlag. |
91 | |
92 | @see PxConvexFlag |
93 | */ |
94 | typedef PxFlags<PxConvexFlag::Enum,PxU16> PxConvexFlags; |
95 | PX_FLAGS_OPERATORS(PxConvexFlag::Enum,PxU16) |
96 | |
97 | |
98 | typedef PxVec3 PxPoint; |
99 | |
100 | /** |
101 | \brief Descriptor class for #PxConvexMesh. |
102 | \note The number of vertices and the number of convex polygons in a cooked convex mesh is limited to 256. |
103 | |
104 | @see PxConvexMesh PxConvexMeshGeometry PxShape PxPhysics.createConvexMesh() |
105 | |
106 | */ |
107 | class PxConvexMeshDesc |
108 | { |
109 | public: |
110 | |
111 | /** |
112 | \brief Vertex positions data in PxBoundedData format. |
113 | |
114 | <b>Default:</b> NULL |
115 | */ |
116 | PxBoundedData points; |
117 | |
118 | /** |
119 | \deprecated |
120 | \brief Triangle indices data in PxBoundedData format. |
121 | <p><pre>These are triplets of 0 based indices: |
122 | vert0 vert1 vert2 |
123 | vert0 vert1 vert2 |
124 | vert0 vert1 vert2 |
125 | ...</pre></p> |
126 | |
127 | <p>Where vertex is either a 32 or 16 bit unsigned integer. There are numTriangles*3 indices.</p> |
128 | |
129 | <p>This function is deprecated in favor of creating hulls from polygons directly. |
130 | To obtain polygons from your triangles use computeHullPolygons.</p> |
131 | |
132 | <b>Default:</b> NULL |
133 | |
134 | @see PxConvexFlag::e16_BIT_INDICES |
135 | */ |
136 | PX_DEPRECATED PxBoundedData triangles; |
137 | |
138 | /** |
139 | \brief Polygons data in PxBoundedData format. |
140 | <p>Pointer to first polygon. </p> |
141 | |
142 | <b>Default:</b> NULL |
143 | |
144 | @see PxHullPolygon |
145 | */ |
146 | PxBoundedData polygons; |
147 | |
148 | /** |
149 | \brief Polygon indices data in PxBoundedData format. |
150 | <p>Pointer to first index.</p> |
151 | |
152 | <b>Default:</b> NULL |
153 | |
154 | <p>This is declared as a void pointer because it is actually either an PxU16 or a PxU32 pointer.</p> |
155 | |
156 | @see PxHullPolygon PxConvexFlag::e16_BIT_INDICES |
157 | */ |
158 | PxBoundedData indices; |
159 | |
160 | /** |
161 | \brief Flags bits, combined from values of the enum ::PxConvexFlag |
162 | |
163 | <b>Default:</b> 0 |
164 | */ |
165 | PxConvexFlags flags; |
166 | |
167 | /** |
168 | \brief Limits the number of vertices of the result convex mesh. Hard maximum limit is 256 and minimum limit is 4. |
169 | |
170 | Please note, that if a vertex limit is used together with the inflation flag, beveling sharp edges in the inflation code |
171 | may cause the limit to be exceeded. |
172 | |
173 | <b>Default:</b> 256 |
174 | */ |
175 | PxU16 vertexLimit; |
176 | |
177 | /** |
178 | \brief constructor sets to default. |
179 | */ |
180 | PX_INLINE PxConvexMeshDesc(); |
181 | /** |
182 | \brief (re)sets the structure to the default. |
183 | */ |
184 | PX_INLINE void setToDefault(); |
185 | /** |
186 | \brief Returns true if the descriptor is valid. |
187 | |
188 | \return True if the current settings are valid |
189 | */ |
190 | PX_INLINE bool isValid() const; |
191 | }; |
192 | |
193 | PX_INLINE PxConvexMeshDesc::PxConvexMeshDesc() //constructor sets to default |
194 | : vertexLimit(256) |
195 | { |
196 | } |
197 | |
198 | PX_INLINE void PxConvexMeshDesc::setToDefault() |
199 | { |
200 | *this = PxConvexMeshDesc(); |
201 | } |
202 | |
203 | PX_INLINE bool PxConvexMeshDesc::isValid() const |
204 | { |
205 | // Check geometry |
206 | if(points.count < 3 || //at least 1 trig's worth of points |
207 | (points.count > 0xffff && flags & PxConvexFlag::e16_BIT_INDICES)) |
208 | return false; |
209 | if(!points.data) |
210 | return false; |
211 | if(points.stride < sizeof(PxPoint)) //should be at least one point's worth of data |
212 | return false; |
213 | |
214 | // Check topology |
215 | // The triangles pointer is not mandatory: the vertex cloud is enough to define the convex hull. |
216 | if(triangles.data) |
217 | { |
218 | // Indexed mesh |
219 | if(triangles.count < 2) //some algos require at least 2 trigs |
220 | return false; |
221 | |
222 | PxU32 limit = (flags & PxConvexFlag::e16_BIT_INDICES) ? sizeof(PxU16)*3 : sizeof(PxU32)*3; |
223 | if(triangles.stride < limit) |
224 | return false; |
225 | } |
226 | else |
227 | { |
228 | if(polygons.data) |
229 | { |
230 | if(polygons.count < 4) // we require 2 neighbours for each vertex - 4 polygons at least |
231 | return false; |
232 | |
233 | if(!indices.data) // indices must be provided together with polygons |
234 | return false; |
235 | |
236 | PxU32 limit = (flags & PxConvexFlag::e16_BIT_INDICES) ? sizeof(PxU16) : sizeof(PxU32); |
237 | if(indices.stride < limit) |
238 | return false; |
239 | |
240 | limit = sizeof(PxHullPolygon); |
241 | if(polygons.stride < limit) |
242 | return false; |
243 | } |
244 | else |
245 | { |
246 | // We can compute the hull from the vertices |
247 | if(!(flags & PxConvexFlag::eCOMPUTE_CONVEX)) |
248 | return false; // If the mesh is convex and we're not allowed to compute the hull, |
249 | // you have to provide it completely (geometry & topology). |
250 | } |
251 | } |
252 | |
253 | if(vertexLimit < 4 || vertexLimit > 256) |
254 | { |
255 | return false; |
256 | } |
257 | return true; |
258 | } |
259 | |
260 | #ifndef PX_DOXYGEN |
261 | } // namespace physx |
262 | #endif |
263 | |
264 | /** @} */ |
265 | #endif |
266 | |