You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
13 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file lib_polyline.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <gr_basic.h>
  29. #include <macros.h>
  30. #include <sch_draw_panel.h>
  31. #include <plotter.h>
  32. #include <trigo.h>
  33. #include <base_units.h>
  34. #include <msgpanel.h>
  35. #include <bitmaps.h>
  36. #include <general.h>
  37. #include <lib_polyline.h>
  38. #include <transform.h>
  39. LIB_POLYLINE::LIB_POLYLINE( LIB_PART* aParent ) :
  40. LIB_ITEM( LIB_POLYLINE_T, aParent )
  41. {
  42. m_Fill = NO_FILL;
  43. m_Width = 0;
  44. m_isFillable = true;
  45. m_ModifyIndex = 0;
  46. }
  47. EDA_ITEM* LIB_POLYLINE::Clone() const
  48. {
  49. return new LIB_POLYLINE( *this );
  50. }
  51. int LIB_POLYLINE::compare( const LIB_ITEM& aOther ) const
  52. {
  53. wxASSERT( aOther.Type() == LIB_POLYLINE_T );
  54. const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
  55. if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
  56. return m_PolyPoints.size() - tmp->m_PolyPoints.size();
  57. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  58. {
  59. if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
  60. return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
  61. if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
  62. return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
  63. }
  64. return 0;
  65. }
  66. void LIB_POLYLINE::SetOffset( const wxPoint& aOffset )
  67. {
  68. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  69. m_PolyPoints[i] += aOffset;
  70. }
  71. bool LIB_POLYLINE::Inside( EDA_RECT& aRect ) const
  72. {
  73. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  74. {
  75. if( aRect.Contains( m_PolyPoints[i].x, -m_PolyPoints[i].y ) )
  76. return true;
  77. }
  78. return false;
  79. }
  80. void LIB_POLYLINE::Move( const wxPoint& aPosition )
  81. {
  82. SetOffset( aPosition - m_PolyPoints[0] );
  83. }
  84. void LIB_POLYLINE::MirrorHorizontal( const wxPoint& aCenter )
  85. {
  86. size_t i, imax = m_PolyPoints.size();
  87. for( i = 0; i < imax; i++ )
  88. {
  89. m_PolyPoints[i].x -= aCenter.x;
  90. m_PolyPoints[i].x *= -1;
  91. m_PolyPoints[i].x += aCenter.x;
  92. }
  93. }
  94. void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
  95. {
  96. size_t i, imax = m_PolyPoints.size();
  97. for( i = 0; i < imax; i++ )
  98. {
  99. m_PolyPoints[i].y -= aCenter.y;
  100. m_PolyPoints[i].y *= -1;
  101. m_PolyPoints[i].y += aCenter.y;
  102. }
  103. }
  104. void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  105. {
  106. int rot_angle = aRotateCCW ? -900 : 900;
  107. size_t i, imax = m_PolyPoints.size();
  108. for( i = 0; i < imax; i++ )
  109. RotatePoint( &m_PolyPoints[i], aCenter, rot_angle );
  110. }
  111. void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  112. const TRANSFORM& aTransform )
  113. {
  114. wxASSERT( aPlotter != NULL );
  115. static std::vector< wxPoint > cornerList;
  116. cornerList.clear();
  117. for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
  118. {
  119. wxPoint pos = m_PolyPoints[ii];
  120. pos = aTransform.TransformCoordinate( pos ) + aOffset;
  121. cornerList.push_back( pos );
  122. }
  123. if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
  124. {
  125. aPlotter->SetColor( GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  126. aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
  127. }
  128. bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
  129. auto pen_size = GetPenSize();
  130. if( !already_filled || pen_size > 0 )
  131. {
  132. pen_size = std::max( 0, pen_size );
  133. aPlotter->SetColor( GetLayerColor( LAYER_DEVICE ) );
  134. aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
  135. }
  136. }
  137. void LIB_POLYLINE::AddPoint( const wxPoint& point )
  138. {
  139. m_PolyPoints.push_back( point );
  140. }
  141. int LIB_POLYLINE::GetPenSize() const
  142. {
  143. if( m_Width > 0 )
  144. return m_Width;
  145. if( m_Width == 0 )
  146. return GetDefaultLineThickness();
  147. return -1; // the minimal pen value
  148. }
  149. void LIB_POLYLINE::drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  150. void* aData, const TRANSFORM& aTransform )
  151. {
  152. COLOR4D color = GetLayerColor( LAYER_DEVICE );
  153. COLOR4D bgColor = GetLayerColor( LAYER_DEVICE_BACKGROUND );
  154. wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
  155. for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
  156. buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
  157. FILL_T fill = aData ? NO_FILL : m_Fill;
  158. EDA_RECT* const clipbox = aPanel? aPanel->GetClipBox() : NULL;
  159. if( fill == FILLED_WITH_BG_BODYCOLOR )
  160. GRPoly( clipbox, aDC, m_PolyPoints.size(), buffer, 1, GetPenSize(), bgColor, bgColor );
  161. else if( fill == FILLED_SHAPE )
  162. GRPoly( clipbox, aDC, m_PolyPoints.size(), buffer, 1, GetPenSize(), color, color );
  163. else
  164. GRPoly( clipbox, aDC, m_PolyPoints.size(), buffer, 0, GetPenSize(), color, color );
  165. delete[] buffer;
  166. }
  167. bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  168. {
  169. int mindist = std::max( aAccuracy + GetPenSize() / 2, MINIMUM_SELECTION_DISTANCE );
  170. wxPoint start, end;
  171. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  172. {
  173. start = DefaultTransform.TransformCoordinate( m_PolyPoints[ii - 1] );
  174. end = DefaultTransform.TransformCoordinate( m_PolyPoints[ii] );
  175. if( TestSegmentHit( aPosition, start, end, mindist ) )
  176. return true;
  177. }
  178. return false;
  179. }
  180. bool LIB_POLYLINE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  181. {
  182. if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
  183. return false;
  184. EDA_RECT rect = aRect;
  185. if ( aAccuracy )
  186. rect.Inflate( aAccuracy );
  187. if( aContained )
  188. return rect.Contains( GetBoundingBox() );
  189. return rect.Intersects( GetBoundingBox() ); // JEY TODO somewhat coarse for filled polylines,
  190. // egregiously coarse for unfilled...
  191. }
  192. const EDA_RECT LIB_POLYLINE::GetBoundingBox() const
  193. {
  194. EDA_RECT rect;
  195. int xmin, xmax, ymin, ymax;
  196. xmin = xmax = m_PolyPoints[0].x;
  197. ymin = ymax = m_PolyPoints[0].y;
  198. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  199. {
  200. xmin = std::min( xmin, m_PolyPoints[ii].x );
  201. xmax = std::max( xmax, m_PolyPoints[ii].x );
  202. ymin = std::min( ymin, m_PolyPoints[ii].y );
  203. ymax = std::max( ymax, m_PolyPoints[ii].y );
  204. }
  205. rect.SetOrigin( xmin, ymin );
  206. rect.SetEnd( xmax, ymax );
  207. rect.Inflate( ( GetPenSize()+1 ) / 2 );
  208. rect.RevertYAxis();
  209. return rect;
  210. }
  211. void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
  212. {
  213. // First segment is kept, only its end point is changed
  214. while( GetCornerCount() > 2 )
  215. {
  216. m_PolyPoints.pop_back();
  217. if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
  218. {
  219. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  220. break;
  221. }
  222. }
  223. }
  224. void LIB_POLYLINE::GetMsgPanelInfo( EDA_UNITS_T aUnits, MSG_PANEL_ITEMS& aList )
  225. {
  226. wxString msg;
  227. EDA_RECT bBox = GetBoundingBox();
  228. LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
  229. msg = MessageTextFromValue( aUnits, m_Width, true );
  230. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
  231. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  232. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  233. aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
  234. }
  235. wxString LIB_POLYLINE::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  236. {
  237. return wxString::Format( _( "Polyline at (%s, %s) with %d points" ),
  238. MessageTextFromValue( aUnits, m_PolyPoints[0].x ),
  239. MessageTextFromValue( aUnits, m_PolyPoints[0].y ),
  240. int( m_PolyPoints.size() ) );
  241. }
  242. BITMAP_DEF LIB_POLYLINE::GetMenuImage() const
  243. {
  244. return add_polygon_xpm;
  245. }
  246. void LIB_POLYLINE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
  247. {
  248. wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  249. wxT( "Invalid edit mode for LIB_POLYLINE object." ) );
  250. if( aEditMode == IS_NEW )
  251. {
  252. m_PolyPoints.push_back( aPosition ); // Start point of first segment.
  253. m_PolyPoints.push_back( aPosition ); // End point of first segment.
  254. }
  255. else if( aEditMode == IS_RESIZED )
  256. {
  257. // Drag one edge point of the polyline
  258. // Find the nearest edge point to be dragged
  259. wxPoint startPoint = m_PolyPoints[0];
  260. // Begin with the first list point as nearest point
  261. int index = 0;
  262. m_ModifyIndex = 0;
  263. m_initialPos = startPoint;
  264. // First distance is the current minimum distance
  265. int distanceMin = (aPosition - startPoint).x * (aPosition - startPoint).x
  266. + (aPosition - startPoint).y * (aPosition - startPoint).y;
  267. wxPoint prevPoint = startPoint;
  268. // Find the right index of the point to be dragged
  269. for( wxPoint point : m_PolyPoints )
  270. {
  271. int distancePoint = (aPosition - point).x * (aPosition - point).x +
  272. (aPosition - point).y * (aPosition - point).y;
  273. if( distancePoint < distanceMin )
  274. {
  275. // Save point.
  276. m_initialPos = point;
  277. m_ModifyIndex = index;
  278. distanceMin = distancePoint;
  279. }
  280. // check middle of an edge
  281. wxPoint offset = ( aPosition + aPosition - point - prevPoint );
  282. distancePoint = ( offset.x * offset.x + offset.y * offset.y ) / 4 + 1;
  283. if( distancePoint < distanceMin )
  284. {
  285. // Save point.
  286. m_initialPos = point;
  287. m_ModifyIndex = -index; // negative indicates new vertex is to be inserted
  288. distanceMin = distancePoint;
  289. }
  290. prevPoint = point;
  291. index++;
  292. }
  293. }
  294. else if( aEditMode == IS_MOVED )
  295. {
  296. m_initialCursorPos = aPosition;
  297. m_initialPos = m_PolyPoints[0];
  298. }
  299. m_Flags = aEditMode;
  300. }
  301. bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
  302. {
  303. wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
  304. wxT( "Bad call to ContinueEdit(). LIB_POLYLINE is not being edited." ) );
  305. if( m_Flags == IS_NEW )
  306. {
  307. // do not add zero length segments
  308. if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
  309. m_PolyPoints.push_back( aPosition );
  310. return true;
  311. }
  312. return false;
  313. }
  314. void LIB_POLYLINE::EndEdit( const wxPoint& aPosition, bool aAbort )
  315. {
  316. wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  317. wxT( "Bad call to EndEdit(). LIB_POLYLINE is not being edited." ) );
  318. // do not include last point twice
  319. if( m_Flags == IS_NEW && 2 < m_PolyPoints.size() )
  320. {
  321. if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
  322. m_PolyPoints.pop_back();
  323. }
  324. if( (m_Flags == IS_RESIZED) && (m_PolyPoints.size() > 2) ) // do not delete last two points... keep it alive
  325. {
  326. if( ( m_ModifyIndex > 0 && m_PolyPoints[ m_ModifyIndex ] ==
  327. m_PolyPoints[ m_ModifyIndex - 1 ] )
  328. || ( m_ModifyIndex < (int) m_PolyPoints.size() - 1
  329. && m_PolyPoints[ m_ModifyIndex ] == m_PolyPoints[ m_ModifyIndex + 1 ] ) )
  330. {
  331. m_PolyPoints.erase( m_PolyPoints.begin() + m_ModifyIndex ); // delete a point on this
  332. }
  333. }
  334. m_Flags = 0;
  335. }
  336. void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
  337. {
  338. if( m_Flags == IS_NEW )
  339. {
  340. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  341. }
  342. else if( m_Flags == IS_RESIZED )
  343. {
  344. if( m_ModifyIndex < 0 ) // negative indicates new vertex is to be inserted
  345. {
  346. m_ModifyIndex = -m_ModifyIndex;
  347. m_PolyPoints.insert( m_PolyPoints.begin() + m_ModifyIndex, aPosition );
  348. }
  349. m_PolyPoints[ m_ModifyIndex ] = aPosition;
  350. }
  351. else if( m_Flags == IS_MOVED )
  352. {
  353. Move( m_initialPos + aPosition - m_initialCursorPos );
  354. }
  355. }