simplify.hpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2023-2024 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2018-2023.
  7. // Modifications copyright (c) 2018-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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_ALGORITHMS_SIMPLIFY_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_SIMPLIFY_HPP
  17. #include <cstddef>
  18. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  19. #include <iostream>
  20. #endif
  21. #include <set>
  22. #include <vector>
  23. #include <boost/core/addressof.hpp>
  24. #include <boost/range/begin.hpp>
  25. #include <boost/range/end.hpp>
  26. #include <boost/range/size.hpp>
  27. #include <boost/range/value_type.hpp>
  28. #include <boost/geometry/algorithms/area.hpp>
  29. #include <boost/geometry/algorithms/clear.hpp>
  30. #include <boost/geometry/algorithms/convert.hpp>
  31. #include <boost/geometry/algorithms/detail/dummy_geometries.hpp>
  32. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  33. #include <boost/geometry/algorithms/detail/visit.hpp>
  34. #include <boost/geometry/algorithms/not_implemented.hpp>
  35. #include <boost/geometry/algorithms/is_empty.hpp>
  36. #include <boost/geometry/algorithms/perimeter.hpp>
  37. #include <boost/geometry/core/cs.hpp>
  38. #include <boost/geometry/core/closure.hpp>
  39. #include <boost/geometry/core/exterior_ring.hpp>
  40. #include <boost/geometry/core/interior_rings.hpp>
  41. #include <boost/geometry/core/mutable_range.hpp>
  42. #include <boost/geometry/core/tags.hpp>
  43. #include <boost/geometry/core/visit.hpp>
  44. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  45. #include <boost/geometry/geometries/concepts/check.hpp>
  46. #include <boost/geometry/strategies/default_strategy.hpp>
  47. #include <boost/geometry/strategies/detail.hpp>
  48. #include <boost/geometry/strategies/distance/comparable.hpp>
  49. #include <boost/geometry/strategies/simplify/cartesian.hpp>
  50. #include <boost/geometry/strategies/simplify/geographic.hpp>
  51. #include <boost/geometry/strategies/simplify/spherical.hpp>
  52. #include <boost/geometry/util/constexpr.hpp>
  53. #include <boost/geometry/util/type_traits_std.hpp>
  54. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  55. #include <boost/geometry/io/dsv/write.hpp>
  56. #endif
  57. namespace boost { namespace geometry
  58. {
  59. #ifndef DOXYGEN_NO_DETAIL
  60. namespace detail { namespace simplify
  61. {
  62. /*!
  63. \brief Small wrapper around a point, with an extra member "included"
  64. \details
  65. It has a const-reference to the original point (so no copy here)
  66. \tparam the enclosed point type
  67. */
  68. template <typename Point>
  69. struct douglas_peucker_point
  70. {
  71. typedef Point point_type;
  72. Point const* p;
  73. bool included;
  74. inline douglas_peucker_point(Point const& ap)
  75. : p(boost::addressof(ap))
  76. , included(false)
  77. {}
  78. };
  79. /*!
  80. \brief Implements the simplify algorithm.
  81. \details The douglas_peucker policy simplifies a linestring, ring or
  82. vector of points using the well-known Douglas-Peucker algorithm.
  83. \note This strategy uses itself a point-segment potentially comparable
  84. distance strategy
  85. \author Barend and Maarten, 1995/1996
  86. \author Barend, revised for Generic Geometry Library, 2008
  87. */
  88. /*
  89. For the algorithm, see for example:
  90. - http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
  91. - http://www2.dcs.hull.ac.uk/CISRG/projects/Royal-Inst/demos/dp.html
  92. */
  93. class douglas_peucker
  94. {
  95. template <typename Iterator, typename Distance, typename PSDistanceStrategy>
  96. static inline void consider(Iterator begin,
  97. Iterator end,
  98. Distance const& max_dist,
  99. int& n,
  100. PSDistanceStrategy const& ps_distance_strategy)
  101. {
  102. typedef typename std::iterator_traits<Iterator>::value_type::point_type point_type;
  103. typedef decltype(ps_distance_strategy.apply(std::declval<point_type>(),
  104. std::declval<point_type>(), std::declval<point_type>())) distance_type;
  105. std::size_t size = end - begin;
  106. // size must be at least 3
  107. // because we want to consider a candidate point in between
  108. if (size <= 2)
  109. {
  110. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  111. if (begin != end)
  112. {
  113. std::cout << "ignore between " << dsv(*(begin->p))
  114. << " and " << dsv(*((end - 1)->p))
  115. << " size=" << size << std::endl;
  116. }
  117. std::cout << "return because size=" << size << std::endl;
  118. #endif
  119. return;
  120. }
  121. Iterator last = end - 1;
  122. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  123. std::cout << "find between " << dsv(*(begin->p))
  124. << " and " << dsv(*(last->p))
  125. << " size=" << size << std::endl;
  126. #endif
  127. // Find most far point, compare to the current segment
  128. //geometry::segment<Point const> s(begin->p, last->p);
  129. distance_type md(-1.0); // any value < 0
  130. Iterator candidate = end;
  131. for (Iterator it = begin + 1; it != last; ++it)
  132. {
  133. distance_type dist = ps_distance_strategy.apply(*(it->p), *(begin->p), *(last->p));
  134. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  135. std::cout << "consider " << dsv(*(it->p))
  136. << " at " << double(dist)
  137. << ((dist > max_dist) ? " maybe" : " no")
  138. << std::endl;
  139. #endif
  140. if (md < dist)
  141. {
  142. md = dist;
  143. candidate = it;
  144. }
  145. }
  146. // If a point is found, set the include flag
  147. // and handle segments in between recursively
  148. if (max_dist < md && candidate != end)
  149. {
  150. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  151. std::cout << "use " << dsv(candidate->p) << std::endl;
  152. #endif
  153. candidate->included = true;
  154. n++;
  155. consider(begin, candidate + 1, max_dist, n, ps_distance_strategy);
  156. consider(candidate, end, max_dist, n, ps_distance_strategy);
  157. }
  158. }
  159. template
  160. <
  161. typename Range, typename OutputIterator, typename Distance,
  162. typename PSDistanceStrategy
  163. >
  164. static inline OutputIterator apply_(Range const& range,
  165. OutputIterator out,
  166. Distance const& max_distance,
  167. PSDistanceStrategy const& ps_distance_strategy)
  168. {
  169. #ifdef BOOST_GEOMETRY_DEBUG_DOUGLAS_PEUCKER
  170. std::cout << "max distance: " << max_distance
  171. << std::endl << std::endl;
  172. #endif
  173. typedef typename boost::range_value<Range>::type point_type;
  174. typedef douglas_peucker_point<point_type> dp_point_type;
  175. // Copy coordinates, a vector of references to all points
  176. std::vector<dp_point_type> ref_candidates(boost::begin(range),
  177. boost::end(range));
  178. // Include first and last point of line,
  179. // they are always part of the line
  180. int n = 2;
  181. ref_candidates.front().included = true;
  182. ref_candidates.back().included = true;
  183. // Get points, recursively, including them if they are further away
  184. // than the specified distance
  185. consider(boost::begin(ref_candidates), boost::end(ref_candidates), max_distance, n,
  186. ps_distance_strategy);
  187. // Copy included elements to the output
  188. for (auto it = boost::begin(ref_candidates); it != boost::end(ref_candidates); ++it)
  189. {
  190. if (it->included)
  191. {
  192. // copy-coordinates does not work because OutputIterator
  193. // does not model Point (??)
  194. //geometry::convert(*(it->p), *out);
  195. *out = *(it->p);
  196. ++out;
  197. }
  198. }
  199. return out;
  200. }
  201. public:
  202. template <typename Range, typename OutputIterator, typename Distance, typename Strategies>
  203. static inline OutputIterator apply(Range const& range,
  204. OutputIterator out,
  205. Distance const& max_distance,
  206. Strategies const& strategies)
  207. {
  208. typedef typename boost::range_value<Range>::type point_type;
  209. typedef decltype(strategies.distance(detail::dummy_point(), detail::dummy_segment())) distance_strategy_type;
  210. typedef typename strategy::distance::services::comparable_type
  211. <
  212. distance_strategy_type
  213. >::type comparable_distance_strategy_type;
  214. comparable_distance_strategy_type cstrategy = strategy::distance::services::get_comparable
  215. <
  216. distance_strategy_type
  217. >::apply(strategies.distance(detail::dummy_point(), detail::dummy_segment()));
  218. return apply_(range, out,
  219. strategy::distance::services::result_from_distance
  220. <
  221. comparable_distance_strategy_type, point_type, point_type
  222. >::apply(cstrategy, max_distance),
  223. cstrategy);
  224. }
  225. };
  226. template <typename Range, typename Strategies>
  227. inline bool is_degenerate(Range const& range, Strategies const& strategies)
  228. {
  229. return boost::size(range) == 2
  230. && detail::equals::equals_point_point(geometry::range::front(range),
  231. geometry::range::back(range),
  232. strategies);
  233. }
  234. struct simplify_range_insert
  235. {
  236. template
  237. <
  238. typename Range, typename OutputIterator, typename Distance,
  239. typename Impl, typename Strategies
  240. >
  241. static inline void apply(Range const& range, OutputIterator out,
  242. Distance const& max_distance,
  243. Impl const& impl,
  244. Strategies const& strategies)
  245. {
  246. if (is_degenerate(range, strategies))
  247. {
  248. std::copy(boost::begin(range), boost::begin(range) + 1, out);
  249. }
  250. else if (boost::size(range) <= 2 || max_distance < 0)
  251. {
  252. std::copy(boost::begin(range), boost::end(range), out);
  253. }
  254. else
  255. {
  256. impl.apply(range, out, max_distance, strategies);
  257. }
  258. }
  259. };
  260. struct simplify_copy_assign
  261. {
  262. template
  263. <
  264. typename In, typename Out, typename Distance,
  265. typename Impl, typename Strategies
  266. >
  267. static inline void apply(In const& in, Out& out,
  268. Distance const& ,
  269. Impl const& ,
  270. Strategies const& )
  271. {
  272. out = in;
  273. }
  274. };
  275. struct simplify_copy
  276. {
  277. template
  278. <
  279. typename RangeIn, typename RangeOut, typename Distance,
  280. typename Impl, typename Strategies
  281. >
  282. static inline void apply(RangeIn const& range, RangeOut& out,
  283. Distance const& ,
  284. Impl const& ,
  285. Strategies const& )
  286. {
  287. std::copy(boost::begin(range), boost::end(range),
  288. geometry::range::back_inserter(out));
  289. }
  290. };
  291. template <std::size_t MinimumToUseStrategy>
  292. struct simplify_range
  293. {
  294. template
  295. <
  296. typename RangeIn, typename RangeOut, typename Distance,
  297. typename Impl, typename Strategies
  298. >
  299. static inline void apply(RangeIn const& range, RangeOut& out,
  300. Distance const& max_distance,
  301. Impl const& impl,
  302. Strategies const& strategies)
  303. {
  304. // For a RING:
  305. // Note that, especially if max_distance is too large,
  306. // the output ring might be self intersecting while the input ring is
  307. // not, although chances are low in normal polygons
  308. if (boost::size(range) <= MinimumToUseStrategy || max_distance < 0)
  309. {
  310. simplify_copy::apply(range, out, max_distance, impl, strategies);
  311. }
  312. else
  313. {
  314. simplify_range_insert::apply(range, geometry::range::back_inserter(out),
  315. max_distance, impl, strategies);
  316. }
  317. // Verify the two remaining points are equal. If so, remove one of them.
  318. // This can cause the output being under the minimum size
  319. if (is_degenerate(out, strategies))
  320. {
  321. range::resize(out, 1);
  322. }
  323. }
  324. };
  325. struct simplify_ring
  326. {
  327. private :
  328. template <typename Area>
  329. static inline int area_sign(Area const& area)
  330. {
  331. return area > 0 ? 1 : area < 0 ? -1 : 0;
  332. }
  333. template <typename Ring, typename Strategies>
  334. static std::size_t get_opposite(std::size_t index, Ring const& ring,
  335. Strategies const& strategies)
  336. {
  337. // TODO: Instead of calling the strategy call geometry::comparable_distance() ?
  338. auto const cdistance_strategy = strategies::distance::detail::make_comparable(strategies)
  339. .distance(detail::dummy_point(), detail::dummy_point());
  340. using point_type = geometry::point_type_t<Ring>;
  341. using cdistance_type = decltype(cdistance_strategy.apply(
  342. std::declval<point_type>(), std::declval<point_type>()));
  343. // Verify if it is NOT the case that all points are less than the
  344. // simplifying distance. If so, output is empty.
  345. cdistance_type max_cdistance(-1);
  346. point_type const& point = range::at(ring, index);
  347. std::size_t i = 0;
  348. for (auto it = boost::begin(ring); it != boost::end(ring); ++it, ++i)
  349. {
  350. cdistance_type const cdistance = cdistance_strategy.apply(*it, point);
  351. if (cdistance > max_cdistance)
  352. {
  353. max_cdistance = cdistance;
  354. index = i;
  355. }
  356. }
  357. return index;
  358. }
  359. public :
  360. template
  361. <
  362. typename RingIn, typename RingOut,
  363. typename Distance, typename Impl, typename Strategies
  364. >
  365. static inline void apply(RingIn const& ring, RingOut& out, Distance const& max_distance,
  366. Impl const& impl, Strategies const& strategies)
  367. {
  368. std::size_t const size = boost::size(ring);
  369. if (size == 0)
  370. {
  371. return;
  372. }
  373. constexpr bool is_closed_in = geometry::closure<RingIn>::value == closed;
  374. constexpr bool is_closed_out = geometry::closure<RingOut>::value == closed;
  375. constexpr bool is_clockwise_in = geometry::point_order<RingIn>::value == clockwise;
  376. constexpr bool is_clockwise_out = geometry::point_order<RingOut>::value == clockwise;
  377. // TODO: instead of area() use calculate_point_order() ?
  378. int const input_sign = area_sign(geometry::area(ring, strategies));
  379. std::set<std::size_t> visited_indexes;
  380. // Rotate it into a copied vector
  381. // (vector, because source type might not support rotation)
  382. // (duplicate end point will be simplified away)
  383. using point_type = geometry::point_type_t<RingIn>;
  384. std::vector<point_type> rotated;
  385. rotated.reserve(size + 1); // 1 because open rings are closed
  386. // Closing point (but it will not start here)
  387. std::size_t index = 0;
  388. // Iterate (usually one iteration is enough)
  389. for (std::size_t iteration = 0; iteration < 4u; iteration++)
  390. {
  391. // Always take the opposite. Opposite guarantees that no point
  392. // "halfway" is chosen, creating an artefact (very narrow triangle)
  393. // Iteration 0: opposite to closing point (1/2, = on convex hull)
  394. // (this will start simplification with that point
  395. // and its opposite ~0)
  396. // Iteration 1: move a quarter on that ring, then opposite to 1/4
  397. // (with its opposite 3/4)
  398. // Iteration 2: move an eight on that ring, then opposite (1/8)
  399. // Iteration 3: again move a quarter, then opposite (7/8)
  400. // So finally 8 "sides" of the ring have been examined (if it were
  401. // a semi-circle). Most probably, there are only 0 or 1 iterations.
  402. switch (iteration)
  403. {
  404. case 1 : index = (index + size / 4) % size; break;
  405. case 2 : index = (index + size / 8) % size; break;
  406. case 3 : index = (index + size / 4) % size; break;
  407. }
  408. index = get_opposite(index, ring, strategies);
  409. if (visited_indexes.count(index) > 0)
  410. {
  411. // Avoid trying the same starting point more than once
  412. continue;
  413. }
  414. // Do not duplicate the closing point
  415. auto rot_end = boost::end(ring);
  416. std::size_t rot_index = index;
  417. if BOOST_GEOMETRY_CONSTEXPR (is_closed_in)
  418. {
  419. if (size > 1)
  420. {
  421. --rot_end;
  422. if (rot_index == size - 1) { rot_index = 0; }
  423. }
  424. }
  425. std::rotate_copy(boost::begin(ring), range::pos(ring, rot_index),
  426. rot_end, std::back_inserter(rotated));
  427. // Close the rotated copy
  428. rotated.push_back(range::at(ring, rot_index));
  429. simplify_range<0>::apply(rotated, out, max_distance, impl, strategies);
  430. // Open output if needed
  431. if BOOST_GEOMETRY_CONSTEXPR (! is_closed_out)
  432. {
  433. if (boost::size(out) > 1)
  434. {
  435. range::pop_back(out);
  436. }
  437. }
  438. // TODO: instead of area() use calculate_point_order() ?
  439. // Verify that what was positive, stays positive (or goes to 0)
  440. // and what was negative stays negative (or goes to 0)
  441. int const output_sign = area_sign(geometry::area(out, strategies));
  442. if (output_sign == input_sign)
  443. {
  444. // Result is considered as satisfactory (usually this is the
  445. // first iteration - only for small rings, having a scale
  446. // similar to simplify_distance, next iterations are tried
  447. return;
  448. }
  449. // Original is simplified away. Possibly there is a solution
  450. // when another starting point is used
  451. geometry::clear(out);
  452. if (iteration == 0
  453. && geometry::perimeter(ring, strategies) < 3 * max_distance)
  454. {
  455. // Check if it is useful to iterate. A minimal triangle has a
  456. // perimeter of a bit more than 3 times the simplify distance
  457. return;
  458. }
  459. // Prepare next try
  460. visited_indexes.insert(index);
  461. rotated.clear();
  462. }
  463. if BOOST_GEOMETRY_CONSTEXPR (is_clockwise_in != is_clockwise_out)
  464. {
  465. std::reverse(boost::begin(out), boost::end(out));
  466. }
  467. }
  468. };
  469. struct simplify_polygon
  470. {
  471. private:
  472. template
  473. <
  474. typename IteratorIn,
  475. typename InteriorRingsOut,
  476. typename Distance,
  477. typename Impl,
  478. typename Strategies
  479. >
  480. static inline void iterate(IteratorIn begin, IteratorIn end,
  481. InteriorRingsOut& interior_rings_out,
  482. Distance const& max_distance,
  483. Impl const& impl, Strategies const& strategies)
  484. {
  485. typedef typename boost::range_value<InteriorRingsOut>::type single_type;
  486. for (IteratorIn it = begin; it != end; ++it)
  487. {
  488. single_type out;
  489. simplify_ring::apply(*it, out, max_distance, impl, strategies);
  490. if (! geometry::is_empty(out))
  491. {
  492. range::push_back(interior_rings_out, std::move(out));
  493. }
  494. }
  495. }
  496. template
  497. <
  498. typename InteriorRingsIn,
  499. typename InteriorRingsOut,
  500. typename Distance,
  501. typename Impl,
  502. typename Strategies
  503. >
  504. static inline void apply_interior_rings(InteriorRingsIn const& interior_rings_in,
  505. InteriorRingsOut& interior_rings_out,
  506. Distance const& max_distance,
  507. Impl const& impl, Strategies const& strategies)
  508. {
  509. range::clear(interior_rings_out);
  510. iterate(boost::begin(interior_rings_in), boost::end(interior_rings_in),
  511. interior_rings_out,
  512. max_distance,
  513. impl, strategies);
  514. }
  515. public:
  516. template
  517. <
  518. typename PolygonIn, typename PolygonOut,
  519. typename Distance, typename Impl, typename Strategies
  520. >
  521. static inline void apply(PolygonIn const& poly_in, PolygonOut& poly_out,
  522. Distance const& max_distance,
  523. Impl const& impl, Strategies const& strategies)
  524. {
  525. // Note that if there are inner rings, and distance is too large,
  526. // they might intersect with the outer ring in the output,
  527. // while it didn't in the input.
  528. simplify_ring::apply(exterior_ring(poly_in), exterior_ring(poly_out),
  529. max_distance, impl, strategies);
  530. apply_interior_rings(interior_rings(poly_in), interior_rings(poly_out),
  531. max_distance, impl, strategies);
  532. }
  533. };
  534. template<typename Policy>
  535. struct simplify_multi
  536. {
  537. template
  538. <
  539. typename MultiGeometryIn, typename MultiGeometryOut,
  540. typename Distance, typename Impl, typename Strategies
  541. >
  542. static inline void apply(MultiGeometryIn const& multi, MultiGeometryOut& out,
  543. Distance const& max_distance,
  544. Impl const& impl, Strategies const& strategies)
  545. {
  546. range::clear(out);
  547. using single_type = typename boost::range_value<MultiGeometryOut>::type;
  548. for (auto it = boost::begin(multi); it != boost::end(multi); ++it)
  549. {
  550. single_type single_out;
  551. Policy::apply(*it, single_out, max_distance, impl, strategies);
  552. if (! geometry::is_empty(single_out))
  553. {
  554. range::push_back(out, std::move(single_out));
  555. }
  556. }
  557. }
  558. };
  559. template <typename Geometry>
  560. struct has_same_tag_as
  561. {
  562. template <typename OtherGeometry>
  563. struct pred
  564. : std::is_same<geometry::tag_t<Geometry>, geometry::tag_t<OtherGeometry>>
  565. {};
  566. };
  567. template <typename StaticGeometryIn, typename DynamicGeometryOut>
  568. struct static_geometry_type
  569. {
  570. using type = typename util::sequence_find_if
  571. <
  572. typename traits::geometry_types<DynamicGeometryOut>::type,
  573. detail::simplify::has_same_tag_as<StaticGeometryIn>::template pred
  574. >::type;
  575. BOOST_GEOMETRY_STATIC_ASSERT(
  576. (! std::is_void<type>::value),
  577. "Unable to find corresponding geometry in GeometryOut",
  578. StaticGeometryIn, DynamicGeometryOut);
  579. };
  580. }} // namespace detail::simplify
  581. #endif // DOXYGEN_NO_DETAIL
  582. #ifndef DOXYGEN_NO_DISPATCH
  583. namespace dispatch
  584. {
  585. template
  586. <
  587. typename GeometryIn,
  588. typename GeometryOut,
  589. typename TagIn = tag_t<GeometryIn>,
  590. typename TagOut = tag_t<GeometryOut>
  591. >
  592. struct simplify: not_implemented<TagIn, TagOut>
  593. {};
  594. template <typename PointIn, typename PointOut>
  595. struct simplify<PointIn, PointOut, point_tag, point_tag>
  596. {
  597. template <typename Distance, typename Impl, typename Strategy>
  598. static inline void apply(PointIn const& point, PointOut& out, Distance const& ,
  599. Impl const& , Strategy const& )
  600. {
  601. geometry::convert(point, out);
  602. }
  603. };
  604. template <typename SegmentIn, typename SegmentOut>
  605. struct simplify<SegmentIn, SegmentOut, segment_tag, segment_tag>
  606. : detail::simplify::simplify_copy_assign
  607. {};
  608. template <typename BoxIn, typename BoxOut>
  609. struct simplify<BoxIn, BoxOut, box_tag, box_tag>
  610. : detail::simplify::simplify_copy_assign
  611. {};
  612. // Linestring, keep 2 points (unless those points are the same)
  613. template <typename LinestringIn, typename LinestringOut>
  614. struct simplify<LinestringIn, LinestringOut, linestring_tag, linestring_tag>
  615. : detail::simplify::simplify_range<2>
  616. {};
  617. template <typename RingIn, typename RingOut>
  618. struct simplify<RingIn, RingOut, ring_tag, ring_tag>
  619. : detail::simplify::simplify_ring
  620. {};
  621. template <typename PolygonIn, typename PolygonOut>
  622. struct simplify<PolygonIn, PolygonOut, polygon_tag, polygon_tag>
  623. : detail::simplify::simplify_polygon
  624. {};
  625. template <typename MultiPointIn, typename MultiPointOut>
  626. struct simplify<MultiPointIn, MultiPointOut, multi_point_tag, multi_point_tag>
  627. : detail::simplify::simplify_copy
  628. {};
  629. template <typename MultiLinestringIn, typename MultiLinestringOut>
  630. struct simplify<MultiLinestringIn, MultiLinestringOut, multi_linestring_tag, multi_linestring_tag>
  631. : detail::simplify::simplify_multi<detail::simplify::simplify_range<2> >
  632. {};
  633. template <typename MultiPolygonIn, typename MultiPolygonOut>
  634. struct simplify<MultiPolygonIn, MultiPolygonOut, multi_polygon_tag, multi_polygon_tag>
  635. : detail::simplify::simplify_multi<detail::simplify::simplify_polygon>
  636. {};
  637. template
  638. <
  639. typename Geometry,
  640. typename Tag = tag_t<Geometry>
  641. >
  642. struct simplify_insert: not_implemented<Tag>
  643. {};
  644. template <typename Linestring>
  645. struct simplify_insert<Linestring, linestring_tag>
  646. : detail::simplify::simplify_range_insert
  647. {};
  648. template <typename Ring>
  649. struct simplify_insert<Ring, ring_tag>
  650. : detail::simplify::simplify_range_insert
  651. {};
  652. } // namespace dispatch
  653. #endif // DOXYGEN_NO_DISPATCH
  654. namespace resolve_strategy
  655. {
  656. template
  657. <
  658. typename Strategies,
  659. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  660. >
  661. struct simplify
  662. {
  663. template <typename GeometryIn, typename GeometryOut, typename Distance>
  664. static inline void apply(GeometryIn const& geometry,
  665. GeometryOut& out,
  666. Distance const& max_distance,
  667. Strategies const& strategies)
  668. {
  669. dispatch::simplify
  670. <
  671. GeometryIn, GeometryOut
  672. >::apply(geometry, out, max_distance,
  673. detail::simplify::douglas_peucker(),
  674. strategies);
  675. }
  676. };
  677. template <typename Strategy>
  678. struct simplify<Strategy, false>
  679. {
  680. template <typename GeometryIn, typename GeometryOut, typename Distance>
  681. static inline void apply(GeometryIn const& geometry,
  682. GeometryOut& out,
  683. Distance const& max_distance,
  684. Strategy const& strategy)
  685. {
  686. using strategies::simplify::services::strategy_converter;
  687. simplify
  688. <
  689. decltype(strategy_converter<Strategy>::get(strategy))
  690. >::apply(geometry, out, max_distance,
  691. strategy_converter<Strategy>::get(strategy));
  692. }
  693. };
  694. template <>
  695. struct simplify<default_strategy, false>
  696. {
  697. template <typename GeometryIn, typename GeometryOut, typename Distance>
  698. static inline void apply(GeometryIn const& geometry,
  699. GeometryOut& out,
  700. Distance const& max_distance,
  701. default_strategy)
  702. {
  703. // NOTE: Alternatively take two geometry types in default_strategy
  704. using cs_tag1_t = geometry::cs_tag_t<GeometryIn>;
  705. using cs_tag2_t = geometry::cs_tag_t<GeometryOut>;
  706. BOOST_GEOMETRY_STATIC_ASSERT(
  707. (std::is_same<cs_tag1_t, cs_tag2_t>::value),
  708. "Incompatible coordinate systems",
  709. cs_tag1_t, cs_tag2_t);
  710. typedef typename strategies::simplify::services::default_strategy
  711. <
  712. GeometryIn
  713. >::type strategy_type;
  714. simplify
  715. <
  716. strategy_type
  717. >::apply(geometry, out, max_distance, strategy_type());
  718. }
  719. };
  720. template
  721. <
  722. typename Strategies,
  723. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  724. >
  725. struct simplify_insert
  726. {
  727. template<typename Geometry, typename OutputIterator, typename Distance>
  728. static inline void apply(Geometry const& geometry,
  729. OutputIterator& out,
  730. Distance const& max_distance,
  731. Strategies const& strategies)
  732. {
  733. dispatch::simplify_insert
  734. <
  735. Geometry
  736. >::apply(geometry, out, max_distance,
  737. detail::simplify::douglas_peucker(),
  738. strategies);
  739. }
  740. };
  741. template <typename Strategy>
  742. struct simplify_insert<Strategy, false>
  743. {
  744. template<typename Geometry, typename OutputIterator, typename Distance>
  745. static inline void apply(Geometry const& geometry,
  746. OutputIterator& out,
  747. Distance const& max_distance,
  748. Strategy const& strategy)
  749. {
  750. using strategies::simplify::services::strategy_converter;
  751. simplify_insert
  752. <
  753. decltype(strategy_converter<Strategy>::get(strategy))
  754. >::apply(geometry, out, max_distance,
  755. strategy_converter<Strategy>::get(strategy));
  756. }
  757. };
  758. template <>
  759. struct simplify_insert<default_strategy, false>
  760. {
  761. template <typename Geometry, typename OutputIterator, typename Distance>
  762. static inline void apply(Geometry const& geometry,
  763. OutputIterator& out,
  764. Distance const& max_distance,
  765. default_strategy)
  766. {
  767. typedef typename strategies::simplify::services::default_strategy
  768. <
  769. Geometry
  770. >::type strategy_type;
  771. simplify_insert
  772. <
  773. strategy_type
  774. >::apply(geometry, out, max_distance, strategy_type());
  775. }
  776. };
  777. } // namespace resolve_strategy
  778. namespace resolve_dynamic {
  779. template
  780. <
  781. typename GeometryIn, typename GeometryOut,
  782. typename TagIn = tag_t<GeometryIn>,
  783. typename TagOut = tag_t<GeometryOut>
  784. >
  785. struct simplify
  786. {
  787. template <typename Distance, typename Strategy>
  788. static inline void apply(GeometryIn const& geometry,
  789. GeometryOut& out,
  790. Distance const& max_distance,
  791. Strategy const& strategy)
  792. {
  793. resolve_strategy::simplify<Strategy>::apply(geometry, out, max_distance, strategy);
  794. }
  795. };
  796. template <typename GeometryIn, typename GeometryOut>
  797. struct simplify<GeometryIn, GeometryOut, dynamic_geometry_tag, dynamic_geometry_tag>
  798. {
  799. template <typename Distance, typename Strategy>
  800. static inline void apply(GeometryIn const& geometry,
  801. GeometryOut& out,
  802. Distance const& max_distance,
  803. Strategy const& strategy)
  804. {
  805. traits::visit<GeometryIn>::apply([&](auto const& g)
  806. {
  807. using geom_t = util::remove_cref_t<decltype(g)>;
  808. using detail::simplify::static_geometry_type;
  809. using geom_out_t = typename static_geometry_type<geom_t, GeometryOut>::type;
  810. geom_out_t o;
  811. simplify<geom_t, geom_out_t>::apply(g, o, max_distance, strategy);
  812. out = std::move(o);
  813. }, geometry);
  814. }
  815. };
  816. template <typename GeometryIn, typename GeometryOut>
  817. struct simplify<GeometryIn, GeometryOut, geometry_collection_tag, geometry_collection_tag>
  818. {
  819. template <typename Distance, typename Strategy>
  820. static inline void apply(GeometryIn const& geometry,
  821. GeometryOut& out,
  822. Distance const& max_distance,
  823. Strategy const& strategy)
  824. {
  825. detail::visit_breadth_first([&](auto const& g)
  826. {
  827. using geom_t = util::remove_cref_t<decltype(g)>;
  828. using detail::simplify::static_geometry_type;
  829. using geom_out_t = typename static_geometry_type<geom_t, GeometryOut>::type;
  830. geom_out_t o;
  831. simplify<geom_t, geom_out_t>::apply(g, o, max_distance, strategy);
  832. traits::emplace_back<GeometryOut>::apply(out, std::move(o));
  833. return true;
  834. }, geometry);
  835. }
  836. };
  837. } // namespace resolve_dynamic
  838. /*!
  839. \brief Simplify a geometry using a specified strategy
  840. \ingroup simplify
  841. \tparam Geometry \tparam_geometry
  842. \tparam GeometryOut The output geometry
  843. \tparam Distance A numerical distance measure
  844. \tparam Strategy A type fulfilling a SimplifyStrategy concept
  845. \param geometry input geometry, to be simplified
  846. \param out output geometry, simplified version of the input geometry
  847. \param max_distance distance (in units of input coordinates) of a vertex
  848. to other segments to be removed
  849. \param strategy simplify strategy to be used for simplification
  850. \note The simplification is done with Douglas-Peucker algorithm
  851. \image html svg_simplify_country.png "The image below presents the simplified country"
  852. \qbk{distinguish,with strategy}
  853. */
  854. template<typename Geometry, typename GeometryOut, typename Distance, typename Strategy>
  855. inline void simplify(Geometry const& geometry, GeometryOut& out,
  856. Distance const& max_distance, Strategy const& strategy)
  857. {
  858. concepts::check<Geometry const>();
  859. concepts::check<GeometryOut>();
  860. geometry::clear(out);
  861. resolve_dynamic::simplify<Geometry, GeometryOut>::apply(geometry, out, max_distance, strategy);
  862. }
  863. /*!
  864. \brief Simplify a geometry
  865. \ingroup simplify
  866. \tparam Geometry \tparam_geometry
  867. \tparam GeometryOut The output geometry
  868. \tparam Distance \tparam_numeric
  869. \param geometry input geometry, to be simplified
  870. \param out output geometry, simplified version of the input geometry
  871. \param max_distance distance (in units of input coordinates) of a vertex
  872. to other segments to be removed
  873. \note The simplification is done with Douglas-Peucker algorithm
  874. \qbk{[include reference/algorithms/simplify.qbk]}
  875. */
  876. template<typename Geometry, typename GeometryOut, typename Distance>
  877. inline void simplify(Geometry const& geometry, GeometryOut& out,
  878. Distance const& max_distance)
  879. {
  880. concepts::check<Geometry const>();
  881. concepts::check<GeometryOut>();
  882. geometry::simplify(geometry, out, max_distance, default_strategy());
  883. }
  884. #ifndef DOXYGEN_NO_DETAIL
  885. namespace detail { namespace simplify
  886. {
  887. /*!
  888. \brief Simplify a geometry, using an output iterator
  889. and a specified strategy
  890. \ingroup simplify
  891. \tparam Geometry \tparam_geometry
  892. \param geometry input geometry, to be simplified
  893. \param out output iterator, outputs all simplified points
  894. \param max_distance distance (in units of input coordinates) of a vertex
  895. to other segments to be removed
  896. \param strategy simplify strategy to be used for simplification
  897. \qbk{distinguish,with strategy}
  898. \qbk{[include reference/algorithms/simplify.qbk]}
  899. */
  900. template<typename Geometry, typename OutputIterator, typename Distance, typename Strategy>
  901. inline void simplify_insert(Geometry const& geometry, OutputIterator out,
  902. Distance const& max_distance, Strategy const& strategy)
  903. {
  904. concepts::check<Geometry const>();
  905. resolve_strategy::simplify_insert<Strategy>::apply(geometry, out, max_distance, strategy);
  906. }
  907. /*!
  908. \brief Simplify a geometry, using an output iterator
  909. \ingroup simplify
  910. \tparam Geometry \tparam_geometry
  911. \param geometry input geometry, to be simplified
  912. \param out output iterator, outputs all simplified points
  913. \param max_distance distance (in units of input coordinates) of a vertex
  914. to other segments to be removed
  915. \qbk{[include reference/algorithms/simplify_insert.qbk]}
  916. */
  917. template<typename Geometry, typename OutputIterator, typename Distance>
  918. inline void simplify_insert(Geometry const& geometry, OutputIterator out,
  919. Distance const& max_distance)
  920. {
  921. // Concept: output point type = point type of input geometry
  922. concepts::check<Geometry const>();
  923. concepts::check<point_type_t<Geometry>>();
  924. simplify_insert(geometry, out, max_distance, default_strategy());
  925. }
  926. }} // namespace detail::simplify
  927. #endif // DOXYGEN_NO_DETAIL
  928. }} // namespace boost::geometry
  929. #endif // BOOST_GEOMETRY_ALGORITHMS_SIMPLIFY_HPP