hetriang.cpp 19.7 KB
Newer Older
Maciej Suminski's avatar
Maciej Suminski committed
1 2 3 4 5 6
/*
 * Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
 * Applied Mathematics, Norway.
 * Copyright (C) 2013 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
7 8 9 10
 * Contact information: E-mail: tor.dokken@sintef.no
 * SINTEF ICT, Department of Applied Mathematics,
 * P.O. Box 124 Blindern,
 * 0314 Oslo, Norway.
Maciej Suminski's avatar
Maciej Suminski committed
11 12 13 14 15 16
 *
 * This file is part of TTL.
 *
 * TTL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
Maciej Suminski's avatar
Maciej Suminski committed
18
 *
19 20 21
 * TTL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Maciej Suminski's avatar
Maciej Suminski committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with TTL. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public
 * License, a covered work must retain the producer line in every data
 * file that is created or manipulated using TTL.
 *
 * Other Usage
 * You can be released from the requirements of the license by purchasing
 * a commercial license. Buying such a license is mandatory as soon as you
 * develop commercial activities involving the TTL library without
 * disclosing the source code of your own applications.
 *
 * This file may be used in accordance with the terms contained in a
39
 * written agreement between you and SINTEF ICT.
Maciej Suminski's avatar
Maciej Suminski committed
40 41 42 43 44 45 46 47
 */

#include <ttl/halfedge/hetriang.h>
#include <ttl/halfedge/hetraits.h>
#include <ttl/ttl.h>
#include <algorithm>
#include <fstream>
#include <limits>
Maciej Suminski's avatar
Maciej Suminski committed
48 49
#include <boost/foreach.hpp>
#include <boost/make_shared.hpp>
Maciej Suminski's avatar
Maciej Suminski committed
50 51 52 53

using namespace hed;

#ifdef TTL_USE_NODE_ID
54
  int NODE::id_count = 0;
Maciej Suminski's avatar
Maciej Suminski committed
55 56 57 58 59 60
#endif


//#define DEBUG_HE
#ifdef DEBUG_HE
#include <iostream>
61 62 63 64 65
static void errorAndExit( char* aMessage )
{
    cout << "\n!!! ERROR: "<< aMessage << " !!!\n" << endl;
    exit( -1 );
}
Maciej Suminski's avatar
Maciej Suminski committed
66 67 68
#endif


69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static EDGE_PTR getLeadingEdgeInTriangle( const EDGE_PTR& aEdge )
{
    EDGE_PTR edge = aEdge;

    // Code: 3EF (assumes triangle)
    if( !edge->IsLeadingEdge() )
    {
        edge = edge->GetNextEdgeInFace();

        if( !edge->IsLeadingEdge() )
            edge = edge->GetNextEdgeInFace();
    }

    if( !edge->IsLeadingEdge() )
    {
        return EDGE_PTR();
    }

    return edge;
Maciej Suminski's avatar
Maciej Suminski committed
88 89 90
}


91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void getLimits( NODES_CONTAINER::iterator aFirst, NODES_CONTAINER::iterator aLast,
                       int& aXmin, int& aYmin, int& aXmax, int& aYmax)
{
    aXmin = aYmin = std::numeric_limits<int>::min();
    aXmax = aYmax = std::numeric_limits<int>::max();

    NODES_CONTAINER::iterator it;

    for( it = aFirst; it != aLast; ++it )
    {
        aXmin = std::min( aXmin, ( *it )->GetX() );
        aYmin = std::min( aYmin, ( *it )->GetY() );
        aXmax = std::max( aXmax, ( *it )->GetX() );
        aYmax = std::max( aYmax, ( *it )->GetY() );
    }
Maciej Suminski's avatar
Maciej Suminski committed
106 107 108
}


