scanbeam.cpp 49 KB
Newer Older
1 2 3 4 5 6 7
/*! \file src/scanbeam.cpp
    \author Klaas Holwerda or Julian Smart
 
    Copyright: 2001-2004 (C) Klaas Holwerda 
 
    Licence: see kboollicense.txt 
 
8
    RCS-ID: $Id: scanbeam.cpp,v 1.7 2009/09/14 16:50:12 titato Exp $
9 10 11 12
*/

// class scanbeam
// this class represents de space between two scanlines
13
#include "kbool/scanbeam.h"
14 15 16
#include <math.h>
#include <assert.h>

17
#include "kbool/booleng.h"
18

19 20
#include "kbool/graph.h"
#include "kbool/node.h"
21 22 23 24

//this here is to initialize the static iterator of scanbeam
//with NOLIST constructor

charras's avatar
charras committed
25
int recordsorter( kbRecord* , kbRecord* );
26

charras's avatar
charras committed
27 28
int recordsorter_ysp_angle( kbRecord* , kbRecord* );
int recordsorter_ysp_angle_back( kbRecord* rec1, kbRecord* rec2 );
29

charras's avatar
charras committed
30
ScanBeam::ScanBeam( Bool_Engine* GC ): DL_List<kbRecord*>()
31
{
32 33 34
    _GC = GC;
    _type = NORMAL;
    _BI.Attach( this );
35 36 37 38
}

ScanBeam::~ScanBeam()
{
39 40 41
    //first delete all record still in the beam
    _BI.Detach();
    remove_all( true );
42

43
    //DeleteRecordPool();
44 45
}

charras's avatar
charras committed
46
void ScanBeam::SetType( kbNode* low, kbNode* high )
47
{
48 49 50 51
    if ( low->GetX() < high->GetX() )
        _type = NORMAL;
    else
        _type = FLAT;
52 53 54 55 56
}

/*
//catch node to link crossings
// must be sorted on ysp
charras's avatar
charras committed
57
int ScanBeam::FindCloseLinksAndCross(TDLI<kbLink>* _I,kbNode* _lowf)
58
{
59
 int merges = 0;
charras's avatar
charras committed
60
 kbRecord* record;
61
 
charras's avatar
charras committed
62
   TDLI<kbRecord> _BBI=TDLI<kbRecord>(this);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 
 if (_BI.count() > 1)
 {
  //first search a link towards this node
  for(_BI.tohead(); !_BI.hitroot(); _BI++)
  {
   record=_BI.item();
   if( (record->GetLink()->GetBeginNode()==_lowf) ||
     (record->GetLink()->GetEndNode()  ==_lowf)
     )
     break;
  }
 
  //NOTICE if the node "a_node" is not inside a record
  //for instance to connected flat links (flatlinks not in beam)
  //then IL will be at end    (those will be catched at 90 degrees rotation)
  if (_BI.hitroot())
80
      {
81
   return(merges);
82
      }
83
 
84 85 86 87 88 89
      //from IL search back for close links
      _BBI.toiter(&_BI);
      _BBI--;
      while(!_BBI.hitroot())
      {
         record=_BBI.item();
90
 
91 92
         if (record->Ysp() != _lowf->GetY())
            break;
93
 
94 95 96 97 98 99 100 101 102 103 104
         // the distance to the low node is smaller then the MARGE
         if( (record->GetLink()->GetBeginNode()!=_lowf) &&
             (record->GetLink()->GetEndNode()  !=_lowf)
           )
         {  // the link is not towards the low node
            record->GetLink()->AddCrossing(_lowf);
            record->SetNewLink(record->GetLink()->ProcessCrossingsSmart(_I));
            merges++;
         }
         _BBI--;
      }
105
 
106 107 108 109 110 111
      //from IL search forward for close links
      _BBI.toiter(&_BI);
      _BBI++;
      while(!_BBI.hitroot())
      {
         record=_BBI.item();
112
 
113 114 115
         if (record->Ysp() != _lowf->GetY())
//         if (record->Ysp() < _lowf->GetY()-MARGE)
            break;
116
 
117 118 119 120 121 122 123 124 125 126 127 128
         // the distance to the low node is smaller then the MARGE
         if( (record->GetLink()->GetBeginNode()!=_lowf) &&
             (record->GetLink()->GetEndNode()  !=_lowf)
           )
         {  // the link is not towards the low node
            record->GetLink()->AddCrossing(_lowf);
            record->SetNewLink(record->GetLink()->ProcessCrossingsSmart(_I));
            merges++;
         }
         _BBI++;
      }
   }
129
 return merges;
130 131 132 133
}
*/

