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-2020 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 <fctsys.h>
26 #include <gr_basic.h>
27 #include <macros.h>
28 #include <sch_draw_panel.h>
29 #include <plotter.h>
30 #include <trigo.h>
31 #include <base_units.h>
32 #include <msgpanel.h>
33 #include <bitmaps.h>
34 
35 #include <general.h>
36 #include <lib_polyline.h>
38 #include <transform.h>
39 
40 
42  LIB_ITEM( LIB_POLYLINE_T, aParent )
43 {
44  m_Fill = NO_FILL;
45  m_Width = 0;
46  m_isFillable = true;
47 }
48 
49 
51 {
52  return new LIB_POLYLINE( *this );
53 }
54 
55 
56 int LIB_POLYLINE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
57 {
58  wxASSERT( aOther.Type() == LIB_POLYLINE_T );
59 
60  int retv = LIB_ITEM::compare( aOther );
61 
62  if( retv )
63  return retv;
64 
65  const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
66 
67  if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
68  return m_PolyPoints.size() - tmp->m_PolyPoints.size();
69 
70  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
71  {
72  if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
73  return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
74 
75  if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
76  return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
77  }
78 
79  return 0;
80 }
81 
82 
83 void LIB_POLYLINE::Offset( const wxPoint& aOffset )
84 {
85  for( wxPoint& point : m_PolyPoints )
86  point += aOffset;
87 }
88 
89 
90 bool LIB_POLYLINE::Inside( EDA_RECT& aRect ) const
91 {
92  for( const wxPoint& point : m_PolyPoints )
93  {
94  if( aRect.Contains( point.x, -point.y ) )
95  return true;
96  }
97 
98  return false;
99 }
100 
101 
102 void LIB_POLYLINE::MoveTo( const wxPoint& aPosition )
103 {
104  Offset( aPosition - m_PolyPoints[ 0 ] );
105 }
106 
107 
109 {
110  for( wxPoint& point : m_PolyPoints )
111  {
112  point.x -= aCenter.x;
113  point.x *= -1;
114  point.x += aCenter.x;
115  }
116 }
117 
118 void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
119 {
120  for( wxPoint& point : m_PolyPoints )
121  {
122  point.y -= aCenter.y;
123  point.y *= -1;
124  point.y += aCenter.y;
125  }
126 }
127 
128 void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
129 {
130  for( wxPoint& point : m_PolyPoints )
131  RotatePoint( &point, aCenter, aRotateCCW ? -900 : 900 );
132 }
133 
134 
135 void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
136  const TRANSFORM& aTransform )
137 {
138  wxASSERT( aPlotter != NULL );
139 
140  static std::vector< wxPoint > cornerList;
141  cornerList.clear();
142 
143  for( wxPoint pos : m_PolyPoints )
144  {
145  pos = aTransform.TransformCoordinate( pos ) + aOffset;
146  cornerList.push_back( pos );
147  }
148 
149  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
150  {
151  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE_BACKGROUND ) );
152  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
153  }
154 
155  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
156  auto pen_size = GetPenSize();
157 
158  if( !already_filled || pen_size > 0 )
159  {
160  pen_size = std::max( 0, pen_size );
161  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE ) );
162  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
163  }
164 }
165 
166 
167 void LIB_POLYLINE::AddPoint( const wxPoint& aPosition )
168 {
169  m_PolyPoints.push_back( aPosition );
170 }
171 
172 
173 void LIB_POLYLINE::AddCorner( const wxPoint& aPosition )
174 {
175  int currentMinDistance = INT_MAX;
176  int closestLineStart = 0;
177 
178  for( unsigned i = 0; i < m_PolyPoints.size() - 1; ++i )
179  {
180  int distance = (int) DistanceLinePoint( m_PolyPoints[i], m_PolyPoints[i + 1], aPosition );
181 
182  if( distance < currentMinDistance )
183  {
184  currentMinDistance = distance;
185  closestLineStart = i;
186  }
187  }
188 
189  m_PolyPoints.insert( m_PolyPoints.begin() + closestLineStart, aPosition );
190 }
191 
192 
194 {
195  m_PolyPoints.erase( m_PolyPoints.begin() + aIdx );
196 }
197 
198 
200 {
201  if( m_Width > 0 )
202  return m_Width;
203 
204  if( m_Width == 0 )
205  return GetDefaultLineThickness();
206 
207  return -1; // the minimal pen value
208 }
209 
210 
211 void LIB_POLYLINE::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
212  const TRANSFORM& aTransform )
213 {
216 
217  wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
218 
219  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
220  buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
221 
222  FILL_T fill = aData ? NO_FILL : m_Fill;
223 
224  if( fill == FILLED_WITH_BG_BODYCOLOR )
225  GRPoly( nullptr, aDC, m_PolyPoints.size(), buffer, 1, GetPenSize(), bgColor, bgColor );
226  else if( fill == FILLED_SHAPE )
227  GRPoly( nullptr, aDC, m_PolyPoints.size(), buffer, 1, GetPenSize(), color, color );
228  else
229  GRPoly( nullptr, aDC, m_PolyPoints.size(), buffer, 0, GetPenSize(), color, color );
230 
231  delete[] buffer;
232 }
233 
234 
235 bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
236 {
237  int delta = std::max( aAccuracy + GetPenSize() / 2,
238  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
239  SHAPE_LINE_CHAIN shape;
240 
241  for( wxPoint pt : m_PolyPoints )
243 
244  if( m_Fill != NO_FILL && m_PolyPoints.size() > 2 )
245  {
246  shape.SetClosed( true );
247  return shape.PointInside( aPosition, delta );
248  }
249  else
250  return shape.PointOnEdge( aPosition, delta );
251 }
252 
253 
254 bool LIB_POLYLINE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
255 {
256  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
257  return false;
258 
259  EDA_RECT sel = aRect;
260 
261  if ( aAccuracy )
262  sel.Inflate( aAccuracy );
263 
264  if( aContained )
265  return sel.Contains( GetBoundingBox() );
266 
267  // Fast test: if rect is outside the polygon bounding box, then they cannot intersect
268  if( !sel.Intersects( GetBoundingBox() ) )
269  return false;
270 
271  // Account for the width of the line
272  sel.Inflate( GetPenSize() / 2 );
273 
274  // Only test closing segment if the polyline is filled
275  int count = m_Fill == NO_FILL ? m_PolyPoints.size() - 1 : m_PolyPoints.size();
276 
277  for( int ii = 0; ii < count; ii++ )
278  {
280  wxPoint ptNext = DefaultTransform.TransformCoordinate( m_PolyPoints[ (ii+1) % count ] );
281 
282  // Test if the point is within aRect
283  if( sel.Contains( pt ) )
284  return true;
285 
286  // Test if this edge intersects aRect
287  if( sel.Intersects( pt, ptNext ) )
288  return true;
289  }
290 
291  return false;
292 }
293 
294 
296 {
297  EDA_RECT rect;
298  int xmin, xmax, ymin, ymax;
299 
300  xmin = xmax = m_PolyPoints[0].x;
301  ymin = ymax = m_PolyPoints[0].y;
302 
303  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
304  {
305  xmin = std::min( xmin, m_PolyPoints[ii].x );
306  xmax = std::max( xmax, m_PolyPoints[ii].x );
307  ymin = std::min( ymin, m_PolyPoints[ii].y );
308  ymax = std::max( ymax, m_PolyPoints[ii].y );
309  }
310 
311  rect.SetOrigin( xmin, ymin );
312  rect.SetEnd( xmax, ymax );
313  rect.Inflate( ( GetPenSize()+1 ) / 2 );
314 
315  rect.RevertYAxis();
316 
317  return rect;
318 }
319 
320 
321 void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
322 {
323  // First segment is kept, only its end point is changed
324  while( GetCornerCount() > 2 )
325  {
326  m_PolyPoints.pop_back();
327 
328  if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
329  {
330  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
331  break;
332  }
333  }
334 }
335 
336 
338 {
339  wxString msg;
340  EDA_RECT bBox = GetBoundingBox();
341 
342  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
343 
344  msg = MessageTextFromValue( aUnits, m_Width, true );
345 
346  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
347 
348  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
349  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
350 
351  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
352 }
353 
354 
356 {
357  return wxString::Format( _( "Polyline at (%s, %s) with %d points" ),
358  MessageTextFromValue( aUnits, m_PolyPoints[0].x ),
359  MessageTextFromValue( aUnits, m_PolyPoints[0].y ),
360  int( m_PolyPoints.size() ) );
361 }
362 
363 
365 {
367 }
368 
369 
370 void LIB_POLYLINE::BeginEdit( const wxPoint aPosition )
371 {
372  m_PolyPoints.push_back( aPosition ); // Start point of first segment.
373  m_PolyPoints.push_back( aPosition ); // End point of first segment.
374 }
375 
376 
377 bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
378 {
379  // do not add zero length segments
380  if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
381  m_PolyPoints.push_back( aPosition );
382 
383  return true;
384 }
385 
386 
388 {
389  // do not include last point twice
390  if( m_PolyPoints.size() > 2 )
391  {
392  if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
393  m_PolyPoints.pop_back();
394  }
395 }
396 
397 
398 void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
399 {
400  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
401 }
Definition: colors.h:57
EDA_UNITS
Definition: common.h:184
void AddCorner(const wxPoint &aPosition)
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
PNG memory record (file in memory).
Definition: bitmap_def.h:29
void GetMsgPanelInfo(EDA_UNITS aUnits, 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:52
Implementation of conversion functions that require both schematic and board internal units.
wxString GetSelectMenuText(EDA_UNITS aUnits) const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Function PointOnEdge()
virtual void SetColor(COLOR4D color)=0
void GRPoly(EDA_RECT *ClipBox, wxDC *DC, int n, const wxPoint *Points, bool Fill, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:542
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:131
int color
Definition: DXF_plotter.cpp:61
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
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.
TRANSFORM DefaultTransform
const BITMAP_OPAQUE add_graphical_segments_xpm[1]
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL)=0
Function PlotPoly.
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:209
bool Contains(const wxPoint &aPoint) const
Function Contains.
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
unsigned GetCornerCount() const
Definition: lib_polyline.h:73
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 components.
Definition: lib_item.h:60
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
This file contains miscellaneous commonly used macros and functions.
bool HitTest(const wxPoint &aPosition, int aAccuracy=0) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item.
const wxPoint GetEnd() const
Definition: eda_rect.h:116
void Offset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
void SetClosed(bool aClosed)
Function SetClosed()
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
#define NULL
void GetMsgPanelInfo(EDA_UNITS aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
int GetPenSize() const override
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:116
COLOR_SETTINGS * ColorSettings()
Definition: plotter.h:145
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:46
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
bool m_isFillable
Definition: lib_item.h:94
Define a library symbol object.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper for all the old plotting/printing code while it still exists.
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:129
std::vector< wxPoint > m_PolyPoints
Definition: lib_polyline.h:34
void AddPoint(const wxPoint &aPoint)
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
void DeleteSegment(const wxPoint aPosition)
Delete the segment at aPosition.
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
Base plotter engine class.
Definition: plotter.h:104
COLOR4D GetColor(int aLayer) const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void BeginEdit(const wxPoint aStartPoint) override
Begin drawing a component library draw item at aPosition.
#define _(s)
Definition: 3d_actions.cpp:33
SHAPE_LINE_CHAIN.
FILL_T m_Fill
The body fill type.
Definition: lib_item.h:93
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:189
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:42
void EndEdit() override
End an object editing action.
const EDA_RECT GetBoundingBox() const override
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
void RemoveCorner(int aIdx)
Message panel definition file.
LIB_POLYLINE(LIB_PART *aParent)
void CalcEdit(const wxPoint &aPosition) override
Calculates 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:76
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
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:145
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
Definition: colors.h:62