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-2019 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 #include <pcbnew.h>
33 #include <class_board.h>
34 #include <class_board_item.h>
35 #include <class_drawsegment.h>
36 #include <class_pad.h>
38 #include <geometry/shape_rect.h>
39 
40 
41 /*
42  * Has meaning only for free shape pads.
43  * add a free shape to the shape list.
44  * the shape is a polygon (can be with thick outline), segment, circle or arc
45  */
46 
47 void D_PAD::AddPrimitivePoly( const SHAPE_POLY_SET& aPoly, int aThickness )
48 {
49  std::vector<wxPoint> points;
50 
51  // If aPoly has holes, convert it to a polygon with no holes.
52  SHAPE_POLY_SET poly_no_hole;
53  poly_no_hole.Append( aPoly );
55 
56  for( auto iter = poly_no_hole.CIterate(); iter; iter++ )
57  points.emplace_back( iter->x, iter->y );
58 
59  AddPrimitivePoly( points, aThickness );
60 }
61 
62 
63 void D_PAD::AddPrimitivePoly( const std::vector<wxPoint>& aPoly, int aThickness )
64 {
65  DRAWSEGMENT* item = new DRAWSEGMENT();
66  item->SetShape( S_POLYGON );
67  item->SetPolyPoints( aPoly );
68  item->SetWidth( aThickness );
69  m_editPrimitives.emplace_back( item );
70  m_shapesDirty = true;
71 }
72 
73 
74 void D_PAD::AddPrimitiveSegment( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
75 {
76  DRAWSEGMENT* item = new DRAWSEGMENT();
77  item->SetStart( aStart );
78  item->SetEnd( aEnd );
79  item->SetWidth( aThickness );
80  m_editPrimitives.emplace_back( item );
81  m_shapesDirty = true;
82 }
83 
84 
85 void D_PAD::AddPrimitiveArc( const wxPoint& aCenter, const wxPoint& aStart, int aArcAngle,
86  int aThickness )
87 {
88  DRAWSEGMENT* item = new DRAWSEGMENT();
89  item->SetShape( S_ARC );
90  item->SetCenter( aCenter );
91  item->SetArcStart( aStart );
92  item->SetAngle( aArcAngle );
93  item->SetWidth( aThickness );
94  m_editPrimitives.emplace_back( item );
95  m_shapesDirty = true;
96 }
97 
98 
99 void D_PAD::AddPrimitiveCurve( const wxPoint& aStart, const wxPoint& aEnd, const wxPoint& aCtrl1,
100  const wxPoint& aCtrl2, int aThickness )
101 {
102  DRAWSEGMENT* item = new DRAWSEGMENT();
103  item->SetShape( S_CURVE );
104  item->SetStart( aStart );
105  item->SetEnd( aEnd );
106  item->SetBezControl1( aCtrl1 );
107  item->SetBezControl2( aCtrl2 );
108  item->SetWidth( aThickness );
109  m_editPrimitives.emplace_back( item );
110  m_shapesDirty = true;
111 }
112 
113 
114 void D_PAD::AddPrimitiveCircle( const wxPoint& aCenter, int aRadius, int aThickness )
115 {
116  DRAWSEGMENT* item = new DRAWSEGMENT();
117  item->SetShape( S_CIRCLE );
118  item->SetStart( aCenter );
119  item->SetEnd( wxPoint( aCenter.x + aRadius, aCenter.y ) );
120  item->SetWidth( aThickness );
121  m_editPrimitives.emplace_back( item );
122  m_shapesDirty = true;
123 }
124 
125 
126 void D_PAD::AddPrimitiveRect( const wxPoint& aStart, const wxPoint& aEnd, int aThickness )
127 {
128  DRAWSEGMENT* item = new DRAWSEGMENT();
129  item->SetShape( S_RECT );
130  item->SetStart( aStart );
131  item->SetEnd( aEnd );
132  item->SetWidth( aThickness );
133  m_editPrimitives.emplace_back( item );
134  m_shapesDirty = true;
135 }
136 
137 
138 void D_PAD::SetPrimitives( const std::vector<std::shared_ptr<DRAWSEGMENT>>& aPrimitivesList )
139 {
140  // clear old list
141  m_editPrimitives.clear();
142 
143  // Import to the basic shape list
144  if( aPrimitivesList.size() )
145  m_editPrimitives = aPrimitivesList;
146 
147  m_shapesDirty = true;
148 }
149 
150 
151 void D_PAD::AddPrimitives( const std::vector<std::shared_ptr<DRAWSEGMENT>>& aPrimitivesList )
152 {
153  for( const std::shared_ptr<DRAWSEGMENT>& prim : aPrimitivesList )
154  m_editPrimitives.push_back( prim );
155 
156  m_shapesDirty = true;
157 }
158 
159 
160 void D_PAD::AddPrimitive( DRAWSEGMENT* aPrimitive )
161 {
162  m_editPrimitives.emplace_back( aPrimitive );
163 
164  m_shapesDirty = true;
165 }
166 
167 
168 // clear the basic shapes list and associated data
170 {
171  m_editPrimitives.clear();
172  m_shapesDirty = true;
173 }
174 
175 
176 void D_PAD::addPadPrimitivesToPolygon( SHAPE_POLY_SET* aMergedPolygon, int aError ) const
177 {
178  SHAPE_POLY_SET polyset;
179 
180  for( const std::shared_ptr<DRAWSEGMENT>& primitive : m_editPrimitives )
181  primitive->TransformShapeWithClearanceToPolygon( polyset, 0, aError );
182 
184 
185  // Merge all polygons with the initial pad anchor shape
186  if( polyset.OutlineCount() )
187  {
188  aMergedPolygon->BooleanAdd( polyset, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
189  aMergedPolygon->Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
190  }
191 }
192 
193 void D_PAD::MergePrimitivesAsPolygon( SHAPE_POLY_SET* aMergedPolygon ) const
194 {
195  auto board = GetBoard();
196  int maxError = ARC_HIGH_DEF;
197 
198  if( board )
199  maxError = board->GetDesignSettings().m_MaxError;
200 
201  aMergedPolygon->RemoveAllContours();
202 
203  // Add the anchor pad shape in aMergedPolygon, others in aux_polyset:
204  // The anchor pad is always at 0,0
205  switch( GetAnchorPadShape() )
206  {
207  case PAD_SHAPE_RECT:
208  {
209  SHAPE_RECT rect( -GetSize().x / 2, -GetSize().y / 2, GetSize().x, GetSize().y );
210  aMergedPolygon->AddOutline( rect.Outline() );
211  }
212  break;
213 
214  default:
215  case PAD_SHAPE_CIRCLE:
216  TransformCircleToPolygon( *aMergedPolygon, wxPoint( 0, 0 ), GetSize().x / 2, maxError );
217  break;
218  }
219 
220  addPadPrimitivesToPolygon( aMergedPolygon, maxError );
221 }
222 
223 
225 {
226  SHAPE_POLY_SET poly;
227  addPadPrimitivesToPolygon( &poly, ARC_LOW_DEF );
228 
229  if( poly.OutlineCount() > 1 )
230  return false;
231 
232  const int minSteps = 10;
233  const int maxSteps = 50;
234 
235  int stepsX, stepsY;
236 
237  auto bbox = poly.BBox();
238 
239  if( bbox.GetWidth() < bbox.GetHeight() )
240  {
241  stepsX = minSteps;
242  stepsY = minSteps * (double) bbox.GetHeight() / (double )(bbox.GetWidth() + 1);
243  }
244  else
245  {
246  stepsY = minSteps;
247  stepsX = minSteps * (double) bbox.GetWidth() / (double )(bbox.GetHeight() + 1);
248  }
249 
250  stepsX = std::max(minSteps, std::min( maxSteps, stepsX ) );
251  stepsY = std::max(minSteps, std::min( maxSteps, stepsY ) );
252 
253  VECTOR2I center = bbox.Centre();
254 
255  int64_t minDist = std::numeric_limits<int64_t>::max();
256  int64_t minDistEdge;
257 
259  {
260  minDistEdge = GetSize().x;
261  }
262  else
263  {
264  minDistEdge = std::max( GetSize().x, GetSize().y );
265  }
266 
267  OPT<VECTOR2I> bestAnchor( []()->OPT<VECTOR2I> { return NULLOPT; }() );
268 
269  for( int y = 0; y < stepsY ; y++ )
270  {
271  for( int x = 0; x < stepsX; x++ )
272  {
273  VECTOR2I p = bbox.GetPosition();
274  p.x += rescale( x, bbox.GetWidth(), (stepsX - 1) );
275  p.y += rescale( y, bbox.GetHeight(), (stepsY - 1) );
276 
277  if( poly.Contains(p) )
278  {
279 
280  int dist = (center - p).EuclideanNorm();
281  int distEdge = poly.COutline(0).Distance( p, true );
282 
283  if( distEdge >= minDistEdge )
284  {
285  if( dist < minDist )
286  {
287  bestAnchor = p;
288  minDist = dist;
289  }
290  }
291  }
292  }
293  }
294 
295  if( bestAnchor )
296  {
297  aPos = *bestAnchor;
298  return true;
299  }
300 
301  return false;
302 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:128
std::vector< std::shared_ptr< DRAWSEGMENT > > m_editPrimitives
Definition: class_pad.h:622
void MergePrimitivesAsPolygon(SHAPE_POLY_SET *aMergedPolygon) const
Merge all basic shapes to a SHAPE_POLY_SET Note: The corners coordinates are relative to the pad posi...
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:174
void addPadPrimitivesToPolygon(SHAPE_POLY_SET *aMergedPolygon, int aError) const
void SetArcStart(const wxPoint &aArcStartPoint)
Initialize the start arc point.
void BooleanAdd(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset union For aFastMode meaning, see function booleanOp
PAD_SHAPE_T GetAnchorPadShape() const
Function GetAnchorPadShape.
Definition: class_pad.h:168
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
bool GetBestAnchorPosition(VECTOR2I &aPos)
bool m_shapesDirty
Definition: class_pad.h:624
polygon (not yet used for tracks, but could be in microwave apps)
int Distance(const VECTOR2I &aP, bool aOutlineOnly=false) const
Function Distance()
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, int aAccuracy=0, bool aUseBBoxCaches=false) const
Returns true if a given subpolygon contains the point aP.
void SetCenter(const wxPoint &aCenterPoint)
For arcs and circles:
void SetBezControl2(const wxPoint &aPoint)
void AddPrimitive(DRAWSEGMENT *aPrimitive)
void AddPrimitives(const std::vector< std::shared_ptr< DRAWSEGMENT >> &aPrimitivesList)
Add to the basic shape list.
double dist(const double ax, const double ay, const double bx, const double by)
Definition: delauney.h:168
segment with non rounded ends
void AddPrimitiveRect(const wxPoint &aStart, const wxPoint &aEnd, int aThickness)
const auto NULLOPT
Definition: optional.h:9
SHAPE_POLY_SET.
void AddPrimitiveCurve(const wxPoint &aStart, const wxPoint &aEnd, const wxPoint &aCtrl1, const wxPoint &aCtrl2, int aThickness)
Arcs (with rounded ends)
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.
void Simplify(POLYGON_MODE aFastMode)
Simplifies the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFast...
Bezier Curve.
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
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 AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index
void AddPrimitiveArc(const wxPoint &aCenter, const wxPoint &aStart, int aArcAngle, int aThickness)
Class to handle a graphic segment.
void AddPrimitivePoly(const SHAPE_POLY_SET &aPoly, int aThickness)
Has meaning only for custom shape pads.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
void TransformCircleToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCenter, int aRadius, int aError)
Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines.
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
coord_type GetHeight() const
Definition: box2.h:197
void AddPrimitiveCircle(const wxPoint &aCenter, int aRadius, int aThickness)
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.
void SetPrimitives(const std::vector< std::shared_ptr< DRAWSEGMENT >> &aPrimitivesList)
Import to the basic shape list.
const wxSize & GetSize() const
Definition: class_pad.h:221
const BOX2I BBox(int aClearance=0) const override
Function BBox()
void DeletePrimitivesList()
clear the basic shapes list
void AddPrimitiveSegment(const wxPoint &aStart, const wxPoint &aEnd, int aThickness)
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)