|
| 1 | +// Copyright (C) 2025 COGIP Robotics association <cogip35@gmail.com> |
| 2 | +// This file is subject to the terms and conditions of the GNU Lesser |
| 3 | +// General Public License v2.1. See the file LICENSE in the top level directory. |
| 4 | + |
| 5 | +/// @ingroup lib_models |
| 6 | +/// @{ |
| 7 | +/// @file |
| 8 | +/// @brief PoseBuffer declaration |
| 9 | +/// @author Eric Courtois <eric.courtois@gmail.com> |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include "models/pose_buffer.hpp" |
| 14 | +#include "models/Pose.hpp" |
| 15 | + |
| 16 | +#include <ostream> |
| 17 | + |
| 18 | +namespace cogip { |
| 19 | + |
| 20 | +namespace models { |
| 21 | + |
| 22 | +class PoseBuffer { |
| 23 | +public: |
| 24 | + /// Constructor. |
| 25 | + PoseBuffer( |
| 26 | + pose_buffer_t* data ///< [in] Pointer to an existing data structure |
| 27 | + ///< If nullptr, will allocate one internally |
| 28 | + ); |
| 29 | + |
| 30 | + /// Destructor |
| 31 | + ~PoseBuffer(); |
| 32 | + |
| 33 | + /// Return the pointer to the underlying data structure. |
| 34 | + pose_buffer_t* data() { return data_; }; |
| 35 | + |
| 36 | + /// Return head. |
| 37 | + double head() const { return data_->head; }; |
| 38 | + |
| 39 | + /// Return tail. |
| 40 | + double tail() const { return data_->tail; }; |
| 41 | + |
| 42 | + /// Return true if the buffer is full, false otherwise. |
| 43 | + double full() const; |
| 44 | + |
| 45 | + /// Get the number of stored positions |
| 46 | + std::size_t size() const; |
| 47 | + |
| 48 | + /// Add a new pose to the buffer. |
| 49 | + void push(float x, float y, float angle); |
| 50 | + |
| 51 | + /// Get last pose pushed in the buffer. |
| 52 | + Pose last() const { return get(0); }; |
| 53 | + |
| 54 | + /// Get the N-th position from head (0 is the most recent). |
| 55 | + Pose get(std::size_t n) const; |
| 56 | + |
| 57 | +protected: |
| 58 | + pose_buffer_t* data_; ///< pointer to internal data structure |
| 59 | + bool external_data_; ///< Flag to indicate if memory is externally managed |
| 60 | +}; |
| 61 | + |
| 62 | +/// Overloads the stream insertion operator for `PoseBuffer`. |
| 63 | +/// Prints the PoseBuffer in a human-readable format. |
| 64 | +/// @param os The output stream. |
| 65 | +/// @param buffer The PoseBuffer to print. |
| 66 | +/// @return A reference to the output stream. |
| 67 | +inline std::ostream& operator<<(std::ostream& os, const PoseBuffer& buffer) { |
| 68 | + os << "PoseBuffer(size=" << buffer.size() << "/" << POSE_BUFFER_SIZE_MAX << ", head=" << buffer.head() << ", tail=" << buffer.tail() << ")"; |
| 69 | + return os; |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace models |
| 73 | + |
| 74 | +} // namespace cogip |
| 75 | + |
| 76 | +/// @} |
0 commit comments