Easy3D 2.6.1
Loading...
Searching...
No Matches
point_cloud.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27
28#ifndef EASY3D_CORE_POINT_CLOUD_H
29#define EASY3D_CORE_POINT_CLOUD_H
30
31#include <easy3d/core/model.h>
32#include <easy3d/core/types.h>
33#include <easy3d/core/property.h>
34
35namespace easy3d {
36
44 class PointCloud : public Model
45 {
46
47 public: //------------------------------------------------------ topology types
48
55 {
56 public:
61 explicit BaseHandle(int _idx=-1) : idx_(_idx) {}
62
67 int idx() const { return idx_; }
68
72 void reset() { idx_=-1; }
73
78 bool is_valid() const { return idx_ != -1; }
79
85 bool operator==(const BaseHandle& _rhs) const {
86 return idx_ == _rhs.idx_;
87 }
88
94 bool operator!=(const BaseHandle& _rhs) const {
95 return idx_ != _rhs.idx_;
96 }
97
103 bool operator<(const BaseHandle& _rhs) const {
104 return idx_ < _rhs.idx_;
105 }
106
110 struct Hash {
116 std::size_t operator()(const BaseHandle& h) const { return h.idx(); }
117 };
118
119 private:
120 friend class PointCloud;
121 int idx_;
122 };
123
124
134 explicit Vertex(int _idx=-1) : BaseHandle(_idx) {}
140 std::ostream& operator<<(std::ostream& os) const { return os << 'v' << idx(); }
141 };
142
143
144 public: //------------------------------------------------------ property types
145
150 template <class T> class VertexProperty : public Property<T>
151 {
152 public:
156 VertexProperty() = default;
161 explicit VertexProperty(Property<T> p) : Property<T>(p) {}
162
169 {
170 return Property<T>::operator[](v.idx());
171 }
172
179 {
180 return Property<T>::operator[](v.idx());
181 }
182 };
183
184
190 template <class T> class ModelProperty : public Property<T>
191 {
192 public:
196 ModelProperty() = default;
201 explicit ModelProperty(Property<T> p) : Property<T>(p) {}
202
208 typename Property<T>::reference operator[](size_t idx) override {
209 return Property<T>::operator[](idx);
210 }
211
217 typename Property<T>::const_reference operator[](size_t idx) const override {
218 return Property<T>::operator[](idx);
219 }
220 };
221
222
223
224 public: //------------------------------------------------------ iterator types
225
232 {
233 public:
239 explicit VertexIterator(Vertex v=Vertex(), const PointCloud* m=nullptr) : hnd_(v), cloud_(m)
240 {
241 if (cloud_ && cloud_->has_garbage()) while (cloud_->is_valid(hnd_) && cloud_->is_deleted(hnd_)) ++hnd_.idx_;
242 }
243
248 Vertex operator*() const { return hnd_; }
249
255 bool operator==(const VertexIterator& rhs) const
256 {
257 return (hnd_==rhs.hnd_);
258 }
259
265 bool operator!=(const VertexIterator& rhs) const
266 {
267 return !operator==(rhs);
268 }
269
275 {
276 ++hnd_.idx_;
277 assert(cloud_);
278 while (cloud_->has_garbage() && cloud_->is_valid(hnd_) && cloud_->is_deleted(hnd_)) ++hnd_.idx_;
279 return *this;
280 }
281
287 {
288 --hnd_.idx_;
289 assert(cloud_);
290 while (cloud_->has_garbage() && cloud_->is_valid(hnd_) && cloud_->is_deleted(hnd_)) --hnd_.idx_;
291 return *this;
292 }
293
294 private:
295 Vertex hnd_;
296 const PointCloud* cloud_;
297 };
298
299
300 public: //-------------------------- containers for C++11 range-based for loops
301
309 {
310 public:
316 VertexContainer(VertexIterator _begin, VertexIterator _end) : begin_(_begin), end_(_end) {}
321 VertexIterator begin() const { return begin_; }
326 VertexIterator end() const { return end_; }
327 private:
328 VertexIterator begin_, end_;
329 };
330
331
332 public: //-------------------------------------------- constructor / destructor
333
335
336
340 PointCloud();
341
345 ~PointCloud() override = default;
346
351 PointCloud(const PointCloud& rhs) { operator=(rhs); }
352
358 PointCloud& operator=(const PointCloud& rhs);
359
369 PointCloud& operator+=(const PointCloud& other) { join(other); return *this; }
370
380 PointCloud& join(const PointCloud& other);
381
387 PointCloud& assign(const PointCloud& rhs);
388
390
391
392 public: //----------------------------------------------- add new vertex
393
395
396
402 Vertex add_vertex(const vec3& p);
403
405
406
407 public: //--------------------------------------------------- memory management
408
410
411
416 unsigned int vertices_size() const { return static_cast<unsigned int>(vprops_.size()); }
417
422 unsigned int n_vertices() const { return vertices_size() - deleted_vertices_; }
423
427 void clear();
428
433 void resize(unsigned int nv) { vprops_.resize(nv); }
434
440 bool has_garbage() const { return garbage_; }
441
446 void collect_garbage();
447
453 void delete_vertex(Vertex v);
454
461 bool is_deleted(Vertex v) const {
462 return vdeleted_[v];
463 }
464
470 bool is_valid(Vertex v) const {
471 return (0 <= v.idx()) && (v.idx() < static_cast<int>(vertices_size()));
472 }
473
475
476 public: //--------------------------------------------------- property handling
477
479
480
488 template <class T> VertexProperty<T> add_vertex_property(const std::string& name, const T t=T()) {
489 return VertexProperty<T>(vprops_.add<T>(name, t));
490 }
491
503 template <class T> ModelProperty<T> add_model_property(const std::string& name, const T t = T()) {
504 return ModelProperty<T>(mprops_.add<T>(name, t));
505 }
506
513 template <class T> VertexProperty<T> get_vertex_property(const std::string& name) const {
514 return VertexProperty<T>(vprops_.get<T>(name));
515 }
516
528 template <class T> ModelProperty<T> get_model_property(const std::string& name) const {
529 return ModelProperty<T>(mprops_.get<T>(name));
530 }
531
539 template <class T> VertexProperty<T> vertex_property(const std::string& name, const T t=T()) {
540 return VertexProperty<T>(vprops_.get_or_add<T>(name, t));
541 }
542
549 template <class T> ModelProperty<T> model_property(const std::string& name, const T t = T()) {
550 return ModelProperty<T>(mprops_.get_or_add<T>(name, t));
551 }
552
558 template<class T>
559 bool remove_vertex_property(VertexProperty<T> &p) { return vprops_.remove(p); }
560
566 bool remove_vertex_property(const std::string &n) { return vprops_.remove(n); }
567
573 template<class T>
574 bool remove_model_property(ModelProperty<T> &p) { return mprops_.remove(p); }
575
581 bool remove_model_property(const std::string &n) { return mprops_.remove(n); }
582
589 bool rename_vertex_property(const std::string &old_name, const std::string &new_name) {
590 return vprops_.rename(old_name, new_name);
591 }
592
599 bool rename_model_property(const std::string &old_name, const std::string &new_name) {
600 return mprops_.rename(old_name, new_name);
601 }
602
609 const std::type_info& get_vertex_property_type(const std::string& name) const {
610 return vprops_.get_type(name);
611 }
612
618 const std::type_info& get_model_property_type(const std::string& name) const {
619 return mprops_.get_type(name);
620 }
621
626 std::vector<std::string> vertex_properties() const {
627 return vprops_.properties();
628 }
629
633 std::vector<std::string> model_properties() const {
634 return mprops_.properties();
635 }
636
641 void property_stats(std::ostream &output) const override;
642
644
645
646 public: //--------------------------------------------- iterators
647
649
650
656 return VertexIterator(Vertex(0), this);
657 }
658
664 return VertexIterator(Vertex(static_cast<int>(vertices_size())), this);
665 }
666
672 return {vertices_begin(), vertices_end()};
673 }
674
676
677 public: //------------------------------------------ geometry-related functions
678
680
681
687 const vec3& position(Vertex v) const { return vpoint_[v]; }
688
694 vec3& position(Vertex v) { return vpoint_[v]; }
695
700 const std::vector<vec3>& points() const override { return vpoint_.vector(); }
701
706 std::vector<vec3>& points() override { return vpoint_.vector(); }
707
709
710 private: //---------------------------------------------- allocate new elements
711
713 Vertex new_vertex()
714 {
715 vprops_.push_back();
716 return Vertex(static_cast<int>(vertices_size()-1));
717 }
718
719
720 private: //------------------------------------------------------- private data
721
722 PropertyContainer vprops_;
723 PropertyContainer mprops_;
724
725 VertexProperty<bool> vdeleted_;
726 VertexProperty<vec3> vpoint_;
727
728 unsigned int deleted_vertices_;
729 bool garbage_;
730 };
731
732
733 //------------------------------------------------------------ output operators
734
741 inline std::ostream& operator<<(std::ostream& os, PointCloud::Vertex v) {
742 return (os << 'v' << v.idx());
743 }
744
745} // namespace easy3d
746
747#endif // EASY3D_CORE_POINT_CLOUD_H
const std::string & name() const
The name of a model.
Definition model.h:61
Model(const std::string &name="unknown")
Default constructor. The parameter name is optional, but it is useful for handling multiple models wi...
Definition model.cpp:33
bool operator!=(const BaseHandle &_rhs) const
Are two handles different?
Definition point_cloud.h:94
bool is_valid() const
Return whether the handle is valid, i.e., the index is not equal to -1.
Definition point_cloud.h:78
int idx() const
Get the underlying index of this handle.
Definition point_cloud.h:67
bool operator<(const BaseHandle &_rhs) const
Compare operator useful for sorting handles.
Definition point_cloud.h:103
bool operator==(const BaseHandle &_rhs) const
Are two handles equal?
Definition point_cloud.h:85
void reset()
Reset handle to be invalid (index = -1).
Definition point_cloud.h:72
BaseHandle(int _idx=-1)
Constructor.
Definition point_cloud.h:61
Cloud property of type T.
Definition point_cloud.h:191
ModelProperty()=default
Default constructor.
Property< T >::reference operator[](size_t idx) override
Access the data stored for the cloud.
Definition point_cloud.h:208
ModelProperty(Property< T > p)
Constructor with property.
Definition point_cloud.h:201
Property< T >::const_reference operator[](size_t idx) const override
Access the data stored for the cloud.
Definition point_cloud.h:217
This helper class is a container for iterating through all vertices using C++11 range-based for-loops...
Definition point_cloud.h:309
VertexContainer(VertexIterator _begin, VertexIterator _end)
Constructor.
Definition point_cloud.h:316
VertexIterator begin() const
Get the begin iterator.
Definition point_cloud.h:321
VertexIterator end() const
Get the end iterator.
Definition point_cloud.h:326
This class iterates linearly over all vertices.
Definition point_cloud.h:232
VertexIterator(Vertex v=Vertex(), const PointCloud *m=nullptr)
Default constructor.
Definition point_cloud.h:239
Vertex operator*() const
Get the vertex the iterator refers to.
Definition point_cloud.h:248
bool operator==(const VertexIterator &rhs) const
Are two iterators equal?
Definition point_cloud.h:255
VertexIterator & operator--()
Pre-decrement iterator.
Definition point_cloud.h:286
bool operator!=(const VertexIterator &rhs) const
Are two iterators different?
Definition point_cloud.h:265
VertexIterator & operator++()
Pre-increment iterator.
Definition point_cloud.h:274
Vertex property of type T.
Definition point_cloud.h:151
VertexProperty(Property< T > p)
Constructor with property.
Definition point_cloud.h:161
Property< T >::const_reference operator[](Vertex v) const
Access the data stored for vertex v.
Definition point_cloud.h:178
Property< T >::reference operator[](Vertex v)
Access the data stored for vertex v.
Definition point_cloud.h:168
VertexProperty()=default
Default constructor.
A data structure for point clouds.
Definition point_cloud.h:45
const vec3 & position(Vertex v) const
Gets the position of a vertex (read only).
Definition point_cloud.h:687
const std::type_info & get_vertex_property_type(const std::string &name) const
Get the type_info T of vertex property name.
Definition point_cloud.h:609
PointCloud()
Default constructor.
Definition point_cloud.cpp:32
bool remove_vertex_property(const std::string &n)
Remove the vertex property named n.
Definition point_cloud.h:566
PointCloud & operator+=(const PointCloud &other)
Merges another point cloud into the current one.
Definition point_cloud.h:369
bool rename_vertex_property(const std::string &old_name, const std::string &new_name)
Rename a vertex property given its name.
Definition point_cloud.h:589
VertexContainer vertices() const
Returns vertex container for C++11 range-based for-loops.
Definition point_cloud.h:671
PointCloud & operator=(const PointCloud &rhs)
Assign rhs to *this. Performs a deep copy of all properties.
Definition point_cloud.cpp:49
bool has_garbage() const
Are there deleted vertices?
Definition point_cloud.h:440
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
Gets or adds a vertex property of type T with name name.
Definition point_cloud.h:539
ModelProperty< T > get_model_property(const std::string &name) const
Gets the model property named name of type T.
Definition point_cloud.h:528
std::vector< std::string > vertex_properties() const
Returns the names of all vertex properties.
Definition point_cloud.h:626
PointCloud & assign(const PointCloud &rhs)
Assign rhs to *this. Does not copy custom properties.
Definition point_cloud.cpp:90
VertexProperty< T > get_vertex_property(const std::string &name) const
Get the vertex property named name of type T.
Definition point_cloud.h:513
bool is_deleted(Vertex v) const
Returns whether vertex v is deleted.
Definition point_cloud.h:461
std::vector< std::string > model_properties() const
Returns the names of all model properties.
Definition point_cloud.h:633
void collect_garbage()
Remove deleted vertices.
Definition point_cloud.cpp:202
const std::type_info & get_model_property_type(const std::string &name) const
Get the type_info T of model property name.
Definition point_cloud.h:618
std::vector< vec3 > & points() override
Returns the vector of vertex positions.
Definition point_cloud.h:706
ModelProperty< T > model_property(const std::string &name, const T t=T())
Gets or adds a model property of type T with name name.
Definition point_cloud.h:549
VertexProperty< T > add_vertex_property(const std::string &name, const T t=T())
Add a vertex property of type T with name name and default value t.
Definition point_cloud.h:488
bool remove_model_property(ModelProperty< T > &p)
Remove the model property p.
Definition point_cloud.h:574
bool rename_model_property(const std::string &old_name, const std::string &new_name)
Rename a model property given its name.
Definition point_cloud.h:599
ModelProperty< T > add_model_property(const std::string &name, const T t=T())
Add a vertex property of type T with name name and default value t.
Definition point_cloud.h:503
void property_stats(std::ostream &output) const override
Prints the names of all properties to an output stream (e.g., std::cout).
Definition point_cloud.cpp:144
void clear()
Clear cloud: remove all vertices.
Definition point_cloud.cpp:122
void resize(unsigned int nv)
Resize space for vertices and their currently associated properties.
Definition point_cloud.h:433
PointCloud(const PointCloud &rhs)
Copy constructor: copies rhs to *this. Performs a deep copy of all properties.
Definition point_cloud.h:351
PointCloud & join(const PointCloud &other)
Merges another point cloud into the current one.
Definition point_cloud.cpp:73
~PointCloud() override=default
Destructor (is virtual, since we inherit from Model).
vec3 & position(Vertex v)
Gets the position of a vertex.
Definition point_cloud.h:694
unsigned int vertices_size() const
Returns number of (deleted and valid) vertices in the cloud.
Definition point_cloud.h:416
bool remove_model_property(const std::string &n)
Remove the model property named n.
Definition point_cloud.h:581
void delete_vertex(Vertex v)
Deletes the vertex v from the cloud.
Definition point_cloud.cpp:188
VertexIterator vertices_begin() const
Returns start iterator for vertices.
Definition point_cloud.h:655
bool remove_vertex_property(VertexProperty< T > &p)
Remove the vertex property p.
Definition point_cloud.h:559
VertexIterator vertices_end() const
Returns end iterator for vertices.
Definition point_cloud.h:663
bool is_valid(Vertex v) const
Return whether vertex v is valid, i.e. the index is stores it within the array bounds.
Definition point_cloud.h:470
const std::vector< vec3 > & points() const override
Returns the vector of vertex positions (read only).
Definition point_cloud.h:700
unsigned int n_vertices() const
Returns number of vertices in the cloud.
Definition point_cloud.h:422
Vertex add_vertex(const vec3 &p)
Add a new vertex with position p.
Definition point_cloud.cpp:177
void push_back()
Adds a new element to each vector.
Definition property.h:771
PropertyArray< T >::const_reference const_reference
The const reference type of the property.
Definition property.h:329
virtual reference operator[](size_t i)
Accesses the i-th element.
Definition property.h:365
Property(PropertyArray< T > *p=nullptr)
Constructor.
Definition property.h:338
PropertyArray< T >::reference reference
The reference type of the property.
Definition property.h:328
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
Helper structure to be able to use std::unordered_map.
Definition point_cloud.h:110
std::size_t operator()(const BaseHandle &h) const
Hash function for BaseHandle.
Definition point_cloud.h:116
This type represents a vertex (internally it is basically an index).
Definition point_cloud.h:129
std::ostream & operator<<(std::ostream &os) const
Output stream operator for Vertex.
Definition point_cloud.h:140
Vertex(int _idx=-1)
Default constructor (with invalid index).
Definition point_cloud.h:134