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,
65  VECTOR2I* aLocation = nullptr ) const override
66  {
67  return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
68  }
69 
70  bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
71  VECTOR2I* aLocation = nullptr ) const override
72  {
73  int min_dist = ( m_width + 1 ) / 2 + aClearance;
74  ecoord dist_sq = m_seg.SquaredDistance( aSeg );
75 
76  if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
77  {
78  if( aLocation )
79  *aLocation = m_seg.NearestPoint( aSeg );
80 
81  if( aActual )
82  *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
83 
84  return true;
85  }
86 
87  return false;
88  }
89 
90  bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
91  VECTOR2I* aLocation = nullptr ) const override
92  {
93  int min_dist = ( m_width + 1 ) / 2 + aClearance;
94  ecoord dist_sq = m_seg.SquaredDistance( aP );
95 
96  if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
97  {
98  if( aLocation )
99  *aLocation = m_seg.NearestPoint( aP );
100 
101  if( aActual )
102  *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
103 
104  return true;
105  }
106 
107  return false;
108  }
109 
110  void SetSeg( const SEG& aSeg )
111  {
112  m_seg = aSeg;
113  }
114 
115  const SEG& GetSeg() const
116  {
117  return m_seg;
118  }
119 
120  void SetWidth( int aWidth )
121  {
122  m_width = aWidth;
123  }
124 
125  int GetWidth() const
126  {
127  return m_width;
128  }
129 
130  bool IsSolid() const override
131  {
132  return true;
133  }
134 
135  void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
136  {
137  m_seg.A -= aCenter;
138  m_seg.B -= aCenter;
139 
140  m_seg.A = m_seg.A.Rotate( aAngle );
141  m_seg.B = m_seg.B.Rotate( aAngle );
142 
143  m_seg.A += aCenter;
144  m_seg.B += aCenter;
145  }
146 
147  void Move( const VECTOR2I& aVector ) override
148  {
149  m_seg.A += aVector;
150  m_seg.B += aVector;
151  }
152 
153  virtual const std::string Format( ) const override;
154 
155 private:
157  int m_width;
158 };
159 
160 #endif
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
void SetSeg(const SEG &aSeg)
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Definition: shape_segment.h:64
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
static SEG::ecoord Square(int a)
Definition: seg.h:116
virtual const std::string Format() const override
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Function Collide()
Definition: shape.h:176
const SEG & GetSeg() const
void Move(const VECTOR2I &aVector) override
bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Function Collide()
Definition: shape_segment.h:90
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
SHAPE.
Definition: shape.h:122
SHAPE_SEGMENT(const SEG &aSeg, int aWidth=0)
Definition: shape_segment.h:44
Definition: seg.h:39
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Function Collide()
Definition: shape_segment.h:70
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
SHAPE * Clone() const override
Function Clone()
Definition: shape_segment.h:49
VECTOR2I::extended_type ecoord
Definition: shape.h:125
VECTOR2I A
Definition: seg.h:47
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:44
VECTOR2I B
Definition: seg.h:48