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_arc.h>
28 #include <geometry/shape_segment.h>
29 #include <math/box2.h>
30 
31 #include <cmath>
32 
33 namespace PNS {
34 
35 const SHAPE_LINE_CHAIN OctagonalHull( const VECTOR2I& aP0, const VECTOR2I& aSize,
36  int aClearance, int aChamfer )
37 {
39 
40  s.SetClosed( true );
41 
42  s.Append( aP0.x - aClearance, aP0.y - aClearance + aChamfer );
43  s.Append( aP0.x - aClearance + aChamfer, aP0.y - aClearance );
44  s.Append( aP0.x + aSize.x + aClearance - aChamfer, aP0.y - aClearance );
45  s.Append( aP0.x + aSize.x + aClearance, aP0.y - aClearance + aChamfer );
46  s.Append( aP0.x + aSize.x + aClearance, aP0.y + aSize.y + aClearance - aChamfer );
47  s.Append( aP0.x + aSize.x + aClearance - aChamfer, aP0.y + aSize.y + aClearance );
48  s.Append( aP0.x - aClearance + aChamfer, aP0.y + aSize.y + aClearance );
49  s.Append( aP0.x - aClearance, aP0.y + aSize.y + aClearance - aChamfer );
50 
51  return s;
52 }
53 
54 
55 const SHAPE_LINE_CHAIN ArcHull( const SHAPE_ARC& aSeg, int aClearance,
56  int aWalkaroundThickness )
57 {
58  int d = aSeg.GetWidth() / 2 + aClearance + aWalkaroundThickness / 2 + HULL_MARGIN;
59  int x = (int)( 2.0 / ( 1.0 + M_SQRT2 ) * d ) / 2;
60 
61  auto line = aSeg.ConvertToPolyline();
62 
64  s.SetClosed( true );
65  std::vector<VECTOR2I> reverse_line;
66 
67  auto seg = line.Segment( 0 );
68  VECTOR2I dir = seg.B - seg.A;
69  VECTOR2I p0 = dir.Perpendicular().Resize( d );
70  VECTOR2I ds = dir.Perpendicular().Resize( x );
71  VECTOR2I pd = dir.Resize( x );
72  VECTOR2I dp = dir.Resize( d );
73 
74  // Append the first curve
75  s.Append( seg.A + p0 - pd );
76  s.Append( seg.A - dp + ds );
77  s.Append( seg.A - dp - ds );
78  s.Append( seg.A - p0 - pd );
79 
80  for( int i = 1; i < line.SegmentCount(); i++ )
81  {
82  auto old_seg = seg;
83  auto endpt = ( old_seg.A - old_seg.B ).Resize( seg.Length() );
84  old_seg.A = old_seg.B + endpt;
85 
86  seg = line.Segment( i );
87  auto dir2 = old_seg.A - seg.B;
88 
89  p0 = dir2.Perpendicular().Resize( d );
90  s.Append( seg.A - p0 );
91  reverse_line.push_back( seg.A + p0 );
92  }
93 
94  pd = dir.Resize( x );
95  dp = dir.Resize( d );
96  s.Append( seg.B - p0 + pd );
97  s.Append( seg.B + dp - ds );
98  s.Append( seg.B + dp + ds );
99  s.Append( seg.B + p0 + pd );
100 
101  for( int i = reverse_line.size() - 1; i >= 0; i-- )
102  s.Append( reverse_line[i] );
103 
104  // make sure the hull outline is always clockwise
105  if( s.CSegment( 0 ).Side( line.Segment( 0 ).A ) < 0 )
106  return s.Reverse();
107  else
108  return s;
109 }
110 
111 
112 const SHAPE_LINE_CHAIN SegmentHull ( const SHAPE_SEGMENT& aSeg, int aClearance,
113  int aWalkaroundThickness )
114 {
115  int d = aSeg.GetWidth() / 2 + aClearance + aWalkaroundThickness / 2 + HULL_MARGIN;
116  int x = (int)( 2.0 / ( 1.0 + M_SQRT2 ) * d );
117 
118  const VECTOR2I a = aSeg.GetSeg().A;
119  const VECTOR2I b = aSeg.GetSeg().B;
120 
121  VECTOR2I dir = b - a;
122  VECTOR2I p0 = dir.Perpendicular().Resize( d );
123  VECTOR2I ds = dir.Perpendicular().Resize( x / 2 );
124  VECTOR2I pd = dir.Resize( x / 2 );
125  VECTOR2I dp = dir.Resize( d );
126 
128 
129  s.SetClosed( true );
130 
131  s.Append( b + p0 + pd );
132  s.Append( b + dp + ds );
133  s.Append( b + dp - ds );
134  s.Append( b - p0 + pd );
135  s.Append( a - p0 - pd );
136  s.Append( a - dp - ds );
137  s.Append( a - dp + ds );
138  s.Append( a + p0 - pd );
139 
140  // make sure the hull outline is always clockwise
141  if( s.CSegment( 0 ).Side( a ) < 0 )
142  return s.Reverse();
143  else
144  return s;
145 }
146 
147 
148 static void MoveDiagonal( SEG& aDiagonal, const SHAPE_LINE_CHAIN& aVertices, int aClearance )
149 {
150  int dist;
151 
152  aVertices.NearestPoint( aDiagonal, dist );
153  dist -= HULL_MARGIN;
154  VECTOR2I moveBy = ( aDiagonal.A - aDiagonal.B ).Perpendicular().Resize( dist - aClearance );
155  aDiagonal.A += moveBy;
156  aDiagonal.B += moveBy;
157 }
158 
159 
160 const SHAPE_LINE_CHAIN ConvexHull( const SHAPE_SIMPLE& aConvex, int aClearance )
161 {
162  // this defines the horizontal and vertical lines in the hull octagon
163  BOX2I box = aConvex.BBox( aClearance + HULL_MARGIN );
164  box.Normalize();
165 
166  SEG topline = SEG( VECTOR2I( box.GetX(), box.GetY() + box.GetHeight() ),
167  VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() + box.GetHeight() ) );
168  SEG rightline = SEG( VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() + box.GetHeight() ),
169  VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() ) );
170  SEG bottomline = SEG( VECTOR2I( box.GetX() + box.GetWidth(), box.GetY() ),
171  box.GetOrigin() );
172  SEG leftline = SEG( box.GetOrigin(), VECTOR2I( box.GetX(), box.GetY() + box.GetHeight() ) );
173 
174  const SHAPE_LINE_CHAIN& vertices = aConvex.Vertices();
175 
176  // top right diagonal
177  VECTOR2I corner = box.GetOrigin() + box.GetSize();
178  SEG toprightline = SEG( corner,
179  corner + VECTOR2I( box.GetHeight(), -box.GetHeight() ) );
180  MoveDiagonal( toprightline, vertices, aClearance );
181 
182  // bottom right diagonal
183  corner = box.GetOrigin() + VECTOR2I( box.GetWidth(), 0 );
184  SEG bottomrightline = SEG( corner + VECTOR2I( box.GetHeight(), box.GetHeight() ),
185  corner );
186  MoveDiagonal( bottomrightline, vertices, aClearance );
187 
188  // bottom left diagonal
189  corner = box.GetOrigin();
190  SEG bottomleftline = SEG( corner,
191  corner + VECTOR2I( -box.GetHeight(), box.GetHeight() ) );
192  MoveDiagonal( bottomleftline, vertices, aClearance );
193 
194  // top left diagonal
195  corner = box.GetOrigin() + VECTOR2I( 0, box.GetHeight() );
196  SEG topleftline = SEG( corner + VECTOR2I( -box.GetHeight(), -box.GetHeight() ),
197  corner );
198  MoveDiagonal( topleftline, vertices, aClearance );
199 
200  SHAPE_LINE_CHAIN octagon;
201  octagon.SetClosed( true );
202 
203  octagon.Append( *leftline.IntersectLines( bottomleftline ) );
204  octagon.Append( *bottomline.IntersectLines( bottomleftline ) );
205  octagon.Append( *bottomline.IntersectLines( bottomrightline ) );
206  octagon.Append( *rightline.IntersectLines( bottomrightline ) );
207  octagon.Append( *rightline.IntersectLines( toprightline ) );
208  octagon.Append( *topline.IntersectLines( toprightline ) );
209  octagon.Append( *topline.IntersectLines( topleftline ) );
210  octagon.Append( *leftline.IntersectLines( topleftline ) );
211 
212  return octagon;
213 }
214 
215 
217 {
218  SHAPE_RECT r;
219 
220  VECTOR2I delta( aSeg.GetWidth() / 2, aSeg.GetWidth() / 2 );
221  VECTOR2I p0( aSeg.GetSeg().A - delta );
222  VECTOR2I p1( aSeg.GetSeg().B + delta );
223 
224  return SHAPE_RECT( std::min( p0.x, p1.x ), std::min( p0.y, p1.y ),
225  std::abs( p1.x - p0.x ), std::abs( p1.y - p0.y ) );
226 }
227 
228 #if 0
229 void DrawDebugPoint( VECTOR2I aP, int aColor )
230 {
232 
233  l.Append( aP - VECTOR2I( -50000, -50000 ) );
234  l.Append( aP + VECTOR2I( -50000, -50000 ) );
235 
236  ROUTER::GetInstance()->DisplayDebugLine ( l, aColor, 10000 );
237 
238  l.Clear();
239  l.Append( aP - VECTOR2I( 50000, -50000 ) );
240  l.Append( aP + VECTOR2I( 50000, -50000 ) );
241 
242  ROUTER::GetInstance()->DisplayDebugLine( l, aColor, 10000 );
243 }
244 
245 
246 void DrawDebugBox( BOX2I aB, int aColor )
247 {
249 
250  VECTOR2I o = aB.GetOrigin();
251  VECTOR2I s = aB.GetSize();
252 
253  l.Append( o );
254  l.Append( o.x + s.x, o.y );
255  l.Append( o.x + s.x, o.y + s.y );
256  l.Append( o.x, o.y + s.y );
257  l.Append( o );
258 
259  ROUTER::GetInstance()->DisplayDebugLine( l, aColor, 10000 );
260 }
261 
262 
263 void DrawDebugSeg( SEG aS, int aColor )
264 {
266 
267  l.Append( aS.A );
268  l.Append( aS.B );
269 
270  ROUTER::GetInstance()->DisplayDebugLine( l, aColor, 10000 );
271 }
272 
273 
274 void DrawDebugDirs( VECTOR2D aP, int aMask, int aColor )
275 {
276  BOX2I b( aP - VECTOR2I( 10000, 10000 ), VECTOR2I( 20000, 20000 ) );
277 
278  DrawDebugBox( b, aColor );
279  for( int i = 0; i < 8; i++ )
280  {
281  if( ( 1 << i ) & aMask )
282  {
283  VECTOR2I v = DIRECTION_45( ( DIRECTION_45::Directions ) i ).ToVector() * 100000;
284  DrawDebugSeg( SEG( aP, aP + v ), aColor );
285  }
286  }
287 }
288 #endif
289 
290 OPT_BOX2I ChangedArea( const ITEM* aItemA, const ITEM* aItemB )
291 {
292  if( aItemA->OfKind( ITEM::VIA_T ) && aItemB->OfKind( ITEM::VIA_T ) )
293  {
294  const VIA* va = static_cast<const VIA*>( aItemA );
295  const VIA* vb = static_cast<const VIA*>( aItemB );
296 
297  return va->ChangedArea( vb );
298  }
299  else if( aItemA->OfKind( ITEM::LINE_T ) && aItemB->OfKind( ITEM::LINE_T ) )
300  {
301  const LINE* la = static_cast<const LINE*> ( aItemA );
302  const LINE* lb = static_cast<const LINE*> ( aItemB );
303 
304  return la->ChangedArea( lb );
305  }
306 
307  return OPT_BOX2I();
308 }
309 
310 OPT_BOX2I ChangedArea( const LINE& aLineA, const LINE& aLineB )
311 {
312  return aLineA.ChangedArea( &aLineB );
313 }
314 
315 }
Directions
Enum Directions Represents available directions - there are 8 of them, as on a rectilinear map (north...
Definition: direction45.h:46
ITEM.
Definition: pns_item.h:54
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:160
constexpr int HULL_MARGIN
Definition: pns_utils.h:34
SHAPE_SIMPLE.
Definition: shape_simple.h:43
OPT_BOX2I ChangedArea(const LINE *aOther) const
Definition: pns_line.cpp:916
coord_type GetX() const
Definition: box2.h:190
VECTOR2< T > Perpendicular() const
Function Perpendicular computes the perpendicular vector.
Definition: vector2d.h:320
const VECTOR2I ToVector() const
Function ToVector()
Definition: direction45.h:269
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:191
OPT_BOX2I ChangedArea(const ITEM *aItemA, const ITEM *aItemB)
Definition: pns_utils.cpp:290
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_simple.h:82
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
OPT_BOX2I ChangedArea(const VIA *aOther) const
Definition: pns_via.cpp:107
const SHAPE_LINE_CHAIN SegmentHull(const SHAPE_SEGMENT &aSeg, int aClearance, int aWalkaroundThickness)
Definition: pns_utils.cpp:112
const SEG & GetSeg() const
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
void SetClosed(bool aClosed)
Function SetClosed()
const SHAPE_LINE_CHAIN ConvertToPolyline(double aAccuracy=0.005 *PCB_IU_PER_MM) const
Constructs a SHAPE_LINE_CHAIN of segments from a given arc.
Definition: shape_arc.cpp:370
static void MoveDiagonal(SEG &aDiagonal, const SHAPE_LINE_CHAIN &aVertices, int aClearance)
Definition: pns_utils.cpp:148
DIRECTION_45.
Definition: direction45.h:37
coord_type GetWidth() const
Definition: box2.h:197
BOX2< Vec > & Normalize()
Function Normalize ensures that the height ant width are positive.
Definition: box2.h:129
const SHAPE_LINE_CHAIN & Vertices() const
Function Vertices()
Definition: shape_simple.h:133
const SHAPE_LINE_CHAIN OctagonalHull(const VECTOR2I &aP0, const VECTOR2I &aSize, int aClearance, int aChamfer)
Definition: pns_utils.cpp:35
Definition: seg.h:39
coord_type GetY() const
Definition: box2.h:191
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 SHAPE_LINE_CHAIN ArcHull(const SHAPE_ARC &aSeg, int aClearance, int aWalkaroundThickness)
Various utility functions.
Definition: pns_utils.cpp:55
int GetWidth() const
Definition: shape_arc.h:102
const SEG CSegment(int aIndex) const
Function CSegment()
SHAPE_LINE_CHAIN.
SHAPE_RECT ApproximateSegmentAsRect(const SHAPE_SEGMENT &aSeg)
Definition: pns_utils.cpp:216
VECTOR2I A
Definition: seg.h:47
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:134
coord_type GetHeight() const
Definition: box2.h:198
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:525
void Clear()
Function Clear() Removes all points from the line chain.
const Vec & GetSize() const
Definition: box2.h:189
const Vec & GetOrigin() const
Definition: box2.h:193
Push and Shove diff pair dimensions (gap) settings dialog.
int GetWidth() const
static ROUTER * GetInstance()
Definition: pns_router.cpp:79
int Side(const VECTOR2I &aP) const
Function Side()
Definition: seg.h:138
VECTOR2I B
Definition: seg.h:48