/*
charras's avatar
charras committed
134
bool  ScanBeam::Update(TDLI<kbLink>* _I,kbNode* _lowf)
135
{
136
 bool found=false;
charras's avatar
charras committed
137
 kbLink* link;
138 139 140 141 142
 
 _BI.tohead();
 while (!_BI.hitroot())
 {
 
charras's avatar
charras committed
143
  kbRecord* record=_BI.item();
144 145 146 147
  record->Calc_Ysp(_type,_low);
  _BI++;
 }
 
148
   FindCloseLinksAndCross(_I,_lowf);
149
 
150 151 152
   _BI.tohead();
   while (!_BI.hitroot())
   {
charras's avatar
charras committed
153
      kbRecord* record=_BI.item();
154 155
      //records containing links towards the new low node
      //are links to be marked for removal
156
 
157 158 159 160 161 162 163
      if ((record->GetLink()->GetEndNode() == _lowf) ||
          (record->GetLink()->GetBeginNode() == _lowf)
         )
      {
         //cross here the links that meat eachother now
         delete _BI.item();
         _BI.remove();
164
 
165 166 167 168
         //cross here the links that meat eachother now
         _BI--;
         if (!_BI.hitroot() && (_BI.count() > 1))
         {
charras's avatar
charras committed
169
            kbRecord* prev=_BI.item();
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
            _BI++;
            if (!_BI.hitroot())
            {
               if (!_BI.item()->Equal(prev)) // records NOT parallel
                  if (_BI.item()->GetLine()->Intersect(prev->GetLine(),MARGE))
                  {
                     //they did cross, integrate the crossings in the graph
                     //this  may modify the links already part of the record
                     //this is why they are returned in set for the record
                     _BI.item()->SetNewLink(_BI.item()->GetLink()->ProcessCrossingsSmart(_I));
                     prev->SetNewLink(prev->GetLink()->ProcessCrossingsSmart(_I));
                  }
            }
         }
         else
185
           _BI++;
186 187 188 189
      }
      else
         _BI++;
   }
190 191 192
 
 //writebeam();
 
193
   //ONLY links towards the low node are possible to be added
194 195 196 197
 //the bin flag will be set if it fits in the beam
 //so for following beams it will not be checked again
 while ( bool(link=_lowf->GetBinHighest(false)) )
 {
charras's avatar
charras committed
198
      kbRecord* record=new kbRecord(link);
199 200 201 202 203 204 205 206
      // yp_new will always be the y of low node since all new links are
      // from this node
      record->SetYsp(_lowf->GetY());
      record->Set_Flags(_type);
      //need to calculate ysn to be able to sort this record in the right order
      //this is only used when the insert node is equal for both records
      // ins_smart and cross neighbour  directly
      // if empty then just insert
207
 
208 209 210 211 212 213 214 215 216 217 218 219
      if (empty())
         insend(record);
      else
      {
         // put new item left of the one that is bigger
         _BI.tohead();
         while(!_BI.hitroot())
         {
            if (recordsorter_ysp_angle(record,_BI.item())==1)
               break;
            _BI++;
         }
220
 
221 222 223 224
         _BI.insbefore(record);
         _BI--;_BI--; //just before the new record inserted
         if (!_BI.hitroot())
         {
charras's avatar
charras committed
225
            kbRecord* prev=_BI.item();
226 227 228 229 230 231 232 233 234 235 236 237 238 239
            _BI++; //goto the new record inserted
            if (!_BI.item()->Equal(prev)) // records NOT parallel
            {
               if (_BI.item()->GetLine()->Intersect(prev->GetLine(),MARGE))
               {
                  //this  may modify the links already part of the record
                  //this is why they are returned in set for the record
                  _BI.item()->SetNewLink(_BI.item()->GetLink()->ProcessCrossingsSmart(_I));
                  prev->SetNewLink(prev->GetLink()->ProcessCrossingsSmart(_I));
               }
            }
         }
         else
           _BI++;
240
 
charras's avatar
charras committed
241
         kbRecord* prev=_BI.item(); //the new record
242 243 244
         _BI++;
         if (!_BI.hitroot() && !_BI.item()->Equal(prev)) // records NOT parallel
         {
charras's avatar
charras committed
245
          kbRecord* cur=_BI.item();
246 247 248 249 250 251 252 253 254 255 256 257
            if (cur->GetLine()->Intersect(prev->GetLine(),MARGE))
            {
               //this  may modify the links already part of the record
               //this is why they are returned in set for the record
               cur->SetNewLink(cur->GetLink()->ProcessCrossingsSmart(_I));
               prev->SetNewLink(prev->GetLink()->ProcessCrossingsSmart(_I));
            }
         }
      }
      //remember this to calculate in/out values for each new link its polygon again.
      GNI->insend(record->GetLink()->GetGraphNum());
      found=true;
258
    record->GetLink()->SetBeenHere();
259
   }
260
 
261
   FindCloseLinksAndCross(_I,_lowf);
262 263
 //writebeam();
 return(found);
264 265 266
}
*/

charras's avatar
charras committed
267
bool ScanBeam::FindNew( SCANTYPE scantype, TDLI<kbLink>* _I, bool& holes )
268
{
269
    bool foundnew = false;
270

271
    _low = _I->item()->GetBeginNode();
272

charras's avatar
charras committed
273
    kbLink* link;
274

275 276
    //if (!checksort())
    // SortTheBeam();
277

278 279 280 281 282 283 284 285 286 287 288
    lastinserted = 0;
    //ONLY links towards the low node are possible to be added
    //the bin flag will be set if it fits in the beam
    //so for following beams it will not be checked again
    while ( ( link = _low->GetBinHighest( false ) ) != NULL )
    {
        if ( ( link->GetEndNode()->GetX() == link->GetBeginNode()->GetX() ) //flatlink in flatbeam
                && ( ( scantype == NODELINK ) || ( scantype == LINKLINK ) || ( scantype == LINKHOLES ) )
           )
        {
            switch( scantype )
289
            {
290 291 292 293
                case NODELINK:
                {
                    //all vertical links in flatbeam are ignored
                    //normal link in beam
charras's avatar
charras committed
294
                    kbRecord * record = new kbRecord( link, _GC );
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
                    // yp_new will always be the y of low node since all new links are
                    // from this node
                    record->SetYsp( _low->GetY() );
                    record->Set_Flags();
                    // put new item left of the one that is lower in the beam
                    // The last one inserted in this loop, is already left of the current
                    // iterator position. So the new links are inerted in proper order.
                    link->SetRecordNode( _BI.insbefore( record ) );
                    _BI--;
                    foundnew = Process_PointToLink_Crossings() != 0 || foundnew;
                    delete record;
                    _BI.remove();
                    break;
                }
                case LINKLINK:
                    //is the new record a flat link
                {
charras's avatar
charras committed
312
                    kbLine flatline = kbLine( link, _GC );
313 314 315 316 317 318 319 320 321 322 323 324
                    foundnew = Process_LinkToLink_Flat( &flatline ) || foundnew;
                    //flatlinks are not part of the beams, still they are used to find new beams
                    //they can be processed now if the beginnode does not change, since this is used to
                    //to find new beams. and its position does not change
                    //ProcessCrossings does take care of this
                    flatline.ProcessCrossings( _I );
                    break;
                }
                case LINKHOLES : //holes are never to flatlinks
                    assert( true );
                default:
                    break;
325
            }
326 327 328 329
        }
        else
        {
            //normal link in beam
charras's avatar
charras committed
330
            kbRecord* record = new kbRecord( link, _GC );
331 332 333 334 335 336 337 338 339 340 341 342 343 344
            // yp_new will always be the y of low node since all new links are
            // from this node
            record->SetYsp( _low->GetY() );
            record->Set_Flags();
            // put new item left of the one that is lower in the beam
            // The last one inserted in this loop, is already left of the current
            // iterator position. So the new links are inserted in proper order.
            link->SetRecordNode( _BI.insbefore( record ) );
            lastinserted++;

            //_GC->Write_Log( "after insert" );
            writebeam();

            switch( scantype )
345
            {
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
                case NODELINK:
                    _BI--;
                    foundnew = Process_PointToLink_Crossings() != 0  || foundnew;
                    _BI++;
                    break;
                case INOUT:
                {
                    _BI--;
                    //now we can set the _inc flag
                    Generate_INOUT( record->GetLink()->GetGraphNum() );
                    _BI++;
                }
                break;
                case GENLR:
                {
                    //now we can set the a/b group flags based on the above link
                    _BI--;
                    _BI--;
charras's avatar
charras committed
364
                    kbRecord* above = 0;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
                    if ( !_BI.hitroot() )
                        above = _BI.item();
                    _BI++;

                    //something to do for winding rule

                    if ( record->Calc_Left_Right( above ) )
                    {
                        delete record;
                        _BI.remove();
                        lastinserted--;
                    }
                    else
                        _BI++;
                }
                break;
                case LINKHOLES:
                    _BI--;
                    holes = ProcessHoles( true, _I ) || holes;
                    _BI++;
                    break;

                default:
                    break;
389
            }
390 391 392
        }
        link->SetBeenHere();
    }
393

394
    writebeam();
395

396
    return foundnew;
397 398
}

