-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.hpp
626 lines (508 loc) · 16.3 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
#pragma once
/** @file Mesh.hpp
* @brief An undirected subset of the graph class that stores triangles
*/
#include "CS207/Util.hpp"
#include <algorithm>
#include <vector>
#include <cassert>
#include <Graph.hpp>
using namespace std;
/** @class Mesh
* @brief A mesh triangular graph class
* @tparam T The value type for a triangle
* @tparam E The value type for an edge
* @tparam N The value type for a node
*/
template <typename N, typename E, typename T>
class Mesh {
private:
struct tri_info_type;
typedef Graph<N,E> GraphType;
public:
/** Template type of a Vertex of the Triangle **/
typedef N node_value_type;
/** Template type of a Edge of the Triangle . */
typedef E edge_value_type;
/** Template type of a Triangle **/
typedef T tri_value_type;
/** Predeclaration of Node type. */
class Node;
/** Predeclaration of Edge type. */
class Edge;
/** Predeclaration of Triangle type. */
class Triangle;
/** Graph typedefs **/
typedef typename Graph<N,E>::Node node_type;
typedef typename Graph<N,E>::Edge edge_type;
typedef typename Graph<N,E>::Node Node;
typedef typename Graph<N,E>::Edge Edge;
typedef typename Graph<N,E>::node_iterator node_iterator;
typedef typename Graph<N,E>::edge_iterator edge_iterator;
/** Synonym for Mesh (following STL conventions). */
typedef Mesh mesh_type;
/** Synonym for Triangle (following STL conventions). */
typedef Triangle tri_type;
/** Type of indexes and sizes. */
typedef unsigned size_type;
/** Type used to return area of triangles. */
typedef double value_type;
/** Forward iterators, which iterates over all triangles. */
class tri_iterator;
/** Forward iterators, which iterates over all triangles with the same vertex. */
class vertex_iterator;
/** Forward iterators, which iterates over all triangles with the same edge. */
class tri_edge_iterator;
/** Initializes an empty mesh. */
Mesh(){
}
/** Default destructor */
~Mesh() = default;
/** Destroys the ability for users to assign mesh by reference */
Mesh& operator=(const Mesh&) = delete;
/** Equality comparison function */
bool operator==(const Mesh& n) const{
return n.graph_==this->graph_;
}
/** Returns the number of nodes in the graph. */
size_type num_nodes() {
return this->graph_.size();
}
/** Adds a node to this graph. O(1) amortized operations */
Node add_node(const Point& position){
return this->graph_.add_node(position);
}
/** Determines if a node is a valid node of this graph. */
bool has_node(const Node& n) const {
return this->graph_.has_node(n);
}
/** Returns the node at index i in the mesh. */
Node node(size_type i) {
return this->graph_.node(i);
}
/** Returns the edge at index i in the mesh. */
Edge edge(size_type i) {
return this->graph_.edge(i);
}
/** For a given edge index, returns true if it is shared */
bool has_neighbor(size_type i){
assert(i<edge_lookup_.size());
return (edge_lookup_[i].size() >=2);
}
/** Returns an iterator to this triangle */
node_iterator node_begin() {
return this->graph_.node_begin();
}
/** Returns an invalid node iterator */
node_iterator node_end() {
return this->graph_.node_end();
}
/** Returns an iterator to an edge */
edge_iterator edge_begin(){
return this->graph_.edge_begin();
}
/** Returns an invalid edge_iterator */
edge_iterator edge_end(){
return this->graph_.edge_end();
}
/** Returns an iterator to the triangle class. */
tri_iterator tri_begin() {
return tri_iterator(this, 0);
}
/** Returns an invalid iterator to the triangle class */
tri_iterator tri_end() {
return tri_iterator(this, size());
}
/** Returns an iterator to the triangle class. */
vertex_iterator vertex_begin(size_type v) {
assert(v<num_nodes());
return vertex_iterator(this, v,0);
}
/** Returns an invalid iterator to the triangle class */
vertex_iterator vertex_end(size_type v) {
assert(v<num_nodes());
return vertex_iterator(this, v, node_lookup_[v].size());
}
/** Returns an iterator to the triangle class. */
tri_edge_iterator tri_edge_begin(size_type e1) {
assert(e1<num_edges());
return tri_edge_iterator(this, e1, 0);
}
/** Returns an invalid iterator to the triangle class */
tri_edge_iterator tri_edge_end(size_type e1) {
assert(e1<num_edges());
return tri_edge_iterator(this, e1,edge_lookup_[e1].size());
}
class Triangle: private totally_ordered<Triangle> {
private:
friend class Mesh;
Mesh* set_;
size_type idx_;
/** Returns a reference to the Triangle */
tri_info_type& fetch() const{
return set_->triangles_[idx_];
}
Triangle(const Mesh* set, size_type idx)
: set_(const_cast<Mesh*>(set)), idx_(idx){
}
/** Helper function */
bool notEqual(const Node n1, const Node n2){
if (!(n1==node1() or n2==node1()))
return node1();
if (!(n1==node2() or n2==node2()))
return node2();
return node3();
}
public:
/** Constructs an invalid triangle */
Triangle(){
}
/** Returns this triangle's index */
const size_type& index() const{
return idx_;
}
/** Returns this triangle's position. */
const Point& position() const {
return (node1().position()+node2().position()+node3.position())/3;
}
/** Returns this triangle's area. */
value_type area() const{
Point A = node1().position();
Point B = node2().position();
Point C = node3().position();
return abs( (A.x *(B.y-C.y) + B.x*(C.y - A.y)+ C.x*(A.y-B.y))/2.0);
}
/** Returns a reference to this triangle's value */
tri_value_type& value(){
return fetch().value_;
}
/** Returns a read-only reference to this triangle's value */
const tri_value_type& value() const{
return fetch().value_;
}
/** Calculates the normal vector between two adjacent triangles. */
Point norm_vector(const Triangle& t1){
if (!t1.has_node(node1()))
{
auto dx =node2().position().x - node3().position().x;
auto dy =node2().position().y - node3().position().y;
Point Normal = Point(-dy, dx, 0);
if (norm(Normal, node1().position()) < norm(Normal, (node2().position() + node3.position())/2.0 )) {
Normal *= -1.0;
}
return Normal;
}
else if (!t1.has_node(node2()))
{
auto dx =node1().position().x - node3().position().x;
auto dy =node1().position().y - node3().position().y;
Point Normal = Point(-dy, dx, 0);
if (norm(Normal, node2().position()) < norm(Normal, (node1().position() + node3.position())/2.0 )) {
Normal *= -1.0;
}
return Normal;
}
else{
auto dx =node1().position().x - node2().position().x;
auto dy =node1().position().y - node2().position().y;
Point Normal = Point(-dy, dx, 0);
if (norm(Normal, node3().position()) < norm(Normal, (node1().position() + node2.position())/2.0 )) {
Normal *= -1.0;
}
return Normal;
}
}
/** Returns the normal vector of an Edge */
Point norm_vector(const Edge& e){
if (e.node1() != node1() && e.node2() != node1())
{
auto dx =node2().position().x - node3().position().x;
auto dy =node2().position().y - node3().position().y;
Point Normal = Point(-dy, dx, 0);
Point outside = node1().position() - node2().position();
if(dot(outside, Normal) > 0)
Normal *= -1.0;
return Normal;
}
else if (e.node1() != node2() && e.node2() != node2())
{
auto dx =node1().position().x - node3().position().x;
auto dy =node1().position().y - node3().position().y;
Point Normal = Point(-dy, dx, 0);
Point outside = node2().position() - node1().position();
if(dot(outside, Normal) > 0)
Normal *= -1.0;
return Normal;
}
else {
auto dx =node1().position().x - node2().position().x;
auto dy =node1().position().y - node2().position().y;
Point Normal = Point(-dy, dx, 0);
Point outside = node3().position() - node2().position();
if(dot(outside, Normal) > 0)
Normal *= -1.0;
return Normal;
}
}
/** Returns the adjacent triangles to this node */
std::vector<Point> adjacent_triangle_vector() const{
std::vector<Point> tri;
auto adj = set_->adjacent_triangles(*this);
for (size_type i=0; i<adj.size(); ++i){
tri.push_back(norm_vector(adj[i]));
}
return tri;
}
/** Determines if a triangle has a particular node */
bool has_node(Node& n1){
return (fetch().n1_==n1.index() or fetch().n2_==n1.index() or fetch().n3_==n1.index());
}
/** Returns the node of this triangle */
Node node1() const {
return set_->node(fetch().n1_);
}
/** Returns the node of this triangle */
Node node2() const {
return set_->node(fetch().n2_);
}
/** Returns the node of this triangle */
Node node3() const {
return set_->node(fetch().n3_);
}
/** Returns the mesh edge of this triangle */
Edge edge1() const {
return set_->edge(fetch().e1_);
}
/** Returns the mesh edge of this triangle */
Edge edge2() const {
return set_->edge(fetch().e2_);
}
/** Returns the mesh edge of this triangle */
Edge edge3() const {
return set_->edge(fetch().e3_);
}
/** Determines the equality of two triangles */
bool operator==(const Triangle& n) const{
return n.idx_==idx_;
}
/** Abstractly compares two triangles for ordering purposes. */
bool operator<(const Triangle& n) const{
return idx_<n.idx_;
}
};
/** Returns a node's value */
node_value_type& value(Node n, node_value_type value){
n.value() = value;
return n.value();
}
/** Returns a read-only reference to a nodes value */
const N& value(const Node& n) const{
return n.value();
}
/** Returns a vectors of all adjacent triangles */
std::vector<Point> adjacent_triangle_vector(const Triangle& t){
return t.adj_triangle_vector();
}
/** Returns adjacent triangles of a triangle */
std::vector<Triangle> adjacent_triangles(Triangle t){
std::vector<Triangle> tri;
for (auto i = edge_lookup_[t.edge1().index()].begin(); i < edge_lookup_[t.edge1().index()].end(); ++i){
if ( (*i) != t.idx_)
tri.push_back(triangle(*i));
}
for (auto i = edge_lookup_[t.edge2().index()].begin(); i < edge_lookup_[t.edge2().index()].end(); ++i){
if ((*i) != t.idx_)
tri.push_back(triangle(*i));
}
for (auto i = edge_lookup_[t.edge3().index()].begin() ; i < edge_lookup_[t.edge3().index()].end(); ++i){
if ((*i) != t.idx_)
tri.push_back(triangle(*i));
}
return tri;
}
/** Returns the number of triangles in the mesh. */
size_type size() const {
return this->triangles_.size();
}
/** Synonym for size(). */
size_type num_triangles() const {
return size();
}
/** Number of edges in this graph. */
size_type num_edges() const {
return this->graph_.num_edges();
}
/** Returns the triangle at index @a i. */
Triangle triangle(size_type i) const {
assert(i<size());
return Triangle(this,i);
}
/** Adds a triangle to the mesh. */
Triangle add_triangle(Node& n1, Node& n2, Node& n3){
/* Triangle index */
size_type tri_idx = size();
/* Edges */
edge_type e1 = this->graph_.add_edge(n1, n2);
edge_type e2 = this->graph_.add_edge(n1, n3);
edge_type e3 = this->graph_.add_edge(n2, n3);
/*Update tri data*/
tri_info_type tri{this,n1.index(),n2.index(),n3.index(),e1.index(),e2.index(),e3.index(),tri_value_type(),tri_idx};
this->triangles_.push_back(tri);
//udpdate edge_lookup table
edge_lookup_[e1.index()].push_back(tri_idx);
edge_lookup_[e2.index()].push_back(tri_idx);
edge_lookup_[e3.index()].push_back(tri_idx);
node_lookup_[n1.index()].push_back(tri_idx);
node_lookup_[n2.index()].push_back(tri_idx);
node_lookup_[n3.index()].push_back(tri_idx);
return this->triangle(tri_idx);
}
/** Iterates over all triangles. */
class tri_iterator: private totally_ordered<tri_iterator> {
private:
friend class Mesh;
Mesh* set_;
size_type idx_;
tri_iterator(const Mesh* set, size_type idx)
: set_(const_cast<Mesh*>(set)),idx_(idx) {
}
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;
/** Constructs an invalid tri_iterator. */
tri_iterator() {
}
/** Return the Triangle at the position this iterator is dereferenced. */
Triangle operator*() const{
return set_->triangle(idx_);
}
/** Increments the iterator. */
tri_iterator& operator++(){
++idx_;
return *this;
}
/** Test the equality of an iterator based on current position. */
bool operator==(const tri_iterator& a) const{
return idx_==a.idx_ && set_==a.set_;
}
/** Test the inequality of an iterator based on current position. */
bool operator<(const tri_iterator& a) const{
return idx_<a.idx_ ;
}
};
/** Iterates over all triangles incident to a node. */
class vertex_iterator: private totally_ordered<vertex_iterator> {
private:
friend class Mesh;
Mesh* set_;
size_type nidx_;
size_type idx_;
vertex_iterator(const Mesh* set, size_type n1, size_type idx)
: set_(const_cast<Mesh*>(set)),nidx_(n1),idx_(idx) {
}
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;
/** Constructs an invalid tri_iterator. */
vertex_iterator() {
}
/** Return the Triangle at the position this iterator is dereferenced. */
Triangle operator*() const{
return set_->triangle(set_->node_lookup_[nidx_][idx_]);
}
/** Increments the iterator. */
vertex_iterator& operator++(){
++idx_;
return *this;
}
/** Test the equality of an iterator based on current position. */
bool operator==(const vertex_iterator& a) const{
return idx_==a.idx_ && set_==a.set_ && nidx_==a.nidx_;
}
/** Test the inequality of an iterator based on current position. */
bool operator<(const vertex_iterator& a) const{
return nidx_<a.nidx_;
}
};
/** Iterates over all triangles incident to a node. */
class tri_edge_iterator: private totally_ordered<tri_edge_iterator> {
private:
friend class Mesh;
Mesh* set_;
size_type eidx_;
size_type idx_;
tri_edge_iterator(const Mesh* set, size_type e1, size_type idx)
: set_(const_cast<Mesh*>(set)),eidx_(e1),idx_(idx) {
}
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;
/** Constructs an invalid iterator. */
tri_edge_iterator() {
}
/** Return the Triangle at the position this iterator is dereferenced. */
Triangle operator*() const{
return set_->triangle(set_->edge_lookup_[eidx_][idx_]);
}
/** Increments the iterator. */
tri_edge_iterator& operator++(){
++idx_;
return *this;
}
/** Test the equality of an iterator based on current position. */
bool operator==(const tri_edge_iterator& a) const{
return idx_==a.idx_ && set_==a.set_ && eidx_==a.eidx_;
}
/** Test the equality of an iterator based on current position. */
bool operator<(const tri_edge_iterator& a) const{
return idx_<a.idx_;
}
};
private:
/* Stores Graph Information */
typename GraphType::graph_type graph_;
/** Lookup tables */
map<size_type, vector<size_type> > edge_lookup_;
map<size_type, vector<size_type> > node_lookup_;
/*indexed by node UID. Stores triangles data.**/
std::vector<tri_info_type> triangles_;
/** Stores Triangle Information */
struct tri_info_type {
Mesh* set_;
size_type n1_;
size_type n2_;
size_type n3_;
size_type e1_;
size_type e2_;
size_type e3_;
tri_value_type value_;
size_type index_;
};
};