AGX Dynamics  2.37.2.0
agxUtil.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 #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  {
323  DETACH_CONTACT_NODES = 1 << 0,
324  DETACH_EYE_NODES = 1 << 1
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 
544  callback_implementation( preCollide )
547 #undef callback_implementation
549 
550  protected:
552  };
553 
571  template< typename T >
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 
std::pair< Real, Real > RealPair
AGXTERRAIN_EXPORT void clear()
AGXPHYSICS_EXPORT bool setEnableGeometries(agx::RigidBody *body, bool enable)
Enable or disable all geometries in a RigidBody.
Utility class to hold custom implementations of constraints.
Definition: agxUtil.h:463
Derive from this class to implement a listener for Collision events.
Mesh is a common base class for triangle meshes, such as Mesh or HeightField.
Definition: Mesh.h:69
Derive from this class to implement a listener for simulation step events.
Utility class to get step event callbacks to any class having the methods implemented.
Definition: agxUtil.h:524
bool Bool
Definition: Integer.h:48
Smart pointer for observed objects, that automatically set pointers to them to null when they deleted...
Definition: observer_ptr.h:60
static SceneRoot * s_baseInstance
Definition: agxUtil.h:627
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
virtual bool createVisual(agx::RigidBody *, float=0.30f)
Definition: agxUtil.h:616
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Less than operator to e.g., sort functions.
Definition: agxUtil.h:349
GeneralContactEventListener(T *obj, int mask=agxSDK::ContactEventListener::DEFAULT)
Definition: agxUtil.h:575
A contact between two geometries.
Definition: Contacts.h:270
agxCollide::GeometryContact *virtual void separation(const agx::TimeStamp &t, agxCollide::GeometryPair &gp)
Called upon separation event if getFilter() contain SEPARATION.
Definition: agxUtil.h:595
virtual ~SceneRoot()
Definition: agxUtil.h:624
GeneralStepListener(T *obj, int mask=agxSDK::StepEventListener::DEFAULT)
Definition: agxUtil.h:527
AGXPHYSICS_EXPORT agx::AffineMatrix4x4 calculateCylinderTransform(const agx::Vec3 &startPoint, const agx::Vec3 &endPoint)
Calculates transform that can be used for cylinders/capsules like geometries.
#define AGXPHYSICS_EXPORT
The base class for a constraint.
Definition: Constraint.h:88
#define callback_implementation(callback_name)
Definition: agxUtil.h:581
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
agx::ref_ptr< ConstraintHolder > ConstraintHolderRef
Definition: agxUtil.h:504
AGXPHYSICS_EXPORT bool isConvexMesh(const agxCollide::Mesh *mesh, agx::Real threshold=1E-5)
Algorithm for determining if a mesh is convex.
agx::Real TimeStamp
Definition: TimeStamp.h:26
This is a sensor class that can be attached to an geometry in the simulation.
Definition: EventSensor.h:47
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc...
AGXPHYSICS_EXPORT bool extractRigidBodies(agx::RigidBodyPtrVector &bodies, agxSDK::Assembly *assembly)
Utility function to extract all Rigid Bodies from an assembly (and its sub-assemblies).
virtual void render(class agxRender::RenderManager *, float) const override
Inherited method of how to render this constraint into DebugRenderer.
Definition: agxUtil.h:495
Jump request options for wire nodes which parent isn&#39;t the given assembly/collection.
Definition: agxUtil.h:319
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
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.
virtual ~GeneralStepListener()
Definition: agxUtil.h:531
AGXPHYSICS_EXPORT void addGroup(const agx::RigidBody *body, unsigned id)
For the specified body, add the collision group ID to its geometries.
Templated vector class.
Definition: Vector.h:52
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
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.
AGXPHYSICS_EXPORT size_t getMeshData(const agxCollide::Mesh *mesh, agx::Vec3Vector &vertices, agx::UInt32Vector &indices)
Extract vertices and indices from mesh.
ContainerT copyContainerMemory(const ContainerT &container)
Returns a copy of the passed container.
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...
The default activation mask.
virtual bool createVisual(agxCollide::Geometry *, float=0.30f)
Definition: agxUtil.h:617
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
This namespace contains general operation and action classes which can be used to control a agxSDK::S...
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:375
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 void setMotionControl(agx::RigidBodyPtrVector &bodies, agx::RigidBody::MotionControl motionControl)
Utility function to set motion control for a vector of rigid bodies.
The complete physical system with bodies, interactions, data layout, time stepper, and solver.
void insertionSort(ContainerT &container)
Insertion sort, supports std::valarray.
Definition: agxUtil.h:391
#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
AGXPHYSICS_EXPORT bool setEnableCollisions(agx::RigidBody *body, bool enable)
Enable or disable collision for all geometries in a RigidBody.
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.
Simulation unique manager that handles logics around and parameters in contact materials given two ag...
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:551
void freeContainerMemory(ContainerT &container)
Free the memory held by the given container by swapping it with a default-constructed instance...
double Real
Definition: Real.h:42
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:65
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.
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:606
AGXPHYSICS_EXPORT bool extractGeometries(agxCollide::GeometryPtrVector &vec, agxCollide::Space *space)
Utility function to extract all geometries from the space into a vector.
Larger than operator to e.g., sort functions.
Definition: agxUtil.h:358
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
uint32_t UInt32
Definition: Integer.h:40
virtual int getNumDOF() const override
Definition: agxUtil.h:491
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
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.
The agxUtil namespace contain classes and methods for utility functionality.
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:66
Base class for Singletons that should have its shutdown called explicitly before exit of the applicat...
Definition: Singleton.h:30
AGXPHYSICS_EXPORT bool extractConstraints(agx::ConstraintPtrVector &vec, agx::DynamicsSystem *system)
Utility function to extract all constraints from the system.
Utility class to get contact event callbacks to any class having the methods implemented.
Definition: agxUtil.h:572