1 #include "VoronoiSegment.h"
2 #include "Border.h"
3 #include "VSVertex.h"
4 
5 #include <iostream>
6 #include <math.h>
7 #include <fstream>
8 #include <stdio.h>
9 #ifdef WIN32
10 #include <direct.h>
11 #else
12 #include <unistd.h>
13 #endif
14 
15 void VoronoiSegment::
discretize(NodeMap & fixedNodes,NodeMap & allNodes,std::list<Element * > & allElements)16 discretize( NodeMap& fixedNodes, NodeMap& allNodes, std::list< Element* >& allElements )
17 {
18 	triangulate();
19 	removeBoundaryConnectors();
20 
21 	generate();
22 	exportNodes( allNodes, allElements );
23 }
24 
25 void VoronoiSegment::
generate()26 generate()
27 {
28 	const double sqrt3 = 1.7320508075688772935274463415059;
29 
30 	std::cout << "Generating" << std::endl;
31 
32 	std::list< Vertex* >::iterator vxIt;
33 	for( vxIt = allVertices.begin(); vxIt != allVertices.end(); ++vxIt )
34 	{
35 		VSVertex *vtx = static_cast<VSVertex *>(*vxIt);
36 		if( vtx->isDeleted() ) continue;
37 
38 		if( vtx->isExternal() )
39 		{
40 			for( int i = 0; i < 3; ++i )
41 			{
42 				VSVertex *vs = static_cast<VSVertex *>( vtx->vertices[i] );
43 				if( vs == (VSVertex *) 0 ) continue;
44 
45 				if( !vs->isExternal() && vs->isWaiting() )
46 				{
47 					vs->makeActive();
48 					actives.insert( vs );
49 				}
50 			}
51 		}
52 		else
53 		{
54 			double ref = bg->interpolate( vtx->vx, vtx->vy ) / sqrt3;
55 			if( vtx->rightSize( ref ) )
56 			{
57 				if( vtx->isActive() ) actives.remove( vtx );
58 				vtx->makeAccepted();
59 				for( int i = 0; i < 3; ++i )
60 				{
61 					VSVertex *vs = static_cast<VSVertex *>( vtx->vertices[i] );
62 					if( !vs->isExternal() && vs->isWaiting() )
63 					{
64 						vs->makeActive();
65 						actives.insert( vs );
66 					}
67 				}
68 			}
69 		}
70 	}
71 
72 	while( actives.size() > 0 )
73 	{
74 		VSVertex *vtx = static_cast<VSVertex *>( actives.first() );
75 
76 		if( !vtx->borderTest( ) )
77 		{
78 			double nx, ny;
79 			if( vtx->computeNewCoordinates( *bg, nx, ny ) > 0 )
80 			{
81 				MeshNode *nd = new MeshNode( nx, ny );
82 
83 				if( addSite( vtx, nd, false, true ) == true )
84 				{
85 					if( deleted.size() == 1 )
86 					{
87 						// This is a terrible hack and we get a lot of it wrong!
88 						vtx->unDelete();
89 						vtx->makeAccepted();
90 						vtx->radiate( actives );
91 						deleted.erase( deleted.begin(), deleted.end() );
92 					}
93 					else
94 					{
95 						actives.removeRelevants( deleted );
96 
97 						int len = newVertices.size(), i;
98 						for( i = 0; i < len; ++i )
99 						{
100 							VSVertex *vs = static_cast<VSVertex *>( newVertices[i] );
101 							vs->setBody( tag );
102 						}
103 
104 						for( i = 0; i < len; ++i )
105 						{
106 							VSVertex *vs = static_cast<VSVertex *>( newVertices[i] );
107 							double ref = bg->interpolate( vs->vx, vs->vy ) / sqrt3;
108 							if( vs->rightSize( ref ) )
109 							{
110 								vs->makeAccepted();
111 							}
112 							else
113 							{
114 								if( !vs->testIfActive( actives ) )
115 								{
116 									vs->makeWaiting();
117 								}
118 							}
119 						}
120 
121 						for( i = 0; i < len; ++i )
122 						{
123 							VSVertex *vs = static_cast<VSVertex *>( newVertices[i] );
124 							if( vs->isWaiting() )
125 							{
126 								vs->testIfActive( actives );
127 							}
128 							else if( vs->isAccepted() )
129 							{
130 								vs->radiate( actives );
131 							}
132 						}
133 					}
134 				}
135 				// 		else std::cout << "Bad node" << std::endl;
136 
137 				recycle();
138 			}
139 		}
140 	}
141 }
142 
143 void VoronoiSegment::
getVertices(std::vector<Vertex * > & v,const int count)144 getVertices( std::vector< Vertex * >& v, const int count )
145 {
146 	int i;
147 	for( i = 0; i < count; ++i )
148 	{
149 		if( vertexStore.empty() ) break;
150 		v.push_back( vertexStore.back() );
151 		vertexStore.pop_back();
152 	}
153 	for( ; i < count; ++i )
154 	{
155 		VSVertex *vtx = new VSVertex;
156 		v.push_back( vtx );
157 		allVertices.push_back( vtx );
158 	}
159 }
160