1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
// Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
// Copyright (c) 2013-2017 Adam Wulkiewicz, Lodz, Poland.
// This file was modified by Oracle on 2014-2023.
// Modifications copyright (c) 2014-2023 Oracle and/or its affiliates.
// Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_GEOMETRY_ALGORITHMS_POINT_ON_SURFACE_HPP
#define BOOST_GEOMETRY_ALGORITHMS_POINT_ON_SURFACE_HPP
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/geometry/core/point_type.hpp>
#include <boost/geometry/geometries/concepts/check.hpp>
#include <boost/geometry/algorithms/detail/extreme_points.hpp>
#include <boost/geometry/algorithms/detail/signed_size_type.hpp>
#include <boost/geometry/strategies/side.hpp>
namespace boost { namespace geometry
{
#ifndef DOXYGEN_NO_DETAIL
namespace detail { namespace point_on_surface
{
template <typename CoordinateType, int Dimension>
struct specific_coordinate_first
{
CoordinateType const m_value_to_be_first;
inline specific_coordinate_first(CoordinateType value_to_be_skipped)
: m_value_to_be_first(value_to_be_skipped)
{}
template <typename Point>
inline bool operator()(Point const& lhs, Point const& rhs)
{
CoordinateType const lh = geometry::get<Dimension>(lhs);
CoordinateType const rh = geometry::get<Dimension>(rhs);
// If both lhs and rhs equal m_value_to_be_first,
// we should handle conform if lh < rh = FALSE
// The first condition meets that, keep it first
if (geometry::math::equals(rh, m_value_to_be_first))
{
// Handle conform lh < -INF, which is always false
return false;
}
if (geometry::math::equals(lh, m_value_to_be_first))
{
// Handle conform -INF < rh, which is always true
return true;
}
return lh < rh;
}
};
template <int Dimension, typename Collection, typename Value, typename Predicate>
inline bool max_value(Collection const& collection, Value& the_max, Predicate const& predicate)
{
bool first = true;
for (auto const& item : collection)
{
if (! item.empty())
{
Value the_value = geometry::get<Dimension>(*std::max_element(item.begin(), item.end(), predicate));
if (first || the_value > the_max)
{
the_max = the_value;
first = false;
}
}
}
return ! first;
}
template <int Dimension, typename Value>
struct select_below
{
Value m_value;
inline select_below(Value const& v)
: m_value(v)
{}
template <typename Intruder>
inline bool operator()(Intruder const& intruder) const
{
if (intruder.empty())
{
return true;
}
Value max = geometry::get<Dimension>(*std::max_element(intruder.begin(), intruder.end(), detail::extreme_points::compare<Dimension>()));
return geometry::math::equals(max, m_value) || max < m_value;
}
};
template <int Dimension, typename Value>
struct adapt_base
{
Value m_value;
inline adapt_base(Value const& v)
: m_value(v)
{}
template <typename Intruder>
inline void operator()(Intruder& intruder) const
{
if (intruder.size() >= 3)
{
detail::extreme_points::move_along_vector<Dimension>(intruder, m_value);
}
}
};
template <int Dimension, typename Value>
struct min_of_intruder
{
template <typename Intruder>
inline bool operator()(Intruder const& lhs, Intruder const& rhs) const
{
Value lhs_min = geometry::get<Dimension>(*std::min_element(lhs.begin(), lhs.end(), detail::extreme_points::compare<Dimension>()));
Value rhs_min = geometry::get<Dimension>(*std::min_element(rhs.begin(), rhs.end(), detail::extreme_points::compare<Dimension>()));
return lhs_min < rhs_min;
}
};
template <typename Point, typename P>
inline void calculate_average(Point& point, std::vector<P> const& points)
{
using coordinate_type = geometry::coordinate_type_t<Point>;
coordinate_type x = 0;
coordinate_type y = 0;
for (auto const& p : points)
{
x += geometry::get<0>(p);
y += geometry::get<1>(p);
}
signed_size_type const count = points.size();
geometry::set<0>(point, x / count);
geometry::set<1>(point, y / count);
}
template <int Dimension, typename Extremes, typename Intruders, typename CoordinateType>
inline void replace_extremes_for_self_tangencies(Extremes& extremes, Intruders& intruders, CoordinateType const& max_intruder)
{
// This function handles self-tangencies.
// Self-tangencies use, as usual, the major part of code...
// ___ e
// /|\ \ .
// / | \ \ .
// / | \ \ .
// / | \ \ .
// / /\ | \ \ .
// i2 i1
// The picture above shows the extreme (outside, "e") and two intruders ("i1","i2")
// Assume that "i1" is self-tangent with the extreme, in one point at the top
// Now the "penultimate" value is searched, this is is the top of i2
// Then everything including and below (this is "i2" here) is removed
// Then the base of "i1" and of "e" is adapted to this penultimate value
// It then looks like:
// b ___ e
// /|\ \ .
// / | \ \ .
// / | \ \ .
// / | \ \ .
// a c i1
// Then intruders (here "i1" but there may be more) are sorted from left to right
// Finally points "a","b" and "c" (in this order) are selected as a new triangle.
// This triangle will have a centroid which is inside (assumed that intruders left segment
// is not equal to extremes left segment, but that polygon would be invalid)
// Find highest non-self tangent intrusion, if any
CoordinateType penultimate_value;
specific_coordinate_first<CoordinateType, Dimension> pu_compare(max_intruder);
if (max_value<Dimension>(intruders, penultimate_value, pu_compare))
{
// Throw away all intrusions <= this value, and of the kept one set this as base.
select_below<Dimension, CoordinateType> predicate(penultimate_value);
intruders.erase
(
std::remove_if(boost::begin(intruders), boost::end(intruders), predicate),
boost::end(intruders)
);
adapt_base<Dimension, CoordinateType> fe_predicate(penultimate_value);
// Sort from left to right (or bottom to top if Dimension=0)
std::for_each(boost::begin(intruders), boost::end(intruders), fe_predicate);
// Also adapt base of extremes
detail::extreme_points::move_along_vector<Dimension>(extremes, penultimate_value);
}
// Then sort in 1-Dim. Take first to calc centroid.
std::sort(boost::begin(intruders), boost::end(intruders), min_of_intruder<1 - Dimension, CoordinateType>());
Extremes triangle;
triangle.reserve(3);
// Make a triangle of first two points of extremes (the ramp, from left to right), and last point of first intruder (which goes from right to left)
std::copy(extremes.begin(), extremes.begin() + 2, std::back_inserter(triangle));
triangle.push_back(intruders.front().back());
// (alternatively we could use the last two points of extremes, and first point of last intruder...):
//// ALTERNATIVE: std::copy(extremes.rbegin(), extremes.rbegin() + 2, std::back_inserter(triangle));
//// ALTERNATIVE: triangle.push_back(intruders.back().front());
// Now replace extremes with this smaller subset, a triangle, such that centroid calculation will result in a point inside
extremes = triangle;
}
template <int Dimension, typename Geometry, typename Point, typename SideStrategy>
inline bool calculate_point_on_surface(Geometry const& geometry, Point& point,
SideStrategy const& strategy)
{
using point_type = geometry::point_type_t<Geometry>;
using coordinate_type = geometry::coordinate_type_t<Geometry>;
std::vector<point_type> extremes;
typedef std::vector<std::vector<point_type> > intruders_type;
intruders_type intruders;
geometry::extreme_points<Dimension>(geometry, extremes, intruders, strategy);
if (extremes.size() < 3)
{
return false;
}
// If there are intruders, find the max.
if (! intruders.empty())
{
coordinate_type max_intruder;
detail::extreme_points::compare<Dimension> compare;
if (max_value<Dimension>(intruders, max_intruder, compare))
{
coordinate_type max_extreme = geometry::get<Dimension>(*std::max_element(extremes.begin(), extremes.end(), detail::extreme_points::compare<Dimension>()));
if (max_extreme > max_intruder)
{
detail::extreme_points::move_along_vector<Dimension>(extremes, max_intruder);
}
else
{
replace_extremes_for_self_tangencies<Dimension>(extremes, intruders, max_intruder);
}
}
}
// Now calculate the average/centroid of the (possibly adapted) extremes
calculate_average(point, extremes);
return true;
}
}} // namespace detail::point_on_surface
#endif // DOXYGEN_NO_DETAIL
/*!
\brief Assigns a Point guaranteed to lie on the surface of the Geometry
\tparam Geometry geometry type. This also defines the type of the output point
\param geometry Geometry to take point from
\param point Point to assign
\param strategy side strategy
*/
template <typename Geometry, typename Point, typename SideStrategy>
inline void point_on_surface(Geometry const& geometry, Point & point,
SideStrategy const& strategy)
{
concepts::check<Point>();
concepts::check<Geometry const>();
// First try in Y-direction (which should always succeed for valid polygons)
if (! detail::point_on_surface::calculate_point_on_surface<1>(geometry, point, strategy))
{
// For invalid polygons, we might try X-direction
detail::point_on_surface::calculate_point_on_surface<0>(geometry, point, strategy);
}
}
/*!
\brief Assigns a Point guaranteed to lie on the surface of the Geometry
\tparam Geometry geometry type. This also defines the type of the output point
\param geometry Geometry to take point from
\param point Point to assign
*/
template <typename Geometry, typename Point>
inline void point_on_surface(Geometry const& geometry, Point & point)
{
using strategy_type = typename strategy::side::services::default_strategy
<
cs_tag_t<Geometry>
>::type;
point_on_surface(geometry, point, strategy_type());
}
/*!
\brief Returns point guaranteed to lie on the surface of the Geometry
\tparam Geometry geometry type. This also defines the type of the output point
\param geometry Geometry to take point from
\param strategy side strategy
\return The Point guaranteed to lie on the surface of the Geometry
*/
template<typename Geometry, typename SideStrategy>
inline geometry::point_type_t<Geometry>
return_point_on_surface(Geometry const& geometry, SideStrategy const& strategy)
{
geometry::point_type_t<Geometry> result;
geometry::point_on_surface(geometry, result, strategy);
return result;
}
/*!
\brief Returns point guaranteed to lie on the surface of the Geometry
\tparam Geometry geometry type. This also defines the type of the output point
\param geometry Geometry to take point from
\return The Point guaranteed to lie on the surface of the Geometry
*/
template<typename Geometry>
inline geometry::point_type_t<Geometry>
return_point_on_surface(Geometry const& geometry)
{
geometry::point_type_t<Geometry> result;
geometry::point_on_surface(geometry, result);
return result;
}
}} // namespace boost::geometry
#endif // BOOST_GEOMETRY_ALGORITHMS_POINT_ON_SURFACE_HPP
|