AWViz-ROS C++ API Reference
Loading...
Searching...
No Matches
tf_tree.hpp
Go to the documentation of this file.
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 <cstring>
20#include <memory>
21#include <optional>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
26namespace awviz_common
27{
28
29constexpr const char * TF_ROOT = "map";
30
35{
36public:
43 TfFrame(const std::string & id, const std::string & parent) : id_(id), parent_(parent) {}
44
50 explicit TfFrame(const std::string & id) : id_(id), parent_("") {}
51
57 const std::string & id() const { return id_; }
58
64 const std::string & parent() const { return parent_; }
65
71 bool is_root() const { return parent_.empty(); }
72
79 bool is_static() const { return std::strcmp(parent_.c_str(), TF_ROOT) != 0 || parent_.empty(); }
80
87 bool operator==(const TfFrame & other) const
88 {
89 return id_ == other.id() && parent_ == other.parent();
90 }
91
92private:
93 std::string id_;
94 std::string parent_;
95};
96
97class TfTree
98{
99public:
103 TfTree() : frames_() { frames_.emplace(TF_ROOT, TF_ROOT); }
104
110 void emplace(const TfFrame & frame) { frames_.emplace(frame.id(), frame); }
111
112 // /**
113 // * @brief Add a new tf frame to the tree with the empty string parent.
114 // *
115 // * @param id Frame ID. If it has been already registered, skip adding.
116 // */
117 // void emplace(const std::string & id) { frames_.emplace(id, id); }
118
124 const std::unordered_map<std::string, TfFrame> & get_frames() const { return frames_; }
125
132 std::optional<TfFrame> get_frame(const std::string & id) const
133 {
134 return contains(id) ? std::make_optional(frames_.at(id)) : std::nullopt;
135 }
136
143 std::optional<TfFrame> get_parent(const std::string & id) const
144 {
145 auto frame = get_frame(id);
146 return frame ? get_frame(frame->parent()) : std::nullopt;
147 }
148
155 bool contains(const std::string & id) const { return frames_.count(id) > 0; }
156
164 bool contains(const TfFrame & frame) const
165 {
166 return contains(frame.id()) && frame == frames_.at(frame.id());
167 }
168
177 std::string entity_path(const TfFrame & frame) const
178 {
179 auto current = std::make_optional<TfFrame>(frame);
180 std::string entity = "/" + current->id();
181 while (current && !current->is_root()) {
182 current = get_frame(current->parent());
183 if (current) {
184 entity = "/" + current->id() + entity;
185 } else {
186 break;
187 }
188 }
189 return entity;
190 }
191
199 bool can_link_to(const TfFrame & frame, const std::string & id) const
200 {
201 auto current = std::make_optional<TfFrame>(frame);
202 while (current && !current->is_root()) {
203 current = get_frame(current->parent());
204 }
205 return current->id() == id;
206 }
207
208private:
209 std::unordered_map<std::string, TfFrame> frames_;
210};
211} // namespace awviz_common
212
213#endif // AWVIZ_COMMON__TF_TREE_HPP_
A class to represent a TF frame information.
Definition: tf_tree.hpp:35
bool is_root() const
Indicate whether the frame is root by checking if parent_ is empty.
Definition: tf_tree.hpp:71
const std::string & parent() const
Return the parent frame ID.
Definition: tf_tree.hpp:64
bool operator==(const TfFrame &other) const
Compare with an another object.
Definition: tf_tree.hpp:87
bool is_static() const
Return whether the tf frame is static or not.
Definition: tf_tree.hpp:79
TfFrame(const std::string &id, const std::string &parent)
Construct a new object.
Definition: tf_tree.hpp:43
TfFrame(const std::string &id)
Construct a new object with empty string for parent.
Definition: tf_tree.hpp:50
const std::string & id() const
Return own frame ID.
Definition: tf_tree.hpp:57
Definition: tf_tree.hpp:98
TfTree()
Construct a object setting TF_ROOT as root.
Definition: tf_tree.hpp:103
void emplace(const TfFrame &frame)
Add a new tf frame to the tree.
Definition: tf_tree.hpp:110
std::optional< TfFrame > get_parent(const std::string &id) const
Get the parent TfFrame object.
Definition: tf_tree.hpp:143
bool contains(const std::string &id) const
Whether to the specified frame ID is contained as a node of tree.
Definition: tf_tree.hpp:155
const std::unordered_map< std::string, TfFrame > & get_frames() const
Return map of all frames.
Definition: tf_tree.hpp:124
bool can_link_to(const TfFrame &frame, const std::string &id) const
Check whether the input frame is linked to the input id.
Definition: tf_tree.hpp:199
std::string entity_path(const TfFrame &frame) const
Return entity path of the specified frame. The entity path will be in the format "/<Parent0>/<Parent1...
Definition: tf_tree.hpp:177
std::optional< TfFrame > get_frame(const std::string &id) const
Get the TfFrame object.
Definition: tf_tree.hpp:132
bool contains(const TfFrame &frame) const
Whether to the specified frame is contained in the tree checking both key and value.
Definition: tf_tree.hpp:164
Definition: display.hpp:30
constexpr const char * TF_ROOT
Root transformation frame.
Definition: tf_tree.hpp:29