-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh2.hpp
1145 lines (1001 loc) · 31.1 KB
/
Mesh2.hpp
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
/**
* CS207 HW4
* Editors: Wenshuai Ye && Yuhao Zhu
**/
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
#include "CS207/Util.hpp"
#include "Point.hpp"
using namespace std;
#pragma once
/** @file Mesh.hpp
* @brief A Mesh is composed of nodes, edges, and triangles such that:
* -- All triangles have three nodes and three edges.
* -- All edges belong to at least one triangle and at most two triangles.
*/
/** @class Mesh
* @brief A template for 3D triangular meshes.
*
* Users can add triangles and retrieve nodes, edges, and triangles.
*/
template <typename N, typename E, typename T>
class Mesh{
public:
/////////////////////////////
// PUBLIC TYPE DEFINITIONS //
/////////////////////////////
typedef N node_value_type;
typedef E edge_value_type;
typedef T triangle_value_type;
/** Type of this graph. */
typedef Mesh mesh_type;
/** Predeclaration of Node type. */
class Node;
/** Synonym for Node (following STL conventions). */
typedef Node node_type;
/** Predeclaration of Edge type. */
class Edge;
/** Synonym for Edge (following STL conventions). */
typedef Edge edge_type;
/** Predeclaration of Triangle type*/
class Triangle;
/** Synonym for Edge (following STL conventions). */
typedef Triangle triangle_type;
/** Type of indexes and sizes.
Return type of Graph::Node::index(), Graph::num_nodes(),
Graph::num_edges(), and argument type of Graph::node(size_type) */
typedef unsigned size_type;
/** Type of node iterators, which iterate over all graph nodes. */
class node_iterator;
/** Type of edge iterators, which iterate over all graph edges. */
class edge_iterator;
/** Type of incident iterators, which iterate incident edges to a node. */
class IncidentIterator;
/** Synonym for IncidentIterator */
typedef IncidentIterator incident_iterator;
/** Type of triangle iterators, which iterate over all triangles. */
class triangle_iterator;
/** Type of triangle incident iterators,
which iterate incident triangles to a node. */
class nincident_tri_iterator;
/** Type of triangle incident iterators,
which iterate incident triangles to a triangle. */
class eincident_tri_iterator;
/** Return the number of nodes in the mesh.
*
* Complexity: O(1).
*/
size_type size() const {
return node_i2u.size();
}
/** Remove all nodes and edges from this mesh.
* @post num_nodes() == 0 && num_edges() == 0
*
* Invalidates all outstanding Node and Edge objects.
*/
void clear() {
nodes_.clear();
edges_.clear();
node_i2u.clear();
edge_values.clear();
edge_size_=0;
}
Mesh(): edge_size_(0),nodes_(), edges_(), triangles_() {}
~Mesh() = default;
class Node: private totally_ordered<Node>{
public:
Node() {
}
/** Return this Node's position.
* Complexity: O(1)
*/
Point& position() const {
return m_->nodes_[node_uid_].points;
}
/** Return this node's index, a number in the range [0, mesh_size).
* Complexity: O(1)
*/
size_type index() const {
return m_->nodes_[node_uid_].index;
}
/** Test whether this node and @a x are equal.
*Return true if @a x and this node have the same mesh and the same uid.
* Complexity: O(1)
*/
bool operator==(const Node& x) const {
if (node_uid_ == x.node_uid_ && m_ == x.m_) return true;
(void) x;
return false;
}
/** Test whether this node is less than @a x in the global order.
*Return true if uid < @a x.uid
* Complexity: O(1)
*/
bool operator<(const Node& x) const {
if (node_uid_ < x.node_uid_) return true;
(void) x;
return false;
}
/** Return this Node's value
* Complexity: O(1)
*/
node_value_type& value(){
return m_-> nodes_[node_uid_].value;
}
/** Return this Node's value
* Complexity: O(1)
*/
const node_value_type& value() const{
return m_->nodes_[node_uid_].value;
}
/** Return the number of incident edges it has to the node
*
* Complexity O(1)
*/
size_type edge_degree() const {
return m_->edges_[node_uid_].size();
}
/* Return the incident_iterator to the current node's first incident edge
* Complexity O(1)
**/
incident_iterator edge_begin() const {
return incident_iterator(m_, node_uid_, 0);
}
/* Return the incident_iterator to the current node's
* last incident edge (invalid one)
* Complexity O(1)
**/
incident_iterator edge_end() const {
return incident_iterator(m_, node_uid_, m_->edges_[node_uid_].size());
}
/** Return the number of incident triangles it has to the node
*
* Complexity O(1)
*/
size_type triangle_degree() const{
return m_->nodes_[node_uid_].adj_triangles_.size();
}
/* Return the nincident_tri_iterator to the current node's
* first incident triangle
* Complexity O(1)
**/
nincident_tri_iterator triangle_begin() const{
return nincident_tri_iterator(m_,node_uid_,0);
}
/* Return the nincident_tri_iterator to the current node's last
* incident triangle (invalid triangle)
* Complexity O(1)
**/
nincident_tri_iterator triangle_end() const{
return nincident_tri_iterator(m_,node_uid_,m_->nodes_[node_uid_].adj_triangles_.size());
}
private:
// Allow Graph to access Node's private member data and functions.
Node(const Mesh* m, size_type uid)
: node_uid_(uid), m_(const_cast<Mesh*>(m)){}
friend class Mesh;
size_type node_uid_;
Mesh *m_;
};
/** Return the number of nodes in the mesh.
* Complexity: O(1).
*/
size_type num_nodes() const {
return size();
}
/** Determine if this Node belongs to this mesh
* @return True if @a n is currently a Node of this mesh
* Complexity: O(1).
*/
bool has_node(const Node& n) const {
return (n.m_ == this && node_i2u[nodes_[n.node_uid_].index] == n.node_uid_);
}
/** Add a node to the mesh, returning the added node.
* @param[in] position The new node's position
* @param[in] value The value corresponding to the node
* @post new size() == old size() + 1
* @post result_node.index() == old size()
* @return the node with corresponding input position
* Complexity: O(1) amortized operations.
*/
Node add_node(const Point& position, const node_value_type& value = node_value_type()) {
node_elem new_elem;
new_elem.value = value;
new_elem.points = position;
new_elem.index = node_i2u.size();
node_i2u.push_back(nodes_.size());
nodes_.push_back(new_elem);
edges_.push_back(vector<edge_elem>());
return Node(this, nodes_.size() - 1);
(void) position;
}
/** Return the node with index @a i.
* @pre 0 <= @a i < num_nodes()
* @post result_node.index() == i
* Complexity: O(1).
*/
Node node(size_type i) const {
return Node(this, node_i2u[i]);
(void) i;
}
class Edge: private totally_ordered<Edge>{
public:
/** Construct an invalid Edge. */
Edge(){}
/** Return a node of this Edge */
Node node1() const {
return Node(m_, uid1_);
}
/** Return the other node of this Edge */
Node node2() const {
return Node(m_, uid2_);
}
/** Return this Edge's value
* Complexity: O(uid1.edge_degree()) worst case
*/
edge_value_type& value(){
size_type edge_index_;
for (size_type i =0; i != m_->edges_[uid1_].size(); ++i){
if (m_->edges_[uid1_][i].uid == uid2_){
edge_index_ = m_->edges_[uid1_][i].edge_index;
return m_->edge_values[edge_index_];
}
}
}
/** Return this Edge's value
* Complexity: O(uid1.edge_degree()) worst case
*/
const edge_value_type& value() const{
size_type edge_index_;
for (size_type i =0; i != m_->edges_[uid1_].size(); ++i){
if (m_->edges_[uid1_][i].uid == uid2_){
edge_index_ = m_->edges_[uid1_][i].edge_index;
return m_->edge_values[edge_index_];
}
}
}
/** Return whether this edge and @a x are equal (they are from the same
* mesh and have the same nodes).
* Complexity: O(1)
*/
bool operator==(const Edge& x) const {
return (m_ == x.m_ && uid1_ == x.uid1_ && uid2_ == x.uid2_);
(void) x;
return false;
}
/** Return whether this edge is less than @a x in the global order
* (if uid is less than de uid of @a x).
* Complexity: O(1)
*/
bool operator<(const Edge& x) const {
if (m_ != x.m_) return true;
else return (uid1_<x.uid1_ && uid2_<x.uid2_);
(void) x;
}
/** Return this Edge's length.
* Complexity: O(1)
*/
double length() const{
return norm(node1().position() - node2().position());
}
/* Return the eincident_tri_iterator to the current
* triangle's first incident triangle
* Complexity O(adj_edges) worst case
**/
eincident_tri_iterator triangle_begin() const{
size_type uid2_index_;
for (size_type i =0; i != m_->edges_[uid1_].size(); ++i){
if (m_->edges_[uid1_][i].uid == uid2_)
uid2_index_ = i;
}
return eincident_tri_iterator(m_,uid1_,uid2_index_,0);
}
/* Return the eincident_tri_iterator to the current
* triangle's first incident triangle
* Complexity O(adj_edges/2) on average
**/
eincident_tri_iterator triangle_end() const{
size_type uid2_index_;
for (size_type i =0; i != m_->edges_[uid1_].size(); ++i){
if (m_->edges_[uid1_][i].uid == uid2_)
uid2_index_ = i;
}
return eincident_tri_iterator(m_,uid1_,uid2_index_,m_->edges_[uid1_][uid2_index_].adj_triangles_.size());
}
/** Return the number of incident triangles it has to
* the current edge
* Complexity O(1)
*/
size_type degree() const{
size_type uid2_index_;
for (size_type i =0; i != m_->edges_[uid1_].size(); ++i){
if (m_->edges_[uid1_][i].uid == uid2_)
uid2_index_ = i;
}
return m_->edges_[uid1_][uid2_index_].adj_triangles_.size();
}
Triangle eAdj_Triangle(size_type i) const{
assert(i>=1 && i<=2);
size_type index_;
for (size_type j =0; j != m_->edges_[uid1_].size(); ++j){
if (m_->edges_[uid1_][j].uid == uid2_)
index_ = j;
}
return Triangle(m_,m_->edges_[uid1_][index_].adj_triangles_[i-1]);
}
private:
// Allow Graph to access Edge's private member data and functions.
friend class Mesh;
size_type uid1_;
size_type uid2_;
Edge(const Mesh* m,size_type uid1, size_type uid2) : uid1_(uid1), uid2_(uid2), m_(const_cast<Mesh*>(m)){}
Mesh* m_;
};
/** Return the number of edges this mesh has.
* Complexity: O(1)
*/
size_type num_edges() const {
return edge_size_;
}
/** Test whether two nodes are connected by an edge.
* @param[in] a, b The nodes that might be connected by an edge
* @pre @a a and @a b are valid nodes of this mesh
* @return true if, for some @a i, edge(@a i) connects @a a and @a b.
*
* Complexity: No more than O(a.degree())
*/
bool has_edge(const Node& a, const Node& b) const {
for (size_type i=0; i<edges_[a.node_uid_].size(); ++i){
if (b.node_uid_ == edges_[a.node_uid_][i].uid) return true;
}
(void) a; (void) b;
return false;
}
/** Add an edge to the mesh, or return the current edge if it already exists.
* @pre @a a and @a b are distinct valid nodes of this mesh
* @param[in] a, b The nodes that might or might not be connected by an edge
* @param[in] value The corresponding value to the edge
* @post has_edge(@a a, @a b) == true
* @post If old has_edge(@a a, @a b), new num_edges() == old num_edges().
* Else, new num_edges() == old num_edges() + 1.
*
* @return an Edge object e with e.node1() == @a a and e.node2() == @a b
* Complexity: O(1) amortized time
*/
Edge add_edge(const Node& a, const Node& b, const edge_value_type& value = edge_value_type()) {
if (!has_edge(a,b)){
edge_elem elem1;
edge_elem elem2;
elem1.uid = a.node_uid_;
elem1.edge_index = edge_size_;
elem2.uid = b.node_uid_;
elem2.edge_index = edge_size_;
edges_[a.node_uid_].push_back(elem2);
edges_[b.node_uid_].push_back(elem1);
edge_values.push_back(value);
++edge_size_;
}
return Edge(this, a.node_uid_, b.node_uid_);
(void) a, (void) b;
}
/** Return the node with index @a i.*/
Edge edge(size_type i) const{
edge_iterator it=edge_begin();
for (;i!=0;--i) ++it;
return *it;
}
class Triangle: private totally_ordered<Triangle>{
public:
/** Construct an invalid Edge. */
Triangle(){}
/** Return a node of this triangle based on the predefined
* index within a triangle
* @param[in] i The index of the Node.
* @pre 0<i<=3
* @post Node.node_uid_==m_->triangles_[tri_uid_].node_uidi_
*
* Complexity: O(1)
* triangleNode(i) is the node opposite to the edge triangleEdge(i)
*/
Node triangle_node(size_type i) const{
assert(0<i && i<=3);
if (i==1) return Node(m_,m_->triangles_[tri_uid_].node_uid1);
else if (i==2) return Node(m_,m_->triangles_[tri_uid_].node_uid2);
else return Node(m_,m_->triangles_[tri_uid_].node_uid3);
}
/** Return an edge of this triangle based on the predefined
* index within a triangle
* @param[in] i The index of the edge.
* @pre 0<i<=3
* @post Node.edge_uid_==m_->triangles_[tri_uid_].edge_uidi_
*
* Complexity: O(1)
* triangleEdge(i) is the edge opposite to the edge triangleNode(i)
*/
Edge triangle_edge(size_type i) const{
assert(0<i && i <= 3);
if (i==1) return Edge(m_,m_->triangles_[tri_uid_].node_uid2,m_->triangles_[tri_uid_].node_uid3);
else if (i==2) return Edge(m_,m_->triangles_[tri_uid_].node_uid1,m_->triangles_[tri_uid_].node_uid3);
else return Edge(m_,m_->triangles_[tri_uid_].node_uid1,m_->triangles_[tri_uid_].node_uid2);
}
/** Return this Triangle's area
* Complexity: O(1)
*/
double area(){
double length1 = triangle_edge(1).length();
double length2 = triangle_edge(2).length();
double length3 = triangle_edge(3).length();
double s = (length1 + length2 + length3)/2;
return sqrt(s*(s-length1)*(s-length2)*(s-length3));
}
/** Return the outward normal vector of this triangle
* corresponding to an edge
* @param[i] i The index of the edge
* @pre 0<i<=3
* Complexity: O(1)
*/
Point normal_vector(size_type i){
Point p = triangle_edge(i).node1().position() - triangle_edge(i).node2().position();
double nx = p.y;
double ny = -p.x;
Point checkP = triangle_edge(i).node1().position() - triangle_node(i).position();
double ln = sqrt(nx*nx + ny*ny);
double le = triangle_edge(i).length();
if (nx*checkP.x + ny*checkP.y<0){
nx = -nx/ln*le;
ny = -ny/ln*le;
}
else{
nx = nx/ln*le;
ny = ny/ln*le;
}
return Point(nx,ny,0);
}
/** Return this Triangle's value
* Complexity: O(1)
*/
triangle_value_type& value(){
return m_->triangles_[tri_uid_].value;
}
/** Return this Triangle's value
* Complexity: O(1)
*/
const triangle_value_type& value() const{
return m_->triangles_[tri_uid_].value;
}
/** Return whether this triangle and @a t are equal
* (they are from the same mesh and have the same nodes).
* Complexity: O(1)
*/
bool operator==(const Triangle& t) const {
return (m_==t.m_ && tri_uid_ == t.tri_uid_);
}
/** Return the adjacent triangle associated with the index.
* @param[in] i the index of the adjacent triangle
* @pre 0 < i <= 3
*
* Complexity: O(1) amortized time*/
Triangle triAdj_triangle(size_type i) const{
assert(i>0 && i<=3);
if (triangle_edge(i).degree() == 1) return triangle_edge(i).eAdj_Triangle(1);
else{
if (triangle_edge(i).eAdj_Triangle(1) == *this)
return triangle_edge(i).eAdj_Triangle(2);
else
return triangle_edge(i).eAdj_Triangle(1);
}
}
private:
friend class Mesh;
Mesh* m_;
size_type tri_uid_;
// Private constructor
Triangle(const Mesh* mesh, size_type uid) : m_(const_cast<Mesh*>(mesh)), tri_uid_(uid) {}
};
/** Return the total number of triangles in the mesh.
* Complexity: O(1)
*/
size_type num_triangles() const {
return triangles_.size();
}
/** Test whether there exist a triangle that contains the 3 nodes.
* @param[in] a,b,c The input nodes that might have formed a triangle
* @pre @a a, @a b and @a c are valid nodes of the mesh
* @return True if, for some @a i, there exists a triangle(@a i)
* whose nodes are @a a, @a b and @a c.
*
* Complexity: O(num_triangles()) worst case
*/
bool has_triangle(const Node& a, const Node& b, const Node& c) const {
assert(has_node(a) && has_node(b) && has_node(c));
for (size_type t;t<triangles_.size();++t){
if (a.node_uid_ == triangles_[t].node_uid1 &&
b.node_uid_ == triangles_[t].node_uid2 &&
c.node_uid_ == triangles_[t].node_uid3) return true;
if (a.node_uid_ == triangles_[t].node_uid2 &&
b.node_uid_ == triangles_[t].node_uid1 &&
c.node_uid_ == triangles_[t].node_uid3) return true;
if (a.node_uid_ == triangles_[t].node_uid3 &&
b.node_uid_ == triangles_[t].node_uid1 &&
c.node_uid_ == triangles_[t].node_uid2) return true;
if (a.node_uid_ == triangles_[t].node_uid1 &&
b.node_uid_ == triangles_[t].node_uid3 &&
c.node_uid_ == triangles_[t].node_uid2) return true;
if (a.node_uid_ == triangles_[t].node_uid2 &&
b.node_uid_ == triangles_[t].node_uid3 &&
c.node_uid_ == triangles_[t].node_uid1) return true;
if (a.node_uid_ == triangles_[t].node_uid3 &&
b.node_uid_ == triangles_[t].node_uid2 &&
c.node_uid_ == triangles_[t].node_uid1) return true;
}
return false;
}
/** Add a triangle to the mesh, or return the current triangle
* if it already exists.
* @param[in] a,b,c The input nodes that might have formed a triangle
* @param[in] value The value corresponding to the triangle
* @pre @a a, @a b and @a c are distinct valid nodes of this mesh
* (They are not on one line)
* @post has_triangle(@a a, @a b, @a c) == true
* @post If old has_triangle(@a a, @a b, @a c),
* new num_triangles() == old num_triangles();
* Else, new num_triangles() == old num_triangles() + 1
* and result_node.uid() == old num_triangles()
*
* @return a Triangle t with t.node1(), t.node2(), t.node3()==@a a, @a b, @a c
* Complexity: O(a.edge_degree() + b.edge_deree() + c.edge_degree()) worst case
*/
Triangle add_triangle(const Node& a, const Node& b, const Node& c, const triangle_value_type& value = triangle_value_type()) {
if (!has_triangle(a,b,c)){
nodes_[a.node_uid_].adj_triangles_.push_back(num_triangles());
nodes_[b.node_uid_].adj_triangles_.push_back(num_triangles());
nodes_[c.node_uid_].adj_triangles_.push_back(num_triangles());
add_edge(a,b);
add_edge(b,c);
add_edge(a,c);
size_type indexac;
size_type indexab;
size_type indexbc;
size_type indexca;
size_type indexba;
size_type indexcb;
for (size_type i =0; i != edges_[a.node_uid_].size(); ++i){
if (edges_[a.node_uid_][i].uid == b.node_uid_)
indexab = i;
if (edges_[a.node_uid_][i].uid == c.node_uid_)
indexac = i;
}
for (size_type i =0; i != edges_[b.node_uid_].size(); ++i){
if (edges_[b.node_uid_][i].uid == c.node_uid_)
indexbc = i;
if (edges_[b.node_uid_][i].uid == a.node_uid_)
indexba = i;
}
for (size_type i =0; i != edges_[c.node_uid_].size(); ++i){
if (edges_[c.node_uid_][i].uid == a.node_uid_)
indexca = i;
if (edges_[c.node_uid_][i].uid == b.node_uid_)
indexcb = i;
}
edges_[a.node_uid_][indexab].adj_triangles_.push_back(num_triangles());
edges_[a.node_uid_][indexac].adj_triangles_.push_back(num_triangles());
edges_[b.node_uid_][indexba].adj_triangles_.push_back(num_triangles());
edges_[b.node_uid_][indexbc].adj_triangles_.push_back(num_triangles());
edges_[c.node_uid_][indexca].adj_triangles_.push_back(num_triangles());
edges_[c.node_uid_][indexcb].adj_triangles_.push_back(num_triangles());
triangle_elem new_triangle;
new_triangle.value = value;
new_triangle.node_uid1 = a.node_uid_;
new_triangle.node_uid2 = b.node_uid_;
new_triangle.node_uid3 = c.node_uid_;
new_triangle.triangle_uid = triangles_.size();
triangles_.push_back(new_triangle);
}
return Triangle(this, triangles_.size()-1);
}
/** Return the triangle with index @a i.
* @pre 0 <= @a i < num_triangles()
* Complexity: O(1)
*/
Triangle triangle(size_type i) const {
assert(i<num_triangles());
return Triangle(this, i);
}
///////////////
// Iterators //
///////////////
/** @class Mesh::NodeIterator
* @brief Iterator class for nodes. A forward iterator. */
class node_iterator : private totally_ordered<node_iterator>{
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Node value_type;
/** Type of pointers to elements. */
typedef Node* pointer;
/** Type of references to elements. */
typedef Node& reference;
/** Iterator category. */
typedef std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid NodeIterator. */
node_iterator() {
}
/** Dereferencing operator
* @return the node it points to
* Complexity: O(1)
*/
Node operator*() const{
return Node(m_, m_->node_i2u[index]);
}
/** Increment operator
* @return the new iterator that points to the next node
* Complexity: O(1)
*/
node_iterator& operator++(){
++index;
return *this;
}
/** Equal operator
* @param[in] x a node iterator
* @return true if they are equal
* Complexity: O(1)
*/
bool operator==(const node_iterator& x) const{
return (m_ == x.m_ && index == x.index);
}
private:
friend class Mesh;
node_iterator(const Mesh* mesh, size_type index_)
: m_(const_cast<Mesh*>(mesh)),index(index_){}
Mesh* m_;
size_type index;
};
/** Get the beginning node
* @return the iterator that points to the first node of the graph
* Complexity: O(1)
*/
node_iterator node_begin() const{
return node_iterator(this, 0);
}
/** Get the last node
* @return the iterator that points to the last node of the graph
* (an invalid node)
* Complexity: O(1)
*/
node_iterator node_end() const{
return node_iterator(this,node_i2u.size());
}
/** @class Mesh::EdgeIterator
* @brief Iterator class for edges. A forward iterator. */
class edge_iterator : private totally_ordered<edge_iterator>{
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Edge value_type;
/** Type of pointers to elements. */
typedef Edge* pointer;
/** Type of references to elements. */
typedef Edge& reference;
/** Iterator category. */
typedef std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid EdgeIterator. */
edge_iterator() {
}
/** Dereferencing operator
* @return the edge it points to
* Complexity: O(1)
*/
Edge operator*() const{
return Edge(m_, uid1_, m_->edges_[uid1_][index].uid);
}
/** Increment operator
* @return the new iterator that points to the next edge
* Complexity: Best case O(1)
*/
edge_iterator& operator++(){
do
{
if (++index >= m_->edges_[uid1_].size()){
index = 0;
if (++uid1_ == m_->edges_.size())
break;
}
}
while (uid1_ > m_->edges_[uid1_][index].uid);
return *this;
}
/** Equal operator
* @param[in] x an edge iterator
* @return true if they are equal
* Complexity: O(1)
*/
bool operator==(const edge_iterator& x) const{
return (m_==x.m_ && uid1_ == x.uid1_ && index == x.index);
}
private:
friend class Mesh;
Mesh* m_;
size_type uid1_;
size_type index;
edge_iterator(const Mesh* mesh, size_type uid1, size_type index_)
: m_(const_cast<Mesh*>(mesh)),uid1_(uid1), index(index_){}
};
/** Get the beginning edge
* @return the iterator that points to the first edge of the graph
* Complexity: O(1)
*/
edge_iterator edge_begin() const{
return edge_iterator(this,0,0);
}
/** Get the last edge
* @pre caller is a valid graph
* @return the iterator that points to the last edge of
* the graph (an invalid edge).
* Complexity: O(1)
*/
edge_iterator edge_end() const{
return edge_iterator(this,edges_.size(),0);
}
/**@class Mesh::IncidentIterator
* @brief Iterator class for edges incident to a node. A forward iterator. */
class IncidentIterator: private totally_ordered<IncidentIterator>{
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Edge value_type;
/** Type of pointers to elements. */
typedef Edge* pointer;
/** Type of references to elements. */
typedef Edge& reference;
/** Iterator category. */
typedef std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid IncidentIterator. */
IncidentIterator() {
}
/** Dereferencing operator
* @return the edge it points to
* Complexity: O(1)
*/
Edge operator*() const{
return Edge(m_, node_uid1_, m_->edges_[node_uid1_][index].uid);
}
/** Increment operator
* @return the new iterator that points to the next edge
* in the adjacency list
* Complexity: O(1)
*/
IncidentIterator& operator++(){
++index;
return *this;
}
/** Equal operator
* @param[in] x an edge iterator
* @return true if they are in the same graph and point to the same edge
* Complexity: O(1)
*/
bool operator==(const IncidentIterator& x) const{
return (m_ == x.m_ && node_uid1_ == x.node_uid1_ && index == x.index);
}
private:
friend class Mesh;
size_type node_uid1_;
size_type index;
Mesh* m_;
IncidentIterator(const Mesh* mesh, size_type uid1, size_type index_)
: m_(const_cast<Mesh*>(mesh)),node_uid1_(uid1),index(index_){}
};
class triangle_iterator: private totally_ordered<triangle_iterator>{
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Triangle value_type;
/** Type of pointers to elements. */
typedef Triangle* pointer;
/** Type of references to elements. */
typedef Triangle& reference;
/** Iterator category. */
typedef input_iterator_tag iterator_category;
/** Difference between iterators */
typedef ptrdiff_t difference_type;
/** Construct an invalid triangleIterator. */
triangle_iterator() {
}
/** Dereferencing operator
* @return the triangle it points to
* Complexity: O(1)
*/
Triangle operator*() const{
return Triangle(m_,tri_uid_);
}
/** Increment operator
* @return the new iterator that points to the next triangle
* Complexity: O(1)
*/
triangle_iterator& operator++(){
++tri_uid_;
return *this;
}
/** Equal operator
* @param[in] x a triangle iterator
* @return true if they are equal
* Complexity: O(1)
*/
bool operator==(const triangle_iterator& x) const{
return (m_==x.m_ && tri_uid_==x.tri_uid_);
}
// all triangle functions from this point on implemented by us
triangle_iterator& operator[](int n){
assert(n < m_->triangles_.size() && n >= 0);
tri_uid_ = n;
return *this;
}
int operator-(triangle_iterator a){
return tri_uid_ - a.tri_uid_;
}
triangle_iterator& operator+=(int n){
tri_uid_ += n;
return *this;
}
triangle_iterator& operator-=(int n){
tri_uid_ -= n;
return *this;
}
private:
friend class Mesh;
Mesh* m_;
size_type tri_uid_;
triangle_iterator(const Mesh* mesh, size_type uid): m_(const_cast<Mesh*>(mesh)),tri_uid_(uid){}
};
friend triangle_iterator operator+(triangle_iterator a, int n){
return a+=n;
}
friend triangle_iterator operator+(int n, triangle_iterator a){
return a+=n;
}
friend triangle_iterator operator-(triangle_iterator a, int n){
return a-=n;
}
// end of our additions
/* Return the first triangle interator
* Complexity O(1)
**/
triangle_iterator triangle_begin(){
return triangle_iterator(this,0);
}
/* Return the last triangle interator
* Complexity O(1)