AGXDynamics  2.31.1.0
SimulationTrack.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 
17 #ifndef AGXSDK_SIMULATIONTRACK_H
18 #define AGXSDK_SIMULATIONTRACK_H
19 
20 #include <agxData/Frame.h>
21 #include <agxData/FrameIO.h>
22 #include <memory>
23 #include <agx/DynamicBitSet.h>
24 #include <agx/Statistics.h>
25 #include <agx/TaskTimingEntity.h>
26 #include <agxSDK/Simulation.h>
28 #include <agxStream/InputArchive.h>
29 
30 namespace agxSDK
31 {
34  {
35  public:
36  SimulationFrameReader(Simulation *simulation, bool passive = false);
37 
38 
39  virtual agxData::Frame *readFrame() override;
40 
45  agxData::Frame *generateKeyFrame();
46 
47  void start();
48  void stop();
49  void disconnect();
50 
51  void setReadFrameOnNextPreTick(bool flag);
52 
53  void forcePushCurrentFrame();
54 
56  void setKeyFrameInterval(agx::UInt interval);
57 
58  void setStoreConstraintData(bool flag);
59  bool getStoreConstraintData() const;
60 
62  agx::UInt getKeyFrameInterval() const;
63 
64  virtual agx::Real getTimeStep() override;
65  virtual agx::UInt getFrameStride() override;
66 
70  void setEnableIncrementalStructure(bool flag);
71 
75  bool getEnableIncrementalStructure() const;
76 
77 
78  protected:
79  void tryPushCurrentFrameToTrack();
80 
81  virtual ~SimulationFrameReader();
82 
83  private:
86 
87 
88  class ValueListener : public agxData::Value::EventListener
89  {
90  public:
91  ValueListener(SimulationFrameReader *reader);
92 
93  virtual void destroyCallback(agxData::Value* value) override;
94  virtual void updateCallback(agxData::Value* value) override;
95 
96  private:
97  SimulationFrameReader *reader();
98  };
99 
100  class BufferListener : public agxData::Buffer::EventListener
101  {
102  public:
103  BufferListener(SimulationFrameReader *reader);
104 
105  virtual void destroyCallback(agxData::Buffer* buffer) override;
106  virtual void resizeCallback(agxData::Buffer* buffer, agx::Index size, agx::Index oldSize) override;
107  virtual void updateCallback(agxData::Buffer* buffer) override;
108  virtual void updateCallback(agxData::Buffer* buffer, agx::Index index) override;
109  virtual void updateCallback(agxData::Buffer* buffer, agx::IndexRange range) override;
110  virtual void updateCallback(agxData::Buffer* buffer, agxData::Array< agx::Index > indexSet) override;
111 
112  private:
113  SimulationFrameReader *reader();
114  };
115 
116  class StorageListener : public agxData::EntityStorage::EventListener
117  {
118  public:
119  StorageListener(SimulationFrameReader *reader);
120 
121  virtual void destroyCallback(agxData::EntityStorage* storage) override;
122  virtual void createInstanceCallback(agxData::EntityStorage* storage, agxData::EntityPtr instance) override;
123  virtual void createInstancesCallback(agxData::EntityStorage* storage, agxData::EntityRange instances) override;
124  virtual void createInstancesCallback(agxData::EntityStorage* storage, agxData::Array<agxData::EntityPtr> instances) override;
125  virtual void destroyInstanceCallback(agxData::EntityStorage* storage, agxData::EntityPtr instance) override;
126  virtual void destroyInstancesCallback(agxData::EntityStorage* storage, agxData::EntityRange instances) override;
127  virtual void destroyInstancesCallback(agxData::EntityStorage* storage, agxData::Array<agxData::EntityPtr> instances) override;
128  virtual void permuteCallback(agxData::EntityStorage* storage, agxData::Array< agx::Index > permutation) override;
129 
130  private:
131  SimulationFrameReader *reader();
132  };
133 
134  class IncrementalArchive : public agx::Observer
135  {
136  public:
137  IncrementalArchive();
138  virtual ~IncrementalArchive();
139 
140  agxStream::OutputArchive& createArchive(bool binary);
141  // void attach(agxStream::OutputArchive *target);
142 
143  std::stringstream& getStream();
144  void clear();
145  void detach();
146 
147  private:
148  virtual void objectDeleted(void *target);
149  void storeCallback(const agxStream::Serializable *object, agx::UInt32 id);
150 
151  private:
152  std::unique_ptr<std::stringstream> m_stream;
158  };
159 
160 
161  virtual agxData::Frame::DataBinding *createDataBinding(const agx::Path& internalPath, const agx::Path& externalPath) override;
162 
163  friend class agx::Journal;
164  void preTickCallback(agx::Clock *clock);
165  void postTickCallback(agx::Clock *clock);
166  void synchronizeConstraintForceData();
167  void postSaveCallback();
168 
169  void addListenerToValue(agxData::Value *value);
170  void addListenerToBuffer(agxData::Buffer *buffer);
171  void addListenerToStorage(agxData::EntityStorage *storage);
172 
173  void addStatisticsToFrame(agxData::Frame *frame);
174  void addStatisticsToFrame(agxData::Frame *frame, agx::Component *frameNode, agx::Statistics::ProviderHandle *providerNode);
175 
176  void addObjectToFrame(agxData::Frame *frame, agx::Object *object, const agx::Path& bindPath, bool resetDirtyFlags, bool recursive);
177  void addValueToFrame(agxData::Frame *frame, agxData::Value *value, const agx::Path& bindPath, bool resetDirtyFlags);
178  void addBufferToFrame(agxData::Frame *frame, agxData::Buffer *buffer, const agx::Path& bindPath, bool resetDirtyFlags);
179  void addStorageToFrame(agxData::Frame *frame, agxData::EntityStorage *storage, const agx::Path& bindPath, bool resetDirtyFlags);
180  void addIndexToIdBufferToFrame(agxData::Frame *frame, agxData::EntityStorage *storage, const agx::Path& bindPath, bool resetDirtyFlags);
181  void initializeBinding(DataBinding *binding, bool doingActualSimulation);
182  void initializeBinding(agx::Object *object, bool doingActualSimulation, bool recursive);
183  agxData::Frame *readFrameImpl(bool isSequenceFrame);
184 
185  void addConstraintDataToFrame( agxData::Frame *frame );
186 
187 
188  agxData::EntityStorage *extractTaskProfilingData(agx::Task *root);
189  void extractTaskProfilingData(agx::Task *task, agx::TaskTimingData& timingData);
190  agxData::Frame::EntityStorage *exportBuffersToFrameStorage(agxData::EntityStorage *storage);
191 
192 
193  private:
194  friend class ValueListener;
195  ValueListener m_valueListener;
196 
197  friend class BufferListener;
198  BufferListener m_bufferListener;
199 
200  friend class StorageListener;
201  StorageListener m_storageListener;
202 
204  DirtyValueTable m_activeValues;
205 
207  DirtyBufferTable m_activeBuffers;
208 
210  StorageTable m_activeStorages;
211 
212  agx::observer_ptr<Simulation> m_simulation;
213  agx::Clock::TickEvent::CallbackType m_preTickCallback;
214  agx::Clock::TickEvent::CallbackType m_postTickCallback;
215  agx::UInt m_frameCount;
216  agx::UInt m_frameToRead;
217  agx::Block m_simBlock;
218  bool m_shutdown;
219  agxData::EntityModelRef m_constraintForceModel;
220  agx::Object::EventRefVector m_events;
221  bool m_saveFrameOnNextPreTick;
222  agx::UInt m_keyFrameInterval;
223  agx::UInt m_framesSinceLastKeyFrame;
224  bool m_storeConstraintData;
225  agx::Real m_nextProfilingTimeStamp;
226  agx::Task::ProfilingMode m_taskProfilingMode;
227  bool m_enableThreadTimeline;
228  bool m_isProfilingFrame;
229  bool m_passive;
230 
231 
233  TaskProfilingSizeTable m_taskProfilingSizeTable;
234 
235  agxData::BufferRefVector m_frameExportBuffers;
236 
237  IncrementalArchive m_incrementalArchive;
238  private:
239  agxData::EntityStorage *extractCustomTaskProfiling();
240  void extractCustomTaskProfiling(agx::TaskTimingReportHandle* node, agx::TaskTimingData& timingData);
241 
242  agx::Component *createFrameArchive(bool isKeyFrame, bool binary = true);
243  agxData::Buffer *createKeyFrameArchive(bool binary);
244 
245  void addRigidBodyCallback(Simulation *, agx::RigidBody *body);
246  void removeRigidBodyCallback(Simulation *, agx::RigidBody *body);
247 
248  void addGeometryCallback(Simulation *, agxCollide::Geometry *geometry);
249  void removeGeometryCallback(Simulation *, agxCollide::Geometry *geometry);
250 
251  void addConstraintCallback(Simulation *, agx::Constraint *constraint);
252  void removeConstraintCallback(Simulation *, agx::Constraint *constraint);
253 
254  Simulation::RigidBodyEvent::CallbackType m_addRigidBodyCallback;
255  Simulation::RigidBodyEvent::CallbackType m_removeRigidBodyCallback;
256 
257  Simulation::ConstraintEvent::CallbackType m_addConstraintCallback;
258  Simulation::ConstraintEvent::CallbackType m_removeConstraintCallback;
259 
260  Simulation::GeometryEvent::CallbackType m_addGeometryCallback;
261  Simulation::GeometryEvent::CallbackType m_removeGeometryCallback;
262 
263  public:
264 
266  {
267  enum Type
268  {
275  GARBAGE_COLLECT
276  };
277 
280  };
281 
282 
283  private:
284  agx::Vector<ArchiveEvent> m_archiveEvents;
285  bool m_enableIncrementalStructure;
286  };
287 
289 
290  class SimulationFrameReader::DataBinding : public agxData::Frame::DataBinding
291  {
292  public:
293  DataBinding(const agx::Path& internalPath, const agx::Path& externalPath, agx::Object *object = nullptr);
294 
295  agx::Object *getObject();
296  void setObject(agx::Object *object);
297 
298  protected:
299  virtual ~DataBinding();
300 
301  private:
302  agx::ObjectObserver m_object;
303  };
304 
306 
307 
309  {
310  public:
311  DirtySet(bool markAllDirty = false);
312 
313  bool empty() const; // Return true if size is 0 and isAllDirty is false
314 
315  size_t size() const;
316 
317  bool isAllDirty() const;
318  void markAllDirty();
319  void reset();
320 
321  void addIndex(agx::Index index);
322  const agx::IndexVector& getIndexList() const;
323 
324  private:
325  bool m_allDirty;
326  agx::IndexVector m_indexList;
327  agx::DynamicBitSet m_bitSet;
328  };
329 
330 
332 
335  {
336  public:
337  SimulationFrameWriter(Simulation *simulation, bool passive = false);
338 
339  virtual void writeFrame(const agxData::Frame *frame) override;
340 
341  virtual void forceFlush() override;
342 
343  agx::Task *getUpdateTask() { return m_updateTask; }
344 
345  void setBlocking(bool blocking) { m_blocking = blocking; }
346 
347  // Bypass thread synchronization
348  void writeFrameImpl(const agxData::Frame *frame);
349 
350 
351  protected:
352  virtual ~SimulationFrameWriter();
353 
354  private:
355  void updateCallback();
356  void write(const agx::Component *component, const agxData::Frame *frame);
357  void write(const agxData::Buffer *buffer, const agxData::Frame *frame);
358  void write(const agxData::Frame::PartialBuffer *buffer, const agxData::Frame *frame);
359  void write(const agxData::Frame::EntityStorage *storage, const agxData::Frame *frame);
360  void write(const agxData::Value *value, const agxData::Frame *frame);
361  void evaluateEventsPre(const agxData::Frame *frame);
362  void evaluateEventsPost(const agxData::Frame *frame);
363  void writeConstraintData(const agxData::Frame *frame);
364  bool writeFrameArchive(const agxData::Frame *frame);
365  agx::UInt64 writeKeyFrameArchive(const agxData::Buffer* keyFrameArchive);
366  void updateArrayBuffers();
367 
368  void setClock();
369 
370  template <typename T>
371  T *getTarget(const Object *source);
372 
373  template <typename T>
374  T *getTarget(agx::UInt32 id);
375 
376  template <typename T>
377  agxData::EntityStorage *getEventStorage(T *event);
378 
379  class IncrementalArchive : public agx::Observer
380  {
381  public:
382  IncrementalArchive();
383  virtual ~IncrementalArchive();
384 
385  agxStream::InputArchive& createArchive(const agxData::Buffer *archiveData, bool binary = true);
386  // void attach(agxStream::OutputArchive *target);
387 
388  std::stringstream& getStream();
389  void clear();
390  void detach();
391 
392  private:
393  virtual void objectDeleted(void *target);
394  void restoreCallback(agx::UInt32 id, agxStream::Serializable *object, const std::string& className);
395 
396  private:
397  std::unique_ptr<std::stringstream> m_stream;
403  agx::Vector<agx::UInt32> m_nonRef;
404  };
405 
407  agxData::Buffer *getBuffer(agxData::Buffer *encodedPtr);
408  void rebuildObjectTable(const agx::Object *node);
409 
410  private:
411  agx::observer_ptr<Simulation> m_simulation;
412  bool m_blocking;
413 
414  agx::UInt m_frameIndex;
415  agx::Real m_timeStamp;
416  agx::Real m_timeStep;
417  bool m_needClockUpdate;
418 
419  agx::TaskGroupRef m_updateTask;
420  bool m_forceFlush;
421  agx::Clock::TickEvent::CallbackType m_postTickCallback;
422 
424  ObjectIdTable m_objectIdTable;
425  agxData::BufferRefVector m_arrayBuffers;
426  bool m_passive;
427  IncrementalArchive m_incrementalArchive;
428  };
429 
430 
431  /* Implementation */
433  AGX_FORCE_INLINE bool SimulationFrameReader::DirtySet::empty() const { return m_indexList.empty() && !m_allDirty; }
434  AGX_FORCE_INLINE size_t SimulationFrameReader::DirtySet::size() const { return m_indexList.size(); }
437 
438  AGX_FORCE_INLINE SimulationFrameReader *SimulationFrameReader::ValueListener::reader() { return this->getOwner<SimulationFrameReader>(); }
439  AGX_FORCE_INLINE SimulationFrameReader *SimulationFrameReader::BufferListener::reader() { return this->getOwner<SimulationFrameReader>(); }
440  AGX_FORCE_INLINE SimulationFrameReader *SimulationFrameReader::StorageListener::reader() { return this->getOwner<SimulationFrameReader>(); }
441 
442 }
443 
444 
445 #endif /* AGXSDK_SIMULATIONTRACK_H */
void setBlocking(bool blocking)
Representation of a path, a list of name components.
Definition: Path.h:33
ProfilingMode
Definition: Task.h:79
AGXTERRAIN_EXPORT void clear()
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:73
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
Abstract representation of a value.
Definition: Value.h:42
The FrameReader reads frames from an external source.
Definition: FrameIO.h:126
#define AGXPHYSICS_EXPORT
The base class for a constraint.
Definition: Constraint.h:86
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
Definition: EntityPtr.h:51
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
A representation of a generic task.
Definition: Task.h:57
#define AGX_FORCE_INLINE
Definition: Object.h:63
agx::UInt getKeyFrameInterval() const
Block synchronization primitive.
Base class providing referencing counted objects.
Definition: Referenced.h:113
The geometry representation used by the collision detection engine.
Definition: Geometry.h:88
The FrameWriter writes frames to an external target.
Definition: FrameIO.h:168
Templated vector class.
Definition: Vector.h:52
uint32_t UInt
Definition: Integer.h:34
friend class ProviderHandle
Definition: Statistics.h:348
Templated callback with one argument.
Definition: Callback.h:100
Templated callback with three arguments.
Definition: Callback.h:181
std::vector< bool > DynamicBitSet
Definition: DynamicBitSet.h:24
Abstract representation of a data buffer.
Definition: Buffer.h:55
agx::ref_ptr< agx::Referenced > object
UInt32 Index
Definition: Integer.h:52
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:52
const agx::IndexVector & getIndexList() const
Templated callback with two arguments.
Definition: Callback.h:139
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
AGXTERRAIN_EXPORT TimingStorage & getStorage()
uint64_t UInt64
Definition: Integer.h:41
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:121
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:37
double Real
Definition: Real.h:42
AGXCORE_EXPORT Component * root()
Inheritance with partial specialization due to bug with ref_ptr containers.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
uint32_t UInt32
Definition: Integer.h:40
agx::Object is a refcounted object with a name.
Definition: Object.h:59