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.

147 lines
3.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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. #ifndef __SHAPE_SEGMENT_H
  25. #define __SHAPE_SEGMENT_H
  26. #include <geometry/seg.h>
  27. #include <geometry/shape.h>
  28. #include <math/box2.h>
  29. #include <math/vector2d.h>
  30. class SHAPE_SEGMENT : public SHAPE {
  31. public:
  32. SHAPE_SEGMENT():
  33. SHAPE( SH_SEGMENT ), m_width( 0 ) {};
  34. SHAPE_SEGMENT( const VECTOR2I& aA, const VECTOR2I& aB, int aWidth = 0 ):
  35. SHAPE( SH_SEGMENT ), m_seg( aA, aB ), m_width( aWidth ) {};
  36. SHAPE_SEGMENT( const SEG& aSeg, int aWidth = 0 ):
  37. SHAPE( SH_SEGMENT ), m_seg( aSeg ), m_width( aWidth ) {};
  38. ~SHAPE_SEGMENT() {};
  39. SHAPE* Clone() const override
  40. {
  41. return new SHAPE_SEGMENT( m_seg, m_width );
  42. }
  43. const BOX2I BBox( int aClearance = 0 ) const override
  44. {
  45. return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
  46. }
  47. bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
  48. {
  49. return SHAPE::Collide( aShape, aClearance, aMTV );
  50. }
  51. bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr ) const override
  52. {
  53. return SHAPE::Collide( aShape, aClearance, aActual );
  54. }
  55. bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr ) const override
  56. {
  57. int min_dist = ( m_width + 1 ) / 2 + aClearance;
  58. ecoord dist_sq = m_seg.SquaredDistance( aSeg );
  59. if( dist_sq == 0 || dist_sq < (ecoord) min_dist * min_dist )
  60. {
  61. if( aActual )
  62. *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
  63. return true;
  64. }
  65. return false;
  66. }
  67. bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr ) const override
  68. {
  69. int min_dist = ( m_width + 1 ) / 2 + aClearance;
  70. ecoord dist_sq = m_seg.SquaredDistance( aP );
  71. if( dist_sq == 0 || dist_sq < (ecoord) min_dist * min_dist )
  72. {
  73. if( aActual )
  74. *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
  75. return true;
  76. }
  77. return false;
  78. }
  79. void SetSeg( const SEG& aSeg )
  80. {
  81. m_seg = aSeg;
  82. }
  83. const SEG& GetSeg() const
  84. {
  85. return m_seg;
  86. }
  87. void SetWidth( int aWidth )
  88. {
  89. m_width = aWidth;
  90. }
  91. int GetWidth() const
  92. {
  93. return m_width;
  94. }
  95. bool IsSolid() const override
  96. {
  97. return true;
  98. }
  99. void Rotate( double aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
  100. {
  101. m_seg.A -= aCenter;
  102. m_seg.B -= aCenter;
  103. m_seg.A = m_seg.A.Rotate( aAngle );
  104. m_seg.B = m_seg.B.Rotate( aAngle );
  105. m_seg.A += aCenter;
  106. m_seg.B += aCenter;
  107. }
  108. void Move( const VECTOR2I& aVector ) override
  109. {
  110. m_seg.A += aVector;
  111. m_seg.B += aVector;
  112. }
  113. private:
  114. SEG m_seg;
  115. int m_width;
  116. };
  117. #endif