1 /********************************************************************************
2 * ReactPhysics3D physics library, http://www.reactphysics3d.com                 *
3 * Copyright (c) 2010-2020 Daniel Chappuis                                       *
4 *********************************************************************************
5 *                                                                               *
6 * This software is provided 'as-is', without any express or implied warranty.   *
7 * In no event will the authors be held liable for any damages arising from the  *
8 * use of this software.                                                         *
9 *                                                                               *
10 * Permission is granted to anyone to use this software for any purpose,         *
11 * including commercial applications, and to alter it and redistribute it        *
12 * freely, subject to the following restrictions:                                *
13 *                                                                               *
14 * 1. The origin of this software must not be misrepresented; you must not claim *
15 *    that you wrote the original software. If you use this software in a        *
16 *    product, an acknowledgment in the product documentation would be           *
17 *    appreciated but is not required.                                           *
18 *                                                                               *
19 * 2. Altered source versions must be plainly marked as such, and must not be    *
20 *    misrepresented as being the original software.                             *
21 *                                                                               *
22 * 3. This notice may not be removed or altered from any source distribution.    *
23 *                                                                               *
24 ********************************************************************************/
25 
26 #ifndef REACTPHYSICS3D_PHYSICS_COMMON_H
27 #define REACTPHYSICS3D_PHYSICS_COMMON_H
28 
29 // Libraries
30 #include <reactphysics3d/memory/MemoryManager.h>
31 #include <reactphysics3d/engine/PhysicsWorld.h>
32 #include <reactphysics3d/collision/shapes/SphereShape.h>
33 #include <reactphysics3d/collision/shapes/BoxShape.h>
34 #include <reactphysics3d/collision/shapes/CapsuleShape.h>
35 #include <reactphysics3d/collision/shapes/HeightFieldShape.h>
36 #include <reactphysics3d/collision/shapes/ConvexMeshShape.h>
37 #include <reactphysics3d/collision/shapes/ConcaveMeshShape.h>
38 #include <reactphysics3d/collision/TriangleMesh.h>
39 #include <reactphysics3d/utils/DefaultLogger.h>
40 
41 /// ReactPhysics3D namespace
42 namespace reactphysics3d {
43 
44 // Class PhysicsCommon
45 /**
46  * This class is a singleton that needs to be instanciated once at the beginning.
47  * Then this class is used by the user as a factory to create the physics world and
48  * other objects.
49  */
50 class PhysicsCommon {
51 
52     private :
53 
54         // -------------------- Attributes -------------------- //
55 
56         /// Memory manager
57         MemoryManager mMemoryManager;
58 
59         /// Set of physics worlds
60         Set<PhysicsWorld*> mPhysicsWorlds;
61 
62         /// Set of sphere shapes
63         Set<SphereShape*> mSphereShapes;
64 
65         /// Set of box shapes
66         Set<BoxShape*> mBoxShapes;
67 
68         /// Set of capsule shapes
69         Set<CapsuleShape*> mCapsuleShapes;
70 
71         /// Set of convex mesh shapes
72         Set<ConvexMeshShape*> mConvexMeshShapes;
73 
74         /// Set of concave mesh shapes
75         Set<ConcaveMeshShape*> mConcaveMeshShapes;
76 
77         /// Set of height field shapes
78         Set<HeightFieldShape*> mHeightFieldShapes;
79 
80         /// Set of polyhedron meshes
81         Set<PolyhedronMesh*> mPolyhedronMeshes;
82 
83         /// Set of triangle meshes
84         Set<TriangleMesh*> mTriangleMeshes;
85 
86         /// Pointer to the current logger
87         static Logger* mLogger;
88 
89         /// Set of profilers
90         Set<Profiler*> mProfilers;
91 
92         /// Set of default loggers
93         Set<DefaultLogger*> mDefaultLoggers;
94 
95         // -------------------- Methods -------------------- //
96 
97         /// Destroy and release everything that has been allocated
98         void release();
99 
100 // If profiling is enabled
101 #ifdef IS_RP3D_PROFILING_ENABLED
102 
103         /// Create and return a new profiler
104         Profiler* createProfiler();
105 
106         /// Destroy a profiler
107         void destroyProfiler(Profiler* profiler);
108 
109 #endif
110 
111     public :
112 
113         // -------------------- Methods -------------------- //
114 
115         /// Constructor
116         PhysicsCommon(MemoryAllocator* baseMemoryAllocator = nullptr);
117 
118         /// Destructor
119         ~PhysicsCommon();
120 
121         /// Create and return an instance of PhysicsWorld
122         PhysicsWorld* createPhysicsWorld(const PhysicsWorld::WorldSettings& worldSettings = PhysicsWorld::WorldSettings());
123 
124         /// Destroy an instance of PhysicsWorld
125         void destroyPhysicsWorld(PhysicsWorld* world);
126 
127         /// Create and return a sphere collision shape
128         SphereShape* createSphereShape(const decimal radius);
129 
130         /// Destroy a sphere collision shape
131         void destroySphereShape(SphereShape* sphereShape);
132 
133         /// Create and return a box collision shape
134         BoxShape* createBoxShape(const Vector3& extent);
135 
136         /// Destroy a box collision shape
137         void destroyBoxShape(BoxShape* boxShape);
138 
139         /// Create and return a capsule shape
140         CapsuleShape* createCapsuleShape(decimal radius, decimal height);
141 
142         /// Destroy a capsule collision shape
143         void destroyCapsuleShape(CapsuleShape* capsuleShape);
144 
145         /// Create and return a convex mesh shape
146         ConvexMeshShape* createConvexMeshShape(PolyhedronMesh* polyhedronMesh, const Vector3& scaling = Vector3(1,1,1));
147 
148         /// Destroy a convex mesh shape
149         void destroyConvexMeshShape(ConvexMeshShape* convexMeshShape);
150 
151         /// Create and return a height-field shape
152         HeightFieldShape* createHeightFieldShape(int nbGridColumns, int nbGridRows, decimal minHeight, decimal maxHeight,
153                                                  const void* heightFieldData, HeightFieldShape::HeightDataType dataType,
154                                                  int upAxis = 1, decimal integerHeightScale = 1.0f,
155                                                   const Vector3& scaling = Vector3(1,1,1));
156 
157         /// Destroy a height-field shape
158         void destroyHeightFieldShape(HeightFieldShape* heightFieldShape);
159 
160         /// Create and return a concave mesh shape
161         ConcaveMeshShape* createConcaveMeshShape(TriangleMesh* triangleMesh, const Vector3& scaling = Vector3(1, 1, 1));
162 
163         /// Destroy a concave mesh shape
164         void destroyConcaveMeshShape(ConcaveMeshShape* concaveMeshShape);
165 
166         /// Create a polyhedron mesh
167         PolyhedronMesh* createPolyhedronMesh(PolygonVertexArray* polygonVertexArray);
168 
169         /// Destroy a polyhedron mesh
170         void destroyPolyhedronMesh(PolyhedronMesh* polyhedronMesh);
171 
172         /// Create a triangle mesh
173         TriangleMesh* createTriangleMesh();
174 
175         /// Destroy a triangle mesh
176         void destroyTriangleMesh(TriangleMesh* triangleMesh);
177 
178         /// Create and return a new default logger
179         DefaultLogger* createDefaultLogger();
180 
181         /// Destroy a default logger
182         void destroyDefaultLogger(DefaultLogger* logger);
183 
184         /// Return the current logger
185         static Logger* getLogger();
186 
187         /// Set the logger
188         static void setLogger(Logger* logger);
189 
190 };
191 
192 // Return the current logger
193 /**
194  * @return A pointer to the current logger
195  */
getLogger()196 inline Logger* PhysicsCommon::getLogger() {
197     return mLogger;
198 }
199 
200 // Set the logger
201 /**
202  * @param logger A pointer to the logger to use
203  */
setLogger(Logger * logger)204 inline void PhysicsCommon::setLogger(Logger* logger) {
205     mLogger = logger;
206 }
207 
208 // Use this macro to log something
209 #define RP3D_LOG(physicsWorldName, level, category, message, filename, lineNumber) if (reactphysics3d::PhysicsCommon::getLogger() != nullptr) PhysicsCommon::getLogger()->log(level, physicsWorldName, category, message, filename, lineNumber)
210 
211 }
212 
213 #endif
214