charras's avatar
charras committed
399
bool ScanBeam::RemoveOld( SCANTYPE scantype, TDLI<kbLink>* _I, bool& holes )
400
{
401 402
    bool found = false;
    bool foundnew = false;
charras's avatar
charras committed
403
    DL_Iter<kbRecord*>  _BBI = DL_Iter<kbRecord*>();
404 405 406 407 408 409 410 411 412

    _low = _I->item()->GetBeginNode();

    switch( scantype )
    {
        case INOUT:
        case GENLR:
        case LINKHOLES:
            if ( _type == NORMAL )
413
            {
414 415 416 417
                if ( _low->GetBinHighest( true ) ) //is there something to remove
                {
                    if ( scantype == LINKHOLES )
                    {
charras's avatar
charras committed
418 419 420 421
                        // Tophole links can be linked at the begin or end point, depending on
                        // which is higher in Y.
                        // A link pointing to the low node, and which is a tophole link,
                        // and which was not linked in sofar should be linked now.
422 423 424
                        _BI.tohead();
                        while ( !_BI.hitroot() )
                        {
charras's avatar
charras committed
425
                            kbRecord * record = _BI.item();
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
                            //records containing links towards the new low node
                            //are links to be removed
                            if ( ( record->GetLink()->GetEndNode() == _low ) ||
                                    ( record->GetLink()->GetBeginNode() == _low )
                               )
                            {
                                holes = ProcessHoles( false, _I ) || holes;
                            }
                            _BI++;
                        }
                    }

                    _BI.tohead();
                    while ( !_BI.hitroot() )
                    {
charras's avatar
charras committed
441
                        kbRecord * record = _BI.item();
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
                        //records containing links towards the new low node
                        //are links to be removed
                        if ( ( record->GetLink()->GetEndNode() == _low ) ||
                                ( record->GetLink()->GetBeginNode() == _low )
                           )
                        {
                            delete _BI.item();
                            _BI.remove();
                            found = true;
                        }
                        else
                        {
                            //recalculate ysp for the new scanline
                            record->Calc_Ysp( _low );
                            _BI++;
                        }
                    }

charras's avatar
charras committed
460 461 462
                    // all records are renewed in Ysp.
                    // found links to remove, we search the new insert position for new links.
                    if ( found )
463
                    {
charras's avatar
charras committed
464 465 466 467 468 469 470 471 472 473
                        _BI.tohead();
                        while ( !_BI.hitroot() )
                        {
                            kbRecord * record = _BI.item();
                            if (  record->Ysp() < _low->GetY() )
                            {
                                break;
                            }
                            _BI++;
                        }
474 475 476 477
                    }
                }
                else
                {
charras's avatar
charras committed
478 479 480 481 482
                    // nothing is removed from the beam, still we moved forward with the scanline
                    // at the new _low, so we need to recalculate the intersections of the links 
                    // with the new scanline.
                    // Also the the insert position for new links is determined, being the first
                    // link below _low.
483 484 485 486 487
                    _BBI.Attach( this );
                    _BBI.toroot();
                    _BI.tohead();
                    while ( !_BI.hitroot() )
                    {
charras's avatar
charras committed
488
                        kbRecord * record = _BI.item();
489 490 491 492 493 494 495 496 497 498 499 500

                        record->Calc_Ysp( _low );
                        if ( !found && ( record->Ysp() < _low->GetY() ) )
                        {
                            found = true;
                            _BBI.toiter( &_BI );
                        }
                        _BI++;
                    }
                    _BI.toiter( &_BBI );
                    _BBI.Detach();
                }
501
            }
charras's avatar
charras committed
502
            else // _type == NORMAL
503 504 505 506 507 508 509 510 511 512
            {  //because the previous beam was flat the links to remove are
                //below the last insert position
                if ( _low->GetBinHighest( true ) ) //is there something to remove
                {

                    if ( scantype == LINKHOLES )
                    {
                        _BI.tohead();
                        while ( !_BI.hitroot() )
                        {
charras's avatar
charras committed
513
                            kbRecord * record = _BI.item();
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
                            //records containing links towards the new low node
                            //are links to be removed
                            if ( ( record->GetLink()->GetEndNode() == _low ) ||
                                    ( record->GetLink()->GetBeginNode() == _low )
                               )
                            {
                                holes = ProcessHoles( false, _I ) || holes;
                            }
                            _BI++;
                        }
                    }

                    //on record back bring us to the last inserted record
                    //or if nothing was inserted the record before the last deleted record
                    //if there was no record before the last deleted record this means
                    //we where at the beginning of the beam, so at root

                    _BI.tohead();
                    while ( !_BI.hitroot() )
                    {
charras's avatar
charras committed
534
                        kbRecord * record = _BI.item();
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
                        //records containing links towards the new low node
                        //are links to be removed
                        if ( ( record->GetLink()->GetEndNode() == _low ) ||
                                ( record->GetLink()->GetBeginNode() == _low )
                           )
                        {
                            delete _BI.item();
                            _BI.remove();
                            found = true;
                        }
                        else if ( found ) //only once in here
                            break;
                        else if ( record->Ysp() < _low->GetY() )
                            //if flatlinks are not in the beam nothing will be found
                            //this will bring us to the right insertion point
                            break;
                        else
                            _BI++;
                    }
                }
                else
                {
                    //on record back bring us to the last inserted record
                    //or if nothing was inserted the record before the last deleted record
                    //if there was no record before the last deleted record this means
                    //we where at the beginning of the beam, so at root

                    _BI.tohead();
                    while ( !_BI.hitroot() )
                    {
charras's avatar
charras committed
565
                        kbRecord * record = _BI.item();
566 567 568 569 570
                        if ( record->Ysp() < _low->GetY() )
                            break;
                        _BI++;
                    }
                }
571
            }
572
            break;
573

574 575 576 577
        case NODELINK:
        case LINKLINK:
        {
            if ( _type == NORMAL )
578
            {
579 580 581 582 583
                Calc_Ysp();
                if ( scantype == LINKLINK )
                    foundnew = Process_LinkToLink_Crossings() != 0 || foundnew;
                else
                    SortTheBeam( false );
584
            }
585 586 587
            //else beam is already sorted because the added/removed flat links
            //do not change the ysp of links already there, new non flat links
            //are inserted in order, as result the beam stays sorted
588

589
            if ( _low->GetBinHighest( true ) ) //is there something to remove
590
            {
591 592 593
                _BI.tohead();
                while ( !_BI.hitroot() )
                {
charras's avatar
charras committed
594
                    kbRecord * record = _BI.item();
595 596 597 598 599 600
                    //records containing links towards the new low node
                    //are links to be removed
                    if ( ( record->GetLink()->GetEndNode() == _low ) ||
                            ( record->GetLink()->GetBeginNode() == _low )
                       )
                    {
charras's avatar
charras committed
601
                        kbLine * line = record->GetLine();
602 603 604 605 606 607 608 609 610 611 612 613 614 615
                        if ( scantype == NODELINK )
                            foundnew = Process_PointToLink_Crossings() != 0 || foundnew;
                        line->ProcessCrossings( _I );
                        delete _BI.item();
                        _BI.remove();
                        found = true;
                    }
                    //because the beam is sorted on ysp, stop when nothing can be there to remove
                    //and the right insertion point for new links has been found
                    else if ( ( record->Ysp() < _low->GetY() ) )
                        break;
                    else
                        _BI++;
                }
616 617 618
            }
            else
            {
619 620 621
                _BI.tohead();
                while ( !_BI.hitroot() )
                {
charras's avatar
charras committed
622
                    kbRecord * record = _BI.item();
623 624 625 626 627 628
                    //because the beam is sorted on ysp, stop when
                    //the right insertion point for new links has been found
                    if ( ( record->Ysp() < _low->GetY() ) )
                        break;
                    _BI++;
                }
629
            }
630 631
        }
        break;
632

633 634 635
        default:
            break;
    }
636

637
    return foundnew;
638 639
}
/*
charras's avatar
charras committed
640
bool ScanBeam::RemoveOld(SCANTYPE scantype,TDLI<kbLink>* _I, bool& holes )
641
{
642 643
 bool found = false;
 bool foundnew = false;
charras's avatar
charras committed
644
   DL_Iter<kbRecord*>  _BBI=DL_Iter<kbRecord*>();
645 646
 bool attached=false;
 
647
   _low = _I->item()->GetBeginNode();
648
 
649 650 651 652 653 654 655
   switch(scantype)
   {
      case INOUT:
      case GENLR:
      case LINKHOLES:
      if (_type==NORMAL )
      {
charras's avatar
charras committed
656
         kbLink* link = _low->GetBinHighest(true);
657 658 659
         if ( link ) //is there something to remove
         {
            link->SetRecordNode( NULL );
660
 
661 662 663 664 665
            if ( scantype == LINKHOLES )
            {
               _BI.tohead();
               while (!_BI.hitroot())
               {
charras's avatar
charras committed
666
                  kbRecord* record = _BI.item();
667 668 669 670 671 672 673 674 675 676 677
                  //records containing links towards the new low node
                  //are links to be removed
                  if ((record->GetLink()->GetEndNode() == _low) ||
                      (record->GetLink()->GetBeginNode() == _low)
                     )
                  {
                     holes = ProcessHoles(false,_I) || holes;
                  }
                  _BI++;
               }
            }
678
 
679 680 681
            _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
682
               kbRecord* record=_BI.item();
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
               //records containing links towards the new low node
               //are links to be removed
               if ((record->GetLink()->GetEndNode() == _low) ||
                   (record->GetLink()->GetBeginNode() == _low)
                  )
               {
                  delete _BI.item();
                  _BI.remove();
                  found=true;
               }
               else if (found) //only once in here
               {
                  attached=true;
                  found=false;
                  //recalculate ysp for the new scanline
                  record->Calc_Ysp(_low);
                  _BI++;
               }
               else
               {
                  //recalculate ysp for the new scanline
                  record->Calc_Ysp(_low);
                  _BI++;
               }
            }
         }
         else
         {
            _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
714
                  kbRecord* record=_BI.item();
715 716 717 718 719 720 721 722
                  record->Calc_Ysp(_low);
                  _BI++;
            }
         }
      }
      else
      {  //because the previous beam was flat the links to remove are
         //below the last insert position
charras's avatar
charras committed
723
         kbLink* link;
724 725 726 727
         link = _low->GetBinHighest(true);
         if( link  )//is there something to remove
         {
            link->SetRecordNode( NULL );
728
 
729 730 731 732
            bool linkf = false;
            _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
733
               kbRecord* record = _BI.item();
734 735 736 737
               if (record->GetLink() == link) 
                  linkf = true;
               _BI++;
            }
738
 
739 740
            if ( !linkf )
               _BI.tohead();
741 742
 
 
743 744 745 746 747
            if ( scantype == LINKHOLES )
            {
               _BI.tohead();
               while (!_BI.hitroot())
               {
charras's avatar
charras committed
748
                  kbRecord* record=_BI.item();
749 750 751 752 753 754 755 756 757 758 759
                  //records containing links towards the new low node
                  //are links to be removed
                  if ((record->GetLink()->GetEndNode() == _low) ||
                      (record->GetLink()->GetBeginNode() == _low)
                     )
                  {
                     holes = ProcessHoles(false,_I) || holes;
                  }
                  _BI++;
               }
            }
760
 
761 762 763
            //_BI.tonode( link->GetRecordNode() );
            //delete _BI.item();
            //_BI.remove();
764
 
765 766 767 768
            //on record back bring us to the last inserted record
            //or if nothing was inserted the record before the last deleted record
            //if there was no record before the last deleted record this means
            //we where at the beginning of the beam, so at root
769
 
770 771 772
            //_BI << (lastinserted+1);
            //_BI--;
            //if (_BI.hitroot())  //only possible when at the begin of the beam
773
 
774
            //found=false;
775
 
776 777 778
            _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
779
               kbRecord* record=_BI.item();
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
               //records containing links towards the new low node
               //are links to be removed
               if ((record->GetLink()->GetEndNode() == _low) ||
                   (record->GetLink()->GetBeginNode() == _low)
                  )
               {
                  if ( link != record->GetLink() )
                  {
                     break;
                  }
                  if ( link->GetRecordNode() != _BI.node() )
                  {
                     delete _BI.item();
                     _BI.remove();
                  }
                  else
                  {
                     delete _BI.item();
                     _BI.remove();
                  }
                  found=true;
               }
               else if (found) //only once in here
                  break;
               else if (record->Ysp() < _low->GetY())
                     //if flatlinks are not in the beam nothing will be found
                     //this will bring us to the right insertion point
                     break;
               else
                  _BI++;
            }
811
 
812
         }
813
 
814 815
         else
         {
816
 
817 818 819 820
            //on record back bring us to the last inserted record
            //or if nothing was inserted the record before the last deleted record
            //if there was no record before the last deleted record this means
            //we where at the beginning of the beam, so at root
821
 
822 823 824 825 826 827
            //_BI << (lastinserted+ 1);
            //_BI--;
            //if (_BI.hitroot())  //only possible when at the begin of the beam
               _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
828
                  kbRecord* record=_BI.item();
829 830 831 832
                  if (record->Ysp() < _low->GetY())
                     break;
                  _BI++;
            }
833
 
834 835 836
         }
      }
      break;
837
 
838 839 840 841 842 843 844 845 846 847 848 849 850 851
      case NODELINK:
      case LINKLINK:
      {
         if (_type == NORMAL)
         {
            Calc_Ysp();
            if (scantype==LINKLINK)
               foundnew = Process_LinkToLink_Crossings() !=0 || foundnew;
            else
               SortTheBeam( false );
         }
         //else beam is already sorted because the added/removed flat links
         //do not change the ysp of links already there, new non flat links
         //are inserted in order, as result the beam stays sorted
852
 
853 854 855 856 857
         if (_low->GetBinHighest(true)) //is there something to remove
         {
            _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
858
               kbRecord* record=_BI.item();
859 860 861 862 863 864
               //records containing links towards the new low node
               //are links to be removed
               if ((record->GetLink()->GetEndNode() == _low) ||
                   (record->GetLink()->GetBeginNode() == _low)
                  )
               {
charras's avatar
charras committed
865
                  kbLine* line=record->GetLine();
866
                  if (scantype==NODELINK)
867
                   foundnew = Process_PointToLink_Crossings() !=0 || foundnew;
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
                  line->ProcessCrossings(_I);
                  delete _BI.item();
                  _BI.remove();
                  found=true;
               }
               //because the beam is sorted on ysp, stop when nothing can be there to remove
               //and the right insertion point for new links has been found
               else if ((record->Ysp() < _low->GetY()))
                  break;
               else
                  _BI++;
            }
         }
         else
         {
            _BI.tohead();
            while (!_BI.hitroot())
            {
charras's avatar
charras committed
886
                  kbRecord* record=_BI.item();
887 888 889 890 891 892 893 894 895
                  //because the beam is sorted on ysp, stop when
                  //the right insertion point for new links has been found
                  if ((record->Ysp() < _low->GetY()))
                     break;
                  _BI++;
            }
         }
      }
      break;
896
 
897 898 899
      default: 
         break; 
   }
900 901
 
 return foundnew;
902 903 904 905 906
}
*/

