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-2012 KiCad Developers, see change_log.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 
28 #include <fctsys.h>
29 #include <gr_basic.h>
30 #include <macros.h>
31 #include <class_drawpanel.h>
32 #include <plot_common.h>
33 #include <trigo.h>
34 #include <wxstruct.h>
35 #include <bezier_curves.h>
36 #include <richio.h>
37 #include <base_units.h>
38 #include <msgpanel.h>
39 
40 #include <general.h>
41 #include <lib_bezier.h>
42 #include <transform.h>
43 
44 
46  LIB_ITEM( LIB_BEZIER_T, aParent )
47 {
48  m_Fill = NO_FILL;
49  m_Width = 0;
50  m_isFillable = true;
51  m_typeName = _( "Bezier" );
52 }
53 
54 
55 bool LIB_BEZIER::Save( OUTPUTFORMATTER& aFormatter )
56 {
57  int ccount = GetCornerCount();
58 
59  aFormatter.Print( 0, "B %d %d %d %d", ccount, m_Unit, m_Convert, m_Width );
60 
61  for( unsigned i = 0; i < GetCornerCount(); i++ )
62  {
63  aFormatter.Print( 0, " %d %d", m_BezierPoints[i].x, m_BezierPoints[i].y );
64  }
65 
66  aFormatter.Print( 0, " %c\n", fill_tab[m_Fill] );
67 
68  return true;
69 }
70 
71 
72 bool LIB_BEZIER::Load( LINE_READER& aLineReader, wxString& aErrorMsg )
73 {
74  char* p;
75  int i, ccount = 0;
76  wxPoint pt;
77  char* line = (char*) aLineReader;
78 
79  i = sscanf( line + 2, "%d %d %d %d", &ccount, &m_Unit, &m_Convert, &m_Width );
80 
81  if( i !=4 )
82  {
83  aErrorMsg.Printf( _( "Bezier only had %d parameters of the required 4" ), i );
84  return false;
85  }
86 
87  if( ccount <= 0 )
88  {
89  aErrorMsg.Printf( _( "Bezier count parameter %d is invalid" ), ccount );
90  return false;
91  }
92 
93  strtok( line + 2, " \t\n" ); // Skip field
94  strtok( NULL, " \t\n" ); // Skip field
95  strtok( NULL, " \t\n" ); // Skip field
96  strtok( NULL, " \t\n" );
97 
98  for( i = 0; i < ccount; i++ )
99  {
100  p = strtok( NULL, " \t\n" );
101 
102  if( sscanf( p, "%d", &pt.x ) != 1 )
103  {
104  aErrorMsg.Printf( _( "Bezier point %d X position not defined" ), i );
105  return false;
106  }
107 
108  p = strtok( NULL, " \t\n" );
109 
110  if( sscanf( p, "%d", &pt.y ) != 1 )
111  {
112  aErrorMsg.Printf( _( "Bezier point %d Y position not defined" ), i );
113  return false;
114  }
115 
116  m_BezierPoints.push_back( pt );
117  }
118 
119  m_Fill = NO_FILL;
120 
121  if( ( p = strtok( NULL, " \t\n" ) ) != NULL )
122  {
123  if( p[0] == 'F' )
125 
126  if( p[0] == 'f' )
128  }
129 
130  return true;
131 }
132 
133 
135 {
136  return new LIB_BEZIER( *this );
137 }
138 
139 
140 int LIB_BEZIER::compare( const LIB_ITEM& aOther ) const
141 {
142  wxASSERT( aOther.Type() == LIB_BEZIER_T );
143 
144  const LIB_BEZIER* tmp = ( LIB_BEZIER* ) &aOther;
145 
146  if( m_BezierPoints.size() != tmp->m_BezierPoints.size() )
147  return m_BezierPoints.size() - tmp->m_BezierPoints.size();
148 
149  for( size_t i = 0; i < m_BezierPoints.size(); i++ )
150  {
151  if( m_BezierPoints[i].x != tmp->m_BezierPoints[i].x )
152  return m_BezierPoints[i].x - tmp->m_BezierPoints[i].x;
153 
154  if( m_BezierPoints[i].y != tmp->m_BezierPoints[i].y )
155  return m_BezierPoints[i].y - tmp->m_BezierPoints[i].y;
156  }
157 
158  return 0;
159 }
160 
161 
162 void LIB_BEZIER::SetOffset( const wxPoint& aOffset )
163 {
164  size_t i;
165 
166  for( i = 0; i < m_BezierPoints.size(); i++ )
167  m_BezierPoints[i] += aOffset;
168 
169  for( i = 0; i < m_PolyPoints.size(); i++ )
170  m_PolyPoints[i] += aOffset;
171 }
172 
173 
174 bool LIB_BEZIER::Inside( EDA_RECT& aRect ) const
175 {
176  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
177  {
178  if( aRect.Contains( m_PolyPoints[i].x, -m_PolyPoints[i].y ) )
179  return true;
180  }
181 
182  return false;
183 }
184 
185 
186 void LIB_BEZIER::Move( const wxPoint& aPosition )
187 {
188  SetOffset( aPosition - m_PolyPoints[0] );
189 }
190 
191 
192 void LIB_BEZIER::MirrorHorizontal( const wxPoint& aCenter )
193 {
194  size_t i, imax = m_PolyPoints.size();
195 
196  for( i = 0; i < imax; i++ )
197  {
198  m_PolyPoints[i].x -= aCenter.x;
199  m_PolyPoints[i].x *= -1;
200  m_PolyPoints[i].x += aCenter.x;
201  }
202 
203  imax = m_BezierPoints.size();
204 
205  for( i = 0; i < imax; i++ )
206  {
207  m_BezierPoints[i].x -= aCenter.x;
208  m_BezierPoints[i].x *= -1;
209  m_BezierPoints[i].x += aCenter.x;
210  }
211 }
212 
213 void LIB_BEZIER::MirrorVertical( const wxPoint& aCenter )
214 {
215  size_t i, imax = m_PolyPoints.size();
216 
217  for( i = 0; i < imax; i++ )
218  {
219  m_PolyPoints[i].y -= aCenter.y;
220  m_PolyPoints[i].y *= -1;
221  m_PolyPoints[i].y += aCenter.y;
222  }
223 
224  imax = m_BezierPoints.size();
225 
226  for( i = 0; i < imax; i++ )
227  {
228  m_BezierPoints[i].y -= aCenter.y;
229  m_BezierPoints[i].y *= -1;
230  m_BezierPoints[i].y += aCenter.y;
231  }
232 }
233 
234 void LIB_BEZIER::Rotate( const wxPoint& aCenter, bool aRotateCCW )
235 {
236  int rot_angle = aRotateCCW ? -900 : 900;
237 
238  size_t i, imax = m_PolyPoints.size();
239 
240  for( i = 0; i < imax; i++ )
241  {
242  RotatePoint( &m_PolyPoints[i], aCenter, rot_angle );
243  }
244 
245  imax = m_BezierPoints.size();
246 
247  for( i = 0; i < imax; i++ )
248  {
249  RotatePoint( &m_BezierPoints[i], aCenter, rot_angle );
250  }
251 }
252 
253 
254 void LIB_BEZIER::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
255  const TRANSFORM& aTransform )
256 {
257  wxASSERT( aPlotter != NULL );
258 
259  static std::vector< wxPoint > cornerList;
260  cornerList.clear();
261 
262  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
263  {
264  wxPoint pos = m_PolyPoints[ii];
265  pos = aTransform.TransformCoordinate( pos ) + aOffset;
266  cornerList.push_back( pos );
267  }
268 
269  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
270  {
272  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
273  }
274 
275  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
276  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
277  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, GetPenSize() );
278 }
279 
280 
282 {
283  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
284 }
285 
286 
287 void LIB_BEZIER::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
288  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
289  const TRANSFORM& aTransform )
290 {
291  std::vector<wxPoint> PolyPointsTraslated;
292 
294  BEZIER_POLY converter( m_BezierPoints );
295  converter.GetPoly( m_PolyPoints );
296 
297  PolyPointsTraslated.clear();
298 
299  for( unsigned int i = 0; i < m_PolyPoints.size() ; i++ )
300  PolyPointsTraslated.push_back( aTransform.TransformCoordinate( m_PolyPoints[i] ) +
301  aOffset );
302 
303  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
304  {
305  if( IsSelected() )
306  color = GetItemSelectedColor();
307  }
308  else
309  {
310  color = aColor;
311  }
312 
313  FILL_T fill = aData ? NO_FILL : m_Fill;
314 
315  if( aColor != COLOR4D::UNSPECIFIED )
316  fill = NO_FILL;
317 
318  GRSetDrawMode( aDC, aDrawMode );
319  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
320 
321  if( fill == FILLED_WITH_BG_BODYCOLOR )
322  GRPoly( clipbox, aDC, m_PolyPoints.size(),
323  &PolyPointsTraslated[0], 1, GetPenSize(),
326  else if( fill == FILLED_SHAPE )
327  GRPoly( clipbox, aDC, m_PolyPoints.size(),
328  &PolyPointsTraslated[0], 1, GetPenSize(), color, color );
329  else
330  GRPoly( clipbox, aDC, m_PolyPoints.size(),
331  &PolyPointsTraslated[0], 0, GetPenSize(), color, color );
332 
333  /* Set to one (1) to draw bounding box around bezier curve to validate
334  * bounding box calculation. */
335 #if 0
336  EDA_RECT bBox = GetBoundingBox();
337  GRRect( aPanel->GetClipBox(), aDC, bBox.GetOrigin().x, bBox.GetOrigin().y,
338  bBox.GetEnd().x, bBox.GetEnd().y, 0, LIGHTMAGENTA );
339 #endif
340 }
341 
342 
343 bool LIB_BEZIER::HitTest( const wxPoint& aRefPos ) const
344 {
345  int mindist = GetPenSize() / 2;
346 
347  // Have a minimal tolerance for hit test
348  if ( mindist < MINIMUM_SELECTION_DISTANCE )
349  mindist = MINIMUM_SELECTION_DISTANCE;
350 
351  return HitTest( aRefPos, mindist, DefaultTransform );
352 }
353 
354 
355 bool LIB_BEZIER::HitTest( const wxPoint &aPosRef, int aThreshold, const TRANSFORM& aTransform ) const
356 {
357  wxPoint start, end;
358 
359  if( aThreshold < 0 )
360  aThreshold = GetPenSize() / 2;
361 
362  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
363  {
364  start = aTransform.TransformCoordinate( m_PolyPoints[ii - 1] );
365  end = aTransform.TransformCoordinate( m_PolyPoints[ii] );
366 
367  if ( TestSegmentHit( aPosRef, start, end, aThreshold ) )
368  return true;
369  }
370 
371  return false;
372 }
373 
374 
376 {
377  EDA_RECT rect;
378  int xmin, xmax, ymin, ymax;
379 
380  if( !GetCornerCount() )
381  return rect;
382 
383  xmin = xmax = m_PolyPoints[0].x;
384  ymin = ymax = m_PolyPoints[0].y;
385 
386  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
387  {
388  xmin = std::min( xmin, m_PolyPoints[ii].x );
389  xmax = std::max( xmax, m_PolyPoints[ii].x );
390  ymin = std::min( ymin, m_PolyPoints[ii].y );
391  ymax = std::max( ymax, m_PolyPoints[ii].y );
392  }
393 
394  rect.SetOrigin( xmin, ymin );
395  rect.SetEnd( xmax, ymax );
396  rect.Inflate( ( GetPenSize()+1 ) / 2 );
397 
398  rect.RevertYAxis();
399 
400  return rect;
401 }
402 
403 
404 void LIB_BEZIER::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
405 {
406  wxString msg;
407  EDA_RECT bBox = GetBoundingBox();
408 
409  LIB_ITEM::GetMsgPanelInfo( aList );
410 
411  msg = StringFromValue( g_UserUnit, m_Width, true );
412 
413  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
414 
415  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
416  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
417 
418  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
419 }
Definition: colors.h:57
int compare(const LIB_ITEM &aOther) const override
Function compare provides the draw object specific comparison called by the == and < operators...
Definition: lib_bezier.cpp:140
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
void SetOffset(const wxPoint &aOffset) override
Function Offset sets the drawing object by aOffset from the current position.
Definition: lib_bezier.cpp:162
void GRPoly(EDA_RECT *ClipBox, wxDC *DC, int n, wxPoint Points[], bool Fill, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:767
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:290
wxString m_typeName
Name of object displayed in the message panel.
Implementation of conversion functions that require both schematic and board internal units...
virtual void SetColor(COLOR4D color)=0
void Move(const wxPoint &aPosition) override
Function Move moves a draw object to aPosition.
Definition: lib_bezier.cpp:186
LIB_BEZIER(LIB_PART *aParent)
Definition: lib_bezier.cpp:45
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:54
std::vector< wxPoint > m_BezierPoints
Definition: lib_bezier.h:42
bool Contains(const wxPoint &aPoint) const
Function Contains.
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:203
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
Definition: lib_bezier.cpp:404
void GetPoly(std::vector< wxPoint > &aOutput)
Converts Bezier curve to a polygon.
const EDA_RECT GetBoundingBox() const override
Definition: lib_bezier.cpp:375
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:254
bool IsSelected() const
Definition: base_struct.h:221
void SetOrigin(const wxPoint &pos)
int m_Unit
Unit identification for multiple parts per package.
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
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:166
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
Class LIB_ITEM is the base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:77
This file contains miscellaneous commonly used macros and functions.
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
const wxPoint & GetOrigin() const
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1077
void MirrorHorizontal(const wxPoint &aCenter) override
Function MirrorHorizontal mirrors the draw object along the horizontal (X) axis about aCenter point...
Definition: lib_bezier.cpp:192
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
bool Inside(EDA_RECT &aRect) const override
Function Inside tests if any part of the draw object is inside rectangle bounds of aRect...
Definition: lib_bezier.cpp:174
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
Base window classes and related definitions.
void SetEnd(int x, int y)
bool Load(LINE_READER &aLineReader, wxString &aErrorMsg) override
Definition: lib_bezier.cpp:72
std::vector< wxPoint > m_PolyPoints
Definition: lib_bezier.h:43
bool m_isFillable
Flag to indicate if draw item is fillable.
Class LIB_PART defines a library part object.
EDA_RECT * GetClipBox()
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
unsigned GetCornerCount() const
Definition: lib_bezier.h:73
virtual void Rotate()
Rotate the draw item.
const wxPoint GetEnd() const
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_bezier.cpp:134
int GetPenSize() const override
Function GetPenSize.
Definition: lib_bezier.cpp:281
Base plotter engine class.
Definition: plot_common.h:86
Bezier curves to polygon converter.
Definition: bezier_curves.h:34
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo displays basic info (type, part and convert) about the current item in messa...
TRANSFORM DefaultTransform
Definition: eeschema.cpp:57
const int fill_tab[3]
bool Save(OUTPUTFORMATTER &aFormatter) override
Function Save writes draw item object to aFormatter in component library "*.lib" format.
Definition: lib_bezier.cpp:55
#define max(a, b)
Definition: auxiliary.h:86
FILL_T m_Fill
The body fill type.
bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
Definition: lib_bezier.cpp:343
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:175
Class EDA_RECT handles the component boundary box.
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Function drawGraphic.
Definition: lib_bezier.cpp:287
void MirrorVertical(const wxPoint &aCenter) override
Function MirrorVertical mirrors the draw object along the MirrorVertical (Y) axis about aCenter point...
Definition: lib_bezier.cpp:213
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
int m_Convert
Shape identification for alternate body styles.
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:56
COLOR4D GetItemSelectedColor()
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Function TestSegmentHit test for hit on line segment i.e.
Definition: trigo.cpp:142
int m_Width
Definition: lib_bezier.h:41
Message panel definition file.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
#define min(a, b)
Definition: auxiliary.h:85
Class LIB_BEZIER defines bezier curve graphic body item.
Definition: lib_bezier.h:39
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112
Definition: colors.h:62