point_on_surface.hpp 13 KB

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