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.

594 lines
17 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-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. * Copyright (C) 2019 CERN
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <sch_draw_panel.h>
  26. #include <plotter.h>
  27. #include <trigo.h>
  28. #include <base_units.h>
  29. #include <widgets/msgpanel.h>
  30. #include <bitmaps.h>
  31. #include <math/util.h> // for KiROUND
  32. #include <eda_draw_frame.h>
  33. #include <general.h>
  34. #include <lib_arc.h>
  35. #include <transform.h>
  36. #include <settings/color_settings.h>
  37. #include <status_popup.h>
  38. // Helper function
  39. static inline wxPoint twoPointVector( const wxPoint &startPoint, const wxPoint &endPoint )
  40. {
  41. return endPoint - startPoint;
  42. }
  43. LIB_ARC::LIB_ARC( LIB_PART* aParent ) : LIB_ITEM( LIB_ARC_T, aParent )
  44. {
  45. m_Radius = 0;
  46. m_t1 = 0;
  47. m_t2 = 0;
  48. m_Width = 0;
  49. m_fill = FILL_TYPE::NO_FILL;
  50. m_isFillable = true;
  51. m_editState = 0;
  52. }
  53. bool LIB_ARC::HitTest( const wxPoint& aRefPoint, int aAccuracy ) const
  54. {
  55. int mindist = std::max( aAccuracy + GetPenWidth() / 2,
  56. Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
  57. wxPoint relativePosition = aRefPoint;
  58. relativePosition.y = -relativePosition.y; // reverse Y axis
  59. int distance = KiROUND( GetLineLength( m_Pos, relativePosition ) );
  60. if( abs( distance - m_Radius ) > mindist )
  61. return false;
  62. // We are on the circle, ensure we are only on the arc, i.e. between
  63. // m_ArcStart and m_ArcEnd
  64. wxPoint startEndVector = twoPointVector( m_ArcStart, m_ArcEnd );
  65. wxPoint startRelativePositionVector = twoPointVector( m_ArcStart, relativePosition );
  66. wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
  67. wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
  68. wxPoint centerRelativePositionVector = twoPointVector( m_Pos, relativePosition );
  69. // Compute the cross product to check if the point is in the sector
  70. double crossProductStart = CrossProduct( centerStartVector, centerRelativePositionVector );
  71. double crossProductEnd = CrossProduct( centerEndVector, centerRelativePositionVector );
  72. // The cross products need to be exchanged, depending on which side the center point
  73. // relative to the start point to end point vector lies
  74. if( CrossProduct( startEndVector, startRelativePositionVector ) < 0 )
  75. {
  76. std::swap( crossProductStart, crossProductEnd );
  77. }
  78. // When the cross products have a different sign, the point lies in sector
  79. // also check, if the reference is near start or end point
  80. return HitTestPoints( m_ArcStart, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
  81. HitTestPoints( m_ArcEnd, relativePosition, MINIMUM_SELECTION_DISTANCE ) ||
  82. ( crossProductStart <= 0 && crossProductEnd >= 0 );
  83. }
  84. bool LIB_ARC::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  85. {
  86. if( m_flags & (STRUCT_DELETED | SKIP_STRUCT ) )
  87. return false;
  88. wxPoint center = DefaultTransform.TransformCoordinate( GetPosition() );
  89. int radius = GetRadius();
  90. int lineWidth = GetWidth();
  91. EDA_RECT sel = aRect ;
  92. if ( aAccuracy )
  93. sel.Inflate( aAccuracy );
  94. if( aContained )
  95. return sel.Contains( GetBoundingBox() );
  96. EDA_RECT arcRect = GetBoundingBox().Common( sel );
  97. /* All following tests must pass:
  98. * 1. Rectangle must intersect arc BoundingBox
  99. * 2. Rectangle must cross the outside of the arc
  100. */
  101. return arcRect.Intersects( sel ) && arcRect.IntersectsCircleEdge( center, radius, lineWidth );
  102. }
  103. EDA_ITEM* LIB_ARC::Clone() const
  104. {
  105. return new LIB_ARC( *this );
  106. }
  107. int LIB_ARC::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  108. {
  109. wxASSERT( aOther.Type() == LIB_ARC_T );
  110. int retv = LIB_ITEM::compare( aOther );
  111. if( retv )
  112. return retv;
  113. const LIB_ARC* tmp = ( LIB_ARC* ) &aOther;
  114. if( m_Pos.x != tmp->m_Pos.x )
  115. return m_Pos.x - tmp->m_Pos.x;
  116. if( m_Pos.y != tmp->m_Pos.y )
  117. return m_Pos.y - tmp->m_Pos.y;
  118. if( m_t1 != tmp->m_t1 )
  119. return m_t1 - tmp->m_t1;
  120. if( m_t2 != tmp->m_t2 )
  121. return m_t2 - tmp->m_t2;
  122. return 0;
  123. }
  124. void LIB_ARC::Offset( const wxPoint& aOffset )
  125. {
  126. m_Pos += aOffset;
  127. m_ArcStart += aOffset;
  128. m_ArcEnd += aOffset;
  129. }
  130. void LIB_ARC::MoveTo( const wxPoint& aPosition )
  131. {
  132. wxPoint offset = aPosition - m_Pos;
  133. m_Pos = aPosition;
  134. m_ArcStart += offset;
  135. m_ArcEnd += offset;
  136. }
  137. void LIB_ARC::MirrorHorizontal( const wxPoint& aCenter )
  138. {
  139. m_Pos.x -= aCenter.x;
  140. m_Pos.x *= -1;
  141. m_Pos.x += aCenter.x;
  142. m_ArcStart.x -= aCenter.x;
  143. m_ArcStart.x *= -1;
  144. m_ArcStart.x += aCenter.x;
  145. m_ArcEnd.x -= aCenter.x;
  146. m_ArcEnd.x *= -1;
  147. m_ArcEnd.x += aCenter.x;
  148. std::swap( m_ArcStart, m_ArcEnd );
  149. std::swap( m_t1, m_t2 );
  150. m_t1 = 1800 - m_t1;
  151. m_t2 = 1800 - m_t2;
  152. if( m_t1 > 3600 || m_t2 > 3600 )
  153. {
  154. m_t1 -= 3600;
  155. m_t2 -= 3600;
  156. }
  157. else if( m_t1 < -3600 || m_t2 < -3600 )
  158. {
  159. m_t1 += 3600;
  160. m_t2 += 3600;
  161. }
  162. }
  163. void LIB_ARC::MirrorVertical( const wxPoint& aCenter )
  164. {
  165. m_Pos.y -= aCenter.y;
  166. m_Pos.y *= -1;
  167. m_Pos.y += aCenter.y;
  168. m_ArcStart.y -= aCenter.y;
  169. m_ArcStart.y *= -1;
  170. m_ArcStart.y += aCenter.y;
  171. m_ArcEnd.y -= aCenter.y;
  172. m_ArcEnd.y *= -1;
  173. m_ArcEnd.y += aCenter.y;
  174. std::swap( m_ArcStart, m_ArcEnd );
  175. std::swap( m_t1, m_t2 );
  176. m_t1 = - m_t1;
  177. m_t2 = - m_t2;
  178. if( m_t1 > 3600 || m_t2 > 3600 )
  179. {
  180. m_t1 -= 3600;
  181. m_t2 -= 3600;
  182. }
  183. else if( m_t1 < -3600 || m_t2 < -3600 )
  184. {
  185. m_t1 += 3600;
  186. m_t2 += 3600;
  187. }
  188. }
  189. void LIB_ARC::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  190. {
  191. int rot_angle = aRotateCCW ? -900 : 900;
  192. RotatePoint( &m_Pos, aCenter, rot_angle );
  193. RotatePoint( &m_ArcStart, aCenter, rot_angle );
  194. RotatePoint( &m_ArcEnd, aCenter, rot_angle );
  195. m_t1 -= rot_angle;
  196. m_t2 -= rot_angle;
  197. if( m_t1 > 3600 || m_t2 > 3600 )
  198. {
  199. m_t1 -= 3600;
  200. m_t2 -= 3600;
  201. }
  202. else if( m_t1 < -3600 || m_t2 < -3600 )
  203. {
  204. m_t1 += 3600;
  205. m_t2 += 3600;
  206. }
  207. }
  208. void LIB_ARC::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  209. const TRANSFORM& aTransform )
  210. {
  211. wxASSERT( aPlotter != NULL );
  212. int t1 = m_t1;
  213. int t2 = m_t2;
  214. wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  215. aTransform.MapAngles( &t1, &t2 );
  216. if( aFill && m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  217. {
  218. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  219. aPlotter->Arc( pos, -t2, -t1, m_Radius, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
  220. }
  221. bool already_filled = m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
  222. int pen_size = GetPenWidth();
  223. if( !already_filled || pen_size > 0 )
  224. {
  225. pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetMinPenWidth() );
  226. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  227. aPlotter->Arc( pos, -t2, -t1, m_Radius, already_filled ? FILL_TYPE::NO_FILL : m_fill,
  228. pen_size );
  229. }
  230. }
  231. int LIB_ARC::GetPenWidth() const
  232. {
  233. // Historically 0 meant "default width" and negative numbers meant "don't stroke".
  234. if( m_Width < 0 && GetFillMode() != FILL_TYPE::NO_FILL )
  235. return 0;
  236. else
  237. return std::max( m_Width, 1 );
  238. }
  239. void LIB_ARC::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  240. const TRANSFORM& aTransform )
  241. {
  242. bool forceNoFill = static_cast<bool>( aData );
  243. int penWidth = GetPenWidth();
  244. if( forceNoFill && m_fill != FILL_TYPE::NO_FILL && penWidth == 0 )
  245. return;
  246. wxDC* DC = aSettings->GetPrintDC();
  247. wxPoint pos1, pos2, posc;
  248. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  249. pos1 = aTransform.TransformCoordinate( m_ArcEnd ) + aOffset;
  250. pos2 = aTransform.TransformCoordinate( m_ArcStart ) + aOffset;
  251. posc = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  252. int pt1 = m_t1;
  253. int pt2 = m_t2;
  254. bool swap = aTransform.MapAngles( &pt1, &pt2 );
  255. if( swap )
  256. {
  257. std::swap( pos1.x, pos2.x );
  258. std::swap( pos1.y, pos2.y );
  259. }
  260. if( forceNoFill || m_fill == FILL_TYPE::NO_FILL )
  261. {
  262. penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
  263. GRArc1( nullptr, DC, pos1.x, pos1.y, pos2.x, pos2.y, posc.x, posc.y, penWidth, color );
  264. }
  265. else
  266. {
  267. if( m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  268. color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  269. GRFilledArc( nullptr, DC, posc.x, posc.y, pt1, pt2, m_Radius, penWidth, color, color );
  270. }
  271. }
  272. const EDA_RECT LIB_ARC::GetBoundingBox() const
  273. {
  274. int minX, minY, maxX, maxY, angleStart, angleEnd;
  275. EDA_RECT rect;
  276. wxPoint nullPoint, startPos, endPos, centerPos;
  277. wxPoint normStart = m_ArcStart - m_Pos;
  278. wxPoint normEnd = m_ArcEnd - m_Pos;
  279. if( ( normStart == nullPoint ) || ( normEnd == nullPoint ) || ( m_Radius == 0 ) )
  280. return rect;
  281. endPos = DefaultTransform.TransformCoordinate( m_ArcEnd );
  282. startPos = DefaultTransform.TransformCoordinate( m_ArcStart );
  283. centerPos = DefaultTransform.TransformCoordinate( m_Pos );
  284. angleStart = m_t1;
  285. angleEnd = m_t2;
  286. if( DefaultTransform.MapAngles( &angleStart, &angleEnd ) )
  287. {
  288. std::swap( endPos.x, startPos.x );
  289. std::swap( endPos.y, startPos.y );
  290. }
  291. /* Start with the start and end point of the arc. */
  292. minX = std::min( startPos.x, endPos.x );
  293. minY = std::min( startPos.y, endPos.y );
  294. maxX = std::max( startPos.x, endPos.x );
  295. maxY = std::max( startPos.y, endPos.y );
  296. /* Zero degrees is a special case. */
  297. if( angleStart == 0 )
  298. maxX = centerPos.x + m_Radius;
  299. /* Arc end angle wrapped passed 360. */
  300. if( angleStart > angleEnd )
  301. angleEnd += 3600;
  302. if( angleStart <= 900 && angleEnd >= 900 ) /* 90 deg */
  303. maxY = centerPos.y + m_Radius;
  304. if( angleStart <= 1800 && angleEnd >= 1800 ) /* 180 deg */
  305. minX = centerPos.x - m_Radius;
  306. if( angleStart <= 2700 && angleEnd >= 2700 ) /* 270 deg */
  307. minY = centerPos.y - m_Radius;
  308. if( angleStart <= 3600 && angleEnd >= 3600 ) /* 0 deg */
  309. maxX = centerPos.x + m_Radius;
  310. rect.SetOrigin( minX, minY );
  311. rect.SetEnd( maxX, maxY );
  312. rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
  313. return rect;
  314. }
  315. void LIB_ARC::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  316. {
  317. wxString msg;
  318. EDA_RECT bBox = GetBoundingBox();
  319. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  320. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width );
  321. aList.emplace_back( _( "Line Width" ), msg );
  322. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  323. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  324. aList.emplace_back( _( "Bounding Box" ), msg );
  325. }
  326. wxString LIB_ARC::GetSelectMenuText( EDA_UNITS aUnits ) const
  327. {
  328. return wxString::Format( _( "Arc, radius %s" ),
  329. MessageTextFromValue( aUnits, m_Radius ) );
  330. }
  331. BITMAP_DEF LIB_ARC::GetMenuImage() const
  332. {
  333. return add_arc_xpm;
  334. }
  335. void LIB_ARC::BeginEdit( const wxPoint aPosition )
  336. {
  337. m_ArcStart = m_ArcEnd = aPosition;
  338. m_editState = 1;
  339. }
  340. void LIB_ARC::CalcEdit( const wxPoint& aPosition )
  341. {
  342. #define sq( x ) pow( x, 2 )
  343. // Edit state 0: drawing: place ArcStart
  344. // Edit state 1: drawing: place ArcEnd (center calculated for 90-degree subtended angle)
  345. // Edit state 2: point editing: move ArcStart (center calculated for invariant subtended angle)
  346. // Edit state 3: point editing: move ArcEnd (center calculated for invariant subtended angle)
  347. // Edit state 4: point editing: move center
  348. switch( m_editState )
  349. {
  350. case 0:
  351. m_ArcStart = aPosition;
  352. m_ArcEnd = aPosition;
  353. m_Pos = aPosition;
  354. m_Radius = 0;
  355. m_t1 = 0;
  356. m_t2 = 0;
  357. return;
  358. case 1:
  359. m_ArcEnd = aPosition;
  360. m_Radius = KiROUND( sqrt( pow( GetLineLength( m_ArcStart, m_ArcEnd ), 2 ) / 2.0 ) );
  361. break;
  362. case 2:
  363. case 3:
  364. {
  365. wxPoint v = m_ArcStart - m_ArcEnd;
  366. double chordBefore = sq( v.x ) + sq( v.y );
  367. if( m_editState == 2 )
  368. m_ArcStart = aPosition;
  369. else
  370. m_ArcEnd = aPosition;
  371. v = m_ArcStart - m_ArcEnd;
  372. double chordAfter = sq( v.x ) + sq( v.y );
  373. double ratio = chordAfter / chordBefore;
  374. if( ratio > 0 )
  375. {
  376. m_Radius = int( sqrt( m_Radius * m_Radius * ratio ) ) + 1;
  377. m_Radius = std::max( m_Radius, int( sqrt( chordAfter ) / 2 ) + 1 );
  378. }
  379. break;
  380. }
  381. case 4:
  382. {
  383. double chordA = GetLineLength( m_ArcStart, aPosition );
  384. double chordB = GetLineLength( m_ArcEnd, aPosition );
  385. m_Radius = int( ( chordA + chordB ) / 2.0 ) + 1;
  386. break;
  387. }
  388. }
  389. // Calculate center based on start, end, and radius
  390. //
  391. // Let 'l' be the length of the chord and 'm' the middle point of the chord
  392. double l = GetLineLength( m_ArcStart, m_ArcEnd );
  393. wxPoint m = ( m_ArcStart + m_ArcEnd ) / 2;
  394. // Calculate 'd', the vector from the chord midpoint to the center
  395. wxPoint d;
  396. d.x = KiROUND( sqrt( sq( m_Radius ) - sq( l/2 ) ) * ( m_ArcStart.y - m_ArcEnd.y ) / l );
  397. d.y = KiROUND( sqrt( sq( m_Radius ) - sq( l/2 ) ) * ( m_ArcEnd.x - m_ArcStart.x ) / l );
  398. wxPoint c1 = m + d;
  399. wxPoint c2 = m - d;
  400. // Solution gives us 2 centers; we need to pick one:
  401. switch( m_editState )
  402. {
  403. case 1:
  404. {
  405. // Keep center clockwise from chord while drawing
  406. wxPoint chordVector = twoPointVector( m_ArcStart, m_ArcEnd );
  407. double chordAngle = ArcTangente( chordVector.y, chordVector.x );
  408. NORMALIZE_ANGLE_POS( chordAngle );
  409. wxPoint c1Test = c1;
  410. RotatePoint( &c1Test, m_ArcStart, -chordAngle );
  411. m_Pos = c1Test.x > 0 ? c2 : c1;
  412. }
  413. break;
  414. case 2:
  415. case 3:
  416. // Pick the one closer to the old center
  417. m_Pos = ( GetLineLength( c1, m_Pos ) < GetLineLength( c2, m_Pos ) ) ? c1 : c2;
  418. break;
  419. case 4:
  420. // Pick the one closer to the mouse position
  421. m_Pos = ( GetLineLength( c1, aPosition ) < GetLineLength( c2, aPosition ) ) ? c1 : c2;
  422. break;
  423. }
  424. CalcRadiusAngles();
  425. }
  426. void LIB_ARC::CalcRadiusAngles()
  427. {
  428. wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
  429. wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
  430. m_Radius = KiROUND( EuclideanNorm( centerStartVector ) );
  431. // Angles in eeschema are still integers
  432. m_t1 = KiROUND( ArcTangente( centerStartVector.y, centerStartVector.x ) );
  433. m_t2 = KiROUND( ArcTangente( centerEndVector.y, centerEndVector.x ) );
  434. NORMALIZE_ANGLE_POS( m_t1 );
  435. NORMALIZE_ANGLE_POS( m_t2 ); // angles = 0 .. 3600
  436. // Restrict angle to less than 180 to avoid PBS display mirror Trace because it is
  437. // assumed that the arc is less than 180 deg to find orientation after rotate or mirror.
  438. if( (m_t2 - m_t1) > 1800 )
  439. m_t2 -= 3600;
  440. else if( (m_t2 - m_t1) <= -1800 )
  441. m_t2 += 3600;
  442. while( (m_t2 - m_t1) >= 1800 )
  443. {
  444. m_t2--;
  445. m_t1++;
  446. }
  447. while( (m_t1 - m_t2) >= 1800 )
  448. {
  449. m_t2++;
  450. m_t1--;
  451. }
  452. NORMALIZE_ANGLE_POS( m_t1 );
  453. if( !IsMoving() )
  454. NORMALIZE_ANGLE_POS( m_t2 );
  455. }
  456. VECTOR2I LIB_ARC::CalcMidPoint() const
  457. {
  458. VECTOR2D midPoint;
  459. double startAngle = static_cast<double>( m_t1 ) / 10.0;
  460. double endAngle = static_cast<double>( m_t2 ) / 10.0;
  461. if( endAngle < startAngle )
  462. endAngle -= 360.0;
  463. double midPointAngle = ( ( endAngle - startAngle ) / 2.0 ) + startAngle;
  464. double x = cos( DEG2RAD( midPointAngle ) ) * m_Radius;
  465. double y = sin( DEG2RAD( midPointAngle ) ) * m_Radius;
  466. midPoint.x = KiROUND( x ) + m_Pos.x;
  467. midPoint.y = KiROUND( y ) + m_Pos.y;
  468. return midPoint;
  469. }