AGX Dynamics  2.37.2.0
ata/Frame.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 #ifndef AGXDATA_FRAME_H
18 #define AGXDATA_FRAME_H
19 
20 #include <agx/Device.h>
21 #include <agx/Component.h>
22 #include <agx/Clock.h>
23 
24 #ifdef _MSC_VER
25 #pragma warning( push )
26 #pragma warning( disable : 4275 ) // non dll-interface class 'std::runtime_error' used as base for dll-interface class 'agxData::Frame::MergeException'
27 #pragma warning( disable : 4290 ) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
28 #endif
29 
30 
32 
33 namespace agxData
34 {
35  class Track;
36 
39  class AGXCORE_EXPORT Frame : public agx::Component
40  {
41  public:
42  static agx::Model *ClassModel();
43 
44  public:
45  AGX_DECLARE_POINTER_TYPES(DataBinding);
46  AGX_DECLARE_VECTOR_TYPES(DataBinding);
47 
48  AGX_DECLARE_POINTER_TYPES(PartialBuffer);
49  AGX_DECLARE_VECTOR_TYPES(PartialBuffer);
50  AGX_DECLARE_POINTER_TYPES(EntityStorage);
51  AGX_DECLARE_VECTOR_TYPES(EntityStorage);
52 
53  public:
54  struct Header
55  {
56  Header() : index(agx::InvalidIndex), timeStep(-1), timeStamp(-1), id(agx::InvalidIndex), isKeyFrame(false) {}
57  agx::UInt index;
58  agx::Real timeStep;
59  agx::Real timeStamp;
60  agx::UInt32 id;
61  bool isKeyFrame;
62  };
63 
64  public:
65  Frame(agx::UInt index, agx::Real timeStamp, agx::Real timeStep);
66  Frame(agx::Clock *clock);
67 
71  Header getHeader() const;
72 
76  void setKeyFrame(bool flag);
77 
81  bool isKeyFrame() const;
82 
86  agx::UInt getIndex() const;
87 
91  void setIndex(agx::UInt index);
92 
96  agx::Real getTimeStamp() const;
97 
101  void setTimeStamp(agx::Real timeStamp);
102 
106  agx::Real getTimeStep() const;
107 
111  void setTimeStep(agx::Real dt);
112 
116  agx::UInt getNumBytes() const;
117 
121  Track *getTrack();
122  const Track *getTrack() const;
123 
124  class AGXCORE_EXPORT MergeException : public std::runtime_error
125  {
126  public:
127  MergeException(const Object* node, const Object* collisionNode);
128 
129  const Object *getNode() const;
130  const Object *getCollisionNode() const;
131 
132  private:
133  const Object *m_node;
134  const Object *m_collisionNode;
135  };
136 
142  void merge(const Frame *other, bool allowOverwrite = false);
143 
144  // Alias for frame->getResource<agxData::Buffer>(path);
145  Buffer *getBuffer(const agx::Path& path);
146 
147  void addStorageMetaData(agxData::EntityStorage *storage, const agx::Path& path);
148  void addComponent(Component *component, const agx::Path& path);
149  void addStorage(EntityStorage *storage, const agx::Path& path);
150  void addBuffer(Buffer *buffer, const agx::Path& path);
151  void addBuffer(PartialBuffer *buffer, const agx::Path& path);
152  void addValue(agxData::Value *value, const agx::Path& path);
153 
154  // Used for tracking attributes of individual entity instances
155  void addInstanceAttribute(agxData::EntityPtr instance, agxData::Attribute *attribute);
156 
157 
158  // TODO: Should always be handled automatically by frame?
159  void setNumBytes(agx::UInt numBytes);
160 
161 
162  void addEvent(agx::Object::Event *event);
163  void addEvents(const agx::Object::EventRefVector& events);
164  agx::Object::EventRefVector& getEvents();
165  const agx::Object::EventRefVector& getEvents() const;
166 
167  void evaluateEventsLocally();
168  bool isQueued() const;
169  bool isDiscardedFromCache() const;
170  bool isJumpFrame() const;
171  bool isSerialized() const;
172 
173  void setIncrementalFrameArchiveBuffer(agxData::Buffer *buffer);
174  const agxData::Buffer *getIncrementalFrameArchiveBuffer() const;
175  agxData::Buffer *getIncrementalFrameArchiveBuffer();
176 
177 
178  protected:
179  virtual ~Frame();
180 
181  private:
182  friend class Track;
183  void setTrack(Track *track);
184 
185  void setQueued(bool flag);
186 
187  void setDiscardedFromCache(bool flag);
188 
189  void setJumpFrame(bool flag);
190 
191  void setSerialized(bool flag);
192 
193  agx::Component *getContext(const agx::Path& path);
194 
195 
196  Frame::EntityStorage *getStorageWithId(agx::Component *node, agx::UInt32 id);
197 
199  void buildFrameMap(agx::Component *node, FrameMap& map, const Frame *root);
200 
201 
202  void merge(agx::Component *targetParent, const agxData::Value *value, bool allowOverwrite);
203  void merge(agx::Component *targetParent, const agxData::Buffer *buffer, bool allowOverwrite);
204  void merge(agx::Component *targetParent, const PartialBuffer *buffer, bool allowOverwrite);
205  void merge(agx::Component *targetParent, const EntityStorage *storage, bool allowOverwrite);
206  void merge(agx::Component *targetParent, const agx::Component *component, bool allowOverwrite);
207  void merge(agx::Component *targetParent, const agx::Object *node, bool allowOverwrite);
208  void merge(agx::Component *targetParent, const agx::ObjectRefVector& objects, bool allowOverwrite);
209 
210  void addNode(Component *parent, Object *object);
211  void removeNode(Object *object);
212  static size_t calculateNumBytes(Object *node);
213 
214  private:
215  Track *m_track;
216  bool m_isQueued;
217  bool m_isDiscardedFromCache;
218  bool m_isJumpFrame;
219  bool m_isKeyFrame;
220  bool m_serialized;
221  agx::UInt m_index;
222  agx::Real m_timeStamp;
223  agx::Real m_timeStep;
224  agx::UInt m_numBytes;
226  agxData::BufferRef m_incrementalFrameBuffer;
227  const agxData::Frame* m_mergeFrame;
228 
229  // typedef HashTable<agx::Path, Buffer *> BufferTable;
230  // BufferTable m_bufferTable;
231  };
232 
233 
234  class AGXCORE_EXPORT Frame::DataBinding : public agx::Referenced
235  {
236  public:
237  DataBinding(const agx::Path& path);
238  DataBinding(const agx::Path& internalPath, const agx::Path& externalPath);
239 
240  // TODO Specify format transformations in binding?
241  // DataBinding(const agx::Path& internalPath, const Format *internalFormat, const agx::Path& externalPath, const Format *externalFormat);
242 
246  const agx::Path& internalPath() const;
247 
251  const agx::Path& externalPath() const;
252 
254  bool isRecursive() const;
255 
257  void setRecursive(bool flag);
258 
259  protected:
260  virtual ~DataBinding() {}
261 
262  private:
263  agx::Path m_internalPath;
264  agx::Path m_externalPath;
265  bool m_isRecursive;
266  };
267 
268  class AGXCORE_EXPORT Frame::PartialBuffer : public agx::Component
269  {
270  public:
271  static agx::Model *ClassModel();
272 
273  public:
274  // PartialBuffer();
275  PartialBuffer(const agx::Name& name, agxData::Format *dataFormat, size_t size, size_t fullSize);
276  PartialBuffer(const agx::Name& name, size_t size, size_t fullSize);
277  PartialBuffer(const agx::Name& name, agxData::Buffer *indexBuffer, agxData::Buffer *dataBuffer, size_t fullSize);
278 
279  bool empty() const;
280  size_t size() const;
281  size_t fullSize() const;
282  // size_t fullCapacity() const;
283 
284 
285  void resize(size_t size);
286  const agxData::Format *getFormat() const;
287 
288  agxData::Buffer *getDataBuffer();
289  const agxData::Buffer *getDataBuffer() const;
290 
291  agxData::Buffer *getIndexBuffer();
292  const agxData::Buffer *getIndexBuffer() const;
293 
294  void setDataBuffer(agxData::Buffer *buffer);
295 
296  size_t getNumBytes() const;
297 
298  void merge(const PartialBuffer *other, bool allowOverwrite);
299 
300  PartialBuffer *clone() const;
301 
302  protected:
303  virtual ~PartialBuffer();
304 
305  private:
306  agxData::BufferRef m_indexBuffer;
307  agxData::BufferRef m_dataBuffer;
308  size_t m_fullSize;
309  // size_t m_fullCapacity;
310  };
311 
312  class AGXCORE_EXPORT Frame::EntityStorage : public agx::Component
313  {
314  public:
315  static agx::Model *ClassModel();
316 
317  public:
318  EntityStorage(agxData::EntityStorage *storage);
319  EntityStorage(const agx::Name& name, agxData::EntityModel *model, size_t size, size_t capacity);
320 
321  EntityModel *getModel();
322  const EntityModel *getModel() const;
323 
324  bool empty() const;
325  size_t size() const;
326  size_t capacity() const;
327 
328  // Explicitly set size parameter
329  void setSize(size_t size);
330  void setCapacity(size_t capacity);
331 
332  virtual void addObject(agx::Object *object, bool assignContext = true);
333  virtual void removeObject(agx::Object *object);
334 
335 
336  // EntityStorage semantics for evaluating events locally during frame merge
337  void resize(size_t size);
338  void reserve(size_t capacity);
339  void destroyInstance(agx::Index id);
340 
341  protected:
342  virtual ~EntityStorage();
343 
344  private:
345  agxData::EntityModelRef m_model;
346  size_t m_size;
347  size_t m_capacity;
348  agxData::Buffer *m_idToIndexBuffer;
349  agxData::Buffer *m_indexToIdBuffer;
350  typedef agx::HashSet<Buffer *> BufferHash;
351  BufferHash m_attributeBuffers;
352  // PartialBufferPtrVector m_partialBuffers;
353  };
354 
355 
356 
357  AGXCORE_EXPORT std::ostream& operator<< ( std::ostream& stream, const agxData::Frame::Header& header );
358 
359  //---------------------------------------------------------------
360 
361 
362 
363  /* Implementation */
364  AGX_FORCE_INLINE Track *Frame::getTrack() { return m_track; }
365  AGX_FORCE_INLINE const Track *Frame::getTrack() const { return m_track; }
366 
367  AGX_FORCE_INLINE bool Frame::isQueued() const { return m_isQueued; }
368  AGX_FORCE_INLINE bool Frame::isJumpFrame() const { return m_isJumpFrame; }
369  AGX_FORCE_INLINE bool Frame::isDiscardedFromCache() const { return m_isDiscardedFromCache; }
370 
371  AGX_FORCE_INLINE bool Frame::isKeyFrame() const { return m_isKeyFrame; }
372  AGX_FORCE_INLINE agx::UInt Frame::getIndex() const { return m_index; }
373  AGX_FORCE_INLINE agx::Real Frame::getTimeStamp() const { return m_timeStamp; }
374  AGX_FORCE_INLINE agx::Real Frame::getTimeStep() const { return m_timeStep; }
375  AGX_FORCE_INLINE agx::UInt Frame::getNumBytes() const { return m_numBytes; }
376 
377  AGX_FORCE_INLINE const agx::Object::EventRefVector& Frame::getEvents() const { return m_events; }
378  AGX_FORCE_INLINE agx::Object::EventRefVector& Frame::getEvents() { return m_events; }
379 
380  //---------------------------------------------------------------
381 
382  AGX_FORCE_INLINE bool Frame::PartialBuffer::empty() const { return size() == 0; }
383  AGX_FORCE_INLINE size_t Frame::PartialBuffer::size() const { return m_dataBuffer ? m_dataBuffer->size() : 0; }
384  AGX_FORCE_INLINE size_t Frame::PartialBuffer::fullSize() const { return m_fullSize; }
385  // AGX_FORCE_INLINE size_t Frame::PartialBuffer::fullCapacity() const { return m_fullCapacity; }
386 
387  AGX_FORCE_INLINE const agxData::Format *Frame::PartialBuffer::getFormat() const { return m_dataBuffer ? m_dataBuffer->getFormat() : 0; }
388 
389  AGX_FORCE_INLINE agxData::Buffer *Frame::PartialBuffer::getDataBuffer() { return m_dataBuffer; }
390  AGX_FORCE_INLINE const agxData::Buffer *Frame::PartialBuffer::getDataBuffer() const { return m_dataBuffer; }
391  AGX_FORCE_INLINE agxData::Buffer *Frame::PartialBuffer::getIndexBuffer() { return m_indexBuffer; }
392  AGX_FORCE_INLINE const agxData::Buffer *Frame::PartialBuffer::getIndexBuffer() const { return m_indexBuffer; }
393 
394  //---------------------------------------------------------------
395 
396  AGX_FORCE_INLINE EntityModel *Frame::EntityStorage::getModel() { return m_model; }
397  AGX_FORCE_INLINE const EntityModel *Frame::EntityStorage::getModel() const { return m_model; }
398  AGX_FORCE_INLINE bool Frame::EntityStorage::empty() const { return size() == 0; }
399  AGX_FORCE_INLINE size_t Frame::EntityStorage::size() const { return m_size; }
400  AGX_FORCE_INLINE size_t Frame::EntityStorage::capacity() const { return m_capacity; }
401 
402 }
403 
405 
406 #ifdef _MSC_VER
407 #pragma warning( pop )
408 #endif
409 
410 #endif /* AGXDATA_FRAME_H */
Representation of a path, a list of name components.
Definition: Path.h:32
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
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
AGXCORE_EXPORT Format * getFormat(const agx::String &typeFormatName)
AGXCORE_EXPORT Model * getModel(const Path &path, const Path &_namespace=Path())
Definition: Object.h:363
#define m_size
Definition: Vector.h:429
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
Definition: EntityPtr.h:51
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
A format is an implementation of a agxData::Type.
Definition: Format.h:63
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
Base class providing referencing counted objects.
Definition: Referenced.h:119
uint32_t UInt
Definition: Integer.h:34
std::ostream & operator<<(std::ostream &o, const agx::Vec6 &v)
Definition: Vec6.h:230
const Node * getNode(const IteratorCompatibleContainer &container, agx::Real distanceFromStart)
Finds the wire node given current distance from the start of the wire (wire defined to be iterator co...
Definition: WireUtils.h:392
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
An abstract attribute description, part of an Entity.
Definition: Attribute.h:57
Abstract representation of a data buffer.
Definition: Buffer.h:55
Contains classes for low level data storage for AGX.
Definition: Container.h:23
UInt32 Index
Definition: Integer.h:52
An abstract description of a data entity stored using SOA (structure of arrays) pattern in a EntitySt...
Definition: EntityModel.h:63
static agx::Model * ClassModel()
Clock class keeps representation of virtual/simulated time.
Definition: Clock.h:37
#define m_capacity
Definition: Vector.h:430
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
AGXCORE_EXPORT Component * root()
Inheritance with partial specialization due to bug with ref_ptr containers.
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define AGXCORE_EXPORT
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
agx::Object is a refcounted object with a name.
Definition: Object.h:58