109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
EDGE_PTR TRIANGULATION::InitTwoEnclosingTriangles( NODES_CONTAINER::iterator aFirst,
                                                  NODES_CONTAINER::iterator aLast)
{
    int xmin, ymin, xmax, ymax;
    getLimits( aFirst, aLast, xmin, ymin, xmax, ymax );

    // Add 10% of range:
    double fac = 10.0;
    double dx = ( xmax - xmin ) / fac;
    double dy = ( ymax - ymin ) / fac;

    NODE_PTR n1 = boost::make_shared<NODE>( xmin - dx, ymin - dy );
    NODE_PTR n2 = boost::make_shared<NODE>( xmax + dx, ymin - dy );
    NODE_PTR n3 = boost::make_shared<NODE>( xmax + dx, ymax + dy );
    NODE_PTR n4 = boost::make_shared<NODE>( xmin - dx, ymax + dy );

    // diagonal
    EDGE_PTR e1d = boost::make_shared<EDGE>();
    EDGE_PTR e2d = boost::make_shared<EDGE>();

    // lower triangle
    EDGE_PTR e11 = boost::make_shared<EDGE>();
    EDGE_PTR e12 = boost::make_shared<EDGE>();

    // upper triangle
    EDGE_PTR e21 = boost::make_shared<EDGE>();
    EDGE_PTR e22 = boost::make_shared<EDGE>();

    // lower triangle
    e1d->SetSourceNode( n3 );
    e1d->SetNextEdgeInFace( e11 );
    e1d->SetTwinEdge( e2d );
    addLeadingEdge( e1d );

    e11->SetSourceNode( n1 );
    e11->SetNextEdgeInFace( e12 );

    e12->SetSourceNode( n2 );
    e12->SetNextEdgeInFace( e1d );

    // upper triangle
    e2d->SetSourceNode( n1 );
    e2d->SetNextEdgeInFace( e21 );
    e2d->SetTwinEdge( e1d );
    addLeadingEdge( e2d );

    e21->SetSourceNode( n3 );
    e21->SetNextEdgeInFace( e22 );

    e22->SetSourceNode( n4 );
    e22->SetNextEdgeInFace( e2d );

    return e11;
Maciej Suminski's avatar
Maciej Suminski committed
162 163 164
}


165 166 167
TRIANGULATION::TRIANGULATION()
{
    m_helper = new ttl::TRIANGULATION_HELPER( *this );
168 169 170
}


171 172 173 174
TRIANGULATION::TRIANGULATION( const TRIANGULATION& aTriangulation )
{
    // Triangulation: Copy constructor not present
    assert( false );
175 176 177
}


178 179 180 181
TRIANGULATION::~TRIANGULATION()
{
    cleanAll();
    delete m_helper;
182 183 184
}


185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
void TRIANGULATION::CreateDelaunay( NODES_CONTAINER::iterator aFirst,
                                    NODES_CONTAINER::iterator aLast )
{
    cleanAll();

    EDGE_PTR bedge = InitTwoEnclosingTriangles( aFirst, aLast );
    DART dc( bedge );

    DART d_iter = dc;

    NODES_CONTAINER::iterator it;
    for( it = aFirst; it != aLast; ++it )
    {
        m_helper->InsertNode<TTLtraits>( d_iter, *it );
    }

    // In general (e.g. for the triangle based data structure), the initial dart
    // may have been changed.
    // It is the users responsibility to get a valid boundary dart here.
    // The half-edge data structure preserves the initial dart.
    // (A dart at the boundary can also be found by trying to locate a
    // triangle "outside" the triangulation.)

    // Assumes rectangular domain
    m_helper->RemoveRectangularBoundary<TTLtraits>( dc );
Maciej Suminski's avatar
Maciej Suminski committed
210 211 212
}


