AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
agxSensor/Environment.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
17#pragma once
18
23
25
27#include <agx/AtomicState.h>
28#include <agx/Timer.h>
29
30#include <thread>
31
32namespace agxCollide
33{
34 class Shape;
35 class Geometry;
36}
37
38namespace agx
39{
40 class RigidBody;
41}
42
43namespace agxSDK
44{
45 class Simulation;
46 class LinkedStructure;
47}
48
49namespace agxWire
50{
51 class Wire;
52}
53
54namespace agxModel
55{
56 class DeformableObject;
57}
58
59namespace agxTerrain
60{
61 class Terrain;
62 class TerrainPager;
63}
64
65namespace agxSensor
66{
68
113 {
114 public:
123
130 static Environment* get( agxSDK::Simulation* simulation );
131
138 static const Environment* get( const agxSDK::Simulation* simulation );
139
140 public:
145
154 bool add( agxCollide::Shape* shape );
155
162 bool remove( agxCollide::Shape* shape );
163
173 bool add( agxCollide::Geometry* geometry );
174
181 bool remove( agxCollide::Geometry* geometry );
182
193 bool add( agx::RigidBody* rb );
194
202
214 bool add( agxTerrain::Terrain* terrain );
215
223
236
244
257 bool add( agxSDK::LinkedStructure* linkedStructure );
258
266 bool remove( agxSDK::LinkedStructure* linkedStructure );
267
276 bool add( agxWire::Wire* wire );
277
284 bool remove( agxWire::Wire* wire );
285
294 bool add( agxModel::DeformableObject* deformable );
295
303
312 bool add( SystemNode* system );
313
319 bool remove( SystemNode* system );
320
325
331 void setMagneticField( MagneticField* magneticField );
332
337
342
343 public:
347 virtual ~Environment();
348
350
351
355 virtual void addNotification() override;
356
360 virtual void removeNotification() override;
361
367 virtual void step( agx::Real dt ) override;
368
374 virtual void fetchStepResults() override;
375
380 virtual void updateGraphics( const agxRender::RenderManager& renderManager ) override;
381
385 template<typename T>
386 agx::VectorPOD<T*> getObjects() const;
387
391 template<typename T>
392 T* findRootSystem() const;
393
397 template<typename T>
398 agx::VectorPOD<T*> findRootSystems() const;
399
403 template<typename T>
404 agx::VectorPOD<T*> findSystems() const;
405
407
408 private:
409 struct RuntimeState
410 {
411 enum Flag : agx::UInt32
412 {
413 INITIALIZED = 1 << 0,
414 DO_STEP = 1 << 1,
415 SYNCHRONIZING_DATA = 1 << 2,
416 SLEEP = 1 << 3,
417 IS_SLEEPING = 1 << 4,
418 JOIN_MAIN = 1 << 30,
419 };
420
421 using Value = agx::AtomicState<Flag>;
422
423 Value state{ (agx::UInt32)0 };
424 agx::Real dt = 1.0 / 60.0;
425 };
426
427 private:
428 static size_t& instanceCount();
429
430 private:
431 void initialize();
432 void cleanup();
433
434 bool fireOnAdd( agx::Referenced& instance );
435 bool fireOnRemove( agx::Referenced& instance );
436
437 template<typename T>
438 bool accept( bool handled, agx::Referenced& instance, bool isAdded );
439
440 template<typename T, typename InOutHelper>
441 void storeType( agxStream::OutputArchive& out,
442 InOutHelper& helper ) const;
443
444 template<typename T, typename InOutHelper>
445 void restoreType( agxStream::InputArchive& in,
446 InOutHelper& helper,
447 bool callAddOnRestored );
448
449 bool isDone() const;
450 void realize();
451
453
454 private:
455 SystemNodeRefVector m_systems;
456 std::thread m_thread;
457 RuntimeState m_runtimeData;
458 agx::ReferencedHandler m_refHandler;
459 RtSceneRef m_scene;
460 MagneticFieldRef m_magneticField;
461 };
462
463 template<typename T>
464 agx::VectorPOD<T*> Environment::getObjects() const
465 {
466 agx::VectorPOD<T*> objects;
467 m_refHandler.template visit<T>( [&objects]( const T& instance )
468 {
469 objects.push_back( const_cast<T*>( &instance ) );
470 } );
471 return objects;
472 }
473
474 template<typename T>
475 T* Environment::findRootSystem() const
476 {
477 for ( const auto& system : m_systems )
478 if ( system->template is<T>() )
479 return system->template as<T>();
480 return nullptr;
481 }
482
483 template<typename T>
484 agx::VectorPOD<T*> Environment::findRootSystems() const
485 {
486 agx::VectorPOD<T*> result;
487 for ( const auto& system : m_systems )
488 if ( system->template is<T>() )
489 result.push_back( system->template as<T>() );
490 return result;
491 }
492
493 template<typename T>
494 agx::VectorPOD<T*> Environment::findSystems() const
495 {
496 agx::VectorPOD<T*> result;
497 for ( const auto& system : m_systems ) {
498 if ( system->template is<T>() )
499 result.push_back( system->template as<T>() );
500
501 system->template visitChildrenOfType<T>( [&result]( T& child )
502 {
503 result.push_back( &child );
504 } );
505 }
506
507 return result;
508 }
509
510 template<typename T>
511 bool Environment::accept( bool handled, agx::Referenced& instance, bool isAdded )
512 {
513 static_assert( std::is_base_of<agxStream::Serializable, T>::value,
514 "Unexpected that type doesn't suport serialization." );
515
516 agxAssert( !handled || instance.template is<T>() );
517 if ( handled && isAdded ) {
518 agxSensor::getOrCreateEnvData( instance.template asSafe<T>() )->environment = this;
519 m_refHandler.template add<T>( instance );
520 }
521 else if ( handled ) {
522 agxSensor::getOrCreateEnvData( instance.template asSafe<T>() )->environment = nullptr;
523 m_refHandler.template remove<T>( instance );
524 }
525
526 return handled;
527 }
528
529 template<typename T, typename InOutHelper>
530 void Environment::storeType( agxStream::OutputArchive& out, InOutHelper& helper ) const
531 {
532 out << agxStream::out( "numInstances", m_refHandler.template size<T>() );
533 m_refHandler.template visit<T>( [&out, &helper]( const T& instance )
534 {
535 out << agxStream::out( "instance", &instance );
536
537 // By design that instances added to the ref-handler has
538 // environment data.
539 const EnvInternalData* envData = getEnvData( &instance );
540 out << agxStream::out( "hasRtData", envData->rtData != nullptr );
541 if ( envData->rtData != nullptr ) {
542 envData->rtData->state.flags.store( out );
543 out << agxStream::out( "rtEntityIndex", indexOf( envData->rtData->getEntityId() ) );
544
545 helper.store( out, envData->rtData->getMaterial() );
546 }
547 } );
548 }
549
550 template<typename T, typename InOutHelper>
551 void Environment::restoreType( agxStream::InputArchive& in,
552 InOutHelper& helper,
553 bool callAddOnRestored )
554 {
555 size_t numInstances = 0u;
556 in >> agxStream::in( "numInstances", numInstances );
557 for ( size_t i = 0u; i < numInstances; ++i ) {
558 T* instance = nullptr;
559 in >> agxStream::in( "instance", instance );
560 if ( instance != nullptr ) {
561 bool hasRtData = false;
562 in >> agxStream::in( "hasRtData", hasRtData );
563 if ( hasRtData ) {
564 RtInternalData* rtData = getOrCreateRtData( instance );
565 rtData->state.flags.restore( in );
566 RtEntityIdIndex rtEntityIndex = 0;
567 in >> agxStream::in( "rtEntityIndex", rtEntityIndex );
568 rtData->setEntityId( (RtEntityId)rtEntityIndex );
569
570 RtMaterialInstance material{};
571 helper.restore( in, material );
572 rtData->setMaterial( material );
573 }
574
575 if ( callAddOnRestored )
576 this->add( instance );
577 this->template accept<T>( true, *instance, true );
578 instance->setFinished();
579 }
580 }
581 }
582}
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#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 AGXSENSOR_EXPORT
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
Base class for a shape.
Definition: Shape.h:58
Deformable object containing a tetrahedron mesh and a set of algorithms.
Interface to a cooperative sensor simulation assumed to be executed in a separate thread spawned in a...
Basic implementation of any structure where bodies are linked/constrained together in a well defined ...
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:131
Sensor environment implementation where different sensors collects data given a set of added objects,...
bool add(agxSDK::LinkedStructure *linkedStructure)
Add agxCable::Cable, agxVehicle::Track, agxModel::Beam, ..., to be visible to sensors in this sensor ...
bool remove(agxCollide::Shape *shape)
Remove shape from this sensor environment.
bool remove(agxModel::DeformableObject *deformable)
Remove deformable object from this sensor environment.
void setMagneticField(MagneticField *magneticField)
Set environment magnetic field.
static Environment * get(agxSDK::Simulation *simulation)
bool add(agx::RigidBody *rb)
Add rigid body to be visible to sensors in this sensor environment.
bool add(agxCollide::Shape *shape)
Add shape to be visible to sensors in this sensor environment.
static Environment * getOrCreate(agxSDK::Simulation *simulation)
Returns an existing or creates and initializes a new instance of a sensor environment given a simulat...
bool remove(agxTerrain::TerrainPager *pager)
Remove terrain pager from this sensor environment.
RtSceneRef getScene() const
static const Environment * get(const agxSDK::Simulation *simulation)
bool add(SystemNode *system)
Add system root node (normally an implementation of a sensor) to this sensor environment.
bool remove(agxSDK::LinkedStructure *linkedStructure)
Remove linked structure (agxCable::Cable, agxVehicle::Track, agxModel::Beam, ...) from this sensor en...
bool remove(agxTerrain::Terrain *terrain)
Remove terrain from this sensor environment.
bool add(agxWire::Wire *wire)
Add wire to be visible to sensors in this sensor environment.
bool add(agxCollide::Geometry *geometry)
Add geometry to be visible to sensors in this sensor environment.
const MagneticField * getMagneticField() const
bool remove(agxWire::Wire *wire)
Remove wire from this sensor environment.
Environment()
Default constructor - the default sensor systems will be added.
bool add(agxTerrain::TerrainPager *pager)
Add terrain pager to be visible to sensors in this sensor environment.
bool add(agxTerrain::Terrain *terrain)
Add terrain to be visible to sensors in this sensor environment.
bool remove(agx::RigidBody *rb)
Remove the rigid body, and all its geometries, from this sensor environment.
bool add(agxModel::DeformableObject *deformable)
Add deformable object to be visible to sensors in this sensor environment.
MagneticField * getMagneticField()
bool remove(SystemNode *system)
Remove system root node (sensor) from this sensor environment.
virtual ~Environment()
Destructor.
bool remove(agxCollide::Geometry *geometry)
Remove geometry, and all its shapes, from this sensor environment.
Base type for the specification of the magnetic field in a sensor environment.
Definition: MagneticField.h:33
System node is a separate step/operation in an execution tree representing a sensor (or such) in a se...
Definition: SystemNode.h:44
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:57
A pager that will dynamically handle terrain tiles in a agxSDK::Simulation.
Definition: TerrainPager.h:100
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
Definition: Terrain.h:92
Interface and placeholder of controllers/helpers for wires.
Definition: Wire.h:62
Base class providing referencing counted objects.
Definition: Referenced.h:120
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:49
Vector containing 'raw' data.
Definition: agx/Vector.h:246
void push_back(const T &value)
Definition: agx/Vector.h:1362
#define agxAssert(expr)
Definition: debug.h:143
#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...
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: Space.h:63
Namespace containing classes for handling debug rendering of collision geometries,...
Definition: Constraint.h:36
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
The agxSensor namespace contains components to model real-time sensors connected to the physics of AG...
RtEntityIdIndex indexOf(RtEntityId entityId)
std::shared_ptr< RtScene > RtSceneRef
std::underlying_type_t< RtEntityId > RtEntityIdIndex
Index type of entity ids.
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:401
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive.
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:59
Implements a Wire model with adaptive resolution.
Definition: MergedBody.h:39
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:32
double Real
Definition: Real.h:41
STL namespace.