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