Bullet Collision Detection & Physics Library: btTriangleInfoMap.h Source File

Bullet Collision Detection & Physics Library

btTriangleInfoMap.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2010 Erwin Coumans http://bulletphysics.org
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #ifndef _BT_TRIANGLE_INFO_MAP_H
17 #define _BT_TRIANGLE_INFO_MAP_H
18
19 #include "LinearMath/btHashMap.h"
20 #include "LinearMath/btSerializer.h"
21
23 #define TRI_INFO_V0V1_CONVEX 1
24 #define TRI_INFO_V1V2_CONVEX 2
25 #define TRI_INFO_V2V0_CONVEX 4
26
27 #define TRI_INFO_V0V1_SWAP_NORMALB 8
28 #define TRI_INFO_V1V2_SWAP_NORMALB 16
29 #define TRI_INFO_V2V0_SWAP_NORMALB 32
30
33 struct btTriangleInfo
34 {
35 btTriangleInfo()
36 {
37 m_edgeV0V1Angle = SIMD_2_PI;
38 m_edgeV1V2Angle = SIMD_2_PI;
39 m_edgeV2V0Angle = SIMD_2_PI;
40 m_flags = 0;
41 }
42
43 int m_flags;
44
45 btScalar m_edgeV0V1Angle;
46 btScalar m_edgeV1V2Angle;
47 btScalar m_edgeV2V0Angle;
48 };
49
50 typedef btHashMap<btHashInt, btTriangleInfo> btInternalTriangleInfoMap;
51
53 struct btTriangleInfoMap : public btInternalTriangleInfoMap
54 {
55 btScalar m_convexEpsilon;
56 btScalar m_planarEpsilon;
57 btScalar m_equalVertexThreshold;
58 btScalar m_edgeDistanceThreshold;
59 btScalar m_maxEdgeAngleThreshold; //ignore edges that connect triangles at an angle larger than this m_maxEdgeAngleThreshold
60 btScalar m_zeroAreaThreshold;
61
62 btTriangleInfoMap()
63 {
64 m_convexEpsilon = 0.00f;
65 m_planarEpsilon = 0.0001f;
66 m_equalVertexThreshold = btScalar(0.0001) * btScalar(0.0001);
67 m_edgeDistanceThreshold = btScalar(0.1);
68 m_zeroAreaThreshold = btScalar(0.0001) * btScalar(0.0001);
69 m_maxEdgeAngleThreshold = SIMD_2_PI;
70 }
71 virtual ~btTriangleInfoMap() {}
72
73 virtual int calculateSerializeBufferSize() const;
74
76 virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
77
78 void deSerialize(struct btTriangleInfoMapData& data);
79 };
80
81 // clang-format off
82
84 struct btTriangleInfoData
85 {
86 int m_flags;
87 float m_edgeV0V1Angle;
88 float m_edgeV1V2Angle;
89 float m_edgeV2V0Angle;
90 };
91
92 struct btTriangleInfoMapData
93 {
94 int *m_hashTablePtr;
95 int *m_nextPtr;
96 btTriangleInfoData *m_valueArrayPtr;
97 int *m_keyArrayPtr;
98
99 float m_convexEpsilon;
100 float m_planarEpsilon;
101 float m_equalVertexThreshold;
102 float m_edgeDistanceThreshold;
103 float m_zeroAreaThreshold;
104
105 int m_nextSize;
106 int m_hashTableSize;
107 int m_numValues;
108 int m_numKeys;
109 char m_padding[4];
110 };
111
112 // clang-format on
113
114 SIMD_FORCE_INLINE int btTriangleInfoMap::calculateSerializeBufferSize() const
115 {
116 return sizeof(btTriangleInfoMapData);
117 }
118
120 SIMD_FORCE_INLINE const char* btTriangleInfoMap::serialize(void* dataBuffer, btSerializer* serializer) const
121 {
122 btTriangleInfoMapData* tmapData = (btTriangleInfoMapData*)dataBuffer;
123 tmapData->m_convexEpsilon = (float)m_convexEpsilon;
124 tmapData->m_planarEpsilon = (float)m_planarEpsilon;
125 tmapData->m_equalVertexThreshold = (float)m_equalVertexThreshold;
126 tmapData->m_edgeDistanceThreshold = (float)m_edgeDistanceThreshold;
127 tmapData->m_zeroAreaThreshold = (float)m_zeroAreaThreshold;
128
129 tmapData->m_hashTableSize = m_hashTable.size();
130
131 tmapData->m_hashTablePtr = tmapData->m_hashTableSize ? (int*)serializer->getUniquePointer((void*)&m_hashTable[0]) : 0;
132 if (tmapData->m_hashTablePtr)
133 {
134 //serialize an int buffer
135 int sz = sizeof(int);
136 int numElem = tmapData->m_hashTableSize;
137 btChunk* chunk = serializer->allocate(sz, numElem);
138 int* memPtr = (int*)chunk->m_oldPtr;
139 for (int i = 0; i < numElem; i++, memPtr++)
140 {
141 *memPtr = m_hashTable[i];
142 }
143 serializer->finalizeChunk(chunk, "int", BT_ARRAY_CODE, (void*)&m_hashTable[0]);
144 }
145
146 tmapData->m_nextSize = m_next.size();
147 tmapData->m_nextPtr = tmapData->m_nextSize ? (int*)serializer->getUniquePointer((void*)&m_next[0]) : 0;
148 if (tmapData->m_nextPtr)
149 {
150 int sz = sizeof(int);
151 int numElem = tmapData->m_nextSize;
152 btChunk* chunk = serializer->allocate(sz, numElem);
153 int* memPtr = (int*)chunk->m_oldPtr;
154 for (int i = 0; i < numElem; i++, memPtr++)
155 {
156 *memPtr = m_next[i];
157 }
158 serializer->finalizeChunk(chunk, "int", BT_ARRAY_CODE, (void*)&m_next[0]);
159 }
160
161 tmapData->m_numValues = m_valueArray.size();
162 tmapData->m_valueArrayPtr = tmapData->m_numValues ? (btTriangleInfoData*)serializer->getUniquePointer((void*)&m_valueArray[0]) : 0;
163 if (tmapData->m_valueArrayPtr)
164 {
165 int sz = sizeof(btTriangleInfoData);
166 int numElem = tmapData->m_numValues;
167 btChunk* chunk = serializer->allocate(sz, numElem);
168 btTriangleInfoData* memPtr = (btTriangleInfoData*)chunk->m_oldPtr;
169 for (int i = 0; i < numElem; i++, memPtr++)
170 {
171 memPtr->m_edgeV0V1Angle = (float)m_valueArray[i].m_edgeV0V1Angle;
172 memPtr->m_edgeV1V2Angle = (float)m_valueArray[i].m_edgeV1V2Angle;
173 memPtr->m_edgeV2V0Angle = (float)m_valueArray[i].m_edgeV2V0Angle;
174 memPtr->m_flags = m_valueArray[i].m_flags;
175 }
176 serializer->finalizeChunk(chunk, "btTriangleInfoData", BT_ARRAY_CODE, (void*)&m_valueArray[0]);
177 }
178
179 tmapData->m_numKeys = m_keyArray.size();
180 tmapData->m_keyArrayPtr = tmapData->m_numKeys ? (int*)serializer->getUniquePointer((void*)&m_keyArray[0]) : 0;
181 if (tmapData->m_keyArrayPtr)
182 {
183 int sz = sizeof(int);
184 int numElem = tmapData->m_numValues;
185 btChunk* chunk = serializer->allocate(sz, numElem);
186 int* memPtr = (int*)chunk->m_oldPtr;
187 for (int i = 0; i < numElem; i++, memPtr++)
188 {
189 *memPtr = m_keyArray[i].getUid1();
190 }
191 serializer->finalizeChunk(chunk, "int", BT_ARRAY_CODE, (void*)&m_keyArray[0]);
192 }
193
194 // Fill padding with zeros to appease msan.
195 tmapData->m_padding[0] = 0;
196 tmapData->m_padding[1] = 0;
197 tmapData->m_padding[2] = 0;
198 tmapData->m_padding[3] = 0;
199
200 return "btTriangleInfoMapData";
201 }
202
204 SIMD_FORCE_INLINE void btTriangleInfoMap::deSerialize(btTriangleInfoMapData& tmapData)
205 {
206 m_convexEpsilon = tmapData.m_convexEpsilon;
207 m_planarEpsilon = tmapData.m_planarEpsilon;
208 m_equalVertexThreshold = tmapData.m_equalVertexThreshold;
209 m_edgeDistanceThreshold = tmapData.m_edgeDistanceThreshold;
210 m_zeroAreaThreshold = tmapData.m_zeroAreaThreshold;
211 m_hashTable.resize(tmapData.m_hashTableSize);
212 int i = 0;
213 for (i = 0; i < tmapData.m_hashTableSize; i++)
214 {
215 m_hashTable[i] = tmapData.m_hashTablePtr[i];
216 }
217 m_next.resize(tmapData.m_nextSize);
218 for (i = 0; i < tmapData.m_nextSize; i++)
219 {
220 m_next[i] = tmapData.m_nextPtr[i];
221 }
222 m_valueArray.resize(tmapData.m_numValues);
223 for (i = 0; i < tmapData.m_numValues; i++)
224 {
225 m_valueArray[i].m_edgeV0V1Angle = tmapData.m_valueArrayPtr[i].m_edgeV0V1Angle;
226 m_valueArray[i].m_edgeV1V2Angle = tmapData.m_valueArrayPtr[i].m_edgeV1V2Angle;
227 m_valueArray[i].m_edgeV2V0Angle = tmapData.m_valueArrayPtr[i].m_edgeV2V0Angle;
228 m_valueArray[i].m_flags = tmapData.m_valueArrayPtr[i].m_flags;
229 }
230
231 m_keyArray.resize(tmapData.m_numKeys, btHashInt(0));
232 for (i = 0; i < tmapData.m_numKeys; i++)
233 {
234 m_keyArray[i].setUid1(tmapData.m_keyArrayPtr[i]);
235 }
236 }
237
238 #endif //_BT_TRIANGLE_INFO_MAP_H
btTriangleInfoMapData::m_padding
char m_padding[4]
Definition: btTriangleInfoMap.h:109
btTriangleInfoMap::calculateSerializeBufferSize
virtual int calculateSerializeBufferSize() const
Definition: btTriangleInfoMap.h:114
btHashMap::m_hashTable
btAlignedObjectArray< int > m_hashTable
Definition: btHashMap.h:222
btTriangleInfo::m_edgeV1V2Angle
btScalar m_edgeV1V2Angle
Definition: btTriangleInfoMap.h:46
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btTriangleInfoMapData::m_convexEpsilon
float m_convexEpsilon
Definition: btTriangleInfoMap.h:99
btSerializer::getUniquePointer
virtual void * getUniquePointer(void *oldPtr)=0
btChunk
Definition: btSerializer.h:48
btInternalTriangleInfoMap
btHashMap< btHashInt, btTriangleInfo > btInternalTriangleInfoMap
Definition: btTriangleInfoMap.h:50
btTriangleInfoMap::m_zeroAreaThreshold
btScalar m_zeroAreaThreshold
Definition: btTriangleInfoMap.h:60
btTriangleInfoMap::m_convexEpsilon
btScalar m_convexEpsilon
Definition: btTriangleInfoMap.h:55
btTriangleInfoMapData::m_nextSize
int m_nextSize
Definition: btTriangleInfoMap.h:105
btTriangleInfoMapData::m_zeroAreaThreshold
float m_zeroAreaThreshold
Definition: btTriangleInfoMap.h:103
btTriangleInfoMap::deSerialize
void deSerialize(struct btTriangleInfoMapData &data)
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition: btTriangleInfoMap.h:204
btTriangleInfoMap::m_edgeDistanceThreshold
btScalar m_edgeDistanceThreshold
used to compute connectivity: if the distance between two vertices is smaller than m_equalVertexThres...
Definition: btTriangleInfoMap.h:58
btTriangleInfo::m_edgeV2V0Angle
btScalar m_edgeV2V0Angle
Definition: btTriangleInfoMap.h:47
btHashMap::m_valueArray
btAlignedObjectArray< Value > m_valueArray
Definition: btHashMap.h:225
btHashMap
The btHashMap template class implements a generic and lightweight hashmap.
Definition: btHashMap.h:220
btTriangleInfoMap::serialize
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition: btTriangleInfoMap.h:120
btTriangleInfoData::m_edgeV2V0Angle
float m_edgeV2V0Angle
Definition: btTriangleInfoMap.h:89
btTriangleInfoMapData::m_edgeDistanceThreshold
float m_edgeDistanceThreshold
Definition: btTriangleInfoMap.h:102
btTriangleInfoMapData::m_keyArrayPtr
int * m_keyArrayPtr
Definition: btTriangleInfoMap.h:97
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:203
btHashInt
Definition: btHashMap.h:65
btTriangleInfoMapData::m_numValues
int m_numValues
Definition: btTriangleInfoMap.h:107
btTriangleInfoMap
The btTriangleInfoMap stores edge angle information for some triangles. You can compute this informat...
Definition: btTriangleInfoMap.h:54
btTriangleInfoMapData::m_valueArrayPtr
btTriangleInfoData * m_valueArrayPtr
Definition: btTriangleInfoMap.h:96
btTriangleInfo::m_edgeV0V1Angle
btScalar m_edgeV0V1Angle
Definition: btTriangleInfoMap.h:45
btHashMap::m_keyArray
btAlignedObjectArray< Key > m_keyArray
Definition: btHashMap.h:226
btSerializer.h
btSerializer::finalizeChunk
virtual void finalizeChunk(btChunk *chunk, const char *structType, int chunkCode, void *oldPtr)=0
btTriangleInfoMapData::m_nextPtr
int * m_nextPtr
Definition: btTriangleInfoMap.h:95
btTriangleInfo::m_flags
int m_flags
Definition: btTriangleInfoMap.h:43
btTriangleInfoData::m_edgeV1V2Angle
float m_edgeV1V2Angle
Definition: btTriangleInfoMap.h:88
btChunk::m_oldPtr
void * m_oldPtr
Definition: btSerializer.h:52
btTriangleInfoMapData::m_planarEpsilon
float m_planarEpsilon
Definition: btTriangleInfoMap.h:100
btTriangleInfoData::m_edgeV0V1Angle
float m_edgeV0V1Angle
Definition: btTriangleInfoMap.h:87
btTriangleInfo
The btTriangleInfo structure stores information to adjust collision normals to avoid collisions again...
Definition: btTriangleInfoMap.h:34
btTriangleInfoMapData::m_numKeys
int m_numKeys
Definition: btTriangleInfoMap.h:108
btTriangleInfoMapData::m_equalVertexThreshold
float m_equalVertexThreshold
Definition: btTriangleInfoMap.h:101
btTriangleInfoMap::m_maxEdgeAngleThreshold
btScalar m_maxEdgeAngleThreshold
used to determine edge contacts: if the closest distance between a contact point and an edge is small...
Definition: btTriangleInfoMap.h:59
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
btHashMap.h
btSerializer
Definition: btSerializer.h:66
btTriangleInfoData
those fields have to be float and not btScalar for the serialization to work properly
Definition: btTriangleInfoMap.h:85
BT_ARRAY_CODE
#define BT_ARRAY_CODE
Definition: btSerializer.h:118
btTriangleInfoMapData::m_hashTablePtr
int * m_hashTablePtr
Definition: btTriangleInfoMap.h:94
btTriangleInfoData::m_flags
int m_flags
Definition: btTriangleInfoMap.h:86
btTriangleInfoMapData
Definition: btTriangleInfoMap.h:93
btTriangleInfo::btTriangleInfo
btTriangleInfo()
Definition: btTriangleInfoMap.h:35
btHashMap::m_next
btAlignedObjectArray< int > m_next
Definition: btHashMap.h:223
btTriangleInfoMap::m_planarEpsilon
btScalar m_planarEpsilon
used to determine if an edge or contact normal is convex, using the dot product
Definition: btTriangleInfoMap.h:56
SIMD_2_PI
#define SIMD_2_PI
Definition: btScalar.h:527
btTriangleInfoMap::~btTriangleInfoMap
virtual ~btTriangleInfoMap()
Definition: btTriangleInfoMap.h:71
btTriangleInfoMap::m_equalVertexThreshold
btScalar m_equalVertexThreshold
used to determine if a triangle edge is planar with zero angle
Definition: btTriangleInfoMap.h:57
btTriangleInfoMap::btTriangleInfoMap
btTriangleInfoMap()
used to determine if a triangle is degenerate (length squared of cross product of 2 triangle edges < ...
Definition: btTriangleInfoMap.h:62
btSerializer::allocate
virtual btChunk * allocate(size_t size, int numElements)=0
btTriangleInfoMapData::m_hashTableSize
int m_hashTableSize
Definition: btTriangleInfoMap.h:106
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142