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 The 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
29
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>
38#include <geometry/shape_rect.h>
41
45enum class LEADER_MODE
46{
50};
51
60int GetArcToSegmentCount( int aRadius, int aErrorMax, const EDA_ANGLE& aArcAngle );
61
70int CircleToEndSegmentDeltaRadius( int aInnerCircleRadius, int aSegCount );
71
88
98int GetCircleToPolyCorrection( int aMaxError );
99
111template<typename T>
112VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
113{
114 using ext_type = typename VECTOR2<T>::extended_type;
115
116 auto newVec = aVec;
117 const VECTOR2<T> absVec{ std::abs( aVec.x ), std::abs( aVec.y ) };
118
119 if( !only45 && absVec.x > ext_type( absVec.y ) * 2 )
120 {
121 // snap along x-axis
122 newVec.y = 0;
123 }
124 else if( !only45 && absVec.y > ext_type( absVec.x ) * 2 )
125 {
126 // snap onto y-axis
127 newVec.x = 0;
128 }
129 else if( absVec.x > absVec.y )
130 {
131 // snap away from x-axis towards 45
132 newVec.y = std::copysign( aVec.x, aVec.y );
133 }
134 else
135 {
136 // snap away from y-axis towards 45
137 newVec.x = std::copysign( aVec.y, aVec.x );
138 }
139
140 return newVec;
141}
142
143
154template <typename T>
156{
157 auto newVec = aVec;
158 const VECTOR2<T> absVec{ std::abs( aVec.x ), std::abs( aVec.y ) };
159
160 if( absVec.x >= absVec.y )
161 newVec.y = 0;
162 else
163 newVec.x = 0;
164
165 return newVec;
166}
167
180template <typename in_type, typename ret_type = in_type, typename pad_type = unsigned int,
181 typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
182VECTOR2<ret_type> GetClampedCoords( const VECTOR2<in_type>& aCoords, pad_type aPadding = 1u )
183{
184 typedef std::numeric_limits<int32_t> coord_limits;
185
186 in_type x = aCoords.x;
187 in_type y = aCoords.y;
188
189 if constexpr( !std::is_floating_point_v<in_type> )
190 {
191 int64_t max = static_cast<int64_t>( coord_limits::max() ) - aPadding;
192 int64_t min = -max;
193 x = std::clamp<int64_t>( static_cast<int64_t>( x ), min, max );
194 y = std::clamp<int64_t>( static_cast<int64_t>( y ), min, max );
195 }
196 else
197 {
198 double max = static_cast<double>( coord_limits::max() ) - aPadding;
199 double min = -max;
200 x = std::clamp<double>( static_cast<double>( x ), min, max );
201 y = std::clamp<double>( static_cast<double>( y ), min, max );
202 }
203
204 if constexpr( !std::is_integral_v<in_type> && std::is_integral_v<ret_type> )
205 {
207 KiROUND<in_type, ret_type>( y, true ) );
208 }
209 else
210 {
211 return VECTOR2<ret_type>( x, y );
212 }
213}
214
215
219template <typename T>
220inline bool IsVec2SafeXY( const VECTOR2<T>& aVec )
221{
222 constexpr T min = std::numeric_limits<int>::min();
223 constexpr T max = std::numeric_limits<int>::max();
224
225 return aVec.x > min && aVec.x < max && aVec.y > min && aVec.y < max;
226}
227
228
242bool ClipLine( const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2 );
243
244
245namespace KIGEOM
246{
254bool BoxHitTest( const VECTOR2I& aHitPoint, const BOX2I& aHittee, int aAccuracy );
255
266bool BoxHitTest( const BOX2I& aHitter, const BOX2I& aHittee, bool aHitteeContained, int aAccuracy );
267
277bool BoxHitTest( const SHAPE_LINE_CHAIN& aHitter, const BOX2I& aHittee, bool aHitteeContained );
278
290bool BoxHitTest( const SHAPE_LINE_CHAIN& aHitter, const BOX2I& aHittee, const EDA_ANGLE& aHitteeRotation,
291 const VECTOR2I& aHitteeRotationCenter, bool aHitteeContained );
292
302bool ShapeHitTest( const SHAPE_LINE_CHAIN& aHitter, const SHAPE& aHittee, bool aHitteeContained );
303}; // namespace KIGEOM
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
An abstract shape on 2D plane.
Definition shape.h:126
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...
LEADER_MODE
The kind of the leader line.
@ DEG45
45 Degree only
@ DIRECT
Unconstrained point-to-point.
@ DEG90
90 Degree only
VECTOR2< T > GetVectorSnapped90(const VECTOR2< T > &aVec)
Snap a vector onto the nearest horizontal or vertical line.
bool ShapeHitTest(const SHAPE_LINE_CHAIN &aHitter, const SHAPE &aHittee, bool aHitteeContained)
Perform a shape-to-shape hit test.
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:400
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695