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 <sch_draw_panel.h>
26 #include <eda_draw_frame.h>
27 #include <plotter.h>
28 #include <trigo.h>
29 #include <base_units.h>
30 #include <widgets/msgpanel.h>
31 #include <bitmaps.h>
32 #include <eda_draw_frame.h>
33 #include <general.h>
34 #include <lib_polyline.h>
36 #include <transform.h>
37 
38 
40  LIB_ITEM( LIB_POLYLINE_T, aParent )
41 {
43  m_Width = 0;
44  m_isFillable = true;
45 }
46 
47 
49 {
50  return new LIB_POLYLINE( *this );
51 }
52 
53 
54 int LIB_POLYLINE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
55 {
56  wxASSERT( aOther.Type() == LIB_POLYLINE_T );
57 
58  int retv = LIB_ITEM::compare( aOther );
59 
60  if( retv )
61  return retv;
62 
63  const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
64 
65  if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
66  return m_PolyPoints.size() - tmp->m_PolyPoints.size();
67 
68  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
69  {
70  if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
71  return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
72 
73  if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
74  return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
75  }
76 
77  return 0;
78 }
79 
80 
81 void LIB_POLYLINE::Offset( const wxPoint& aOffset )
82 {
83  for( wxPoint& point : m_PolyPoints )
84  point += aOffset;
85 }
86 
87 
88 void LIB_POLYLINE::MoveTo( const wxPoint& aPosition )
89 {
90  Offset( aPosition - m_PolyPoints[ 0 ] );
91 }
92 
93 
94 void LIB_POLYLINE::MirrorHorizontal( const wxPoint& aCenter )
95 {
96  for( wxPoint& point : m_PolyPoints )
97  {
98  point.x -= aCenter.x;
99  point.x *= -1;
100  point.x += aCenter.x;
101  }
102 }
103 
104 void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
105 {
106  for( wxPoint& point : m_PolyPoints )
107  {
108  point.y -= aCenter.y;
109  point.y *= -1;
110  point.y += aCenter.y;
111  }
112 }
113 
114 void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
115 {
116  for( wxPoint& point : m_PolyPoints )
117  RotatePoint( &point, aCenter, aRotateCCW ? -900 : 900 );
118 }
119 
120 
121 void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
122  const TRANSFORM& aTransform )
123 {
124  wxASSERT( aPlotter != NULL );
125 
126  static std::vector< wxPoint > cornerList;
127  cornerList.clear();
128 
129  for( wxPoint pos : m_PolyPoints )
130  {
131  pos = aTransform.TransformCoordinate( pos ) + aOffset;
132  cornerList.push_back( pos );
133  }
134 
136  {
137  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
138  aPlotter->PlotPoly( cornerList, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
139  }
140 
141  bool already_filled = m_Fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
142  int pen_size = GetPenWidth();
143 
144  if( !already_filled || pen_size > 0 )
145  {
146  pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetDefaultPenWidth() );
147 
148  aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
149  aPlotter->PlotPoly( cornerList, already_filled ? FILL_TYPE::NO_FILL : m_Fill, pen_size );
150  }
151 }
152 
153 
154 void LIB_POLYLINE::AddPoint( const wxPoint& aPosition )
155 {
156  m_PolyPoints.push_back( aPosition );
157 }
158 
159 
160 void LIB_POLYLINE::AddCorner( const wxPoint& aPosition )
161 {
162  int currentMinDistance = INT_MAX;
163  int closestLineStart = 0;
164 
165  for( unsigned i = 0; i < m_PolyPoints.size() - 1; ++i )
166  {
167  int distance = (int) DistanceLinePoint( m_PolyPoints[i], m_PolyPoints[i + 1], aPosition );
168 
169  if( distance < currentMinDistance )
170  {
171  currentMinDistance = distance;
172  closestLineStart = i;
173  }
174  }
175 
176  m_PolyPoints.insert( m_PolyPoints.begin() + closestLineStart, aPosition );
177 }
178 
179 
181 {
182  m_PolyPoints.erase( m_PolyPoints.begin() + aIdx );
183 }
184 
185 
187 {
188  // Historically 0 meant "default width" and negative numbers meant "don't stroke".
189  if( m_Width < 0 && GetFillMode() != FILL_TYPE::NO_FILL )
190  return 0;
191  else
192  return std::max( m_Width, 1 );
193 }
194 
195 
196 void LIB_POLYLINE::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
197  const TRANSFORM& aTransform )
198 {
199  bool forceNoFill = static_cast<bool>( aData );
200  int penWidth = GetPenWidth();
201 
202  if( forceNoFill && m_Fill != FILL_TYPE::NO_FILL && penWidth == 0 )
203  return;
204 
205  wxDC* DC = aSettings->GetPrintDC();
206  COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
207  wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
208 
209  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
210  buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
211 
212  if( forceNoFill || m_Fill == FILL_TYPE::NO_FILL )
213  {
214  penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
215 
216  GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, false, penWidth, color, color );
217  }
218  else
219  {
222 
223  GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, true, penWidth, color, color );
224  }
225 
226  delete[] buffer;
227 }
228 
229 
230 bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
231 {
232  int delta = std::max( aAccuracy + GetPenWidth() / 2, Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
233  SHAPE_LINE_CHAIN shape;
234 
235  for( wxPoint pt : m_PolyPoints )
237 
238  if( m_Fill != FILL_TYPE::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( ( GetPenWidth() / 2 ) + 1 );
267 
268  // Only test closing segment if the polyline is filled
269  int count = m_Fill == FILL_TYPE::NO_FILL ? m_PolyPoints.size() - 1 : m_PolyPoints.size();
270 
271  for( int ii = 0; ii < count; ii++ )
272  {
273  wxPoint pt = DefaultTransform.TransformCoordinate( m_PolyPoints[ ii ] );
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( ( 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" ),
352  int( m_PolyPoints.size() ) );
353 }
354 
355 
357 {
359 }
360 
361 
362 void LIB_POLYLINE::BeginEdit( const wxPoint aPosition )
363 {
364  m_PolyPoints.push_back( aPosition ); // Start point of first segment.
365  m_PolyPoints.push_back( aPosition ); // End point of first segment.
366 }
367 
368 
369 bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
370 {
371  // do not add zero length segments
372  if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
373  m_PolyPoints.push_back( aPosition );
374 
375  return true;
376 }
377 
378 
380 {
381  // do not include last point twice
382  if( m_PolyPoints.size() > 2 )
383  {
384  if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
385  m_PolyPoints.pop_back();
386  }
387 }
388 
389 
390 void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
391 {
392  m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
393 }
void AddCorner(const wxPoint &aPosition)
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Definition: base_units.cpp:123
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
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:540
int color
Definition: DXF_plotter.cpp:60
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
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
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_TYPE aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL)=0
Draw a polygon ( filled or not )
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
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.
Definition: eda_rect.cpp:57
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:62
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
FILL_TYPE m_Fill
The body fill type.
Definition: lib_item.h:94
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()
FILL_TYPE GetFillMode() const
Definition: lib_item.h:302
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:117
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:48
bool ContinueEdit(const wxPoint aNextPoint) override
Continue an edit in progress at aPosition.
bool m_isFillable
Definition: lib_item.h:95
Define a library symbol object.
std::vector< wxPoint > m_PolyPoints
Definition: lib_polyline.h:34
#define STRUCT_DELETED
flag indication structures to be erased
Definition: eda_item.h:115
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:50
void print(RENDER_SETTINGS *aSettings, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs 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:131
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:165
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.
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: eda_item.h:148
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests 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
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:54
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: eda_item.h:117
void RemoveCorner(int aIdx)
STATUS_FLAGS m_flags
Definition: eda_item.h:164
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:74
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:151
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.
Definition: eda_rect.cpp:363
KICAD_T Type() const
Function Type()
Definition: eda_item.h:181
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100