|
| 1 | +// Copyright 2024 Kotaro Uetake. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef AWVIZ_COMMON__TF_TREE_HPP_ |
| 16 | +#define AWVIZ_COMMON__TF_TREE_HPP_ |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <memory> |
| 20 | +#include <optional> |
| 21 | +#include <string> |
| 22 | +#include <unordered_map> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace awviz_common |
| 26 | +{ |
| 27 | +/** |
| 28 | + * @brief A class to represent a TF frame information. |
| 29 | + */ |
| 30 | +class TfFrame |
| 31 | +{ |
| 32 | +public: |
| 33 | + /** |
| 34 | + * @brief Construct a new object. |
| 35 | + * |
| 36 | + * @param id Frame ID. |
| 37 | + * @param parent Parent frame ID. |
| 38 | + */ |
| 39 | + TfFrame(const std::string & id, const std::string & parent) : id_(id), parent_(parent) {} |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief Construct a new object with empty string for parent. |
| 43 | + * |
| 44 | + * @param id Frame ID. |
| 45 | + */ |
| 46 | + explicit TfFrame(const std::string & id) : id_(id), parent_("") {} |
| 47 | + |
| 48 | + /** |
| 49 | + * @brief Return own frame ID. |
| 50 | + * |
| 51 | + * @return Own frame ID. |
| 52 | + */ |
| 53 | + const std::string & id() const { return id_; } |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief Return the parent frame ID. |
| 57 | + * |
| 58 | + * @return Parent frame ID. |
| 59 | + */ |
| 60 | + const std::string & parent() const { return parent_; } |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief Indicate whether the frame is root by checking if `parent_` is empty. |
| 64 | + * |
| 65 | + * @return Return true, if the `parent_` is empty. |
| 66 | + */ |
| 67 | + bool is_root() const { return parent_.empty(); } |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief Return whether the tf frame is static or not. |
| 71 | + * @note Currently, this returns true if the parent id is not `"map"`. |
| 72 | + * |
| 73 | + * @return Return true if the parent id is not `"map"`. |
| 74 | + */ |
| 75 | + bool is_static() const { return parent_ != "map"; } |
| 76 | + |
| 77 | +private: |
| 78 | + std::string id_; //!< Frame ID. |
| 79 | + std::string parent_; //!< Parent frame ID. |
| 80 | +}; |
| 81 | + |
| 82 | +class TfTree |
| 83 | +{ |
| 84 | +public: |
| 85 | + /** |
| 86 | + * @brief Add a new tf frame to the tree. |
| 87 | + * |
| 88 | + * @param frame A new tf frame. If it has been already registered, skip adding. |
| 89 | + */ |
| 90 | + void emplace(const TfFrame & frame) |
| 91 | + { |
| 92 | + if (!contains(frame.id())) { |
| 93 | + frames_.emplace(frame.id(), frame); |
| 94 | + } |
| 95 | + |
| 96 | + if (!frame.is_root()) { |
| 97 | + emplace(frame.parent()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @brief Add a new tf frame to the tree with the empty string parent. |
| 103 | + * |
| 104 | + * @param id Frame ID. If it has been already registered, skip adding. |
| 105 | + */ |
| 106 | + void emplace(const std::string & id) |
| 107 | + { |
| 108 | + if (!contains(id)) { |
| 109 | + frames_.emplace(id, id); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Return map of all frames. |
| 115 | + * |
| 116 | + * @return Shared pointer of all frames. |
| 117 | + */ |
| 118 | + const std::unordered_map<std::string, TfFrame> & get_frames() const { return frames_; } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief Get the `TfFrame` object. |
| 122 | + * |
| 123 | + * @param id Frame ID. |
| 124 | + * @return Corresponding `TfFrame` object. If there is no target `TfFrame`, returns `nullptr`. |
| 125 | + */ |
| 126 | + std::optional<TfFrame> get_frame(const std::string & id) const |
| 127 | + { |
| 128 | + return contains(id) ? std::make_optional(frames_.at(id)) : std::nullopt; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @brief Get the parent `TfFrame` object. |
| 133 | + * |
| 134 | + * @param id Frame ID. |
| 135 | + * @return Parent `TfFrame` object. If there is no parent, returns `nullptr`. |
| 136 | + */ |
| 137 | + std::optional<TfFrame> get_parent(const std::string & id) const |
| 138 | + { |
| 139 | + auto frame = get_frame(id); |
| 140 | + return frame ? get_frame(frame->parent()) : std::nullopt; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @brief Whether to the specified frame is contained in the tree. |
| 145 | + * |
| 146 | + * @param id Frame ID. |
| 147 | + * @return Returns true, if the frame is contained. |
| 148 | + */ |
| 149 | + bool contains(const std::string & id) const { return frames_.count(id) > 0; } |
| 150 | + |
| 151 | + /** |
| 152 | + * @brief Whether to the parent of specified frame is contained in the tree. |
| 153 | + * |
| 154 | + * @param id Frame ID. |
| 155 | + * @return Returns true, if the parent frame is contained. |
| 156 | + */ |
| 157 | + bool is_root(const std::string & id) const { return contains(id) && frames_.at(id).is_root(); } |
| 158 | + |
| 159 | +private: |
| 160 | + std::unordered_map<std::string, TfFrame> frames_; //!< Map to store frames. |
| 161 | +}; |
| 162 | +} // namespace awviz_common |
| 163 | + |
| 164 | +#endif // AWVIZ_COMMON__TF_TREE_HPP_ |
0 commit comments