Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec6adac

Browse files
committedApr 6, 2020
Refactored code such that non-cs-specific code is moved to policies
1 parent c899eb6 commit ec6adac

18 files changed

+1194
-834
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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_BOX_HPP
13+
#define BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_BOX_HPP
14+
15+
#include <random>
16+
#include <array>
17+
#include <functional>
18+
#include <algorithm>
19+
20+
#include <boost/geometry/algorithms/equals.hpp>
21+
#include <boost/geometry/algorithms/transform.hpp>
22+
#include <boost/geometry/algorithms/within.hpp>
23+
#include <boost/geometry/geometries/box.hpp>
24+
#include <boost/geometry/core/point_type.hpp>
25+
26+
#include <boost/geometry/extensions/random/policies/uniform_default_policy.hpp>
27+
28+
#include <boost/geometry/extensions/random/strategies/cartesian/uniform_point_distribution_box.hpp>
29+
30+
namespace boost { namespace geometry
31+
{
32+
33+
namespace policy { namespace uniform_point_distribution
34+
{
35+
36+
template
37+
<
38+
typename Point,
39+
typename DomainGeometry,
40+
typename BoxStrategy = void,
41+
typename CalculationType = void
42+
>
43+
struct box
44+
{
45+
private:
46+
typedef typename boost::mpl::if_
47+
<
48+
boost::is_void<BoxStrategy>,
49+
typename boost::geometry::strategy::uniform_point_distribution::services::
50+
default_box_strategy
51+
<
52+
Point,
53+
DomainGeometry
54+
>::type,
55+
BoxStrategy
56+
>::type box_strategy;
57+
typedef typename boost::mpl::if_
58+
<
59+
boost::is_void<CalculationType>,
60+
typename select_most_precise
61+
<
62+
typename coordinate_type<Point>::type,
63+
double
64+
>::type,
65+
CalculationType
66+
>::type ct;
67+
box_strategy m_box_strategy;
68+
public:
69+
box(DomainGeometry const& d, box_strategy const& b = box_strategy()) : m_box_strategy(b) {}
70+
bool equals(DomainGeometry const& l_domain,
71+
DomainGeometry const& r_domain,
72+
box const& r_policy) const
73+
{
74+
return boost::geometry::equals(l_domain, r_domain);
75+
}
76+
template<typename Generator>
77+
Point apply(Generator& g, DomainGeometry const& d)
78+
{
79+
typedef typename point_type<DomainGeometry>::type domain_point;
80+
domain_point p;
81+
std::array<ct, dimension<DomainGeometry>::value> fractions;
82+
std::uniform_real_distribution<ct> dist(0, 1);
83+
std::generate(fractions.begin(), fractions.end(), std::bind(dist, std::ref(g)));
84+
p = m_box_strategy.template apply<>(d, fractions);
85+
Point r;
86+
boost::geometry::transform(p, r);
87+
return r;
88+
}
89+
void reset(DomainGeometry const&) {};
90+
};
91+
92+
namespace services
93+
{
94+
95+
template
96+
<
97+
typename Point,
98+
typename DomainGeometry,
99+
int Dim,
100+
typename CsTag
101+
>
102+
struct default_policy
103+
<
104+
Point,
105+
DomainGeometry,
106+
box_tag,
107+
single_tag,
108+
Dim,
109+
CsTag
110+
>
111+
{
112+
typedef box<Point, DomainGeometry> type;
113+
};
114+
115+
} // namespace services
116+
117+
}} // namespace policy::uniform_point_distribution
118+
119+
}} // namespace boost::geometry
120+
121+
#endif // BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_BOX_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)
Please sign in to comment.