void ScanBeam::SortTheBeam( bool backangle )
{
907 908 909 910
    if ( backangle )
        _BI.mergesort( recordsorter_ysp_angle_back );
    else
        _BI.mergesort( recordsorter_ysp_angle );
911 912
}

913
void ScanBeam::Calc_Ysp()
914
{
915 916 917
    _BI.tohead();
    while ( !_BI.hitroot() )
    {
charras's avatar
charras committed
918 919
        kbRecord * record = _BI.item();
//  kbLink* link=_BI.item()->GetLink();
920 921 922
        record->Calc_Ysp( _low );
        _BI++;
    }
923 924 925 926 927
}

// this function will set for all the records which contain a link with the
// corresponding graphnumber the inc flag.
// The inc flag's function is to see in a beam if we go deeper in the graph or not
928
void ScanBeam::Generate_INOUT( int graphnumber )
929
{
930 931 932
    DIRECTION first_dir = GO_LEFT;
    int diepte          = 0;

charras's avatar
charras committed
933
    DL_Iter<kbRecord*> _BBI = DL_Iter<kbRecord*>();
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
    _BBI.Attach( this );
    for( _BBI.tohead(); !_BBI.hitroot(); _BBI++ )
    {
        // recalculate _inc again
        if ( _BBI.item()->GetLink()->GetGraphNum() == graphnumber )
        { //found a link that belongs to the graph
            if ( diepte == 0 )
            { // first link found or at depth zero again
                // the direction is important since this is used to find out
                // if we go further in or out for coming links
                first_dir = _BBI.item()->Direction();
                _BBI.item()->GetLink()->SetInc( true );
                diepte = 1;
            }
            else
            { // according to depth=1 links set depth
                // verhoog of verlaag diepte
                if ( _BBI.item()->Direction() == first_dir )
                {
                    diepte++;
                    _BBI.item()->GetLink()->SetInc( true );
                }
                else
                {
                    diepte--;
                    _BBI.item()->GetLink()->SetInc( false );
                }
            }
        }
        if ( _BBI.item() == _BI.item() ) break; //not need to do the rest, will come in a later beam
    }
    _BBI.Detach();
966 967 968 969 970 971 972
}


