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

12 13 14
#include "kbool/node.h"
#include "kbool/link.h"
#include "kbool/line.h"
15 16 17 18
#include <math.h>

//this here is to initialize the static iterator of node
//with NOLIST constructor
charras's avatar
charras committed
19
//TDLI<kbLink>  kbNode::_linkiter=TDLI<kbLink>(_GC);
20

charras's avatar
charras committed
21
kbNode::kbNode( Bool_Engine* GC ) : kbLPoint( 0, 0 )
22
{
23 24
    _GC = GC;
    _linklist = new DL_List<void*>();
25 26 27
}


charras's avatar
charras committed
28
kbNode::kbNode( B_INT const X, B_INT const Y, Bool_Engine* GC ) : kbLPoint( X, Y )
29
{
30 31
    _GC = GC;
    _linklist = new DL_List<void*>();
32 33 34
}


charras's avatar
charras committed
35
kbNode::kbNode( kbLPoint* const a_point, Bool_Engine* GC ) : kbLPoint( a_point )
36
{
37 38
    _GC = GC;
    _linklist = new DL_List<void*>();
39 40 41
}


charras's avatar
charras committed
42 43
//kbNode::kbNode(kbNode * const other) : kbLPoint(other)
kbNode::kbNode( kbNode * const other, Bool_Engine* GC )
44
{
45 46 47 48
    _GC = GC;
    _x = other->_x;
    _y = other->_y;
    _linklist = new DL_List<void*>();
49 50
}

charras's avatar
charras committed
51
kbNode& kbNode::operator=( const kbNode &other_node )
52
{
53 54
    _x = other_node._x;
    _y = other_node._y;
55

56
    return *this;
57 58 59 60 61
}


// x and y of the point will be rounded to the nearest
// xnew=N*grid and ynew=N*grid
charras's avatar
charras committed
62
void kbNode::RoundInt( B_INT grid )
63
{
64 65
    _x = ( B_INT ) floor( ( _x + grid * 0.5 ) / grid ) * grid;
    _y = ( B_INT ) floor( ( _y + grid * 0.5 ) / grid ) * grid;
66 67
}

charras's avatar
charras committed
68
kbNode::~kbNode()
69
{
70
    delete _linklist;
71 72
}

charras's avatar
charras committed
73
DL_List<void*>* kbNode::GetLinklist()
74
{
75
    return _linklist;
76 77
}

charras's avatar
charras committed
78
void kbNode::AddLink( kbLink *a_link )
79
{
80 81
// assert(a_link);
    _linklist->insbegin( a_link );
82 83
}

charras's avatar
charras committed
84
kbLink* kbNode::GetIncomingLink()
85
{
charras's avatar
charras committed
86 87
    if ( ( ( kbLink* )_linklist->headitem() )->GetEndNode() == this )
        return ( kbLink* )_linklist->headitem();
88
    else
charras's avatar
charras committed
89
        return ( kbLink* )_linklist->tailitem();
90 91
}

charras's avatar
charras committed
92
kbLink* kbNode::GetOutgoingLink()
93
{
charras's avatar
charras committed
94 95
    if ( ( ( kbLink* )_linklist->headitem() )->GetBeginNode() == this )
        return ( kbLink* )_linklist->headitem();
96
    else
charras's avatar
charras committed
97
        return ( kbLink* )_linklist->tailitem();
98 99 100 101 102
}

//
// Returns the number of connected links
//
charras's avatar
charras committed
103
int kbNode::GetNumberOfLinks()
104
{
105
    return _linklist->count();
106 107
}

charras's avatar
charras committed
108
kbLink* kbNode::GetOtherLink( kbLink* prev )
109
{
charras's avatar
charras committed
110 111 112 113
    if ( prev == ( kbLink* )_linklist->headitem() )
        return ( kbLink* )_linklist->tailitem();
    if ( prev == ( kbLink* )_linklist->tailitem() )
        return ( kbLink* )_linklist->headitem();
114

115
    return NULL;
116 117 118
}


