graph.cpp 75.1 KB
Newer Older
1
/*! \file src/graph.cpp
2
    \brief Used to Intercect and other process functions
charras's avatar
charras committed
3 4
    \author Klaas Holwerda 
 
5
    Copyright: 2001-2004 (C) Klaas Holwerda
charras's avatar
charras committed
6 7 8
 
    Licence: see kboollicense.txt 
 
9
    RCS-ID: $Id: graph.cpp,v 1.7 2009/09/14 16:50:12 titato Exp $
10 11 12 13
*/

// Grpah is the structure used to store polygons

14 15 16 17
#include "kbool/booleng.h"
#include "kbool/graph.h"
#include "kbool/graphlst.h"
#include "kbool/node.h"
18 19

// Prototype of function
charras's avatar
charras committed
20 21 22 23 24
int linkXYsorter( kbLink *, kbLink * );
int linkYXsorter( kbLink *, kbLink * );
int linkLsorter( kbLink *, kbLink * );
int linkYXtopsorter( kbLink *a, kbLink *b );
int linkGraphNumsorter( kbLink *_l1, kbLink* _l2 );
25 26

// constructor, initialize with one link
charras's avatar
charras committed
27 28
// usage: kbGraph *a_graph = new kbGraph(a_link);
kbGraph::kbGraph( kbLink *a_link, Bool_Engine* GC )
29
{
30 31
    _GC = GC;
    _linklist = new DL_List<void*>();
32

33 34
    _linklist->insbegin( a_link );
    _bin = false;
35 36 37 38 39

}


// constructor, initialize graph with no contents
charras's avatar
charras committed
40 41
// usage: kbGraph *a_graph = new kbGraph();
kbGraph::kbGraph( Bool_Engine* GC )
42
{
43 44 45
    _GC = GC;
    _linklist = new DL_List<void*>();
    _bin = false;
46 47
}

charras's avatar
charras committed
48
kbGraph::kbGraph( kbGraph* other )
49
{
50 51 52 53 54
    _GC = other->_GC;
    _linklist = new DL_List<void*>();
    _bin = false;

    int _nr_of_points = other->_linklist->count();
charras's avatar
charras committed
55
    kbLink* _current = other->GetFirstLink();
56

charras's avatar
charras committed
57 58 59
    kbNode* _last = _current->GetBeginNode();
    kbNode* node = new kbNode( _current->GetBeginNode()->GetX(), _current->GetBeginNode()->GetY(), _GC );
    kbNode* nodefirst = node;
60 61 62 63 64 65 66
    for ( int i = 0; i < _nr_of_points; i++ )
    {
        // get the other node on the link
        _last = _current->GetOther( _last );
        // get the other link on the _last node
        _current = _current->Forth( _last );

charras's avatar
charras committed
67 68
        kbNode* node2 = new kbNode( _current->GetBeginNode()->GetX(), _current->GetBeginNode()->GetY(), _GC );
        _linklist->insend( new kbLink( node,  node2, _GC ) );
69 70
        node = node2;
    }
charras's avatar
charras committed
71
    _linklist->insend( new kbLink( node,  nodefirst, _GC ) );
72 73 74 75
}

// destructor
// deletes all object of the linklist
charras's avatar
charras committed
76
kbGraph::~kbGraph()
77
{
78
    {
charras's avatar
charras committed
79
        TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
80

81 82 83
        //first empty the graph
        _LI.delete_all();
    }
84

85
    delete _linklist;
86 87
}

charras's avatar
charras committed
88
kbLink* kbGraph::GetFirstLink()
89
{
charras's avatar
charras committed
90
    return ( kbLink* ) _linklist->headitem();
jean-pierre charras's avatar
jean-pierre charras committed
91
}
92 93


charras's avatar
charras committed
94
void kbGraph::Prepare( int intersectionruns )
95
{
96 97 98 99 100 101 102 103 104
    _GC->SetState( "Intersection" );

    bool found = true;
    int run = 0;
    while( run < intersectionruns && found )
    {
        found = CalculateCrossings( _GC->GetInternalMarge() );
        run++;
    }
105 106

//WHY
107
// Round(_GC->Get_Grid());
108

109
    {
charras's avatar
charras committed
110 111
        TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
        _LI.foreach_mf( &kbLink::UnMark );// Reset Bin and Mark flag
112 113
    }
    _GC->SetState( "Set group A/B Flags" );
114

115
    bool dummy = false;
116

117 118
    if ( _GC->GetWindingRule() )
        ScanGraph2( INOUT, dummy );
119

120
    ScanGraph2( GENLR, dummy );
121

122
// writegraph();
123

124 125
    _GC->SetState( "Set operation Flags" );
    Set_Operation_Flags();
126

127 128 129
    _GC->SetState( "Remove doubles" );
    // Remove the marked links
    {
charras's avatar
charras committed
130
        TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
131 132 133 134 135 136 137 138 139 140 141 142
        _LI.tohead();
        while( !_LI.hitroot() )
        {
            if ( _LI.item()->IsMarked() )
            {
                delete _LI.item();
                _LI.remove();
            }
            else
                _LI++;
        }
    }
143

144 145
    _GC->SetState( "Remove inlinks" );
    Remove_IN_Links();
146 147


148
    _GC->SetState( "Finished prepare graph" );
149 150 151 152 153 154
}



// x and y of the point will be rounded to the nearest
// xnew=N*grid and ynew=N*grid
charras's avatar
charras committed
155
void kbGraph::RoundInt( B_INT grid )
156
{
charras's avatar
charras committed
157
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
158 159 160 161 162 163 164
    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        _LI.item()->GetBeginNode()->RoundInt( grid );
        _LI.item()->GetEndNode()->RoundInt( grid );
        _LI++;
    }
165 166 167
}

// rotate graph minus 90 degrees or plus 90 degrees
charras's avatar
charras committed
168
void kbGraph::Rotate( bool plus90 )
169
{
170
    B_INT swap;
charras's avatar
charras committed
171
    kbNode* last = NULL;
172 173 174 175 176

    B_INT neg = -1;
    if ( plus90 )
        neg = 1;

charras's avatar
charras committed
177
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
178 179 180 181 182 183 184 185 186 187 188 189 190 191
    _LI.mergesort( linkXYsorter );

    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        if ( _LI.item()->GetBeginNode() != last )
        {
            swap = _LI.item()->GetBeginNode()->GetX();
            _LI.item()->GetBeginNode()->SetX( -neg * ( _LI.item()->GetBeginNode()->GetY() ) );
            _LI.item()->GetBeginNode()->SetY( neg * swap );
            last = _LI.item()->GetBeginNode();
        }
        _LI++;
    }
192 193
}

charras's avatar
charras committed
194
bool kbGraph::RemoveNullLinks()
195
{
196 197
    bool graph_is_modified = false;

charras's avatar
charras committed
198
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        if ( _LI.item()->GetBeginNode() == _LI.item()->GetEndNode() )
        {
            _LI.item()->MergeNodes( _LI.item()->GetBeginNode() );
            delete _LI.item();
            _LI.remove();
            graph_is_modified = true;
        }
        else
            _LI++;
    }
    return ( graph_is_modified );
213 214 215 216
}

// Add a link to the graph connection with
// other links is through the link his nodes
charras's avatar
charras committed
217
void kbGraph::AddLink( kbLink *a_link )
218
{
219
    assert( a_link );
220

221
    _linklist->insend( a_link );
222 223 224 225 226
}


// Add a link to the graph, by giving it two nodes
// the link is then made and added to the graph
charras's avatar
charras committed
227
void kbGraph::AddLink( kbNode *begin, kbNode *end )
228
{
229 230
    assert( begin && end );
    assert( begin != end );
charras's avatar
charras committed
231
    AddLink( new kbLink( 0, begin, end, _GC ) );
232 233 234 235
}


// Checks if there is a zeroline in the graph
charras's avatar
charras committed
236
bool kbGraph::AreZeroLines( B_INT Marge )
237
{
238 239
    bool Found_it = false;

charras's avatar
charras committed
240
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
241 242 243 244 245 246 247 248 249 250 251
    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        if ( _LI.item()->IsZero( Marge ) )
        {
            Found_it = true;
            break;
        }
        _LI++;
    }
    return Found_it;
252 253 254 255
}


// Delete links that do not fit the condition for given operation
charras's avatar
charras committed
256
void kbGraph::DeleteNonCond( BOOL_OP operation )
257
{
charras's avatar
charras committed
258
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
259 260 261 262 263 264 265 266 267 268 269
    _LI.tohead();
    while( !_LI.hitroot() )
    {
        if ( !_LI.item()->IsMarked( operation ) )
        {
            delete _LI.item();
            _LI.remove();
        }
        else
            _LI++;
    }
270 271
}

charras's avatar
charras committed
272
void kbGraph::HandleNonCond( BOOL_OP operation )
273
{
charras's avatar
charras committed
274
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
275 276 277 278 279 280 281 282 283 284
    _LI.tohead();
    while( !_LI.hitroot() )
    {
        if ( !_LI.item()->IsMarked( operation ) )
        {
            _LI.item()->SetBeenHere();
            _LI.item()->SetGraphNum( -1 );
        }
        _LI++;
    }
285 286 287 288 289 290 291
}

// All lines in the graph wich have a length < _GC->Get_Marge() will be deleted
//
// input : a marge, standard on _GC->Get_Marge()
// return: true if lines in the graph are deleted
//       : false if no lines in the graph are deleted
charras's avatar
charras committed
292
bool kbGraph::DeleteZeroLines( B_INT Marge )
293
{
294 295
    // Is the graph modified ?
    bool Is_Modified = false;
charras's avatar
charras committed
296
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

    int Processed = _LI.count();

    _LI.tohead();
    while ( Processed > 0 )
    {
        Processed--;
        if ( _LI.item()->IsZero( Marge ) )
        {
            // the current link is zero, so make from both nodes one node
            // and delete the current link from this graph
            _LI.item()->MergeNodes( _LI.item()->GetBeginNode() );
            // if an item is deleted the cursor of the list is set to the next
            // so no explicit forth is needed
            delete _LI.item();
            _LI.remove();
            // we have to set Processed, because if a zero line is deleted
            // another can be made zero by this deletion
            Processed = _LI.count();
            Is_Modified = true;
            if ( _LI.hitroot() )
                _LI.tohead();
        }
        else
            _LI++;
        if ( _LI.hitroot() )
            _LI.tohead();
    }
    return Is_Modified;
326 327 328 329 330 331 332 333 334 335
}


