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.

603 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-2021 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 <plotters/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_SYMBOL* 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 ) const
  210. {
  211. wxASSERT( aPlotter != nullptr );
  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 = GetEffectivePenWidth( aPlotter->RenderSettings() );
  223. if( !already_filled || pen_size > 0 )
  224. {
  225. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  226. aPlotter->Arc( pos, -t2, -t1, m_Radius, already_filled ? FILL_TYPE::NO_FILL : m_fill,
  227. pen_size );
  228. }
  229. }
  230. int LIB_ARC::GetPenWidth() const
  231. {
  232. return m_Width;
  233. }
  234. void LIB_ARC::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  235. const TRANSFORM& aTransform )
  236. {
  237. bool forceNoFill = static_cast<bool>( aData );
  238. int penWidth = GetEffectivePenWidth( aSettings );
  239. if( forceNoFill && m_fill != FILL_TYPE::NO_FILL && penWidth == 0 )
  240. return;
  241. wxDC* DC = aSettings->GetPrintDC();
  242. wxPoint pos1, pos2, posc;
  243. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  244. pos1 = aTransform.TransformCoordinate( m_ArcEnd ) + aOffset;
  245. pos2 = aTransform.TransformCoordinate( m_ArcStart ) + aOffset;
  246. posc = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  247. int pt1 = m_t1;
  248. int pt2 = m_t2;
  249. bool swap = aTransform.MapAngles( &pt1, &pt2 );
  250. if( swap )
  251. {
  252. std::swap( pos1.x, pos2.x );
  253. std::swap( pos1.y, pos2.y );
  254. }
  255. if( forceNoFill || m_fill == FILL_TYPE::NO_FILL )
  256. {
  257. GRArc1( nullptr, DC, pos1.x, pos1.y, pos2.x, pos2.y, posc.x, posc.y, penWidth, color );
  258. }
  259. else
  260. {
  261. if( m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  262. color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  263. GRFilledArc( nullptr, DC, posc.x, posc.y, pt1, pt2, m_Radius, penWidth, color, color );
  264. }
  265. }
  266. const EDA_RECT LIB_ARC::GetBoundingBox() const
  267. {
  268. int minX, minY, maxX, maxY, angleStart, angleEnd;
  269. EDA_RECT rect;
  270. wxPoint nullPoint, startPos, endPos, centerPos;
  271. wxPoint normStart = m_ArcStart - m_Pos;
  272. wxPoint normEnd = m_ArcEnd - m_Pos;
  273. if( ( normStart == nullPoint ) || ( normEnd == nullPoint ) || ( m_Radius == 0 ) )
  274. return rect;
  275. endPos = DefaultTransform.TransformCoordinate( m_ArcEnd );
  276. startPos = DefaultTransform.TransformCoordinate( m_ArcStart );
  277. centerPos = DefaultTransform.TransformCoordinate( m_Pos );
  278. angleStart = m_t1;
  279. angleEnd = m_t2;
  280. if( DefaultTransform.MapAngles( &angleStart, &angleEnd ) )
  281. {
  282. std::swap( endPos.x, startPos.x );
  283. std::swap( endPos.y, startPos.y );
  284. }
  285. /* Start with the start and end point of the arc. */
  286. minX = std::min( startPos.x, endPos.x );
  287. minY = std::min( startPos.y, endPos.y );
  288. maxX = std::max( startPos.x, endPos.x );
  289. maxY = std::max( startPos.y, endPos.y );
  290. /* Zero degrees is a special case. */
  291. if( angleStart == 0 )
  292. maxX = centerPos.x + m_Radius;
  293. /* Arc end angle wrapped passed 360. */
  294. if( angleStart > angleEnd )
  295. angleEnd += 3600;
  296. if( angleStart <= 900 && angleEnd >= 900 ) /* 90 deg */
  297. maxY = centerPos.y + m_Radius;
  298. if( angleStart <= 1800 && angleEnd >= 1800 ) /* 180 deg */
  299. minX = centerPos.x - m_Radius;
  300. if( angleStart <= 2700 && angleEnd >= 2700 ) /* 270 deg */
  301. minY = centerPos.y - m_Radius;
  302. if( angleStart <= 3600 && angleEnd >= 3600 ) /* 0 deg */
  303. maxX = centerPos.x + m_Radius;
  304. rect.SetOrigin( minX, minY );
  305. rect.SetEnd( maxX, maxY );
  306. rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
  307. return rect;
  308. }
  309. void LIB_ARC::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  310. {
  311. wxString msg;
  312. EDA_RECT bBox = GetBoundingBox();
  313. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  314. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width );
  315. aList.emplace_back( _( "Line Width" ), msg );
  316. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  317. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  318. aList.emplace_back( _( "Bounding Box" ), msg );
  319. }
  320. wxString LIB_ARC::GetSelectMenuText( EDA_UNITS aUnits ) const
  321. {
  322. return wxString::Format( _( "Arc, radius %s" ),
  323. MessageTextFromValue( aUnits, m_Radius ) );
  324. }
  325. BITMAPS LIB_ARC::GetMenuImage() const
  326. {
  327. return BITMAPS::add_arc;
  328. }
  329. void LIB_ARC::BeginEdit( const wxPoint& aPosition )
  330. {
  331. m_ArcStart = m_ArcEnd = aPosition;
  332. m_editState = 1;
  333. }
  334. void LIB_ARC::CalcEdit( const wxPoint& aPosition )
  335. {
  336. #define sq( x ) pow( x, 2 )
  337. // Edit state 0: drawing: place ArcStart
  338. // Edit state 1: drawing: place ArcEnd (center calculated for 90-degree subtended angle)
  339. // Edit state 2: point editing: move ArcStart (center calculated for invariant subtended angle)
  340. // Edit state 3: point editing: move ArcEnd (center calculated for invariant subtended angle)
  341. // Edit state 4: point editing: move center
  342. switch( m_editState )
  343. {
  344. case 0:
  345. m_ArcStart = aPosition;
  346. m_ArcEnd = aPosition;
  347. m_Pos = aPosition;
  348. m_Radius = 0;
  349. m_t1 = 0;
  350. m_t2 = 0;
  351. return;
  352. case 1:
  353. m_ArcEnd = aPosition;
  354. m_Radius = KiROUND( sqrt( pow( GetLineLength( m_ArcStart, m_ArcEnd ), 2 ) / 2.0 ) );
  355. break;
  356. case 2:
  357. case 3:
  358. {
  359. wxPoint v = m_ArcStart - m_ArcEnd;
  360. double chordBefore = sq( v.x ) + sq( v.y );
  361. if( m_editState == 2 )
  362. m_ArcStart = aPosition;
  363. else
  364. m_ArcEnd = aPosition;
  365. v = m_ArcStart - m_ArcEnd;
  366. double chordAfter = sq( v.x ) + sq( v.y );
  367. double ratio = chordAfter / chordBefore;
  368. if( ratio > 0 )
  369. {
  370. m_Radius = int( sqrt( m_Radius * m_Radius * ratio ) ) + 1;
  371. m_Radius = std::max( m_Radius, int( sqrt( chordAfter ) / 2 ) + 1 );
  372. }
  373. break;
  374. }
  375. case 4:
  376. {
  377. double chordA = GetLineLength( m_ArcStart, aPosition );
  378. double chordB = GetLineLength( m_ArcEnd, aPosition );
  379. m_Radius = int( ( chordA + chordB ) / 2.0 ) + 1;
  380. break;
  381. }
  382. }
  383. // Calculate center based on start, end, and radius
  384. //
  385. // Let 'l' be the length of the chord and 'm' the middle point of the chord
  386. double l = GetLineLength( m_ArcStart, m_ArcEnd );
  387. wxPoint m = ( m_ArcStart + m_ArcEnd ) / 2;
  388. // Calculate 'd', the vector from the chord midpoint to the center
  389. wxPoint d;
  390. d.x = KiROUND( sqrt( sq( m_Radius ) - sq( l/2 ) ) * ( m_ArcStart.y - m_ArcEnd.y ) / l );
  391. d.y = KiROUND( sqrt( sq( m_Radius ) - sq( l/2 ) ) * ( m_ArcEnd.x - m_ArcStart.x ) / l );
  392. wxPoint c1 = m + d;
  393. wxPoint c2 = m - d;
  394. // Solution gives us 2 centers; we need to pick one:
  395. switch( m_editState )
  396. {
  397. case 1:
  398. {
  399. // Keep center clockwise from chord while drawing
  400. wxPoint chordVector = twoPointVector( m_ArcStart, m_ArcEnd );
  401. double chordAngle = ArcTangente( chordVector.y, chordVector.x );
  402. NORMALIZE_ANGLE_POS( chordAngle );
  403. wxPoint c1Test = c1;
  404. RotatePoint( &c1Test, m_ArcStart, -chordAngle );
  405. m_Pos = c1Test.x > 0 ? c2 : c1;
  406. }
  407. break;
  408. case 2:
  409. case 3:
  410. // Pick the one closer to the old center
  411. m_Pos = ( GetLineLength( c1, m_Pos ) < GetLineLength( c2, m_Pos ) ) ? c1 : c2;
  412. break;
  413. case 4:
  414. // Pick the one closer to the mouse position
  415. m_Pos = ( GetLineLength( c1, aPosition ) < GetLineLength( c2, aPosition ) ) ? c1 : c2;
  416. break;
  417. }
  418. CalcRadiusAngles();
  419. }
  420. void LIB_ARC::CalcRadiusAngles()
  421. {
  422. wxPoint centerStartVector = twoPointVector( m_Pos, m_ArcStart );
  423. wxPoint centerEndVector = twoPointVector( m_Pos, m_ArcEnd );
  424. m_Radius = KiROUND( EuclideanNorm( centerStartVector ) );
  425. // Angles in Eeschema are still integers
  426. m_t1 = KiROUND( ArcTangente( centerStartVector.y, centerStartVector.x ) );
  427. m_t2 = KiROUND( ArcTangente( centerEndVector.y, centerEndVector.x ) );
  428. NORMALIZE_ANGLE_POS( m_t1 );
  429. NORMALIZE_ANGLE_POS( m_t2 ); // angles = 0 .. 3600
  430. // Restrict angle to less than 180 to avoid PBS display mirror Trace because it is
  431. // assumed that the arc is less than 180 deg to find orientation after rotate or mirror.
  432. if( ( m_t2 - m_t1 ) > 1800 )
  433. m_t2 -= 3600;
  434. else if( ( m_t2 - m_t1 ) <= -1800 )
  435. m_t2 += 3600;
  436. while( ( m_t2 - m_t1 ) >= 1800 )
  437. {
  438. m_t2--;
  439. m_t1++;
  440. }
  441. while( ( m_t1 - m_t2 ) >= 1800 )
  442. {
  443. m_t2++;
  444. m_t1--;
  445. }
  446. NORMALIZE_ANGLE_POS( m_t1 );
  447. if( !IsMoving() )
  448. NORMALIZE_ANGLE_POS( m_t2 );
  449. }
  450. VECTOR2I LIB_ARC::CalcMidPoint() const
  451. {
  452. VECTOR2D midPoint;
  453. double startAngle = static_cast<double>( m_t1 ) / 10.0;
  454. double endAngle = static_cast<double>( m_t2 ) / 10.0;
  455. if( endAngle < startAngle )
  456. endAngle -= 360.0;
  457. double midPointAngle = ( ( endAngle - startAngle ) / 2.0 ) + startAngle;
  458. double x = cos( DEG2RAD( midPointAngle ) ) * m_Radius;
  459. double y = sin( DEG2RAD( midPointAngle ) ) * m_Radius;
  460. midPoint.x = KiROUND( x ) + m_Pos.x;
  461. midPoint.y = KiROUND( y ) + m_Pos.y;
  462. return midPoint;
  463. }
  464. void LIB_ARC::CalcEndPoints()
  465. {
  466. double startAngle = DEG2RAD( static_cast<double>( m_t1 ) / 10.0 );
  467. double endAngle = DEG2RAD( static_cast<double>( m_t2 ) / 10.0 );
  468. m_ArcStart.x = KiROUND( cos( startAngle ) * m_Radius ) + m_Pos.x;
  469. m_ArcStart.y = KiROUND( sin( startAngle ) * m_Radius ) + m_Pos.y;
  470. m_ArcEnd.x = KiROUND( cos( endAngle ) * m_Radius ) + m_Pos.x;
  471. m_ArcEnd.y = KiROUND( sin( endAngle ) * m_Radius ) + m_Pos.y;
  472. }