charras's avatar
charras committed
119
int kbNode::Merge( kbNode *other )
120
{
121 122 123 124 125 126 127 128 129
    if ( this == other ) //they are already merged dummy
        return 0;

    _GC->_linkiter->Attach( _linklist );
    int Counter;
    // used to delete Iterator on other->_linklist
    // otherwise there can't be a takeover, because for takeover there can't
    // be an iterator on other->_linklist;
    {
charras's avatar
charras committed
130 131
        TDLI<kbLink> Iother( other->_linklist );
        kbLink* temp;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

        Counter = Iother.count();

        Iother.tohead();
        while ( !Iother.hitroot() )
        {
            temp = Iother.item();
            //need to test both nodes because it may be a zero length link
            if ( temp->GetEndNode() == other )
                temp->SetEndNode( this );
            if ( temp->GetBeginNode() == other )
                temp->SetBeginNode( this );
            Iother++;
        }
        _GC->_linkiter->takeover( &Iother );
    }
    _GC->_linkiter->Detach();

    //at this moment the other nodes has no link pointing to it so it needs to be deleted
    delete other;
    return Counter;
153 154 155
}


charras's avatar
charras committed
156
void kbNode::RemoveLink( kbLink *a_link )
157
{
158 159
// assert(a_link);
    _GC->_linkiter->Attach( _linklist );
160

161 162 163
    if ( _GC->_linkiter->toitem( a_link ) ) // find the link
        _GC->_linkiter->remove();
    _GC->_linkiter->Detach();
164 165 166 167 168 169
}

// This function will determinate if the given three points
// can be simplified to two points
//
// input : three nodes, the first and the second must be points of
170
//     a line in correct order, the third point is a point of another
171 172 173
//         line.
// output: -
// return: true if points can be simplified
174
//     false if points can't be simplified
charras's avatar
charras committed
175
bool kbNode::Simplify( kbNode *First, kbNode *Second, B_INT Marge )
176
{
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    double distance = 0;

    // The first and second point are a zero line, if so we can
    // make a line between the first and third point
    if ( First->Equal( Second, Marge ) )
        return true;

    // Are the first and third point equal, if so
    // we can delete the second point
    if ( First->Equal( this, Marge ) )
        return true;

    // Used tmp_link.set here, because the link may not be linked in the graph,
    // because the point of the graphs are used, after use of the line we have
    //to set the link to zero so the nodes will not be destructed by exit of the function
charras's avatar
charras committed
192
    kbLink tmp_link( _GC );
193
    tmp_link.Set( First, Second );
charras's avatar
charras committed
194
    kbLine tmp_line( _GC );
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    tmp_line.Set( &tmp_link );

    // If third point is on the same line which is made from the first
    // and second point then we can delete the second point
    if ( tmp_line.PointOnLine( this, distance, ( double ) Marge ) == ON_AREA )
    {
        tmp_link.Set( NULL, NULL );
        return true;
    }
    //
    //
    tmp_link.Set( Second, this );
    tmp_line.Set( &tmp_link );
    if ( tmp_line.PointOnLine( First, distance, ( double ) Marge ) == ON_AREA )
    {
        tmp_link.Set( NULL, NULL );
        return true;
    }
    tmp_link.Set( NULL, NULL );
    return false;
215 216 217
}


charras's avatar
charras committed
218
kbLink* kbNode::GetNextLink()
219
{
220 221 222 223 224 225 226 227
    int Aantal = _linklist->count();

// assert (Aantal != 0);

    // there is one link, so there is no previous link
    if ( Aantal == 1 )
        return NULL;
    int Marked_Counter = 0;
charras's avatar
charras committed
228
    kbLink *the_link = NULL;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    // count the marked links
    _GC->_linkiter->Attach( _linklist );
    _GC->_linkiter->tohead();
    while ( !_GC->_linkiter->hitroot() )
    {
        if ( _GC->_linkiter->item()->IsMarked() )
            Marked_Counter++;
        else
        {
            if ( !the_link )
                the_link = _GC->_linkiter->item();
        }
        ( *_GC->_linkiter )++;
    }
    _GC->_linkiter->Detach();
    if ( Aantal - Marked_Counter != 1 )
        // there arent two unmarked links
        return NULL;
    else
    {
        if ( the_link->GetBeginNode() == this )
            return the_link;
        else
            return NULL;
    }
255 256 257
}


