KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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#pragma once
31
32#include <algorithm>
33#include <math.h> // for copysign
34#include <stdlib.h> // for abs
35#include <math/box2.h>
36#include <geometry/eda_angle.h>
37
46int GetArcToSegmentCount( int aRadius, int aErrorMax, const EDA_ANGLE& aArcAngle );
47
56int CircleToEndSegmentDeltaRadius( int aInnerCircleRadius, int aSegCount );
57
69{
70public:
73};
74
84int GetCircleToPolyCorrection( int aMaxError );
85
97template<typename T>
98VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
99{
100 using ext_type = typename VECTOR2<T>::extended_type;
101
102 auto newVec = aVec;
103 const VECTOR2<T> absVec{ std::abs( aVec.x ), std::abs( aVec.y ) };
104
105 if( !only45 && absVec.x > ext_type( absVec.y ) * 2 )
106 {
107 // snap along x-axis
108 newVec.y = 0;
109 }
110 else if( !only45 && absVec.y > ext_type( absVec.x ) * 2 )
111 {
112 // snap onto y-axis
113 newVec.x = 0;
114 }
115 else if( absVec.x > absVec.y )
116 {
117 // snap away from x-axis towards 45
118 newVec.y = std::copysign( aVec.x, aVec.y );
119 }
120 else
121 {
122 // snap away from y-axis towards 45
123 newVec.x = std::copysign( aVec.y, aVec.x );
124 }
125
126 return newVec;
127}
128
129
142template <typename in_type, typename ret_type = in_type, typename pad_type = unsigned int,
143 typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
144VECTOR2<ret_type> GetClampedCoords( const VECTOR2<in_type>& aCoords, pad_type aPadding = 1u )
145{
146 typedef std::numeric_limits<int32_t> coord_limits;
147
148 in_type x = aCoords.x;
149 in_type y = aCoords.y;
150
151 if constexpr( !std::is_floating_point_v<in_type> )
152 {
153 int64_t max = static_cast<int64_t>( coord_limits::max() ) - aPadding;
154 int64_t min = -max;
155 x = std::clamp<int64_t>( static_cast<int64_t>( x ), min, max );
156 y = std::clamp<int64_t>( static_cast<int64_t>( y ), min, max );
157 }
158 else
159 {
160 double max = static_cast<double>( coord_limits::max() ) - aPadding;
161 double min = -max;
162 x = std::clamp<double>( static_cast<double>( x ), min, max );
163 y = std::clamp<double>( static_cast<double>( y ), min, max );
164 }
165
166 if constexpr( !std::is_integral_v<in_type> && std::is_integral_v<ret_type> )
167 {
168 return VECTOR2<ret_type>( KiROUND<in_type, ret_type>( x, true ),
169 KiROUND<in_type, ret_type>( y, true ) );
170 }
171
172 return VECTOR2<ret_type>( x, y );
173}
174
175
179template <typename T>
180inline bool IsVec2SafeXY( const VECTOR2<T>& aVec )
181{
182 constexpr T min = std::numeric_limits<int>::min();
183 constexpr T max = std::numeric_limits<int>::max();
184
185 return aVec.x > min && aVec.x < max && aVec.y > min && aVec.y < max;
186}
187
188
202bool ClipLine( const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2 );
203
204
205namespace KIGEOM
206{
214bool BoxHitTest( const VECTOR2I& aHitPoint, const BOX2I& aHittee, int aAccuracy );
215
226bool BoxHitTest( const BOX2I& aHitter, const BOX2I& aHittee, bool aHitteeContained, int aAccuracy );
227}; // namespace KIGEOM
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:71
VECTOR2_TRAITS< T >::extended_type extended_type
Definition: vector2d.h:73
int GetCircleToPolyCorrection(int aMaxError)
int CircleToEndSegmentDeltaRadius(int aInnerCircleRadius, int aSegCount)
VECTOR2< T > GetVectorSnapped45(const VECTOR2< T > &aVec, bool only45=false)
Snap a vector onto the nearest 0, 45 or 90 degree line.
bool IsVec2SafeXY(const VECTOR2< T > &aVec)
Check if both coordinates of a vector are within the limits of the integer type.
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=1u)
Clamps a vector to values that can be negated, respecting numeric limits of coordinates data type wit...
bool BoxHitTest(const VECTOR2I &aHitPoint, const BOX2I &aHittee, int aAccuracy)
Perform a point-to-box hit test.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:390