213 214 215
void TRIANGULATION::RemoveTriangle( EDGE_PTR& aEdge )
{
  EDGE_PTR e1 = getLeadingEdgeInTriangle( aEdge );
Maciej Suminski's avatar
Maciej Suminski committed
216 217

#ifdef DEBUG_HE
218 219
    if( !e1 )
    errorAndExit( "Triangulation::removeTriangle: could not find leading aEdge" );
Maciej Suminski's avatar
Maciej Suminski committed
220
#endif
221 222 223 224 225 226 227 228 229 230

    removeLeadingEdgeFromList( e1 );
    // cout << "No leading edges = " << leadingEdges_.size() << endl;
    // Remove the triangle
    EDGE_PTR e2( e1->GetNextEdgeInFace() );
    EDGE_PTR e3( e2->GetNextEdgeInFace() );

    e1->Clear();
    e2->Clear();
    e3->Clear();
Maciej Suminski's avatar
Maciej Suminski committed
231 232 233
}


234 235 236 237 238
void TRIANGULATION::ReverseSplitTriangle( EDGE_PTR& aEdge )
{
    // Reverse operation of splitTriangle
    EDGE_PTR e1( aEdge->GetNextEdgeInFace() );
    EDGE_PTR le( getLeadingEdgeInTriangle( e1 ) );
Maciej Suminski's avatar
Maciej Suminski committed
239
#ifdef DEBUG_HE
240
    if (!le)
Maciej Suminski's avatar
Maciej Suminski committed
241 242
    errorAndExit("Triangulation::removeTriangle: could not find leading edge");
#endif
243 244 245 246
    removeLeadingEdgeFromList( le );

    EDGE_PTR e2( e1->GetNextEdgeInFace()->GetTwinEdge()->GetNextEdgeInFace() );
    le = getLeadingEdgeInTriangle( e2 );
Maciej Suminski's avatar
Maciej Suminski committed
247
#ifdef DEBUG_HE
248
    if (!le)
Maciej Suminski's avatar
Maciej Suminski committed
249 250
    errorAndExit("Triangulation::removeTriangle: could not find leading edge");
#endif
251 252 253 254
    removeLeadingEdgeFromList( le );

    EDGE_PTR e3( aEdge->GetTwinEdge()->GetNextEdgeInFace()->GetNextEdgeInFace() );
    le = getLeadingEdgeInTriangle( e3 );
Maciej Suminski's avatar
Maciej Suminski committed
255
#ifdef DEBUG_HE
256
    if (!le)
Maciej Suminski's avatar
Maciej Suminski committed
257 258
    errorAndExit("Triangulation::removeTriangle: could not find leading edge");
#endif
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    removeLeadingEdgeFromList( le );

    // The three triangles at the node have now been removed
    // from the triangulation, but the arcs have not been deleted.
    // Next delete the 6 half edges radiating from the node
    // The node is maintained by handle and need not be deleted explicitly
    EDGE_PTR estar = aEdge;
    EDGE_PTR enext = estar->GetTwinEdge()->GetNextEdgeInFace();
    estar->GetTwinEdge()->Clear();
    estar->Clear();

    estar = enext;
    enext = estar->GetTwinEdge()->GetNextEdgeInFace();
    estar->GetTwinEdge()->Clear();
    estar->Clear();

    enext->GetTwinEdge()->Clear();
    enext->Clear();

    // Create the new triangle
    e1->SetNextEdgeInFace( e2 );
    e2->SetNextEdgeInFace( e3 );
    e3->SetNextEdgeInFace( e1 );
    addLeadingEdge( e1 );
Maciej Suminski's avatar
Maciej Suminski committed
283 284 285
}


286 287
DART TRIANGULATION::CreateDart()
{
Maciej Suminski's avatar
Maciej Suminski committed
288
  // Return an arbitrary CCW dart
289
    return DART( *m_leadingEdges.begin() );
Maciej Suminski's avatar
Maciej Suminski committed
290 291 292
}


