KiCad PCB EDA Suite
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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 __SHAPE_SEGMENT_H
26 #define __SHAPE_SEGMENT_H
27 
28 #include <geometry/seg.h>
29 #include <geometry/shape.h>
30 #include <math/box2.h>
31 #include <math/vector2d.h>
32 
33 #include <algorithm>
34 
35 class SHAPE_SEGMENT : public SHAPE {
36 
37 public:
39  SHAPE( SH_SEGMENT ), m_width( 0 ) {};
40 
41  SHAPE_SEGMENT( const VECTOR2I& aA, const VECTOR2I& aB, int aWidth = 0 ):
42  SHAPE( SH_SEGMENT ), m_seg( aA, aB ), m_width( aWidth ) {};
43 
44  SHAPE_SEGMENT( const SEG& aSeg, int aWidth = 0 ):
45  SHAPE( SH_SEGMENT ), m_seg( aSeg ), m_width( aWidth ) {};
46 
48 
49  SHAPE* Clone() const override
50  {
51  return new SHAPE_SEGMENT( m_seg, m_width );
52  }
53 
54  const BOX2I BBox( int aClearance = 0 ) const override
55  {
56  return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
57  }
58 
59  bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
60  {
61  return SHAPE::Collide( aShape, aClearance, aMTV );
62  }
63 
64  bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr ) const override
65  {
66  return SHAPE::Collide( aShape, aClearance, aActual );
67  }
68 
69  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr ) const override
70  {
71  int min_dist = ( m_width + 1 ) / 2 + aClearance;
72  ecoord dist_sq = m_seg.SquaredDistance( aSeg );
73 
74  if( dist_sq == 0 || dist_sq < (ecoord) min_dist * min_dist )
75  {
76  if( aActual )
77  *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
78 
79  return true;
80  }
81 
82  return false;
83  }
84 
85  bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr ) const override
86  {
87  int min_dist = ( m_width + 1 ) / 2 + aClearance;
88  ecoord dist_sq = m_seg.SquaredDistance( aP );
89 
90  if( dist_sq == 0 || dist_sq < (ecoord) min_dist * min_dist )
91  {
92  if( aActual )
93  *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
94 
95  return true;
96  }
97 
98  return false;
99  }
100 
101  void SetSeg( const SEG& aSeg )
102  {
103  m_seg = aSeg;
104  }
105 
106  const SEG& GetSeg() const
107  {
108  return m_seg;
109  }
110 
111  void SetWidth( int aWidth )
112  {
113  m_width = aWidth;
114  }
115 
116  int GetWidth() const
117  {
118  return m_width;
119  }
120 
121  bool IsSolid() const override
122  {
123  return true;
124  }
125 
126  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
127  {
128  m_seg.A -= aCenter;
129  m_seg.B -= aCenter;
130 
131  m_seg.A = m_seg.A.Rotate( aAngle );
132  m_seg.B = m_seg.B.Rotate( aAngle );
133 
134  m_seg.A += aCenter;
135  m_seg.B += aCenter;
136  }
137 
138  void Move( const VECTOR2I& aVector ) override
139  {
140  m_seg.A += aVector;
141  m_seg.B += aVector;
142  }
143 
144 private:
146  int m_width;
147 };
148 
149 #endif
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
void SetSeg(const SEG &aSeg)
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Function Rotate.
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_segment.h:54
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:37
const SEG & GetSeg() const
bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr) const override
Function Collide()
Definition: shape_segment.h:85
void Move(const VECTOR2I &aVector) override
SHAPE.
Definition: shape.h:74
SHAPE_SEGMENT(const SEG &aSeg, int aWidth=0)
Definition: shape_segment.h:44
Definition: seg.h:39
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:302
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr) const
Function Collide()
Definition: shape.h:125
SHAPE * Clone() const override
Function Clone()
Definition: shape_segment.h:49
VECTOR2I::extended_type ecoord
Definition: shape.h:77
VECTOR2I A
Definition: seg.h:47
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr) const override
Function Collide()
Definition: shape_segment.h:69
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr) const override
Definition: shape_segment.h:64
int GetWidth() const
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Function Collide()
Definition: shape_segment.h:59
SHAPE_SEGMENT(const VECTOR2I &aA, const VECTOR2I &aB, int aWidth=0)
Definition: shape_segment.h:41
bool IsSolid() const override
void SetWidth(int aWidth)
axis-aligned rectangle
Definition: shape.h:42
VECTOR2I B
Definition: seg.h:48