btSerializer.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_SERIALIZER_H
  14. #define BT_SERIALIZER_H
  15. #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
  16. #include "btHashMap.h"
  17. #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
  18. #include <memory.h>
  19. #endif
  20. #include <string.h>
  21. ///only the 32bit versions for now
  22. extern char sBulletDNAstr[];
  23. extern int sBulletDNAlen;
  24. extern char sBulletDNAstr64[];
  25. extern int sBulletDNAlen64;
  26. SIMD_FORCE_INLINE int btStrLen(const char* str)
  27. {
  28. if (!str)
  29. return(0);
  30. int len = 0;
  31. while (*str != 0)
  32. {
  33. str++;
  34. len++;
  35. }
  36. return len;
  37. }
  38. class btChunk
  39. {
  40. public:
  41. int m_chunkCode;
  42. int m_length;
  43. void *m_oldPtr;
  44. int m_dna_nr;
  45. int m_number;
  46. };
  47. enum btSerializationFlags
  48. {
  49. BT_SERIALIZE_NO_BVH = 1,
  50. BT_SERIALIZE_NO_TRIANGLEINFOMAP = 2,
  51. BT_SERIALIZE_NO_DUPLICATE_ASSERT = 4
  52. };
  53. class btSerializer
  54. {
  55. public:
  56. virtual ~btSerializer() {}
  57. virtual const unsigned char* getBufferPointer() const = 0;
  58. virtual int getCurrentBufferSize() const = 0;
  59. virtual btChunk* allocate(size_t size, int numElements) = 0;
  60. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)= 0;
  61. virtual void* findPointer(void* oldPtr) = 0;
  62. virtual void* getUniquePointer(void*oldPtr) = 0;
  63. virtual void startSerialization() = 0;
  64. virtual void finishSerialization() = 0;
  65. virtual const char* findNameForPointer(const void* ptr) const = 0;
  66. virtual void registerNameForPointer(const void* ptr, const char* name) = 0;
  67. virtual void serializeName(const char* ptr) = 0;
  68. virtual int getSerializationFlags() const = 0;
  69. virtual void setSerializationFlags(int flags) = 0;
  70. virtual int getNumChunks() const = 0;
  71. virtual const btChunk* getChunk(int chunkIndex) const = 0;
  72. };
  73. #define BT_HEADER_LENGTH 12
  74. #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
  75. # define BT_MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
  76. #else
  77. # define BT_MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
  78. #endif
  79. #define BT_SOFTBODY_CODE BT_MAKE_ID('S','B','D','Y')
  80. #define BT_COLLISIONOBJECT_CODE BT_MAKE_ID('C','O','B','J')
  81. #define BT_RIGIDBODY_CODE BT_MAKE_ID('R','B','D','Y')
  82. #define BT_CONSTRAINT_CODE BT_MAKE_ID('C','O','N','S')
  83. #define BT_BOXSHAPE_CODE BT_MAKE_ID('B','O','X','S')
  84. #define BT_QUANTIZED_BVH_CODE BT_MAKE_ID('Q','B','V','H')
  85. #define BT_TRIANLGE_INFO_MAP BT_MAKE_ID('T','M','A','P')
  86. #define BT_SHAPE_CODE BT_MAKE_ID('S','H','A','P')
  87. #define BT_ARRAY_CODE BT_MAKE_ID('A','R','A','Y')
  88. #define BT_SBMATERIAL_CODE BT_MAKE_ID('S','B','M','T')
  89. #define BT_SBNODE_CODE BT_MAKE_ID('S','B','N','D')
  90. #define BT_DYNAMICSWORLD_CODE BT_MAKE_ID('D','W','L','D')
  91. #define BT_DNA_CODE BT_MAKE_ID('D','N','A','1')
  92. struct btPointerUid
  93. {
  94. union
  95. {
  96. void* m_ptr;
  97. int m_uniqueIds[2];
  98. };
  99. };
  100. struct btBulletSerializedArrays
  101. {
  102. btBulletSerializedArrays()
  103. {
  104. }
  105. btAlignedObjectArray<struct btQuantizedBvhDoubleData*> m_bvhsDouble;
  106. btAlignedObjectArray<struct btQuantizedBvhFloatData*> m_bvhsFloat;
  107. btAlignedObjectArray<struct btCollisionShapeData*> m_colShapeData;
  108. btAlignedObjectArray<struct btDynamicsWorldDoubleData*> m_dynamicWorldInfoDataDouble;
  109. btAlignedObjectArray<struct btDynamicsWorldFloatData*> m_dynamicWorldInfoDataFloat;
  110. btAlignedObjectArray<struct btRigidBodyDoubleData*> m_rigidBodyDataDouble;
  111. btAlignedObjectArray<struct btRigidBodyFloatData*> m_rigidBodyDataFloat;
  112. btAlignedObjectArray<struct btCollisionObjectDoubleData*> m_collisionObjectDataDouble;
  113. btAlignedObjectArray<struct btCollisionObjectFloatData*> m_collisionObjectDataFloat;
  114. btAlignedObjectArray<struct btTypedConstraintFloatData*> m_constraintDataFloat;
  115. btAlignedObjectArray<struct btTypedConstraintDoubleData*> m_constraintDataDouble;
  116. btAlignedObjectArray<struct btTypedConstraintData*> m_constraintData;//for backwards compatibility
  117. btAlignedObjectArray<struct btSoftBodyFloatData*> m_softBodyFloatData;
  118. btAlignedObjectArray<struct btSoftBodyDoubleData*> m_softBodyDoubleData;
  119. };
  120. ///The btDefaultSerializer is the main Bullet serialization class.
  121. ///The constructor takes an optional argument for backwards compatibility, it is recommended to leave this empty/zero.
  122. class btDefaultSerializer : public btSerializer
  123. {
  124. protected:
  125. btAlignedObjectArray<char*> mTypes;
  126. btAlignedObjectArray<short*> mStructs;
  127. btAlignedObjectArray<short> mTlens;
  128. btHashMap<btHashInt, int> mStructReverse;
  129. btHashMap<btHashString,int> mTypeLookup;
  130. btHashMap<btHashPtr,void*> m_chunkP;
  131. btHashMap<btHashPtr,const char*> m_nameMap;
  132. btHashMap<btHashPtr,btPointerUid> m_uniquePointers;
  133. int m_uniqueIdGenerator;
  134. int m_totalSize;
  135. unsigned char* m_buffer;
  136. int m_currentSize;
  137. void* m_dna;
  138. int m_dnaLength;
  139. int m_serializationFlags;
  140. btAlignedObjectArray<btChunk*> m_chunkPtrs;
  141. protected:
  142. virtual void* findPointer(void* oldPtr)
  143. {
  144. void** ptr = m_chunkP.find(oldPtr);
  145. if (ptr && *ptr)
  146. return *ptr;
  147. return 0;
  148. }
  149. void writeDNA()
  150. {
  151. btChunk* dnaChunk = allocate(m_dnaLength,1);
  152. memcpy(dnaChunk->m_oldPtr,m_dna,m_dnaLength);
  153. finalizeChunk(dnaChunk,"DNA1",BT_DNA_CODE, m_dna);
  154. }
  155. int getReverseType(const char *type) const
  156. {
  157. btHashString key(type);
  158. const int* valuePtr = mTypeLookup.find(key);
  159. if (valuePtr)
  160. return *valuePtr;
  161. return -1;
  162. }
  163. void initDNA(const char* bdnaOrg,int dnalen)
  164. {
  165. ///was already initialized
  166. if (m_dna)
  167. return;
  168. int littleEndian= 1;
  169. littleEndian= ((char*)&littleEndian)[0];
  170. m_dna = btAlignedAlloc(dnalen,16);
  171. memcpy(m_dna,bdnaOrg,dnalen);
  172. m_dnaLength = dnalen;
  173. int *intPtr=0;
  174. short *shtPtr=0;
  175. char *cp = 0;int dataLen =0;
  176. intPtr = (int*)m_dna;
  177. /*
  178. SDNA (4 bytes) (magic number)
  179. NAME (4 bytes)
  180. <nr> (4 bytes) amount of names (int)
  181. <string>
  182. <string>
  183. */
  184. if (strncmp((const char*)m_dna, "SDNA", 4)==0)
  185. {
  186. // skip ++ NAME
  187. intPtr++; intPtr++;
  188. }
  189. // Parse names
  190. if (!littleEndian)
  191. *intPtr = btSwapEndian(*intPtr);
  192. dataLen = *intPtr;
  193. intPtr++;
  194. cp = (char*)intPtr;
  195. int i;
  196. for ( i=0; i<dataLen; i++)
  197. {
  198. while (*cp)cp++;
  199. cp++;
  200. }
  201. cp = btAlignPointer(cp,4);
  202. /*
  203. TYPE (4 bytes)
  204. <nr> amount of types (int)
  205. <string>
  206. <string>
  207. */
  208. intPtr = (int*)cp;
  209. btAssert(strncmp(cp, "TYPE", 4)==0); intPtr++;
  210. if (!littleEndian)
  211. *intPtr = btSwapEndian(*intPtr);
  212. dataLen = *intPtr;
  213. intPtr++;
  214. cp = (char*)intPtr;
  215. for (i=0; i<dataLen; i++)
  216. {
  217. mTypes.push_back(cp);
  218. while (*cp)cp++;
  219. cp++;
  220. }
  221. cp = btAlignPointer(cp,4);
  222. /*
  223. TLEN (4 bytes)
  224. <len> (short) the lengths of types
  225. <len>
  226. */
  227. // Parse type lens
  228. intPtr = (int*)cp;
  229. btAssert(strncmp(cp, "TLEN", 4)==0); intPtr++;
  230. dataLen = (int)mTypes.size();
  231. shtPtr = (short*)intPtr;
  232. for (i=0; i<dataLen; i++, shtPtr++)
  233. {
  234. if (!littleEndian)
  235. shtPtr[0] = btSwapEndian(shtPtr[0]);
  236. mTlens.push_back(shtPtr[0]);
  237. }
  238. if (dataLen & 1) shtPtr++;
  239. /*
  240. STRC (4 bytes)
  241. <nr> amount of structs (int)
  242. <typenr>
  243. <nr_of_elems>
  244. <typenr>
  245. <namenr>
  246. <typenr>
  247. <namenr>
  248. */
  249. intPtr = (int*)shtPtr;
  250. cp = (char*)intPtr;
  251. btAssert(strncmp(cp, "STRC", 4)==0); intPtr++;
  252. if (!littleEndian)
  253. *intPtr = btSwapEndian(*intPtr);
  254. dataLen = *intPtr ;
  255. intPtr++;
  256. shtPtr = (short*)intPtr;
  257. for (i=0; i<dataLen; i++)
  258. {
  259. mStructs.push_back (shtPtr);
  260. if (!littleEndian)
  261. {
  262. shtPtr[0]= btSwapEndian(shtPtr[0]);
  263. shtPtr[1]= btSwapEndian(shtPtr[1]);
  264. int len = shtPtr[1];
  265. shtPtr+= 2;
  266. for (int a=0; a<len; a++, shtPtr+=2)
  267. {
  268. shtPtr[0]= btSwapEndian(shtPtr[0]);
  269. shtPtr[1]= btSwapEndian(shtPtr[1]);
  270. }
  271. } else
  272. {
  273. shtPtr+= (2*shtPtr[1])+2;
  274. }
  275. }
  276. // build reverse lookups
  277. for (i=0; i<(int)mStructs.size(); i++)
  278. {
  279. short *strc = mStructs.at(i);
  280. mStructReverse.insert(strc[0], i);
  281. mTypeLookup.insert(btHashString(mTypes[strc[0]]),i);
  282. }
  283. }
  284. public:
  285. btDefaultSerializer(int totalSize=0)
  286. :m_totalSize(totalSize),
  287. m_currentSize(0),
  288. m_dna(0),
  289. m_dnaLength(0),
  290. m_serializationFlags(0)
  291. {
  292. m_buffer = m_totalSize?(unsigned char*)btAlignedAlloc(totalSize,16):0;
  293. const bool VOID_IS_8 = ((sizeof(void*)==8));
  294. #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  295. if (VOID_IS_8)
  296. {
  297. #if _WIN64
  298. initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
  299. #else
  300. btAssert(0);
  301. #endif
  302. } else
  303. {
  304. #ifndef _WIN64
  305. initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
  306. #else
  307. btAssert(0);
  308. #endif
  309. }
  310. #else //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  311. if (VOID_IS_8)
  312. {
  313. initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
  314. } else
  315. {
  316. initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
  317. }
  318. #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  319. }
  320. virtual ~btDefaultSerializer()
  321. {
  322. if (m_buffer)
  323. btAlignedFree(m_buffer);
  324. if (m_dna)
  325. btAlignedFree(m_dna);
  326. }
  327. void writeHeader(unsigned char* buffer) const
  328. {
  329. #ifdef BT_USE_DOUBLE_PRECISION
  330. memcpy(buffer, "BULLETd", 7);
  331. #else
  332. memcpy(buffer, "BULLETf", 7);
  333. #endif //BT_USE_DOUBLE_PRECISION
  334. int littleEndian= 1;
  335. littleEndian= ((char*)&littleEndian)[0];
  336. if (sizeof(void*)==8)
  337. {
  338. buffer[7] = '-';
  339. } else
  340. {
  341. buffer[7] = '_';
  342. }
  343. if (littleEndian)
  344. {
  345. buffer[8]='v';
  346. } else
  347. {
  348. buffer[8]='V';
  349. }
  350. buffer[9] = '2';
  351. buffer[10] = '8';
  352. buffer[11] = '3';
  353. }
  354. virtual void startSerialization()
  355. {
  356. m_uniqueIdGenerator= 1;
  357. if (m_totalSize)
  358. {
  359. unsigned char* buffer = internalAlloc(BT_HEADER_LENGTH);
  360. writeHeader(buffer);
  361. }
  362. }
  363. virtual void finishSerialization()
  364. {
  365. writeDNA();
  366. //if we didn't pre-allocate a buffer, we need to create a contiguous buffer now
  367. int mysize = 0;
  368. if (!m_totalSize)
  369. {
  370. if (m_buffer)
  371. btAlignedFree(m_buffer);
  372. m_currentSize += BT_HEADER_LENGTH;
  373. m_buffer = (unsigned char*)btAlignedAlloc(m_currentSize,16);
  374. unsigned char* currentPtr = m_buffer;
  375. writeHeader(m_buffer);
  376. currentPtr += BT_HEADER_LENGTH;
  377. mysize+=BT_HEADER_LENGTH;
  378. for (int i=0;i< m_chunkPtrs.size();i++)
  379. {
  380. int curLength = sizeof(btChunk)+m_chunkPtrs[i]->m_length;
  381. memcpy(currentPtr,m_chunkPtrs[i], curLength);
  382. btAlignedFree(m_chunkPtrs[i]);
  383. currentPtr+=curLength;
  384. mysize+=curLength;
  385. }
  386. }
  387. mTypes.clear();
  388. mStructs.clear();
  389. mTlens.clear();
  390. mStructReverse.clear();
  391. mTypeLookup.clear();
  392. m_chunkP.clear();
  393. m_nameMap.clear();
  394. m_uniquePointers.clear();
  395. m_chunkPtrs.clear();
  396. }
  397. virtual void* getUniquePointer(void*oldPtr)
  398. {
  399. if (!oldPtr)
  400. return 0;
  401. btPointerUid* uptr = (btPointerUid*)m_uniquePointers.find(oldPtr);
  402. if (uptr)
  403. {
  404. return uptr->m_ptr;
  405. }
  406. m_uniqueIdGenerator++;
  407. btPointerUid uid;
  408. uid.m_uniqueIds[0] = m_uniqueIdGenerator;
  409. uid.m_uniqueIds[1] = m_uniqueIdGenerator;
  410. m_uniquePointers.insert(oldPtr,uid);
  411. return uid.m_ptr;
  412. }
  413. virtual const unsigned char* getBufferPointer() const
  414. {
  415. return m_buffer;
  416. }
  417. virtual int getCurrentBufferSize() const
  418. {
  419. return m_currentSize;
  420. }
  421. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
  422. {
  423. if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
  424. {
  425. btAssert(!findPointer(oldPtr));
  426. }
  427. chunk->m_dna_nr = getReverseType(structType);
  428. chunk->m_chunkCode = chunkCode;
  429. void* uniquePtr = getUniquePointer(oldPtr);
  430. m_chunkP.insert(oldPtr,uniquePtr);//chunk->m_oldPtr);
  431. chunk->m_oldPtr = uniquePtr;//oldPtr;
  432. }
  433. virtual unsigned char* internalAlloc(size_t size)
  434. {
  435. unsigned char* ptr = 0;
  436. if (m_totalSize)
  437. {
  438. ptr = m_buffer+m_currentSize;
  439. m_currentSize += int(size);
  440. btAssert(m_currentSize<m_totalSize);
  441. } else
  442. {
  443. ptr = (unsigned char*)btAlignedAlloc(size,16);
  444. m_currentSize += int(size);
  445. }
  446. return ptr;
  447. }
  448. virtual btChunk* allocate(size_t size, int numElements)
  449. {
  450. unsigned char* ptr = internalAlloc(int(size)*numElements+sizeof(btChunk));
  451. unsigned char* data = ptr + sizeof(btChunk);
  452. btChunk* chunk = (btChunk*)ptr;
  453. chunk->m_chunkCode = 0;
  454. chunk->m_oldPtr = data;
  455. chunk->m_length = int(size)*numElements;
  456. chunk->m_number = numElements;
  457. m_chunkPtrs.push_back(chunk);
  458. return chunk;
  459. }
  460. virtual const char* findNameForPointer(const void* ptr) const
  461. {
  462. const char*const * namePtr = m_nameMap.find(ptr);
  463. if (namePtr && *namePtr)
  464. return *namePtr;
  465. return 0;
  466. }
  467. virtual void registerNameForPointer(const void* ptr, const char* name)
  468. {
  469. m_nameMap.insert(ptr,name);
  470. }
  471. virtual void serializeName(const char* name)
  472. {
  473. if (name)
  474. {
  475. //don't serialize name twice
  476. if (findPointer((void*)name))
  477. return;
  478. int len = btStrLen(name);
  479. if (len)
  480. {
  481. int newLen = len+1;
  482. int padding = ((newLen+3)&~3)-newLen;
  483. newLen += padding;
  484. //serialize name string now
  485. btChunk* chunk = allocate(sizeof(char),newLen);
  486. char* destinationName = (char*)chunk->m_oldPtr;
  487. for (int i=0;i<len;i++)
  488. {
  489. destinationName[i] = name[i];
  490. }
  491. destinationName[len] = 0;
  492. finalizeChunk(chunk,"char",BT_ARRAY_CODE,(void*)name);
  493. }
  494. }
  495. }
  496. virtual int getSerializationFlags() const
  497. {
  498. return m_serializationFlags;
  499. }
  500. virtual void setSerializationFlags(int flags)
  501. {
  502. m_serializationFlags = flags;
  503. }
  504. int getNumChunks() const
  505. {
  506. return m_chunkPtrs.size();
  507. }
  508. const btChunk* getChunk(int chunkIndex) const
  509. {
  510. return m_chunkPtrs[chunkIndex];
  511. }
  512. };
  513. ///In general it is best to use btDefaultSerializer,
  514. ///in particular when writing the data to disk or sending it over the network.
  515. ///The btInMemorySerializer is experimental and only suitable in a few cases.
  516. ///The btInMemorySerializer takes a shortcut and can be useful to create a deep-copy
  517. ///of objects. There will be a demo on how to use the btInMemorySerializer.
  518. #ifdef ENABLE_INMEMORY_SERIALIZER
  519. struct btInMemorySerializer : public btDefaultSerializer
  520. {
  521. btHashMap<btHashPtr,btChunk*> m_uid2ChunkPtr;
  522. btHashMap<btHashPtr,void*> m_orgPtr2UniqueDataPtr;
  523. btHashMap<btHashString,const void*> m_names2Ptr;
  524. btHashMap<btHashPtr,void*> m_skipPointers;
  525. btBulletSerializedArrays m_arrays;
  526. virtual void startSerialization()
  527. {
  528. m_uid2ChunkPtr.clear();
  529. //todo: m_arrays.clear();
  530. btDefaultSerializer::startSerialization();
  531. }
  532. btChunk* findChunkFromUniquePointer(void* uniquePointer)
  533. {
  534. btChunk** chkPtr = m_uid2ChunkPtr[uniquePointer];
  535. if (chkPtr)
  536. {
  537. return *chkPtr;
  538. }
  539. return 0;
  540. }
  541. virtual void registerNameForPointer(const void* ptr, const char* name)
  542. {
  543. btDefaultSerializer::registerNameForPointer(ptr,name);
  544. m_names2Ptr.insert(name,ptr);
  545. }
  546. virtual void finishSerialization()
  547. {
  548. }
  549. virtual void* getUniquePointer(void*oldPtr)
  550. {
  551. if (oldPtr==0)
  552. return 0;
  553. // void* uniquePtr = getUniquePointer(oldPtr);
  554. btChunk* chunk = findChunkFromUniquePointer(oldPtr);
  555. if (chunk)
  556. {
  557. return chunk->m_oldPtr;
  558. } else
  559. {
  560. const char* n = (const char*) oldPtr;
  561. const void** ptr = m_names2Ptr[n];
  562. if (ptr)
  563. {
  564. return oldPtr;
  565. } else
  566. {
  567. void** ptr2 = m_skipPointers[oldPtr];
  568. if (ptr2)
  569. {
  570. return 0;
  571. } else
  572. {
  573. //If this assert hit, serialization happened in the wrong order
  574. // 'getUniquePointer'
  575. btAssert(0);
  576. }
  577. }
  578. return 0;
  579. }
  580. return oldPtr;
  581. }
  582. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
  583. {
  584. if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
  585. {
  586. btAssert(!findPointer(oldPtr));
  587. }
  588. chunk->m_dna_nr = getReverseType(structType);
  589. chunk->m_chunkCode = chunkCode;
  590. //void* uniquePtr = getUniquePointer(oldPtr);
  591. m_chunkP.insert(oldPtr,oldPtr);//chunk->m_oldPtr);
  592. // chunk->m_oldPtr = uniquePtr;//oldPtr;
  593. void* uid = findPointer(oldPtr);
  594. m_uid2ChunkPtr.insert(uid,chunk);
  595. switch (chunk->m_chunkCode)
  596. {
  597. case BT_SOFTBODY_CODE:
  598. {
  599. #ifdef BT_USE_DOUBLE_PRECISION
  600. m_arrays.m_softBodyDoubleData.push_back((btSoftBodyDoubleData*) chunk->m_oldPtr);
  601. #else
  602. m_arrays.m_softBodyFloatData.push_back((btSoftBodyFloatData*) chunk->m_oldPtr);
  603. #endif
  604. break;
  605. }
  606. case BT_COLLISIONOBJECT_CODE:
  607. {
  608. #ifdef BT_USE_DOUBLE_PRECISION
  609. m_arrays.m_collisionObjectDataDouble.push_back((btCollisionObjectDoubleData*)chunk->m_oldPtr);
  610. #else//BT_USE_DOUBLE_PRECISION
  611. m_arrays.m_collisionObjectDataFloat.push_back((btCollisionObjectFloatData*)chunk->m_oldPtr);
  612. #endif //BT_USE_DOUBLE_PRECISION
  613. break;
  614. }
  615. case BT_RIGIDBODY_CODE:
  616. {
  617. #ifdef BT_USE_DOUBLE_PRECISION
  618. m_arrays.m_rigidBodyDataDouble.push_back((btRigidBodyDoubleData*)chunk->m_oldPtr);
  619. #else
  620. m_arrays.m_rigidBodyDataFloat.push_back((btRigidBodyFloatData*)chunk->m_oldPtr);
  621. #endif//BT_USE_DOUBLE_PRECISION
  622. break;
  623. };
  624. case BT_CONSTRAINT_CODE:
  625. {
  626. #ifdef BT_USE_DOUBLE_PRECISION
  627. m_arrays.m_constraintDataDouble.push_back((btTypedConstraintDoubleData*)chunk->m_oldPtr);
  628. #else
  629. m_arrays.m_constraintDataFloat.push_back((btTypedConstraintFloatData*)chunk->m_oldPtr);
  630. #endif
  631. break;
  632. }
  633. case BT_QUANTIZED_BVH_CODE:
  634. {
  635. #ifdef BT_USE_DOUBLE_PRECISION
  636. m_arrays.m_bvhsDouble.push_back((btQuantizedBvhDoubleData*) chunk->m_oldPtr);
  637. #else
  638. m_arrays.m_bvhsFloat.push_back((btQuantizedBvhFloatData*) chunk->m_oldPtr);
  639. #endif
  640. break;
  641. }
  642. case BT_SHAPE_CODE:
  643. {
  644. btCollisionShapeData* shapeData = (btCollisionShapeData*) chunk->m_oldPtr;
  645. m_arrays.m_colShapeData.push_back(shapeData);
  646. break;
  647. }
  648. case BT_TRIANLGE_INFO_MAP:
  649. case BT_ARRAY_CODE:
  650. case BT_SBMATERIAL_CODE:
  651. case BT_SBNODE_CODE:
  652. case BT_DYNAMICSWORLD_CODE:
  653. case BT_DNA_CODE:
  654. {
  655. break;
  656. }
  657. default:
  658. {
  659. }
  660. };
  661. }
  662. int getNumChunks() const
  663. {
  664. return m_uid2ChunkPtr.size();
  665. }
  666. const btChunk* getChunk(int chunkIndex) const
  667. {
  668. return *m_uid2ChunkPtr.getAtIndex(chunkIndex);
  669. }
  670. };
  671. #endif //ENABLE_INMEMORY_SERIALIZER
  672. #endif //BT_SERIALIZER_H