graphpcb.cpp 23.4 KB
Newer Older
1 2 3 4
/**
 * @file graphpcb.cpp
 * @brief PCB editor autorouting and "graphics" routines.
 */
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 *
 * Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

33 34 35 36 37
#include <fctsys.h>
#include <common.h>
#include <macros.h>
#include <trigo.h>
#include <pcbcommon.h>
38
#include <math_for_graphics.h>
39 40 41 42
#include <class_board.h>
#include <class_track.h>

#include <pcbnew.h>
43
#include <autorout.h>
44
#include <cell.h>
45

46
void TracePcbLine( int x0, int y0, int x1, int y1, LAYER_NUM layer, int color );
47

48 49
void TraceArc( int ux0, int uy0,
               int ux1, int uy1,
50
               double ArcAngle,
51
               int lg, LAYER_NUM layer, int color,
52
               int op_logic );
53 54


55 56
static void DrawSegmentQcq( int ux0, int uy0,
                            int ux1, int uy1,
57
                            int lg, LAYER_NUM layer, int color,
58
                            int op_logic );
59

60
static void TraceFilledCircle( int    cx, int cy, int radius,
61
                               LAYER_MSK aLayerMask,
62
                               int    color,
63 64
                               int    op_logic );

65
static void TraceCircle( int ux0, int uy0, int ux1, int uy1, int lg, LAYER_NUM layer,
66
                         int color, int op_logic );
67

68
// Macro call to update cell.
69 70
#define OP_CELL( layer, dy, dx )                                        \
    {                                                                   \
71
        if( layer == UNDEFINED_LAYER )                                  \
72
        {                                                               \
73
            RoutingMatrix.WriteCell( dy, dx, BOTTOM, color );           \
74 75
            if( RoutingMatrix.m_RoutingLayersCount > 1 )                \
                RoutingMatrix.WriteCell( dy, dx, TOP, color );          \
76 77 78
        }                                                               \
        else                                                            \
        {                                                               \
79
            if( layer == g_Route_Layer_BOTTOM )                         \
80
                RoutingMatrix.WriteCell( dy, dx, BOTTOM, color );       \
81
            if( RoutingMatrix.m_RoutingLayersCount > 1 )                \
82
                if( layer == g_Route_Layer_TOP )                        \
83
                    RoutingMatrix.WriteCell( dy, dx, TOP, color );      \
84 85 86
        }                                                               \
    }

87
void PlacePad( D_PAD* aPad, int color, int marge, int op_logic )
88
{
89
    int     dx, dy;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
90
    wxPoint shape_pos = aPad->ReturnShapePos();
91

Dick Hollenbeck's avatar
Dick Hollenbeck committed
92
    dx = aPad->GetSize().x / 2;
93
    dx += marge;
94

Dick Hollenbeck's avatar
Dick Hollenbeck committed
95
    if( aPad->GetShape() == PAD_CIRCLE )
96
    {
97
        TraceFilledCircle( shape_pos.x, shape_pos.y, dx,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
98
                           aPad->GetLayerMask(), color, op_logic );
99 100 101
        return;
    }

Dick Hollenbeck's avatar
Dick Hollenbeck committed
102
    dy = aPad->GetSize().y / 2;
103
    dy += marge;
104

Dick Hollenbeck's avatar
Dick Hollenbeck committed
105
    if( aPad->GetShape() == PAD_TRAPEZOID )
106
    {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
107 108
        dx += abs( aPad->GetDelta().y ) / 2;
        dy += abs( aPad->GetDelta().x ) / 2;
109 110
    }

111
    // The pad is a rectangle ( horizontal or vertical )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
112
    if( int( aPad->GetOrientation() ) % 900 == 0 )
113
    {
114
        // Orientation turned 90 deg.
Dick Hollenbeck's avatar
Dick Hollenbeck committed
115
        if( aPad->GetOrientation() == 900  ||  aPad->GetOrientation() == 2700 )
116
        {
117 118 119
            EXCHG( dx, dy );
        }

120
        TraceFilledRectangle( shape_pos.x - dx, shape_pos.y - dy,
121
                              shape_pos.x + dx, shape_pos.y + dy,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
122
                              aPad->GetLayerMask(), color, op_logic );
123 124 125
    }
    else
    {
126
        TraceFilledRectangle( shape_pos.x - dx, shape_pos.y - dy,
127
                              shape_pos.x + dx, shape_pos.y + dy,
128
                              aPad->GetOrientation(),
Dick Hollenbeck's avatar
Dick Hollenbeck committed
129
                              aPad->GetLayerMask(), color, op_logic );
130
    }
131 132 133
}


