38 #ifndef PCL_IO_IMPL_BUFFERS_HPP
39 #define PCL_IO_IMPL_BUFFERS_HPP
56 static float invalid () {
return std::numeric_limits<float>::quiet_NaN (); };
57 static bool is_invalid (
float value) {
return std::isnan (value); };
63 static double invalid () {
return std::numeric_limits<double>::quiet_NaN (); };
64 static bool is_invalid (
double value) {
return std::isnan (value); };
95 template <
typename T> T
102 template <
typename T>
void
105 assert (data.size () == size_);
106 std::lock_guard<std::mutex> lock (data_mutex_);
111 template <
typename T>
113 unsigned char window_size)
115 , window_size_ (window_size)
116 , midpoint_ (window_size_ / 2)
117 , data_current_idx_ (window_size_ - 1)
120 assert (window_size_ > 0);
122 data_.resize (window_size_);
123 for (std::size_t i = 0; i < window_size_; ++i)
126 data_argsort_indices_.resize (
size_);
127 for (std::size_t i = 0; i <
size_; ++i)
129 data_argsort_indices_[i].resize (window_size_);
130 for (std::size_t j = 0; j < window_size_; ++j)
131 data_argsort_indices_[i][j] = j;
134 data_invalid_count_.resize (
size_, window_size_);
137 template <
typename T>
141 template <
typename T> T
144 assert (idx < size_);
145 int midpoint = (window_size_ - data_invalid_count_[idx]) / 2;
146 return (data_[data_argsort_indices_[idx][midpoint]][idx]);
149 template <
typename T>
void
152 assert (data.size () == size_);
153 std::lock_guard<std::mutex> lock (data_mutex_);
155 if (++data_current_idx_ >= window_size_)
156 data_current_idx_ = 0;
161 for (std::size_t i = 0; i < size_; ++i)
163 const T& new_value = data[i];
164 const T& old_value = data_[data_current_idx_][i];
167 if (compare (new_value, old_value) == 0)
169 std::vector<unsigned char>& argsort_indices = data_argsort_indices_[i];
172 if (compare (new_value, old_value) == 1)
174 for (
int j = 0; j < window_size_; ++j)
175 if (argsort_indices[j] == data_current_idx_)
178 while (k < window_size_ && compare (new_value, data_[argsort_indices[k]][i]) == 1)
180 std::swap (argsort_indices[k - 1], argsort_indices[k]);
188 for (
int j = window_size_ - 1; j >= 0; --j)
189 if (argsort_indices[j] == data_current_idx_)
192 while (k >= 0 && compare (new_value, data_[argsort_indices[k]][i]) == -1)
194 std::swap (argsort_indices[k], argsort_indices[k + 1]);
201 if (new_is_invalid && !old_is_invalid)
202 ++data_invalid_count_[i];
203 else if (!new_is_invalid && old_is_invalid)
204 --data_invalid_count_[i];
208 data_[data_current_idx_].swap (data);
212 template <
typename T>
int
217 if (a_is_invalid && b_is_invalid)
225 return a > b ? 1 : -1;
228 template <
typename T>
230 unsigned char window_size)
232 , window_size_ (window_size)
233 , data_current_idx_ (window_size_ - 1)
236 assert (window_size_ > 0);
238 data_.resize (window_size_);
239 for (std::size_t i = 0; i < window_size_; ++i)
242 data_sum_.resize (
size_, 0);
243 data_invalid_count_.resize (
size_, window_size_);
246 template <
typename T>
250 template <
typename T> T
253 assert (idx < size_);
254 if (data_invalid_count_[idx] == window_size_)
256 return (data_sum_[idx] /
static_cast<T
> (window_size_ - data_invalid_count_[idx]));
259 template <
typename T>
void
262 assert (data.size () == size_);
263 std::lock_guard<std::mutex> lock (data_mutex_);
265 if (++data_current_idx_ >= window_size_)
266 data_current_idx_ = 0;
271 for (std::size_t i = 0; i < size_; ++i)
273 const float& new_value = data[i];
274 const float& old_value = data_[data_current_idx_][i];
279 data_sum_[i] -= old_value;
281 data_sum_[i] += new_value;
283 if (new_is_invalid && !old_is_invalid)
284 ++data_invalid_count_[i];
285 else if (!new_is_invalid && old_is_invalid)
286 --data_invalid_count_[i];
290 data_[data_current_idx_].swap (data);
void push(std::vector< T > &data) override
Insert a new chunk of data into the buffer.
AverageBuffer(std::size_t size, unsigned char window_size)
Construct a buffer of given size with given running window size.
~AverageBuffer() override
T operator[](std::size_t idx) const override
Access an element at a given index.
An abstract base class for fixed-size data buffers.
void push(std::vector< T > &data) override
Insert a new chunk of data into the buffer.
T operator[](std::size_t idx) const override
Access an element at a given index.
SingleBuffer(std::size_t size)
Construct a buffer of given size.
Defines all the PCL and non-PCL macros used.
static bool is_invalid(double value)
static bool is_invalid(float value)
static bool is_invalid(T value)