AGX Dynamics  2.35.0.1
Clock.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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:
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:
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 
93 
97  void setEnable(agx::Bool flag);
98 
103 
107  void setFrequency(agx::Real frequency);
108 
113 
118 
123 
128 
132  void setTimeModulation(agx::Real modulation);
133 
139 
145 
150  void addFrameJob(agx::Job *job);
151 
157 
162  void addFrameTask(agx::Task *task);
163 
169 
174  void addSlave(agx::Clock *slave);
175 
179  void setFrame(UInt frame);
180 
185  void removeSlave(agx::Clock *slave);
186 
191 
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 
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:
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 
291 
297 
298 
303 
308  void setInterval(agx::Real interval);
309 
310 
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:
393  void addFrameTask(Task *child);
394 
395  void execute();
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_ */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXCORE_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
const T & get() const
Definition: Value.h:448
Abstract representation of a value.
Definition: Value.h:43
Generalized callback, using std::function.
Definition: Callback.h:54
Internal class.
Definition: Clock.h:390
FrameWrapper(Clock *clock)
void addFrameTask(Task *child)
Internal class.
Definition: Clock.h:378
Frame(UInt frameId)
Definition: Clock.h:381
Event * eventList
Definition: Clock.h:383
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:38
void removeEvent(agx::Clock::Event *event)
Remove an event from the clock.
static Clock::Event * load(TiXmlElement *eEvent, Device *device)
agx::Real getFrequency() const
agx::Real getInterval() const
Get the trigger interval.
static agx::Model * ClassModel()
void setInterval(agx::Real interval)
Set the trigger interval.
void stepTo(agx::Real time)
Advance the clock to a specified time.
virtual ~Event()
bool isRepeating() const
TickEvent postTick
Definition: Clock.h:56
agx::Clock * getClock()
Event(const agx::Callback &callback, agx::Real startDelay, agx::Real interval=-1.0)
virtual void configure(TiXmlElement *eClock) override
void setEnable(bool flag)
Pause/unpause.
void reset()
Reset to frame zero (and time=0), remove all events.
virtual void rebind() override
Event(const agx::Name &name="Event")
Constructor.
void addSlave(agx::Clock *slave)
Add a slave clock, to be driven by this clock.
void setFrequency(agx::Real frequency)
Set the clock frequency.
agx::Real getTimeStep() const
void removeFrameJob(agx::Job *job)
Remove a frame job.
virtual void configure(TiXmlElement *eEvent) override
void removeSlave(agx::Clock *slave)
Remove a slave clock.
void start()
Same as setEnable(true)
void removeFrameTask(agx::Task *task)
Remove a frame task.
void addFrameTask(agx::Task *task)
Add a task to be executed on each tick.
void setTime(agx::Real64 time)
Set the time explicitly, without ticking.
agx::Real getStartDelay() const
bool getIsThreadTimelineResponsible() const
agx::Real getTime() const
void setTimeModulation(agx::Real modulation)
Set the time modulation of the clock, default is 1.0.
void stop()
Same as setEnable(false)
void tick(bool blocking=true)
Advance the clock to the next time step.
virtual void save(TiXmlElement *eParent) const override
void setStartDelay(agx::Real time)
Set the start delay when added to a clock or reset.
void setIsThreadTimelineResponsible(bool isResponsible)
Control whether this Clock is the main clock for thread timeline generation.
virtual ~Clock()
Event1< Clock * > TickEvent
Definition: Clock.h:54
bool setTimeStep(agx::Real dt)
Set the time step of the clock, 1/frequency.
void setEnable(agx::Bool flag)
Set to false to disable the clock, tick will be void operation.
void setRepeating(bool flag)
Set if event trigger multiple times.
const agx::Path & getBindPath() const
Clock(const agx::Name &name="clock", agx::Real frequency=60)
void setFrame(UInt frame)
Explicitly set the current frame index.
void addEvent(agx::Clock::Event *event)
Add an event to the clock to be triggered as specified by the event.
size_t getIndex() const
agx::Real getTimeModulation() const
TickEvent preTick
Definition: Clock.h:55
const agx::ClockPtrVector & getSlaves() const
void bind(const agx::Path &path)
Bind the clock to a reference clock using a bindpath.
agx::Bool isEnabled() const
void stepForward(agx::Real delta)
Advance the clock a specified interval.
agx::Clock * getReference()
agx::UInt getFrame() const
static Clock * load(TiXmlElement *eClock, Device *device)
void addFrameJob(agx::Job *job)
Add a job to be executed on each tick.
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:39
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:58
T CallbackType
Definition: Event.h:37
An event with no arguments.
Definition: Event.h:90
An abstract job/workblock representation, which allows work threads to execute arbitrary tasks.
Definition: Job.h:63
Inheritance with partial specialization due to bug with ref_ptr containers.
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:41
Representation of a name string.
Definition: Name.h:33
Representation of a path, a list of name components.
Definition: Path.h:34
TaskGroup.
Definition: Task.h:586
A representation of a generic task.
Definition: Task.h:58
T * get() const
Definition: ref_ptr.h:256
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
double Real64
Definition: Real.h:45
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42