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 The 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
57 static SHAPE_SEGMENT BySizeAndCenter( const VECTOR2I& aSize, const VECTOR2I& aCenter, const EDA_ANGLE& aRotation );
58
60
61 SHAPE* Clone() const override
62 {
63 return new SHAPE_SEGMENT( m_seg, m_width );
64 }
65
66 const BOX2I BBox( int aClearance = 0 ) const override
67 {
68 return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
69 }
70
71 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
72 {
73 return SHAPE::Collide( aShape, aClearance, aMTV );
74 }
75
76 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
77 VECTOR2I* aLocation = nullptr ) const override
78 {
79 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
80 }
81
82 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
83 VECTOR2I* aLocation = nullptr ) const override
84 {
85 if( aSeg.A == aSeg.B )
86 return Collide( aSeg.A, aClearance, aActual, aLocation );
87
88 int min_dist = ( m_width + 1 ) / 2 + aClearance;
89 ecoord dist_sq = m_seg.SquaredDistance( aSeg );
90
91 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
92 {
93 if( aLocation )
94 *aLocation = m_seg.NearestPoint( aSeg );
95
96 if( aActual )
97 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
98
99 return true;
100 }
101
102 return false;
103 }
104
105 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
106 VECTOR2I* aLocation = nullptr ) const override
107 {
108 int min_dist = ( m_width + 1 ) / 2 + aClearance;
109 ecoord dist_sq = m_seg.SquaredDistance( aP );
110
111 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
112 {
113 if( aLocation )
114 *aLocation = m_seg.NearestPoint( aP );
115
116 if( aActual )
117 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
118
119 return true;
120 }
121
122 return false;
123 }
124
125 void SetSeg( const SEG& aSeg )
126 {
127 m_seg = aSeg;
128 }
129
130 const SEG& GetSeg() const
131 {
132 return m_seg;
133 }
134
135 VECTOR2I GetStart() const override { return m_seg.A; }
136 VECTOR2I GetEnd() const override { return m_seg.B; }
137
138 void SetWidth( int aWidth ) override
139 {
140 m_width = aWidth;
141 }
142
143 int GetWidth() const override
144 {
145 return m_width;
146 }
147
151 int GetTotalLength() const
152 {
153 return m_seg.Length() + m_width;
154 }
155
157 {
158 return m_seg.Center();
159 }
160
162 {
163 return EDA_ANGLE( m_seg.B - m_seg.A );
164 }
165
166 bool IsSolid() const override
167 {
168 return true;
169 }
170
171 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
172 {
173 RotatePoint( m_seg.A, aCenter, aAngle );
174 RotatePoint( m_seg.B, aCenter, aAngle );
175 }
176
177 void Move( const VECTOR2I& aVector ) override
178 {
179 m_seg.A += aVector;
180 m_seg.B += aVector;
181 }
182
183 bool Is45Degree( EDA_ANGLE aTollerance = EDA_ANGLE( 1.0, DEGREES_T ) ) const;
184
185 virtual const std::string Format( bool aCplusPlus = true ) const override;
186
187 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
188 ERROR_LOC aErrorLoc ) const override;
189
190private:
193};
194
195#endif
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
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
VECTOR2I B
Definition seg.h:50
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
const SEG & GetSeg() const
bool IsSolid() const override
VECTOR2I GetEnd() const override
EDA_ANGLE GetAngle() const
bool Is45Degree(EDA_ANGLE aTollerance=EDA_ANGLE(1.0, DEGREES_T)) const
SHAPE_SEGMENT(const VECTOR2I &aA, const VECTOR2I &aB, int aWidth=0)
VECTOR2I GetStart() const override
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
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,...
SHAPE_SEGMENT(const SEG &aSeg, int aWidth=0)
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.
int GetWidth() const override
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
int GetTotalLength() const
Get the total length of the segment, from tip to tip.
void SetWidth(int aWidth) override
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,...
static SHAPE_SEGMENT BySizeAndCenter(const VECTOR2I &aSize, const VECTOR2I &aCenter, const EDA_ANGLE &aRotation)
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,...
VECTOR2I GetCenter() const
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:301
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:136
@ 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
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695