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.

140 lines
4.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  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. /**
  25. * @file marker_base.cpp
  26. * @brief Implementation of MARKER_BASE class.
  27. * Markers are used to show something (usually a drc/erc problem).
  28. * Markers in Pcbnew and Eeschema are derived from this base class.
  29. */
  30. #include "base_screen.h"
  31. #include "marker_base.h"
  32. #include <core/arraydim.h>
  33. #include <geometry/shape_line_chain.h>
  34. #include "dialogs/dialog_display_html_text_base.h"
  35. /**
  36. * The graphic shape of markers is a polygon.
  37. *
  38. * MarkerShapeCorners contains the coordinates of corners of the polygonal default shape
  39. * they are arbitrary units to make coding shape easy.
  40. * Internal units coordinates are these values scaled by .m_ScalingFactor
  41. */
  42. static const VECTOR2I MarkerShapeCorners[] =
  43. {
  44. VECTOR2I( 0, 0 ),
  45. VECTOR2I( 8, 1 ),
  46. VECTOR2I( 4, 3 ),
  47. VECTOR2I( 13, 8 ),
  48. VECTOR2I( 9, 9 ),
  49. VECTOR2I( 8, 13 ),
  50. VECTOR2I( 3, 4 ),
  51. VECTOR2I( 1, 8 ),
  52. VECTOR2I( 0, 0 )
  53. };
  54. const unsigned CORNERS_COUNT = arrayDim( MarkerShapeCorners );
  55. MARKER_BASE::MARKER_BASE( int aScalingFactor, std::shared_ptr<RC_ITEM> aItem, MARKER_T aType ) :
  56. m_markerType( aType ),
  57. m_excluded( false ),
  58. m_rcItem( std::move( aItem ) ),
  59. m_scalingFactor( aScalingFactor )
  60. {
  61. const VECTOR2I* point_shape = MarkerShapeCorners;
  62. VECTOR2I start( point_shape->x, point_shape->y );
  63. VECTOR2I end = start;
  64. for( unsigned ii = 1; ii < CORNERS_COUNT; ii++ )
  65. {
  66. ++point_shape;
  67. start.x = std::min( start.x, point_shape->x );
  68. start.y = std::min( start.y, point_shape->y );
  69. end.x = std::max( end.x, point_shape->x );
  70. end.y = std::max( end.y, point_shape->y );
  71. }
  72. m_shapeBoundingBox.SetOrigin( start);
  73. m_shapeBoundingBox.SetEnd( end);
  74. }
  75. bool MARKER_BASE::HitTestMarker( const VECTOR2I& aHitPosition, int aAccuracy ) const
  76. {
  77. const BOX2I bbox = GetBoundingBoxMarker().GetInflated( aAccuracy );
  78. // Fast hit test using boundary box. A finer test will be made if requested
  79. bool hit = bbox.Contains( aHitPosition );
  80. if( hit ) // Fine test
  81. {
  82. SHAPE_LINE_CHAIN polygon;
  83. ShapeToPolygon( polygon );
  84. VECTOR2I rel_pos( aHitPosition - m_Pos );
  85. hit = polygon.PointInside( rel_pos, aAccuracy );
  86. }
  87. return hit;
  88. }
  89. bool MARKER_BASE::HitTestMarker( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  90. {
  91. const BOX2I bbox = GetBoundingBoxMarker().GetInflated( aAccuracy );
  92. if( aContained )
  93. return aRect.Contains( bbox );
  94. return aRect.Intersects( bbox );
  95. }
  96. void MARKER_BASE::ShapeToPolygon( SHAPE_LINE_CHAIN& aPolygon, int aScale ) const
  97. {
  98. if( aScale < 0 )
  99. aScale = MarkerScale();
  100. for( const VECTOR2I& corner : MarkerShapeCorners )
  101. aPolygon.Append( corner * aScale );
  102. // Be sure aPolygon is seen as a closed polyline:
  103. aPolygon.SetClosed( true );
  104. }
  105. BOX2I MARKER_BASE::GetBoundingBoxMarker() const
  106. {
  107. BOX2I bbox = m_shapeBoundingBox;
  108. VECTOR2I pos = m_Pos;
  109. pos += m_shapeBoundingBox.GetPosition() * m_scalingFactor;
  110. return BOX2I( pos, bbox.GetSize() * m_scalingFactor );
  111. }