AGX Dynamics  2.37.3.2
LinkedStructure.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 <agxSDK/Assembly.h>
23 
24 #include <agx/BitState.h>
25 
26 namespace agxSDK
27 {
29 
36  {
37  public:
41  static LinkedStructure* get(agxCollide::Geometry* geometry);
42 
46  static const LinkedStructure* get(const agxCollide::Geometry* geometry);
47 
51  static LinkedStructure* get(agx::RigidBody* body);
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 
78  virtual agx::UInt32 getDisabledCollisionGroupId() const;
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 
141  virtual agxCollide::GroupIdCollection findGroupIdCollection() const;
142 
150  virtual void setLinearVelocityDamping( const agx::Vec3& linearVelocityDamping );
151 
155  virtual agx::Vec3 getLinearVelocityDamping() const;
156 
164  virtual void setAngularVelocityDamping( const agx::Vec3& angularVelocityDamping );
165 
169  virtual agx::Vec3 getAngularVelocityDamping() const;
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 
249  virtual agx::Bool addComponent( agxSDK::LinkedStructureComponent* component );
250 
256  virtual agx::Bool removeComponent( agxSDK::LinkedStructureComponent* component );
257 
261  template<typename T>
262  T* getComponent() const;
263 
267  agxSDK::LinkedStructureComponent* getComponent( const agx::Name& name ) const;
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:
412  LinkedStructure();
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 
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 }
Basic implementation of any structure where bodies are linked/constrained together in a well defined ...
IdData(const agx::Name &name)
Construct group given name where id == agx::InvalidIndex.
agx::UInt getNumSegments() const
bool Bool
Definition: Integer.h:48
Segment range object for linked structure.
virtual void removeNotification(agxSDK::Simulation *)
Called when this assembly is removed from a simulation.
Definition: Assembly.h:621
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:194
#define AGXPHYSICS_EXPORT
agx::UInt getSegmentIndex(const agx::RigidBody *rb) const
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
IdData()
Default constructor, group id is invalid with id == agx::InvalidIndex and name == ""...
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
LinkedSegment * getSegment(agx::UInt index) const
const agxSDK::LinkedSegmentContainer & getSegmentsContainer() const
static LinkedSegmentIterator begin(const agxSDK::LinkedSegmentContainer &segments)
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
uint32_t UInt
Definition: Integer.h:34
IdData(agx::UInt32 id)
Construct group given int id where name == "".
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Group id &#39;union&#39; of int id and name id.
agx::Real getLength() const
Collection of geometry group names and id&#39;s.
LinkedSegmentRange< T > getSegments() const
Constructs a segment range over all segments.
LinkedSegmentIterator< T > getSegmentIterator(agx::UInt index, agx::Bool boundaryless=false) const
#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
Optionally unbounded linked structure segment iterator object to iterate segments in a SegmentStructu...
static LinkedSegmentIterator end(const agxSDK::LinkedSegmentContainer &segments)
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...
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
#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
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
LinkedStructureObjectData * getData(const agx::RigidBody *rb) const
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:616
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
Segment object in a linked structure.
Definition: LinkedSegment.h:38
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:200
Connection back from bodies, constraints etc.
double Real
Definition: Real.h:42
static T * getT(agx::RigidBody *rb)
Base implementation of a component (e.g., specific functionality) in linked structures.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
uint32_t UInt32
Definition: Integer.h:40
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:66