// Collects a graph starting at currentnode or attached link
// follow graphs right around.
// since the input node is always a topleft node, we can see on
// a connected link if we or dealing with a hole or NON hole.
// for instance a top link of a hole that is horizontal, always
// is IN above the link and OUT underneath the link.
// this for a non hole the opposite
charras's avatar
charras committed
336
void kbGraph::CollectGraph( kbNode *current_node, BOOL_OP operation, bool detecthole, int graphnumber, bool& foundholes )
337
{
charras's avatar
charras committed
338 339 340 341 342 343
    kbLink * currentlink;
    kbLink *nextlink;
    kbNode *next_node;
    kbNode *MyFirst;
    kbNode *Unlinked;
    kbLink *MyFirstlink;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

    bool Hole;
    LinkStatus whatside;

    currentlink = current_node->GetNotFlat();
    if ( !currentlink )
    {
        char buf[100];
        if ( detecthole )
            sprintf( buf, "no NON flat link Collectgraph for operation at %15.3lf , %15.3lf",
                     double( current_node->GetX() ), double( current_node->GetY() ) );
        else
            sprintf( buf, "no NON flat link Collectgraph at %15.3lf , %15.3lf",
                     double( current_node->GetX() ), double( current_node->GetY() ) );
        throw Bool_Engine_Error( buf, "Error", 9, 0 );
    }
360

361
    currentlink->SetBeenHere();
362

363 364 365 366
    if ( detecthole )
        Hole = currentlink->IsHole( operation );
    else
        Hole = currentlink->GetHole(); //simple extract do not detect holes, but use hole flag.
367

368
    currentlink->Redirect( current_node );
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    foundholes = Hole || foundholes;

    //depending if we have a hole or not
    //we take the left node or right node from the selected link (currentlink)
    //this MEANS for holes go left around and for non holes go right around
    //since the currentlink is already set to binhere, it will not go in that direction
    if ( Hole )
    {
        whatside = IS_LEFT;
        if ( currentlink->GetEndNode()->GetX() > current_node->GetX() )
            current_node = currentlink->GetEndNode();
    }
    else
    {
        whatside = IS_RIGHT;
        if ( currentlink->GetEndNode()->GetX() < current_node->GetX() )
            current_node = currentlink->GetEndNode();
    }
    currentlink->Redirect( current_node );
    MyFirst = current_node; //remember this as the start
    MyFirstlink = currentlink;

    next_node = currentlink->GetEndNode();

    // If this is a hole, Set as special link, this is the top link of this hole !
    // from this link we have to make links to the link above later on.
    if ( Hole )
        currentlink->SetTopHole( true );
    //set also the link as being part of a hole
    if ( detecthole )
        currentlink->SetHole( Hole );
    currentlink->SetGraphNum( graphnumber );

    // Walk over links and redirect them. taking most right links around
    while ( currentlink != NULL )
    {
        if ( Hole )
        {
            nextlink = next_node->GetMost( currentlink, IS_RIGHT, operation );
        }
        else
        {
            nextlink = next_node->GetMost( currentlink, IS_LEFT, operation );
            // next works too if same is used in CollectGraphLast
            //nextlink = next_node->GetMost(currentlink, IS_RIGHT, operation);
        }

        if ( nextlink == NULL )
        { //END POINT MUST BE EQAUL TO BEGIN POINT
            if ( !next_node->Equal( MyFirst, 1 ) )
            {
                throw Bool_Engine_Error( "no next (endpoint != beginpoint)", "graph", 9, 0 );

                //for god sake try this
                //nextlink = next_node->GetMost(currentlink, whatside ,operation);
            }
        }

        current_node = next_node;

        if ( nextlink != NULL )
        {
            nextlink->Redirect( current_node );
            nextlink->SetBeenHere();
            next_node = nextlink->GetEndNode();

            if ( current_node->GetNumberOfLinks() > 2 )
            {  // replace endnode of currentlink and beginnode of nextlink with new node
charras's avatar
charras committed
438
                Unlinked = new kbNode( current_node, _GC );
439 440 441 442 443 444 445 446 447 448 449 450 451
                currentlink->Replace( current_node, Unlinked );
                nextlink->Replace( current_node, Unlinked );
            }

            if ( detecthole )
                nextlink->SetHole( Hole );
            nextlink->SetGraphNum( graphnumber );
        }
        else
        {
            //close the found graph properly
            if ( current_node->GetNumberOfLinks() > 2 )
            {  // replace endnode of currentlink and beginnode of nextlink with new node
charras's avatar
charras committed
452
                Unlinked = new kbNode( current_node, _GC );
453 454 455 456 457 458 459 460 461 462 463 464 465
                currentlink->Replace( current_node, Unlinked );
                MyFirstlink->Replace( current_node, Unlinked );
            }
        }

        currentlink = nextlink;
    }

    //END POINT MUST BE EQAUL TO BEGIN POINT
    if ( !current_node->Equal( MyFirst, 1 ) )
    {
        throw Bool_Engine_Error( "in collect graph endpoint != beginpoint", "Error", 9, 0 );
    }
466 467
}

charras's avatar
charras committed
468
void kbGraph::CollectGraphLast( kbNode *current_node, BOOL_OP operation, bool detecthole, int graphnumber, bool& foundholes )
469
{
charras's avatar
charras committed
470 471 472 473 474 475
    kbLink * currentlink;
    kbLink *nextlink;
    kbNode *next_node;
    kbNode *MyFirst;
    kbNode *Unlinked;
    kbLink *MyFirstlink;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 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

    bool Hole;
    LinkStatus whatside;

    currentlink = current_node->GetNotFlat();
    if ( !currentlink )
    {
        char buf[100];
        if ( detecthole )
            sprintf( buf, "no NON flat link Collectgraph for operation at %15.3lf , %15.3lf",
                     double( current_node->GetX() ), double( current_node->GetY() ) );
        else
            sprintf( buf, "no NON flat link Collectgraph at %15.3lf , %15.3lf",
                     double( current_node->GetX() ), double( current_node->GetY() ) );
        throw Bool_Engine_Error( buf, "Error", 9, 0 );
    }

    currentlink->SetBeenHere();

    if ( detecthole )
        Hole = currentlink->IsHole( operation );
    else
        Hole = currentlink->GetHole(); //simple extract do not detect holes, but use hole flag.

    currentlink->Redirect( current_node );

    foundholes = Hole || foundholes;

    //depending if we have a hole or not
    //we take the left node or right node from the selected link (currentlink)
    //this MEANS for holes go left around and for non holes go right around
    //since the currentlink is already set to binhere, it will not go in that direction
    if ( Hole )
    {
        whatside = IS_LEFT;
        if ( currentlink->GetEndNode()->GetX() > current_node->GetX() )
            current_node = currentlink->GetEndNode();
    }
    else
    {
        whatside = IS_RIGHT;
        if ( currentlink->GetEndNode()->GetX() < current_node->GetX() )
            current_node = currentlink->GetEndNode();
    }
    currentlink->Redirect( current_node );
    MyFirst = current_node; //remember this as the start
    MyFirstlink = currentlink;

    next_node = currentlink->GetEndNode();

    // If this is a hole, Set as special link, this is the top link of this hole !
    // from this link we have to make links to the link above later on.
    if ( Hole )
        currentlink->SetTopHole( true );
    currentlink->SetGraphNum( graphnumber );

    // Walk over links and redirect them. taking most right links around
    while ( currentlink != NULL )
    {
        if ( Hole )
        {
            if ( currentlink->GetHoleLink() )
538
            {
539
                //in case we entered the hole via the hole link just now, we follow the hole.
540
                //This is taking as many holes as possible ( most right around)
541
                nextlink = next_node->GetMostHole( currentlink, IS_RIGHT , operation, false );
542 543 544 545
                if ( !nextlink ) // hole done?
                    //if we did get to this hole via a holelink?, then we might now be on the return link.
                    //BTW it is also possible that holes are already found via a non linked hole path,
                    //in that case, we did go to the HoleLink here, and directly return on the other holelink.
546
                    nextlink = next_node->GetHoleLink( currentlink, IS_RIGHT, true, operation );
547 548 549 550 551 552 553
                if ( !nextlink )
                {
                    //we did get to this hole via a holelink and we are on the returning holelink.
                    //So we left the hole collection, and continue with contours.
                    //Most Right is needed!
                    nextlink = next_node->GetMost( currentlink, IS_RIGHT, operation );
                }
554
            }
555
            else
556
            {
557
                nextlink = next_node->GetMostHole( currentlink, IS_RIGHT, operation ); // other holes first
558
                if ( !nextlink )
559
                    nextlink = next_node->GetHoleLink( currentlink,  IS_RIGHT, true, operation ); // other linked holes first
560 561 562 563 564 565 566
                if ( !nextlink )
                {
                    //We are leaving the hole.
                    //So we left the hole collection, and continue with contours.
                    //Most Right is needed!
                    nextlink = next_node->GetMost( currentlink, IS_RIGHT, operation );
                }
567
            }
568 569 570
        }
        else
        {
571 572
            //nextlink = next_node->GetMost( currentlink, IS_RIGHT, operation );
            //if ( !nextlink )
573

574
            //a hole link is preferred above a normal link. If not no holes would be linked in anyway.
575
            nextlink = next_node->GetHoleLink( currentlink, IS_RIGHT, true, operation );
576 577 578 579 580 581
            if ( !nextlink )
                //also if we can get to a hole directly within a contour, that is better (get as much as possible)
                nextlink = next_node->GetMostHole( currentlink, IS_RIGHT, operation );
            if ( !nextlink )
                //if non of the above, we are still on the contour and take as must as possible to the left.
                //Like that we take as many contour togethere as possible.
582

583
                nextlink = next_node->GetMost( currentlink, IS_LEFT, operation );
584 585
            // next works too if same is used in CollectGraphLast
            //nextlink = next_node->GetMost(currentlink, IS_RIGHT, operation);
586
        }
587

588 589 590 591 592
        if ( nextlink == NULL )
        { //END POINT MUST BE EQAUL TO BEGIN POINT
            if ( !next_node->Equal( MyFirst, 1 ) )
            {
                throw Bool_Engine_Error( "no next (endpoint != beginpoint)", "graph", 9, 0 );
593

594 595 596 597 598 599 600 601 602 603 604
                //for god sake try this
                //nextlink = next_node->GetMost(currentlink, whatside, operation);
            }
        }
        else
        {
            // when holes are already know, use the hole information to
            // go left are right around.
            Hole = nextlink->GetHole() || nextlink->GetHoleLink();
        }
        current_node = next_node;
605

606 607 608 609 610 611 612 613
        if ( nextlink != NULL )
        {
            nextlink->Redirect( current_node );
            nextlink->SetBeenHere();
            next_node = nextlink->GetEndNode();

            if ( current_node->GetNumberOfLinks() > 2 )
            {  // replace endnode of currentlink and beginnode of nextlink with new node
charras's avatar
charras committed
614
                Unlinked = new kbNode( current_node, _GC );
615 616 617
                currentlink->Replace( current_node, Unlinked );
                nextlink->Replace( current_node, Unlinked );
            }
618

619 620 621 622 623 624 625
            nextlink->SetGraphNum( graphnumber );
        }
        else
        {
            //close the found graph properly
            if ( current_node->GetNumberOfLinks() > 2 )
            {  // replace endnode of currentlink and beginnode of nextlink with new node
charras's avatar
charras committed
626
                Unlinked = new kbNode( current_node, _GC );
627 628 629 630 631 632 633 634 635 636 637 638 639
                currentlink->Replace( current_node, Unlinked );
                MyFirstlink->Replace( current_node, Unlinked );
            }
        }

        currentlink = nextlink;
    }

    //END POINT MUST BE EQAUL TO BEGIN POINT
    if ( !current_node->Equal( MyFirst, 1 ) )
    {
        throw Bool_Engine_Error( "in collect graph endpoint != beginpoint", "Error", 9, 0 );
    }
640 641 642 643 644 645
}
//==============================================================================
//==============================================================================

