failing_reason_policy.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2015, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #ifndef BOOST_GEOMETRY_POLICIES_IS_VALID_FAILING_REASON_POLICY_HPP
  9. #define BOOST_GEOMETRY_POLICIES_IS_VALID_FAILING_REASON_POLICY_HPP
  10. #include <sstream>
  11. #include <boost/geometry/io/dsv/write.hpp>
  12. #include <boost/geometry/util/constexpr.hpp>
  13. #include <boost/geometry/util/range.hpp>
  14. #include <boost/geometry/algorithms/validity_failure_type.hpp>
  15. #include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. inline char const* validity_failure_type_message(validity_failure_type failure)
  19. {
  20. switch (failure)
  21. {
  22. case no_failure:
  23. return "Geometry is valid";
  24. case failure_few_points:
  25. return "Geometry has too few points";
  26. case failure_wrong_topological_dimension:
  27. return "Geometry has wrong topological dimension";
  28. case failure_not_closed:
  29. return "Geometry is defined as closed but is open";
  30. case failure_spikes:
  31. return "Geometry has spikes";
  32. case failure_self_intersections:
  33. return "Geometry has invalid self-intersections";
  34. case failure_wrong_orientation:
  35. return "Geometry has wrong orientation";
  36. case failure_interior_rings_outside:
  37. return "Geometry has interior rings defined outside the outer boundary";
  38. case failure_nested_interior_rings:
  39. return "Geometry has nested interior rings";
  40. case failure_disconnected_interior:
  41. return "Geometry has disconnected interior";
  42. case failure_intersecting_interiors:
  43. return "Multi-polygon has intersecting interiors";
  44. case failure_duplicate_points:
  45. return "Geometry has duplicate (consecutive) points";
  46. case failure_wrong_corner_order:
  47. return "Box has corners in wrong order";
  48. case failure_invalid_coordinate:
  49. return "Geometry has point(s) with invalid coordinate(s)";
  50. default: // to avoid -Wreturn-type warning
  51. return "";
  52. }
  53. }
  54. template <bool AllowDuplicates = true, bool AllowSpikes = true>
  55. class failing_reason_policy
  56. {
  57. private:
  58. static inline
  59. validity_failure_type transform_failure_type(validity_failure_type failure)
  60. {
  61. if BOOST_GEOMETRY_CONSTEXPR (AllowDuplicates)
  62. {
  63. if (failure == failure_duplicate_points)
  64. {
  65. return no_failure;
  66. }
  67. }
  68. return failure;
  69. }
  70. static inline
  71. validity_failure_type transform_failure_type(validity_failure_type failure,
  72. bool is_linear)
  73. {
  74. if BOOST_GEOMETRY_CONSTEXPR (AllowSpikes)
  75. {
  76. if (is_linear && failure == failure_spikes)
  77. {
  78. return no_failure;
  79. }
  80. }
  81. return transform_failure_type(failure);
  82. }
  83. inline void set_failure_message(validity_failure_type failure)
  84. {
  85. m_oss.str("");
  86. m_oss.clear();
  87. m_oss << validity_failure_type_message(failure);
  88. }
  89. template
  90. <
  91. validity_failure_type Failure,
  92. typename Data1,
  93. typename Data2 = Data1,
  94. typename Dummy = void
  95. >
  96. struct process_data
  97. {
  98. static inline void apply(std::ostringstream&, Data1 const&)
  99. {
  100. }
  101. static inline void apply(std::ostringstream&,
  102. Data1 const&,
  103. Data2 const&)
  104. {
  105. }
  106. };
  107. template <typename SpikePoint>
  108. struct process_data<failure_spikes, bool, SpikePoint>
  109. {
  110. static inline void apply(std::ostringstream& oss,
  111. bool is_linear,
  112. SpikePoint const& spike_point)
  113. {
  114. if BOOST_GEOMETRY_CONSTEXPR (AllowSpikes)
  115. {
  116. if (is_linear)
  117. {
  118. return;
  119. }
  120. }
  121. oss << ". A spike point was found with apex at "
  122. << geometry::dsv(spike_point);
  123. }
  124. };
  125. template <typename Turns>
  126. struct process_data<failure_self_intersections, Turns>
  127. {
  128. static inline
  129. void apply_to_segment_identifier(std::ostringstream& oss,
  130. segment_identifier seg_id)
  131. {
  132. oss << "{" << seg_id.source_index
  133. << ", " << seg_id.multi_index
  134. << ", " << seg_id.ring_index
  135. << ", " << seg_id.segment_index
  136. << "}";
  137. }
  138. static inline void apply(std::ostringstream& oss,
  139. Turns const& turns)
  140. {
  141. typedef typename boost::range_value<Turns>::type turn_type;
  142. turn_type const& turn = range::front(turns);
  143. oss << ". A self-intersection point was found at "
  144. << geometry::dsv(turn.point);
  145. oss << "; method: " << method_char(turn.method)
  146. << "; operations: "
  147. << operation_char(turn.operations[0].operation)
  148. << "/"
  149. << operation_char(turn.operations[1].operation)
  150. << "; segment IDs {source, multi, ring, segment}: ";
  151. apply_to_segment_identifier(oss, turn.operations[0].seg_id);
  152. oss << "/";
  153. apply_to_segment_identifier(oss, turn.operations[1].seg_id);
  154. }
  155. };
  156. template <typename Point>
  157. struct process_data<failure_duplicate_points, Point>
  158. {
  159. static inline void apply(std::ostringstream& oss,
  160. Point const& point)
  161. {
  162. if BOOST_GEOMETRY_CONSTEXPR (! AllowDuplicates)
  163. {
  164. oss << ". Duplicate points were found near point "
  165. << geometry::dsv(point);
  166. }
  167. }
  168. };
  169. public:
  170. failing_reason_policy(std::ostringstream& oss)
  171. : m_oss(oss)
  172. {}
  173. template <validity_failure_type Failure>
  174. inline bool apply()
  175. {
  176. validity_failure_type const failure = transform_failure_type(Failure);
  177. set_failure_message(failure);
  178. return failure == no_failure;
  179. }
  180. template <validity_failure_type Failure, typename Data>
  181. inline bool apply(Data const& data)
  182. {
  183. validity_failure_type const failure = transform_failure_type(Failure);
  184. set_failure_message(failure);
  185. process_data<Failure, Data>::apply(m_oss, data);
  186. return failure == no_failure;
  187. }
  188. template <validity_failure_type Failure, typename Data1, typename Data2>
  189. inline bool apply(Data1 const& data1, Data2 const& data2)
  190. {
  191. validity_failure_type const failure
  192. = transform_failure_type(Failure, data1);
  193. set_failure_message(failure);
  194. process_data<Failure, Data1, Data2>::apply(m_oss, data1, data2);
  195. return failure == no_failure;
  196. }
  197. private:
  198. std::ostringstream& m_oss;
  199. };
  200. }} // namespace boost::geometry
  201. #endif // BOOST_GEOMETRY_POLICIES_IS_VALID_FAILING_REASON_POLICY_HPP