Debian Codegraph @

3rd_party/router/kimath/include/geometry/shape_segment.h

raw
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 <[email protected]>
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:
38 SHAPE_SEGMENT() :
39 SHAPE( SH_SEGMENT ),
40 m_width( 0 )
41 {};
42
43 SHAPE_SEGMENT( const VECTOR2I& aA, const VECTOR2I& aB, int aWidth = 0 ) :
44 SHAPE( SH_SEGMENT ),
45 m_seg( aA, aB ),
46 m_width( aWidth )
47 {};
48
49 SHAPE_SEGMENT( const SEG& aSeg, int aWidth = 0 ) :
50 SHAPE( SH_SEGMENT ),
51 m_seg( aSeg ),
52 m_width( aWidth )
53 {};
54
55 ~SHAPE_SEGMENT() {};
56
57 SHAPE* Clone() const override
58 {
59 return new SHAPE_SEGMENT( m_seg, m_width );
60 }
61
62 const BOX2I BBox( int aClearance = 0 ) const override
63 {
64 return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
65 }
66
67 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
68 {
69 return SHAPE::Collide( aShape, aClearance, aMTV );
70 }
71
72 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
73 VECTOR2I* aLocation = nullptr ) const override
74 {
75 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
76 }
77
78 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
79 VECTOR2I* aLocation = nullptr ) const override
80 {
81 if( aSeg.A == aSeg.B )
82 return Collide( aSeg.A, aClearance, aActual, aLocation );
83
84 int min_dist = ( m_width + 1 ) / 2 + aClearance;
85 ecoord dist_sq = m_seg.SquaredDistance( aSeg );
86
87 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
88 {
89 if( aLocation )
90 *aLocation = m_seg.NearestPoint( aSeg );
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 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
102 VECTOR2I* aLocation = nullptr ) const override
103 {
104 int min_dist = ( m_width + 1 ) / 2 + aClearance;
105 ecoord dist_sq = m_seg.SquaredDistance( aP );
106
107 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
108 {
109 if( aLocation )
110 *aLocation = m_seg.NearestPoint( aP );
111
112 if( aActual )
113 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
114
115 return true;
116 }
117
118 return false;
119 }
120
121 void SetSeg( const SEG& aSeg )
122 {
123 m_seg = aSeg;
124 }
125
126 const SEG& GetSeg() const
127 {
128 return m_seg;
129 }
130
131 void SetWidth( int aWidth )
132 {
133 m_width = aWidth;
134 }
135
136 int GetWidth() const
137 {
138 return m_width;
139 }
140
141 bool IsSolid() const override
142 {
143 return true;
144 }
145
146 void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
147 {
148 m_seg.A -= aCenter;
149 m_seg.B -= aCenter;
150
151 m_seg.A = m_seg.A.Rotate( aAngle );
152 m_seg.B = m_seg.B.Rotate( aAngle );
153
154 m_seg.A += aCenter;
155 m_seg.B += aCenter;
156 }
157
158 void Move( const VECTOR2I& aVector ) override
159 {
160 m_seg.A += aVector;
161 m_seg.B += aVector;
162 }
163
164 virtual const std::string Format( ) const override;
165
166 private:
167 SEG m_seg;
168 int m_width;
169 };
170
171 #endif