// Extract bi-directional graphs from this graph
// Mark the graphs also as being a hole or not.
charras's avatar
charras committed
646
void kbGraph::Extract_Simples( BOOL_OP operation, bool detecthole, bool& foundholes )
647
{
charras's avatar
charras committed
648
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
649
    if ( _LI.empty() ) return;
charras's avatar
charras committed
650
    kbNode *begin;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    int graphnumber = 1;

    _LI.mergesort( linkYXtopsorter );
    _LI.tohead();
    while ( true )
    {
        begin = GetMostTopLeft( &_LI ); // from all the most Top nodes,
        // take the most left one
        // to most or not to most, that is the question
        if ( !begin )
            break;

        try // collect the graph
        {
            if ( detecthole )
                CollectGraph( begin, operation, detecthole, graphnumber++, foundholes );
            else
                //CollectGraph( begin,operation,detecthole,graphnumber++, foundholes );
                CollectGraphLast( begin, operation, detecthole, graphnumber++, foundholes );
        }
        catch ( Bool_Engine_Error & error )
        {
            _GC->info( error.GetErrorMessage(), "error" );
            throw error;
        }
    }
677 678
}

charras's avatar
charras committed
679
void kbGraph::Split( kbGraphList* partlist )
680
{
charras's avatar
charras committed
681
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
682
    if ( _LI.empty() ) return;
683

charras's avatar
charras committed
684
    kbGraph *part = NULL;
685
    int graphnumber = 0;
686

687 688
    //sort the graph on graphnumber
    _LI.mergesort( linkGraphNumsorter );
689

690 691
    _LI.tohead();
    while ( !_LI.hitroot() )
692
    {
693 694 695
        if ( _LI.item()->GetGraphNum() > 0 && graphnumber != _LI.item()->GetGraphNum() )
        {
            graphnumber = _LI.item()->GetGraphNum();
charras's avatar
charras committed
696
            part = new kbGraph( _GC );
697 698
            partlist->insend( part );
        }
charras's avatar
charras committed
699
        kbLink* tmp = _LI.item();
700 701 702 703 704 705 706 707 708
        if ( _LI.item()->GetGraphNum() > 0 )
        {
            part->AddLink( tmp );
        }
        else
        {
            delete tmp;
        }
        _LI.remove();
709 710 711
    }
}

charras's avatar
charras committed
712
bool kbGraph::GetBeenHere()
713
{
714
    return _bin;
715 716 717
}

// return total number of links in this graph
charras's avatar
charras committed
718
int kbGraph::GetNumberOfLinks()
719
{
charras's avatar
charras committed
720
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
721
    return _LI.count();
722 723 724 725
}

//for all operations set the operation flags for the links
//based on the Group_Left_Right values
charras's avatar
charras committed
726
void kbGraph::Set_Operation_Flags()
727
{
charras's avatar
charras committed
728
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
729 730 731 732 733 734
    _LI.tohead();
    while( !_LI.hitroot() )
    {
        _LI.item()->SetLineTypes();
        _LI++;
    }
735 736 737
}

//  Remove unused (those not used for any operation) links
charras's avatar
charras committed
738
void kbGraph::Remove_IN_Links()
739
{
charras's avatar
charras committed
740
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
741 742 743 744 745 746 747 748 749 750 751 752
    _LI.tohead();
    for ( int t = _LI.count() ; t > 0; t-- )
    {
        // Is this link not used for any operation?
        if ( _LI.item()->IsUnused() )
        {
            delete _LI.item();
            _LI.remove();
        }
        else
            _LI++;
    }
753 754
}

charras's avatar
charras committed
755
void kbGraph::ResetBinMark()
756
{
charras's avatar
charras committed
757
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
758
    if ( _LI.empty() ) return;
charras's avatar
charras committed
759
    _LI.foreach_mf( &kbLink::UnMark );//reset bin and mark flag of each link
760 761
}

charras's avatar
charras committed
762
void kbGraph::ReverseAllLinks()
763
{
charras's avatar
charras committed
764 765
    kbNode * dummy;
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
766 767 768 769 770 771 772 773 774
    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        // swap the begin- and endnode of the current link
        dummy = _LI.item()->GetBeginNode();
        _LI.item()->SetBeginNode( _LI.item()->GetEndNode() );
        _LI.item()->SetEndNode( dummy );
        _LI++;
    }
775 776
}

charras's avatar
charras committed
777
void kbGraph::SetBeenHere( bool value )
778
{
779
    _bin = value;
780 781 782
}

// ReSet the flags  of the links
charras's avatar
charras committed
783
void kbGraph::Reset_flags()
784
{
charras's avatar
charras committed
785 786
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
    _LI.foreach_mf( &kbLink::Reset_flags );
787 788 789
}

// ReSet the bin and mark flag of the links
charras's avatar
charras committed
790
void kbGraph::Reset_Mark_and_Bin()
791
{
charras's avatar
charras committed
792 793
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
    _LI.foreach_mf( &kbLink::UnMark );//reset bin and mark flag of each link
794 795 796
}

// Set the group of the links to the same as newgroup
charras's avatar
charras committed
797
void kbGraph::SetGroup( GroupType newgroup )
798
{
charras's avatar
charras committed
799
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
800 801 802 803 804 805
    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        _LI.item()->SetGroup( newgroup );
        _LI++;
    }
806 807 808 809
}


// Set the number of the links to the same as newnr
charras's avatar
charras committed
810
void kbGraph::SetNumber( const int newnr )
811
{
charras's avatar
charras committed
812
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
813 814 815 816 817 818
    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        _LI.item()->SetGraphNum( newnr );
        _LI++;
    }
819 820 821 822 823 824 825 826 827 828 829 830
}


// This function will simplify a graph with the following rules
//
// This are the rules for symplifying the graphs
// 1. The following point is the same as the current one
// 2. The point after the following point is the same as the current one
// 3. The point after the following point lies on the same line as the current
//
// input : a marge
// return: true if graph is modified
831
//   : false if graph is NOT simplified
charras's avatar
charras committed
832
bool kbGraph::Simplify( B_INT Marge )
833
{
834
    bool graph_is_modified = false;
charras's avatar
charras committed
835
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
836 837
    int Processed = _LI.count();

charras's avatar
charras committed
838
    _LI.foreach_mf( &kbLink::UnMark );//reset bin and mark flag of each link
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

    _LI.tohead();
    GroupType mygroup = _LI.item()->Group();

    // All items must be processed
    while ( Processed > 0 )
    {
        // Gives the number of items to process
        Processed--;
        // Check if line is marked
        // Links will be marked during the process
        if ( _LI.item()->IsMarked() )
        {
            delete _LI.item();
            _LI.remove();
            graph_is_modified = true;
            Processed = _LI.count();
            if ( _LI.hitroot() )
                _LI.tohead();
            continue;
        }

        // Line is not marked, check if line is zero
        if ( _LI.item()->IsZero( Marge ) )
        {
            _LI.item()->MergeNodes( _LI.item()->GetBeginNode() );
            delete _LI.item();
            _LI.remove();
            graph_is_modified = true;
            Processed = _LI.count();
            if ( _LI.hitroot() )
                _LI.tohead();
            continue;
        }

        // begin with trying to simplify the link
        {
            // Line is not marked, not zero, so maybe it can be simplified
            bool virtual_link_is_modified;
charras's avatar
charras committed
878 879
            kbNode *new_begin, *new_end, *a_node;
            kbLink *a_link;
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

            _LI.item()->Mark();
            new_begin = _LI.item()->GetBeginNode();
            new_end   = _LI.item()->GetEndNode();

            // while virtual link is modified
            do
            {
                virtual_link_is_modified = false;
                // look in the previous direction
                if ( ( a_link = new_begin->GetPrevLink() ) != NULL )
                {
                    a_node = a_link->GetBeginNode();
                    if ( a_node->Simplify( new_begin, new_end, Marge ) )
                    {
                        new_begin->GetPrevLink()->Mark();
                        new_begin = a_node;
                        virtual_link_is_modified = true;
                    }
                }
                // look in the next direction
                if ( ( a_link = new_end->GetNextLink() ) != NULL )
                {
                    a_node = a_link->GetEndNode();
                    if ( a_node->Simplify( new_begin, new_end, Marge ) )
                    {
                        new_end->GetNextLink()->Mark();
                        new_end = a_node;
                        virtual_link_is_modified = true;
                    }
                }
                graph_is_modified = ( bool ) ( graph_is_modified || virtual_link_is_modified );
            }
            while ( virtual_link_is_modified );

            // was the link simplified
            if ( ( _LI.item()->GetBeginNode() != new_begin ) ||
                    ( _LI.item()->GetEndNode() != new_end ) )
            {
                // YES !!!!!
                int number = _LI.item()->GetGraphNum();
                delete _LI.item();
                _LI.remove();

                if ( _LI.hitroot() )
                    _LI.tohead();

charras's avatar
charras committed
927
                kbLink *newlink = new kbLink( number, new_begin, new_end, _GC );
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
                newlink->SetGroup( mygroup );

                _LI.insend( newlink );
                Processed = _LI.count();
                graph_is_modified = true;
                continue;
            }
            _LI.item()->UnMark();
        } // end of try to simplify
        _LI++;
        if ( _LI.hitroot() )
            _LI.tohead();
    }//end while all processed

    return graph_is_modified;
943 944 945 946 947
}

