AGX Dynamics  2.35.0.1
SimulationTrack.FrameReader.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 
17 #pragma once
18 
19 #include <agxData/Frame.h>
20 #include <agxData/FrameIO.h>
21 #include <memory>
22 #include <agx/DynamicBitSet.h>
23 #include <agx/Statistics.h>
24 #include <agx/TaskTimingEntity.h>
25 #include <agxSDK/Simulation.h>
27 #include <agxStream/InputArchive.h>
28 
29 namespace agxCollide
30 {
31  class Geometry;
32 }
33 
34 namespace agxSDK
35 {
36  class Assembly;
37 
40  {
41  public:
42  SimulationFrameReader(Simulation *simulation, bool passive = false);
43 
44 
45  virtual agxData::Frame *readFrame() override;
46 
51  agxData::Frame *generateKeyFrame();
52 
53  void start();
54  void stop();
55  void disconnect();
56 
57  void setReadFrameOnNextPreTick(bool flag);
58 
60 
63 
64  void setStoreConstraintData(bool flag);
65  bool getStoreConstraintData() const;
66 
68  agx::UInt getKeyFrameInterval() const;
69 
70  virtual agx::Real getTimeStep() override;
71  virtual agx::UInt getFrameStride() override;
72 
77 
82 
83 
84  protected:
86 
88 
89  private:
92 
93 
94  class ValueListener : public agxData::Value::EventListener
95  {
96  public:
97  ValueListener(SimulationFrameReader *reader);
98 
99  virtual void destroyCallback(agxData::Value* value) override;
100  virtual void updateCallback(agxData::Value* value) override;
101 
102  private:
103  SimulationFrameReader *reader();
104  };
105 
106  class BufferListener : public agxData::Buffer::EventListener
107  {
108  public:
109  BufferListener(SimulationFrameReader *reader);
110 
111  virtual void destroyCallback(agxData::Buffer* buffer) override;
112  virtual void resizeCallback(agxData::Buffer* buffer, agx::Index size, agx::Index oldSize) override;
113  virtual void updateCallback(agxData::Buffer* buffer) override;
114  virtual void updateCallback(agxData::Buffer* buffer, agx::Index index) override;
115  virtual void updateCallback(agxData::Buffer* buffer, agx::IndexRange range) override;
116  virtual void updateCallback(agxData::Buffer* buffer, agxData::Array< agx::Index > indexSet) override;
117 
118  private:
119  SimulationFrameReader *reader();
120  };
121 
122  class StorageListener : public agxData::EntityStorage::EventListener
123  {
124  public:
125  StorageListener(SimulationFrameReader *reader);
126 
127  virtual void destroyCallback(agxData::EntityStorage* storage) override;
128  virtual void createInstanceCallback(agxData::EntityStorage* storage, agxData::EntityPtr instance) override;
129  virtual void createInstancesCallback(agxData::EntityStorage* storage, agxData::EntityRange instances) override;
130  virtual void createInstancesCallback(agxData::EntityStorage* storage, agxData::Array<agxData::EntityPtr> instances) override;
131  virtual void destroyInstanceCallback(agxData::EntityStorage* storage, agxData::EntityPtr instance) override;
132  virtual void destroyInstancesCallback(agxData::EntityStorage* storage, agxData::EntityRange instances) override;
133  virtual void destroyInstancesCallback(agxData::EntityStorage* storage, agxData::Array<agxData::EntityPtr> instances) override;
134  virtual void permuteCallback(agxData::EntityStorage* storage, agxData::Array< agx::Index > permutation) override;
135 
136  private:
137  SimulationFrameReader *reader();
138  };
139 
140  class IncrementalArchive : public agx::Observer
141  {
142  public:
143  IncrementalArchive();
144  virtual ~IncrementalArchive();
145 
146  agxStream::OutputArchive& createArchive(bool binary);
147  // void attach(agxStream::OutputArchive *target);
148 
149  std::stringstream& getStream();
150  void clear();
151  void detach();
152 
153  agx::UInt32 getIdCounter();
154  void setIdCounter(agx::UInt32 id);
155 
156  private:
157  virtual void objectDeleted(void *target);
158  void storeCallback(const agxStream::Serializable *object, agx::UInt32 id);
159 
160  private:
161  std::unique_ptr<std::stringstream> m_stream;
167  };
168 
169 
170  virtual agxData::Frame::DataBinding *createDataBinding(const agx::Path& internalPath, const agx::Path& externalPath) override;
171 
172  friend class agx::Journal;
173  void preTickCallback(agx::Clock *clock);
174  void postTickCallback(agx::Clock *clock);
175  void synchronizeConstraintForceData();
176  void postSaveCallback();
177 
178  void addListenerToValue(agxData::Value *value);
179  void addListenerToBuffer(agxData::Buffer *buffer);
180  void addListenerToStorage(agxData::EntityStorage *storage);
181 
182  void addStatisticsToFrame(agxData::Frame *frame);
183  void addStatisticsToFrame(agxData::Frame *frame, agx::Component *frameNode, agx::Statistics::ProviderHandle *providerNode);
184 
185  void addObjectToFrame(agxData::Frame *frame, agx::Object *object, const agx::Path& bindPath, bool resetDirtyFlags, bool recursive);
186  void addValueToFrame(agxData::Frame *frame, agxData::Value *value, const agx::Path& bindPath, bool resetDirtyFlags);
187  void addBufferToFrame(agxData::Frame *frame, agxData::Buffer *buffer, const agx::Path& bindPath, bool resetDirtyFlags);
188  void addStorageToFrame(agxData::Frame *frame, agxData::EntityStorage *storage, const agx::Path& bindPath, bool resetDirtyFlags);
189  void addIndexToIdBufferToFrame(agxData::Frame *frame, agxData::EntityStorage *storage, const agx::Path& bindPath, bool resetDirtyFlags);
190  void initializeBinding(DataBinding *binding, bool doingActualSimulation);
191  void initializeBinding(agx::Object *object, bool doingActualSimulation, bool recursive);
192  agxData::Frame *readFrameImpl(bool isSequenceFrame);
193 
194  void addConstraintDataToFrame( agxData::Frame *frame );
195 
196 
197  agxData::EntityStorage *extractTaskProfilingData(agx::Task *root);
198  void extractTaskProfilingData(agx::Task *task, agx::TaskTimingData& timingData);
199  agxData::Frame::EntityStorage *exportBuffersToFrameStorage(agxData::EntityStorage *storage);
200 
201 
202  private:
203  friend class ValueListener;
204  ValueListener m_valueListener;
205 
206  friend class BufferListener;
207  BufferListener m_bufferListener;
208 
209  friend class StorageListener;
210  StorageListener m_storageListener;
211 
213  DirtyValueTable m_activeValues;
214 
216  DirtyBufferTable m_activeBuffers;
217 
219  StorageTable m_activeStorages;
220 
221  agx::observer_ptr<Simulation> m_simulation;
222  agx::Clock::TickEvent::CallbackType m_preTickCallback;
223  agx::Clock::TickEvent::CallbackType m_postTickCallback;
224  agx::UInt m_frameCount;
225  agx::UInt m_frameToRead;
226  agx::Block m_simBlock;
227  bool m_shutdown;
228  agxData::EntityModelRef m_constraintForceModel;
230  bool m_saveFrameOnNextPreTick;
231  agx::UInt m_keyFrameInterval;
232  agx::UInt m_framesSinceLastKeyFrame;
233  bool m_storeConstraintData;
234  agx::Real m_nextProfilingTimeStamp;
235  agx::Task::ProfilingMode m_taskProfilingMode;
236  bool m_enableThreadTimeline;
237  bool m_isProfilingFrame;
238  bool m_passive;
239 
240 
242  TaskProfilingSizeTable m_taskProfilingSizeTable;
243 
244  agxData::BufferRefVector m_frameExportBuffers;
245 
246  IncrementalArchive m_incrementalArchive;
247 
248  private:
249  agxData::EntityStorage *extractCustomTaskProfiling();
250  void extractCustomTaskProfiling(agx::TaskTimingReportHandle* node, agx::TaskTimingData& timingData);
251 
252  agx::Component *createFrameArchive(agxData::Frame* frame, bool binary = true);
253  agxData::Buffer *createKeyFrameArchive(bool binary);
254 
255  void addRigidBodyCallback(Simulation *, agx::RigidBody *body);
256  void removeRigidBodyCallback(Simulation *, agx::RigidBody *body);
257 
258  void addGeometryCallback(Simulation *, agxCollide::Geometry *geometry);
259  void removeGeometryCallback(Simulation *, agxCollide::Geometry *geometry);
260 
261  void addConstraintCallback(Simulation *, agx::Constraint *constraint);
262  void removeConstraintCallback(Simulation *, agx::Constraint *constraint);
263 
264  Simulation::RigidBodyEvent::CallbackType m_addRigidBodyCallback;
265  Simulation::RigidBodyEvent::CallbackType m_removeRigidBodyCallback;
266 
267  Simulation::ConstraintEvent::CallbackType m_addConstraintCallback;
268  Simulation::ConstraintEvent::CallbackType m_removeConstraintCallback;
269 
270  Simulation::GeometryEvent::CallbackType m_addGeometryCallback;
271  Simulation::GeometryEvent::CallbackType m_removeGeometryCallback;
272 
273  public:
274 
276  {
277  enum Type
278  {
285  GARBAGE_COLLECT
286  };
287 
290  };
291 
292 
293  private:
294  agx::Vector<ArchiveEvent> m_archiveEvents;
295  bool m_enableIncrementalStructure;
296  };
297 
299 
300  class SimulationFrameReader::DataBinding : public agxData::Frame::DataBinding
301  {
302  public:
303  DataBinding(const agx::Path& internalPath, const agx::Path& externalPath, agx::Object *object = nullptr);
304 
306  void setObject(agx::Object *object);
307 
308  protected:
309  virtual ~DataBinding();
310 
311  private:
312  agx::ObjectObserver m_object;
313  };
314 
316 
317 
319  {
320  public:
321  DirtySet(bool markAllDirty = false);
322 
323  bool empty() const; // Return true if size is 0 and isAllDirty is false
324 
325  size_t size() const;
326 
327  bool isAllDirty() const;
328  void markAllDirty();
329  void reset();
330 
331  void addIndex(agx::Index index);
332  const agx::IndexVector& getIndexList() const;
333 
334  private:
335  bool m_allDirty;
336  agx::IndexVector m_indexList;
337  agx::DynamicBitSet m_bitSet;
338  };
339 
340 
341 
342  /* Implementation */
344  AGX_FORCE_INLINE bool SimulationFrameReader::DirtySet::empty() const { return m_indexList.empty() && !m_allDirty; }
345  AGX_FORCE_INLINE size_t SimulationFrameReader::DirtySet::size() const { return m_indexList.size(); }
348 
349  AGX_FORCE_INLINE SimulationFrameReader *SimulationFrameReader::ValueListener::reader() { return this->getOwner<SimulationFrameReader>(); }
350  AGX_FORCE_INLINE SimulationFrameReader *SimulationFrameReader::BufferListener::reader() { return this->getOwner<SimulationFrameReader>(); }
351  AGX_FORCE_INLINE SimulationFrameReader *SimulationFrameReader::StorageListener::reader() { return this->getOwner<SimulationFrameReader>(); }
352 
353 }
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXPHYSICS_EXPORT
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
Abstract representation of a data buffer.
Definition: Buffer.h:56
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
Definition: EntityPtr.h:52
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:73
The FrameReader reads frames from an external source.
Definition: FrameIO.h:127
Abstract representation of a value.
Definition: Value.h:43
DataBinding(const agx::Path &internalPath, const agx::Path &externalPath, agx::Object *object=nullptr)
DirtySet(bool markAllDirty=false)
agxData::Frame * generateKeyFrame()
Generate an explicit key frame.
virtual agx::Real getTimeStep() override
SimulationFrameReader(Simulation *simulation, bool passive=false)
virtual agx::UInt getFrameStride() override
void setStoreConstraintData(bool flag)
bool getEnableIncrementalStructure() const
virtual agxData::Frame * readFrame() override
Reads the next frame from the stream and moves the stream forward one frame.
void setKeyFrameInterval(agx::UInt interval)
Set keyframe interval.
void setEnableIncrementalStructure(bool flag)
Enable/disable incremental simulation structure recording.
void setReadFrameOnNextPreTick(bool flag)
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:57
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:45
Block synchronization primitive.
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:38
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:39
The base class for a constraint.
Definition: Constraint.h:89
bool empty() const
Definition: Container.h:136
T CallbackType
Definition: Event.h:37
Inheritance with partial specialization due to bug with ref_ptr containers.
agx::Object is a refcounted object with a name.
Definition: Object.h:60
Representation of a path, a list of name components.
Definition: Path.h:34
Base class providing referencing counted objects.
Definition: Referenced.h:114
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
friend class ProviderHandle
Definition: Statistics.h:348
A representation of a generic task.
Definition: Task.h:58
ProfilingMode
Definition: Task.h:80
#define AGX_FORCE_INLINE
Definition: macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
AGXTERRAIN_EXPORT void clear()
uint32_t UInt32
Definition: Integer.h:40
AGXCORE_EXPORT Component * root()
std::vector< bool > DynamicBitSet
Definition: DynamicBitSet.h:24
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
UInt32 Index
Definition: Integer.h:52
This struct is used to handle id-collisions when using incremental journal.
Definition: OutputArchive.h:76