KiCad PCB EDA Suite
convert_basic_shapes_to_polygon.h File Reference
#include <vector>
#include <fctsys.h>
#include <trigo.h>
#include <macros.h>
#include <geometry/shape_poly_set.h>

Go to the source code of this file.

Functions

void TransformCircleToPolygon (SHAPE_POLY_SET &aCornerBuffer, wxPoint aCenter, int aRadius, int aCircleToSegmentsCount)
 Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines. More...
 
void GetRoundRectCornerCenters (wxPoint aCenters[4], int aRadius, const wxPoint &aPosition, const wxSize &aSize, double aRotation)
 Helper function GetRoundRectCornerCenters Has meaning only for rounded rect Returns the centers of the rounded corners. More...
 
void TransformRoundRectToPolygon (SHAPE_POLY_SET &aCornerBuffer, const wxPoint &aPosition, const wxSize &aSize, double aRotation, int aCornerRadius, int aCircleToSegmentsCount)
 Function TransformRoundRectToPolygon convert a rectangle with rounded corners to a polygon Convert arcs to multiple straight lines. More...
 
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 Convert arcs to multiple straight lines. More...
 
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 segments. More...
 
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 segments. More...
 

Function Documentation

void GetRoundRectCornerCenters ( wxPoint  aCenters[4],
int  aRadius,
const wxPoint aPosition,
const wxSize &  aSize,
double  aRotation 
)

Helper function GetRoundRectCornerCenters Has meaning only for rounded rect Returns the centers of the rounded corners.

Parameters
aCentersis the buffer to store the 4 coordinates.
aRadius= the radius of the of the rounded corners.
aPosition= position of the round rect
aSize= size of the of the round rect.
aRotation= rotation of the of the round rect

Definition at line 69 of file convert_basic_shapes_to_polygon.cpp.

References RotatePoint(), wxPoint::x, and wxPoint::y.

Referenced by DRC::checkClearancePadToPad(), CINFO3D_VISU::createNewPadWithClearance(), D_PAD::DrawShape(), and TransformRoundRectToPolygon().

71 {
72  wxSize size( aSize/2 );
73 
74  size.x -= aRadius;
75  size.y -= aRadius;
76 
77  // Ensure size is > 0, to avoid generating unusable shapes
78  // which can crash kicad.
79  if( size.x <= 1 )
80  size.x = 1;
81  if( size.y <= 1 )
82  size.y = 1;
83 
84  aCenters[0].x = -size.x;
85  aCenters[0].y = size.y;
86 
87  aCenters[1].x = size.x;
88  aCenters[1].y = size.y;
89 
90  aCenters[2].x = size.x;
91  aCenters[2].y = -size.y;
92 
93  aCenters[3].x = -size.x;
94  aCenters[3].y = -size.y;
95 
96  // Rotate the polygon
97  if( aRotation )
98  {
99  for( int ii = 0; ii < 4; ii++ )
100  RotatePoint( &aCenters[ii], aRotation );
101  }
102 
103  // move the polygon to the position
104  for( int ii = 0; ii < 4; ii++ )
105  aCenters[ii] += aPosition;
106 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
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 segments.

Parameters
aCornerBuffer= a buffer to store the polygon
aCentre= centre of the arc or circle
aStart= start point of the arc, or a point on the circle
aArcAngle= arc angle in 0.1 degrees. For a circle, aArcAngle = 3600
aCircleToSegmentsCount= the number of segments to approximate a circle
aWidth= width (thickness) of the line

Definition at line 232 of file convert_basic_shapes_to_polygon.cpp.

References delta, RotatePoint(), and TransformRoundedEndsSegmentToPolygon().

Referenced by D_PAD::MergePrimitivesAsPolygon(), and DRAWSEGMENT::TransformShapeWithClearanceToPolygon().

235 {
236  wxPoint arc_start, arc_end;
237  int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
238 
239  arc_end = arc_start = aStart;
240 
241  if( aArcAngle != 3600 )
242  {
243  RotatePoint( &arc_end, aCentre, -aArcAngle );
244  }
245 
246  if( aArcAngle < 0 )
247  {
248  std::swap( arc_start, arc_end );
249  aArcAngle = -aArcAngle;
250  }
251 
252  // Compute the ends of segments and creates poly
253  wxPoint curr_end = arc_start;
254  wxPoint curr_start = arc_start;
255 
256  for( int ii = delta; ii < aArcAngle; ii += delta )
257  {
258  curr_end = arc_start;
259  RotatePoint( &curr_end, aCentre, -ii );
260  TransformRoundedEndsSegmentToPolygon( aCornerBuffer, curr_start, curr_end,
261  aCircleToSegmentsCount, aWidth );
262  curr_start = curr_end;
263  }
264 
265  if( curr_end != arc_end )
267  curr_end, arc_end,
268  aCircleToSegmentsCount, aWidth );
269 }
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...
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static const int delta[8][2]
Definition: solve.cpp:112
void TransformCircleToPolygon ( SHAPE_POLY_SET aCornerBuffer,
wxPoint  aCenter,
int  aRadius,
int  aCircleToSegmentsCount 
)

Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines.

