AGX Dynamics  2.37.2.0
SimulationTrack.FrameWriter.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 
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  SimulationFrameWriter(Simulation *simulation, bool passive = false);
43 
44  virtual void writeFrame(const agxData::Frame *frame) override;
45 
46  virtual void forceFlush() override;
47 
48  agx::Task *getUpdateTask() { return m_updateTask; }
49 
50  void setBlocking(bool blocking) { m_blocking = blocking; }
51 
52  // Bypass thread synchronization
53  void writeFrameImpl(const agxData::Frame *frame);
54 
55 
56  protected:
57  virtual ~SimulationFrameWriter();
58 
59  private:
60  void writeIncrementalArchive(const agxData::Buffer *archiveData, agx::UInt64 skipFrame, agxSDK::Assembly *assembly);
61  void updateCallback();
62  void write(const agx::Component *component, const agxData::Frame *frame);
63  void write(const agxData::Buffer *buffer, const agxData::Frame *frame);
64  void write(const agxData::Frame::PartialBuffer *buffer, const agxData::Frame *frame);
65  void write(const agxData::Frame::EntityStorage *storage, const agxData::Frame *frame);
66  void write(const agxData::Value *value, const agxData::Frame *frame);
67  void evaluateEventsPre(const agxData::Frame *frame);
68  void evaluateEventsPost(const agxData::Frame *frame);
69  void writeConstraintData(const agxData::Frame *frame);
70  bool writeFrameArchive(const agxData::Frame *frame);
71  agx::UInt64 writeKeyFrameArchive(const agxData::Buffer* keyFrameArchive, agxSDK::Assembly* renderAssembly );
72  void updateArrayBuffers();
73 
74  void debugPrint(const agxData::Frame *frame);
75  void debugPrint(const agx::Component *component, const agxData::Frame *frame);
76  void debugPrint(const agxData::Frame::PartialBuffer *buffer, const agxData::Frame *frame);
77  void debugPrint(const agxData::Buffer *buffer, const agxData::Frame *frame);
78  void debugPrint(const agxData::EntityStorage *storage, const agxData::Frame *frame);
79  void debugPrint(const agxData::Value *value, const agxData::Frame *frame);
80 
81  void setClock();
82 
83  void handleGeometryRestore(agxCollide::Geometry* geometry);
84  void storeAndReplaceMaterialPtr(agxCollide::Geometry* geometry);
85  void storeTrimeshShallowCopyRefs(agxCollide::Geometry* geometry);
86 
87  template <typename T>
88  T *getTarget(const Object *source);
89 
90  template <typename T>
91  T *getTarget(agx::UInt32 id);
92 
93  template <typename T>
94  agxData::EntityStorage *getEventStorage(T *event);
95 
96  struct ArchiveObjectEntry {
97  agx::UInt32 id;
98  agxStream::Serializable* serializablePtr;
99  };
100 
101  class IncrementalArchive : public agx::Observer
102  {
103  public:
104  IncrementalArchive();
105  virtual ~IncrementalArchive();
106 
107  agxStream::InputArchive& createArchive(const agxData::Buffer *archiveData, bool binary = true);
108  // void attach(agxStream::OutputArchive *target);
109 
110  std::stringstream& getStream();
111  void clear();
112  void detach();
113 
114  private:
115  virtual void objectDeleted(void *target);
116  void restoreCallback(agx::UInt32 id, agxStream::Serializable *object, const std::string& className);
117 
118  private:
119  std::unique_ptr<std::stringstream> m_stream;
125  agx::Vector<agx::UInt32> m_nonRef;
126  };
127 
129  agxData::Buffer *getBuffer(agxData::Buffer *encodedPtr);
130  void rebuildObjectTable(const agx::Object *node);
131 
132  private:
133  agx::observer_ptr<Simulation> m_simulation;
134  bool m_blocking;
135 
136  agx::UInt m_frameIndex;
137  agx::Real m_timeStamp;
138  agx::Real m_timeStep;
139  bool m_needClockUpdate;
140 
141  agx::TaskGroupRef m_updateTask;
142  bool m_forceFlush;
143  agx::Clock::TickEvent::CallbackType m_postTickCallback;
144 
146  ObjectIdTable m_objectIdTable;
147  agxData::BufferRefVector m_arrayBuffers;
148  bool m_passive;
149  IncrementalArchive m_incrementalArchive;
150  agx::HashSet<agx::MaterialRef> m_incrementalJournalMaterials;
151  agx::HashSet<agxCollide::ShapeConstRef> m_uniqueTrimeshes;
152  agx::HashSet<agxCollide::RenderDataRef> m_uniqueRenderData;
153  };
154 
155 }
AGXTERRAIN_EXPORT void clear()
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
Data storage for a collection of entity instances of a specified EntityModel.
Definition: EntityStorage.h:72
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
Abstract representation of a value.
Definition: Value.h:48
This namespace consists of a set of classes for handling geometric intersection tests including boole...
#define AGXPHYSICS_EXPORT
A representation of a generic task.
Definition: Task.h:57
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
The FrameWriter writes frames to an external target.
Definition: FrameIO.h:168
uint32_t UInt
Definition: Integer.h:34
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
Templated callback with one argument.
Definition: Callback.h:100
Templated callback with three arguments.
Definition: Callback.h:181
Abstract representation of a data buffer.
Definition: Buffer.h:55
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
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
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
double Real
Definition: Real.h:42
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_DECLARE_POINTER_TYPES(SpaceListener)
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:66
agx::Object is a refcounted object with a name.
Definition: Object.h:58