KiCad PCB EDA Suite
class_drawsegment.cpp
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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
7  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <fctsys.h>
28 #include <macros.h>
29 #include <gr_basic.h>
30 #include <bezier_curves.h>
31 #include <pcb_screen.h>
32 #include <trigo.h>
33 #include <msgpanel.h>
34 #include <bitmaps.h>
35 #include <pcb_edit_frame.h>
36 #include <pcbnew.h>
37 #include <class_board.h>
38 #include <class_module.h>
39 #include <class_drawsegment.h>
40 #include <base_units.h>
41 #include <math/util.h> // for KiROUND
42 
43 
45  BOARD_ITEM( aParent, idtype )
46 {
47  m_Type = 0;
48  m_Angle = 0;
49  m_Flags = 0;
51  m_Width = Millimeter2iu( DEFAULT_LINE_WIDTH );
52 }
53 
54 
56 {
57 }
58 
59 
60 void DRAWSEGMENT::SetPosition( const wxPoint& aPos )
61 {
62  m_Start = aPos;
63 }
64 
65 
67 {
68  if( m_Shape == S_POLYGON )
69  return (wxPoint) m_Poly.CVertex( 0 );
70  else
71  return m_Start;
72 }
73 
74 
75 double DRAWSEGMENT::GetLength() const
76 {
77  double length = 0.0;
78 
79  switch( m_Shape )
80  {
81  case S_CURVE:
82  for( size_t ii = 1; ii < m_BezierPoints.size(); ++ii )
83  length += GetLineLength( m_BezierPoints[ii - 1], m_BezierPoints[ii] );
84 
85  break;
86 
87  default:
88  length = GetLineLength( GetStart(), GetEnd() );
89  break;
90  }
91 
92  return length;
93 }
94 
95 
96 void DRAWSEGMENT::Move( const wxPoint& aMoveVector )
97 {
98  // Move vector should not affect start/end for polygon since it will
99  // be applied directly to polygon outline.
100  if( m_Shape != S_POLYGON )
101  {
102  m_Start += aMoveVector;
103  m_End += aMoveVector;
104  }
105 
106  switch ( m_Shape )
107  {
108  case S_POLYGON:
109  m_Poly.Move( VECTOR2I( aMoveVector ) );
110  break;
111 
112  case S_CURVE:
113  m_BezierC1 += aMoveVector;
114  m_BezierC2 += aMoveVector;
115 
116  for( unsigned int ii = 0; ii < m_BezierPoints.size(); ii++ )
117  {
118  m_BezierPoints[ii] += aMoveVector;
119  }
120 
121  break;
122 
123  default:
124  break;
125  }
126 }
127 
128 
129 void DRAWSEGMENT::Rotate( const wxPoint& aRotCentre, double aAngle )
130 {
131  switch( m_Shape )
132  {
133  case S_ARC:
134  case S_SEGMENT:
135  case S_CIRCLE:
136  // these can all be done by just rotating the start and end points
137  RotatePoint( &m_Start, aRotCentre, aAngle);
138  RotatePoint( &m_End, aRotCentre, aAngle);
139  break;
140 
141  case S_POLYGON:
142  m_Poly.Rotate( -DECIDEG2RAD( aAngle ), VECTOR2I( aRotCentre ) );
143  break;
144 
145  case S_CURVE:
146  RotatePoint( &m_Start, aRotCentre, aAngle);
147  RotatePoint( &m_End, aRotCentre, aAngle);
148  RotatePoint( &m_BezierC1, aRotCentre, aAngle);
149  RotatePoint( &m_BezierC2, aRotCentre, aAngle);
150 
151  for( unsigned int ii = 0; ii < m_BezierPoints.size(); ii++ )
152  {
153  RotatePoint( &m_BezierPoints[ii], aRotCentre, aAngle);
154  }
155  break;
156 
157  case S_RECT:
158  default:
159  // un-handled edge transform
160  wxASSERT_MSG( false, wxT( "DRAWSEGMENT::Rotate not implemented for "
161  + ShowShape( m_Shape ) ) );
162  break;
163  }
164 }
165 
166 
167 void DRAWSEGMENT::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
168 {
169  if( aFlipLeftRight )
170  {
171  m_Start.x = aCentre.x - ( m_Start.x - aCentre.x );
172  m_End.x = aCentre.x - ( m_End.x - aCentre.x );
173  }
174  else
175  {
176  m_Start.y = aCentre.y - ( m_Start.y - aCentre.y );
177  m_End.y = aCentre.y - ( m_End.y - aCentre.y );
178  }
179 
180  switch ( m_Shape )
181  {
182  case S_ARC:
183  m_Angle = -m_Angle;
184  break;
185 
186  case S_POLYGON:
187  m_Poly.Mirror( aFlipLeftRight, !aFlipLeftRight, VECTOR2I( aCentre ) );
188  break;
189 
190  case S_CURVE:
191  {
192  if( aFlipLeftRight )
193  {
194  m_BezierC1.x = aCentre.x - ( m_BezierC1.x - aCentre.x );
195  m_BezierC2.x = aCentre.x - ( m_BezierC2.x - aCentre.x );
196  }
197  else
198  {
199  m_BezierC1.y = aCentre.y - ( m_BezierC1.y - aCentre.y );
200  m_BezierC2.y = aCentre.y - ( m_BezierC2.y - aCentre.y );
201  }
202 
203  // Rebuild the poly points shape
204  std::vector<wxPoint> ctrlPoints = { m_Start, m_BezierC1, m_BezierC2, m_End };
205  BEZIER_POLY converter( ctrlPoints );
206  converter.GetPoly( m_BezierPoints, m_Width );
207  }
208  break;
209 
210  default:
211  break;
212  }
213 
214  // DRAWSEGMENT items are not allowed on copper layers, so
215  // copper layers count is not taken in account in Flip transform
216  SetLayer( FlipLayer( GetLayer() ) );
217 }
218 
219 
221 {
222  // Has meaning only for S_CURVE DRAW_SEGMENT shape
223  if( m_Shape != S_CURVE )
224  {
225  m_BezierPoints.clear();
226  return;
227  }
228  // Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
229  std::vector<wxPoint> ctrlPoints = { m_Start, m_BezierC1, m_BezierC2, m_End };
230  BEZIER_POLY converter( ctrlPoints );
231  converter.GetPoly( m_BezierPoints, aMinSegLen );
232 }
233 
234 
236 {
237  wxPoint c;
238 
239  switch( m_Shape )
240  {
241  case S_ARC:
242  case S_CIRCLE:
243  c = m_Start;
244  break;
245 
246  case S_SEGMENT:
247  // Midpoint of the line
248  c = ( GetStart() + GetEnd() ) / 2;
249  break;
250 
251  case S_POLYGON:
252  case S_RECT:
253  case S_CURVE:
254  c = GetBoundingBox().Centre();
255  break;
256 
257  default:
258  wxASSERT_MSG( false, "DRAWSEGMENT::GetCentre not implemented for shape"
259  + ShowShape( GetShape() ) );
260  break;
261  }
262 
263  return c;
264 }
265 
266 
268 {
269  wxPoint endPoint( m_End ); // start of arc
270 
271  switch( m_Shape )
272  {
273  case S_ARC:
274  // rotate the starting point of the arc, given by m_End, through the
275  // angle m_Angle to get the ending point of the arc.
276  // m_Start is the arc centre
277  endPoint = m_End; // m_End = start point of arc
278  RotatePoint( &endPoint, m_Start, -m_Angle );
279  break;
280 
281  default:
282  break;
283  }
284 
285  return endPoint; // after rotation, the end of the arc.
286 }
287 
288 
290 {
291  wxPoint endPoint( m_End );
292 
293  switch( m_Shape )
294  {
295  case S_ARC:
296  // rotate the starting point of the arc, given by m_End, through half
297  // the angle m_Angle to get the middle of the arc.
298  // m_Start is the arc centre
299  endPoint = m_End; // m_End = start point of arc
300  RotatePoint( &endPoint, m_Start, -m_Angle / 2.0 );
301  break;
302 
303  default:
304  break;
305  }
306 
307  return endPoint; // after rotation, the end of the arc.
308 }
309 
310 
312 {
313  // due to the Y axis orient atan2 needs - y value
314  double angleStart = ArcTangente( GetArcStart().y - GetCenter().y,
315  GetArcStart().x - GetCenter().x );
316 
317  // Normalize it to 0 ... 360 deg, to avoid discontinuity for angles near 180 deg
318  // because 180 deg and -180 are very near angles when ampping betewwen -180 ... 180 deg.
319  // and this is not easy to handle in calculations
320  NORMALIZE_ANGLE_POS( angleStart );
321 
322  return angleStart;
323 }
324 
325 
326 void DRAWSEGMENT::SetAngle( double aAngle )
327 {
328  // m_Angle must be >= -360 and <= +360 degrees
329  m_Angle = NormalizeAngle360Max( aAngle );
330 }
331 
332 
334 {
335  if( !m_Parent || m_Parent->Type() != PCB_MODULE_T )
336  return NULL;
337 
338  return (MODULE*) m_Parent;
339 }
340 
341 
342 void DRAWSEGMENT::Print( PCB_BASE_FRAME* aFrame, wxDC* DC, const wxPoint& aOffset )
343 {
344  int ux0, uy0, dx, dy;
345  int l_trace;
346  int radius;
347 
348  BOARD* brd = GetBoard( );
349 
350  if( brd->IsLayerVisible( GetLayer() ) == false )
351  return;
352 
353  auto color = aFrame->Settings().Colors().GetLayerColor( GetLayer() );
354  auto displ_opts = aFrame->GetDisplayOptions();
355 
356  l_trace = m_Width >> 1; // half trace width
357 
358  // Line start point or Circle and Arc center
359  ux0 = m_Start.x + aOffset.x;
360  uy0 = m_Start.y + aOffset.y;
361 
362  // Line end point or circle and arc start point
363  dx = m_End.x + aOffset.x;
364  dy = m_End.y + aOffset.y;
365 
366  bool filled = displ_opts.m_DisplayDrawItemsFill;
367 
368  if( m_Flags & FORCE_SKETCH )
369  filled = SKETCH;
370 
371  switch( m_Shape )
372  {
373  case S_CIRCLE:
374  radius = KiROUND( Distance( ux0, uy0, dx, dy ) );
375 
376  if( filled )
377  {
378  GRCircle( nullptr, DC, ux0, uy0, radius, m_Width, color );
379  }
380  else
381  {
382  GRCircle( nullptr, DC, ux0, uy0, radius - l_trace, color );
383  GRCircle( nullptr, DC, ux0, uy0, radius + l_trace, color );
384  }
385 
386  break;
387 
388  case S_ARC:
389  double StAngle, EndAngle;
390  radius = KiROUND( Distance( ux0, uy0, dx, dy ) );
391  StAngle = ArcTangente( dy - uy0, dx - ux0 );
392  EndAngle = StAngle + m_Angle;
393 
394  if( StAngle > EndAngle )
395  std::swap( StAngle, EndAngle );
396 
397  if( filled )
398  {
399  GRArc( nullptr, DC, ux0, uy0, StAngle, EndAngle, radius, m_Width, color );
400  }
401  else
402  {
403  GRArc( nullptr, DC, ux0, uy0, StAngle, EndAngle, radius - l_trace, color );
404  GRArc( nullptr, DC, ux0, uy0, StAngle, EndAngle, radius + l_trace, color );
405  }
406 
407  break;
408 
409  case S_CURVE:
410  {
412 
413  wxPoint& startp = m_BezierPoints[0];
414 
415  for( unsigned int i = 1; i < m_BezierPoints.size(); i++ )
416  {
417  wxPoint& endp = m_BezierPoints[i];
418 
419  if( filled )
420  GRFilledSegment( nullptr, DC, startp+aOffset, endp+aOffset, m_Width, color );
421  else
422  GRCSegm( nullptr, DC, startp+aOffset, endp+aOffset, m_Width, color );
423 
424  startp = m_BezierPoints[i];
425  }
426  }
427  break;
428 
429  case S_POLYGON:
430  {
431  SHAPE_POLY_SET& outline = GetPolyShape();
432  // Draw the polygon: only one polygon is expected
433  // However we provide a multi polygon shape drawing
434  // ( for the future or to show a non expected shape )
435  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
436  {
437  SHAPE_LINE_CHAIN& poly = outline.Outline( jj );
438  GRClosedPoly( nullptr, DC, poly.PointCount(), (const wxPoint*) &poly.CPoint( 0 ),
440  }
441  }
442  break;
443 
444  default:
445  if( filled )
446  GRFillCSegm( nullptr, DC, ux0, uy0, dx, dy, m_Width, color );
447  else
448  GRCSegm( nullptr, DC, ux0, uy0, dx, dy, m_Width, color );
449 
450  break;
451  }
452 }
453 
454 
455 void DRAWSEGMENT::GetMsgPanelInfo( EDA_UNITS aUnits, std::vector<MSG_PANEL_ITEM>& aList )
456 {
457  wxString msg;
458 
459  msg = _( "Drawing" );
460 
461  aList.emplace_back( _( "Type" ), msg, DARKCYAN );
462 
463  wxString shape = _( "Shape" );
464 
465  switch( m_Shape )
466  {
467  case S_CIRCLE:
468  aList.emplace_back( shape, _( "Circle" ), RED );
469 
470  msg = MessageTextFromValue( aUnits, GetLineLength( m_Start, m_End ) );
471  aList.emplace_back( _( "Radius" ), msg, RED );
472  break;
473 
474  case S_ARC:
475  aList.emplace_back( shape, _( "Arc" ), RED );
476  msg.Printf( wxT( "%.1f" ), m_Angle / 10.0 );
477  aList.emplace_back( _( "Angle" ), msg, RED );
478 
479  msg = MessageTextFromValue( aUnits, GetLineLength( m_Start, m_End ) );
480  aList.emplace_back( _( "Radius" ), msg, RED );
481  break;
482 
483  case S_CURVE:
484  aList.emplace_back( shape, _( "Curve" ), RED );
485 
486  msg = MessageTextFromValue( aUnits, GetLength() );
487  aList.emplace_back( _( "Length" ), msg, DARKGREEN );
488  break;
489 
490  case S_POLYGON:
491  aList.emplace_back( shape, _( "Polygon" ), RED );
492 
493  msg.Printf( "%d", GetPolyShape().Outline(0).PointCount() );
494  aList.emplace_back( _( "Points" ), msg, DARKGREEN );
495  break;
496 
497  default:
498  {
499  aList.emplace_back( shape, _( "Segment" ), RED );
500 
501  msg = MessageTextFromValue( aUnits, GetLineLength( m_Start, m_End ) );
502  aList.emplace_back( _( "Length" ), msg, DARKGREEN );
503 
504  // angle counter-clockwise from 3'o-clock
505  const double deg = RAD2DEG( atan2( (double)( m_Start.y - m_End.y ),
506  (double)( m_End.x - m_Start.x ) ) );
507  msg.Printf( wxT( "%.1f" ), deg );
508  aList.emplace_back( _( "Angle" ), msg, DARKGREEN );
509  }
510  }
511 
512  if( m_Shape == S_POLYGON )
513  {
514  VECTOR2I point0 = GetPolyShape().Outline(0).CPoint(0);
515  wxString origin = wxString::Format( "@(%s, %s)",
516  MessageTextFromValue( aUnits, point0.x ),
517  MessageTextFromValue( aUnits, point0.y ) );
518 
519  aList.emplace_back( _( "Origin" ), origin, DARKGREEN );
520  }
521  else
522  {
523  wxString start = wxString::Format( "@(%s, %s)",
524  MessageTextFromValue( aUnits, GetStart().x ),
525  MessageTextFromValue( aUnits, GetStart().y ) );
526  wxString end = wxString::Format( "@(%s, %s)",
527  MessageTextFromValue( aUnits, GetEnd().x ),
528  MessageTextFromValue( aUnits, GetEnd().y ) );
529 
530  aList.emplace_back( start, end, DARKGREEN );
531  }
532 
533  aList.emplace_back( _( "Layer" ), GetLayerName(), DARKBROWN );
534 
535  msg = MessageTextFromValue( aUnits, m_Width, true );
536  aList.emplace_back( _( "Width" ), msg, DARKCYAN );
537 }
538 
539 
541 {
542  EDA_RECT bbox;
543 
544  bbox.SetOrigin( m_Start );
545 
546  switch( m_Shape )
547  {
548  case S_SEGMENT:
549  bbox.SetEnd( m_End );
550  break;
551 
552  case S_CIRCLE:
553  bbox.Inflate( GetRadius() );
554  break;
555 
556  case S_ARC:
557  computeArcBBox( bbox );
558  break;
559 
560  case S_POLYGON:
561  if( m_Poly.IsEmpty() )
562  break;
563  {
564  wxPoint p_end;
565  MODULE* module = GetParentModule();
566  bool first = true;
567 
568  for( auto iter = m_Poly.CIterate(); iter; iter++ )
569  {
570  wxPoint pt ( iter->x, iter->y );
571 
572  if( module ) // Transform, if we belong to a module
573  {
574  RotatePoint( &pt, module->GetOrientation() );
575  pt += module->GetPosition();
576  }
577 
578 
579  if( first )
580  {
581  p_end = pt;
582  bbox.SetX( pt.x );
583  bbox.SetY( pt.y );
584  first = false;
585  }
586  else
587  {
588 
589  bbox.SetX( std::min( bbox.GetX(), pt.x ) );
590  bbox.SetY( std::min( bbox.GetY(), pt.y ) );
591 
592  p_end.x = std::max( p_end.x, pt.x );
593  p_end.y = std::max( p_end.y, pt.y );
594  }
595  }
596 
597  bbox.SetEnd( p_end );
598  break;
599  }
600 
601  case S_CURVE:
602 
603  bbox.Merge( m_BezierC1 );
604  bbox.Merge( m_BezierC2 );
605  bbox.Merge( m_End );
606  break;
607 
608  default:
609  break;
610  }
611 
612  bbox.Inflate( ((m_Width+1) / 2) + 1 );
613  bbox.Normalize();
614 
615  return bbox;
616 }
617 
618 
619 bool DRAWSEGMENT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
620 {
621  int maxdist = aAccuracy + ( m_Width / 2 );
622 
623  switch( m_Shape )
624  {
625  case S_CIRCLE:
626  case S_ARC:
627  {
628  wxPoint relPos = aPosition - GetCenter();
629  int radius = GetRadius();
630  int dist = KiROUND( EuclideanNorm( relPos ) );
631 
632  if( abs( radius - dist ) <= maxdist )
633  {
634  if( m_Shape == S_CIRCLE )
635  return true;
636 
637  // For arcs, the test point angle must be >= arc angle start
638  // and <= arc angle end
639  // However angle values > 360 deg are not easy to handle
640  // so we calculate the relative angle between arc start point and teast point
641  // this relative arc should be < arc angle if arc angle > 0 (CW arc)
642  // and > arc angle if arc angle < 0 (CCW arc)
643  double arc_angle_start = GetArcAngleStart(); // Always 0.0 ... 360 deg, in 0.1 deg
644 
645  double arc_hittest = ArcTangente( relPos.y, relPos.x );
646 
647  // Calculate relative angle between the starting point of the arc, and the test point
648  arc_hittest -= arc_angle_start;
649 
650  // Normalise arc_hittest between 0 ... 360 deg
651  NORMALIZE_ANGLE_POS( arc_hittest );
652 
653  // Check angle: inside the arc angle when it is > 0
654  // and outside the not drawn arc when it is < 0
655  if( GetAngle() >= 0.0 )
656  {
657  if( arc_hittest <= GetAngle() )
658  return true;
659  }
660  else
661  {
662  if( arc_hittest >= (3600.0 + GetAngle()) )
663  return true;
664  }
665  }
666  }
667  break;
668 
669  case S_CURVE:
670  ((DRAWSEGMENT*)this)->RebuildBezierToSegmentsPointsList( m_Width );
671 
672  for( unsigned int i= 1; i < m_BezierPoints.size(); i++)
673  {
674  if( TestSegmentHit( aPosition, m_BezierPoints[i-1], m_BezierPoints[i], maxdist ) )
675  return true;
676  }
677  break;
678 
679  case S_SEGMENT:
680  if( TestSegmentHit( aPosition, m_Start, m_End, maxdist ) )
681  return true;
682  break;
683 
684  case S_POLYGON:
685  {
686  if( !IsPolygonFilled() )
687  {
689  auto poly = m_Poly; //todo: Fix CollideEdge to be const
690  return poly.CollideEdge( VECTOR2I( aPosition ), i,
691  std::max( maxdist, Millimeter2iu( 0.25 ) ) );
692  }
693  else
694  return m_Poly.Collide( VECTOR2I( aPosition ), maxdist );
695  }
696  break;
697 
698  default:
699  wxASSERT_MSG( 0, wxString::Format( "unknown DRAWSEGMENT shape: %d", m_Shape ) );
700  break;
701  }
702 
703  return false;
704 }
705 
706 
707 bool DRAWSEGMENT::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
708 {
709  EDA_RECT arect = aRect;
710  arect.Normalize();
711  arect.Inflate( aAccuracy );
712 
713  EDA_RECT arcRect;
714  EDA_RECT bb = GetBoundingBox();
715 
716  switch( m_Shape )
717  {
718  case S_CIRCLE:
719  // Test if area intersects or contains the circle:
720  if( aContained )
721  return arect.Contains( bb );
722  else
723  {
724  // If the rectangle does not intersect the bounding box, this is a much quicker test
725  if( !aRect.Intersects( bb ) )
726  {
727  return false;
728  }
729  else
730  {
731  return arect.IntersectsCircleEdge( GetCenter(), GetRadius(), GetWidth() );
732  }
733  }
734  break;
735 
736  case S_ARC:
737  // Test for full containment of this arc in the rect
738  if( aContained )
739  {
740  return arect.Contains( bb );
741  }
742  // Test if the rect crosses the arc
743  else
744  {
745  arcRect = bb.Common( arect );
746 
747  /* All following tests must pass:
748  * 1. Rectangle must intersect arc BoundingBox
749  * 2. Rectangle must cross the outside of the arc
750  */
751  return arcRect.Intersects( arect ) &&
753  }
754  break;
755 
756  case S_SEGMENT:
757  if( aContained )
758  {
759  return arect.Contains( GetStart() ) && aRect.Contains( GetEnd() );
760  }
761  else
762  {
763  // Account for the width of the line
764  arect.Inflate( GetWidth() / 2 );
765  return arect.Intersects( GetStart(), GetEnd() );
766  }
767 
768  break;
769 
770  case S_POLYGON:
771  if( aContained )
772  {
773  return arect.Contains( bb );
774  }
775  else
776  {
777  // Fast test: if aRect is outside the polygon bounding box,
778  // rectangles cannot intersect
779  if( !arect.Intersects( bb ) )
780  return false;
781 
782  // Account for the width of the line
783  arect.Inflate( GetWidth() / 2 );
784  int count = m_Poly.TotalVertices();
785 
786  for( int ii = 0; ii < count; ii++ )
787  {
788  auto vertex = m_Poly.CVertex( ii );
789  auto vertexNext = m_Poly.CVertex( ( ii + 1 ) % count );
790 
791  // Test if the point is within aRect
792  if( arect.Contains( ( wxPoint ) vertex ) )
793  return true;
794 
795  // Test if this edge intersects aRect
796  if( arect.Intersects( ( wxPoint ) vertex, ( wxPoint ) vertexNext ) )
797  return true;
798  }
799  }
800  break;
801 
802  case S_CURVE: // not yet handled
803  if( aContained )
804  {
805  return arect.Contains( bb );
806  }
807  else
808  {
809  // Fast test: if aRect is outside the polygon bounding box,
810  // rectangles cannot intersect
811  if( !arect.Intersects( bb ) )
812  return false;
813 
814  // Account for the width of the line
815  arect.Inflate( GetWidth() / 2 );
816  unsigned count = m_BezierPoints.size();
817 
818  for( unsigned ii = 1; ii < count; ii++ )
819  {
820  wxPoint vertex = m_BezierPoints[ii-1];
821  wxPoint vertexNext = m_BezierPoints[ii];
822 
823  // Test if the point is within aRect
824  if( arect.Contains( ( wxPoint ) vertex ) )
825  return true;
826 
827  // Test if this edge intersects aRect
828  if( arect.Intersects( vertex, vertexNext ) )
829  return true;
830  }
831  }
832  break;
833 
834 
835  default:
836  wxASSERT_MSG( 0, wxString::Format( "unknown DRAWSEGMENT shape: %d", m_Shape ) );
837  break;
838  }
839 
840  return false;
841 }
842 
843 
845 {
846  return wxString::Format( _( "Pcb Graphic %s, length %s on %s" ),
847  ShowShape( m_Shape ),
848  MessageTextFromValue( aUnits, GetLength() ),
849  GetLayerName() );
850 }
851 
852 
854 {
855  return add_dashed_line_xpm;
856 }
857 
858 
860 {
861  return new DRAWSEGMENT( *this );
862 }
863 
864 
866 {
867  // For arcs - do not include the center point in the bounding box,
868  // it is redundant for displaying an arc
869  if( m_Shape == S_ARC )
870  {
871  EDA_RECT bbox;
872  bbox.SetOrigin( m_End );
873  computeArcBBox( bbox );
874  return BOX2I( bbox.GetOrigin(), bbox.GetSize() );
875  }
876 
877  return EDA_ITEM::ViewBBox();
878 }
879 
880 
882 {
883  // Do not include the center, which is not necessarily
884  // inside the BB of a arc with a small angle
885  aBBox.SetOrigin( m_End );
886 
887  wxPoint end = m_End;
888  RotatePoint( &end, m_Start, -m_Angle );
889  aBBox.Merge( end );
890 
891  // Determine the starting quarter
892  // 0 right-bottom
893  // 1 left-bottom
894  // 2 left-top
895  // 3 right-top
896  unsigned int quarter = 0; // assume right-bottom
897 
898  if( m_End.x < m_Start.x )
899  {
900  if( m_End.y <= m_Start.y )
901  quarter = 2;
902  else // ( m_End.y > m_Start.y )
903  quarter = 1;
904  }
905  else if( m_End.x >= m_Start.x )
906  {
907  if( m_End.y < m_Start.y )
908  quarter = 3;
909  else if( m_End.x == m_Start.x )
910  quarter = 1;
911  }
912 
913  int radius = GetRadius();
914  int angle = (int) GetArcAngleStart() % 900 + m_Angle;
915  bool directionCW = ( m_Angle > 0 ); // Is the direction of arc clockwise?
916 
917  // Make the angle positive, so we go clockwise and merge points belonging to the arc
918  if( !directionCW )
919  {
920  angle = 900 - angle;
921  quarter = ( quarter + 3 ) % 4; // -1 modulo arithmetic
922  }
923 
924  while( angle > 900 )
925  {
926  switch( quarter )
927  {
928  case 0:
929  aBBox.Merge( wxPoint( m_Start.x, m_Start.y + radius ) ); // down
930  break;
931 
932  case 1:
933  aBBox.Merge( wxPoint( m_Start.x - radius, m_Start.y ) ); // left
934  break;
935 
936  case 2:
937  aBBox.Merge( wxPoint( m_Start.x, m_Start.y - radius ) ); // up
938  break;
939 
940  case 3:
941  aBBox.Merge( wxPoint( m_Start.x + radius, m_Start.y ) ); // right
942  break;
943  }
944 
945  if( directionCW )
946  ++quarter;
947  else
948  quarter += 3; // -1 modulo arithmetic
949 
950  quarter %= 4;
951  angle -= 900;
952  }
953 }
954 
955 
956 void DRAWSEGMENT::SetPolyPoints( const std::vector<wxPoint>& aPoints )
957 {
959  m_Poly.NewOutline();
960 
961  for ( auto p : aPoints )
962  {
963  m_Poly.Append( p.x, p.y );
964  }
965 }
966 
967 
968 const std::vector<wxPoint> DRAWSEGMENT::BuildPolyPointsList() const
969 {
970  std::vector<wxPoint> rv;
971 
972  if( m_Poly.OutlineCount() )
973  {
974  if( m_Poly.COutline( 0 ).PointCount() )
975  {
976  for ( auto iter = m_Poly.CIterate(); iter; iter++ )
977  {
978  rv.emplace_back( iter->x, iter->y );
979  }
980  }
981  }
982 
983  return rv;
984 }
985 
986 
988 {
989  // return true if the polygonal shape is valid (has more than 2 points)
990  if( GetPolyShape().OutlineCount() == 0 )
991  return false;
992 
993  const SHAPE_LINE_CHAIN& outline = ((SHAPE_POLY_SET&)GetPolyShape()).Outline( 0 );
994 
995  return outline.PointCount() > 2;
996 }
997 
998 
1000 {
1001  // return the number of corners of the polygonal shape
1002  // this shape is expected to be only one polygon without hole
1003  if( GetPolyShape().OutlineCount() )
1004  return GetPolyShape().VertexCount( 0 );
1005 
1006  return 0;
1007 }
1008 
1009 
1011 {
1012  DRAWSEGMENT* image = dynamic_cast<DRAWSEGMENT*>( aImage );
1013  assert( image );
1014 
1015  std::swap( m_Width, image->m_Width );
1016  std::swap( m_Start, image->m_Start );
1017  std::swap( m_End, image->m_End );
1018  std::swap( m_Shape, image->m_Shape );
1019  std::swap( m_Type, image->m_Type );
1020  std::swap( m_Angle, image->m_Angle );
1021  std::swap( m_BezierC1, image->m_BezierC1 );
1022  std::swap( m_BezierC2, image->m_BezierC2 );
1023  std::swap( m_BezierPoints, image->m_BezierPoints );
1024  std::swap( m_Poly, image->m_Poly );
1025 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:123
const wxPoint GetArcMid() const
int TotalVertices() const
Returns total number of vertices stored in the set.
EDA_UNITS
Definition: common.h:72
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 })
Function Rotate rotates all vertices by a given angle.
static wxString ShowShape(STROKE_T aShape)
Function ShowShape converts the enum STROKE_T integer value to a wxString.
double GetOrientation() const
Definition: class_module.h:215
BOX2< VECTOR2I > BOX2I
Definition: box2.h:521
virtual const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
EDA_ITEM * m_Parent
Linked list: Link (parent struct)
Definition: base_struct.h:177
int OutlineCount() const
Returns the number of outlines in the set
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Function GetLineLength returns the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:202
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect.
PNG memory record (file in memory).
Definition: bitmap_def.h:29
virtual void Move(const wxPoint &aMoveVector) override
Function Move move this object.
static const int dist[10][10]
Definition: ar_matrix.cpp:326
const BITMAP_OPAQUE add_dashed_line_xpm[1]
T NormalizeAngle360Max(T Angle)
Normalize angle to be >=-360.0 and <= 360.0 Angle can be equal to -360 or +360.
Definition: trigo.h:222
bool CollideEdge(const VECTOR2I &aPoint, VERTEX_INDEX &aClosestVertex, int aClearance=0)
Function CollideEdge Checks whether aPoint collides with any edge of any of the contours of the polyg...
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
Implementation of conversion functions that require both schematic and board internal units.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
const wxPoint GetCenter() const override
Function GetCenter()
int GetX() const
Definition: eda_rect.h:111
STROKE_T GetShape() const
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:127
const wxPoint GetArcEnd() const
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Returns the index-th vertex in a given hole outline within a given outline
SHAPE_POLY_SET m_Poly
Stores the S_POLYGON shape.
int color
Definition: DXF_plotter.cpp:61
polygon (not yet used for tracks, but could be in microwave apps)
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Function FlippedLayerNumber.
Definition: lset.cpp:485
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
virtual BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments Has me...
double RAD2DEG(double rad)
Definition: trigo.h:211
int VertexCount(int aOutline=-1, int aHole=-1) const
Returns the number of vertices in a given outline/hole
double GetArcAngleStart() const
function GetArcAngleStart()
#define DEFAULT_LINE_WIDTH
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
Struct VERTEX_INDEX.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
void GRCSegm(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, int aPenSize, COLOR4D Color)
Definition: gr_basic.cpp:312
const wxPoint GetPosition() const override
bool IntersectsCircleEdge(const wxPoint &aCenter, const int aRadius, const int aWidth) const
IntersectsCircleEdge Tests for intersection between this rect and the edge (radius) of a circle.
EDA_RECT Common(const EDA_RECT &aRect) const
Function Common returns the area that is common with another rectangle.
usual segment : line with rounded ends
void SetPosition(const wxPoint &aPos) override
double m_Angle
Used only for Arcs: Arc angle in 1/10 deg.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:252
const std::vector< wxPoint > BuildPolyPointsList() const
Build and return the list of corners in a std::vector<wxPoint> It must be used only to convert the SH...
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
int PointCount() const
Function PointCount()
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Function GetDisplayOptions returns the display options current in use Display options are relative to...
virtual void SwapData(BOARD_ITEM *aImage) override
Swap data between aItem and aImage.
bool Contains(const wxPoint &aPoint) const
Function Contains.
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
void Mirror(bool aX=true, bool aY=false, const VECTOR2I &aRef={ 0, 0 })
Mirrors the line points about y or x (or both)
This file contains miscellaneous commonly used macros and functions.
wxPoint m_BezierC1
Bezier Control Point 1.
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:129
const VECTOR2I & CPoint(int aIndex) const
Function Point()
COLOR4D GetLayerColor(LAYER_NUM aLayer) const
Function GetLayerColor.
segment with non rounded ends
class MODULE, a footprint
Definition: typeinfo.h:89
virtual EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
wxPoint m_Start
Line start point or Circle and Arc center.
#define NULL
void computeArcBBox(EDA_RECT &aBBox) const
void Move(const VECTOR2I &aVector) override
wxPoint m_End
Line end point or circle and arc start point.
SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
void GRFillCSegm(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color)
Definition: gr_basic.cpp:415
PCB_GENERAL_SETTINGS & Settings()
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
Arcs (with rounded ends)
void GRArc(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, double StAngle, double EndAngle, int r, COLOR4D Color)
Definition: gr_basic.cpp:749
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
MODULE * GetParentModule() const
Function GetParentModule returns a pointer to the parent module, or NULL if DRAWSEGMENT does not belo...
void SetX(int val)
Definition: eda_rect.h:168
STROKE_T m_Shape
Shape: line, Circle, Arc.
Definition: colors.h:60
const wxPoint & GetArcStart() const
bool Collide(const VECTOR2I &aP, int aClearance=0) const override
Function Collide Checks whether the point aP collides with the inside of the polygon set; if the poin...
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
SHAPE_POLY_SET & GetPolyShape()
Bezier Curve.
int NewOutline()
Creates a new empty polygon in the set and returns its index
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
int GetPointCount() const
int GetWidth() const
COLORS_DESIGN_SETTINGS & Colors()
void SetY(int val)
Definition: eda_rect.h:174
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
virtual void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Function Flip Flip this object, i.e.
void Normalize()
Function Normalize ensures that the height ant width are positive.
double GetAngle() const
void GetMsgPanelInfo(EDA_UNITS aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
Bezier curves to polygon converter.
Definition: bezier_curves.h:35
bool IsLayerVisible(PCB_LAYER_ID aLayer) const
Function IsLayerVisible is a proxy function that calls the correspondent function in m_BoardSettings ...
Definition: class_board.h:449
virtual void Rotate(const wxPoint &aRotCentre, double aAngle) override
Function Rotate Rotate this object.
double GetLength() const
Function GetLength returns the length of the track using the hypotenuse calculation.
int m_Type
Used in complex associations ( Dimensions.. )
Class to handle a graphic segment.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:160
#define _(s)
Definition: 3d_actions.cpp:31
SHAPE_LINE_CHAIN.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void GRFilledSegment(EDA_RECT *aClipBox, wxDC *aDC, wxPoint aStart, wxPoint aEnd, int aWidth, COLOR4D aColor)
Definition: gr_basic.cpp:423
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void GRCircle(EDA_RECT *ClipBox, wxDC *DC, int xc, int yc, int r, int width, COLOR4D Color)
Definition: gr_basic.cpp:596
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:184
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
double DECIDEG2RAD(double deg)
Definition: trigo.h:214
void Print(PCB_BASE_FRAME *aFrame, wxDC *DC, const wxPoint &aOffset=ZeroOffset) override
Function Print BOARD_ITEMs have their own color information.
wxPoint m_BezierC2
Bezier Control Point 2.
int GetY() const
Definition: eda_rect.h:112
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:163
bool IsPolyShapeValid() const
wxPoint Centre() const
Definition: eda_rect.h:62
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
void SetAngle(double aAngle)
Function SetAngle sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
int m_Width
thickness of lines ...
Module description (excepted pads)
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:162
DRAWSEGMENT(BOARD_ITEM *aParent=NULL, KICAD_T idtype=PCB_LINE_T)
virtual const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
Message panel definition file.
wxString GetLayerName() const
Function GetLayerName returns the name of the PCB layer on which the item resides.
#define FORCE_SKETCH
Definition: pcbnew.h:44
void GRClosedPoly(EDA_RECT *ClipBox, wxDC *DC, int n, const wxPoint *Points, bool Fill, COLOR4D Color, COLOR4D BgColor)
Function GRClosedPoly draws a closed polygon onto the drawing context aDC and optionally fills and/or...
Definition: gr_basic.cpp:552
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
bool IsPolygonFilled() const
Polygonal shape is not always filled.
const wxPoint GetPosition() const override
Definition: class_module.h:210
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
const wxSize GetSize() const
Definition: eda_rect.h:103
KICAD_T Type() const
Function Type()
Definition: base_struct.h:207
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
std::vector< wxPoint > m_BezierPoints
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.