Blender V2.61 - r43446

btTriangleInfoMap.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2010 Erwin Coumans  http://bulletphysics.org
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #ifndef _BT_TRIANGLE_INFO_MAP_H
00017 #define _BT_TRIANGLE_INFO_MAP_H
00018 
00019 
00020 #include "LinearMath/btHashMap.h"
00021 #include "LinearMath/btSerializer.h"
00022 
00023 
00025 #define TRI_INFO_V0V1_CONVEX 1
00026 #define TRI_INFO_V1V2_CONVEX 2
00027 #define TRI_INFO_V2V0_CONVEX 4
00028 
00029 #define TRI_INFO_V0V1_SWAP_NORMALB 8
00030 #define TRI_INFO_V1V2_SWAP_NORMALB 16
00031 #define TRI_INFO_V2V0_SWAP_NORMALB 32
00032 
00033 
00036 struct  btTriangleInfo
00037 {
00038     btTriangleInfo()
00039     {
00040         m_edgeV0V1Angle = SIMD_2_PI;
00041         m_edgeV1V2Angle = SIMD_2_PI;
00042         m_edgeV2V0Angle = SIMD_2_PI;
00043         m_flags=0;
00044     }
00045 
00046     int         m_flags;
00047 
00048     btScalar    m_edgeV0V1Angle;
00049     btScalar    m_edgeV1V2Angle;
00050     btScalar    m_edgeV2V0Angle;
00051 
00052 };
00053 
00054 typedef btHashMap<btHashInt,btTriangleInfo> btInternalTriangleInfoMap;
00055 
00056 
00058 struct  btTriangleInfoMap : public btInternalTriangleInfoMap
00059 {
00060     btScalar    m_convexEpsilon;
00061     btScalar    m_planarEpsilon; 
00062     btScalar    m_equalVertexThreshold; 
00063     btScalar    m_edgeDistanceThreshold; 
00064     btScalar    m_zeroAreaThreshold; 
00065     
00066     
00067     btTriangleInfoMap()
00068     {
00069         m_convexEpsilon = 0.00f;
00070         m_planarEpsilon = 0.0001f;
00071         m_equalVertexThreshold = btScalar(0.0001)*btScalar(0.0001);
00072         m_edgeDistanceThreshold = btScalar(0.1);
00073         m_zeroAreaThreshold = btScalar(0.0001)*btScalar(0.0001);
00074     }
00075     virtual ~btTriangleInfoMap() {}
00076 
00077     virtual int calculateSerializeBufferSize() const;
00078 
00080     virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
00081 
00082     void    deSerialize(struct btTriangleInfoMapData& data);
00083 
00084 };
00085 
00086 struct  btTriangleInfoData
00087 {
00088     int         m_flags;
00089     float   m_edgeV0V1Angle;
00090     float   m_edgeV1V2Angle;
00091     float   m_edgeV2V0Angle;
00092 };
00093 
00094 struct  btTriangleInfoMapData
00095 {
00096     int                 *m_hashTablePtr;
00097     int                 *m_nextPtr;
00098     btTriangleInfoData  *m_valueArrayPtr;
00099     int                 *m_keyArrayPtr;
00100 
00101     float   m_convexEpsilon;
00102     float   m_planarEpsilon;
00103     float   m_equalVertexThreshold; 
00104     float   m_edgeDistanceThreshold;
00105     float   m_zeroAreaThreshold;
00106 
00107     int     m_nextSize;
00108     int     m_hashTableSize;
00109     int     m_numValues;
00110     int     m_numKeys;
00111     char    m_padding[4];
00112 };
00113 
00114 SIMD_FORCE_INLINE   int btTriangleInfoMap::calculateSerializeBufferSize() const
00115 {
00116     return sizeof(btTriangleInfoMapData);
00117 }
00118 
00120 SIMD_FORCE_INLINE   const char* btTriangleInfoMap::serialize(void* dataBuffer, btSerializer* serializer) const
00121 {
00122     btTriangleInfoMapData* tmapData = (btTriangleInfoMapData*) dataBuffer;
00123     tmapData->m_convexEpsilon = m_convexEpsilon;
00124     tmapData->m_planarEpsilon = m_planarEpsilon;
00125     tmapData->m_equalVertexThreshold = m_equalVertexThreshold;
00126     tmapData->m_edgeDistanceThreshold = m_edgeDistanceThreshold;
00127     tmapData->m_zeroAreaThreshold = m_zeroAreaThreshold;
00128     
00129     tmapData->m_hashTableSize = m_hashTable.size();
00130 
00131     tmapData->m_hashTablePtr = tmapData->m_hashTableSize ? (int*)serializer->getUniquePointer((void*)&m_hashTable[0]) : 0;
00132     if (tmapData->m_hashTablePtr)
00133     { 
00134         //serialize an int buffer
00135         int sz = sizeof(int);
00136         int numElem = tmapData->m_hashTableSize;
00137         btChunk* chunk = serializer->allocate(sz,numElem);
00138         int* memPtr = (int*)chunk->m_oldPtr;
00139         for (int i=0;i<numElem;i++,memPtr++)
00140         {
00141             *memPtr = m_hashTable[i];
00142         }
00143         serializer->finalizeChunk(chunk,"int",BT_ARRAY_CODE,(void*)&m_hashTable[0]);
00144 
00145     }
00146 
00147     tmapData->m_nextSize = m_next.size();
00148     tmapData->m_nextPtr = tmapData->m_nextSize? (int*)serializer->getUniquePointer((void*)&m_next[0]): 0;
00149     if (tmapData->m_nextPtr)
00150     {
00151         int sz = sizeof(int);
00152         int numElem = tmapData->m_nextSize;
00153         btChunk* chunk = serializer->allocate(sz,numElem);
00154         int* memPtr = (int*)chunk->m_oldPtr;
00155         for (int i=0;i<numElem;i++,memPtr++)
00156         {
00157             *memPtr = m_next[i];
00158         }
00159         serializer->finalizeChunk(chunk,"int",BT_ARRAY_CODE,(void*)&m_next[0]);
00160     }
00161     
00162     tmapData->m_numValues = m_valueArray.size();
00163     tmapData->m_valueArrayPtr = tmapData->m_numValues ? (btTriangleInfoData*)serializer->getUniquePointer((void*)&m_valueArray[0]): 0;
00164     if (tmapData->m_valueArrayPtr)
00165     {
00166         int sz = sizeof(btTriangleInfoData);
00167         int numElem = tmapData->m_numValues;
00168         btChunk* chunk = serializer->allocate(sz,numElem);
00169         btTriangleInfoData* memPtr = (btTriangleInfoData*)chunk->m_oldPtr;
00170         for (int i=0;i<numElem;i++,memPtr++)
00171         {
00172             memPtr->m_edgeV0V1Angle = m_valueArray[i].m_edgeV0V1Angle;
00173             memPtr->m_edgeV1V2Angle = m_valueArray[i].m_edgeV1V2Angle;
00174             memPtr->m_edgeV2V0Angle = m_valueArray[i].m_edgeV2V0Angle;
00175             memPtr->m_flags = m_valueArray[i].m_flags;
00176         }
00177         serializer->finalizeChunk(chunk,"btTriangleInfoData",BT_ARRAY_CODE,(void*) &m_valueArray[0]);
00178     }
00179     
00180     tmapData->m_numKeys = m_keyArray.size();
00181     tmapData->m_keyArrayPtr = tmapData->m_numKeys ? (int*)serializer->getUniquePointer((void*)&m_keyArray[0]) : 0;
00182     if (tmapData->m_keyArrayPtr)
00183     {
00184         int sz = sizeof(int);
00185         int numElem = tmapData->m_numValues;
00186         btChunk* chunk = serializer->allocate(sz,numElem);
00187         int* memPtr = (int*)chunk->m_oldPtr;
00188         for (int i=0;i<numElem;i++,memPtr++)
00189         {
00190             *memPtr = m_keyArray[i].getUid1();
00191         }
00192         serializer->finalizeChunk(chunk,"int",BT_ARRAY_CODE,(void*) &m_keyArray[0]);
00193 
00194     }
00195     return "btTriangleInfoMapData";
00196 }
00197 
00198 
00199 
00201 SIMD_FORCE_INLINE   void    btTriangleInfoMap::deSerialize(btTriangleInfoMapData& tmapData )
00202 {
00203 
00204 
00205     m_convexEpsilon = tmapData.m_convexEpsilon;
00206     m_planarEpsilon = tmapData.m_planarEpsilon;
00207     m_equalVertexThreshold = tmapData.m_equalVertexThreshold;
00208     m_edgeDistanceThreshold = tmapData.m_edgeDistanceThreshold;
00209     m_zeroAreaThreshold = tmapData.m_zeroAreaThreshold;
00210     m_hashTable.resize(tmapData.m_hashTableSize);
00211     int i =0;
00212     for (i=0;i<tmapData.m_hashTableSize;i++)
00213     {
00214         m_hashTable[i] = tmapData.m_hashTablePtr[i];
00215     }
00216     m_next.resize(tmapData.m_nextSize);
00217     for (i=0;i<tmapData.m_nextSize;i++)
00218     {
00219         m_next[i] = tmapData.m_nextPtr[i];
00220     }
00221     m_valueArray.resize(tmapData.m_numValues);
00222     for (i=0;i<tmapData.m_numValues;i++)
00223     {
00224         m_valueArray[i].m_edgeV0V1Angle = tmapData.m_valueArrayPtr[i].m_edgeV0V1Angle;
00225         m_valueArray[i].m_edgeV1V2Angle = tmapData.m_valueArrayPtr[i].m_edgeV1V2Angle;
00226         m_valueArray[i].m_edgeV2V0Angle = tmapData.m_valueArrayPtr[i].m_edgeV2V0Angle;
00227         m_valueArray[i].m_flags = tmapData.m_valueArrayPtr[i].m_flags;
00228     }
00229     
00230     m_keyArray.resize(tmapData.m_numKeys,btHashInt(0));
00231     for (i=0;i<tmapData.m_numKeys;i++)
00232     {
00233         m_keyArray[i].setUid1(tmapData.m_keyArrayPtr[i]);
00234     }
00235 }
00236 
00237 
00238 #endif //_BT_TRIANGLE_INFO_MAP_H