KiCad PCB EDA Suite
geometry_utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <stdint.h> // for int64_t
31 #include <algorithm> // for max, min
32 
33 #include <eda_rect.h>
35 #include <math/util.h> // for KiROUND
36 
37 // To approximate a circle by segments, a minimal seg count is mandatory.
38 // Note that this is rarely used as the maxError constraint will yield a higher
39 // segment count on everything but very small circles. (Even a 0.125mm track
40 // with a 0.01mm maximum deviation yields 11 segments.)
41 #define MIN_SEGCOUNT_FOR_CIRCLE 8
42 
43 int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree )
44 {
45  // calculate the number of segments to approximate a circle by segments
46  // given the max distance between the middle of a segment and the circle
47 
48  // avoid divide-by-zero
49  aRadius = std::max( 1, aRadius );
50 
51  // error relative to the radius value:
52  double rel_error = (double)aErrorMax / aRadius;
53  // minimal arc increment in degrees:
54  double arc_increment = 180 / M_PI * acos( 1.0 - rel_error ) * 2;
55 
56  // Ensure a minimal arc increment reasonable value for a circle
57  // (360.0 degrees). For very small radius values, this is mandatory.
58  arc_increment = std::min( 360.0/MIN_SEGCOUNT_FOR_CIRCLE, arc_increment );
59 
60  int segCount = KiROUND( fabs( aArcAngleDegree ) / arc_increment );
61 
62  // Ensure at least two segments are used for algorithmic safety
63  return std::max( segCount, 2 );
64 }
65 
66 // When creating polygons to create a clearance polygonal area, the polygon must
67 // be same or bigger than the original shape.
68 // Polygons are bigger if the original shape has arcs (round rectangles, ovals,
69 // circles...). However, when building the solder mask layer modifying the shapes
70 // when converting them to polygons is not acceptable (the modification can break
71 // calculations).
72 // So one can disable the shape expansion within a particular scope by allocating
73 // a DISABLE_ARC_CORRECTION.
74 
75 static bool s_disable_arc_correction = false;
76 
78 {
80 }
81 
83 {
85 }
86 
87 int GetCircleToPolyCorrection( int aMaxError )
88 {
89  // Push all the error to the outside by increasing the radius
90  return s_disable_arc_correction ? 0 : aMaxError;
91 }
92 
93 
94 /***
95  * Utility for the line clipping code, returns the boundary code of
96  * a point. Bit allocation is arbitrary
97  */
98 inline int clipOutCode( const EDA_RECT *aClipBox, int x, int y )
99 {
100  int code;
101 
102  if( y < aClipBox->GetY() )
103  code = 2;
104  else if( y > aClipBox->GetBottom() )
105  code = 1;
106  else
107  code = 0;
108 
109  if( x < aClipBox->GetX() )
110  code |= 4;
111  else if( x > aClipBox->GetRight() )
112  code |= 8;
113 
114  return code;
115 }
116 
117 
118 bool ClipLine( const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2 )
119 {
120  // Stock Cohen-Sutherland algorithm; check *any* CG book for details
121  int outcode1 = clipOutCode( aClipBox, x1, y1 );
122  int outcode2 = clipOutCode( aClipBox, x2, y2 );
123 
124  while( outcode1 || outcode2 )
125  {
126  // Fast reject
127  if( outcode1 & outcode2 )
128  return true;
129 
130  // Choose a side to clip
131  int thisoutcode, x, y;
132 
133  if( outcode1 )
134  thisoutcode = outcode1;
135  else
136  thisoutcode = outcode2;
137 
138  /* One clip round
139  * Since we use the full range of 32 bit ints, the proportion
140  * computation has to be done in 64 bits to avoid horrible
141  * results */
142  if( thisoutcode & 1 ) // Clip the bottom
143  {
144  y = aClipBox->GetBottom();
145  x = x1 + (x2 - x1) * int64_t(y - y1) / (y2 - y1);
146  }
147  else if( thisoutcode & 2 ) // Clip the top
148  {
149  y = aClipBox->GetY();
150  x = x1 + (x2 - x1) * int64_t(y - y1) / (y2 - y1);
151  }
152  else if( thisoutcode & 8 ) // Clip the right
153  {
154  x = aClipBox->GetRight();
155  y = y1 + (y2 - y1) * int64_t(x - x1) / (x2 - x1);
156  }
157  else // if( thisoutcode & 4), obviously, clip the left
158  {
159  x = aClipBox->GetX();
160  y = y1 + (y2 - y1) * int64_t(x - x1) / (x2 - x1);
161  }
162 
163  // Put the result back and update the boundary code
164  // No ambiguity, otherwise it would have been a fast reject
165  if( thisoutcode == outcode1 )
166  {
167  x1 = x;
168  y1 = y;
169  outcode1 = clipOutCode( aClipBox, x1, y1 );
170  }
171  else
172  {
173  x2 = x;
174  y2 = y;
175  outcode2 = clipOutCode( aClipBox, x2, y2 );
176  }
177  }
178  return false;
179 }
180 
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)
int clipOutCode(const EDA_RECT *aClipBox, int x, int y)
int GetCircleToPolyCorrection(int aMaxError)
int GetX() const
Definition: eda_rect.h:111
int GetBottom() const
Definition: eda_rect.h:124
int GetRight() const
Definition: eda_rect.h:121
static bool s_disable_arc_correction
#define MIN_SEGCOUNT_FOR_CIRCLE
a few functions useful in geometry calculations.
bool ClipLine(const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
int GetY() const
Definition: eda_rect.h:112
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68