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-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 
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>
37 #include <transform.h>
38 
39 
41  LIB_ITEM( LIB_POLYLINE_T, aParent )
42 {
43  m_Fill = NO_FILL;
44  m_Width = 0;
45  m_isFillable = true;
46 }
47 
48 
50 {
51  return new LIB_POLYLINE( *this );
52 }
53 
54 
55 int LIB_POLYLINE::compare( const LIB_ITEM& aOther ) const
56 {
57  wxASSERT( aOther.Type() == LIB_POLYLINE_T );
58 
59  const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
60 
61  if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
62  return m_PolyPoints.size() - tmp->m_PolyPoints.size();
63 
64  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
65  {
66  if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
67  return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
68 
69  if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
70  return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
71  }
72 
73  return 0;
74 }
75 
76 
77 void LIB_POLYLINE::Offset( const wxPoint& aOffset )
78 {
79  for( wxPoint& point : m_PolyPoints )
80  point += aOffset;
81 }
82 
83 
84 bool LIB_POLYLINE::Inside( EDA_RECT& aRect ) const
85 {
86  for( const wxPoint& point : m_PolyPoints )
87  {
88  if( aRect.Contains( point.x, -point.y ) )
89  return true;
90  }
91 
92  return false;
93 }
94 
95 
96 void LIB_POLYLINE::MoveTo( const wxPoint& aPosition )
97 {
98  Offset( aPosition - m_PolyPoints[ 0 ] );
99 }
100 
101 
103 {
104  for( wxPoint& point : m_PolyPoints )
105  {
106  point.x -= aCenter.x;
107  point.x *= -1;
108  point.x += aCenter.x;
109  }
110 }
111 
112 void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
113 {
114  for( wxPoint& point : m_PolyPoints )
115  {
116  point.y -= aCenter.y;
117  point.y *= -1;
118  point.y += aCenter.y;
119  }
120 }
121 
122 void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
123 {
124  for( wxPoint& point : m_PolyPoints )
125  RotatePoint( &point, aCenter, aRotateCCW ? -900 : 900 );
126 }
127 
128 
129 void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
130  const TRANSFORM& aTransform )
131 {
132  wxASSERT( aPlotter != NULL );
133 
134  static std::vector< wxPoint > cornerList;
135  cornerList.clear();
136 
137  for( wxPoint pos : m_PolyPoints )
138  {
139  pos = aTransform.TransformCoordinate( pos ) + aOffset;
140  cornerList.push_back( pos );
141  }
142 
143  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
144  {
146  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
147  }
148 
149  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
150  auto pen_size = GetPenSize();
151 
152  if( !already_filled || pen_size > 0 )
153  {
154  pen_size = std::max( 0, pen_size );
155  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
156  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
157  }
158 }
159 
160 
161 void LIB_POLYLINE::AddPoint( const wxPoint& aPosition )
162 {
163  m_PolyPoints.push_back( aPosition );
164 }
165 
166 
167 void LIB_POLYLINE::AddCorner( const wxPoint& aPosition )
168 {
169  int currentMinDistance = INT_MAX;
170  int closestLineStart = 0;
171 
172  for( unsigned i = 0; i < m_PolyPoints.size() - 1; ++i )
173  {
174  int distance = (int) DistanceLinePoint( m_PolyPoints[i], m_PolyPoints[i + 1], aPosition );
175 
176  if( distance < currentMinDistance )
177  {
178  currentMinDistance = distance;
179  closestLineStart = i;
180  }
181  }
182 
183  m_PolyPoints.insert( m_PolyPoints.begin() + closestLineStart, aPosition );
184 }
185 
186 
188 {
189  m_PolyPoints.erase( m_PolyPoints.begin() + aIdx );
190 }
191 
192 
194 {
195  if( m_Width > 0 )
196  return m_Width;
197 
198  if( m_Width == 0 )
199  return GetDefaultLineThickness();
200 
201  return -1; // the minimal pen value
202 }
203 
204 
205 void LIB_POLYLINE::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
206  const TRANSFORM& aTransform )
207 {
210 
211  wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
212 
213  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
214  buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
215 
216  FILL_T fill = aData ? NO_FILL : m_Fill;
217 
218  if( fill == FILLED_WITH_BG_BODYCOLOR )
219  GRPoly( nullptr, aDC, m_PolyPoints.size(), buffer, 1, GetPenSize(), bgColor, bgColor );
220  else if( fill == FILLED_SHAPE )
221  GRPoly( nullptr, aDC, m_PolyPoints.size(), buffer, 1, GetPenSize(), color, color );
222  else
223  GRPoly( nullptr, aDC, m_PolyPoints.size(), buffer, 0, GetPenSize(), color, color );
224 
225  delete[] buffer;
226 }
227 
228 
229 bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
230 {
231  int delta = std::max( aAccuracy + GetPenSize() / 2,
232  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
233  SHAPE_LINE_CHAIN shape;
234 
235  for( wxPoint pt : m_PolyPoints )
237 
238  if( m_Fill != NO_FILL && m_PolyPoints.size() > 2 )
239  {
240  shape.SetClosed( true );
241  return shape.PointInside( aPosition, delta );
242  }
243  else
244  return shape.PointOnEdge( aPosition, delta );
245 }
246 
247 
248 bool LIB_POLYLINE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
249 {
250  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
251  return false;
252 
253  EDA_RECT sel = aRect;
254 
255  if ( aAccuracy )
256  sel.Inflate( aAccuracy );
257 
258  if( aContained )
259  return sel.Contains( GetBoundingBox() );
260 
261  // Fast test: if rect is outside the polygon bounding box, then they cannot intersect
262  if( !sel.Intersects( GetBoundingBox() ) )
263  return false;
264 
265  // Account for the width of the line
266  sel.Inflate( GetPenSize() / 2 );
267 
268  // Only test closing segment if the polyline is filled
269  int count = m_Fill == NO_FILL ? m_PolyPoints.size() - 1 : m_PolyPoints.size();
270 
271  for( int ii = 0; ii < count; ii++ )
272  {
274  wxPoint ptNext = DefaultTransform.TransformCoordinate( m_PolyPoints[ (ii+1) % count ] );
275 
276  // Test if the point is within aRect
277  if( sel.Contains( pt ) )
278  return true;
279 
280  // Test if this edge intersects aRect
281  if( sel.Intersects( pt, ptNext ) )
282  return true;
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( ( GetPenSize()+1 ) / 2 );
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( aUnits, aList );
337 
338  msg = MessageTextFromValue( aUnits, m_Width, true );
339 
340  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
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, BROWN ) );
346 }
347 
348 
350 {
351  return wxString::Format( _( "Polyline at (%s, %s) with %d points" ),
352  MessageTextFromValue( aUnits, m_PolyPoints[0].x ),
353  MessageTextFromValue( aUnits, m_PolyPoints[0].y ),
354  int( m_PolyPoints.size() ) );
355 }
356 
357 
359 {
361 }
362 
363 
364 void LIB_POLYLINE::BeginEdit( const wxPoint aPosition )
365 {
366  m_PolyPoints.push_back( aPosition ); // Start point of first segment.
367  m_PolyPoints.push_back( aPosition ); // End point of first segment.
368 }
369 
370 
371 bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
372 {
373  // do not add zero length segments
374  if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
375  m_PolyPoints.push_back( aPosition );
376 
377  return true;
378 }
379 
380 
382 {
383  // do not include last point twice
384  if( m_PolyPoints.size() > 2 )
385  {
386  if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
387  m_PolyPoints.pop_back();
388  }
389 }
390 
391 
392 void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
393 {
394  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
395 }
Definition: colors.h:57
EDA_UNITS
Definition: common.h:72
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
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:127
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 compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
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
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
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()
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
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.
#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.
Base plotter engine class.
Definition: plotter.h:103
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:31
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:184
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:163
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.
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:140
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:207
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
Definition: colors.h:62