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 <PolyLine.h>
32 #include <trigo.h>
33 
34 #include <pcbnew.h>
35 
36 #include <class_pad.h>
37 #include <class_drawsegment.h>
38 #include <class_edge_mod.h>
40 #include <geometry/shape_rect.h>
41 #include <geometry/convex_hull.h>
42 
43 
45 {
46  aTarget->SetShape( m_Shape );
47  aTarget->SetWidth( m_Thickness );
48  aTarget->SetStart( m_Start );
49  aTarget->SetEnd( m_End );
50 
51  // in a DRAWSEGMENT the radius of a circle is calculated from the
52  // center and one point on the circle outline (stored in m_End)
53  if( m_Shape == S_CIRCLE )
54  {
55  wxPoint end = m_Start;
56  end.x += m_Radius;
57  aTarget->SetEnd( end );
58  }
59 
60  aTarget->SetAngle( m_ArcAngle );
61  aTarget->SetPolyPoints( m_Poly );
62 }
63 
64 
66 {
67  ExportTo( static_cast<DRAWSEGMENT*>( aTarget ) );
68  // Initialize coordinates specific to the EDGE_MODULE (m_Start0 and m_End0)
69  aTarget->SetLocalCoord();
70 }
71 
72 
73 void PAD_CS_PRIMITIVE::Move( wxPoint aMoveVector )
74 {
75  m_Start += aMoveVector;
76  m_End += aMoveVector;
77 
78  for( auto& corner : m_Poly )
79  {
80  corner += aMoveVector;
81  }
82 }
83 
84 
85 /*
86  * Has meaning only for free shape pads.
87  * add a free shape to the shape list.
88  * the shape is a polygon (can be with thick outline), segment, circle or arc
89  */
90 
91 void D_PAD::AddPrimitive( const SHAPE_POLY_SET& aPoly, int aThickness )
92 {
93  std::vector<wxPoint> points;
94 
95  // If aPoly has holes, convert it to a polygon with no holes.
96  SHAPE_POLY_SET poly_no_hole;
97  poly_no_hole.Append( aPoly );
99 
100  for( auto iter = poly_no_hole.CIterate(); iter; iter++ )
101  points.push_back( wxPoint( iter->x, iter->y ) );
102 
103  AddPrimitive( points, aThickness );
104 }
105 
106 void D_PAD::AddPrimitive( const std::vector<wxPoint>& aPoly, int aThickness )
107 {
108  PAD_CS_PRIMITIVE shape( S_POLYGON );
109  shape.m_Poly = aPoly;
110  shape.m_Thickness = aThickness;
111  m_basicShapes.push_back( shape );
112 
113  MergePrimitivesAsPolygon();
114 }
115 
116 
117 void D_PAD::AddPrimitive( wxPoint aStart, wxPoint aEnd, int aThickness )
118 {
119  PAD_CS_PRIMITIVE shape( S_SEGMENT );
120  shape.m_Start = aStart;
121  shape.m_End = aEnd;
122  shape.m_Thickness = aThickness;
123  m_basicShapes.push_back( shape );
124 
125  MergePrimitivesAsPolygon();
126 }
127 
128 
129 void D_PAD::AddPrimitive( wxPoint aCenter, wxPoint aStart, int aArcAngle, int aThickness )
130 {
131  PAD_CS_PRIMITIVE shape( S_ARC );
132  shape.m_Start = aCenter;
133  shape.m_End = aStart;
134  shape.m_ArcAngle = aArcAngle;
135  shape.m_Thickness = aThickness;
136  m_basicShapes.push_back( shape );
137 
138  MergePrimitivesAsPolygon();
139 }
140 
141 
142 void D_PAD::AddPrimitive( wxPoint aCenter, int aRadius, int aThickness )
143 {
144  PAD_CS_PRIMITIVE shape( S_CIRCLE );
145  shape.m_Start = aCenter;
146  shape.m_Radius = aRadius;
147  shape.m_Thickness = aThickness;
148  m_basicShapes.push_back( shape );
149 
150  MergePrimitivesAsPolygon();
151 }
152 
153 
154 bool D_PAD::SetPrimitives( const std::vector<PAD_CS_PRIMITIVE>& aPrimitivesList )
155 {
156  // clear old list
157  m_basicShapes.clear();
158 
159  // Import to the basic shape list
160  if( aPrimitivesList.size() )
161  m_basicShapes = aPrimitivesList;
162 
163  // Only one polygon is expected (pad area = only one copper area)
164  return MergePrimitivesAsPolygon();
165 }
166 
167 bool D_PAD::AddPrimitives( const std::vector<PAD_CS_PRIMITIVE>& aPrimitivesList )
168 {
169  for( const auto& prim : aPrimitivesList )
170  m_basicShapes.push_back( prim );
171 
172  return MergePrimitivesAsPolygon();
173 }
174 
175 
176 // clear the basic shapes list and associated data
178 {
179  m_basicShapes.clear();
180  m_customShapeAsPolygon.RemoveAllContours();
181 }
182 
183 
185  int aCircleToSegmentsCount )
186 
187 {
188  SHAPE_POLY_SET aux_polyset;
189 
190  for( unsigned cnt = 0; cnt < m_basicShapes.size(); ++cnt )
191  {
192  const PAD_CS_PRIMITIVE& bshape = m_basicShapes[cnt];
193 
194  switch( bshape.m_Shape )
195  {
196  case S_SEGMENT: // usual segment : line with rounded ends
198  bshape.m_Start, bshape.m_End, aCircleToSegmentsCount, bshape.m_Thickness );
199  break;
200 
201  case S_ARC: // Arc with rounded ends
202  TransformArcToPolygon( aux_polyset,
203  bshape.m_Start, bshape.m_End, bshape.m_ArcAngle,
204  aCircleToSegmentsCount, bshape.m_Thickness );
205  break;
206 
207  case S_CIRCLE: // ring or circle
208  if( bshape.m_Thickness ) // ring
209  TransformRingToPolygon( aux_polyset,
210  bshape.m_Start, bshape.m_Radius,
211  aCircleToSegmentsCount, bshape.m_Thickness ) ;
212  else // Filled circle
213  TransformCircleToPolygon( aux_polyset,
214  bshape.m_Start, bshape.m_Radius,
215  aCircleToSegmentsCount ) ;
216  break;
217 
218  case S_POLYGON: // polygon
219  if( bshape.m_Poly.size() < 2 )
220  break; // Malformed polygon.
221 
222  {
223  // Insert the polygon:
224  const std::vector< wxPoint>& poly = bshape.m_Poly;
225  aux_polyset.NewOutline();
226 
227  if( bshape.m_Thickness )
228  {
229  SHAPE_POLY_SET polyset;
230  polyset.NewOutline();
231 
232  for( unsigned ii = 0; ii < poly.size(); ii++ )
233  {
234  polyset.Append( poly[ii].x, poly[ii].y );
235  }
236 
237  polyset.Inflate( bshape.m_Thickness/2, 32 );
238 
239  aux_polyset.Append( polyset );
240  }
241 
242  else
243  for( unsigned ii = 0; ii < poly.size(); ii++ )
244  aux_polyset.Append( poly[ii].x, poly[ii].y );
245  }
246  break;
247 
248  default:
249  break;
250  }
251  }
252 
253  aux_polyset.Simplify( SHAPE_POLY_SET::PM_FAST );
254 
255  // Merge all polygons with the initial pad anchor shape
256  if( aux_polyset.OutlineCount() )
257  {
258  aMergedPolygon->BooleanAdd( aux_polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
259  aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
260  }
261 
262  return aMergedPolygon->OutlineCount() <= 1;
263 }
264 
265 /* Merge all basic shapes, converted to a polygon in one polygon,
266  * return true if OK, false in there is more than one polygon
267  * in aMergedPolygon
268  */
270  int aCircleToSegmentsCount )
271 {
272  // if aMergedPolygon == NULL, use m_customShapeAsPolygon as target
273 
274  if( !aMergedPolygon )
275  aMergedPolygon = &m_customShapeAsPolygon;
276 
277  aMergedPolygon->RemoveAllContours();
278 
279  // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
280  // The anchor pad is always at 0,0
281  switch( GetAnchorPadShape() )
282  {
283  default:
284  case PAD_SHAPE_CIRCLE:
285  TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0,0 ), GetSize().x/2,
286  aCircleToSegmentsCount );
287  break;
288 
289  case PAD_SHAPE_RECT:
290  {
291  SHAPE_RECT rect( -GetSize().x/2, -GetSize().y/2, GetSize().x, GetSize().y );
292  aMergedPolygon->AddOutline( rect.Outline() );
293  }
294  break;
295  }
296 
297  if ( !buildCustomPadPolygon( aMergedPolygon, aCircleToSegmentsCount ) )
298  return false;
299 
300  m_boundingRadius = -1; // The current bouding radius is no more valid.
301 
302  return aMergedPolygon->OutlineCount() <= 1;
303 }
304 
306  wxPoint aPosition, double aRotation ) const
307 {
308  if( aMergedPolygon->OutlineCount() == 0 )
309  return;
310 
311  // Move, rotate, ... coordinates in aMergedPolygon according to the
312  // pad position and orientation
313  for( int cnt = 0; cnt < aMergedPolygon->OutlineCount(); ++cnt )
314  {
315  SHAPE_LINE_CHAIN& poly = aMergedPolygon->Outline( cnt );
316 
317  for( int ii = 0; ii < poly.PointCount(); ++ii )
318  {
319  wxPoint corner( poly.Point( ii ).x, poly.Point( ii ).y );
320  RotatePoint( &corner, aRotation );
321  corner += aPosition;
322 
323  poly.Point( ii ).x = corner.x;
324  poly.Point( ii ).y = corner.y;
325  }
326  }
327 }
328 
330 {
331  SHAPE_POLY_SET poly;
332 
333  if ( !buildCustomPadPolygon( &poly, 16 ) )
334  return false;
335 
336  const int minSteps = 10;
337  const int maxSteps = 50;
338 
339  int stepsX, stepsY;
340 
341  auto bbox = poly.BBox();
342 
343  if( bbox.GetWidth() < bbox.GetHeight() )
344  {
345  stepsX = minSteps;
346  stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
347  }
348  else
349  {
350  stepsY = minSteps;
351  stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
352  }
353 
354  stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
355  stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
356 
357  auto center = bbox.Centre();
358 
359  auto minDist = std::numeric_limits<int64_t>::max();
360  int64_t minDistEdge;
361 
362  if( GetAnchorPadShape() == PAD_SHAPE_CIRCLE )
363  {
364  minDistEdge = GetSize().x;
365  }
366  else
367  {
368  minDistEdge = std::max( GetSize().x, GetSize().y );
369  }
370 
371  OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
372 
373  for ( int y = 0; y < stepsY ; y++ )
374  {
375  for ( int x = 0; x < stepsX; x++ )
376  {
377  VECTOR2I p = bbox.GetPosition();
378  p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
379  p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
380 
381  if ( poly.Contains(p) )
382  {
383 
384  auto dist = (center - p).EuclideanNorm();
385  auto distEdge = poly.COutline(0).Distance( p, true );
386  if ( distEdge >= minDistEdge )
387  {
388  if ( dist < minDist )
389  {
390  bestAnchor = p;
391  minDist = dist;
392  }
393  }
394  }
395  }
396  }
397 
398  if ( bestAnchor )
399  {
400  aPos = *bestAnchor;
401  return true;
402  }
403 
404  return false;
405 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
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...
const SHAPE_LINE_CHAIN Outline() const
Definition: shape_rect.h:145
bool MergePrimitivesAsPolygon(SHAPE_POLY_SET *aMergedPolygon=NULL, int aCircleToSegmentsCount=32)
Merge all basic shapes, converted to a polygon in one polygon, in m_customShapeAsPolygon.
void SetShape(STROKE_T aShape)
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
void TransformCircleToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCenter, int aRadius, int aCircleToSegmentsCount)
Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines...
int PointCount() const
Function PointCount()
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)
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, bool aIgnoreHoles=false) const
Returns true if a given subpolygon contains the point aP.
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 OutlineCount() const
Returns the number of outlines in the set
int m_Radius
thickness of segment or outline For filled S_CIRCLE shape, thickness = 0.
Definition: class_pad.h:98
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 auto NULLOPT
Definition: optional.h:9
void Inflate(int aFactor, int aCircleSegmentsCount)
Performs outline inflation/deflation, using round corners.
std::vector< wxPoint > m_Poly
is also the start point of the arc
Definition: class_pad.h:102
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
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
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
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.
void SetStart(const wxPoint &aStart)
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
int rescale(int aNumerator, int aValue, int aDenominator)
Definition: math_util.cpp:32
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.
coord_type GetHeight() const
Definition: box2.h:196
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...
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
Class SHAPE_LINE_CHAIN.
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.
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)
EDGE_MODULE class definition.
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
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
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)