/*
// This function will smoothen a graph with the following rules
//
948 949
// 0. Process graphs with more than 3 links only. (while more than 3)
//  Otherwise some objects may end-up as lines or disappear completely.
950
// 1.
951 952 953 954
//  a. ?  Does begin-node lay on line(prevline.beginnode,endnode)
//     ->  merge beginnode to endnode
//  b. ?  Does end-node lay on line(beginnode,nextline.endnode)
//     ->  merge endnode to beginnode
955
// 2.
956 957 958 959
//  a. ?  Is distance between prevline.beginnode and endnode to short
//     ->  merge beginnode to endnode
//   b. ?  Is distance between beginnode and nextline.endnode to short
//     ->  merge endnode to beginnode
960
// 3.
961 962 963 964 965 966
//  a. ?  Does (this)beginnode lay in area of nextline
//    AND does cross-node lay on nextline
//   ->   move endnode to crossing of prevline and nextline
//  b. ?  Does (this)endnode lay in area of prevline
//    AND does cross-node lay on prevline
//   ->   move beginnode to crossing of prevline and nextline
967
// 4.
968 969 970 971 972 973
//  ?  Is this link too short
//   ?  Is prevline shorter than nextline
//     Y ->  ?  Is prevlink shorter than maxlength
//     ->  merge endnode to beginnode
//     N ->  ?  Is nextlink shorter than maxlength
//     ->  merge endnode to beginnode
974 975
//
//
976
// Types of glitches / lines to remove :
977
//
978 979
//    \         /      \     /       \         /
//   Z---A---B  OR  Z-------B---A    =>   Z-------B
980
//
981
//   (1)
982
//
983 984 985 986
//   ----A    C----          =>  ----A-----C----
//    \   /
//   (2)   \ /
//        B
987
//
988 989 990 991
//   ---Z                                          ---Z
//       \                                                \
//   (3)  \                                                \
//         \   B----------C--       =>          A---B----------C--
992 993 994
//          \ /
//           A
//
995
//   --Z---A                                          --Z__
996
//          \                                              -__
997
//   (4)     B------------C--       =>            B------------C--
998
//
999 1000 1001 1002 1003
// linkLsorter(L1,L2)
//  ret:
//   +1  L1 < L2
//    0  L1 == L2
//   -1  L1 > L2
1004 1005
//
*/
charras's avatar
charras committed
1006
bool kbGraph::Smoothen( B_INT Marge )
1007
{
charras's avatar
charras committed
1008
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
1009 1010
    if ( _LI.count() <= 3 ) // Don't modify it
        return false;
1011

charras's avatar
charras committed
1012 1013 1014 1015
    kbNode *Z, *A, *B, *C, *cross_node = new kbNode( _GC );
    kbLink *prevlink, *nextlink, *thislink;
    kbLine prevline( _GC ),  nextline( _GC ),  thisline( _GC );
    kbLine prevhelp( _GC ),  nexthelp( _GC );
1016

charras's avatar
charras committed
1017 1018
    kbLink  LZB( new kbNode( _GC ), new kbNode( _GC ), _GC ),
    LAC( new kbNode( _GC ), new kbNode( _GC ), _GC );
1019

1020 1021
    double distance = 0;
    double prevdist, nextdist;
1022

1023 1024 1025
    bool doprev, donext;
    bool graph_is_modified = false;
    bool kill = false; // for first instance
1026

1027 1028 1029 1030 1031
    _LI.tohead();
    int todo = _LI.count();
    thislink = _LI.item();
    B = thislink->GetEndNode();
    nextlink = thislink->Forth( B );
1032

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    // Type 1
    while ( todo > 0 )
    {
        if ( kill == true )
        {
            // remove link from graph
            _LI.toitem( thislink );
            graph_is_modified = true;
            delete _LI.item();
            _LI.remove();
            kill = false;
            thislink = nextlink;
            todo--;
            if ( _LI.count() < 3 )
                break;
        }
1049

1050 1051
        A = thislink->GetBeginNode();
        B = thislink->GetEndNode();
1052

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
        if ( A->ShorterThan( B, 1 ) )
        {
            A->Merge( B );
            kill = true;
            continue;
        }

        Z = thislink->Forth( A )->GetBeginNode();
        C = thislink->Forth( B )->GetEndNode();
        thisline.Set( thislink );
        thisline.CalculateLineParameters();
        nextlink = thislink->Forth( B );

        if ( thisline.PointInLine( Z, distance, 0.0 ) == ON_AREA )
        { // Z--A--B =>  Z--B       Merge this to previous
            thislink->MergeNodes( B ); // remove A
            kill = true;
            continue;
        }
        else if ( thisline.PointInLine( C, distance, 0.0 ) == ON_AREA )
        { // A--B--C =>  A--C       Merge this to next
            thislink->MergeNodes( A ); // remove B
            kill = true;
            continue;
        }

        thislink = nextlink;
        todo--;
    }

    kill = false;
    todo = _LI.count();
    _LI.mergesort( linkLsorter );
    _LI.tohead();

    while ( todo > 0 )
    {
        if ( kill == true )
        {
            delete _LI.item();
            _LI.remove();
            graph_is_modified = true;
            kill = false;
            //mergesort(linkLsorter);
            _LI.mergesort( linkLsorter );
            _LI.tohead();
            todo = _LI.count();
            if ( todo < 3 )  // A polygon, at least, has 3 sides
                break;
        }

        // Keep this order!
        thislink = _LI.item();
        A = thislink->GetBeginNode();
        B = thislink->GetEndNode();
        prevlink = thislink->Forth( A );
        nextlink = thislink->Forth( B );
        Z = prevlink->GetBeginNode();
        C = nextlink->GetEndNode();

        if ( A->ShorterThan( B, 1 ) )
        {
            A->Merge( B );
            kill = true;
            continue;
        }

        prevline.Set( prevlink );
        prevline.CalculateLineParameters();
        nextline.Set( nextlink );
        nextline.CalculateLineParameters();

        // Type 2
        if ( B->ShorterThan( Z, Marge ) )
        { // Z--A--B =>  Z--B       Merge this to previous
            thislink->MergeNodes( B ); // remove A
            kill = true;
            continue;
        }
        else if ( A->ShorterThan( C, Marge ) )
        { // A--B--C =>  A--C       Merge this to next
            thislink->MergeNodes( A ); // remove B
            kill = true;
            continue;
        }


        *LZB.GetBeginNode() = *Z;
        *LZB.GetEndNode() = *B;
        *LAC.GetBeginNode() = *A;
        *LAC.GetEndNode() = *C;
        prevhelp.Set( &LZB );
        nexthelp.Set( &LAC );
        prevhelp.CalculateLineParameters();
        nexthelp.CalculateLineParameters();


        // Type 4
        doprev = bool( prevhelp.PointInLine( A, prevdist, ( double )Marge ) == IN_AREA );
        donext = bool( nexthelp.PointInLine( B, nextdist, ( double )Marge ) == IN_AREA );
        doprev = bool( B->ShorterThan( Z, _GC->GetInternalMaxlinemerge() ) && doprev );
        donext = bool( A->ShorterThan( C, _GC->GetInternalMaxlinemerge() ) && donext );

        if ( doprev && donext )
        {
            if ( fabs( prevdist ) <= fabs( nextdist ) )
                thislink->MergeNodes( B ); // remove A
            else
                thislink->MergeNodes( A ); // remove B

            kill = true;
            continue;
        }
        else if ( doprev )
        {
            thislink->MergeNodes( B ); // remove A
            kill = true;
            continue;
        }
        else if ( donext )
        {
            thislink->MergeNodes( A ); // remove B
            kill = true;
            continue;
        }


        thisline.Set( thislink );
        thisline.CalculateLineParameters();

        // Type 1
        if ( thisline.PointInLine( Z, distance, 0.0 ) == ON_AREA )
        { // Z--A--B =>  Z--B       Merge this to previous
            thislink->MergeNodes( B ); // remove A
            kill = true;
            continue;
        }
        else if ( thisline.PointInLine( C, distance, 0.0 ) == ON_AREA )
        { // A--B--C =>  A--C       Merge this to next
            thislink->MergeNodes( A ); // remove B
            kill = true;
            continue;
        }


        // Type 3
        if ( nextline.PointInLine( A, distance, ( double ) Marge ) == IN_AREA )
        {
            if ( nextline.Intersect2( cross_node, &prevline ) )
            {
                if ( nextline.PointInLine( cross_node, distance, 0.0 ) == IN_AREA )
                {
                    B->Set( cross_node );
                    thislink->MergeNodes( B ); // remove A
                    kill = true;
                    continue;
                }
                else
                {
                    thislink->MergeNodes( A ); // remove B
                    kill = true;
                    continue;
                }
            }
            else
            {
                thislink->MergeNodes( A ); // remove B
                kill = true;
                continue;
            }
        }

        // Type 3
        if ( prevline.PointInLine( B, distance, ( double )Marge ) == IN_AREA )
        {
            if ( prevline.Intersect2( cross_node, &nextline ) )
            {
                if ( prevline.PointInLine( cross_node, distance, 0.0 ) == IN_AREA )
                {
                    A->Set( cross_node );
                    thislink->MergeNodes( A ); // remove B
                    kill = true;
                    continue;
                }
                else
                {
                    thislink->MergeNodes( B ); // remove A
                    kill = true;
                    continue;
                }
            }
            else
            {
                thislink->MergeNodes( B ); // remove A
                kill = true;
                continue;
            }
        }

        todo--;
        _LI++;
    } // end: while all processed

    delete cross_node;

    return graph_is_modified;
}


// Give the graphnumber of the first link in the graphlist
charras's avatar
charras committed
1263
int kbGraph::GetGraphNum()
1264
{
charras's avatar
charras committed
1265
    return ( ( kbLink* )_linklist->headitem() )->GetGraphNum();
1266 1267 1268 1269
}


// get the node with the highest Y value
charras's avatar
charras committed
1270
kbNode* kbGraph::GetTopNode()
1271 1272
{
    B_INT max_Y = MAXB_INT;
charras's avatar
charras committed
1273
    kbNode* result;
1274

charras's avatar
charras committed
1275
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

    _LI.tohead();
    while ( !_LI.hitroot() )
    {
        if ( !( _LI.item()->GetBeginNode()->GetY() < max_Y ) )
            break;
        _LI++;
    }
    result = _LI.item()->GetBeginNode();

    return result;
}

// THE GRAPH MUST be SORTED before using this function
// mergesort(linkYXtopsorter);
// Get the mostleft top node from the graph  for which the link flag is not set yet
charras's avatar
charras committed
1292
kbNode* kbGraph::GetMostTopLeft( TDLI<kbLink>* _LI )
1293 1294 1295 1296 1297
{
    while ( !_LI->hitroot() )
    {
        if ( !_LI->item()->BeenHere() )
        {
charras's avatar
charras committed
1298
            kbLink * a = _LI->item();
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
            //find the top node of this link (sorted on top allready)
            if ( a->GetBeginNode()->GetY() > a->GetEndNode()->GetY() )
                return( a->GetBeginNode() );
            if ( a->GetBeginNode()->GetY() < a->GetEndNode()->GetY() )
                return( a->GetEndNode() );
            else
                return( a->GetBeginNode() );
        }
        ( *_LI )++;
    }
    return NULL;
1310 1311 1312 1313
}

// Take the linkslist over from a other graph
// The linklist of the other graph will be empty afterwards
charras's avatar
charras committed
1314
void kbGraph::TakeOver( kbGraph *other )
1315
{
charras's avatar
charras committed
1316
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
1317
    _LI.takeover( other->_linklist );
1318 1319
}

1320
// calculate crossing with scanbeams
charras's avatar
charras committed
1321
bool kbGraph::CalculateCrossings( B_INT Marge )
1322
{
1323 1324
    // POINT <==> POINT
    _GC->SetState( "Node to Node" );
1325

1326 1327
    bool found = false;
    bool dummy = false;
1328

1329
    found = Merge_NodeToNode( Marge ) != 0;
1330

1331 1332
    if ( _linklist->count() < 3 )
        return found;
1333

1334
    // POINT <==> LINK
charras's avatar
charras committed
1335
    _GC->SetState( "Node to kbLink 0" );
1336

1337
    found = ScanGraph2( NODELINK, dummy ) != 0 || found;
1338

1339 1340
    _GC->SetState( "Rotate -90" );
    Rotate( false );
1341

charras's avatar
charras committed
1342
    _GC->SetState( "Node to kbLink -90" );
1343
    found = ScanGraph2( NODELINK, dummy ) != 0 || found;
1344

1345 1346
    _GC->SetState( "Rotate +90" );
    Rotate( true );
1347

1348 1349
    // LINK <==> LINK
    _GC->SetState( "intersect" );
1350

1351
    found = ScanGraph2( LINKLINK, dummy ) != 0 || found;
1352

1353 1354 1355
    /*
       if (!checksort())
       { {
charras's avatar
charras committed
1356
        TDLI<kbLink> _LI=TDLI<kbLink>(_linklist);
1357 1358 1359 1360 1361 1362
      _LI.mergesort(linkXYsorter);
         }
       writeintersections();
       writegraph(true);
       }
    */
1363

1364
// Rotate(false);
charras's avatar
charras committed
1365
// _GC->SetState("kbLink to kbLink -90");
1366
//   ScanGraph2(LINKLINK);
1367
// Rotate(true);
1368

1369
    writegraph( true );
1370

1371 1372
    _GC->Write_Log( "Node to Node" );
    _GC->SetState( "Node to Node" );
1373

1374 1375
    found = Merge_NodeToNode( Marge ) != 0 || found;
    writegraph( true );
1376

1377
    return found;
1378 1379
}