134
/* Set to color the cells included in the circle
135
 * Parameters:
136
 * center: cx, cy.
137
 * radius: a value add to the radius or half the score pad
138
 * aLayerMask: layer occupied
139
 * color: mask write in cells
140
 * op_logic: type of writing in the cell (WRITE, OR)
141
 */
142
void TraceFilledCircle( int    cx, int cy, int radius,
143
                        LAYER_MSK aLayerMask,
144
                        int    color,
145
                        int    op_logic )
146
{
147 148 149 150
    int   row, col;
    int   ux0, uy0, ux1, uy1;
    int   row_max, col_max, row_min, col_min;
    int   trace = 0;
151
    double fdistmin, fdistx, fdisty;
152 153 154
    int   tstwrite = 0;
    int   distmin;

155
    if( aLayerMask & GetLayerMask( g_Route_Layer_BOTTOM ) )
156
        trace = 1;       // Trace on BOTTOM
157

158
    if( aLayerMask & GetLayerMask( g_Route_Layer_TOP ) )
159
        if( RoutingMatrix.m_RoutingLayersCount > 1 )
160
            trace |= 2;  // Trace on TOP
161 162 163 164

    if( trace == 0 )
        return;

165
    RoutingMatrix.SetCellOperation( op_logic );
166

167 168
    cx -= RoutingMatrix.GetBrdCoordOrigin().x;
    cy -= RoutingMatrix.GetBrdCoordOrigin().y;
169

170
    distmin = radius;
171

172
    // Calculate the bounding rectangle of the circle.
173 174 175 176
    ux0 = cx - radius;
    uy0 = cy - radius;
    ux1 = cx + radius;
    uy1 = cy + radius;
177

178
    // Calculate limit coordinates of cells belonging to the rectangle.
179 180 181 182
    row_max = uy1 / RoutingMatrix.m_GridRouting;
    col_max = ux1 / RoutingMatrix.m_GridRouting;
    row_min = uy0 / RoutingMatrix.m_GridRouting;  // if (uy0 > row_min*Board.m_GridRouting) row_min++;
    col_min = ux0 / RoutingMatrix.m_GridRouting;  // if (ux0 > col_min*Board.m_GridRouting) col_min++;
183 184 185

    if( row_min < 0 )
        row_min = 0;
186

187 188
    if( row_max >= (RoutingMatrix.m_Nrows - 1) )
        row_max = RoutingMatrix.m_Nrows - 1;
189

190 191
    if( col_min < 0 )
        col_min = 0;
192

193 194
    if( col_max >= (RoutingMatrix.m_Ncols - 1) )
        col_max = RoutingMatrix.m_Ncols - 1;
195

196
    // Calculate coordinate limits of cell belonging to the rectangle.
197 198
    if( row_min > row_max )
        row_max = row_min;
199

200 201 202
    if( col_min > col_max )
        col_max = col_min;

203
    fdistmin = (double) distmin * distmin;
204 205 206

    for( row = row_min; row <= row_max; row++ )
    {
207
        fdisty  = (double) ( cy - ( row * RoutingMatrix.m_GridRouting ) );
208
        fdisty *= fdisty;
209

210 211
        for( col = col_min; col <= col_max; col++ )
        {
212
            fdistx  = (double) ( cx - ( col * RoutingMatrix.m_GridRouting ) );
213 214
            fdistx *= fdistx;

215
            if( fdistmin <= ( fdistx + fdisty ) )
216 217 218
                continue;

            if( trace & 1 )
219
                RoutingMatrix.WriteCell( row, col, BOTTOM, color );
220

221
            if( trace & 2 )
222
                RoutingMatrix.WriteCell( row, col, TOP, color );
223

224 225 226 227 228 229 230
            tstwrite = 1;
        }
    }

    if( tstwrite )
        return;

231 232 233
    /* If no cell has been written, it affects the 4 neighboring diagonal
     * (Adverse event: pad off grid in the center of the 4 neighboring
     * diagonal) */
234 235
    distmin  = RoutingMatrix.m_GridRouting / 2 + 1;
    fdistmin = ( (double) distmin * distmin ) * 2; // Distance to center point diagonally
236 237 238

    for( row = row_min; row <= row_max; row++ )
    {
239
        fdisty  = (double) ( cy - ( row * RoutingMatrix.m_GridRouting ) );
240
        fdisty *= fdisty;
241

242 243
        for( col = col_min; col <= col_max; col++ )
        {
244
            fdistx  = (double) ( cx - ( col * RoutingMatrix.m_GridRouting ) );
245 246
            fdistx *= fdistx;

247
            if( fdistmin <= ( fdistx + fdisty ) )
248 249 250
                continue;

            if( trace & 1 )
251
                RoutingMatrix.WriteCell( row, col, BOTTOM, color );
252

253
            if( trace & 2 )
254
                RoutingMatrix.WriteCell( row, col, TOP, color );
255 256
        }
    }
257 258 259
}


