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.

157 lines
5.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 1992-2020 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 polygon_2d.h
  26. */
  27. #ifndef _CPOLYGON2D_H_
  28. #define _CPOLYGON2D_H_
  29. #include "object_2d.h"
  30. #include "../accelerators/container_2d.h"
  31. #include <geometry/shape_poly_set.h>
  32. #include <vector>
  33. struct POLYSEGMENT
  34. {
  35. SFVEC2F m_Start;
  36. float m_inv_JY_minus_IY = 0.0;
  37. float m_JX_minus_IX = 0.0;
  38. };
  39. struct SEG_NORMALS
  40. {
  41. SFVEC2F m_Start;
  42. SFVEC2F m_End;
  43. };
  44. struct SEGMENT_WITH_NORMALS
  45. {
  46. SFVEC2F m_Start;
  47. SFVEC2F m_Precalc_slope;
  48. SEG_NORMALS m_Normals;
  49. };
  50. typedef std::vector< POLYSEGMENT > SEGMENTS;
  51. /**
  52. * List used to test ray2d intersections.
  53. *
  54. * It will be a subset of an original polygon. The normals will be passed already interpolated.
  55. */
  56. typedef std::vector< SEGMENT_WITH_NORMALS > SEGMENTS_WIDTH_NORMALS;
  57. /**
  58. * Handle a subset of a polygon.
  59. *
  60. * It can contain multiple closed polygons and holes and us used to test if points are inside.
  61. * A point will be inside the polygon if it is not inside a hole and it is inside an outer polygon.
  62. */
  63. struct OUTERS_AND_HOLES
  64. {
  65. std::vector<SEGMENTS> m_Outers;
  66. std::vector<SEGMENTS> m_Holes;
  67. };
  68. /**
  69. * Represent a sub polygon block.
  70. *
  71. * This polygon block was created from a general polygon definition that was sub divided and
  72. * to create blocks of polygons. This polygon class represent a sub part of that main polygon.
  73. * There is information for the contours (used to test the ray2d intersection) and a close
  74. * definition of the block polygon to test if a point is inside.
  75. */
  76. class POLYGON_2D : public OBJECT_2D
  77. {
  78. public:
  79. POLYGON_2D( const SEGMENTS_WIDTH_NORMALS& aOpenSegmentList,
  80. const OUTERS_AND_HOLES& aOuterAndHoles, const BOARD_ITEM& aBoardItem );
  81. bool Overlaps( const BBOX_2D& aBBox ) const override;
  82. bool Intersects( const BBOX_2D& aBBox ) const override;
  83. bool Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const override;
  84. INTERSECTION_RESULT IsBBoxInside( const BBOX_2D& aBBox ) const override;
  85. bool IsPointInside( const SFVEC2F& aPoint ) const override;
  86. private:
  87. /**
  88. * The outer part of the polygon.
  89. *
  90. * This list is used to test a ray intersection with the boundaries of this sub polygon.
  91. * It contains also the interpolated normals that are passed from the main polygon.
  92. */
  93. SEGMENTS_WIDTH_NORMALS m_open_segments;
  94. ///< A polygon block can have multiple polygon and holes
  95. OUTERS_AND_HOLES m_outers_and_holes;
  96. };
  97. /**
  98. * A dummy block defined by a 2d box size.
  99. *
  100. * If the point is inside the bounding box it will return always true. However, the
  101. * intersection with a ray will return always false. This is used as a sub block
  102. * extracted from polygon (pcb polygon areas) and represents an area that is fully filled.
  103. */
  104. class DUMMY_BLOCK_2D : public OBJECT_2D
  105. {
  106. public:
  107. DUMMY_BLOCK_2D( const SFVEC2F& aPbMin, const SFVEC2F& aPbMax, const BOARD_ITEM& aBoardItem );
  108. DUMMY_BLOCK_2D( const BBOX_2D& aBBox, const BOARD_ITEM& aBoardItem );
  109. bool Overlaps( const BBOX_2D& aBBox ) const override;
  110. bool Intersects( const BBOX_2D& aBBox ) const override;
  111. bool Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const override;
  112. INTERSECTION_RESULT IsBBoxInside( const BBOX_2D& aBBox ) const override;
  113. bool IsPointInside( const SFVEC2F& aPoint ) const override;
  114. };
  115. /**
  116. * Use a polygon in the format of the ClipperLib::Path and process it and create multiple 2d
  117. * objects (POLYGON_2D and DUMMY_BLOCK_2D) that can be used to represent this polygon area.
  118. *
  119. * @param aMainPath the polygon are that was converted from the pcb board
  120. * @param aDstContainer the destination container to put the created sub blocks
  121. * @param aBiuTo3dUnitsScale the rendering target 3d scale
  122. * @param aDivFactor a division factor (in 3Dunits) to divide the polygon plane,
  123. * 0.0f will use the internal polygon segm statistics
  124. */
  125. void ConvertPolygonToBlocks( const SHAPE_POLY_SET& aMainPath, CONTAINER_2D_BASE& aDstContainer,
  126. float aBiuTo3dUnitsScale, float aDivFactor,
  127. const BOARD_ITEM& aBoardItem, int aPolyIndex );
  128. void Polygon2d_TestModule();
  129. #endif // _CPOLYGON2D_H_