UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
range.hpp
1
41#ifndef UFO_CORE_RANGE_HPP
42#define UFO_CORE_RANGE_HPP
43
44// STL
45#include <format>
46#include <ostream>
47#include <type_traits>
48#include <utility>
49
50namespace ufo
51{
65template <typename T>
66struct Range {
70 using value_type = T;
71
76
81
89 // struct Comparator {
90 // using is_transparent = std::true_type;
91
92 // /**
93 // * @brief Interval ordering for associative containers.
94 // *
95 // * @param lhs Left operand (Range or pair).
96 // * @param rhs Right operand (Range or pair).
97 // * @return True if lhs.upper < rhs.lower.
98 // */
99 // template <typename L, typename R>
100 // bool operator()(L const& lhs, R const& rhs) const
101 // {
102 // return lhs.upper < rhs.lower;
103 // }
104 // };
105
106 struct Comparator {
107 using range_type = std::pair<T, T>;
108 using is_transparent = std::true_type;
109
110 [[nodiscard]] constexpr bool operator()(Range lhs, Range rhs) const noexcept
111 {
112 // return lhs.upper < rhs.lower;
113 return lhs.lower < rhs.lower && lhs.upper < rhs.upper;
114 }
115 [[nodiscard]] constexpr bool operator()(Range lhs, range_type rhs) const noexcept
116 {
117 // return lhs.upper < rhs.first;
118 return lhs.lower < rhs.first && lhs.upper < rhs.second;
119 }
120 [[nodiscard]] constexpr bool operator()(range_type lhs, Range rhs) const noexcept
121 {
122 // return lhs.second < rhs.lower;
123 return lhs.first < rhs.lower && lhs.second < rhs.upper;
124 }
125 };
126
133 [[nodiscard]] constexpr bool contains(value_type value) const noexcept
134 {
135 return lower <= value && value <= upper;
136 }
137
144 [[nodiscard]] constexpr bool contains(Range const& other) const noexcept
145 {
146 return lower <= other.lower && other.upper <= upper;
147 }
148
155 [[nodiscard]] constexpr bool overlaps(Range const& other) const noexcept
156 {
157 return lower <= other.upper && other.lower <= upper;
158 }
159
167 [[nodiscard]] friend constexpr bool operator==(Range const& lhs,
168 Range const& rhs) noexcept = default;
169
177 [[nodiscard]] friend constexpr bool operator<(Range const& lhs,
178 Range const& rhs) noexcept
179 {
180 return lhs.upper < rhs.lower;
181 }
182
190 [[nodiscard]] friend constexpr bool operator<=(Range const& lhs,
191 Range const& rhs) noexcept
192 {
193 return lhs.upper <= rhs.lower;
194 }
195
203 [[nodiscard]] friend constexpr bool operator>(Range const& lhs,
204 Range const& rhs) noexcept
205 {
206 return lhs.lower > rhs.upper;
207 }
208
216 [[nodiscard]] friend constexpr bool operator>=(Range const& lhs,
217 Range const& rhs) noexcept
218 {
219 return lhs.lower >= rhs.upper;
220 }
221
229 [[nodiscard]] friend constexpr bool operator==(Range const& lhs,
230 value_type rhs) noexcept
231 {
232 return lhs.lower == rhs && lhs.upper == rhs;
233 }
234
242 [[nodiscard]] friend constexpr bool operator<(Range const& lhs, value_type rhs) noexcept
243 {
244 return lhs.upper < rhs;
245 }
246
254 [[nodiscard]] friend constexpr bool operator<=(Range const& lhs,
255 value_type rhs) noexcept
256 {
257 return lhs.upper <= rhs;
258 }
259
267 [[nodiscard]] friend constexpr bool operator>(Range const& lhs, value_type rhs) noexcept
268 {
269 return lhs.lower > rhs;
270 }
271
279 [[nodiscard]] friend constexpr bool operator>=(Range const& lhs,
280 value_type rhs) noexcept
281 {
282 return lhs.lower >= rhs;
283 }
284
292 [[nodiscard]] friend constexpr bool operator==(value_type lhs,
293 Range const& rhs) noexcept
294 {
295 return rhs == lhs;
296 }
297
305 [[nodiscard]] friend constexpr bool operator<(value_type lhs, Range const& rhs) noexcept
306 {
307 return rhs > lhs;
308 }
309
317 [[nodiscard]] friend constexpr bool operator<=(value_type lhs,
318 Range const& rhs) noexcept
319 {
320 return rhs >= lhs;
321 }
322
330 [[nodiscard]] friend constexpr bool operator>(value_type lhs, Range const& rhs) noexcept
331 {
332 return rhs < lhs;
333 }
334
342 [[nodiscard]] friend constexpr bool operator>=(value_type lhs,
343 Range const& rhs) noexcept
344 {
345 return rhs <= lhs;
346 }
347};
348
352} // namespace ufo
353
354template <class T>
355struct std::formatter<ufo::Range<T>> {
356 constexpr auto parse(std::format_parse_context& ctx) const { return ctx.begin(); }
357
358 auto format(ufo::Range<T> const& r, std::format_context& ctx) const
359 {
360 if (r.lower == r.upper) {
361 return std::format_to(ctx.out(), "[{}]", +r.lower);
362 } else if constexpr (std::is_floating_point_v<T>) {
363 return std::format_to(ctx.out(), "[{},{}]", +r.lower, +r.upper);
364 } else {
365 return std::format_to(ctx.out(), "[{}..{}]", +r.lower, +r.upper);
366 }
367 }
368};
369
370namespace ufo
371{
386template <class T>
387std::ostream& operator<<(std::ostream& out, Range<T> const& r)
388{
389 return out << std::format("{}", r);
390}
391} // namespace ufo
392
393#endif // UFO_CORE_RANGE_HPP
All vision-related classes and functions.
Definition cloud.hpp:49
Comparator for interval ordering in associative containers.
Definition range.hpp:106
Represents a closed interval [lower, upper] of a scalar type.
Definition range.hpp:66
friend constexpr bool operator>(Range const &lhs, value_type rhs) noexcept
Range-scalar ordering: [a, b] > t iff a > t.
Definition range.hpp:267
friend constexpr bool operator<=(Range const &lhs, Range const &rhs) noexcept
Interval ordering: [a, b] <= [c, d] iff b <= c.
Definition range.hpp:190
friend constexpr bool operator>=(Range const &lhs, value_type rhs) noexcept
Range-scalar ordering: [a, b] >= t iff a >= t.
Definition range.hpp:279
value_type lower
Lower bound of the interval (inclusive).
Definition range.hpp:75
friend constexpr bool operator>(Range const &lhs, Range const &rhs) noexcept
Interval ordering: [a, b] > [c, d] iff a > d.
Definition range.hpp:203
friend constexpr bool operator<=(value_type lhs, Range const &rhs) noexcept
Scalar-range ordering: t <= [a, b] iff t <= a.
Definition range.hpp:317
constexpr bool contains(Range const &other) const noexcept
Checks if another range is fully contained within this interval.
Definition range.hpp:144
friend constexpr bool operator>=(Range const &lhs, Range const &rhs) noexcept
Interval ordering: [a, b] >= [c, d] iff a >= d.
Definition range.hpp:216
value_type upper
Upper bound of the interval (inclusive).
Definition range.hpp:80
friend constexpr bool operator>=(value_type lhs, Range const &rhs) noexcept
Scalar-range ordering: t >= [a, b] iff t >= b.
Definition range.hpp:342
T value_type
Scalar type of the interval bounds.
Definition range.hpp:70
friend constexpr bool operator==(Range const &lhs, value_type rhs) noexcept
Equality comparison with scalar: [a, b] == t iff a == t && b == t.
Definition range.hpp:229
friend constexpr bool operator<(Range const &lhs, Range const &rhs) noexcept
Interval ordering: [a, b] < [c, d] iff b < c.
Definition range.hpp:177
friend constexpr bool operator==(Range const &lhs, Range const &rhs) noexcept=default
Equality comparison.
friend constexpr bool operator==(value_type lhs, Range const &rhs) noexcept
Equality comparison with scalar: t == [a, b] iff t == a && t == b.
Definition range.hpp:292
friend constexpr bool operator<=(Range const &lhs, value_type rhs) noexcept
Range-scalar ordering: [a, b] <= t iff b <= t.
Definition range.hpp:254
friend constexpr bool operator>(value_type lhs, Range const &rhs) noexcept
Scalar-range ordering: t > [a, b] iff t > b.
Definition range.hpp:330
friend constexpr bool operator<(Range const &lhs, value_type rhs) noexcept
Range-scalar ordering: [a, b] < t iff b < t.
Definition range.hpp:242
friend constexpr bool operator<(value_type lhs, Range const &rhs) noexcept
Scalar-range ordering: t < [a, b] iff t < a.
Definition range.hpp:305
constexpr bool overlaps(Range const &other) const noexcept
Checks if this interval overlaps with another range.
Definition range.hpp:155
constexpr bool contains(value_type value) const noexcept
Checks if a value is contained within the interval.
Definition range.hpp:133