-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.hpp
1059 lines (919 loc) · 31.5 KB
/
Mesh.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
#pragma once
#include "Graph.hpp"
#include "Point.hpp"
#include <cmath>
/** @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.
*/
/** Water column characteristics */
struct QVar {
double h; // Height of column
double hx; // Height times average x velocity of column
double hy; // Height times average y velocity of column
/** Default constructor.
*
* A default water column is 1 unit high with no velocity. */
QVar()
: h(1), hx(0), hy(0) {
}
/** Construct the given water column. */
QVar(double h_, double hx_, double hy_)
: h(h_), hx(hx_), hy(hy_) {
}
// More operators
QVar& operator+=(const QVar& b) {
h += b.h;
hx += b.hx;
hy += b.hy;
return *this;
}
QVar& operator-=(const QVar& b) {
h -= b.h;
hx -= b.hx;
hy -= b.hy;
return *this;
}
QVar& operator*=(double b) {
h *= b;
hx *= b;
hy *= b;
return *this;
}
QVar& operator/=(double b) {
h /= b;
hx /= b;
hy /= b;
return *this;
}
};
QVar operator-(const QVar& a) {
return QVar(-a.h, -a.hx, -a.hy);
}
QVar operator+(QVar a, const QVar& b) {
return a += b;
}
QVar operator-(QVar a, const QVar& b) {
return a -= b;
}
QVar operator*(QVar a, double b) {
return a *= b;
}
QVar operator*(double b, QVar a) {
return a *= b;
}
QVar operator/(QVar a, double b) {
return a /= b;
}
/** @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 {
// HW3: YOUR CODE HERE
// Write all typedefs, public member functions, private member functions,
// inner classes, private member data, etc.hello
private:
struct internal_node_value;
struct internal_edge_value;
struct internal_tri_value;
public:
/** Type of indexes and sizes. Return type of Mesh::num_nodes(). */
typedef unsigned size_type;
typedef Graph<internal_node_value, internal_edge_value> GraphType;
class Node;
typedef Node node_type;
class Edge;
typedef Edge edge_type;
class NodeIterator;
typedef NodeIterator node_iterator;
class EdgeIterator;
typedef EdgeIterator edge_iterator;
typedef N node_value_type;
typedef E edge_value_type;
typedef T triangle_value_type;
/** Predeclaration of Triangle type. */
class Triangle;
/** Type of triangle iterators, which iterate over all adjacent mesh triangles of a triangle. */
typedef Triangle triangle_type;
class IncidentIterator_Triangle;
/** Synonym for IncidentIterator_Triangle */
typedef IncidentIterator_Triangle incidentiterator_triangle;
class IncidentIterator_Node;
typedef IncidentIterator_Node incidentiterator_node;
class IncidentEdgeIterator;
typedef IncidentEdgeIterator incident_edge_iterator;
Mesh() {}
/** Return the number of nodes in the mesh. */
size_type num_nodes() const {
return graph_.num_nodes();
}
/** Return the number of edges in the mesh. */
size_type num_edges() const {
return graph_.num_edges();
}
/** Return the number of triangles in the mesh. */
size_type num_triangles() const {
return tri_vec.size();
}
class Node:private totally_ordered<Node>{
public:
/** Construct an invalid node.
*
* Valid nodes are obtained from the Graph class, but it
* is occasionally useful to declare an @i invalid node, and assign a
* valid node to it later. For example:
*
* @code
* Mesh::node_type x;
* if (...should pick the first node...)
* x = graph.node(0);
* else
* x = some other node using a complicated calculation
* do_something(x);
* @endcode
*/
Node(){}
/** Return this node's position. */
const Point& position() const {
return mesh_->graph_.node(uid_).position();
}
Point& position(){
return mesh_->graph_.node(uid_).position();
}
/** Return this node's index, a number in the range [0, graph_size()). */
size_type index() const {
return uid_;
}
/** Test whether this node and @a x are equal.
*
* Equal nodes have the same graph and the same index.
*/
bool operator==(const Node& x) const {
if (x.mesh_ == mesh_ && x.uid_ == uid_)
return true;
return false;
}
/** Test whether this node is less than @a x in the global order.
*
* This ordering function is useful for STL containers such as
* std::map<>. It need not have any geometric meaning.
*
* The node ordering relation must obey trichotomy: For any two nodes x
* and y, exactly one of x == y, x < y, and y < x is true.
*/
bool operator<(const Node& x) const {
if (mesh_ == x.mesh_)
return (uid_ < x.uid_);
else
return (mesh_ < x.mesh_);
}
/** Return the user-specifies value of this Node
* For rvalue operations
*/
node_value_type& value(){
return mesh_->graph_.node(uid_).value().value;
}
/** Return the user-specifies value of this Node
* For lvalue operations
*/
const node_value_type& value() const {
return mesh_->graph_.node(uid_).value().value;
}
/** Return the number of incident edges of this node
* Incident are edges spawned by this node
*/
size_type degree() const{
return mesh_->graph_.node(uid_).degree();
}
/* Return an iterator that points to the first incident edge of this node
* If degree()==0, the returned iterator value shall not be dereferenced
*/
incident_edge_iterator edge_begin() const{
return IncidentEdgeIterator(mesh_, mesh_->graph_.node(uid_).edge_begin());
}
/* Returns an iterator referring to the past-the-end incident edge
* of this node
*The past-the-end incident edge is the theoretical incident edge that
* would follow the last edge. It does not point to any element, and
* thus shall not be dereferenced.
*If degree()==0, this function returns the same as edge_begin().
*/
incident_edge_iterator edge_end() const{
return IncidentEdgeIterator(mesh_, mesh_->graph_.node(uid_).edge_end());
}
/* Get the iterator that points to the first adjacent triangle of a node
* @param n The node in the center
* @return the iterator that points to the first adjacent triangle of @a n
* @post result.n_ == n.uid_
* @post result.t_ result.t2_ are the adjacent triangles of edge(n_, result.last_)
* @post result.last_ == result.first_
*/
IncidentIterator_Node triangle_begin(){
Edge start = *(edge_begin());
size_type othernode;
if (start.node1().index() == index())
othernode = start.node2().index();
else
othernode = start.node1().index();
return IncidentIterator_Node(mesh_, index(), mesh_->graph_.edge(start.index()).value().triangle1,
mesh_->graph_.edge(start.index()).value().triangle2, othernode, othernode);
}
/* Get the iterator that points to an invalid triangle of a node
* @param n The node in the center
* @return the iterator that points to an invalid triangle of @a n
* @post result.n_ == n.uid_
* @post result.t_ == -1
*/
IncidentIterator_Node triangle_end(){
return IncidentIterator_Node(mesh_, index(), -1,
-1, -1, -1);
}
private:
friend class Mesh;
Mesh* mesh_; //pointer to the associated Mesh
size_type uid_; //uid of the node; the same uid as in graph
// a private constructor for graph to construct a Node instance
Node(const Mesh* mesh, size_type uid)
: mesh_(const_cast<Mesh*>(mesh)), uid_(uid){
}
};
/** Add a node to the graph, returning the added node.
* @param[in] position The new node's position
* @param[in] value_in The value associated with this node
* @post new graph_.size() == old graph_.size() + 1
* @post result_node.index() == old size()
* Complexity: O(1)
*/
Node add_node(const Point& position, const N& value_in = N()) {
auto n = graph_.add_node(position, internal_node_value(value_in));
return Node(this, n.index());
}
Node node(size_type i){
return Node(this, i);
}
class Edge:private totally_ordered<Edge>{
public:
/** Construct an invalid Edge. */
Edge() {
}
/** Return a node of this Edge */
Node node1() const {
// fetch the node index stored in the graph first
// then fetch the node from graph
return mesh_->node(mesh_->graph_.edge(uid_).node1().index());
}
/** Return the other node of this Edge */
Node node2() const {
// fetch the node index stored in the graph first
// then fetch the node from graph
return mesh_->node(mesh_->graph_.edge(uid_).node2().index());
}
double length() const{
return mesh_->graph_.edge(uid_).length();
}
Triangle triangle1() {
return Triangle(mesh_, mesh_->graph_.edge(uid_).value().triangle1);
}
Triangle triangle2() {
return Triangle(mesh_, mesh_->graph_.edge(uid_).value().triangle2);
}
/** Test whether this edge and @a x are equal.
*
* Equal edges are from the same graph and have the same nodes.
*/
bool operator==(const Edge& x) const {
if (mesh_ == x.mesh_ && uid_ == x.uid_)
return true;
return false;
}
/** Test whether this edge is less than @a x in the global order.
*
* This ordering function is useful for STL containers such as
* std::map<>. It need not have any geometric meaning.
*
* The edge ordering relation must obey trichotomy: For any two edges x
* and y, exactly one of x == y, x < y, and y < x is true.
*/
bool operator<(const Edge& x) const {
if (mesh_ == x.mesh_)
return (uid_ < x.uid_);
else
return (mesh_ < x.mesh_);
}
//Return the user-specifies value of this edge
edge_value_type& value(){
return mesh_->graph_.edge(uid_).value().value;
}
//Return the user-specifies value of this edge
const edge_value_type& value() const{
return mesh_->graph_.edge(uid_).value().value;
}
size_type index() const{
return uid_;
}
private:
// Allow Graph to access Edge's private member data and functions.
friend class Mesh;
Mesh* mesh_; //pointer to the associated graph
size_type uid_; //the unique id of the edge
//private constructor for graph to construct edge instance
Edge(const Mesh* mesh, size_type uid)
: mesh_(const_cast<Mesh*>(mesh)), uid_(uid){
}
};
Edge edge(size_type i){
return Edge(this, i);
}
class Triangle
{
public:
/**construc an invalid triangle*/
Triangle():uid_(-1){
}
/**Access the i node of the triangle
* @pre 0<=i<3
* @post result.index() == mesh_->tri_vec[uid_].nodes[i]
* Complexity = O(1)
*/
Node node(size_type i) {
size_type node_uid = mesh_->tri_vec[uid_].nodes[i];
return mesh_->node(node_uid);
}
/**Access the first node of the triangle
* @pre 0<=i<3
* @post result.index() == mesh_->tri_vec[uid_].edges[i]
* Complexity = O(1)
*/
Edge edge(size_type i) {
size_type edge_uid = mesh_->tri_vec[uid_].edges[i];
return mesh_->edge(edge_uid);
}
/**return the area of this triangle
*Comlexity = O(1)
*/
double area() {
mesh_->tri_vec[uid_].area = norm(cross(node(0).position()-node(1).position(),
node(0).position()-node(2).position()))/2;
return mesh_->tri_vec[uid_].area;
}
/**return the index of the triangle
*Complexity = O(1)
*/
size_type index(){
return uid_;
}
/** Return the value associated with this Node
* For rvalue operations
*/
triangle_value_type& value(){
return mesh_->tri_vec[uid_].value;
}
/** Return the value associated with this Node
* For lvalue operations
*/
const triangle_value_type& value() const {
return mesh_->tri_vec[uid_].value;
}
/**return the unit normal vector of the edge
*that is opposite to node i
*/
Point normal(size_type i){
return mesh_->tri_vec[uid_].n[i];
}
/* Return an iterator points to the first adjacent triangle of this triangle.
* @post result->incident_i ==0
* @post result->mesh_ == this->mesh_
* @post result->triangle_uid == this->uid_
*
*Complexity = O(1)
*/
incidentiterator_triangle triangle_begin() const {
return IncidentIterator_Triangle(mesh_, uid_, 0);
}
/* Return an iterator points to the last adjacent triangle of this triangle.
* @post result->incident_i == this->mesh_->edges.size()
* @post result->mesh_ == this->mesh_
* @post result->triangle_uid == this->uid_
*
*Complexity = O(1)
*/
incidentiterator_triangle triangle_end() const {
return IncidentIterator_Triangle(mesh_, uid_, 3);
}
private:
friend class Mesh;
Triangle(Mesh* mesh, size_type uid):
uid_(uid), mesh_(mesh) {}
size_type uid_;
Mesh* mesh_;
};
/** Add an triangle to the graph
*@param[in] a, b, c are all valid nodes in the trigraph
*@return The triangle that has been added
*@post new size() = old size() +1
* Complexity: O(d) d is the degree of a node
*/
Triangle add_triangle(const Node& a, const Node& b, const Node& c){
internal_triangle_value new_triangle;
//add edge in graph
auto edge0 = graph_.add_edge(graph_.node(a.index()), graph_.node(b.index()));
new_triangle.nodes[0] = c.index();
new_triangle.edges[0] = edge0.index();
auto edge1 = graph_.add_edge(graph_.node(b.index()), graph_.node(c.index()));
new_triangle.nodes[1] = a.index();
new_triangle.edges[1] = edge1.index();
auto edge2 = graph_.add_edge(graph_.node(a.index()), graph_.node(c.index()));
new_triangle.nodes[2] = b.index();
new_triangle.edges[2] = edge2.index();
double xa = a.position().x;
double ya = a.position().y;
double xb = b.position().x;
double yb = b.position().y;
double xc = c.position().x;
double yc = c.position().y;
new_triangle.area = (xa*yb+xb*yc+xc*ya-xb*ya-xc*yb-xa*yc)/2;
if (new_triangle.area < 0)
new_triangle.area = -new_triangle.area;
Point p = a.position() - b.position();
double nx = p.y;
double ny = 0-p.x;
// check if it is in the right direction
Point checkp = c.position() - a.position();
// check direction, if wrong, flip it. & adjust length to be equal to |e|
if(nx*checkp.x + ny*checkp.y>0) {
nx = -nx;
ny = -ny;
}
new_triangle.n.push_back(Point(nx, ny, 0));
p = b.position() - c.position();
nx = p.y;
ny = 0-p.x;
// check if it is in the right direction
checkp = a.position() - b.position();
// check direction, if wrong, flip it. & adjust length to be equal to |e|
if(nx*checkp.x + ny*checkp.y>0) {
nx = -nx;
ny = -ny;
}
new_triangle.n.push_back(Point(nx, ny, 0));
p = a.position() - c.position();
nx = p.y;
ny = 0-p.x;
// check if it is in the right direction
checkp = b.position() - a.position();
// check direction, if wrong, flip it. & adjust length to be equal to |e|
if(nx*checkp.x + ny*checkp.y>0) {
nx = -nx;
ny = -ny;
}
new_triangle.n.push_back(Point(nx, ny, 0));
tri_vec.push_back(new_triangle);
if(edge0.value().triangle1 == unsigned(-1))
edge0.value().triangle1 = tri_vec.size()-1;
else
edge0.value().triangle2 = tri_vec.size()-1;
if(edge1.value().triangle1 == unsigned(-1))
edge1.value().triangle1 = tri_vec.size()-1;
else
edge1.value().triangle2 = tri_vec.size()-1;
if(edge2.value().triangle1 == unsigned(-1))
edge2.value().triangle1 = tri_vec.size()-1;
else
edge2.value().triangle2 = tri_vec.size()-1;
return Triangle(this,tri_vec.size()-1);
}
/** Determine if this Triangle belongs to this Graph
* @return True if @a t is currently a Triangle of this Graph
*
* Complexity: O(1).
*/
bool has_triangle(const Triangle& t){
//compare the trigraph_ and uid_
return ((t.graph_ != this->graph_) && (t.uid_ < tri_vec.size()));
}
/** Return the triangle with index @a i.
* @pre 0 <= @a i < size()
* @post result.index() == i
* Complexity: O(1).
*/
Triangle triangle(size_type i){
assert(i<tri_vec.size());
return Triangle(this,i);
}
// /** Return the first triangle adjacent to the input edge.
// * @param[in] e, an edge inside the mesh
// * @pre @a e.index() < graph_->num_edges()
// * @post result.uid_ == graph_->get_edge_value(edge_uid).F_triangle1.
// * Complexity: O(1).
// */
// Triangle triangle1(const Edge& e){
// return Triangle(this,graph_.edges[e.index()].triangle1);
// }
// /** Return the second triangle adjacent to the input edge.
// * @param[in] e, an edge inside the mesh
// * @pre @a e.index() < graph_->num_edges()
// * @post result.uid_ == graph_->get_edge_value(edge_uid).F_triangle2.
// * Complexity: O(1).
// */
// Triangle triangle2(const Edge& e){
// return Triangle(this,graph_.edges[e.index()].triangle2);
// }
class NodeIterator:private totally_ordered<NodeIterator> {
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. */
NodeIterator() {
}
//return the Node pointed by the interator
Node operator*() const{
return mesh_->node((*it_).index());
}
NodeIterator& operator++(){
++it_;
return *this;
}
bool operator==(const NodeIterator& x) const{
return (it_ == x.it_);
}
private:
friend class Mesh;
Mesh* mesh_;
typename GraphType::node_iterator it_;
//private constructor for graph to construct NodeIterator instance
NodeIterator(const Mesh* mesh, typename GraphType::node_iterator it)
: mesh_(const_cast<Mesh*>(mesh)), it_(it){
}
};
/* Return an iterator points to the first node in graph_.
* Complexity: O(1).
*/
node_iterator node_begin(){
return NodeIterator(this, graph_.node_begin());
}
/* Return an iterator points to the last node in graph_.
* Complexity: O(1).
*/
node_iterator node_end(){
return NodeIterator(this, graph_.node_end());
}
class EdgeIterator:private totally_ordered<EdgeIterator> {
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. */
EdgeIterator() {
}
//return the Edge pointed by the interator
Edge operator*() const{
return mesh_->edge((*it_).index());
}
EdgeIterator& operator++(){
++it_;
return *this;
}
bool operator==(const EdgeIterator& x) const{
return (it_ == x.it_);
}
private:
friend class Mesh;
Mesh* mesh_;
typename GraphType::edge_iterator it_;
//private constructor for graph to construct EdgeIterator instance
EdgeIterator(const Mesh* mesh, typename GraphType::edge_iterator it)
: mesh_(const_cast<Mesh*>(mesh)), it_(it){
}
};
/* Return an iterator points to the first edge in graph_.
* Complexity: O(1).
*/
edge_iterator edge_begin(){
return EdgeIterator(this, graph_.edge_begin());
}
/* Return an iterator points to the last edge in graph_.
* Complexity: O(1).
*/
edge_iterator edge_end(){
return EdgeIterator(this, graph_.edge_end());
}
class IncidentEdgeIterator:private totally_ordered<IncidentEdgeIterator> {
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 IncidentEdgeIterator. */
IncidentEdgeIterator() {
}
//return the Edge pointed by the interator
Edge operator*() const{
return mesh_->edge((*it_).index());
}
IncidentEdgeIterator& operator++(){
++it_;
return *this;
}
bool operator==(const IncidentEdgeIterator& x) const{
return (it_ == x.it_);
}
private:
friend class Mesh;
Mesh* mesh_;
typename GraphType::incident_iterator it_;
//private constructor for graph to construct EdgeIterator instance
IncidentEdgeIterator(const Mesh* mesh, typename GraphType::incident_iterator it)
: mesh_(const_cast<Mesh*>(mesh)), it_(it){
}
};
//Iterator that iterates through the adjacent triangle of a node
class IncidentIterator_Node:private totally_ordered<IncidentIterator_Node>
{
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 std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid IncidentIterator_Node*/
IncidentIterator_Node(){
}
/* Return the Triangle pointed by the IncidentIterator_Node
* @post return.uid_ = t_
*/
Triangle operator*() const{
return mesh_->triangle(t_);
}
/* return the IncidentIterator_Node that points to the
* next incident triangle
* @pre t_ old != -1
* @post If t_ old has two adjacent triangles, init_ != last_
* t_ new points to the adjacent triangle which does not
* contain node last_.
* If t_ old has two adjacent triangles, init_ == last_, t_ new = -1
* If t_ old has one adjacent triangles, t2_!=-1, t_ new = t2_
* If t_ old has one adjacent triangles, t2_ == -1, t_ new = -1
*/
IncidentIterator_Node& operator++(){
//get an incident edge of the node
//record the last_ = first_ = incident_edge.node().index()
//t_ = incident_edge.value().triangle1 t2_ = incident_edge.value().triangle2
//get the new t_ and update last_
//if t_ == t2_ set t_ to invalid to mark the end
//if meets the border. set last_ = init_ and explore t2_
//when t2_ meets the end, set t_ to invilid
if (t_ == t2_){
t_ = -1;
}
else{
Triangle old = mesh_->triangle(t_);
size_type i = 0;
while(old.node(i).index() != last_)
++i;
if (old.mesh_->graph_.edge(old.edge(i).index()).value().triangle1 == unsigned(-1) ||
old.mesh_->graph_.edge(old.edge(i).index()).value().triangle2 == unsigned(-1)){
if (t2_ == unsigned(-1))
t_ = -1;
else{
t_ = t2_;
t2_ = -1;
last_ = init_;
}
}
else{
if (old.edge(i).node1().index() == n_)
last_ = old.edge(i).node2().index();
else
last_ = old.edge(i).node1().index();
if (old.mesh_->graph_.edge(old.edge(i).index()).value().triangle1 == t_)
t_ = old.mesh_->graph_.edge(old.edge(i).index()).value().triangle2;
else
t_ = old.mesh_->graph_.edge(old.edge(i).index()).value().triangle1;
}
}
return *this;
}
//True if n_ and t_ are the same
bool operator==(const IncidentIterator_Node& x) const{
if (mesh_ == x.mesh_ && t_ == x.t_ && n_ == x.n_)
return true;
return false;
}
private:
friend class Mesh;
Mesh* mesh_;
size_type n_; //The uid of the node in the center
size_type t_; //The uid of the triangle pointed to
size_type t2_; //the uid of the triangle to be explored when t_ reaches boundary
size_type last_; //the uid of the other node of the edge just discovered
size_type init_; //the uid of the other node of the first edge
/** Construct a valid IncidentIterator_Node*/
IncidentIterator_Node(const Mesh* m, size_type n, size_type t, size_type t2, size_type last, size_type init): mesh_(const_cast<Mesh*>(m)),n_(n),t_(t),t2_(t2),last_(last),init_(init){
}
};
/* Get the iterator that points to the first adjacent triangle of a node
* @param n The node in the center
* @return the iterator that points to the first adjacent triangle of @a n
* @post result.n_ == n.uid_
* @post result.t_ result.t2_ are the adjacent triangles of edge(n_, result.last_)
* @post result.last_ == result.first_
*/
IncidentIterator_Node triangle_begin(Node n){
Edge start = *(n.edge_begin());
size_type othernode;
if (start.node1().index() == n.index())
othernode = start.node2().index();
else
othernode = start.node1().index();
return IncidentIterator_Node(this, n.index(), graph_.edge(start.index()).value().triangle1,
graph_.edge(start.index()).value().triangle2, othernode, othernode);
}
/* Get the iterator that points to an invalid triangle of a node
* @param n The node in the center
* @return the iterator that points to an invalid triangle of @a n
* @post result.n_ == n.uid_
* @post result.t_ == -1
*/
IncidentIterator_Node triangle_end(Node n){
return IncidentIterator_Node(this, n.index(), -1,
-1, -1, -1);
}
//Iterators:
//all triangles
//adjacent triangles of a node
//adjacent triangles of a triangle
//Iterator that iterates through the adjacent triangle of a triangle
/** @class Mesh::IncidentIterator_Triangle
* @brief Interator Class for triangles incident to a triangle. A forward iterator.
* @RI graph_pointer != nullptr && incident_i <= 2
*/
class IncidentIterator_Triangle:private totally_ordered<IncidentIterator_Triangle>
{
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 std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid IncidentIterator_Triangle*/
IncidentIterator_Triangle(){
}
/**return the Triangle pointed by the IncidentIterator_Triangle
*@pre this != nullptr
*@post result is a triange adjacent to Triangle(this->mesh_,this->triangle_uid)
*@post result is one of the two adjacent triangles to the ith edge of Triangle(this->mesh_,this->triangle_uid)
*@post result.uid_ != this->triangle_uid
*
*Complexity = O(1)
*/
Triangle operator*() {
size_type e_uid = mesh_->tri_vec[triangle_uid].edges[incident_i];
//Edge e = mesh_->graph_.edge(e_uid);
if(mesh_->graph_.edge(e_uid).value().triangle1 != triangle_uid)
return Triangle(mesh_,mesh_->graph_.edge(e_uid).value().triangle1);
else
return Triangle(mesh_,mesh_->graph_.edge(e_uid).value().triangle2);
}
/*return the IncidentIterator_Triangle that points to the next incident edge
*@pre this->incident_i < 2
*@post result.incident_i > this->incident_i
*@post result.incident_i <=2
*
*Complexity = O(1)
*/
IncidentIterator_Triangle& operator++(){
incident_i++;
return *this;
}
/*return true if the two triangles are same and false if not
*@param[in] x, reference to a triangle
*@pre @a x.uid_ >=0
*@pre triangle_uid >=0
*@post result==true if @a x.uid_ == this->triangle_uid && @a x.mesh_ == this->mesh_
*@post result==false if @a x.uid_ != this->triangle_uid || @a x.mesh_ != this->mesh_
*
*Complexity = O(1)
*/
bool operator==(const IncidentIterator_Triangle& x) const{
return((x.triangle_uid == this->triangle_uid) && (x.mesh_ == this->mesh_) && (x.incident_i == this->incident_i));
}
size_type index(){
return incident_i;
}
private:
friend class Mesh;
Mesh* mesh_; //poiter pointing to this Mesh
size_type triangle_uid; //the uid of this triangle
/** incident_i represents the ith edge of this triangle*/
size_type incident_i;
/** Construct a valid IncidentIterator_Triangle*/
IncidentIterator_Triangle(const Mesh* m, size_type uid, size_type i): mesh_(const_cast<Mesh*>(m)),triangle_uid(uid),incident_i(i){
}
};
class TriangleIterator: private totally_ordered<TriangleIterator>{
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 std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
TriangleIterator(){}
Triangle operator*() const{
return mesh_->triangle(idx_);
}
TriangleIterator& operator++(){
++idx_;