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.

187 lines
4.2 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. #include <geometry/eda_angle.h>
  26. #include <trigo.h>
  27. using namespace KIGFX::PREVIEW;
  28. ///< Snap an angle to the nearest 45 degrees
  29. static EDA_ANGLE snapAngle( const EDA_ANGLE& aAngle )
  30. {
  31. return ANGLE_45 * KiROUND( aAngle / ANGLE_45 );
  32. }
  33. bool ARC_GEOM_MANAGER::acceptPoint( const VECTOR2I& aPt )
  34. {
  35. switch( getStep() )
  36. {
  37. case SET_ORIGIN: return setOrigin( aPt );
  38. case SET_START: return setStart( aPt );
  39. case SET_ANGLE: return setEnd( aPt );
  40. case COMPLETE: return false;
  41. }
  42. return false;
  43. }
  44. void ARC_GEOM_MANAGER::SetClockwise( bool aCw )
  45. {
  46. m_clockwise = aCw;
  47. m_directionLocked = true;
  48. setGeometryChanged();
  49. }
  50. void ARC_GEOM_MANAGER::ToggleClockwise()
  51. {
  52. m_clockwise = !m_clockwise;
  53. m_directionLocked = true;
  54. setGeometryChanged();
  55. }
  56. VECTOR2I ARC_GEOM_MANAGER::GetOrigin() const
  57. {
  58. return m_origin;
  59. }
  60. VECTOR2I ARC_GEOM_MANAGER::GetStartRadiusEnd() const
  61. {
  62. VECTOR2I vec( m_radius, 0 );
  63. RotatePoint( vec, -m_startAngle );
  64. return m_origin +vec;
  65. }
  66. VECTOR2I ARC_GEOM_MANAGER::GetEndRadiusEnd() const
  67. {
  68. VECTOR2I vec( m_radius, 0 );
  69. RotatePoint( vec, -m_endAngle );
  70. return m_origin + vec;
  71. }
  72. double ARC_GEOM_MANAGER::GetRadius() const
  73. {
  74. return m_radius;
  75. }
  76. EDA_ANGLE ARC_GEOM_MANAGER::GetStartAngle() const
  77. {
  78. EDA_ANGLE angle = m_startAngle;
  79. if( m_clockwise )
  80. angle -= ANGLE_360;
  81. return -angle;
  82. }
  83. EDA_ANGLE ARC_GEOM_MANAGER::GetSubtended() const
  84. {
  85. EDA_ANGLE angle = m_endAngle - m_startAngle;
  86. if( m_endAngle <= m_startAngle )
  87. angle += ANGLE_360;
  88. if( m_clockwise )
  89. angle -= ANGLE_360;
  90. return -angle;
  91. }
  92. bool ARC_GEOM_MANAGER::setOrigin( const VECTOR2I& aOrigin )
  93. {
  94. m_origin = aOrigin;
  95. m_startAngle = ANGLE_0;
  96. m_endAngle = ANGLE_0;
  97. return true;
  98. }
  99. bool ARC_GEOM_MANAGER::setStart( const VECTOR2I& aEnd )
  100. {
  101. const VECTOR2I radVec = aEnd - m_origin;
  102. m_radius = radVec.EuclideanNorm();
  103. m_startAngle = EDA_ANGLE( radVec );
  104. if( m_angleSnap )
  105. m_startAngle = snapAngle( m_startAngle );
  106. // normalise to 0..360
  107. while( m_startAngle < ANGLE_0 )
  108. m_startAngle += ANGLE_360;
  109. m_endAngle = m_startAngle;
  110. return m_radius != 0.0;
  111. }
  112. bool ARC_GEOM_MANAGER::setEnd( const VECTOR2I& aCursor )
  113. {
  114. const VECTOR2I radVec = aCursor - m_origin;
  115. m_endAngle = EDA_ANGLE( radVec );
  116. if( m_angleSnap )
  117. m_endAngle = snapAngle( m_endAngle );
  118. // normalise to 0..360
  119. while( m_endAngle < ANGLE_0 )
  120. m_endAngle += ANGLE_360;
  121. if( !m_directionLocked )
  122. {
  123. EDA_ANGLE ccwAngle = m_endAngle - m_startAngle;
  124. if( m_endAngle <= m_startAngle )
  125. ccwAngle += ANGLE_360;
  126. EDA_ANGLE cwAngle = std::abs( ccwAngle - ANGLE_360 );
  127. if( std::min( ccwAngle, cwAngle ) >= ANGLE_90 )
  128. m_directionLocked = true;
  129. else
  130. m_clockwise = cwAngle < ccwAngle;
  131. }
  132. else if( std::abs( GetSubtended() ) < ANGLE_90 )
  133. {
  134. m_directionLocked = false;
  135. }
  136. // if the end is the same as the start, this is a bad point
  137. return m_endAngle != m_startAngle;
  138. }