|
| 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