point_on_surface.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2017 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014-2023.
  7. // Modifications copyright (c) 2014-2023 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_POINT_ON_SURFACE_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_POINT_ON_SURFACE_HPP
  15. #include <boost/range/begin.hpp>
  16. #include <boost/range/end.hpp>
  17. #include <boost/geometry/core/point_type.hpp>
  18. #include <boost/geometry/geometries/concepts/check.hpp>
  19. #include <boost/geometry/algorithms/detail/extreme_points.hpp>
  20. #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
  21. #include <boost/geometry/strategies/side.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail { namespace point_on_surface
  26. {
  27. template <typename CoordinateType, int Dimension>
  28. struct specific_coordinate_first
  29. {
  30. CoordinateType const m_value_to_be_first;
  31. inline specific_coordinate_first(CoordinateType value_to_be_skipped)
  32. : m_value_to_be_first(value_to_be_skipped)
  33. {}
  34. template <typename Point>
  35. inline bool operator()(Point const& lhs, Point const& rhs)
  36. {
  37. CoordinateType const lh = geometry::get<Dimension>(lhs);
  38. CoordinateType const rh = geometry::get<Dimension>(rhs);
  39. // If both lhs and rhs equal m_value_to_be_first,
  40. // we should handle conform if lh < rh = FALSE
  41. // The first condition meets that, keep it first
  42. if (geometry::math::equals(rh, m_value_to_be_first))
  43. {
  44. // Handle conform lh < -INF, which is always false
  45. return false;
  46. }
  47. if (geometry::math::equals(lh, m_value_to_be_first))
  48. {
  49. // Handle conform -INF < rh, which is always true
  50. return true;
  51. }
  52. return lh < rh;
  53. }
  54. };
  55. template <int Dimension, typename Collection, typename Value, typename Predicate>
  56. inline bool max_value(Collection const& collection, Value& the_max, Predicate const& predicate)
  57. {
  58. bool first = true;
  59. for (auto const& item : collection)
  60. {
  61. if (! item.empty())
  62. {
  63. Value the_value = geometry::get<Dimension>(*std::max_element(item.begin(), item.end(), predicate));
  64. if (first || the_value > the_max)
  65. {
  66. the_max = the_value;
  67. first = false;
  68. }
  69. }
  70. }
  71. return ! first;
  72. }
  73. template <int Dimension, typename Value>
  74. struct select_below
  75. {
  76. Value m_value;
  77. inline select_below(Value const& v)
  78. : m_value(v)
  79. {}
  80. template <typename Intruder>
  81. inline bool operator()(Intruder const& intruder) const
  82. {
  83. if (intruder.empty())
  84. {
  85. return true;
  86. }
  87. Value max = geometry::get<Dimension>(*std::max_element(intruder.begin(), intruder.end(), detail::extreme_points::compare<Dimension>()));
  88. return geometry::math::equals(max, m_value) || max < m_value;
  89. }
  90. };
  91. template <int Dimension, typename Value>
  92. struct adapt_base
  93. {
  94. Value m_value;
  95. inline adapt_base(Value const& v)
  96. : m_value(v)
  97. {}
  98. template <typename Intruder>
  99. inline void operator()(Intruder& intruder) const
  100. {
  101. if (intruder.size() >= 3)
  102. {
  103. detail::extreme_points::move_along_vector<Dimension>(intruder, m_value);
  104. }
  105. }
  106. };
  107. template <int Dimension, typename Value>
  108. struct min_of_intruder
  109. {
  110. template <typename Intruder>
  111. inline bool operator()(Intruder const& lhs, Intruder const& rhs) const
  112. {
  113. Value lhs_min = geometry::get<Dimension>(*std::min_element(lhs.begin(), lhs.end(), detail::extreme_points::compare<Dimension>()));
  114. Value rhs_min = geometry::get<Dimension>(*std::min_element(rhs.begin(), rhs.end(), detail::extreme_points::compare<Dimension>()));
  115. return lhs_min < rhs_min;
  116. }
  117. };
  118. template <typename Point, typename P>
  119. inline void calculate_average(Point& point, std::vector<P> const& points)
  120. {
  121. typedef typename geometry::coordinate_type<Point>::type coordinate_type;
  122. coordinate_type x = 0;
  123. coordinate_type y = 0;
  124. for (auto const& p : points)
  125. {
  126. x += geometry::get<0>(p);
  127. y += geometry::get<1>(p);
  128. }
  129. signed_size_type const count = points.size();
  130. geometry::set<0>(point, x / count);
  131. geometry::set<1>(point, y / count);
  132. }
  133. template <int Dimension, typename Extremes, typename Intruders, typename CoordinateType>
  134. inline void replace_extremes_for_self_tangencies(Extremes& extremes, Intruders& intruders, CoordinateType const& max_intruder)
  135. {
  136. // This function handles self-tangencies.
  137. // Self-tangencies use, as usual, the major part of code...
  138. // ___ e
  139. // /|\ \ .
  140. // / | \ \ .
  141. // / | \ \ .
  142. // / | \ \ .
  143. // / /\ | \ \ .
  144. // i2 i1
  145. // The picture above shows the extreme (outside, "e") and two intruders ("i1","i2")
  146. // Assume that "i1" is self-tangent with the extreme, in one point at the top
  147. // Now the "penultimate" value is searched, this is is the top of i2
  148. // Then everything including and below (this is "i2" here) is removed
  149. // Then the base of "i1" and of "e" is adapted to this penultimate value
  150. // It then looks like:
  151. // b ___ e
  152. // /|\ \ .
  153. // / | \ \ .
  154. // / | \ \ .
  155. // / | \ \ .
  156. // a c i1
  157. // Then intruders (here "i1" but there may be more) are sorted from left to right
  158. // Finally points "a","b" and "c" (in this order) are selected as a new triangle.
  159. // This triangle will have a centroid which is inside (assumed that intruders left segment
  160. // is not equal to extremes left segment, but that polygon would be invalid)
  161. // Find highest non-self tangent intrusion, if any
  162. CoordinateType penultimate_value;
  163. specific_coordinate_first<CoordinateType, Dimension> pu_compare(max_intruder);
  164. if (max_value<Dimension>(intruders, penultimate_value, pu_compare))
  165. {
  166. // Throw away all intrusions <= this value, and of the kept one set this as base.
  167. select_below<Dimension, CoordinateType> predicate(penultimate_value);
  168. intruders.erase
  169. (
  170. std::remove_if(boost::begin(intruders), boost::end(intruders), predicate),
  171. boost::end(intruders)
  172. );
  173. adapt_base<Dimension, CoordinateType> fe_predicate(penultimate_value);
  174. // Sort from left to right (or bottom to top if Dimension=0)
  175. std::for_each(boost::begin(intruders), boost::end(intruders), fe_predicate);
  176. // Also adapt base of extremes
  177. detail::extreme_points::move_along_vector<Dimension>(extremes, penultimate_value);
  178. }
  179. // Then sort in 1-Dim. Take first to calc centroid.
  180. std::sort(boost::begin(intruders), boost::end(intruders), min_of_intruder<1 - Dimension, CoordinateType>());
  181. Extremes triangle;
  182. triangle.reserve(3);
  183. // Make a triangle of first two points of extremes (the ramp, from left to right), and last point of first intruder (which goes from right to left)
  184. std::copy(extremes.begin(), extremes.begin() + 2, std::back_inserter(triangle));
  185. triangle.push_back(intruders.front().back());
  186. // (alternatively we could use the last two points of extremes, and first point of last intruder...):
  187. //// ALTERNATIVE: std::copy(extremes.rbegin(), extremes.rbegin() + 2, std::back_inserter(triangle));
  188. //// ALTERNATIVE: triangle.push_back(intruders.back().front());
  189. // Now replace extremes with this smaller subset, a triangle, such that centroid calculation will result in a point inside
  190. extremes = triangle;
  191. }
  192. template <int Dimension, typename Geometry, typename Point, typename SideStrategy>
  193. inline bool calculate_point_on_surface(Geometry const& geometry, Point& point,
  194. SideStrategy const& strategy)
  195. {
  196. typedef typename geometry::point_type<Geometry>::type point_type;
  197. typedef typename geometry::coordinate_type<Geometry>::type coordinate_type;
  198. std::vector<point_type> extremes;
  199. typedef std::vector<std::vector<point_type> > intruders_type;
  200. intruders_type intruders;
  201. geometry::extreme_points<Dimension>(geometry, extremes, intruders, strategy);
  202. if (extremes.size() < 3)
  203. {
  204. return false;
  205. }
  206. // If there are intruders, find the max.
  207. if (! intruders.empty())
  208. {
  209. coordinate_type max_intruder;
  210. detail::extreme_points::compare<Dimension> compare;
  211. if (max_value<Dimension>(intruders, max_intruder, compare))
  212. {
  213. coordinate_type max_extreme = geometry::get<Dimension>(*std::max_element(extremes.begin(), extremes.end(), detail::extreme_points::compare<Dimension>()));
  214. if (max_extreme > max_intruder)
  215. {
  216. detail::extreme_points::move_along_vector<Dimension>(extremes, max_intruder);
  217. }
  218. else
  219. {
  220. replace_extremes_for_self_tangencies<Dimension>(extremes, intruders, max_intruder);
  221. }
  222. }
  223. }
  224. // Now calculate the average/centroid of the (possibly adapted) extremes
  225. calculate_average(point, extremes);
  226. return true;
  227. }
  228. }} // namespace detail::point_on_surface
  229. #endif // DOXYGEN_NO_DETAIL
  230. /*!
  231. \brief Assigns a Point guaranteed to lie on the surface of the Geometry
  232. \tparam Geometry geometry type. This also defines the type of the output point
  233. \param geometry Geometry to take point from
  234. \param point Point to assign
  235. \param strategy side strategy
  236. */
  237. template <typename Geometry, typename Point, typename SideStrategy>
  238. inline void point_on_surface(Geometry const& geometry, Point & point,
  239. SideStrategy const& strategy)
  240. {
  241. concepts::check<Point>();
  242. concepts::check<Geometry const>();
  243. // First try in Y-direction (which should always succeed for valid polygons)
  244. if (! detail::point_on_surface::calculate_point_on_surface<1>(geometry, point, strategy))
  245. {
  246. // For invalid polygons, we might try X-direction
  247. detail::point_on_surface::calculate_point_on_surface<0>(geometry, point, strategy);
  248. }
  249. }
  250. /*!
  251. \brief Assigns a Point guaranteed to lie on the surface of the Geometry
  252. \tparam Geometry geometry type. This also defines the type of the output point
  253. \param geometry Geometry to take point from
  254. \param point Point to assign
  255. */
  256. template <typename Geometry, typename Point>
  257. inline void point_on_surface(Geometry const& geometry, Point & point)
  258. {
  259. typedef typename strategy::side::services::default_strategy
  260. <
  261. typename cs_tag<Geometry>::type
  262. >::type strategy_type;
  263. point_on_surface(geometry, point, strategy_type());
  264. }
  265. /*!
  266. \brief Returns point guaranteed to lie on the surface of the Geometry
  267. \tparam Geometry geometry type. This also defines the type of the output point
  268. \param geometry Geometry to take point from
  269. \param strategy side strategy
  270. \return The Point guaranteed to lie on the surface of the Geometry
  271. */
  272. template<typename Geometry, typename SideStrategy>
  273. inline typename geometry::point_type<Geometry>::type
  274. return_point_on_surface(Geometry const& geometry, SideStrategy const& strategy)
  275. {
  276. typename geometry::point_type<Geometry>::type result;
  277. geometry::point_on_surface(geometry, result, strategy);
  278. return result;
  279. }
  280. /*!
  281. \brief Returns point guaranteed to lie on the surface of the Geometry
  282. \tparam Geometry geometry type. This also defines the type of the output point
  283. \param geometry Geometry to take point from
  284. \return The Point guaranteed to lie on the surface of the Geometry
  285. */
  286. template<typename Geometry>
  287. inline typename geometry::point_type<Geometry>::type
  288. return_point_on_surface(Geometry const& geometry)
  289. {
  290. typename geometry::point_type<Geometry>::type result;
  291. geometry::point_on_surface(geometry, result);
  292. return result;
  293. }
  294. }} // namespace boost::geometry
  295. #endif // BOOST_GEOMETRY_ALGORITHMS_POINT_ON_SURFACE_HPP