25#ifndef EASY3D_CORE_PROPERTIES_H
26#define EASY3D_CORE_PROPERTIES_H
34#include <easy3d/util/logging.h>
77 virtual void reset(
size_t idx) = 0;
99 virtual void swap(
size_t i0,
size_t i1) = 0;
106 virtual void copy(
size_t from,
size_t to) = 0;
124 virtual const std::type_info&
type()
const = 0;
130 const std::string&
name()
const {
return name_; }
188 data_.resize(n, value_);
193 data_.push_back(value_);
205 std::copy((*pa).data_.begin(), (*pa).data_.end(), data_.end()-(*pa).data_.size());
216 data_[to] = (*pa)[from];
228 void swap(
size_t i0,
size_t i1)
override
235 void copy(
size_t from,
size_t to)
override
237 data_[to]=data_[from];
253 const std::type_info&
type()
const override {
return typeid(T); }
282 assert(
size_t(_idx) < data_.size() );
293 assert(
size_t(_idx) < data_.size());
331 friend class PropertyContainer;
355 operator bool()
const
357 return parray_ !=
nullptr;
367 assert(parray_ !=
nullptr);
368 return (*parray_)[i];
378 assert(parray_ !=
nullptr);
379 return (*parray_)[i];
388 assert(parray_ !=
nullptr);
389 return parray_->data();
398 assert(parray_ !=
nullptr);
399 return parray_->vector();
408 assert(parray_ !=
nullptr);
409 return parray_->vector();
418 assert(parray_ !=
nullptr);
428 assert(parray_ !=
nullptr);
436 const std::string&
name()
const {
437 assert(parray_ !=
nullptr);
438 return parray_->name();
446 assert(parray_ !=
nullptr);
447 parray_->set_name(n);
493 for (
size_t i=0; i<parrays_.size(); ++i)
494 parrays_[i] = _rhs.parrays_[i]->clone();
505 for(
auto pa : parrays_) {
506 for (
auto rpa : _rhs.parrays_) {
507 if(pa->is_same(*rpa)){
521 for (
auto rpa : _rhs.parrays_)
523 bool property_already_exists =
false;
524 for(
auto pa : parrays_)
525 if (rpa->is_same(*pa))
527 property_already_exists =
true;
531 if (property_already_exists)
534 parrays_.push_back(rpa->empty_clone());
535 parrays_.back()->resize(size_);
550 for(std::size_t i=0; i<parrays_.size(); ++i)
551 if (!(parrays_[i]->
transfer(* _rhs.parrays_[i], from, to)))
560 size_t size()
const {
return size_; }
574 std::vector<std::string> names(parrays_.size());
575 for(std::size_t i=0; i<parrays_.size(); ++i)
576 names[i] = parrays_[i]->name();
590 for(
auto pa : parrays_)
592 if (pa->name() == name)
594 LOG(ERROR) <<
"A property with name \""
595 << name <<
"\" already exists. Returning invalid property.";
603 parrays_.push_back(p);
615 for(
auto pa : parrays_)
616 if (pa->name() == name)
631 if (!p) p =
add<T>(name, t);
640 const std::type_info&
get_type(
const std::string& name)
const
642 for(
auto pa : parrays_)
643 if (pa->name() == name)
656 auto it=parrays_.begin(), end=parrays_.end();
657 for (; it!=end; ++it)
659 if (*it == h.parray_)
677 auto it=parrays_.begin(), end=parrays_.end();
678 for (; it!=end; ++it)
680 if ((*it)->name() == name)
696 bool rename(
const std::string& old_name,
const std::string& new_name)
698 assert(!old_name.empty());
699 assert(!new_name.empty());
700 auto it=parrays_.begin(), end=parrays_.end();
701 for (; it!=end; ++it)
703 if ((*it)->name() == old_name)
705 (*it)->set_name(new_name);
717 for(
auto pa : parrays_)
729 for(
auto pa : parrays_)
739 for(
auto pa : parrays_)
750 if (parrays_.size()<=n) {
754 for (std::size_t i=n; i<parrays_.size(); ++i)
764 for(
auto pa : parrays_)
773 for(
auto pa : parrays_)
783 for(
auto pa : parrays_)
792 void swap(
size_t i0,
size_t i1)
const
794 for(
auto pa : parrays_)
803 this->parrays_.swap(other.parrays_);
804 std::swap(this->size_, other.size_);
812 void copy(
size_t from,
size_t to)
const
814 for(
auto pa : parrays_)
822 const std::vector<BasePropertyArray*>&
arrays()
const {
return parrays_; }
827 std::vector<BasePropertyArray*>&
arrays() {
return parrays_; }
830 std::vector<BasePropertyArray*> parrays_;
virtual ~BasePropertyArray()=default
Destructor.
virtual bool transfer(const BasePropertyArray &other, std::size_t from, std::size_t to)=0
Copies a property from one index to another.
const std::string & name() const
Returns the name of the property.
Definition property.h:130
virtual void swap(size_t i0, size_t i1)=0
Swaps the storage place of two elements.
virtual BasePropertyArray * clone() const =0
Returns a deep copy of the property array.
virtual void push_back()=0
Extend the number of elements by one.
virtual void reserve(size_t n)=0
Reserves memory for n elements.
virtual BasePropertyArray * empty_clone() const =0
Returns an empty copy of the property array.
virtual void resize(size_t n)=0
Resizes storage to hold n elements.
bool is_same(const BasePropertyArray &other) const
Tests if two properties are the same.
Definition property.h:143
virtual void copy(size_t from, size_t to)=0
Copies an element from one index to another.
virtual const std::type_info & type() const =0
Returns the type information of the property.
virtual void shrink_to_fit()=0
Free unused memory.
virtual void reset(size_t idx)=0
Resets an element to its default value.
virtual bool transfer(const BasePropertyArray &other)=0
Copies the entire properties from another property array.
BasePropertyArray(const std::string &name)
Default constructor.
Definition property.h:50
void set_name(const std::string &n)
Sets the name of the property.
Definition property.h:136
Implementation of a generic property array.
Definition property.h:164
std::vector< value_type > vector_type
The vector type of the property.
Definition property.h:167
void push_back() override
Extend the number of elements by one.
Definition property.h:191
const std::type_info & type() const override
Returns the type information of the property.
Definition property.h:253
T value_type
The value type of the property.
Definition property.h:166
reference operator[](size_t _idx)
Accesses the i-th element. No range check is performed.
Definition property.h:280
void swap(size_t i0, size_t i1) override
Swaps the storage place of two elements.
Definition property.h:228
void copy(size_t from, size_t to) override
Copies an element from one index to another.
Definition property.h:235
const T * data() const
Gets a pointer to the array (does not work for T == bool).
Definition property.h:261
BasePropertyArray * clone() const override
Returns a deep copy of the property array.
Definition property.h:240
PropertyArray(const std::string &name, T t=T())
Constructor.
Definition property.h:176
bool transfer(const BasePropertyArray &other, std::size_t from, std::size_t to) override
Copies a property from one index to another.
Definition property.h:211
BasePropertyArray * empty_clone() const override
Returns an empty copy of the property array.
Definition property.h:247
void reserve(size_t n) override
Reserves memory for n elements.
Definition property.h:181
void shrink_to_fit() override
Free unused memory.
Definition property.h:223
void reset(size_t idx) override
Resets an element to its default value.
Definition property.h:196
std::vector< T > & vector()
Gets a reference to the underlying vector.
Definition property.h:270
const_reference operator[](size_t _idx) const
Const access to the i-th element. No range check is performed.
Definition property.h:291
vector_type::const_reference const_reference
The const reference type of the property.
Definition property.h:169
void resize(size_t n) override
Resizes storage to hold n elements.
Definition property.h:186
vector_type::reference reference
The reference type of the property.
Definition property.h:168
bool transfer(const BasePropertyArray &other) override
Copies the entire properties from another property array.
Definition property.h:201
void shrink_to_fit() const
Frees unused space in all arrays.
Definition property.h:762
bool remove(const std::string &name)
Removes a property by its name.
Definition property.h:675
void copy_properties(const PropertyContainer &_rhs)
Copies properties that don't already exist from another container.
Definition property.h:519
void swap(size_t i0, size_t i1) const
Swaps elements i0 and i1 in all arrays.
Definition property.h:792
size_t size() const
Returns the current size of the property arrays.
Definition property.h:560
void swap(PropertyContainer &other) noexcept
Swaps content with another property container.
Definition property.h:802
Property< T > add(const std::string &name, const T t=T())
Adds a property with a given name and default value.
Definition property.h:587
bool transfer(const PropertyContainer &_rhs, std::size_t from, std::size_t to) const
Transfers one element with all properties.
Definition property.h:547
std::vector< BasePropertyArray * > & arrays()
Returns the vector of property arrays.
Definition property.h:827
virtual ~PropertyContainer()
Destructor. Deletes all property arrays.
Definition property.h:473
bool rename(const std::string &old_name, const std::string &new_name)
Renames a property.
Definition property.h:696
void resize(size_t n)
Resizes all arrays to size n.
Definition property.h:737
void reserve(size_t n) const
Reserves memory for n entries in all arrays.
Definition property.h:727
size_t n_properties() const
Returns the number of property arrays.
Definition property.h:566
PropertyContainer(const PropertyContainer &_rhs)
Copy constructor. Performs deep copy of property arrays.
Definition property.h:479
Property< T > get(const std::string &name) const
Gets a property by its name.
Definition property.h:613
PropertyContainer()
Default constructor.
Definition property.h:468
Property< T > get_or_add(const std::string &name, const T t=T())
Gets or adds a property by its name.
Definition property.h:628
const std::vector< BasePropertyArray * > & arrays() const
Returns the vector of property arrays (read-only).
Definition property.h:822
void reset(size_t idx) const
Resets an element to its default property values.
Definition property.h:782
void transfer(const PropertyContainer &_rhs) const
Transfers properties from another property container.
Definition property.h:503
bool remove(Property< T > &h)
Removes a property.
Definition property.h:654
void resize_property_array(size_t n)
Resizes the vector of properties to n, deleting all other properties.
Definition property.h:748
void clear()
Deletes all properties.
Definition property.h:715
std::vector< std::string > properties() const
Returns a vector of all property names.
Definition property.h:572
void push_back()
Adds a new element to each vector.
Definition property.h:771
const std::type_info & get_type(const std::string &name) const
Gets the type information of a property by its name.
Definition property.h:640
PropertyContainer & operator=(const PropertyContainer &_rhs)
Assignment operator. Performs deep copy of property arrays.
Definition property.h:486
void copy(size_t from, size_t to) const
Copies an element from one index to another in all arrays.
Definition property.h:812
Implementation of a generic property.
Definition property.h:326
const std::string & name() const
Returns the name of the property.
Definition property.h:436
virtual const_reference operator[](size_t i) const
Const access to the i-th element.
Definition property.h:376
const T * data() const
Gets a pointer to the array.
Definition property.h:386
const PropertyArray< T > & array() const
Const access to the property array.
Definition property.h:426
PropertyArray< T > & array()
Gets a reference to the property array.
Definition property.h:416
PropertyArray< T >::const_reference const_reference
The const reference type of the property.
Definition property.h:329
virtual ~Property()=default
Destructor.
virtual reference operator[](size_t i)
Accesses the i-th element.
Definition property.h:365
std::vector< T > & vector()
Gets a reference to the underlying vector.
Definition property.h:396
const std::vector< T > & vector() const
Const access to the underlying vector.
Definition property.h:406
Property(PropertyArray< T > *p=nullptr)
Constructor.
Definition property.h:338
void reset()
Resets the property.
Definition property.h:346
PropertyArray< T >::reference reference
The reference type of the property.
Definition property.h:328
void set_name(const std::string &n)
Sets the name of the property.
Definition property.h:445
Definition collider.cpp:182