KiCad PCB EDA Suite
lib_bezier.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) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <fctsys.h>
25 #include <gr_basic.h>
26 #include <macros.h>
27 #include <sch_draw_panel.h>
28 #include <plotter.h>
29 #include <trigo.h>
30 #include <bezier_curves.h>
31 #include <base_units.h>
32 #include <msgpanel.h>
33 
34 #include <general.h>
35 #include <lib_bezier.h>
36 #include <transform.h>
37 
38 
40  LIB_ITEM( LIB_BEZIER_T, aParent )
41 {
42  m_Fill = NO_FILL;
43  m_Width = 0;
44  m_isFillable = true;
45 }
46 
47 
49 {
50  return new LIB_BEZIER( *this );
51 }
52 
53 
54 int LIB_BEZIER::compare( const LIB_ITEM& aOther ) const
55 {
56  wxASSERT( aOther.Type() == LIB_BEZIER_T );
57 
58  const LIB_BEZIER* tmp = ( LIB_BEZIER* ) &aOther;
59 
60  if( m_BezierPoints.size() != tmp->m_BezierPoints.size() )
61  return m_BezierPoints.size() - tmp->m_BezierPoints.size();
62 
63  for( size_t i = 0; i < m_BezierPoints.size(); i++ )
64  {
65  if( m_BezierPoints[i].x != tmp->m_BezierPoints[i].x )
66  return m_BezierPoints[i].x - tmp->m_BezierPoints[i].x;
67 
68  if( m_BezierPoints[i].y != tmp->m_BezierPoints[i].y )
69  return m_BezierPoints[i].y - tmp->m_BezierPoints[i].y;
70  }
71 
72  return 0;
73 }
74 
75 
76 void LIB_BEZIER::Offset( const wxPoint& aOffset )
77 {
78  size_t i;
79 
80  for( i = 0; i < m_BezierPoints.size(); i++ )
81  m_BezierPoints[i] += aOffset;
82 
83  for( i = 0; i < m_PolyPoints.size(); i++ )
84  m_PolyPoints[i] += aOffset;
85 }
86 
87 
88 bool LIB_BEZIER::Inside( EDA_RECT& aRect ) const
89 {
90  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
91  {
92  if( aRect.Contains( m_PolyPoints[i].x, -m_PolyPoints[i].y ) )
93  return true;
94  }
95 
96  return false;
97 }
98 
99 
100 void LIB_BEZIER::MoveTo( const wxPoint& aPosition )
101 {
102  if ( !m_PolyPoints.size() )
103  m_PolyPoints.push_back( wxPoint(0, 0) );
104 
105  Offset( aPosition - m_PolyPoints[ 0 ] );
106 }
107 
108 const wxPoint LIB_BEZIER::GetOffset() const
109 {
110  if ( !m_PolyPoints.size() )
111  return wxPoint(0, 0);
112 
113  return m_PolyPoints[0];
114 }
115 
116 void LIB_BEZIER::MirrorHorizontal( const wxPoint& aCenter )
117 {
118  size_t i, imax = m_PolyPoints.size();
119 
120  for( i = 0; i < imax; i++ )
121  {
122  m_PolyPoints[i].x -= aCenter.x;
123  m_PolyPoints[i].x *= -1;
124  m_PolyPoints[i].x += aCenter.x;
125  }
126 
127  imax = m_BezierPoints.size();
128 
129  for( i = 0; i < imax; i++ )
130  {
131  m_BezierPoints[i].x -= aCenter.x;
132  m_BezierPoints[i].x *= -1;
133  m_BezierPoints[i].x += aCenter.x;
134  }
135 }
136 
137 void LIB_BEZIER::MirrorVertical( const wxPoint& aCenter )
138 {
139  size_t i, imax = m_PolyPoints.size();
140 
141  for( i = 0; i < imax; i++ )
142  {
143  m_PolyPoints[i].y -= aCenter.y;
144  m_PolyPoints[i].y *= -1;
145  m_PolyPoints[i].y += aCenter.y;
146  }
147 
148  imax = m_BezierPoints.size();
149 
150  for( i = 0; i < imax; i++ )
151  {
152  m_BezierPoints[i].y -= aCenter.y;
153  m_BezierPoints[i].y *= -1;
154  m_BezierPoints[i].y += aCenter.y;
155  }
156 }
157 
158 void LIB_BEZIER::Rotate( const wxPoint& aCenter, bool aRotateCCW )
159 {
160  int rot_angle = aRotateCCW ? -900 : 900;
161 
162  for( wxPoint& point : m_PolyPoints )
163  RotatePoint( &point, aCenter, rot_angle );
164 
165  for( wxPoint& bezierPoint : m_BezierPoints )
166  RotatePoint( &bezierPoint, aCenter, rot_angle );
167 }
168 
169 
170 void LIB_BEZIER::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
171  const TRANSFORM& aTransform )
172 {
173  wxASSERT( aPlotter != NULL );
174 
175  static std::vector< wxPoint > cornerList;
176  cornerList.clear();
177 
178  for( wxPoint pos : m_PolyPoints )
179  {
180  pos = aTransform.TransformCoordinate( pos ) + aOffset;
181  cornerList.push_back( pos );
182  }
183 
184  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
185  {
187  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
188  }
189 
190  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
191  auto pen_size = GetPenSize();
192 
193  if( !already_filled || pen_size > 0 )
194  {
195  pen_size = std::max( 0, pen_size );
196  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
197  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
198  }
199 }
200 
201 
203 {
204  if( m_Width > 0 )
205  return m_Width;
206 
207  if( m_Width == 0 )
208  return GetDefaultLineThickness();
209 
210  return -1; // a value to use a minimal pen size
211 }
212 
213 
214 void LIB_BEZIER::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
215  const TRANSFORM& aTransform )
216 {
217  std::vector<wxPoint> PolyPointsTraslated;
218 
221  BEZIER_POLY converter( m_BezierPoints );
222  converter.GetPoly( m_PolyPoints );
223 
224  PolyPointsTraslated.clear();
225 
226  for( wxPoint& point : m_PolyPoints )
227  PolyPointsTraslated.push_back( aTransform.TransformCoordinate( point ) + aOffset );
228 
229  FILL_T fill = aData ? NO_FILL : m_Fill;
230 
231  if( fill == FILLED_WITH_BG_BODYCOLOR )
232  {
233  GRPoly( nullptr, aDC, m_PolyPoints.size(), &PolyPointsTraslated[0], true, GetPenSize(),
234  bgColor, bgColor );
235  }
236  else if( fill == FILLED_SHAPE )
237  {
238  GRPoly( nullptr, aDC, m_PolyPoints.size(), &PolyPointsTraslated[0], true, GetPenSize(),
239  color, color );
240  }
241  else
242  {
243  GRPoly( nullptr, aDC, m_PolyPoints.size(), &PolyPointsTraslated[0], false, GetPenSize(),
244  color, color );
245  }
246 }
247 
248 
249 bool LIB_BEZIER::HitTest( const wxPoint& aRefPos, int aAccuracy ) const
250 {
251  int mindist = std::max( aAccuracy + GetPenSize() / 2, MINIMUM_SELECTION_DISTANCE );
252  wxPoint start, end;
253 
254  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
255  {
258 
259  if ( TestSegmentHit( aRefPos, start, end, mindist ) )
260  return true;
261  }
262 
263  return false;
264 }
265 
266 
267 bool LIB_BEZIER::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
268 {
269  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
270  return false;
271 
272  EDA_RECT sel = aRect;
273 
274  if ( aAccuracy )
275  sel.Inflate( aAccuracy );
276 
277  if( aContained )
278  return sel.Contains( GetBoundingBox() );
279 
280  // Fast test: if aRect is outside the polygon bounding box, rectangles cannot intersect
281  if( !sel.Intersects( GetBoundingBox() ) )
282  return false;
283 
284  // Account for the width of the line
285  sel.Inflate( GetWidth() / 2 );
286  unsigned count = m_BezierPoints.size();
287 
288  for( unsigned ii = 1; ii < count; ii++ )
289  {
290  wxPoint vertex = DefaultTransform.TransformCoordinate( m_BezierPoints[ii-1] );
291  wxPoint vertexNext = DefaultTransform.TransformCoordinate( m_BezierPoints[ii] );
292 
293  // Test if the point is within aRect
294  if( sel.Contains( vertex ) )
295  return true;
296 
297  // Test if this edge intersects aRect
298  if( sel.Intersects( vertex, vertexNext ) )
299  return true;
300  }
301 
302  return false;
303 }
304 
305 
307 {
308  EDA_RECT rect;
309  int xmin, xmax, ymin, ymax;
310 
311  if( !GetCornerCount() )
312  return rect;
313 
314  xmin = xmax = m_PolyPoints[0].x;
315  ymin = ymax = m_PolyPoints[0].y;
316 
317  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
318  {
319  xmin = std::min( xmin, m_PolyPoints[ii].x );
320  xmax = std::max( xmax, m_PolyPoints[ii].x );
321  ymin = std::min( ymin, m_PolyPoints[ii].y );
322  ymax = std::max( ymax, m_PolyPoints[ii].y );
323  }
324 
325  rect.SetOrigin( xmin, ymin );
326  rect.SetEnd( xmax, ymax );
327  rect.Inflate( ( GetPenSize()+1 ) / 2 );
328 
329  rect.RevertYAxis();
330 
331  return rect;
332 }
333 
334 
335 void LIB_BEZIER::GetMsgPanelInfo( EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM >& aList )
336 {
337  wxString msg;
338  EDA_RECT bBox = GetBoundingBox();
339 
340  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
341 
342  msg = MessageTextFromValue( aUnits, m_Width, true );
343 
344  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
345 
346  msg.Printf( wxT( "(%d, %d, %d, %d)" ),
347  bBox.GetOrigin().x,
348  bBox.GetOrigin().y,
349  bBox.GetEnd().x,
350  bBox.GetEnd().y );
351 
352  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
353 }
354 
355 wxPoint LIB_BEZIER::GetPosition() const
356 {
357  if( !m_PolyPoints.size() )
358  return wxPoint(0, 0);
359 
360  return m_PolyPoints[0];
361 }
Definition: colors.h:57
int compare(const LIB_ITEM &aOther) const override
Provide the draw object specific comparison called by the == and < operators.
Definition: lib_bezier.cpp:54
void GetMsgPanelInfo(EDA_UNITS_T 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
void GRPoly(EDA_RECT *ClipBox, wxDC *DC, int n, wxPoint Points[], bool Fill, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:546
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_bezier.cpp:100
const wxPoint GetOffset() const
Definition: lib_bezier.cpp:108
Implementation of conversion functions that require both schematic and board internal units.
virtual void SetColor(COLOR4D color)=0
LIB_BEZIER(LIB_PART *aParent)
Definition: lib_bezier.cpp:39
std::vector< wxPoint > m_BezierPoints
Definition: lib_bezier.h:38
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:131
int color
Definition: DXF_plotter.cpp:62
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i....
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: lib_bezier.cpp:306
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.
Definition: lib_bezier.cpp:249
void Plot(PLOTTER *aPlotter, const wxPoint &aOffset, bool aFill, const TRANSFORM &aTransform) override
Plot the draw item using the plot object.
Definition: lib_bezier.cpp:170
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:206
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL)=0
Function PlotPoly.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:160
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size)
Definition: eda_rect.h:209
wxPoint GetPosition() const override
Definition: lib_bezier.cpp:355
bool Contains(const wxPoint &aPoint) const
Function Contains.
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
The base class for drawable items used by schematic library components.
Definition: lib_item.h:61
This file contains miscellaneous commonly used macros and functions.
int GetWidth() const override
Definition: lib_bezier.h:92
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
unsigned GetCornerCount() const
Definition: lib_bezier.h:70
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.
Definition: lib_bezier.cpp:76
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_bezier.cpp:116
Class for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
void Rotate(const wxPoint &aCenter, bool aRotateCCW=true) override
Rotate the object about aCenter point.
Definition: lib_bezier.cpp:158
bool Inside(EDA_RECT &aRect) const override
Test if any part of the draw object is inside rectangle bounds of aRect.
Definition: lib_bezier.cpp:88
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_bezier.cpp:214
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
void SetEnd(int x, int y)
Definition: eda_rect.h:192
std::vector< wxPoint > m_PolyPoints
Definition: lib_bezier.h:39
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:47
bool m_isFillable
Definition: lib_item.h:95
Define a library symbol object.
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:129
void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
Definition: lib_bezier.cpp:335
#define _(s)
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_bezier.cpp:48
int GetPenSize() const override
Definition: lib_bezier.cpp:202
Base plotter engine class.
Definition: plotter.h:97
Bezier curves to polygon converter.
Definition: bezier_curves.h:35
TRANSFORM DefaultTransform
Definition: eeschema.cpp:50
#define max(a, b)
Definition: auxiliary.h:86
FILL_T m_Fill
The body fill type.
Definition: lib_item.h:94
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:187
size_t i
Definition: json11.cpp:597
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_bezier.cpp:137
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
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
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:127
int m_Width
Definition: lib_bezier.h:37
Message panel definition file.
EDA_UNITS_T
Definition: common.h:133
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:210
#define min(a, b)
Definition: auxiliary.h:85
Class LIB_BEZIER defines bezier curve graphic body item.
Definition: lib_bezier.h:35
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
Definition: colors.h:62