// function ProcessHoles
//
// this function will search the closest link to a hole
// step one, search for a link that is marked (this is a hole)
973
// step two, the closest link is the previous link in
974 975 976 977
//     the beam, but only in the beam that contains the highest node
//     from the marked link.
//     why ? : if the marked link has for the begin and end node different
//     x,y values, see below as link C
978 979 980
//                                 B
//               A            +---------+
//          +----------+
981 982 983
//                     ___--+
//                  ___---
//                +---    C
984
//
985 986 987 988 989 990 991
//     when we at first detect link C we would link it to link A, should work he
//     but; we always link a hole at its topleft node, so the highest node
//     and then we can't link to A but we should link to B
//     so when we found the link, we will look if the node that will come
//     in a later beam will be higher than the current, if so we will wait
//     till that node comes around otherwise we will link this node to the
//     closest link (prev in beam)
charras's avatar
charras committed
992
bool ScanBeam::ProcessHoles( bool atinsert, TDLI<kbLink>* _LI )
993
{
994
    // The scanbeam must already be sorted at this moment
charras's avatar
charras committed
995
    kbNode * topnode;
996 997
    bool foundholes = false;

charras's avatar
charras committed
998 999
    kbRecord* record = _BI.item();
    kbLink* link = record->GetLink();
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    kbNode* _low = _LI->item()->GetBeginNode();

    if( _GC->GetAllowNonTopHoleLinking() )
    {
        _BI++;
        if ( !_BI.hitroot() && _BI.item()->GetLink()->IsTopHole() )
        {      

            kbLink* linkToThis = _BI.item()->GetLink();
            //calculate linkToThis its Y at X of topnode.
            kbLine line( _GC );
            line.Set( linkToThis );
            B_INT Y;
            kbNode * leftnode; //left node of clossest link
            //link right now
            if ( linkToThis->GetEndNode()->GetX() == _low->GetX() )
                Y = linkToThis->GetEndNode()->GetY();
            else if ( linkToThis->GetBeginNode()->GetX() == _low->GetX() )
                Y = linkToThis->GetBeginNode()->GetY();
            else
                Y =  line.Calculate_Y( _low->GetX() );

            if ( linkToThis->GetBeginNode()->GetX() < linkToThis->GetEndNode()->GetX() ) 
                leftnode = linkToThis->GetBeginNode();
            else
                leftnode = linkToThis->GetEndNode();
            kbNode *topnode = new kbNode( _low->GetX(), Y, _GC );
            kbLink *link_A = new kbLink( 0, topnode, leftnode, _GC );
            // the orginal linkToThis
            linkToThis->Replace( leftnode, topnode );
            _LI->insbegin( link_A );
            //reset mark to flag that this hole has been processed
            linkToThis->SetTopHole( false );

            kbLink *link_B = new kbLink( 0, _low, topnode, _GC );
            kbLink *link_BB = new kbLink( 0, topnode, _low, _GC );       
            _LI->insbegin( link_B );
            _LI->insbegin( link_BB );
            //mark those two segments as hole linking segments
            link_B->SetHoleLink( true );
            link_BB->SetHoleLink( true );

            //is where we come from/link to a hole
            bool closest_is_hole = linkToThis->GetHole();

            // if the polygon linked to, is a hole, this hole here
            // just gets bigger, so we take over the links its hole marking.
            link_A->SetHole( closest_is_hole );
            link_B->SetHole( closest_is_hole );
            link_BB->SetHole( closest_is_hole );

            // we have only one operation at the time, taking
            // over the operation flags is enough, since the linking segments will
            // be part of that output for any operation done.
            link_A->TakeOverOperationFlags( linkToThis );
            link_B->TakeOverOperationFlags( linkToThis );
            link_BB->TakeOverOperationFlags( linkToThis );

            foundholes = true;

            SortTheBeam( atinsert );
        }
        _BI--;
    }
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

    if ( !record->GetLine()->CrossListEmpty() )
    {
        SortTheBeam( atinsert );

        // link the holes in the graph to a link above.
        // a the link where the linecrosslist is not empty, means that
        // there are links which refer to this link (must be linked to this link)
        // make new nodes and links and set them, re-use the old link, so the links
        // that still stand in the linecrosslist will not be lost.
        // There is a hole that must be linked to this link !
1075 1076 1077

        kbLink* linkToThis = link;

charras's avatar
charras committed
1078
        TDLI<kbNode> I( record->GetLine()->GetCrossList() );
1079 1080 1081 1082 1083 1084
        I.tohead();
        while( !I.hitroot() )
        {
            topnode = I.item();
            I.remove();

1085
            //calculate linkToThis its Y at X of topnode.
charras's avatar
charras committed
1086
            kbLine line( _GC );
1087
            line.Set( linkToThis );
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
            kbNode * leftnode; //left node of clossest link
            B_INT Y;
            //check if flatlink ( i think is always linkBB from a topnode at same X coordinate.
            //but lets accept all flatlinks 
            if ( linkToThis->GetEndNode()->GetX() == linkToThis->GetBeginNode()->GetX() )
            {
                //we take the lowest of the flatlink nodes, which is right for
                // a second hole at same X
                if ( linkToThis->GetEndNode()->GetY() >= linkToThis->GetBeginNode()->GetY() )
                {
                    Y = linkToThis->GetBeginNode()->GetY(); 
                    leftnode = linkToThis->GetBeginNode();
                }
                else
                {
                    Y = linkToThis->GetEndNode()->GetY();
                    leftnode = linkToThis->GetEndNode();
                }
            }
            else
            {   
                if ( linkToThis->GetEndNode()->GetX() == topnode->GetX() )
                    Y = linkToThis->GetEndNode()->GetY();
                else if ( linkToThis->GetBeginNode()->GetX() == topnode->GetX() )
                    Y = linkToThis->GetBeginNode()->GetY();
                else
                    Y =  line.Calculate_Y( topnode->GetX() );
1116

1117 1118 1119 1120 1121
                if ( linkToThis->GetBeginNode()->GetX() < linkToThis->GetEndNode()->GetX() ) 
                    leftnode = linkToThis->GetBeginNode();
                else
                    leftnode = linkToThis->GetEndNode();
            }
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
            // Now we'll create new nodes and new links to make the link between
            // the graphs.

            //holes are always linked in a non hole or hole
            //for a non hole this link will be to the right
            //because non holes are right around
            //for holes this will be to the right also,
            //because they are left around but the link is always on the
            //bottom of the hole

1132
            //    linkA                      linkToThis
1133
            //   o-------->--------NodeA------->------------o
1134 1135 1136 1137 1138 1139
            //  leftnode          |  |
            //                    |  |
            //             linkB  v  ^ linkBB
            //                    |  |
            //                    |  |
            //   outgoing*        |  |          incoming*
1140 1141 1142 1143 1144
            //   o------<---------topnode--------<----------o
            //
            // all holes are oriented left around


charras's avatar
charras committed
1145 1146 1147 1148
            kbNode *node_A = new kbNode( topnode->GetX(), Y, _GC );
            kbLink *link_A = new kbLink( 0, leftnode, node_A, _GC );
            kbLink *link_B = new kbLink( 0, node_A, topnode, _GC );
            kbLink *link_BB = new kbLink( 0, topnode, node_A, _GC );
1149 1150
            // the orginal linkToThis
            linkToThis->Replace( leftnode, node_A );
1151 1152 1153 1154 1155 1156 1157 1158 1159
            _LI->insbegin( link_A );
            _LI->insbegin( link_B );
            _LI->insbegin( link_BB );

            //mark those two segments as hole linking segments
            link_B->SetHoleLink( true );
            link_BB->SetHoleLink( true );

            //is where we come from/link to a hole
1160
            bool closest_is_hole = linkToThis->GetHole();
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

            // if the polygon linked to, is a hole, this hole here
            // just gets bigger, so we take over the links its hole marking.
            link_A->SetHole( closest_is_hole );
            link_B->SetHole( closest_is_hole );
            link_BB->SetHole( closest_is_hole );

            // we have only one operation at the time, taking
            // over the operation flags is enough, since the linking segments will
            // be part of that output for any operation done.
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
            link_A->TakeOverOperationFlags( linkToThis );
            link_B->TakeOverOperationFlags( linkToThis );
            link_BB->TakeOverOperationFlags( linkToThis );

            // check next top node is at same X
            if ( !I.hitroot() )
            {
                kbNode *newtopnode = I.item();
                if ( topnode->GetX() == newtopnode->GetX() )
                    linkToThis = link_BB;
                else
                    linkToThis = link;
            }
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
        }
    }

    if ( link->IsTopHole() )
    {
        SortTheBeam( atinsert );
        writebeam();
    }

    if ( link->IsTopHole() && !_BI.athead() )
    {
        // now we check if this hole should now be linked, or later
        // we always link on the node with the maximum y value, Why ? because i like it !
        // to link we put the node of the hole into the crosslist of the closest link !

        assert( record->Direction() == GO_LEFT );
        // he goes to the left
        if ( atinsert )
        {
            if ( link->GetBeginNode()->GetY() <= link->GetEndNode()->GetY() )
            {
                topnode = link->GetEndNode();
                //the previous link in the scanbeam == the closest link to the hole in vertical
                //direction PUT this node into this link
                _BI--;
                _BI.item()->GetLine()->AddCrossing( topnode );
                _BI++;
                //reset tophole flag, hole has been processed
                link->SetTopHole( false );
                foundholes = true;
            }
        }
        else  //remove stage of links from te beam
        {
            //the tophole link was NOT linked at the insert stage, so it most be linked now
            topnode = _BI.item()->GetLink()->GetBeginNode();
1220 1221 1222
            //the previous link in the scanbeam == the closest link to the hole in vertical
            //direction PUT this node into this link
            _BI--;
1223
            _BI.item()->GetLine()->AddCrossing( topnode );
1224
            _BI++;
1225 1226
            //reset mark to flag that this hole has been processed
            link->SetTopHole( false );
1227
            foundholes = true;
1228 1229 1230
        }
    }
    return foundholes;
1231 1232 1233
}

