AGX Dynamics  2.37.3.2
EntityStorage.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 <agx/agx.h>
20 #include <agx/agxCore_export.h>
21 #include <agxData/Buffer.h>
22 #include <agxData/EntityModel.h>
25 #include <agxData/LocalVector.h>
26 #include <agx/Vector.h>
27 #include <agx/HashTable.h>
28 #include <agx/Referenced.h>
29 #include <agx/Component.h>
30 
31 
32 #ifdef _MSC_VER
33 # pragma warning(push)
34 # pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer
35 #endif
36 
37 
38 namespace agx
39 {
40  class Device;
41  class TiXmlElement;
42  class GlobalResultStorage;
43  class AliasStorage;
44 }
45 
46 namespace agxSDK
47 {
48  class SimulationFrameWriter;
49 }
50 
51 namespace agxStream
52 {
53  class InputArchive;
54  class OutputArchive;
55 }
56 
57 namespace agxData
58 {
60 
61 
64 
73  {
74  public:
75  static agx::Model *ClassModel();
76 
77  static agxData::EntityStorage *load(agx::TiXmlElement *eStorage, agx::Device *device);
78  virtual void configure(agx::TiXmlElement *eStorage) override;
79 
80  public:
81  // Generated event interface, defined in EntityStorage.agxEvent
83 
84 #ifndef SWIG
85  void addListener(EventListener *listener);
86  void removeListener(EventListener *listener);
87  bool hasListener(EventListener *listener);
88 #endif
91 
92  public:
93  EntityStorage(agxData::EntityModel *entity, size_t initialCapacity = 32);
94  EntityStorage(agxData::EntityModel *entity, const agx::Name& name, size_t initialCapacity = 32);
95 
99  bool empty() const;
100 
104  size_t size() const;
105 
109  size_t capacity() const;
110 
114  size_t getNumBytes() const;
115 
119  agxData::EntityModel *getEntityModel();
120  const agxData::EntityModel *getEntityModel() const;
121 
125  void reserve(size_t size);
126 
130  const agxData::Array<EntityPtr> getInstances() const;
131  agxData::Array<EntityPtr> getInstances();
132 
133  template <typename T>
134  const agxData::Array<T> getInstances() const;
135 
136  template <typename T>
137  agxData::Array<T> getInstances();
138 
143  agxData::EntityPtr createInstance();
144 
149  agxData::EntityRange createInstances(size_t numInstances);
150 
151  void resize(size_t size, bool initializeElements = true);
152 
153  size_t presize(size_t size, size_t initialSize = 0); // Resize without generating events, use with caution
154 
155  void destroyInstance(agxData::EntityPtr instance);
156  void destroyInstance(size_t id);
157 
158  template <typename VectorT>
159  void destroyInstances(const VectorT& instances);
160 
161  void destroyInstances(agxData::EntityRange range);
162 
163 
165  void swapInstances(const InstanceSwapVector &swaps);
166 
168  void swapInstances(const InstanceSwapLocalVector& swaps);
169 
170  // Must be followed by a matching resize...
171  void preDestroy(agxData::EntityPtr instance);
172 
173  agxData::EntityPtr transferInstance(size_t id, agxData::EntityStorage* target);
174  agxData::EntityPtr copyInstance(size_t id, agxData::EntityStorage* target);
175  agxData::EntityPtr copyInstance(size_t id, agxData::EntityStorage* target, size_t targetId);
176 
177  agxData::EntityStorage *clone() const;
178  static void copy(agxData::EntityStorage *target, const agxData::EntityStorage *source);
179 
183  void clear();
184 
188  void reset();
189 
193  // void swap(EntityStorage *other);
194 
195 
199  agxData::EntityStorage *createSharedStorage();
200 
204  agxData::Buffer *getBuffer(const agx::Name& name);
205  agxData::Buffer *getBuffer(const agxData::Attribute *attribute);
206 
207 
211  const agxData::BufferTable& getBuffers() const;
212 
214  agxData::Buffer *getIdToIndexBuffer();
215 
217  const agxData::Buffer *getIdToIndexBuffer() const;
218 
220  agxData::Buffer *getInstanceBuffer();
221 
223  const agxData::Buffer *getInstanceBuffer() const;
224 
228  size_t idToIndex(size_t id) const;
229 
233  size_t indexToId(size_t index) const;
234 
235 
239  agxData::EntityData *getData();
240 
241  template <typename T>
242  T* getData();
243 
244  template <typename T>
245  const T* getData() const;
246 
252  void setUseInstanceTable(bool flag);
253 
257  bool useInstanceTable() const;
258 
262  void addCustomAttribute(agxData::Attribute *attribute);
263 
267  agxData::Buffer *getGlobalOrderBuffer();
268 
272  const agxData::Buffer *getGlobalOrderBuffer() const;
273 
277  agxData::Buffer *getOrCreateGlobalOrderBuffer();
278 
282  void reorder(const agxData::IndexArray& permutation);
283 
284 
288  // void reorder(const IndexArray& permutation, agx::IndexRange range);
289 
290 
291  void print(std::ostream& stream) const;
292  void print() const;
293 
294  void verifyConsistency();
295 
296 
300  template <typename T>
301  T& get(const agx::Name& attributeName, size_t index);
302 
303 
304 
305  void preparePermutation();
306  void commitPermutation(const agxData::IndexArray& permutation);
307  void reorderAttributes(agx::IndexRange32 range, const agxData::IndexArray& permutation);
308 
309  agxData::Byte *getPermutationBuffer(agxData::Attribute *attribute);
310  agxData::Byte *getPermutationBuffer(size_t index);
311 
312 
313  void unregisterBuffer(agxData::Buffer *buffer);
314 
315  agxData::IndexArray& getIdToIndexTable();
316  const agxData::IndexArray& getIdToIndexTable() const;
317 
318 
319  // Moved here from EntityPtr because of include loop, see EntityPtr.h
320  template <typename T>
321  agxData::Array<T> dereferenceMultipliedAttribute(const agxData::EntityPtr& entity, const agxData::ScalarAttributeT<T> *attribute);
322 
323  template <typename T>
324  const agxData::Array<T> dereferenceMultipliedAttribute(const agxData::EntityPtr& entity, const agxData::ScalarAttributeT<T> *attribute) const;
325 
326 
327  static const agx::Name IdToIndexName;
328  static const agx::Name IndexToIdName;
329 
330  // Internal
331  void keyFrameResize(size_t size, size_t capacity);
332 
333  void setEnableEvents(bool flag);
334 
335 
336  void storePermutation(agxStream::OutputArchive& archive) const;
337  void restorePermutation(agxStream::InputArchive& archive, bool skip = false);
338  static void extractPermutation(agx::IndexVector& result, agxStream::InputArchive& archive);
339  void printInstanceTable() const;
340 
341  protected:
342  EntityStorage(const agx::Name& name);
343  EntityStorage(EntityModel *entity, const agx::Name& name, bool isFrameStorage, size_t capacity, size_t size);
344 
345  virtual ~EntityStorage();
346  virtual agx::Object *getResourceImpl(const agx::Path& path, agx::Model *model) override;
347 
348  void explicityRegisterBuffer(agxData::Buffer *buffer, agxData::Attribute *attribute);
349 
350  private:
351  friend class EntityPtr;
352  void swapInstanceIds(agx::Index id1, agx::Index id2);
353 
354  private:
355  friend class EntityData;
356  void beginBatch();
357  void commitBatch(size_t size);
358 
359  private:
360  friend class Buffer;
361  friend class EntityModel;
364  void initializeInstances(const agx::IndexRange& range);
365  void swapInstances(size_t index1, size_t index2);
366  void init(size_t initialCapacity);
367 
368 
369  // Buffer *tryGetBuffer(const Attribute *attribute, Device *device);
370  // Buffer *tryGetBuffer(const String& name, Device* device);
371  void resizeCallback(agxData::Value* value);
372  void reserveCallback(agxData::Value* value);
373  void elementBufferReallocationCallback(Buffer* elementBuffer);
374  agxData::Buffer* createBuffer(const agxData::Attribute* attribute);
375  void registerBuffer(agxData::Buffer *buffer);
376 
377  void sharedResizeCallback(agxData::Value* value);
378  void sharedReserveCallback(agxData::Value* value);
379 
380 
381  void destroyInstances(const EntityPtr* instances, size_t numInstances, bool triggerEvent = true);
382 
389  template <typename T>
390  T& get(const ScalarAttributeT<T> *attribute, size_t index);
391 
392 
393 
394  private:
395 
396  size_t m_size;
397  size_t m_capacity;
398  bool m_useInstanceTable;
399  bool m_implicitResize;
400  bool m_initElements;
401  bool m_usedResizeMethod;
402 
403  EntityModelRef m_entity;
404  BufferTable m_bufferTable;
405  BufferRefVector m_buffers;
406  BufferRef m_instanceBuffer;
407  EntityDataRef m_dataSet;
408 
410  AttributeTable m_customAttributes;
411 
412  agx::VectorPOD<Byte *> m_permutationBuffers;
413 
414  BufferRef m_globalOrderBuffer;
415 
416  agx::Vector<agx::ref_ptr<agx::Referenced> > m_arrayAllocators;
417 
418  BufferRef m_idToIndexBuffer;
419  IndexArray m_idToIndexArray;
420 
421  agxData::Value::Event::CallbackType m_resizeCallback;
422  agxData::Value::Event::CallbackType m_reserveCallback;
423  Buffer::Event::CallbackType m_elementBufferReallocationCallback;
424  EventDispatch m_eventDispatch;
425  size_t m_batchIndex;
426  bool m_enableEvents;
427  };
428 
429  AGXCORE_EXPORT std::ostream& operator << (std::ostream& output, const EntityStorage& storage);
430 
431 
432 
433 
434 
435 
436 
437 
438 
439  /* Implementation */
440 
442  AGX_FORCE_INLINE size_t EntityStorage::size() const { return m_size; }
443  AGX_FORCE_INLINE bool EntityStorage::empty() const { return m_size == 0; }
444  AGX_FORCE_INLINE size_t EntityStorage::capacity() const { agxAssert(capacityParameter->get() == m_capacity); return m_capacity; }
445  AGX_FORCE_INLINE bool EntityStorage::useInstanceTable() const { return m_useInstanceTable; }
446  AGX_FORCE_INLINE EntityModel *EntityStorage::getEntityModel() { return m_entity; }
447  AGX_FORCE_INLINE const EntityModel *EntityStorage::getEntityModel() const { return m_entity; }
448  AGX_FORCE_INLINE const BufferTable& EntityStorage::getBuffers() const { return m_bufferTable; }
449  AGX_FORCE_INLINE const IndexArray& EntityStorage::getIdToIndexTable() const { return m_idToIndexArray; }
450  AGX_FORCE_INLINE IndexArray& EntityStorage::getIdToIndexTable() { return m_idToIndexArray; }
451  AGX_FORCE_INLINE Buffer *EntityStorage::getGlobalOrderBuffer() { return m_globalOrderBuffer; }
452  AGX_FORCE_INLINE const Buffer *EntityStorage::getGlobalOrderBuffer() const { return m_globalOrderBuffer; }
453 
454  AGX_FORCE_INLINE Array<EntityPtr> EntityStorage::getInstances() { return Array<EntityPtr>(m_instanceBuffer); }
455  AGX_FORCE_INLINE const Array<EntityPtr> EntityStorage::getInstances() const { return Array<EntityPtr>(m_instanceBuffer); }
456 
457  template <typename T>
458  AGX_FORCE_INLINE Array<T> EntityStorage::getInstances()
459  {
460  agxAssert(agxData::getFormat<T>()->is(agxData::getFormat<EntityPtr>()));
461  return Array<T>::raw((T *)m_instanceBuffer->ptr(), (agx::Index)m_instanceBuffer->size());
462  }
463 
464  template <typename T>
465  AGX_FORCE_INLINE const Array<T> EntityStorage::getInstances() const { return const_cast<EntityStorage *>(this)->getInstances<T>(); }
466 
467  AGX_FORCE_INLINE Buffer *EntityStorage::getBuffer(const Attribute *attribute)
468  {
469  Buffer *buffer = m_buffers[attribute->getId()];
470 
471  if (!buffer)
472  buffer = this->createBuffer(attribute);
473 
474  return buffer;
475  }
476 
477 
478  template <typename T>
479  AGX_FORCE_INLINE T& EntityStorage::get(const ScalarAttributeT<T> *attribute, size_t index)
480  {
481  Buffer *buffer = this->getBuffer(attribute);
482  return buffer->getElement<T>(index);
483  }
484 
485  template <typename T>
486  AGX_FORCE_INLINE T& EntityStorage::get(const agx::Name& attributeName, size_t index)
487  {
488  for (size_t i = 0; i < m_buffers.size(); ++i)
489  {
490  Buffer *buffer = m_buffers[i];
491  if (buffer->getName() == attributeName)
492  return buffer->getElement<T>(index);
493  }
494 
495  agxAbort();
496  return m_buffers[0]->getElement<T>(0); // Just to make compiler happy
497  }
498 
499 
500  AGX_FORCE_INLINE size_t EntityStorage::idToIndex(size_t id) const
501  {
502  return m_idToIndexArray[id];
503  }
504 
505  AGX_FORCE_INLINE size_t EntityStorage::indexToId(size_t index) const
506  {
507  return m_instanceBuffer->getElement<agxData::EntityPtr>(index).getId();
508  }
509 
510 
511  AGX_FORCE_INLINE void EntityStorage::destroyInstance(EntityPtr instance)
512  {
513  destroyInstance(instance.getId());
514  }
515 
516  // inline EntityIterator EntityStorage::iterator() { return EntityIterator(this); }
517  // inline EntityIterator EntityStorage::iterator(IndexRange range) { return EntityIterator(this, range); }
518 
519 
520  AGX_FORCE_INLINE EntityData *EntityStorage::getData() { return m_dataSet; }
521 
522  template <typename T>
523  AGX_FORCE_INLINE T *EntityStorage::getData() { return m_dataSet->as<T>(); }
524 
525  template <typename T>
526  AGX_FORCE_INLINE const T *EntityStorage::getData() const { return m_dataSet->as<const T>(); }
527 
528  AGX_FORCE_INLINE Buffer *EntityStorage::getIdToIndexBuffer() { return m_idToIndexBuffer; }
529  AGX_FORCE_INLINE const Buffer *EntityStorage::getIdToIndexBuffer() const { return m_idToIndexBuffer; }
530 
531  AGX_FORCE_INLINE Buffer *EntityStorage::getInstanceBuffer() { return m_instanceBuffer; }
532  AGX_FORCE_INLINE const Buffer *EntityStorage::getInstanceBuffer() const { return m_instanceBuffer; }
533 
534 
536 
537  AGX_FORCE_INLINE size_t EntityData::idToIndex(size_t id) const
538  {
539  agxAssert(m_storage);
540  return m_storage->idToIndex(id);
541  }
542 
543  AGX_FORCE_INLINE size_t EntityData::indexToId(size_t index) const
544  {
545  agxAssert(m_storage);
546  return m_storage->indexToId(index);
547  }
548 
549  AGX_FORCE_INLINE agx::Index EntityData::createInstance()
550  {
551  return this->createInstances(1);
552  }
553 
554  AGX_FORCE_INLINE agx::Index EntityData::createInstances(agx::Index numInstances)
555  {
556  if (m_numElements + numInstances >= m_storage->size())
557  {
558  if (!m_hasBatchInstances)
559  {
560  m_storage->beginBatch();
561  m_hasBatchInstances = true;
562  }
563 
564  agx::UInt head = m_numElements; // Save current size as resize will implicitly update m_numElements
565  m_storage->resize(agx::align_ceil((agx::Index)(m_numElements + numInstances), (agx::Index)m_instanceBatchSize));
566  m_numElements = head;
567  }
568 
569  agx::Index instanceIndex = (agx::Index)m_numElements;
570  m_numElements += numInstances;
571 
572  return instanceIndex;
573  }
574 
575 
576  AGX_FORCE_INLINE void EntityData::commitInstanceBatch()
577  {
578  if (m_hasBatchInstances)
579  {
580  m_storage->commitBatch(m_numElements);
581  m_hasBatchInstances = false;
582  }
583  }
584 
585 
586 
589  {
590  m_storage = storage;
591  m_id = id;
592  }
593 
594  AGX_FORCE_INLINE EntityData *EntityPtr::getData()
595  {
596  agxAssert(m_storage);
597  return m_storage->getData();
598  }
599 
600  AGX_FORCE_INLINE const EntityData *EntityPtr::getData() const { return const_cast<EntityPtr *>(this)->getData(); }
601 
602  template <typename T>
603  AGX_FORCE_INLINE T& EntityPtr::dataSet()
604  {
605  agxAssert(dynamic_cast<T *>(this->getData()));
606  return *static_cast<T *>(this->getData());
607  }
608 
609  template <typename T>
610  AGX_FORCE_INLINE const T& EntityPtr::dataSet() const { return const_cast<EntityPtr *>(this)->dataSet<T>(); }
611 
612 
613  AGX_FORCE_INLINE agx::Index EntityPtr::calculateIndex() const
614  {
615  agxAssert1(m_storage, "This is a null ptr");
616  return (agx::Index)m_storage->idToIndex(m_id);
617  }
618 
619 
620  template <typename T>
621  AGX_FORCE_INLINE T& EntityPtr::getElement(const Attribute *attribute) const
622  {
623  agxAssert(attribute);
624  agxAssertN(m_storage, "Can not access attribute \'%s\' from an invalid pointer.", attribute->fullName().c_str());
625  Buffer *buffer = m_storage->getBuffer(attribute);
626  agx::Index index = this->calculateIndex();
627  agxAssertN(index < m_storage->size(), "%s: Entity (%p, %u) claims to have index %u, but storage size is %u.", m_storage->getPath().c_str(), (void *)m_storage, (unsigned)m_id, (unsigned)index, (unsigned)m_storage->size());
628  return *(T *)buffer->getElement(index);
629  }
630 
631  AGX_FORCE_INLINE void EntityPtr::verifyIndex() const
632  {
633 #ifdef AGX_DEBUG
634  agx::Index index = this->calculateIndex();
635  agxAssertN(index < m_storage->size(), "%s: Entity (%p, %u) claims to have index %u, but storage size is %u.", m_storage->getPath().c_str(), (void *)m_storage, (unsigned)m_id, (unsigned)index, (unsigned)m_storage->size());
636 #endif
637  }
638 
639 
640  template <typename T>
641  AGX_FORCE_INLINE T& EntityPtr::operator() (const ScalarAttributeT<T> *attribute)
642  {
643  return this->getElement<T>(attribute);
644  }
645 
646  template <typename T>
647  AGX_FORCE_INLINE const T& EntityPtr::operator() (const ScalarAttributeT<T> *attribute) const
648  {
649  return this->getElement<const T>(attribute);
650  }
651 
652  template <typename T>
653  AGX_FORCE_INLINE T& EntityPtr::operator() (const PointerAttributeT<T> *attribute)
654  {
655  return this->getElement<T>(attribute);
656  }
657 
658  template <typename T>
659  AGX_FORCE_INLINE const T& EntityPtr::operator() (const PointerAttributeT<T> *attribute) const
660  {
661  return this->getElement<const T>(attribute);
662  }
663 
664 
665  template <typename T>
666  AGX_FORCE_INLINE typename ArrayAttributeT<T>::Type& EntityPtr::operator() (const ArrayAttributeT<T> *attribute)
667  {
668  return this->getElement<typename ArrayAttributeT<T>::Type>(attribute);
669  }
670 
671  template <typename T>
672  AGX_FORCE_INLINE const typename ArrayAttributeT<T>::Type& EntityPtr::operator() (const ArrayAttributeT<T> *attribute) const
673  {
675  }
676 
677 
678 
679  template <typename T>
680  AGX_FORCE_INLINE typename VectorAttributeT<T>::Type& EntityPtr::operator() (const VectorAttributeT<T> *attribute)
681  {
682  return this->getElement<typename VectorAttributeT<T>::Type>(attribute);
683  }
684 
685  template <typename T>
686  AGX_FORCE_INLINE const typename VectorAttributeT<T>::Type& EntityPtr::operator() (const VectorAttributeT<T> *attribute) const
687  {
689  }
690 
691  template <typename T>
692  AGX_FORCE_INLINE typename HashSetAttributeT<T>::Type& EntityPtr::operator() (const HashSetAttributeT<T> *attribute)
693  {
694  return this->getElement<typename HashSetAttributeT<T>::Type>(attribute);
695  }
696 
697  template <typename T>
698  AGX_FORCE_INLINE const typename HashSetAttributeT<T>::Type& EntityPtr::operator() (const HashSetAttributeT<T> *attribute) const
699  {
701  }
702 
703  template <typename T>
704  AGX_FORCE_INLINE Array<T> EntityStorage::dereferenceMultipliedAttribute(const EntityPtr& entity, const ScalarAttributeT<T> *attribute)
705  {
706  agxAssert(entity.getStorage() == this);
707  Buffer *buffer = this->getBuffer(attribute);
708  // agxAssert1(buffer->getFormat()->getSize() >= sizeof(T), "Only down-cast allowed, eg int64 -> int32");
709  agxAssertN(entity.calculateIndex() < this->size(), "%s: Entity with id %u claims to have index %u, but storage size is %u.", this->getPath().c_str(), (unsigned)entity.getId(), (unsigned)entity.calculateIndex(), (unsigned)this->size());
710  size_t arraySize = buffer->getElementArraySize();
711  size_t offset = entity.calculateIndex() * arraySize;
712  return Array<T>(buffer, agx::IndexRange(offset, offset + arraySize));
713  }
714 
715  template <typename T>
716  AGX_FORCE_INLINE const Array<T> EntityStorage::dereferenceMultipliedAttribute(const EntityPtr& entity, const ScalarAttributeT<T> *attribute) const
717  {
718  return const_cast<EntityStorage *>(this)->dereferenceMultipliedAttribute(entity, attribute);
719  }
720 
721 
722 
723  template <typename VectorT>
724  void EntityStorage::destroyInstances(const VectorT& instances)
725  {
726  this->destroyInstances(instances.begin(), instances.size());
727  }
728 
729 
730  AGX_FORCE_INLINE Byte *EntityStorage::getPermutationBuffer(agxData::Attribute *attribute)
731  {
732  return this->getPermutationBuffer(attribute->getId());
733  }
734 
735  AGX_FORCE_INLINE Byte *EntityStorage::getPermutationBuffer(size_t index)
736  {
737  return m_permutationBuffers[index];
738  }
739 
740  //---------------------------------------------------------------
741 
742  template <typename T>
743  AGX_FORCE_INLINE T& EntityPtr::operator() (const SharedAttributeT<T> *attribute)
744  {
745  agxAssert(m_storage);
746  agxData::Value *value = m_storage->getObject<agxData::Value>(attribute->getName());
747  return const_cast<T&>(value->get<T>());
748  }
749 
750  template <typename T>
751  AGX_FORCE_INLINE const T& EntityPtr::operator() (const SharedAttributeT<T> *attribute) const
752  {
753  return const_cast<EntityPtr *>(this)->operator()(attribute);
754  }
755 
756 
758  {
759  return m_storage ? m_storage->getEntityModel() : nullptr;
760  }
761 
763  {
764  return m_storage ? m_storage->getEntityModel() : nullptr;
765  }
766 
767  AGX_FORCE_INLINE void EntityPtr::swapId(EntityPtr& other)
768  {
769  agxAssert(m_storage);
770  agxAssert(m_storage == other.m_storage);
771 
772  m_storage->swapInstanceIds(m_id, other.m_id);
773  }
774 
775  AGX_FORCE_INLINE void EntityPtr::swapIndex(EntityPtr& other)
776  {
777  agxAssert(m_storage);
778  agxAssert(m_storage == other.m_storage);
779 
780  m_storage->swapInstances(calculateIndex(), other.calculateIndex());
781  }
782 
783 
784  AGX_FORCE_INLINE bool EntityPtr::isInstanceOf(agxData::EntityModel *model) const
785  {
786  return m_storage && m_storage->getEntityModel()->is(model);
787  }
788 
789 
790  inline EntityPtr EntityPtr::createWithIndex(EntityStorage *storage, agx::UInt32 index)
791  {
792  agxAssert(storage);
793  return EntityPtr(storage, (agx::Index)storage->indexToId(index));
794  }
795 
796  AGX_FORCE_INLINE EntityPtr::EntityPtr(const EntityInstance& instance) : m_storage(const_cast<EntityStorage *>(instance.getStorage())), m_id(agx::InvalidIndex)
797  {
798  if (m_storage)
799  m_id = (agx::Index)m_storage->indexToId(instance.getIndex());
800  }
801 
803 
804  AGX_FORCE_INLINE EntityInstance::EntityInstance(agxData::EntityStorage* storage, agx::Index index) : m_data(storage->getData()), m_index(index)
805  {}
806 
808  {
809  agxAssert(other);
810 
811  m_data = const_cast<EntityData *>(other.getData());
812  m_index = other.calculateIndex();
813  }
814 
816  {
817  agxAssert(getStorage() && getStorage()->useInstanceTable());
818  return (agx::Index)getStorage()->indexToId(m_index);
819  }
820 
821 
822  AGX_FORCE_INLINE agxData::EntityStorage *EntityInstance::getStorage() { return m_data ? m_data->getStorage() : nullptr; }
824 
827 
828 
830  {
831  const agxData::EntityModel *m = getModel();
832  return m ? m->is(model) : false;
833  }
834 
835 
837  {
838 #ifdef AGX_DEBUG
839  const EntityStorage *storage = this->getStorage();
840  agx::Index index = this->getIndex();
841  agxAssertN(index < storage->size(), "%s: Entity instance (%p, %u) is out of bounds, storage size is %u.", storage->getPath().c_str(), (void *)storage, (unsigned)index, (unsigned)storage->size());
842 #endif
843  }
844 
845 
847 
849  {}
850 
851  AGX_FORCE_INLINE EntityRange::EntityRange(EntityStorage *storage, size_t start, size_t end) : agx::IndexRange(start, end), m_storage(storage)
852  {
853  }
854 
855  inline EntityPtr EntityRange::at(size_t index) { return operator[](index); }
856  AGX_FORCE_INLINE EntityRange EntityRange::operator[] (agx::IndexRange subRange) { return EntityRange(m_storage, subRange.begin(), subRange.end()); }
857 
858  inline EntityPtr EntityRange::operator[] (size_t index)
859  {
860  agxAssert(m_storage);
861  agxAssert(index < this->size());
862  return EntityPtr(m_storage, (agx::UInt32)m_storage->indexToId(this->begin() + index));
863  }
864 
865 
866 
868  template <typename T>
869  inline AttributePtr<T>::AttributePtr() : m_attribute(nullptr)
870  {
871  }
872 
873  template <typename T>
874  inline AttributePtr<T>::AttributePtr(EntityPtr entity, const Attribute *attribute) : m_entityPtr(entity), m_attribute(attribute)
875  {
876  }
877 
878  template <typename T>
880  {
881  agxAssert(this->isValid());
882  return m_entityPtr.getElement<T>(m_attribute);
883  }
884 
885  template <typename T>
887  {
888  agxAssert(this->isValid());
889  return m_entityPtr.getElement<T>(m_attribute);
890  }
891 
892  template <typename T>
894  {
895  return m_entityPtr.isValid() && m_attribute;
896  }
897 
898 
899  template <typename T>
901  {
902  return !this->isValid();
903  }
904 
905  template <typename T>
907  {
908  return this->isValid();
909  }
910 
911 }
912 
914 
915 #ifdef _MSC_VER
916 # pragma warning(pop)
917 #endif
918 
agxData::EntityStorage * getStorage()
Alias for getData()->getStorage()
Representation of a path, a list of name components.
Definition: Path.h:32
agx::Index getId() const
Definition: Attribute.h:365
AGXTERRAIN_EXPORT void clear()
void AGXPHYSICS_EXPORT init()
Initialize AGX Dynamics API including thread resources and must be executed before using the AGX API...
An attribute container.
agxData::EntityData * getData()
bool is(const agx::Model *other) const
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
agxData::LocalVector< std::pair< size_t, size_t > > InstanceSwapLocalVector
static const agx::Name IdToIndexName
size_t indexToId(size_t index) const
Convert between instance id and data index.
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
agx::Index getIndex() const
Abstract representation of a value.
Definition: Value.h:48
agxData::EntityModel * getEntityModel()
#define agxAssertN(expr, format,...)
Definition: debug.h:145
AGXCORE_EXPORT Model * getModel(const Path &path, const Path &_namespace=Path())
Definition: Object.h:363
#define m_size
Definition: Vector.h:429
Entity instance provides access to a single instance in an EntityStorage.
agx::String fullName() const
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
#define agxAbort()
Definition: debug.h:164
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
T align_ceil(T value, T alignment)
Definition: Math.h:466
agx::Index getId() const
Definition: EntityPtr.h:273
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
Type-specific Array used for fast access into the data held by a Buffer.
Definition: agxData.h:30
EntityPtr operator[](size_t index)
const char * c_str() const
uint32_t UInt
Definition: Integer.h:34
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agxData::EntityModel * getModel()
Alias for getData()->getStorage()->getModel()
IndexRangeT< agx::UInt > IndexRange
Definition: IndexRange.h:140
An abstract attribute description, part of an Entity.
Definition: Attribute.h:57
bool is() const
Subclass test.
Abstract representation of a data buffer.
Definition: Buffer.h:55
void verifyIndex() const
Check for broken ptr, by verifying that the index is active in the storage.
Contains classes for low level data storage for AGX.
Definition: Container.h:23
UInt32 Index
Definition: Integer.h:52
agx::UInt8 Byte
Definition: agxData.h:45
T & getElement(size_t index)
Definition: Buffer.h:517
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
agx::VectorPOD< std::pair< size_t, size_t > > InstanceSwapVector
const agx::Name & getName() const
Definition: Object.h:368
Callback1< T1 > CallbackType
Definition: Event.h:37
A range of indices.
Definition: IndexRange.h:32
EntityInstance()
Constructors.
#define agxAssert(expr)
Definition: debug.h:143
agx::HashTable< Attribute *, BufferRef > BufferTable
Definition: EntityStorage.h:59
An abstract description of a data entity stored using SOA (structure of arrays) pattern in a EntitySt...
Definition: EntityModel.h:63
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
agx::Index calculateId() const
bool isInstanceOf(agxData::EntityModel *model) const
AGXTERRAIN_EXPORT TimingStorage & getStorage()
EntityPtr at(size_t index)
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
Local scope vector.
Definition: LocalVector.h:43
const T & get() const
Definition: Value.h:331
#define agxAssert1(expr, msg)
Definition: debug.h:144
agx::UInt size() const
Definition: IndexRange.h:168
#define m_capacity
Definition: Vector.h:430
agxData::ValueRefT< agx::UInt > capacityParameter
Definition: EntityStorage.h:90
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
Inheritance with partial specialization due to bug with ref_ptr containers.
agxData::EntityStorage * getStorage() const
Definition: EntityPtr.h:272
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
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::Index calculateIndex() const
Calculate the index using the indirect id.
#define AGXCORE_EXPORT
agx::UInt32 getId() const
Definition: Object.h:369
agx::Object is a refcounted object with a name.
Definition: Object.h:58
agx::Path getPath() const
agxData::ValueRefT< agx::UInt > sizeParameter
Definition: EntityStorage.h:89
static const agx::Name IndexToIdName