simplify.hpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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 = typename geometry::point_type<Ring>::type;
  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. typedef typename geometry::point_type<RingIn>::type point_type;
  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
  565. <
  566. typename geometry::tag<Geometry>::type,
  567. typename geometry::tag<OtherGeometry>::type
  568. >
  569. {};
  570. };
  571. template <typename StaticGeometryIn, typename DynamicGeometryOut>
  572. struct static_geometry_type
  573. {
  574. using type = typename util::sequence_find_if
  575. <
  576. typename traits::geometry_types<DynamicGeometryOut>::type,
  577. detail::simplify::has_same_tag_as<StaticGeometryIn>::template pred
  578. >::type;
  579. BOOST_GEOMETRY_STATIC_ASSERT(
  580. (! std::is_void<type>::value),
  581. "Unable to find corresponding geometry in GeometryOut",
  582. StaticGeometryIn, DynamicGeometryOut);
  583. };
  584. }} // namespace detail::simplify
  585. #endif // DOXYGEN_NO_DETAIL
  586. #ifndef DOXYGEN_NO_DISPATCH
  587. namespace dispatch
  588. {
  589. template
  590. <
  591. typename GeometryIn,
  592. typename GeometryOut,
  593. typename TagIn = typename tag<GeometryIn>::type,
  594. typename TagOut = typename tag<GeometryOut>::type
  595. >
  596. struct simplify: not_implemented<TagIn, TagOut>
  597. {};
  598. template <typename PointIn, typename PointOut>
  599. struct simplify<PointIn, PointOut, point_tag, point_tag>
  600. {
  601. template <typename Distance, typename Impl, typename Strategy>
  602. static inline void apply(PointIn const& point, PointOut& out, Distance const& ,
  603. Impl const& , Strategy const& )
  604. {
  605. geometry::convert(point, out);
  606. }
  607. };
  608. template <typename SegmentIn, typename SegmentOut>
  609. struct simplify<SegmentIn, SegmentOut, segment_tag, segment_tag>
  610. : detail::simplify::simplify_copy_assign
  611. {};
  612. template <typename BoxIn, typename BoxOut>
  613. struct simplify<BoxIn, BoxOut, box_tag, box_tag>
  614. : detail::simplify::simplify_copy_assign
  615. {};
  616. // Linestring, keep 2 points (unless those points are the same)
  617. template <typename LinestringIn, typename LinestringOut>
  618. struct simplify<LinestringIn, LinestringOut, linestring_tag, linestring_tag>
  619. : detail::simplify::simplify_range<2>
  620. {};
  621. template <typename RingIn, typename RingOut>
  622. struct simplify<RingIn, RingOut, ring_tag, ring_tag>
  623. : detail::simplify::simplify_ring
  624. {};
  625. template <typename PolygonIn, typename PolygonOut>
  626. struct simplify<PolygonIn, PolygonOut, polygon_tag, polygon_tag>
  627. : detail::simplify::simplify_polygon
  628. {};
  629. template <typename MultiPointIn, typename MultiPointOut>
  630. struct simplify<MultiPointIn, MultiPointOut, multi_point_tag, multi_point_tag>
  631. : detail::simplify::simplify_copy
  632. {};
  633. template <typename MultiLinestringIn, typename MultiLinestringOut>
  634. struct simplify<MultiLinestringIn, MultiLinestringOut, multi_linestring_tag, multi_linestring_tag>
  635. : detail::simplify::simplify_multi<detail::simplify::simplify_range<2> >
  636. {};
  637. template <typename MultiPolygonIn, typename MultiPolygonOut>
  638. struct simplify<MultiPolygonIn, MultiPolygonOut, multi_polygon_tag, multi_polygon_tag>
  639. : detail::simplify::simplify_multi<detail::simplify::simplify_polygon>
  640. {};
  641. template
  642. <
  643. typename Geometry,
  644. typename Tag = typename tag<Geometry>::type
  645. >
  646. struct simplify_insert: not_implemented<Tag>
  647. {};
  648. template <typename Linestring>
  649. struct simplify_insert<Linestring, linestring_tag>
  650. : detail::simplify::simplify_range_insert
  651. {};
  652. template <typename Ring>
  653. struct simplify_insert<Ring, ring_tag>
  654. : detail::simplify::simplify_range_insert
  655. {};
  656. } // namespace dispatch
  657. #endif // DOXYGEN_NO_DISPATCH
  658. namespace resolve_strategy
  659. {
  660. template
  661. <
  662. typename Strategies,
  663. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  664. >
  665. struct simplify
  666. {
  667. template <typename GeometryIn, typename GeometryOut, typename Distance>
  668. static inline void apply(GeometryIn const& geometry,
  669. GeometryOut& out,
  670. Distance const& max_distance,
  671. Strategies const& strategies)
  672. {
  673. dispatch::simplify
  674. <
  675. GeometryIn, GeometryOut
  676. >::apply(geometry, out, max_distance,
  677. detail::simplify::douglas_peucker(),
  678. strategies);
  679. }
  680. };
  681. template <typename Strategy>
  682. struct simplify<Strategy, false>
  683. {
  684. template <typename GeometryIn, typename GeometryOut, typename Distance>
  685. static inline void apply(GeometryIn const& geometry,
  686. GeometryOut& out,
  687. Distance const& max_distance,
  688. Strategy const& strategy)
  689. {
  690. using strategies::simplify::services::strategy_converter;
  691. simplify
  692. <
  693. decltype(strategy_converter<Strategy>::get(strategy))
  694. >::apply(geometry, out, max_distance,
  695. strategy_converter<Strategy>::get(strategy));
  696. }
  697. };
  698. template <>
  699. struct simplify<default_strategy, false>
  700. {
  701. template <typename GeometryIn, typename GeometryOut, typename Distance>
  702. static inline void apply(GeometryIn const& geometry,
  703. GeometryOut& out,
  704. Distance const& max_distance,
  705. default_strategy)
  706. {
  707. // NOTE: Alternatively take two geometry types in default_strategy
  708. using cs_tag1_t = typename geometry::cs_tag<GeometryIn>::type;
  709. using cs_tag2_t = typename geometry::cs_tag<GeometryOut>::type;
  710. BOOST_GEOMETRY_STATIC_ASSERT(
  711. (std::is_same<cs_tag1_t, cs_tag2_t>::value),
  712. "Incompatible coordinate systems",
  713. cs_tag1_t, cs_tag2_t);
  714. typedef typename strategies::simplify::services::default_strategy
  715. <
  716. GeometryIn
  717. >::type strategy_type;
  718. simplify
  719. <
  720. strategy_type
  721. >::apply(geometry, out, max_distance, strategy_type());
  722. }
  723. };
  724. template
  725. <
  726. typename Strategies,
  727. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategies>::value
  728. >
  729. struct simplify_insert
  730. {
  731. template<typename Geometry, typename OutputIterator, typename Distance>
  732. static inline void apply(Geometry const& geometry,
  733. OutputIterator& out,
  734. Distance const& max_distance,
  735. Strategies const& strategies)
  736. {
  737. dispatch::simplify_insert
  738. <
  739. Geometry
  740. >::apply(geometry, out, max_distance,
  741. detail::simplify::douglas_peucker(),
  742. strategies);
  743. }
  744. };
  745. template <typename Strategy>
  746. struct simplify_insert<Strategy, false>
  747. {
  748. template<typename Geometry, typename OutputIterator, typename Distance>
  749. static inline void apply(Geometry const& geometry,
  750. OutputIterator& out,
  751. Distance const& max_distance,
  752. Strategy const& strategy)
  753. {
  754. using strategies::simplify::services::strategy_converter;
  755. simplify_insert
  756. <
  757. decltype(strategy_converter<Strategy>::get(strategy))
  758. >::apply(geometry, out, max_distance,
  759. strategy_converter<Strategy>::get(strategy));
  760. }
  761. };
  762. template <>
  763. struct simplify_insert<default_strategy, false>
  764. {
  765. template <typename Geometry, typename OutputIterator, typename Distance>
  766. static inline void apply(Geometry const& geometry,
  767. OutputIterator& out,
  768. Distance const& max_distance,
  769. default_strategy)
  770. {
  771. typedef typename strategies::simplify::services::default_strategy
  772. <
  773. Geometry
  774. >::type strategy_type;
  775. simplify_insert
  776. <
  777. strategy_type
  778. >::apply(geometry, out, max_distance, strategy_type());
  779. }
  780. };
  781. } // namespace resolve_strategy
  782. namespace resolve_dynamic {
  783. template
  784. <
  785. typename GeometryIn, typename GeometryOut,
  786. typename TagIn = typename tag<GeometryIn>::type,
  787. typename TagOut = typename tag<GeometryOut>::type
  788. >
  789. struct simplify
  790. {
  791. template <typename Distance, typename Strategy>
  792. static inline void apply(GeometryIn const& geometry,
  793. GeometryOut& out,
  794. Distance const& max_distance,
  795. Strategy const& strategy)
  796. {
  797. resolve_strategy::simplify<Strategy>::apply(geometry, out, max_distance, strategy);
  798. }
  799. };
  800. template <typename GeometryIn, typename GeometryOut>
  801. struct simplify<GeometryIn, GeometryOut, dynamic_geometry_tag, dynamic_geometry_tag>
  802. {
  803. template <typename Distance, typename Strategy>
  804. static inline void apply(GeometryIn const& geometry,
  805. GeometryOut& out,
  806. Distance const& max_distance,
  807. Strategy const& strategy)
  808. {
  809. traits::visit<GeometryIn>::apply([&](auto const& g)
  810. {
  811. using geom_t = util::remove_cref_t<decltype(g)>;
  812. using detail::simplify::static_geometry_type;
  813. using geom_out_t = typename static_geometry_type<geom_t, GeometryOut>::type;
  814. geom_out_t o;
  815. simplify<geom_t, geom_out_t>::apply(g, o, max_distance, strategy);
  816. out = std::move(o);
  817. }, geometry);
  818. }
  819. };
  820. template <typename GeometryIn, typename GeometryOut>
  821. struct simplify<GeometryIn, GeometryOut, geometry_collection_tag, geometry_collection_tag>
  822. {
  823. template <typename Distance, typename Strategy>
  824. static inline void apply(GeometryIn const& geometry,
  825. GeometryOut& out,
  826. Distance const& max_distance,
  827. Strategy const& strategy)
  828. {
  829. detail::visit_breadth_first([&](auto const& g)
  830. {
  831. using geom_t = util::remove_cref_t<decltype(g)>;
  832. using detail::simplify::static_geometry_type;
  833. using geom_out_t = typename static_geometry_type<geom_t, GeometryOut>::type;
  834. geom_out_t o;
  835. simplify<geom_t, geom_out_t>::apply(g, o, max_distance, strategy);
  836. traits::emplace_back<GeometryOut>::apply(out, std::move(o));
  837. return true;
  838. }, geometry);
  839. }
  840. };
  841. } // namespace resolve_dynamic
  842. /*!
  843. \brief Simplify a geometry using a specified strategy
  844. \ingroup simplify
  845. \tparam Geometry \tparam_geometry
  846. \tparam GeometryOut The output geometry
  847. \tparam Distance A numerical distance measure
  848. \tparam Strategy A type fulfilling a SimplifyStrategy concept
  849. \param strategy A strategy to calculate simplification
  850. \param geometry input geometry, to be simplified
  851. \param out output geometry, simplified version of the input geometry
  852. \param max_distance distance (in units of input coordinates) of a vertex
  853. to other segments to be removed
  854. \param strategy simplify strategy to be used for simplification
  855. \note The simplification is done with Douglas-Peucker algorithm
  856. \image html svg_simplify_country.png "The image below presents the simplified country"
  857. \qbk{distinguish,with strategy}
  858. */
  859. template<typename Geometry, typename GeometryOut, typename Distance, typename Strategy>
  860. inline void simplify(Geometry const& geometry, GeometryOut& out,
  861. Distance const& max_distance, Strategy const& strategy)
  862. {
  863. concepts::check<Geometry const>();
  864. concepts::check<GeometryOut>();
  865. geometry::clear(out);
  866. resolve_dynamic::simplify<Geometry, GeometryOut>::apply(geometry, out, max_distance, strategy);
  867. }
  868. /*!
  869. \brief Simplify a geometry
  870. \ingroup simplify
  871. \tparam Geometry \tparam_geometry
  872. \tparam GeometryOut The output geometry
  873. \tparam Distance \tparam_numeric
  874. \param geometry input geometry, to be simplified
  875. \param out output geometry, simplified version of the input geometry
  876. \param max_distance distance (in units of input coordinates) of a vertex
  877. to other segments to be removed
  878. \note The simplification is done with Douglas-Peucker algorithm
  879. \qbk{[include reference/algorithms/simplify.qbk]}
  880. */
  881. template<typename Geometry, typename GeometryOut, typename Distance>
  882. inline void simplify(Geometry const& geometry, GeometryOut& out,
  883. Distance const& max_distance)
  884. {
  885. concepts::check<Geometry const>();
  886. concepts::check<GeometryOut>();
  887. geometry::simplify(geometry, out, max_distance, default_strategy());
  888. }
  889. #ifndef DOXYGEN_NO_DETAIL
  890. namespace detail { namespace simplify
  891. {
  892. /*!
  893. \brief Simplify a geometry, using an output iterator
  894. and a specified strategy
  895. \ingroup simplify
  896. \tparam Geometry \tparam_geometry
  897. \param geometry input geometry, to be simplified
  898. \param out output iterator, outputs all simplified points
  899. \param max_distance distance (in units of input coordinates) of a vertex
  900. to other segments to be removed
  901. \param strategy simplify strategy to be used for simplification
  902. \qbk{distinguish,with strategy}
  903. \qbk{[include reference/algorithms/simplify.qbk]}
  904. */
  905. template<typename Geometry, typename OutputIterator, typename Distance, typename Strategy>
  906. inline void simplify_insert(Geometry const& geometry, OutputIterator out,
  907. Distance const& max_distance, Strategy const& strategy)
  908. {
  909. concepts::check<Geometry const>();
  910. resolve_strategy::simplify_insert<Strategy>::apply(geometry, out, max_distance, strategy);
  911. }
  912. /*!
  913. \brief Simplify a geometry, using an output iterator
  914. \ingroup simplify
  915. \tparam Geometry \tparam_geometry
  916. \param geometry input geometry, to be simplified
  917. \param out output iterator, outputs all simplified points
  918. \param max_distance distance (in units of input coordinates) of a vertex
  919. to other segments to be removed
  920. \qbk{[include reference/algorithms/simplify_insert.qbk]}
  921. */
  922. template<typename Geometry, typename OutputIterator, typename Distance>
  923. inline void simplify_insert(Geometry const& geometry, OutputIterator out,
  924. Distance const& max_distance)
  925. {
  926. // Concept: output point type = point type of input geometry
  927. concepts::check<Geometry const>();
  928. concepts::check<typename point_type<Geometry>::type>();
  929. simplify_insert(geometry, out, max_distance, default_strategy());
  930. }
  931. }} // namespace detail::simplify
  932. #endif // DOXYGEN_NO_DETAIL
  933. }} // namespace boost::geometry
  934. #endif // BOOST_GEOMETRY_ALGORITHMS_SIMPLIFY_HPP