//sort the records on Ysp if eqaul, sort on tangent at ysp
charras's avatar
charras committed
1234
int recordsorter_ysp_angle( kbRecord* rec1, kbRecord* rec2 )
1235
{
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    if ( rec1->Ysp() > rec2->Ysp() )
        return( 1 );
    if ( rec1->Ysp() < rec2->Ysp() )
        return( -1 );
    //it seems they are equal
    B_INT rightY1;
    if ( rec1->Direction() == GO_LEFT )
        rightY1 = rec1->GetLink()->GetBeginNode()->GetY();
    else
        rightY1 = rec1->GetLink()->GetEndNode()->GetY();
    B_INT rightY2;
    if ( rec2->Direction() == GO_LEFT )
        rightY2 = rec2->GetLink()->GetBeginNode()->GetY();
    else
        rightY2 = rec2->GetLink()->GetEndNode()->GetY();

    if ( rightY1 > rightY2 )
        return( 1 );
    if ( rightY1 < rightY2 )
        return( -1 );
    return( 0 );
1257 1258 1259
}

//sort the records on Ysp if eqaul, sort on tangent at ysp
charras's avatar
charras committed
1260
int recordsorter_ysp_angle_back( kbRecord* rec1, kbRecord* rec2 )
1261
{
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    if ( rec1->Ysp() > rec2->Ysp() )
        return( 1 );
    if ( rec1->Ysp() < rec2->Ysp() )
        return( -1 );
    //it seems they are equal
    B_INT leftY1;
    if ( rec1->Direction() == GO_RIGHT )
        leftY1 = rec1->GetLink()->GetBeginNode()->GetY();
    else
        leftY1 = rec1->GetLink()->GetEndNode()->GetY();
    B_INT leftY2;
    if ( rec2->Direction() == GO_RIGHT )
        leftY2 = rec2->GetLink()->GetBeginNode()->GetY();
    else
        leftY2 = rec2->GetLink()->GetEndNode()->GetY();

    if ( leftY1 > leftY2 )
        return( 1 );
    if ( leftY1 < leftY2 )
        return( -1 );
    return( 0 );
1283 1284 1285
}