260
void TraceSegmentPcb( TRACK* pt_segm, int color, int marge, int op_logic )
261
{
262
    int half_width;
263 264
    int ux0, uy0, ux1, uy1;

265
    half_width = ( pt_segm->GetWidth() / 2 ) + marge;
266

267
    // Calculate the bounding rectangle of the segment (if H, V or Via)
268 269 270 271
    ux0 = pt_segm->GetStart().x - RoutingMatrix.GetBrdCoordOrigin().x;
    uy0 = pt_segm->GetStart().y - RoutingMatrix.GetBrdCoordOrigin().y;
    ux1 = pt_segm->GetEnd().x - RoutingMatrix.GetBrdCoordOrigin().x;
    uy1 = pt_segm->GetEnd().y - RoutingMatrix.GetBrdCoordOrigin().y;
272

273
    // Test if VIA (filled circle was drawn)
274
    if( pt_segm->Type() == PCB_VIA_T )
275
    {
276
        LAYER_MSK layer_mask = NO_LAYERS;
277

278
        if( pt_segm->IsOnLayer( g_Route_Layer_BOTTOM ) )
279
            layer_mask = GetLayerMask( g_Route_Layer_BOTTOM );
280

281
        if( pt_segm->IsOnLayer( g_Route_Layer_TOP ) )
282
        {
283 284
            if( layer_mask == 0 )
                layer_mask = GetLayerMask( g_Route_Layer_TOP );
285
            else
286
                layer_mask = FULL_LAYERS;
287 288 289
        }

        if( color == VIA_IMPOSSIBLE )
290
            layer_mask = FULL_LAYERS;
291

292
        if( layer_mask )
293
            TraceFilledCircle( pt_segm->GetStart().x, pt_segm->GetStart().y,
294
                               half_width, layer_mask, color, op_logic );
295 296 297
        return;
    }

298
    LAYER_NUM layer = pt_segm->GetLayer();
299

300
    if( color == VIA_IMPOSSIBLE )
301
        layer = UNDEFINED_LAYER;
302

303
    // The segment is here a straight line or a circle or an arc.:
304
    if( pt_segm->GetShape() == S_CIRCLE )
305
    {
306
        TraceCircle( ux0, uy0, ux1, uy1, half_width, layer, color, op_logic );
307 308 309
        return;
    }

310
    if( pt_segm->GetShape() == S_ARC )
311
    {
312
        TraceArc( ux0, uy0, ux1, uy1, pt_segm->m_Param, half_width, layer, color, op_logic );
313 314 315
        return;
    }

316
    // The segment is here a line segment.
317
    if( ( ux0 != ux1 ) && ( uy0 != uy1 ) ) // Segment tilts.
318
    {
319
        DrawSegmentQcq( ux0, uy0, ux1, uy1, half_width, layer, color, op_logic );
320 321 322
        return;
    }

323 324
    // The segment is horizontal or vertical.
    // F4EXB 051018-01
325
    DrawSegmentQcq( ux0, uy0, ux1, uy1, half_width, layer, color, op_logic );
326 327 328
}


