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