AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Constraint.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 #ifndef AGX_CONSTRAINT_H
18 #define AGX_CONSTRAINT_H
19 
21 #include <agx/Frame.h>
22 #include <agx/Interaction.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 
55 
87  {
88  public:
90  enum SolveType
91  {
92  DIRECT = 1,
93  ITERATIVE = (1<<2),
94  DIRECT_AND_ITERATIVE = (DIRECT | ITERATIVE)
95  };
96 
97  public:
102  virtual agx::Bool rebind();
103 
105  agx::UInt getNumBodies() const;
106 
110  agx::RigidBody* getBodyAt( agx::UInt i );
111 
115  const agx::RigidBody* getBodyAt( agx::UInt i ) const;
116 
122  virtual void setCompliance( agx::Real compliance, agx::Int dof );
123 
128  void setCompliance( agx::Real compliance );
129 
136  virtual void setElasticity(agx::Real elasticity, agx::Int dof);
137 
142  virtual void setElasticity(agx::Real elasticity);
143 
144 
151  virtual agx::Real getCompliance( agx::UInt dof ) const;
152 
159  virtual agx::Real getElasticity(agx::UInt dof) const;
160 
166  virtual void setDamping( agx::Real damping, agx::Int dof );
167 
172  void setDamping( agx::Real damping );
173 
180  virtual Real getDamping( agx::UInt dof ) const;
181 
188  void setForceRange( agx::RangeReal forceRange );
189 
196  void setForceRange( agx::RangeReal forceRange, agx::Int dof );
197 
205  void setForceRange( agx::Real lower, agx::Real upper, agx::Int dof );
206 
213  agx::RangeReal getForceRange( agx::UInt dof = 0 ) const;
214 
222  agx::Real getCurrentForce( agx::UInt dof ) const;
223 
227  virtual agx::RegularizationParameters* getRegularizationParameters( agx::UInt i );
228 
232  virtual const agx::RegularizationParameters* getRegularizationParameters( agx::UInt i ) const;
233 
240  agx::Bool getValid() const;
241 
243  virtual void render( class agxRender::RenderManager* mgr, float scale ) const = 0;
244 
247 
248  #ifndef SWIG
249  const agx::ConstraintImplementation* getRep() const;
251  #endif
252 
254  agx::UInt32 getEntityId() const;
255 
260  virtual void setEnable( agx::Bool enable );
261 
265  agx::Bool getEnable() const;
266 
270  agx::Bool isEnabled() const;
271 
275  virtual int getNumDOF() const = 0;
276 
283  void setSolveType( agx::Constraint::SolveType solveType );
284 
289  agx::Constraint::SolveType getSolveType() const;
290 
294  agx::RigidBodyAttachment* getAttachment( const agx::RigidBody* rb ) const;
295 
299  agx::Attachment* getAttachment( agx::UInt i ) const;
300 
304  agx::AttachmentPair* getAttachmentPair() const;
305 
309  void setEnableDebugRendering( agx::Bool enable );
310 
314  bool getEnableDebugRendering() const;
315 
322  void setEnableComputeForces( agx::Bool enable );
323 
327  agx::Bool getEnableComputeForces() const;
328 
348  agx::Bool getLastForce( agx::UInt bodyIndex, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
349 
368  agx::Bool getLastForce( const agx::RigidBody* rb, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
369 
388  agx::Bool getLastLocalForce( agx::UInt bodyIndex, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
389 
407  agx::Bool getLastLocalForce( const agx::RigidBody* rb, agx::Vec3& retForce, agx::Vec3& retTorque, agx::Bool giveForceAtCm = false ) const;
408 
424  void setEnableLinearization(bool enable);
425 
430  bool getEnableLinearization() const;
431 
438  agx::Bool addElementaryConstraint( const agx::Name& name, agx::ElementaryConstraint* elementaryConstraint );
439 
446  agx::Bool addSecondaryConstraint( const agx::Name& name, agx::ElementaryConstraint* secondaryConstraint );
447 
453  agx::Bool removeElementaryConstraint( agx::ElementaryConstraint* elementaryConstraint );
454 
460  agx::Bool removeSecondaryConstraint( agx::ElementaryConstraint* secondaryConstraint );
461 
467  agx::Bool removeElementaryConstraint( const agx::Name& name );
468 
474  agx::Bool removeSecondaryConstraint( const agx::Name& name );
475 
480  agx::ElementaryConstraint* getElementaryConstraint( const agx::UInt index ) const;
481 
487  agx::ElementaryConstraint* getElementaryConstraintGivenName( const agx::Name& name ) const;
488 
493  agx::ElementaryConstraint* getSecondaryConstraint( const agx::UInt index ) const;
494 
500  agx::ElementaryConstraint* getSecondaryConstraintGivenName( const agx::Name& name ) const;
501 
505  agx::UInt getNumElementaryConstraints() const;
506 
510  agx::UInt getNumSecondaryConstraints() const;
511 
518  agx::UInt calculateNumActiveRows() const;
519 
531  static agx::Bool calculateFramesFromWorld( agx::Vec3 worldPoint, agx::Vec3 worldAxis, const agx::RigidBody* rb1, agx::Frame* rb1Frame, const agx::RigidBody* rb2, agx::Frame* rb2Frame );
532 
546  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);
547 
558  static agx::Bool calculateFramesFromBody( agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, const agx::RigidBody* body, agx::Frame* bodyFrame, const agx::RigidBody* otherBody, agx::Frame* otherFrame );
559 
572  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);
573 
584  template< typename T >
585  static agx::ref_ptr< T > createFromWorld( agx::Vec3 worldPoint, agx::Vec3 worldAxis, agx::RigidBody* rb1, agx::RigidBody* rb2 = nullptr );
586 
596  template< typename T >
597  static agx::ref_ptr< T > createFromBody( agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, agx::RigidBody* rb1, agx::RigidBody* rb2 = nullptr );
598 
600  template< typename T >
601  T *as();
602 
604  template< typename T >
605  const T *as() const;
606 
608 
609  void store( agxStream::OutputArchive& out ) const override;
610  void restore( agxStream::InputArchive& in ) override;
611 
612  virtual void storeLightData( agxStream::StorageStream& str ) const override;
613  virtual void restoreLightData( agxStream::StorageStream& str ) override;
614 
615  protected:
616  Constraint();
617  virtual ~Constraint();
618 
619  friend class DynamicsSystem;
620  virtual void setSystem( agx::DynamicsSystem* ) {}
621 
622  friend class agxSDK::Simulation;
623  virtual void setSimulation( agxSDK::Simulation* simulation ) override;
624  /*
625  Called when this Constraint is added to a simulation
626  (given that it not already is in the simulation).
627  */
628  virtual void addNotification() override;
629  virtual void removeNotification() override;
630 
631 #ifndef SWIG
632  virtual void preSystemCallback( agx::DynamicsSystem* dynamicsSystem ) override;
633  virtual void postSystemCallback( agx::DynamicsSystem* dynamicsSystem ) override;
634 #endif
635 
636  void setRep( agx::ConstraintImplementation* _rep );
637 
638  protected:
640 
641  private:
643  friend class InternalData;
647  agx::Referenced* getInternalData() const;
648 
652  void setInternalData( agx::Referenced* data );
654 
655  private:
656  agx::Bool m_debugRenderingEnable;
657  };
658 
659  // Backward compatibility. In export to Lua use:
660  // class Motor1D : public agx::TargetSpeedController {};
661  // class Lock1D : public agx::LockController {};
662  // class Range1D : public agx::RangeController {};
663  // class Screw1D : public agx::ScrewController {};
665  typedef agx::LockController Lock1D;
666  typedef agx::RangeController Range1D;
667  typedef agx::ScrewController Screw1D;
668 
674  class CALLABLE AGXPHYSICS_EXPORT Constraint1DOF : public agx::Constraint
675  {
676  public:
680  static agx::Constraint1DOF* safeCast( const agx::Constraint* constraint );
681 
682  public:
686  agx::Lock1D* getLock1D();
687 
691  const agx::Lock1D* getLock1D() const;
692 
696  agx::Motor1D* getMotor1D();
697 
701  const agx::Motor1D* getMotor1D() const;
702 
706  agx::ElectricMotorController* getElectricMotorController();
707 
711  const agx::ElectricMotorController* getElectricMotorController() const;
712 
716  agx::Range1D* getRange1D();
717 
721  const agx::Range1D* getRange1D() const;
722 
726  agx::FrictionController* getFrictionController();
727 
731  const agx::FrictionController* getFrictionController() const;
732 
739  agx::Real getAngle() const;
740 
747  agx::Real getCurrentSpeed() const;
748 
749  protected:
750  Constraint1DOF();
751  virtual ~Constraint1DOF();
752  };
753 
760  {
761  public:
765  static agx::Constraint2DOF* safeCast( const agx::Constraint* constraint );
766 
767  public:
769  enum DOF
770  {
771  FIRST = 0,
772  SECOND
773  };
774 
778  agx::Lock1D* getLock1D( agx::Constraint2DOF::DOF dof );
779 
783  const agx::Lock1D* getLock1D( agx::Constraint2DOF::DOF dof ) const;
784 
788  agx::Motor1D* getMotor1D( agx::Constraint2DOF::DOF dof );
789 
793  const agx::Motor1D* getMotor1D( agx::Constraint2DOF::DOF dof ) const;
794 
798  agx::ElectricMotorController* getElectricMotorController( agx::Constraint2DOF::DOF dof );
799 
803  const agx::ElectricMotorController* getElectricMotorController( agx::Constraint2DOF::DOF dof ) const;
804 
808  agx::Range1D* getRange1D( agx::Constraint2DOF::DOF dof );
809 
813  const agx::Range1D* getRange1D( agx::Constraint2DOF::DOF dof ) const;
814 
818  agx::FrictionController* getFrictionController( agx::Constraint2DOF::DOF dof );
819 
823  const agx::FrictionController* getFrictionController( agx::Constraint2DOF::DOF dof ) const;
824 
829  agx::Screw1D* getScrew1D();
830 
835  const agx::Screw1D* getScrew1D() const;
836 
841 
845  agx::Real getCurrentSpeed( agx::Constraint2DOF::DOF dof ) const;
846 
847  protected:
848  Constraint2DOF();
849  virtual ~Constraint2DOF();
850  };
851 
853  class AGXPHYSICS_EXPORT ConstraintFrame
854  {
855  public:
856  ConstraintFrame();
857  virtual ~ConstraintFrame();
858 
859  Vec3& operator [] ( const unsigned int& i );
860  const Vec3& operator [] ( const unsigned int& i ) const;
861 
862  void setTranslate( const Vec3& translate );
863  const Vec3& getTranslate() const;
864 
865  protected:
866  Vec3 m_data[ 4 ];
867  };
869 
871  {
872  return m_implementation;
873  }
874 
876  {
877  return m_implementation;
878  }
879 
880  template< typename T >
881  ref_ptr< T > Constraint::createFromWorld( Vec3 worldPoint, Vec3 worldAxis, RigidBody* rb1, RigidBody* rb2 /* = nullptr */ )
882  {
883  FrameRef f1 = new Frame();
884  FrameRef f2 = new Frame();
885  Constraint::calculateFramesFromWorld( worldPoint, worldAxis, rb1, f1, rb2, f2 );
886  ref_ptr< T > constraint = new T( rb1, f1, rb2, f2 );
887  return constraint;
888  }
889 
890  template< typename T >
891  ref_ptr< T > Constraint::createFromBody( Vec3 bodyPoint, Vec3 bodyAxis, RigidBody* rb1, RigidBody* rb2 /* = nullptr */ )
892  {
893  FrameRef f1 = new Frame();
894  FrameRef f2 = new Frame();
895  Constraint::calculateFramesFromBody( bodyPoint, bodyAxis, rb1, f1, rb2, f2 );
896  ref_ptr< T > constraint = new T( rb1, f1, rb2, f2 );
897  return constraint;
898  }
899 
901  template< typename T >
903  {
904  return dynamic_cast<T*>(this);
905  }
906 
908  template< typename T >
909  const T *Constraint::as() const
910  {
911  return dynamic_cast<const T*>(this);
912  }
913 
914 } //namespace agx
915 
916 #if defined(_MSC_VER)
917 #pragma warning( pop )
918 #endif
919 
920 #endif
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
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
static agx::ref_ptr< T > createFromBody(agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, agx::RigidBody *rb1, agx::RigidBody *rb2=nullptr)
Creates a constraint given a point and an axis in rb1 model frame.
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
static agx::Bool calculateFramesFromBody(agx::Vec3 bodyPoint, agx::Vec3 bodyAxis, const agx::RigidBody *body, agx::Frame *bodyFrame, const agx::RigidBody *otherBody, agx::Frame *otherFrame)
Calculates the constraint attachment frames given point and axis in body coordinates of body...
Specialization for constraints that have only one degree of freedom such as Hinge and Prismatic...
Definition: Constraint.h:759
#define AGXPHYSICS_EXPORT
The base class for a constraint.
Definition: Constraint.h:86
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
Specialization for constraints that have only one degree of freedom such as Hinge and Prismatic...
Definition: Constraint.h:674
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:113
The base class for interactions.
Definition: Interaction.h:30
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:620
Constraint attachment base class for any type of frame.
Definition: Attachment.h:36
Elementary secondary constraint to keep constraint angle within two given values. ...
DOF
Specifies a selected degree of freedom.
Definition: Constraint.h:769
static agx::Bool calculateFramesFromWorld(agx::Vec3 worldPoint, agx::Vec3 worldAxis, const agx::RigidBody *rb1, agx::Frame *rb1Frame, const agx::RigidBody *rb2, agx::Frame *rb2Frame)
Given a point and an axis in world, this function calculates each local attachment frame for one or t...
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:52
Internal data for any object, ObjT, with methods: agx::Referenced* ObjT::getInternalData() const; voi...
Definition: InternalData.h:32
#define CALLABLE
Definition: Object.h:76
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:639
All ghost variables have compliance and damping attributes.
SolveType
Specifies in what solvers the constraint will be solved.
Definition: Constraint.h:90
The object defining a frame of reference and providing transformations operations.
Definition: Frame.h:67
agx::ConstraintImplementation * getRep()
Definition: Constraint.h:870
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
static agx::ref_ptr< T > createFromWorld(agx::Vec3 worldPoint, agx::Vec3 worldAxis, agx::RigidBody *rb1, agx::RigidBody *rb2=nullptr)
Creates a constraint given a point and an axis in world frame.
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:121
#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:220
double Real
Definition: Real.h:42
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:101
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:100
T * as()
Try to do dynamic_cast on the constraint and return a pointer to the casted constraint.
Definition: Constraint.h:902
AGX_DECLARE_VECTOR_TYPES(Clock)