1380
// neem de nodes die binnen de margeafstand bij elkaar liggen samen RICHARD
charras's avatar
charras committed
1381
int kbGraph::Merge_NodeToNode( B_INT Marge )
1382
{
1383 1384 1385
    //aantal punten dat verplaatst is
    int merges = 0;
    {
charras's avatar
charras committed
1386
        TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
1387

1388 1389
        //unmark all links; markflag wordt gebruikt om aan te geven
        //of een link (eigenlijk beginnode ervan) al verwerkt is
charras's avatar
charras committed
1390
        _LI.foreach_mf( &kbLink::UnMark );
1391

1392 1393
        //sort links on x value of beginnode
        _LI.mergesort( linkXYsorter );
1394

1395 1396
        //extra iterator voor doorlopen links in graph
        {
charras's avatar
charras committed
1397
            TDLI<kbLink>  links = TDLI<kbLink>( _linklist );
1398

charras's avatar
charras committed
1399
            kbNode *nodeOne, *nodeTwo;
1400 1401
            //verwerk alle links (alle (begin)nodes)
            for( _LI.tohead(); !_LI.hitroot(); _LI++ )
1402
            {
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
                nodeOne = _LI.item()->GetBeginNode();

                // link (beginnode) al verwerkt?
                if( !_LI.item()->IsMarked() )
                {
                    // wordt verwerkt dus markeer
                    _LI.item()->Mark();

                    // doorloop alle links vanaf huidige tot link buiten marge
                    links.toiter( &_LI );links++;
                    while ( !links.hitroot() )
                    {
                        nodeTwo = links.item()->GetBeginNode();

                        // marked?
                        if( !links.item()->IsMarked() )
                        {
                            // x van beginnode vd link binnen marge?
                            if( babs( nodeOne->GetX() - nodeTwo->GetX() ) <= Marge )
                            {
                                // y van beginnode vd link binnen marge?
                                // zijn twee node-object referenties wel verschillend?
                                if( babs( nodeOne->GetY() - nodeTwo->GetY() ) <= Marge &&
                                        ( !( nodeOne == nodeTwo ) )
                                  )
                                {
                                    links.item()->Mark();
                                    nodeOne->Merge( nodeTwo );
                                    merges++;
                                }//y binnen marge en niet zelfde node
                            }//x binnen marge?
                            else
                            {
                                // link valt buiten marge; de rest vd links
                                // dus ook (omdat lijst gesorteerd is)
                                links.totail();
                            }
                        }//marked?
                        links++;
                    }//current -> ongeldig
                }//verwerkt?
            }//all links

        }//om de extra iterator te verwijderen
    }
    RemoveNullLinks();
1449

1450
    return merges;
1451 1452 1453
}


charras's avatar
charras committed
1454
int kbGraph::ScanGraph2( SCANTYPE scantype, bool& holes )
1455
{
charras's avatar
charras committed
1456
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
1457
    int found = 0;
1458

1459 1460
    //sort links on x and y value of beginnode
    _LI.mergesort( linkXYsorter );
1461

1462
    writegraph( false );
1463

1464
    //bin flag is used in scanbeam so reset
charras's avatar
charras committed
1465
    _LI.foreach_mf( &kbLink::SetNotBeenHere );
1466

1467
    ScanBeam* scanbeam = new ScanBeam( _GC );
charras's avatar
charras committed
1468 1469
    kbNode*  _low;
    kbNode*  _high;
1470

1471 1472 1473 1474 1475 1476
    _LI.tohead();
    while ( !_LI.attail() )
    {
        _low = _LI.item()->GetBeginNode();
        //find new links for the new beam and remove the old links
        if ( scanbeam->FindNew( scantype, &_LI, holes ) )
1477 1478
            found++;

1479 1480 1481 1482 1483
        //find a new low node, this should be a node not eqaul to the current _low
        do
        {  _LI++;}
        while ( !_LI.hitroot() && ( _low == _LI.item()->GetBeginNode() ) );
        if ( _LI.hitroot() )
1484 1485 1486
            //if the last few links share the same beginnode
            //we arive here
            break;
1487 1488
        else
            _high = _LI.item()->GetBeginNode();
1489

1490
        scanbeam->SetType( _low, _high );
1491

1492
        if ( scanbeam->RemoveOld( scantype, &_LI, holes ) )
1493
            found++;
1494
    }
1495

1496 1497
    delete scanbeam;
    return found;
1498 1499 1500 1501
}


/*
charras's avatar
charras committed
1502
 
1503
//       scanbeam->writebeam();
charras's avatar
charras committed
1504
 
1505 1506 1507 1508 1509
      if (j%100 ==0)
      {
        long x;
        long y;
        char buf[80];
1510 1511
     x=(long)_lowlink->GetBeginNode()->GetX();
     y=(long)_lowlink->GetBeginNode()->GetY();
1512
        sprintf(buf," x=%I64d , y=%I64d here %I64d",x,y,scanbeam->count());
1513 1514
   _GC->SetState(buf);
       scanbeam->writebeam();
1515
      }
charras's avatar
charras committed
1516 1517 1518
 
 
 
1519 1520 1521 1522 1523 1524
         writegraph(false);
            if (!checksort())
            {
               double x=_lowlink->GetBeginNode()->GetX();
               checksort();
            }
charras's avatar
charras committed
1525 1526 1527
 
 
 
1528 1529 1530
         _LI++;
      }
   }
charras's avatar
charras committed
1531
 
1532 1533
 delete scanbeam;
 return 0;
1534
}
charras's avatar
charras committed
1535 1536
 
 
1537 1538 1539 1540 1541
         if (!checksort())
         {
            x=_lowlink->GetBeginNode()->GetX();
            checksort();
         }
charras's avatar
charras committed
1542
 
1543 1544
         if (x >= -112200)
         {
1545 1546
//          writegraph(true);
//          scanbeam->writebeam();
1547 1548 1549 1550 1551 1552 1553
         }
*/


//=============================== Global Functions =============================

// Sorts the links on the X values
charras's avatar
charras committed
1554
int linkXYsorter( kbLink *a, kbLink* b )
1555
{
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
    if ( a->GetBeginNode()->GetX() < b->GetBeginNode()->GetX() )
        return( 1 );
    if ( a->GetBeginNode()->GetX() > b->GetBeginNode()->GetX() )
        return( -1 );
    //they are eqaul in x
    if ( a->GetBeginNode()->GetY() < b->GetBeginNode()->GetY() )
        return( -1 );
    if ( a->GetBeginNode()->GetY() > b->GetBeginNode()->GetY() )
        return( 1 );
    //they are eqaul in y
    return( 0 );
1567 1568 1569
}

// Sorts the links on the Y value of the beginnode
charras's avatar
charras committed
1570
int linkYXsorter( kbLink *a, kbLink* b )
1571
{
1572 1573 1574 1575 1576 1577 1578 1579 1580
    if ( a->GetBeginNode()->GetY() > b->GetBeginNode()->GetY() )
        return( 1 );
    if ( a->GetBeginNode()->GetY() < b->GetBeginNode()->GetY() )
        return( -1 );
    if ( a->GetBeginNode()->GetX() > b->GetBeginNode()->GetX() )
        return( -1 );
    if ( a->GetBeginNode()->GetX() < b->GetBeginNode()->GetX() )
        return( 1 );
    return( 0 );
1581 1582 1583 1584
}


// The sort function for sorting graph from shortest to longest (_l1 < _l2)
charras's avatar
charras committed
1585
int linkLsorter( kbLink *_l1, kbLink* _l2 )
1586
{
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
    B_INT dx1, dx2, dy1, dy2;
    dx1 = ( _l1->GetEndNode()->GetX() - _l1->GetBeginNode()->GetX() );
    dx1 *= dx1;
    dy1 = ( _l1->GetEndNode()->GetY() - _l1->GetBeginNode()->GetY() );
    dy1 *= dy1;
    dx2 = ( _l2->GetEndNode()->GetX() - _l2->GetBeginNode()->GetX() );
    dx2 *= dx2;
    dy2 = ( _l2->GetEndNode()->GetY() - _l2->GetBeginNode()->GetY() );
    dy2 *= dy2;

    dx1 += dy1; dx2 += dy2;

    if ( dx1 > dx2 )
        return( -1 );
    if ( dx1 < dx2 )
        return( 1 );
    return( 0 );
1604 1605 1606 1607 1608
}

// The sort function for the links in a graph (a.topnode < b.topnode)
// if the two links lay with the top nodes on eachother the most left will be selected

charras's avatar
charras committed
1609
int linkYXtopsorter( kbLink *a, kbLink *b )
1610 1611
{

1612 1613
    if ( bmax( a->GetBeginNode()->GetY(), a->GetEndNode()->GetY() ) < bmax( b->GetBeginNode()->GetY(), b->GetEndNode()->GetY() ) )
        return -1;
1614

1615 1616
    if ( bmax( a->GetBeginNode()->GetY(), a->GetEndNode()->GetY() ) > bmax( b->GetBeginNode()->GetY(), b->GetEndNode()->GetY() ) )
        return 1;
1617

1618 1619 1620
    //equal
    if ( bmin( a->GetBeginNode()->GetX(), a->GetEndNode()->GetX() ) < bmin( b->GetBeginNode()->GetX(), b->GetEndNode()->GetX() ) )
        return -1;
1621

1622 1623
    if ( bmin( a->GetBeginNode()->GetX(), a->GetEndNode()->GetX() ) > bmin( b->GetBeginNode()->GetX(), b->GetEndNode()->GetX() ) )
        return 1;
1624

1625
    return 0;
1626 1627 1628
}

// The sort function for sorting graph from shortest to longest (_l1 < _l2)
charras's avatar
charras committed
1629
int linkGraphNumsorter( kbLink *_l1, kbLink* _l2 )
1630
{
1631 1632 1633 1634 1635
    if ( _l1->GetGraphNum() > _l2->GetGraphNum() )
        return( -1 );
    if ( _l1->GetGraphNum() < _l2->GetGraphNum() )
        return( 1 );
    return( 0 );
1636 1637 1638
}

// Perform an operation on the graph
charras's avatar
charras committed
1639
void kbGraph::Boolean( BOOL_OP operation, kbGraphList* Result )
1640
{
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
    _GC->SetState( "Performing Operation" );

    // At this moment we have one graph
    // step one, split it up in single graphs, and mark the holes
    // step two, make one graph again and link the holes
    // step three, split up again and dump the result in Result

    _GC->SetState( "Extract simples first " );

    ResetBinMark();
    DeleteNonCond( operation );
    HandleNonCond( operation );

    bool foundholes = false;
    try
    {
        WriteGraphKEY( _GC );
        writegraph( true );
1659

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
        Extract_Simples( operation, true, foundholes );
    }
    catch ( Bool_Engine_Error & error )
    {
        throw error;
    }

    // now we will link all the holes in de graphlist
    // By the scanbeam method we will search all the links that are marked
    // as topleft link of a the hole polygon, when we find them we will get the
    // closest link, being the one higher in the beam.
    // Next we will create a link and nodes toceoonect the hole into it outside contour
    // or other hole.
    _GC->SetState( "Linking Holes" );

    if ( _linklist->count() == 0 )
        //extract simples did leaf an empty graph
        //so we are ready
        return;

    if ( foundholes && _GC->GetLinkHoles() )
    {
        //the first extract simples introduced nodes at the same location that are not merged.
        //e.g. Butterfly polygons as two seperate polygons.
        //The scanlines can not cope with this, so merge them, and later extract one more time.
        Merge_NodeToNode( 0 );

charras's avatar
charras committed
1687
#if KBOOL_LOG == 1
1688
        //_GC->SetLog( true ); 
1689
        _GC->Write_Log( "LINKHOLES\n" );
charras's avatar
charras committed
1690
#endif
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
        writegraph( false );

        //link the holes into the non holes if there are any.
        bool holes = false;
        ScanGraph2( LINKHOLES, holes );

        WriteGraphKEY( _GC );
        writegraph( true );
        if ( holes )
        {
            //to delete extra points
            //those extra points are caused by link holes
1703
            //and are eqaul ( the smallest number in integer is 1 )
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
            DeleteZeroLines( 1 );

            _GC->SetState( "extract simples last" );
            ResetBinMark();
            HandleNonCond( operation );
            DeleteNonCond( operation );
            Extract_Simples( operation, false, foundholes );
        }
    }

    //writegraph( false );
    Split( Result );
1716 1717 1718
}

