AGX Dynamics  2.35.0.1
agxUtil.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 #pragma once
17 
18 
19 #include <agx/agx.h>
20 #include <agx/agxPhysics_export.h>
21 #include <agx/Singleton.h>
23 #include <agx/agx_vector_types.h>
24 #include <agx/AffineMatrix4x4.h>
25 #include <agx/Constraint.h>
26 
27 #include <agxCollide/Convex.h>
28 
31 #include <agxSDK/Simulation.h>
32 
33 
34 namespace agx
35 {
36  typedef std::pair< Real, Real > RealPair;
37  class RigidBody;
38  class Material;
39  class ConstraintImplementation;
40 
41 }
42 
43 namespace agxSDK {
44  class Assembly;
45 }
46 
47 namespace agxCollide {
48  class Mesh;
49 }
50 
51 namespace agxControl
52 {
53  class EventSensor;
54 }
55 
56 
61 namespace agxUtil {
62 
71 
80 
89 
99 
109 
119 
129 
139 
147 
155 
156 
167 
176 
177 
187 
195 
203 
211 
218 
226 
232  AGXPHYSICS_EXPORT void addGroup( const agx::RigidBody *body, unsigned id );
233 
239  AGXPHYSICS_EXPORT void removeGroup( const agx::RigidBody *body, unsigned id );
240 
248 
249 
262  bool onlyIncludeEmittedBodies);
263 
272 
284  AGXPHYSICS_EXPORT bool isConvexMesh( const agxCollide::Mesh* mesh, agx::Real threshold=1E-5 );
285 
291 
297 
306 
315 
320  {
322  {
324  DETACH_EYE_NODES = 1 << 1
325  };
326  };
327 
340  agx::RigidBody* parentBody,
341  const agx::AffineMatrix4x4& parentBodyWorldTransform,
342  agx::UInt32 wireOptions = agx::UInt32( 0 ) );
343 
344 
348  template< typename T >
350  {
351  agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 < o2; }
352  };
353 
357  template< typename T >
359  {
360  agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 > o2; }
361  };
362 
368  template< typename ContainerT, typename Pred >
369  void insertionSort( ContainerT& container, Pred pred )
370  {
371  int size = (int)container.size();
372  if ( size < 2 )
373  return;
374 
375  for ( int i = 1; i < size; ++i ) {
376  typename ContainerT::value_type tmp = container[ i ];
377  int j = 0;
378  for ( j = i; j > 0; --j )
379  if ( pred( tmp, container[ j - 1 ] ) )
380  container[ j ] = container[ j - 1 ];
381  else break;
382  container[ j ] = tmp;
383  }
384  }
385 
390  template< typename ContainerT >
391  void insertionSort( ContainerT& container )
392  {
394  }
395 
400  template< typename ContainerT >
401  agx::Bool isSorted( const ContainerT& container )
402  {
403  if ( container.size() < 2 )
404  return true;
405 
406  for ( size_t i = 0; i < container.size() - 1; ++i )
407  if ( container[ i + 1 ] < container[ i ] )
408  return false;
409  return true;
410  }
411 
412 
427  template<typename ContainerT>
428  void freeContainerMemory(ContainerT& container);
429 
455  template<typename ContainerT>
456  ContainerT copyContainerMemory(const ContainerT& container);
457 
464  {
465  public:
467 
474  bool add( agx::ConstraintImplementation* constraint );
475 
482 
486  void clear();
487 
488  protected:
489  virtual ~ConstraintHolder();
490 
491  virtual int getNumDOF() const override
492  {
493  return 0;
494  }
495  virtual void render( class agxRender::RenderManager* , float /*scale = 1.0f */ ) const override
496  {}
497 
499 
500  private:
501  class ConstraintHolderImplementation* m_implementation;
502  };
503 
505 
523  template< typename T >
525  {
526  public:
528  : agxSDK::StepEventListener( mask ), m_obj( obj ) {}
529 
530  protected:
531  virtual ~GeneralStepListener() {}
532 
534 #define callback_implementation( callback_name ) \
535  virtual void callback_name( const agx::TimeStamp& t ) \
536  { \
537  if ( !m_obj.isValid() ) { \
538  getSimulation()->remove( this ); \
539  return; \
540  } \
541  m_obj->callback_name( t ); \
542  }
543 
547 #undef callback_implementation
549 
550  protected:
551  agx::observer_ptr< T > m_obj;
552  };
553 
571  template< typename T >
572  class GeneralContactEventListener : public agxSDK::ContactEventListener
573  {
574  public:
576  : agxSDK::ContactEventListener( mask ), m_obj( obj ) {}
577 
578  protected:
580 
581 #define callback_implementation( callback_name, ArgT ) \
582  virtual agxSDK::ContactEventListener::KeepContactPolicy callback_name( const agx::TimeStamp& t, ArgT obj ) \
583  { \
584  if ( !m_obj.isValid() ) { \
585  getSimulation()->remove( this ); \
586  return agxSDK::ContactEventListener::KEEP_CONTACT; \
587  } \
588  return m_obj->callback_name( t, obj ); \
589  }
590 
593 #undef callback_implementation
594 
595  virtual void separation( const agx::TimeStamp& t, agxCollide::GeometryPair& gp )
596  {
597  if ( !m_obj.isValid() ) {
598  getSimulation()->remove( this );
599  return;
600  }
601 
602  m_obj->separation( t, gp );
603  }
604 
605  protected:
607  };
608 
609 
610 
612  {
613  public:
614  static SceneRoot* object();
615 
616  virtual bool createVisual( agx::RigidBody* /*rb*/, float /*detailRation*/ = 0.30f ) { return false; };
617  virtual bool createVisual( agxCollide::Geometry* /*geometry*/, float /*detailRation*/ = 0.30f ) { return false; }
618 
619  static void setInstance( SceneRoot* baseInstance );
620 
621  SINGLETON_CLASSNAME_METHOD();
622 
623  protected:
624  virtual ~SceneRoot() {}
625 
626  protected:
628  };
629 
630 
631 
632 }
633 
#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
#define callback_implementation(callback_name)
Definition: agxUtil.h:581
A contact between two geometries.
Definition: Contacts.h:270
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
Mesh is a common base class for triangle meshes, such as Mesh or HeightField.
Definition: Mesh.h:70
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:82
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:48
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc.
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:67
@ DEFAULT
The default activation mask.
Simulation * getSimulation()
Simulation unique manager that handles logics around and parameters in contact materials given two ag...
bool remove(agx::ContactMaterial *material)
Remove an explicit contact material from the set of existing ContactMaterials.
Derive from this class to implement a listener for simulation step events.
virtual void preCollide(const agx::TimeStamp &time)
Called before collision detection is performed in the simulation Implement this method in the derived...
virtual void pre(const agx::TimeStamp &time)
Called before a step is taken in the simulation Implement this method in the derived class to get cal...
virtual void post(const agx::TimeStamp &time)
Called after a step is taken in the simulation Implement this method in the derived class to get call...
@ DEFAULT
The default activation mask.
StepEventListener(int mask=DEFAULT)
Default constructor, sets the default activation mask to all (POST_STEP and PRE_STEP) events.
Utility class to hold custom implementations of constraints.
Definition: agxUtil.h:464
bool add(agx::ConstraintImplementation *constraint)
Add 'interface less' constraint implementation.
bool remove(agx::ConstraintImplementation *constraint)
Remove constraint implementation.
void clear()
Remove all constraints.
virtual int getNumDOF() const override
Definition: agxUtil.h:491
virtual void render(class agxRender::RenderManager *, float) const override
Inherited method of how to render this constraint into DebugRenderer.
Definition: agxUtil.h:495
Utility class to get contact event callbacks to any class having the methods implemented.
Definition: agxUtil.h:573
GeneralContactEventListener(T *obj, int mask=agxSDK::ContactEventListener::DEFAULT)
Definition: agxUtil.h:575
agxCollide::GeometryContact *virtual void separation(const agx::TimeStamp &t, agxCollide::GeometryPair &gp)
Called upon separation event if getFilter() contain SEPARATION.
Definition: agxUtil.h:595
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:606
callback_implementation(impact, agxCollide::GeometryContact *) callback_implementation(contact
Utility class to get step event callbacks to any class having the methods implemented.
Definition: agxUtil.h:525
GeneralStepListener(T *obj, int mask=agxSDK::StepEventListener::DEFAULT)
Definition: agxUtil.h:527
virtual ~GeneralStepListener()
Definition: agxUtil.h:531
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:551
static void setInstance(SceneRoot *baseInstance)
virtual bool createVisual(agx::RigidBody *, float=0.30f)
Definition: agxUtil.h:616
virtual bool createVisual(agxCollide::Geometry *, float=0.30f)
Definition: agxUtil.h:617
virtual ~SceneRoot()
Definition: agxUtil.h:624
static SceneRoot * s_baseInstance
Definition: agxUtil.h:627
static SceneRoot * object()
The base class for a constraint.
Definition: Constraint.h:89
The complete physical system with bodies, interactions, data layout, time stepper,...
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:388
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:66
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:31
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Templated vector class.
Definition: agx/Vector.h:53
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:61
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
This namespace consists of a set of classes for handling geometric intersection tests including boole...
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
The agxUtil namespace contain classes and methods for utility functionality.
AGXPHYSICS_EXPORT void addParentVelocity(const agx::RigidBody *parentBody, agx::RigidBody *body)
body will get the parents velocity added to its velocity as if it was rigidly attached.
AGXPHYSICS_EXPORT bool setEnableGeometries(agx::RigidBody *body, bool enable)
Enable or disable all geometries in a RigidBody.
AGXPHYSICS_EXPORT agx::Vec3 transformPointFromTo(const agx::Vec3 point, const agx::RigidBody *fromBody, const agx::RigidBody *toBody)
Point transform from rigid body fromBody to rigid body toBody.
AGXPHYSICS_EXPORT agxControl::EventSensor * createSinkOnGeometry(agxCollide::Geometry *geometry, bool onlyIncludeEmittedBodies)
Utillity function for creating a "sink" on a geometry that will remove rigid bodies and particles tha...
agx::Bool isSorted(const ContainerT &container)
std::valarray friendly is sorted function.
Definition: agxUtil.h:401
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
AGXPHYSICS_EXPORT void setMotionControl(agx::RigidBodyPtrVector &bodies, agx::RigidBody::MotionControl motionControl)
Utility function to set motion control for a vector of rigid bodies.
AGXPHYSICS_EXPORT agx::Vec3 transformVectorFromTo(const agx::Vec3 vec, const agx::RigidBody *fromBody, const agx::RigidBody *toBody)
Vector transform from rigid body fromBody to rigid body toBody.
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.
void insertionSort(ContainerT &container, Pred pred)
Insertion sort, supports std::valarray.
Definition: agxUtil.h:369
AGXPHYSICS_EXPORT bool extractConstraints(agx::ConstraintPtrVector &vec, agx::DynamicsSystem *system)
Utility function to extract all constraints from the system.
agx::ref_ptr< ConstraintHolder > ConstraintHolderRef
Definition: agxUtil.h:504
AGXPHYSICS_EXPORT bool extractGeometries(agxCollide::GeometryPtrVector &geometries, agxSDK::Assembly *assembly)
Utility function to extract all geometries from an assembly (and its sub-assemblies),...
AGXPHYSICS_EXPORT size_t jumpRequest(agxSDK::Assembly *collection, agx::RigidBody *parentBody, const agx::AffineMatrix4x4 &parentBodyWorldTransform, agx::UInt32 wireOptions=agx::UInt32(0))
This method will perform a "jump request" will all bodies in the assembly, including bodies which are...
AGXPHYSICS_EXPORT bool isConvexMesh(const agxCollide::Mesh *mesh, agx::Real threshold=1E-5)
Algorithm for determining if a mesh is convex.
AGXPHYSICS_EXPORT size_t getContactMaterialVector(agxSDK::MaterialManager *mgr, agx::ContactMaterialPtrVector &contactMaterials)
Get all contact materials from the material manager and add them to the supplied vector.
ContainerT copyContainerMemory(const ContainerT &container)
Returns a copy of the passed container.
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
AGXPHYSICS_EXPORT bool setBodyMaterial(agx::RigidBody *body, agx::Material *material)
Utility function to loop through a body and set the material for associated geometry.
AGXPHYSICS_EXPORT size_t getMaterialVector(agxSDK::MaterialManager *mgr, agx::MaterialPtrVector &materials)
Get all materials from the material manager and add them to the supplied vector.
AGXPHYSICS_EXPORT bool extractRigidBodies(agx::RigidBodyPtrVector &bodies, agxSDK::Assembly *assembly)
Utility function to extract all Rigid Bodies from an assembly (and its sub-assemblies).
AGXPHYSICS_EXPORT agx::AffineMatrix4x4 calculateCylinderTransform(const agx::Vec3 &startPoint, const agx::Vec3 &endPoint)
Calculates transform that can be used for cylinders/capsules like geometries.
AGXPHYSICS_EXPORT size_t getMeshData(const agxCollide::Mesh *mesh, agx::Vec3Vector &vertices, agx::UInt32Vector &indices)
Extract vertices and indices from mesh.
void freeContainerMemory(ContainerT &container)
Free the memory held by the given container by swapping it with a default-constructed instance.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
bool Bool
Definition: Integer.h:48
double Real
Definition: Real.h:42
std::pair< Real, Real > RealPair
agx::Real TimeStamp
Definition: TimeStamp.h:26
Jump request options for wire nodes which parent isn't the given assembly/collection.
Definition: agxUtil.h:320
@ DETACH_CONTACT_NODES
Contact nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:323
@ DETACH_EYE_NODES
Eye nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:324
Larger than operator to e.g., sort functions.
Definition: agxUtil.h:359
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:360
Less than operator to e.g., sort functions.
Definition: agxUtil.h:350
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:351