UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
average.hpp
1
45#ifndef UFO_VISION_COLOR_AVERAGE_HPP
46#define UFO_VISION_COLOR_AVERAGE_HPP
47
48// UFO
49#include <ufo/vision/color/arithmetic.hpp>
50#include <ufo/vision/color/concepts.hpp>
51#include <ufo/vision/color/convert.hpp>
52#include <ufo/vision/color/type_traits.hpp>
53#include <ufo/vision/color/weight.hpp>
54
55// STL
56#include <algorithm>
57#include <concepts>
58#include <functional>
59#include <iterator>
60#include <ranges>
61
62namespace ufo
63{
76template <Color C>
77[[nodiscard]] constexpr C average(C a, C const& b)
78{
79 if constexpr (ColorWithWeight<C>) {
80 return a + b;
81 } else {
83 }
84}
85
92template <std::ranges::input_range R>
93 requires Color<std::ranges::range_value_t<R>>
94[[nodiscard]] constexpr std::ranges::range_value_t<R> average(R&& r)
95{
96 using C = std::ranges::range_value_t<R>;
97 using D = typename color_traits<C>::template rebind<float, color_traits<C>::flags |
99
100 if constexpr (std::same_as<C, D>) {
101 return std::ranges::fold_left(r, D{}, std::plus{});
102 } else if constexpr (!ColorWithWeight<C> && FloatingPointColor<C>) {
103 auto sum = std::ranges::fold_left(
104 r | std::views::transform([](C c) { return addWeight(c); }), D{}, std::plus{});
105 return removeWeight(sum);
106 } else {
107 // Normalise to weighted float, sum, then convert back to target type
108 auto sum = std::ranges::fold_left(
109 r | std::views::transform([](C c) { return convert<D>(c); }), D{}, std::plus{});
110 return convert<C>(sum);
111 }
112}
113
122template <std::input_iterator InputIt, std::sentinel_for<InputIt> Sentinel>
123 requires Color<std::iter_value_t<InputIt>>
124[[nodiscard]] constexpr std::iter_value_t<InputIt> average(InputIt first, Sentinel last)
125{
126 return average(std::ranges::subrange(std::move(first), std::move(last)));
127}
128
135template <Color C>
136[[nodiscard]] constexpr C average(std::initializer_list<C> il)
137{
138 return average(std::ranges::subrange(il));
139}
140
144} // namespace ufo
145
146#endif // UFO_VISION_COLOR_AVERAGE_HPP
Colors that include a weight channel.
Definition concepts.hpp:109
Colors with a floating-point channel type.
Definition concepts.hpp:85
constexpr add_weight_t< C > addWeight(C color, float w=1.0f) noexcept
Returns a copy of color with a weight field set to w.
Definition weight.hpp:87
constexpr remove_weight_t< C > removeWeight(C color) noexcept
Returns a copy of color with the weight field removed.
Definition weight.hpp:137
constexpr C average(C a, C const &b)
Computes the average of two colors.
Definition average.hpp:77
@ Weight
Include an accumulation weight.
All vision-related classes and functions.
Definition cloud.hpp:49
constexpr T b(Lab< T, Flags > color) noexcept
Returns the un-weighted blue–yellow axis value.
Definition lab.hpp:326
constexpr T a(Lab< T, Flags > color) noexcept
Returns the un-weighted green–red axis value.
Definition lab.hpp:310
constexpr T sum(Vec< Dim, T > const &v) noexcept
Computes the sum of all elements in a vector.
Definition vec.hpp:1309
Primary template - intentionally undefined.