// Perform an correction on the graph
charras's avatar
charras committed
1719
void kbGraph::Correction( kbGraphList* Result, double factor )
1720
{
1721 1722 1723 1724 1725
    // At this moment we have one graph
    // step one, split it up in single graphs, and mark the holes
    // step two, make one graph again and link the holes
    // step three, split up again and dump the result in Result
    _GC->SetState( "Extract simple graphs" );
1726

1727 1728 1729 1730
    //extract the (MERGE or OR) result from the graph
    if ( Simplify( _GC->GetGrid() ) )
        if ( GetNumberOfLinks() < 3 )
            return;
1731

charras's avatar
charras committed
1732
    kbGraph* original = new kbGraph( _GC );
1733

1734 1735
    {
        if ( _linklist->empty() ) return;
1736

charras's avatar
charras committed
1737 1738 1739 1740 1741
        kbLink* _current = GetFirstLink();
        kbNode *_first = new kbNode( _current->GetBeginNode(), _GC );
        kbNode *_last  = _current->GetBeginNode();
        kbNode *_begin = _first;
        kbNode *_end   = _first;
1742

1743 1744 1745 1746 1747 1748
        int _nr_of_points = GetNumberOfLinks();
        for ( int i = 1; i < _nr_of_points; i++ )
        {
            // get the other node on the link
            _last = _current->GetOther( _last );
            // make a node from this point
charras's avatar
charras committed
1749
            _end = new kbNode( _last, _GC );
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782

            // make a link between begin and end
            original->AddLink( _begin, _end );

            _begin = _end;
            _current = _current->Forth( _last );
        }

        // make a link between the _begin and the first to close the graph
        original->AddLink( _begin, _first );
    }

    SetNumber( 1 );
    SetGroup( GROUP_A );
    Prepare( 1 );
    ResetBinMark();
    //DeleteNonCond(BOOL_OR);
    HandleNonCond( BOOL_OR );

    bool foundholes = false;
    Extract_Simples( BOOL_OR, true, foundholes );
    Split( Result );

    //Result contains the separate boundaries and holes

    //ring creation should never be alternate rule, since it overlaps.
    //So temprarely modify it.
    bool rule = _GC->GetWindingRule();
    _GC->SetWindingRule( true );

    _GC->SetState( "Create rings" );
    //first create a ring around all simple graphs
    {
charras's avatar
charras committed
1783 1784
        TDLI<kbGraph> IResult = TDLI<kbGraph>( Result );
        kbGraphList *_ring = new kbGraphList( _GC );
1785 1786 1787 1788 1789 1790
        {
            //put into one graphlist
            IResult.tohead();
            int i;
            int n = IResult.count();
            for ( i = 0;i < n;i++ )
1791
            {
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
                {
                    IResult.item()->MakeClockWise();
                    IResult.item()->CreateRing_fast( _ring, fabs( factor ) );
                    //     IResult.item()->CreateRing(_ring,fabs(factor));
                }
                delete( IResult.item() );
                IResult.remove();

                //move ring graphlist to result
                while ( !_ring->empty() )
                {
                    //add to end
charras's avatar
charras committed
1804 1805
                    ( ( kbGraph* )_ring->headitem() )->MakeClockWise();
                    IResult.insend( ( kbGraph* )_ring->headitem() );
1806 1807
                    _ring->removehead();
                }
1808
            }
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
        }
        delete _ring;

        //IResult contains all rings
        //prepare the graphs for extracting the links of a certain operation
        //set original graphlist to groupA and ring to groupB
        int i = 2;
        IResult.tohead();
        while ( !IResult.hitroot() )
        {
            IResult.item()->Reset_flags();
            IResult.item()->SetGroup( GROUP_B );
            IResult.item()->SetNumber( i );
            i++;
            IResult++;
        }
    }
1826

1827 1828 1829 1830 1831 1832 1833
    //a ring shape can overlap itself, for alternate filling this is problem.
    //doing a merge in winding rule makes this oke, since overlap is removed by it.
    if ( !rule ) //alternate rule?
    {
        Prepare( 1 );
        Boolean( BOOL_OR, Result );

charras's avatar
charras committed
1834
        TDLI<kbGraph> IResult = TDLI<kbGraph>( Result );
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
        //IResult contains all rings
        //prepare the graphs for extracting the links of a certain operation
        //set original graphlist to groupA and ring to groupB
        int i = 2;
        IResult.tohead();
        while ( !IResult.hitroot() )
        {
            IResult.item()->Reset_flags();
            IResult.item()->SetGroup( GROUP_B );
            IResult.item()->SetNumber( i );
            i++;
            IResult++;
        }
    }
1849

1850 1851
    //restore filling rule
    _GC->SetWindingRule( rule );
1852

1853 1854 1855 1856 1857 1858
    TakeOver( original );
    Reset_flags();
    SetNumber( 1 );
    SetGroup( GROUP_A );
    Result->MakeOneGraph( this ); // adds all graph its links to original
    // Result will be empty afterwords
1859 1860


1861
    //merge ring with original shapes for positive correction else subtract ring
1862

1863 1864 1865 1866 1867 1868
    //calculate intersections etc.
    //SINCE correction will calculate intersections between
    //ring and original _GC->Get_Marge() must be set a lot smaller then factor
    //during the rest of this routine
    //else wierd effects will be the result.
    double Backup_Marge = _GC->GetMarge();
1869

1870 1871 1872 1873 1874 1875 1876
    if ( _GC->GetInternalMarge() > fabs( factor / 100 ) )
    {
        _GC->SetInternalMarge( ( B_INT ) fabs( factor / 100 ) );
        //less then 1 is usless since all coordinates are integers
        if ( _GC->GetInternalMarge() < 1 )
            _GC->SetInternalMarge( 1 );
    }
1877 1878


1879
    Prepare( 1 );
1880

1881
    _GC->SetState( "Add/Substract rings" );
1882

1883 1884 1885 1886
    if ( factor > 0 )
        Boolean( BOOL_OR, Result );
    else
        Boolean( BOOL_A_SUB_B, Result );
1887

1888
    _GC->SetMarge( Backup_Marge );
1889

1890 1891
    //the result of the graph correction is in Result
    delete original;
1892 1893 1894
}

// Perform an operation on the graph
charras's avatar
charras committed
1895
void kbGraph::MakeRing( kbGraphList* Result, double factor )
1896 1897
{

1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
    bool rule = _GC->GetWindingRule();
    _GC->SetWindingRule( true );

    // At this moment we have one graph
    // step one, split it up in single graphs, and mark the holes
    // step two, make one graph again and link the holes
    // step three, split up again and dump the result in Result
    _GC->SetState( "Extract simple graphs" );

    //extract the (MERGE or OR) result from the graph
    SetNumber( 1 );
    Prepare( 1 );

    ResetBinMark();
    //DeleteNonCond(BOOL_OR);
    HandleNonCond( BOOL_OR );

    bool foundholes = false;
    Extract_Simples( BOOL_OR, true, foundholes );
    Split( Result );
    //Iresult contains the separate boundaries and holes
    //make a correction on the boundaries factor
    //make a correction on the holes -factor

    _GC->SetState( "Create rings" );

    //first create a ring around all simple graphs
charras's avatar
charras committed
1925 1926
    TDLI<kbGraph> IResult = TDLI<kbGraph>( Result );
    kbGraphList *_ring = new kbGraphList( _GC );
1927 1928 1929 1930 1931
    {
        IResult.tohead();
        int i;
        int n = IResult.count();
        for ( i = 0;i < n;i++ )
1932
        {
1933 1934 1935 1936 1937 1938
            {
                IResult.item()->MakeClockWise();
                IResult.item()->CreateRing_fast( _ring, fabs( factor ) );
            }
            delete( IResult.item() );
            IResult.remove();
1939

1940 1941 1942 1943
            //move ring graphlist to result
            while ( !_ring->empty() )
            {
                //add to end
charras's avatar
charras committed
1944 1945
                ( ( kbGraph* )_ring->headitem() )->MakeClockWise();
                IResult.insend( ( kbGraph* )_ring->headitem() );
1946 1947 1948 1949 1950 1951
                _ring->removehead();
            }
        }
    }
    delete _ring;
    _GC->SetWindingRule( rule );
1952 1953 1954
}

//create a ring shapes on every edge of the graph
charras's avatar
charras committed
1955
void kbGraph::CreateRing( kbGraphList *ring, double factor )
1956
{
charras's avatar
charras committed
1957
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
1958 1959 1960
    _LI.tohead();
    while( !_LI.hitroot() )
    {
charras's avatar
charras committed
1961
        kbGraph * shape = new kbGraph( _GC );
1962 1963 1964 1965 1966
        //generate shape around link
        shape->Make_Rounded_Shape( _LI.item(), factor );
        ring->insbegin( shape );
        _LI++;
    }
1967 1968 1969
}