Parameters
aCornerBuffer= a buffer to store the polygon
aCenter= the center of the circle
aRadius= the radius of the circle
aCircleToSegmentsCount= the number of segments to approximate a circle Note: the polygon is inside the circle, so if you want to have the polygon outside the circle, you should give aRadius calculated with a correction factor

Definition at line 46 of file convert_basic_shapes_to_polygon.cpp.

References PNS::angle(), SHAPE_POLY_SET::Append(), delta, SHAPE_POLY_SET::NewOutline(), RotatePoint(), wxPoint::x, and wxPoint::y.

Referenced by addHoleToPolygon(), D_PAD::BuildPadDrillShapePolygon(), ConvertOutlineToPolygon(), D_CODE::ConvertShapeToPolygon(), CINFO3D_VISU::createLayers(), AM_PRIMITIVE::DrawBasicShape(), D_PAD::MergePrimitivesAsPolygon(), TransformRingToPolygon(), TRACK::TransformShapeWithClearanceToPolygon(), and D_PAD::TransformShapeWithClearanceToPolygon().

49 {
50  wxPoint corner_position;
51  int delta = 3600 / aCircleToSegmentsCount; // rot angle in 0.1 degree
52  int halfstep = 1800 / aCircleToSegmentsCount; // the starting value for rot angles
53 
54  aCornerBuffer.NewOutline();
55 
56  for( int ii = 0; ii < aCircleToSegmentsCount; ii++ )
57  {
58  corner_position.x = aRadius;
59  corner_position.y = 0;
60  int angle = (ii * delta) + halfstep;
61  RotatePoint( &corner_position.x, &corner_position.y, angle );
62  corner_position += aCenter;
63  aCornerBuffer.Append( corner_position.x, corner_position.y );
64  }
65 }
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static const int delta[8][2]
Definition: solve.cpp:112
int NewOutline()
Creates a new empty polygon in the set and returns its index
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
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 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 segments.

Parameters
aCornerBuffer= a buffer to store the polygon
aCentre= centre of the arc or circle
aRadius= radius of the circle
aCircleToSegmentsCount= the number of segments to approximate a circle
aWidth= width (thickness) of the ring

Definition at line 282 of file convert_basic_shapes_to_polygon.cpp.

References SHAPE_POLY_SET::Append(), delta, SHAPE_POLY_SET::NewOutline(), RotatePoint(), TransformCircleToPolygon(), wxPoint::x, and wxPoint::y.

Referenced by CINFO3D_VISU::buildPadShapeThickOutlineAsPolygon(), AM_PRIMITIVE::DrawBasicShape(), D_PAD::MergePrimitivesAsPolygon(), and DRAWSEGMENT::TransformShapeWithClearanceToPolygon().

