-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
202 lines (181 loc) · 6.41 KB
/
Mesh.cpp
File metadata and controls
202 lines (181 loc) · 6.41 KB
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
//
// Created by bpiranda on 22/11/2019.
//
#include <glutWindow.h>
#include "Mesh.h"
#include "Polygon.h"
#include <list>
Mesh::Mesh(const float p_vert[][2], int p_nvert,const int p_tri[][3],int p_ntri) {
for (int i=0; i<p_nvert; i++) {
vertices.emplace_back(Vector2D(p_vert[i][0],p_vert[i][1]));
}
for (int i=0; i<p_ntri; i++) {
triangles.emplace_back(Triangle(&vertices[p_tri[i][0]],
&vertices[p_tri[i][1]],&vertices[p_tri[i][2]]));
}
checkDelaunay();
}
Mesh::Mesh(vector<Vector2D> &p_vert) {
int n = p_vert.size();
assert(n>=3);
if (n==3) {
for (auto v:p_vert) {
vertices.emplace_back(Vector2D(v.x,v.y));
}
// order the 3 points
Triangle T(&vertices[0],&vertices[1],&vertices[2]);
if (T.isOnTheLeft(&vertices[2],&vertices[0],&vertices[1])) {
triangles.push_back(T);
} else {
triangles.emplace_back(Triangle(&vertices[0],&vertices[2],&vertices[1]));
}
} else {
MyPolygon CH(p_vert); // calculate convex hull
Vector2D *ptrPt;
int nCH = CH.getNbPts();
for (int i=0; i<nCH; i++) {
ptrPt=CH.getPtrPt(i);
vertices.emplace_back(Vector2D(ptrPt->x,ptrPt->y));
}
// construct a triangulation of the hull
for (int i=1; i<nCH-1; i++) {
triangles.emplace_back(Triangle(&vertices[0],&vertices[i],&vertices[(i+1)%nCH]));
}
// add internal points
auto v = p_vert.begin();
while (v!=p_vert.end()) {
if (!CH.isAVertex(*v)) {
cout << "internal point:" << *v << endl;
subdivide(*v);
}
v++;
}
}
checkDelaunay();
}
void Mesh::draw() {
for (auto t :triangles) {
t.draw();
}
int i=0;
glColor3fv(&BLACK[0]);
for (auto p : vertices) {
GlutWindow::drawText(p.x-10,p.y,to_string(i++),GlutWindow::ALIGN_RIGHT);
}
for (auto t :triangles) {
t.drawCircle();
}
}
void Mesh::onMouseMove(double x, double y) {
Vector2D mousePos((float)x,(float)y);
auto t = triangles.begin();
while (t!=triangles.end()) {
t->onMouseMove(mousePos);
t++;
}
}
bool Mesh::checkDelaunay() {
bool test=true;
auto t = triangles.begin();
while (t!=triangles.end()) {
test= t->checkDelaunay(vertices) && test;
t++;
}
return test;
}
const Triangle* Mesh::getTriangle(const Vector2D &pt) const {
auto t = triangles.begin();
while (t!=triangles.end() && !t->isInside(pt)) {
t++;
}
if (t==triangles.end()) {
return nullptr;
}
return &(*t);
}
Triangle* Mesh::findTriangle(const Vector2D *P1,const Vector2D *P2) {
auto t = triangles.begin();
while (t!=triangles.end() && !((t->ptr[0]==P1 && t->ptr[1]==P2) ||
(t->ptr[1]==P1 && t->ptr[2]==P2) || (t->ptr[2]==P1 && t->ptr[0]==P2))) {
t++;
}
if (t==triangles.end()) {
return nullptr;
}
return &(*t);
}
Triangle* Mesh::neighborInside(const Triangle *T) {
Triangle *T1 = findTriangle(T->ptr[1],T->ptr[0]);
if (T1) {
if (T1->ptr[0]!=T->ptr[0] && T1->ptr[0]!=T->ptr[1] && T1->ptr[0]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[0])) { return T1; }
if (T1->ptr[1]!=T->ptr[0] && T1->ptr[1]!=T->ptr[1] && T1->ptr[1]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[1])) { return T1; }
if (T1->ptr[2]!=T->ptr[0] && T1->ptr[2]!=T->ptr[1] && T1->ptr[2]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[2])) { return T1; }
}
T1 = findTriangle(T->ptr[2],T->ptr[1]);
if (T1) {
if (T1->ptr[0]!=T->ptr[0] && T1->ptr[0]!=T->ptr[1] && T1->ptr[0]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[0])) { return T1; }
if (T1->ptr[1]!=T->ptr[0] && T1->ptr[1]!=T->ptr[1] && T1->ptr[1]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[1])) { return T1; }
if (T1->ptr[2]!=T->ptr[0] && T1->ptr[2]!=T->ptr[1] && T1->ptr[2]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[2])) { return T1; }
}
T1 = findTriangle(T->ptr[0],T->ptr[2]);
if (T1) {
if (T1->ptr[0]!=T->ptr[0] && T1->ptr[0]!=T->ptr[1] && T1->ptr[0]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[0])) { return T1; }
if (T1->ptr[1]!=T->ptr[0] && T1->ptr[1]!=T->ptr[1] && T1->ptr[1]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[1])) { return T1; }
if (T1->ptr[2]!=T->ptr[0] && T1->ptr[2]!=T->ptr[1] && T1->ptr[2]!=T->ptr[2] && T->isInsideCircle(*T1->ptr[2])) { return T1; }
}
return nullptr;
}
void Mesh::flip(Triangle &ptr1,Triangle &ptr2) {
// search common edge
Vector2D *P = ptr1.getVertexNotIn(ptr2);
Vector2D *Q = ptr2.getVertexNotIn(ptr1);
Vector2D *R = ptr1.getNextVertex(P);
Vector2D *S = ptr2.getNextVertex(Q);
ptr1.updateVertices(P,R,Q);
ptr2.updateVertices(Q,S,P);
}
void Mesh::solveDelaunay() {
// copy tabTriangles in a list
list<Triangle*> processList;
auto t = triangles.begin();
while (t!=triangles.end()) {
processList.push_back(&(*t));
t++;
}
while (processList.size()>1) {
Triangle *current = processList.front();
processList.pop_front();
cout << "Treat:" << current->whoami(vertices) << endl;
if (!current->isDelaunay) {
cout << "is not valid" << endl;
Triangle *Tneighbor = neighborInside(current);
if (Tneighbor!=nullptr) {
cout << "Neighbor:" << Tneighbor->whoami(vertices) << endl;
flip(*current,*Tneighbor);
// remove Tneighbor form the list
auto tr=processList.begin();
while (tr!=processList.end() && (*tr)!=Tneighbor) {
tr++;
}
if (tr!=processList.end()) processList.erase(tr);
} else {
cout << "no neighbor" << endl;
processList.push_back(current);
}
}
}
}
void Mesh::subdivide(const Vector2D &P) {
auto it=triangles.begin();
while (it!=triangles.end() && !it->isInside(P)) it++;
if (it!=triangles.end()) {
int i = vertices.size();
vertices.emplace_back(Vector2D(P.x,P.y));
Vector2D *v[4] = {(*it).ptr[0],(*it).ptr[1],(*it).ptr[2],&vertices[i]};
triangles.erase(it);
triangles.emplace_back(Triangle(v[0], v[3], v[2]));
triangles.emplace_back(Triangle(v[1], v[2], v[3]));
triangles.emplace_back(Triangle(v[0], v[1], v[3]));
checkDelaunay();
}
}