MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
collisionshapemanager.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _collisionshapemanager_h
41 #define _collisionshapemanager_h
42 
43 #include "Physics/collisionshape.h"
44 #include "datatypes.h"
45 #include "managerbase.h"
46 #include "managerfactory.h"
47 #include "singleton.h"
48 
49 class btTriangleMesh;
50 class btCollisionShape;
51 
52 namespace Mezzanine
53 {
54  namespace Graphics
55  {
56  class Mesh;
57  }
58  namespace Physics
59  {
60  class CollisionShape;
61  class ConvexHullCollisionShape;
62  class DynamicMeshCollisionShape;
63  class StaticMeshCollisionShape;
64  class CompoundCollisionShape;
65 
66  // Used by the scripting language binder to help create bindgings for this class.
67  #ifdef SWIG
68  %template(SingletonCollisionShapeManager) Mezzanine::Singleton<CollisionShapeManager>;
69  #endif
70 
71  ///////////////////////////////////////////////////////////////////////////////
72  /// @class CollisionShapeManager
73  /// @headerfile collisionshapemanager.h
74  /// @brief This manager is for the storage of all shapes and creation of mesh shapes.
75  /// @details Collision shapes do not need to be stored in this manager, but can be re-used
76  /// across multiple World objects with physics representations. For performance reasons, it is
77  /// recommended to store and re-use a collision shape anytime you need it in multiple objects,
78  /// rather then re-creating the same shape again and again.
79  ///////////////////////////////////////
81  {
82  public:
83  /// @brief Map container type for CollisionShape storage by this class.
84  typedef std::map< String, CollisionShape* > ShapeMap;
85  /// @brief Map Iterator type for CollisionShape instances stored by this class.
86  typedef ShapeMap::iterator ShapeMapIterator;
87  /// @brief Const Map Iterator type for CollisionShape instances stored by this class.
88  typedef ShapeMap::const_iterator ConstShapeMapIterator;
89  /// @brief Vector container type for CollisionShape storage by this class.
90  typedef std::vector< CollisionShape* > ShapeVector;
91  /// @brief Vector Iterator type for CollisionShape instances stored by this class.
92  typedef ShapeVector::iterator ShapeVectorIterator;
93  /// @brief Const Vector Iterator type for CollisionShape instances stored by this class.
94  typedef ShapeVector::const_iterator ConstShapeVectorIterator;
95  protected:
96  /// @internal
97  /// @brief This stores the names and collision Shapes
98  ShapeMap CollisionShapes;
99  /// @internal
100  /// @brief Stores shapes that have not been given a name.
101  ShapeVector UnnamedShapes;
102 
103  /// @brief Creates a TriMesh to be used in TriMesh based collision shapes.
104  btTriangleMesh* CreateBulletTrimesh(Graphics::Mesh* ObjectMesh, bool UseAllSubmeshes);
105  /// @brief Creates a wrapper for an internal bullet shape.
106  CollisionShape* WrapShape(const String& Name, btCollisionShape* InternalShape);
107  public:
108  /// @brief Class constructor.
110  /// @brief XML constructor.
111  /// @param XMLNode The node of the xml document to construct from.
113  /// @brief Class destructor.
114  virtual ~CollisionShapeManager();
115 
116  ///////////////////////////////////////////////////////////////////////////////
117  // Generic Shape Management
118 
119  /// @brief Stores a pre-made shape in this manager.
120  /// @param Shape The shape to be stored.
121  virtual void StoreShape(CollisionShape* Shape);
122  /// @brief Gets a shape already stored in this manager.
123  /// @param Name the name of the desired shape.
124  /// @return Returns a pointer to the desired shape, or NULL if no such shape exists.
125  virtual CollisionShape* GetShape(const String& Name);
126  /// @brief Gets the number of stored shapes in this manager.
127  /// @return Returns a whole representing how many shapes this manager is storing.
128  virtual Whole GetNumStoredShapes();
129  /// @brief Removes a shape from this manager without deleting it.
130  /// @param Shape Pointer to the shape to be removed.
131  virtual void RemoveShape(CollisionShape* Shape);
132  /// @brief Removes a shape from this manager without deleting it.
133  /// @param Name The name of the shape to be removed.
134  virtual void RemoveShape(const String& Name);
135  /// @brief Removes all shapes from the manager without deleting them.
136  virtual void RemoveAllShapes();
137  /// @brief Removes a shape from this manager and deletes it.
138  /// @param Shape Pointer to the shape to be destroyed.
139  virtual void DestroyShape(CollisionShape* Shape);
140  /// @brief Removes a shape from this manager and deletes it.
141  /// @param Name The name of the shape to be destroyed.
142  virtual void DestroyShape(const String& Name);
143  /// @brief Removes all shapes from the manager and then deletes them.
144  virtual void DestroyAllShapes();
145 
146  /// @brief Gets an Iterator to the first CollisionShape in this manager.
147  /// @return Returns an Iterator to the first CollisionShape.
148  ShapeMapIterator BeginCollisionShape();
149  /// @brief Gets an Iterator to one passed the last CollisionShape in this manager.
150  /// @return Returns an Iterator to one passed the last CollisionShape.
151  ShapeMapIterator EndCollisionShape();
152  /// @brief Gets a Const Iterator to the first CollisionShape in this manager.
153  /// @return Returns a Const Iterator to the first CollisionShape.
154  ConstShapeMapIterator BeginCollisionShape() const;
155  /// @brief Gets a Const Iterator to one passed the last CollisionShape in this manager.
156  /// @return Returns a Const Iterator to one passed the last CollisionShape.
157  ConstShapeMapIterator EndCollisionShape() const;
158 
159  ///////////////////////////////////////////////////////////////////////////////
160  // Shape Creation Utilities
161 
162  /// @brief Generates a Convex Hull from a provided mesh.
163  /// @return Returns a pointer to the created shape.
164  /// @param Name The name to give the created shape.
165  /// @param ObjectMesh The mesh to base this shape off of.
166  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
167  virtual ConvexHullCollisionShape* GenerateConvexHull(const String& Name, Graphics::Mesh* ObjectMesh, bool UseAllSubmeshes = false);
168  /// @brief Generates a Convex Hull from a provided mesh.
169  /// @return Returns a pointer to the created shape.
170  /// @param Name The name to give the created shape.
171  /// @param MeshName The name of the mesh to base this shape off of.
172  /// @param Group The resource group where the mesh can be found.
173  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
174  virtual ConvexHullCollisionShape* GenerateConvexHull(const String& Name, const String& MeshName, const String& Group, bool UseAllSubmeshes = false);
175  /// @brief Generates a mesh shape for dynamic objects.
176  /// @note Dynamic Mesh shapes cannot be scaled per object, only globally. If you are generating this shape and intend to scale it, you will need to make a separate copy of
177  /// the shape for each object you intend to set to a different scaling.
178  /// @return Returns a pointer to the created shape.
179  /// @param Name The name to give the created shape.
180  /// @param ObjectMesh The mesh to base this shape off of.
181  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
182  virtual DynamicMeshCollisionShape* GenerateDynamicTriMesh(const String& Name, Graphics::Mesh* ObjectMesh, bool UseAllSubmeshes = false);
183  /// @brief Generates a mesh shape for dynamic objects.
184  /// @note Dynamic Mesh shapes cannot be scaled per object, only globally. If you are generating this shape and intend to scale it, you will need to make a separate copy of
185  /// the shape for each object you intend to set to a different scaling.
186  /// @return Returns a pointer to the created shape.
187  /// @param Name The name to give the created shape.
188  /// @param MeshName The name of the mesh to base this shape off of.
189  /// @param Group The resource group where the mesh can be found.
190  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
191  virtual DynamicMeshCollisionShape* GenerateDynamicTriMesh(const String& Name, const String& MeshName, const String& Group, bool UseAllSubmeshes = false);
192  /// @brief Generates a mesh shape for static objects.
193  /// @return Returns a pointer to the created shape.
194  /// @param Name The name to give the created shape.
195  /// @param ObjectMesh The mesh to base this shape off of.
196  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
197  virtual StaticMeshCollisionShape* GenerateStaticTriMesh(const String& Name, Graphics::Mesh* ObjectMesh, bool UseAllSubmeshes = false);
198  /// @brief Generates a mesh shape for static objects.
199  /// @return Returns a pointer to the created shape.
200  /// @param Name The name to give the created shape.
201  /// @param MeshName The name of the mesh to base this shape off of.
202  /// @param Group The resource group where the mesh can be found.
203  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
204  virtual StaticMeshCollisionShape* GenerateStaticTriMesh(const String& Name, const String& MeshName, const String& Group, bool UseAllSubmeshes = false);
205  /// @brief Generates a compound shape of Convex Hulls from a provided mesh.
206  /// @note Compound shapes cannot be scaled per object, only globally. If you are generating this shape and intend to scale it, you will need to make a separate copy of
207  /// the shape for each object you intend to set to a different scaling.
208  /// @return Returns a pointer to the created shape.
209  /// @param Name The name to give the created shape.
210  /// @param ObjectMesh The mesh to base this shape off of.
211  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
212  virtual CompoundCollisionShape* PerformConvexDecomposition(const String& Name, Graphics::Mesh* ObjectMesh, Whole Depth, Real CPercent, Real PPercent, bool UseAllSubmeshes = false);
213  /// @brief Generates a compound shape of Convex Hulls from a provided mesh.
214  /// @note Compound shapes cannot be scaled per object, only globally. If you are generating this shape and intend to scale it, you will need to make a separate copy of
215  /// the shape for each object you intend to set to a different scaling.
216  /// @return Returns a pointer to the created shape.
217  /// @param Name The name to give the created shape.
218  /// @param MeshName The name of the mesh to base this shape off of.
219  /// @param Group The resource group where the mesh can be found.
220  /// @param UseAllSubMeshes Whether or not you want to use all submesh information when generating this shape.
221  virtual CompoundCollisionShape* PerformConvexDecomposition(const String& Name, const String& MeshName, const String& Group, Whole Depth, Real CPercent, Real PPercent, bool UseAllSubmeshes = false);
222 
223  ///////////////////////////////////////////////////////////////////////////////
224  // Shape Saving/Loading Utilities
225 
226  /// @brief Loads all shapes saved in an existing XML file, and stores them in this manager.
227  /// @param FileName The name of the XML file to load shapes from.
228  /// @param Group The resource group the .bullet file is located in.
229  virtual void LoadAllShapesFromXMLFile(const String& FileName, const String& Group);
230  /// @brief Takes all the shapes currently stored this manager and saves them to a XML file.
231  /// @param FileName The name of the file to save the shapes to.
232  virtual void SaveAllStoredShapesToXMLFile(const String& FileName);
233  /// @brief Saves all shapes contained in a vector and saves them to a XML file.
234  /// @param FileName The name of the file to save the shapes to.
235  /// @param ShapesToSave A vector of collisions shapes that will be saved.
236  virtual void SaveShapesToXMLFile(const String& FileName, ShapeVector& ShapesToSave);
237 
238  /// @brief Loads all shapes saved in an existing binary file, and stores them in this manager.
239  /// @param FileName The name of the binary file to load shapes from.
240  /// @param Group The resource group the .bullet file is located in.
241  virtual void LoadAllShapesFromBinaryFile(const String& FileName, const String& Group);
242  /// @brief Takes all the shapes currently stored this manager and saves them to a binary file.
243  /// @remarks The binary file the shapes will be saved to is a file generated by the Bullet Physics library, typically with a .bullet extension.
244  /// @param FileName The name of the file to save the shapes to.
245  virtual void SaveAllStoredShapesToBinaryFile(const String& FileName);
246  /// @brief Saves all shapes contained in a vector and saves them to a binary file.
247  /// @remarks The binary file the shapes will be saved to is a file generated by the Bullet Physics library, typically with a .bullet extension.
248  /// @param FileName The name of the file to save the shapes to.
249  /// @param ShapesToSave A vector of collisions shapes that will be saved.
250  virtual void SaveShapesToBinaryFile(const String& FileName, ShapeVector& ShapesToSave);
251 
252  ///////////////////////////////////////////////////////////////////////////////
253  // Unnamed Shape Management
254 
255  /// @brief Returns a vector of unnamed shapes stored in this manager.
256  /// @return Returns a reference to a vector storing all the unnamed shapes loaded from files.
257  /// @details Shapes created in code require a name to be constructed. However, sometimes when loading a file
258  /// a shape may not have a name, since one isn't required by the .bullet file format in order for a shape
259  /// to be serialized. When that happens those shapes go here, and from there can be handled by the game
260  /// programmer however they see fit.
261  ShapeVector& GetUnnamedShapes();
262  /// @brief Assigns a name to an unnamed shape.
263  /// @param NewName The new name to be assigned to a shape.
264  /// @param Shape The shape to be given the new name. This shape must be a valid shape currently stored in the
265  /// set of unnamed shapes. Calling this fucntion will not remove it from that set, but will move it into
266  /// the named collision shape map. If you want the shape removed from the Unnamed set, you must do it yourself.
267  void SetNameForUnnamedShape(const String& NewName, CollisionShape* Shape);
268 
269  ///////////////////////////////////////////////////////////////////////////////
270  // Utility
271 
272  /// @copydoc ManagerBase::Initialize()
273  virtual void Initialize();
274  /// @copydoc ManagerBase::Deinitialize()
275  virtual void Deinitialize();
276 
277  ///////////////////////////////////////////////////////////////////////////////
278  // Type Identifier Methods
279 
280  /// @copydoc ManagerBase::GetInterfaceType()
281  virtual ManagerType GetInterfaceType() const;
282  /// @copydoc ManagerBase::GetImplementationTypeName()
283  virtual String GetImplementationTypeName() const;
284  };// ©ollisionShapeManager
285 
286  ///////////////////////////////////////////////////////////////////////////////
287  /// @class DefaultCollisionShapeManagerFactory
288  /// @headerfile collisionshapemanager.h
289  /// @brief A factory responsible for the creation and destruction of the default collisionshapemanager.
290  ///////////////////////////////////////
292  {
293  public:
294  /// @brief Class constructor.
296  /// @brief Class destructor.
298 
299  /// @copydoc ManagerFactory::GetManagerTypeName()
300  String GetManagerTypeName() const;
301 
302  /// @copydoc ManagerFactory::CreateManager(NameValuePairList&)
303  ManagerBase* CreateManager(NameValuePairList& Params);
304  /// @copydoc ManagerFactory::CreateManager(XML::Node&)
305  ManagerBase* CreateManager(XML::Node& XMLNode);
306  /// @copydoc ManagerFactory::DestroyManager(ManagerBase*)
307  void DestroyManager(ManagerBase* ToBeDestroyed);
308  };//DefaultCollisionShapeManagerFactory
309  }//Physics
310 }//Mezzanine
311 
312 #endif