geometry/geometry_utils.cpp: Fix a minimal value (6) to calculate the segments by circle count to approximate a circle.

Use also this minimal value to calculate  the circle to poly correction factor
Also, remove a useless code.
This commit is contained in:
jean-pierre charras 2019-04-02 09:03:47 +02:00
parent abc605fcfa
commit 0d2dd48cad
1 changed files with 21 additions and 10 deletions

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -30,6 +30,9 @@
#include <eda_rect.h> #include <eda_rect.h>
#include <geometry/geometry_utils.h> #include <geometry/geometry_utils.h>
// To approximate a circle by segments, a minimal seg count is mandatory
#define MIN_SEGCOUNT_FOR_CIRCLE 6
int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree ) int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree )
{ {
// calculate the number of segments to approximate a circle by segments // calculate the number of segments to approximate a circle by segments
@ -38,11 +41,15 @@ int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree )
// error relative to the radius value: // error relative to the radius value:
double rel_error = (double)aErrorMax / aRadius; double rel_error = (double)aErrorMax / aRadius;
// minimal arc increment in degrees: // minimal arc increment in degrees:
double step = 180 / M_PI * acos( 1.0 - rel_error ) * 2; double arc_increment = 180 / M_PI * acos( 1.0 - rel_error ) * 2;
// the minimal seg count for a arc
int segCount = round_nearest( fabs( aArcAngleDegree ) / step );
// Ensure at least one segment is used // Ensure a minimal arc increment reasonable value for a circle
// (360.0 degrees). For very small radius values, this is mandatory.
arc_increment = std::min( 360.0/MIN_SEGCOUNT_FOR_CIRCLE, arc_increment );
int segCount = round_nearest( fabs( aArcAngleDegree ) / arc_increment );
// Ensure at least one segment is used (can happen for small arcs)
return std::max( segCount, 1 ); return std::max( segCount, 1 );
} }
@ -53,12 +60,12 @@ double GetCircletoPolyCorrectionFactor( int aSegCountforCircle )
* due to the segment approx. * due to the segment approx.
* For a circle the min radius is radius * cos( 2PI / aSegCountforCircle / 2) * For a circle the min radius is radius * cos( 2PI / aSegCountforCircle / 2)
* this is the distance between the center and the middle of the segment. * this is the distance between the center and the middle of the segment.
* therfore, to move the middle of the segment to the circle (distance = radius) * therefore, to move the middle of the segment to the circle (distance = radius)
* the correctionFactor is 1 /cos( PI/aSegCountforCircle ) * the correctionFactor is 1 /cos( PI/aSegCountforCircle )
*/ */
if( aSegCountforCircle < 6 ) if( aSegCountforCircle < MIN_SEGCOUNT_FOR_CIRCLE )
aSegCountforCircle = 6; aSegCountforCircle = MIN_SEGCOUNT_FOR_CIRCLE;
if( 1 || aSegCountforCircle > 64 )
return 1.0 / cos( M_PI / aSegCountforCircle ); return 1.0 / cos( M_PI / aSegCountforCircle );
} }
@ -70,16 +77,19 @@ double GetCircletoPolyCorrectionFactor( int aSegCountforCircle )
inline int clipOutCode( const EDA_RECT *aClipBox, int x, int y ) inline int clipOutCode( const EDA_RECT *aClipBox, int x, int y )
{ {
int code; int code;
if( y < aClipBox->GetY() ) if( y < aClipBox->GetY() )
code = 2; code = 2;
else if( y > aClipBox->GetBottom() ) else if( y > aClipBox->GetBottom() )
code = 1; code = 1;
else else
code = 0; code = 0;
if( x < aClipBox->GetX() ) if( x < aClipBox->GetX() )
code |= 4; code |= 4;
else if( x > aClipBox->GetRight() ) else if( x > aClipBox->GetRight() )
code |= 8; code |= 8;
return code; return code;
} }
@ -98,6 +108,7 @@ bool ClipLine( const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2 )
// Choose a side to clip // Choose a side to clip
int thisoutcode, x, y; int thisoutcode, x, y;
if( outcode1 ) if( outcode1 )
thisoutcode = outcode1; thisoutcode = outcode1;
else else