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-2021 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 
25 /**
26  * @file geometry_utils.h
27  * @brief a few functions useful in geometry calculations.
28  */
29 
30 #ifndef GEOMETRY_UTILS_H
31 #define GEOMETRY_UTILS_H
32 
33 #include <math.h>           // for copysign
34 #include <stdlib.h>         // for abs
35 
36 #include <math/vector2d.h>
37 
38 class EDA_RECT;
39 
40 /**
41  * When approximating an arc or circle, should the error be placed on the outside
42  * or inside of the curve?  (Generally speaking filled shape errors go on the inside
43  * and knockout errors go on the outside.  This preserves minimum clearances.)
44  */
45 enum ERROR_LOC { ERROR_OUTSIDE, ERROR_INSIDE };
46 
47 /**
48  * @return the number of segments to approximate a arc by segments
49  * with a given max error (this number is >= 1)
50  * @param aRadius is the radius od the circle or arc
51  * @param aErrorMax is the max error
52  * This is the max distance between the middle of a segment and the circle.
53  * @param aArcAngleDegree is the arc angle in degrees
54  */
55 int GetArcToSegmentCount( int aRadius, int aErrorMax, double aArcAngleDegree );
56 
57 /**
58  * @return the radius diffence of the circle defined by segments inside the circle
59  * and the radius of the circle tangent to the middle of segments (defined by
60  * segments outside this circle)
61  * @param aInnerCircleRadius is the radius of the circle tangent to the middle
62  * of segments
63  * @param aSegCount is the seg count to approximate the circle
64  */
65 int CircleToEndSegmentDeltaRadius( int aInnerCircleRadius, int aSegCount );
66 
67 /**
68  * When creating polygons to create a clearance polygonal area, the polygon must
69  * be same or bigger than the original shape.
70  * Polygons are bigger if the original shape has arcs (round rectangles, ovals,
71  * circles...).  However, when building the solder mask layer modifying the shapes
72  * when converting them to polygons is not acceptable (the modification can break
73  * calculations).
74  * So one can disable the shape expansion within a particular scope by allocating
75  * a DISABLE_ARC_CORRECTION.
76  */
77 class DISABLE_ARC_RADIUS_CORRECTION
78 {
79 public:
80     DISABLE_ARC_RADIUS_CORRECTION();
81     ~DISABLE_ARC_RADIUS_CORRECTION();
82 };
83 
84 /**
85  * @return the radius correction to approximate a circle.
86  * @param aMaxError is the same error value used to calculate the number of segments.
87  *
88  * When creating a polygon from a circle, the polygon is inside the circle.
89  * Only corners are on the circle.
90  * This is incorrect when building clearance areas of circles, that need to build
91  * the equivalent polygon outside the circle.
92  */
93 int GetCircleToPolyCorrection( int aMaxError );
94 
95 /**
96  * Snap a vector onto the nearest 0, 45 or 90 degree line.
97  *
98  * The magnitude of the vector is NOT kept, instead the coordinates are
99  * set equal (and/or opposite) or to zero as needed. The effect of this is
100  * that if the starting vector is on a square grid, the resulting snapped
101  * vector will still be on the same grid.
102 
103  * @param a vector to be snapped
104  * @return the snapped vector
105  */
106 template<typename T>
107 VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
108 {
109     auto newVec = aVec;
110     const VECTOR2<T> absVec { std::abs( aVec.x ), std::abs( aVec.y ) };
111 
112     if ( !only45 && absVec.x > absVec.y * 2 )
113     {
114         // snap along x-axis
115         newVec.y = 0;
116     }
117     else if ( !only45 && absVec.y > absVec.x * 2 )
118     {
119         // snap onto y-axis
120         newVec.x = 0;
121     }
122     else if ( absVec.x > absVec.y )
123     {
124         // snap away from x-axis towards 45
125         newVec.y = std::copysign( aVec.x, aVec.y );
126     } else
127     {
128         // snap away from y-axis towards 45
129         newVec.x = std::copysign( aVec.y, aVec.x );
130     }
131 
132     return newVec;
133 }
134 
135 
136 /**
137  * Test if any part of a line falls within the bounds of a rectangle.
138  *
139  * Please note that this is only accurate for lines that are one pixel wide.
140  *
141  * @param aClipBox - The rectangle to test.
142  * @param x1 - X coordinate of one end of a line.
143  * @param y1 - Y coordinate of one end of a line.
144  * @param x2 - X coordinate of the other end of a line.
145  * @param y2 - Y coordinate of the other  end of a line.
146  *
147  * @return - False if any part of the line lies within the rectangle.
148  */
149 bool ClipLine( const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2 );
150 
151 
152 /**
153  * Dashed and dotted line patterns.
154  */
155 
dot_mark_len(double aLineWidth)156 constexpr double dot_mark_len( double aLineWidth )
157 {
158     return std::max( 1.0, aLineWidth );
159 }
160 
dash_gap_len(double aLineWidth)161 constexpr double dash_gap_len( double aLineWidth )
162 {
163     return 3.0 * dot_mark_len( aLineWidth ) + ( 2.0 * aLineWidth );
164 }
165 
dash_mark_len(double aLineWidth)166 constexpr double dash_mark_len( double aLineWidth )
167 {
168     return std::max( dash_gap_len( aLineWidth ), 5.0 * dot_mark_len( aLineWidth ) );
169 }
170 
171 #endif  // #ifndef GEOMETRY_UTILS_H
172 
173