AGX Dynamics  2.38.0.2
agxModel/Tree.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 <agxModel/export.h>
20 #include <agxSDK/Assembly.h>
23 
24 #include <agx/LockJoint.h>
25 #include <agx/HashVector.h>
26 
27 #include <agxCollide/Cylinder.h>
28 #include <agxCollide/Capsule.h>
29 
30 namespace agxModel
31 {
40  {
41  public:
42  class Branch;
43 
48  typedef BranchContainer::const_iterator BranchConstIterator;
51 
56  {
57  public:
58  enum MaxLoadType { TRANSLATIONAL = 0, ROTATIONAL };
59 
60  public:
62  Branch();
63 
70  Branch( const agx::Vec3& start, const agx::Vec3& end );
71 
78 
85  bool addBranch( agxModel::Tree::Branch* branch );
86 
92  bool removeBranch( agxModel::Tree::Branch* branch );
93 
99  bool removeBranch( size_t index );
100 
104  size_t getNumBranches() const;
105 
109  agxModel::Tree::Branch* getBranch( size_t i ) const;
110 
116  void setCustomData( agx::Referenced* customData );
117 
121  agx::Referenced* getCustomData() const;
122 
126  agxModel::Tree::Branch* getParent() const;
127 
131  agx::RigidBody* getRigidBody() const;
132 
136  agx::Constraint* getConstraint() const;
137 
141  agx::Vec3 getDirection() const;
142 
146  agx::Vec3 getLocalDirection() const;
147 
155  void setMaxLoad( agx::Real translational, agx::Real rotational );
156 
161  void setMaxTranslationalLoad( agx::Real maxLoad );
162 
167  void setMaxRotationalLoad( agx::Real maxLoad );
168 
173  agx::Vec2 getMaxLoad() const;
174 
178  agx::Real getMaxTranslationalLoad() const;
179 
183  agx::Real getMaxRotationalLoad() const;
184 
189  agx::Vec2 getCurrentLoad() const;
190 
194  agx::Real getCurrentTranslationalLoad() const;
195 
199  agx::Real getCurrentRotationalLoad() const;
200 
201  public:
208  virtual agx::Real getLength( const agxCollide::Geometry* geometry ) const;
209 
225  virtual agxCollide::Shape* splitShape( agxCollide::Shape* shapeInBranch, agx::Real shapePosition, agxModel::Tree::Branch* newBranch, agx::Real& newBelowLength, agx::Real& newAboveLength );
226 
231  virtual agx::ConstraintRef createConstraintWithParent() const;
232 
236  virtual agxModel::Tree::Branch* clone();
237 
239 
240  protected:
241  virtual ~Branch();
242 
247  void setParent( agxModel::Tree::Branch* parent );
248 
249  friend class Tree;
254  void setConstraint( agx::Constraint* constraint );
255 
256  protected:
260 
264  };
265 
270  {
271  public:
277 
283  virtual void onCreate( agxModel::Tree::Branch* branch );
284 
289  virtual void onHighLoad( agxModel::Tree::Branch* branch );
290 
295  virtual agxModel::Tree::BranchEventListener* clone();
296 
298 
299  protected:
300  virtual ~BranchEventListener();
301 
305  Tree* getTree() const;
306 
307  private:
308  friend class Tree;
309  void setTree( Tree* tree );
310 
311  private:
313  };
314 
316 
325  {
326  public:
329 
334  virtual bool match( const agxCollide::GeometryContact& contact ) const override;
335 
340  virtual bool match( const agxCollide::GeometryPair& pair ) const override;
342 
346  virtual agxModel::Tree::TreeExecuteFilter* clone();
347 
348  protected:
349  virtual ~TreeExecuteFilter();
350  agx::UInt32 getTreeID() const;
351 
352  private:
353  friend class Tree;
354  agx::UInt32 m_treeID;
355  };
356 
358 
359  enum State { INITIALIZED = (1<<0), STATIC = (1<<1), WAKE_ON_CONTACT = (1<<2), RESTORING = (1<<3) };
360 
361  public:
363  Tree();
364 
370  virtual Tree* clone();
371 
375  Branch* getRoot() const;
376 
380  bool initialized() const;
381 
387  virtual bool initialize();
388 
392  bool isStatic() const;
393 
397  bool wakeOnContact() const;
398 
411  bool makeStatic( bool wakeOnContactEvent, agxModel::Tree::TreeExecuteFilter* filter
412 #ifndef SWIG
413  // SWIG cannot handle default arguments with the new keyword, at the moment.
415 #endif
416  );
417 
423  bool makeDynamic();
424 
433  virtual bool setTreeExecuteFilter(agxModel::Tree::TreeExecuteFilter* filter);
434 
441  virtual void setBranchEventListener( agxModel::Tree::BranchEventListener* branchListener );
442 
446  agxModel::Tree::BranchEventListener* getBranchEventListener() const;
447 
455  virtual bool attach( agx::RigidBody* rb, agx::Frame* frame );
456 
462  bool addGroupID( agx::UInt32 id );
463 
469  bool removeGroupID( agx::UInt32 id );
470 
474  bool hasGroupID( agx::UInt32 id ) const;
475 
482  bool setEnableCollisions( agx::RigidBody* rb, bool flag );
483 
490  bool setEnableCollisions( agxCollide::Geometry* geometry, bool flag );
491 
495  bool getEnableCollisions( const agx::RigidBody* rb ) const;
496 
500  bool getEnableCollisions( const agxCollide::Geometry* geometry ) const;
501 
506  void setCustomData( agx::Referenced* customData );
507 
511  agx::Referenced* getCustomData() const;
512 
517  virtual void setMaterial( agx::Material* material );
518 
522  agx::Material* getMaterial() const;
523 
529  virtual void setConstraintSolveType( agx::Constraint::SolveType solveType );
530 
547  virtual Tree* cut( agxCollide::Geometry* geometry, const agx::Vec3& point, agx::Real minShapeHeight = agx::Real( 1E-2 ) );
548 
555  virtual Tree* cut( agxModel::Tree::Branch* branch );
556 
572  virtual Branch* split( agxCollide::Geometry* geometry, const agx::Vec3& point, agx::Real minShapeHeight = agx::Real( 1E-2 ) );
573 
577  static void setDefaultParameters( agx::Material* material );
578 
584  Branch* findBranch( const agx::RigidBody* rb ) const;
585 
592  Branch* findBranch( const agxModel::Tree::Branch* fromBranch, const agx::RigidBody* rb ) const;
593 
597  virtual void clear();
598 
608  static void setLinearVelocityDamping( agxModel::Tree::Branch* branch, const agx::Vec3& linearVelocityDamping );
609 
619  static void setAngularVelocityDamping( agxModel::Tree::Branch* branch, const agx::Vec3& angularVelocityDamping );
620 
621  static Tree* find(agxSDK::Simulation* simulation, const agx::Name& name);
622 
623  static Tree* find(agxSDK::Simulation* simulation, const agx::Uuid& uuid);
624 
625  static TreePtrVector findAll(agxSDK::Simulation* simulation, const agx::Name& name);
626 
627  static TreePtrVector getAll(agxSDK::Simulation* simulation);
628 
630 
631  protected:
633  virtual ~Tree();
634 
635  virtual void addNotification( agxSDK::Simulation* simulation ) override;
637  virtual void removeNotification( agxSDK::Simulation* simulation ) override;
639 
643  virtual void preCollide();
644 
648  virtual void pre();
649 
653  virtual void post();
654 
659  void configure( Branch* branch );
660 
666  void addGroupID( Branch* branch, agx::UInt32 id );
667 
673  void removeGroupID( Branch* branch, agx::UInt32 id );
674 
680  void simplify( Branch* branch, bool flag, agxSDK::Simulation* simulation );
681 
686  virtual void checkLoad( Branch* branch );
687 
691  void split( Branch* source, Branch* destination ) const;
692 
693  using agxSDK::Assembly::add;
695 
699  void remove( Branch* branch );
700 
705  void add( Branch* branch );
706 
712  void setMaterial( Branch* branch, agx::Material* material );
713 
719  void setConstraintSolveType( Branch* branch, agx::Constraint::SolveType solveType );
720 
724  void copyParameters( const Branch* source, Branch* destination ) const;
725 
729  void copyParameters( const agx::Constraint* source, agx::Constraint* destination ) const;
730 
735  Branch* guessConnectingChild( Branch* branch ) const;
736 
737  protected:
739  {
740  public:
742  : m_tree( tree ) { setMask( IMPACT ); }
743 
744  protected:
746 
747  virtual KeepContactPolicy impact( const agx::TimeStamp&, agxCollide::GeometryContact* );
748 
749  protected:
751  };
752 
754 
756  {
757  public:
759  : m_tree( tree ) { setMask( PRE_COLLIDE | PRE_STEP | POST_STEP ); }
760 
761  protected:
763 
764  virtual void preCollide( const agx::TimeStamp& ) override;
765  virtual void pre( const agx::TimeStamp& ) override;
766  virtual void post( const agx::TimeStamp& ) override;
767 
768  protected:
770  };
771 
773 
774  protected:
776  int m_state;
786  };
787 
788  template< typename T >
789  T* cutShape( T* shape, agx::Real position, agx::Real& newBelowLength, agx::Real& newAboveLength )
790  {
791  agx::Real oldLength = shape->getHeight();
792  newBelowLength = agx::Real( 0.5 ) * oldLength + position;
793  newAboveLength = oldLength - newBelowLength;
794  if ( newBelowLength < 0 || newAboveLength < 0 )
795  return nullptr;
796  shape->setHeight( newBelowLength );
797  return new T( shape->getRadius(), newAboveLength );
798  }
799 
800  AGXMODEL_EXPORT agxCollide::Shape* cutShape( agxCollide::Shape* shape, agx::Real position, agx::Real& newBelowLength, agx::Real& newAboveLength );
803 
805 
811  {
812  public:
819 
826  virtual void onCreate(agxModel::Tree::Branch* branch) override;
827 
834  virtual void onHighLoad(agxModel::Tree::Branch* branch) override;
835 
841  virtual agxModel::GenericBranchEventListener* clone() override;
842 
843  // For getTree to be accessible in python, we need to make it public
845 
846  protected:
847  virtual ~GenericBranchEventListener();
848 
850  };
851 }
852 
agxModel::Tree::BranchEventListener
BranchEventListener handles certain events happening to a Tree.
Definition: agxModel/Tree.h:269
agxModel::Tree::TreeContactEventListenerRef
agx::ref_ptr< TreeContactEventListener > TreeContactEventListenerRef
Definition: agxModel/Tree.h:753
agx::end
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Definition: LinearProbingHashSet.h:593
agxModel::Tree::TreeStepEventListenerRef
agx::ref_ptr< TreeStepEventListener > TreeStepEventListenerRef
Definition: agxModel/Tree.h:772
agxModel::cutShape
T * cutShape(T *shape, agx::Real position, agx::Real &newBelowLength, agx::Real &newAboveLength)
Definition: agxModel/Tree.h:789
Cylinder.h
agx::Real
double Real
Definition: Real.h:42
agxSDK::ExecuteFilter::match
virtual bool match(const agxCollide::GeometryContact &gc) const
Called when narrow phase tests have determined overlap (when we have detailed intersection between th...
Definition: ExecuteFilter.h:109
agxModel::Tree::m_customData
agx::ref_ptr< agx::Referenced > m_customData
Definition: agxModel/Tree.h:781
agxModel
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
agxData::transform
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
agxSDK::StepEventListener
Derive from this class to implement a listener for simulation step events.
Definition: StepEventListener.h:37
agxData::GeometryPair
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:65
agxModel::Tree::DisabledCollisionsGeometryContainer
agx::HashVector< agxCollide::Geometry *, agx::observer_ptr< agxCollide::Geometry > > DisabledCollisionsGeometryContainer
Definition: agxModel/Tree.h:49
agx::Vector< BranchRef >
agx::RigidBody
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:51
agxModel::Tree::Branch::MaxLoadType
MaxLoadType
Definition: agxModel/Tree.h:58
agxStream::Serializable
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
agxModel::Tree::BranchConstIterator
BranchContainer::const_iterator BranchConstIterator
Definition: agxModel/Tree.h:48
export.h
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
HashVector.h
agx::AffineMatrix4x4T< Real >
agxSDK::ExecuteFilter
Abstract base class that implements a filter that selects which events should trigger a Listener.
Definition: ExecuteFilter.h:33
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxModel::Tree::Branch::m_customData
agx::ref_ptr< agx::Referenced > m_customData
Definition: agxModel/Tree.h:259
Assembly.h
agxModel::Tree::TreeContactEventListener::~TreeContactEventListener
virtual ~TreeContactEventListener()
Definition: agxModel/Tree.h:745
agxModel::Tree::BranchIterator
BranchContainer::iterator BranchIterator
Definition: agxModel/Tree.h:47
agx::Vector< BranchRef >::iterator
BranchRef * iterator
Definition: Vector.h:62
agxModel::Tree::GroupIdContainer
agx::SetVector< agx::UInt32 > GroupIdContainer
Definition: agxModel/Tree.h:50
agxModel::Tree::Branch::m_maxLoad
agx::Vec2 m_maxLoad
Definition: agxModel/Tree.h:263
StepEventListener.h
AGXSTREAM_DECLARE_SERIALIZABLE
#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
agx::VectorPOD
Vector containing 'raw' data.
Definition: Vector.h:245
agxSDK::Assembly::remove
bool remove(agx::Constraint *constraint)
Remove a constraint from this assembly (does not recurse down in tree).
agx::observer_ptr< Branch >
agxModel::Tree::TreeStepEventListener::TreeStepEventListener
TreeStepEventListener(Tree *tree)
Definition: agxModel/Tree.h:758
agxModel::Tree::m_geometryIDs
GroupIdContainer m_geometryIDs
Definition: agxModel/Tree.h:780
agxModel::Tree::m_material
agx::MaterialRef m_material
Definition: agxModel/Tree.h:782
agxModel::Tree::TreeStepEventListener::~TreeStepEventListener
virtual ~TreeStepEventListener()
Definition: agxModel/Tree.h:762
agxModel::Tree::m_stepListener
TreeStepEventListenerRef m_stepListener
Definition: agxModel/Tree.h:785
agxModel::Tree::BranchEventListenerRef
agx::ref_ptr< BranchEventListener > BranchEventListenerRef
Definition: agxModel/Tree.h:315
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agxSDK::Assembly
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:67
agx::ref_ptr< Branch >
agxSDK::Simulation
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:125
agxCollide::Shape
Base class for a shape.
Definition: Shape.h:53
agx::SetVector< agx::UInt32 >
agxModel::Tree::TreeContactEventListener::TreeContactEventListener
TreeContactEventListener(Tree *tree)
Definition: agxModel/Tree.h:741
agxSDK::Assembly::add
bool add(agx::Constraint *constraint)
Add a constraint to the assembly.
agxModel::Tree::m_wakeOnContactListener
TreeContactEventListenerRef m_wakeOnContactListener
Definition: agxModel/Tree.h:784
agxModel::Tree
Tree is a structure of Branch objects in a mathematical Tree structure where a Branch can have severa...
Definition: agxModel/Tree.h:39
agxUtil::setEnableCollisions
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.
agxModel::Tree::TreeStepEventListener
Definition: agxModel/Tree.h:755
agxModel::Tree::BranchObs
agx::observer_ptr< Branch > BranchObs
Definition: agxModel/Tree.h:45
agxCollide::Geometry
The geometry representation used by the collision detection engine.
Definition: Geometry.h:91
agxModel::Tree::m_disabledCollisionsID
agx::UInt32 m_disabledCollisionsID
Definition: agxModel/Tree.h:778
agx::Vec2T< Real >
agx::Name
Representation of a name string.
Definition: Name.h:32
agxModel::getShapeRadius
AGXMODEL_EXPORT agx::Real getShapeRadius(const agxCollide::Shape *shape)
LockJoint.h
agxModel::Tree::TreeExecuteFilterRef
agx::ref_ptr< TreeExecuteFilter > TreeExecuteFilterRef
Definition: agxModel/Tree.h:357
agx::root
AGXCORE_EXPORT Component * root()
agxModel::GenericBranchEventListener
Generic BranchEventListener throwing exceptions for non-implemented virtual methods.
Definition: agxModel/Tree.h:810
agxModel::Tree::State
State
Definition: agxModel/Tree.h:359
agxModel::Tree::m_root
BranchRef m_root
Definition: agxModel/Tree.h:775
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:119
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
ContactEventListener.h
agxModel::Tree::Branch
Branch is a part of the Tree, with a body and a constraint to its parent.
Definition: agxModel/Tree.h:55
agxModel::Tree::BranchRef
agx::ref_ptr< Branch > BranchRef
Definition: agxModel/Tree.h:42
agxCollide::GeometryContact
A contact between two geometries.
Definition: Contacts.h:270
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
agxModel::Tree::TreeExecuteFilter
TreeExecuteFilter allows to catch contacts with the Tree.
Definition: agxModel/Tree.h:324
agxModel::Tree::Branch::m_constraint
agx::ConstraintRef m_constraint
Definition: agxModel/Tree.h:262
agx::Constraint
The base class for a constraint.
Definition: Constraint.h:88
agxModel::Tree::TreeContactEventListener
Definition: agxModel/Tree.h:738
AGXMODEL_EXPORT
#define AGXMODEL_EXPORT
Definition: agxModel/export.h:42
agx::Constraint::SolveType
SolveType
Specifies in what solvers the constraint will be solved.
Definition: Constraint.h:92
agxModel::Tree::m_externalDisabledGeometries
DisabledCollisionsGeometryContainer m_externalDisabledGeometries
Definition: agxModel/Tree.h:779
agx::Material
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:375
agx::Vec3T< Real >
agxModel::Tree::TreeContactEventListener::m_tree
agx::observer_ptr< Tree > m_tree
Definition: agxModel/Tree.h:750
agx::Uuid
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
agxTerrain::Profiler::clear
AGXTERRAIN_EXPORT void clear()
agxModel::Tree::m_geometriesID
agx::UInt32 m_geometriesID
Definition: agxModel/Tree.h:777
agx::UInt32
uint32_t UInt32
Definition: Integer.h:32
agxModel::Tree::Branch::m_parent
BranchObs m_parent
Definition: agxModel/Tree.h:257
agx::Frame
The object defining a frame of reference and providing transformations operations.
Definition: Frame.h:67
Capsule.h
agxModel::Tree::Branch::m_rb
agx::RigidBodyRef m_rb
Definition: agxModel/Tree.h:261
agx::HashVector
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
agxModel::Tree::m_state
int m_state
Definition: agxModel/Tree.h:776
agxModel::Tree::TreeStepEventListener::m_tree
agx::observer_ptr< Tree > m_tree
Definition: agxModel/Tree.h:769
agxSDK::Assembly::removeNotification
virtual void removeNotification(agxSDK::Simulation *)
Called when this assembly is removed from a simulation.
Definition: Assembly.h:657
agxModel::Tree::BranchEventListener::getTree
Tree * getTree() const
agxModel::Tree::m_branchListener
BranchEventListenerRef m_branchListener
Definition: agxModel/Tree.h:783
agxModel::getShapeLength
AGXMODEL_EXPORT agx::Real getShapeLength(const agxCollide::Shape *shape)
agxModel::Tree::Branch::m_branches
BranchContainer m_branches
Definition: agxModel/Tree.h:258
agxModel::Tree::BranchContainer
agx::Vector< BranchRef > BranchContainer
Definition: agxModel/Tree.h:46
agxSDK::ContactEventListener
Derive from this class to implement a listener for Collision events.
Definition: ContactEventListener.h:44
agxSDK::Assembly::addNotification
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:652