293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
bool TRIANGULATION::removeLeadingEdgeFromList( EDGE_PTR& aLeadingEdge )
{
    // Remove the edge from the list of leading edges,
    // but don't delete it.
    // Also set flag for leading edge to false.
    // Must search from start of list. Since edges are added to the
    // start of the list during triangulation, this operation will
    // normally be fast (when used in the triangulation algorithm)
    std::list<EDGE_PTR>::iterator it;
    for( it = m_leadingEdges.begin(); it != m_leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;

        if( edge == aLeadingEdge )
        {
            edge->SetAsLeadingEdge( false );
            it = m_leadingEdges.erase( it );

            return true;
        }
Maciej Suminski's avatar
Maciej Suminski committed
313
    }
314 315

    return false;
Maciej Suminski's avatar
Maciej Suminski committed
316 317 318
}


319 320 321 322
void TRIANGULATION::cleanAll()
{
    BOOST_FOREACH( EDGE_PTR& edge, m_leadingEdges )
        edge->SetNextEdgeInFace( EDGE_PTR() );
Maciej Suminski's avatar
Maciej Suminski committed
323 324 325
}


326 327 328
void TRIANGULATION::swapEdge( DART& aDart )
{
    SwapEdge( aDart.GetEdge() );
329 330 331
}


332 333 334 335
void TRIANGULATION::splitTriangle( DART& aDart, const NODE_PTR& aPoint )
{
    EDGE_PTR edge = SplitTriangle( aDart.GetEdge(), aPoint );
    aDart.Init( edge );
336 337 338
}


339 340 341
void TRIANGULATION::reverseSplitTriangle( DART& aDart )
{
    ReverseSplitTriangle( aDart.GetEdge() );
342 343 344
}


345 346 347
void TRIANGULATION::removeBoundaryTriangle( DART& aDart )
{
    RemoveTriangle( aDart.GetEdge() );
348 349 350
}


Maciej Suminski's avatar
Maciej Suminski committed
351
#ifdef TTL_USE_NODE_FLAG
352 353 354 355 356 357 358 359 360 361 362 363
void TRIANGULATION::FlagNodes( bool aFlag ) const
{
    std::list<EDGE_PTR>::const_iterator it;
    for( it = m_leadingEdges.begin(); it != m_leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;

        for( int i = 0; i < 3; ++i )
        {
            edge->GetSourceNode()->SetFlag( aFlag );
            edge = edge->GetNextEdgeInFace();
        }
Maciej Suminski's avatar
Maciej Suminski committed
364 365 366 367
    }
}


368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
std::list<NODE_PTR>* TRIANGULATION::GetNodes() const
{
    FlagNodes( false );
    std::list<NODE_PTR>* nodeList = new std::list<NODE_PTR>;
    std::list<EDGE_PTR>::const_iterator it;

    for( it = m_leadingEdges.begin(); it != m_leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;

        for( int i = 0; i < 3; ++i )
        {
            const NODE_PTR& node = edge->GetSourceNode();

            if( node->GetFlag() == false )
            {
                nodeList->push_back( node );
                node->SetFlag( true );
            }
            edge = edge->GetNextEdgeInFace();
        }
Maciej Suminski's avatar
Maciej Suminski committed
389
    }
390
    return nodeList;
Maciej Suminski's avatar
Maciej Suminski committed
391 392 393 394
}
#endif


395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
std::list<EDGE_PTR>* TRIANGULATION::GetEdges( bool aSkipBoundaryEdges ) const
{
    // collect all arcs (one half edge for each arc)
    // (boundary edges are also collected).
    std::list<EDGE_PTR>::const_iterator it;
    std::list<EDGE_PTR>* elist = new std::list<EDGE_PTR>;

    for( it = m_leadingEdges.begin(); it != m_leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;
        for( int i = 0; i < 3; ++i )
        {
            EDGE_PTR twinedge = edge->GetTwinEdge();
            // only one of the half-edges

            if( ( !twinedge && !aSkipBoundaryEdges )
                    || ( twinedge && ( (size_t) edge.get() > (size_t) twinedge.get() ) ) )
                elist->push_front( edge );

            edge = edge->GetNextEdgeInFace();
        }
Maciej Suminski's avatar
Maciej Suminski committed
416
    }
417 418

    return elist;
Maciej Suminski's avatar
Maciej Suminski committed
419 420 421
}