285 {
286  // Compute the corners positions and creates the poly
287  wxPoint curr_point;
288  int inner_radius = aRadius - ( aWidth / 2 );
289  int outer_radius = inner_radius + aWidth;
290 
291  if( inner_radius <= 0 )
292  { //In this case, the ring is just a circle (no hole inside)
293  TransformCircleToPolygon( aCornerBuffer, aCentre, aRadius + ( aWidth / 2 ),
294  aCircleToSegmentsCount );
295  return;
296  }
297 
298  aCornerBuffer.NewOutline();
299 
300  // Draw the inner circle of the ring
301  int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
302 
303  for( int ii = 0; ii < 3600; ii += delta )
304  {
305  curr_point.x = inner_radius;
306  curr_point.y = 0;
307  RotatePoint( &curr_point, ii );
308  curr_point += aCentre;
309  aCornerBuffer.Append( curr_point.x, curr_point.y );
310  }
311 
312  // Draw the last point of inner circle
313  aCornerBuffer.Append( aCentre.x + inner_radius, aCentre.y );
314 
315  // Draw the outer circle of the ring
316  for( int ii = 0; ii < 3600; ii += delta )
317  {
318  curr_point.x = outer_radius;
319  curr_point.y = 0;
320  RotatePoint( &curr_point, -ii );
321  curr_point += aCentre;
322  aCornerBuffer.Append( curr_point.x, curr_point.y );
323  }
324 
325  // Draw the last point of outer circle
326  aCornerBuffer.Append( aCentre.x + outer_radius, aCentre.y );
327  aCornerBuffer.Append( aCentre.x + inner_radius, aCentre.y );
328 }
void TransformCircleToPolygon(SHAPE_POLY_SET &aCornerBuffer, wxPoint aCenter, int aRadius, int aCircleToSegmentsCount)
Function TransformCircleToPolygon convert a circle to a polygon, using multiple straight lines...
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static const int delta[8][2]
Definition: solve.cpp:112
int NewOutline()
Creates a new empty polygon in the set and returns its index
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 TransformRoundedEndsSegmentToPolygon ( SHAPE_POLY_SET aCornerBuffer,
wxPoint  aStart,
wxPoint  aEnd,
int  aCircleToSegmentsCount,
int  aWidth 
)

Function TransformRoundedEndsSegmentToPolygon convert a segment with rounded ends to a polygon Convert arcs to multiple straight lines.

Parameters
aCornerBuffer= a buffer to store the polygon
aStart= the segment start point coordinate
aEnd= the segment end point coordinate
aCircleToSegmentsCount= the number of segments to approximate a circle
aWidth= the segment width Note: the polygon is inside the arc ends, so if you want to have the polygon outside the circle, you should give aStart and aEnd calculated with a correction factor

Definition at line 152 of file convert_basic_shapes_to_polygon.cpp.

References SHAPE_POLY_SET::Append(), ArcTangente(), delta, EuclideanNorm(), KiROUND(), SHAPE_POLY_SET::NewOutline(), RotatePoint(), wxPoint::x, VECTOR2< T >::x, wxPoint::y, and VECTOR2< T >::y.

Referenced by addTextSegmToPoly(), D_PAD::BuildPadDrillShapePolygon(), CINFO3D_VISU::buildPadShapeThickOutlineAsPolygon(), D_PAD::MergePrimitivesAsPolygon(), DXF_PLOTTER::PlotPoly(), TransformArcToPolygon(), TRACK::TransformShapeWithClearanceToPolygon(), DRAWSEGMENT::TransformShapeWithClearanceToPolygon(), D_PAD::TransformShapeWithClearanceToPolygon(), and ZONE_CONTAINER::TransformSolidAreasShapesToPolygonSet().

