Helios++
Helios software for LiDAR simulations
DynMovingObject.h
1 #pragma once
2 
3 #include <memory>
4 #include <deque>
5 
6 #include <scene/dynamic/DynObject.h>
7 #include <scene/dynamic/DynMotion.h>
8 #include <rigidmotion/RigidMotionEngine.h>
9 #include <scene/dynamic/DynMotionEngine.h>
10 #include <adt/grove/KDGroveSubject.h>
11 #include <sim/tools/VoidStepLoop.h>
12 
13 using std::shared_ptr;
14 using std::make_shared;
15 using std::deque;
16 
18 
19 class KDGrove;
20 
39 class DynMovingObject : public DynObject, public KDGroveSubject{
40 private:
41  // *** SERIALIZATION *** //
42  // *********************** //
43  friend class boost::serialization::access;
50  template <typename Archive>
51  void serialize(Archive &ar, const unsigned int version){
52  boost::serialization::split_member(ar, *this, version);
53  }
59  template <typename Archive>
60  void save(Archive &ar, const unsigned int version) const{
61  boost::serialization::void_cast_register<DynMovingObject, DynObject>();
62  ar &boost::serialization::base_object<DynObject>(*this);
65  ar &dme;
67  }
73  template <typename Archive>
74  void load(Archive &ar, const unsigned int version) {
75  boost::serialization::void_cast_register<DynMovingObject, DynObject>();
76  ar &boost::serialization::base_object<DynObject>(*this);
79  ar &dme;
80  int stepInterval;
81  ar &stepInterval;
82  observerStepLoop.setStepInterval(stepInterval);
83  }
84 protected:
85  // *** ATTRIBUTES *** //
86  // ******************** //
95  deque<shared_ptr<DynMotion>> positionMotionQueue;
105  deque<shared_ptr<DynMotion>> normalMotionQueue;
118  std::shared_ptr<KDGrove> kdGroveObserver;
134 
135 public:
136  // *** CONSTRUCTION / DESTRUCTION *** //
137  // ************************************ //
142  DynObject(),
143  observerStepLoop(1, [&] () -> void {doObserverUpdate();})
144  {}
148  DynMovingObject(ScenePart const &sp, bool const shallowPrimitives=false) :
149  DynObject(sp, shallowPrimitives),
150  kdGroveObserver(nullptr),
151  observerStepLoop(1, [&] () -> void {doObserverUpdate();})
152  {}
156  DynMovingObject(string const id) :
157  DynObject(id),
158  kdGroveObserver(nullptr),
159  observerStepLoop(1, [&] () -> void {doObserverUpdate();})
160  {}
164  DynMovingObject(vector<Primitive *> const &primitives) :
165  DynObject(primitives),
166  kdGroveObserver(nullptr),
167  observerStepLoop(1, [&] () -> void {doObserverUpdate();})
168  {}
172  DynMovingObject(string const id, vector<Primitive *> const &primitives) :
173  DynObject(id, primitives),
174  kdGroveObserver(nullptr),
175  observerStepLoop(1, [&] () -> void {doObserverUpdate();})
176  {}
177  virtual ~DynMovingObject() = default;
178 
179  // *** DYNAMIC BEHAVIOR *** //
180  // ************************** //
211  virtual bool doSimStep();
221  virtual void doObserverUpdate();
222 
223 protected:
240  bool applyDynMotionQueue(
241  std::function<arma::mat()> matrixFromPrimitives,
242  std::function<void(arma::mat const &X)> matrixToPrimitives,
243  std::function<bool()> queueHasNext,
244  std::function<shared_ptr<DynMotion>()> queueNext
245  );
246 
247 public:
248  // *** MOTION QUEUES METHODS *** //
249  // ******************************* //
254  inline void pushPositionMotion(shared_ptr<DynMotion> const dm)
255  {positionMotionQueue.push_back(dm);}
263  inline shared_ptr<DynMotion> nextPositionMotion()
264  {return _next(positionMotionQueue);}
269  {positionMotionQueue.clear();}
275  inline bool positionMotionQueueHasNext() const
276  {return !positionMotionQueue.empty();}
281  inline void pushNormalMotion(shared_ptr<DynMotion> const dm)
282  {normalMotionQueue.push_back(dm);}
290  inline shared_ptr<DynMotion> nextNormalMotion()
291  {return _next(normalMotionQueue);}
296  {normalMotionQueue.clear();}
302  inline bool normalMotionQueueHasNext() const
303  {return !normalMotionQueue.empty();}
304 
305 protected:
315  shared_ptr<DynMotion> _next(deque<shared_ptr<DynMotion>> &deck);
316 
317 public:
318  // *** GROVE SUBSCRIBER METHODS *** //
319  // ********************************** //
325  void registerObserverGrove(shared_ptr<KDGrove> kdGroveObserver) override;
330  void unregisterObserverGrove() override;
334  void setGroveSubjectId(std::size_t const id) override;
338  std::size_t getGroveSubjectId() override;
339 
340  // *** GETTERs and SETTERs *** //
341  // ***************************** //
345  ObjectType getType() const override {return ObjectType::DYN_MOVING_OBJECT;}
356  inline void setObserverStepInterval(int const stepInterval)
357  {observerStepLoop.setStepInterval(stepInterval);}
368  inline int getObserverStepInterval() const
369  {return observerStepLoop.getStepInterval();}
370 };
Adapter which wraps a rigid motion engine to make it fit the dynamic Helios context.
Definition: DynMotionEngine.h:23
Implementation of a dynamic object which supports dynamic motions (extended rigid motions)
Definition: DynMovingObject.h:39
virtual void doObserverUpdate()
Handle update notifications to the subscribed observer. It is, notify the observer that it has been u...
Definition: DynMovingObject.cpp:66
deque< shared_ptr< DynMotion > > positionMotionQueue
Queue of motions to be applied to the position of each primitive.
Definition: DynMovingObject.h:95
DynMovingObject(string const id, vector< Primitive * > const &primitives)
Definition: DynMovingObject.h:172
bool normalMotionQueueHasNext() const
Check if normal motion queue has a next dynamic motion or not.
Definition: DynMovingObject.h:302
deque< shared_ptr< DynMotion > > normalMotionQueue
Queue of motions to be applied to the normal vector of each primitive.
Definition: DynMovingObject.h:105
DynMotionEngine dme
The dynamic motion engine to apply dynamic motions.
Definition: DynMovingObject.h:111
VoidStepLoop observerStepLoop
Handle how many consecutive updates must elapse so the observer is notified.
Definition: DynMovingObject.h:133
shared_ptr< DynMotion > nextPositionMotion()
Retrieve the first dynamic motion in the position motion queue.
Definition: DynMovingObject.h:263
void setObserverStepInterval(int const stepInterval)
Set the step interval between consecutive observer update notifications.
Definition: DynMovingObject.h:356
void load(Archive &ar, const unsigned int version)
Load a serialized dynamic moving object from a stream of bytes.
Definition: DynMovingObject.h:74
DynMovingObject()
Definition: DynMovingObject.h:141
size_t groveSubjectId
The identifier of the dynamic moving object as subject of a KDGrove.
Definition: DynMovingObject.h:124
shared_ptr< DynMotion > nextNormalMotion()
Retrieve the first dynamic motion in the normal motion queue.
Definition: DynMovingObject.h:290
void pushNormalMotion(shared_ptr< DynMotion > const dm)
Push given dynamic motion to the normal motion queue.
Definition: DynMovingObject.h:281
void clearNormalMotionQueue()
Remove all dynamic motions from the normal motion queue.
Definition: DynMovingObject.h:295
void clearPositionMotionQueue()
Remove all dynamic motions from the position motion queue.
Definition: DynMovingObject.h:268
void serialize(Archive &ar, const unsigned int version)
Serialize a dynamic moving object to a stream of bytes.
Definition: DynMovingObject.h:51
void save(Archive &ar, const unsigned int version) const
Save a serialized dynamic moving object to a stream of bytes.
Definition: DynMovingObject.h:60
int getObserverStepInterval() const
Get the step interval between consecutive observer update notifications.
Definition: DynMovingObject.h:368
void pushPositionMotion(shared_ptr< DynMotion > const dm)
Push given dynamic motion to the position motion queue.
Definition: DynMovingObject.h:254
bool positionMotionQueueHasNext() const
Check if position motion queue has a next dynamic motion or not.
Definition: DynMovingObject.h:275
std::shared_ptr< KDGrove > kdGroveObserver
The observer grove to which the dynamic moving object is registered.
Definition: DynMovingObject.h:118
DynMovingObject(string const id)
Definition: DynMovingObject.h:156
DynMovingObject(vector< Primitive * > const &primitives)
Definition: DynMovingObject.h:164
DynMovingObject(ScenePart const &sp, bool const shallowPrimitives=false)
Definition: DynMovingObject.h:148
ObjectType getType() const override
Definition: DynMovingObject.h:345
Dynamic object base implementation.
Definition: DynObject.h:23
Interface defining the behaviors that must be supported by any object that can notify to a KDGrove.
Definition: KDGroveSubject.h:20
Grove of KDTrees. It supports both static and dynamic KDTrees, handling each accordingly.
Definition: KDGrove.h:20
Class representing a scene part.
Definition: ScenePart.h:20
ObjectType
Specify the type of scene part / object. By default, the ScenePart is a static object.
Definition: ScenePart.h:53
virtual int getStepInterval() const
Obtain the step interval.
Definition: StepLoop.h:108
virtual void setStepInterval(int const stepInterval)
Set the step interval.
Definition: StepLoop.h:114
Class extending StepLoop to support functions with no return.
Definition: VoidStepLoop.h:15
Class to handle operations with rigid motions.
Definition: RigidMotionEngine.h:19