329
/* Draws a line, if layer = -1 on all layers
330
 */
331
void TracePcbLine( int x0, int y0, int x1, int y1, LAYER_NUM layer, int color, int op_logic  )
332
{
333 334 335
    int  dx, dy, lim;
    int  cumul, inc, il, delta;

336
    RoutingMatrix.SetCellOperation( op_logic );
337

338
    if( x0 == x1 )  // Vertical.
339 340 341
    {
        if( y1 < y0 )
            EXCHG( y0, y1 );
342

343 344 345
        dy  = y0 / RoutingMatrix.m_GridRouting;
        lim = y1 / RoutingMatrix.m_GridRouting;
        dx  = x0 / RoutingMatrix.m_GridRouting;
346

347
        // Clipping limits of board.
348
        if( ( dx < 0 ) || ( dx >= RoutingMatrix.m_Ncols ) )
349
            return;
350

351 352
        if( dy < 0 )
            dy = 0;
353

354 355
        if( lim >= RoutingMatrix.m_Nrows )
            lim = RoutingMatrix.m_Nrows - 1;
356

357 358 359 360 361 362 363 364
        for( ; dy <= lim; dy++ )
        {
            OP_CELL( layer, dy, dx );
        }

        return;
    }

365
    if( y0 == y1 )  // Horizontal
366 367 368
    {
        if( x1 < x0 )
            EXCHG( x0, x1 );
369

370 371 372
        dx  = x0 / RoutingMatrix.m_GridRouting;
        lim = x1 / RoutingMatrix.m_GridRouting;
        dy  = y0 / RoutingMatrix.m_GridRouting;
373

374
        // Clipping limits of board.
375
        if( ( dy < 0 ) || ( dy >= RoutingMatrix.m_Nrows ) )
376
            return;
377

378 379
        if( dx < 0 )
            dx = 0;
380

381 382
        if( lim >= RoutingMatrix.m_Ncols )
            lim = RoutingMatrix.m_Ncols - 1;
383

384 385 386 387 388 389 390 391
        for( ; dx <= lim; dx++ )
        {
            OP_CELL( layer, dy, dx );
        }

        return;
    }

392 393
    // Here is some perspective: using the algorithm LUCAS.
    if( abs( x1 - x0 ) >= abs( y1 - y0 ) ) // segment slightly inclined/
394 395 396 397 398 399
    {
        if( x1 < x0 )
        {
            EXCHG( x1, x0 ); EXCHG( y1, y0 );
        }

400 401 402
        dx  = x0 / RoutingMatrix.m_GridRouting;
        lim = x1 / RoutingMatrix.m_GridRouting;
        dy  = y0 / RoutingMatrix.m_GridRouting;
403 404 405
        inc = 1;

        if( y1 < y0 )
406
            inc = -1;
407

408
        il    = lim - dx; cumul = il / 2;
409
        delta = abs( y1 - y0 ) / RoutingMatrix.m_GridRouting;
410

411 412
        for( ; dx <= lim; )
        {
413 414 415
            if( ( dx >= 0 ) && ( dy >= 0 ) &&
                ( dx < RoutingMatrix.m_Ncols ) &&
                ( dy < RoutingMatrix.m_Nrows ) )
416 417 418
            {
                OP_CELL( layer, dy, dx );
            }
419 420 421 422

            dx++;
            cumul += delta;

423 424
            if( cumul > il )
            {
425 426
                cumul -= il;
                dy    += inc;
427 428 429 430 431 432 433
            }
        }
    }
    else
    {
        if( y1 < y0 )
        {
434 435
            EXCHG( x1, x0 );
            EXCHG( y1, y0 );
436 437
        }

438 439 440
        dy  = y0 / RoutingMatrix.m_GridRouting;
        lim = y1 / RoutingMatrix.m_GridRouting;
        dx  = x0 / RoutingMatrix.m_GridRouting;
441
        inc = 1;
442

443
        if( x1 < x0 )
444
            inc = -1;
445

446 447 448
        il    = lim - dy;
        cumul = il / 2;
        delta = abs( x1 - x0 ) / RoutingMatrix.m_GridRouting;
449

450 451
        for( ; dy <= lim; )
        {
452
            if( ( dx >= 0 ) && ( dy >= 0 ) && ( dx < RoutingMatrix.m_Ncols ) && ( dy < RoutingMatrix.m_Nrows ) )
453 454 455
            {
                OP_CELL( layer, dy, dx );
            }
456 457 458 459

            dy++;
            cumul += delta;

460 461
            if( cumul > il )
            {
462 463
                cumul -= il;
                dx    += inc;
464 465 466
            }
        }
    }
467 468
}

