AGX Dynamics 2.41.1.2
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
27#include <agxCollide/Convex.h>
28
31#include <agxSDK/Simulation.h>
32
33
34namespace agx
35{
36 typedef std::pair< Real, Real > RealPair;
37 class RigidBody;
38 class Material;
39 class ConstraintImplementation;
40
41}
42
43namespace agxSDK {
44 class Assembly;
45}
46
47namespace agxCollide {
48 class Mesh;
49}
50
51namespace agxControl
52{
53 class EventSensor;
54}
55
56
61namespace agxUtil {
62
71
80
89
99
109
119
129
139
147
155
156
167
176
177
187
195
203
211
218
226
227
233 template <typename T>
234 void addGroup(const agx::RigidBody* body, const T& id)
235 {
236 if (body == nullptr) {
237 LOGGER_WARNING() << "Invalid null pointer for body " << std::endl << LOGGER_END();
238 return;
239 }
240
241 for (const auto& geometry : body->getGeometries())
242 geometry->addGroup(id);
243 }
244
250 template <typename T>
251 void removeGroup(const agx::RigidBody* body, const T& id)
252 {
253 if (body == nullptr) {
254 LOGGER_WARNING() << "Invalid null pointer for body " << std::endl << LOGGER_END();
255 return;
256 }
257
258 for (const auto& geometry : body->getGeometries())
259 geometry->removeGroup(id);
260 }
261
267 template<typename T>
268 void addGroup(agxSDK::Assembly* assembly,T id)
269 {
271 agxUtil::extractGeometries(geometries, assembly);
272
273 for (auto& g: geometries)
274 g->addGroup(id);
275 }
276
282 template<typename T>
283 void removeGroup(agxSDK::Assembly* assembly, T id)
284 {
286 agxUtil::extractGeometries(geometries, assembly);
287
288 for (auto& g: geometries)
289 g->removeGroup(id);
290 }
291
299
312 bool onlyIncludeEmittedBodies);
313
322
334 AGXPHYSICS_EXPORT bool isConvexMesh( const agxCollide::Mesh* mesh, agx::Real threshold=1E-5 );
335
341
347
356
365
369
374 {
376 {
378 DETACH_EYE_NODES = 1 << 1
379 };
380 };
381
388
401 agx::RigidBody* parentBody,
402 const agx::AffineMatrix4x4& parentBodyWorldTransform,
403 agx::UInt32 wireOptions = agx::UInt32( 0 ) );
404
405
409 template< typename T >
411 {
412 agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 < o2; }
413 };
414
418 template< typename T >
420 {
421 agx::Bool operator() ( const T& o1, const T& o2 ) { return o1 > o2; }
422 };
423
429 template< typename ContainerT, typename Pred >
430 void insertionSort( ContainerT& container, Pred pred )
431 {
432 int size = (int)container.size();
433 if ( size < 2 )
434 return;
435
436 for ( int i = 1; i < size; ++i ) {
437 typename ContainerT::value_type tmp = container[ i ];
438 int j = 0;
439 for ( j = i; j > 0; --j )
440 if ( pred( tmp, container[ j - 1 ] ) )
441 container[ j ] = container[ j - 1 ];
442 else break;
443 container[ j ] = tmp;
444 }
445 }
446
451 template< typename ContainerT >
452 void insertionSort( ContainerT& container )
453 {
455 }
456
461 template< typename ContainerT >
462 agx::Bool isSorted( const ContainerT& container )
463 {
464 if ( container.size() < 2 )
465 return true;
466
467 for ( size_t i = 0; i < container.size() - 1; ++i )
468 if ( container[ i + 1 ] < container[ i ] )
469 return false;
470 return true;
471 }
472
473
488 template<typename ContainerT>
489 void freeContainerMemory(ContainerT& container);
490
516 template<typename ContainerT>
517 ContainerT copyContainerMemory(const ContainerT& container);
518
525 {
526 public:
528
536
543
547 void clear();
548
549 protected:
551
552 virtual int getNumDOF() const override
553 {
554 return 0;
555 }
556 virtual void render( class agxRender::RenderManager* , float /*scale = 1.0f */ ) const override
557 {}
558
560
561 private:
562 class ConstraintHolderImplementation* m_implementation;
563 };
564
566
584 template< typename T >
586 {
587 public:
589 : agxSDK::StepEventListener( mask ), m_obj( obj ) {}
590
591 protected:
593
595#define callback_implementation( callback_name ) \
596 virtual void callback_name( const agx::TimeStamp& t ) \
597 { \
598 if ( !m_obj.isValid() ) { \
599 getSimulation()->remove( this ); \
600 return; \
601 } \
602 m_obj->callback_name( t ); \
603 }
604
608#undef callback_implementation
610
611 protected:
612 agx::observer_ptr< T > m_obj;
613 };
614
632 template< typename T >
633 class GeneralContactEventListener : public agxSDK::ContactEventListener
634 {
635 public:
637 : agxSDK::ContactEventListener( mask ), m_obj( obj ) {}
638
639 protected:
641
642#define callback_implementation( callback_name, ArgT ) \
643 virtual agxSDK::ContactEventListener::KeepContactPolicy callback_name( const agx::TimeStamp& t, ArgT obj ) \
644 { \
645 if ( !m_obj.isValid() ) { \
646 getSimulation()->remove( this ); \
647 return agxSDK::ContactEventListener::KEEP_CONTACT; \
648 } \
649 return m_obj->callback_name( t, obj ); \
650 }
651
654#undef callback_implementation
655
656 virtual void separation( const agx::TimeStamp& t, agxCollide::GeometryPair& gp )
657 {
658 if ( !m_obj.isValid() ) {
659 getSimulation()->remove( this );
660 return;
661 }
662
663 m_obj->separation( t, gp );
664 }
665
666 protected:
668 };
669
670
671
673 {
674 public:
675 static SceneRoot* object();
676
677 virtual bool createVisual( agx::RigidBody* /*rb*/, float /*detailRation*/ = 0.30f ) { return false; };
678 virtual bool createVisual( agxCollide::Geometry* /*geometry*/, float /*detailRation*/ = 0.30f ) { return false; }
679
680 static void setInstance( SceneRoot* baseInstance );
681
682 SINGLETON_CLASSNAME_METHOD();
683
684 protected:
685 virtual ~SceneRoot() {}
686 void shutdown() override;
687
688 protected:
690 };
691
692
693
694}
695
#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:208
#define AGXPHYSICS_EXPORT
#define callback_implementation(callback_name)
Definition: agxUtil.h:642
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:38
A contact between two geometries.
Definition: Contacts.h:336
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:525
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:552
virtual void render(class agxRender::RenderManager *, float) const override
Inherited method of how to render this constraint into DebugRenderer.
Definition: agxUtil.h:556
Utility class to get contact event callbacks to any class having the methods implemented.
Definition: agxUtil.h:634
GeneralContactEventListener(T *obj, int mask=agxSDK::ContactEventListener::DEFAULT)
Definition: agxUtil.h:636
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:667
agxCollide::GeometryContact *virtual void separation(const agx::TimeStamp &t, agxCollide::GeometryPair &gp)
Called upon separation event if getFilter() contain SEPARATION.
Definition: agxUtil.h:656
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:586
GeneralStepListener(T *obj, int mask=agxSDK::StepEventListener::DEFAULT)
Definition: agxUtil.h:588
virtual ~GeneralStepListener()
Definition: agxUtil.h:592
agx::observer_ptr< T > m_obj
Definition: agxUtil.h:612
static void setInstance(SceneRoot *baseInstance)
static SceneRoot * object()
virtual bool createVisual(agx::RigidBody *, float=0.30f)
Definition: agxUtil.h:677
void shutdown() override
Implement this method to cleanup your Singleton class.
virtual bool createVisual(agxCollide::Geometry *, float=0.30f)
Definition: agxUtil.h:678
virtual ~SceneRoot()
Definition: agxUtil.h:685
static SceneRoot * s_baseInstance
Definition: agxUtil.h:689
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:376
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:52
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:63
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:251
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:462
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:430
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:565
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:234
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: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:374
@ DETACH_CONTACT_NODES
Contact nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:377
@ DETACH_EYE_NODES
Eye nodes which parent isn't included in the assembly will become lumped nodes instead.
Definition: agxUtil.h:378
Larger than operator to e.g., sort functions.
Definition: agxUtil.h:420
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:421
Less than operator to e.g., sort functions.
Definition: agxUtil.h:411
agx::Bool operator()(const T &o1, const T &o2)
Definition: agxUtil.h:412