AGXDynamics  2.35.0.2
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 
79  AGXPHYSICS_EXPORT bool setEnableCollisions( agx::RigidBody *body, const agxCollide::Geometry *geom, bool enable );
80 
88  AGXPHYSICS_EXPORT bool setEnableCollisions( const agxCollide::Geometry *geom, agx::RigidBody *body, bool enable );
89 
99 
108  AGXPHYSICS_EXPORT bool setEnableCollisions( agxSDK::Assembly *assembly, agx::RigidBody *body, bool enable );
109 
118  AGXPHYSICS_EXPORT bool setEnableCollisions( agx::RigidBody *body, agxSDK::Assembly *assembly, bool enable );
119 
128  AGXPHYSICS_EXPORT bool setEnableCollisions( agxSDK::Assembly *assembly, agxCollide::Geometry *geo, bool enable );
129 
138  AGXPHYSICS_EXPORT bool setEnableCollisions( agxCollide::Geometry *geo, agxSDK::Assembly *assembly, bool enable );
139 
146  AGXPHYSICS_EXPORT bool setEnableGeometries( agx::RigidBody *body, bool enable );
147 
154  AGXPHYSICS_EXPORT bool setEnableCollisions(agx::RigidBody* body, bool enable);
155 
156 
167 
176 
177 
187 
195 
203 
211 
218 
225  AGXPHYSICS_EXPORT void addParentVelocity( const agx::RigidBody *parentBody, agx::RigidBody *body );
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 
271  AGXPHYSICS_EXPORT size_t getMeshData( const agxCollide::Mesh* mesh, agx::Vec3Vector& vertices, agx::UInt32Vector& indices );
272 
284  AGXPHYSICS_EXPORT bool isConvexMesh( const agxCollide::Mesh* mesh, agx::Real threshold=1E-5 );
285 
291 
297 
305  AGXPHYSICS_EXPORT agx::Vec3 transformVectorFromTo( const agx::Vec3 vec, const agx::RigidBody* fromBody, const agx::RigidBody* toBody );
306 
314  AGXPHYSICS_EXPORT agx::Vec3 transformPointFromTo( const agx::Vec3 point, const agx::RigidBody* fromBody, const agx::RigidBody* toBody );
315 
320  {
322  {
325  };
326  };
327 
339  AGXPHYSICS_EXPORT size_t jumpRequest( agxSDK::Assembly* collection,
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 
481  bool remove( agx::ConstraintImplementation* constraint );
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 
agxPhysics_export.h
agxUtil::setEnableGeometries
AGXPHYSICS_EXPORT bool setEnableGeometries(agx::RigidBody *body, bool enable)
Enable or disable all geometries in a RigidBody.
agxUtil::LargerThanPred
Larger than operator to e.g., sort functions.
Definition: agxUtil.h:358
agxcollide_vector_types.h
agxUtil::LargerThanPred::operator()
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:360
agxSDK::StepEventListener::pre
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...
Definition: StepEventListener.h:106
agxUtil::GeneralContactEventListener::separation
agxCollide::GeometryContact *virtual void separation(const agx::TimeStamp &t, agxCollide::GeometryPair &gp)
Called upon separation event if getFilter() contain SEPARATION.
Definition: agxUtil.h:595
agxCollide::Mesh
Mesh is a common base class for triangle meshes, such as Mesh or HeightField.
Definition: Mesh.h:69
agx::Real
double Real
Definition: Real.h:42
agxSDK::StepEventListener
Derive from this class to implement a listener for simulation step events.
Definition: StepEventListener.h:37
agxData::GeometryPair
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
agxUtil::GeneralStepListener
Utility class to get step event callbacks to any class having the methods implemented.
Definition: agxUtil.h:524
agxUtil::calculateCylinderTransform
AGXPHYSICS_EXPORT agx::AffineMatrix4x4 calculateCylinderTransform(const agx::Vec3 &startPoint, const agx::Vec3 &endPoint)
Calculates transform that can be used for cylinders/capsules like geometries.
agx::Vector
Templated vector class.
Definition: Vector.h:52
agx::RigidBody::MotionControl
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:65
agxControl
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
agxSDK::StepEventListener::StepEventListener
StepEventListener(int mask=DEFAULT)
Default constructor, sets the default activation mask to all (POST_STEP and PRE_STEP) events.
agxUtil::ConstraintHolderRef
agx::ref_ptr< ConstraintHolder > ConstraintHolderRef
Definition: agxUtil.h:504
Simulation.h
agx::RigidBody
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
agxUtil::isSorted
agx::Bool isSorted(const ContainerT &container)
std::valarray friendly is sorted function.
Definition: agxUtil.h:401
agxRender::RenderManager
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc.
Definition: RenderManager.h:106
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
Constraint.h
agxUtil::ConstraintHolder
Utility class to hold custom implementations of constraints.
Definition: agxUtil.h:463
agx::AffineMatrix4x4T< Real >
agx::RealPair
std::pair< Real, Real > RealPair
Definition: ConstraintImplementation.h:68
agxSDK::StepEventListener::post
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...
Definition: StepEventListener.h:107
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxUtil::JumpRequestWireOption::DETACH_CONTACT_NODES
@ DETACH_CONTACT_NODES
Contact nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:323
agx::ConstraintImplementation
Definition: ConstraintImplementation.h:100
agxUtil::SceneRoot::createVisual
virtual bool createVisual(agxCollide::Geometry *, float=0.30f)
Definition: agxUtil.h:617
agxSDK::ContactEventListener::DEFAULT
@ DEFAULT
The default activation mask.
Definition: ContactEventListener.h:54
agxUtil::addGroup
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
StepEventListener.h
agxUtil::createSinkOnGeometry
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...
AGXSTREAM_DECLARE_SERIALIZABLE
#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
agx::VectorPOD
Vector containing 'raw' data.
Definition: Vector.h:245
agxUtil::GeneralContactEventListener
Utility class to get contact event callbacks to any class having the methods implemented.
Definition: agxUtil.h:572
agx::observer_ptr
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:60
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
agxUtil::JumpRequestWireOption
Jump request options for wire nodes which parent isn't the given assembly/collection.
Definition: agxUtil.h:319
agxUtil::getContactMaterialVector
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.
AffineMatrix4x4.h
agxUtil::getMeshData
AGXPHYSICS_EXPORT size_t getMeshData(const agxCollide::Mesh *mesh, agx::Vec3Vector &vertices, agx::UInt32Vector &indices)
Extract vertices and indices from mesh.
agxControl::EventSensor
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:47
agxUtil::SceneRoot::createVisual
virtual bool createVisual(agx::RigidBody *, float=0.30f)
Definition: agxUtil.h:616
agxUtil::GeneralContactEventListener::m_obj
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:606
agxSDK::Assembly
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:66
callback_implementation
#define callback_implementation(callback_name)
Definition: agxUtil.h:581
agxUtil::SceneRoot::~SceneRoot
virtual ~SceneRoot()
Definition: agxUtil.h:624
agxUtil::isConvexMesh
AGXPHYSICS_EXPORT bool isConvexMesh(const agxCollide::Mesh *mesh, agx::Real threshold=1E-5)
Algorithm for determining if a mesh is convex.
agx::ref_ptr< ConstraintHolder >
agxSDK::EventListener::getSimulation
Simulation * getSimulation()
agxUtil::ConstraintHolder::render
virtual void render(class agxRender::RenderManager *, float) const override
Inherited method of how to render this constraint into DebugRenderer.
Definition: agxUtil.h:495
agxUtil::insertionSort
void insertionSort(ContainerT &container, Pred pred)
Insertion sort, supports std::valarray.
Definition: agxUtil.h:369
agx::Singleton
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:30
agxUtil::copyContainerMemory
ContainerT copyContainerMemory(const ContainerT &container)
Returns a copy of the passed container.
agxUtil::setMotionControl
AGXPHYSICS_EXPORT void setMotionControl(agx::RigidBodyPtrVector &bodies, agx::RigidBody::MotionControl motionControl)
Utility function to set motion control for a vector of rigid bodies.
agxSDK::MaterialManager
Simulation unique manager that handles logics around and parameters in contact materials given two ag...
Definition: MaterialManager.h:49
agxUtil::setEnableCollisions
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.
Convex.h
agxUtil::extractRigidBodies
AGXPHYSICS_EXPORT bool extractRigidBodies(agx::RigidBodyPtrVector &bodies, agxSDK::Assembly *assembly)
Utility function to extract all Rigid Bodies from an assembly (and its sub-assemblies).
agxCollide::Geometry
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
agxUtil::addParentVelocity
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.
agxUtil::setBodyMaterial
AGXPHYSICS_EXPORT bool setBodyMaterial(agx::RigidBody *body, agx::Material *material)
Utility function to loop through a body and set the material for associated geometry.
agxUtil::extractGeometries
AGXPHYSICS_EXPORT bool extractGeometries(agxCollide::GeometryPtrVector &geometries, agxSDK::Assembly *assembly)
Utility function to extract all geometries from an assembly (and its sub-assemblies),...
agxUtil::LessThanPred
Less than operator to e.g., sort functions.
Definition: agxUtil.h:349
agxCollide::Space
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
agxUtil::GeneralContactEventListener::~GeneralContactEventListener
virtual ~GeneralContactEventListener()
Definition: agxUtil.h:579
agxUtil::ConstraintHolder::getNumDOF
virtual int getNumDOF() const override
Definition: agxUtil.h:491
agx.h
agxUtil::GeneralContactEventListener::GeneralContactEventListener
GeneralContactEventListener(T *obj, int mask=agxSDK::ContactEventListener::DEFAULT)
Definition: agxUtil.h:575
agxUtil::GeneralStepListener::GeneralStepListener
GeneralStepListener(T *obj, int mask=agxSDK::StepEventListener::DEFAULT)
Definition: agxUtil.h:527
agxUtil::removeGroup
AGXPHYSICS_EXPORT void removeGroup(const agx::RigidBody *body, unsigned id)
For the specified body, remove the collision group ID to its geometries.
ContactEventListener.h
agxCollide
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Definition: ContactForceFilterParticipant.h:22
agx::Bool
bool Bool
Definition: Integer.h:48
agxUtil::jumpRequest
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...
agxCollide::GeometryContact
A contact between two geometries.
Definition: Contacts.h:269
agx::TimeStamp
agx::Real TimeStamp
Definition: TimeStamp.h:26
Singleton.h
agx::Constraint
The base class for a constraint.
Definition: Constraint.h:88
agxUtil::freeContainerMemory
void freeContainerMemory(ContainerT &container)
Free the memory held by the given container by swapping it with a default-constructed instance.
agxUtil::transformVectorFromTo
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.
agxUtil::JumpRequestWireOption::DETACH_EYE_NODES
@ DETACH_EYE_NODES
Eye nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:324
agxSDK::Simulation::remove
bool remove(agx::ContactMaterial *material)
Remove an explicit contact material from the set of existing ContactMaterials.
agxUtil::getMaterialVector
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.
agxSDK
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
agxUtil::JumpRequestWireOption::Option
Option
Definition: agxUtil.h:321
agx::Material
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:387
agx::Vec3T< Real >
agxUtil::SceneRoot
Definition: agxUtil.h:611
agxUtil::extractConstraints
AGXPHYSICS_EXPORT bool extractConstraints(agx::ConstraintPtrVector &vec, agx::DynamicsSystem *system)
Utility function to extract all constraints from the system.
agxUtil::LessThanPred::operator()
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:351
agxUtil::GeneralStepListener::~GeneralStepListener
virtual ~GeneralStepListener()
Definition: agxUtil.h:531
agxUtil::transformPointFromTo
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.
agxTerrain::Profiler::clear
AGXTERRAIN_EXPORT void clear()
agx::DynamicsSystem
The complete physical system with bodies, interactions, data layout, time stepper,...
Definition: DynamicsSystem.h:65
agx::UInt32
uint32_t UInt32
Definition: Integer.h:40
agxSDK::StepEventListener::DEFAULT
@ DEFAULT
The default activation mask.
Definition: StepEventListener.h:48
agxUtil::SceneRoot::s_baseInstance
static SceneRoot * s_baseInstance
Definition: agxUtil.h:627
agxUtil
The agxUtil namespace contain classes and methods for utility functionality.
Definition: CylindricalJoint.h:22
agx_vector_types.h
agxUtil::GeneralStepListener::m_obj
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:551
agxSDK::StepEventListener::preCollide
virtual void preCollide(const agx::TimeStamp &time)
Called before collision detection is performed in the simulation Implement this method in the derived...
Definition: StepEventListener.h:105