1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2014 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_COLLISION_WORLD_IMPORTER_H
17 #define BT_COLLISION_WORLD_IMPORTER_H
18 
19 #include "LinearMath/btTransform.h"
20 #include "LinearMath/btVector3.h"
21 #include "LinearMath/btAlignedObjectArray.h"
22 #include "LinearMath/btHashMap.h"
23 
24 class btCollisionShape;
25 class btCollisionObject;
26 struct btBulletSerializedArrays;
27 
28 struct ConstraintInput;
29 class btCollisionWorld;
30 struct btCollisionShapeData;
31 class btTriangleIndexVertexArray;
32 class btStridingMeshInterface;
33 struct btStridingMeshInterfaceData;
34 class btGImpactMeshShape;
35 class btOptimizedBvh;
36 struct btTriangleInfoMap;
37 class btBvhTriangleMeshShape;
38 class btPoint2PointConstraint;
39 class btHingeConstraint;
40 class btConeTwistConstraint;
41 class btGeneric6DofConstraint;
42 class btGeneric6DofSpringConstraint;
43 class btSliderConstraint;
44 class btGearConstraint;
45 struct btContactSolverInfo;
46 
47 class btCollisionWorldImporter
48 {
49 protected:
50 	btCollisionWorld* m_collisionWorld;
51 
52 	int m_verboseMode;
53 
54 	btAlignedObjectArray<btCollisionShape*> m_allocatedCollisionShapes;
55 	btAlignedObjectArray<btCollisionObject*> m_allocatedRigidBodies;
56 
57 	btAlignedObjectArray<btOptimizedBvh*> m_allocatedBvhs;
58 	btAlignedObjectArray<btTriangleInfoMap*> m_allocatedTriangleInfoMaps;
59 	btAlignedObjectArray<btTriangleIndexVertexArray*> m_allocatedTriangleIndexArrays;
60 	btAlignedObjectArray<btStridingMeshInterfaceData*> m_allocatedbtStridingMeshInterfaceDatas;
61 	btAlignedObjectArray<btCollisionObject*> m_allocatedCollisionObjects;
62 
63 	btAlignedObjectArray<char*> m_allocatedNames;
64 
65 	btAlignedObjectArray<int*> m_indexArrays;
66 	btAlignedObjectArray<short int*> m_shortIndexArrays;
67 	btAlignedObjectArray<unsigned char*> m_charIndexArrays;
68 
69 	btAlignedObjectArray<btVector3FloatData*> m_floatVertexArrays;
70 	btAlignedObjectArray<btVector3DoubleData*> m_doubleVertexArrays;
71 
72 	btHashMap<btHashPtr, btOptimizedBvh*> m_bvhMap;
73 	btHashMap<btHashPtr, btTriangleInfoMap*> m_timMap;
74 
75 	btHashMap<btHashString, btCollisionShape*> m_nameShapeMap;
76 	btHashMap<btHashString, btCollisionObject*> m_nameColObjMap;
77 
78 	btHashMap<btHashPtr, const char*> m_objectNameMap;
79 
80 	btHashMap<btHashPtr, btCollisionShape*> m_shapeMap;
81 	btHashMap<btHashPtr, btCollisionObject*> m_bodyMap;
82 
83 	//methods
84 
85 	char* duplicateName(const char* name);
86 
87 	btCollisionShape* convertCollisionShape(btCollisionShapeData* shapeData);
88 
89 public:
90 	btCollisionWorldImporter(btCollisionWorld* world);
91 
92 	virtual ~btCollisionWorldImporter();
93 
94 	bool convertAllObjects(btBulletSerializedArrays* arrays);
95 
96 	///delete all memory collision shapes, rigid bodies, constraints etc. allocated during the load.
97 	///make sure you don't use the dynamics world containing objects after you call this method
98 	virtual void deleteAllData();
99 
setVerboseMode(int verboseMode)100 	void setVerboseMode(int verboseMode)
101 	{
102 		m_verboseMode = verboseMode;
103 	}
104 
getVerboseMode()105 	int getVerboseMode() const
106 	{
107 		return m_verboseMode;
108 	}
109 
110 	// query for data
111 	int getNumCollisionShapes() const;
112 	btCollisionShape* getCollisionShapeByIndex(int index);
113 	int getNumRigidBodies() const;
114 	btCollisionObject* getRigidBodyByIndex(int index) const;
115 
116 	int getNumBvhs() const;
117 	btOptimizedBvh* getBvhByIndex(int index) const;
118 	int getNumTriangleInfoMaps() const;
119 	btTriangleInfoMap* getTriangleInfoMapByIndex(int index) const;
120 
121 	// queris involving named objects
122 	btCollisionShape* getCollisionShapeByName(const char* name);
123 	btCollisionObject* getCollisionObjectByName(const char* name);
124 
125 	const char* getNameForPointer(const void* ptr) const;
126 
127 	///those virtuals are called by load and can be overridden by the user
128 
129 	//bodies
130 
131 	virtual btCollisionObject* createCollisionObject(const btTransform& startTransform, btCollisionShape* shape, const char* bodyName);
132 
133 	///shapes
134 
135 	virtual btCollisionShape* createPlaneShape(const btVector3& planeNormal, btScalar planeConstant);
136 	virtual btCollisionShape* createBoxShape(const btVector3& halfExtents);
137 	virtual btCollisionShape* createSphereShape(btScalar radius);
138 	virtual btCollisionShape* createCapsuleShapeX(btScalar radius, btScalar height);
139 	virtual btCollisionShape* createCapsuleShapeY(btScalar radius, btScalar height);
140 	virtual btCollisionShape* createCapsuleShapeZ(btScalar radius, btScalar height);
141 
142 	virtual btCollisionShape* createCylinderShapeX(btScalar radius, btScalar height);
143 	virtual btCollisionShape* createCylinderShapeY(btScalar radius, btScalar height);
144 	virtual btCollisionShape* createCylinderShapeZ(btScalar radius, btScalar height);
145 	virtual btCollisionShape* createConeShapeX(btScalar radius, btScalar height);
146 	virtual btCollisionShape* createConeShapeY(btScalar radius, btScalar height);
147 	virtual btCollisionShape* createConeShapeZ(btScalar radius, btScalar height);
148 	virtual class btTriangleIndexVertexArray* createTriangleMeshContainer();
149 	virtual btBvhTriangleMeshShape* createBvhTriangleMeshShape(btStridingMeshInterface* trimesh, btOptimizedBvh* bvh);
150 	virtual btCollisionShape* createConvexTriangleMeshShape(btStridingMeshInterface* trimesh);
151 #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
152 	virtual btGImpactMeshShape* createGimpactShape(btStridingMeshInterface* trimesh);
153 #endif  //SUPPORT_GIMPACT_SHAPE_IMPORT
154 	virtual btStridingMeshInterfaceData* createStridingMeshInterfaceData(btStridingMeshInterfaceData* interfaceData);
155 
156 	virtual class btConvexHullShape* createConvexHullShape();
157 	virtual class btCompoundShape* createCompoundShape();
158 	virtual class btScaledBvhTriangleMeshShape* createScaledTrangleMeshShape(btBvhTriangleMeshShape* meshShape, const btVector3& localScalingbtBvhTriangleMeshShape);
159 
160 	virtual class btMultiSphereShape* createMultiSphereShape(const btVector3* positions, const btScalar* radi, int numSpheres);
161 
162 	virtual btTriangleIndexVertexArray* createMeshInterface(btStridingMeshInterfaceData& meshData);
163 
164 	///acceleration and connectivity structures
165 	virtual btOptimizedBvh* createOptimizedBvh();
166 	virtual btTriangleInfoMap* createTriangleInfoMap();
167 };
168 
169 #endif  //BT_WORLD_IMPORTER_H
170