AGX Dynamics  2.37.3.2
Link.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
9 having been advised so by Algoryx Simulation AB for a time limited evaluation,
10 or having purchased a 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 
19 #include <agxWire/ILinkNode.h>
21 
22 #define LINK_V2_SERIALIZATION_VERSION agx::UInt16( 37 )
23 
24 namespace agxSDK
25 {
26  class SimulationProxy;
27 }
28 
29 namespace agxWire
30 {
31  class Winch;
32 
34 
41  {
42  public:
46 
47  public:
53  {
54  WIRE_BEGIN = 0,
55  WIRE_END = 1,
56  LINK ,
57  INVALID_CONNECTION = 0xFF
59  };
60 
64  enum class WinchState
65  {
66  PULLED_IN,
68  INSIDE,
71  OVERLAPPING,
73  NONE
74  };
75 
76  public:
84  static agxWire::Wire* getWire( const agxWire::ConnectingNode* connectingNode,
85  agx::Bool findClosestIfLinkConnections = false );
86 
90  static agxWire::Link* getLink( const agxWire::ConnectingNode* connectingNode );
91 
95  static agxWire::Link* getConnectingLink( const agxWire::ConnectingNode* connectingNode );
96 
100  static agxWire::Link::ConnectionType getConnectionType( const agxWire::ConnectingNode* connectingNode );
101 
112  static agx::Bool connect( agxWire::Wire* wire1,
113  const agx::Vec3& relativeTranslate1,
114  agxWire::Link* link,
115  agxWire::Wire* wire2,
116  const agx::Vec3& relativeTranslate2 );
117 
118  public:
122  Link( agx::RigidBody* rb );
123 
127  agx::RigidBody* getRigidBody() const;
128 
138  agx::Bool connect( agxWire::Wire* wire, const agx::Vec3& relativeTranslate, agxWire::Link::ConnectionType type );
139 
146  agx::Bool connect( agxWire::Link* link, agx::Constraint* constraint );
147 
156  agx::Bool attach( agxWire::Wire* wire, const agx::Vec3& relativeTranslate, agxWire::Link::ConnectionType type );
157 
163  agx::Bool disconnect( agxWire::Wire* wire );
164 
170  agx::Bool disconnect( agxWire::Link* otherLink );
171 
178  agxWire::ILinkNode::ConnectionProperties* getDefaultWireConnectionProperties();
179 
183  const agxWire::ILinkNode::ConnectionProperties* getDefaultWireConnectionProperties() const;
184 
190  agx::Bool add( agxWire::Link::Algorithm* algorithm );
191 
197  agx::Bool remove( agxWire::Link::Algorithm* algorithm );
198 
203  template< typename T >
204  T* getAlgorithm() const;
205 
209  void transform();
210 
214  agx::UInt getNumConnections() const;
215 
219  const agxWire::Link::ConnectionsContainer& getConnections() const;
220 
224  agx::Bool hasConnection( const agxWire::Wire* wire ) const;
225 
229  agxWire::ILinkNode* getConnectingNode( const agxWire::Wire* wire ) const;
230 
234  agxWire::ILinkNode* getConnectingNode( const agxWire::Link* link ) const;
235 
239  agx::Constraint* getConnectingConstraint( const agxWire::Link* link ) const;
240 
244  template<typename Pred>
245  agxWire::Link::ConnectionsContainer getConnectingNodes( Pred pred ) const;
246 
250  template<typename Pred>
251  agxWire::ILinkNode* findConnectingNode( Pred pred ) const;
252 
256  agx::Bool getEnable() const;
257 
261  void debugRenderConnections() const;
262 
274  WinchState findWinchState() const;
275 
282  agxWire::Winch* findPulledInWinch() const;
283 
285  public:
289  void initialize( agxSDK::SimulationProxy* simulationProxy );
290 
296  void setEnable( agx::Bool enable, agxSDK::SimulationProxy* simulationProxy );
297 
301  void onRemoveNotification( agxWire::Wire* wire, agxWire::ConnectingNode* connectingNode );
302 
304 
306 
307  protected:
311  enum State
312  {
313  NONE = 0,
314  INITIALIZED = 1 << 0,
315  ENABLE_TOGGLED = 1 << 1
316  };
317 
319 
320  protected:
324  Link();
325 
329  virtual ~Link();
330 
334  void collectGarbage();
335 
339  void addConnection( agxWire::Link::ConnectionsContainer& connections, agxWire::ILinkNode* node );
340 
344  void eraseConnection( agxWire::Link::ConnectionsContainer& connections, agxWire::ILinkNode* node );
345 
349  void addState( agxWire::Link::State state );
350 
354  void removeState( agxWire::Link::State state );
355 
359  agx::Bool hasState( agxWire::Link::State state ) const;
360 
361  protected:
362  friend class LinkController;
363 
368  virtual agx::Bool executeUpdate( const agxSDK::SimulationProxy* simulationProxy );
369 
373  virtual void update( agxWire::WireParallelCallbacksHandler::CallbackTypes type, agxSDK::SimulationProxy* simulationProxy );
374 
375  protected:
377  AlgorithmContainer m_algorithms;
378  AlgorithmContainer m_newlyAddedAlgorithms;
382 
383  private:
384  ConnectionsContainer m_connections;
385  };
386 
391  class AGXPHYSICS_EXPORT Link::Algorithm : public agx::Referenced, public agxStream::Serializable
392  {
393  public:
400  virtual void onLinkEnable(agx::Bool enable, agxWire::Link* link, agxSDK::SimulationProxy* simulationProxy);
401 
407  virtual void preCollide(agxWire::Link* link, agxSDK::SimulationProxy* simulationProxy);
408 
414  virtual void pre(agxWire::Link* link, agxSDK::SimulationProxy* simulationProxy);
415 
421  virtual void post(agxWire::Link* link, agxSDK::SimulationProxy* simulationProxy);
422 
427  agx::Bool addChild( agxWire::Link::Algorithm* algorithm );
428 
433  agx::Bool removeChild( agxWire::Link::Algorithm* algorithm );
434 
438  agx::UInt getNumChildren() const;
439 
443  agxWire::Link::Algorithm* getChild( agx::UInt i ) const;
444 
448  agxWire::Link::Algorithm* getParent() const;
449 
454  void preChildrenUpdate();
455 
460  void postChildrenUpdate();
461 
465  template< typename T >
466  T* find() const;
467 
471  template< typename T >
472  const T* as() const;
473 
477  template< typename T >
478  T* as();
479 
481  public:
485  virtual void store( agxStream::OutputArchive& out ) const override;
486 
490  virtual void restore( agxStream::InputArchive& in ) override;
491 
492  AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE( Link::Algorithm );
493 
495 
496  protected:
500  Algorithm();
501 
502  private:
503  enum State { IDLE = 0, UPDATING_CHILDREN = (1<<0) };
504 
505  private:
507 
508  private:
509  ChildrenContainer m_children;
510  ChildrenContainer m_removedChildren;
511  Link::Algorithm* m_parent;
512  agx::Int32 m_state;
513  };
514 
516 
517  inline void Link::Algorithm::onLinkEnable(agx::Bool /*enable*/, agxWire::Link* /*link*/, agxSDK::SimulationProxy* /*simulationProxy*/) {}
518 
519  inline void Link::Algorithm::preCollide(agxWire::Link* /*link*/, agxSDK::SimulationProxy* /*simulationProxy*/) {}
520 
521  inline void Link::Algorithm::pre(agxWire::Link* /*link*/, agxSDK::SimulationProxy* /*simulationProxy*/) {}
522 
523  inline void Link::Algorithm::post(agxWire::Link* /*link*/, agxSDK::SimulationProxy* /*simulationProxy*/) {}
524 
525  template<typename T>
526  T* Link::getAlgorithm() const
527  {
528  T* algorithm = nullptr;
529  for ( agx::UInt i = 0, numAlgorithms = m_algorithms.size(); algorithm == nullptr && i < numAlgorithms; ++i )
530  algorithm = m_algorithms[ i ]->find<T>();
531 
532  return algorithm;
533  }
534 
536 
537  template<typename Pred>
538  Link::ConnectionsContainer Link::getConnectingNodes( Pred pred ) const
539  {
541  ret.reserve( m_connections.size() );
542  for ( agx::UInt i = 0, numConnections = m_connections.size(); i < numConnections; ++i )
543  if ( pred( m_connections[ i ] ) )
544  ret.push_back( m_connections[ i ] );
545  return ret;
546  }
547 
548  template<typename Pred>
549  ILinkNode* Link::findConnectingNode( Pred pred ) const
550  {
551  for ( agx::UInt i = 0, numConnections = m_connections.size(); i < numConnections; ++i )
552  if ( pred( m_connections[ i ] ) )
553  return m_connections[ i ];
554  return nullptr;
555  }
556 
557  template<typename T>
558  T* Link::Algorithm::find() const
559  {
560  T* algorithm = const_cast<Link::Algorithm*>( this )->as<T>();
561  for ( agx::UInt i = 0; algorithm == nullptr && i < m_children.size(); ++i )
562  algorithm = m_children[ i ]->find<T>();
563 
564  return algorithm;
565  }
566 
567  template<typename T>
568  const T* Link::Algorithm::as() const
569  {
570  return dynamic_cast<const T*>( this );
571  }
572 
573  template<typename T>
574  T* Link::Algorithm::as()
575  {
576  return dynamic_cast<T*>( this );
577  }
578 }
579 
580 
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
Winch object which works like agxWire::WireWinchController but enables the features of having stored...
Definition: Winch.h:30
Connecting nodes contains two nodes, one at center of mass and the other at the user defined position...
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...
int32_t Int32
Definition: Integer.h:45
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 reserve(size_t size)
Reserve capacity in the vector.
Definition: Vector.h:583
Base class providing referencing counted objects.
Definition: Referenced.h:119
Interface class for nodes connected to links.
Definition: ILinkNode.h:28
Interface and placeholder of controllers/helpers for wires.
Definition: Wire.h:61
uint32_t UInt
Definition: Integer.h:34
Implements a Wire model with adaptive resolution.
Definition: MergedBody.h:39
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
#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
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
Object holding parameters, such as bend and twist stiffness, related to wire to link connections...
Definition: ILinkNode.h:35
#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
Interface class used by internal objects running in parallel.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
void push_back(const T2 &value)
Definition: Vector.h:715
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97