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-2020 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>
38 
39 
41  LIB_ITEM( LIB_BEZIER_T, aParent )
42 {
43  m_Fill = NO_FILL;
44  m_Width = 0;
45  m_isFillable = true;
46 }
47 
48 
50 {
51  return new LIB_BEZIER( *this );
52 }
53 
54 
55 int LIB_BEZIER::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
56 {
57  wxASSERT( aOther.Type() == LIB_BEZIER_T );
58 
59  int retv = LIB_ITEM::compare( aOther );
60 
61  if( retv )
62  return retv;
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::Offset( 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::MoveTo( const wxPoint& aPosition )
107 {
108  if ( !m_PolyPoints.size() )
109  m_PolyPoints.emplace_back(0, 0 );
110 
111  Offset( aPosition - m_PolyPoints[ 0 ] );
112 }
113 
115 {
116  if ( !m_PolyPoints.size() )
117  return wxPoint(0, 0);
118 
119  return m_PolyPoints[0];
120 }
121 
122 void LIB_BEZIER::MirrorHorizontal( const wxPoint& aCenter )
123 {
124  size_t i, imax = m_PolyPoints.size();
125 
126  for( i = 0; i < imax; i++ )
127  {
128  m_PolyPoints[i].x -= aCenter.x;
129  m_PolyPoints[i].x *= -1;
130  m_PolyPoints[i].x += aCenter.x;
131  }
132 
133  imax = m_BezierPoints.size();
134 
135  for( i = 0; i < imax; i++ )
136  {
137  m_BezierPoints[i].x -= aCenter.x;
138  m_BezierPoints[i].x *= -1;
139  m_BezierPoints[i].x += aCenter.x;
140  }
141 }
142 
143 void LIB_BEZIER::MirrorVertical( const wxPoint& aCenter )
144 {
145  size_t i, imax = m_PolyPoints.size();
146 
147  for( i = 0; i < imax; i++ )
148  {
149  m_PolyPoints[i].y -= aCenter.y;
150  m_PolyPoints[i].y *= -1;
151  m_PolyPoints[i].y += aCenter.y;
152  }
153 
154  imax = m_BezierPoints.size();
155 
156  for( i = 0; i < imax; i++ )
157  {
158  m_BezierPoints[i].y -= aCenter.y;
159  m_BezierPoints[i].y *= -1;
160  m_BezierPoints[i].y += aCenter.y;
161  }
162 }
163 
164 void LIB_BEZIER::Rotate( const wxPoint& aCenter, bool aRotateCCW )
165 {
166  int rot_angle = aRotateCCW ? -900 : 900;
167 
168  for( wxPoint& point : m_PolyPoints )
169  RotatePoint( &point, aCenter, rot_angle );
170 
171  for( wxPoint& bezierPoint : m_BezierPoints )
172  RotatePoint( &bezierPoint, aCenter, rot_angle );
173 }
174 
175 
176 void LIB_BEZIER::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
177  const TRANSFORM& aTransform )
178 {
179  wxASSERT( aPlotter != NULL );
180 
181  static std::vector< wxPoint > cornerList;
182  cornerList.clear();
183 
184  for( wxPoint pos : m_PolyPoints )
185  {
186  pos = aTransform.TransformCoordinate( pos ) + aOffset;
187  cornerList.push_back( pos );
188  }
189 
190  if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
191  {
192  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE_BACKGROUND ) );
193  aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
194  }
195 
196  bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
197  auto pen_size = GetPenSize();
198 
199  if( !already_filled || pen_size > 0 )
200  {
201  pen_size = std::max( 0, pen_size );
202  aPlotter->SetColor( aPlotter->ColorSettings()->GetColor( LAYER_DEVICE ) );
203  aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
204  }
205 }
206 
207 
209 {
210  if( m_Width > 0 )
211  return m_Width;
212 
213  if( m_Width == 0 )
214  return GetDefaultLineThickness();
215 
216  return -1; // a value to use a minimal pen size
217 }
218 
219 
220 void LIB_BEZIER::print( wxDC* aDC, const wxPoint& aOffset, void* aData,
221  const TRANSFORM& aTransform )
222 {
223  std::vector<wxPoint> PolyPointsTraslated;
224 
227  BEZIER_POLY converter( m_BezierPoints );
228  converter.GetPoly( m_PolyPoints );
229 
230  PolyPointsTraslated.clear();
231 
232  for( wxPoint& point : m_PolyPoints )
233  PolyPointsTraslated.push_back( aTransform.TransformCoordinate( point ) + aOffset );
234 
235  FILL_T fill = aData ? NO_FILL : m_Fill;
236 
237  if( fill == FILLED_WITH_BG_BODYCOLOR )
238  {
239  GRPoly( nullptr, aDC, m_PolyPoints.size(), &PolyPointsTraslated[0], true, GetPenSize(),
240  bgColor, bgColor );
241  }
242  else if( fill == FILLED_SHAPE )
243  {
244  GRPoly( nullptr, aDC, m_PolyPoints.size(), &PolyPointsTraslated[0], true, GetPenSize(),
245  color, color );
246  }
247  else
248  {
249  GRPoly( nullptr, aDC, m_PolyPoints.size(), &PolyPointsTraslated[0], false, GetPenSize(),
250  color, color );
251  }
252 }
253 
254 
255 bool LIB_BEZIER::HitTest( const wxPoint& aRefPos, int aAccuracy ) const
256 {
257  int mindist = std::max( aAccuracy + GetPenSize() / 2,
258  Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
259  wxPoint start, end;
260 
261  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
262  {
265 
266  if ( TestSegmentHit( aRefPos, start, end, mindist ) )
267  return true;
268  }
269 
270  return false;
271 }
272 
273 
274 bool LIB_BEZIER::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
275 {
276  if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
277  return false;
278 
279  EDA_RECT sel = aRect;
280 
281  if ( aAccuracy )
282  sel.Inflate( aAccuracy );
283 
284  if( aContained )
285  return sel.Contains( GetBoundingBox() );
286 
287  // Fast test: if aRect is outside the polygon bounding box, rectangles cannot intersect
288  if( !sel.Intersects( GetBoundingBox() ) )
289  return false;
290 
291  // Account for the width of the line
292  sel.Inflate( GetWidth() / 2 );
293  unsigned count = m_BezierPoints.size();
294 
295  for( unsigned ii = 1; ii < count; ii++ )
296  {
299 
300  // Test if the point is within aRect
301  if( sel.Contains( vertex ) )
302  return true;
303 
304  // Test if this edge intersects aRect
305  if( sel.Intersects( vertex, vertexNext ) )
306  return true;
307  }
308 
309  return false;
310 }
311 
312 
314 {
315  EDA_RECT rect;
316  int xmin, xmax, ymin, ymax;
317 
318  if( !GetCornerCount() )
319  return rect;
320 
321  xmin = xmax = m_PolyPoints[0].x;
322  ymin = ymax = m_PolyPoints[0].y;
323 
324  for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
325  {
326  xmin = std::min( xmin, m_PolyPoints[ii].x );
327  xmax = std::max( xmax, m_PolyPoints[ii].x );
328  ymin = std::min( ymin, m_PolyPoints[ii].y );
329  ymax = std::max( ymax, m_PolyPoints[ii].y );
330  }
331 
332  rect.SetOrigin( xmin, ymin );
333  rect.SetEnd( xmax, ymax );
334  rect.Inflate( ( GetPenSize()+1 ) / 2 );
335 
336  rect.RevertYAxis();
337 
338  return rect;
339 }
340 
341 
342 void LIB_BEZIER::GetMsgPanelInfo( EDA_UNITS aUnits, std::vector<MSG_PANEL_ITEM>& aList )
343 {
344  wxString msg;
345  EDA_RECT bBox = GetBoundingBox();
346 
347  LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
348 
349  msg = MessageTextFromValue( aUnits, m_Width, true );
350 
351  aList.emplace_back( _( "Line Width" ), msg, BLUE );
352 
353  msg.Printf( wxT( "(%d, %d, %d, %d)" ),
354  bBox.GetOrigin().x,
355  bBox.GetOrigin().y,
356  bBox.GetEnd().x,
357  bBox.GetEnd().y );
358 
359  aList.emplace_back( _( "Bounding Box" ), msg, BROWN );
360 }
361 
363 {
364  if( !m_PolyPoints.size() )
365  return wxPoint(0, 0);
366 
367  return m_PolyPoints[0];
368 }
Definition: colors.h:57
EDA_UNITS
Definition: common.h:184
void MoveTo(const wxPoint &aPosition) override
Move a draw object to aPosition.
Definition: lib_bezier.cpp:106
const wxPoint GetOffset() const
Definition: lib_bezier.cpp:114
void GetMsgPanelInfo(EDA_UNITS 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
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:40
std::vector< wxPoint > m_BezierPoints
Definition: lib_bezier.h:37
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.
void GRPoly(EDA_RECT *ClipBox, wxDC *DC, int n, const wxPoint *Points, bool Fill, int width, COLOR4D Color, COLOR4D BgColor)
Definition: gr_basic.cpp:542
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:131
int color
Definition: DXF_plotter.cpp:61
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:313
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:255
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:176
TRANSFORM DefaultTransform
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
void GetMsgPanelInfo(EDA_UNITS 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:342
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=NULL)=0
Function PlotPoly.
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:362
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:42
The base class for drawable items used by schematic library components.
Definition: lib_item.h:60
This file contains miscellaneous commonly used macros and functions.
bool TestSegmentHit(const wxPoint &aRefPoint, wxPoint aStart, wxPoint aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition: trigo.cpp:129
int GetWidth() const override
Definition: lib_bezier.h:91
unsigned GetCornerCount() const
Definition: lib_bezier.h:69
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:82
void MirrorHorizontal(const wxPoint &aCenter) override
Mirror the draw object along the horizontal (X) axis about aCenter point.
Definition: lib_bezier.cpp:122
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:164
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
void print(wxDC *aDC, const wxPoint &aOffset, void *aData, const TRANSFORM &aTransform) override
Print the item to aDC.
Definition: lib_bezier.cpp:220
#define NULL
COMPARE_FLAGS
The list of flags used by the compare function.
Definition: lib_item.h:116
COLOR_SETTINGS * ColorSettings()
Definition: plotter.h:145
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:38
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.
Definition: lib_bezier.cpp:55
#define MINIMUM_SELECTION_DISTANCE
Definition: lib_item.h:46
bool m_isFillable
Definition: lib_item.h:94
Define a library symbol object.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Helper for all the old plotting/printing code while it still exists.
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:129
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: lib_bezier.cpp:49
int GetPenSize() const override
Definition: lib_bezier.cpp:208
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:127
Base plotter engine class.
Definition: plotter.h:104
COLOR4D GetColor(int aLayer) const
Bezier curves to polygon converter.
Definition: bezier_curves.h:35
#define _(s)
Definition: 3d_actions.cpp:33
FILL_T m_Fill
The body fill type.
Definition: lib_item.h:93
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:189
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:143
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
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
int m_Width
Definition: lib_bezier.h:36
Message panel definition file.
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:76
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:212
Define a bezier curve graphic body item.
Definition: lib_bezier.h:34
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40
Definition: colors.h:62