simplify.hpp 37 KB

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