422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
EDGE_PTR TRIANGULATION::SplitTriangle( EDGE_PTR& aEdge, const NODE_PTR& aPoint )
{
    // Add a node by just splitting a triangle into three triangles
    // Assumes the half aEdge is located in the triangle
    // Returns a half aEdge with source node as the new node

    // e#_n are new edges
    // e# are existing edges
    // e#_n and e##_n are new twin edges
    // e##_n are edges incident to the new node

    // Add the node to the structure
    //NODE_PTR new_node(new Node(x,y,z));

    NODE_PTR n1( aEdge->GetSourceNode() );
    EDGE_PTR e1( aEdge );

    EDGE_PTR e2( aEdge->GetNextEdgeInFace() );
    NODE_PTR n2( e2->GetSourceNode() );

    EDGE_PTR e3( e2->GetNextEdgeInFace() );
    NODE_PTR n3( e3->GetSourceNode() );

    EDGE_PTR e1_n = boost::make_shared<EDGE>();
    EDGE_PTR e11_n = boost::make_shared<EDGE>();
    EDGE_PTR e2_n = boost::make_shared<EDGE>();
    EDGE_PTR e22_n = boost::make_shared<EDGE>();
    EDGE_PTR e3_n = boost::make_shared<EDGE>();
    EDGE_PTR e33_n = boost::make_shared<EDGE>();

    e1_n->SetSourceNode( n1 );
    e11_n->SetSourceNode( aPoint );
    e2_n->SetSourceNode( n2 );
    e22_n->SetSourceNode( aPoint );
    e3_n->SetSourceNode( n3 );
    e33_n->SetSourceNode( aPoint );

    e1_n->SetTwinEdge( e11_n );
    e11_n->SetTwinEdge( e1_n );
    e2_n->SetTwinEdge( e22_n );
    e22_n->SetTwinEdge( e2_n );
    e3_n->SetTwinEdge( e33_n );
    e33_n->SetTwinEdge( e3_n );

    e1_n->SetNextEdgeInFace( e33_n );
    e2_n->SetNextEdgeInFace( e11_n );
    e3_n->SetNextEdgeInFace( e22_n );

    e11_n->SetNextEdgeInFace( e1 );
    e22_n->SetNextEdgeInFace( e2 );
    e33_n->SetNextEdgeInFace( e3 );

    // and update old's next aEdge
    e1->SetNextEdgeInFace( e2_n );
    e2->SetNextEdgeInFace( e3_n );
    e3->SetNextEdgeInFace( e1_n );

    // add the three new leading edges,
    // Must remove the old leading aEdge from the list.
    // Use the field telling if an aEdge is a leading aEdge
    // NOTE: Must search in the list!!!

    if( e1->IsLeadingEdge() )
        removeLeadingEdgeFromList( e1 );
    else if( e2->IsLeadingEdge() )
        removeLeadingEdgeFromList( e2 );
    else if( e3->IsLeadingEdge() )
        removeLeadingEdgeFromList( e3 );
    else
        assert( false );        // one of the edges should be leading

    addLeadingEdge( e1_n );
    addLeadingEdge( e2_n );
    addLeadingEdge( e3_n );

    // Return a half aEdge incident to the new node (with the new node as source node)

    return e11_n;
Maciej Suminski's avatar
Maciej Suminski committed
500 501 502
}


