2018-03-08 12:48:29 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2021-06-26 16:57:07 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-03-08 12:48:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file geometry_utils.h
|
|
|
|
* @brief a few functions useful in geometry calculations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GEOMETRY_UTILS_H
|
|
|
|
#define GEOMETRY_UTILS_H
|
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math.h> // for copysign
|
|
|
|
#include <stdlib.h> // for abs
|
2022-08-31 16:17:14 +00:00
|
|
|
#include <math/box2.h>
|
2022-01-15 01:12:24 +00:00
|
|
|
#include <geometry/eda_angle.h>
|
2018-09-07 21:33:01 +00:00
|
|
|
|
2020-10-13 10:55:24 +00:00
|
|
|
/**
|
|
|
|
* When approximating an arc or circle, should the error be placed on the outside
|
|
|
|
* or inside of the curve? (Generally speaking filled shape errors go on the inside
|
|
|
|
* and knockout errors go on the outside. This preserves minimum clearances.)
|
|
|
|
*/
|
2023-10-13 11:25:52 +00:00
|
|
|
enum ERROR_LOC { ERROR_OUTSIDE = 0, ERROR_INSIDE };
|
2020-10-13 10:55:24 +00:00
|
|
|
|
2018-03-08 12:48:29 +00:00
|
|
|
/**
|
|
|
|
* @return the number of segments to approximate a arc by segments
|
|
|
|
* with a given max error (this number is >= 1)
|
|
|
|
* @param aRadius is the radius od the circle or arc
|
|
|
|
* @param aErrorMax is the max error
|
|
|
|
* This is the max distance between the middle of a segment and the circle.
|
2022-01-14 15:34:41 +00:00
|
|
|
* @param aArcAngleDegree is the arc angle
|
2018-03-08 12:48:29 +00:00
|
|
|
*/
|
2022-01-14 15:34:41 +00:00
|
|
|
int GetArcToSegmentCount( int aRadius, int aErrorMax, const EDA_ANGLE& aArcAngle );
|
2018-03-08 12:48:29 +00:00
|
|
|
|
2021-06-26 16:57:07 +00:00
|
|
|
/**
|
2021-06-28 16:51:37 +00:00
|
|
|
* @return the radius diffence of the circle defined by segments inside the circle
|
|
|
|
* and the radius of the circle tangent to the middle of segments (defined by
|
|
|
|
* segments outside this circle)
|
|
|
|
* @param aInnerCircleRadius is the radius of the circle tangent to the middle
|
|
|
|
* of segments
|
2021-06-26 16:57:07 +00:00
|
|
|
* @param aSegCount is the seg count to approximate the circle
|
|
|
|
*/
|
2021-06-28 16:51:37 +00:00
|
|
|
int CircleToEndSegmentDeltaRadius( int aInnerCircleRadius, int aSegCount );
|
2021-06-26 16:57:07 +00:00
|
|
|
|
2020-09-10 23:05:20 +00:00
|
|
|
/**
|
|
|
|
* When creating polygons to create a clearance polygonal area, the polygon must
|
2020-03-25 15:16:47 +00:00
|
|
|
* be same or bigger than the original shape.
|
2020-09-10 23:05:20 +00:00
|
|
|
* Polygons are bigger if the original shape has arcs (round rectangles, ovals,
|
|
|
|
* circles...). However, when building the solder mask layer modifying the shapes
|
|
|
|
* when converting them to polygons is not acceptable (the modification can break
|
|
|
|
* calculations).
|
|
|
|
* So one can disable the shape expansion within a particular scope by allocating
|
|
|
|
* a DISABLE_ARC_CORRECTION.
|
2020-03-25 15:16:47 +00:00
|
|
|
*/
|
2020-09-10 23:05:20 +00:00
|
|
|
class DISABLE_ARC_RADIUS_CORRECTION
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DISABLE_ARC_RADIUS_CORRECTION();
|
|
|
|
~DISABLE_ARC_RADIUS_CORRECTION();
|
|
|
|
};
|
2020-03-25 15:16:47 +00:00
|
|
|
|
2018-03-08 12:48:29 +00:00
|
|
|
/**
|
2020-09-10 23:05:20 +00:00
|
|
|
* @return the radius correction to approximate a circle.
|
|
|
|
* @param aMaxError is the same error value used to calculate the number of segments.
|
2018-03-08 12:48:29 +00:00
|
|
|
*
|
|
|
|
* When creating a polygon from a circle, the polygon is inside the circle.
|
|
|
|
* Only corners are on the circle.
|
|
|
|
* This is incorrect when building clearance areas of circles, that need to build
|
2020-09-10 23:05:20 +00:00
|
|
|
* the equivalent polygon outside the circle.
|
2018-03-08 12:48:29 +00:00
|
|
|
*/
|
2020-09-10 23:05:20 +00:00
|
|
|
int GetCircleToPolyCorrection( int aMaxError );
|
2018-03-08 12:48:29 +00:00
|
|
|
|
2018-07-12 12:05:18 +00:00
|
|
|
/**
|
|
|
|
* Snap a vector onto the nearest 0, 45 or 90 degree line.
|
|
|
|
*
|
2021-06-09 19:32:58 +00:00
|
|
|
* The magnitude of the vector is NOT kept, instead the coordinates are
|
2018-07-12 12:05:18 +00:00
|
|
|
* set equal (and/or opposite) or to zero as needed. The effect of this is
|
|
|
|
* that if the starting vector is on a square grid, the resulting snapped
|
|
|
|
* vector will still be on the same grid.
|
|
|
|
|
|
|
|
* @param a vector to be snapped
|
|
|
|
* @return the snapped vector
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2020-09-15 18:21:10 +00:00
|
|
|
VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
|
2018-07-12 12:05:18 +00:00
|
|
|
{
|
2022-12-08 01:43:30 +00:00
|
|
|
using ext_type = typename VECTOR2<T>::extended_type;
|
|
|
|
|
2022-12-08 00:58:36 +00:00
|
|
|
auto newVec = aVec;
|
|
|
|
const VECTOR2<T> absVec{ std::abs( aVec.x ), std::abs( aVec.y ) };
|
2018-07-12 12:05:18 +00:00
|
|
|
|
2022-12-08 01:43:30 +00:00
|
|
|
if( !only45 && absVec.x > ext_type( absVec.y ) * 2 )
|
2018-07-12 12:05:18 +00:00
|
|
|
{
|
|
|
|
// snap along x-axis
|
|
|
|
newVec.y = 0;
|
|
|
|
}
|
2022-12-08 01:43:30 +00:00
|
|
|
else if( !only45 && absVec.y > ext_type( absVec.x ) * 2 )
|
2018-07-12 12:05:18 +00:00
|
|
|
{
|
|
|
|
// snap onto y-axis
|
|
|
|
newVec.x = 0;
|
|
|
|
}
|
2022-12-08 00:58:36 +00:00
|
|
|
else if( absVec.x > absVec.y )
|
2018-07-12 12:05:18 +00:00
|
|
|
{
|
|
|
|
// snap away from x-axis towards 45
|
|
|
|
newVec.y = std::copysign( aVec.x, aVec.y );
|
2022-12-08 00:58:36 +00:00
|
|
|
}
|
|
|
|
else
|
2018-07-12 12:05:18 +00:00
|
|
|
{
|
|
|
|
// snap away from y-axis towards 45
|
|
|
|
newVec.x = std::copysign( aVec.y, aVec.x );
|
|
|
|
}
|
|
|
|
|
|
|
|
return newVec;
|
|
|
|
}
|
2018-03-08 12:48:29 +00:00
|
|
|
|
2018-09-07 21:33:01 +00:00
|
|
|
|
2022-02-11 16:42:52 +00:00
|
|
|
/**
|
|
|
|
* Clamps a vector to values that can be negated, respecting numeric limits
|
|
|
|
* of coordinates data type with specified padding.
|
2022-12-08 00:57:11 +00:00
|
|
|
*
|
2022-02-11 16:42:52 +00:00
|
|
|
* Numeric limits are (-2^31 + 1) to (2^31 - 1).
|
2022-12-08 00:57:11 +00:00
|
|
|
*
|
2022-02-11 16:42:52 +00:00
|
|
|
* Takes care of rounding in case of floating point to integer conversion.
|
|
|
|
*
|
|
|
|
* @param aCoord - vector to clamp.
|
|
|
|
* @param aPadding - padding from the limits. Must not be negative.
|
|
|
|
* @return clamped vector.
|
|
|
|
*/
|
|
|
|
template <typename in_type, typename ret_type = in_type, typename pad_type = unsigned int,
|
|
|
|
typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
|
2022-12-08 00:57:11 +00:00
|
|
|
VECTOR2<ret_type> GetClampedCoords( const VECTOR2<in_type>& aCoords, pad_type aPadding = 1u )
|
2022-02-11 16:42:52 +00:00
|
|
|
{
|
2023-01-16 14:41:58 +00:00
|
|
|
typedef std::numeric_limits<int32_t> coord_limits;
|
2022-02-11 16:42:52 +00:00
|
|
|
|
2023-01-15 10:10:30 +00:00
|
|
|
long long max = static_cast<long long>( coord_limits::max() ) - aPadding;
|
2023-01-15 06:54:02 +00:00
|
|
|
long long min = -max;
|
2022-02-11 16:42:52 +00:00
|
|
|
|
|
|
|
in_type x = aCoords.x;
|
|
|
|
in_type y = aCoords.y;
|
|
|
|
|
|
|
|
if( x < min )
|
2023-01-15 06:54:02 +00:00
|
|
|
x = in_type( min );
|
2022-02-11 16:42:52 +00:00
|
|
|
else if( x > max )
|
2023-01-15 06:54:02 +00:00
|
|
|
x = in_type( max );
|
2022-02-11 16:42:52 +00:00
|
|
|
|
|
|
|
if( y < min )
|
2023-01-15 06:54:02 +00:00
|
|
|
y = in_type( min );
|
2022-02-11 16:42:52 +00:00
|
|
|
else if( y > max )
|
2023-01-15 06:54:02 +00:00
|
|
|
y = in_type( max );
|
2022-02-11 16:42:52 +00:00
|
|
|
|
|
|
|
if( !std::is_integral<in_type>() && std::is_integral<ret_type>() )
|
|
|
|
return VECTOR2<ret_type>( KiROUND( x ), KiROUND( y ) );
|
|
|
|
|
|
|
|
return VECTOR2<ret_type>( x, y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-07 21:33:01 +00:00
|
|
|
/**
|
|
|
|
* Test if any part of a line falls within the bounds of a rectangle.
|
|
|
|
*
|
|
|
|
* Please note that this is only accurate for lines that are one pixel wide.
|
|
|
|
*
|
|
|
|
* @param aClipBox - The rectangle to test.
|
|
|
|
* @param x1 - X coordinate of one end of a line.
|
|
|
|
* @param y1 - Y coordinate of one end of a line.
|
|
|
|
* @param x2 - X coordinate of the other end of a line.
|
|
|
|
* @param y2 - Y coordinate of the other end of a line.
|
|
|
|
*
|
|
|
|
* @return - False if any part of the line lies within the rectangle.
|
|
|
|
*/
|
2022-08-31 16:17:14 +00:00
|
|
|
bool ClipLine( const BOX2I *aClipBox, int &x1, int &y1, int &x2, int &y2 );
|
2018-09-07 21:33:01 +00:00
|
|
|
|
|
|
|
|
2018-03-08 12:48:29 +00:00
|
|
|
#endif // #ifndef GEOMETRY_UTILS_H
|
|
|
|
|