AGX Dynamics  2.37.3.2
WireGeometryController.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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 <agx/agxPhysics_export.h>
20 
24 
25 #include <agxCollide/WireShape.h>
26 
29 
31 
32 #ifdef _MSC_VER
33 # pragma warning(push)
34 # pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer
35 #endif
36 
37 
38 namespace agxWire
39 {
40 #define AGX_USE_WIRE_CCD 1
41 
42 #if AGX_USE_WIRE_CCD
43 # define AGX_WIRE_SHAPE agxCollide::WireShape
44 # define AGX_WIRE_SHAPE_TYPE agxCollide::Shape::WIRE_SHAPE
45 #else
46 # define AGX_WIRE_SHAPE agxCollide::Capsule
47 # define AGX_WIRE_SHAPE_TYPE agxCollide::Shape::CAPSULE
48 #endif
49 
50  // Typedefs
51  typedef agx::HashVector< agx::RigidBody*, agxCollide::GeometryRef > RigidBodyPtrGeometryRefTable;
53  typedef std::pair< agxWire::Node*, agxWire::Node* > NodePtrPair;
54 
56 #if HASH_FOR_WIRE == HASH_NEW
57  using GeometryPtrNodeObsPtrPairTable = agx::LinearProbingHashTable<const agxCollide::Geometry*, NodePtrPair>;
58  using ContactNodePtrGeometryRefTable = agx::LinearProbingHashTable<ContactNode*, agxCollide::GeometryRef>;
59  using GeometryPtrContactNodePtrTable = agx::LinearProbingHashTable<agxCollide::Geometry*, ContactNode*>;
60  using GeometryPtrHashSet = agx::LinearProbingHashSet<agxCollide::Geometry*>;
61  using UnsignedHashSet = agx::LinearProbingHashSet<uint32_t>;
62  using NameHashSet = agx::LinearProbingHashSet<agx::Name>;
63 #elif HASH_FOR_WIRE == HASH_OLD
66  typedef agx::QuadraticProbingHashTable< agxCollide::Geometry*, ContactNode*> GeometryPtrContactNodePtrTable;
67  typedef agx::HashSet< agxCollide::Geometry* > GeometryPtrHashSet;
68  typedef agx::HashSet< uint32_t > UnsignedHashSet;
69  using NameHashSet = agx::HashSet<agx::Name>;
70 #else
71  #error
72 #endif
73 
74  // Forward declarations
75  class WireParameterController;
76  class Wire;
77  class WireContactController;
78  class WireStabilityController;
79  class WireShapeContactController;
80  class WireOldContactController;
81  class ShapeContactColliderUtils;
82 
83  template< typename T >
84  class AGXPHYSICS_EXPORT ConfigureGeometryStruct
85  {
86  public:
90  static bool configureWireGeometry(agxCollide::Geometry* wireGeometry, const agx::Vec3& pStart, const agx::Vec3& pEnd, const agx::Vec3& oldPStart, const agx::Vec3& oldPEnd, agx::Real radius, agxCollide::BoundingAABB& ret_localBound);
91  };
92 
93  enum WireContactControllerType
94  {
95  OLD_CONTACT_CONTROLLER = 0,
96  SHAPE_CONTACT_CONTROLLER
97  };
98 
102  class AGXPHYSICS_EXPORT WireGeometryController : public WireListener
103  {
104  public:
105  static void onEnableCollisions( agxWire::Wire* wire1, agxWire::Wire* wire2, agx::Bool enable );
106  static agx::Real getGlobalWireRadiusScaleIncrement();
107 
108  public:
112  typedef std::function<void( agxCollide::Geometry*, agxWire::WireGeometryType, agx::Bool )> GeometryVisitor;
113 
114  public:
115  // Geometry groups for the spheres and for the wire geometries. These two groups can't collide.
116  enum GeometryGroups
117  {
118  WIRE_SPHERE_GEOMETRY_GROUP = 170000,
119  WIRE_WIRE_GEOMETRY_GROUP,
120  WIRE_DYNAMIC_CONTACTS_GROUP,
121  WIRE_SENSOR_SPHERE_CONTACTS_GROUP
122  };
123 
127  WireGeometryController();
128 
133  void setActiveContactControllerType(Wire* wire, const WireContactControllerType type);
134  WireContactControllerType getActiveContactControllerType() const;
135 
136 
137  const ShapeContactColliderUtils* getShapeContactControllerUtils() const;
138 
139 #ifndef SWIG
140 
146  void traverse( GeometryVisitor visitor, agx::Bool onlyEnabled ) const;
147 #endif
148 
153  void preCollideUpdate();
154 
161  agx::Bool preUpdate( );
162 
166  void updateState();
167 
174  virtual void setWire( agxWire::Wire* wire, agxCollide::Space* space, agxSDK::SimulationProxy* simulationProxy );
175 
179  virtual void removeNotification();
180 
184  void updateGeometries();
185 
189  void updateMaterial( agx::Material* material );
190 
194  void initialize();
195 
200  void removeGeometries( bool safeRemoveAll = false );
201 
205  agx::Bool forceAddRemoveSphereGeometryOnLumpedNode(agxWire::BodyFixedNode* bfn, agx::Bool add);
206 
212  bool setEnableCollisions( agxCollide::Geometry* geometry, bool enable );
213 
217  bool getEnableCollisions( const agxCollide::Geometry* geometry ) const;
218 
223  void copyCollisionSettings( agxWire::WireGeometryController* otherLGC );
224 
228  void getGeometries( agxCollide::GeometryPtrVector& geoms );
229 
234  void addGroup( agx::UInt32 id );
235 
240  void addGroup( const agx::Name& name );
241 
245  bool hasGroup( const agx::UInt32 id ) const;
246 
250  bool hasGroup( const agx::Name& name ) const;
251 
256  void removeGroup( agx::UInt32 id );
257 
262  void removeGroup( const agx::Name& name );
263 
267  bool hasContactNodesOn( const agx::RigidBody* rb ) const;
268 
272  void updateSensorTransforms();
273 
279  void enableContactWireNodeSensor( agxWire::ContactNode* cln, bool enable );
280 
284  bool hasGeometry( const agxCollide::Geometry* geometry ) const;
285 
289  inline agx::UInt32 getWireUniqueGroupID() const { agxAssert( m_wireUniqueGroupID ); return m_wireUniqueGroupID; }
290 
294  inline agx::UInt32 getSensorUniqueGroupID() const { agxAssert( m_sensorUniqueGroupID ); return m_sensorUniqueGroupID; }
295 
299  agx::UInt32 getDisabledCollisionsGroupId() const;
300 
304  agx::UInt32Vector getGroupIds() const;
305 
309  agx::NameVector getGroupNames() const;
310 
314  agxCollide::Geometry* getSensor( agxWire::ContactNode* cln );
315  const agxCollide::Geometry* getSensor( const agxWire::ContactNode* cln ) const;
316 
320  agxWire::ContactNode* getContactWireNode( agxCollide::Geometry* sensor );
321 
326  const NodePtrPair& getNodePair( const agxCollide::Geometry* geometry ) const;
327 
331  agxWire::WireContactController* getActiveContactController() const;
332 
336  agxWire::WireOldContactController* getOldContactController() const;
337 
341  agxWire::WireShapeContactController* getShapeContactController() const;
342 
346  bool getEnable() const;
347 
351  bool isEnabled() const;
352 
353 
357  bool getEnableFlag() const;
358 
363  void setEnable( bool enable );
364 
368  bool isActive() const;
369 
373  agx::Real getRadius() const;
374 
378  agx::Real getRadiusIncMultiplier() const;
379 
383  agx::Real getMaximumContactMovementOneTimestep() const;
384 
388  agx::Real getSensorRadius( agx::Real wireRadius ) const;
389 
394 
398  bool areColliding( agxCollide::Geometry* geom1, agxCollide::Geometry* geom2 );
399 
406  void removeGeometryReference( agx::RigidBody* lumpedNodeBody );
407 
411  agxWire::WireImpactController* getImpactController() const;
412 
416  void setSimulationProxy( agxSDK::SimulationProxy* simulationProxy );
417 
422  bool match( agx::Physics::GeometryPtr geometry ) const;
423 
427  void store( agxStream::OutputArchive& out ) const;
428 
432  void restore( agxStream::InputArchive& in );
433 
434  protected:
435  friend class WireContactController;
436 
440  virtual ~WireGeometryController();
441 
442 
446  void createContactController(Wire* wire);
447 
452  virtual void onAdd( agxWire::NodeIterator nodeIt ) override;
453 
458  virtual void onInsert( agxWire::NodeIterator nodeIt ) override;
459 
464  virtual void onRemove( agxWire::NodeIterator nodeIt ) override;
465 
469  virtual void preSolve() override;
470 
474  virtual void postSolve() override;
475 
479  agx::Bool insertSphere(agxWire::BodyFixedNode* node);
480 
484  agx::Bool removeSphere(agxWire::BodyFixedNode* node);
485 
490  void onAdd( agxWire::BodyFixedNode* node );
491 
496  void onRemove( agxWire::BodyFixedNode* node );
497 
501  void onAdd( agxWire::ContactNode* cn );
502 
506  void onRemove( agxWire::ContactNode* cn );
507 
511  agxSDK::SimulationProxy* getSimulationProxy() const;
512 
516  void allocateWireGeometries( agxCollide::GeometryRefVector& geometryVector, size_t size ) const;
517 
521  void allocateSensorGeometries( agxCollide::GeometryRefVector& geometryVector, size_t size ) const;
522 
527  agxCollide::GeometryRef createDefaultSphere();
528 
533  agxCollide::GeometryRef createSensorSphere();
534 
538  void pushToEnabledWireGeometries();
539 
544  void handleFirstAndLastNodeSphereGeometry( agxWire::WireDistanceCompositeConstraint* wire ) const;
545 
546  template <class IdT, class ContainerT>
547  void addGroupT( IdT id, ContainerT& container );
548 
549  template <class IdT, class ContainerT>
550  void removeGroupT( IdT id, ContainerT& container );
551 
552  protected:
554  WireImpactControllerRef m_impactController;
555  agx::ref_ptr<agx::Referenced> m_shapeContactController;
556  agx::ref_ptr<agx::Referenced> m_oldContactController;
557  agxCollide::GeometryRefVector m_enabledWireGeometries;
558  agxCollide::GeometryRefVector m_disabledWireGeometries;
559  GeometryPtrHashSet m_enabledGeometries;
560  RigidBodyPtrGeometryRefTable m_lumpedSphereGeometries;
561  ContactNodePtrGeometryRefTable m_nodeSensors;
562  GeometryPtrContactNodePtrTable m_sensorNodes;
563  agxCollide::GeometryRefVector m_disabledSensorGeometries;
564  agx::UInt32 m_disabledCollisionsGroupIDOtherGeometries;
565  agx::UInt32 m_disabledCollisionsGroupIDWire;
566  agx::UInt32 m_sensorUniqueGroupID;
567  agx::UInt32 m_wireUniqueGroupID;
568  GeometryPtrGeometryObsTable m_externalDisabledGeometries;
569  UnsignedHashSet m_groupIDs;
570  NameHashSet m_groupNames;
571  GeometryPtrNodeObsPtrPairTable m_geometryNodePairTable;
572  agx::observer_ptr< WireMaterialController > m_materialController;
573  WireParameterController* m_parameters;
574  bool m_enable;
575  bool m_initialized;
576  agxCollide::GeometryRefVector m_wireGeometryPool;
577  agxSDK::SimulationProxyRef m_simulationProxy;
578  WireContactControllerType m_contactControllerType;
579  agx::ref_ptr<agx::Referenced> m_colliderUtils;
580 
581  private:
582  static NodePtrPair s_nullObsPtrPair;
583 
584  };
585 
586  typedef agx::ref_ptr< WireGeometryController > WireGeometryControllerRef;
587 
588  // Inline methods *****************************************************************************
589 
590  inline bool WireGeometryController::isActive() const
591  {
592  return m_enable && m_initialized;
593  }
594 
595  inline bool WireGeometryController::hasGeometry( const agxCollide::Geometry* geometry ) const
596  {
597  agxAssert( m_wireUniqueGroupID > 0 && (m_wire == nullptr || m_geometryNodePairTable.contains( geometry ) == geometry->hasGroup( m_wireUniqueGroupID )) );
598  return m_wire != nullptr && geometry->hasGroup( m_wireUniqueGroupID );
599  }
600 
601  inline void WireGeometryController::pushToEnabledWireGeometries()
602  {
603  if ( m_disabledWireGeometries.empty() )
604  allocateWireGeometries( m_disabledWireGeometries, 32 );
605  m_enabledWireGeometries.push_back( m_disabledWireGeometries.back() );
606  m_disabledWireGeometries.pop_back();
607  }
608 
609  inline const NodePtrPair& WireGeometryController::getNodePair( const agxCollide::Geometry* geometry ) const
610  {
611  GeometryPtrNodeObsPtrPairTable::const_iterator i = m_geometryNodePairTable.find( geometry );
612  return i != m_geometryNodePairTable.end() ? i->second : s_nullObsPtrPair;
613  }
614 
615  inline agxSDK::SimulationProxy* WireGeometryController::getSimulationProxy() const
616  {
617  return m_simulationProxy;
618  }
619 
620  inline agxCollide::Geometry* WireGeometryController::getSensor( ContactNode* cln )
621  {
622  ContactNodePtrGeometryRefTable::iterator i = m_nodeSensors.find(cln);
623  if ( i != m_nodeSensors.end() )
624  return i->second;
625  return nullptr;
626  }
627 
628  inline ContactNode* WireGeometryController::getContactWireNode( agxCollide::Geometry* sensor )
629  {
630  GeometryPtrContactNodePtrTable::iterator i = m_sensorNodes.find( sensor );
631  if ( i != m_sensorNodes.end() )
632  return i->second;
633  return nullptr;
634  }
635 
636  inline bool WireGeometryController::getEnable() const
637  {
638  return m_enable && m_wire.isValid() && m_materialController.isValid() && m_parameters;
639  }
640 
641  inline bool WireGeometryController::isEnabled() const
642  {
643  return getEnable();
644  }
645 
646  inline void WireGeometryController::setEnable( bool enable )
647  {
648  m_enable = enable;
649  }
650 
651  inline bool WireGeometryController::getEnableFlag() const
652  {
653  return m_enable;
654  }
655 
656  inline agx::Real WireGeometryController::getRadius() const
657  {
658  agxAssert( m_materialController );
659  return m_materialController->getRadius();
660  }
661 
662  inline agx::Real WireGeometryController::getRadiusIncMultiplier() const
663  {
664  agxAssert( m_materialController && m_parameters );
665  return m_materialController->getRadius() * m_parameters->getRadiusMultiplier(m_materialController->getRadius());
666  }
667 
668  inline agx::Real WireGeometryController::getMaximumContactMovementOneTimestep() const
669  {
670  agxAssert( m_materialController && m_parameters );
671  return m_parameters->getMaximumContactMovementOneTimestep();
672  }
673 
674  inline agx::Real WireGeometryController::getSensorRadius( agx::Real wireRadius ) const
675  {
676  agxAssert( m_materialController && m_parameters );
677  return m_parameters->getSensorRadius(wireRadius );
678  }
679 
680  inline WireImpactController* WireGeometryController::getImpactController() const
681  {
682  return m_impactController;
683  }
684 
685  AGX_FORCE_INLINE bool WireGeometryController::match( agx::Physics::GeometryPtr geometry ) const
686  {
687  agx::Physics::Geometry::ShapePtr shape = geometry.shape();
688  return shape && shape.type() == agxCollide::Shape::WIRE_SHAPE && ((agx::Physics::Geometry::WireShapePtr)shape).wireId() == m_wireUniqueGroupID;
689  }
690 
691  // ********************************************************************************************
692 }
693 
694 #ifdef _MSC_VER
695 # pragma warning(pop)
696 #endif
697 
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
Inheritance with partial specialization due to bug with ref_ptr containers.
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
bool Bool
Definition: Integer.h:48
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:60
#define AGXPHYSICS_EXPORT
Inheritance with partial specialization due to bug with ref_ptr containers.
Pointer to a entity instance of type Physics.Geometry.Shape.
Definition: ShapeEntity.h:195
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
Templated vector class.
Definition: Vector.h:52
The body fixed node is attached to a body at a given offset.
Interface and placeholder of controllers/helpers for wires.
Definition: Wire.h:61
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
Implements a Wire model with adaptive resolution.
Definition: MergedBody.h:39
Pointer to a entity instance of type Physics.Geometry.
AGXPHYSICS_EXPORT agx::UInt8 & type()
bool hasGroup(agx::UInt32 id) const
This is performing a linear search among the collision group id for this Geometry.
Definition: Geometry.h:906
#define agxAssert(expr)
Definition: debug.h:143
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:375
AGXPHYSICS_EXPORT bool setEnableCollisions(agx::RigidBody *rb1, agx::RigidBody *rb2, bool enable)
Enable or disable the collision between all geometries in RigidBody rb1 and RigidBody rb2...
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
Contact node class is a node that, currently, is defined to live on an edge on a geometry (agxCollide...
AGXPHYSICS_EXPORT agx::Physics::Geometry::ShapePtr & shape()
Interface class used by internal objects running in parallel.
double Real
Definition: Real.h:42
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:37
Inheritance with partial specialization due to bug with ref_ptr containers.
Pointer to a entity instance of type Physics.Geometry.WireShape.
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97