503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
void TRIANGULATION::SwapEdge( EDGE_PTR& aDiagonal )
{
    // Note that diagonal is both input and output and it is always
    // kept in counterclockwise direction (this is not required by all
    // functions in TriangulationHelper now)

    // Swap by rotating counterclockwise
    // Use the same objects - no deletion or new objects
    EDGE_PTR eL( aDiagonal );
    EDGE_PTR eR( eL->GetTwinEdge() );
    EDGE_PTR eL_1( eL->GetNextEdgeInFace() );
    EDGE_PTR eL_2( eL_1->GetNextEdgeInFace() );
    EDGE_PTR eR_1( eR->GetNextEdgeInFace() );
    EDGE_PTR eR_2( eR_1->GetNextEdgeInFace() );

    // avoid node to be dereferenced to zero and deleted
    NODE_PTR nR( eR_2->GetSourceNode() );
    NODE_PTR nL( eL_2->GetSourceNode() );

    eL->SetSourceNode( nR );
    eR->SetSourceNode( nL );

    // and now 6 1-sewings
    eL->SetNextEdgeInFace( eL_2 );
    eL_2->SetNextEdgeInFace( eR_1 );
    eR_1->SetNextEdgeInFace( eL );

    eR->SetNextEdgeInFace( eR_2 );
    eR_2->SetNextEdgeInFace( eL_1 );
    eL_1->SetNextEdgeInFace( eR );

    if( eL->IsLeadingEdge() )
        removeLeadingEdgeFromList( eL );
    else if( eL_1->IsLeadingEdge() )
        removeLeadingEdgeFromList( eL_1 );
    else if( eL_2->IsLeadingEdge() )
        removeLeadingEdgeFromList( eL_2 );

    if( eR->IsLeadingEdge() )
        removeLeadingEdgeFromList( eR );
    else if( eR_1->IsLeadingEdge() )
        removeLeadingEdgeFromList( eR_1 );
    else if( eR_2->IsLeadingEdge() )
        removeLeadingEdgeFromList( eR_2 );

    addLeadingEdge( eL );
    addLeadingEdge( eR );
Maciej Suminski's avatar
Maciej Suminski committed
550 551 552
}


553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
bool TRIANGULATION::CheckDelaunay() const
{
    // ???? outputs !!!!
    // ofstream os("qweND.dat");
    const std::list<EDGE_PTR>& leadingEdges = GetLeadingEdges();

    std::list<EDGE_PTR>::const_iterator it;
    bool ok = true;
    int noNotDelaunay = 0;

    for( it = leadingEdges.begin(); it != leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;

        for( int i = 0; i < 3; ++i )
        {
            EDGE_PTR twinedge = edge->GetTwinEdge();

            // only one of the half-edges
            if( !twinedge || (size_t) edge.get() > (size_t) twinedge.get() )
            {
                DART dart( edge );
                if( m_helper->SwapTestDelaunay<TTLtraits>( dart ) )
                {
                    noNotDelaunay++;

                    //printEdge(dart,os); os << "\n";
                    ok = false;
                    //cout << "............. not Delaunay .... " << endl;
                }
            }

            edge = edge->GetNextEdgeInFace();
Maciej Suminski's avatar
Maciej Suminski committed
586 587
        }
    }
588

Maciej Suminski's avatar
Maciej Suminski committed
589
#ifdef DEBUG_HE
590
    cout << "!!! Triangulation is NOT Delaunay: " << noNotDelaunay << " edges\n" << endl;
Maciej Suminski's avatar
Maciej Suminski committed
591
#endif
592 593

    return ok;
Maciej Suminski's avatar
Maciej Suminski committed
594 595 596
}


597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
void TRIANGULATION::OptimizeDelaunay()
{
    // This function is also present in ttl where it is implemented
    // generically.
    // The implementation below is tailored for the half-edge data structure,
    // and is thus more efficient

    // Collect all interior edges (one half edge for each arc)
    bool skip_boundary_edges = true;
    std::list<EDGE_PTR>* elist = GetEdges( skip_boundary_edges );

    // Assumes that elist has only one half-edge for each arc.
    bool cycling_check = true;
    bool optimal = false;
    std::list<EDGE_PTR>::const_iterator it;

    while( !optimal )
    {
        optimal = true;

        for( it = elist->begin(); it != elist->end(); ++it )
        {
            EDGE_PTR edge = *it;

            DART dart( edge );
            // Constrained edges should not be swapped
            if( m_helper->SwapTestDelaunay<TTLtraits>( dart, cycling_check ) )
            {
                optimal = false;
                SwapEdge( edge );
            }
        }
Maciej Suminski's avatar
Maciej Suminski committed
629
    }
630 631

    delete elist;
Maciej Suminski's avatar
Maciej Suminski committed
632 633 634
}


