AGXDynamics  2.31.1.0
LinkedStructure.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 
201  virtual agx::Bool rebind();
202 
210  virtual agx::Bool addComponent( agxSDK::LinkedStructureComponent* component );
211 
217  virtual agx::Bool removeComponent( agxSDK::LinkedStructureComponent* component );
218 
222  template<typename T>
223  T* getComponent() const;
224 
228  agxSDK::LinkedStructureComponent* getComponent( const agx::Name& name ) const;
229 
230 
235 
239  agx::UInt getSegmentIndex( const agx::RigidBody* rb ) const;
240 
244  const agxSDK::LinkedSegmentContainer& getSegmentsContainer() const;
245 
246  public:
248 
254  void addNotification( agxSDK::Simulation* simulation ) final override;
255 
261  void removeNotification( agxSDK::Simulation* simulation ) final override;
262 
265 
266  protected:
272  struct IdData
273  {
277  IdData() : id( agx::InvalidIndex ), name( "" ) {}
278 
282  IdData( agx::UInt32 id ) : id( id ), name( "" ) {}
283 
287  IdData( const agx::Name& name ) : id( agx::InvalidIndex ), name( name ) {}
288 
292  agx::Bool operator== ( const IdData& other ) const { return id == other.id && name == other.name; }
293 
297  agx::Bool operator!= ( const IdData& other ) const { return !(*this == other); }
298 
301  };
302 
303  protected:
308  virtual void onAddNotification( agxSDK::Simulation* simulation );
309 
314  virtual void onRemoveNotification( agxSDK::Simulation* simulation );
315 
319  virtual void onMaterialNew();
320 
324  virtual void onGroupAdd( const IdData& group );
325 
329  virtual void onGroupRemove( const IdData& group );
330 
334  virtual void onSetEnableCollisions( agxCollide::Geometry* geometry, agx::Bool enable );
335 
339  virtual void onPreCollideStep();
340 
344  virtual void onPreStep();
345 
349  virtual void onPostStep();
350 
354  virtual void onLastStep();
355 
356  protected:
360  template<typename T>
361  static T* getT( agx::RigidBody* rb );
362 
366  template<typename T>
367  static const T* getT( const agx::RigidBody* rb );
368 
369  protected:
373  LinkedStructure();
374 
378  virtual ~LinkedStructure();
379 
386  void add( LinkedSegment* segment );
387 
391  agx::UInt getNumSegments() const;
392 
397  template<typename T>
398  LinkedSegmentRange<T> getSegments() const;
399 
406  template<typename T>
407  LinkedSegmentRange<T> getSegments( agx::UInt beginIndex, agx::UInt endIndex ) const;
408 
412  LinkedSegment* getSegment( agx::UInt index ) const;
413 
417  template<typename ObjT>
418  LinkedSegment* getSegment( const ObjT* rb ) const;
419 
423  template<typename T>
424  LinkedSegmentIterator<T> getSegmentIterator( agx::UInt index, agx::Bool boundaryless = false ) const;
425 
429  template<typename T, typename ObjT>
430  LinkedSegmentIterator<T> getSegmentIterator( const ObjT* rb, agx::Bool boundaryless = false ) const;
431 
435  LinkedStructureObjectData* getData( const agx::RigidBody* rb ) const;
436 
437  private:
438  // Events from the simulation.
439  friend class Simulation;
440 
441  enum SimulationEvent
442  {
443  PRE_COLLIDE_STEP,
444  PRE_STEP,
445  POST_STEP,
446  LAST_STEP
447  };
448 
450 
451  private:
452  void trigger( SimulationEvent simulationEvent );
453  void addGroup( const IdData& group );
454  void removeGroup( const IdData& group );
455  agx::Bool hasGroup( const IdData& group ) const;
456 
457  private:
458  agx::Uuid m_uniqueId;
459  agx::Uuid m_disabledCollisionsId;
460  LinkedSegmentContainer m_segments;
461  agx::MaterialRef m_material;
462  IdDataContainer m_externallyAddedGroups;
464  agx::Vec3 m_linearVelocityDamping;
465  agx::Vec3 m_angularVelocityDamping;
466  };
467 
468  template<typename T>
470  {
471  auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
472  return data != nullptr && data->linkedStructure != nullptr ? data->linkedStructure->template asSafe<T>() : nullptr;
473  }
474 
475  template<typename T>
476  const T* LinkedStructure::getT( const agx::RigidBody* rb )
477  {
478  return LinkedStructure::getT<T>( const_cast<agx::RigidBody*>( rb ) );
479  }
480 
481  template<typename T>
483  {
484  for ( auto component : m_components ) {
485  auto typed = component->template asSafe<T>();
486  if ( typed != nullptr )
487  return typed;
488  }
489 
490  return nullptr;
491  }
492 
494  {
495  return m_segments;
496  }
497 
499  {
500  return m_segments.size();
501  }
502 
503  template<typename T>
505  {
507  LinkedSegmentIterator<T>::end( m_segments ) );
508  }
509 
510  template<typename T>
512  {
513  return LinkedSegmentRange<T>( getSegmentIterator<T>( beginIndex ),
514  getSegmentIterator<T>( endIndex ) );
515  }
516 
518  {
519  agxAssert( index < m_segments.size() );
520  return m_segments[ index ];
521  }
522 
523  template<>
525  {
526  auto index = getSegmentIndex( rb );
527  return index != agx::InvalidIndex ?
528  m_segments[ index ] :
529  nullptr;
530  }
531 
533  {
534  auto data = getData( rb );
535  return data != nullptr && data->segment != nullptr && data->linkedStructure == this ?
536  data->segment->getIndex() :
538  }
539 
540  template<typename T>
542  {
543  return LinkedSegmentIterator<T>( index, boundaryless, m_segments );
544  }
545 
546  template<typename T, typename ObjT>
547  LinkedSegmentIterator<T> LinkedStructure::getSegmentIterator( const ObjT* rb, agx::Bool boundaryless /* = false */ ) const
548  {
549  auto segment = this->getSegment( rb );
550  if ( segment == nullptr )
551  return LinkedSegmentIterator<T>::end( m_segments );
552  return getSegmentIterator<T>( segment->getIndex(), boundaryless );
553  }
554 
556  {
557  auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
558  // Accepting data when linkedStructure == null since it can be considered implicitly
559  // related to this linked structure (data->linkedStructure = this to be made).
560  return data != nullptr && ( data->linkedStructure == this || data->linkedStructure == nullptr ) ?
561  data :
562  nullptr;
563  }
564 }
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:584
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:191
#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:248
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:88
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 == "".
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
Group id &#39;union&#39; of int id and name id.
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:52
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:384
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:206
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:579
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:121
Segment object in a linked structure.
Definition: LinkedSegment.h:38
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:197
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:73