// swap functie for cocktailsort ==> each swap means an intersection of links
charras's avatar
charras committed
1286
bool swap_crossing_normal( kbRecord *a, kbRecord *b )
1287
{
1288 1289 1290 1291 1292 1293
    if ( !a->Equal( b ) ) // records NOT parallel
    {
        a->GetLine()->Intersect_simple( b->GetLine() );
        return true;
    }
    return false;
1294 1295 1296 1297
}

int ScanBeam::Process_LinkToLink_Crossings()
{
1298 1299
    // sort on y value of next intersection; and find the intersections
    return _BI.cocktailsort( recordsorter_ysp_angle_back, swap_crossing_normal );
1300 1301 1302 1303 1304 1305
}

//catch node to link crossings
// must be sorted on ysp
int ScanBeam::Process_PointToLink_Crossings()
{
1306
    int merges = 0;
charras's avatar
charras committed
1307
    kbRecord* record;
1308 1309 1310

    if ( _BI.count() > 1 )
    {
charras's avatar
charras committed
1311
        DL_Iter<kbRecord*> IL = DL_Iter<kbRecord*>( this );
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
        IL.toiter( &_BI );

        //from IL search back for close links
        IL--;
        while( !IL.hitroot() )
        {
            record = IL.item();

            if ( record->Ysp() > _low->GetY() + _GC->GetInternalMarge() )
                break;

            // the distance to the lo/hi node is smaller then the _GC->GetInternalMarge()
            if( ( record->GetLink()->GetBeginNode() != _low ) &&
                    ( record->GetLink()->GetEndNode()  != _low )
              )
            {  // the link is not towards the lohi node
                record->GetLine()->AddCrossing( _low );
                merges++;
            }
            IL--;
        }

        //from IL search forward for close links
        IL.toiter( &_BI );
        IL++;
        while( !IL.hitroot() )
        {
            record = IL.item();

            if ( record->Ysp() < _low->GetY() - _GC->GetInternalMarge() )
                break;

            // the distance to the lohi node is smaller then the booleng->Get_Marge()
            if( ( record->GetLink()->GetBeginNode() != _low ) &&
                    ( record->GetLink()->GetEndNode()  != _low )
              )
            {  // the link is not towards the low node
                record->GetLine()->AddCrossing( _low );
                merges++;
            }
            IL++;
        }

    }

    return merges;
1358 1359
}

