AGX Dynamics  2.37.2.0
LinkedSegment.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 
21 #include <agx/RigidBody.h>
22 #include <agx/Constraint.h>
23 
24 #include <agxUtil/agxUtil.h>
25 
26 namespace agxSDK
27 {
30 
39  {
40  public:
48  LinkedSegment( const agx::AffineMatrix4x4& transform, const agx::Vec3& halfExtents );
49 
53  agx::RigidBody* getRigidBody() const;
54 
62  agx::Constraint* getConstraint() const;
63 
67  const agx::Vec3& getHalfExtents() const;
68 
72  agx::Real getLength() const;
73 
78  agx::Real getCurrentLength() const;
79 
84  agx::Vec3 getBeginPosition() const;
85 
90  agx::Vec3 getEndPosition() const;
91 
95  agx::Vec3 getCenterPosition() const;
96 
100  agx::Vec3 getDirection() const;
101 
105  agx::UInt getIndex() const;
106 
110  agxSDK::LinkedStructure* getLinkedStructure() const;
111 
112  public:
114 
121  template<typename T>
122  T* createConstraintWithPrev( const agxSDK::LinkedSegment& prev,
123  const agx::Vec3& localConstraintAxis,
124  const agx::Vec3& localConstraintY,
125  agx::Bool disableCollisions );
126 
131  static LinkedSegment* get( agx::RigidBody* rb );
132 
137  static const LinkedSegment* get( const agx::RigidBody* rb );
138 
143  template<typename T>
144  static T* getT( agx::RigidBody* rb );
145 
150  template<typename T>
151  static const T* getT( const agx::RigidBody* rb );
153 
154  protected:
158  LinkedSegment();
159  LinkedSegment(agx::RigidBody* body, const agx::Vec3& halfExtents, agx::Constraint* constraint);
160 
164  virtual ~LinkedSegment();
165 
166  void setHalfExtents(const agx::Vec3& halfExtens);
167  void setRigidBody(agx::RigidBody* body);
168  void setConstraint(agx::Constraint* constraint);
169 
170  private:
171  friend class LinkedStructure;
172 
177  LinkedStructureObjectData* getData() const;
178 
182  void setIndex( agx::UInt index );
184 
185  private:
186  agx::UInt m_index;
187  agx::RigidBodyRef m_rb;
188  agx::Vec3 m_halfExtents;
189  agx::ConstraintRef m_constraint;
190  };
191 
192  inline agx::UInt LinkedSegment::getIndex() const
193  {
194  return m_index;
195  }
196 
197  inline LinkedStructureObjectData* LinkedSegment::getData() const
198  {
199  return LinkedStructureObjectData::get<LinkedStructureObjectData>( m_rb );
200  }
201 
203  {
204  const auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( m_rb );
205  return data != nullptr ? data->linkedStructure : nullptr;
206  }
207 
208  template<typename T>
209  T* LinkedSegment::createConstraintWithPrev( const LinkedSegment& prev,
210  const agx::Vec3& localConstraintAxis,
211  const agx::Vec3& localConstraintY,
212  agx::Bool disableCollisions )
213  {
214  agxAssert( m_constraint == nullptr );
215  if ( m_constraint != nullptr )
216  return nullptr;
217 
218  const auto localConstraintX = localConstraintY ^ localConstraintAxis;
219  agx::OrthoMatrix3x3 localRotation = agx::OrthoMatrix3x3( localConstraintX.x(), localConstraintX.y(), localConstraintX.z(),
220  localConstraintY.x(), localConstraintY.y(), localConstraintY.z(),
221  localConstraintAxis.x(), localConstraintAxis.y(), localConstraintAxis.z() );
222 
223  agx::FrameRef prevFrame = new agx::Frame();
224  agx::FrameRef thisFrame = new agx::Frame();
225 
226  prevFrame->setLocalMatrix( agx::AffineMatrix4x4( localRotation,
227  agx::Vec3( 0, 0, prev.getLength() ) ) );
228  thisFrame->setLocalMatrix( agx::AffineMatrix4x4( localRotation, agx::Vec3( 0, 0, 0 ) ) );
229 
230  m_constraint = new T( prev.getRigidBody(), prevFrame, getRigidBody(), thisFrame );
231 
232  if ( disableCollisions )
233  agxUtil::setEnableCollisions( prev.getRigidBody(), getRigidBody(), false );
234 
235  return m_constraint->as<T>();
236  }
237 
238  inline LinkedSegment* LinkedSegment::get( agx::RigidBody* rb )
239  {
240  auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
241  if ( data == nullptr )
242  return nullptr;
243  return data->segment;
244  }
245 
246  inline const LinkedSegment* LinkedSegment::get( const agx::RigidBody* rb )
247  {
248  const auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
249  if ( data == nullptr )
250  return nullptr;
251  return data->segment;
252  }
253 
254  template<typename T>
255  T* LinkedSegment::getT( agx::RigidBody* rb )
256  {
257  auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
258  if ( data == nullptr || data->segment == nullptr )
259  return nullptr;
260  return data->segment->template asSafe<T>();
261  }
262 
263  template<typename T>
264  const T* LinkedSegment::getT( const agx::RigidBody* rb )
265  {
266  const auto data = LinkedStructureObjectData::get<LinkedStructureObjectData>( rb );
267  if ( data == nullptr || data->segment == nullptr )
268  return nullptr;
269  return data->segment->template asSafe<T>();
270  }
271 }
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
Basic implementation of any structure where bodies are linked/constrained together in a well defined ...
bool Bool
Definition: Integer.h:48
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
#define AGXPHYSICS_EXPORT
The base class for a constraint.
Definition: Constraint.h:88
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
void setLocalMatrix(const agx::AffineMatrix4x4 &matrix)
Assign the local transformation matrix for this frame ignoring any eventual parent transformation...
Definition: Frame.h:526
Base class providing referencing counted objects.
Definition: Referenced.h:119
uint32_t UInt
Definition: Integer.h:34
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::ref_ptr< Constraint > ConstraintRef
Definition: Constraint.h:53
agx::Real getLength() const
agx::RigidBody * getRigidBody() const
#define agxAssert(expr)
Definition: debug.h:143
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
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...
agx::ref_ptr< RigidBody > RigidBodyRef
Definition: RigidBody.h:47
The object defining a frame of reference and providing transformations operations.
Definition: Frame.h:67
#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
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Segment object in a linked structure.
Definition: LinkedSegment.h:38
Connection back from bodies, constraints etc.
double Real
Definition: Real.h:42
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
agxSDK::LinkedStructure * getLinkedStructure() const
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
Specialized types of matrices for holding orthogonal transformation matrices.