| 1 |
/* |
| 2 |
* This program source code file is part of KiCad, a free EDA CAD application. |
| 3 |
* |
| 4 |
* Copyright (C) 2012 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 |
|
| 25 |
#ifndef CONVERT_BASIC_SHAPES_TO_POLYGON_H |
| 26 |
#define CONVERT_BASIC_SHAPES_TO_POLYGON_H |
| 27 |
|
| 28 |
#include <geometry/shape_poly_set.h> |
| 29 |
#include <geometry/geometry_utils.h> |
| 30 |
#include <wx/gdicmn.h> // for wxPoint |
| 31 |
|
| 32 |
|
| 33 |
// The chamfer positions of chamfered rect shape. |
| 34 |
// the position is relative to a pad with orientation = 0 |
| 35 |
// we can have 1 to 4 chamfered corners (0 corner = roundrect) |
| 36 |
// The position list is the OR of corner to chamfer |
| 37 |
enum RECT_CHAMFER_POSITIONS : int |
| 38 |
{ |
| 39 |
RECT_NO_CHAMFER = 0, |
| 40 |
RECT_CHAMFER_TOP_LEFT = 1, |
| 41 |
RECT_CHAMFER_TOP_RIGHT = 2, |
| 42 |
RECT_CHAMFER_BOTTOM_LEFT = 4, |
| 43 |
RECT_CHAMFER_BOTTOM_RIGHT = 8, |
| 44 |
RECT_CHAMFER_ALL = RECT_CHAMFER_BOTTOM_RIGHT |
| 45 |
| RECT_CHAMFER_BOTTOM_LEFT |
| 46 |
| RECT_CHAMFER_TOP_RIGHT |
| 47 |
| RECT_CHAMFER_TOP_LEFT |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Generate a polyline to approximate a arc |
| 53 |
* |
| 54 |
* @param aPolyline is a buffer to store the polyline. |
| 55 |
* @param aCenter is the center of the arc. |
| 56 |
* @param aRadius is the radius of the arc. |
| 57 |
* @param aStartAngleDeg is the starting point (in degrees) of the arc. |
| 58 |
* @param aArcAngleDeg is the angle (in degrees) of the arc. |
| 59 |
* @param aError is the internal units allowed for error approximation. |
| 60 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 61 |
*/ |
| 62 |
int ConvertArcToPolyline( SHAPE_LINE_CHAIN& aPolyline, VECTOR2I aCenter, int aRadius, |
| 63 |
double aStartAngleDeg, double aArcAngleDeg, double aAccuracy, |
| 64 |
ERROR_LOC aErrorLoc ); |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Convert a circle to a polygon, using multiple straight lines. |
| 69 |
* |
| 70 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 71 |
* @param aCenter is the center of the circle. |
| 72 |
* @param aRadius is the radius of the circle. |
| 73 |
* @param aError is the internal units allowed for error approximation. |
| 74 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 75 |
* @param aMinSegCount is the min count of segments to approximate. |
| 76 |
* Default = 0 to do not force a min count. |
| 77 |
*/ |
| 78 |
void TransformCircleToPolygon( SHAPE_LINE_CHAIN& aCornerBuffer, const wxPoint& aCenter, int aRadius, |
| 79 |
int aError, ERROR_LOC aErrorLoc, int aMinSegCount = 0 ); |
| 80 |
|
| 81 |
/** |
| 82 |
* Convert a circle to a polygon, using multiple straight lines. |
| 83 |
* |
| 84 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 85 |
* @param aCenter is the center of the circle. |
| 86 |
* @param aRadius is the radius of the circle. |
| 87 |
* @param aError is the internal units allowed for error in approximation. |
| 88 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 89 |
* @param aMinSegCount is the min count of segments to approximate. |
| 90 |
* Default = 0 to do not force a min count. |
| 91 |
*/ |
| 92 |
void TransformCircleToPolygon( SHAPE_POLY_SET& aCornerBuffer, const wxPoint& aCenter, int aRadius, |
| 93 |
int aError, ERROR_LOC aErrorLoc, int aMinSegCount = 0 ); |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Convert a oblong shape to a polygon, using multiple segments. |
| 98 |
* |
| 99 |
* It is similar to TransformRoundedEndsSegmentToPolygon, but the polygon is outside the actual |
| 100 |
* oblong shape (a segment with rounded ends). It is suitable to create oblong clearance areas |
| 101 |
* because multiple segments create a smaller area than the circle. The radius of the circle to |
| 102 |
* approximate must be bigger ( radius*aCorrectionFactor) to create segments outside the circle. |
| 103 |
* |
| 104 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 105 |
* @param aStart is the first point of the segment. |
| 106 |
* @param aEnd is the second point of the segment. |
| 107 |
* @param aWidth is the width of the segment. |
| 108 |
* @param aError is the internal units allowed for error in approximation. |
| 109 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 110 |
* @param aMinSegCount is the min count of segments to approximate. |
| 111 |
* Default = 0 to do not force a min count. |
| 112 |
*/ |
| 113 |
void TransformOvalToPolygon( SHAPE_POLY_SET& aCornerBuffer, const wxPoint& aStart, |
| 114 |
const wxPoint& aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, |
| 115 |
int aMinSegCount = 0 ); |
| 116 |
|
| 117 |
/** |
| 118 |
* Convert a rectangle or trapezoid to a polygon. |
| 119 |
* |
| 120 |
* This will generate at least 16 segments per circle (when using inflate). |
| 121 |
* |
| 122 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 123 |
* @param aPosition is the coordinate of the center of the rectangle. |
| 124 |
* @param aSize is the size of the rectangle. |
| 125 |
* @param aDeltaX is the delta for trapezoids in X direction |
| 126 |
* @param aDeltaY is the delta for trapezoids in Y direction |
| 127 |
* @param aInflate is the (positive) shape inflation or 0 |
| 128 |
* @param aError is the IU allowed for error in approximation. |
| 129 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 130 |
*/ |
| 131 |
void TransformTrapezoidToPolygon( SHAPE_POLY_SET& aCornerBuffer, |
| 132 |
const wxPoint& aPosition, const wxSize& aSize, |
| 133 |
double aRotation, int aDeltaX, int aDeltaY, int aInflate, |
| 134 |
int aError, ERROR_LOC aErrorLoc ); |
| 135 |
|
| 136 |
/** |
| 137 |
* Convert a rectangle with rounded corners and/or chamfered corners to a polygon. |
| 138 |
* |
| 139 |
* Convert rounded corners arcs to multiple straight lines. This will generate at least |
| 140 |
* 16 segments per circle. |
| 141 |
* |
| 142 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 143 |
* @param aPosition is the coordinate of the center of the rectangle. |
| 144 |
* @param aSize is the size of the rectangle. |
| 145 |
* @param aCornerRadius is the radius of rounded corners (can be 0). |
| 146 |
* @param aRotation is the rotation in 0.1 degrees of the rectangle. |
| 147 |
* @param aChamferRatio is the ratio between smaller rect side and chamfer value. |
| 148 |
* @param aChamferCorners is the identifier of the corners to chamfer: |
| 149 |
* - 0 = no chamfer |
| 150 |
* - 1 = TOP_LEFT |
| 151 |
* - 2 = TOP_RIGHT |
| 152 |
* - 4 = BOTTOM_LEFT |
| 153 |
* - 8 = BOTTOM_RIGHT |
| 154 |
* One can have more than one chamfered corner by ORing the corner identifiers. |
| 155 |
* @param aInflate is the (positive) shape inflation or 0 |
| 156 |
* @param aError is the IU allowed for error in approximation. |
| 157 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 158 |
*/ |
| 159 |
void TransformRoundChamferedRectToPolygon( SHAPE_POLY_SET& aCornerBuffer, |
| 160 |
const wxPoint& aPosition, const wxSize& aSize, |
| 161 |
double aRotation, int aCornerRadius, |
| 162 |
double aChamferRatio, int aChamferCorners, int aInflate, |
| 163 |
int aError, ERROR_LOC aErrorLoc ); |
| 164 |
|
| 165 |
/** |
| 166 |
* Convert arc to multiple straight segments. |
| 167 |
* |
| 168 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 169 |
* @param aCentre is the center of the arc or circle. |
| 170 |
* @param aStart is the start point of the arc or a point on the circle. |
| 171 |
* @param aArcAngle is the arc angle in 0.1 degrees. For a circle, aArcAngle = 3600. |
| 172 |
* @param aWidth is the width (thickness) of the line. |
| 173 |
* @param aError is the internal units allowed for error in approximation. |
| 174 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 175 |
*/ |
| 176 |
void TransformArcToPolygon( SHAPE_POLY_SET& aCornerBuffer, const wxPoint& aStart, |
| 177 |
const wxPoint& aMid, const wxPoint& aEnd, int aWidth, int aError, |
| 178 |
ERROR_LOC aErrorLoc ); |
| 179 |
|
| 180 |
/** |
| 181 |
* Convert arcs to multiple straight segments. |
| 182 |
* |
| 183 |
* @param aCornerBuffer is a buffer to store the polygon. |
| 184 |
* @param aCentre is the center of the arc or circle. |
| 185 |
* @param aRadius is the radius of the circle. |
| 186 |
* @param aWidth is the width (thickness) of the ring. |
| 187 |
* @param aError is the internal units allowed for error in approximation. |
| 188 |
* @param aErrorLoc determines if the approximation error be placed outside or inside the polygon. |
| 189 |
*/ |
| 190 |
void TransformRingToPolygon( SHAPE_POLY_SET& aCornerBuffer, const wxPoint& aCentre, int aRadius, |
| 191 |
int aWidth, int aError, ERROR_LOC aErrorLoc ); |
| 192 |
|
| 193 |
#endif // CONVERT_BASIC_SHAPES_TO_POLYGON_H |