charras's avatar
charras committed
1360
int ScanBeam::Process_LinkToLink_Flat( kbLine* flatline )
1361
{
1362
    int crossfound = 0;
charras's avatar
charras committed
1363 1364
    kbRecord* record;
    DL_Iter<kbRecord*> _BBI = DL_Iter<kbRecord*>();
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
    _BBI.Attach( this );
    _BBI.toiter( &_BI );

    for( _BI.tohead(); !_BI.hitroot(); _BI++ )
    {
        record = _BI.item();

        if ( record->Ysp() < ( flatline->GetLink()->GetLowNode()->GetY() - _GC->GetInternalMarge() ) )
            break;//they are sorted so no other can be there

        if ( ( record->Ysp() > ( flatline->GetLink()->GetLowNode()->GetY() - _GC->GetInternalMarge() ) )
                &&
                ( record->Ysp() < ( flatline->GetLink()->GetHighNode()->GetY() + _GC->GetInternalMarge() ) )
           )
        { //it is in between the flat link region
            //create a new node at ysp and insert it in both the flatlink and the crossing link

            if (
                ( record->GetLink()->GetEndNode()  != flatline->GetLink()->GetHighNode() ) &&
                ( record->GetLink()->GetEndNode()  != flatline->GetLink()->GetLowNode() ) &&
                ( record->GetLink()->GetBeginNode() != flatline->GetLink()->GetHighNode() ) &&
                ( record->GetLink()->GetBeginNode() != flatline->GetLink()->GetLowNode() )
            )
            {
charras's avatar
charras committed
1389
                kbNode * newnode = new kbNode( _low->GetX(), _BI.item()->Ysp(), _GC );
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
                flatline->AddCrossing( newnode );
                record->GetLine()->AddCrossing( newnode );
                crossfound++;
            }
        }
    }

    _BI.toiter( &_BBI );
    _BBI.Detach();
    return crossfound;
1400 1401 1402 1403
}

bool ScanBeam::checksort()
{
1404 1405 1406 1407 1408 1409
    // if empty then just insert
    if ( empty() )
        return true;

    // put new item left of the one that is bigger
    _BI.tohead();
charras's avatar
charras committed
1410
    kbRecord* prev = _BI.item();
1411 1412 1413
    _BI++;
    while( !_BI.hitroot() )
    {
charras's avatar
charras committed
1414
        kbRecord * curr = _BI.item();
1415 1416 1417 1418 1419 1420 1421 1422 1423
        if ( recordsorter_ysp_angle( prev, curr ) == -1 )
        {
            recordsorter_ysp_angle( prev, curr );
            return false;
        }
        prev = _BI.item();
        _BI++;
    }
    return true;
1424 1425 1426 1427 1428
}

bool ScanBeam::writebeam()
{
#if KBOOL_DEBUG == 1
1429
    FILE * file = _GC->GetLogFile();
1430

1431 1432
    if ( file == NULL )
        return true;
1433

1434 1435 1436
    fprintf( file, "# beam %d \n", count() );
    fprintf( file, " low %I64d %I64d \n", _low->GetX() , _low->GetY() );
    fprintf( file, " type %d \n", _type );
1437

1438 1439 1440 1441 1442
    if ( empty() )
    {
        fprintf( file, "             empty \n" );
        return true;
    }
1443

charras's avatar
charras committed
1444
    DL_Iter<kbRecord*> _BI( this );
1445

1446 1447 1448 1449
    // put new item left of the one that is bigger
    _BI.tohead();
    while( !_BI.hitroot() )
    {
charras's avatar
charras committed
1450
        kbRecord * cur = _BI.item();
1451

1452
        fprintf( file, " ysp %I64d \n", cur->Ysp() );
1453

charras's avatar
charras committed
1454
        kbLink* curl = cur->GetLink();
1455

1456 1457
        fprintf( file, "             linkbegin %I64d %I64d \n", curl->GetBeginNode()->GetX(), curl->GetBeginNode()->GetY() );
        fprintf( file, "             linkend %I64d %I64d \n", curl->GetEndNode()->GetX(), curl->GetEndNode()->GetY() );
1458

1459 1460 1461 1462 1463
        if ( curl->GetEndNode()->GetX() == -2459565876494606883 )
            fprintf( file, "             linkend %I64d %I64d \n", curl->GetEndNode()->GetX(), curl->GetEndNode()->GetY() );

        _BI++;
    }
1464 1465
#endif

1466
    return true;
1467
}