AGX Dynamics  2.35.0.1
LinkedStructure.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/Assembly.h>
23 
24 #include <agx/BitState.h>
25 
26 namespace agxSDK
27 {
29 
36  {
37  public:
42 
46  static const LinkedStructure* get(const agxCollide::Geometry* geometry);
47 
52 
56  static const LinkedStructure* get(const agx::RigidBody* body);
57 
58  public:
63  virtual void setMaterial( agx::Material* material );
64 
68  virtual agx::Material* getMaterial() const;
69 
73  virtual agx::UInt32 getUniqueId() const;
74 
79 
85  virtual void setEnableCollisions( agx::RigidBody* rb, agx::Bool enable );
86 
90  virtual agx::Bool getEnableCollisions( const agx::RigidBody* rb ) const;
91 
97  virtual void setEnableCollisions( agxCollide::Geometry* geometry, agx::Bool enable );
98 
102  virtual agx::Bool getEnableCollisions( const agxCollide::Geometry* geometry ) const;
103 
108  virtual void addGroup( agx::UInt32 id );
109 
114  virtual void removeGroup( agx::UInt32 id );
115 
119  virtual agx::Bool hasGroup( agx::UInt32 id ) const;
120 
125  virtual void addGroup( const agx::Name& name );
126 
131  virtual void removeGroup( const agx::Name& name );
132 
136  virtual agx::Bool hasGroup( const agx::Name& name ) const;
137 
142 
150  virtual void setLinearVelocityDamping( const agx::Vec3& linearVelocityDamping );
151 
156 
164  virtual void setAngularVelocityDamping( const agx::Vec3& angularVelocityDamping );
165 
170 
175  virtual agx::Real getRestLength() const;
176 
181  virtual agx::Real getCurrentLength() const;
182 
188  virtual agx::Real getMass() const;
189 
194  virtual agx::Real getRestVolume() const;
195 
202  virtual agx::Vec3 findPoint( agx::Real restLengthFromStart ) const;
203 
210  virtual agx::Vec3 findPoint( const agx::Vec3& worldPoint ) const;
211 
219  virtual agx::Real findCurrentLength( agx::Real restLengthFromStart ) const;
220 
226  virtual agx::Real findRestLength( agx::Real currentLengthFromStart ) const;
227 
233  virtual agx::Real findRestLengthTo( const agxSDK::LinkedSegment* segment ) const;
234 
240  virtual agx::Bool rebind();
241 
250 
257 
261  template<typename T>
262  T* getComponent() const;
263 
268 
269 
274 
278  agx::UInt getSegmentIndex( const agx::RigidBody* rb ) const;
279 
283  const agxSDK::LinkedSegmentContainer& getSegmentsContainer() const;
284 
285  public:
287 
293  void addNotification( agxSDK::Simulation* simulation ) final override;
294 
300  void removeNotification( agxSDK::Simulation* simulation ) final override;
301 
304 
305  protected:
311  struct IdData
312  {
316  IdData() : id( agx::InvalidIndex ), name( "" ) {}
317 
321  IdData( agx::UInt32 id ) : id( id ), name( "" ) {}
322 
326  IdData( const agx::Name& name ) : id( agx::InvalidIndex ), name( name ) {}
327 
331  agx::Bool operator== ( const IdData& other ) const { return id == other.id && name == other.name; }
332 
336  agx::Bool operator!= ( const IdData& other ) const { return !(*this == other); }
337 
340  };
341 
342  protected:
347  virtual void onAddNotification( agxSDK::Simulation* simulation );
348 
353  virtual void onRemoveNotification( agxSDK::Simulation* simulation );
354 
358  virtual void onMaterialNew();
359 
363  virtual void onGroupAdd( const IdData& group );
364 
368  virtual void onGroupRemove( const IdData& group );
369 
373  virtual void onSetEnableCollisions( agxCollide::Geometry* geometry, agx::Bool enable );
374 
378  virtual void onPreCollideStep();
379 
383  virtual void onPreStep();
384 
388  virtual void onPostStep();
389 
393  virtual void onLastStep();
394 
395  protected:
399  template<typename T>
400  static T* getT( agx::RigidBody* rb );
401 
405  template<typename T>
406  static const T* getT( const agx::RigidBody* rb );
407 
408  protected:
413 
417  virtual ~LinkedStructure();
418 
425  void add( LinkedSegment* segment );
426 
430  agx::UInt getNumSegments() const;
431 
436  template<typename T>
437  LinkedSegmentRange<T> getSegments() const;
438 
445  template<typename T>
446  LinkedSegmentRange<T> getSegments( agx::UInt beginIndex, agx::UInt endIndex ) const;
447 
451  LinkedSegment* getSegment( agx::UInt index ) const;
452 
456  template<typename T>
457  T* getSegment( agx::Real restLengthFromStart ) const;
458 
462  template<typename ObjT>
463  LinkedSegment* getSegment( const ObjT* rb ) const;
464 
468  template<typename T>
469  LinkedSegmentIterator<T> getSegmentIterator( agx::UInt index, agx::Bool boundaryless = false ) const;
470 
474  template<typename T, typename ObjT>
475  LinkedSegmentIterator<T> getSegmentIterator( const ObjT* rb, agx::Bool boundaryless = false ) const;
476 
480  LinkedStructureObjectData* getData( const agx::RigidBody* rb ) const;
481 
482  private:
483  // Events from the simulation.
484  friend class Simulation;
485 
486  enum SimulationEvent
487  {
488  PRE_COLLIDE_STEP,
489  PRE_STEP,
490  POST_STEP,
491  LAST_STEP
492  };
493 
494  using IdDataContainer = agx::Vector<IdData>;
495 
496  private:
497  void trigger( SimulationEvent simulationEvent );
498  void addGroup( const IdData& group );
499  void removeGroup( const IdData& group );
500  agx::Bool hasGroup( const IdData& group ) const;
501 
502  private:
503  agx::Uuid m_uniqueId;
504  agx::Uuid m_disabledCollisionsId;
505  LinkedSegmentContainer m_segments;
506  agx::MaterialRef m_material;
507  IdDataContainer m_externallyAddedGroups;
509  agx::Vec3 m_linearVelocityDamping;
510  agx::Vec3 m_angularVelocityDamping;
511  };
512 
513  template<typename T>
515  {
516  auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
517  return data != nullptr && data->linkedStructure != nullptr ? data->linkedStructure->template asSafe<T>() : nullptr;
518  }
519 
520  template<typename T>
521  const T* LinkedStructure::getT( const agx::RigidBody* rb )
522  {
523  return LinkedStructure::getT<T>( const_cast<agx::RigidBody*>( rb ) );
524  }
525 
526  template<typename T>
528  {
529  for ( auto component : m_components ) {
530  auto typed = component->template asSafe<T>();
531  if ( typed != nullptr )
532  return typed;
533  }
534 
535  return nullptr;
536  }
537 
539  {
540  return m_segments;
541  }
542 
544  {
545  return m_segments.size();
546  }
547 
548  template<typename T>
550  {
552  LinkedSegmentIterator<T>::end( m_segments ) );
553  }
554 
555  template<typename T>
557  {
558  return LinkedSegmentRange<T>( getSegmentIterator<T>( beginIndex ),
559  getSegmentIterator<T>( endIndex ) );
560  }
561 
563  {
564  agxAssert( index < m_segments.size() );
565  return m_segments[ index ];
566  }
567 
568  template<typename T>
569  T* LinkedStructure::getSegment( agx::Real restLengthFromStart ) const
570  {
571  if ( m_segments.empty() )
572  return nullptr;
573 
574  if ( restLengthFromStart <= 0.0 )
575  return m_segments.front()->template as<T>();
576 
577  agx::Real restLengthAlongStructure = 0.0;
578  for ( auto segment : this->template getSegments<T>() ) {
579  if ( restLengthAlongStructure + segment->getLength() < restLengthFromStart )
580  restLengthAlongStructure += segment->getLength();
581  else
582  return segment->template as<T>();
583  }
584 
585  return m_segments.back()->template as<T>();
586  }
587 
588  template<>
590  {
591  auto index = getSegmentIndex( rb );
592  return index != agx::InvalidIndex ?
593  m_segments[ index ] :
594  nullptr;
595  }
596 
598  {
599  auto data = getData( rb );
600  return data != nullptr && data->segment != nullptr && data->linkedStructure == this ?
601  data->segment->getIndex() :
603  }
604 
605  template<typename T>
607  {
608  return LinkedSegmentIterator<T>( index, boundaryless, m_segments );
609  }
610 
611  template<typename T, typename ObjT>
612  LinkedSegmentIterator<T> LinkedStructure::getSegmentIterator( const ObjT* rb, agx::Bool boundaryless /* = false */ ) const
613  {
614  auto segment = this->getSegment( rb );
615  if ( segment == nullptr )
616  return LinkedSegmentIterator<T>::end( m_segments );
617  return getSegmentIterator<T>( segment->getIndex(), boundaryless );
618  }
619 
621  {
622  auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
623  // Accepting data when linkedStructure == null since it can be considered implicitly
624  // related to this linked structure (data->linkedStructure = this to be made).
625  return data != nullptr && ( data->linkedStructure == this || data->linkedStructure == nullptr ) ?
626  data :
627  nullptr;
628  }
629 }
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
#define AGXPHYSICS_EXPORT
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
Collection of geometry group names and id's.
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:67
virtual void removeNotification(agxSDK::Simulation *)
Called when this assembly is removed from a simulation.
Definition: Assembly.h:618
virtual void addNotification(agxSDK::Simulation *)
Called when this assembly is added to a simulation (given that it not already is in the simulation).
Definition: Assembly.h:613
Optionally unbounded linked structure segment iterator object to iterate segments in a SegmentStructu...
static LinkedSegmentIterator end(const agxSDK::LinkedSegmentContainer &segments)
Segment range object for linked structure.
Segment object in a linked structure.
Definition: LinkedSegment.h:39
Base implementation of a component (e.g., specific functionality) in linked structures.
Basic implementation of any structure where bodies are linked/constrained together in a well defined ...
LinkedStructure()
Hidden default constructor.
LinkedSegment * getSegment(const ObjT *rb) const
virtual void setMaterial(agx::Material *material)
Assign material to all segments in this linked structure.
virtual void removeGroup(agx::UInt32 id)
Remove group id from all segments in this linked structure.
LinkedSegmentRange< T > getSegments() const
Constructs a segment range over all segments.
agx::UInt getSegmentIndex(const agx::RigidBody *rb) const
const agxSDK::LinkedSegmentContainer & getSegmentsContainer() const
virtual agxCollide::GroupIdCollection findGroupIdCollection() const
virtual void onSetEnableCollisions(agxCollide::Geometry *geometry, agx::Bool enable)
Called when collision state changed for given geometry.
virtual void setLinearVelocityDamping(const agx::Vec3 &linearVelocityDamping)
Assigns linear velocity damping to all segments in this linked structure.
virtual void onLastStep()
Called on simulation last step event - after user lastStep.
virtual agx::Bool getEnableCollisions(const agxCollide::Geometry *geometry) const
agxSDK::LinkedSegmentRange< agxSDK::LinkedSegment > segments()
virtual void removeGroup(const agx::Name &name)
Remove group name from all segments in this linked structure.
virtual void addGroup(agx::UInt32 id)
Add group id to all segments in this linked structure.
virtual agx::Bool rebind()
Make the current relative positions of all nodes the rest state.
virtual agx::Bool removeComponent(agxSDK::LinkedStructureComponent *component)
Remove an added component from this linked structure.
virtual agx::Bool getEnableCollisions(const agx::RigidBody *rb) const
virtual void onAddNotification(agxSDK::Simulation *simulation)
Called when addNotification is executed when this linked structure is added to the simulation.
virtual void onPreStep()
Called on simulation pre step event - after user preStep.
static LinkedStructure * get(agx::RigidBody *body)
static const LinkedStructure * get(const agx::RigidBody *body)
virtual agx::Vec3 findPoint(agx::Real restLengthFromStart) const
Finds center point on this structure given rest length from the start.
virtual void onGroupRemove(const IdData &group)
Called when an existing group has been removed.
agxSDK::LinkedStructureComponent * getComponent(const agx::Name &name) const
virtual void onRemoveNotification(agxSDK::Simulation *simulation)
Called when removeNotification is executed when this linked structure is removed from the simulation.
virtual agx::Bool addComponent(agxSDK::LinkedStructureComponent *component)
Add new component to this linked structure.
virtual void setEnableCollisions(agxCollide::Geometry *geometry, agx::Bool enable)
Enable/disable collisions between the geometry and all segments of this linked structure.
LinkedStructureObjectData * getData(const agx::RigidBody *rb) const
virtual agx::Real getCurrentLength() const
Calculates and returns the total length of this linked structure, at current state.
void removeNotification(agxSDK::Simulation *simulation) final override
Called when this linked structure is added to the simulation.
virtual agx::Real getRestLength() const
Calculates and returns the total length of this linked structure, at rest.
LinkedSegmentIterator< T > getSegmentIterator(agx::UInt index, agx::Bool boundaryless=false) const
virtual agx::Real findCurrentLength(agx::Real restLengthFromStart) const
Calculates the current length of a given rest length.
virtual ~LinkedStructure()
Reference counted object - protected destructor.
virtual void setEnableCollisions(agx::RigidBody *rb, agx::Bool enable)
Enable/disable collisions between the rigid body and all segments of this linked structure.
LinkedSegment * getSegment(agx::UInt index) const
virtual void onPostStep()
Called on simulation post step event - after user postStep.
virtual agx::Material * getMaterial() const
static LinkedStructure * get(agxCollide::Geometry *geometry)
virtual agx::UInt32 getDisabledCollisionGroupId() const
virtual agx::Real getRestVolume() const
Calculates and returns the total volume of this linked structure, at rest.
void add(LinkedSegment *segment)
Add segment to this linked structure.
virtual agx::Bool hasGroup(agx::UInt32 id) const
virtual agx::Real findRestLength(agx::Real currentLengthFromStart) const
Calculates the rest length of a given current length from start.
virtual void addGroup(const agx::Name &name)
Add group name to all segments in this linked structure.
agx::UInt getNumSegments() const
virtual agx::Real findRestLengthTo(const agxSDK::LinkedSegment *segment) const
Finds rest length to the start of the given segment.
virtual agx::Bool hasGroup(const agx::Name &name) const
virtual void onPreCollideStep()
Called on simulation pre-collide step event - after user preCollide.
virtual agx::UInt32 getUniqueId() const
virtual agx::Vec3 getAngularVelocityDamping() const
virtual void setAngularVelocityDamping(const agx::Vec3 &angularVelocityDamping)
Assigns angular velocity damping to all segments in this linked structure.
virtual agx::Vec3 findPoint(const agx::Vec3 &worldPoint) const
Given any world point, find a center point on this structure that is closest to the given worldPoint.
static const LinkedStructure * get(const agxCollide::Geometry *geometry)
static T * getT(agx::RigidBody *rb)
virtual void onGroupAdd(const IdData &group)
Called when a new group has been added.
void addNotification(agxSDK::Simulation *simulation) final override
Called when this linked structure is added to the simulation.
virtual void onMaterialNew()
Called when a new material has been assigned.
virtual agx::Real getMass() const
Calculates and returns the total mass of this linked structure.
virtual agx::Vec3 getLinearVelocityDamping() const
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
bool empty() const
Definition: Container.h:136
size_t size() const
Definition: Container.h:134
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:388
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
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:42
T & front() const
Definition: agx/Vector.h:699
T & back() const
Definition: agx/Vector.h:707
#define agxAssert(expr)
Definition: debug.h:143
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:196
double Real
Definition: Real.h:42
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:190
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
Connection back from bodies, constraints etc.
Group id 'union' of int id and name id.
IdData(agx::UInt32 id)
Construct group given int id where name == "".
IdData(const agx::Name &name)
Construct group given name where id == agx::InvalidIndex.
IdData()
Default constructor, group id is invalid with id == agx::InvalidIndex and name == "".