failing_reason_policy.hpp 7.0 KB

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