Path: blob/devel/meshgen2d/src/SSSFVoronoiSegment.cpp
3196 views
#include "SSSFVoronoiSegment.h"1#include "Border.h"2#include "SSSFVertex.h"3#include "MGError.h"45#include <iostream>67#include <math.h>8#include <fstream>9#include <stdio.h>10#ifdef WIN3211#include <direct.h>12#else13#include <unistd.h>14#endif1516#include "coreGeometry.h"1718void SSSFVoronoiSegment::19discretize( NodeMap& fixedNodes, NodeMap& allNodes, std::list< Element* >& allElements )20{21triangulate();22removeBoundaryConnectors();2324makeSeed(fixedNodes);2526generate();27exportNodes( allNodes, allElements );28}2930void SSSFVoronoiSegment::31makeSeed(NodeMap& fixedNodes)32{33SSSFVertex *vtx;34double nx, ny;3536if( explicitSeed )37{38int j;39for (j = 0; j < 3; j++)40{41NodeMapIt it = fixedNodes.find(seedTags[j]);42if (it == fixedNodes.end())43{44std::cerr << "Seed node " << seedTags[j] << "does not exist!" << std::endl;45exit(1);46}47seed[j] = static_cast<MeshNode *>(it->second);48}4950for( j = 0; j < 3; ++j )51{52Vertex *v = root->firstAcceptableFound( seed[j]->x, seed[j]->y);5354if( addSite( v, seed[j], false ) == true )55{56int len = newVertices.size();57for( int i = 0; i < len; ++i )58{59vtx = static_cast<SSSFVertex *>( newVertices[i] );60vtx->setBody( tag );61}62recycle();63}64else65{66blm_error("Could not insert the seed node", seedTags[j]);67}68}69}70else71{72Node *a, *b;7374baseEdge->midNodes( a, b, baseDirection );7576seed[0] = static_cast<MeshNode *>(a);77seed[1] = static_cast<MeshNode *>(b);7879vtx = static_cast<SSSFVertex *>( root->vertexWith( seed[0], seed[1] ) );80vtx->computeNewCoordinates( *bg, nx, ny );81seed[2] = new MeshNode( nx, ny );8283if( addSite( vtx, seed[2], false ) == true )84{85int len = newVertices.size();86for( int i = 0; i < len; ++i )87{88vtx = static_cast<SSSFVertex *>( newVertices[i] );89vtx->setBody( tag );90}91recycle();92}93else94{95std::cerr << "Could not insert the seed!" << std::endl;96exit(1);97}98}99100vtx = static_cast<SSSFVertex *>( root->vertexWith( seed[0], seed[1] ) );101if (vtx == NULL)102{103std::cerr << "Could not insert the seed!" << std::endl;104exit(1);105}106107vtx->makeAccepted();108vtx->radiate( actives, crystals );109}110111void SSSFVoronoiSegment::112generate()113{114SSSFVertex *vs;115int i;116double nx, ny;117double ref;118119const double sqrt3 = 1.7320508075688772935274463415059;120121std::cout << "Generating" << std::endl;122123nx = ny = 0.0;124125int steps = 0;126while( actives.size() > 0 || crystals.size() > 0)127{128bool active = true;129SSSFVertex *vtx;130131if( crystals.size() > 0)132{133active = false;134vtx = static_cast<SSSFVertex *>( crystals.first() );135}136else137{138vtx = static_cast<SSSFVertex *>( actives.first() );139}140if( !vtx->borderTest( ) )141{142if( vtx->computeNewCoordinates( *bg, nx, ny ) > 0 )143{144MeshNode *nd = new MeshNode( nx, ny );145146if( addSite( vtx, nd, false, false ) == true )147{148if( deleted.size() == 1 )149{150// This is a terrible hack and we get a lot of it wrong!151vtx->unDelete();152vtx->makeAccepted();153vtx->radiate( actives, crystals );154deleted.erase( deleted.begin(), deleted.end() );155}156else157{158std::list< Vertex* >::iterator vxIt;159for( vxIt = deleted.begin(); vxIt != deleted.end(); ++vxIt )160{161vs = static_cast<SSSFVertex *>( *vxIt );162if( vs->isCrystal() && vs->isAtHeap() )163crystals.remove( vs );164else if( vs->isActive() && vs->isAtHeap() )165actives.remove( vs );166}167168int len = newVertices.size();169for( i = 0; i < len; ++i )170{171vs = static_cast<SSSFVertex *>( newVertices[i] );172vs->setBody( tag );173}174175for( i = 0; i < len; ++i )176{177vs = static_cast<SSSFVertex *>( newVertices[i] );178ref = bg->interpolate( vs->vx, vs->vy );179ref /= sqrt3;180181if( vs->rightSize( ref ) )182{183vs->makeAccepted();184}185else186{187vs->makeWaiting();188}189}190191for( i = 0; i < len; ++i )192{193vs = static_cast<SSSFVertex *>( newVertices[i] );194if( vs->isWaiting() )195{196vs->testIfActive( actives, crystals );197}198else if( vs->isAccepted() )199{200vs->radiate( actives, crystals );201}202}203}204}205recycle();206}207}208++steps;209}210}211212void SSSFVoronoiSegment::213getVertices( std::vector< Vertex * >& v, const int count )214{215int i;216for( i = 0; i < count; ++i )217{218if( vertexStore.empty() ) break;219v.push_back( vertexStore.back() );220vertexStore.pop_back();221}222223for( ; i < count; ++i )224{225SSSFVertex *vtx = new SSSFVertex;226v.push_back( vtx );227allVertices.push_back( vtx );228}229}230231232