KiCad PCB EDA Suite
geometry_utils.h
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-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
30#ifndef GEOMETRY_UTILS_H
31#define GEOMETRY_UTILS_H
32
33#include <math.h> // for copysign
34#include <stdlib.h> // for abs
35#include <math/box2.h>
36#include <geometry/eda_angle.h>
37
44
53int GetArcToSegmentCount( int aRadius, int aErrorMax, const EDA_ANGLE& aArcAngle );
54
63int CircleToEndSegmentDeltaRadius( int aInnerCircleRadius, int aSegCount );
64
76{
77public:
80};
81
91int GetCircleToPolyCorrection( int aMaxError );
92
104template<typename T>
105VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
106{
107 auto newVec = aVec;
108 const VECTOR2<T> absVec { std::abs( aVec.x ), std::abs( aVec.y ) };
109
110 if ( !only45 && absVec.x > absVec.y * 2 )
111 {
112 // snap along x-axis
113 newVec.y = 0;
114 }
115 else if ( !only45 && absVec.y > absVec.x * 2 )
116 {
117 // snap onto y-axis
118 newVec.x = 0;
119 }
120 else if ( absVec.x > absVec.y )
121 {
122 // snap away from x-axis towards 45
123 newVec.y = std::copysign( aVec.x, aVec.y );
124 } else
125 {
126 // snap away from y-axis towards 45
127 newVec.x = std::copysign( aVec.y, aVec.x );
128 }
129
130 return newVec;
131}
132
133
146template <typename in_type, typename ret_type = in_type, typename pad_type = unsigned int,
147 typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
148VECTOR2<ret_type> GetClampedCoords( const VECTOR2<in_type>& aCoords, pad_type aPadding = 0u )
149{
150 typedef std::numeric_limits<int> coord_limits;
151
152 long max = coord_limits::max() - aPadding;
153 long min = -max;
154
155 in_type x = aCoords.x;
156 in_type y = aCoords.y;
157
158 if( x < min )
159 x = min;
160 else if( x > max )
161 x = max;
162
163 if( y < min )
164 y = min;
165 else if( y > max )
166 y = max;
167
168 if( !std::is_integral<in_type>() && std::is_integral<ret_type>() )
169 return VECTOR2<ret_type>( KiROUND( x ), KiROUND( y ) );
170
171 return VECTOR2<ret_type>( x, y );
172}
173
174
188bool ClipLine( const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2 );
189
190
191#endif // #ifndef GEOMETRY_UTILS_H
192
When creating polygons to create a clearance polygonal area, the polygon must be same or bigger than ...
Define a general 2D-vector/point.
Definition: vector2d.h:74
int GetCircleToPolyCorrection(int aMaxError)
int CircleToEndSegmentDeltaRadius(int aInnerCircleRadius, int aSegCount)
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ ERROR_OUTSIDE
@ ERROR_INSIDE
VECTOR2< T > GetVectorSnapped45(const VECTOR2< T > &aVec, bool only45=false)
Snap a vector onto the nearest 0, 45 or 90 degree line.
bool ClipLine(const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2)
Test if any part of a line falls within the bounds of a rectangle.
int GetArcToSegmentCount(int aRadius, int aErrorMax, const EDA_ANGLE &aArcAngle)
VECTOR2< ret_type > GetClampedCoords(const VECTOR2< in_type > &aCoords, pad_type aPadding=0u)
Clamps a vector to values that can be negated, respecting numeric limits of coordinates data type wit...
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401
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:80