AGXDynamics  2.31.1.0
agxUtil.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 
56 namespace agxUtil {
57 
66 
74  AGXPHYSICS_EXPORT bool setEnableCollisions( agx::RigidBody *body, const agxCollide::Geometry *geom, bool enable );
75 
83  AGXPHYSICS_EXPORT bool setEnableCollisions( const agxCollide::Geometry *geom, agx::RigidBody *body, bool enable );
84 
94 
103  AGXPHYSICS_EXPORT bool setEnableCollisions( agxSDK::Assembly *assembly, agx::RigidBody *body, bool enable );
104 
113  AGXPHYSICS_EXPORT bool setEnableCollisions( agx::RigidBody *body, agxSDK::Assembly *assembly, bool enable );
114 
123  AGXPHYSICS_EXPORT bool setEnableCollisions( agxSDK::Assembly *assembly, agxCollide::Geometry *geo, bool enable );
124 
133  AGXPHYSICS_EXPORT bool setEnableCollisions( agxCollide::Geometry *geo, agxSDK::Assembly *assembly, bool enable );
134 
141  AGXPHYSICS_EXPORT bool setEnableGeometries( agx::RigidBody *body, bool enable );
142 
149  AGXPHYSICS_EXPORT bool setEnableCollisions(agx::RigidBody* body, bool enable);
150 
151 
162 
171 
172 
182 
189  AGXPHYSICS_EXPORT bool extractConstraints( agx::ConstraintPtrVector& vec, agx::DynamicsSystem *system );
190 
198 
206 
213 
220  AGXPHYSICS_EXPORT void addParentVelocity( const agx::RigidBody *parentBody, agx::RigidBody *body );
221 
227  AGXPHYSICS_EXPORT void addGroup( const agx::RigidBody *body, unsigned id );
228 
234  AGXPHYSICS_EXPORT void removeGroup( const agx::RigidBody *body, unsigned id );
235 
243 
251  AGXPHYSICS_EXPORT size_t getMeshData( const agxCollide::Mesh* mesh, agx::Vec3Vector& vertices, agx::UInt32Vector& indices );
252 
258 
264 
272  AGXPHYSICS_EXPORT agx::Vec3 transformVectorFromTo( const agx::Vec3 vec, const agx::RigidBody* fromBody, const agx::RigidBody* toBody );
273 
281  AGXPHYSICS_EXPORT agx::Vec3 transformPointFromTo( const agx::Vec3 point, const agx::RigidBody* fromBody, const agx::RigidBody* toBody );
282 
287  {
289  {
290  DETACH_CONTACT_NODES = 1 << 0,
291  DETACH_EYE_NODES = 1 << 1
292  };
293  };
294 
306  AGXPHYSICS_EXPORT size_t jumpRequest( agxSDK::Assembly* collection,
307  agx::RigidBody* parentBody,
308  const agx::AffineMatrix4x4& parentBodyWorldTransform,
309  agx::UInt32 wireOptions = agx::UInt32( 0 ) );
310 
311 
315  template< typename T >
317  {
318  agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 < o2; }
319  };
320 
324  template< typename T >
326  {
327  agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 > o2; }
328  };
329 
335  template< typename ContainerT, typename Pred >
336  void insertionSort( ContainerT& container, Pred pred )
337  {
338  int size = (int)container.size();
339  if ( size < 2 )
340  return;
341 
342  for ( int i = 1; i < size; ++i ) {
343  typename ContainerT::value_type tmp = container[ i ];
344  int j = 0;
345  for ( j = i; j > 0; --j )
346  if ( pred( tmp, container[ j - 1 ] ) )
347  container[ j ] = container[ j - 1 ];
348  else break;
349  container[ j ] = tmp;
350  }
351  }
352 
357  template< typename ContainerT >
358  void insertionSort( ContainerT& container )
359  {
361  }
362 
367  template< typename ContainerT >
368  agx::Bool isSorted( const ContainerT& container )
369  {
370  if ( container.size() < 2 )
371  return true;
372 
373  for ( size_t i = 0; i < container.size() - 1; ++i )
374  if ( container[ i + 1 ] < container[ i ] )
375  return false;
376  return true;
377  }
378 
385  {
386  public:
388 
395  bool add( agx::ConstraintImplementation* constraint );
396 
402  bool remove( agx::ConstraintImplementation* constraint );
403 
407  void clear();
408 
409  protected:
410  virtual ~ConstraintHolder();
411 
412  virtual int getNumDOF() const override
413  {
414  return 0;
415  }
416  virtual void render( class agxRender::RenderManager* , float /*scale = 1.0f */ ) const override
417  {}
418 
420 
421  private:
422  class ConstraintHolderImplementation* m_implementation;
423  };
424 
426 
444  template< typename T >
446  {
447  public:
449  : agxSDK::StepEventListener( mask ), m_obj( obj ) {}
450 
451  protected:
452  virtual ~GeneralStepListener() {}
453 
455 #define callback_implementation( callback_name ) \
456  virtual void callback_name( const agx::TimeStamp& t ) \
457  { \
458  if ( !m_obj.isValid() ) { \
459  getSimulation()->remove( this ); \
460  return; \
461  } \
462  m_obj->callback_name( t ); \
463  }
464 
465  callback_implementation( preCollide )
468 #undef callback_implementation
470 
471  protected:
473  };
474 
492  template< typename T >
494  {
495  public:
497  : agxSDK::ContactEventListener( mask ), m_obj( obj ) {}
498 
499  protected:
501 
502 #define callback_implementation( callback_name, ArgT ) \
503  virtual agxSDK::ContactEventListener::KeepContactPolicy callback_name( const agx::TimeStamp& t, ArgT obj ) \
504  { \
505  if ( !m_obj.isValid() ) { \
506  getSimulation()->remove( this ); \
507  return agxSDK::ContactEventListener::KEEP_CONTACT; \
508  } \
509  return m_obj->callback_name( t, obj ); \
510  }
511 
514 #undef callback_implementation
515 
516  virtual void separation( const agx::TimeStamp& t, agxCollide::GeometryPair& gp )
517  {
518  if ( !m_obj.isValid() ) {
519  getSimulation()->remove( this );
520  return;
521  }
522 
523  m_obj->separation( t, gp );
524  }
525 
526  protected:
528  };
529 
530 
531 
533  {
534  public:
535  static SceneRoot* object();
536 
537  virtual bool createVisual( agx::RigidBody* /*rb*/, float /*detailRation*/ = 0.30f ) { return false; };
538  virtual bool createVisual( agxCollide::Geometry* /*geometry*/, float /*detailRation*/ = 0.30f ) { return false; }
539 
540  static void setInstance( SceneRoot* baseInstance );
541 
542  SINGLETON_CLASSNAME_METHOD();
543 
544  protected:
545  virtual ~SceneRoot() {}
546 
547  protected:
549  };
550 
551 
552 
553 }
554 
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:384
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:68
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:445
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:548
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:63
virtual bool createVisual(agx::RigidBody *, float=0.30f)
Definition: agxUtil.h:537
agxCollide namespace consists of a set of classes for handling geometric intersection tests including...
Less than operator to e.g., sort functions.
Definition: agxUtil.h:316
GeneralContactEventListener(T *obj, int mask=agxSDK::ContactEventListener::DEFAULT)
Definition: agxUtil.h:496
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:516
virtual ~SceneRoot()
Definition: agxUtil.h:545
GeneralStepListener(T *obj, int mask=agxSDK::StepEventListener::DEFAULT)
Definition: agxUtil.h:448
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:86
#define callback_implementation(callback_name)
Definition: agxUtil.h:502
agx::Bool isSorted(const ContainerT &container)
std::valarray friendly is sorted function.
Definition: agxUtil.h:368
agx::ref_ptr< ConstraintHolder > ConstraintHolderRef
Definition: agxUtil.h:425
agx::Real TimeStamp
Definition: TimeStamp.h:26
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:416
Jump request options for wire nodes which parent isn&#39;t the given assembly/collection.
Definition: agxUtil.h:286
The geometry representation used by the collision detection engine.
Definition: Geometry.h:88
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:452
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
Namespace agx 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.
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:538
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:52
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:384
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.
agx::VectorPOD< RigidBodyPtr > RigidBodyPtrVector
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:358
#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:206
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:472
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:63
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:527
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:325
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:412
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:102
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:101
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:73
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:493