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.

456 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 ) const
  168. {
  169. int mindist = GetPenSize() / 2;
  170. // Have a minimal tolerance for hit test
  171. if( mindist < MINIMUM_SELECTION_DISTANCE )
  172. mindist = MINIMUM_SELECTION_DISTANCE;
  173. return HitTest( aPosition, mindist, DefaultTransform );
  174. }
  175. bool LIB_POLYLINE::HitTest( const wxPoint &aPosition, int aThreshold, const TRANSFORM& aTransform ) const
  176. {
  177. wxPoint start, end;
  178. if( aThreshold < 0 )
  179. aThreshold = GetPenSize() / 2;
  180. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  181. {
  182. start = aTransform.TransformCoordinate( m_PolyPoints[ii - 1] );
  183. end = aTransform.TransformCoordinate( m_PolyPoints[ii] );
  184. if( TestSegmentHit( aPosition, start, end, aThreshold ) )
  185. return true;
  186. }
  187. return false;
  188. }
  189. const EDA_RECT LIB_POLYLINE::GetBoundingBox() const
  190. {
  191. EDA_RECT rect;
  192. int xmin, xmax, ymin, ymax;
  193. xmin = xmax = m_PolyPoints[0].x;
  194. ymin = ymax = m_PolyPoints[0].y;
  195. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  196. {
  197. xmin = std::min( xmin, m_PolyPoints[ii].x );
  198. xmax = std::max( xmax, m_PolyPoints[ii].x );
  199. ymin = std::min( ymin, m_PolyPoints[ii].y );
  200. ymax = std::max( ymax, m_PolyPoints[ii].y );
  201. }
  202. rect.SetOrigin( xmin, ymin );
  203. rect.SetEnd( xmax, ymax );
  204. rect.Inflate( ( GetPenSize()+1 ) / 2 );
  205. rect.RevertYAxis();
  206. return rect;
  207. }
  208. void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
  209. {
  210. // First segment is kept, only its end point is changed
  211. while( GetCornerCount() > 2 )
  212. {
  213. m_PolyPoints.pop_back();
  214. if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
  215. {
  216. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  217. break;
  218. }
  219. }
  220. }
  221. void LIB_POLYLINE::GetMsgPanelInfo( EDA_UNITS_T aUnits, MSG_PANEL_ITEMS& aList )
  222. {
  223. wxString msg;
  224. EDA_RECT bBox = GetBoundingBox();
  225. LIB_ITEM::GetMsgPanelInfo( aUnits, aList );
  226. msg = MessageTextFromValue( aUnits, m_Width, true );
  227. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
  228. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  229. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  230. aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
  231. }
  232. wxString LIB_POLYLINE::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  233. {
  234. return wxString::Format( _( "Polyline at (%s, %s) with %d points" ),
  235. MessageTextFromValue( aUnits, m_PolyPoints[0].x ),
  236. MessageTextFromValue( aUnits, m_PolyPoints[0].y ),
  237. int( m_PolyPoints.size() ) );
  238. }
  239. BITMAP_DEF LIB_POLYLINE::GetMenuImage() const
  240. {
  241. return add_polygon_xpm;
  242. }
  243. void LIB_POLYLINE::BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aPosition )
  244. {
  245. wxCHECK_RET( ( aEditMode & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  246. wxT( "Invalid edit mode for LIB_POLYLINE object." ) );
  247. if( aEditMode == IS_NEW )
  248. {
  249. m_PolyPoints.push_back( aPosition ); // Start point of first segment.
  250. m_PolyPoints.push_back( aPosition ); // End point of first segment.
  251. }
  252. else if( aEditMode == IS_RESIZED )
  253. {
  254. // Drag one edge point of the polyline
  255. // Find the nearest edge point to be dragged
  256. wxPoint startPoint = m_PolyPoints[0];
  257. // Begin with the first list point as nearest point
  258. int index = 0;
  259. m_ModifyIndex = 0;
  260. m_initialPos = startPoint;
  261. // First distance is the current minimum distance
  262. int distanceMin = (aPosition - startPoint).x * (aPosition - startPoint).x
  263. + (aPosition - startPoint).y * (aPosition - startPoint).y;
  264. wxPoint prevPoint = startPoint;
  265. // Find the right index of the point to be dragged
  266. for( wxPoint point : m_PolyPoints )
  267. {
  268. int distancePoint = (aPosition - point).x * (aPosition - point).x +
  269. (aPosition - point).y * (aPosition - point).y;
  270. if( distancePoint < distanceMin )
  271. {
  272. // Save point.
  273. m_initialPos = point;
  274. m_ModifyIndex = index;
  275. distanceMin = distancePoint;
  276. }
  277. // check middle of an edge
  278. wxPoint offset = ( aPosition + aPosition - point - prevPoint );
  279. distancePoint = ( offset.x * offset.x + offset.y * offset.y ) / 4 + 1;
  280. if( distancePoint < distanceMin )
  281. {
  282. // Save point.
  283. m_initialPos = point;
  284. m_ModifyIndex = -index; // negative indicates new vertex is to be inserted
  285. distanceMin = distancePoint;
  286. }
  287. prevPoint = point;
  288. index++;
  289. }
  290. }
  291. else if( aEditMode == IS_MOVED )
  292. {
  293. m_initialCursorPos = aPosition;
  294. m_initialPos = m_PolyPoints[0];
  295. }
  296. m_Flags = aEditMode;
  297. }
  298. bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
  299. {
  300. wxCHECK_MSG( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0, false,
  301. wxT( "Bad call to ContinueEdit(). LIB_POLYLINE is not being edited." ) );
  302. if( m_Flags == IS_NEW )
  303. {
  304. // do not add zero length segments
  305. if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
  306. m_PolyPoints.push_back( aPosition );
  307. return true;
  308. }
  309. return false;
  310. }
  311. void LIB_POLYLINE::EndEdit( const wxPoint& aPosition, bool aAbort )
  312. {
  313. wxCHECK_RET( ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0,
  314. wxT( "Bad call to EndEdit(). LIB_POLYLINE is not being edited." ) );
  315. // do not include last point twice
  316. if( m_Flags == IS_NEW && 2 < m_PolyPoints.size() )
  317. {
  318. if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
  319. m_PolyPoints.pop_back();
  320. }
  321. if( (m_Flags == IS_RESIZED) && (m_PolyPoints.size() > 2) ) // do not delete last two points... keep it alive
  322. {
  323. if( ( m_ModifyIndex > 0 && m_PolyPoints[ m_ModifyIndex ] ==
  324. m_PolyPoints[ m_ModifyIndex - 1 ] )
  325. || ( m_ModifyIndex < (int) m_PolyPoints.size() - 1
  326. && m_PolyPoints[ m_ModifyIndex ] == m_PolyPoints[ m_ModifyIndex + 1 ] ) )
  327. {
  328. m_PolyPoints.erase( m_PolyPoints.begin() + m_ModifyIndex ); // delete a point on this
  329. }
  330. }
  331. m_Flags = 0;
  332. }
  333. void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
  334. {
  335. if( m_Flags == IS_NEW )
  336. {
  337. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  338. }
  339. else if( m_Flags == IS_RESIZED )
  340. {
  341. if( m_ModifyIndex < 0 ) // negative indicates new vertex is to be inserted
  342. {
  343. m_ModifyIndex = -m_ModifyIndex;
  344. m_PolyPoints.insert( m_PolyPoints.begin() + m_ModifyIndex, aPosition );
  345. }
  346. m_PolyPoints[ m_ModifyIndex ] = aPosition;
  347. }
  348. else if( m_Flags == IS_MOVED )
  349. {
  350. Move( m_initialPos + aPosition - m_initialCursorPos );
  351. }
  352. }