//create a ring shapes on every edge of the graph
charras's avatar
charras committed
1970
void kbGraph::CreateRing_fast( kbGraphList *ring, double factor )
1971
{
charras's avatar
charras committed
1972 1973 1974
    kbNode * begin;
    kbLink* currentlink;
    kbLine  currentline( _GC );
1975

charras's avatar
charras committed
1976
    kbLine  firstline( _GC );
1977

charras's avatar
charras committed
1978 1979
    kbLink* nextlink;
    kbLine nextline( _GC );
1980

1981
    {
charras's avatar
charras committed
1982 1983
        TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
        _LI.foreach_mf( &kbLink::UnMark );//reset bin and mark flag of each link
1984 1985
        _LI.mergesort( linkYXsorter );
        _LI.tohead();
1986

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
        begin = GetMostTopLeft( &_LI ); // from all the most Top nodes,
        // take the most left one
    }
    if ( !begin )
        return;

    currentlink = begin->GetIncomingLink();
    currentline.Set( currentlink );
    currentline.CalculateLineParameters();

    nextlink = begin->GetOutgoingLink();
    nextline.Set( nextlink );
    nextline.CalculateLineParameters();

    firstline.Set( nextlink );
    firstline.CalculateLineParameters();

    while ( nextlink )
    {
charras's avatar
charras committed
2006
        kbGraph * shape = new kbGraph( _GC );
2007 2008
        {

charras's avatar
charras committed
2009 2010
            kbNode* _last_ins_left  = 0;
            kbNode* _last_ins_right = 0;
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044

            currentline.Create_Begin_Shape( &nextline, &_last_ins_left, &_last_ins_right, factor, shape );

            while( true )
            {
                currentline = nextline;
                currentlink = nextlink;
                currentlink->SetBeenHere();

                nextlink = currentlink->GetEndNode()->Follow( currentlink );
                if ( nextlink )
                {
                    nextline.Set( nextlink );
                    nextline.CalculateLineParameters();
                    if ( !currentline.Create_Ring_Shape( &nextline, &_last_ins_left, &_last_ins_right, factor, shape ) )
                        break;
                }
                else
                    break;
            }

            //finish this part
            if ( nextlink )
                currentline.Create_End_Shape( &nextline, _last_ins_left, _last_ins_right, factor, shape );
            else
                currentline.Create_End_Shape( &firstline, _last_ins_left, _last_ins_right, factor, shape );

            shape->MakeOneDirection();
            shape->MakeClockWise();
        }

        //if the shape is very small first merge it with the previous shape
        if ( !ring->empty() && shape->Small( ( B_INT ) fabs( factor * 3 ) ) )
        {
charras's avatar
charras committed
2045
            TDLI<kbGraph> Iring = TDLI<kbGraph>( ring );
2046 2047 2048

            Iring.totail();

charras's avatar
charras committed
2049
            kbGraphList *_twoshapes = new kbGraphList( _GC );
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
            _twoshapes->insbegin( shape );
            _twoshapes->insbegin( Iring.item() );
            Iring.remove();
            _twoshapes->Merge();

            //move merged graphlist to ring
            Iring.takeover( _twoshapes );
            delete _twoshapes;
        }
        else
            ring->insend( shape );

        currentlink->SetBeenHere();
    }
2064 2065 2066 2067 2068 2069 2070 2071
}

//create an arc and add it to the graph
//center of circle
//begin point of arc
//end point of arc
//radius of arc
//aberation for generating the segments
charras's avatar
charras committed
2072
void kbGraph::CreateArc( kbNode* center, kbNode* begin, kbNode* end, double radius, bool clock, double aber )
2073
{
2074 2075 2076 2077 2078
    double phi, dphi, dx, dy;
    int Segments;
    int i;
    double ang1, ang2, phit;

charras's avatar
charras committed
2079 2080
    kbNode* _last_ins;
    kbNode* _current;
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129

    _last_ins = begin;

    dx = ( double ) _last_ins->GetX() - center->GetX();
    dy = ( double ) _last_ins->GetY() - center->GetY();
    ang1 = atan2( dy, dx );
    if ( ang1 < 0 ) ang1 += 2.0 * M_PI;
    dx = ( double ) end->GetX() - center->GetX();
    dy = ( double ) end->GetY() - center->GetY();
    ang2 = atan2( dy, dx );
    if ( ang2 < 0 ) ang2 += 2.0 * M_PI;

    if ( clock )
    { //clockwise
        if ( ang2 > ang1 )
            phit = 2.0 * M_PI - ang2 + ang1;
        else
            phit = ang1 - ang2;
    }
    else
    { //counter_clockwise
        if ( ang1 > ang2 )
            phit = -( 2.0 * M_PI - ang1 + ang2 );
        else
            phit = -( ang2 - ang1 );
    }

    //what is the delta phi to get an accurancy of aber
    dphi = 2 * acos( ( radius - aber ) / radius );

    //set the number of segments
    if ( phit > 0 )
        Segments = ( int )ceil( phit / dphi );
    else
        Segments = ( int )ceil( -phit / dphi );

    if ( Segments <= 1 )
        Segments = 1;
    if ( Segments > 6 )
        Segments = 6;

    dphi = phit / ( Segments );

    for ( i = 1; i < Segments; i++ )
    {
        dx = ( double ) _last_ins->GetX() - center->GetX();
        dy = ( double ) _last_ins->GetY() - center->GetY();
        phi = atan2( dy, dx );

charras's avatar
charras committed
2130
        _current = new kbNode( ( B_INT ) ( center->GetX() + radius * cos( phi - dphi ) ),
2131 2132 2133 2134 2135 2136 2137 2138
                             ( B_INT ) ( center->GetY() + radius * sin( phi - dphi ) ), _GC );
        AddLink( _last_ins, _current );

        _last_ins = _current;
    }

    // make a node from the endnode of link
    AddLink( _last_ins, end );
2139 2140
}

charras's avatar
charras committed
2141
void kbGraph::CreateArc( kbNode* center, kbLine* incoming, kbNode* end, double radius, double aber )
2142
{
2143 2144 2145 2146 2147
    double distance = 0;
    if ( incoming->PointOnLine( center, distance, _GC->GetAccur() ) == RIGHT_SIDE )
        CreateArc( center, incoming->GetEndNode(), end, radius, true, aber );
    else
        CreateArc( center, incoming->GetEndNode(), end, radius, false, aber );
2148 2149
}

charras's avatar
charras committed
2150
void kbGraph::MakeOneDirection()
2151
{
2152
    int _nr_of_points = _linklist->count();
charras's avatar
charras committed
2153
    kbLink* _current = ( kbLink* )_linklist->headitem();
2154

charras's avatar
charras committed
2155 2156
    kbNode* _last = _current->GetBeginNode();
    kbNode* dummy;
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172

    for ( int i = 0; i < _nr_of_points; i++ )
    {
        // get the other node on the link
        _last = _current->GetOther( _last );
        // get the other link on the node
        _current = _current->Forth( _last );

        if ( _current->GetBeginNode() != _last )
        {
            // swap the begin- and endnode of the current link
            dummy = _current->GetBeginNode();
            _current->SetBeginNode( _current->GetEndNode() );
            _current->SetEndNode( dummy );
        }
    }
2173 2174
}

charras's avatar
charras committed
2175
bool kbGraph::Small( B_INT howsmall )
2176 2177
{

charras's avatar
charras committed
2178
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
2179
    _LI.tohead();
charras's avatar
charras committed
2180
    kbNode* bg = _LI.item()->GetBeginNode();
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
    B_INT xmin = bg->GetX();
    B_INT xmax = bg->GetX();
    B_INT ymin = bg->GetY();
    B_INT ymax = bg ->GetY();
    while ( !_LI.hitroot() )
    {
        bg = _LI.item()->GetBeginNode();
        // make _boundingbox bigger if the link makes the graph bigger
        // Checking if point is in bounding-box with marge
        xmin = bmin( xmin, bg->GetX() );
        xmax = bmax( xmax, bg->GetX() );
        ymin = bmin( ymin, bg->GetY() );
        ymax = bmax( ymax, bg->GetY() );
        _LI++;
    }

    B_INT dx = ( xmax - xmin );
    B_INT dy = ( ymax - ymin );

    if ( ( dx < howsmall ) && ( dy < howsmall ) )
        return true;

    return false;
2204 2205 2206 2207 2208
}


//create a circle at end and begin point
// and block in between
charras's avatar
charras committed
2209
void kbGraph::Make_Rounded_Shape( kbLink* a_link, double factor )
2210
{
2211 2212 2213
    double phi, dphi, dx, dy;
    int Segments = 6;
    int i;
2214 2215


charras's avatar
charras committed
2216
    kbLine theline( a_link, _GC );
2217
    theline.CalculateLineParameters();
2218

charras's avatar
charras committed
2219 2220 2221
    kbNode* _current;
    kbNode *_first = new kbNode( a_link->GetBeginNode(), _GC );
    kbNode *_last_ins = _first;
2222

2223
    theline.Virtual_Point( _first, factor );
2224

2225
    // make a node from this point
charras's avatar
charras committed
2226
    _current = new kbNode( a_link->GetEndNode(), _GC );
2227
    theline.Virtual_Point( _current, factor );
2228

2229 2230 2231
    // make a link between the current and the previous and add this to graph
    AddLink( _last_ins, _current );
    _last_ins = _current;
2232

2233 2234 2235 2236 2237 2238 2239 2240
    // make a half circle around the clock with the opposite point as
    // the middle point of the circle
    dphi = M_PI / ( Segments );
    for ( i = 1; i < Segments; i++ )
    {
        dx = ( double ) _last_ins->GetX() - a_link->GetEndNode()->GetX();
        dy = ( double ) _last_ins->GetY() - a_link->GetEndNode()->GetY();
        phi = atan2( dy, dx );
2241

charras's avatar
charras committed
2242
        _current = new kbNode( ( B_INT ) ( a_link->GetEndNode()->GetX() + factor * cos( phi - dphi ) ),
2243
                             ( B_INT ) ( a_link->GetEndNode()->GetY() + factor * sin( phi - dphi ) ), _GC );
2244

2245
        AddLink( _last_ins, _current );
2246

2247 2248
        _last_ins = _current;
    }
2249

2250
    // make a node from the endnode of link
charras's avatar
charras committed
2251
    _current = new kbNode( a_link->GetEndNode(), _GC );
2252 2253 2254
    theline.Virtual_Point( _current, -factor );
    AddLink( _last_ins, _current );
    _last_ins = _current;
2255

2256
    // make a node from this beginnode of link
charras's avatar
charras committed
2257
    _current = new kbNode( a_link->GetBeginNode(), _GC );
2258 2259 2260
    theline.Virtual_Point( _current, -factor );
    AddLink( _last_ins, _current );
    _last_ins = _current;
2261

2262 2263 2264 2265 2266
    for ( i = 1; i < Segments; i++ )
    {
        dx = ( double ) _last_ins->GetX() - a_link->GetBeginNode()->GetX();
        dy = ( double ) _last_ins->GetY() - a_link->GetBeginNode()->GetY();
        phi = atan2( dy, dx );
2267

charras's avatar
charras committed
2268
        _current = new kbNode( ( B_INT )( a_link->GetBeginNode()->GetX() + factor * cos( phi - dphi ) ),
2269
                             ( B_INT )( a_link->GetBeginNode()->GetY() + factor * sin( phi - dphi ) ), _GC );
2270

2271
        AddLink( _last_ins, _current );
2272

2273 2274
        _last_ins = _current;
    }
2275

2276 2277
    // make a link between the last and the first to close the graph
    AddLink( _last_ins, _first );
jean-pierre charras's avatar
jean-pierre charras committed
2278
}
2279 2280 2281

//make the graph clockwise orientation,
//return if the graph needed redirection
charras's avatar
charras committed
2282
bool kbGraph::MakeClockWise()
2283
{
2284 2285 2286
    if ( _GC->GetOrientationEntryMode() )
        return false;

charras's avatar
charras committed
2287
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
2288 2289
    if ( _LI.empty() ) return false;

charras's avatar
charras committed
2290 2291
    kbLink *currentlink;
    kbNode *begin;
2292

charras's avatar
charras committed
2293
    _LI.foreach_mf( &kbLink::UnMark );//reset bin and mark flag of each link
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
    _LI.mergesort( linkYXtopsorter );
    _LI.tohead();

    begin = GetMostTopLeft( &_LI ); // from all the most Top nodes,
    // take the most left one

    currentlink = begin->GetNotFlat();
    if ( !currentlink )
    {
        char buf[100];
        sprintf( buf, "no NON flat link MakeClockWise at %15.3lf , %15.3lf",
                 double( begin->GetX() ), double( begin->GetY() ) );
        throw Bool_Engine_Error( buf, "Error", 9, 0 );
    }

    //test to see if the orientation is right or left
    if ( currentlink->GetBeginNode() == begin )
    {
        if ( currentlink->GetEndNode()->GetX() < begin->GetX() )
        {
            //going left
            //it needs redirection
            ReverseAllLinks();
            return true;
        }
    }
    else
    {
        if ( currentlink->GetBeginNode()->GetX() > begin->GetX() )
        {  //going left
            //it needs redirection
            ReverseAllLinks();
            return true;
        }
    }
    return false;
2330 2331
}