635 636 637 638
EDGE_PTR TRIANGULATION::GetInteriorNode() const
{
    const std::list<EDGE_PTR>& leadingEdges = GetLeadingEdges();
    std::list<EDGE_PTR>::const_iterator it;
639

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    for( it = leadingEdges.begin(); it != leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;

        // multiple checks, but only until found
        for( int i = 0; i < 3; ++i )
        {
            if( edge->GetTwinEdge() )
            {
                if( !m_helper->IsBoundaryNode( DART( edge ) ) )
                    return edge;
            }

            edge = edge->GetNextEdgeInFace();
        }
Maciej Suminski's avatar
Maciej Suminski committed
655
    }
656 657

    return EDGE_PTR(); // no boundary nodes
Maciej Suminski's avatar
Maciej Suminski committed
658 659 660
}


661 662 663
EDGE_PTR TRIANGULATION::GetBoundaryEdgeInTriangle( const EDGE_PTR& aEdge ) const
{
    EDGE_PTR edge = aEdge;
Maciej Suminski's avatar
Maciej Suminski committed
664

665 666
    if( m_helper->IsBoundaryEdge( DART( edge ) ) )
        return edge;
Maciej Suminski's avatar
Maciej Suminski committed
667

668 669 670 671 672 673 674 675 676
    edge = edge->GetNextEdgeInFace();
    if( m_helper->IsBoundaryEdge( DART( edge ) ) )
        return edge;

    edge = edge->GetNextEdgeInFace();
    if( m_helper->IsBoundaryEdge( DART( edge ) ) )
        return edge;

    return EDGE_PTR();
Maciej Suminski's avatar
Maciej Suminski committed
677 678 679
}


680 681 682 683 684 685 686
EDGE_PTR TRIANGULATION::GetBoundaryEdge() const
{
    // Get an arbitrary (CCW) boundary edge
    // If the triangulation is closed, NULL is returned
    const std::list<EDGE_PTR>& leadingEdges = GetLeadingEdges();
    std::list<EDGE_PTR>::const_iterator it;
    EDGE_PTR edge;
Maciej Suminski's avatar
Maciej Suminski committed
687

688 689 690
    for( it = leadingEdges.begin(); it != leadingEdges.end(); ++it )
    {
        edge = GetBoundaryEdgeInTriangle( *it );
Maciej Suminski's avatar
Maciej Suminski committed
691

692 693 694 695
        if( edge )
            return edge;
    }
    return EDGE_PTR();
Maciej Suminski's avatar
Maciej Suminski committed
696 697 698
}


699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
void TRIANGULATION::PrintEdges( std::ofstream& aOutput ) const
{
    // Print source node and target node for each edge face by face,
    // but only one of the half-edges.
    const std::list<EDGE_PTR>& leadingEdges = GetLeadingEdges();
    std::list<EDGE_PTR>::const_iterator it;

    for( it = leadingEdges.begin(); it != leadingEdges.end(); ++it )
    {
        EDGE_PTR edge = *it;

        for( int i = 0; i < 3; ++i )
        {
            EDGE_PTR twinedge = edge->GetTwinEdge();

            // Print only one edge (the highest value of the pointer)
            if( !twinedge || (size_t) edge.get() > (size_t) twinedge.get() )
            {
                // Print source node and target node
                NODE_PTR node = edge->GetSourceNode();
                aOutput << node->GetX() << " " << node->GetY() << std::endl;
                node = edge->GetTargetNode();
                aOutput << node->GetX() << " " << node->GetY() << std::endl;
                aOutput << '\n'; // blank line
            }

            edge = edge->GetNextEdgeInFace();
        }
Maciej Suminski's avatar
Maciej Suminski committed
727 728
    }
}