KiCad PCB EDA Suite
pns_utils.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2014 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "pns_utils.h"
23 #include "pns_line.h"
24 #include "pns_via.h"
25 #include "pns_router.h"
26 
27 #include <geometry/shape_segment.h>
28 #include <math/box2.h>
29 
30 #include <cmath>
31 
32 namespace PNS {
33 
34 const SHAPE_LINE_CHAIN OctagonalHull( const VECTOR2I& aP0, const VECTOR2I& aSize,
35  int aClearance, int aChamfer )
36 {
38 
39  s.SetClosed( true );
40 
41  s.Append( aP0.x - aClearance, aP0.y - aClearance + aChamfer );
42  s.Append( aP0.x - aClearance + aChamfer, aP0.y - aClearance );
43  s.Append( aP0.x + aSize.x + aClearance - aChamfer, aP0.y - aClearance );
44  s.Append( aP0.x + aSize.x + aClearance, aP0.y - aClearance + aChamfer );
45  s.Append( aP0.x + aSize.x + aClearance, aP0.y + aSize.y + aClearance - aChamfer );
46  s.Append( aP0.x + aSize.x + aClearance - aChamfer, aP0.y + aSize.y + aClearance );
47  s.Append( aP0.x - aClearance + aChamfer, aP0.y + aSize.y + aClearance );
48  s.Append( aP0.x - aClearance, aP0.y + aSize.y + aClearance - aChamfer );
49 
50  return s;
51 }
52 
53 
54 const SHAPE_LINE_CHAIN SegmentHull ( const SHAPE_SEGMENT& aSeg, int aClearance,
55  int aWalkaroundThickness )
56 {
57  int d = aSeg.GetWidth() / 2 + aClearance + aWalkaroundThickness / 2 + HULL_MARGIN;
58  int x = (int)( 2.0 / ( 1.0 + M_SQRT2 ) * d );
59 
60  const VECTOR2I a = aSeg.GetSeg().A;
61  const VECTOR2I b = aSeg.GetSeg().B;
62 
63  VECTOR2I dir = b - a;
64  VECTOR2I p0 = dir.Perpendicular().Resize( d );
65  VECTOR2I ds = dir.Perpendicular().Resize( x / 2 );
66  VECTOR2I pd = dir.Resize( x / 2 );
67  VECTOR2I dp = dir.Resize( d );
68 
70 
71  s.SetClosed( true );
72 
73  s.Append( b + p0 + pd );
74  s.Append( b + dp + ds );
75  s.Append( b + dp - ds );
76  s.Append( b - p0 + pd );
77  s.Append( a - p0 - pd );
78  s.Append( a - dp - ds );
79  s.Append( a - dp + ds );
80  s.Append( a + p0 - pd );
81 
82  // make sure the hull outline is always clockwise
83  if( s.CSegment( 0 ).Side( a ) < 0 )
84  return s.Reverse();
85  else
86  return s;
87 }
88 
89 
90 static void MoveDiagonal( SEG& aDiagonal, const SHAPE_LINE_CHAIN& aVertices, int aClearance )
91 {
92  int dist;
93 
94  aVertices.NearestPoint( aDiagonal, dist );
95  dist -= HULL_MARGIN;
96  VECTOR2I moveBy = ( aDiagonal.A - aDiagonal.B ).Perpendicular().Resize( dist - aClearance );
97  aDiagonal.A += moveBy;
98  aDiagonal.B += moveBy;
99 }
100 
101 
102 const SHAPE_LINE_CHAIN ConvexHull( const SHAPE_SIMPLE& aConvex, int aClearance )
103 {
104  // this defines the horizontal and vertical lines in the hull octagon
105  BOX2I box = aConvex.BBox( aClearance + HULL_MARGIN );
106  box.Normalize();
107 
108  SEG topline = SEG( VECTOR2I( box.GetX(), box.GetY() + box.GetHeight() ),
109  VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() + box.GetHeight() ) );
110  SEG rightline = SEG( VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() + box.GetHeight() ),
111  VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() ) );
112  SEG bottomline = SEG( VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() ),
113  box.GetOrigin() );
114  SEG leftline = SEG( box.GetOrigin(), VECTOR2I( box.GetX(), box.GetY() + box.GetHeight() ) );
115 
116  const SHAPE_LINE_CHAIN& vertices = aConvex.Vertices();
117 
118  // top right diagonal
119  VECTOR2I corner = box.GetOrigin() + box.GetSize();
120  SEG toprightline = SEG( corner,
121  corner + VECTOR2I( box.GetHeight(), -box.GetHeight() ) );
122  MoveDiagonal( toprightline, vertices, aClearance );
123 
124  // bottom right diagonal
125  corner = box.GetOrigin() + VECTOR2I( box.GetWidth(), 0 );
126  SEG bottomrightline = SEG( corner + VECTOR2I( box.GetHeight(), box.GetHeight() ),
127  corner );
128  MoveDiagonal( bottomrightline, vertices, aClearance );
129 
130  // bottom left diagonal
131  corner = box.GetOrigin();
132  SEG bottomleftline = SEG( corner,
133  corner + VECTOR2I( -box.GetHeight(), box.GetHeight() ) );
134  MoveDiagonal( bottomleftline, vertices, aClearance );
135 
136  // top left diagonal
137  corner = box.GetOrigin() + VECTOR2I( 0, box.GetHeight() );
138  SEG topleftline = SEG( corner + VECTOR2I( -box.GetHeight(), -box.GetHeight() ),
139  corner );
140  MoveDiagonal( topleftline, vertices, aClearance );
141 
142  SHAPE_LINE_CHAIN octagon;
143  octagon.SetClosed( true );
144 
145  octagon.Append( *leftline.IntersectLines( bottomleftline ) );
146  octagon.Append( *bottomline.IntersectLines( bottomleftline ) );
147  octagon.Append( *bottomline.IntersectLines( bottomrightline ) );
148  octagon.Append( *rightline.IntersectLines( bottomrightline ) );
149  octagon.Append( *rightline.IntersectLines( toprightline ) );
150  octagon.Append( *topline.IntersectLines( toprightline ) );
151  octagon.Append( *topline.IntersectLines( topleftline ) );
152  octagon.Append( *leftline.IntersectLines( topleftline ) );
153 
154  return octagon;
155 }
156 
157 
159 {
160  SHAPE_RECT r;
161 
162  VECTOR2I delta( aSeg.GetWidth() / 2, aSeg.GetWidth() / 2 );
163  VECTOR2I p0( aSeg.GetSeg().A - delta );
164  VECTOR2I p1( aSeg.GetSeg().B + delta );
165 
166  return SHAPE_RECT( std::min( p0.x, p1.x ), std::min( p0.y, p1.y ),
167  std::abs( p1.x - p0.x ), std::abs( p1.y - p0.y ) );
168 }
169 
170 #if 0
171 void DrawDebugPoint( VECTOR2I aP, int aColor )
172 {
174 
175  l.Append( aP - VECTOR2I( -50000, -50000 ) );
176  l.Append( aP + VECTOR2I( -50000, -50000 ) );
177 
178  ROUTER::GetInstance()->DisplayDebugLine ( l, aColor, 10000 );
179 
180  l.Clear();
181  l.Append( aP - VECTOR2I( 50000, -50000 ) );
182  l.Append( aP + VECTOR2I( 50000, -50000 ) );
183 
184  ROUTER::GetInstance()->DisplayDebugLine( l, aColor, 10000 );
185 }
186 
187 
188 void DrawDebugBox( BOX2I aB, int aColor )
189 {
191 
192  VECTOR2I o = aB.GetOrigin();
193  VECTOR2I s = aB.GetSize();
194 
195  l.Append( o );
196  l.Append( o.x + s.x, o.y );
197  l.Append( o.x + s.x, o.y + s.y );
198  l.Append( o.x, o.y + s.y );
199  l.Append( o );
200 
201  ROUTER::GetInstance()->DisplayDebugLine( l, aColor, 10000 );
202 }
203 
204 
205 void DrawDebugSeg( SEG aS, int aColor )
206 {
208 
209  l.Append( aS.A );
210  l.Append( aS.B );
211 
212  ROUTER::GetInstance()->DisplayDebugLine( l, aColor, 10000 );
213 }
214 
215 
216 void DrawDebugDirs( VECTOR2D aP, int aMask, int aColor )
217 {
218  BOX2I b( aP - VECTOR2I( 10000, 10000 ), VECTOR2I( 20000, 20000 ) );
219 
220  DrawDebugBox( b, aColor );
221  for( int i = 0; i < 8; i++ )
222  {
223  if( ( 1 << i ) & aMask )
224  {
225  VECTOR2I v = DIRECTION_45( ( DIRECTION_45::Directions ) i ).ToVector() * 100000;
226  DrawDebugSeg( SEG( aP, aP + v ), aColor );
227  }
228  }
229 }
230 #endif
231 
232 OPT_BOX2I ChangedArea( const ITEM* aItemA, const ITEM* aItemB )
233 {
234  if( aItemA->OfKind( ITEM::VIA_T ) && aItemB->OfKind( ITEM::VIA_T ) )
235  {
236  const VIA* va = static_cast<const VIA*>( aItemA );
237  const VIA* vb = static_cast<const VIA*>( aItemB );
238 
239  return va->ChangedArea( vb );
240  }
241  else if( aItemA->OfKind( ITEM::LINE_T ) && aItemB->OfKind( ITEM::LINE_T ) )
242  {
243  const LINE* la = static_cast<const LINE*> ( aItemA );
244  const LINE* lb = static_cast<const LINE*> ( aItemB );
245 
246  return la->ChangedArea( lb );
247  }
248 
249  return OPT_BOX2I();
250 }
251 
252 OPT_BOX2I ChangedArea( const LINE& aLineA, const LINE& aLineB )
253 {
254  return aLineA.ChangedArea( &aLineB );
255 }
256 
257 }
ITEM.
Definition: pns_item.h:53
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
const SHAPE_LINE_CHAIN ConvexHull(const SHAPE_SIMPLE &aConvex, int aClearance)
Function ConvexHull()
Definition: pns_utils.cpp:102
constexpr int HULL_MARGIN
Definition: pns_utils.h:34
SHAPE_SIMPLE.
Definition: shape_simple.h:42
static const int dist[10][10]
Definition: ar_matrix.cpp:326
OPT_BOX2I ChangedArea(const LINE *aOther) const
Definition: pns_line.cpp:875
coord_type GetX() const
Definition: box2.h:189
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:320
const VECTOR2I ToVector() const
Function ToVector()
Definition: direction45.h:299
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:186
OPT_BOX2I ChangedArea(const ITEM *aItemA, const ITEM *aItemB)
Definition: pns_utils.cpp:232
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_simple.h:74
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
OPT_BOX2I ChangedArea(const VIA *aOther) const
Definition: pns_via.cpp:104
const SHAPE_LINE_CHAIN SegmentHull(const SHAPE_SEGMENT &aSeg, int aClearance, int aWalkaroundThickness)
Definition: pns_utils.cpp:54
const SEG & GetSeg() const
Definition: shape_segment.h:72
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void SetClosed(bool aClosed)
Function SetClosed()
static void MoveDiagonal(SEG &aDiagonal, const SHAPE_LINE_CHAIN &aVertices, int aClearance)
Definition: pns_utils.cpp:90
DIRECTION_45.
Definition: direction45.h:37
coord_type GetWidth() const
Definition: box2.h:196
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:128
const SHAPE_LINE_CHAIN & Vertices() const
Function Vertices()
Definition: shape_simple.h:125
const SHAPE_LINE_CHAIN OctagonalHull(const VECTOR2I &aP0, const VECTOR2I &aSize, int aClearance, int aChamfer)
Various utility functions.
Definition: pns_utils.cpp:34
Definition: seg.h:39
coord_type GetY() const
Definition: box2.h:190
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:392
const SEG CSegment(int aIndex) const
Function CSegment()
SHAPE_LINE_CHAIN.
SHAPE_RECT ApproximateSegmentAsRect(const SHAPE_SEGMENT &aSeg)
Definition: pns_utils.cpp:158
VECTOR2I A
Definition: seg.h:47
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:132
coord_type GetHeight() const
Definition: box2.h:197
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:524
void Clear()
Function Clear() Removes all points from the line chain.
const Vec & GetSize() const
Definition: box2.h:188
const Vec & GetOrigin() const
Definition: box2.h:192
Directions
Enum Directions Represents available directions - there are 8 of them, as on a rectilinear map (north...
Definition: direction45.h:46
Push and Shove diff pair dimensions (gap) settings dialog.
int GetWidth() const
Definition: shape_segment.h:82
static ROUTER * GetInstance()
Definition: pns_router.cpp:77
int Side(const VECTOR2I &aP) const
Function Side()
Definition: seg.h:133
VECTOR2I B
Definition: seg.h:48