charras's avatar
charras committed
2332
bool kbGraph::writegraph( bool linked )
2333 2334 2335
{
#if KBOOL_DEBUG == 1

2336
    FILE * file = _GC->GetLogFile();
2337

2338 2339
    if ( file == NULL )
        return true;
2340

2341
    fprintf( file, "# graph\n" );
2342

charras's avatar
charras committed
2343
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
2344 2345 2346 2347
    if ( _LI.empty() )
    {
        return true;
    }
2348

2349 2350 2351
    _LI.tohead();
    while( !_LI.hitroot() )
    {
charras's avatar
charras committed
2352
        kbLink * curl = _LI.item();
2353

2354
        fprintf( file, " linkbegin %I64d %I64d \n", curl->GetBeginNode()->GetX() , curl->GetBeginNode()->GetY() );
2355

2356 2357
        if ( linked )
        {
charras's avatar
charras committed
2358
            TDLI<kbLink> Inode( curl->GetBeginNode()->GetLinklist() );
2359 2360 2361
            Inode.tohead();
            while( !Inode.hitroot() )
            {
2362

2363
                fprintf( file, " b %I64d %I64d \n", Inode.item()->GetBeginNode()->GetX() , Inode.item()->GetBeginNode()->GetY() );
2364

2365
                fprintf( file, " e %I64d %I64d \n", Inode.item()->GetEndNode()->GetX() , Inode.item()->GetEndNode()->GetY() );
2366

2367 2368 2369 2370
                Inode++;
            }
        }
        fprintf( file, " linkend %I64d %I64d \n", curl->GetEndNode()->GetX() , curl->GetEndNode()->GetY() );
2371 2372


2373 2374
        if ( linked )
        {
charras's avatar
charras committed
2375
            TDLI<kbLink> Inode( curl->GetEndNode()->GetLinklist() );
2376 2377 2378
            Inode.tohead();
            while( !Inode.hitroot() )
            {
2379

2380
                fprintf( file, " b %I64d %I64d \n", Inode.item()->GetBeginNode()->GetX() , Inode.item()->GetBeginNode()->GetY() );
2381

2382
                fprintf( file, " e %I64d %I64d \n", Inode.item()->GetEndNode()->GetX() , Inode.item()->GetEndNode()->GetY() );
2383

2384 2385
                Inode++;
            }
2386

2387
        }
2388

2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
        if ( curl->GetBeginNode() == curl->GetEndNode() )
            fprintf( file, "     null_link \n" );

        fprintf( file, "    group %d ", curl->Group() );
        fprintf( file, "    bin   %d ", curl->BeenHere() );
        fprintf( file, "    mark  %d ", curl->IsMarked() );
        fprintf( file, "    leftA %d ", curl->GetLeftA() );
        fprintf( file, "    rightA %d ", curl->GetRightA() );
        fprintf( file, "    leftB %d ", curl->GetLeftB() );
        fprintf( file, "    rightB %d \n", curl->GetRightB() );
        fprintf( file, "    or %d ", curl->IsMarked( BOOL_OR ) );
        fprintf( file, "    and %d " , curl->IsMarked( BOOL_AND ) );
        fprintf( file, "    exor %d " , curl->IsMarked( BOOL_EXOR ) );
        fprintf( file, "    a_sub_b %d " , curl->IsMarked( BOOL_A_SUB_B ) );
        fprintf( file, "    b_sub_a %d " , curl->IsMarked( BOOL_B_SUB_A ) );
        fprintf( file, "    hole %d " , curl->GetHole() );
        fprintf( file, "    top_hole %d \n" , curl->IsTopHole() );

        _LI++;
    }
2409 2410 2411

#endif

2412
    return true;
2413 2414
}

charras's avatar
charras committed
2415
bool kbGraph::writeintersections()
2416 2417 2418 2419
{

#if KBOOL_DEBUG == 1

2420
    FILE * file = _GC->GetLogFile();
2421

2422 2423
    if ( file == NULL )
        return true;
2424

2425
    fprintf( file, "# graph\n" );
2426

charras's avatar
charras committed
2427
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
2428 2429 2430 2431
    if ( _LI.empty() )
    {
        return true;
    }
2432

2433 2434 2435
    _LI.tohead();
    while( !_LI.hitroot() )
    {
charras's avatar
charras committed
2436 2437
        kbLink * curl = _LI.item();
        TDLI<kbLink> Inode( curl->GetBeginNode()->GetLinklist() );
2438 2439 2440 2441 2442 2443 2444 2445
        Inode.tohead();
        if ( Inode.count() > 2 )
        {
            fprintf( file, " count %I64d", Inode.count() );
            fprintf( file, " b %I64d %I64d \n\n", curl->GetBeginNode()->GetX() , curl->GetBeginNode()->GetY() );
        }
        _LI++;
    }
2446 2447
#endif

2448
    return true;
2449 2450
}

charras's avatar
charras committed
2451
bool kbGraph::checksort()
2452
{
2453 2454 2455 2456
    // if empty then just insert
    if ( _linklist->empty() )
        return true;

charras's avatar
charras committed
2457
    TDLI<kbLink> _LI = TDLI<kbLink>( _linklist );
2458 2459
    // put new item left of the one that is bigger
    _LI.tohead();
charras's avatar
charras committed
2460 2461
    kbLink* prev = _LI.item();
    kbLink* cur = _LI.item();
2462 2463 2464
    _LI++;
    while( !_LI.hitroot() )
    {
charras's avatar
charras committed
2465
        kbLink * aap = _LI.item();
2466 2467 2468
        if ( linkXYsorter( prev, _LI.item() ) == -1 )
        {
            cur = aap;
2469

2470 2471 2472 2473 2474 2475
            return false;
        }
        prev = _LI.item();
        _LI++;
    }
    return true;
2476 2477 2478
}


charras's avatar
charras committed
2479
void kbGraph::WriteKEY( Bool_Engine* GC, FILE* file )
2480
{
2481
    double scale = 1.0 / GC->GetGrid() / GC->GetGrid();
2482

2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
    bool ownfile = false;
    if ( !file )
    {
        file = fopen( "keyfile.key", "w" );
        ownfile = true;

        fprintf( file, "\
                 HEADER 5; \
                 BGNLIB; \
                 LASTMOD {2-11-15  15:39:21}; \
                 LASTACC {2-11-15  15:39:21}; \
                 LIBNAME trial; \
                 UNITS; \
                 USERUNITS 0.0001; PHYSUNITS 1e-009; \
                 \
                 BGNSTR;  \
                 CREATION {2-11-15  15:39:21}; \
                 LASTMOD  {2-11-15  15:39:21}; \
                 STRNAME top; \
                 ");
    }
2504

charras's avatar
charras committed
2505
    TDLI<kbLink> _LI=TDLI<kbLink>(_linklist);
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
    if (_LI.empty())
    {
        if ( ownfile )
        {
            fprintf(file,"\
                    ENDSTR top; \
                    ENDLIB; \
                    ");
            fclose (file);

        }
        return;
    }
2519 2520


2521
    _LI.tohead();
charras's avatar
charras committed
2522
    kbLink* curl = _LI.item();
2523

2524 2525 2526 2527 2528
    if ( _LI.item()->Group() == GROUP_A )
        fprintf(file,"BOUNDARY; LAYER 0;  DATATYPE 0;\n");
    else
        fprintf(file,"BOUNDARY; LAYER 1;  DATATYPE 0;\n");
    fprintf(file," XY % d; \n", _LI.count()+1 );
2529

2530 2531 2532 2533 2534
    double firstx = curl->GetBeginNode()->GetX()*scale;
    double firsty = curl->GetBeginNode()->GetY()*scale;
    fprintf(file,"X % f;\t", firstx);
    fprintf(file,"Y % f; \n", firsty);
    _LI++;
2535

2536 2537
    while(!_LI.hitroot())
    {
charras's avatar
charras committed
2538
        kbLink* curl = _LI.item();
2539

2540 2541
        fprintf(file,"X % f;\t", curl->GetBeginNode()->GetX()*scale);
        fprintf(file,"Y % f; \n", curl->GetBeginNode()->GetY()*scale);
2542

2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
        _LI++;
    }
    fprintf(file,"X % f;\t", firstx);
    fprintf(file,"Y % f; \n", firsty);
    fprintf(file,"ENDEL;\n");

    if ( ownfile )
    {
        fprintf(file,"\
                ENDSTR top; \
                ENDLIB; \
                ");
        fclose (file);

    }
2558
}
2559

2560

charras's avatar
charras committed
2561
void kbGraph::WriteGraphKEY(Bool_Engine* GC)
2562
{
charras's avatar
charras committed
2563
#if KBOOL_DEBUG
2564

2565
    double scale = 1.0/GC->GetGrid()/GC->GetGrid();
2566

2567
    FILE* file = fopen("keygraphfile.key", "w");
2568

2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
    fprintf(file,"\
            HEADER 5; \
            BGNLIB; \
            LASTMOD {2 - 11 - 15  15: 39: 21}; \
            LASTACC {2 - 11 - 15  15: 39: 21}; \
            LIBNAME trial; \
            UNITS; \
            USERUNITS 1; PHYSUNITS 1e-006; \
            \
            BGNSTR;  \
            CREATION {2 - 11 - 15  15: 39: 21}; \
            LASTMOD  {2 - 11 - 15  15: 39: 21}; \
            STRNAME top; \
            ");
2583 2584


charras's avatar
charras committed
2585
    TDLI<kbLink> _LI=TDLI<kbLink>(_linklist);
2586 2587 2588 2589 2590 2591 2592 2593 2594
    if (_LI.empty())
    {
        fprintf(file,"\
                ENDSTR top; \
                ENDLIB; \
                ");
        fclose (file);
        return;
    }
2595

2596
    _LI.tohead();
charras's avatar
charras committed
2597
    kbLink* curl;
2598

2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
    int _nr_of_points = _linklist->count();
    for (int i = 0; i < _nr_of_points; i++)
    {
        curl = _LI.item();

        if ( curl->Group() == GROUP_A )
            fprintf(file,"PATH; LAYER 0;\n");
        else
            fprintf(file,"PATH; LAYER 1;\n");

        fprintf(file," XY % d; \n", 2 );
        fprintf(file,"X % f;\t", curl->GetBeginNode()->GetX()*scale);
        fprintf(file,"Y % f; \n", curl->GetBeginNode()->GetY()*scale);
        fprintf(file,"X % f;\t", curl->GetEndNode()->GetX()*scale);
        fprintf(file,"Y % f; \n", curl->GetEndNode()->GetY()*scale);
        _LI++;
        fprintf(file,"ENDEL;\n");
    }
2617

2618 2619 2620 2621 2622 2623
    fprintf(file,"\
            ENDSTR top; \
            ENDLIB; \
            ");

    fclose (file);
charras's avatar
charras committed
2624
#endif
2625 2626 2627 2628 2629
}