AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
agxUtil.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16#pragma once
17
18
19#include <agx/agx.h>
21#include <agx/Singleton.h>
24#include <agx/AffineMatrix4x4.h>
25#include <agx/Constraint.h>
26
29#include <agxSDK/Simulation.h>
30
31
32namespace agx
33{
34 typedef std::pair< Real, Real > RealPair;
35 class RigidBody;
36 class Material;
37 class ConstraintImplementation;
38
39}
40
41namespace agxSDK {
42 class Assembly;
43}
44
45namespace agxCollide {
46 class Mesh;
47}
48
49namespace agxControl
50{
51 class EventSensor;
52}
53
54
59namespace agxUtil {
60
69
78
87
97
107
117
127
137
145
153
154
165
174
175
185
193
201
209
216
224
225
231 template <typename T>
232 void addGroup(const agx::RigidBody* body, const T& id)
233 {
234 if (body == nullptr) {
235 LOGGER_WARNING() << "Invalid null pointer for body " << std::endl << LOGGER_END();
236 return;
237 }
238
239 for (const auto& geometry : body->getGeometries())
240 geometry->addGroup(id);
241 }
242
248 template <typename T>
249 void removeGroup(const agx::RigidBody* body, const T& id)
250 {
251 if (body == nullptr) {
252 LOGGER_WARNING() << "Invalid null pointer for body " << std::endl << LOGGER_END();
253 return;
254 }
255
256 for (const auto& geometry : body->getGeometries())
257 geometry->removeGroup(id);
258 }
259
265 template<typename T>
266 void addGroup(agxSDK::Assembly* assembly,T id)
267 {
269 agxUtil::extractGeometries(geometries, assembly);
270
271 for (auto& g: geometries)
272 g->addGroup(id);
273 }
274
280 template<typename T>
281 void removeGroup(agxSDK::Assembly* assembly, T id)
282 {
284 agxUtil::extractGeometries(geometries, assembly);
285
286 for (auto& g: geometries)
287 g->removeGroup(id);
288 }
289
297
310 bool onlyIncludeEmittedBodies);
311
320
332 AGXPHYSICS_EXPORT bool isConvexMesh( const agxCollide::Mesh* mesh, agx::Real threshold=1E-5 );
333
339
345
354
363
367
372 {
374 {
376 DETACH_EYE_NODES = 1 << 1
377 };
378 };
379
386
399 agx::RigidBody* parentBody,
400 const agx::AffineMatrix4x4& parentBodyWorldTransform,
401 agx::UInt32 wireOptions = agx::UInt32( 0 ) );
402
403
407 template< typename T >
409 {
410 agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 < o2; }
411 };
412
416 template< typename T >
418 {
419 agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 > o2; }
420 };
421
427 template< typename ContainerT, typename Pred >
428 void insertionSort( ContainerT& container, Pred pred )
429 {
430 int size = (int)container.size();
431 if ( size < 2 )
432 return;
433
434 for ( int i = 1; i < size; ++i ) {
435 typename ContainerT::value_type tmp = container[ i ];
436 int j = 0;
437 for ( j = i; j > 0; --j )
438 if ( pred( tmp, container[ j - 1 ] ) )
439 container[ j ] = container[ j - 1 ];
440 else break;
441 container[ j ] = tmp;
442 }
443 }
444
449 template< typename ContainerT >
450 void insertionSort( ContainerT& container )
451 {
453 }
454
459 template< typename ContainerT >
460 agx::Bool isSorted( const ContainerT& container )
461 {
462 if ( container.size() < 2 )
463 return true;
464
465 for ( size_t i = 0; i < container.size() - 1; ++i )
466 if ( container[ i + 1 ] < container[ i ] )
467 return false;
468 return true;
469 }
470
471
486 template<typename ContainerT>
487 void freeContainerMemory(ContainerT& container);
488
514 template<typename ContainerT>
515 ContainerT copyContainerMemory(const ContainerT& container);
516
523 {
524 public:
526
534
541
545 void clear();
546
547 protected:
549
550 virtual int getNumDOF() const override
551 {
552 return 0;
553 }
554 virtual void render( class agxRender::RenderManager* , float /*scale = 1.0f */ ) const override
555 {}
556
558
559 private:
560 class ConstraintHolderImplementation* m_implementation;
561 };
562
564
582 template< typename T >
584 {
585 public:
587 : agxSDK::StepEventListener( mask ), m_obj( obj ) {}
588
589 protected:
591
593#define callback_implementation( callback_name ) \
594 virtual void callback_name( const agx::TimeStamp& t ) \
595 { \
596 if ( !m_obj.isValid() ) { \
597 getSimulation()->remove( this ); \
598 return; \
599 } \
600 m_obj->callback_name( t ); \
601 }
602
606#undef callback_implementation
608
609 protected:
610 agx::observer_ptr< T > m_obj;
611 };
612
630 template< typename T >
631 class GeneralContactEventListener : public agxSDK::ContactEventListener
632 {
633 public:
635 : agxSDK::ContactEventListener( mask ), m_obj( obj ) {}
636
637 protected:
639
640#define callback_implementation( callback_name, ArgT ) \
641 virtual agxSDK::ContactEventListener::KeepContactPolicy callback_name( const agx::TimeStamp& t, ArgT obj ) \
642 { \
643 if ( !m_obj.isValid() ) { \
644 getSimulation()->remove( this ); \
645 return agxSDK::ContactEventListener::KEEP_CONTACT; \
646 } \
647 return m_obj->callback_name( t, obj ); \
648 }
649
652#undef callback_implementation
653
654 virtual void separation( const agx::TimeStamp& t, agxCollide::GeometryPair& gp )
655 {
656 if ( !m_obj.isValid() ) {
657 getSimulation()->remove( this );
658 return;
659 }
660
661 m_obj->separation( t, gp );
662 }
663
664 protected:
666 };
667
668
669
671 {
672 public:
673 static SceneRoot* object();
674
675 virtual bool createVisual( agx::RigidBody* /*rb*/, float /*detailRation*/ = 0.30f ) { return false; };
676 virtual bool createVisual( agxCollide::Geometry* /*geometry*/, float /*detailRation*/ = 0.30f ) { return false; }
677
678 static void setInstance( SceneRoot* baseInstance );
679
680 SINGLETON_CLASSNAME_METHOD();
681
682 protected:
683 virtual ~SceneRoot() {}
684 void shutdown() override;
685
686 protected:
688 };
689
690
691
692}
693
#define LOGGER_WARNING()
Definition: Logger.h:23
#define LOGGER_END()
Definition: Logger.h:26
#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:640
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:38
A contact between two geometries.
Definition: Contacts.h:354
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
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:86
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:70
@ 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:523
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:550
virtual void render(class agxRender::RenderManager *, float) const override
Inherited method of how to render this constraint into DebugRenderer.
Definition: agxUtil.h:554
Utility class to get contact event callbacks to any class having the methods implemented.
Definition: agxUtil.h:632
GeneralContactEventListener(T *obj, int mask=agxSDK::ContactEventListener::DEFAULT)
Definition: agxUtil.h:634
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:665
agxCollide::GeometryContact *virtual void separation(const agx::TimeStamp &t, agxCollide::GeometryPair &gp)
Called upon separation event if getFilter() contain SEPARATION.
Definition: agxUtil.h:654
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:584
GeneralStepListener(T *obj, int mask=agxSDK::StepEventListener::DEFAULT)
Definition: agxUtil.h:586
virtual ~GeneralStepListener()
Definition: agxUtil.h:590
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:610
static void setInstance(SceneRoot *baseInstance)
static SceneRoot * object()
virtual bool createVisual(agx::RigidBody *, float=0.30f)
Definition: agxUtil.h:675
void shutdown() override
Implement this method to cleanup your Singleton class.
virtual bool createVisual(agxCollide::Geometry *, float=0.30f)
Definition: agxUtil.h:676
virtual ~SceneRoot()
Definition: agxUtil.h:683
static SceneRoot * s_baseInstance
Definition: agxUtil.h:687
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:378
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:49
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:60
const agxCollide::GeometryRefVector & getGeometries() const
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:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
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...
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.
void removeGroup(const agx::RigidBody *body, const T &id)
For the specified body, remove the collision group ID to its geometries.
Definition: agxUtil.h:249
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:460
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:428
AGXPHYSICS_EXPORT bool extractConstraints(agx::ConstraintPtrVector &vec, agx::DynamicsSystem *system)
Utility function to extract all constraints from the system.
AGXPHYSICS_EXPORT agxCollide::BoundingAABB computeRigidBodyBoundingVolume(const agx::RigidBody *body)
Compute the BoundingAABB for a specified rigid body using it's geometries.
AGXPHYSICS_EXPORT agx::RealVector linspace(agx::Real start, agx::Real end, agx::UInt num)
agx::ref_ptr< ConstraintHolder > ConstraintHolderRef
Definition: agxUtil.h:563
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.
void addGroup(const agx::RigidBody *body, const T &id)
For the specified body, add the collision group ID to its geometries.
Definition: agxUtil.h:232
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 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:32
bool Bool
Definition: Integer.h:40
uint64_t UInt
Definition: Integer.h:27
double Real
Definition: Real.h:41
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:372
@ DETACH_CONTACT_NODES
Contact nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:375
@ DETACH_EYE_NODES
Eye nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:376
Larger than operator to e.g., sort functions.
Definition: agxUtil.h:418
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:419
Less than operator to e.g., sort functions.
Definition: agxUtil.h:409
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:410