AGX Dynamics  2.35.0.1
agx/Attachment.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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_ATTACHMENT_H
18 #define AGX_ATTACHMENT_H
19 
20 #include <agx/ConstraintAngle.h>
21 #include <agx/RigidBody.h>
22 
23 #ifdef _MSC_VER
24 # pragma warning( push )
25 # pragma warning( disable : 4251 ) // class X needs to have dll-interface to be used by clients of class Y
26 # pragma warning(disable: 6385) // Disable warning C6385: Reading invalid data
27 #endif
28 
29 
30 namespace agx
31 {
37  {
38  public:
43  {
44  U,
45  V,
46  N,
49  NUM_ELEMENTS
50  };
51 
52  public:
59  static void createAttachmentBase( const agx::Vec3d& N, agx::Vec3d& U, agx::Vec3d& V );
60  static void createAttachmentBase( const agx::Vec3f& N, agx::Vec3f& U, agx::Vec3f& V );
61 
62  public:
67  Attachment( agx::Frame* relFrame );
68 
75  virtual void transform();
76 
80  virtual agx::Bool objectDeleted() const;
81 
85  virtual agx::Bool valid() const;
86 
90  const agx::Vec3& get( agx::Attachment::Transformed entry ) const;
91 
96 
101  void setFrame( agx::Frame* frame );
102 
106  agx::Frame* getFrame() const;
107 
112  virtual agx::Real calculateDistance( const agx::Attachment* other ) const;
113 
117  virtual agx::Real calculateLinearDistance( const agx::Attachment* other ) const;
118 
122  virtual agx::Real calculateLinearSpeed( const agx::Attachment* other ) const;
123 
127  virtual agx::Real calculateAngularSpeed( const agx::Attachment* other ) const;
128 
132  virtual agx::Vec3 getLinearVelocity() const;
133 
137  virtual agx::Vec3 getAngularVelocity() const;
138 
142  template< typename T >
143  agx::Bool is() const;
144 
150  template< typename T >
151  T* as();
152 
157  template< typename T >
158  T* safeAs();
159 
165  template< typename T >
166  const T* as() const;
167 
172  template< typename T >
173  const T* safeAs() const;
174 
176 
177  protected:
182 
186  virtual ~Attachment();
187 
195  void transform( const agx::AffineMatrix4x4& cmTransform, const agx::Vec3& cmToModel, const agx::AffineMatrix4x4& localConstraintTransform );
196 
197  protected:
198  agx::Vec3 m_transformed[ NUM_ELEMENTS ];
200  };
201 
203 
208  {
209  public:
216 
220  virtual agx::Bool objectDeleted() const override;
221 
225  virtual agx::Bool valid() const override;
226 
231  virtual agx::Real calculateDistance( const agx::Attachment* other ) const override;
232 
236  virtual agx::Real calculateLinearDistance( const agx::Attachment* other ) const override;
237 
241  virtual agx::Real calculateLinearSpeed( const agx::Attachment* other ) const override;
242 
246  virtual agx::Real calculateAngularSpeed( const agx::Attachment* other ) const override;
247 
251  virtual agx::Vec3 getLinearVelocity() const override;
252 
256  virtual agx::Vec3 getAngularVelocity() const override;
257 
261  virtual void transform() override;
262 
269 
273  agx::RigidBody* getRigidBody() const;
274 
276 
277  protected:
282 
287 
288  protected:
291  };
292 
294 
301  {
302  public:
307  {
309  NUM_ELEMENTS
310  };
311 
312  public:
319  {
320  public:
324  virtual void transform( const agx::AttachmentPair& attachmentPair ) = 0;
325 
326  protected:
330  virtual ~CustomData() {}
331  };
332 
334 
335  public:
340 
344  virtual ~AttachmentPair();
345 
352 
357  void add( agx::Angle* angle );
358 
364  agx::Angle* getAngle( agx::UInt index ) const;
365 
370 
374  agx::Bool hasAngle( const agx::Angle* angle ) const;
375 
382 
389 
395 
400 
404  agx::Attachment* operator[] ( agx::UInt i ) const;
405 
409  agx::Attachment* getAttachment1() const;
410 
414  agx::Attachment* getAttachment2() const;
415 
419  agx::Bool valid() const;
420 
424  virtual void transform();
425 
429  virtual agx::Bool rebind();
430 
434  const agx::Vec3& get( agx::AttachmentPair::Transformed entry ) const;
435 
439  virtual void store( agxStream::OutputArchive& out ) const;
440 
445 
449  virtual void storeLightData( agxStream::StorageStream& str ) const;
450 
455 
456  protected:
457  AttachmentRef m_attachments[ 2 ];
458  agx::Vec3 m_transformed[ NUM_ELEMENTS ];
461  };
462 
464  {
465  return m_transformed[ entry ];
466  }
467 
469  {
470  return m_relFrame;
471  }
472 
473  template< typename T >
475  {
476  return dynamic_cast< const T* >( this ) != nullptr;
477  }
478 
479  template<>
480  AGX_FORCE_INLINE Bool Attachment::is<Attachment>() const
481  {
482  return true;
483  }
484 
485  template< typename T >
487  {
488  // Compiler warns on 'this == nullptr' because it is illegal to call member
489  // functions on nullptr. However, if it helps us find a nullptr dereference
490  // earlier, and with an assert instead of segmentation fault, then I think
491  // it's ok to leave the check.
492  #include <agx/PushDisableWarnings.h>
493  agxAssert( (void *)this == nullptr || this->is<T>() );
494  return static_cast< T* >( this );
495  #include <agx/PopDisableWarnings.h>
496  }
497 
498  template< typename T >
500  {
501  return dynamic_cast< T* >( this );
502  }
503 
504  template< typename T >
506  {
507  // Compiler warns on 'this == nullptr' because it is illegal to call member
508  // functions on nullptr. However, if it helps us find a nullptr dereference
509  // earlier, and with an assert instead of segmentation fault, then I think
510  // it's ok to leave the check.
511  #include <agx/PushDisableWarnings.h>
512  agxAssert( (void *)this == nullptr || this->is< T >() ); return static_cast< const T* >( this );
513  #include <agx/PopDisableWarnings.h>
514  }
515 
516  template< typename T >
518  {
519  return dynamic_cast< const T* >( this );
520  }
521 
523  {
524  return m_rb;
525  }
526 
528  {
529  agxAssert( i < 2 );
530  return m_attachments[ i ].get();
531  }
532 
534  {
535  return (*this)[ 0 ];
536  }
537 
539  {
540  return (*this)[ 1 ];
541  }
542 
544  {
545  return m_transformed[ entry ];
546  }
547 } // namespace agx
548 
549 #ifdef _MSC_VER
550 #pragma warning( pop )
551 #endif
552 
553 #endif
#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
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:52
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:57
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:45
Abstract base class for storing/restoring a line/drums with version control.
Definition: StorageStream.h:49
Constraint angle class.
Custom data for separation pair.
virtual void transform(const agx::AttachmentPair &attachmentPair)=0
Called after the other attachments have been transformed.
virtual ~CustomData()
Reference counted object, protected destructor.
Constraint attachment pair class.
AttachmentPair()
Default constructor, attachment frame 1 = attachment frame 2 = 0.
virtual void transform()
Transform attachment frames and updates data.
void setCustomData(agx::AttachmentPair::CustomData *customData)
Assign custom data to receive transform calls.
virtual void restoreLightData(agxStream::StorageStream &str)
Restore structural independent data from stream.
agx::Attachment * getAttachment2() const
agx::Angle * getAngle(agx::UInt index) const
Access constraint angle given index.
virtual void store(agxStream::OutputArchive &out) const
Store given stream.
void add(agx::Angle *angle)
Add constraint angle.
agx::Attachment * operator[](agx::UInt i) const
agx::Bool valid() const
agx::Attachment * getAttachment1() const
virtual agx::Bool rebind()
Rebind given first frame.
virtual void storeLightData(agxStream::StorageStream &str) const
Store structural independent data to stream.
Transformed
Data access enum.
@ SEPARATION
Separation vector, i.e., the vector from frame 2 to frame 1.
const agx::Vec3 & get(agx::AttachmentPair::Transformed entry) const
agx::Real calculateLinearSpeed() const
Calculates the linear speed between the two attachments, along the constraint axis.
virtual void restore(agxStream::InputArchive &in)
Restore given stream.
AngleContainer m_angles
agx::ref_ptr< CustomData > CustomDataRef
const agx::AngleContainer & getAngles() const
agx::Real calculateAngularSpeed() const
Calculates the rotational/angular speed between the two attachments, about the constraint axis.
virtual ~AttachmentPair()
Destructor.
agx::Bool hasAngle(const agx::Angle *angle) const
AttachmentRef m_attachments[2]
CustomDataRef m_customData
void set(agx::Attachment *a1, agx::Attachment *a2)
Assign constraint attachment frames.
agx::Vec3 m_transformed[NUM_ELEMENTS]
agx::AttachmentPair::CustomData * getCustomData() const
Constraint attachment base class for any type of frame.
static void createAttachmentBase(const agx::Vec3d &N, agx::Vec3d &U, agx::Vec3d &V)
Given vector N, create orthonormal base.
virtual agx::Bool valid() const
Transformed
Data access enum.
@ ANCHOR_POS
Constraint attachment world position.
@ V
Constraint attachment y-axis.
@ U
Constraint attachment x-axis.
@ N
Constraint attachment z-axis.
@ CM_TO_ANCHOR_POS
Vector from center of mass (if moving) to anchor point given in world frame.
agx::Frame * getFrame() const
virtual ~Attachment()
Reference counted object, protected destructor.
const agx::Vec3 & get(agx::Attachment::Transformed entry) const
T * safeAs()
Dynamic cast of this to T.
virtual agx::Vec3 getLinearVelocity() const
agx::Vec3 m_transformed[NUM_ELEMENTS]
static void createAttachmentBase(const agx::Vec3f &N, agx::Vec3f &U, agx::Vec3f &V)
virtual agx::Vec3 getAngularVelocity() const
virtual agx::Real calculateLinearDistance(const agx::Attachment *other) const
Attachment(agx::Frame *relFrame)
Construct given frame.
T * as()
Unsafe version, static cast of this to T.
Attachment()
Constructor to use during restore.
agx::Vec3 getLocal(agx::Attachment::Transformed entry) const
virtual agx::Real calculateLinearSpeed(const agx::Attachment *other) const
virtual agx::Real calculateDistance(const agx::Attachment *other) const
void setFrame(agx::Frame *frame)
Assign a new frame - transformed data will be updated.
virtual void transform()
Subclass - implement this method!
agx::Bool is() const
void transform(const agx::AffineMatrix4x4 &cmTransform, const agx::Vec3 &cmToModel, const agx::AffineMatrix4x4 &localConstraintTransform)
Fill Transformed data given center of mass transform and offset from model frame to center of mass fr...
virtual agx::Bool objectDeleted() const
agx::FrameRef m_relFrame
virtual agx::Real calculateAngularSpeed(const agx::Attachment *other) const
The object defining a frame of reference and providing transformations operations.
Definition: agx/Frame.h:68
Base class providing referencing counted objects.
Definition: Referenced.h:114
Constraint attachment class for agx::RigidBody.
RigidBodyAttachment()
Protected default constructor used by restore.
virtual agx::Bool objectDeleted() const override
virtual agx::Vec3 getAngularVelocity() const override
agx::RigidBodyObserver m_rb
virtual agx::Real calculateDistance(const agx::Attachment *other) const override
virtual agx::Vec3 getLinearVelocity() const override
virtual agx::Real calculateAngularSpeed(const agx::Attachment *other) const override
virtual ~RigidBodyAttachment()
Reference counted object, protected destructor.
virtual agx::Bool valid() const override
virtual agx::Real calculateLinearSpeed(const agx::Attachment *other) const override
agx::RigidBody * getRigidBody() const
virtual agx::Real calculateLinearDistance(const agx::Attachment *other) const override
virtual void transform() override
Transform given rigid body and frame.
void setRigidBody(agx::RigidBody *rb)
Assign rigid body to this constraint frame.
RigidBodyAttachment(agx::RigidBody *rb, agx::Frame *relFrame)
Construct given rigid body and relative frame.
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
A class holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3Template.h:46
T * get() const
Definition: ref_ptr.h:256
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
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
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.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::ref_ptr< RigidBodyAttachment > RigidBodyAttachmentRef
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
agx::ref_ptr< Attachment > AttachmentRef