|
| 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 2020 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_GENERIC_ROBUST_PREDICATES_STRATEGIES_CARTESIAN_DETAIL_ALMOST_STATIC_FILTER_HPP |
| 13 | +#define BOOST_GEOMETRY_EXTENSIONS_GENERIC_ROBUST_PREDICATES_STRATEGIES_CARTESIAN_DETAIL_ALMOST_STATIC_FILTER_HPP |
| 14 | + |
| 15 | +#include <array> |
| 16 | +#include <algorithm> |
| 17 | +#include <limits> |
| 18 | + |
| 19 | +#include <boost/geometry/extensions/generic_robust_predicates/strategies/cartesian/detail/expression_tree.hpp> |
| 20 | + |
| 21 | +namespace boost { namespace geometry |
| 22 | +{ |
| 23 | + |
| 24 | +namespace detail { namespace generic_robust_predicates |
| 25 | +{ |
| 26 | + |
| 27 | +template <typename Filter, std::size_t N, std::size_t End> |
| 28 | +struct make_filter_impl |
| 29 | +{ |
| 30 | + template <typename ExtremaArray, typename ...Reals> |
| 31 | + static Filter apply(const ExtremaArray& extrema, const Reals&... args) |
| 32 | + { |
| 33 | + return make_filter_impl<Filter, N + 1, End> |
| 34 | + ::apply(extrema, args..., extrema[N]); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +template <typename Filter, std::size_t End> |
| 39 | +struct make_filter_impl<Filter, End, End> |
| 40 | +{ |
| 41 | + template <typename ExtremaArray, typename ...Reals> |
| 42 | + static Filter apply(const ExtremaArray& extrema, const Reals&... args) |
| 43 | + { |
| 44 | + Filter f(args...); |
| 45 | + return f; |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +template |
| 50 | +< |
| 51 | + typename Expression, |
| 52 | + typename CalculationType, |
| 53 | + typename StaticFilter |
| 54 | +> |
| 55 | +class almost_static_filter |
| 56 | +{ |
| 57 | +private: |
| 58 | + using expression_max_argn = max_argn<Expression>; |
| 59 | + using ct = CalculationType; |
| 60 | + using extrema_array = std::array<ct, 2 * expression_max_argn::value>; |
| 61 | + extrema_array m_extrema; |
| 62 | + StaticFilter m_filter; |
| 63 | +public: |
| 64 | + const StaticFilter& filter() const { return m_filter; } |
| 65 | + inline almost_static_filter() |
| 66 | + { |
| 67 | + std::fill(m_extrema.begin(), |
| 68 | + m_extrema.begin() + m_extrema.size() / 2, |
| 69 | + -std::numeric_limits<ct>::infinity()); |
| 70 | + std::fill(m_extrema.begin() + m_extrema.size() / 2, |
| 71 | + m_extrema.end(), |
| 72 | + std::numeric_limits<ct>::infinity()); |
| 73 | + } |
| 74 | + template <typename ...Reals> |
| 75 | + int apply(const Reals&... args) const |
| 76 | + { |
| 77 | + return m_filter.apply(args...); |
| 78 | + } |
| 79 | + |
| 80 | + template <typename ...Reals> |
| 81 | + inline void update_extrema(const Reals&... args) |
| 82 | + { |
| 83 | + std::array<ct, sizeof...(Reals)> input {{ static_cast<ct>(args)... }}; |
| 84 | + for(int i = 0; i < m_extrema.size() / 2; ++i) |
| 85 | + { |
| 86 | + m_extrema[i] = std::max(m_extrema[i], input[i]); |
| 87 | + } |
| 88 | + for(int i = m_extrema.size() / 2; i < m_extrema.size(); ++i) |
| 89 | + { |
| 90 | + m_extrema[i] = std::min(m_extrema[i], input[i]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + template <typename ...Reals> |
| 95 | + inline bool update_extrema_check(const Reals&... args) |
| 96 | + { |
| 97 | + bool changed = false; |
| 98 | + std::array<ct, sizeof...(Reals)> input {{ static_cast<ct>(args)... }}; |
| 99 | + for(int i = 0; i < m_extrema.size() / 2; ++i) |
| 100 | + { |
| 101 | + if(input[i] > m_extrema[i]) |
| 102 | + { |
| 103 | + changed = true; |
| 104 | + m_extrema[i] = input[i]; |
| 105 | + } |
| 106 | + } |
| 107 | + for(int i = m_extrema.size() / 2; i < m_extrema.size(); ++i) |
| 108 | + { |
| 109 | + if(input[i] < m_extrema[i]) |
| 110 | + { |
| 111 | + changed = true; |
| 112 | + m_extrema[i] = input[i]; |
| 113 | + } |
| 114 | + } |
| 115 | + return changed; |
| 116 | + } |
| 117 | + |
| 118 | + inline void update_filter() |
| 119 | + { |
| 120 | + m_filter = make_filter_impl |
| 121 | + < |
| 122 | + StaticFilter, |
| 123 | + 0, |
| 124 | + 2 * expression_max_argn::value |
| 125 | + >::apply(m_extrema); |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +}} // namespace detail::generic_robust_predicates |
| 130 | + |
| 131 | +}} // namespace boost::geometry |
| 132 | + |
| 133 | +#endif // BOOST_GEOMETRY_EXTENSIONS_GENERIC_ROBUST_PREDICATES_STRATEGIES_CARTESIAN_DETAIL_ALMOST_STATIC_FILTER_HPP |
0 commit comments