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.

181 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-2021 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. #include <preview_items/arc_geom_manager.h>
  24. #include <math/util.h> // for KiROUND
  25. using namespace KIGFX::PREVIEW;
  26. ///< Snap an angle to the nearest 45 degrees
  27. static double snapAngle( double aAngle )
  28. {
  29. return KiROUND( aAngle / M_PI_4 ) * M_PI_4;
  30. }
  31. bool ARC_GEOM_MANAGER::acceptPoint( const VECTOR2I& aPt )
  32. {
  33. switch( getStep() )
  34. {
  35. case SET_ORIGIN: return setOrigin( aPt );
  36. case SET_START: return setStart( aPt );
  37. case SET_ANGLE: return setEnd( aPt );
  38. case COMPLETE: return false;
  39. }
  40. return false;
  41. }
  42. void ARC_GEOM_MANAGER::SetClockwise( bool aCw )
  43. {
  44. m_clockwise = aCw;
  45. m_directionLocked = true;
  46. setGeometryChanged();
  47. }
  48. void ARC_GEOM_MANAGER::ToggleClockwise()
  49. {
  50. m_clockwise = !m_clockwise;
  51. m_directionLocked = true;
  52. setGeometryChanged();
  53. }
  54. VECTOR2I ARC_GEOM_MANAGER::GetOrigin() const
  55. {
  56. return m_origin;
  57. }
  58. VECTOR2I ARC_GEOM_MANAGER::GetStartRadiusEnd() const
  59. {
  60. return m_origin + VECTOR2I( m_radius, 0 ).Rotate( m_startAngle );
  61. }
  62. VECTOR2I ARC_GEOM_MANAGER::GetEndRadiusEnd() const
  63. {
  64. return m_origin + VECTOR2I( m_radius, 0 ).Rotate( m_endAngle );
  65. }
  66. double ARC_GEOM_MANAGER::GetRadius() const
  67. {
  68. return m_radius;
  69. }
  70. double ARC_GEOM_MANAGER::GetStartAngle() const
  71. {
  72. double angle = m_startAngle;
  73. if( m_clockwise )
  74. angle -= 2 * M_PI;
  75. return -angle;
  76. }
  77. double ARC_GEOM_MANAGER::GetSubtended() const
  78. {
  79. double angle = m_endAngle - m_startAngle;
  80. if( m_endAngle <= m_startAngle )
  81. angle += 2 * M_PI;
  82. if( m_clockwise )
  83. angle -= 2 * M_PI;
  84. return -angle;
  85. }
  86. bool ARC_GEOM_MANAGER::setOrigin( const VECTOR2I& aOrigin )
  87. {
  88. m_origin = aOrigin;
  89. m_startAngle = 0.0;
  90. m_endAngle = 0.0;
  91. return true;
  92. }
  93. bool ARC_GEOM_MANAGER::setStart( const VECTOR2I& aEnd )
  94. {
  95. const VECTOR2I radVec = aEnd - m_origin;
  96. m_radius = radVec.EuclideanNorm();
  97. m_startAngle = radVec.Angle();
  98. if( m_angleSnap )
  99. m_startAngle = snapAngle( m_startAngle );
  100. // normalise into 0-2Pi
  101. while( m_startAngle < 0 )
  102. m_startAngle += M_PI * 2;
  103. m_endAngle = m_startAngle;
  104. return m_radius != 0.0;
  105. }
  106. bool ARC_GEOM_MANAGER::setEnd( const VECTOR2I& aCursor )
  107. {
  108. const VECTOR2I radVec = aCursor - m_origin;
  109. m_endAngle = radVec.Angle();
  110. if( m_angleSnap )
  111. m_endAngle = snapAngle( m_endAngle );
  112. // normalise into 0-2Pi
  113. while( m_endAngle < 0 )
  114. m_endAngle += M_PI * 2;
  115. if( !m_directionLocked )
  116. {
  117. double ccwAngle = m_endAngle - m_startAngle;
  118. if( m_endAngle <= m_startAngle )
  119. ccwAngle += 2 * M_PI;
  120. double cwAngle = std::abs( ccwAngle - 2 * M_PI );
  121. if( std::min( ccwAngle, cwAngle ) >= M_PI_2 )
  122. m_directionLocked = true;
  123. else
  124. m_clockwise = cwAngle < ccwAngle;
  125. }
  126. else if( std::abs( GetSubtended() ) < M_PI_2 )
  127. {
  128. m_directionLocked = false;
  129. }
  130. // if the end is the same as the start, this is a bad point
  131. return m_endAngle != m_startAngle;
  132. }