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

Bullet Collision Detection & Physics Library

btSerializer.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 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_SERIALIZER_H
17 #define BT_SERIALIZER_H
18
19 #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
20 #include "btHashMap.h"
21
22 #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
23 #include <memory.h>
24 #endif
25 #include <string.h>
26
27
28
29
30 extern char sBulletDNAstr[];
31 extern int sBulletDNAlen;
32 extern char sBulletDNAstr64[];
33 extern int sBulletDNAlen64;
34
35 SIMD_FORCE_INLINE int btStrLen(const char* str)
36 {
37 if (!str)
38 return(0);
39 int len = 0;
40
41 while (*str != 0)
42 {
43 str++;
44 len++;
45 }
46
47 return len;
48 }
49
50
51 class btChunk
52 {
53 public:
54 int m_chunkCode;
55 int m_length;
56 void *m_oldPtr;
57 int m_dna_nr;
58 int m_number;
59 };
60
61 enum btSerializationFlags
62 {
63 BT_SERIALIZE_NO_BVH = 1,
64 BT_SERIALIZE_NO_TRIANGLEINFOMAP = 2,
65 BT_SERIALIZE_NO_DUPLICATE_ASSERT = 4
66 };
67
68 class btSerializer
69 {
70
71 public:
72
73 virtual ~btSerializer() {}
74
75 virtual const unsigned char* getBufferPointer() const = 0;
76
77 virtual int getCurrentBufferSize() const = 0;
78
79 virtual btChunk* allocate(size_t size, int numElements) = 0;
80
81 virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)= 0;
82
83 virtual void* findPointer(void* oldPtr) = 0;
84
85 virtual void* getUniquePointer(void*oldPtr) = 0;
86
87 virtual void startSerialization() = 0;
88
89 virtual void finishSerialization() = 0;
90
91 virtual const char* findNameForPointer(const void* ptr) const = 0;
92
93 virtual void registerNameForPointer(const void* ptr, const char* name) = 0;
94
95 virtual void serializeName(const char* ptr) = 0;
96
97 virtual int getSerializationFlags() const = 0;
98
99 virtual void setSerializationFlags(int flags) = 0;
100
101 virtual int getNumChunks() const = 0;
102
103 virtual const btChunk* getChunk(int chunkIndex) const = 0;
104
105 };
106
107
108
109 #define BT_HEADER_LENGTH 12
110 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
111 # define BT_MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
112 #else
113 # define BT_MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
114 #endif
115
116
117 #define BT_MULTIBODY_CODE BT_MAKE_ID('M','B','D','Y')
118 #define BT_SOFTBODY_CODE BT_MAKE_ID('S','B','D','Y')
119 #define BT_COLLISIONOBJECT_CODE BT_MAKE_ID('C','O','B','J')
120 #define BT_RIGIDBODY_CODE BT_MAKE_ID('R','B','D','Y')
121 #define BT_CONSTRAINT_CODE BT_MAKE_ID('C','O','N','S')
122 #define BT_BOXSHAPE_CODE BT_MAKE_ID('B','O','X','S')
123 #define BT_QUANTIZED_BVH_CODE BT_MAKE_ID('Q','B','V','H')
124 #define BT_TRIANLGE_INFO_MAP BT_MAKE_ID('T','M','A','P')
125 #define BT_SHAPE_CODE BT_MAKE_ID('S','H','A','P')
126 #define BT_ARRAY_CODE BT_MAKE_ID('A','R','A','Y')
127 #define BT_SBMATERIAL_CODE BT_MAKE_ID('S','B','M','T')
128 #define BT_SBNODE_CODE BT_MAKE_ID('S','B','N','D')
129 #define BT_DYNAMICSWORLD_CODE BT_MAKE_ID('D','W','L','D')
130 #define BT_DNA_CODE BT_MAKE_ID('D','N','A','1')
131
132
133 struct btPointerUid
134 {
135 union
136 {
137 void* m_ptr;
138 int m_uniqueIds[2];
139 };
140 };
141
142 struct btBulletSerializedArrays
143 {
144 btBulletSerializedArrays()
145 {
146 }
147 btAlignedObjectArray<struct btQuantizedBvhDoubleData*> m_bvhsDouble;
148 btAlignedObjectArray<struct btQuantizedBvhFloatData*> m_bvhsFloat;
149 btAlignedObjectArray<struct btCollisionShapeData*> m_colShapeData;
150 btAlignedObjectArray<struct btDynamicsWorldDoubleData*> m_dynamicWorldInfoDataDouble;
151 btAlignedObjectArray<struct btDynamicsWorldFloatData*> m_dynamicWorldInfoDataFloat;
152 btAlignedObjectArray<struct btRigidBodyDoubleData*> m_rigidBodyDataDouble;
153 btAlignedObjectArray<struct btRigidBodyFloatData*> m_rigidBodyDataFloat;
154 btAlignedObjectArray<struct btCollisionObjectDoubleData*> m_collisionObjectDataDouble;
155 btAlignedObjectArray<struct btCollisionObjectFloatData*> m_collisionObjectDataFloat;
156 btAlignedObjectArray<struct btTypedConstraintFloatData*> m_constraintDataFloat;
157 btAlignedObjectArray<struct btTypedConstraintDoubleData*> m_constraintDataDouble;
158 btAlignedObjectArray<struct btTypedConstraintData*> m_constraintData;//for backwards compatibility
159 btAlignedObjectArray<struct btSoftBodyFloatData*> m_softBodyFloatData;
160 btAlignedObjectArray<struct btSoftBodyDoubleData*> m_softBodyDoubleData;
161
162 };
163
164
167 class btDefaultSerializer : public btSerializer
168 {
169
170 protected:
171
172 btAlignedObjectArray<char*> mTypes;
173 btAlignedObjectArray<short*> mStructs;
174 btAlignedObjectArray<short> mTlens;
175 btHashMap<btHashInt, int> mStructReverse;
176 btHashMap<btHashString,int> mTypeLookup;
177
178
179
180 btHashMap<btHashPtr,void*> m_chunkP;
181
182 btHashMap<btHashPtr,const char*> m_nameMap;
183
184 btHashMap<btHashPtr,btPointerUid> m_uniquePointers;
185 int m_uniqueIdGenerator;
186
187 int m_totalSize;
188 unsigned char* m_buffer;
189 bool m_ownsBuffer;
190 int m_currentSize;
191 void* m_dna;
192 int m_dnaLength;
193
194 int m_serializationFlags;
195
196
197 btAlignedObjectArray<btChunk*> m_chunkPtrs;
198
199 protected:
200
201
202 virtual void* findPointer(void* oldPtr)
203 {
204 void** ptr = m_chunkP.find(oldPtr);
205 if (ptr && *ptr)
206 return *ptr;
207 return 0;
208 }
209
210
211
212
213
214 virtual void writeDNA()
215 {
216 btChunk* dnaChunk = allocate(m_dnaLength,1);
217 memcpy(dnaChunk->m_oldPtr,m_dna,m_dnaLength);
218 finalizeChunk(dnaChunk,"DNA1",BT_DNA_CODE, m_dna);
219 }
220
221 int getReverseType(const char *type) const
222 {
223
224 btHashString key(type);
225 const int* valuePtr = mTypeLookup.find(key);
226 if (valuePtr)
227 return *valuePtr;
228
229 return -1;
230 }
231
232 void initDNA(const char* bdnaOrg,int dnalen)
233 {
235 if (m_dna)
236 return;
237
238 int littleEndian= 1;
239 littleEndian= ((char*)&littleEndian)[0];
240
241
242 m_dna = btAlignedAlloc(dnalen,16);
243 memcpy(m_dna,bdnaOrg,dnalen);
244 m_dnaLength = dnalen;
245
246 int *intPtr=0;
247 short *shtPtr=0;
248 char *cp = 0;int dataLen =0;
249 intPtr = (int*)m_dna;
250
251 /*
252 SDNA (4 bytes) (magic number)
253 NAME (4 bytes)
254 <nr> (4 bytes) amount of names (int)
255 <string>
256 <string>
257 */
258
259 if (strncmp((const char*)m_dna, "SDNA", 4)==0)
260 {
261 // skip ++ NAME
262 intPtr++; intPtr++;
263 }
264
265 // Parse names
266 if (!littleEndian)
267 *intPtr = btSwapEndian(*intPtr);
268
269 dataLen = *intPtr;
270
271 intPtr++;
272
273 cp = (char*)intPtr;
274 int i;
275 for ( i=0; i<dataLen; i++)
276 {
277
278 while (*cp)cp++;
279 cp++;
280 }
281 cp = btAlignPointer(cp,4);
282
283 /*
284 TYPE (4 bytes)
285 <nr> amount of types (int)
286 <string>
287 <string>
288 */
289
290 intPtr = (int*)cp;
291 btAssert(strncmp(cp, "TYPE", 4)==0); intPtr++;
292
293 if (!littleEndian)
294 *intPtr = btSwapEndian(*intPtr);
295
296 dataLen = *intPtr;
297 intPtr++;
298
299
300 cp = (char*)intPtr;
301 for (i=0; i<dataLen; i++)
302 {
303 mTypes.push_back(cp);
304 while (*cp)cp++;
305 cp++;
306 }
307
308 cp = btAlignPointer(cp,4);
309
310
311 /*
312 TLEN (4 bytes)
313 <len> (short) the lengths of types
314 <len>
315 */
316
317 // Parse type lens
318 intPtr = (int*)cp;
319 btAssert(strncmp(cp, "TLEN", 4)==0); intPtr++;
320
321 dataLen = (int)mTypes.size();
322
323 shtPtr = (short*)intPtr;
324 for (i=0; i<dataLen; i++, shtPtr++)
325 {
326 if (!littleEndian)
327 shtPtr[0] = btSwapEndian(shtPtr[0]);
328 mTlens.push_back(shtPtr[0]);
329 }
330
331 if (dataLen & 1) shtPtr++;
332
333 /*
334 STRC (4 bytes)
335 <nr> amount of structs (int)
336 <typenr>
337 <nr_of_elems>
338 <typenr>
339 <namenr>
340 <typenr>
341 <namenr>
342 */
343
344 intPtr = (int*)shtPtr;
345 cp = (char*)intPtr;
346 btAssert(strncmp(cp, "STRC", 4)==0); intPtr++;
347
348 if (!littleEndian)
349 *intPtr = btSwapEndian(*intPtr);
350 dataLen = *intPtr ;
351 intPtr++;
352
353
354 shtPtr = (short*)intPtr;
355 for (i=0; i<dataLen; i++)
356 {
357 mStructs.push_back (shtPtr);
358
359 if (!littleEndian)
360 {
361 shtPtr[0]= btSwapEndian(shtPtr[0]);
362 shtPtr[1]= btSwapEndian(shtPtr[1]);
363
364 int len = shtPtr[1];
365 shtPtr+= 2;
366
367 for (int a=0; a<len; a++, shtPtr+=2)
368 {
369 shtPtr[0]= btSwapEndian(shtPtr[0]);
370 shtPtr[1]= btSwapEndian(shtPtr[1]);
371 }
372
373 } else
374 {
375 shtPtr+= (2*shtPtr[1])+2;
376 }
377 }
378
379 // build reverse lookups
380 for (i=0; i<(int)mStructs.size(); i++)
381 {
382 short *strc = mStructs.at(i);
383 mStructReverse.insert(strc[0], i);
384 mTypeLookup.insert(btHashString(mTypes[strc[0]]),i);
385 }
386 }
387
388 public:
389
390 btHashMap<btHashPtr,void*> m_skipPointers;
391
392
393 btDefaultSerializer(int totalSize=0, unsigned char* buffer=0)
394 :m_uniqueIdGenerator(0),
395 m_totalSize(totalSize),
396 m_currentSize(0),
397 m_dna(0),
398 m_dnaLength(0),
399 m_serializationFlags(0)
400 {
401 if (buffer==0)
402 {
403 m_buffer = m_totalSize?(unsigned char*)btAlignedAlloc(totalSize,16):0;
404 m_ownsBuffer = true;
405 } else
406 {
407 m_buffer = buffer;
408 m_ownsBuffer = false;
409 }
410
411 const bool VOID_IS_8 = ((sizeof(void*)==8));
412
413 #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
414 if (VOID_IS_8)
415 {
416 #if _WIN64
417 initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
418 #else
419 btAssert(0);
420 #endif
421 } else
422 {
423 #ifndef _WIN64
424 initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
425 #else
426 btAssert(0);
427 #endif
428 }
429
430 #else //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
431 if (VOID_IS_8)
432 {
433 initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
434 } else
435 {
436 initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
437 }
438 #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
439
440 }
441
442 virtual ~btDefaultSerializer()
443 {
444 if (m_buffer && m_ownsBuffer)
445 btAlignedFree(m_buffer);
446 if (m_dna)
447 btAlignedFree(m_dna);
448 }
449
450 static int getMemoryDnaSizeInBytes()
451 {
452 const bool VOID_IS_8 = ((sizeof(void*) == 8));
453
454 if (VOID_IS_8)
455 {
456 return sBulletDNAlen64;
457 }
458 return sBulletDNAlen;
459 }
460 static const char* getMemoryDna()
461 {
462 const bool VOID_IS_8 = ((sizeof(void*) == 8));
463 if (VOID_IS_8)
464 {
465 return (const char*)sBulletDNAstr64;
466 }
467 return (const char*)sBulletDNAstr;
468 }
469
470 void insertHeader()
471 {
472 writeHeader(m_buffer);
473 m_currentSize += BT_HEADER_LENGTH;
474 }
475
476 void writeHeader(unsigned char* buffer) const
477 {
478
479
480 #ifdef BT_USE_DOUBLE_PRECISION
481 memcpy(buffer, "BULLETd", 7);
482 #else
483 memcpy(buffer, "BULLETf", 7);
484 #endif //BT_USE_DOUBLE_PRECISION
485
486 int littleEndian= 1;
487 littleEndian= ((char*)&littleEndian)[0];
488
489 if (sizeof(void*)==8)
490 {
491 buffer[7] = '-';
492 } else
493 {
494 buffer[7] = '_';
495 }
496
497 if (littleEndian)
498 {
499 buffer[8]='v';
500 } else
501 {
502 buffer[8]='V';
503 }
504
505
506 buffer[9] = '2';
507 buffer[10] = '8';
508 buffer[11] = '7';
509
510 }
511
512 virtual void startSerialization()
513 {
514 m_uniqueIdGenerator= 1;
515 if (m_totalSize)
516 {
517 unsigned char* buffer = internalAlloc(BT_HEADER_LENGTH);
518 writeHeader(buffer);
519 }
520
521 }
522
523 virtual void finishSerialization()
524 {
525 writeDNA();
526
527 //if we didn't pre-allocate a buffer, we need to create a contiguous buffer now
528 int mysize = 0;
529 if (!m_totalSize)
530 {
531 if (m_buffer)
532 btAlignedFree(m_buffer);
533
534 m_currentSize += BT_HEADER_LENGTH;
535 m_buffer = (unsigned char*)btAlignedAlloc(m_currentSize,16);
536
537 unsigned char* currentPtr = m_buffer;
538 writeHeader(m_buffer);
539 currentPtr += BT_HEADER_LENGTH;
540 mysize+=BT_HEADER_LENGTH;
541 for (int i=0;i< m_chunkPtrs.size();i++)
542 {
543 int curLength = sizeof(btChunk)+m_chunkPtrs[i]->m_length;
544 memcpy(currentPtr,m_chunkPtrs[i], curLength);
545 btAlignedFree(m_chunkPtrs[i]);
546 currentPtr+=curLength;
547 mysize+=curLength;
548 }
549 }
550
551 mTypes.clear();
552 mStructs.clear();
553 mTlens.clear();
554 mStructReverse.clear();
555 mTypeLookup.clear();
556 m_skipPointers.clear();
557 m_chunkP.clear();
558 m_nameMap.clear();
559 m_uniquePointers.clear();
560 m_chunkPtrs.clear();
561 }
562
563 virtual void* getUniquePointer(void*oldPtr)
564 {
565 btAssert(m_uniqueIdGenerator >= 0);
566 if (!oldPtr)
567 return 0;
568
569 btPointerUid* uptr = (btPointerUid*)m_uniquePointers.find(oldPtr);
570 if (uptr)
571 {
572 return uptr->m_ptr;
573 }
574
575 void** ptr2 = m_skipPointers[oldPtr];
576 if (ptr2)
577 {
578 return 0;
579 }
580
581 m_uniqueIdGenerator++;
582
583 btPointerUid uid;
584 uid.m_uniqueIds[0] = m_uniqueIdGenerator;
585 uid.m_uniqueIds[1] = m_uniqueIdGenerator;
586 m_uniquePointers.insert(oldPtr,uid);
587 return uid.m_ptr;
588
589 }
590
591 virtual const unsigned char* getBufferPointer() const
592 {
593 return m_buffer;
594 }
595
596 virtual int getCurrentBufferSize() const
597 {
598 return m_currentSize;
599 }
600
601 virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
602 {
603 if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
604 {
605 btAssert(!findPointer(oldPtr));
606 }
607
608 chunk->m_dna_nr = getReverseType(structType);
609
610 chunk->m_chunkCode = chunkCode;
611
612 void* uniquePtr = getUniquePointer(oldPtr);
613
614 m_chunkP.insert(oldPtr,uniquePtr);//chunk->m_oldPtr);
615 chunk->m_oldPtr = uniquePtr;//oldPtr;
616
617 }
618
619
620 virtual unsigned char* internalAlloc(size_t size)
621 {
622 unsigned char* ptr = 0;
623
624 if (m_totalSize)
625 {
626 ptr = m_buffer+m_currentSize;
627 m_currentSize += int(size);
628 btAssert(m_currentSize<m_totalSize);
629 } else
630 {
631 ptr = (unsigned char*)btAlignedAlloc(size,16);
632 m_currentSize += int(size);
633 }
634 return ptr;
635 }
636
637
638
639 virtual btChunk* allocate(size_t size, int numElements)
640 {
641
642 unsigned char* ptr = internalAlloc(int(size)*numElements+sizeof(btChunk));
643
644 unsigned char* data = ptr + sizeof(btChunk);
645
646 btChunk* chunk = (btChunk*)ptr;
647 chunk->m_chunkCode = 0;
648 chunk->m_oldPtr = data;
649 chunk->m_length = int(size)*numElements;
650 chunk->m_number = numElements;
651
652 m_chunkPtrs.push_back(chunk);
653
654
655 return chunk;
656 }
657
658 virtual const char* findNameForPointer(const void* ptr) const
659 {
660 const char*const * namePtr = m_nameMap.find(ptr);
661 if (namePtr && *namePtr)
662 return *namePtr;
663 return 0;
664
665 }
666
667 virtual void registerNameForPointer(const void* ptr, const char* name)
668 {
669 m_nameMap.insert(ptr,name);
670 }
671
672 virtual void serializeName(const char* name)
673 {
674 if (name)
675 {
676 //don't serialize name twice
677 if (findPointer((void*)name))
678 return;
679
680 int len = btStrLen(name);
681 if (len)
682 {
683
684 int newLen = len+1;
685 int padding = ((newLen+3)&~3)-newLen;
686 newLen += padding;
687
688 //serialize name string now
689 btChunk* chunk = allocate(sizeof(char),newLen);
690 char* destinationName = (char*)chunk->m_oldPtr;
691 for (int i=0;i<len;i++)
692 {
693 destinationName[i] = name[i];
694 }
695 destinationName[len] = 0;
696 finalizeChunk(chunk,"char",BT_ARRAY_CODE,(void*)name);
697 }
698 }
699 }
700
701 virtual int getSerializationFlags() const
702 {
703 return m_serializationFlags;
704 }
705
706 virtual void setSerializationFlags(int flags)
707 {
708 m_serializationFlags = flags;
709 }
710 int getNumChunks() const
711 {
712 return m_chunkPtrs.size();
713 }
714
715 const btChunk* getChunk(int chunkIndex) const
716 {
717 return m_chunkPtrs[chunkIndex];
718 }
719 };
720
721
727 #ifdef ENABLE_INMEMORY_SERIALIZER
728
729 struct btInMemorySerializer : public btDefaultSerializer
730 {
731 btHashMap<btHashPtr,btChunk*> m_uid2ChunkPtr;
732 btHashMap<btHashPtr,void*> m_orgPtr2UniqueDataPtr;
733 btHashMap<btHashString,const void*> m_names2Ptr;
734
735
736 btBulletSerializedArrays m_arrays;
737
738 btInMemorySerializer(int totalSize=0, unsigned char* buffer=0)
739 :btDefaultSerializer(totalSize,buffer)
740 {
741
742 }
743
744 virtual void startSerialization()
745 {
746 m_uid2ChunkPtr.clear();
747 //todo: m_arrays.clear();
748 btDefaultSerializer::startSerialization();
749 }
750
751
752
753 btChunk* findChunkFromUniquePointer(void* uniquePointer)
754 {
755 btChunk** chkPtr = m_uid2ChunkPtr[uniquePointer];
756 if (chkPtr)
757 {
758 return *chkPtr;
759 }
760 return 0;
761 }
762
763 virtual void registerNameForPointer(const void* ptr, const char* name)
764 {
765 btDefaultSerializer::registerNameForPointer(ptr,name);
766 m_names2Ptr.insert(name,ptr);
767 }
768
769 virtual void finishSerialization()
770 {
771 }
772
773 virtual void* getUniquePointer(void*oldPtr)
774 {
775 if (oldPtr==0)
776 return 0;
777
778 // void* uniquePtr = getUniquePointer(oldPtr);
779 btChunk* chunk = findChunkFromUniquePointer(oldPtr);
780 if (chunk)
781 {
782 return chunk->m_oldPtr;
783 } else
784 {
785 const char* n = (const char*) oldPtr;
786 const void** ptr = m_names2Ptr[n];
787 if (ptr)
788 {
789 return oldPtr;
790 } else
791 {
792 void** ptr2 = m_skipPointers[oldPtr];
793 if (ptr2)
794 {
795 return 0;
796 } else
797 {
798 //If this assert hit, serialization happened in the wrong order
799 // 'getUniquePointer'
800 btAssert(0);
801 }
802
803 }
804 return 0;
805 }
806 return oldPtr;
807 }
808
809 virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
810 {
811 if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
812 {
813 btAssert(!findPointer(oldPtr));
814 }
815
816 chunk->m_dna_nr = getReverseType(structType);
817 chunk->m_chunkCode = chunkCode;
818 //void* uniquePtr = getUniquePointer(oldPtr);
819 m_chunkP.insert(oldPtr,oldPtr);//chunk->m_oldPtr);
820 // chunk->m_oldPtr = uniquePtr;//oldPtr;
821
822 void* uid = findPointer(oldPtr);
823 m_uid2ChunkPtr.insert(uid,chunk);
824
825 switch (chunk->m_chunkCode)
826 {
827 case BT_SOFTBODY_CODE:
828 {
829 #ifdef BT_USE_DOUBLE_PRECISION
830 m_arrays.m_softBodyDoubleData.push_back((btSoftBodyDoubleData*) chunk->m_oldPtr);
831 #else
832 m_arrays.m_softBodyFloatData.push_back((btSoftBodyFloatData*) chunk->m_oldPtr);
833 #endif
834 break;
835 }
836 case BT_COLLISIONOBJECT_CODE:
837 {
838 #ifdef BT_USE_DOUBLE_PRECISION
839 m_arrays.m_collisionObjectDataDouble.push_back((btCollisionObjectDoubleData*)chunk->m_oldPtr);
840 #else//BT_USE_DOUBLE_PRECISION
841 m_arrays.m_collisionObjectDataFloat.push_back((btCollisionObjectFloatData*)chunk->m_oldPtr);
842 #endif //BT_USE_DOUBLE_PRECISION
843 break;
844 }
845 case BT_RIGIDBODY_CODE:
846 {
847 #ifdef BT_USE_DOUBLE_PRECISION
848 m_arrays.m_rigidBodyDataDouble.push_back((btRigidBodyDoubleData*)chunk->m_oldPtr);
849 #else
850 m_arrays.m_rigidBodyDataFloat.push_back((btRigidBodyFloatData*)chunk->m_oldPtr);
851 #endif//BT_USE_DOUBLE_PRECISION
852 break;
853 };
854 case BT_CONSTRAINT_CODE:
855 {
856 #ifdef BT_USE_DOUBLE_PRECISION
857 m_arrays.m_constraintDataDouble.push_back((btTypedConstraintDoubleData*)chunk->m_oldPtr);
858 #else
859 m_arrays.m_constraintDataFloat.push_back((btTypedConstraintFloatData*)chunk->m_oldPtr);
860 #endif
861 break;
862 }
863 case BT_QUANTIZED_BVH_CODE:
864 {
865 #ifdef BT_USE_DOUBLE_PRECISION
866 m_arrays.m_bvhsDouble.push_back((btQuantizedBvhDoubleData*) chunk->m_oldPtr);
867 #else
868 m_arrays.m_bvhsFloat.push_back((btQuantizedBvhFloatData*) chunk->m_oldPtr);
869 #endif
870 break;
871 }
872
873 case BT_SHAPE_CODE:
874 {
875 btCollisionShapeData* shapeData = (btCollisionShapeData*) chunk->m_oldPtr;
876 m_arrays.m_colShapeData.push_back(shapeData);
877 break;
878 }
879 case BT_TRIANLGE_INFO_MAP:
880 case BT_ARRAY_CODE:
881 case BT_SBMATERIAL_CODE:
882 case BT_SBNODE_CODE:
883 case BT_DYNAMICSWORLD_CODE:
884 case BT_DNA_CODE:
885 {
886 break;
887 }
888 default:
889 {
890 }
891 };
892 }
893
894 int getNumChunks() const
895 {
896 return m_uid2ChunkPtr.size();
897 }
898
899 const btChunk* getChunk(int chunkIndex) const
900 {
901 return *m_uid2ChunkPtr.getAtIndex(chunkIndex);
902 }
903
904 };
905 #endif //ENABLE_INMEMORY_SERIALIZER
906
907 #endif //BT_SERIALIZER_H
908
btHashMap::clear
void clear()
Definition: btHashMap.h:472
btBulletSerializedArrays::m_rigidBodyDataDouble
btAlignedObjectArray< struct btRigidBodyDoubleData * > m_rigidBodyDataDouble
Definition: btSerializer.h:152
btDefaultSerializer::getMemoryDnaSizeInBytes
static int getMemoryDnaSizeInBytes()
Definition: btSerializer.h:450
BT_SERIALIZE_NO_BVH
Definition: btSerializer.h:63
btDefaultSerializer::serializeName
virtual void serializeName(const char *name)
Definition: btSerializer.h:672
btCollisionObjectFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btCollisionObject.h:637
btDefaultSerializer::m_chunkPtrs
btAlignedObjectArray< btChunk * > m_chunkPtrs
Definition: btSerializer.h:197
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:274
btChunk
Definition: btSerializer.h:51
btPointerUid::m_uniqueIds
int m_uniqueIds[2]
Definition: btSerializer.h:138
btDefaultSerializer::m_totalSize
int m_totalSize
Definition: btSerializer.h:187
btDefaultSerializer::setSerializationFlags
virtual void setSerializationFlags(int flags)
Definition: btSerializer.h:706
btChunk::m_number
int m_number
Definition: btSerializer.h:58
BT_CONSTRAINT_CODE
#define BT_CONSTRAINT_CODE
Definition: btSerializer.h:121
btDefaultSerializer::finalizeChunk
virtual void finalizeChunk(btChunk *chunk, const char *structType, int chunkCode, void *oldPtr)
Definition: btSerializer.h:601
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:53
btQuantizedBvhFloatData
Definition: btQuantizedBvh.h:539
btDefaultSerializer::m_nameMap
btHashMap< btHashPtr, const char * > m_nameMap
Definition: btSerializer.h:182
btDefaultSerializer::registerNameForPointer
virtual void registerNameForPointer(const void *ptr, const char *name)
Definition: btSerializer.h:667
btHashMap::find
const Value * find(const Key &key) const
Definition: btHashMap.h:434
btBulletSerializedArrays::m_dynamicWorldInfoDataFloat
btAlignedObjectArray< struct btDynamicsWorldFloatData * > m_dynamicWorldInfoDataFloat
Definition: btSerializer.h:151
btBulletSerializedArrays::m_bvhsFloat
btAlignedObjectArray< struct btQuantizedBvhFloatData * > m_bvhsFloat
Definition: btSerializer.h:148
BT_QUANTIZED_BVH_CODE
#define BT_QUANTIZED_BVH_CODE
Definition: btSerializer.h:123
btBulletSerializedArrays::m_bvhsDouble
btAlignedObjectArray< struct btQuantizedBvhDoubleData * > m_bvhsDouble
Definition: btSerializer.h:147
btDefaultSerializer::allocate
virtual btChunk * allocate(size_t size, int numElements)
Definition: btSerializer.h:639
btBulletSerializedArrays::m_dynamicWorldInfoDataDouble
btAlignedObjectArray< struct btDynamicsWorldDoubleData * > m_dynamicWorldInfoDataDouble
Definition: btSerializer.h:150
btDefaultSerializer::m_currentSize
int m_currentSize
Definition: btSerializer.h:190
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
btAssert
#define btAssert(x)
Definition: btScalar.h:131
btAlignedObjectArray::at
const T & at(int n) const
Definition: btAlignedObjectArray.h:160
btPointerUid::m_ptr
void * m_ptr
Definition: btSerializer.h:137
BT_SOFTBODY_CODE
#define BT_SOFTBODY_CODE
Definition: btSerializer.h:118
BT_DYNAMICSWORLD_CODE
#define BT_DYNAMICSWORLD_CODE
Definition: btSerializer.h:129
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btDefaultSerializer::getMemoryDna
static const char * getMemoryDna()
Definition: btSerializer.h:460
btSerializationFlags
btSerializationFlags
Definition: btSerializer.h:61
btBulletSerializedArrays::m_softBodyDoubleData
btAlignedObjectArray< struct btSoftBodyDoubleData * > m_softBodyDoubleData
Definition: btSerializer.h:160
btDefaultSerializer::finishSerialization
virtual void finishSerialization()
Definition: btSerializer.h:523
btDefaultSerializer::m_dnaLength
int m_dnaLength
Definition: btSerializer.h:192
btDefaultSerializer::mTypeLookup
btHashMap< btHashString, int > mTypeLookup
Definition: btSerializer.h:176
btDefaultSerializer::findPointer
virtual void * findPointer(void *oldPtr)
Definition: btSerializer.h:202
btHashMap< btHashInt, int >
btDefaultSerializer::getUniquePointer
virtual void * getUniquePointer(void *oldPtr)
Definition: btSerializer.h:563
btDefaultSerializer::mStructReverse
btHashMap< btHashInt, int > mStructReverse
Definition: btSerializer.h:175
btHashMap::getAtIndex
const Value * getAtIndex(int index) const
Definition: btHashMap.h:389
btSerializer
Definition: btSerializer.h:68
btHashMap::size
int size() const
Definition: btHashMap.h:384
btAlignedObjectArray::clear
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
Definition: btAlignedObjectArray.h:190
btDefaultSerializer
The btDefaultSerializer is the main Bullet serialization class.
Definition: btSerializer.h:167
btChunk::m_chunkCode
int m_chunkCode
Definition: btSerializer.h:54
btDefaultSerializer::findNameForPointer
virtual const char * findNameForPointer(const void *ptr) const
Definition: btSerializer.h:658
btSerializer::~btSerializer
virtual ~btSerializer()
Definition: btSerializer.h:73
btBulletSerializedArrays::m_constraintDataFloat
btAlignedObjectArray< struct btTypedConstraintFloatData * > m_constraintDataFloat
Definition: btSerializer.h:156
btCollisionObjectDoubleData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btCollisionObject.h:602
BT_SBNODE_CODE
#define BT_SBNODE_CODE
Definition: btSerializer.h:128
btDefaultSerializer::internalAlloc
virtual unsigned char * internalAlloc(size_t size)
Definition: btSerializer.h:620
btDefaultSerializer::m_dna
void * m_dna
Definition: btSerializer.h:191
btDefaultSerializer::m_buffer
unsigned char * m_buffer
Definition: btSerializer.h:188
btBulletSerializedArrays::m_collisionObjectDataFloat
btAlignedObjectArray< struct btCollisionObjectFloatData * > m_collisionObjectDataFloat
Definition: btSerializer.h:155
bParse::VOID_IS_8
const bool VOID_IS_8
Definition: bChunk.h:89
BT_HEADER_LENGTH
#define BT_HEADER_LENGTH
Definition: btSerializer.h:109
btPointerUid
Definition: btSerializer.h:133
btRigidBodyDoubleData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btRigidBody.h:590
btBulletSerializedArrays::m_colShapeData
btAlignedObjectArray< struct btCollisionShapeData * > m_colShapeData
Definition: btSerializer.h:149
sBulletDNAlen
int sBulletDNAlen
Definition: btSerializer.cpp:599
btBulletSerializedArrays::m_constraintData
btAlignedObjectArray< struct btTypedConstraintData * > m_constraintData
Definition: btSerializer.h:158
btDefaultSerializer::initDNA
void initDNA(const char *bdnaOrg, int dnalen)
Definition: btSerializer.h:232
btDefaultSerializer::mStructs
btAlignedObjectArray< short * > mStructs
Definition: btSerializer.h:173
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:48
btTypedConstraintDoubleData
Definition: btTypedConstraint.h:416
btHashMap::insert
void insert(const Key &key, const Value &value)
Definition: btHashMap.h:274
btBulletSerializedArrays::btBulletSerializedArrays
btBulletSerializedArrays()
Definition: btSerializer.h:144
btDefaultSerializer::getNumChunks
int getNumChunks() const
Definition: btSerializer.h:710
btDefaultSerializer::mTypes
btAlignedObjectArray< char * > mTypes
Definition: btSerializer.h:172
btBulletSerializedArrays::m_softBodyFloatData
btAlignedObjectArray< struct btSoftBodyFloatData * > m_softBodyFloatData
Definition: btSerializer.h:159
btStrLen
int btStrLen(const char *str)
Definition: btSerializer.h:35
btRigidBodyFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btRigidBody.h:564
btSoftBodyFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btSoftBodyData.h:191
BT_COLLISIONOBJECT_CODE
#define BT_COLLISIONOBJECT_CODE
Definition: btSerializer.h:119
btDefaultSerializer::btDefaultSerializer
btDefaultSerializer(int totalSize=0, unsigned char *buffer=0)
Definition: btSerializer.h:393
BT_ARRAY_CODE
#define BT_ARRAY_CODE
Definition: btSerializer.h:126
btQuantizedBvhDoubleData
Definition: btQuantizedBvh.h:556
btDefaultSerializer::getBufferPointer
virtual const unsigned char * getBufferPointer() const
Definition: btSerializer.h:591
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:155
btBulletSerializedArrays::m_collisionObjectDataDouble
btAlignedObjectArray< struct btCollisionObjectDoubleData * > m_collisionObjectDataDouble
Definition: btSerializer.h:154
btDefaultSerializer::m_skipPointers
btHashMap< btHashPtr, void * > m_skipPointers
Definition: btSerializer.h:390
btHashString
very basic hashable string implementation, compatible with btHashMap
Definition: btHashMap.h:23
btDefaultSerializer::getReverseType
int getReverseType(const char *type) const
Definition: btSerializer.h:221
btDefaultSerializer::insertHeader
void insertHeader()
Definition: btSerializer.h:470
BT_SERIALIZE_NO_TRIANGLEINFOMAP
Definition: btSerializer.h:64
sBulletDNAlen64
int sBulletDNAlen64
Definition: btSerializer64.cpp:599
BT_SBMATERIAL_CODE
#define BT_SBMATERIAL_CODE
Definition: btSerializer.h:127
btBulletSerializedArrays::m_rigidBodyDataFloat
btAlignedObjectArray< struct btRigidBodyFloatData * > m_rigidBodyDataFloat
Definition: btSerializer.h:153
btDefaultSerializer::m_ownsBuffer
bool m_ownsBuffer
Definition: btSerializer.h:189
BT_RIGIDBODY_CODE
#define BT_RIGIDBODY_CODE
Definition: btSerializer.h:120
btDefaultSerializer::startSerialization
virtual void startSerialization()
Definition: btSerializer.h:512
btDefaultSerializer::writeHeader
void writeHeader(unsigned char *buffer) const
Definition: btSerializer.h:476
BT_SERIALIZE_NO_DUPLICATE_ASSERT
Definition: btSerializer.h:65
btDefaultSerializer::mTlens
btAlignedObjectArray< short > mTlens
Definition: btSerializer.h:174
btChunk::m_length
int m_length
Definition: btSerializer.h:55
btSwapEndian
unsigned btSwapEndian(unsigned val)
Definition: btScalar.h:629
btDefaultSerializer::m_serializationFlags
int m_serializationFlags
Definition: btSerializer.h:194
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:47
btAlignPointer
T * btAlignPointer(T *unalignedPtr, size_t alignment)
align a pointer to the provided alignment, upwards
Definition: btScalar.h:792
btBulletSerializedArrays
Definition: btSerializer.h:142
BT_TRIANLGE_INFO_MAP
#define BT_TRIANLGE_INFO_MAP
Definition: btSerializer.h:124
btDefaultSerializer::m_uniquePointers
btHashMap< btHashPtr, btPointerUid > m_uniquePointers
Definition: btSerializer.h:184
btDefaultSerializer::m_uniqueIdGenerator
int m_uniqueIdGenerator
Definition: btSerializer.h:185
btDefaultSerializer::~btDefaultSerializer
virtual ~btDefaultSerializer()
Definition: btSerializer.h:442
BT_SHAPE_CODE
#define BT_SHAPE_CODE
Definition: btSerializer.h:125
btHashMap.h
btDefaultSerializer::m_chunkP
btHashMap< btHashPtr, void * > m_chunkP
Definition: btSerializer.h:180
BT_DNA_CODE
#define BT_DNA_CODE
Definition: btSerializer.h:130
btChunk::m_oldPtr
void * m_oldPtr
Definition: btSerializer.h:56
sBulletDNAstr
char sBulletDNAstr[]
Definition: btSerializer.cpp:1
btDefaultSerializer::getSerializationFlags
virtual int getSerializationFlags() const
Definition: btSerializer.h:701
btCollisionShapeData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btCollisionShape.h:155
btChunk::m_dna_nr
int m_dna_nr
Definition: btSerializer.h:57
btDefaultSerializer::writeDNA
virtual void writeDNA()
Definition: btSerializer.h:214
btBulletSerializedArrays::m_constraintDataDouble
btAlignedObjectArray< struct btTypedConstraintDoubleData * > m_constraintDataDouble
Definition: btSerializer.h:157
btTypedConstraintFloatData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btTypedConstraint.h:366
sBulletDNAstr64
char sBulletDNAstr64[]
Definition: btSerializer64.cpp:1
btDefaultSerializer::getCurrentBufferSize
virtual int getCurrentBufferSize() const
Definition: btSerializer.h:596
btScalar.h
btDefaultSerializer::getChunk
const btChunk * getChunk(int chunkIndex) const
Definition: btSerializer.h:715