|
| 1 | +// Boost.Geometry |
| 2 | + |
| 3 | +// Copyright (c) 2023 Barend Gehrels, Amsterdam, the Netherlands. |
| 4 | + |
| 5 | +// Use, modification and distribution is subject to the Boost Software License, |
| 6 | +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 8 | + |
| 9 | +#ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COLOCATE_CLUSTERS_HPP |
| 10 | +#define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COLOCATE_CLUSTERS_HPP |
| 11 | + |
| 12 | +#include <boost/geometry/core/access.hpp> |
| 13 | +#include <boost/geometry/core/cs.hpp> |
| 14 | +#include <boost/geometry/core/coordinate_type.hpp> |
| 15 | +#include <boost/geometry/core/tags.hpp> |
| 16 | + |
| 17 | +namespace boost { namespace geometry |
| 18 | +{ |
| 19 | + |
| 20 | +#ifndef DOXYGEN_NO_DETAIL |
| 21 | +namespace detail { namespace overlay |
| 22 | +{ |
| 23 | + |
| 24 | +// Default implementation, using the first point for all turns in the cluster. |
| 25 | +template |
| 26 | +< |
| 27 | + typename Point, |
| 28 | + typename CoordinateType = typename geometry::coordinate_type<Point>::type, |
| 29 | + typename CsTag = typename geometry::cs_tag<Point>::type, |
| 30 | + bool IsIntegral = std::is_integral<CoordinateType>::value |
| 31 | +> |
| 32 | +struct cluster_colocator |
| 33 | +{ |
| 34 | + template <typename TurnIndices, typename Turns> |
| 35 | + static inline void apply(TurnIndices const& indices, Turns& turns) |
| 36 | + { |
| 37 | + // This approach works for all but one testcase (rt_p13) |
| 38 | + // The problem is fill_sbs, which uses sides and these sides might change slightly |
| 39 | + // depending on the exact location of the cluster. |
| 40 | + // Using the centroid is, on the average, a safer choice for sides. |
| 41 | + // Alternatively fill_sbs could be revised, but that requires a lot of work |
| 42 | + // and is outside current scope. |
| 43 | + // Integer coordinates are always colocated already and do not need centroid calculation. |
| 44 | + // Geographic/spherical coordinates might (in extremely rare cases) cross the date line |
| 45 | + // and therefore the first point is taken for them as well. |
| 46 | + auto it = indices.begin(); |
| 47 | + auto const& first_point = turns[*it].point; |
| 48 | + for (++it; it != indices.end(); ++it) |
| 49 | + { |
| 50 | + turns[*it].point = first_point; |
| 51 | + } |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +// Specialization for non-integral cartesian coordinates, calculating |
| 56 | +// the centroid of the points of the turns in the cluster. |
| 57 | +template <typename Point, typename CoordinateType> |
| 58 | +struct cluster_colocator<Point, CoordinateType, geometry::cartesian_tag, false> |
| 59 | +{ |
| 60 | + template <typename TurnIndices, typename Turns> |
| 61 | + static inline void apply(TurnIndices const& indices, Turns& turns) |
| 62 | + { |
| 63 | + CoordinateType centroid_0 = 0; |
| 64 | + CoordinateType centroid_1 = 0; |
| 65 | + for (const auto& index : indices) |
| 66 | + { |
| 67 | + centroid_0 += geometry::get<0>(turns[index].point); |
| 68 | + centroid_1 += geometry::get<1>(turns[index].point); |
| 69 | + } |
| 70 | + centroid_0 /= indices.size(); |
| 71 | + centroid_1 /= indices.size(); |
| 72 | + for (const auto& index : indices) |
| 73 | + { |
| 74 | + geometry::set<0>(turns[index].point, centroid_0); |
| 75 | + geometry::set<1>(turns[index].point, centroid_1); |
| 76 | + } |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +// Moves intersection points per cluster such that they are identical. |
| 81 | +// Because clusters are intersection close together, and |
| 82 | +// handled as one location. Then they should also have one location. |
| 83 | +// It is necessary to avoid artefacts and invalidities. |
| 84 | +template <typename Clusters, typename Turns> |
| 85 | +inline void colocate_clusters(Clusters const& clusters, Turns& turns) |
| 86 | +{ |
| 87 | + for (auto const& pair : clusters) |
| 88 | + { |
| 89 | + auto const& indices = pair.second.turn_indices; |
| 90 | + if (indices.size() < 2) |
| 91 | + { |
| 92 | + // Defensive check |
| 93 | + continue; |
| 94 | + } |
| 95 | + using point_t = decltype(turns[*indices.begin()].point); |
| 96 | + cluster_colocator<point_t>::apply(indices, turns); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +}} // namespace detail::overlay |
| 102 | +#endif //DOXYGEN_NO_DETAIL |
| 103 | + |
| 104 | + |
| 105 | +}} // namespace boost::geometry |
| 106 | + |
| 107 | +#endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COLOCATE_CLUSTERS_HPP |
0 commit comments