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.

158 lines
4.2 KiB

11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <geometry/seg.h>
  25. template <typename T>
  26. int sgn( T aVal )
  27. {
  28. return ( T( 0 ) < aVal ) - ( aVal < T( 0 ) );
  29. }
  30. bool SEG::PointCloserThan( const VECTOR2I& aP, int aDist ) const
  31. {
  32. VECTOR2I d = B - A;
  33. ecoord dist_sq = (ecoord) aDist * aDist;
  34. SEG::ecoord l_squared = d.Dot( d );
  35. SEG::ecoord t = d.Dot( aP - A );
  36. if( t <= 0 || !l_squared )
  37. return ( aP - A ).SquaredEuclideanNorm() < dist_sq;
  38. else if( t >= l_squared )
  39. return ( aP - B ).SquaredEuclideanNorm() < dist_sq;
  40. int dxdy = abs( d.x ) - abs( d.y );
  41. if( ( dxdy >= -1 && dxdy <= 1 ) || abs( d.x ) <= 1 || abs( d.y ) <= 1 )
  42. {
  43. int ca = -sgn( d.y );
  44. int cb = sgn( d.x );
  45. int cc = -ca * A.x - cb * A.y;
  46. ecoord num = (ecoord) ca * aP.x + (ecoord) cb * aP.y + cc;
  47. num *= num;
  48. if( ca && cb )
  49. num >>= 1;
  50. if( num > ( dist_sq + 100 ) )
  51. return false;
  52. else if( num < ( dist_sq - 100 ) )
  53. return true;
  54. }
  55. VECTOR2I nearest;
  56. nearest.x = A.x + rescale( t, (ecoord) d.x, l_squared );
  57. nearest.y = A.y + rescale( t, (ecoord) d.y, l_squared );
  58. return ( nearest - aP ).SquaredEuclideanNorm() <= dist_sq;
  59. }
  60. SEG::ecoord SEG::SquaredDistance( const SEG& aSeg ) const
  61. {
  62. // fixme: rather inefficient....
  63. if( Intersect( aSeg ) )
  64. return 0;
  65. const VECTOR2I pts[4] =
  66. {
  67. aSeg.NearestPoint( A ) - A,
  68. aSeg.NearestPoint( B ) - B,
  69. NearestPoint( aSeg.A ) - aSeg.A,
  70. NearestPoint( aSeg.B ) - aSeg.B
  71. };
  72. ecoord m = VECTOR2I::ECOORD_MAX;
  73. for( int i = 0; i < 4; i++ )
  74. m = std::min( m, pts[i].SquaredEuclideanNorm() );
  75. return m;
  76. }
  77. OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
  78. {
  79. const VECTOR2I e( B - A );
  80. const VECTOR2I f( aSeg.B - aSeg.A );
  81. const VECTOR2I ac( aSeg.A - A );
  82. ecoord d = f.Cross( e );
  83. ecoord p = f.Cross( ac );
  84. ecoord q = e.Cross( ac );
  85. if( d == 0 )
  86. return OPT_VECTOR2I();
  87. if( !aLines && d > 0 && ( q < 0 || q > d || p < 0 || p > d ) )
  88. return OPT_VECTOR2I();
  89. if( !aLines && d < 0 && ( q < d || p < d || p > 0 || q > 0 ) )
  90. return OPT_VECTOR2I();
  91. if( !aLines && aIgnoreEndpoints && ( q == 0 || q == d ) && ( p == 0 || p == d ) )
  92. return OPT_VECTOR2I();
  93. VECTOR2I ip( aSeg.A.x + rescale( q, (ecoord) f.x, d ),
  94. aSeg.A.y + rescale( q, (ecoord) f.y, d ) );
  95. return ip;
  96. }
  97. bool SEG::ccw( const VECTOR2I& aA, const VECTOR2I& aB, const VECTOR2I& aC ) const
  98. {
  99. return (ecoord) ( aC.y - aA.y ) * ( aB.x - aA.x ) > (ecoord) ( aB.y - aA.y ) * ( aC.x - aA.x );
  100. }
  101. bool SEG::Collide( const SEG& aSeg, int aClearance ) const
  102. {
  103. // check for intersection
  104. // fixme: move to a method
  105. if( ccw( A, aSeg.A, aSeg.B ) != ccw( B, aSeg.A, aSeg.B ) &&
  106. ccw( A, B, aSeg.A ) != ccw( A, B, aSeg.B ) )
  107. return true;
  108. #define CHK( _seg, _pt ) \
  109. if( (_seg).PointCloserThan( _pt, aClearance ) ) return true;
  110. CHK( *this, aSeg.A );
  111. CHK( *this, aSeg.B );
  112. CHK( aSeg, A );
  113. CHK( aSeg, B );
  114. #undef CHK
  115. return false;
  116. }
  117. bool SEG::Contains( const VECTOR2I& aP ) const
  118. {
  119. return PointCloserThan( aP, 1 );
  120. }