KiCad PCB EDA Suite
router_preview_item.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-2021 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 <deque>
23 #include <gal/color4d.h>
24 
25 #include <geometry/shape_rect.h>
26 #include <geometry/shape_simple.h>
27 #include <pcb_painter.h>
28 #include <trigo.h>
29 
30 #include "router_preview_item.h"
31 
32 #include "pns_arc.h"
33 #include "pns_line.h"
34 #include "pns_segment.h"
35 #include "pns_via.h"
36 
37 using namespace KIGFX;
38 
39 
42 {
43  m_view = aView;
44 
45  m_shape = aItem ? aItem->Shape()->Clone() : nullptr;
46 
47  m_clearance = -1;
49 
50  m_showTrackClearance = false;
51  m_showViaClearance = false;
52 
53  // initialize variables, overwritten by Update( aItem ), if aItem != NULL
54  m_router = nullptr;
55  m_type = PR_SHAPE;
56  m_style = 0;
57  m_width = 0;
58  m_depth = 0;
59 
60  if( aItem )
61  Update( aItem );
62 }
63 
64 
66 {
67  delete m_shape;
68 }
69 
70 
72 {
73  m_originLayer = aItem->Layers().Start();
74 
75  if( const auto l = dyn_cast<const PNS::LINE*>( aItem ) )
76  {
77  if( !l->SegmentCount() )
78  return;
79  }
80  else if( const auto v = dyn_cast<const PNS::VIA*>( aItem ) )
81  {
82  if( v->IsVirtual() )
83  return;
84  }
85 
86  assert( m_originLayer >= 0 );
87 
90  m_color.a = 0.8;
91  m_depth = BaseOverlayDepth - aItem->Layers().Start();
92 
93  switch( aItem->Kind() )
94  {
95  case PNS::ITEM::LINE_T:
96  m_type = PR_SHAPE;
97  m_width = static_cast<const PNS::LINE*>( aItem )->Width();
98  break;
99 
100  case PNS::ITEM::ARC_T:
101  m_type = PR_SHAPE;
102  m_width = static_cast<const PNS::ARC*>( aItem )->Width();
103  break;
104 
106  m_type = PR_SHAPE;
107  m_width = static_cast<const PNS::SEGMENT*>( aItem )->Width();
108  break;
109 
110  case PNS::ITEM::VIA_T:
112  m_type = PR_SHAPE;
113  m_width = 0;
114  m_color = COLOR4D( 0.7, 0.7, 0.7, 0.8 );
116  break;
117 
118  case PNS::ITEM::SOLID_T:
119  m_type = PR_SHAPE;
120  m_width = 0;
121  break;
122 
123  default:
124  break;
125  }
126 
127  if( aItem->Marker() & PNS::MK_VIOLATION )
128  m_color = COLOR4D( 0, 1, 0, 1 );
129 }
130 
131 
133 {
134  BOX2I bbox;
135 
136  switch( m_type )
137  {
138  case PR_SHAPE:
139  if( m_shape )
140  {
141  bbox = m_shape->BBox();
142  bbox.Inflate( m_width / 2 );
143  }
144  return bbox;
145 
146  case PR_POINT:
147  bbox = BOX2I ( m_pos - VECTOR2I( 100000, 100000 ), VECTOR2I( 200000, 200000 ) );
148  return bbox;
149 
150  default:
151  break;
152  }
153 
154  return bbox;
155 }
156 
157 
159 {
160  gal->SetIsFill( false );
161 
162  for( int s = 0; s < aL->GetSegmentCount(); s++ )
163  gal->DrawLine( aL->GetSegment( s ).A, aL->GetSegment( s ).B );
164 
165  const SHAPE_LINE_CHAIN* lineChain = dynamic_cast<const SHAPE_LINE_CHAIN*>( aL );
166 
167  for( size_t s = 0; lineChain && s < lineChain->ArcCount(); s++ )
168  {
169  const SHAPE_ARC& arc = lineChain->CArcs()[s];
170 
171  double start_angle = DEG2RAD( arc.GetStartAngle() );
172  double angle = DEG2RAD( arc.GetCentralAngle() );
173 
174  gal->DrawArc( arc.GetCenter(), arc.GetRadius(), start_angle, start_angle + angle);
175  }
176 
177  if( aL->IsClosed() )
178  gal->DrawLine( aL->GetSegment( -1 ).B, aL->GetSegment( 0 ).A );
179 }
180 
181 
182 void ROUTER_PREVIEW_ITEM::drawShape( const SHAPE* aShape, KIGFX::GAL* gal ) const
183 {
184  switch( aShape->Type() )
185  {
187  {
188  const SHAPE_LINE_CHAIN_BASE* l = (const SHAPE_LINE_CHAIN_BASE*) aShape;
189 
190  if( m_showTrackClearance && m_clearance > 0 )
191  {
192  gal->SetLineWidth( m_width + 2 * m_clearance );
193  drawLineChain( l, gal );
194  }
195 
196  gal->SetLayerDepth( m_depth );
197  gal->SetLineWidth( m_width );
198  gal->SetStrokeColor( m_color );
199  gal->SetFillColor( m_color );
200  drawLineChain( l, gal );
201  break;
202  }
203 
204  case SH_LINE_CHAIN:
205  {
206  const SHAPE_LINE_CHAIN* l = (const SHAPE_LINE_CHAIN*) aShape;
207  const int w = l->Width();
208 
209  if( m_showTrackClearance && m_clearance > 0 )
210  {
211  gal->SetLineWidth( w + 2 * m_clearance );
212  drawLineChain( l, gal );
213  }
214 
215  gal->SetLayerDepth( m_depth );
216  gal->SetLineWidth( w );
217  gal->SetStrokeColor( m_color );
218  gal->SetFillColor( m_color );
219  drawLineChain( l, gal );
220  break;
221  }
222 
223  case SH_SEGMENT:
224  {
225  const SHAPE_SEGMENT* s = (const SHAPE_SEGMENT*) aShape;
226  const int w = s->GetWidth();
227 
228  if( m_showTrackClearance && m_clearance > 0 )
229  {
230  gal->SetLineWidth( w + 2 * m_clearance );
231  gal->DrawSegment( s->GetSeg().A, s->GetSeg().B, s->GetWidth() + 2 * m_clearance );
232  }
233 
234  gal->SetLayerDepth( m_depth );
235  gal->SetLineWidth( w );
236  gal->SetStrokeColor( m_color );
237  gal->SetFillColor( m_color );
238  gal->DrawSegment( s->GetSeg().A, s->GetSeg().B, s->GetWidth() );
239  break;
240  }
241 
242  case SH_CIRCLE:
243  {
244  const SHAPE_CIRCLE* c = (const SHAPE_CIRCLE*) aShape;
245  gal->SetStrokeColor( m_color );
246 
247  if( m_showViaClearance && m_clearance > 0 )
248  {
249  gal->SetIsStroke( false );
250  gal->DrawCircle( c->GetCenter(), c->GetRadius() + m_clearance );
251  }
252 
253  gal->SetLayerDepth( m_depth );
254  gal->SetIsStroke( m_width ? true : false );
255  gal->SetLineWidth( m_width );
256  gal->SetFillColor( m_color );
257  gal->DrawCircle( c->GetCenter(), c->GetRadius() );
258 
259  break;
260  }
261 
262  case SH_RECT:
263  {
264  const SHAPE_RECT* r = (const SHAPE_RECT*) aShape;
265  gal->SetFillColor( m_color );
266 
267  if( m_clearance > 0 )
268  {
269  VECTOR2I p0( r->GetPosition() ), s( r->GetSize() );
270  gal->SetIsStroke( true );
271  gal->SetLineWidth( 2 * m_clearance );
272  gal->DrawLine( p0, VECTOR2I( p0.x + s.x, p0.y ) );
273  gal->DrawLine( p0, VECTOR2I( p0.x, p0.y + s.y ) );
274  gal->DrawLine( p0 + s , VECTOR2I( p0.x + s.x, p0.y ) );
275  gal->DrawLine( p0 + s, VECTOR2I( p0.x, p0.y + s.y ) );
276  }
277 
278  gal->SetLayerDepth( m_depth );
279  gal->SetIsStroke( m_width ? true : false );
280  gal->SetLineWidth( m_width);
281  gal->SetStrokeColor( m_color );
282  gal->DrawRectangle( r->GetPosition(), r->GetPosition() + r->GetSize() );
283 
284  break;
285  }
286 
287  case SH_SIMPLE:
288  {
289  const SHAPE_SIMPLE* c = (const SHAPE_SIMPLE*) aShape;
290  std::deque<VECTOR2D> polygon = std::deque<VECTOR2D>();
291 
292  for( int i = 0; i < c->PointCount(); i++ )
293  {
294  polygon.push_back( c->CDPoint( i ) );
295  }
296 
297  gal->SetFillColor( m_color );
298 
299  if( m_clearance > 0 )
300  {
301  gal->SetIsStroke( true );
302  gal->SetLineWidth( 2 * m_clearance );
303 
304  // need the implicit last segment to be explicit for DrawPolyline
305  polygon.push_back( c->CDPoint( 0 ) );
306  gal->DrawPolyline( polygon );
307  }
308 
309  gal->SetLayerDepth( m_depth );
310  gal->SetIsStroke( m_width ? true : false );
311  gal->SetLineWidth( m_width );
312  gal->SetStrokeColor( m_color );
313  gal->DrawPolygon( polygon );
314  break;
315  }
316 
317  case SH_ARC:
318  {
319  const SHAPE_ARC* arc = static_cast<const SHAPE_ARC*>( aShape );
320  const int w = arc->GetWidth();
321 
322  auto start_angle = DEG2RAD( arc->GetStartAngle() );
323  auto angle = DEG2RAD( arc->GetCentralAngle() );
324 
325  gal->SetIsFill( false );
326  gal->SetIsStroke( true );
327 
328  if( m_showTrackClearance && m_clearance > 0 )
329  {
330  gal->SetLineWidth( w + 2 * m_clearance );
331  gal->DrawArc( arc->GetCenter(), arc->GetRadius(), start_angle, start_angle + angle );
332  }
333 
334  gal->SetLayerDepth( m_depth );
335  gal->SetStrokeColor( m_color );
336  gal->SetFillColor( m_color );
337  gal->SetLineWidth( w );
338  gal->DrawArc( arc->GetCenter(), arc->GetRadius(), start_angle, start_angle + angle );
339  break;
340  }
341 
342  case SH_COMPOUND:
343  wxFAIL_MSG( "Router preview item: nested compound shapes not supported" );
344  break;
345 
346  case SH_POLY_SET:
347  wxFAIL_MSG( "Router preview item: SHAPE_POLY_SET not supported" );
348  break;
349 
350  case SH_NULL:
351  break;
352  }
353 }
354 
355 
356 void ROUTER_PREVIEW_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
357 {
358  GAL* gal = aView->GetGAL();
359  //col.Brighten(0.7);
360 
361  if( m_type == PR_SHAPE )
362  {
363  if( !m_shape )
364  return;
365 
366  // N.B. The order of draw here is important
367  // Cairo doesn't current support z-ordering, so we need
368  // to draw the clearance first to ensure it is in the background
370 
371  //TODO(snh) Add configuration option for the color/alpha here
372  gal->SetStrokeColor( COLOR4D( DARKDARKGRAY ).WithAlpha( 0.9 ) );
373  gal->SetFillColor( COLOR4D( DARKDARKGRAY ).WithAlpha( 0.7 ) );
374  gal->SetIsStroke( m_width ? true : false );
375  gal->SetIsFill( true );
376 
378  {
379  std::vector<SHAPE*> subshapes;
380  m_shape->GetIndexableSubshapes( subshapes );
381 
382  for( SHAPE* shape : subshapes )
383  drawShape( shape, gal );
384  }
385  else
386  {
387  drawShape( m_shape, gal );
388  }
389  }
390 }
391 
392 
393 void ROUTER_PREVIEW_ITEM::Line( const SHAPE_LINE_CHAIN& aLine, int aWidth, int aStyle )
394 {
395  m_width = aWidth;
396 
397  if( aStyle >= 0 )
398  {
399  m_color = assignColor( aStyle );
400  }
401 
402  m_type = PR_SHAPE;
403  m_depth = -1024; // TODO gal->GetMinDepth()
404 
405  SHAPE_LINE_CHAIN *lc = static_cast<SHAPE_LINE_CHAIN*>( aLine.Clone() );
406  lc->SetWidth( aWidth );
407  m_shape = lc;
408 }
409 
410 
411 void ROUTER_PREVIEW_ITEM::Point( const VECTOR2I& aPos, int aStyle )
412 {
413 }
414 
415 
416 void ROUTER_PREVIEW_ITEM::Box( const BOX2I& aBox, int aStyle )
417 {
418 }
419 
420 
422 {
423  auto settings = static_cast<PCB_RENDER_SETTINGS*>( m_view->GetPainter()->GetSettings() );
424 
425  return settings->GetLayerColor( aLayer );
426 }
427 
428 
429 const COLOR4D ROUTER_PREVIEW_ITEM::assignColor( int aStyle ) const
430 {
431  COLOR4D color;
432 
433  switch( aStyle )
434  {
435  case 0:
436  color = COLOR4D( 0, 1, 0, 1 ); break;
437 
438  case 1:
439  color = COLOR4D( 1, 0, 0, 1 ); break;
440 
441  case 2:
442  color = COLOR4D( 1, 1, 0, 1 ); break;
443 
444  case 3:
445  color = COLOR4D( 0, 0, 1, 1 ); break;
446 
447  case 4:
448  color = COLOR4D( 1, 1, 1, 1 ); break;
449 
450  case 5:
451  color = COLOR4D( 1, 1, 0, 1 ); break;
452 
453  case 6:
454  color = COLOR4D( 0, 1, 1, 1 ); break;
455 
456  case 32:
457  color = COLOR4D( 0, 0, 1, 1 ); break;
458 
459  default:
460  color = COLOR4D( 0.4, 0.4, 0.4, 1 ); break;
461  }
462 
463  return color;
464 }
465 
466 const int ROUTER_PREVIEW_ITEM::ClearanceOverlayDepth = -VIEW::VIEW_MAX_LAYERS - 10;
467 const int ROUTER_PREVIEW_ITEM::BaseOverlayDepth = -VIEW::VIEW_MAX_LAYERS - 20;
468 const int ROUTER_PREVIEW_ITEM::ViaOverlayDepth = -VIEW::VIEW_MAX_LAYERS - 50;
compound shape, consisting of multiple simple shapes
Definition: shape.h:49
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
Base class for PNS router board items.
Definition: pns_item.h:55
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList)
Draw a polyline.
void drawShape(const SHAPE *aShape, KIGFX::GAL *aGal) const
static const int ClearanceOverlayDepth
currently selected items overlay
Definition: layer_ids.h:214
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a rectangle.
BOX2< VECTOR2I > BOX2I
Definition: box2.h:506
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:41
void drawLineChain(const SHAPE_LINE_CHAIN_BASE *aL, KIGFX::GAL *aGal) const
int GetRadius() const
Definition: shape_circle.h:107
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
static const int BaseOverlayDepth
double GetRadius() const
Definition: shape_arc.cpp:492
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, double aStartAngle, double aEndAngle)
Draw an arc.
virtual bool IsClosed() const =0
the 3d code uses this value
Definition: typeinfo.h:79
int color
Definition: DXF_plotter.cpp:57
int Width() const
Get the current width of the segments in the chain.
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:190
double GetStartAngle() const
Definition: shape_arc.cpp:444
const VECTOR2I GetCenter() const
Definition: shape_circle.h:112
size_t ArcCount() const
virtual size_t GetSegmentCount() const =0
const std::vector< SHAPE_ARC > & CArcs() const
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:208
const KIGFX::COLOR4D getLayerColor(int aLayer) const
const SEG & GetSeg() const
virtual void SetLineWidth(float aLineWidth)
Set the line width.
int Start() const
Definition: pns_layerset.h:82
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
void SetWidth(int aWidth)
Set the width of all segments in the chain.
const VECTOR2D CDPoint(int aIndex) const
Return a given point as a vector with elements of type double.
Definition: shape_simple.h:113
double a
Alpha component.
Definition: color4d.h:374
virtual void DrawSegment(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth)
Draw a rounded segment.
Meta control for all vias opacity/visibility.
Definition: layer_ids.h:188
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual const SHAPE * Shape() const
Return the geometrical shape of the item.
Definition: pns_item.h:198
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
void Update(const PNS::ITEM *aItem)
void Line(const SHAPE_LINE_CHAIN &aLine, int aWidth=0, int aStyle=-1)
const KIGFX::COLOR4D assignColor(int aStyle) const
virtual SHAPE * Clone() const
Return a dynamically allocated copy of the shape.
Definition: shape.h:139
circular arc
Definition: shape.h:50
An abstract shape on 2D plane.
Definition: shape.h:116
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
circle
Definition: shape.h:46
void Point(const VECTOR2I &aPos, int aStyle=0)
virtual const SEG GetSegment(int aIndex) const =0
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:281
int GetWidth() const
Definition: shape_arc.h:156
set of polygons (with holes, etc.)
Definition: shape.h:48
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList)
Draw a polygon.
a single triangle belonging to a POLY_SET triangulation
Definition: shape.h:52
virtual void GetIndexableSubshapes(std::vector< SHAPE * > &aSubshapes)
Definition: shape.h:106
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
double DEG2RAD(double deg)
Definition: trigo.h:229
Represent a polyline (an zero-thickness chain of connected line segments).
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
VECTOR2I A
Definition: seg.h:48
int PointCount() const
Return the number of points (vertices) in this polygon.
Definition: shape_simple.h:88
double GetCentralAngle() const
Definition: shape_arc.cpp:479
empty shape (no shape...),
Definition: shape.h:51
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
line chain (polyline)
Definition: shape.h:45
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:128
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
virtual bool HasIndexableSubshapes() const
Definition: shape.h:99
axis-aligned rectangle
Definition: shape.h:43
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
static const int ViaOverlayDepth
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
int GetWidth() const
simple polygon
Definition: shape.h:47
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94
void Box(const BOX2I &aBox, int aStyle=0)
VECTOR2I GetCenter() const
Definition: shape_arc.cpp:464
const LAYER_RANGE & Layers() const
Definition: pns_item.h:152
virtual int Marker() const
Definition: pns_item.h:210
ROUTER_PREVIEW_ITEM(const PNS::ITEM *aItem=nullptr, KIGFX::VIEW *aView=nullptr)
line segment
Definition: shape.h:44
Abstract interface for drawing on a 2D-surface.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
VECTOR2I B
Definition: seg.h:49