UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
predicate_interval.hpp
1
42#ifndef UFO_CONTAINER_TREE_PREDICATE_PREDICATE_INTERVAL_HPP
43#define UFO_CONTAINER_TREE_PREDICATE_PREDICATE_INTERVAL_HPP
44
45// UFO
46#include <ufo/utility/type_traits.hpp>
47
48// STL
49#include <algorithm>
50#include <cassert>
51#include <cmath>
52#include <concepts>
53#include <limits>
54
55namespace ufo::pred
56{
57template <class T, bool Negated>
59 using value_type = T::value_type;
60
61 constexpr PredicateInterval() noexcept = default;
62
63 constexpr PredicateInterval(value_type value) noexcept : min(value), max(value) {}
64
65 constexpr PredicateInterval(value_type min, value_type max) noexcept
66 : min(min), max(max)
67 {
68 }
69
70 value_type min = std::numeric_limits<value_type>::lowest();
71 value_type max = std::numeric_limits<value_type>::max();
72};
73
74template <class T, bool Negated>
75[[nodiscard]] constexpr PredicateInterval<T, !Negated> operator!(
76 PredicateInterval<T, Negated> const& p) noexcept
77{
78 return PredicateInterval<T, !Negated>(p.min, p.max);
79}
80
81template <class T, bool Negated>
82[[nodiscard]] constexpr PredicateInterval<T, Negated> operator==(
83 [[maybe_unused]] PredicateInterval<T, Negated> const& p,
84 typename T::value_type const& v) noexcept
85{
86 return PredicateInterval<T, Negated>(v, v);
87}
88
89template <class T, bool Negated>
90[[nodiscard]] constexpr PredicateInterval<T, Negated> operator==(
91 typename T::value_type const& v,
92 [[maybe_unused]] PredicateInterval<T, Negated> const& p) noexcept
93{
94 return PredicateInterval<T, Negated>(v, v);
95}
96
97template <class T, bool Negated>
98[[nodiscard]] constexpr PredicateInterval<T, true> operator!=(
99 [[maybe_unused]] PredicateInterval<T, Negated> const& p,
100 typename T::value_type const& v) noexcept
101{
102 return PredicateInterval<T, true>(v, v);
103}
104
105template <class T, bool Negated>
106[[nodiscard]] constexpr PredicateInterval<T, true> operator!=(
107 typename T::value_type const& v,
108 [[maybe_unused]] PredicateInterval<T, Negated> const& p) noexcept
109{
110 return PredicateInterval<T, true>(v, v);
111}
112
113template <class T, bool Negated>
114[[nodiscard]] constexpr PredicateInterval<T, Negated> operator<=(
115 PredicateInterval<T, Negated> const& p, typename T::value_type const& v) noexcept
116{
117 return PredicateInterval<T, Negated>(std::min(p.min, v), v);
118}
119
120template <class T, bool Negated>
121[[nodiscard]] constexpr PredicateInterval<T, Negated> operator<=(
122 typename T::value_type const& v, PredicateInterval<T, Negated> const& p) noexcept
123{
124 return PredicateInterval<T, Negated>(v, std::max(p.max, v));
125}
126
127template <class T, bool Negated>
128[[nodiscard]] constexpr PredicateInterval<T, Negated> operator<(
129 PredicateInterval<T, Negated> const& p, typename T::value_type const& v) noexcept
130{
131 using value_type = typename T::value_type;
132 if constexpr (std::floating_point<value_type>) {
133 return p <= std::nextafter(v, std::numeric_limits<value_type>::lowest());
134 } else {
135 assert(v > v - value_type(1));
136 return p <= v - value_type(1);
137 }
138}
139
140template <class T, bool Negated>
141[[nodiscard]] constexpr PredicateInterval<T, Negated> operator<(
142 typename T::value_type const& v, PredicateInterval<T, Negated> const& p) noexcept
143{
144 using value_type = typename T::value_type;
145 if constexpr (std::floating_point<value_type>) {
146 return std::nextafter(v, std::numeric_limits<value_type>::max()) <= p;
147 } else {
148 assert(v < v + value_type(1));
149 return v + value_type(1) <= p;
150 }
151}
152
153template <class T, bool Negated>
154[[nodiscard]] constexpr PredicateInterval<T, Negated> operator>=(
155 PredicateInterval<T, Negated> const& p, typename T::value_type const& v) noexcept
156{
157 return v <= p;
158}
159
160template <class T, bool Negated>
161[[nodiscard]] constexpr PredicateInterval<T, Negated> operator>=(
162 typename T::value_type const& v, PredicateInterval<T, Negated> const& p) noexcept
163{
164 return p <= v;
165}
166
167template <class T, bool Negated>
168[[nodiscard]] constexpr PredicateInterval<T, Negated> operator>(
169 PredicateInterval<T, Negated> const& p, typename T::value_type const& v) noexcept
170{
171 return v < p;
172}
173
174template <class T, bool Negated>
175[[nodiscard]] constexpr PredicateInterval<T, Negated> operator>(
176 typename T::value_type const& v, PredicateInterval<T, Negated> const& p) noexcept
177{
178 return p < v;
179}
180} // namespace ufo::pred
181
182#endif // UFO_CONTAINER_TREE_PREDICATE_PREDICATE_INTERVAL_HPP
constexpr Vec< Geometry::dimension(), typename Geometry::value_type > max(Geometry const &g)
Returns the maximum coordinate of the minimum spanning axis-aligned bounding box of a geometry.
Definition fun.hpp:72
constexpr Vec< Geometry::dimension(), typename Geometry::value_type > min(Geometry const &g)
Returns the minimum coordinate of the minimum spanning axis-aligned bounding box of a geometry.
Definition fun.hpp:58