Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/meshgen2d/src/SSSFVoronoiSegment.cpp
3196 views
1
#include "SSSFVoronoiSegment.h"
2
#include "Border.h"
3
#include "SSSFVertex.h"
4
#include "MGError.h"
5
6
#include <iostream>
7
8
#include <math.h>
9
#include <fstream>
10
#include <stdio.h>
11
#ifdef WIN32
12
#include <direct.h>
13
#else
14
#include <unistd.h>
15
#endif
16
17
#include "coreGeometry.h"
18
19
void SSSFVoronoiSegment::
20
discretize( NodeMap& fixedNodes, NodeMap& allNodes, std::list< Element* >& allElements )
21
{
22
triangulate();
23
removeBoundaryConnectors();
24
25
makeSeed(fixedNodes);
26
27
generate();
28
exportNodes( allNodes, allElements );
29
}
30
31
void SSSFVoronoiSegment::
32
makeSeed(NodeMap& fixedNodes)
33
{
34
SSSFVertex *vtx;
35
double nx, ny;
36
37
if( explicitSeed )
38
{
39
int j;
40
for (j = 0; j < 3; j++)
41
{
42
NodeMapIt it = fixedNodes.find(seedTags[j]);
43
if (it == fixedNodes.end())
44
{
45
std::cerr << "Seed node " << seedTags[j] << "does not exist!" << std::endl;
46
exit(1);
47
}
48
seed[j] = static_cast<MeshNode *>(it->second);
49
}
50
51
for( j = 0; j < 3; ++j )
52
{
53
Vertex *v = root->firstAcceptableFound( seed[j]->x, seed[j]->y);
54
55
if( addSite( v, seed[j], false ) == true )
56
{
57
int len = newVertices.size();
58
for( int i = 0; i < len; ++i )
59
{
60
vtx = static_cast<SSSFVertex *>( newVertices[i] );
61
vtx->setBody( tag );
62
}
63
recycle();
64
}
65
else
66
{
67
blm_error("Could not insert the seed node", seedTags[j]);
68
}
69
}
70
}
71
else
72
{
73
Node *a, *b;
74
75
baseEdge->midNodes( a, b, baseDirection );
76
77
seed[0] = static_cast<MeshNode *>(a);
78
seed[1] = static_cast<MeshNode *>(b);
79
80
vtx = static_cast<SSSFVertex *>( root->vertexWith( seed[0], seed[1] ) );
81
vtx->computeNewCoordinates( *bg, nx, ny );
82
seed[2] = new MeshNode( nx, ny );
83
84
if( addSite( vtx, seed[2], false ) == true )
85
{
86
int len = newVertices.size();
87
for( int i = 0; i < len; ++i )
88
{
89
vtx = static_cast<SSSFVertex *>( newVertices[i] );
90
vtx->setBody( tag );
91
}
92
recycle();
93
}
94
else
95
{
96
std::cerr << "Could not insert the seed!" << std::endl;
97
exit(1);
98
}
99
}
100
101
vtx = static_cast<SSSFVertex *>( root->vertexWith( seed[0], seed[1] ) );
102
if (vtx == NULL)
103
{
104
std::cerr << "Could not insert the seed!" << std::endl;
105
exit(1);
106
}
107
108
vtx->makeAccepted();
109
vtx->radiate( actives, crystals );
110
}
111
112
void SSSFVoronoiSegment::
113
generate()
114
{
115
SSSFVertex *vs;
116
int i;
117
double nx, ny;
118
double ref;
119
120
const double sqrt3 = 1.7320508075688772935274463415059;
121
122
std::cout << "Generating" << std::endl;
123
124
nx = ny = 0.0;
125
126
int steps = 0;
127
while( actives.size() > 0 || crystals.size() > 0)
128
{
129
bool active = true;
130
SSSFVertex *vtx;
131
132
if( crystals.size() > 0)
133
{
134
active = false;
135
vtx = static_cast<SSSFVertex *>( crystals.first() );
136
}
137
else
138
{
139
vtx = static_cast<SSSFVertex *>( actives.first() );
140
}
141
if( !vtx->borderTest( ) )
142
{
143
if( vtx->computeNewCoordinates( *bg, nx, ny ) > 0 )
144
{
145
MeshNode *nd = new MeshNode( nx, ny );
146
147
if( addSite( vtx, nd, false, false ) == true )
148
{
149
if( deleted.size() == 1 )
150
{
151
// This is a terrible hack and we get a lot of it wrong!
152
vtx->unDelete();
153
vtx->makeAccepted();
154
vtx->radiate( actives, crystals );
155
deleted.erase( deleted.begin(), deleted.end() );
156
}
157
else
158
{
159
std::list< Vertex* >::iterator vxIt;
160
for( vxIt = deleted.begin(); vxIt != deleted.end(); ++vxIt )
161
{
162
vs = static_cast<SSSFVertex *>( *vxIt );
163
if( vs->isCrystal() && vs->isAtHeap() )
164
crystals.remove( vs );
165
else if( vs->isActive() && vs->isAtHeap() )
166
actives.remove( vs );
167
}
168
169
int len = newVertices.size();
170
for( i = 0; i < len; ++i )
171
{
172
vs = static_cast<SSSFVertex *>( newVertices[i] );
173
vs->setBody( tag );
174
}
175
176
for( i = 0; i < len; ++i )
177
{
178
vs = static_cast<SSSFVertex *>( newVertices[i] );
179
ref = bg->interpolate( vs->vx, vs->vy );
180
ref /= sqrt3;
181
182
if( vs->rightSize( ref ) )
183
{
184
vs->makeAccepted();
185
}
186
else
187
{
188
vs->makeWaiting();
189
}
190
}
191
192
for( i = 0; i < len; ++i )
193
{
194
vs = static_cast<SSSFVertex *>( newVertices[i] );
195
if( vs->isWaiting() )
196
{
197
vs->testIfActive( actives, crystals );
198
}
199
else if( vs->isAccepted() )
200
{
201
vs->radiate( actives, crystals );
202
}
203
}
204
}
205
}
206
recycle();
207
}
208
}
209
++steps;
210
}
211
}
212
213
void SSSFVoronoiSegment::
214
getVertices( std::vector< Vertex * >& v, const int count )
215
{
216
int i;
217
for( i = 0; i < count; ++i )
218
{
219
if( vertexStore.empty() ) break;
220
v.push_back( vertexStore.back() );
221
vertexStore.pop_back();
222
}
223
224
for( ; i < count; ++i )
225
{
226
SSSFVertex *vtx = new SSSFVertex;
227
v.push_back( vtx );
228
allVertices.push_back( vtx );
229
}
230
}
231
232