KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_segment.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) 2013 CERN
5 * Copyright (C) 2021-2022 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_SEGMENT_H
27#define __SHAPE_SEGMENT_H
28
29#include <geometry/seg.h>
30#include <geometry/shape.h>
31#include <math/box2.h>
32#include <math/vector2d.h>
33#include <trigo.h>
34
35#include <algorithm>
36
37class SHAPE_SEGMENT : public SHAPE
38{
39public:
42 m_width( 0 )
43 {};
44
45 SHAPE_SEGMENT( const VECTOR2I& aA, const VECTOR2I& aB, int aWidth = 0 ) :
47 m_seg( aA, aB ),
48 m_width( aWidth )
49 {};
50
51 SHAPE_SEGMENT( const SEG& aSeg, int aWidth = 0 ) :
53 m_seg( aSeg ),
54 m_width( aWidth )
55 {};
56
58
59 SHAPE* Clone() const override
60 {
61 return new SHAPE_SEGMENT( m_seg, m_width );
62 }
63
64 const BOX2I BBox( int aClearance = 0 ) const override
65 {
66 return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
67 }
68
69 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
70 {
71 return SHAPE::Collide( aShape, aClearance, aMTV );
72 }
73
74 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
75 VECTOR2I* aLocation = nullptr ) const override
76 {
77 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
78 }
79
80 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
81 VECTOR2I* aLocation = nullptr ) const override
82 {
83 if( aSeg.A == aSeg.B )
84 return Collide( aSeg.A, aClearance, aActual, aLocation );
85
86 int min_dist = ( m_width + 1 ) / 2 + aClearance;
87 ecoord dist_sq = m_seg.SquaredDistance( aSeg );
88
89 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
90 {
91 if( aLocation )
92 *aLocation = m_seg.NearestPoint( aSeg );
93
94 if( aActual )
95 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
96
97 return true;
98 }
99
100 return false;
101 }
102
103 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
104 VECTOR2I* aLocation = nullptr ) const override
105 {
106 int min_dist = ( m_width + 1 ) / 2 + aClearance;
107 ecoord dist_sq = m_seg.SquaredDistance( aP );
108
109 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
110 {
111 if( aLocation )
112 *aLocation = m_seg.NearestPoint( aP );
113
114 if( aActual )
115 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
116
117 return true;
118 }
119
120 return false;
121 }
122
123 void SetSeg( const SEG& aSeg )
124 {
125 m_seg = aSeg;
126 }
127
128 const SEG& GetSeg() const
129 {
130 return m_seg;
131 }
132
133 void SetWidth( int aWidth )
134 {
135 m_width = aWidth;
136 }
137
138 int GetWidth() const
139 {
140 return m_width;
141 }
142
143 bool IsSolid() const override
144 {
145 return true;
146 }
147
148 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
149 {
150 RotatePoint( m_seg.A, aCenter, aAngle );
151 RotatePoint( m_seg.B, aCenter, aAngle );
152 }
153
154 void Move( const VECTOR2I& aVector ) override
155 {
156 m_seg.A += aVector;
157 m_seg.B += aVector;
158 }
159
160 bool Is45Degree( EDA_ANGLE aTollerance = EDA_ANGLE( 1.0, DEGREES_T ) ) const;
161
162 virtual const std::string Format( bool aCplusPlus = true ) const override;
163
164 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
165 ERROR_LOC aErrorLoc ) const override;
166
167private:
170};
171
172#endif
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
Definition: approximation.h:32
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:558
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:75
VECTOR2I B
Definition: seg.h:50
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:327
static SEG::ecoord Square(int a)
Definition: seg.h:123
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_segment.h:74
const SEG & GetSeg() const
bool IsSolid() const override
bool Is45Degree(EDA_ANGLE aTollerance=EDA_ANGLE(1.0, DEGREES_T)) const
SHAPE_SEGMENT(const VECTOR2I &aA, const VECTOR2I &aB, int aWidth=0)
Definition: shape_segment.h:45
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition: shape_segment.h:59
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition: shape_segment.h:80
int GetWidth() const
SHAPE_SEGMENT(const SEG &aSeg, int aWidth=0)
Definition: shape_segment.h:51
void Move(const VECTOR2I &aVector) override
virtual const std::string Format(bool aCplusPlus=true) const override
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_segment.h:64
void SetSeg(const SEG &aSeg)
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition: shape_segment.h:69
void SetWidth(int aWidth)
bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
An abstract shape on 2D plane.
Definition: shape.h:126
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape.h:181
VECTOR2I::extended_type ecoord
Definition: shape.h:284
@ DEGREES_T
Definition: eda_angle.h:31
@ SH_SEGMENT
line segment
Definition: shape.h:48
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:229