charras's avatar
charras committed
258
kbLink* kbNode::GetPrevLink()
259
{
260 261 262 263 264 265 266 267 268 269 270 271 272
    int Aantal;
    if ( !_linklist )
        return NULL;

    Aantal = _linklist->count();

// assert (Aantal != 0);

    // there is one link, so there is no previous link
    if ( Aantal == 1 )
        return NULL;

    int Marked_Counter = 0;
charras's avatar
charras committed
273
    kbLink *the_link = NULL;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

    _GC->_linkiter->Attach( _linklist );
    // count the marked links
    _GC->_linkiter->tohead();
    while ( !_GC->_linkiter->hitroot() )
    {
        if ( _GC->_linkiter->item()->IsMarked() )
            Marked_Counter++;
        else
        {
            if ( !the_link )
                the_link = _GC->_linkiter->item();
        }
        ( *_GC->_linkiter )++;
    }
    _GC->_linkiter->Detach();
    if ( Aantal - Marked_Counter != 1 )
        // there arent two unmarked links
        return NULL;
    else
    {
        if ( the_link->GetEndNode() == this )
            return the_link;
        else
            return NULL;
    }
300 301
}

charras's avatar
charras committed
302
bool kbNode::SameSides( kbLink* const prev , kbLink* const link, BOOL_OP operation )
303
{
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    bool directedLeft;
    bool directedRight;
    if ( prev->GetEndNode() == this ) //forward direction
    {
        directedLeft = prev->IsMarkedLeft( operation );
        directedRight = prev->IsMarkedRight( operation );
        if ( link->GetBeginNode() == this ) //forward direction
        {
            return directedLeft == link->IsMarkedLeft( operation ) &&
                   directedRight == link->IsMarkedRight( operation );
        }

        return directedLeft == link->IsMarkedRight( operation ) &&
               directedRight == link->IsMarkedLeft( operation );
    }

    directedLeft = prev->IsMarkedRight( operation );
    directedRight = prev->IsMarkedLeft( operation );
    if ( link->GetBeginNode() == this ) //forward direction
    {
        return directedLeft == link->IsMarkedLeft( operation ) &&
               directedRight == link->IsMarkedRight( operation );
    }
    return directedLeft == link->IsMarkedRight( operation ) &&
           directedRight == link->IsMarkedLeft( operation );
329 330 331 332 333
}

//  on the node get the link
//  is the most right or left one
//  This function is used to collect the simple graphs from a graph
charras's avatar
charras committed
334
kbLink* kbNode::GetMost( kbLink* const prev , LinkStatus whatside, BOOL_OP operation )
335
{
charras's avatar
charras committed
336 337 338
    kbLink * reserve = 0;
    kbLink *Result = NULL, *link;
    kbNode* prevbegin = prev->GetOther( this );
339 340 341

    if ( _linklist->count() == 2 ) // only two links to this node take the one != prev
    {
charras's avatar
charras committed
342 343
        if ( ( link = ( kbLink* )_linklist->headitem() ) == prev )      //this is NOT the one to go on
            link = ( kbLink* )_linklist->tailitem();
344 345 346 347 348 349 350 351
        if ( !link->BeenHere() && SameSides( prev, link, operation ) )
            //we are back where we started (bin is true) return Null
            return link;
        return( 0 );
    }

    _GC->_linkiter->Attach( _linklist );
    _GC->_linkiter->tohead();
charras's avatar
charras committed
352
    //more then 2 links to the kbNode
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    while( !_GC->_linkiter->hitroot() )
    {
        link = _GC->_linkiter->item();
        if ( !link->BeenHere() &&
                SameSides( prev, link, operation ) &&
                link != prev   //should be set to bin already
           )
        {
            if ( prevbegin == link->GetOther( this ) )//pointers equal
                //we are going back in the same direction on a parallel link
                //only take this possibility if nothing else is possible
                reserve = link;
            else
            {  //this link is in a different direction
                if ( !Result )
                    Result = link; //first one found sofar
                else
                {
                    if ( prev->PointOnCorner( Result, link ) == whatside )
                        //more to the whatside than take this one
                        Result = link;
                }
            }
        }
        ( *_GC->_linkiter )++;
    }

    // if there is a next link found return it
    // else if a parallel link is found return that one
    // else return NULL
    _GC->_linkiter->Detach();
    return ( ( Result ) ? Result : reserve );
385 386 387 388 389
}