469

470
void TraceFilledRectangle( int ux0, int uy0, int ux1, int uy1,
471
                           int aLayerMask, int color, int op_logic )
472
{
473 474 475 476
    int  row, col;
    int  row_min, row_max, col_min, col_max;
    int  trace = 0;

477
    if( ( aLayerMask & GetLayerMask( g_Route_Layer_BOTTOM ) ) )
478
        trace = 1;     // Trace on BOTTOM
479

480
    if( ( aLayerMask & GetLayerMask( g_Route_Layer_TOP ) ) &&
481
        RoutingMatrix.m_RoutingLayersCount > 1 )
482
        trace |= 2;    // Trace on TOP
483 484 485 486

    if( trace == 0 )
        return;

487
    RoutingMatrix.SetCellOperation( op_logic );
488

489 490 491 492
    ux0 -= RoutingMatrix.GetBrdCoordOrigin().x;
    uy0 -= RoutingMatrix.GetBrdCoordOrigin().y;
    ux1 -= RoutingMatrix.GetBrdCoordOrigin().x;
    uy1 -= RoutingMatrix.GetBrdCoordOrigin().y;
493

494
    // Calculating limits coord cells belonging to the rectangle.
495 496 497
    row_max = uy1 / RoutingMatrix.m_GridRouting;
    col_max = ux1 / RoutingMatrix.m_GridRouting;
    row_min = uy0 / RoutingMatrix.m_GridRouting;
498

499
    if( uy0 > row_min * RoutingMatrix.m_GridRouting )
500
        row_min++;
501

502
    col_min = ux0 / RoutingMatrix.m_GridRouting;
503

504
    if( ux0 > col_min * RoutingMatrix.m_GridRouting )
505 506 507 508
        col_min++;

    if( row_min < 0 )
        row_min = 0;
509

510 511
    if( row_max >= ( RoutingMatrix.m_Nrows - 1 ) )
        row_max = RoutingMatrix.m_Nrows - 1;
512

513 514
    if( col_min < 0 )
        col_min = 0;
515

516 517
    if( col_max >= ( RoutingMatrix.m_Ncols - 1 ) )
        col_max = RoutingMatrix.m_Ncols - 1;
518 519 520 521 522 523

    for( row = row_min; row <= row_max; row++ )
    {
        for( col = col_min; col <= col_max; col++ )
        {
            if( trace & 1 )
524
                RoutingMatrix.WriteCell( row, col, BOTTOM, color );
525

526
            if( trace & 2 )
527
                RoutingMatrix.WriteCell( row, col, TOP, color );
528 529
        }
    }
530 531 532
}


