Path: blob/devel/meshgen2d/src/SSMFVoronoiSegment.cpp
3196 views
#include "SSMFVoronoiSegment.h"1#include "Border.h"2#include "SSMFVertex.h"3#include "MGError.h"45#include <iostream>6#include <math.h>7#include <fstream>8#include <stdio.h>9#ifdef WIN3210#include <direct.h>11#else12#include <unistd.h>13#endif1415void SSMFVoronoiSegment::16discretize( NodeMap& fixedNodes, NodeMap& allNodes, std::list< Element* >& allElements )17{18triangulate();19removeBoundaryConnectors();2021makeSeed(fixedNodes);2223generate();24exportNodes( allNodes, allElements );25}2627void SSMFVoronoiSegment::28makeSeed(NodeMap& fixedNodes)29{30SSMFVertex *vtx;31double nx, ny;3233if( explicitSeed )34{35int j;36for (j = 0; j < 3; j++)37{38NodeMapIt it = fixedNodes.find(seedTags[j]);39if (it == fixedNodes.end())40{41std::cerr << "Seed node " << seedTags[j] << "does not exist!" << std::endl;42exit(1);43}44seed[j] = static_cast<MeshNode *>(it->second);45}4647for( j = 0; j < 3; ++j )48{49Vertex *v = root->firstAcceptableFound( seed[j]->x, seed[j]->y);5051if( addSite( v, seed[j], false ) == true )52{53int len = newVertices.size();54for( int i = 0; i < len; ++i )55{56vtx = static_cast<SSMFVertex *>( newVertices[i] );57vtx->setBody( tag );58}59recycle();60}61else62{63blm_error("Could not insert the seed node", seedTags[j]);64}65}66}67else68{69Node *a, *b;7071baseEdge->midNodes( a, b, baseDirection );7273seed[0] = static_cast<MeshNode *>(a);74seed[1] = static_cast<MeshNode *>(b);7576vtx = static_cast<SSMFVertex *>( root->vertexWith( seed[0], seed[1] ) );77vtx->computeNewCoordinates( *bg, nx, ny );78seed[2] = new MeshNode( nx, ny );7980if( addSite( vtx, seed[2], false ) == true )81{82int len = newVertices.size();83for( int i = 0; i < len; ++i )84{85vtx = static_cast<SSMFVertex *>( newVertices[i] );86vtx->setBody( tag );87}88recycle();89}90else91{92blm_error("Could not insert the seed!");93}94}9596vtx = static_cast<SSMFVertex *>( root->vertexWith( seed[0], seed[1] ) );97if (vtx == NULL)98{99std::cerr << "Could not insert the seed!" << std::endl;100exit(1);101}102103vtx->makeAccepted();104vtx->radiate( actives );105}106107void SSMFVoronoiSegment::108generate()109{110const double sqrt3 = 1.7320508075688772935274463415059;111112std::cout << "Generating" << std::endl;113114size_t steps = 0;115while( actives.size() > 0 )116{117SSMFVertex *vtx = static_cast<SSMFVertex *>( actives.first() );118119if( !vtx->borderTest( ) )120{121double nx, ny;122if( vtx->computeNewCoordinates( *bg, nx, ny ) > 0)123{124MeshNode *nd = new MeshNode( nx, ny );125126if( addSite( vtx, nd, false, false ) == true )127{128if( deleted.size() == 1 )129{130// This is a terrible hack and we get a lot of it wrong!131vtx->unDelete();132vtx->makeAccepted();133vtx->radiate( actives );134deleted.erase( deleted.begin(), deleted.end() );135}136else137{138actives.removeRelevants( deleted );139140int len = newVertices.size(), i;141for( i = 0; i < len; ++i )142{143SSMFVertex *vs = static_cast<SSMFVertex *>( newVertices[i] );144vs->setBody( tag );145}146147for( i = 0; i < len; ++i )148{149SSMFVertex *vs = static_cast<SSMFVertex *>( newVertices[i] );150double ref = bg->interpolate( vs->vx, vs->vy ) / sqrt3;151if( vs->rightSize( ref ) )152{153vs->makeAccepted();154}155else156{157if( !vs->testIfActive( actives ) )158{159vs->makeWaiting();160}161}162}163164for( i = 0; i < len; ++i )165{166SSMFVertex *vs = static_cast<SSMFVertex *>( newVertices[i] );167if( vs->isWaiting() )168{169vs->testIfActive( actives );170}171else if( vs->isAccepted() )172{173vs->radiate( actives );174}175}176}177}178179recycle();180}181}182++steps;183}184}185186void SSMFVoronoiSegment::187getVertices( std::vector< Vertex * >& v, const int count )188{189int i;190for( i = 0; i < count; ++i )191{192if( vertexStore.empty() ) break;193v.push_back( vertexStore.back() );194vertexStore.pop_back();195}196197for( ; i < count; ++i )198{199SSMFVertex *vtx = new SSMFVertex;200v.push_back( vtx );201allVertices.push_back( vtx );202}203}204205206207208209210211