AGX Dynamics  2.37.3.2
Constraint.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 
20 #include <agx/Frame.h>
21 #include <agx/Interaction.h>
22 #include <agx/SetVector.h>
23 
24 #if defined(_MSC_VER)
25 #pragma warning( push )
26 #pragma warning( disable : 4589 ) // Disable: 4589: Constructor of abstract class 'agx::FrictionModel' ignores initializer for virtual base class 'agxStream::Serializable'
27 #endif
28 
29 
30 namespace agxSDK
31 {
32  class Simulation;
33 }
34 
35 namespace agxRender
36 {
37  class RenderManager;
38 }
39 
40 namespace agx
41 {
42  // forward declaration
43  class Constraint;
44  class DynamicsSystem;
45  class ConstraintImplementation;
46  class DebugRendererCache;
47  class Attachment;
48  class AttachmentPair;
49  class RigidBodyAttachment;
50  class RigidBody;
51  class RegularizationParameters;
52 
56 
57 
89  {
90  public:
92  enum SolveType
93  {
94  DIRECT = 1,
95  ITERATIVE = (1<<2),
96  DIRECT_AND_ITERATIVE = (DIRECT | ITERATIVE)
97  };
98 
99  public:
104  virtual agx::Bool rebind();
105 
107  agx::UInt getNumBodies() const;
108 
112  agx::RigidBody* getBodyAt( agx::UInt i );
113 
117  const agx::RigidBody* getBodyAt( agx::UInt i ) const;
118 
124  virtual void setCompliance( agx::Real compliance, agx::Int dof );
125 
130  void setCompliance( agx::Real compliance );
131 
138  virtual void setElasticity(agx::Real elasticity, agx::Int dof);
139 
144  virtual void setElasticity(agx::Real elasticity);
145 
146 
153  virtual agx::Real getCompliance( agx::UInt dof ) const;
154 
161  virtual agx::Real getElasticity(agx::UInt dof) const;
162 
168  virtual void setDamping( agx::Real damping, agx::Int dof );
169 
174  void setDamping( agx::Real damping );
175 
182  virtual Real getDamping( agx::UInt dof ) const;
183 
190  void setForceRange( agx::RangeReal forceRange );
191 
198  void setForceRange( agx::RangeReal forceRange, agx::Int dof );
199 
207  void setForceRange( agx::Real lower, agx::Real upper, agx::Int dof );
208 
215  agx::RangeReal getForceRange( agx::UInt dof = 0 ) const;
216 
224  agx::Real getCurrentForce( agx::UInt dof ) const;
225 
229  virtual agx::RegularizationParameters* getRegularizationParameters( agx::UInt i );
230 
234  virtual const agx::RegularizationParameters* getRegularizationParameters( agx::UInt i ) const;
235 
242  agx::Bool getValid() const;
243 
245  virtual void render( class agxRender::RenderManager* mgr, float scale ) const = 0;
246 
249 
250  #ifndef SWIG
251  const agx::ConstraintImplementation* getRep() const;
253  #endif
254 
256  agx::UInt32 getEntityId() const;
257 
262  virtual void setEnable( agx::Bool enable );
263 
267  agx::Bool getEnable() const;
268 
272  agx::Bool isEnabled() const;
273 
277  virtual int getNumDOF() const = 0;
278 
285  void setSolveType( agx::Constraint::SolveType solveType );
286 
291  agx::Constraint::SolveType getSolveType() const;
292 
296  agx::RigidBodyAttachment* getAttachment( const agx::RigidBody* rb ) const;
297 
301  agx::Attachment* getAttachment( agx::UInt i ) const;
302 
306  agx::AttachmentPair* getAttachmentPair() const;
307 
311  void setEnableDebugRendering( agx::Bool enable );
312 
316  bool getEnableDebugRendering() const;
317 
324  void setEnableComputeForces( agx::Bool enable );
325 
329  agx::Bool getEnableComputeForces() const;
330 
350  agx::Bool getLastForce( agx::UInt bodyIndex, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
351 
370  agx::Bool getLastForce( const agx::RigidBody* rb, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
371 
390  agx::Bool getLastLocalForce( agx::UInt bodyIndex, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
391 
409  agx::Bool getLastLocalForce( const agx::RigidBody* rb, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
410 
426  void setEnableLinearization(bool enable);
427 
432  bool getEnableLinearization() const;
433 
440  agx::Bool addElementaryConstraint( const agx::Name& name, agx::ElementaryConstraint* elementaryConstraint );
441 
448  agx::Bool addSecondaryConstraint( const agx::Name& name, agx::ElementaryConstraint* secondaryConstraint );
449 
455  agx::Bool removeElementaryConstraint( agx::ElementaryConstraint* elementaryConstraint );
456 
462  agx::Bool removeSecondaryConstraint( agx::ElementaryConstraint* secondaryConstraint );
463 
469  agx::Bool removeElementaryConstraint( const agx::Name& name );
470 
476  agx::Bool removeSecondaryConstraint( const agx::Name& name );
477 
482  agx::ElementaryConstraint* getElementaryConstraint( const agx::UInt index ) const;
483 
489  agx::ElementaryConstraint* getElementaryConstraintGivenName( const agx::Name& name ) const;
490 
495  agx::ElementaryConstraint* getSecondaryConstraint( const agx::UInt index ) const;
496 
502  agx::ElementaryConstraint* getSecondaryConstraintGivenName( const agx::Name& name ) const;
503 
507  agx::UInt getNumElementaryConstraints() const;
508 
512  agx::UInt getNumSecondaryConstraints() const;
513 
520  agx::UInt calculateNumActiveRows() const;
521 
533  static agx::Bool calculateFramesFromWorld( agx::Vec3 worldPoint, agx::Vec3 worldAxis, const agx::RigidBody* rb1, agx::Frame* rb1Frame, const agx::RigidBody* rb2, agx::Frame* rb2Frame );
534 
548  static agx::Bool calculateFramesFromWorld(agx::Vec3 worldPoint, agx::Vec3 worldAxis, agx::Vec3 secondWorldAxis, const agx::RigidBody* rb1, agx::Frame* rb1Frame, const agx::RigidBody* rb2, agx::Frame* rb2Frame);
549 
560  static agx::Bool calculateFramesFromBody( agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, const agx::RigidBody* body, agx::Frame* bodyFrame, const agx::RigidBody* otherBody, agx::Frame* otherFrame );
561 
574  static agx::Bool calculateFramesFromBody(agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, agx::Vec3 secondBodyAxis, const agx::RigidBody* body, agx::Frame* bodyFrame, const agx::RigidBody* otherBody, agx::Frame* otherFrame);
575 
586  template< typename T >
587  static agx::ref_ptr< T > createFromWorld( agx::Vec3 worldPoint, agx::Vec3 worldAxis, agx::RigidBody* rb1, agx::RigidBody* rb2 = nullptr );
588 
598  template< typename T >
599  static agx::ref_ptr< T > createFromBody( agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, agx::RigidBody* rb1, agx::RigidBody* rb2 = nullptr );
600 
602  template< typename T >
603  T *as();
604 
606  template< typename T >
607  const T *as() const;
608 
610 
611  void store( agxStream::OutputArchive& out ) const override;
612  void restore( agxStream::InputArchive& in ) override;
613 
614  virtual void storeLightData( agxStream::StorageStream& str ) const override;
615  virtual void restoreLightData( agxStream::StorageStream& str ) override;
616 
617  protected:
618  Constraint();
619  virtual ~Constraint();
620 
621  friend class DynamicsSystem;
622  virtual void setSystem( agx::DynamicsSystem* ) {}
623 
624  friend class agxSDK::Simulation;
625  virtual void setSimulation( agxSDK::Simulation* simulation ) override;
626  /*
627  Called when this Constraint is added to a simulation
628  (given that it not already is in the simulation).
629  */
630  virtual void addNotification() override;
631  virtual void removeNotification() override;
632 
633 #ifndef SWIG
634  virtual void preSystemCallback( agx::DynamicsSystem* dynamicsSystem ) override;
635  virtual void postSystemCallback( agx::DynamicsSystem* dynamicsSystem ) override;
636 #endif
637 
638  void setRep( agx::ConstraintImplementation* _rep );
639 
640  protected:
642 
643  private:
645  friend class InternalData;
649  agx::Referenced* getInternalData() const;
650 
654  void setInternalData( agx::Referenced* data );
656 
657  private:
658  agx::Bool m_debugRenderingEnable;
659  };
660 
665 
667  AGX_DECLARE_VECTOR_TYPES(Constraint1DOF);
668 
673  class CALLABLE AGXPHYSICS_EXPORT Constraint1DOF : public agx::Constraint
674  {
675  public:
679  static agx::Constraint1DOF* safeCast( const agx::Constraint* constraint );
680 
681  public:
685  agx::Lock1D* getLock1D();
686 
690  const agx::Lock1D* getLock1D() const;
691 
695  agx::Motor1D* getMotor1D();
696 
700  const agx::Motor1D* getMotor1D() const;
701 
705  agx::ElectricMotorController* getElectricMotorController();
706 
710  const agx::ElectricMotorController* getElectricMotorController() const;
711 
715  agx::Range1D* getRange1D();
716 
720  const agx::Range1D* getRange1D() const;
721 
725  agx::FrictionController* getFrictionController();
726 
730  const agx::FrictionController* getFrictionController() const;
731 
738  agx::Real getAngle() const;
739 
746  agx::Real getCurrentSpeed() const;
747 
748  protected:
749  Constraint1DOF();
750  virtual ~Constraint1DOF();
751  };
752 
753 
754 
756 
762  {
763  public:
767  static agx::Constraint2DOF* safeCast( const agx::Constraint* constraint );
768 
769  public:
771  enum DOF
772  {
773  FIRST = 0,
774  SECOND
775  };
776 
780  agx::Lock1D* getLock1D( agx::Constraint2DOF::DOF dof );
781 
785  const agx::Lock1D* getLock1D( agx::Constraint2DOF::DOF dof ) const;
786 
790  agx::Motor1D* getMotor1D( agx::Constraint2DOF::DOF dof );
791 
795  const agx::Motor1D* getMotor1D( agx::Constraint2DOF::DOF dof ) const;
796 
800  agx::ElectricMotorController* getElectricMotorController( agx::Constraint2DOF::DOF dof );
801 
805  const agx::ElectricMotorController* getElectricMotorController( agx::Constraint2DOF::DOF dof ) const;
806 
810  agx::Range1D* getRange1D( agx::Constraint2DOF::DOF dof );
811 
815  const agx::Range1D* getRange1D( agx::Constraint2DOF::DOF dof ) const;
816 
820  agx::FrictionController* getFrictionController( agx::Constraint2DOF::DOF dof );
821 
825  const agx::FrictionController* getFrictionController( agx::Constraint2DOF::DOF dof ) const;
826 
831  agx::Screw1D* getScrew1D();
832 
837  const agx::Screw1D* getScrew1D() const;
838 
843 
847  agx::Real getCurrentSpeed( agx::Constraint2DOF::DOF dof ) const;
848 
849  protected:
850  Constraint2DOF();
851  virtual ~Constraint2DOF();
852  };
853 
855  class AGXPHYSICS_EXPORT ConstraintFrame
856  {
857  public:
858  ConstraintFrame();
859  virtual ~ConstraintFrame();
860 
861  Vec3& operator [] ( const unsigned int& i );
862  const Vec3& operator [] ( const unsigned int& i ) const;
863 
864  void setTranslate( const Vec3& translate );
865  const Vec3& getTranslate() const;
866 
867  protected:
868  Vec3 m_data[ 4 ];
869  };
871 
873  {
874  return m_implementation;
875  }
876 
877  inline const ConstraintImplementation* Constraint::getRep() const
878  {
879  return m_implementation;
880  }
881 
882  template< typename T >
883  ref_ptr< T > Constraint::createFromWorld( Vec3 worldPoint, Vec3 worldAxis, RigidBody* rb1, RigidBody* rb2 /* = nullptr */ )
884  {
885  FrameRef f1 = new Frame();
886  FrameRef f2 = new Frame();
887  Constraint::calculateFramesFromWorld( worldPoint, worldAxis, rb1, f1, rb2, f2 );
888  ref_ptr< T > constraint = new T( rb1, f1, rb2, f2 );
889  return constraint;
890  }
891 
892  template< typename T >
893  ref_ptr< T > Constraint::createFromBody( Vec3 bodyPoint, Vec3 bodyAxis, RigidBody* rb1, RigidBody* rb2 /* = nullptr */ )
894  {
895  FrameRef f1 = new Frame();
896  FrameRef f2 = new Frame();
897  Constraint::calculateFramesFromBody( bodyPoint, bodyAxis, rb1, f1, rb2, f2 );
898  ref_ptr< T > constraint = new T( rb1, f1, rb2, f2 );
899  return constraint;
900  }
901 
903  template< typename T >
904  T *Constraint::as()
905  {
906  return dynamic_cast<T*>(this);
907  }
908 
910  template< typename T >
911  const T *Constraint::as() const
912  {
913  return dynamic_cast<const T*>(this);
914  }
915 
916 } //namespace agx
917 
918 #if defined(_MSC_VER)
919 #pragma warning( pop )
920 #endif
921 
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
AGXFMI_EXPORT InputVariable_Real * setCompliance(agx::Constraint *constraint)
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
Elementary constraint base class with interface and global constraint functionality.
Implementation of a electric motor controller.
bool Bool
Definition: Integer.h:48
Specialization for constraints that have two degree of freedom such as Cylindrical.
Definition: Constraint.h:761
#define AGXPHYSICS_EXPORT
The base class for a constraint.
Definition: Constraint.h:88
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Specialization for constraints that have only one degree of freedom such as Hinge and Prismatic...
Definition: Constraint.h:673
Namespace containing classes for handling debug rendering of collision geometries, constraints, contacts and rigid bodies.
Definition: Constraint.h:35
Constraint attachment class for agx::RigidBody.
Definition: Attachment.h:207
Representation of a name string.
Definition: Name.h:32
Translational or rotational friction controller for Hinge, Prismatic and CylindricalJoint.
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc...
Base class providing referencing counted objects.
Definition: Referenced.h:119
The base class for interactions.
Definition: Interaction.h:31
Elementary secondary constraint to keep constraint angle at a given target position.
uint32_t UInt
Definition: Integer.h:34
Constraint attachment pair class.
Definition: Attachment.h:300
virtual void setSystem(agx::DynamicsSystem *)
Definition: Constraint.h:622
Constraint attachment base class for any type of frame.
Definition: Attachment.h:36
Elementary secondary constraint to keep constraint angle within two given values. ...
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
DOF
Specifies a selected degree of freedom.
Definition: Constraint.h:771
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
Internal data for any object, ObjT, with methods: agx::Referenced* ObjT::getInternalData() const; voi...
Definition: InternalData.h:32
#define CALLABLE
Definition: Object.h:73
The complete physical system with bodies, interactions, data layout, time stepper, and solver.
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
Abstract base class for storing/restoring a line/drums with version control.
Definition: StorageStream.h:48
agx::ConstraintImplementation * m_implementation
Definition: Constraint.h:641
All ghost variables have compliance and damping attributes.
SolveType
Specifies in what solvers the constraint will be solved.
Definition: Constraint.h:92
The object defining a frame of reference and providing transformations operations.
Definition: Frame.h:67
AGXFMI_EXPORT InputVariable_Real * setDamping(agx::Constraint *constraint)
Elementary secondary constraint to drive something given target speed (translational or rotational)...
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
#define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE(T)
Use this in a pure abstract Serializable class to add the required methods Important: Use full namesp...
Definition: Serializable.h:221
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
Real getAngle(const Vec3 &a, const Vec3 &b, const Vec3 &c)
a and b should be orthogonal
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:37
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
agx::SetVector< ref_ptr< Constraint > > ConstraintRefSetVector
Definition: Constraint.h:55