| 1 |
/* |
| 2 |
* This program source code file is part of KiCad, a free EDA CAD application. |
| 3 |
* |
| 4 |
* Copyright (C) 2018 CERN |
| 5 |
* Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors. |
| 6 |
* @author Tomasz Wlostowski <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2 |
| 11 |
* of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, you may find one here: |
| 20 |
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 21 |
* or you may search the http://www.gnu.org website for the version 2 license, |
| 22 |
* or you may write to the Free Software Foundation, Inc., |
| 23 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef __SHAPE_ARC_H |
| 27 |
#define __SHAPE_ARC_H |
| 28 |
|
| 29 |
#include <geometry/shape.h> |
| 30 |
#include <convert_to_biu.h> |
| 31 |
#include <math/vector2d.h> // for VECTOR2I |
| 32 |
|
| 33 |
class SHAPE_LINE_CHAIN; |
| 34 |
|
| 35 |
class SHAPE_ARC : public SHAPE |
| 36 |
{ |
| 37 |
public: |
| 38 |
|
| 39 |
SHAPE_ARC() : |
| 40 |
SHAPE( SH_ARC ), |
| 41 |
m_width( 0 ) |
| 42 |
{}; |
| 43 |
|
| 44 |
/** |
| 45 |
* Construct and arc using center, start, angle. |
| 46 |
* |
| 47 |
* Center and angle are used to calculate the mid and end points of the arc, and are not |
| 48 |
* stored. |
| 49 |
* |
| 50 |
* @param aArcCenter is the arc center. |
| 51 |
* @param aArcStartPoint is the arc start point. |
| 52 |
* @param aCenterAngle is the arc angle in degrees. |
| 53 |
* @param aWidth is the arc line thickness. |
| 54 |
*/ |
| 55 |
SHAPE_ARC( const VECTOR2I& aArcCenter, const VECTOR2I& aArcStartPoint, double aCenterAngle, |
| 56 |
int aWidth = 0 ); |
| 57 |
|
| 58 |
/** |
| 59 |
* @param aArcStart is the arc start point. |
| 60 |
* @param aArcEnd is the arc end point. |
| 61 |
* @param aArcMid is the arc mid point. |
| 62 |
* @param aWidth is the arc line thickness. |
| 63 |
*/ |
| 64 |
SHAPE_ARC( const VECTOR2I& aArcStart, const VECTOR2I& aArcMid, const VECTOR2I& aArcEnd, |
| 65 |
int aWidth ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Build a SHAPE_ARC which is tangent to two segments and a given radius. |
| 69 |
* |
| 70 |
* @param aSegmentA is the first segment |
| 71 |
* @param aSegmentB is the second segment |
| 72 |
* @param aRadius is the arc radius |
| 73 |
* @param aWidth is the arc line thickness |
| 74 |
*/ |
| 75 |
SHAPE_ARC( const SEG& aSegmentA, const SEG& aSegmentB, int aRadius, int aWidth = 0 ); |
| 76 |
|
| 77 |
SHAPE_ARC( const SHAPE_ARC& aOther ); |
| 78 |
|
| 79 |
virtual ~SHAPE_ARC() {} |
| 80 |
|
| 81 |
SHAPE* Clone() const override |
| 82 |
{ |
| 83 |
return new SHAPE_ARC( *this ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Construct this arc from the given start, end and angle. |
| 88 |
* |
| 89 |
* @param aStart is the arc starting point |
| 90 |
* @param aEnd is the arc endpoint |
| 91 |
* @param aAngle is the arc included angle |
| 92 |
* @param aWidth is the arc line thickness |
| 93 |
* @return this arc. |
| 94 |
*/ |
| 95 |
SHAPE_ARC& ConstructFromStartEndAngle( const VECTOR2I& aStart, const VECTOR2I& aEnd, |
| 96 |
double aAngle, double aWidth = 0 ); |
| 97 |
|
| 98 |
/** |
| 99 |
* Constructs this arc from the given start, end and center. |
| 100 |
* @param aStart is the arc starting point |
| 101 |
* @param aEnd is the arc endpoint |
| 102 |
* @param aCenter is the arc center |
| 103 |
* @param aClockwise determines which of the two solutions to construct |
| 104 |
* @param aWidth is the arc line thickness |
| 105 |
* @return *this |
| 106 |
*/ |
| 107 |
SHAPE_ARC& ConstructFromStartEndCenter( const VECTOR2I& aStart, const VECTOR2I& aEnd, |
| 108 |
const VECTOR2I& aCenter, bool aClockwise = false, |
| 109 |
double aWidth = 0 ); |
| 110 |
|
| 111 |
const VECTOR2I& GetP0() const { return m_start; } |
| 112 |
const VECTOR2I& GetP1() const { return m_end; } |
| 113 |
const VECTOR2I& GetArcMid() const { return m_mid; } |
| 114 |
VECTOR2I GetCenter() const; |
| 115 |
|
| 116 |
const BOX2I BBox( int aClearance = 0 ) const override; |
| 117 |
|
| 118 |
bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr, |
| 119 |
VECTOR2I* aLocation = nullptr ) const override; |
| 120 |
bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr, |
| 121 |
VECTOR2I* aLocation = nullptr ) const override; |
| 122 |
|
| 123 |
|
| 124 |
bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr, |
| 125 |
VECTOR2I* aLocation = nullptr ) const override |
| 126 |
{ |
| 127 |
return SHAPE::Collide( aShape, aClearance, aActual, aLocation ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Find intersection points between this arc and aSeg, treating aSeg as an infinite line. |
| 132 |
* Ignores arc width. |
| 133 |
* |
| 134 |
* @param aSeg Line to intersect against (treated as an infinite line) |
| 135 |
* @param aIpsBuffer Buffer to store the resulting intersection points (if any) |
| 136 |
* @return Number of intersection points found |
| 137 |
*/ |
| 138 |
int IntersectLine( const SEG& aSeg, std::vector<VECTOR2I>* aIpsBuffer ) const; |
| 139 |
|
| 140 |
/** |
| 141 |
* Find intersection points between this arc and aArc. Ignores arc width. |
| 142 |
* |
| 143 |
* @param aSeg |
| 144 |
* @param aIpsBuffer Buffer to store the resulting intersection points (if any) |
| 145 |
* @return Number of intersection points found |
| 146 |
*/ |
| 147 |
int Intersect( const SHAPE_ARC& aArc, std::vector<VECTOR2I>* aIpsBuffer ) const; |
| 148 |
|
| 149 |
bool IsClockwise() const; |
| 150 |
|
| 151 |
void SetWidth( int aWidth ) |
| 152 |
{ |
| 153 |
m_width = aWidth; |
| 154 |
} |
| 155 |
|
| 156 |
int GetWidth() const |
| 157 |
{ |
| 158 |
return m_width; |
| 159 |
} |
| 160 |
|
| 161 |
bool IsSolid() const override |
| 162 |
{ |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
void Move( const VECTOR2I& aVector ) override; |
| 167 |
|
| 168 |
/** |
| 169 |
* Rotate the arc by a given angle about a point. |
| 170 |
* |
| 171 |
* @param aCenter is the rotation center. |
| 172 |
* @param aAngle rotation angle in radians. |
| 173 |
*/ |
| 174 |
void Rotate( double aAngle, const VECTOR2I& aCenter ) override; |
| 175 |
|
| 176 |
void Mirror( bool aX = true, bool aY = false, const VECTOR2I& aVector = { 0, 0 } ); |
| 177 |
|
| 178 |
void Mirror( const SEG& axis ); |
| 179 |
|
| 180 |
void Reverse(); |
| 181 |
|
| 182 |
SHAPE_ARC Reversed() const; |
| 183 |
|
| 184 |
double GetRadius() const; |
| 185 |
|
| 186 |
SEG GetChord() const |
| 187 |
{ |
| 188 |
return SEG( m_start, m_end ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @return the central angle of the arc shape in degrees, normalized between 0.0, 360.0 deg. |
| 193 |
*/ |
| 194 |
double GetCentralAngle() const; |
| 195 |
|
| 196 |
/** |
| 197 |
* @return the start angle of the arc shape in degrees, normalized between 0.0, 360.0 deg. |
| 198 |
*/ |
| 199 |
double GetStartAngle() const; |
| 200 |
|
| 201 |
/** |
| 202 |
* @return the end angle of the arc shape in degrees, normalized between 0.0, 360.0 deg. |
| 203 |
*/ |
| 204 |
double GetEndAngle() const; |
| 205 |
|
| 206 |
/** |
| 207 |
* @return the length of the arc shape. |
| 208 |
*/ |
| 209 |
double GetLength() const; |
| 210 |
|
| 211 |
/** |
| 212 |
* @note The default is #ARC_HIGH_DEF in Pcbnew units. This is to allow common geometry |
| 213 |
* collision functions. Other programs should call this using explicit accuracy |
| 214 |
* values. |
| 215 |
* |
| 216 |
* @todo Unify KiCad internal units. |
| 217 |
* |
| 218 |
* @return a default accuracy value for ConvertToPolyline() to build the polyline. |
| 219 |
*/ |
| 220 |
static double DefaultAccuracyForPCB(){ return 0.005 * PCB_IU_PER_MM; } |
| 221 |
|
| 222 |
/** |
| 223 |
* Construct a SHAPE_LINE_CHAIN of segments from a given arc. |
| 224 |
* |
| 225 |
* @note The default is #ARC_HIGH_DEF in Pcbnew units. This is to allow common geometry |
| 226 |
* collision functions. Other programs should call this using explicit accuracy |
| 227 |
* values. |
| 228 |
* |
| 229 |
* @todo Unify KiCad internal units. |
| 230 |
* |
| 231 |
* @param aAccuracy maximum divergence from true arc given in internal units. |
| 232 |
* @param aEffectiveAccuracy is the actual divergence from true arc given. |
| 233 |
* the approximation error is between -aEffectiveAccuracy/2 and +aEffectiveAccuracy/2 |
| 234 |
* in internal units |
| 235 |
* @return a #SHAPE_LINE_CHAIN. |
| 236 |
*/ |
| 237 |
const SHAPE_LINE_CHAIN ConvertToPolyline( double aAccuracy = DefaultAccuracyForPCB(), |
| 238 |
double* aEffectiveAccuracy = nullptr ) const; |
| 239 |
|
| 240 |
bool operator==( SHAPE_ARC const& aArc ) const |
| 241 |
{ |
| 242 |
return ( aArc.m_start == m_start ) && ( aArc.m_end == m_end ) && ( aArc.m_mid == m_mid ) |
| 243 |
&& ( aArc.m_width == m_width ); |
| 244 |
} |
| 245 |
|
| 246 |
private: |
| 247 |
bool ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const |
| 248 |
{ |
| 249 |
return ( ecoord{ aC.y } - aA.y ) * ( ecoord{ aB.x } - aA.x ) > |
| 250 |
( ecoord{ aB.y } - aA.y ) * ( ecoord{ aC.x } - aA.x ); |
| 251 |
} |
| 252 |
|
| 253 |
void update_bbox(); |
| 254 |
|
| 255 |
bool sliceContainsPoint( const VECTOR2I& p ) const; |
| 256 |
|
| 257 |
private: |
| 258 |
VECTOR2I m_start; |
| 259 |
VECTOR2I m_mid; |
| 260 |
VECTOR2I m_end; |
| 261 |
|
| 262 |
int m_width; |
| 263 |
BOX2I m_bbox; |
| 264 |
}; |
| 265 |
|
| 266 |
// Required for Boost Test BOOST_CHECK_EQUAL: |
| 267 |
std::ostream& operator<<( std::ostream& aStream, const SHAPE_ARC& aArc ); |
| 268 |
|
| 269 |
#endif |