Skip to content

Commit b9a3660

Browse files
committed
Move implementations into strategies.
1 parent d906785 commit b9a3660

11 files changed

+1009
-745
lines changed

include/boost/geometry/extensions/random/detail/uniform_point_distribution.hpp

Lines changed: 0 additions & 667 deletions
This file was deleted.

include/boost/geometry/extensions/random/dispatch/uniform_point_distribution.hpp

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Boost.Geometry (aka GGL, Generic Geometry Library)
2+
3+
// Copyright (c) 2019 Tinko Bartels, Berlin, Germany.
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_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_ENVELOPE_REJECTION_HPP
10+
#define BOOST_GEOMETRY_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_ENVELOPE_REJECTION_HPP
11+
12+
#include <random>
13+
#include <iterator>
14+
15+
#include <boost/geometry/algorithms/equals.hpp>
16+
#include <boost/geometry/algorithms/transform.hpp>
17+
#include <boost/geometry/algorithms/within.hpp>
18+
#include <boost/geometry/geometries/box.hpp>
19+
#include <boost/geometry/core/point_type.hpp>
20+
21+
#include <boost/geometry/extensions/random/strategies/uniform_point_distribution.hpp>
22+
#include <boost/geometry/extensions/random/strategies/cartesian/uniform_point_distribution_box.hpp>
23+
#include <boost/geometry/extensions/random/strategies/spherical/uniform_inverse_transform_sampling.hpp>
24+
25+
namespace boost { namespace geometry
26+
{
27+
28+
namespace strategy { namespace uniform_point_distribution {
29+
30+
template
31+
<
32+
typename Point,
33+
typename DomainGeometry,
34+
typename BoxStrategy = services::default_strategy
35+
<
36+
typename point_type<DomainGeometry>::type,
37+
model::box<typename point_type<DomainGeometry>::type>
38+
>
39+
>
40+
struct uniform_envelope_rejection
41+
{
42+
private:
43+
typedef model::box<typename point_type<DomainGeometry>::type> envelope_type;
44+
envelope_type m_env;
45+
BoxStrategy m_env_strat;
46+
public:
47+
uniform_envelope_rejection(DomainGeometry const& g) :
48+
m_env(return_envelope<envelope_type>(g)),
49+
m_env_strat(m_env) {}
50+
bool equals(DomainGeometry const& l_domain,
51+
DomainGeometry const& r_domain,
52+
uniform_envelope_rejection const& r_strategy) const
53+
{
54+
return boost::geometry::equals(l_domain, r_domain);
55+
}
56+
template<typename Gen>
57+
Point apply(Gen& g, DomainGeometry const& d)
58+
{
59+
typedef typename point_type<DomainGeometry>::type domain_point;
60+
domain_point p;
61+
do {
62+
p = m_env_strat.apply(g, m_env);
63+
}while( !::boost::geometry::within(p, d) );
64+
Point r;
65+
boost::geometry::transform(p, r);
66+
return r;
67+
}
68+
void reset(DomainGeometry const&) {};
69+
};
70+
71+
namespace services {
72+
73+
template
74+
<
75+
typename Point,
76+
typename DomainGeometry,
77+
typename MultiOrSingle,
78+
typename CsTag
79+
>
80+
struct default_strategy
81+
<
82+
Point,
83+
DomainGeometry,
84+
areal_tag,
85+
MultiOrSingle,
86+
2,
87+
CsTag
88+
> : public uniform_envelope_rejection<Point, DomainGeometry> {
89+
typedef uniform_envelope_rejection<Point, DomainGeometry> base;
90+
using base::base;
91+
};
92+
93+
template
94+
<
95+
typename Point,
96+
typename DomainGeometry,
97+
typename MultiOrSingle,
98+
typename CsTag
99+
>
100+
struct default_strategy
101+
<
102+
Point,
103+
DomainGeometry,
104+
volumetric_tag,
105+
MultiOrSingle,
106+
3,
107+
CsTag
108+
> : public uniform_envelope_rejection<Point, DomainGeometry> {
109+
typedef uniform_envelope_rejection<Point, DomainGeometry> base;
110+
using base::base;
111+
};
112+
113+
} // namespace services
114+
115+
}} // namespace strategy::uniform_point_distribution
116+
117+
}} // namespace boost::geometry
118+
119+
#endif // BOOST_GEOMETRY_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_ENVELOPE_REJECTION_HPP
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Boost.Geometry (aka GGL, Generic Geometry Library)
2+
3+
// Copyright (c) 2019 Tinko Bartels, Berlin, Germany.
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_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_LINEAR_HPP
10+
#define BOOST_GEOMETRY_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_LINEAR_HPP
11+
12+
#include <random>
13+
#include <iterator>
14+
#include <algorithm>
15+
16+
#include <boost/geometry/algorithms/equals.hpp>
17+
#include <boost/geometry/algorithms/transform.hpp>
18+
#include <boost/geometry/algorithms/within.hpp>
19+
#include <boost/geometry/geometries/box.hpp>
20+
#include <boost/geometry/core/point_type.hpp>
21+
22+
#include <boost/geometry/extensions/random/strategies/uniform_point_distribution.hpp>
23+
#include <boost/geometry/extensions/random/strategies/cartesian/uniform_point_distribution_segment.hpp>
24+
#include <boost/geometry/extensions/random/strategies/spherical/edwilliams_avform_intermediate.hpp>
25+
26+
namespace boost { namespace geometry
27+
{
28+
29+
namespace strategy { namespace uniform_point_distribution {
30+
31+
template
32+
<
33+
typename Point,
34+
typename DomainGeometry,
35+
typename SegmentStrategy = services::default_strategy
36+
<
37+
typename point_type<DomainGeometry>::type,
38+
model::segment<typename point_type<DomainGeometry>::type>
39+
>
40+
>
41+
class uniform_linear
42+
{
43+
private:
44+
typedef typename default_length_result<DomainGeometry>::type length_type;
45+
typedef typename point_type<DomainGeometry>::type domain_point_type;
46+
std::vector<std::size_t> skip_list;
47+
std::vector<domain_point_type> point_cache;
48+
std::vector<length_type> accumulated_lengths;
49+
public:
50+
uniform_linear(DomainGeometry const& g)
51+
{
52+
std::size_t i = 0;
53+
point_cache.push_back(*segments_begin(g)->first);
54+
accumulated_lengths.push_back(0);
55+
for (auto it = segments_begin(g) ; it != segments_end(g) ; ++it)
56+
{
57+
accumulated_lengths.push_back(
58+
accumulated_lengths.back() + length(*it));
59+
if (!boost::geometry::equals(point_cache.back(), *it->first))
60+
{
61+
point_cache.push_back(*it->first);
62+
skip_list.push_back(i);
63+
}
64+
point_cache.push_back(*it->second);
65+
++i;
66+
}
67+
}
68+
bool equals(DomainGeometry const& l_domain,
69+
DomainGeometry const& r_domain,
70+
uniform_linear const& r_strategy) const
71+
{
72+
if(r_strategy.skip_list.size() != skip_list.size()
73+
|| r_strategy.point_cache.size() != point_cache.size())
74+
return false;
75+
for (std::size_t i = 0; i < skip_list.size(); ++i)
76+
{
77+
if (skip_list[i] != r_strategy.skip_list[i]) return false;
78+
}
79+
for (std::size_t i = 0; i < point_cache.size(); ++i)
80+
{
81+
if (!boost::geometry::equals(point_cache[i], r_strategy.point_cache[i])) return false;
82+
}
83+
return true;
84+
}
85+
template<typename Gen>
86+
Point apply(Gen& g, DomainGeometry const& d)
87+
{
88+
typedef typename select_most_precise
89+
<
90+
double,
91+
typename coordinate_type<Point>::type
92+
>::type sample_type;
93+
std::uniform_real_distribution<sample_type> dist(0, accumulated_lengths.back());
94+
sample_type r = dist(g);
95+
std::size_t i = std::distance(
96+
accumulated_lengths.begin(),
97+
std::lower_bound(accumulated_lengths.begin(),
98+
accumulated_lengths.end(),
99+
r));
100+
std::size_t offset = std::distance(
101+
skip_list.begin(),
102+
std::lower_bound(skip_list.begin(), skip_list.end(), i));
103+
104+
return SegmentStrategy::template map
105+
<
106+
sample_type,
107+
domain_point_type
108+
>(point_cache[ i + offset - 1 ], point_cache[ i + offset ],
109+
( r - accumulated_lengths[ i - 1 ]) /
110+
( accumulated_lengths[ i ] - accumulated_lengths[ i - 1 ] ));
111+
}
112+
void reset(DomainGeometry const&) {};
113+
};
114+
115+
namespace services {
116+
117+
template
118+
<
119+
typename Point,
120+
typename DomainGeometry,
121+
typename MultiOrSingle,
122+
int Dim,
123+
typename CsTag
124+
>
125+
struct default_strategy
126+
<
127+
Point,
128+
DomainGeometry,
129+
linear_tag,
130+
MultiOrSingle,
131+
Dim,
132+
CsTag
133+
> : public uniform_linear<Point, DomainGeometry> {
134+
typedef uniform_linear<Point, DomainGeometry> base;
135+
using base::base;
136+
};
137+
138+
} // namespace services
139+
140+
}} // namespace strategy::uniform_point_distribution
141+
142+
}} // namespace boost::geometry
143+
144+
#endif // BOOST_GEOMETRY_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_LINEAR_HPP

0 commit comments

Comments
 (0)