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 <eda_draw_frame.h>
30 #include <plotter.h>
31 #include <trigo.h>
32 #include <base_units.h>
33 #include <msgpanel.h>
34 #include <bitmaps.h>
35 #include <eda_draw_frame.h>
36 #include <general.h>
37 #include <lib_polyline.h>
39 #include <transform.h>
40 
41 
43  LIB_ITEM( LIB_POLYLINE_T, aParent )
44 {
45  m_Fill = NO_FILL;
46  m_Width = 0;
47  m_isFillable = true;
48 }
49 
50 
52 {
53  return new LIB_POLYLINE( *this );
54 }
55 
56 
57 int LIB_POLYLINE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
58 {
59  wxASSERT( aOther.Type() == LIB_POLYLINE_T );
60 
61  int retv = LIB_ITEM::compare( aOther );
62 
63  if( retv )
64  return retv;
65 
66  const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
67 
68  if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
69  return m_PolyPoints.size() - tmp->m_PolyPoints.size();
70 
71  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
72  {
73  if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
74  return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
75 
76  if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
77  return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
78  }
79 
80  return 0;
81 }
82 
83 
84 void LIB_POLYLINE::Offset( const wxPoint& aOffset )
85 {
86  for( wxPoint& point : m_PolyPoints )
87  point += aOffset;
88 }
89 
90 
91 void LIB_POLYLINE::MoveTo( const wxPoint& aPosition )
92 {
93  Offset( aPosition - m_PolyPoints[ 0 ] );
94 }
95 
96 
98 {
99  for( wxPoint& point : m_PolyPoints )
100  {
101  point.x -= aCenter.x;
102  point.x *= -1;
103  point.x += aCenter.x;
104  }
105 }
106 
107 void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
108 {
109  for( wxPoint& point : m_PolyPoints )
110  {
111  point.y -= aCenter.y;
112  point.y *= -1;
113  point.y += aCenter.y;
114  }
115 }
116 
117 void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
118 {
119  for( wxPoint& point : m_PolyPoints )
120  RotatePoint( &point, aCenter, aRotateCCW ? -900 : 900 );
121 }
122 
123 
124 void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
125  const TRANSFORM& aTransform )
126 {
127  wxASSERT( aPlotter != NULL );
128 
129  static std::vector< wxPoint > cornerList;
130  cornerList.clear();
131 
132  for( wxPoint pos : m_PolyPoints )
133  {
134  pos = aTransform.TransformCoordinate( pos ) + aOffset;
135  cornerList.push_back( pos );
136  }
137 
138  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
139  {
140  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
141  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
142  }
143 
144  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
145  int pen_size = GetPenWidth();
146 
147  if( !already_filled || pen_size > 0 )
148  {
149  pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetDefaultPenWidth() );
150 
151  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
152  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
153  }
154 }
155 
156 
157 void LIB_POLYLINE::AddPoint( const wxPoint& aPosition )
158 {
159  m_PolyPoints.push_back( aPosition );
160 }
161 
162 
163 void LIB_POLYLINE::AddCorner( const wxPoint& aPosition )
164 {
165  int currentMinDistance = INT_MAX;
166  int closestLineStart = 0;
167 
168  for( unsigned i = 0; i < m_PolyPoints.size() - 1; ++i )
169  {
170  int distance = (int) DistanceLinePoint( m_PolyPoints[i], m_PolyPoints[i + 1], aPosition );
171 
172  if( distance < currentMinDistance )
173  {
174  currentMinDistance = distance;
175  closestLineStart = i;
176  }
177  }
178 
179  m_PolyPoints.insert( m_PolyPoints.begin() + closestLineStart, aPosition );
180 }
181 
182 
184 {
185  m_PolyPoints.erase( m_PolyPoints.begin() + aIdx );
186 }
187 
188 
190 {
191  // Historically 0 meant "default width" and negative numbers meant "don't stroke".
192  if( m_Width < 0 && GetFillMode() != NO_FILL )
193  return 0;
194  else
195  return std::max( m_Width, 1 );
196 }
197 
198 
199 void LIB_POLYLINE::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
200  const TRANSFORM& aTransform )
201 {
202  bool forceNoFill = static_cast<bool>( aData );
203  int penWidth = GetPenWidth();
204 
205  if( forceNoFill && m_Fill != NO_FILL && penWidth == 0 )
206  return;
207 
208  wxDC* DC = aSettings->GetPrintDC();
209  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
210  wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
211 
212  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
213  buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
214 
215  if( forceNoFill || m_Fill == NO_FILL )
216  {
217  penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
218 
219  GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, false, penWidth, color, color );
220  }
221  else
222  {
225 
226  GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, true, penWidth, color, color );
227  }
228 
229  delete[] buffer;
230 }
231 
232 
233 bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
234 {
235  int delta = std::max( aAccuracy + GetPenWidth() / 2, Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
236  SHAPE_LINE_CHAIN shape;
237 
238  for( wxPoint pt : m_PolyPoints )
240 
241  if( m_Fill != NO_FILL && m_PolyPoints.size() > 2 )
242  {
243  shape.SetClosed( true );
244  return shape.PointInside( aPosition, delta );
245  }
246  else
247  return shape.PointOnEdge( aPosition, delta );
248 }
249 
250 
251 bool LIB_POLYLINE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
252 {
253  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
254  return false;
255 
256  EDA_RECT sel = aRect;
257 
258  if ( aAccuracy )
259  sel.Inflate( aAccuracy );
260 
261  if( aContained )
262  return sel.Contains( GetBoundingBox() );
263 
264  // Fast test: if rect is outside the polygon bounding box, then they cannot intersect
265  if( !sel.Intersects( GetBoundingBox() ) )
266  return false;
267 
268  // Account for the width of the line
269  sel.Inflate( ( GetPenWidth() / 2 ) + 1 );
270 
271  // Only test closing segment if the polyline is filled
272  int count = m_Fill == NO_FILL ? m_PolyPoints.size() - 1 : m_PolyPoints.size();
273 
274  for( int ii = 0; ii < count; ii++ )
275  {
277  wxPoint ptNext = DefaultTransform.TransformCoordinate( m_PolyPoints[ (ii+1) % count ] );
278 
279  // Test if the point is within aRect
280  if( sel.Contains( pt ) )
281  return true;
282 
283  // Test if this edge intersects aRect
284  if( sel.Intersects( pt, ptNext ) )
285  return true;
286  }
287 
288  return false;
289 }
290 
291 
293 {
294  EDA_RECT rect;
295  int xmin, xmax, ymin, ymax;
296 
297  xmin = xmax = m_PolyPoints[0].x;
298  ymin = ymax = m_PolyPoints[0].y;
299 
300  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
301  {
302  xmin = std::min( xmin, m_PolyPoints[ii].x );
303  xmax = std::max( xmax, m_PolyPoints[ii].x );
304  ymin = std::min( ymin, m_PolyPoints[ii].y );
305  ymax = std::max( ymax, m_PolyPoints[ii].y );
306  }
307 
308  rect.SetOrigin( xmin, ymin );
309  rect.SetEnd( xmax, ymax );
310  rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
311 
312  rect.RevertYAxis();
313 
314  return rect;
315 }
316 
317 
318 void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
319 {
320  // First segment is kept, only its end point is changed
321  while( GetCornerCount() > 2 )
322  {
323  m_PolyPoints.pop_back();
324 
325  if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
326  {
327  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
328  break;
329  }
330  }
331 }
332 
333 
335 {
336  wxString msg;
337  EDA_RECT bBox = GetBoundingBox();
338 
339  LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
340 
341  msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width, true );
342 
343  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
344 
345  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
346  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
347 
348  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
349 }
350 
351 
353 {
354  return wxString::Format( _( "Polyline, %d points" ),
355  int( m_PolyPoints.size() ) );
356 }
357 
358 
360 {
362 }
363 
364 
365 void LIB_POLYLINE::BeginEdit( const wxPoint aPosition )
366 {
367  m_PolyPoints.push_back( aPosition ); // Start point of first segment.
368  m_PolyPoints.push_back( aPosition ); // End point of first segment.
369 }
370 
371 
372 bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
373 {
374  // do not add zero length segments
375  if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
376  m_PolyPoints.push_back( aPosition );
377 
378  return true;
379 }
380 
381 
383 {
384  // do not include last point twice
385  if( m_PolyPoints.size() > 2 )
386  {
387  if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
388  m_PolyPoints.pop_back();
389  }
390 }
391 
392 
393 void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
394 {
395  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
396 }
EDA_UNITS
Definition: common.h:198
void AddCorner(const wxPoint &aPosition)
PNG memory record (file in memory).
Definition: bitmap_def.h:29
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
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...
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:544
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:128
int color
Definition: DXF_plotter.cpp:61
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.
FILL_T GetFillMode() const
Definition: lib_item.h:301
Definition: color4d.h:61
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
TRANSFORM DefaultTransform
const BITMAP_OPAQUE add_graphical_segments_xpm[1]
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Function PointOnEdge()
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.
The base class for create windows for drawing purpose.
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.
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:61
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
int GetPenWidth() const override
#define NULL
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:116
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:47
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.
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:126
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 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:52
void print(RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
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:124
Base plotter engine class.
Definition: plotter.h:114
Definition: color4d.h:56
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:147
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:201
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
Definition: base_struct.h:176
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:159
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.
void EndEdit() override
End an object editing action.
const EDA_RECT GetBoundingBox() const override
int GetDefaultPenWidth() const
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:150
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
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:193
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99