17 #ifndef AGXDATA_ENTITY_STORAGE_H
18 #define AGXDATA_ENTITY_STORAGE_H
34 # pragma warning(push)
35 # pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer
43 class GlobalResultStorage;
49 class SimulationFrameWriter;
79 virtual void configure(agx::TiXmlElement *eStorage)
override;
86 void addListener(EventListener *listener);
87 void removeListener(EventListener *listener);
88 bool hasListener(EventListener *listener);
110 size_t capacity()
const;
115 size_t getNumBytes()
const;
126 void reserve(
size_t size);
134 template <
typename T>
137 template <
typename T>
152 void resize(
size_t size,
bool initializeElements =
true);
154 size_t presize(
size_t size,
size_t initialSize = 0);
157 void destroyInstance(
size_t id);
159 template <
typename VectorT>
160 void destroyInstances(
const VectorT& instances);
229 size_t idToIndex(
size_t id)
const;
234 size_t indexToId(
size_t index)
const;
240 agxData::EntityData *getData();
242 template <
typename T>
245 template <
typename T>
246 const T* getData()
const;
253 void setUseInstanceTable(
bool flag);
258 bool useInstanceTable()
const;
292 void print(std::ostream& stream)
const;
295 void verifyConsistency();
301 template <
typename T>
302 T&
get(
const agx::Name& attributeName,
size_t index);
306 void preparePermutation();
321 template <
typename T>
324 template <
typename T>
332 void keyFrameResize(
size_t size,
size_t capacity);
334 void setEnableEvents(
bool flag);
340 void printInstanceTable()
const;
356 friend class EntityData;
358 void commitBatch(
size_t size);
366 void swapInstances(
size_t index1,
size_t index2);
367 void init(
size_t initialCapacity);
374 void elementBufferReallocationCallback(
Buffer* elementBuffer);
382 void destroyInstances(
const EntityPtr* instances,
size_t numInstances,
bool triggerEvent =
true);
390 template <
typename T>
399 bool m_useInstanceTable;
400 bool m_implicitResize;
402 bool m_usedResizeMethod;
406 BufferRefVector m_buffers;
408 EntityDataRef m_dataSet;
425 EventDispatch m_eventDispatch;
458 template <
typename T>
461 agxAssert(agxData::getFormat<T>()->
is(agxData::getFormat<EntityPtr>()));
465 template <
typename T>
473 buffer = this->createBuffer(attribute);
479 template <
typename T>
486 template <
typename T>
489 for (
size_t i = 0; i < m_buffers.size(); ++i)
491 Buffer *buffer = m_buffers[i];
492 if (buffer->
getName() == attributeName)
497 return m_buffers[0]->getElement<T>(0);
503 return m_idToIndexArray[
id];
523 template <
typename T>
526 template <
typename T>
541 return m_storage->idToIndex(
id);
547 return m_storage->indexToId(index);
552 return this->createInstances(1);
557 if (m_numElements + numInstances >= m_storage->size())
559 if (!m_hasBatchInstances)
561 m_storage->beginBatch();
562 m_hasBatchInstances =
true;
567 m_numElements = head;
571 m_numElements += numInstances;
573 return instanceIndex;
579 if (m_hasBatchInstances)
581 m_storage->commitBatch(m_numElements);
582 m_hasBatchInstances =
false;
603 template <
typename T>
607 return *
static_cast<T *
>(this->
getData());
610 template <
typename T>
621 template <
typename T>
625 agxAssertN(m_storage,
"Can not access attribute \'%s\' from an invalid pointer.", attribute->
fullName().c_str());
628 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());
629 return *(T *)buffer->getElement(index);
636 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());
641 template <
typename T>
644 return this->getElement<T>(attribute);
647 template <
typename T>
650 return this->getElement<const T>(attribute);
653 template <
typename T>
656 return this->getElement<T>(attribute);
659 template <
typename T>
662 return this->getElement<const T>(attribute);
666 template <
typename T>
672 template <
typename T>
680 template <
typename T>
686 template <
typename T>
692 template <
typename T>
698 template <
typename T>
704 template <
typename T>
708 Buffer *buffer = this->getBuffer(attribute);
711 size_t arraySize = buffer->getElementArraySize();
716 template <
typename T>
719 return const_cast<EntityStorage *
>(
this)->dereferenceMultipliedAttribute(entity, attribute);
724 template <
typename VectorT>
727 this->destroyInstances(instances.begin(), instances.size());
733 return this->getPermutationBuffer(attribute->
getId());
738 return m_permutationBuffers[index];
743 template <
typename T>
748 return const_cast<T&
>(value->
get<T>());
751 template <
typename T>
754 return const_cast<EntityPtr *
>(
this)->
operator()(attribute);
773 m_storage->swapInstanceIds(m_id, other.m_id);
812 m_data =
const_cast<EntityData *
>(other.
getData());
833 return m ? m->
is(model) :
false;
842 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());
869 template <
typename T>
874 template <
typename T>
879 template <
typename T>
883 return m_entityPtr.getElement<T>(m_attribute);
886 template <
typename T>
890 return m_entityPtr.getElement<T>(m_attribute);
893 template <
typename T>
896 return m_entityPtr.isValid() && m_attribute;
900 template <
typename T>
903 return !this->isValid();
906 template <
typename T>
909 return this->isValid();
917 # pragma warning(pop)
agxData::EntityStorage * getStorage()
Alias for getData()->getStorage()
agxData::EntityModel * getModel()
Representation of a path, a list of name components.
AGXTERRAIN_EXPORT void clear()
void init(agxData::EntityStorage *storage, agx::Index id)
Set the target storage and id.
void AGXPHYSICS_EXPORT init()
Initialize agx.
agxData::EntityData * getData()
Inheritance with partial specialization due to bug with ref_ptr containers.
agxData::LocalVector< std::pair< size_t, size_t > > InstanceSwapLocalVector
agx::Index calculateIndex() const
Calculate the index using the indirect id.
agxData::EntityInstance instance()
static const agx::Name IdToIndexName
bool is() const
Subclass test.
static agxData::Array< T > raw(T *buffer, agx::Index size)
Array creation helper method that creates an array covering the given memory area.
Data storage for a collection of entity instances of a specified EntityModel.
A component is an object containing other objects, enabling hierarchical structuring.
A model is an abstract representation of the class of an agx::Object.
Abstract representation of a value.
agxData::EntityModel * getEntityModel()
#define agxAssertN(expr, format,...)
agxData::Buffer * getBuffer(const agx::Name &name)
T & operator()(const agxData::ScalarAttributeT< T > *attribute)
Access the value of a specific attribute.
T & get(const agx::Name &attributeName, size_t index)
Slow but convenient accessor.
bool isInstanceOf(agxData::EntityModel *model) const
void swapIndex(EntityPtr &other)
Swap index with another instance.
Entity instance provides access to a single instance in an EntityStorage.
bool is(const agx::Model *other) const
Accessor to a entity instance stored in a EntityStorage, to allow SOA (structure of arrays) data to b...
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
size_t indexToId(size_t index) const
Convert between instance id and data index.
agxData::Buffer * getIdToIndexBuffer()
void swapId(EntityPtr &other)
Swap id with another instance.
#define AGX_DECLARE_POINTER_TYPES(type)
T align_ceil(T value, T alignment)
agxData::Array< T > dereferenceMultipliedAttribute(const agxData::EntityPtr &entity, const agxData::ScalarAttributeT< T > *attribute)
Representation of a name string.
const agx::Name & getName() const
agxData::EntityData * getData()
static EntityPtr createWithIndex(EntityStorage *storage, agx::Index index)
Type-specific Array used for fast access into the data held by a Buffer.
agxData::EntityStorage * getStorage() const
const char * c_str() const
EntityPtr operator[](size_t index)
void destroyInstance(agxData::EntityPtr instance)
const agxData::BufferTable & getBuffers() const
void destroyInstances(const VectorT &instances)
agxData::Buffer * getGlobalOrderBuffer()
If the storage is used as a global result, we provide a permutation buffer for parallel determinism...
Vector containing 'raw' data.
agxData::Buffer * getInstanceBuffer()
void verifyIndex() const
Check for broken ptr, by verifying that the index is active in the storage.
AGX_DECLARE_VECTOR_TYPES(BinaryData)
agxData::EntityModel * getModel()
Alias for getData()->getStorage()->getModel()
IndexRangeT< agx::UInt > IndexRange
An abstract attribute description, part of an Entity.
Abstract representation of a data buffer.
void verifyIndex() const
Check for broken ptr, by verifying that the id is active in the storage.
agx::Index calculateId() const
agx::VectorPOD< std::pair< size_t, size_t > > InstanceSwapVector
Callback1< Value * > CallbackType
EntityInstance()
Constructors.
agx::Path getPath() const
agx::HashTable< Attribute *, BufferRef > BufferTable
agxData::Byte * getPermutationBuffer(agxData::Attribute *attribute)
An abstract description of a data entity stored using SOA (structure of arrays) pattern in a EntitySt...
T & getElement(size_t index)
bool useInstanceTable() const
bool isInstanceOf(agxData::EntityModel *model) const
agx::Index getIndex() const
EntityPtr at(size_t index)
Class for writing serialized data in binary format to a stream.
std::ostream & operator<<(std::ostream &output, const Attribute &attribute)
agx::UInt32 getId() const
#define agxAssert1(expr, msg)
agx::String fullName() const
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
agxData::ValueRefT< agx::UInt > capacityParameter
void swapInstances(const InstanceSwapVector &swaps)
agxData::IndexArray & getIdToIndexTable()
Inheritance with partial specialization due to bug with ref_ptr containers.
const agxData::Array< EntityPtr > getInstances() const
An agx::Device is an abstract representation of a device on which data can be stored and processed...
size_t idToIndex(size_t id) const
Convert between instance id and data index.
agx::Object * getObject(const agx::Name &name, size_t index=0)
agx::Object is a refcounted object with a name.
agxData::ValueRefT< agx::UInt > sizeParameter
EntityStorage(agxData::EntityModel *entity, size_t initialCapacity=32)
static const agx::Name IndexToIdName