AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
SystemNode.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#include <agxSensor/export.h>
18
19#include <agx/Referenced.h>
20#include <agx/BitState.h>
21
23
24#include <queue>
25#include <stack>
26
27namespace agxRender
28{
29 class RenderManager;
30}
31
32namespace agxSensor
33{
34 class Environment;
35
38
44 {
45 public:
47 {
48 agx::Real dt = 1.0 / 60.0;
49 };
50
51 public:
56 void setName( const agx::Name& name );
57
61 const agx::Name& getName() const;
62
67 void setEnable( bool enable );
68
72 bool getEnable() const;
73
79 bool addChild( SystemNode* child );
80
88 bool appendChild( SystemNode* child );
89
95 bool removeChild( SystemNode* child );
96
103 bool detachChild( SystemNode* child );
104
111 virtual bool insertParent( SystemNode& parent );
112
119 virtual bool eraseAndConnect();
120
127 bool hasChild( const SystemNode* child, bool recurse = true ) const;
128
132 size_t getNumChildren() const;
133
138 SystemNode* getChild( size_t index ) const;
139
148 template<typename VisitorT>
149 SystemNode* visitBf( VisitorT visitor ) const;
150
159 template<typename VisitorT>
160 SystemNode* visitChildrenBf( VisitorT visitor ) const;
161
170 template<typename VisitorT>
171 SystemNode* visitDf( VisitorT visitor ) const;
172
181 template<typename VisitorT>
182 SystemNode* visitChildrenDf( VisitorT visitor ) const;
183
190 template<typename T>
191 T* getChild() const;
192
202 template<typename PredicateT>
203 SystemNode* findChild( PredicateT pred, bool recurse = true ) const;
204
212 template<typename T>
213 T* findChild( bool recurse = true ) const;
214
219
226 template<typename PredicateT>
227 SystemNode* findParent( PredicateT pred ) const;
228
232 template<typename T>
233 T* findParent() const;
234
241 template<typename VisitorT>
242 void visitChildren( VisitorT visitor, bool recurse = true ) const;
243
250 template<typename T, typename VisitorT>
251 void visitChildrenOfType( VisitorT visitor, bool recurse = true) const;
252
257
262
263 public:
267 SystemNode( const SystemNode& ) = delete;
268
269 virtual ~SystemNode() = default;
270
275 virtual bool addNotification();
276
280 virtual void removeNotification();
281
286 virtual void onChildAdded( SystemNode& child );
287
292 virtual void onChildRemoved( SystemNode& child );
293
300 virtual bool onAdd( agx::Referenced& instance );
301
308 virtual bool onRemove( agx::Referenced& instance );
309
316 virtual void synchronize( const CallbackData& data );
317
324 virtual void execute( const CallbackData& data );
325
331 virtual void complete();
332
339 virtual void result( const CallbackData& data );
340
345 virtual void synchronizeGraphics( const agxRender::RenderManager& renderManager );
346
350 virtual void cleanup() = 0;
351
353
354 public:
359 void printTree(std::ostream& out) const;
360
366 void printTree(std::ostream& out, size_t& depth) const;
367
369
370 virtual void store( agxStream::OutputArchive& out ) const override;
371 virtual void restore( agxStream::InputArchive& in ) override;
372
373 protected:
374 SystemNode();
375
376 protected:
377 SystemNode* m_parent;
378 SystemNodeRefVector m_children;
379
380 struct CallbackEvent
381 {
382 enum CallbackType
383 {
384 SYNC,
385 EXECUTE,
386 COMPLETE,
387 RESULT,
388 CLEANUP
389 };
390
391 CallbackType type;
392 CallbackData data;
393 };
394
395 virtual void invoke( const CallbackEvent& callbackEvent );
396
397 private:
398 friend Environment;
399
400 struct InternalState
401 {
402 enum Flag : agx::UInt32
403 {
404 ENABLED = 1 << 0,
405 };
406 using Flags = agx::BitState<Flag, std::underlying_type_t<Flag>>;
407 Flags flags;
408
409 InternalState()
410 : flags( ENABLED )
411 {
412 }
413 };
414
416
417 private:
418 bool setEnvironment( Environment* environment );
419
420 private:
421 Environment* m_environment;
422 agx::Name m_name;
423 InternalState m_state;
424 };
425
426 template<typename VisitorT>
427 SystemNode* SystemNode::visitBf( VisitorT visitor ) const
428 {
429 auto self = const_cast<SystemNode*>( this );
430 if ( visitor( *self ) )
431 return self;
432
433 return this->visitChildrenBf( visitor );
434 }
435
436 template<typename VisitorT>
437 SystemNode* SystemNode::visitChildrenBf( VisitorT visitor ) const
438 {
439 using SystemNodeQueue = std::queue<SystemNode*>;
440
441 SystemNodeQueue queue;
442
443 for ( auto& child : m_children )
444 queue.push( child );
445
446 while ( !queue.empty() ) {
447 auto node = queue.front();
448 queue.pop();
449
450 if ( visitor( *node ) )
451 return node;
452
453 for ( auto& child : node->m_children )
454 queue.push( child );
455 }
456
457 return nullptr;
458 }
459
460 template<typename VisitorT>
461 SystemNode* SystemNode::visitDf( VisitorT visitor ) const
462 {
463 auto self = const_cast<SystemNode*>( this );
464 if ( visitor( *self ) )
465 return self;
466
467 return this->visitChildrenDf( visitor );
468 }
469
470 template<typename VisitorT>
471 SystemNode* SystemNode::visitChildrenDf( VisitorT visitor ) const
472 {
473 using SystemNodeStack = std::stack<SystemNode*>;
474
475 SystemNodeStack stack;
476
477 for ( auto& myChild : m_children ) {
478 stack.push( myChild );
479
480 while ( !stack.empty() ) {
481 auto node = stack.top();
482 stack.pop();
483
484 if ( visitor( *node ) )
485 return node;
486
487 for ( auto it = node->m_children.rbegin(); it != node->m_children.rend(); ++it )
488 stack.push( *it );
489 }
490 }
491
492 return nullptr;
493 }
494
495 template<typename T>
496 T* SystemNode::getChild() const
497 {
498 return this->template findChild<T>( false );
499 }
500
501 template<typename PredicateT>
502 SystemNode* SystemNode::findChild( PredicateT pred, bool recurse /*= true*/ ) const
503 {
504 if ( recurse ) {
505 return this->visitChildrenDf( [&pred]( const SystemNode& node )
506 {
507 return pred( node );
508 } );
509 }
510 else {
511 auto it = std::find_if( m_children.begin(),
512 m_children.end(),
513 [&pred]( const SystemNodeRef& child )
514 {
515 return child != nullptr && pred( *child.get() );
516 } );
517 return it != m_children.end() ?
518 (SystemNode*)it->get() :
519 nullptr;
520 }
521 }
522
523 template<typename T>
524 T* SystemNode::findChild( bool recurse /*= true*/ ) const
525 {
526 auto child = this->findChild( []( const SystemNode& child )
527 {
528 return child.template asSafe<T>() != nullptr;
529 },
530 recurse );
531 return child != nullptr ? child->template as<T>() : nullptr;
532 }
533
534 template<typename VisitorT>
535 void SystemNode::visitChildren( VisitorT visitor, bool recurse /*= true*/ ) const
536 {
537 if ( recurse ) {
538 this->visitChildrenBf( [&visitor]( SystemNode& child )
539 {
540 visitor( child );
541 return false;
542 } );
543 }
544 else {
545 for ( auto& child : m_children )
546 visitor( *child );
547 }
548 }
549
550 template<typename PredicateT>
551 SystemNode* SystemNode::findParent( PredicateT pred ) const
552 {
553 auto parent = const_cast<SystemNode*>( this );
554 while ( ( parent = parent->getParent() ) != nullptr && !pred( *parent ) )
555 ;
556
557 return parent;
558 }
559
560 template<typename T>
561 T* SystemNode::findParent() const
562 {
563 auto parent = this->findParent( []( const SystemNode& parent )
564 {
565 return parent.template asSafe<T>() != nullptr;
566 } );
567 return parent != nullptr ? parent->template asSafe<T>() : nullptr;
568 }
569
570 template<typename T, typename VisitorT>
571 void SystemNode::visitChildrenOfType( VisitorT visitor, bool recurse /*= true*/ ) const
572 {
573 this->visitChildren( [&visitor]( SystemNode& child )
574 {
575 if ( auto childT = child.template asSafe<T>() )
576 visitor( *childT );
577 },
578 recurse );
579 }
580}
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE(T)
Use this in a pure abstract Serializable class to add the required methods Important: Use full namesp...
Definition: Serializable.h:221
#define AGXSENSOR_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc.
Sensor environment implementation where different sensors collects data given a set of added objects,...
System node is a separate step/operation in an execution tree representing a sensor (or such) in a se...
Definition: SystemNode.h:44
virtual void execute(const CallbackData &data)
Execute this system, integrating it data.dt forward in time.
virtual void onChildAdded(SystemNode &child)
Called when child has been added to this parent.
const agx::Name & getName() const
void setName(const agx::Name &name)
Assign name to this node.
void setEnable(bool enable)
Enable/disable this node.
bool detachChild(SystemNode *child)
Detach/erase child from vector of children, silently.
virtual bool addNotification()
Called when this system node has been added to a sensor environment.
size_t getNumChildren() const
SystemNode * getRoot() const
virtual void removeNotification()
Called when this system node is being removed from a sensor environment.
bool getEnable() const
bool appendChild(SystemNode *child)
Append child to vector of children, silently.
virtual bool onAdd(agx::Referenced &instance)
Called when an object has been added to the sensor environment.
bool hasChild(const SystemNode *child, bool recurse=true) const
Environment * getEnvironment() const
bool addChild(SystemNode *child)
Add unique child to this node.
virtual void cleanup()=0
The sensor environment is being put down, clean everything.
SystemNode(const SystemNode &)=delete
Disallow direct copying of nodes.
SystemNode * getChild(size_t index) const
virtual void synchronize(const CallbackData &data)
Synchronization from the main thread.
virtual bool insertParent(SystemNode &parent)
Inserts new parent to this node.
virtual void synchronizeGraphics(const agxRender::RenderManager &renderManager)
At the very end of a step, synchronize any graphics related aspects of the system node.
virtual void complete()
Complete the execution of this system, finishing the integration forward in time.
virtual bool onRemove(agx::Referenced &instance)
Called when an object has been removed from the sensor environment.
virtual void onChildRemoved(SystemNode &child)
Called when child has been removed from this parent.
virtual ~SystemNode()=default
bool removeChild(SystemNode *child)
Remove child from this node.
virtual void result(const CallbackData &data)
If necessary assemble, and fetch results related to this system node after a step by data....
SystemNode * getParent() const
virtual bool eraseAndConnect()
Erase this node from the tree and connect its children to the current parent of this node.
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Representation of a name string.
Definition: Name.h:33
Base class providing referencing counted objects.
Definition: Referenced.h:120
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
Namespace containing classes for handling debug rendering of collision geometries,...
Definition: Constraint.h:36
The agxSensor namespace contains components to model real-time sensors connected to the physics of AG...
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
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.