-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathsurface_mesh.h
2085 lines (1702 loc) · 75.3 KB
/
surface_mesh.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
* Copyright (C) 2015-2021 by Liangliang Nan <[email protected]>
* Copyright (C) 2011-2013 by Graphics & Geometry Group, Bielefeld University
* Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen
*
* The code in this file is partly from Surface_mesh (v1.1) with
* modifications and enhancement:
* https://opensource.cit-ec.de/projects/surface_mesh
* The original code was distributed under the GNU GPL License.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation, version 2.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
********************************************************************/
#ifndef EASY3D_CORE_SURFACE_MESH_H
#define EASY3D_CORE_SURFACE_MESH_H
#include <easy3d/core/model.h>
#include <easy3d/core/types.h>
#include <easy3d/core/property.h>
namespace easy3d {
/**
* @brief A halfedge data structure for polygonal meshes of 2-manifold.
*
* @details A surface mesh is a consistent and orientable polygonal mesh that may have one or more boundaries.
* The faces are simple polygons and the edges are line segments. Each edge connects two vertices, and is
* shared by two faces (including the null face for boundary edges). A surface mesh can have any number
* of connected components and also some self-intersections.
*
* @note The construction of a manifold surface mesh can be done by iteratively calling add_vertex() and
* add_face(). These two methods can ONLY be used when you're sure that the mesh is manifold. Otherwise,
* SurfaceMeshBuilder should be used for the construction, which guarantees you end up with a polygonal
* mesh of a 2-manifold topology. In any case, client code is highly recommended to use SurfaceMeshBuilder.
*
* \class SurfaceMesh easy3d/core/surface_mesh.h
* \sa SurfaceMeshBuilder.
*/
class SurfaceMesh : public virtual Model
{
public: //------------------------------------------------------ topology types
/// Base class for all topology types (internally it is basically an index)
/// \sa Vertex, Halfedge, Edge, Face
class BaseHandle
{
public:
/// constructor
explicit BaseHandle(int _idx=-1) : idx_(_idx) {}
/// Get the underlying index of this handle
int idx() const { return idx_; }
/// reset handle to be invalid (index=-1)
void reset() { idx_=-1; }
/// return whether the handle is valid, i.e., the index is not equal to -1.
bool is_valid() const { return idx_ != -1; }
/// are two handles equal?
bool operator==(const BaseHandle& _rhs) const {
return idx_ == _rhs.idx_;
}
/// are two handles different?
bool operator!=(const BaseHandle& _rhs) const {
return idx_ != _rhs.idx_;
}
/// compare operator useful for sorting handles
bool operator<(const BaseHandle& _rhs) const {
return idx_ < _rhs.idx_;
}
/// helper structure to be able to use std::unordered_map
struct Hash {
std::size_t operator()(const BaseHandle& h) const { return h.idx(); }
};
private:
friend class SurfaceMesh;
int idx_;
};
/// this type represents a vertex (internally it is basically an index)
/// \sa Halfedge, Edge, Face
struct Vertex : public BaseHandle
{
/// default constructor (with invalid index)
explicit Vertex(int _idx=-1) : BaseHandle(_idx) {}
std::ostream& operator<<(std::ostream& os) const { return os << 'v' << idx(); }
};
/// this type represents a halfedge (internally it is basically an index)
/// \sa Vertex, Edge, Face
struct Halfedge : public BaseHandle
{
/// default constructor (with invalid index)
explicit Halfedge(int _idx=-1) : BaseHandle(_idx) {}
std::ostream& operator<<(std::ostream& os) const { return os << 'h' << idx(); }
};
/// this type represents an edge (internally it is basically an index)
/// \sa Vertex, Halfedge, Face
struct Edge : public BaseHandle
{
/// default constructor (with invalid index)
explicit Edge(int _idx=-1) : BaseHandle(_idx) {}
std::ostream& operator<<(std::ostream& os) const { return os << 'e' << idx(); }
};
/// this type represents a face (internally it is basically an index)
/// \sa Vertex, Halfedge, Edge
struct Face : public BaseHandle
{
/// default constructor (with invalid index)
explicit Face(int _idx=-1) : BaseHandle(_idx) {}
std::ostream& operator<<(std::ostream& os) const { return os << 'f' << idx(); }
};
public: //-------------------------------------------------- connectivity types
/// This type stores the vertex connectivity
/// \sa HalfedgeConnectivity, FaceConnectivity
struct VertexConnectivity
{
/// an outgoing halfedge per vertex (it will be a boundary halfedge for boundary vertices)
Halfedge halfedge_;
};
/// This type stores the halfedge connectivity
/// \sa VertexConnectivity, FaceConnectivity
struct HalfedgeConnectivity
{
/// face incident to halfedge
Face face_;
/// vertex the halfedge points to
Vertex vertex_;
/// next halfedge within a face (or along a boundary)
Halfedge next_;
/// previous halfedge within a face (or along a boundary)
Halfedge prev_;
};
/// This type stores the face connectivity
/// \sa VertexConnectivity, HalfedgeConnectivity
struct FaceConnectivity
{
/// a halfedge that is part of the face
Halfedge halfedge_;
};
public: //------------------------------------------------------ property types
/// Vertex property of type T
/// \sa HalfedgeProperty, EdgeProperty, FaceProperty
template <class T> class VertexProperty : public Property<T>
{
public:
/// default constructor
VertexProperty() = default;
explicit VertexProperty(Property<T> p) : Property<T>(p) {}
/// access the data stored for vertex \c v
typename Property<T>::reference operator[](Vertex v)
{
return Property<T>::operator[](v.idx());
}
/// access the data stored for vertex \c v
typename Property<T>::const_reference operator[](Vertex v) const
{
return Property<T>::operator[](v.idx());
}
};
/// Halfedge property of type T
/// \sa VertexProperty, EdgeProperty, FaceProperty
template <class T> class HalfedgeProperty : public Property<T>
{
public:
/// default constructor
HalfedgeProperty() = default;
explicit HalfedgeProperty(Property<T> p) : Property<T>(p) {}
/// access the data stored for halfedge \c h
typename Property<T>::reference operator[](Halfedge h)
{
return Property<T>::operator[](h.idx());
}
/// access the data stored for halfedge \c h
typename Property<T>::const_reference operator[](Halfedge h) const
{
return Property<T>::operator[](h.idx());
}
};
/// Edge property of type T
/// \sa VertexProperty, HalfedgeProperty, FaceProperty
template <class T> class EdgeProperty : public Property<T>
{
public:
/// default constructor
EdgeProperty() = default;
explicit EdgeProperty(Property<T> p) : Property<T>(p) {}
/// access the data stored for edge \c e
typename Property<T>::reference operator[](Edge e)
{
return Property<T>::operator[](e.idx());
}
/// access the data stored for edge \c e
typename Property<T>::const_reference operator[](Edge e) const
{
return Property<T>::operator[](e.idx());
}
};
/// Face property of type T
/// \sa VertexProperty, HalfedgeProperty, EdgeProperty
template <class T> class FaceProperty : public Property<T>
{
public:
/// default constructor
FaceProperty() = default;
explicit FaceProperty(Property<T> p) : Property<T>(p) {}
/// access the data stored for face \c f
typename Property<T>::reference operator[](Face f)
{
return Property<T>::operator[](f.idx());
}
/// access the data stored for face \c f
typename Property<T>::const_reference operator[](Face f) const
{
return Property<T>::operator[](f.idx());
}
};
/// Mesh property of type T
/// \sa VertexProperty, HalfedgeProperty, EdgeProperty
template <class T> class ModelProperty : public Property<T>
{
public:
/// default constructor
ModelProperty() = default;
explicit ModelProperty(Property<T> p) : Property<T>(p) {}
/// access the data stored for the mesh
typename Property<T>::reference operator[](size_t idx)
{
return Property<T>::operator[](idx);
}
/// access the data stored for the mesh
typename Property<T>::const_reference operator[](size_t idx) const
{
return Property<T>::operator[](idx);
}
};
public: //------------------------------------------------------ iterator types
/// This class iterates linearly over all vertices
/// \sa vertices_begin(), vertices_end()
/// \sa HalfedgeIterator, EdgeIterator, FaceIterator
class VertexIterator
{
public:
/// Default constructor
explicit VertexIterator(Vertex v=Vertex(), const SurfaceMesh* m=nullptr) : hnd_(v), mesh_(m)
{
if (mesh_ && mesh_->has_garbage()) while (mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
}
/// get the vertex the iterator refers to
Vertex operator*() const { return hnd_; }
/// are two iterators equal?
bool operator==(const VertexIterator& rhs) const
{
return (hnd_==rhs.hnd_);
}
/// are two iterators different?
bool operator!=(const VertexIterator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment iterator
VertexIterator& operator++()
{
++hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
return *this;
}
/// pre-decrement iterator
VertexIterator& operator--()
{
--hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) --hnd_.idx_;
return *this;
}
private:
Vertex hnd_;
const SurfaceMesh* mesh_;
};
/// This class iterates linearly over all halfedges
/// \sa halfedges_begin(), halfedges_end()
/// \sa VertexIterator, EdgeIterator, FaceIterator
class HalfedgeIterator
{
public:
/// Default constructor
explicit HalfedgeIterator(Halfedge h=Halfedge(), const SurfaceMesh* m=nullptr) : hnd_(h), mesh_(m)
{
if (mesh_ && mesh_->has_garbage()) while (mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
}
/// get the halfedge the iterator refers to
Halfedge operator*() const { return hnd_; }
/// are two iterators equal?
bool operator==(const HalfedgeIterator& rhs) const
{
return (hnd_==rhs.hnd_);
}
/// are two iterators different?
bool operator!=(const HalfedgeIterator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment iterator
HalfedgeIterator& operator++()
{
++hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
return *this;
}
/// pre-decrement iterator
HalfedgeIterator& operator--()
{
--hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) --hnd_.idx_;
return *this;
}
private:
Halfedge hnd_;
const SurfaceMesh* mesh_;
};
/// This class iterates linearly over all edges
/// \sa edges_begin(), edges_end()
/// \sa VertexIterator, HalfedgeIterator, FaceIterator
class EdgeIterator
{
public:
/// Default constructor
explicit EdgeIterator(Edge e=Edge(), const SurfaceMesh* m=nullptr) : hnd_(e), mesh_(m)
{
if (mesh_ && mesh_->has_garbage()) while (mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
}
/// get the edge the iterator refers to
Edge operator*() const { return hnd_; }
/// are two iterators equal?
bool operator==(const EdgeIterator& rhs) const
{
return (hnd_==rhs.hnd_);
}
/// are two iterators different?
bool operator!=(const EdgeIterator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment iterator
EdgeIterator& operator++()
{
++hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
return *this;
}
/// pre-decrement iterator
EdgeIterator& operator--()
{
--hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) --hnd_.idx_;
return *this;
}
private:
Edge hnd_;
const SurfaceMesh* mesh_;
};
/// This class iterates linearly over all faces
/// \sa faces_begin(), faces_end()
/// \sa VertexIterator, HalfedgeIterator, EdgeIterator
class FaceIterator
{
public:
/// Default constructor
explicit FaceIterator(Face f=Face(), const SurfaceMesh* m=nullptr) : hnd_(f), mesh_(m)
{
if (mesh_ && mesh_->has_garbage()) while (mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
}
/// get the face the iterator refers to
Face operator*() const { return hnd_; }
/// are two iterators equal?
bool operator==(const FaceIterator& rhs) const
{
return (hnd_==rhs.hnd_);
}
/// are two iterators different?
bool operator!=(const FaceIterator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment iterator
FaceIterator& operator++()
{
++hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) ++hnd_.idx_;
return *this;
}
/// pre-decrement iterator
FaceIterator& operator--()
{
--hnd_.idx_;
assert(mesh_);
while (mesh_->has_garbage() && mesh_->is_valid(hnd_) && mesh_->is_deleted(hnd_)) --hnd_.idx_;
return *this;
}
private:
Face hnd_;
const SurfaceMesh* mesh_;
};
public: //-------------------------- containers for C++11 range-based for loops
/// This helper class is a container for iterating through all
/// vertices using C++11 range-based for-loops.
/// \sa vertices()
class VertexContainer
{
public:
VertexContainer(VertexIterator _begin, VertexIterator _end) : begin_(_begin), end_(_end) {}
VertexIterator begin() const { return begin_; }
VertexIterator end() const { return end_; }
private:
VertexIterator begin_, end_;
};
/// This helper class is a container for iterating through all
/// halfedge using C++11 range-based for-loops.
/// \sa halfedges()
class HalfedgeContainer
{
public:
HalfedgeContainer(HalfedgeIterator _begin, HalfedgeIterator _end) : begin_(_begin), end_(_end) {}
HalfedgeIterator begin() const { return begin_; }
HalfedgeIterator end() const { return end_; }
private:
HalfedgeIterator begin_, end_;
};
/// This helper class is a container for iterating through all
/// edges using C++11 range-based for-loops.
/// \sa edges()
class EdgeContainer
{
public:
EdgeContainer(EdgeIterator _begin, EdgeIterator _end) : begin_(_begin), end_(_end) {}
EdgeIterator begin() const { return begin_; }
EdgeIterator end() const { return end_; }
private:
EdgeIterator begin_, end_;
};
/// This helper class is a container for iterating through all
/// faces using C++11 range-based for-loops.
/// \sa faces()
class FaceContainer
{
public:
FaceContainer(FaceIterator _begin, FaceIterator _end) : begin_(_begin), end_(_end) {}
FaceIterator begin() const { return begin_; }
FaceIterator end() const { return end_; }
private:
FaceIterator begin_, end_;
};
public: //---------------------------------------------------- circulator types
/**
* This class circulates through all one-ring neighbors of a vertex.
* It also acts as a container-concept for C++11 range-based for loops.
*
* The follow code shows how to use VertexAroundVertexCirculator:
* \code
* SurfaceMesh::VertexAroundVertexCirculator cir(mesh, v);
* SurfaceMesh::VertexAroundVertexCirculator end = cir;
* do {
* SurfaceMesh::Vertex v = *cir;
* // do something with v
* ++cir;
* } while (cir != end);
* \endcode
* \sa HalfedgeAroundVertexCirculator, FaceAroundVertexCirculator, vertices()
*/
class VertexAroundVertexCirculator
{
public:
/// default constructor
explicit VertexAroundVertexCirculator(const SurfaceMesh* m=nullptr, Vertex v=Vertex())
: mesh_(m), active_(true)
{
if (mesh_) halfedge_ = mesh_->out_halfedge(v);
}
/// are two circulators equal?
bool operator==(const VertexAroundVertexCirculator& rhs) const
{
assert(mesh_);
return (active_ && (mesh_==rhs.mesh_) && (halfedge_==rhs.halfedge_));
}
/// are two circulators different?
bool operator!=(const VertexAroundVertexCirculator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment (rotate counter-clockwise)
VertexAroundVertexCirculator& operator++()
{
assert(mesh_);
halfedge_ = mesh_->prev_around_source(halfedge_);
active_ = true;
return *this;
}
/// pre-decrement (rotate clockwise)
VertexAroundVertexCirculator& operator--()
{
assert(mesh_);
halfedge_ = mesh_->next_around_source(halfedge_);
return *this;
}
/// get the vertex the circulator refers to
Vertex operator*() const
{
assert(mesh_);
return mesh_->target(halfedge_);
}
/// cast to bool: true if vertex is not isolated
operator bool() const { return halfedge_.is_valid(); }
/// return current halfedge
Halfedge halfedge() const { return halfedge_; }
// helper for C++11 range-based for-loops
VertexAroundVertexCirculator& begin() { active_=!halfedge_.is_valid(); return *this; }
// helper for C++11 range-based for-loops
VertexAroundVertexCirculator& end() { active_=true; return *this; }
private:
const SurfaceMesh* mesh_;
Halfedge halfedge_;
// helper for C++11 range-based for-loops
bool active_;
};
/**
* This class circulates through all outgoing halfedges of a vertex.
* It also acts as a container-concept for C++11 range-based for loops.
*
* The follow code shows how to use HalfedgeAroundVertexCirculator:
* \code
* SurfaceMesh::HalfedgeAroundVertexCirculator cir(mesh, v);
* SurfaceMesh::HalfedgeAroundVertexCirculator end = cir;
* do {
* SurfaceMesh::Halfedge h = *cir;
* // do something with h
* ++cir;
* } while (cir != end);
* \endcode
* \sa VertexAroundVertexCirculator, FaceAroundVertexCirculator, halfedges()
*/
class HalfedgeAroundVertexCirculator
{
public:
/// default constructor
explicit HalfedgeAroundVertexCirculator(const SurfaceMesh* m=nullptr, Vertex v=Vertex())
: mesh_(m), active_(true)
{
if (mesh_) halfedge_ = mesh_->out_halfedge(v);
}
/// are two circulators equal?
bool operator==(const HalfedgeAroundVertexCirculator& rhs) const
{
assert(mesh_);
return (active_ && (mesh_==rhs.mesh_) && (halfedge_==rhs.halfedge_));
}
/// are two circulators different?
bool operator!=(const HalfedgeAroundVertexCirculator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment (rotate counter-clockwise)
HalfedgeAroundVertexCirculator& operator++()
{
assert(mesh_);
halfedge_ = mesh_->prev_around_source(halfedge_);
active_ = true;
return *this;
}
/// pre-decrement (rotate clockwise)
HalfedgeAroundVertexCirculator& operator--()
{
assert(mesh_);
halfedge_ = mesh_->next_around_source(halfedge_);
return *this;
}
/// get the halfedge the circulator refers to
Halfedge operator*() const { return halfedge_; }
/// cast to bool: true if vertex is not isolated
operator bool() const { return halfedge_.is_valid(); }
// helper for C++11 range-based for-loops
HalfedgeAroundVertexCirculator& begin() { active_=!halfedge_.is_valid(); return *this; }
// helper for C++11 range-based for-loops
HalfedgeAroundVertexCirculator& end() { active_=true; return *this; }
private:
const SurfaceMesh* mesh_;
Halfedge halfedge_;
// helper for C++11 range-based for-loops
bool active_;
};
/**
* This class circulates through all incident faces of a vertex.
* It also acts as a container-concept for C++11 range-based for loops.
*
* The follow code shows how to use FaceAroundVertexCirculator:
* \code
* SurfaceMesh::FaceAroundVertexCirculator cir(mesh, v);
* SurfaceMesh::FaceAroundVertexCirculator end = cir;
* do {
* SurfaceMesh::Face f = *cir;
* // do something with f
* ++cir;
* } while (cir != end);
* \endcode
* \sa VertexAroundVertexCirculator, HalfedgeAroundVertexCirculator, faces()
*/
class FaceAroundVertexCirculator
{
public:
/// construct with mesh and vertex (vertex should not be isolated!)
explicit FaceAroundVertexCirculator(const SurfaceMesh* m=nullptr, Vertex v=Vertex())
: mesh_(m), active_(true)
{
if (mesh_)
{
halfedge_ = mesh_->out_halfedge(v);
if (halfedge_.is_valid() && mesh_->is_border(halfedge_))
operator++();
}
}
/// are two circulators equal?
bool operator==(const FaceAroundVertexCirculator& rhs) const
{
assert(mesh_);
return (active_ && (mesh_==rhs.mesh_) && (halfedge_==rhs.halfedge_));
}
/// are two circulators different?
bool operator!=(const FaceAroundVertexCirculator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment (rotates counter-clockwise)
FaceAroundVertexCirculator& operator++()
{
assert(mesh_ && halfedge_.is_valid());
do {
halfedge_ = mesh_->prev_around_source(halfedge_);
} while (mesh_->is_border(halfedge_));
active_ = true;
return *this;
}
/// pre-decrement (rotate clockwise)
FaceAroundVertexCirculator& operator--()
{
assert(mesh_ && halfedge_.is_valid());
do
halfedge_ = mesh_->next_around_source(halfedge_);
while (mesh_->is_border(halfedge_));
return *this;
}
/// get the face the circulator refers to
Face operator*() const
{
assert(mesh_ && halfedge_.is_valid());
return mesh_->face(halfedge_);
}
/// cast to bool: true if vertex is not isolated
operator bool() const { return halfedge_.is_valid(); }
// helper for C++11 range-based for-loops
FaceAroundVertexCirculator& begin() { active_=!halfedge_.is_valid(); return *this; }
// helper for C++11 range-based for-loops
FaceAroundVertexCirculator& end() { active_=true; return *this; }
private:
const SurfaceMesh* mesh_;
Halfedge halfedge_;
// helper for C++11 range-based for-loops
bool active_;
};
/**
* This class circulates through the vertices of a face.
* It also acts as a container-concept for C++11 range-based for loops.
*
* The follow code shows how to use VertexAroundFaceCirculator:
* \code
* SurfaceMesh::VertexAroundFaceCirculator cir = mesh->vertices(f);
* SurfaceMesh::VertexAroundFaceCirculator end = cir;
* do {
* SurfaceMesh::Vertex v = *cir;
* // do something with v
* ++cir;
* } while (cir != end);
* \endcode
* \sa HalfedgeAroundFaceCirculator, vertices()
*/
class VertexAroundFaceCirculator
{
public:
/// default constructor
explicit VertexAroundFaceCirculator(const SurfaceMesh* m=nullptr, Face f=Face())
: mesh_(m), active_(true)
{
if (mesh_) halfedge_ = mesh_->halfedge(f);
}
/// are two circulators equal?
bool operator==(const VertexAroundFaceCirculator& rhs) const
{
assert(mesh_);
return (active_ && (mesh_==rhs.mesh_) && (halfedge_==rhs.halfedge_));
}
/// are two circulators different?
bool operator!=(const VertexAroundFaceCirculator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment (rotates counter-clockwise)
VertexAroundFaceCirculator& operator++()
{
assert(mesh_ && halfedge_.is_valid());
halfedge_ = mesh_->next(halfedge_);
active_ = true;
return *this;
}
/// pre-decrement (rotates clockwise)
VertexAroundFaceCirculator& operator--()
{
assert(mesh_ && halfedge_.is_valid());
halfedge_ = mesh_->prev(halfedge_);
return *this;
}
/// get the vertex the circulator refers to
Vertex operator*() const
{
assert(mesh_ && halfedge_.is_valid());
return mesh_->target(halfedge_);
}
// helper for C++11 range-based for-loops
VertexAroundFaceCirculator& begin() { active_=false; return *this; }
// helper for C++11 range-based for-loops
VertexAroundFaceCirculator& end() { active_=true; return *this; }
private:
const SurfaceMesh* mesh_;
Halfedge halfedge_;
// helper for C++11 range-based for-loops
bool active_;
};
/**
* This class circulates through all halfedges of a face.
* It also acts as a container-concept for C++11 range-based for loops.
*
* The following code shows how to use HalfedgeAroundFaceCirculator:
* \code
* SurfaceMesh::HalfedgeAroundFaceCirculator cir(mesh, f);
* SurfaceMesh::HalfedgeAroundFaceCirculator end = cir;
* do {
* SurfaceMesh::Halfedge h = *cir;
* // do something with h
* ++cir;
* } while (cir != end);
* \endcode
* \sa VertexAroundFaceCirculator, halfedges()
*/
class HalfedgeAroundFaceCirculator
{
public:
/// default constructor
explicit HalfedgeAroundFaceCirculator(const SurfaceMesh* m=nullptr, Face f=Face())
: mesh_(m), active_(true)
{
if (mesh_) halfedge_ = mesh_->halfedge(f);
}
/// are two circulators equal?
bool operator==(const HalfedgeAroundFaceCirculator& rhs) const
{
assert(mesh_);
return (active_ && (mesh_==rhs.mesh_) && (halfedge_==rhs.halfedge_));
}
/// are two circulators different?
bool operator!=(const HalfedgeAroundFaceCirculator& rhs) const
{
return !operator==(rhs);
}
/// pre-increment (rotates counter-clockwise)
HalfedgeAroundFaceCirculator& operator++()
{
assert(mesh_ && halfedge_.is_valid());
halfedge_ = mesh_->next(halfedge_);
active_ = true;
return *this;
}
/// pre-decrement (rotates clockwise)
HalfedgeAroundFaceCirculator& operator--()
{
assert(mesh_ && halfedge_.is_valid());
halfedge_ = mesh_->prev(halfedge_);
return *this;
}
/// get the halfedge the circulator refers to
Halfedge operator*() const { return halfedge_; }
// helper for C++11 range-based for-loops
HalfedgeAroundFaceCirculator& begin() { active_=false; return *this; }
// helper for C++11 range-based for-loops
HalfedgeAroundFaceCirculator& end() { active_=true; return *this; }
private:
const SurfaceMesh* mesh_;
Halfedge halfedge_;
// helper for C++11 range-based for-loops
bool active_;
};
public: //-------------------------------------------- constructor / destructor
/// \name Construct, destruct, assignment
//@{
/// default constructor
SurfaceMesh();
/// destructor (is virtual, since we inherit from Geometry_representation)
~SurfaceMesh() override = default;
/// copy constructor: copies \c rhs to \c *this. performs a deep copy of all properties.
SurfaceMesh(const SurfaceMesh& rhs) { operator=(rhs); }
/// assign \c rhs to \c *this. performs a deep copy of all properties.
SurfaceMesh& operator=(const SurfaceMesh& rhs);
/// \brief Merges another surface mesh into the current one.
/// Shifts the indices of vertices of the other mesh by `number_of_vertices() + number_of_removed_vertices()`
/// and analogously for halfedges, edges, and faces.
/// Copies entries of all property maps which have the same name in both meshes. That is, properties maps which
/// are only in `other` are ignored.
/// Also copies elements which are marked as removed, and concatenates the freelists of both meshes.
SurfaceMesh& operator+=(const SurfaceMesh& other) { join(other); return *this; }