25#ifndef EASY3D_CORE_PROPERTIES_H
26#define EASY3D_CORE_PROPERTIES_H
63 virtual void reset(
size_t idx) = 0;
71 virtual void swap(
size_t i0,
size_t i1) = 0;
74 virtual void copy(
size_t from,
size_t to) = 0;
83 virtual const std::type_info&
type()
const = 0;
86 const std::string&
name()
const {
return name_; }
89 void set_name(
const std::string& n) { name_ = n; }
114 typedef T value_type;
115 typedef std::vector<value_type> vector_type;
116 typedef typename vector_type::reference reference;
117 typedef typename vector_type::const_reference const_reference;
131 data_.resize(n, value_);
136 data_.push_back(value_);
148 std::copy((*pa).data_.begin(), (*pa).data_.end(), data_.end()-(*pa).data_.size());
159 data_[to] = (*pa)[from];
168 vector_type(data_).swap(data_);
171 void swap(
size_t i0,
size_t i1)
override
178 void copy(
size_t from,
size_t to)
override
180 data_[to]=data_[from];
196 const std::type_info&
type()
const override {
return typeid(T); }
218 assert(
size_t(_idx) < data_.size() );
225 assert(
size_t(_idx) < data_.size());
256 typedef typename PropertyArray<T>::reference reference;
257 typedef typename PropertyArray<T>::const_reference const_reference;
270 operator bool()
const
272 return parray_ !=
nullptr;
275 virtual reference operator[](
size_t i)
277 assert(parray_ !=
nullptr);
278 return (*parray_)[i];
281 virtual const_reference operator[](
size_t i)
const
283 assert(parray_ !=
nullptr);
284 return (*parray_)[i];
287 const T* data()
const
289 assert(parray_ !=
nullptr);
290 return parray_->data();
293 std::vector<T>& vector()
295 assert(parray_ !=
nullptr);
296 return parray_->vector();
299 const std::vector<T>& vector()
const
301 assert(parray_ !=
nullptr);
302 return parray_->vector();
307 assert(parray_ !=
nullptr);
313 assert(parray_ !=
nullptr);
318 const std::string&
name()
const {
319 assert(parray_ !=
nullptr);
320 return parray_->name();
325 assert(parray_ !=
nullptr);
326 parray_->set_name(n);
359 parrays_.resize(_rhs.n_properties());
361 for (
size_t i=0; i<parrays_.size(); ++i)
362 parrays_[i] = _rhs.parrays_[i]->clone();
369 for(
auto pa : parrays_) {
370 for (
auto rpa : _rhs.parrays_) {
371 if(pa->is_same (*rpa)){
382 for (
auto rpa : _rhs.parrays_)
384 bool property_already_exists =
false;
385 for(
auto pa : parrays_)
386 if (rpa->is_same(*pa))
388 property_already_exists =
true;
392 if (property_already_exists)
395 parrays_.push_back(rpa->empty_clone());
396 parrays_.back()->resize(size_);
405 for(std::size_t i=0; i<parrays_.size(); ++i)
406 if (!(parrays_[i]->transfer(* _rhs.parrays_[i], from, to)))
412 size_t size()
const {
return size_; }
415 size_t n_properties()
const {
return parrays_.size(); }
418 std::vector<std::string> properties()
const
420 std::vector<std::string> names;
421 for(
auto pa : parrays_)
422 names.push_back(pa->name());
428 template <
class T>
Property<T> add(
const std::string& name,
const T t=T())
431 for(
auto pa : parrays_)
433 if (pa->name() == name)
435 LOG(ERROR) <<
"A property with name \""
436 << name <<
"\" already exists. Returning invalid property.";
444 parrays_.push_back(p);
450 template <
class T>
Property<T> get(
const std::string& name)
const
452 for(
auto pa : parrays_)
453 if (pa->name() == name)
460 template <
class T>
Property<T> get_or_add(
const std::string& name,
const T t=T())
463 if (!p) p = add<T>(name, t);
469 const std::type_info& get_type(
const std::string& name)
const
471 for(
auto pa : parrays_)
472 if (pa->name() == name)
481 auto it=parrays_.begin(), end=parrays_.end();
482 for (; it!=end; ++it)
484 if (*it == h.parray_)
496 bool remove(
const std::string& name)
498 auto it=parrays_.begin(), end=parrays_.end();
499 for (; it!=end; ++it)
501 if ((*it)->name() == name)
512 bool rename(
const std::string& old_name,
const std::string& new_name)
514 assert(!old_name.empty());
515 assert(!new_name.empty());
516 auto it=parrays_.begin(), end=parrays_.end();
517 for (; it!=end; ++it)
519 if ((*it)->name() == old_name)
521 (*it)->set_name(new_name);
532 for(
auto pa : parrays_)
540 void reserve(
size_t n)
const
542 for(
auto pa : parrays_)
547 void resize(
size_t n)
549 for(
auto pa : parrays_)
555 void resize_property_array(
size_t n)
557 if (parrays_.size()<=n) {
561 for (std::size_t i=n; i<parrays_.size(); ++i)
567 void shrink_to_fit()
const
569 for(
auto pa : parrays_)
576 for(
auto pa : parrays_)
582 void reset(
size_t idx)
584 for(
auto pa : parrays_)
589 void swap(
size_t i0,
size_t i1)
const
591 for(
auto pa : parrays_)
598 this->parrays_.swap (other.parrays_);
599 std::swap(this->size_, other.size_);
603 void copy(
size_t from,
size_t to)
const
605 for(
auto pa : parrays_)
609 const std::vector<BasePropertyArray*>& arrays()
const {
return parrays_; }
610 std::vector<BasePropertyArray*>& arrays() {
return parrays_; }
613 std::vector<BasePropertyArray*> parrays_;
Base class for a property array.
Definition: property.h:41
virtual ~BasePropertyArray()=default
Destructor.
virtual bool transfer(const BasePropertyArray &other, std::size_t from, std::size_t to)=0
Copy the property[from] of other to this->property[to].
const std::string & name() const
Return the name of the property.
Definition: property.h:86
virtual void swap(size_t i0, size_t i1)=0
Let two elements swap their storage place.
virtual BasePropertyArray * clone() const =0
Return a deep copy of self.
virtual void push_back()=0
Extend the number of elements by one.
virtual void reserve(size_t n)=0
Reserve memory for n elements.
virtual BasePropertyArray * empty_clone() const =0
Return a empty copy of self.
virtual void resize(size_t n)=0
Resize storage to hold n elements.
bool is_same(const BasePropertyArray &other) const
Definition: property.h:93
virtual void copy(size_t from, size_t to)=0
Let copy 'from' -> 'to'.
virtual const std::type_info & type() const =0
Return the type_info of the property.
virtual void shrink_to_fit()=0
Free unused memory.
virtual void reset(size_t idx)=0
Reset element to default value.
virtual bool transfer(const BasePropertyArray &other)=0
Copy the entire properties from other.
BasePropertyArray(const std::string &name)
Default constructor.
Definition: property.h:45
void set_name(const std::string &n)
Set the name of the property.
Definition: property.h:89
Implementation of a generic property array.
Definition: property.h:111
void push_back() override
Extend the number of elements by one.
Definition: property.h:134
const std::type_info & type() const override
Return the type_info of the property.
Definition: property.h:196
reference operator[](size_t _idx)
Access the i'th element. No range check is performed!
Definition: property.h:216
void swap(size_t i0, size_t i1) override
Let two elements swap their storage place.
Definition: property.h:171
void copy(size_t from, size_t to) override
Let copy 'from' -> 'to'.
Definition: property.h:178
const T * data() const
Get pointer to array (does not work for T==bool)
Definition: property.h:202
BasePropertyArray * clone() const override
Return a deep copy of self.
Definition: property.h:183
bool transfer(const BasePropertyArray &other, std::size_t from, std::size_t to) override
Copy the property[from] of other to this->property[to].
Definition: property.h:154
BasePropertyArray * empty_clone() const override
Return a empty copy of self.
Definition: property.h:190
void reserve(size_t n) override
Reserve memory for n elements.
Definition: property.h:124
void shrink_to_fit() override
Free unused memory.
Definition: property.h:166
void reset(size_t idx) override
Reset element to default value.
Definition: property.h:139
std::vector< T > & vector()
Get reference to the underlying vector.
Definition: property.h:209
const_reference operator[](size_t _idx) const
Const access to the i'th element. No range check is performed!
Definition: property.h:223
void resize(size_t n) override
Resize storage to hold n elements.
Definition: property.h:129
bool transfer(const BasePropertyArray &other) override
Copy the entire properties from other.
Definition: property.h:144
Implementation of generic property container.
Definition: property.h:341
Implementation of a generic property.
Definition: property.h:253
const std::string & name() const
Return the name of the property.
Definition: property.h:318
void set_name(const std::string &n)
Set the name of the property.
Definition: property.h:324
Definition: collider.cpp:182
void swap(Matrix< FT > &, Matrix< FT > &)
Definition: matrix.h:1439