KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
31
32#include <array>
33#include <optional>
34
35#include <math/vector2d.h>
36#include <math/box2.h>
37
39
40class CIRCLE;
41class HALF_LINE;
42class LINE;
43class SEG;
44class SHAPE_RECT;
45class SHAPE_POLY_SET;
46struct TYPED_POINT2I;
47
48namespace KIGEOM
49{
50
55SEG NormalisedSeg( const SEG& aSeg );
56
60const VECTOR2I& GetOtherEnd( const SEG& aSeg, const VECTOR2I& aPoint );
61
66OPT_VECTOR2I GetSharedEndpoint( const SEG& aSegA, const SEG& aSegB );
67
73std::array<SEG, 4> BoxToSegs( const BOX2I& aBox );
74
78void CollectBoxCorners( const BOX2I& aBox, std::vector<VECTOR2I>& aCorners );
79
80/*
81 * Get a SHAPE_LINE_CHAIN representing the outline of a box.
82 *
83 * The first point and winding direction are not specified.
84 */
85SHAPE_LINE_CHAIN BoxToLineChain( const BOX2I& aBox );
86
94std::vector<SEG> GetSegsInDirection( const BOX2I& aBox, DIRECTION_45::Directions aDir );
95
99std::optional<SEG> ClipHalfLineToBox( const HALF_LINE& aRay, const BOX2I& aBox );
100
104std::optional<SEG> ClipLineToBox( const LINE& aLine, const BOX2I& aBox );
105
106
123SHAPE_ARC MakeArcCw90( const VECTOR2I& aCenter, int aRadius, DIRECTION_45::Directions aDir );
124
131SHAPE_ARC MakeArcCw180( const VECTOR2I& aCenter, int aRadius, DIRECTION_45::Directions aDir );
132
143VECTOR2I GetPoint( const SHAPE_RECT& aRect, DIRECTION_45::Directions aDir, int aOutset = 0 );
144
145
152std::vector<TYPED_POINT2I> GetCircleKeyPoints( const CIRCLE& aCircle, bool aIncludeCenter );
153
154
155/*
156 * Take a polygon and 'rectify' it, so that all sides are H/V.
157 *
158 * The entire original polygon is contained within the new one.
159 * The new polygon will pass though each original corner,
160 * but it can have additional corners, or corners can be simplified away.
161 *
162 * E.g.:
163 * ____ _______
164 * / \___ -> | |___
165 * |________\ |_________|
166 */
167SHAPE_LINE_CHAIN RectifyPolygon( const SHAPE_LINE_CHAIN& aPoly );
168
169
182bool AddHoleIfValid( SHAPE_POLY_SET& aOutline, SHAPE_LINE_CHAIN&& aHole );
183
184
189std::vector<VECTOR2I> MakeRegularPolygonPoints( const VECTOR2I& aCenter, size_t aN,
190 const VECTOR2I& aPt0 );
191
202std::vector<VECTOR2I> MakeRegularPolygonPoints( const VECTOR2I& aCenter, size_t aN, int aRadius,
203 bool aAcrossCorners, EDA_ANGLE aAngle );
204
212std::vector<SEG> MakeCrossSegments( const VECTOR2I& aCenter, const VECTOR2I& aSize,
213 EDA_ANGLE aAngle );
214
215} // namespace KIGEOM
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
Represent basic circle geometry with utility geometry functions.
Definition circle.h:33
Directions
Available directions, there are 8 of them, as on a rectilinear map (north = up) + an extra undefined ...
Definition direction45.h:49
Definition line.h:32
Definition seg.h:38
Represent a set of closed polygons.
bool AddHoleIfValid(SHAPE_POLY_SET &aOutline, SHAPE_LINE_CHAIN &&aHole)
Adds a hole to a polygon if it is valid (i.e.
VECTOR2I GetPoint(const SHAPE_RECT &aRect, DIRECTION_45::Directions aDir, int aOutset=0)
Get the point on a rectangle that corresponds to a given direction.
std::vector< TYPED_POINT2I > GetCircleKeyPoints(const CIRCLE &aCircle, bool aIncludeCenter)
Get key points of an CIRCLE.
std::vector< VECTOR2I > MakeRegularPolygonPoints(const VECTOR2I &aCenter, size_t aN, const VECTOR2I &aPt0)
Get the corners of a regular polygon from the centre, one point and the number of sides.
OPT_VECTOR2I GetSharedEndpoint(const SEG &aSegA, const SEG &aSegB)
Get the shared endpoint of two segments, if it exists, or std::nullopt if the segments are not connec...
std::vector< SEG > GetSegsInDirection(const BOX2I &aBox, DIRECTION_45::Directions aDir)
Get the segments of a box that are in the given direction.
SEG NormalisedSeg(const SEG &aSeg)
Returns a SEG such that the start point is smaller or equal in x and y compared to the end point.
std::vector< SEG > MakeCrossSegments(const VECTOR2I &aCenter, const VECTOR2I &aSize, EDA_ANGLE aAngle)
Create the two segments for a cross.
std::array< SEG, 4 > BoxToSegs(const BOX2I &aBox)
Decompose a BOX2 into four segments.
std::optional< SEG > ClipHalfLineToBox(const HALF_LINE &aRay, const BOX2I &aBox)
Get the segment of a half-line that is inside a box, if any.
std::optional< SEG > ClipLineToBox(const LINE &aLine, const BOX2I &aBox)
Get the segment of a line that is inside a box, if any.
SHAPE_ARC MakeArcCw180(const VECTOR2I &aCenter, int aRadius, DIRECTION_45::Directions aDir)
Get a SHAPE_ARC representing a 180-degree arc in the clockwise direction with the midpoint in the giv...
SHAPE_LINE_CHAIN RectifyPolygon(const SHAPE_LINE_CHAIN &aPoly)
SHAPE_ARC MakeArcCw90(const VECTOR2I &aCenter, int aRadius, DIRECTION_45::Directions aDir)
Get a SHAPE_ARC representing a 90-degree arc in the clockwise direction with the midpoint in the give...
SHAPE_LINE_CHAIN BoxToLineChain(const BOX2I &aBox)
void CollectBoxCorners(const BOX2I &aBox, std::vector< VECTOR2I > &aCorners)
Add the 4 corners of a BOX2I to a vector.
const VECTOR2I & GetOtherEnd(const SEG &aSeg, const VECTOR2I &aPoint)
Get the end point of the segment that is not the given point.
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:35
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683