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.

138 lines
3.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <math/vector2d.h>
  20. class SEG;
  21. namespace KIGEOM
  22. {
  23. /**
  24. * @file vector_utils.h
  25. *
  26. * Supplemental functions for working with vectors and
  27. * simple objects that interact with vectors.
  28. */
  29. /*
  30. * Determine if a point is in a given direction from another point.
  31. *
  32. * This returns true if the vector from aFrom to aPoint is within
  33. * 90 degrees of aDirection.
  34. *
  35. * ------> aDirection
  36. *
  37. * /-- aFrom
  38. * O /-- aPoint
  39. * O
  40. *
  41. * If the point is perpendicular to the direction, it is considered
  42. * to NOT be in the direction (e.g. both the direction and the
  43. * reversed direction would return false).
  44. *
  45. * @param aPoint The point to test.
  46. * @param aDirection The direction vector.
  47. * @param aFrom The point to test from.
  48. *
  49. * @return true if the point is in the direction.
  50. */
  51. template <typename T>
  52. bool PointIsInDirection( const VECTOR2<T>& aPoint, const VECTOR2<T>& aDirection,
  53. const VECTOR2<T>& aFrom )
  54. {
  55. return ( aPoint - aFrom ).Dot( aDirection ) > 0;
  56. }
  57. /**
  58. * Determine if a segment's vector is within 90 degrees of a given direction.
  59. */
  60. bool SegIsInDirection( const SEG& aSeg, const VECTOR2I& aDirection );
  61. /**
  62. * Determine if a point projects onto a segment.
  63. *
  64. * /--- projects /--- does not project
  65. * o o
  66. * | |
  67. * |<------------>| x
  68. * aSeg
  69. */
  70. bool PointProjectsOntoSegment( const VECTOR2I& aPoint, const SEG& aSeg );
  71. /**
  72. * Get the ratio of the vector to a point from the segment's start,
  73. * compared to the segment's length.
  74. *
  75. * /--- aPoint
  76. * A<---+-------->B <-- Length L
  77. * | |
  78. * >|----|<-- Length R
  79. *
  80. * The point doesn't have to lie on the segment.
  81. */
  82. double GetLengthRatioFromStart( const VECTOR2I& aPoint, const SEG& aSeg );
  83. /**
  84. * Get the ratio of the vector to a point projected onto a segment
  85. * from the start, relative to the segment's length.
  86. *
  87. * /--- projects
  88. * o
  89. * |
  90. * A<---+-------->B <-- Length L
  91. * | |
  92. * >|----|<-- Length R
  93. *
  94. * The ratio is R / L. IF 0, the point is at A. If 1, the point is at B.
  95. * It assumes the point projects onto the segment.
  96. */
  97. double GetProjectedPointLengthRatio( const VECTOR2I& aPoint, const SEG& aSeg );
  98. /**
  99. * Get the nearest end of a segment to a point.
  100. *
  101. * If equidistant, the start point is returned.
  102. */
  103. const VECTOR2I& GetNearestEndpoint( const SEG& aSeg, const VECTOR2I& aPoint );
  104. /**
  105. * Round a vector to the nearest grid point in any direction.
  106. */
  107. VECTOR2I RoundGrid( const VECTOR2I& aVec, int aGridSize );
  108. /**
  109. * Round a vector to the nearest grid point in the NW direction.
  110. *
  111. * This means x and y are both rounded downwards (regardless of sign).
  112. */
  113. VECTOR2I RoundNW( const VECTOR2I& aVec, int aGridSize );
  114. /**
  115. * Round a vector to the nearest grid point in the SE direction.
  116. *
  117. * This means x and y are both rounded upwards (regardless of sign).
  118. */
  119. VECTOR2I RoundSE( const VECTOR2I& aVec, int aGridSize );
  120. } // namespace KIGEOM