KiCad PCB EDA Suite
pad_custom_shape_functions.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) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
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 
30 #include <fctsys.h>
31 #include <trigo.h>
32 
33 #include <pcbnew.h>
34 
35 #include <class_pad.h>
36 #include <class_drawsegment.h>
37 #include <class_edge_mod.h>
39 #include <geometry/shape_rect.h>
40 #include <geometry/convex_hull.h>
42 #include <bezier_curves.h>
43 
44 
46 {
47  aTarget->SetShape( m_Shape );
48  aTarget->SetWidth( m_Thickness );
49  aTarget->SetStart( m_Start );
50  aTarget->SetEnd( m_End );
51  aTarget->SetBezControl1( m_Ctrl1 );
52  aTarget->SetBezControl2( m_Ctrl2 );
53 
54  // in a DRAWSEGMENT the radius of a circle is calculated from the
55  // center and one point on the circle outline (stored in m_End)
56  if( m_Shape == S_CIRCLE )
57  {
58  wxPoint end = m_Start;
59  end.x += m_Radius;
60  aTarget->SetEnd( end );
61  }
62 
63  aTarget->SetAngle( m_ArcAngle );
64  aTarget->SetPolyPoints( m_Poly );
65 }
66 
67 
69 {
70  ExportTo( static_cast<DRAWSEGMENT*>( aTarget ) );
71  // Initialize coordinates specific to the EDGE_MODULE (m_Start0 and m_End0)
72  aTarget->SetLocalCoord();
73 }
74 
75 
76 void PAD_CS_PRIMITIVE::Move( wxPoint aMoveVector )
77 {
78  m_Start += aMoveVector;
79  m_End += aMoveVector;
80  m_Ctrl1 += aMoveVector;
81  m_Ctrl2 += aMoveVector;
82 
83  for( auto& corner : m_Poly )
84  {
85  corner += aMoveVector;
86  }
87 }
88 
89 
90 /*
91  * Has meaning only for free shape pads.
92  * add a free shape to the shape list.
93  * the shape is a polygon (can be with thick outline), segment, circle or arc
94  */
95 
96 void D_PAD::AddPrimitive( const SHAPE_POLY_SET& aPoly, int aThickness )
97 {
98  std::vector<wxPoint> points;
99 
100  // If aPoly has holes, convert it to a polygon with no holes.
101  SHAPE_POLY_SET poly_no_hole;
102  poly_no_hole.Append( aPoly );
104 
105  for( auto iter = poly_no_hole.CIterate(); iter; iter++ )
106  points.push_back( wxPoint( iter->x, iter->y ) );
107 
108  AddPrimitive( points, aThickness );
109 }
110 
111 void D_PAD::AddPrimitive( const std::vector<wxPoint>& aPoly, int aThickness )
112 {
113  PAD_CS_PRIMITIVE shape( S_POLYGON );
114  shape.m_Poly = aPoly;
115  shape.m_Thickness = aThickness;
116  m_basicShapes.push_back( shape );
117 
119 }
120 
121 
122 void D_PAD::AddPrimitive( wxPoint aStart, wxPoint aEnd, int aThickness )
123 {
124  PAD_CS_PRIMITIVE shape( S_SEGMENT );
125  shape.m_Start = aStart;
126  shape.m_End = aEnd;
127  shape.m_Thickness = aThickness;
128  m_basicShapes.push_back( shape );
129 
131 }
132 
133 
134 void D_PAD::AddPrimitive( wxPoint aCenter, wxPoint aStart, int aArcAngle, int aThickness )
135 {
136  PAD_CS_PRIMITIVE shape( S_ARC );
137  shape.m_Start = aCenter;
138  shape.m_End = aStart;
139  shape.m_ArcAngle = aArcAngle;
140  shape.m_Thickness = aThickness;
141  m_basicShapes.push_back( shape );
142 
144 }
145 
146 
147 void D_PAD::AddPrimitive( wxPoint aStart, wxPoint aEnd, wxPoint aCtrl1, wxPoint aCtrl2, int aThickness )
148 {
149  PAD_CS_PRIMITIVE shape( S_CURVE );
150  shape.m_Start = aStart;
151  shape.m_End = aEnd;
152  shape.m_Ctrl1 = aCtrl1;
153  shape.m_Ctrl2 = aCtrl2;
154  shape.m_Thickness = aThickness;
155  m_basicShapes.push_back( shape );
156 
158 }
159 
160 
161 void D_PAD::AddPrimitive( wxPoint aCenter, int aRadius, int aThickness )
162 {
163  PAD_CS_PRIMITIVE shape( S_CIRCLE );
164  shape.m_Start = aCenter;
165  shape.m_Radius = aRadius;
166  shape.m_Thickness = aThickness;
167  m_basicShapes.push_back( shape );
168 
170 }
171 
172 
173 bool D_PAD::SetPrimitives( const std::vector<PAD_CS_PRIMITIVE>& aPrimitivesList )
174 {
175  // clear old list
176  m_basicShapes.clear();
177 
178  // Import to the basic shape list
179  if( aPrimitivesList.size() )
180  m_basicShapes = aPrimitivesList;
181 
182  // Only one polygon is expected (pad area = only one copper area)
183  return MergePrimitivesAsPolygon();
184 }
185 
186 bool D_PAD::AddPrimitives( const std::vector<PAD_CS_PRIMITIVE>& aPrimitivesList )
187 {
188  for( const auto& prim : aPrimitivesList )
189  m_basicShapes.push_back( prim );
190 
191  return MergePrimitivesAsPolygon();
192 }
193 
194 
195 // clear the basic shapes list and associated data
197 {
198  m_basicShapes.clear();
200 }
201 
202 
204  int aCircleToSegmentsCount )
205 
206 {
207  SHAPE_POLY_SET aux_polyset;
208 
209  for( unsigned cnt = 0; cnt < m_basicShapes.size(); ++cnt )
210  {
211  const PAD_CS_PRIMITIVE& bshape = m_basicShapes[cnt];
212 
213  switch( bshape.m_Shape )
214  {
215  case S_CURVE:
216  {
217  std::vector<wxPoint> ctrlPoints = { bshape.m_Start, bshape.m_Ctrl1, bshape.m_Ctrl2, bshape.m_End };
218  BEZIER_POLY converter( ctrlPoints );
219  std::vector< wxPoint> poly;
220  converter.GetPoly( poly, bshape.m_Thickness );
221 
222  for( unsigned ii = 1; ii < poly.size(); ii++ )
223  {
225  poly[ii-1], poly[ii], aCircleToSegmentsCount, bshape.m_Thickness );
226  }
227  break;
228  }
229 
230  case S_SEGMENT: // usual segment : line with rounded ends
232  bshape.m_Start, bshape.m_End, aCircleToSegmentsCount, bshape.m_Thickness );
233  break;
234 
235  case S_ARC: // Arc with rounded ends
236  TransformArcToPolygon( aux_polyset,
237  bshape.m_Start, bshape.m_End, bshape.m_ArcAngle,
238  aCircleToSegmentsCount, bshape.m_Thickness );
239  break;
240 
241  case S_CIRCLE: // ring or circle
242  if( bshape.m_Thickness ) // ring
243  TransformRingToPolygon( aux_polyset,
244  bshape.m_Start, bshape.m_Radius,
245  aCircleToSegmentsCount, bshape.m_Thickness ) ;
246  else // Filled circle
247  TransformCircleToPolygon( aux_polyset,
248  bshape.m_Start, bshape.m_Radius,
249  aCircleToSegmentsCount ) ;
250  break;
251 
252  case S_POLYGON: // polygon
253  if( bshape.m_Poly.size() < 2 )
254  break; // Malformed polygon.
255 
256  {
257  // Insert the polygon:
258  const std::vector< wxPoint>& poly = bshape.m_Poly;
259  aux_polyset.NewOutline();
260 
261  if( bshape.m_Thickness )
262  {
263  SHAPE_POLY_SET polyset;
264  polyset.NewOutline();
265 
266  for( unsigned ii = 0; ii < poly.size(); ii++ )
267  {
268  polyset.Append( poly[ii].x, poly[ii].y );
269  }
270 
272 
273  aux_polyset.Append( polyset );
274  }
275 
276  else
277  for( unsigned ii = 0; ii < poly.size(); ii++ )
278  aux_polyset.Append( poly[ii].x, poly[ii].y );
279  }
280  break;
281 
282  default:
283  break;
284  }
285  }
286 
287  aux_polyset.Simplify( SHAPE_POLY_SET::PM_FAST );
288 
289  // Merge all polygons with the initial pad anchor shape
290  if( aux_polyset.OutlineCount() )
291  {
292  aMergedPolygon->BooleanAdd( aux_polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
293  aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
294  }
295 
296  return aMergedPolygon->OutlineCount() <= 1;
297 }
298 
299 /* Merge all basic shapes, converted to a polygon in one polygon,
300  * return true if OK, false in there is more than one polygon
301  * in aMergedPolygon
302  */
304  int aCircleToSegmentsCount )
305 {
306  // if aMergedPolygon == NULL, use m_customShapeAsPolygon as target
307 
308  if( !aMergedPolygon )
309  aMergedPolygon = &m_customShapeAsPolygon;
310 
311  aMergedPolygon->RemoveAllContours();
312 
313  // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
314  // The anchor pad is always at 0,0
315  switch( GetAnchorPadShape() )
316  {
317  default:
318  case PAD_SHAPE_CIRCLE:
319  TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0,0 ), GetSize().x/2,
320  aCircleToSegmentsCount );
321  break;
322 
323  case PAD_SHAPE_RECT:
324  {
325  SHAPE_RECT rect( -GetSize().x/2, -GetSize().y/2, GetSize().x, GetSize().y );
326  aMergedPolygon->AddOutline( rect.Outline() );
327  }
328  break;
329  }
330 
331  if ( !buildCustomPadPolygon( aMergedPolygon, aCircleToSegmentsCount ) )
332  return false;
333 
334  m_boundingRadius = -1; // The current bouding radius is no more valid.
335 
336  return aMergedPolygon->OutlineCount() <= 1;
337 }
338 
340  wxPoint aPosition, double aRotation ) const
341 {
342  if( aMergedPolygon->OutlineCount() == 0 )
343  return;
344 
345  // Move, rotate, ... coordinates in aMergedPolygon according to the
346  // pad position and orientation
347  for( int cnt = 0; cnt < aMergedPolygon->OutlineCount(); ++cnt )
348  {
349  SHAPE_LINE_CHAIN& poly = aMergedPolygon->Outline( cnt );
350 
351  for( int ii = 0; ii < poly.PointCount(); ++ii )
352  {
353  wxPoint corner( poly.Point( ii ).x, poly.Point( ii ).y );
354  RotatePoint( &corner, aRotation );
355  corner += aPosition;
356 
357  poly.Point( ii ).x = corner.x;
358  poly.Point( ii ).y = corner.y;
359  }
360  }
361 }
362 
364 {
365  SHAPE_POLY_SET poly;
366 
368  return false;
369 
370  const int minSteps = 10;
371  const int maxSteps = 50;
372 
373  int stepsX, stepsY;
374 
375  auto bbox = poly.BBox();
376 
377  if( bbox.GetWidth() < bbox.GetHeight() )
378  {
379  stepsX = minSteps;
380  stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
381  }
382  else
383  {
384  stepsY = minSteps;
385  stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
386  }
387 
388  stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
389  stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
390 
391  auto center = bbox.Centre();
392 
393  auto minDist = std::numeric_limits<int64_t>::max();
394  int64_t minDistEdge;
395 
397  {
398  minDistEdge = GetSize().x;
399  }
400  else
401  {
402  minDistEdge = std::max( GetSize().x, GetSize().y );
403  }
404 
405  OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
406 
407  for ( int y = 0; y < stepsY ; y++ )
408  {
409  for ( int x = 0; x < stepsX; x++ )
410  {
411  VECTOR2I p = bbox.GetPosition();
412  p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
413  p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
414 
415  if ( poly.Contains(p) )
416  {
417 
418  auto dist = (center - p).EuclideanNorm();
419  auto distEdge = poly.COutline(0).Distance( p, true );
420  if ( distEdge >= minDistEdge )
421  {
422  if ( dist < minDist )
423  {
424  bestAnchor = p;
425  minDist = dist;
426  }
427  }
428  }
429  }
430  }
431 
432  if ( bestAnchor )
433  {
434  aPos = *bestAnchor;
435  return true;
436  }
437 
438  return false;
439 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:121
void TransformRoundedEndsSegmentToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aStart, wxPoint aEnd, int aCircleToSegmentsCount, int aWidth)
Function TransformRoundedEndsSegmentToPolygon convert a segment with rounded ends to a polygon Conver...
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, bool aIgnoreHoles=false) const
Returns true if a given subpolygon contains the point aP.
int m_boundingRadius
radius of the circle containing the pad shape
Definition: class_pad.h:841
std::vector< PAD_CS_PRIMITIVE > m_basicShapes
for free shape pads: a list of basic shapes, in local coordinates, orient 0, coordinates relative to ...
Definition: class_pad.h:856
void SetShape(STROKE_T aShape)
int OutlineCount() const
Returns the number of outlines in the set
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:145
static const int dist[10][10]
Definition: ar_matrix.cpp:320
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset union For aFastMode meaning, see function booleanOp
bool MergePrimitivesAsPolygon(SHAPE_POLY_SET *aMergedPolygon=NULL, int aCircleToSegmentsCount=ARC_APPROX_SEGMENTS_COUNT_HIGH_DEF)
Merge all basic shapes, converted to a polygon in one polygon, in m_customShapeAsPolygon.
PAD_SHAPE_T GetAnchorPadShape() const
Function GetAnchorPadShape.
Definition: class_pad.h:228
#define ARC_APPROX_SEGMENTS_COUNT_LOW_DEF
Definition: pcbnew.h:41
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.
bool GetBestAnchorPosition(VECTOR2I &aPos)
bool AddPrimitives(const std::vector< PAD_CS_PRIMITIVE > &aPrimitivesList)
Add to the basic shape list.
void ExportTo(DRAWSEGMENT *aTarget)
Export the PAD_CS_PRIMITIVE parameters to a DRAWSEGMENT useful to draw a primitive shape.
wxPoint m_Start
angle of an arc, from its starting point, in 0.1 deg
Definition: class_pad.h:100
polygon (not yet used for tracks, but could be in microwave apps)
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
usual segment : line with rounded ends
void Move(wxPoint aMoveVector)
returns the start point of an arc
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
int PointCount() const
Function PointCount()
void SetBezControl2(const wxPoint &aPoint)
wxPoint m_Ctrl1
is also the start point of the arc
Definition: class_pad.h:102
int m_Radius
thickness of segment or outline For filled S_CIRCLE shape, thickness = 0.
Definition: class_pad.h:98
const auto NULLOPT
Definition: optional.h:9
void Inflate(int aFactor, int aCircleSegmentsCount)
Performs outline inflation/deflation, using round corners.
std::vector< wxPoint > m_Poly
Bezier Control point 2.
Definition: class_pad.h:104
Class SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
Arcs (with rounded ends)
Helper class to handle a primitive (basic shape: polygon, segment, circle or arc) to build a custom p...
Definition: class_pad.h:91
a few functions useful in geometry calculations.
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
SHAPE_POLY_SET m_customShapeAsPolygon
for free shape pads: the set of basic shapes, merged as one polygon, in local coordinates,...
Definition: class_pad.h:861
void TransformCircleToPolygon(SHAPE_LINE_CHAIN &aBuffer, wxPoint aCenter, int aRadius, int aCircleToSegmentsCount)
Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines.
Bezier Curve.
wxPoint m_End
is also the center of the circle and arc
Definition: class_pad.h:101
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
STROKE_T m_Shape
Definition: class_pad.h:94
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
Pad object description.
wxPoint m_Ctrl2
Bezier Control point 1.
Definition: class_pad.h:103
void SetStart(const wxPoint &aStart)
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
int m_Thickness
S_SEGMENT, S_ARC, S_CIRCLE, S_POLYGON only (same as DRAWSEGMENT)
Definition: class_pad.h:95
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
void SetLocalCoord()
Set relative coordinates from draw coordinates.
void TransformRingToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCentre, int aRadius, int aCircleToSegmentsCount, int aWidth)
Function TransformRingToPolygon Creates a polygon from a ring Convert arcs to multiple straight segme...
Bezier curves to polygon converter.
Definition: bezier_curves.h:35
void TransformArcToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCentre, wxPoint aStart, double aArcAngle, int aCircleToSegmentsCount, int aWidth)
Function TransformArcToPolygon Creates a polygon from an Arc Convert arcs to multiple straight segmen...
Class to handle a graphic segment.
#define max(a, b)
Definition: auxiliary.h:86
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Class SHAPE_LINE_CHAIN.
void CustomShapeAsPolygonToBoardPosition(SHAPE_POLY_SET *aMergedPolygon, wxPoint aPosition, double aRotation) const
When created, the corners coordinates are relative to the pad position, orientation 0,...
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
void AddPrimitive(const SHAPE_POLY_SET &aPoly, int aThickness)
Has meaning only for free shape pads.
coord_type GetHeight() const
Definition: box2.h:196
boost::optional< T > OPT
Definition: optional.h:7
void SetEnd(const wxPoint &aEnd)
void SetAngle(double aAngle)
Function SetAngle sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
VECTOR2I & Point(int aIndex)
Function Point()
bool buildCustomPadPolygon(SHAPE_POLY_SET *aMergedPolygon, int aCircleToSegmentsCount)
const wxSize & GetSize() const
Definition: class_pad.h:271
EDGE_MODULE class definition.
#define ARC_APPROX_SEGMENTS_COUNT_HIGH_DEF
Definition: pcbnew.h:42
const BOX2I BBox(int aClearance=0) const override
Function BBox()
bool SetPrimitives(const std::vector< PAD_CS_PRIMITIVE > &aPrimitivesList)
Import to the basic shape list.
void DeletePrimitivesList()
clear the basic shapes list
double m_ArcAngle
radius of a circle
Definition: class_pad.h:99
#define min(a, b)
Definition: auxiliary.h:85
void SetBezControl1(const wxPoint &aPoint)
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)
void SetWidth(int aWidth)