extreme_points.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 2017-2021.
  7. // Modifications copyright (c) 2017-2021 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_DETAIL_EXTREME_POINTS_HPP
  13. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXTREME_POINTS_HPP
  14. #include <cstddef>
  15. #include <boost/range/begin.hpp>
  16. #include <boost/range/end.hpp>
  17. #include <boost/range/size.hpp>
  18. #include <boost/range/value_type.hpp>
  19. #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
  20. #include <boost/geometry/arithmetic/arithmetic.hpp>
  21. #include <boost/geometry/core/cs.hpp>
  22. #include <boost/geometry/core/point_order.hpp>
  23. #include <boost/geometry/core/point_type.hpp>
  24. #include <boost/geometry/core/ring_type.hpp>
  25. #include <boost/geometry/core/tags.hpp>
  26. #include <boost/geometry/geometries/concepts/check.hpp>
  27. #include <boost/geometry/iterators/ever_circling_iterator.hpp>
  28. #include <boost/geometry/strategies/side.hpp>
  29. #include <boost/geometry/util/math.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace extreme_points
  34. {
  35. template <std::size_t Dimension>
  36. struct compare
  37. {
  38. template <typename Point>
  39. inline bool operator()(Point const& lhs, Point const& rhs)
  40. {
  41. return geometry::get<Dimension>(lhs) < geometry::get<Dimension>(rhs);
  42. }
  43. };
  44. template <std::size_t Dimension, typename PointType, typename CoordinateType>
  45. inline void move_along_vector(PointType& point, PointType const& extreme, CoordinateType const& base_value)
  46. {
  47. // Moves a point along the vector (point, extreme) in the direction of the extreme point
  48. // This adapts the possibly uneven legs of the triangle (or trapezium-like shape)
  49. // _____extreme _____
  50. // / \ / \ .
  51. // /base \ => / \ point .
  52. // \ point .
  53. //
  54. // For so-called intruders, it can be used to adapt both legs to the level of "base"
  55. // For the base, it can be used to adapt both legs to the level of the max-value of the intruders
  56. // If there are 2 or more extreme values, use the one close to 'point' to have a correct vector
  57. CoordinateType const value = geometry::get<Dimension>(point);
  58. //if (geometry::math::equals(value, base_value))
  59. if (value >= base_value)
  60. {
  61. return;
  62. }
  63. PointType vector = point;
  64. subtract_point(vector, extreme);
  65. CoordinateType const diff = geometry::get<Dimension>(vector);
  66. // diff should never be zero
  67. // because of the way our triangle/trapezium is build.
  68. // We just return if it would be the case.
  69. if (geometry::math::equals(diff, 0))
  70. {
  71. return;
  72. }
  73. CoordinateType const base_diff = base_value - geometry::get<Dimension>(extreme);
  74. multiply_value(vector, base_diff);
  75. divide_value(vector, diff);
  76. // The real move:
  77. point = extreme;
  78. add_point(point, vector);
  79. }
  80. template <std::size_t Dimension, typename Range, typename CoordinateType>
  81. inline void move_along_vector(Range& range, CoordinateType const& base_value)
  82. {
  83. if (range.size() >= 3)
  84. {
  85. move_along_vector<Dimension>(range.front(), *(range.begin() + 1), base_value);
  86. move_along_vector<Dimension>(range.back(), *(range.rbegin() + 1), base_value);
  87. }
  88. }
  89. template<typename Ring, std::size_t Dimension>
  90. struct extreme_points_on_ring
  91. {
  92. typedef typename geometry::coordinate_type<Ring>::type coordinate_type;
  93. typedef typename geometry::point_type<Ring>::type point_type;
  94. template <typename CirclingIterator, typename Points>
  95. static inline bool extend(CirclingIterator& it,
  96. std::size_t n,
  97. coordinate_type max_coordinate_value,
  98. Points& points, int direction)
  99. {
  100. std::size_t safe_index = 0;
  101. do
  102. {
  103. it += direction;
  104. points.push_back(*it);
  105. if (safe_index++ >= n)
  106. {
  107. // E.g.: ring is completely horizontal or vertical (= invalid, but we don't want to have an infinite loop)
  108. return false;
  109. }
  110. } while (geometry::math::equals(geometry::get<Dimension>(*it), max_coordinate_value));
  111. return true;
  112. }
  113. // Overload without adding to poinst
  114. template <typename CirclingIterator>
  115. static inline bool extend(CirclingIterator& it,
  116. std::size_t n,
  117. coordinate_type max_coordinate_value,
  118. int direction)
  119. {
  120. std::size_t safe_index = 0;
  121. do
  122. {
  123. it += direction;
  124. if (safe_index++ >= n)
  125. {
  126. // E.g.: ring is completely horizontal or vertical (= invalid, but we don't want to have an infinite loop)
  127. return false;
  128. }
  129. } while (geometry::math::equals(geometry::get<Dimension>(*it), max_coordinate_value));
  130. return true;
  131. }
  132. template <typename CirclingIterator>
  133. static inline bool extent_both_sides(Ring const& ring,
  134. point_type extreme,
  135. CirclingIterator& left,
  136. CirclingIterator& right)
  137. {
  138. std::size_t const n = boost::size(ring);
  139. coordinate_type const max_coordinate_value = geometry::get<Dimension>(extreme);
  140. if (! extend(left, n, max_coordinate_value, -1))
  141. {
  142. return false;
  143. }
  144. if (! extend(right, n, max_coordinate_value, +1))
  145. {
  146. return false;
  147. }
  148. return true;
  149. }
  150. template <typename Collection, typename CirclingIterator>
  151. static inline bool collect(Ring const& ring,
  152. point_type extreme,
  153. Collection& points,
  154. CirclingIterator& left,
  155. CirclingIterator& right)
  156. {
  157. std::size_t const n = boost::size(ring);
  158. coordinate_type const max_coordinate_value = geometry::get<Dimension>(extreme);
  159. // Collects first left, which is reversed (if more than one point) then adds the top itself, then right
  160. if (! extend(left, n, max_coordinate_value, points, -1))
  161. {
  162. return false;
  163. }
  164. std::reverse(points.begin(), points.end());
  165. points.push_back(extreme);
  166. if (! extend(right, n, max_coordinate_value, points, +1))
  167. {
  168. return false;
  169. }
  170. return true;
  171. }
  172. template <typename Extremes, typename Intruders, typename CirclingIterator, typename SideStrategy>
  173. static inline void get_intruders(Ring const& ring, CirclingIterator left, CirclingIterator right,
  174. Extremes const& extremes,
  175. Intruders& intruders,
  176. SideStrategy const& strategy)
  177. {
  178. if (boost::size(extremes) < 3)
  179. {
  180. return;
  181. }
  182. coordinate_type const min_value = geometry::get<Dimension>(*std::min_element(boost::begin(extremes), boost::end(extremes), compare<Dimension>()));
  183. // Also select left/right (if Dimension=1)
  184. coordinate_type const other_min = geometry::get<1 - Dimension>(*std::min_element(boost::begin(extremes), boost::end(extremes), compare<1 - Dimension>()));
  185. coordinate_type const other_max = geometry::get<1 - Dimension>(*std::max_element(boost::begin(extremes), boost::end(extremes), compare<1 - Dimension>()));
  186. std::size_t defensive_check_index = 0; // in case we skip over left/right check, collect modifies right too
  187. std::size_t const n = boost::size(ring);
  188. while (left != right && defensive_check_index < n)
  189. {
  190. coordinate_type const coordinate = geometry::get<Dimension>(*right);
  191. coordinate_type const other_coordinate = geometry::get<1 - Dimension>(*right);
  192. if (coordinate > min_value && other_coordinate > other_min && other_coordinate < other_max)
  193. {
  194. int const factor = geometry::point_order<Ring>::value == geometry::clockwise ? 1 : -1;
  195. int const first_side = strategy.apply(*right, extremes.front(), *(extremes.begin() + 1)) * factor;
  196. int const last_side = strategy.apply(*right, *(extremes.rbegin() + 1), extremes.back()) * factor;
  197. // If not lying left from any of the extemes side
  198. if (first_side != 1 && last_side != 1)
  199. {
  200. //std::cout << "first " << first_side << " last " << last_side << std::endl;
  201. // we start at this intrusion until it is handled, and don't affect our initial left iterator
  202. CirclingIterator left_intrusion_it = right;
  203. typename boost::range_value<Intruders>::type intruder;
  204. collect(ring, *right, intruder, left_intrusion_it, right);
  205. // Also moves these to base-level, makes sorting possible which can be done in case of self-tangencies
  206. // (we might postpone this action, it is often not necessary. However it is not time-consuming)
  207. move_along_vector<Dimension>(intruder, min_value);
  208. intruders.push_back(intruder);
  209. --right;
  210. }
  211. }
  212. ++right;
  213. defensive_check_index++;
  214. }
  215. }
  216. template <typename Extremes, typename Intruders, typename SideStrategy>
  217. static inline void get_intruders(Ring const& ring,
  218. Extremes const& extremes,
  219. Intruders& intruders,
  220. SideStrategy const& strategy)
  221. {
  222. std::size_t const n = boost::size(ring);
  223. if (n >= 3)
  224. {
  225. geometry::ever_circling_range_iterator<Ring const> left(ring);
  226. geometry::ever_circling_range_iterator<Ring const> right(ring);
  227. ++right;
  228. get_intruders(ring, left, right, extremes, intruders, strategy);
  229. }
  230. }
  231. template <typename Iterator, typename SideStrategy>
  232. static inline bool right_turn(Ring const& ring, Iterator it, SideStrategy const& strategy)
  233. {
  234. auto const index = std::distance(boost::begin(ring), it);
  235. geometry::ever_circling_range_iterator<Ring const> left(ring);
  236. geometry::ever_circling_range_iterator<Ring const> right(ring);
  237. left += index;
  238. right += index;
  239. if (! extent_both_sides(ring, *it, left, right))
  240. {
  241. return false;
  242. }
  243. int const factor = geometry::point_order<Ring>::value == geometry::clockwise ? 1 : -1;
  244. int const first_side = strategy.apply(*(right - 1), *right, *left) * factor;
  245. int const last_side = strategy.apply(*left, *(left + 1), *right) * factor;
  246. //std::cout << "Candidate at " << geometry::wkt(*it) << " first=" << first_side << " last=" << last_side << std::endl;
  247. // Turn should not be left (actually, it should be right because extent removes horizontal/collinear cases)
  248. return first_side != 1 && last_side != 1;
  249. }
  250. // Gets the extreme segments (top point plus neighbouring points), plus intruders, if any, on the same ring
  251. template <typename Extremes, typename Intruders, typename SideStrategy>
  252. static inline bool apply(Ring const& ring,
  253. Extremes& extremes,
  254. Intruders& intruders,
  255. SideStrategy const& strategy)
  256. {
  257. std::size_t const n = boost::size(ring);
  258. if (n < 3)
  259. {
  260. return false;
  261. }
  262. // Get all maxima, usually one. In case of self-tangencies, or self-crossings,
  263. // the max might be is not valid. A valid max should make a right turn
  264. auto max_it = boost::begin(ring);
  265. compare<Dimension> smaller;
  266. for (auto it = max_it + 1; it != boost::end(ring); ++it)
  267. {
  268. if (smaller(*max_it, *it) && right_turn(ring, it, strategy))
  269. {
  270. max_it = it;
  271. }
  272. }
  273. if (max_it == boost::end(ring))
  274. {
  275. return false;
  276. }
  277. auto const index = std::distance(boost::begin(ring), max_it);
  278. //std::cout << "Extreme point lies at " << index << " having " << geometry::wkt(*max_it) << std::endl;
  279. geometry::ever_circling_range_iterator<Ring const> left(ring);
  280. geometry::ever_circling_range_iterator<Ring const> right(ring);
  281. left += index;
  282. right += index;
  283. // Collect all points (often 3) in a temporary vector
  284. std::vector<point_type> points;
  285. points.reserve(3);
  286. if (! collect(ring, *max_it, points, left, right))
  287. {
  288. return false;
  289. }
  290. //std::cout << "Built vector of " << points.size() << std::endl;
  291. coordinate_type const front_value = geometry::get<Dimension>(points.front());
  292. coordinate_type const back_value = geometry::get<Dimension>(points.back());
  293. coordinate_type const base_value = (std::max)(front_value, back_value);
  294. if (front_value < back_value)
  295. {
  296. move_along_vector<Dimension>(points.front(), *(points.begin() + 1), base_value);
  297. }
  298. else
  299. {
  300. move_along_vector<Dimension>(points.back(), *(points.rbegin() + 1), base_value);
  301. }
  302. std::copy(points.begin(), points.end(), std::back_inserter(extremes));
  303. get_intruders(ring, left, right, extremes, intruders, strategy);
  304. return true;
  305. }
  306. };
  307. }} // namespace detail::extreme_points
  308. #endif // DOXYGEN_NO_DETAIL
  309. #ifndef DOXYGEN_NO_DISPATCH
  310. namespace dispatch
  311. {
  312. template
  313. <
  314. typename Geometry,
  315. std::size_t Dimension,
  316. typename GeometryTag = typename tag<Geometry>::type
  317. >
  318. struct extreme_points
  319. {};
  320. template<typename Ring, std::size_t Dimension>
  321. struct extreme_points<Ring, Dimension, ring_tag>
  322. : detail::extreme_points::extreme_points_on_ring<Ring, Dimension>
  323. {};
  324. template<typename Polygon, std::size_t Dimension>
  325. struct extreme_points<Polygon, Dimension, polygon_tag>
  326. {
  327. template <typename Extremes, typename Intruders, typename SideStrategy>
  328. static inline bool apply(Polygon const& polygon, Extremes& extremes, Intruders& intruders,
  329. SideStrategy const& strategy)
  330. {
  331. typedef typename geometry::ring_type<Polygon>::type ring_type;
  332. typedef detail::extreme_points::extreme_points_on_ring
  333. <
  334. ring_type, Dimension
  335. > ring_implementation;
  336. if (! ring_implementation::apply(geometry::exterior_ring(polygon),
  337. extremes, intruders, strategy))
  338. {
  339. return false;
  340. }
  341. // For a polygon, its interior rings can contain intruders
  342. auto const& rings = interior_rings(polygon);
  343. for (auto it = boost::begin(rings); it != boost::end(rings); ++it)
  344. {
  345. ring_implementation::get_intruders(*it, extremes, intruders, strategy);
  346. }
  347. return true;
  348. }
  349. };
  350. template<typename Box>
  351. struct extreme_points<Box, 1, box_tag>
  352. {
  353. template <typename Extremes, typename Intruders, typename SideStrategy>
  354. static inline bool apply(Box const& box, Extremes& extremes, Intruders& ,
  355. SideStrategy const& )
  356. {
  357. extremes.resize(4);
  358. geometry::detail::assign_box_corners_oriented<false>(box, extremes);
  359. // ll,ul,ur,lr, contains too exactly the right info
  360. return true;
  361. }
  362. };
  363. template<typename Box>
  364. struct extreme_points<Box, 0, box_tag>
  365. {
  366. template <typename Extremes, typename Intruders, typename SideStrategy>
  367. static inline bool apply(Box const& box, Extremes& extremes, Intruders& ,
  368. SideStrategy const& )
  369. {
  370. extremes.resize(4);
  371. geometry::detail::assign_box_corners_oriented<false>(box, extremes);
  372. // ll,ul,ur,lr, rotate one to start with UL and end with LL
  373. std::rotate(extremes.begin(), extremes.begin() + 1, extremes.end());
  374. return true;
  375. }
  376. };
  377. template<typename MultiPolygon, std::size_t Dimension>
  378. struct extreme_points<MultiPolygon, Dimension, multi_polygon_tag>
  379. {
  380. template <typename Extremes, typename Intruders, typename SideStrategy>
  381. static inline bool apply(MultiPolygon const& multi, Extremes& extremes,
  382. Intruders& intruders, SideStrategy const& strategy)
  383. {
  384. // Get one for the very first polygon, that is (for the moment) enough.
  385. // It is not guaranteed the "extreme" then, but for the current purpose
  386. // (point_on_surface) it can just be this point.
  387. if (boost::size(multi) >= 1)
  388. {
  389. return extreme_points
  390. <
  391. typename boost::range_value<MultiPolygon const>::type,
  392. Dimension,
  393. polygon_tag
  394. >::apply(*boost::begin(multi), extremes, intruders, strategy);
  395. }
  396. return false;
  397. }
  398. };
  399. } // namespace dispatch
  400. #endif // DOXYGEN_NO_DISPATCH
  401. /*!
  402. \brief Returns extreme points (for Edge=1 in dimension 1, so the top,
  403. for Edge=0 in dimension 0, the right side)
  404. \note We could specify a strategy (less/greater) to get bottom/left side too. However, until now we don't need that.
  405. */
  406. template
  407. <
  408. std::size_t Edge,
  409. typename Geometry,
  410. typename Extremes,
  411. typename Intruders,
  412. typename SideStrategy
  413. >
  414. inline bool extreme_points(Geometry const& geometry,
  415. Extremes& extremes,
  416. Intruders& intruders,
  417. SideStrategy const& strategy)
  418. {
  419. concepts::check<Geometry const>();
  420. // Extremes is not required to follow a geometry concept (but it should support an output iterator),
  421. // but its elements should fulfil the point-concept
  422. concepts::check<typename boost::range_value<Extremes>::type>();
  423. // Intruders should contain collections which value type is point-concept
  424. // Extremes might be anything (supporting an output iterator), but its elements should fulfil the point-concept
  425. concepts::check
  426. <
  427. typename boost::range_value
  428. <
  429. typename boost::range_value<Intruders>::type
  430. >::type
  431. const
  432. >();
  433. return dispatch::extreme_points
  434. <
  435. Geometry,
  436. Edge
  437. >::apply(geometry, extremes, intruders, strategy);
  438. }
  439. template
  440. <
  441. std::size_t Edge,
  442. typename Geometry,
  443. typename Extremes,
  444. typename Intruders
  445. >
  446. inline bool extreme_points(Geometry const& geometry,
  447. Extremes& extremes,
  448. Intruders& intruders)
  449. {
  450. typedef typename strategy::side::services::default_strategy
  451. <
  452. typename cs_tag<Geometry>::type
  453. >::type strategy_type;
  454. return geometry::extreme_points<Edge>(geometry,extremes, intruders, strategy_type());
  455. }
  456. }} // namespace boost::geometry
  457. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXTREME_POINTS_HPP