Skip to content

Commit

Permalink
Refactored code such that non-cs-specific code is moved to policies
Browse files Browse the repository at this point in the history
  • Loading branch information
tinko92 committed Apr 6, 2020
1 parent c899eb6 commit ec6adac
Show file tree
Hide file tree
Showing 18 changed files with 1,194 additions and 834 deletions.
121 changes: 121 additions & 0 deletions include/boost/geometry/extensions/random/policies/uniform_box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2020 Tinko Bartels, Berlin, Germany.

// Contributed and/or modified by Tinko Bartels,
// as part of Google Summer of Code 2019 program.

// 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_EXTENSIONS_RANDOM_POLICIES_UNIFORM_BOX_HPP
#define BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_BOX_HPP

#include <random>
#include <array>
#include <functional>
#include <algorithm>

#include <boost/geometry/algorithms/equals.hpp>
#include <boost/geometry/algorithms/transform.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/core/point_type.hpp>

#include <boost/geometry/extensions/random/policies/uniform_default_policy.hpp>

#include <boost/geometry/extensions/random/strategies/cartesian/uniform_point_distribution_box.hpp>

namespace boost { namespace geometry
{

namespace policy { namespace uniform_point_distribution
{

template
<
typename Point,
typename DomainGeometry,
typename BoxStrategy = void,
typename CalculationType = void
>
struct box
{
private:
typedef typename boost::mpl::if_
<
boost::is_void<BoxStrategy>,
typename boost::geometry::strategy::uniform_point_distribution::services::
default_box_strategy
<
Point,
DomainGeometry
>::type,
BoxStrategy
>::type box_strategy;
typedef typename boost::mpl::if_
<
boost::is_void<CalculationType>,
typename select_most_precise
<
typename coordinate_type<Point>::type,
double
>::type,
CalculationType
>::type ct;
box_strategy m_box_strategy;
public:
box(DomainGeometry const& d, box_strategy const& b = box_strategy()) : m_box_strategy(b) {}
bool equals(DomainGeometry const& l_domain,
DomainGeometry const& r_domain,
box const& r_policy) const
{
return boost::geometry::equals(l_domain, r_domain);
}
template<typename Generator>
Point apply(Generator& g, DomainGeometry const& d)
{
typedef typename point_type<DomainGeometry>::type domain_point;
domain_point p;
std::array<ct, dimension<DomainGeometry>::value> fractions;
std::uniform_real_distribution<ct> dist(0, 1);
std::generate(fractions.begin(), fractions.end(), std::bind(dist, std::ref(g)));
p = m_box_strategy.template apply<>(d, fractions);
Point r;
boost::geometry::transform(p, r);
return r;
}
void reset(DomainGeometry const&) {};
};

namespace services
{

template
<
typename Point,
typename DomainGeometry,
int Dim,
typename CsTag
>
struct default_policy
<
Point,
DomainGeometry,
box_tag,
single_tag,
Dim,
CsTag
>
{
typedef box<Point, DomainGeometry> type;
};

} // namespace services

}} // namespace policy::uniform_point_distribution

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_BOX_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2020 Tinko Bartels, Berlin, Germany.

// Contributed and/or modified by Tinko Bartels,
// as part of Google Summer of Code 2019 program.

// 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_EXTENSIONS_RANDOM_POLICIES_UNIFORM_CONVEX_HULL_REJECTION_HPP
#define BOOST_GEOMETRY_EXTENSIONS_RANDOM_POLICIES_UNIFORM_CONVEX_HULL_REJECTION_HPP

#include <random>
#include <vector>

#include <boost/geometry/algorithms/equals.hpp>
#include <boost/geometry/algorithms/transform.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/algorithms/convex_hull.hpp>

#include <boost/geometry/strategies/cartesian/area.hpp>
#include <boost/geometry/strategies/geographic/area.hpp>
#include <boost/geometry/strategies/spherical/area.hpp>

#include <boost/geometry/strategies/cartesian/point_in_poly_winding.hpp>
#include <boost/geometry/strategies/spherical/point_in_poly_winding.hpp>
#include <boost/geometry/strategies/geographic/point_in_poly_winding.hpp>

#include <boost/geometry/extensions/random/strategies/cartesian/uniform_point_distribution_triangle.hpp>
#include <boost/geometry/extensions/random/policies/uniform_convex_polygon.hpp>

namespace boost { namespace geometry
{

namespace policy { namespace uniform_point_distribution
{

// The following strategy is suitable for geometries for which
// a Triangle sampling strategy can be provided
// a SideStrategy that computes the triangle area can be provided.
template
<
typename Point,
typename DomainGeometry,
typename TriangleStrategy = void,
typename AreaStrategy = void,
// typename ConvexHullStrategy
typename WithinStrategy = void
>
struct convex_hull_rejection
{
private:
typedef typename boost::mpl::if_
<
boost::is_void<TriangleStrategy>,
typename boost::geometry::strategy::uniform_point_distribution::services
::default_triangle_strategy
<
Point, typename point_type<DomainGeometry>::type
>::type,
TriangleStrategy
>::type triangle_strategy;
typedef typename boost::mpl::if_
<
boost::is_void<AreaStrategy>,
typename boost::geometry::strategy::area::services::default_strategy
<
typename cs_tag<DomainGeometry>::type
>::type,
AreaStrategy
>::type area_strategy;
typedef typename boost::mpl::if_
<
boost::is_void<WithinStrategy>,
typename boost::geometry::strategy::within::services::default_strategy
<
Point,
DomainGeometry
>::type,
WithinStrategy
>::type within_strategy;
typedef typename point_type<DomainGeometry>::type domain_point_type;
typedef boost::geometry::model::ring<domain_point_type> ring;
ring m_hull;
convex_polygon<Point, ring, triangle_strategy, area_strategy> m_policy;
within_strategy m_within_strategy;
public:
convex_hull_rejection(DomainGeometry const& d,
triangle_strategy const& t = triangle_strategy(),
area_strategy const& a = area_strategy(),
within_strategy const& w = within_strategy()) : m_within_strategy(w)

{
boost::geometry::convex_hull(d, m_hull);
m_policy = convex_polygon
<
Point,
ring,
triangle_strategy,
area_strategy
>(m_hull, t, a);
}
bool equals(DomainGeometry const& l_domain,
DomainGeometry const& r_domain,
convex_hull_rejection const& r_policy) const
{
return boost::geometry::equals(l_domain, r_domain)
&& m_policy.equals(l_domain, r_domain, r_policy.m_policy);
}
template<typename Generator>
Point apply(Generator& g, DomainGeometry const& d)
{
Point p;
do {
p = m_policy.apply(g, m_hull);
} while( !boost::geometry::within(p, d, m_within_strategy) );
return p;
}
void reset(DomainGeometry const&) {};
};

}} // namespace policy::uniform_point_distribution

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_EXTENSIONS_RANDOM_STRATEGIES_AGNOSTIC_UNIFORM_CONVEX_HULL_REJECTION_HPP
Loading

0 comments on commit ec6adac

Please sign in to comment.