//  on the node get the link
//  is the most right or left one
//  This function is used to collect the simple graphs from a graph
390
kbLink* kbNode::GetMostHole( kbLink* const prev, LinkStatus whatside, BOOL_OP operation, bool searchholelink )
391
{
charras's avatar
charras committed
392 393 394
    kbLink * reserve = 0;
    kbLink *Result = NULL, *link;
    kbNode* prevbegin = prev->GetOther( this );
395 396 397

    if ( _linklist->count() == 2 ) // only two links to this node take the one != prev
    {
charras's avatar
charras committed
398 399
        if ( ( link = ( kbLink* )_linklist->headitem() ) == prev )      //this is NOT the one to go on
            link = ( kbLink* )_linklist->tailitem();
400 401 402 403 404
        if ( 
             !link->BeenHere() &&
             link->GetHole() && 
             ( searchholelink && link->GetHoleLink() || !link->GetHoleLink() ) && 
             SameSides( prev, link, operation ) )
405 406 407 408 409 410 411
            //we are back where we started (bin is true) return Null
            return link;
        return( 0 );
    }

    _GC->_linkiter->Attach( _linklist );
    _GC->_linkiter->tohead();
charras's avatar
charras committed
412
    //more then 2 links to the kbNode
413 414 415
    while( !_GC->_linkiter->hitroot() )
    {
        link = _GC->_linkiter->item();
416 417 418 419
        if ( 
               !link->BeenHere() &&
               link->GetHole() && 
               ( searchholelink && link->GetHoleLink() || !link->GetHoleLink() ) && 
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
                SameSides( prev, link, operation ) &&
                link != prev   //should be set to bin already
           )
        {
            if ( prevbegin == link->GetOther( this ) )//pointers equal
                //we are going back in the same direction on a parallel link
                //only take this possibility if nothing else is possible
                reserve = link;
            else
            {  //this link is in a different direction
                if ( !Result )
                    Result = link; //first one found sofar
                else
                {
                    if ( prev->PointOnCorner( Result, link ) == whatside )
                        //more to the whatside than take this one
                        Result = link;
                }
            }
        }
        ( *_GC->_linkiter )++;
    }

    // if there is a next link found return it
    // else if a parallel link is found return that one
    // else return NULL
    _GC->_linkiter->Detach();
    return ( ( Result ) ? Result : reserve );
448 449 450
}

// this function gets the highest not flat link
451
kbLink* kbNode::GetHoleLink( kbLink* const prev, LinkStatus whatside, bool checkbin, BOOL_OP operation )
452
{
charras's avatar
charras committed
453
    kbLink * Result = NULL, *link;
454 455 456 457 458 459 460 461 462 463 464

    _GC->_linkiter->Attach( _linklist );

    for( _GC->_linkiter->tohead();!_GC->_linkiter->hitroot();( *_GC->_linkiter )++ )
    {
        link = _GC->_linkiter->item();
        if ( link->GetHoleLink() &&
                ( !checkbin || ( checkbin && !link->BeenHere() ) ) &&
                SameSides( prev, link, operation )
           )
        {
465 466 467 468 469 470 471 472
            if ( !Result )
                Result = link; //first one found sofar
            else
            {
                if ( prev->PointOnCorner( Result, link ) == whatside )
                    //more to the whatside than take this one
                    Result = link;
            }
473 474 475 476 477
        }
    }

    _GC->_linkiter->Detach();
    return ( Result );
478 479 480
}