533
void TraceFilledRectangle( int ux0, int uy0, int ux1, int uy1,
534
                           double angle, LAYER_MSK aLayerMask, int color, int op_logic )
535
{
536
    int  row, col;
537 538
    int  cx, cy;    // Center of rectangle
    int  radius;     // Radius of the circle
539 540 541 542
    int  row_min, row_max, col_min, col_max;
    int  rotrow, rotcol;
    int  trace = 0;

543
    if( aLayerMask & GetLayerMask( g_Route_Layer_BOTTOM ) )
544
        trace = 1;     // Trace on BOTTOM
545

546
    if( aLayerMask & GetLayerMask( g_Route_Layer_TOP ) )
547
    {
548
        if( RoutingMatrix.m_RoutingLayersCount > 1 )
549
            trace |= 2;  // Trace on TOP
550
    }
551 552 553 554

    if( trace == 0 )
        return;

555
    RoutingMatrix.SetCellOperation( op_logic );
556

557 558 559 560
    ux0 -= RoutingMatrix.GetBrdCoordOrigin().x;
    uy0 -= RoutingMatrix.GetBrdCoordOrigin().y;
    ux1 -= RoutingMatrix.GetBrdCoordOrigin().x;
    uy1 -= RoutingMatrix.GetBrdCoordOrigin().y;
561 562 563

    cx    = (ux0 + ux1) / 2;
    cy    = (uy0 + uy1) / 2;
564
    radius = KiROUND( Distance( ux0, uy0, cx, cy ) );
565

566
    // Calculating coordinate limits belonging to the rectangle.
567 568 569
    row_max = ( cy + radius ) / RoutingMatrix.m_GridRouting;
    col_max = ( cx + radius ) / RoutingMatrix.m_GridRouting;
    row_min = ( cy - radius ) / RoutingMatrix.m_GridRouting;
570

571
    if( uy0 > row_min * RoutingMatrix.m_GridRouting )
572
        row_min++;
573

574
    col_min = ( cx - radius ) / RoutingMatrix.m_GridRouting;
575

576
    if( ux0 > col_min * RoutingMatrix.m_GridRouting )
577 578 579 580
        col_min++;

    if( row_min < 0 )
        row_min = 0;
581

582 583
    if( row_max >= ( RoutingMatrix.m_Nrows - 1 ) )
        row_max = RoutingMatrix.m_Nrows - 1;
584

585 586
    if( col_min < 0 )
        col_min = 0;
587

588 589
    if( col_max >= ( RoutingMatrix.m_Ncols - 1 ) )
        col_max = RoutingMatrix.m_Ncols - 1;
590 591 592 593 594

    for( row = row_min; row <= row_max; row++ )
    {
        for( col = col_min; col <= col_max; col++ )
        {
595 596
            rotrow = row * RoutingMatrix.m_GridRouting;
            rotcol = col * RoutingMatrix.m_GridRouting;
597
            RotatePoint( &rotcol, &rotrow, cx, cy, -angle );
598

599 600
            if( rotrow <= uy0 )
                continue;
601

602 603
            if( rotrow >= uy1 )
                continue;
604

605 606
            if( rotcol <= ux0 )
                continue;
607

608 609
            if( rotcol >= ux1 )
                continue;
610

611
            if( trace & 1 )
612
                RoutingMatrix.WriteCell( row, col, BOTTOM, color );
613

614
            if( trace & 2 )
615
                RoutingMatrix.WriteCell( row, col, TOP, color );
616 617
        }
    }
618 619 620
}


