Bayes Filters Library
HistoryBuffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 
10 using namespace bfl;
11 using namespace Eigen;
12 
13 
14 HistoryBuffer::HistoryBuffer(const std::size_t state_size) noexcept :
15  state_size_(state_size)
16 { }
17 
18 
19 HistoryBuffer::HistoryBuffer(HistoryBuffer&& history_buffer) noexcept :
20  window_(history_buffer.window_),
21  history_buffer_(std::move(history_buffer.history_buffer_)),
22  state_size_(history_buffer.state_size_)
23 {
24  history_buffer.window_ = 0;
25  history_buffer.state_size_ = 0;
26 }
27 
28 
30 {
31  if (this == &history_buffer)
32  return *this;
33 
34  window_ = history_buffer.window_;
35  history_buffer.window_ = 0;
36 
37  state_size_ = history_buffer.state_size_;
38  history_buffer.state_size_ = 0;
39 
40  history_buffer_ = std::move(history_buffer.history_buffer_);
41 
42  return *this;
43 }
44 
45 
46 void HistoryBuffer::addElement(const Ref<const VectorXd>& element)
47 {
48  history_buffer_.push_front(element);
49 
50  if (history_buffer_.size() > window_)
51  history_buffer_.pop_back();
52 }
53 
54 
56 {
57  MatrixXd hist_out(state_size_, history_buffer_.size());
58 
59  unsigned int i = 0;
60  for (const Ref<const VectorXd>& element : history_buffer_)
61  hist_out.col(i++) = element;
62 
63  return hist_out;
64 }
65 
66 
67 bool HistoryBuffer::setHistorySize(const unsigned int window)
68 {
69  unsigned int tmp;
70  if (window == window_) return true;
71  else if (window < 2) tmp = 2;
72  else if (window >= max_window_) tmp = max_window_;
73  else tmp = window;
74 
75  if (tmp < window_ && tmp < history_buffer_.size())
76  {
77  for (unsigned int i = 0; i < (window_ - tmp); ++i)
78  history_buffer_.pop_back();
79  }
80 
81  window_ = tmp;
82 
83  return true;
84 }
85 
86 
88 {
89  return setHistorySize(window_ - 1);
90 }
91 
92 
94 {
95  return setHistorySize(window_ + 1);
96 }
97 
98 
100 {
101  history_buffer_.clear();
102  return true;
103 }
bfl::HistoryBuffer::HistoryBuffer
HistoryBuffer(const std::size_t state_size) noexcept
Definition: HistoryBuffer.cpp:14
bfl::HistoryBuffer::window_
unsigned int window_
Definition: HistoryBuffer.h:47
bfl::HistoryBuffer::decreaseHistorySize
bool decreaseHistorySize()
Definition: HistoryBuffer.cpp:87
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::HistoryBuffer::increaseHistorySize
bool increaseHistorySize()
Definition: HistoryBuffer.cpp:93
HistoryBuffer.h
bfl::HistoryBuffer::setHistorySize
bool setHistorySize(const unsigned int window)
Definition: HistoryBuffer.cpp:67
bfl::HistoryBuffer
Definition: HistoryBuffer.h:20
bfl::HistoryBuffer::clear
bool clear()
Definition: HistoryBuffer.cpp:99
bfl::HistoryBuffer::operator=
HistoryBuffer & operator=(HistoryBuffer &&history_buffer) noexcept
Definition: HistoryBuffer.cpp:29
bfl::HistoryBuffer::getHistoryBuffer
Eigen::MatrixXd getHistoryBuffer() const
Definition: HistoryBuffer.cpp:55
bfl::HistoryBuffer::addElement
void addElement(const Eigen::Ref< const Eigen::VectorXd > &element)
Definition: HistoryBuffer.cpp:46