Easy3D 2.5.3
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 virtual Model
45 {
46
47 public: //------------------------------------------------------ topology types
48
49
53 {
54 public:
55
57 explicit BaseHandle(int _idx=-1) : idx_(_idx) {}
58
60 int idx() const { return idx_; }
61
63 void reset() { idx_=-1; }
64
66 bool is_valid() const { return idx_ != -1; }
67
69 bool operator==(const BaseHandle& _rhs) const {
70 return idx_ == _rhs.idx_;
71 }
72
74 bool operator!=(const BaseHandle& _rhs) const {
75 return idx_ != _rhs.idx_;
76 }
77
79 bool operator<(const BaseHandle& _rhs) const {
80 return idx_ < _rhs.idx_;
81 }
82
84 struct Hash {
85 std::size_t operator()(const BaseHandle& h) const { return h.idx(); }
86 };
87
88 private:
89 friend class PointCloud;
90 int idx_;
91 };
92
93
95 struct Vertex : public BaseHandle
96 {
98 explicit Vertex(int _idx=-1) : BaseHandle(_idx) {}
99 std::ostream& operator<<(std::ostream& os) const { return os << 'v' << idx(); }
100 };
101
102
103 public: //------------------------------------------------------ property types
104
106 template <class T> class VertexProperty : public Property<T>
107 {
108 public:
109
111 VertexProperty() = default;
112 explicit VertexProperty(Property<T> p) : Property<T>(p) {}
113
115 typename Property<T>::reference operator[](Vertex v)
116 {
117 return Property<T>::operator[](v.idx());
118 }
119
121 typename Property<T>::const_reference operator[](Vertex v) const
122 {
123 return Property<T>::operator[](v.idx());
124 }
125 };
126
127
130 template <class T> class ModelProperty : public Property<T>
131 {
132 public:
133
135 ModelProperty() = default;
136 explicit ModelProperty(Property<T> p) : Property<T>(p) {}
137
139 typename Property<T>::reference operator[](size_t idx)
140 {
141 return Property<T>::operator[](idx);
142 }
143
145 typename Property<T>::const_reference operator[](size_t idx) const
146 {
147 return Property<T>::operator[](idx);
148 }
149 };
150
151
152
153 public: //------------------------------------------------------ iterator types
154
158 {
159 public:
160
162 explicit VertexIterator(Vertex v=Vertex(), const PointCloud* m=nullptr) : hnd_(v), cloud_(m)
163 {
164 if (cloud_ && cloud_->has_garbage()) while (cloud_->is_valid(hnd_) && cloud_->is_deleted(hnd_)) ++hnd_.idx_;
165 }
166
168 Vertex operator*() const { return hnd_; }
169
171 bool operator==(const VertexIterator& rhs) const
172 {
173 return (hnd_==rhs.hnd_);
174 }
175
177 bool operator!=(const VertexIterator& rhs) const
178 {
179 return !operator==(rhs);
180 }
181
184 {
185 ++hnd_.idx_;
186 assert(cloud_);
187 while (cloud_->has_garbage() && cloud_->is_valid(hnd_) && cloud_->is_deleted(hnd_)) ++hnd_.idx_;
188 return *this;
189 }
190
193 {
194 --hnd_.idx_;
195 assert(cloud_);
196 while (cloud_->has_garbage() && cloud_->is_valid(hnd_) && cloud_->is_deleted(hnd_)) --hnd_.idx_;
197 return *this;
198 }
199
200 private:
201 Vertex hnd_;
202 const PointCloud* cloud_;
203 };
204
205
206 public: //-------------------------- containers for C++11 range-based for loops
207
212 {
213 public:
214 VertexContainer(VertexIterator _begin, VertexIterator _end) : begin_(_begin), end_(_end) {}
215 VertexIterator begin() const { return begin_; }
216 VertexIterator end() const { return end_; }
217 private:
218 VertexIterator begin_, end_;
219 };
220
221
222 public: //-------------------------------------------- constructor / destructor
223
225
226
228 PointCloud();
229
231 ~PointCloud() override = default;
232
234 PointCloud(const PointCloud& rhs) { operator=(rhs); }
235
237 PointCloud& operator=(const PointCloud& rhs);
238
244 PointCloud& operator+=(const PointCloud& other) { join(other); return *this; }
245
251 PointCloud& join(const PointCloud& other);
252
254 PointCloud& assign(const PointCloud& rhs);
255
257
258
259 public: //----------------------------------------------- add new vertex
260
262
263
265 Vertex add_vertex(const vec3& p);
266
268
269
270 public: //--------------------------------------------------- memory management
271
273
274
276 unsigned int vertices_size() const { return (unsigned int) vprops_.size(); }
277
279 unsigned int n_vertices() const { return vertices_size() - deleted_vertices_; }
280
282 void clear();
283
285 void resize(unsigned int nv) { vprops_.resize(nv); }
286
288 bool has_garbage() const { return garbage_; }
289
291 void collect_garbage();
292
294 void delete_vertex(Vertex v);
295
298 bool is_deleted(Vertex v) const
299 {
300 return vdeleted_[v];
301 }
302
304 bool is_valid(Vertex v) const
305 {
306 return (0 <= v.idx()) && (v.idx() < (int)vertices_size());
307 }
308
310
311 public: //--------------------------------------------------- property handling
312
314
315
319 template <class T> VertexProperty<T> add_vertex_property(const std::string& name, const T t=T())
320 {
321 return VertexProperty<T>(vprops_.add<T>(name, t));
322 }
333 template <class T> ModelProperty<T> add_model_property(const std::string& name, const T t = T())
334 {
335 return ModelProperty<T>(mprops_.add<T>(name, t));
336 }
337
340 template <class T> VertexProperty<T> get_vertex_property(const std::string& name) const
341 {
342 return VertexProperty<T>(vprops_.get<T>(name));
343 }
354 template <class T> ModelProperty<T> get_model_property(const std::string& name) const
355 {
356 return ModelProperty<T>(mprops_.get<T>(name));
357 }
358
361 template <class T> VertexProperty<T> vertex_property(const std::string& name, const T t=T())
362 {
363 return VertexProperty<T>(vprops_.get_or_add<T>(name, t));
364 }
367 template <class T> ModelProperty<T> model_property(const std::string& name, const T t = T())
368 {
369 return ModelProperty<T>(mprops_.get_or_add<T>(name, t));
370 }
371
373 template<class T>
374 bool remove_vertex_property(VertexProperty<T> &p) { return vprops_.remove(p); }
375
377 bool remove_vertex_property(const std::string &n) { return vprops_.remove(n); }
378
380 template<class T>
381 bool remove_model_property(ModelProperty<T> &p) { return mprops_.remove(p); }
382
384 bool remove_model_property(const std::string &n) { return mprops_.remove(n); }
385
387 bool rename_vertex_property(const std::string &old_name, const std::string &new_name) {
388 return vprops_.rename(old_name, new_name);
389 }
390
392 bool rename_model_property(const std::string &old_name, const std::string &new_name) {
393 return mprops_.rename(old_name, new_name);
394 }
395
398 const std::type_info& get_vertex_property_type(const std::string& name) const
399 {
400 return vprops_.get_type(name);
401 }
404 const std::type_info& get_model_property_type(const std::string& name) const
405 {
406 return mprops_.get_type(name);
407 }
408
410 std::vector<std::string> vertex_properties() const
411 {
412 return vprops_.properties();
413 }
415 std::vector<std::string> model_properties() const
416 {
417 return mprops_.properties();
418 }
419
421 void property_stats(std::ostream &output) const override;
422
424
425
426 public: //--------------------------------------------- iterators
427
429
430
433 {
434 return VertexIterator(Vertex(0), this);
435 }
436
439 {
440 return VertexIterator(Vertex(static_cast<int>(vertices_size())), this);
441 }
442
445 {
447 }
448
450
451 public: //------------------------------------------ geometry-related functions
452
454
455
457 const vec3& position(Vertex v) const { return vpoint_[v]; }
458
460 vec3& position(Vertex v) { return vpoint_[v]; }
461
463 const std::vector<vec3>& points() const override { return vpoint_.vector(); }
464
466 std::vector<vec3>& points() override { return vpoint_.vector(); }
467
469
470 private: //---------------------------------------------- allocate new elements
471
473 Vertex new_vertex()
474 {
475 vprops_.push_back();
476 return Vertex(static_cast<int>(vertices_size()-1));
477 }
478
479
480 private: //------------------------------------------------------- private data
481
482 PropertyContainer vprops_;
483 PropertyContainer mprops_;
484
485 VertexProperty<bool> vdeleted_;
486 VertexProperty<vec3> vpoint_;
487
488 unsigned int deleted_vertices_;
489 bool garbage_;
490 };
491
492
493 //------------------------------------------------------------ output operators
494
496 inline std::ostream& operator<<(std::ostream& os, PointCloud::Vertex v)
497 {
498 return (os << 'v' << v.idx());
499 }
500
501} // namespace easy3d
502
503#endif // EASY3D_CORE_POINT_CLOUD_H
The base class of renderable 3D models.
Definition: model.h:49
const std::string & name() const
The name of a model.
Definition: model.h:60
Base class for topology types (internally it is basically an index)
Definition: point_cloud.h:53
bool operator!=(const BaseHandle &_rhs) const
are two handles different?
Definition: point_cloud.h:74
bool is_valid() const
return whether the handle is valid, i.e., the index is not equal to -1.
Definition: point_cloud.h:66
int idx() const
Get the underlying index of this handle.
Definition: point_cloud.h:60
bool operator<(const BaseHandle &_rhs) const
compare operator useful for sorting handles
Definition: point_cloud.h:79
bool operator==(const BaseHandle &_rhs) const
are two handles equal?
Definition: point_cloud.h:69
void reset()
reset handle to be invalid (index=-1)
Definition: point_cloud.h:63
BaseHandle(int _idx=-1)
constructor
Definition: point_cloud.h:57
Cloud property of type T.
Definition: point_cloud.h:131
ModelProperty()=default
default constructor
Property< T >::const_reference operator[](size_t idx) const
access the data stored for the cloud
Definition: point_cloud.h:145
Property< T >::reference operator[](size_t idx)
access the data stored for the cloud
Definition: point_cloud.h:139
this helper class is a container for iterating through all vertices using C++11 range-based for-loops...
Definition: point_cloud.h:212
this class iterates linearly over all vertices
Definition: point_cloud.h:158
VertexIterator(Vertex v=Vertex(), const PointCloud *m=nullptr)
Default constructor.
Definition: point_cloud.h:162
Vertex operator*() const
get the vertex the iterator refers to
Definition: point_cloud.h:168
bool operator==(const VertexIterator &rhs) const
are two iterators equal?
Definition: point_cloud.h:171
VertexIterator & operator--()
pre-decrement iterator
Definition: point_cloud.h:192
bool operator!=(const VertexIterator &rhs) const
are two iterators different?
Definition: point_cloud.h:177
VertexIterator & operator++()
pre-increment iterator
Definition: point_cloud.h:183
Vertex property of type T.
Definition: point_cloud.h:107
Property< T >::const_reference operator[](Vertex v) const
access the data stored for vertex v
Definition: point_cloud.h:121
Property< T >::reference operator[](Vertex v)
access the data stored for vertex v
Definition: point_cloud.h:115
VertexProperty()=default
default constructor
A data structure for point clouds.
Definition: point_cloud.h:45
const vec3 & position(Vertex v) const
position of a vertex (read only)
Definition: point_cloud.h:457
const std::type_info & get_vertex_property_type(const std::string &name) const
get the type_info T of vertex property name. returns an typeid(void) if the property does not exist o...
Definition: point_cloud.h:398
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:377
PointCloud & operator+=(const PointCloud &other)
Merges another point cloud into the current one. Shifts the indices of vertices of the other point cl...
Definition: point_cloud.h:244
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:387
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: point_cloud.h:444
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:288
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
if a vertex property of type T with name name exists, it is returned. otherwise this property is adde...
Definition: point_cloud.h:361
ModelProperty< T > get_model_property(const std::string &name) const
Gets the model property named name of type T.
Definition: point_cloud.h:354
std::vector< std::string > vertex_properties() const
returns the names of all vertex properties
Definition: point_cloud.h:410
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. returns an invalid VertexProperty if the property does ...
Definition: point_cloud.h:340
bool is_deleted(Vertex v) const
returns whether vertex v is deleted
Definition: point_cloud.h:298
std::vector< std::string > model_properties() const
returns the names of all model properties
Definition: point_cloud.h:415
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. returns an typeid(void) if the property does not exist or...
Definition: point_cloud.h:404
std::vector< vec3 > & points() override
vector of vertex positions
Definition: point_cloud.h:466
ModelProperty< T > model_property(const std::string &name, const T t=T())
if a model property of type T with name name exists, it is returned. otherwise this property is added...
Definition: point_cloud.h:367
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. fails if a property named name ex...
Definition: point_cloud.h:319
bool remove_model_property(ModelProperty< T > &p)
remove the model property p
Definition: point_cloud.h:381
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:392
ModelProperty< T > add_model_property(const std::string &name, const T t=T())
Adds a model property of type T with name name and default value t.
Definition: point_cloud.h:333
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:285
PointCloud(const PointCloud &rhs)
copy constructor: copies rhs to *this. performs a deep copy of all properties.
Definition: point_cloud.h:234
PointCloud & join(const PointCloud &other)
Merges another point cloud into the current one. Shifts the indices of vertices of the other point cl...
Definition: point_cloud.cpp:73
~PointCloud() override=default
destructor (is virtual, since we inherit from Geometry_representation)
vec3 & position(Vertex v)
position of a vertex
Definition: point_cloud.h:460
unsigned int vertices_size() const
returns number of (deleted and valid) vertices in the cloud
Definition: point_cloud.h:276
bool remove_model_property(const std::string &n)
remove the model property named n
Definition: point_cloud.h:384
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:432
bool remove_vertex_property(VertexProperty< T > &p)
remove the vertex property p
Definition: point_cloud.h:374
VertexIterator vertices_end() const
returns end iterator for vertices
Definition: point_cloud.h:438
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:304
const std::vector< vec3 > & points() const override
vector of vertex positions (read only)
Definition: point_cloud.h:463
unsigned int n_vertices() const
returns number of vertices in the cloud
Definition: point_cloud.h:279
Vertex add_vertex(const vec3 &p)
add a new vertex with position p
Definition: point_cloud.cpp:177
Implementation of a generic property.
Definition: property.h:253
Definition: collider.cpp:182
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Definition: graph.h:920
helper structure to be able to use std::unordered_map
Definition: point_cloud.h:84
this type represents a vertex (internally it is basically an index)
Definition: point_cloud.h:96
Vertex(int _idx=-1)
default constructor (with invalid index)
Definition: point_cloud.h:98