|
| 1 | +// Boost.Geometry (aka GGL, Generic Geometry Library) |
| 2 | + |
| 3 | +// Copyright (c) 2020 Tinko Bartels, Berlin, Germany. |
| 4 | + |
| 5 | +// Contributed and/or modified by Tinko Bartels, |
| 6 | +// as part of Google Summer of Code 2019 program. |
| 7 | + |
| 8 | +// Use, modification and distribution is subject to the Boost Software License, |
| 9 | +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 10 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 11 | + |
| 12 | +#ifndef BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_CONVEX_HULL_REJECTION_HPP |
| 13 | +#define BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_CONVEX_HULL_REJECTION_HPP |
| 14 | + |
| 15 | +#include <random> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include <boost/geometry/algorithms/equals.hpp> |
| 19 | +#include <boost/geometry/algorithms/transform.hpp> |
| 20 | +#include <boost/geometry/algorithms/within.hpp> |
| 21 | +#include <boost/geometry/geometries/polygon.hpp> |
| 22 | +#include <boost/geometry/algorithms/convex_hull.hpp> |
| 23 | + |
| 24 | +#include <boost/geometry/strategies/cartesian/area.hpp> |
| 25 | +#include <boost/geometry/strategies/geographic/area.hpp> |
| 26 | +#include <boost/geometry/strategies/spherical/area.hpp> |
| 27 | + |
| 28 | +#include <boost/geometry/strategies/cartesian/point_in_poly_winding.hpp> |
| 29 | +#include <boost/geometry/strategies/spherical/point_in_poly_winding.hpp> |
| 30 | +#include <boost/geometry/strategies/geographic/point_in_poly_winding.hpp> |
| 31 | + |
| 32 | +#include <boost/geometry/extensions/random/strategies/cartesian/uniform_point_distribution_triangle.hpp> |
| 33 | +#include <boost/geometry/extensions/random/policies/uniform_convex_polygon.hpp> |
| 34 | + |
| 35 | +namespace boost { namespace geometry |
| 36 | +{ |
| 37 | + |
| 38 | +namespace policy { namespace uniform_point_distribution |
| 39 | +{ |
| 40 | + |
| 41 | +// The following strategy is suitable for geometries for which |
| 42 | +// a Triangle sampling strategy can be provided |
| 43 | +// a SideStrategy that computes the triangle area can be provided. |
| 44 | +template |
| 45 | +< |
| 46 | + typename Point, |
| 47 | + typename DomainGeometry, |
| 48 | + typename TriangleStrategy = void, |
| 49 | + typename AreaStrategy = void, |
| 50 | +// typename ConvexHullStrategy |
| 51 | + typename WithinStrategy = void |
| 52 | +> |
| 53 | +struct convex_hull_rejection |
| 54 | +{ |
| 55 | +private: |
| 56 | + typedef typename boost::mpl::if_ |
| 57 | + < |
| 58 | + boost::is_void<TriangleStrategy>, |
| 59 | + typename boost::geometry::strategy::uniform_point_distribution::services |
| 60 | + ::default_triangle_strategy |
| 61 | + < |
| 62 | + Point, typename point_type<DomainGeometry>::type |
| 63 | + >::type, |
| 64 | + TriangleStrategy |
| 65 | + >::type triangle_strategy; |
| 66 | + typedef typename boost::mpl::if_ |
| 67 | + < |
| 68 | + boost::is_void<AreaStrategy>, |
| 69 | + typename boost::geometry::strategy::area::services::default_strategy |
| 70 | + < |
| 71 | + typename cs_tag<DomainGeometry>::type |
| 72 | + >::type, |
| 73 | + AreaStrategy |
| 74 | + >::type area_strategy; |
| 75 | + typedef typename boost::mpl::if_ |
| 76 | + < |
| 77 | + boost::is_void<WithinStrategy>, |
| 78 | + typename boost::geometry::strategy::within::services::default_strategy |
| 79 | + < |
| 80 | + Point, |
| 81 | + DomainGeometry |
| 82 | + >::type, |
| 83 | + WithinStrategy |
| 84 | + >::type within_strategy; |
| 85 | + typedef typename point_type<DomainGeometry>::type domain_point_type; |
| 86 | + typedef boost::geometry::model::ring<domain_point_type> ring; |
| 87 | + ring m_hull; |
| 88 | + convex_polygon<Point, ring, triangle_strategy, area_strategy> m_policy; |
| 89 | + within_strategy m_within_strategy; |
| 90 | +public: |
| 91 | + convex_hull_rejection(DomainGeometry const& d, |
| 92 | + triangle_strategy const& t = triangle_strategy(), |
| 93 | + area_strategy const& a = area_strategy(), |
| 94 | + within_strategy const& w = within_strategy()) : m_within_strategy(w) |
| 95 | + |
| 96 | + { |
| 97 | + boost::geometry::convex_hull(d, m_hull); |
| 98 | + m_policy = convex_polygon |
| 99 | + < |
| 100 | + Point, |
| 101 | + ring, |
| 102 | + triangle_strategy, |
| 103 | + area_strategy |
| 104 | + >(m_hull, t, a); |
| 105 | + } |
| 106 | + bool equals(DomainGeometry const& l_domain, |
| 107 | + DomainGeometry const& r_domain, |
| 108 | + convex_hull_rejection const& r_policy) const |
| 109 | + { |
| 110 | + return boost::geometry::equals(l_domain, r_domain) |
| 111 | + && m_policy.equals(l_domain, r_domain, r_policy.m_policy); |
| 112 | + } |
| 113 | + template<typename Generator> |
| 114 | + Point apply(Generator& g, DomainGeometry const& d) |
| 115 | + { |
| 116 | + Point p; |
| 117 | + do { |
| 118 | + p = m_policy.apply(g, m_hull); |
| 119 | + } while( !boost::geometry::within(p, d, m_within_strategy) ); |
| 120 | + return p; |
| 121 | + } |
| 122 | + void reset(DomainGeometry const&) {}; |
| 123 | +}; |
| 124 | + |
| 125 | +}} // namespace policy::uniform_point_distribution |
| 126 | + |
| 127 | +}} // namespace boost::geometry |
| 128 | + |
| 129 | +#endif // BOOST_GEOMETRY_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_CONVEX_HULL_REJECTION_HPP |
0 commit comments