AGX Dynamics  2.37.2.0
Clock.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 #ifndef AGX_CLOCK_H
17 #define AGX_CLOCK_H
18 
19 
20 #include <agxData/Value.h>
21 #include <agx/Task.h>
22 
23 
24 namespace agx
25 {
26  class TiXmlElement;
27 
30 
38  {
39  public:
40  static agx::Model *ClassModel();
41 
42  static Clock *load(TiXmlElement *eClock, Device *device);
43  virtual void configure(TiXmlElement *eClock) override;
44  virtual void save(TiXmlElement *eParent) const override;
45 
46 
49 
52 
53  public:
55  TickEvent preTick;
56  TickEvent postTick;
57 
58  public:
59  Clock(const agx::Name& name = "clock", agx::Real frequency = 60);
60 
65  void tick(bool blocking = true);
66 
71  void stepTo(agx::Real time);
72 
77  void stepForward(agx::Real delta);
78 
82  void setTime(agx::Real64 time);
83 
87  agx::Real getTime() const;
88 
92  agx::Bool isEnabled() const;
93 
97  void setEnable(agx::Bool flag);
98 
102  agx::Real getFrequency() const;
103 
107  void setFrequency(agx::Real frequency);
108 
112  agx::UInt getFrame() const;
113 
117  agx::Real getTimeStep() const;
118 
122  bool setTimeStep(agx::Real dt);
123 
127  agx::Real getTimeModulation() const;
128 
132  void setTimeModulation(agx::Real modulation);
133 
138  void addEvent(agx::Clock::Event *event);
139 
144  void removeEvent(agx::Clock::Event *event);
145 
150  void addFrameJob(agx::Job *job);
151 
156  void removeFrameJob(agx::Job *job);
157 
162  void addFrameTask(agx::Task *task);
163 
168  void removeFrameTask(agx::Task *task);
169 
174  void addSlave(agx::Clock *slave);
175 
179  void setFrame(UInt frame);
180 
185  void removeSlave(agx::Clock *slave);
186 
190  const agx::ClockPtrVector& getSlaves() const;
191 
195  agx::Clock *getReference();
196 
197 
201  void reset();
202 
204  void bind(const agx::Path& path);
205 
207  const agx::Path& getBindPath() const;
208 
209  virtual void rebind() override;
210 
216  void setIsThreadTimelineResponsible(bool isResponsible);
217 
219  bool getIsThreadTimelineResponsible() const;
220 
221  protected:
222  virtual ~Clock();
223 
224  // virtual bool bind(Object *object) override;
225 
226  private:
227  void slaveTick();
228  void rebuild( Real oldTimeStep );
229  void schedule(Event *event);
230  void schedule(Event *event, Real timeUntilDispatch);
231  void unschedule(Event *event);
232  void reschedule(Event *event);
233 
234  void enableCallback(agxData::Value *);
235  void calculateTimestep();
236 
237  private:
238  agxData::ValueRefT<Bool> m_enabled;
240  agxData::ValueRefT<Real> m_frequency;
241  agxData::ValueRefT<Real> m_timeModulation;
242  agxData::ValueRefT<Real> m_timeStep;
243  agxData::ValueRefT<UInt> m_frame;
244 
245  agxData::Value::Event::CallbackType m_updateTimeStepCallback;
246  agxData::Value::Event::CallbackType m_enableCallback;
247 
248  Path m_bindPath;
249 
250  ClockRef m_reference;
251  ClockPtrVector m_slaves;
252  EventRef m_syncEvent;
253 
254  class Frame;
256  // typedef VectorPOD<Frame *> FramePtrVector;
257  FrameTable m_frameTable;
258  EventPtrVector m_events;
259  // EventPtrVector m_tickEvents;
260  TaskObserverVector m_frameTasks;
261  JobPtrVector m_frameJobs;
262  // Job m_framePostJob;
263  FrameWrapperRef m_frameTaskWrapper;
264  bool m_slaveTrigger;
265  bool m_isTimelineResponsible = true;
266  };
267 
269 
273  class AGXCORE_EXPORT Clock::Event : public Component, public agx::Event
274  {
275  public:
276  static agx::Model *ClassModel();
277 
278  static Clock::Event *load(TiXmlElement *eEvent, Device *device);
279  virtual void configure(TiXmlElement *eEvent) override;
280 
281  public:
282 
284  Event(const agx::Name& name = "Event");
285  Event(const agx::Callback& callback, agx::Real startDelay, agx::Real interval = -1.0);
286 
290  agx::Clock *getClock();
291 
296  void setStartDelay(agx::Real time);
297 
298 
302  agx::Real getStartDelay() const;
303 
308  void setInterval(agx::Real interval);
309 
310 
315  agx::Real getInterval() const;
316 
321  void setRepeating(bool flag);
322 
326  bool isRepeating() const;
327 
331  void setEnable(bool flag);
332 
334  void start();
335 
337  void stop();
338 
342  void reset();
343 
345  size_t getIndex() const;
346 
347  protected:
348  virtual ~Event();
349 
350  private:
351  Real calculateTimeUntilDispatch( Real timeStep );
352  Real calculateTimeUntilDispatch();
353  void valueCallback(agxData::Value *value);
354 
355  friend class Clock;
356  void setIndex(size_t index);
357  Event *m_next;
358 
359  size_t m_index;
360  Clock *m_clock;
361  agxData::ValueRefT<Real> m_startDelay;
362  agxData::ValueRefT<Real> m_interval;
363  agxData::ValueRefT<Bool> m_repeating;
364  agxData::ValueRefT<Bool> m_enabled;
365 
366  // CallbackVector m_callbacks;
367  Callback m_mainCallback;
368  Real m_subInterval; // Accumulator
369  UInt m_scheduledFrame;
370  agxData::Value::Event::CallbackType m_valueCallback;
371  };
372 
373 
375 
378  {
379  public:
380  Frame() {}
381  Frame(UInt frameId) : id(frameId), eventList(nullptr) {}
384  // Real m_time;
385  };
386 
387 
390  {
391  public:
392  FrameWrapper(Clock *clock);
393  void addFrameTask(Task *child);
394 
395  void execute();
396  Clock* getClock();
397 
398  protected:
399  virtual ~FrameWrapper();
400 
401  private:
402  Clock *m_clock;
403  TaskRefVector m_children;
404  };
405 
407 
409  /* Implementation */
410  AGX_FORCE_INLINE Clock *Clock::getReference() { return m_reference; }
411  AGX_FORCE_INLINE const ClockPtrVector& Clock::getSlaves() const { return m_slaves; }
412 
413  AGX_FORCE_INLINE bool Clock::isEnabled() const { return m_enabled->get(); }
414  AGX_FORCE_INLINE Real Clock::getTime() const { return agx::Real(m_time->get()); }
415  AGX_FORCE_INLINE Real Clock::getFrequency() const { return m_frequency->get(); }
416  AGX_FORCE_INLINE Real Clock::getTimeModulation() const { return m_timeModulation->get(); }
417  AGX_FORCE_INLINE Real Clock::getTimeStep() const { return m_timeStep->get(); }
418  AGX_FORCE_INLINE UInt Clock::getFrame() const { return m_frame->get(); }
419  AGX_FORCE_INLINE const Path& Clock::getBindPath() const { return m_bindPath; }
420 
421  AGX_FORCE_INLINE Real Clock::Event::getStartDelay() const { return m_startDelay->get(); }
422  AGX_FORCE_INLINE Real Clock::Event::getInterval() const { return m_interval->get(); }
423  AGX_FORCE_INLINE bool Clock::Event::isRepeating() const { return m_interval->get() >= 0; }
424  AGX_FORCE_INLINE Clock *Clock::Event::getClock() { return m_clock; }
425  AGX_FORCE_INLINE size_t Clock::Event::getIndex() const { return m_index; }
427 
428  // AGX_FORCE_INLINE const CallbackVector& Clock::Event::getCallbacks() const { return m_callbacks; }
429 }
430 
431 #endif /* _AGX_CLOCK_H_ */
Representation of a path, a list of name components.
Definition: Path.h:32
double Real64
Definition: Real.h:45
TickEvent preTick
Definition: Clock.h:55
agx::UInt getFrame() const
Internal class.
Definition: Clock.h:377
bool Bool
Definition: Integer.h:48
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
Abstract representation of a value.
Definition: Value.h:48
agx::Real getTimeModulation() const
An abstract job/workblock representation, which allows work threads to execute arbitrary tasks...
Definition: Job.h:62
Generalized callback, using std::function.
Definition: Callback.h:53
agx::Bool isEnabled() const
Event * eventList
Definition: Clock.h:383
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
A representation of a generic task.
Definition: Task.h:57
const agx::ClockPtrVector & getSlaves() const
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
Internal class.
Definition: Clock.h:389
agx::Clock * getReference()
TickEvent postTick
Definition: Clock.h:56
uint32_t UInt
Definition: Integer.h:34
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Templated callback with one argument.
Definition: Callback.h:100
TaskGroup.
Definition: Task.h:585
agx::Real getTimeStep() const
agx::Real getFrequency() const
Frame(UInt frameId)
Definition: Clock.h:381
agx::Real getTime() const
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:37
const agx::Path & getBindPath() const
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
Inheritance with partial specialization due to bug with ref_ptr containers.
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
An event with no arguments.
Definition: Event.h:89
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
Event1< Clock * > TickEvent
Definition: Clock.h:54
#define AGXCORE_EXPORT
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97