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