156 {
157  int radius = aWidth / 2;
158  wxPoint endp = aEnd - aStart; // end point coordinate for the same segment starting at (0,0)
159  wxPoint startp = aStart;
160  wxPoint corner;
161  VECTOR2I polypoint;
162 
163  aCornerBuffer.NewOutline();
164 
165  // normalize the position in order to have endp.x >= 0;
166  if( endp.x < 0 )
167  {
168  endp = aStart - aEnd;
169  startp = aEnd;
170  }
171 
172  double delta_angle = ArcTangente( endp.y, endp.x ); // delta_angle is in 0.1 degrees
173  int seg_len = KiROUND( EuclideanNorm( endp ) );
174 
175  int delta = 3600 / aCircleToSegmentsCount; // rot angle in 0.1 degree
176 
177  // Compute the outlines of the segment, and creates a polygon
178  // add right rounded end:
179  for( int ii = 0; ii < 1800; ii += delta )
180  {
181  corner = wxPoint( 0, radius );
182  RotatePoint( &corner, ii );
183  corner.x += seg_len;
184  RotatePoint( &corner, -delta_angle );
185  corner += startp;
186  polypoint.x = corner.x;
187  polypoint.y = corner.y;
188  aCornerBuffer.Append( polypoint.x, polypoint.y );
189  }
190 
191  // Finish arc:
192  corner = wxPoint( seg_len, -radius );
193  RotatePoint( &corner, -delta_angle );
194  corner += startp;
195  polypoint.x = corner.x;
196  polypoint.y = corner.y;
197  aCornerBuffer.Append( polypoint.x, polypoint.y );
198 
199  // add left rounded end:
200  for( int ii = 0; ii < 1800; ii += delta )
201  {
202  corner = wxPoint( 0, -radius );
203  RotatePoint( &corner, ii );
204  RotatePoint( &corner, -delta_angle );
205  corner += startp;
206  polypoint.x = corner.x;
207  polypoint.y = corner.y;
208  aCornerBuffer.Append( polypoint.x, polypoint.y );
209  }
210 
211  // Finish arc:
212  corner = wxPoint( 0, radius );
213  RotatePoint( &corner, -delta_angle );
214  corner += startp;
215  polypoint.x = corner.x;
216  polypoint.y = corner.y;
217  aCornerBuffer.Append( polypoint.x, polypoint.y );
218 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:104
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
static const int delta[8][2]
Definition: solve.cpp:112
double ArcTangente(int dy, int dx)
Definition: trigo.cpp:271
int NewOutline()
Creates a new empty polygon in the set and returns its index
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 TransformRoundRectToPolygon ( SHAPE_POLY_SET aCornerBuffer,
const wxPoint aPosition,
const wxSize &  aSize,
double  aRotation,
int  aCornerRadius,
int  aCircleToSegmentsCount 
)

Function TransformRoundRectToPolygon convert a rectangle with rounded corners to a polygon Convert arcs to multiple straight lines.

Parameters
aCornerBuffer= a buffer to store the polygon
aPosition= the coordinate of the center of the rectangle
aSize= the size of the rectangle
aCornerRadius= radius of rounded corners
aRotation= rotation in 0.1 degrees of the rectangle
aCircleToSegmentsCount= the number of segments to approximate a circle

Definition at line 119 of file convert_basic_shapes_to_polygon.cpp.

References SHAPE_POLY_SET::Append(), GetRoundRectCornerCenters(), SHAPE_POLY_SET::Inflate(), SHAPE_POLY_SET::NewOutline(), wxPoint::x, and wxPoint::y.

Referenced by KIGFX::PCB_PAINTER::draw(), D_PAD::DrawShape(), HPGL_PLOTTER::FlashPadRoundRect(), PSLIKE_PLOTTER::FlashPadRoundRect(), GERBER_PLOTTER::FlashPadRoundRect(), DXF_PLOTTER::FlashPadRoundRect(), D_PAD::HitTest(), and D_PAD::TransformShapeWithClearanceToPolygon().

123 {
124  wxPoint corners[4];
125  GetRoundRectCornerCenters( corners, aCornerRadius, aPosition, aSize, aRotation );
126 
127  SHAPE_POLY_SET outline;
128  outline.NewOutline();
129 
130  for( int ii = 0; ii < 4; ++ii )
131  outline.Append( corners[ii].x, corners[ii].y );
132 
133  outline.Inflate( aCornerRadius, aCircleToSegmentsCount );
134 
135  // Add the outline:
136  aCornerBuffer.Append( outline );
137 }
void Inflate(int aFactor, int aCircleSegmentsCount)
Performs outline inflation/deflation, using round corners.
Class SHAPE_POLY_SET.
int NewOutline()
Creates a new empty polygon in the set and returns its index
void GetRoundRectCornerCenters(wxPoint aCenters[4], int aRadius, const wxPoint &aPosition, const wxSize &aSize, double aRotation)
Helper function GetRoundRectCornerCenters Has meaning only for rounded rect Returns the centers of th...
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) ...