Atoms Crowd  7.0.0
AtomsPhysx.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 #ifndef ATOMS_UNREAL
10 #include <Atoms/Globals.h>
11 #include <AtomsCore/AtomsMath.h>
12 #include <Atoms/Physx/RigidBody.h>
13 #include <Atoms/Physx/RigidBodyConstraint.h>
14 #include <string>
15 #include <unordered_map>
16 #include <mutex>
17 
18 namespace physx
19 {
20  class PxFoundation;
21  class PxPhysics;
22  class PxDefaultCpuDispatcher;
23  class PxScene;
24  class PxPvd;
25  class PxRigidActor;
26  class PxCooking;
27  class PxAggregate;
28  class PxMaterial;
29  class PxHeightField;
30  class PxCudaContextManager;
31 }
32 
33 namespace Atoms
34 {
35  class ContactFilterCallback;
36  class ContactModifyCallback;
37  class AtomsCCDContactModification;
38 
39  class ATOMS_EXPORT AtomsPhysx
40  {
41  public:
42 
44  {
45  std::unordered_map<int, std::vector<int>> aggregates;
46  std::unordered_map<int, bool> aggregateEnableCollisions;
47  };
48 
50  {
51  size_t id;
52  RigidBodyAttributes attributes;
53  AtomsUtils::Mesh* mesh;
54  };
55 
57  {
59  Atoms::RigidBody* firstRb;
60  AtomsCore::Matrixf fMatrix;
61  Atoms::RigidBody* secondRb;
62  AtomsCore::Matrixf sMatrix;
63  std::string name;
64  bool lateCompute;
65  };
66 
67  enum SolverType
68  {
69  kPGS = 0,
70  kTGS
71  };
72 
73  AtomsPhysx();
74 
75  ~AtomsPhysx();
76 
77  bool isValid() const { return m_physics != nullptr; }
78 
79  void init();
80 
81  void release();
82 
83  void initScene();
84 
85  void releaseScene();
86 
87  void updateKinematics(float stepSize);
88 
89  void updateDynamicPool(float stepSize);
90 
91  void updateKinematicAgents(float stepSize);
92 
93  void updatedForces(float stepSize);
94 
95  void clearForces();
96 
97  void clearFeedbacks();
98 
99  void storeTransforms();
100 
101  void fetchStates();
102 
103  void storeStates();
104 
105  void updateConstraints();
106 
107  void createLazyData();
108 
109  void createDynamicConstraintData();
110 
111  void updateForceCollisionThreshold();
112 
113  void stepPhysics();
114 
115  //Attributes Pvd
116 
117  const std::string& pvdHost() const;
118 
119  void setPvdHost(const std::string& host);
120 
121  int pvdPort() const;
122 
123  void setPvdPort(int value);
124 
125  unsigned int pvdTimeout() const;
126 
127  void setPvdTimeout(unsigned int value);
128 
129 
130  // Sim attributes
131 
132  double fps() const;
133 
134  void setFps(double value);
135 
136  unsigned int subSamples() const;
137 
138  void setSubSamples(unsigned int value);
139 
140  double scale() const;
141 
142  void setScale(double value);
143 
144  const AtomsCore::Vector3& gravity() const;
145 
146  void setGravity(const AtomsCore::Vector3& value);
147 
148  void enablePvd(bool value);
149 
150  bool isPvdEnabled() const;
151 
152  bool isInitialized() const;
153 
154  unsigned int threadsNumber() const;
155 
156  void setThreadsNumber(unsigned int value);
157 
158  void setFrictionOffsetThreshold(double value);
159 
160  void setCcdMaxSeparation(double value);
161 
162  void setSolverOffsetSlop(double value);
163 
164  void setBounceThresholdVelocity(double value);
165 
166  short solverType() const;
167 
168  void setSolverType(short value);
169 
170  void setKinematicKinematicFilter(bool value);
171 
172  void setStaticKinematicFilter(bool value);
173 
174  bool useGpu() const;
175 
176  void setUseGpu(bool value);
177 
178  bool enableStabilization() const;
179 
180  void setEnableStabilization(bool value);
181 
182  bool enableEnhancedDeterminism() const;
183 
184  void setEnableEnhancedDeterminism(bool value);
185 
186  bool enableFrictionEveryIteration() const;
187 
188  void setEnableFrictionEveryIteration(bool value);
189 
190  bool enablePCM() const;
191 
192  void setEnablePCM(bool value);
193 
194  bool enableAdaptiveForce() const;
195 
196  void setEnableAdaptiveForce(bool value);
197 
198  bool enableCCD() const;
199 
200  void setEnableCCD(bool value);
201 
202  double elapsedTime() const;
203 
204  // Rbs
205  RigidBody* addRigidBody(
206  const std::string& name,
207  const RigidBodyAttributes& attributes,
208  AtomsUtils::Mesh* mesh = nullptr,
209  bool lazyCreation = false);
210 
211  void rigidBodyPose(const std::string& name, AtomsCore::Vector3f& pos, AtomsCore::Quaternionf& rot) const;
212 
213  void rigidBodyPose(size_t id, AtomsCore::Vector3f& pos, AtomsCore::Quaternionf& rot) const;
214 
215  std::unordered_map<size_t, RigidBody>& rigidBodies();
216 
217  const RigidBody* rigidBody(size_t id) const;
218 
219  const std::unordered_map<size_t, RigidBody>& rigidBodies() const;
220 
221  const std::unordered_map<size_t, std::string>& rigidBodyNames() const;
222 
223  const std::map<std::string, size_t>& rigidBodyNameIds() const;
224 
225  std::map<std::string, RigidBodyConstraint>& constraints();
226 
227  const std::map<std::string, RigidBodyConstraint>& constraints() const;
228 
229  const std::map<std::pair<size_t, size_t>, std::string>& constraintNames() const;
230 
231  physx::PxRigidActor* createPhysicObject(size_t id, const RigidBodyAttributes& attributes, physx::PxGeometry* geo, const physx::PxTransform* shapeOffset);
232 
233  physx::PxRigidActor* createRigidBody(size_t id, const RigidBodyAttributes& attributes, AtomsUtils::Mesh* mesh=nullptr);
234 
235  void setupDynamicAttributes(physx::PxRigidDynamic* rigid, const RigidBodyAttributes& attributes) const;
236 
237  void setupKinematicAttributes(physx::PxRigidDynamic* rigid, const RigidBodyAttributes& attributes) const;
238 
239  void updateRigidBodyMeshPoints(RigidBody& rb, const std::vector<AtomsCore::Vector3f>& newPoints);
240 
241  // Constraints
242 
243  RigidBodyConstraint* addRigidBodyConstraint(
244  const std::string& name,
245  const RigidBodyConstraintAttributes& attributes,
246  int first,
247  int second,
248  const AtomsCore::Matrixf& matrix);
249 
250  RigidBodyConstraint* addRigidBodyConstraint(
251  const std::string& name,
252  const RigidBodyConstraintAttributes& attributes,
253  Atoms::RigidBody* firstRb,
254  const AtomsCore::Matrixf& fMatrix,
255  Atoms::RigidBody* secondRb,
256  const AtomsCore::Matrixf& sMatrix,
257  bool lazyCreation = false);
258 
259  RigidBodyConstraint* addRigidBodyDynamicConstraint(
260  const std::string& name,
261  const RigidBodyConstraintAttributes& attributes,
262  Atoms::RigidBody* firstRb,
263  const AtomsCore::Matrixf& fMatrix,
264  Atoms::RigidBody* secondRb,
265  const AtomsCore::Matrixf& sMatrix);
266 
267  physx::PxJoint* createRigidBodyConstraint(
268  const RigidBodyConstraintAttributes& attributes,
269  physx::PxRigidActor* first,
270  physx::PxTransform& firstMatrix,
271  physx::PxRigidActor* second,
272  physx::PxTransform& secondMatrix);
273 
274  void addAggregates(
275  const std::unordered_map<int, std::vector<int>> &aggregates,
276  const std::unordered_map<int, bool> &aggregateEnableCollisions,
277  size_t id = 0,
278  bool lazyCreation = false);
279 
280  void removeRigidBody(size_t id);
281 
282  void removeConstraint(const std::string& name);
283 
284  private:
285 #ifndef VS2012
286  AtomsPhysx(const AtomsPhysx&) = delete;
287  AtomsPhysx& operator=(const AtomsPhysx&) = delete;
288 #else
289  AtomsPhysx(const AtomsPhysx&);
290  AtomsPhysx& operator=(const AtomsPhysx&);
291 #endif
292  private:
293 
294  std::unordered_map<size_t, RigidBody> m_rbs;
295 
296  std::unordered_map<size_t, std::string> m_rbNames;
297 
298  std::map<std::string, size_t> m_rbNameIds;
299 
300  std::map<std::string, RigidBodyConstraint> m_constraints;
301 
302  std::map<std::pair<size_t, size_t>, std::string> m_constraintNames;
303 
304  std::vector<physx::PxAggregate *> m_aggregates;
305 
306  std::vector<physx::PxHeightField*> m_heigthFields;
307 
308  physx::PxFoundation* m_foundation;
309 
310  physx::PxPhysics* m_physics;
311 
312  physx::PxCooking* m_cooking;
313 #ifndef ATOMS_UNREAL
314  physx::PxCudaContextManager* m_cudaContextManager;
315 #endif
316  physx::PxDefaultCpuDispatcher* m_dispatcher;
317 
318  physx::PxScene* m_scene;
319 
320  physx::PxPvd* m_pvd;
321 
322  std::string m_pvdHost;
323 
324  int m_pvdPort;
325 
326  unsigned int m_pvdTimeout;
327 
328  double m_fps;
329 
330  unsigned int m_subSamples;
331 
332  double m_scale;
333 
334  AtomsCore::Vector3 m_gravity;
335 
336  int m_threadsNumber;
337 
338  unsigned int m_maxThreads;
339 
340  double m_frictionOffsetThreshold;
341 
342  double m_ccdMaxSeparation;
343 
344  double m_solverOffsetSlop;
345 
346  double m_bounceThresholdVelocity;
347 
348  short m_solverType;
349 
350  bool m_isInitialized;
351 
352  bool m_activePvd;
353 
354  bool m_filterKinematicKinematicCollision;
355 
356  bool m_filterStaticKinematicCollision;
357 
358  std::atomic<size_t> m_freeId;
359 
360  AtomsUPtr<ContactFilterCallback> m_contactFilterCallback;
361 
362  AtomsUPtr<ContactModifyCallback> m_contactModifyCallback;
363 
364  AtomsUPtr<AtomsCCDContactModification> m_ccdContactModifyCallback;
365 
366  mutable std::mutex m_mutex;
367 
368  std::map<size_t, LazyAggregateData> m_lazyAggregates;
369 
370  std::map<std::string, LazyRigidBodyData> m_lazyRigidBodies;
371 
372  std::map<std::string, LazyRigidBodyConstraintData> m_lazyConstraints;
373 
374  std::map<size_t, physx::PxMaterial*> m_lazyMaterials;
375 
376  bool m_useGpu;
377 
378  bool m_enableStabilization;
379 
380  bool m_enableEnhancedDeterminism;
381 
382  bool m_enableFrictionEveryIteration;
383 
384  bool m_enablePCM;
385 
386  bool m_enableAdaptiveForce;
387 
388  bool m_enableCCD;
389 
390  double m_elapsedTime;
391  };
392 }
393 #endif
Definition: AtomsPhysx.h:40
Definition: RigidBody.h:40
Definition: RigidBodyConstraint.h:23
Definition: RigidBody.h:155
Mesh class.
Definition: Mesh.h:30
AtomsMath::Matrixf Matrixf
Matrix class.
Definition: AtomsMath.h:64
AtomsMath::Vector3 Vector3
Vector3 class.
Definition: AtomsMath.h:57
AtomsMath::Vector3f Vector3f
Vector3 class.
Definition: AtomsMath.h:58
AtomsMath::Quaternionf Quaternionf
Quaternion class.
Definition: AtomsMath.h:68
Atoms namespace.
Definition: Agent.h:29
Definition: AtomsPhysx.h:44
Definition: AtomsPhysx.h:50