Spinning Topp Logo BlackTopp Studios
inc
collisionshapemanager.h
1 // © Copyright 2010 - 2016 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 "datatypes.h"
44 #include "Physics/collisionshape.h"
45 #ifndef SWIG
46  #include "entresolmanager.h"
47  #include "entresolmanagerfactory.h"
48 #endif
49 #include "singleton.h"
50 
51 class btTriangleMesh;
52 class btCollisionShape;
53 
54 namespace Mezzanine
55 {
56  // Used by the scripting language binder to help create bindgings for this class.
57  #ifdef SWIG
58  %template(SingletonCollisionShapeManager) Mezzanine::Singleton<CollisionShapeManager>;
59  #endif
60 
61  namespace Graphics
62  {
63  class Mesh;
64  }
65  namespace Physics
66  {
67  class CollisionShape;
68  class ConvexHullCollisionShape;
69  class DynamicMeshCollisionShape;
70  class StaticMeshCollisionShape;
71  class CompoundCollisionShape;
72 
73 
74  ///////////////////////////////////////////////////////////////////////////////
75  /// @headerfile collisionshapemanager.h
76  /// @brief This manager is for the storage of all shapes and creation of mesh shapes.
77  /// @details Collision shapes do not need to be stored in this manager, but can be re-used
78  /// across multiple World objects with physics representations. For performance reasons, it is
79  /// recommended to store and re-use a collision shape anytime you need it in multiple objects,
80  /// rather then re-creating the same shape again and again.
81  ///////////////////////////////////////
83  {
84  public:
85  /// @brief Map container type for CollisionShape storage by this class.
86  typedef std::map< String, CollisionShape* > ShapeMap;
87  /// @brief Map Iterator type for CollisionShape instances stored by this class.
88  typedef ShapeMap::iterator ShapeMapIterator;
89  /// @brief Const Map Iterator type for CollisionShape instances stored by this class.
90  typedef ShapeMap::const_iterator ConstShapeMapIterator;
91  /// @brief Vector container type for CollisionShape storage by this class.
92  typedef std::vector< CollisionShape* > ShapeVector;
93  /// @brief Vector Iterator type for CollisionShape instances stored by this class.
94  typedef ShapeVector::iterator ShapeVectorIterator;
95  /// @brief Const Vector Iterator type for CollisionShape instances stored by this class.
96  typedef ShapeVector::const_iterator ConstShapeVectorIterator;
97 
98  /// @brief A String containing the name of this manager implementation.
99  static const String ImplementationName;
100  /// @brief A ManagerType enum value used to describe the type of interface/functionality this manager provides.
102  protected:
103  /// @internal
104  /// @brief This stores the names and collision Shapes
105  ShapeMap CollisionShapes;
106  /// @internal
107  /// @brief Stores shapes that have not been given a name.
108  ShapeVector UnnamedShapes;
109 
110  /// @brief Creates a TriMesh to be used in TriMesh based collision shapes.
111  btTriangleMesh* CreateBulletTrimesh(Graphics::Mesh* ObjectMesh, Boole UseAllSubmeshes);
112  /// @brief Creates a wrapper for an internal bullet shape.
113  CollisionShape* WrapShape(const String& Name, btCollisionShape* InternalShape);
114  public:
115  /// @brief Class constructor.
117  /// @brief XML constructor.
118  /// @param XMLNode The node of the xml document to construct from.
119  CollisionShapeManager(const XML::Node& XMLNode);
120  /// @brief Class destructor.
121  virtual ~CollisionShapeManager();
122 
123  ///////////////////////////////////////////////////////////////////////////////
124  // Generic Shape Management
125 
126  /// @brief Stores a pre-made shape in this manager.
127  /// @param Shape The shape to be stored.
128  virtual void StoreShape(CollisionShape* Shape);
129  /// @brief Gets a shape already stored in this manager.
130  /// @param Name the name of the desired shape.
131  /// @return Returns a pointer to the desired shape, or NULL if no such shape exists.
132  virtual CollisionShape* GetShape(const String& Name);
133  /// @brief Gets the number of stored shapes in this manager.
134  /// @return Returns a whole representing how many shapes this manager is storing.
135  virtual Whole GetNumStoredShapes();
136  /// @brief Removes a shape from this manager without deleting it.
137  /// @param Shape Pointer to the shape to be removed.
138  virtual void RemoveShape(CollisionShape* Shape);
139  /// @brief Removes a shape from this manager without deleting it.
140  /// @param Name The name of the shape to be removed.
141  virtual void RemoveShape(const String& Name);
142  /// @brief Removes all shapes from the manager without deleting them.
143  virtual void RemoveAllShapes();
144  /// @brief Removes a shape from this manager and deletes it.
145  /// @param Shape Pointer to the shape to be destroyed.
146  virtual void DestroyShape(CollisionShape* Shape);
147  /// @brief Removes a shape from this manager and deletes it.
148  /// @param Name The name of the shape to be destroyed.
149  virtual void DestroyShape(const String& Name);
150  /// @brief Removes all shapes from the manager and then deletes them.
151  virtual void DestroyAllShapes();
152 
153  /// @brief Gets an Iterator to the first CollisionShape in this manager.
154  /// @return Returns an Iterator to the first CollisionShape.
155  ShapeMapIterator BeginCollisionShape();
156  /// @brief Gets an Iterator to one passed the last CollisionShape in this manager.
157  /// @return Returns an Iterator to one passed the last CollisionShape.
158  ShapeMapIterator EndCollisionShape();
159  #ifndef SWIG
160  /// @brief Gets a Const Iterator to the first CollisionShape in this manager.
161  /// @return Returns a Const Iterator to the first CollisionShape.
162  ConstShapeMapIterator BeginCollisionShape() const;
163  /// @brief Gets a Const Iterator to one passed the last CollisionShape in this manager.
164  /// @return Returns a Const Iterator to one passed the last CollisionShape.
165  ConstShapeMapIterator EndCollisionShape() const;
166  #endif
167  ///////////////////////////////////////////////////////////////////////////////
168  // Shape Creation Utilities
169 
170  /// @brief Generates a Convex Hull from a provided mesh.
171  /// @return Returns a pointer to the created shape.
172  /// @param Name The name to give the created shape.
173  /// @param ObjectMesh The mesh to base this shape off of.
174  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
175  virtual ConvexHullCollisionShape* GenerateConvexHull(const String& Name, Graphics::Mesh* ObjectMesh, Boole UseAllSubmeshes = false);
176  /// @brief Generates a Convex Hull from a provided mesh.
177  /// @return Returns a pointer to the created shape.
178  /// @param Name The name to give the created shape.
179  /// @param MeshName The name of the mesh to base this shape off of.
180  /// @param Group The resource group where the mesh can be found.
181  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
182  virtual ConvexHullCollisionShape* GenerateConvexHull(const String& Name, const String& MeshName, const String& Group, Boole 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 ObjectMesh The mesh to base this shape off of.
189  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
190  virtual DynamicMeshCollisionShape* GenerateDynamicTriMesh(const String& Name, Graphics::Mesh* ObjectMesh, Boole UseAllSubmeshes = false);
191  /// @brief Generates a mesh shape for dynamic objects.
192  /// @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
193  /// the shape for each object you intend to set to a different scaling.
194  /// @return Returns a pointer to the created shape.
195  /// @param Name The name to give the created shape.
196  /// @param MeshName The name of the mesh to base this shape off of.
197  /// @param Group The resource group where the mesh can be found.
198  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
199  virtual DynamicMeshCollisionShape* GenerateDynamicTriMesh(const String& Name, const String& MeshName, const String& Group, Boole UseAllSubmeshes = false);
200  /// @brief Generates a mesh shape for static objects.
201  /// @return Returns a pointer to the created shape.
202  /// @param Name The name to give the created shape.
203  /// @param ObjectMesh The mesh to base this shape off of.
204  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
205  virtual StaticMeshCollisionShape* GenerateStaticTriMesh(const String& Name, Graphics::Mesh* ObjectMesh, Boole UseAllSubmeshes = false);
206  /// @brief Generates a mesh shape for static objects.
207  /// @return Returns a pointer to the created shape.
208  /// @param Name The name to give the created shape.
209  /// @param MeshName The name of the mesh to base this shape off of.
210  /// @param Group The resource group where the mesh can be found.
211  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
212  virtual StaticMeshCollisionShape* GenerateStaticTriMesh(const String& Name, const String& MeshName, const String& Group, Boole 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 ObjectMesh The mesh to base this shape off of.
219  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
220  virtual CompoundCollisionShape* PerformConvexDecomposition(const String& Name, Graphics::Mesh* ObjectMesh, Whole Depth, Real CPercent, Real PPercent, Boole UseAllSubmeshes = false);
221  /// @brief Generates a compound shape of Convex Hulls from a provided mesh.
222  /// @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
223  /// the shape for each object you intend to set to a different scaling.
224  /// @return Returns a pointer to the created shape.
225  /// @param Name The name to give the created shape.
226  /// @param MeshName The name of the mesh to base this shape off of.
227  /// @param Group The resource group where the mesh can be found.
228  /// @param UseAllSubmeshes Whether or not you want to use all submesh information when generating this shape.
229  virtual CompoundCollisionShape* PerformConvexDecomposition(const String& Name, const String& MeshName, const String& Group, Whole Depth, Real CPercent, Real PPercent, Boole UseAllSubmeshes = false);
230 
231  ///////////////////////////////////////////////////////////////////////////////
232  // Shape Saving/Loading Utilities
233 
234  /// @brief Loads all shapes saved in an existing XML file, and stores them in this manager.
235  /// @param FileName The name of the XML file to load shapes from.
236  /// @param Group The resource group the .bullet file is located in.
237  virtual void LoadAllShapesFromXMLFile(const String& FileName, const String& Group);
238  /// @brief Takes all the shapes currently stored this manager and saves them to a XML file.
239  /// @param FileName The name of the file to save the shapes to.
240  virtual void SaveAllStoredShapesToXMLFile(const String& FileName);
241  /// @brief Saves all shapes contained in a vector and saves them to a XML file.
242  /// @param FileName The name of the file to save the shapes to.
243  /// @param ShapesToSave A vector of collisions shapes that will be saved.
244  virtual void SaveShapesToXMLFile(const String& FileName, ShapeVector& ShapesToSave);
245 
246  /// @brief Loads all shapes saved in an existing binary file, and stores them in this manager.
247  /// @param FileName The name of the binary file to load shapes from.
248  /// @param Group The resource group the .bullet file is located in.
249  virtual void LoadAllShapesFromBinaryFile(const String& FileName, const String& Group);
250  /// @brief Takes all the shapes currently stored this manager and saves them to a binary file.
251  /// @remarks The binary file the shapes will be saved to is a file generated by the Bullet Physics library, typically with a .bullet extension.
252  /// @param FileName The name of the file to save the shapes to.
253  virtual void SaveAllStoredShapesToBinaryFile(const String& FileName);
254  /// @brief Saves all shapes contained in a vector and saves them to a binary file.
255  /// @remarks The binary file the shapes will be saved to is a file generated by the Bullet Physics library, typically with a .bullet extension.
256  /// @param FileName The name of the file to save the shapes to.
257  /// @param ShapesToSave A vector of collisions shapes that will be saved.
258  virtual void SaveShapesToBinaryFile(const String& FileName, ShapeVector& ShapesToSave);
259 
260  ///////////////////////////////////////////////////////////////////////////////
261  // Unnamed Shape Management
262 
263  /// @brief Returns a vector of unnamed shapes stored in this manager.
264  /// @return Returns a reference to a vector storing all the unnamed shapes loaded from files.
265  /// @details Shapes created in code require a name to be constructed. However, sometimes when loading a file
266  /// a shape may not have a name, since one isn't required by the .bullet file format in order for a shape
267  /// to be serialized. When that happens those shapes go here, and from there can be handled by the game
268  /// programmer however they see fit.
269  ShapeVector& GetUnnamedShapes();
270  /// @brief Assigns a name to an unnamed shape.
271  /// @param NewName The new name to be assigned to a shape.
272  /// @param Shape The shape to be given the new name. This shape must be a valid shape currently stored in the
273  /// set of unnamed shapes. Calling this fucntion will not remove it from that set, but will move it into
274  /// the named collision shape map. If you want the shape removed from the Unnamed set, you must do it yourself.
275  void SetNameForUnnamedShape(const String& NewName, CollisionShape* Shape);
276 
277  ///////////////////////////////////////////////////////////////////////////////
278  // Utility
279 
280  /// @copydoc ManagerBase::Initialize()
281  virtual void Initialize();
282  /// @copydoc ManagerBase::Deinitialize()
283  virtual void Deinitialize();
284 
285  ///////////////////////////////////////////////////////////////////////////////
286  // Type Identifier Methods
287 
288  /// @copydoc ManagerBase::GetInterfaceType()
289  virtual ManagerBase::ManagerType GetInterfaceType() const;
290  /// @copydoc ManagerBase::GetImplementationTypeName()
291  virtual String GetImplementationTypeName() const;
292  };// CollisionShapeManager
293 
294  ///////////////////////////////////////////////////////////////////////////////
295  /// @class DefaultCollisionShapeManagerFactory
296  /// @brief A factory responsible for the creation and destruction of the default collisionshapemanager.
297  ///////////////////////////////////////
299  {
300  public:
301  /// @brief Class constructor.
303  /// @brief Class destructor.
305 
306  /// @copydoc ManagerFactory::GetManagerImplName()
307  String GetManagerImplName() const;
308  /// @copydoc ManagerFactory::GetManagerType() const
309  ManagerBase::ManagerType GetManagerType() const;
310 
311  /// @copydoc EntresolManagerFactory::CreateManager(const NameValuePairList&)
312  EntresolManager* CreateManager(const NameValuePairList& Params);
313  /// @copydoc EntresolManagerFactory::CreateManager(const XML::Node&)
314  EntresolManager* CreateManager(const XML::Node& XMLNode);
315  /// @copydoc EntresolManagerFactory::DestroyManager(EntresolManager*)
316  void DestroyManager(EntresolManager* ToBeDestroyed);
317  };//DefaultCollisionShapeManagerFactory
318  }//Physics
319 }//Mezzanine
320 
321 #endif
This is the base class for all collision shapes.
A triangle mesh collsion shape based on a graphics mesh.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
ManagerType
A listing of Manager Types.
Definition: managerbase.h:65
This is a base class for factories that construct managers used by the Entresol class.
static const String ImplementationName
A String containing the name of this manager implementation.
static const ManagerBase::ManagerType InterfaceType
A ManagerType enum value used to describe the type of interface/functionality this manager provides...
ShapeVector::iterator ShapeVectorIterator
Vector Iterator type for CollisionShape instances stored by this class.
This class is used to check and modify the properties of a graphics mesh.
Definition: mesh.h:63
All the definitions for datatypes as well as some basic conversion functions are defined here...
A triangle mesh collsion shape based on a graphics mesh.
A simple convex shape built from a low number of points in local space.
ShapeMap CollisionShapes
This stores the names and collision Shapes.
std::vector< CollisionShape * > ShapeVector
Vector container type for CollisionShape storage by this class.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
A factory responsible for the creation and destruction of the default collisionshapemanager.
This is the base class for all managers that do no describe properties of a single world...
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
ShapeVector::const_iterator ConstShapeVectorIterator
Const Vector Iterator type for CollisionShape instances stored by this class.
ProcessDepth Depth
The current process depth as interpretted by Main.
Definition: mezztest.cpp:82
std::list< NameValuePair > NameValuePairList
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:206
ShapeMap::const_iterator ConstShapeMapIterator
Const Map Iterator type for CollisionShape instances stored by this class.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
ShapeVector UnnamedShapes
Stores shapes that have not been given a name.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
ShapeMap::iterator ShapeMapIterator
Map Iterator type for CollisionShape instances stored by this class.
std::map< String, CollisionShape * > ShapeMap
Map container type for CollisionShape storage by this class.
This manager is for the storage of all shapes and creation of mesh shapes.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
A collision shape composed of many other collision shapes placed and oriented in local space...