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-2017 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 
28 #include <fctsys.h>
29 #include <gr_basic.h>
30 #include <macros.h>
31 #include <class_drawpanel.h>
32 #include <plotter.h>
33 #include <trigo.h>
34 #include <bezier_curves.h>
35 #include <base_units.h>
36 #include <msgpanel.h>
37 
38 #include <general.h>
39 #include <lib_bezier.h>
40 #include <transform.h>
41 
42 
44  LIB_ITEM( LIB_BEZIER_T, aParent )
45 {
46  m_Fill = NO_FILL;
47  m_Width = 0;
48  m_isFillable = true;
49 }
50 
51 
53 {
54  return new LIB_BEZIER( *this );
55 }
56 
57 
58 int LIB_BEZIER::compare( const LIB_ITEM& aOther ) const
59 {
60  wxASSERT( aOther.Type() == LIB_BEZIER_T );
61 
62  const LIB_BEZIER* tmp = ( LIB_BEZIER* ) &aOther;
63 
64  if( m_BezierPoints.size() != tmp->m_BezierPoints.size() )
65  return m_BezierPoints.size() - tmp->m_BezierPoints.size();
66 
67  for( size_t i = 0; i < m_BezierPoints.size(); i++ )
68  {
69  if( m_BezierPoints[i].x != tmp->m_BezierPoints[i].x )
70  return m_BezierPoints[i].x - tmp->m_BezierPoints[i].x;
71 
72  if( m_BezierPoints[i].y != tmp->m_BezierPoints[i].y )
73  return m_BezierPoints[i].y - tmp->m_BezierPoints[i].y;
74  }
75 
76  return 0;
77 }
78 
79 
80 void LIB_BEZIER::SetOffset( const wxPoint& aOffset )
81 {
82  size_t i;
83 
84  for( i = 0; i < m_BezierPoints.size(); i++ )
85  m_BezierPoints[i] += aOffset;
86 
87  for( i = 0; i < m_PolyPoints.size(); i++ )
88  m_PolyPoints[i] += aOffset;
89 }
90 
91 
92 bool LIB_BEZIER::Inside( EDA_RECT& aRect ) const
93 {
94  for( size_t i = 0; i < m_PolyPoints.size(); i++ )
95  {
96  if( aRect.Contains( m_PolyPoints[i].x, -m_PolyPoints[i].y ) )
97  return true;
98  }
99 
100  return false;
101 }
102 
103 
104 void LIB_BEZIER::Move( const wxPoint& aPosition )
105 {
106  SetOffset( aPosition - m_PolyPoints[0] );
107 }
108 
109 
110 void LIB_BEZIER::MirrorHorizontal( const wxPoint& aCenter )
111 {
112  size_t i, imax = m_PolyPoints.size();
113 
114  for( i = 0; i < imax; i++ )
115  {
116  m_PolyPoints[i].x -= aCenter.x;
117  m_PolyPoints[i].x *= -1;
118  m_PolyPoints[i].x += aCenter.x;
119  }
120 
121  imax = m_BezierPoints.size();
122 
123  for( i = 0; i < imax; i++ )
124  {
125  m_BezierPoints[i].x -= aCenter.x;
126  m_BezierPoints[i].x *= -1;
127  m_BezierPoints[i].x += aCenter.x;
128  }
129 }
130 
131 void LIB_BEZIER::MirrorVertical( const wxPoint& aCenter )
132 {
133  size_t i, imax = m_PolyPoints.size();
134 
135  for( i = 0; i < imax; i++ )
136  {
137  m_PolyPoints[i].y -= aCenter.y;
138  m_PolyPoints[i].y *= -1;
139  m_PolyPoints[i].y += aCenter.y;
140  }
141 
142  imax = m_BezierPoints.size();
143 
144  for( i = 0; i < imax; i++ )
145  {
146  m_BezierPoints[i].y -= aCenter.y;
147  m_BezierPoints[i].y *= -1;
148  m_BezierPoints[i].y += aCenter.y;
149  }
150 }
151 
152 void LIB_BEZIER::Rotate( const wxPoint& aCenter, bool aRotateCCW )
153 {
154  int rot_angle = aRotateCCW ? -900 : 900;
155 
156  size_t i, imax = m_PolyPoints.size();
157 
158  for( i = 0; i < imax; i++ )
159  {
160  RotatePoint( &m_PolyPoints[i], aCenter, rot_angle );
161  }
162 
163  imax = m_BezierPoints.size();
164 
165  for( i = 0; i < imax; i++ )
166  {
167  RotatePoint( &m_BezierPoints[i], aCenter, rot_angle );
168  }
169 }
170 
171 
172 void LIB_BEZIER::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
173  const TRANSFORM& aTransform )
174 {
175  wxASSERT( aPlotter != NULL );
176 
177  static std::vector< wxPoint > cornerList;
178  cornerList.clear();
179 
180  for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
181  {
182  wxPoint pos = m_PolyPoints[ii];
183  pos = aTransform.TransformCoordinate( pos ) + aOffset;
184  cornerList.push_back( pos );
185  }
186 
187  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
188  {
190  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
191  }
192 
193  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
194  aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
195  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, GetPenSize() );
196 }
197 
198 
200 {
201  return ( m_Width == 0 ) ? GetDefaultLineThickness() : m_Width;
202 }
203 
204 
205 void LIB_BEZIER::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
206  COLOR4D aColor, GR_DRAWMODE aDrawMode, void* aData,
207  const TRANSFORM& aTransform )
208 {
209  std::vector<wxPoint> PolyPointsTraslated;
210 
212  BEZIER_POLY converter( m_BezierPoints );
213  converter.GetPoly( m_PolyPoints );
214 
215  PolyPointsTraslated.clear();
216 
217  for( unsigned int i = 0; i < m_PolyPoints.size() ; i++ )
218  PolyPointsTraslated.push_back( aTransform.TransformCoordinate( m_PolyPoints[i] ) +
219  aOffset );
220 
221  if( aColor == COLOR4D::UNSPECIFIED ) // Used normal color or selected color
222  {
223  if( IsSelected() )
224  color = GetItemSelectedColor();
225  }
226  else
227  {
228  color = aColor;
229  }
230 
231  FILL_T fill = aData ? NO_FILL : m_Fill;
232 
233  if( aColor != COLOR4D::UNSPECIFIED )
234  fill = NO_FILL;
235 
236  GRSetDrawMode( aDC, aDrawMode );
237  EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
238 
239  if( fill == FILLED_WITH_BG_BODYCOLOR )
240  GRPoly( clipbox, aDC, m_PolyPoints.size(),
241  &PolyPointsTraslated[0], 1, GetPenSize(),
244  else if( fill == FILLED_SHAPE )
245  GRPoly( clipbox, aDC, m_PolyPoints.size(),
246  &PolyPointsTraslated[0], 1, GetPenSize(), color, color );
247  else
248  GRPoly( clipbox, aDC, m_PolyPoints.size(),
249  &PolyPointsTraslated[0], 0, GetPenSize(), color, color );
250 
251  /* Set to one (1) to draw bounding box around bezier curve to validate
252  * bounding box calculation. */
253 #if 0
254  EDA_RECT bBox = GetBoundingBox();
255  GRRect( aPanel->GetClipBox(), aDC, bBox.GetOrigin().x, bBox.GetOrigin().y,
256  bBox.GetEnd().x, bBox.GetEnd().y, 0, LIGHTMAGENTA );
257 #endif
258 }
259 
260 
261 bool LIB_BEZIER::HitTest( const wxPoint& aRefPos ) const
262 {
263  int mindist = GetPenSize() / 2;
264 
265  // Have a minimal tolerance for hit test
266  if ( mindist < MINIMUM_SELECTION_DISTANCE )
267  mindist = MINIMUM_SELECTION_DISTANCE;
268 
269  return HitTest( aRefPos, mindist, DefaultTransform );
270 }
271 
272 
273 bool LIB_BEZIER::HitTest( const wxPoint &aPosRef, int aThreshold, const TRANSFORM& aTransform ) const
274 {
275  wxPoint start, end;
276 
277  if( aThreshold < 0 )
278  aThreshold = GetPenSize() / 2;
279 
280  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
281  {
282  start = aTransform.TransformCoordinate( m_PolyPoints[ii - 1] );
283  end = aTransform.TransformCoordinate( m_PolyPoints[ii] );
284 
285  if ( TestSegmentHit( aPosRef, start, end, aThreshold ) )
286  return true;
287  }
288 
289  return false;
290 }
291 
292 
294 {
295  EDA_RECT rect;
296  int xmin, xmax, ymin, ymax;
297 
298  if( !GetCornerCount() )
299  return rect;
300 
301  xmin = xmax = m_PolyPoints[0].x;
302  ymin = ymax = m_PolyPoints[0].y;
303 
304  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
305  {
306  xmin = std::min( xmin, m_PolyPoints[ii].x );
307  xmax = std::max( xmax, m_PolyPoints[ii].x );
308  ymin = std::min( ymin, m_PolyPoints[ii].y );
309  ymax = std::max( ymax, m_PolyPoints[ii].y );
310  }
311 
312  rect.SetOrigin( xmin, ymin );
313  rect.SetEnd( xmax, ymax );
314  rect.Inflate( ( GetPenSize()+1 ) / 2 );
315 
316  rect.RevertYAxis();
317 
318  return rect;
319 }
320 
321 
322 void LIB_BEZIER::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
323 {
324  wxString msg;
325  EDA_RECT bBox = GetBoundingBox();
326 
327  LIB_ITEM::GetMsgPanelInfo( aList );
328 
329  msg = StringFromValue( g_UserUnit, m_Width, true );
330 
331  aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
332 
333  msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
334  bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
335 
336  aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
337 }
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:58
KICAD_T Type() const
Function Type()
Definition: base_struct.h:209
void SetOffset(const wxPoint &aOffset) override
Set the drawing object by aOffset from the current position.
Definition: lib_bezier.cpp:80
void GRPoly(EDA_RECT *ClipBox, wxDC *DC, int n, wxPoint Points[], bool Fill, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:796
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:318
Implementation of conversion functions that require both schematic and board internal units...
virtual void SetColor(COLOR4D color)=0
void Move(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_bezier.cpp:104
LIB_BEZIER(LIB_PART *aParent)
Definition: lib_bezier.cpp:43
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_draw_item.h:52
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:205
int color
Definition: DXF_plotter.cpp:62
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
Display basic info (type, part and convert) about the current item in message panel.
Definition: lib_bezier.cpp:322
void GetPoly(std::vector< wxPoint > &aOutput)
Converts Bezier curve to a polygon.
const EDA_RECT GetBoundingBox() const override
Definition: lib_bezier.cpp:293
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:172
bool IsSelected() const
Definition: base_struct.h:232
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:124
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
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:167
void RevertYAxis()
Function RevertYAxis Mirror the rectangle from the X axis (negate Y pos and size) ...
Definition: eda_rect.h:144
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:66
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
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1098
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_bezier.cpp:110
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
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:92
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:37
void SetEnd(int x, int y)
Definition: eda_rect.h:134
std::vector< wxPoint > m_PolyPoints
Definition: lib_bezier.h:43
bool m_isFillable
Flag to indicate if draw item is fillable.
Define a library symbol object.
EDA_RECT * GetClipBox()
unsigned GetCornerCount() const
Definition: lib_bezier.h:73
virtual void Rotate()
Rotate the draw item.
const wxPoint GetEnd() const
Definition: eda_rect.h:114
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_bezier.cpp:52
int GetPenSize() const override
Definition: lib_bezier.cpp:199
Base plotter engine class.
Definition: plotter.h:96
Bezier curves to polygon converter.
Definition: bezier_curves.h:34
virtual void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Display basic info (type, part and convert) about the current item in message panel.
TRANSFORM DefaultTransform
Definition: eeschema.cpp:58
#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:261
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:186
size_t i
Definition: json11.cpp:597
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
void drawGraphic(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, COLOR4D aColor, GR_DRAWMODE aDrawMode, void *aData, const TRANSFORM &aTransform) override
Draw the item on aPanel.
Definition: lib_bezier.cpp:205
void MirrorVertical(const wxPoint &aCenter) override
Mirror the draw object along the MirrorVertical (Y) axis about aCenter point.
Definition: lib_bezier.cpp:131
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:162
FILL_T
Enum FILL_T is the set of fill types used in plotting or drawing enclosed areas.
Definition: base_struct.h:52
COLOR4D GetItemSelectedColor()
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:122
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:108
Definition: colors.h:62