// this function gets the highest not flat link
charras's avatar
charras committed
481
kbLink* kbNode::GetNotFlat()
482
{
charras's avatar
charras committed
483
    kbLink * Result = NULL, *link;
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

    _GC->_linkiter->Attach( _linklist );

    double tangold = 0.0;
    double tangnew = 0.0;

    for( _GC->_linkiter->tohead();!_GC->_linkiter->hitroot();( *_GC->_linkiter )++ )
    {
        link = _GC->_linkiter->item();
        if ( !_GC->_linkiter->item()->BeenHere() )
        {
            B_INT dx = link->GetOther( this )->GetX() - _x;
            B_INT dy = link->GetOther( this )->GetY() - _y;
            if ( dx != 0 )
            {
                tangnew = fabs( ( double ) dy / ( double ) dx );
            }
            else
            {
                tangnew = MAXDOUBLE;
            }

            if ( !Result )
            {
                //this link is in a different direction
                Result = link; //first one found sofar
                tangold = tangnew;
            }
            else
            {
                if( tangnew < tangold )
                {
                    //this one is higher (more horizontal) then the old Result
                    Result = link;
                    tangold = tangnew;
                }
            }
        }
    }

    // if there is a next link found return it
    // else if a parallel link is found return that one
    // else return NULL
    _GC->_linkiter->Detach();
    return ( Result );
529 530 531 532
}

//  on the node get the link that is not BIN
//  and that has the same graphnumber and is in same direction
charras's avatar
charras committed
533
kbLink *kbNode::Follow( kbLink* const prev )
534
{
charras's avatar
charras committed
535
    kbLink * temp;
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
    _GC->_linkiter->Attach( _linklist );

    _GC->_linkiter->tohead();
    while( !_GC->_linkiter->hitroot() )
    {
        if ( ( _GC->_linkiter->item() != prev ) &&
                ( !_GC->_linkiter->item()->BeenHere() ) &&
                ( _GC->_linkiter->item()->GetGraphNum() == prev->GetGraphNum() ) &&
                (
                    ( ( prev->GetEndNode()   == this ) &&
                      ( _GC->_linkiter->item()->GetEndNode()  != this )
                    )
                    ||
                    ( ( prev->GetBeginNode() == this ) &&
                      ( _GC->_linkiter->item()->GetBeginNode() != this )
                    )
                )
           )
        {
            temp = _GC->_linkiter->item();
            _GC->_linkiter->Detach();
            return( temp );
        }
        ( *_GC->_linkiter )++;
    }

    _GC->_linkiter->Detach();
    return ( 0 );
564 565 566 567 568
}

// this function gets the highest (other node) link ascending from the node
// that has the bin flag set as the argument binset
// if no such link exists return 0
charras's avatar
charras committed
569
kbLink* kbNode::GetBinHighest( bool binset )
570
{
charras's avatar
charras committed
571
    kbLink * Result = NULL, *link;
572 573 574 575 576 577 578 579 580
    _GC->_linkiter->Attach( _linklist );

    double tangold = 0.0;
    double tangnew = 0.0;

    for( _GC->_linkiter->tohead();!_GC->_linkiter->hitroot();( *_GC->_linkiter )++ )
    {
        link = _GC->_linkiter->item();
        if ( _GC->_linkiter->item()->BeenHere() == binset )
581
        {
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
            B_INT dx = link->GetOther( this )->GetX() - _x;
            B_INT dy = link->GetOther( this )->GetY() - _y;
            if ( dx != 0 )
            {
                tangnew = ( double ) dy / ( double ) dx;
            }
            else if ( dy > 0 )
            {
                tangnew = MAXDOUBLE;
            }
            else
            {
                tangnew = -MAXDOUBLE;
            }

            if ( !Result )
            {
                Result = link; //first one found sofar
                tangold = tangnew;
            }
            else
            {
                if( tangnew > tangold )
                {
                    //this one is higher then the old Result
                    Result = link;
                    tangold = tangnew;
                }
            }
611
        }
612 613 614 615 616 617
    }

    // if there is a link found return it
    // else return NULL
    _GC->_linkiter->Detach();
    return ( Result );
618 619 620
}