AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
LinkedStructure.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agxSDK/Assembly.h>
23
24#include <agx/BitState.h>
25
26namespace agx
27{
28 class InternalData;
29}
30
31namespace agxSDK
32{
34
41 {
42 public:
47
51 static const LinkedStructure* get(const agxCollide::Geometry* geometry);
52
57
61 static const LinkedStructure* get(const agx::RigidBody* body);
62
63 public:
68 virtual void setMaterial( agx::Material* material );
69
73 virtual agx::Material* getMaterial() const;
74
78 virtual agx::UInt32 getUniqueId() const;
79
83 virtual void setUniqueId( const agx::Uuid& uuid );
84
89
93 virtual void setDisabledCollisionsId( const agx::Uuid& uuid );
94
100 virtual void setEnableCollisions( agx::RigidBody* rb, agx::Bool enable );
101
105 virtual agx::Bool getEnableCollisions( const agx::RigidBody* rb ) const;
106
112 virtual void setEnableCollisions( agxCollide::Geometry* geometry, agx::Bool enable );
113
117 virtual agx::Bool getEnableCollisions( const agxCollide::Geometry* geometry ) const;
118
123 virtual void addGroup( agx::UInt32 id );
124
129 virtual void removeGroup( agx::UInt32 id );
130
134 virtual agx::Bool hasGroup( agx::UInt32 id ) const;
135
140 virtual void addGroup( const agx::Name& name );
141
146 virtual void removeGroup( const agx::Name& name );
147
151 virtual agx::Bool hasGroup( const agx::Name& name ) const;
152
157
165 virtual void setLinearVelocityDamping( const agx::Vec3& linearVelocityDamping );
166
171
179 virtual void setAngularVelocityDamping( const agx::Vec3& angularVelocityDamping );
180
185
190 virtual agx::Real getRestLength() const;
191
197
203 virtual agx::Real getMass() const;
204
209 virtual agx::Real getRestVolume() const;
210
217 virtual agx::Vec3 findPoint( agx::Real restLengthFromStart ) const;
218
225 virtual agx::Vec3 findPoint( const agx::Vec3& worldPoint ) const;
226
234 virtual agx::Real findCurrentLength( agx::Real restLengthFromStart ) const;
235
241 virtual agx::Real findRestLength( agx::Real currentLengthFromStart ) const;
242
249 virtual agx::Real findRestLengthTo( const agx::Vec3& worldPoint ) const;
250
256 virtual agx::Real findRestLengthTo( const agxSDK::LinkedSegment* segment ) const;
257
263 virtual agx::Bool rebind();
264
273
280
284 template<typename T>
285 T* getComponent() const;
286
291
292
297
301 agx::UInt getSegmentIndex( const agx::RigidBody* rb ) const;
302
306 const agxSDK::LinkedSegmentContainer& getSegmentsContainer() const;
307
308 public:
310
316 void addNotification( agxSDK::Simulation* simulation ) final override;
317
323 void removeNotification( agxSDK::Simulation* simulation ) final override;
324
325 using Assembly::addNotification;
326 using Assembly::removeNotification;
327
328 public:
334 struct IdData
335 {
339 IdData() : id( agx::InvalidIndex ), name( "" ) {}
340
344 IdData( agx::UInt32 id ) : id( id ), name( "" ) {}
345
349 IdData( const agx::Name& name ) : id( agx::InvalidIndex ), name( name ) {}
350
354 agx::Bool operator== ( const IdData& other ) const { return id == other.id && name == other.name; }
355
359 agx::Bool operator!= ( const IdData& other ) const { return !(*this == other); }
360
363 };
364
365 protected:
370 virtual void onAddNotification( agxSDK::Simulation* simulation );
371
376 virtual void onRemoveNotification( agxSDK::Simulation* simulation );
377
381 virtual void onMaterialNew();
382
386 virtual void onGroupAdd( const IdData& group );
387
391 virtual void onGroupRemove( const IdData& group );
392
396 virtual void onSetEnableCollisions( agxCollide::Geometry* geometry, agx::Bool enable );
397
401 virtual void onPreCollideStep();
402
406 virtual void onPreStep();
407
411 virtual void onPostStep();
412
416 virtual void onLastStep();
417
423 virtual void onPostIntegrate();
424
425 protected:
429 template<typename T>
430 static T* getT( agx::RigidBody* rb );
431
435 template<typename T>
436 static const T* getT( const agx::RigidBody* rb );
437
438 protected:
443
448
455 void add( LinkedSegment* segment );
456
460 agx::UInt getNumSegments() const;
461
466 template<typename T>
467 LinkedSegmentRange<T> getSegments() const;
468
475 template<typename T>
476 LinkedSegmentRange<T> getSegments( agx::UInt beginIndex, agx::UInt endIndex ) const;
477
481 LinkedSegment* getSegment( agx::UInt index ) const;
482
486 template<typename T>
487 T* getSegment( agx::Real restLengthFromStart ) const;
488
493 template<typename T>
494 T* findSegment( const agx::Vec3& worldPoint ) const;
495
499 template<typename ObjT>
500 LinkedSegment* getSegment( const ObjT* rb ) const;
501
505 template<typename T>
506 LinkedSegmentIterator<T> getSegmentIterator( agx::UInt index, agx::Bool boundaryless = false ) const;
507
511 template<typename T, typename ObjT>
512 LinkedSegmentIterator<T> getSegmentIterator( const ObjT* rb, agx::Bool boundaryless = false ) const;
513
517 LinkedStructureObjectData* getData( const agx::RigidBody* rb ) const;
518
522 std::tuple<agx::Vec3, LinkedSegment*> findPointAndSegment( const agx::Vec3& worldPoint ) const;
523
524 private:
525 // Events from the simulation.
526 friend class Simulation;
527
528 enum SimulationEvent
529 {
530 PRE_COLLIDE_STEP,
531 PRE_STEP,
532 POST_STEP,
533 LAST_STEP,
534 POST_INTEGRATE,
535 };
536
537 using IdDataContainer = agx::Vector<IdData>;
538
539 private:
540 void trigger( SimulationEvent simulationEvent );
541 void addGroup( const IdData& group );
542 void removeGroup( const IdData& group );
543 agx::Bool hasGroup( const IdData& group ) const;
544
546 friend class agx::InternalData;
550 agx::Referenced* getInternalData() const;
551
555 void setInternalData( agx::Referenced* data );
557
558 private:
559 agx::Uuid m_uniqueId;
560 agx::Uuid m_disabledCollisionsId;
561 LinkedSegmentContainer m_segments;
562 agx::MaterialRef m_material;
563 IdDataContainer m_externallyAddedGroups;
565 agx::Vec3 m_linearVelocityDamping;
566 agx::Vec3 m_angularVelocityDamping;
567 agx::ref_ptr<agx::Referenced> m_internalData;
568 };
569
570 template<typename T>
571 T* LinkedStructure::getT( agx::RigidBody* rb )
572 {
573 auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
574 return data != nullptr && data->linkedStructure != nullptr ? data->linkedStructure->template asSafe<T>() : nullptr;
575 }
576
577 template<typename T>
578 const T* LinkedStructure::getT( const agx::RigidBody* rb )
579 {
580 return LinkedStructure::getT<T>( const_cast<agx::RigidBody*>( rb ) );
581 }
582
583 template<typename T>
584 T* LinkedStructure::getComponent() const
585 {
586 for ( auto component : m_components ) {
587 auto typed = component->template asSafe<T>();
588 if ( typed != nullptr )
589 return typed;
590 }
591
592 return nullptr;
593 }
594
595 inline const LinkedSegmentContainer& LinkedStructure::getSegmentsContainer() const
596 {
597 return m_segments;
598 }
599
600 inline agx::UInt LinkedStructure::getNumSegments() const
601 {
602 return m_segments.size();
603 }
604
605 template<typename T>
606 LinkedSegmentRange<T> LinkedStructure::getSegments() const
607 {
609 LinkedSegmentIterator<T>::end( m_segments ) );
610 }
611
612 template<typename T>
613 LinkedSegmentRange<T> LinkedStructure::getSegments( agx::UInt beginIndex, agx::UInt endIndex ) const
614 {
615 return LinkedSegmentRange<T>( getSegmentIterator<T>( beginIndex ),
616 getSegmentIterator<T>( endIndex ) );
617 }
618
619 inline LinkedSegment* LinkedStructure::getSegment( agx::UInt index ) const
620 {
621 agxAssert( index < m_segments.size() );
622 return m_segments[ index ];
623 }
624
625 template<typename T>
626 T* LinkedStructure::getSegment( agx::Real restLengthFromStart ) const
627 {
628 if ( m_segments.empty() )
629 return nullptr;
630
631 if ( restLengthFromStart <= 0.0 )
632 return m_segments.front()->template as<T>();
633
634 agx::Real restLengthAlongStructure = 0.0;
635 for ( auto segment : this->template getSegments<T>() ) {
636 if ( restLengthAlongStructure + segment->getLength() < restLengthFromStart )
637 restLengthAlongStructure += segment->getLength();
638 else
639 return segment->template as<T>();
640 }
641
642 return m_segments.back()->template as<T>();
643 }
644
645 template<typename T>
646 T* LinkedStructure::findSegment( const agx::Vec3 &worldPoint ) const
647 {
648 auto [point, segment] = findPointAndSegment( worldPoint );
649 return segment->template as<T>();
650 }
651
652 template<>
653 inline LinkedSegment* LinkedStructure::getSegment( const agx::RigidBody* rb ) const
654 {
655 auto index = getSegmentIndex( rb );
656 return index != agx::InvalidIndex ?
657 m_segments[ index ] :
658 nullptr;
659 }
660
661 inline agx::UInt LinkedStructure::getSegmentIndex( const agx::RigidBody* rb ) const
662 {
663 auto data = getData( rb );
664 return data != nullptr && data->segment != nullptr && data->linkedStructure == this ?
665 data->segment->getIndex() :
667 }
668
669 template<typename T>
670 LinkedSegmentIterator<T> LinkedStructure::getSegmentIterator( agx::UInt index, agx::Bool boundaryless /* = false */ ) const
671 {
672 return LinkedSegmentIterator<T>( index, boundaryless, m_segments );
673 }
674
675 template<typename T, typename ObjT>
676 LinkedSegmentIterator<T> LinkedStructure::getSegmentIterator( const ObjT* rb, agx::Bool boundaryless /* = false */ ) const
677 {
678 auto segment = this->getSegment( rb );
679 if ( segment == nullptr )
680 return LinkedSegmentIterator<T>::end( m_segments );
681 return getSegmentIterator<T>( segment->getIndex(), boundaryless );
682 }
683
684 inline LinkedStructureObjectData* LinkedStructure::getData( const agx::RigidBody* rb ) const
685 {
686 auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
687 // Accepting data when linkedStructure == null since it can be considered implicitly
688 // related to this linked structure (data->linkedStructure = this to be made).
689 return data != nullptr && ( data->linkedStructure == this || data->linkedStructure == nullptr ) ?
690 data :
691 nullptr;
692 }
693
694 inline agx::Referenced* LinkedStructure::getInternalData() const
695 {
696 return m_internalData;
697 }
698}
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#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:92
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:70
Optionally unbounded linked structure segment iterator object to iterate segments in a SegmentStructu...
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.
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.
virtual agx::Real findRestLengthTo(const agx::Vec3 &worldPoint) const
Given any world point, find a center point on this structure that is closest to the given worldPoint ...
virtual agxCollide::GroupIdCollection findGroupIdCollection() const
static const LinkedStructure * get(const agx::RigidBody *body)
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
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 const LinkedStructure * get(const agxCollide::Geometry *geometry)
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.
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.
virtual agx::Real getCurrentLength() const
Calculates and returns the total length of this linked structure, at current state.
virtual void setUniqueId(const agx::Uuid &uuid)
Set the unique id of this linked structure.
void removeNotification(agxSDK::Simulation *simulation) final override
Called when this linked structure is added to the simulation.
agxSDK::LinkedStructureComponent * getComponent(const agx::Name &name) const
static LinkedStructure * get(agx::RigidBody *body)
virtual agx::Real getRestLength() const
Calculates and returns the total length of this linked structure, at rest.
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.
virtual void onPostStep()
Called on simulation post step event - after user postStep.
std::tuple< agx::Vec3, LinkedSegment * > findPointAndSegment(const agx::Vec3 &worldPoint) const
virtual agx::UInt32 getDisabledCollisionGroupId() const
virtual agx::Real getRestVolume() const
Calculates and returns the total volume of this linked structure, at rest.
virtual void setDisabledCollisionsId(const agx::Uuid &uuid)
Set the collision id of this linked structure.
agxSDK::LinkedSegmentRange< agxSDK::LinkedSegment > segments()
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.
virtual void onPostIntegrate()
Called on simulation post integrate event - when transforms has been updated given current velocities...
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
static LinkedStructure * get(agxCollide::Geometry *geometry)
virtual void setAngularVelocityDamping(const agx::Vec3 &angularVelocityDamping)
Assigns angular velocity damping to all segments in this linked structure.
virtual agx::Material * getMaterial() const
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.
virtual void onGroupAdd(const IdData &group)
Called when a new group has been added.
LinkedSegment * getSegment(const ObjT *rb) const
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:131
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:378
Representation of a name string.
Definition: Name.h:33
Base class providing referencing counted objects.
Definition: Referenced.h:120
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:49
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:42
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:32
bool Bool
Definition: Integer.h:40
uint64_t UInt
Definition: Integer.h:27
double Real
Definition: Real.h:41
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 == "".