AGX Dynamics  2.35.0.1
WireSimulationProxy.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agxSDK/Simulation.h>
20 #include <agx/StrongInteraction.h>
21 
23 
24 
25 namespace agxWire
26 {
31  template< typename T >
32  class SimulationProxyT : public T
33  {
34  public:
35  agxSDK::SimulationProxy* getSimulationProxy() const { return m_simulationProxy; }
36 
37  protected:
38  SimulationProxyT()
39  : m_simulationProxy( agxSDK::Simulation::createDefaultProxy( nullptr ) ) {}
40 
41  virtual ~SimulationProxyT() {}
42 
46  virtual void addNotification( agxSDK::Simulation* simulation ) { handleAddNotification( simulation ); }
47 
51  virtual void removeNotification( agxSDK::Simulation* simulation ) { handleRemoveNotification( simulation ); }
52 
56  virtual void addNotification() { handleAddNotification( getSimulation() ); }
57 
61  virtual void removeNotification() { handleRemoveNotification( getSimulation() ); }
62 
67  virtual void handleAddNotification( agxSDK::Simulation* simulation )
68  {
69  if ( simulation != nullptr ) {
70  simulation->commit( m_simulationProxy );
71  m_simulationProxy = simulation->getSimulationProxy();
72  }
73  }
74 
79  virtual void handleRemoveNotification( agxSDK::Simulation* /*simulation*/ )
80  {
81  agxAssert( m_simulationProxy->getSimulation() == nullptr || m_simulationProxy->empty() );
82  m_simulationProxy = agxSDK::Simulation::createDefaultProxy( nullptr );
83  }
84 
85  protected:
86  virtual void addNotification(agxSDK::Assembly*) {};
87  virtual void removeNotification(agxSDK::Assembly*) {};
88 
89  private:
90  agxSDK::Simulation* getSimulation() { return T::getSimulation(); }
91  const agxSDK::Simulation* getSimulation() const { return T::getSimulation(); }
92 
93 
94 
95  private:
96  agxSDK::SimulationProxyRef m_simulationProxy;
97  };
98 
102  class AGXPHYSICS_EXPORT SimulationProxy : public agxSDK::SimulationProxy
103  {
104  public:
109  SimulationProxy( agxSDK::Simulation* simulation );
110 
115  virtual bool add( agxCollide::Geometry* geometry ) override;
116 
121  virtual bool remove( agxCollide::Geometry* geometry ) override;
122 
127  virtual void setEnable( agxCollide::Geometry* geometry, bool enable ) override;
128 
133  virtual void addGroup( agxCollide::Geometry* geometry, agx::UInt32 id ) override;
134 
139  virtual void addGroup( agxCollide::Geometry* geometry, const agx::Name& name ) override;
140 
145  virtual void removeGroup( agxCollide::Geometry* geometry, agx::UInt32 id ) override;
146 
151  virtual void removeGroup( agxCollide::Geometry* geometry, const agx::Name& name ) override;
152 
157  virtual bool add( agx::RigidBody* body, bool addGeometries = true ) override;
158 
163  virtual bool remove( agx::RigidBody* body, bool removeGeometries = true ) override;
164 
169  virtual void setEnable( agx::RigidBody* body, bool enable ) override;
170 
175  virtual bool add( agx::Constraint* constraint ) override;
176 
181  virtual bool remove( agx::Constraint* constraint ) override;
182 
187  virtual bool add( agx::StrongInteraction* si ) override;
188 
193  virtual bool remove( agx::StrongInteraction* si ) override;
194 
199  virtual bool add( agxSDK::Assembly* assembly ) override;
200 
205  virtual bool remove( agxSDK::Assembly* assembly ) override;
206 
211  virtual bool add( agxSDK::EventListener* listener ) override;
212 
217  virtual bool remove( agxSDK::EventListener* listener ) override;
218 
223  virtual void addGeometryContacts( const agxCollide::LocalGeometryContactVector& contacts ) override;
224 
229  virtual void transferToDefaultStorage( agx::ContactMaterial* material ) override;
230 
235  virtual void transferToSimulationStorage( agx::ContactMaterial* material ) override;
236 
240  virtual bool empty() const override;
241 
245  virtual bool getRunningParallel() const override;
246 
250  virtual void preParallelRun() override;
251 
255  virtual void postParallelRun() override;
256 
262  virtual void commit( agxSDK::Simulation* simulation ) override;
263 
267  virtual agx::Real getTimeStep() const override;
268 
272  virtual const agx::GravityField* getGravityField() const override;
273 
277  virtual const agxSDK::MaterialManager* getMaterialManager() const override;
278 
282  virtual const agxSDK::Simulation* getSimulation() const override;
283 
287  virtual void setSimulation( agxSDK::Simulation* simulation ) override;
288 
292  virtual agxSDK::Simulation* getSimulationUnsafe() const override;
293 
294  protected:
298  virtual ~SimulationProxy() {}
299 
303  agx::Bool storeLocal() const;
304 
305  protected:
309  template< typename T >
310  class AddRemoveCompatibleObject
311  {
312  public:
313  AddRemoveCompatibleObject() {}
314 
315  bool add( T* object )
316  {
317  if ( object == nullptr || m_objectsToAdd.contains( object ) )
318  return false;
319 
320  agx::ScopeLock< agx::Mutex > sl( m_mutex );
321 
322  m_objectsToAdd.push_back( object );
323  m_objectsToRemove.findAndErase( object );
324 
325  return true;
326  }
327 
328  bool remove( T* object )
329  {
330  if ( object == nullptr || m_objectsToRemove.contains( object ) )
331  return false;
332 
333  agx::ScopeLock< agx::Mutex > sl( m_mutex );
334 
335  m_objectsToRemove.push_back( object );
336  m_objectsToAdd.findAndErase( object );
337 
338  return true;
339  }
340 
341  void commit( agxSDK::Simulation* simulation )
342  {
343  while ( !m_objectsToAdd.empty() ) {
344  simulation->add( m_objectsToAdd.back() );
345  m_objectsToAdd.pop_back();
346  }
347 
348  while ( !m_objectsToRemove.empty() ) {
349  simulation->remove( m_objectsToRemove.back() );
350  m_objectsToRemove.pop_back();
351  }
352  }
353 
354  bool empty() const { return m_objectsToAdd.empty() && m_objectsToRemove.empty(); }
355 
356  protected:
357  typedef agx::Vector< agx::ref_ptr< T > > ObjRefContainer;
358 
359  protected:
360  ObjRefContainer m_objectsToAdd;
361  ObjRefContainer m_objectsToRemove;
362  agx::Mutex m_mutex;
363  };
364 
365  template< typename T >
366  class AddRemoveEnableCompatibleObject : public AddRemoveCompatibleObject< T >
367  {
368  public:
369  AddRemoveEnableCompatibleObject() {}
370 
371  void setEnable( T* object, bool enable )
372  {
373  if ( object == nullptr )
374  return;
375 
376  agx::ScopeLock< agx::Mutex > sl( m_mutex );
377 
378  if ( enable ) {
379  if ( m_objectsToEnable.contains( object ) )
380  return;
381  m_objectsToEnable.push_back( object );
382  m_objectsToDisable.findAndErase( object );
383  }
384  else {
385  if ( m_objectsToDisable.contains( object ) )
386  return;
387  m_objectsToDisable.push_back( object );
388  m_objectsToEnable.findAndErase( object );
389  }
390  }
391 
392  void commit( agxSDK::Simulation* simulation )
393  {
394  AddRemoveCompatibleObject< T >::commit( simulation );
395 
396  while ( !m_objectsToEnable.empty() ) {
397  m_objectsToEnable.back()->setEnable( true );
398  m_objectsToEnable.pop_back();
399  }
400 
401  while ( !m_objectsToDisable.empty() ) {
402  m_objectsToDisable.back()->setEnable( false );
403  m_objectsToDisable.pop_back();
404  }
405  }
406 
407  bool empty() const { return AddRemoveCompatibleObject<T>::empty() && m_objectsToEnable.empty() && m_objectsToDisable.empty(); }
408 
409  protected:
410  typedef agx::Vector< agx::ref_ptr< T > > ObjRefContainer;
411 
412  protected:
413  ObjRefContainer m_objectsToEnable;
414  ObjRefContainer m_objectsToDisable;
415  agx::Mutex m_mutex;
416  };
417 
418  protected:
419  using GeometryRefIdContainer = agx::Vector< std::pair<agxCollide::GeometryRef, agx::UInt32> >;
420  using GeometryRefNameContainer = agx::Vector< std::pair<agxCollide::GeometryRef, agx::Name> >;
421 
422  protected:
424  bool m_runningParallel;
425  AddRemoveEnableCompatibleObject< agxCollide::Geometry > m_geometries;
426  AddRemoveEnableCompatibleObject< agx::RigidBody > m_rigidBodies;
427  AddRemoveEnableCompatibleObject< agx::Constraint > m_constraints;
428  AddRemoveEnableCompatibleObject< agx::StrongInteraction > m_strongInteractions;
429  AddRemoveCompatibleObject< agxSDK::Assembly > m_asssemblies;
430  AddRemoveEnableCompatibleObject< agxSDK::EventListener > m_eventListeners;
431  GeometryRefIdContainer m_geometryGroupsAdd;
432  GeometryRefIdContainer m_geometryGroupsRemove;
433  GeometryRefNameContainer m_namedGeometryGroupsAdd;
434  GeometryRefNameContainer m_namedGeometryGroupsRemove;
435  agxCollide::LocalGeometryContactVector m_localGeometryContacts;
436  agx::ContactMaterialRefVector m_contactMaterialsToDefaultStorage;
437  agx::ContactMaterialRefVector m_contactMaterialsToSimulationStorage;
439  };
440 }
441 
#define AGXPHYSICS_EXPORT
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:67
An EventListener can be associated to a Simulation and triggered upon various events.
Definition: EventListener.h:41
Simulation unique manager that handles logics around and parameters in contact materials given two ag...
Interface class used by internal objects running in parallel.
virtual bool add(agxCollide::Geometry *geometry)=0
virtual const agx::GravityField * getGravityField() const =0
virtual void setSimulation(agxSDK::Simulation *simulation)=0
virtual agx::Real getTimeStep() const =0
virtual void commit(agxSDK::Simulation *simulation)=0
virtual void transferToDefaultStorage(agx::ContactMaterial *material)=0
virtual void preParallelRun()=0
virtual void postParallelRun()=0
virtual void removeGroup(agxCollide::Geometry *geometry, agx::UInt32 id)=0
virtual void addGeometryContacts(const agxCollide::LocalGeometryContactVector &contacts)=0
virtual const agxSDK::MaterialManager * getMaterialManager() const =0
virtual void setEnable(agxCollide::Geometry *geometry, bool enable)=0
virtual agxSDK::Simulation * getSimulationUnsafe() const =0
virtual bool getRunningParallel() const =0
virtual void transferToSimulationStorage(agx::ContactMaterial *material)=0
virtual const agxSDK::Simulation * getSimulation() const =0
virtual bool remove(agxCollide::Geometry *geometry)=0
virtual void addGroup(agxCollide::Geometry *geometry, agx::UInt32 id)=0
virtual bool empty() const =0
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
bool remove(agx::ContactMaterial *material)
Remove an explicit contact material from the set of existing ContactMaterials.
bool add(agx::Material *material)
Add a material to the simulation.
void commit(agxSDK::SimulationProxy *simulationProxy)
Internal method.
static agxSDK::SimulationProxyRef createDefaultProxy(agxSDK::Simulation *simulation)
Creates new simulation proxy of default type.
agxSDK::SimulationProxy * getSimulationProxy() const
Internal method.
The base class for a constraint.
Definition: Constraint.h:89
This class store the combine material properties between two agx::Material's.
Definition: Material.h:528
The class GravityField is responsible for calculating and applying a gravity force on a set of bodies...
Definition: GravityField.h:39
Representation of a name string.
Definition: Name.h:33
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
Base class for a strong interaction concept where there interaction is defined in the mass matrix par...
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
Implements a Wire model with adaptive resolution.
Definition: MergedBody.h:40
std::mutex Mutex
Definition: Referenced.h:93
uint32_t UInt32
Definition: Integer.h:40
bool Bool
Definition: Integer.h:48
double Real
Definition: Real.h:42
std::lock_guard< T > ScopeLock
agx::ScopeLock is an alias for std::lock_guard
Definition: ScopeLock.h:31