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, see <https://www.gnu.org/licenses/>.
19 */
20
25
26#pragma once
27
28#include <algorithm>
29#include <math.h> // for copysign
30#include <stdlib.h> // for abs
31#include <math/box2.h>
32#include <geometry/eda_angle.h>
34#include <geometry/shape_rect.h>
37
41enum class LEADER_MODE
42{
46};
47
56int GetArcToSegmentCount( int aRadius, int aErrorMax, const EDA_ANGLE& aArcAngle );
57
66int CircleToEndSegmentDeltaRadius( int aInnerCircleRadius, int aSegCount );
67
84
94int GetCircleToPolyCorrection( int aMaxError );
95
107template<typename T>
108VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
109{
110 using ext_type = typename VECTOR2<T>::extended_type;
111
112 auto newVec = aVec;
113 const VECTOR2<T> absVec{ std::abs( aVec.x ), std::abs( aVec.y ) };
114
115 if( !only45 && absVec.x > ext_type( absVec.y ) * 2 )
116 {
117 // snap along x-axis
118 newVec.y = 0;
119 }
120 else if( !only45 && absVec.y > ext_type( absVec.x ) * 2 )
121 {
122 // snap onto y-axis
123 newVec.x = 0;
124 }
125 else if( absVec.x > absVec.y )
126 {
127 // snap away from x-axis towards 45
128 newVec.y = std::copysign( aVec.x, aVec.y );
129 }
130 else
131 {
132 // snap away from y-axis towards 45
133 newVec.x = std::copysign( aVec.y, aVec.x );
134 }
135
136 return newVec;
137}
138
139
150template <typename T>
152{
153 auto newVec = aVec;
154 const VECTOR2<T> absVec{ std::abs( aVec.x ), std::abs( aVec.y ) };
155
156 if( absVec.x >= absVec.y )
157 newVec.y = 0;
158 else
159 newVec.x = 0;
160
161 return newVec;
162}
163
176template <typename in_type, typename ret_type = in_type, typename pad_type = unsigned int,
177 typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
178VECTOR2<ret_type> GetClampedCoords( const VECTOR2<in_type>& aCoords, pad_type aPadding = 1u )
179{
180 typedef std::numeric_limits<int32_t> coord_limits;
181
182 in_type x = aCoords.x;
183 in_type y = aCoords.y;
184
185 if constexpr( !std::is_floating_point_v<in_type> )
186 {
187 int64_t max = static_cast<int64_t>( coord_limits::max() ) - aPadding;
188 int64_t min = -max;
189 x = std::clamp<int64_t>( static_cast<int64_t>( x ), min, max );
190 y = std::clamp<int64_t>( static_cast<int64_t>( y ), min, max );
191 }
192 else
193 {
194 double max = static_cast<double>( coord_limits::max() ) - aPadding;
195 double min = -max;
196 x = std::clamp<double>( static_cast<double>( x ), min, max );
197 y = std::clamp<double>( static_cast<double>( y ), min, max );
198 }
199
200 if constexpr( !std::is_integral_v<in_type> && std::is_integral_v<ret_type> )
201 {
203 KiROUND<in_type, ret_type>( y, true ) );
204 }
205 else
206 {
207 return VECTOR2<ret_type>( x, y );
208 }
209}
210
211
215template <typename T>
216inline bool IsVec2SafeXY( const VECTOR2<T>& aVec )
217{
218 constexpr T min = std::numeric_limits<int>::min();
219 constexpr T max = std::numeric_limits<int>::max();
220
221 return aVec.x > min && aVec.x < max && aVec.y > min && aVec.y < max;
222}
223
224
238bool ClipLine( const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2 );
239
240
241namespace KIGEOM
242{
250bool BoxHitTest( const VECTOR2I& aHitPoint, const BOX2I& aHittee, int aAccuracy );
251
262bool BoxHitTest( const BOX2I& aHitter, const BOX2I& aHittee, bool aHitteeContained, int aAccuracy );
263
273bool BoxHitTest( const SHAPE_LINE_CHAIN& aHitter, const BOX2I& aHittee, bool aHitteeContained );
274
286bool BoxHitTest( const SHAPE_LINE_CHAIN& aHitter, const BOX2I& aHittee, const EDA_ANGLE& aHitteeRotation,
287 const VECTOR2I& aHitteeRotationCenter, bool aHitteeContained );
288
298bool ShapeHitTest( const SHAPE_LINE_CHAIN& aHitter, const SHAPE& aHittee, bool aHitteeContained );
299}; // namespace KIGEOM
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
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:124
Define a general 2D-vector/point.
Definition vector2d.h:67
VECTOR2_TRAITS< T >::extended_type extended_type
Definition vector2d.h:69
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:683