621
/* Fills all cells inside a segment
622 623
 * half-width = lg, org = ux0,uy0 end = ux1,uy1
 * coordinates are in PCB units
624
 */
625
void DrawSegmentQcq( int ux0, int uy0, int ux1, int uy1, int lg, LAYER_NUM layer,
626
                     int color, int op_logic )
627
{
628 629 630 631 632 633 634
    int  row, col;
    int  inc;
    int  row_max, col_max, row_min, col_min;
    int  demi_pas;

    int  cx, cy, dx, dy;

635
    RoutingMatrix.SetCellOperation( op_logic );
636

637
    // Make coordinate ux1 tj > ux0 to simplify calculations
638 639
    if( ux1 < ux0 )
    {
640 641
        EXCHG( ux1, ux0 );
        EXCHG( uy1, uy0 );
642 643
    }

644
    // Calculating the incrementing the Y axis
645
    inc = 1;
646

647
    if( uy1 < uy0 )
648 649
        inc = -1;

650
    demi_pas = RoutingMatrix.m_GridRouting / 2;
651

652
    col_min = ( ux0 - lg ) / RoutingMatrix.m_GridRouting;
653

654 655
    if( col_min < 0 )
        col_min = 0;
656

657
    col_max = ( ux1 + lg + demi_pas ) / RoutingMatrix.m_GridRouting;
658

659 660
    if( col_max > ( RoutingMatrix.m_Ncols - 1 ) )
        col_max = RoutingMatrix.m_Ncols - 1;
661 662 663

    if( inc > 0 )
    {
664 665
        row_min = ( uy0 - lg ) / RoutingMatrix.m_GridRouting;
        row_max = ( uy1 + lg + demi_pas ) / RoutingMatrix.m_GridRouting;
666 667 668
    }
    else
    {
669 670
        row_min = ( uy1 - lg ) / RoutingMatrix.m_GridRouting;
        row_max = ( uy0 + lg + demi_pas ) / RoutingMatrix.m_GridRouting;
671 672 673 674
    }

    if( row_min < 0 )
        row_min = 0;
675

676 677
    if( row_min > ( RoutingMatrix.m_Nrows - 1 ) )
        row_min = RoutingMatrix.m_Nrows - 1;
678

679 680
    if( row_max < 0 )
        row_max = 0;
681

682 683
    if( row_max > ( RoutingMatrix.m_Nrows - 1 ) )
        row_max = RoutingMatrix.m_Nrows - 1;
684

685 686
    dx = ux1 - ux0;
    dy = uy1 - uy0;
687

688
    double angle;
689
    if( dx )
690
    {
691
        angle = ArcTangente( dy, dx );
692
    }
693 694
    else
    {
695
        angle = 900;
696

697
        if( dy < 0 )
698 699 700
            angle = -900;
    }

701
    RotatePoint( &dx, &dy, angle );   // dx = length, dy = 0
702

703 704 705
    for( col = col_min; col <= col_max; col++ )
    {
        int cxr;
706
        cxr = ( col * RoutingMatrix.m_GridRouting ) - ux0;
707

708
        for( row = row_min; row <= row_max; row++ )
709
        {
710
            cy = (row * RoutingMatrix.m_GridRouting) - uy0;
711 712
            cx = cxr;
            RotatePoint( &cx, &cy, angle );
713

714
            if( abs( cy ) > lg )
715
                continue;             // The point is too far on the Y axis.
716

717 718 719 720
            /* This point a test is close to the segment: the position
             * along the X axis must be tested.
             */
            if( ( cx >= 0 ) && ( cx <= dx ) )
721 722 723 724
            {
                OP_CELL( layer, row, col );
                continue;
            }
725

726
            // Examination of extremities are rounded.
727
            if( ( cx < 0 ) && ( cx >= -lg ) )
728
            {
729
                if( ( ( cx * cx ) + ( cy * cy ) ) <= ( lg * lg ) )
730
                    OP_CELL( layer, row, col );
731

732 733
                continue;
            }
734

735
            if( ( cx > dx ) && ( cx <= ( dx + lg ) ) )
736
            {
737
                if( ( ( ( cx - dx ) * ( cx - dx ) ) + ( cy * cy ) ) <= ( lg * lg ) )
738
                    OP_CELL( layer, row, col );
739

740 741 742 743
                continue;
            }
        }
    }
744 745
}

