Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/meshgen2d/src/Vertex.cpp
3196 views
1
#include <math.h>
2
3
#include "Vertex.h"
4
#include "MeshNode.h"
5
#include "coreGeometry.h"
6
7
#include <algorithm>
8
#ifdef _NO_STD_MINMAX
9
#include "minmaxpatch.h"
10
#endif
11
12
static int nextTag = 1;
13
14
Vertex::Vertex() : TriangleElement( -1 )
15
{
16
tag = nextTag;
17
heap = -1;
18
++nextTag;
19
}
20
21
Vertex::Vertex(Node *n1, Node *n2, Node *n3) : TriangleElement( -1 )
22
{
23
tag = nextTag;
24
++nextTag;
25
26
reset(n1, n2, n3);
27
}
28
29
void Vertex::
30
reset(Node *n1, Node *n2, Node *n3)
31
{
32
nodes[0] = n1; nodes[1]= n2; nodes[2] = n3;
33
34
vertexLocation(n1->x ,n1->y, n2->x ,n2->y, n3->x, n3->y, &vx, &vy);
35
radius = distance(vx, vy, n2->x, n2->y);
36
37
deleted = false;
38
heap = -1;
39
body = 0;
40
}
41
42
void Vertex::
43
setVertices(Vertex *v1, Vertex *v2, Vertex *v3)
44
{
45
vertices[0] = v1; vertices[1] = v2; vertices[2] = v3;
46
}
47
48
Vertex* Vertex::
49
firstAcceptableFound(double tx, double ty)
50
{
51
int i;
52
Vertex *curr = this;
53
Vertex *prev = (Vertex *)0;
54
55
int s[3] = {2,3,1};
56
int e[3] = {1,2,3};
57
58
double x[4],y[4];
59
60
x[0] = tx; y[0] = ty;
61
x[1] = nodes[0]->x; y[1] = nodes[0]->y;
62
x[2] = nodes[1]->x; y[2] = nodes[1]->y;
63
x[3] = nodes[2]->x; y[3] = nodes[2]->y;
64
65
do //while(!inCircle(x,y))
66
{
67
for(i = 0; i < 3; ++i)
68
{
69
if((curr->vertices[i]) && (curr->vertices[i] != prev) &&
70
orientation(tx,ty,x[s[i]],y[s[i]],x[e[i]],y[e[i]]))
71
{
72
prev = curr;
73
curr = curr->vertices[i];
74
75
x[0] = tx; y[0] = ty;
76
x[1] = curr->nodes[0]->x; y[1] = curr->nodes[0]->y;
77
x[2] = curr->nodes[1]->x; y[2] = curr->nodes[1]->y;
78
x[3] = curr->nodes[2]->x; y[3] = curr->nodes[2]->y;
79
break;
80
}
81
}
82
} while (i < 3);
83
84
return curr;
85
}
86
87
int Vertex::
88
isDestroyedBy(double tx, double ty, bool extOK)
89
{
90
double x[4],y[4];
91
92
x[0] = tx; y[0] = ty;
93
x[1] = nodes[0]->x; y[1] = nodes[0]->y;
94
x[2] = nodes[1]->x; y[2] = nodes[1]->y;
95
x[3] = nodes[2]->x; y[3] = nodes[2]->y;
96
if(inCircle(x,y))
97
{
98
if(isExternal())
99
{
100
if(!extOK)
101
return -1;
102
else
103
return 1;
104
}
105
else return 1;
106
}
107
return 0;
108
}
109
110
Vertex* Vertex::
111
vertexOwning( Node* a, Node* b)
112
{
113
bool found = false;
114
int i, holder = -1;
115
Vertex *curr = this;
116
Vertex *prev = (Vertex *)0;
117
118
double tx = (a->x + b->x)/2.0;
119
double ty = (a->y + b->y)/2.0;
120
121
int s[3] = {2,3,1};
122
int e[3] = {1,2,3};
123
124
double x[4],y[4];
125
126
x[0] = tx; y[0] = ty;
127
x[1] = nodes[0]->x; y[1] = nodes[0]->y;
128
x[2] = nodes[1]->x; y[2] = nodes[1]->y;
129
x[3] = nodes[2]->x; y[3] = nodes[2]->y;
130
131
// What if we already have Node* a?
132
for(i = 0; i < 3; ++i)
133
{
134
if(nodes[i] == a)
135
{
136
found = true;
137
holder = i;
138
if(nodes[(i+1)%3] == b) return curr;
139
if(nodes[(i - 1 + 3)%3] == b) return curr;
140
break;
141
}
142
}
143
144
while(!found)
145
{
146
for(i = 0; i < 3; ++i)
147
{
148
if((curr->vertices[i]) && (curr->vertices[i] != prev) &&
149
orientation(tx,ty,x[s[i]],y[s[i]],x[e[i]],y[e[i]]))
150
{
151
prev = curr;
152
curr = curr->vertices[i];
153
154
x[0] = tx; y[0] = ty;
155
x[1] = curr->nodes[0]->x; y[1] = curr->nodes[0]->y;
156
x[2] = curr->nodes[1]->x; y[2] = curr->nodes[1]->y;
157
x[3] = curr->nodes[2]->x; y[3] = curr->nodes[2]->y;
158
break;
159
}
160
}
161
162
for(i = 0; i < 3; ++i)
163
{
164
if(curr->nodes[i] == a)
165
{
166
found = true;
167
holder = i;
168
if(curr->nodes[(i+1)%3] == b) return curr;
169
if(curr->nodes[(i - 1 + 3)%3] == b) return curr;
170
break;
171
}
172
}
173
}
174
// So, curr owns Node* a.
175
// Where is Node* b?
176
// Let's go around Node* a using a linked list.
177
178
Vertex* owner = (Vertex*)0;
179
Vertex* start = curr;
180
do {
181
curr = curr->vertices[holder];
182
for(i = 0; i < 3; ++i)
183
{
184
if(curr->nodes[i] == a)
185
{
186
holder = i;
187
break;
188
}
189
}
190
if(curr->nodes[(i+1)%3] == b) return curr;
191
if(curr->nodes[(i - 1 + 3)%3] == b) return curr;
192
} while(curr != start);
193
194
if((curr == start) && (owner == (Vertex*)0)) return (Vertex*)0;
195
return owner;
196
}
197
198
Vertex* Vertex::
199
vertexWith( Node* a, Node* b)
200
{
201
bool found = false;
202
int i;
203
Vertex *curr = this;
204
Vertex *prev = (Vertex *)0;
205
206
double tx = (a->x + b->x)/2.0;
207
double ty = (a->y + b->y)/2.0;
208
209
int s[3] = {2,3,1};
210
int e[3] = {1,2,3};
211
212
double x[4],y[4];
213
214
x[0] = tx; y[0] = ty;
215
x[1] = nodes[0]->x; y[1] = nodes[0]->y;
216
x[2] = nodes[1]->x; y[2] = nodes[1]->y;
217
x[3] = nodes[2]->x; y[3] = nodes[2]->y;
218
219
// What if we already have Node* a?
220
for(i = 0; i < 3; ++i)
221
{
222
if(nodes[i] == a)
223
{
224
if(nodes[(i+1)%3] == b) return curr;
225
if(nodes[(i - 1 + 3)%3] == b) return curr->vertices[(i - 1 + 3)%3];
226
}
227
}
228
229
while(!found)
230
{
231
for(i = 0; i < 3; ++i)
232
{
233
if((curr->vertices[i]) && (curr->vertices[i] != prev) &&
234
orientation(tx,ty,x[s[i]],y[s[i]],x[e[i]],y[e[i]]))
235
{
236
prev = curr;
237
curr = curr->vertices[i];
238
239
x[0] = tx; y[0] = ty;
240
x[1] = curr->nodes[0]->x; y[1] = curr->nodes[0]->y;
241
x[2] = curr->nodes[1]->x; y[2] = curr->nodes[1]->y;
242
x[3] = curr->nodes[2]->x; y[3] = curr->nodes[2]->y;
243
break;
244
}
245
}
246
247
for(int j = 0; j < 3; ++j)
248
{
249
if(curr->nodes[j] == a && curr->nodes[(j+1)%3] == b)
250
{
251
found = true;
252
break;
253
}
254
else if(curr->nodes[j] == a && curr->nodes[((j-1)+3)%3] == b)
255
{
256
curr = curr->vertices[(j - 1 + 3)%3];
257
found = true;
258
break;
259
}
260
}
261
262
if (!found && i == 3) // This is wrong!!!!
263
return NULL;
264
}
265
return curr;
266
}
267
268
void Vertex::
269
labelBodies( const int bd, std::set< std::pair< int, int > >& bounds )
270
{
271
int i;
272
if(body == bd) return;
273
274
body = bd;
275
276
for(i = 0; i < 3; ++i)
277
{
278
int t1 = nodes[i]->tag;
279
int t2 = nodes[(i+1)%3]->tag;
280
281
if(bounds.find( std::make_pair(std::min(t1,t2),std::max(t1,t2)) ) == bounds.end())
282
{
283
vertices[i]->labelBodies( bd, bounds );
284
}
285
}
286
}
287
288