KiCad PCB EDA Suite
lib_polyline.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) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2004-2021 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 
25 #include <sch_draw_panel.h>
26 #include <plotters/plotter.h>
27 #include <trigo.h>
28 #include <base_units.h>
29 #include <widgets/msgpanel.h>
30 #include <bitmaps.h>
31 #include <eda_draw_frame.h>
32 #include <general.h>
33 #include <lib_polyline.h>
35 #include <transform.h>
36 
37 
39  LIB_ITEM( LIB_POLYLINE_T, aParent )
40 {
42  m_Width = 0;
43  m_isFillable = true;
44 }
45 
46 
48 {
49  return new LIB_POLYLINE( *this );
50 }
51 
52 
53 int LIB_POLYLINE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
54 {
55  wxASSERT( aOther.Type() == LIB_POLYLINE_T );
56 
57  int retv = LIB_ITEM::compare( aOther );
58 
59  if( retv )
60  return retv;
61 
62  const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
63 
64  if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
65  return m_PolyPoints.size() - tmp->m_PolyPoints.size();
66 
67  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
68  {
69  if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
70  return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
71 
72  if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
73  return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
74  }
75 
76  return 0;
77 }
78 
79 
80 void LIB_POLYLINE::Offset( const wxPoint& aOffset )
81 {
82  for( wxPoint& point : m_PolyPoints )
83  point += aOffset;
84 }
85 
86 
87 void LIB_POLYLINE::MoveTo( const wxPoint& aPosition )
88 {
89  Offset( aPosition - m_PolyPoints[ 0 ] );
90 }
91 
92 
93 void LIB_POLYLINE::MirrorHorizontal( const wxPoint& aCenter )
94 {
95  for( wxPoint& point : m_PolyPoints )
96  {
97  point.x -= aCenter.x;
98  point.x *= -1;
99  point.x += aCenter.x;
100  }
101 }
102 
103 void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
104 {
105  for( wxPoint& point : m_PolyPoints )
106  {
107  point.y -= aCenter.y;
108  point.y *= -1;
109  point.y += aCenter.y;
110  }
111 }
112 
113 void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
114 {
115  for( wxPoint& point : m_PolyPoints )
116  RotatePoint( &point, aCenter, aRotateCCW ? -900 : 900 );
117 }
118 
119 
120 void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
121  const TRANSFORM& aTransform ) const
122 {
123  wxASSERT( aPlotter != nullptr );
124 
125  static std::vector< wxPoint > cornerList;
126  cornerList.clear();
127 
128  for( wxPoint pos : m_PolyPoints )
129  {
130  pos = aTransform.TransformCoordinate( pos ) + aOffset;
131  cornerList.push_back( pos );
132  }
133 
135  {
136  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
137  aPlotter->PlotPoly( cornerList, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
138  }
139 
140  bool already_filled = m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
141  int pen_size = GetEffectivePenWidth( aPlotter->RenderSettings() );
142 
143  if( !already_filled || pen_size > 0 )
144  {
145  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
146  aPlotter->PlotPoly( cornerList, already_filled ? FILL_TYPE::NO_FILL : m_fill, pen_size );
147  }
148 }
149 
150 
151 void LIB_POLYLINE::AddPoint( const wxPoint& aPosition )
152 {
153  m_PolyPoints.push_back( aPosition );
154 }
155 
156 
157 void LIB_POLYLINE::AddCorner( const wxPoint& aPosition )
158 {
159  int currentMinDistance = INT_MAX;
160  int closestLineStart = 0;
161 
162  for( unsigned i = 0; i < m_PolyPoints.size() - 1; ++i )
163  {
164  int distance = (int) DistanceLinePoint( m_PolyPoints[i], m_PolyPoints[i + 1], aPosition );
165 
166  if( distance < currentMinDistance )
167  {
168  currentMinDistance = distance;
169  closestLineStart = i;
170  }
171  }
172 
173  m_PolyPoints.insert( m_PolyPoints.begin() + closestLineStart, aPosition );
174 }
175 
176 
178 {
179  m_PolyPoints.erase( m_PolyPoints.begin() + aIdx );
180 }
181 
182 
184 {
185  return m_Width;
186 }
187 
188 
189 void LIB_POLYLINE::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
190  const TRANSFORM& aTransform )
191 {
192  bool forceNoFill = static_cast<bool>( aData );
193  int penWidth = GetEffectivePenWidth( aSettings );
194 
195  if( forceNoFill && m_fill != FILL_TYPE::NO_FILL && penWidth == 0 )
196  return;
197 
198  wxDC* DC = aSettings->GetPrintDC();
199  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
200  wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
201 
202  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
203  buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
204 
205  if( forceNoFill || m_fill == FILL_TYPE::NO_FILL )
206  {
207  GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, false, penWidth, color, color );
208  }
209  else
210  {
213 
214  GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, true, penWidth, color, color );
215  }
216 
217  delete[] buffer;
218 }
219 
220 
221 bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
222 {
223  int delta = std::max( aAccuracy + GetPenWidth() / 2, Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
224  SHAPE_LINE_CHAIN shape;
225 
226  for( wxPoint pt : m_PolyPoints )
228 
229  if( m_fill != FILL_TYPE::NO_FILL && m_PolyPoints.size() > 2 )
230  {
231  shape.SetClosed( true );
232  return shape.PointInside( aPosition, delta );
233  }
234  else
235  return shape.PointOnEdge( aPosition, delta );
236 }
237 
238 
239 bool LIB_POLYLINE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
240 {
241  if( m_flags & (STRUCT_DELETED | SKIP_STRUCT ) )
242  return false;
243 
244  EDA_RECT sel = aRect;
245 
246  if ( aAccuracy )
247  sel.Inflate( aAccuracy );
248 
249  if( aContained )
250  return sel.Contains( GetBoundingBox() );
251 
252  // Fast test: if rect is outside the polygon bounding box, then they cannot intersect
253  if( !sel.Intersects( GetBoundingBox() ) )
254  return false;
255 
256  // Account for the width of the line
257  sel.Inflate( ( GetPenWidth() / 2 ) + 1 );
258 
259  for( size_t ii = 0; ii < m_PolyPoints.size(); ii++ )
260  {
261  wxPoint pt = DefaultTransform.TransformCoordinate( m_PolyPoints[ ii ] );
262 
263  // Test if the point is within aRect
264  if( sel.Contains( pt ) )
265  return true;
266 
267  if( ii + 1 < m_PolyPoints.size() )
268  {
269  wxPoint ptNext = DefaultTransform.TransformCoordinate( m_PolyPoints[ ii + 1 ] );
270 
271  // Test if this edge intersects aRect
272  if( sel.Intersects( pt, ptNext ) )
273  return true;
274  }
275  else if( m_fill != FILL_TYPE::NO_FILL )
276  {
277  wxPoint ptNext = DefaultTransform.TransformCoordinate( m_PolyPoints[ 0 ] );
278 
279  // Test if this edge intersects aRect
280  if( sel.Intersects( pt, ptNext ) )
281  return true;
282  }
283  }
284 
285  return false;
286 }
287 
288 
290 {
291  EDA_RECT rect;
292  int xmin, xmax, ymin, ymax;
293 
294  xmin = xmax = m_PolyPoints[0].x;
295  ymin = ymax = m_PolyPoints[0].y;
296 
297  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
298  {
299  xmin = std::min( xmin, m_PolyPoints[ii].x );
300  xmax = std::max( xmax, m_PolyPoints[ii].x );
301  ymin = std::min( ymin, m_PolyPoints[ii].y );
302  ymax = std::max( ymax, m_PolyPoints[ii].y );
303  }
304 
305  rect.SetOrigin( xmin, ymin );
306  rect.SetEnd( xmax, ymax );
307  rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
308 
309  rect.RevertYAxis();
310 
311  return rect;
312 }
313 
314 
315 void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
316 {
317  // First segment is kept, only its end point is changed
318  while( GetCornerCount() > 2 )
319  {
320  m_PolyPoints.pop_back();
321 
322  if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
323  {
324  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
325  break;
326  }
327  }
328 }
329 
330 
332 {
333  wxString msg;
334  EDA_RECT bBox = GetBoundingBox();
335 
336  LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
337 
338  msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width );
339 
340  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg ) );
341 
342  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
343  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
344 
345  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg ) );
346 }
347 
348 
350 {
351  return wxString::Format( _( "Polyline, %d points" ), int( m_PolyPoints.size() ) );
352 }
353 
354 
356 {
358 }
359 
360 
361 void LIB_POLYLINE::BeginEdit( const wxPoint& aPosition )
362 {
363  m_PolyPoints.push_back( aPosition ); // Start point of first segment.
364  m_PolyPoints.push_back( aPosition ); // End point of first segment.
365 }
366 
367 
368 bool LIB_POLYLINE::ContinueEdit( const wxPoint& aPosition )
369 {
370  // do not add zero length segments
371  if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
372  m_PolyPoints.push_back( aPosition );
373 
374  return true;
375 }
376 
377 
379 {
380  // do not include last point twice
381  if( m_PolyPoints.size() > 2 )
382  {
383  if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
384  m_PolyPoints.pop_back();
385  }
386 }
387 
388 
389 void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
390 {
391  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
392 }
void AddCorner(const wxPoint &aPosition)
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_TYPE aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=nullptr)=0
Draw a polygon ( filled or not ).
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
void BeginEdit(const wxPoint &aStartPoint) override
Begin drawing a symbol library draw item at aPosition.
Implementation of conversion functions that require both schematic and board internal units.
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Return the text to display to be used in the selection clarification context menu when multiple items...
int color
Definition: DXF_plotter.cpp:57
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:121
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
virtual int GetEffectivePenWidth(const RENDER_SETTINGS *aSettings) const
Definition: lib_item.h:146
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
EDA_ITEM_FLAGS m_flags
Definition: eda_item.h:481
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
The base class for create windows for drawing purpose.
LIB_POLYLINE(LIB_SYMBOL *aParent)
Define a library symbol object.
Definition: lib_symbol.h:96
void RevertYAxis()
Mirror the rectangle from the X axis (negate Y pos and size).
Definition: eda_rect.h:198
FILL_TYPE m_fill
The body fill type.
Definition: lib_item.h:339
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
unsigned GetCornerCount() const
Definition: lib_polyline.h:67
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:42
The base class for drawable items used by schematic library symbols.
Definition: lib_item.h:61
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) const override
Plot the draw item using the plot object.
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Test if aPosition is contained within or on the bounding box of an item.
const wxPoint GetEnd() const
Definition: eda_rect.h:103
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const override
Provide the draw object specific comparison called by the == and < operators.
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
int GetPenWidth() const override
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:81
const wxPoint GetOrigin() const
Definition: eda_rect.h:101
void SetEnd(int x, int y)
Definition: eda_rect.h:182
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a polygon (any type) defined by the line chain.
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:47
bool m_isFillable
Definition: lib_item.h:340
void print(const RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
#define STRUCT_DELETED
flag indication structures to be erased
#define _(s)
std::vector< wxPoint > m_PolyPoints
Definition: lib_polyline.h:121
wxDC * GetPrintDC() const
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
void AddPoint(const wxPoint &aPoint)
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_item.cpp:50
virtual void SetColor(const COLOR4D &color)=0
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void DeleteSegment(const wxPoint aPosition)
Delete the segment at aPosition.
EDA_UNITS
Definition: eda_units.h:38
Base plotter engine class.
Definition: plotter.h:121
void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:156
TRANSFORM DefaultTransform
Definition: eeschema.cpp:56
#define SKIP_STRUCT
flag indicating that the structure should be ignored
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
Represent a polyline (an zero-thickness chain of connected line segments).
Handle the component boundary box.
Definition: eda_rect.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
bool Intersects(const EDA_RECT &aRect) const
Test for a common area between rectangles.
Definition: eda_rect.cpp:150
void EndEdit() override
End an object editing action.
const EDA_RECT GetBoundingBox() const override
constexpr int delta
void GRPoly(EDA_RECT *ClipBox, wxDC *DC, int n, const wxPoint *Points, bool Fill, int width, const COLOR4D &Color, const COLOR4D &BgColor)
Draw a new polyline and fill it if Fill, in drawing space.
Definition: gr_basic.cpp:497
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:53
void RemoveCorner(int aIdx)
Message panel definition file.
void CalcEdit(const wxPoint &aPosition) override
Calculate the attributes of an item at aPosition when it is being edited.
virtual int compare(const LIB_ITEM &aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags=LIB_ITEM::COMPARE_FLAGS::NORMAL) const
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_item.cpp:74
bool ContinueEdit(const wxPoint &aNextPoint) override
Continue an edit in progress at aPosition.
double DistanceLinePoint(const wxPoint &linePointA, const wxPoint &linePointB, const wxPoint &referencePoint)
Compute the distance between a line and a reference point Reference: http://mathworld....
Definition: trigo.h:163
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103