746

747 748 749
/* Fills all cells of the routing matrix contained in the circle
 * half-width = lg, center = ux0, uy0, ux1,uy1 is a point on the circle.
 * coord are in PCB units.
750
 */
751
void TraceCircle( int ux0, int uy0, int ux1, int uy1, int lg, LAYER_NUM layer,
752
                  int color, int op_logic )
753
{
754
    int radius, nb_segm;
755 756
    int x0, y0,             // Starting point of the current segment trace.
        x1, y1;             // End point.
757 758 759
    int ii;
    int angle;

760
    radius = KiROUND( Distance( ux0, uy0, ux1, uy1 ) ); 
761

762
    x0 = x1 = radius;
763
    y0 = y1 = 0;
764

765 766
    if( lg < 1 )
        lg = 1;
767

768
    nb_segm = ( 2 * radius ) / lg;
769

770 771
    if( nb_segm < 5 )
        nb_segm = 5;
772

773 774
    if( nb_segm > 100 )
        nb_segm = 100;
775

776 777 778
    for( ii = 1; ii < nb_segm; ii++ )
    {
        angle = (3600 * ii) / nb_segm;
779 780
        x1    = KiROUND( cosdecideg( radius, angle ) );
        y1    = KiROUND( sindecideg( radius, angle ) );
781
        DrawSegmentQcq( x0 + ux0, y0 + uy0, x1 + ux0, y1 + uy0, lg, layer, color, op_logic );
782 783
        x0 = x1;
        y0 = y1;
784 785
    }

786
    DrawSegmentQcq( x1 + ux0, y1 + uy0, ux0 + radius, uy0, lg, layer, color, op_logic );
787 788
}

789

790 791 792 793
/* Fills all routing matrix cells contained in the arc
 * angle = ArcAngle, half-width lg
 * center = ux0,uy0, starting at ux1, uy1.  Coordinates are in
 * PCB units.
794
 */
795
void TraceArc( int ux0, int uy0, int ux1, int uy1, double ArcAngle, int lg,
796
               LAYER_NUM layer, int color, int op_logic )
797
{
798
    int radius, nb_segm;
799 800
    int x0, y0,             // Starting point of the current segment trace
        x1, y1;             // End point
801
    int ii;
802
    double angle, StAngle;
803 804


805
    radius = KiROUND( Distance( ux0, uy0, ux1, uy1 ) );
806

807 808
    x0 = ux1 - ux0;
    y0 = uy1 - uy0;
809
    StAngle = ArcTangente( uy1 - uy0, ux1 - ux0 );
810

811 812
    if( lg < 1 )
        lg = 1;
813

814
    nb_segm = ( 2 * radius ) / lg;
815
    nb_segm = ( nb_segm * std::abs( ArcAngle ) ) / 3600;
816

817 818
    if( nb_segm < 5 )
        nb_segm = 5;
819

820 821 822 823 824
    if( nb_segm > 100 )
        nb_segm = 100;

    for( ii = 1; ii <= nb_segm; ii++ )
    {
825
        angle  = ( ArcAngle * ii ) / nb_segm;
826 827
        angle += StAngle;

828
        NORMALIZE_ANGLE_POS( angle );
829

830 831
        x1 = KiROUND( cosdecideg( radius, angle ) );
        y1 = KiROUND( cosdecideg( radius, angle ) );
832
        DrawSegmentQcq( x0 + ux0, y0 + uy0, x1 + ux0, y1 + uy0, lg, layer, color, op_logic );
833 834
        x0 = x1;
        y0 = y1;
835
    }
836
}