My Project 1.0
Holistic Fusion: A Generic Framework for Multimodal Sensor Fusion
Loading...
Searching...
No Matches
TransformsDictionary.h
Go to the documentation of this file.
1/*
2Copyright 2024 by Julian Nubert, Robotic Systems Lab, ETH Zurich.
3All rights reserved.
4This file is released under the "BSD-3-Clause License".
5Please see the LICENSE file that has been included as part of this package.
6 */
7
8#ifndef TRANSFORMS_DICTIONARY_H
9#define TRANSFORMS_DICTIONARY_H
10
11// C++
12#include <iostream>
13#include <map>
14
15// Workspace
17
18namespace holistic_fusion {
19
20template <class TRANSFORM_TYPE>
22 public:
23 // Constructor
24 TransformsDictionary(TRANSFORM_TYPE identityObject) : identity_(identityObject) {}
25
26 // Copy Constructor
32
33 // Getters ------------------------------------------------------------
34 // Check for specific transformation pair
35 bool isFramePairInDictionary(const std::string& frame1, const std::string& frame2) {
36 std::pair<std::string, std::string> framePair(frame1, frame2);
37 auto keyIterator = T_frame1_frame2_map_.find(framePair);
38 return static_cast<bool>(keyIterator != T_frame1_frame2_map_.end());
39 }
40
41 // Cleanup
42 virtual bool removeTransform(const std::string& frame1, const std::string& frame2, TRANSFORM_TYPE& removedTransform) {
43 std::pair<std::string, std::string> framePair(frame1, frame2);
44 auto framePairTransformMapIterator = T_frame1_frame2_map_.find(framePair);
45 // Erase forward and backward (to avoid memory leaks)
46 // Case 1: not present --> do nothing
47 if (framePairTransformMapIterator == T_frame1_frame2_map_.end()) {
48 return false;
49 }
50 // Case 2: present --> remove
51 else {
52 removedTransform = framePairTransformMapIterator->second;
53 T_frame1_frame2_map_.erase(framePairTransformMapIterator);
54 return true;
55 }
56 }
57
58 // Number of transformations
60
61 // Returns a left value of the requested transformation
62 TRANSFORM_TYPE& lv_T_frame1_frame2(const std::string& frame1, const std::string& frame2) {
63 if (frame1 == "") {
64 std::cout << YELLOW_START << "GMsf-TransformsDict" << COLOR_END << " No frame1 given." << std::endl;
65 throw std::runtime_error("No frame1 given.");
66 } else if (frame2 == "") {
67 std::cout << YELLOW_START << "GMsf-TransformsDict" << COLOR_END << " No frame2 given." << std::endl;
68 throw std::runtime_error("No frame2 given.");
69 } else {
70 std::pair<std::string, std::string> framePair(frame1, frame2);
71 return T_frame1_frame2_map_[framePair];
72 }
73 }
74
75 // Returns a right value to the requested transformation
76 const TRANSFORM_TYPE& rv_T_frame1_frame2(const std::string& frame1, const std::string& frame2) const {
77 // Check whether it is identity
78 if (frame1 == frame2) {
79 return identity_;
80 }
81 // Normal Operation
82 std::pair<std::string, std::string> framePair(frame1, frame2);
83 auto keyIterator = T_frame1_frame2_map_.find(framePair);
84 if (keyIterator == T_frame1_frame2_map_.end()) {
85 std::cout << YELLOW_START << "GMsf-TransformsDict" << COLOR_END << " No transform found between " << frame1 << " and " << frame2
86 << "." << std::endl;
87 throw std::runtime_error("No transform found for " + frame1 + " and " + frame2 + ".");
88 }
89 return keyIterator->second;
90 }
91
92 // Return reference to the map
93 std::map<std::pair<std::string, std::string>, TRANSFORM_TYPE>& getTransformsMap() { return T_frame1_frame2_map_; }
94 const std::map<std::pair<std::string, std::string>, TRANSFORM_TYPE>& getTransformsMap() const { return T_frame1_frame2_map_; }
95
96 // Setters ------------------------------------------------------------
97 // With inverse
98 void set_T_frame1_frame2_andInverse(const std::string& frame1, const std::string& frame2, const TRANSFORM_TYPE& T_frame1_frame2) {
99 // Check whether transformation pair is already there
100 if (!isFramePairInDictionary(frame1, frame2)) {
102 } else {
103 std::cout << YELLOW_START << "GMsf-TransformsDict" << COLOR_END << " Transformation pair " << frame1 << " and " << frame2
104 << " already exists." << std::endl;
105 }
106 // Set transformation
107 lv_T_frame1_frame2(frame1, frame2) = T_frame1_frame2;
108 lv_T_frame1_frame2(frame2, frame1) = rv_T_frame1_frame2(frame1, frame2).inverse();
109 }
110
111 // Without inverse
112 void set_T_frame1_frame2(const std::string& frame1, const std::string& frame2, const TRANSFORM_TYPE T_frame1_frame2) {
113 // Check whether transformation pair is already there
114 if (!isFramePairInDictionary(frame1, frame2)) {
116 }
117 // Set transformation
118 lv_T_frame1_frame2(frame1, frame2) = T_frame1_frame2;
119 }
120
121 private:
122 // General container class
123 std::map<std::pair<std::string, std::string>, TRANSFORM_TYPE> T_frame1_frame2_map_;
124
125 // Identity transformation
126 TRANSFORM_TYPE identity_;
127
128 // Number of stored transformations
129 unsigned long numStoredTransforms_ = 0;
130};
131
132} // namespace holistic_fusion
133
134#endif // TRANSFORMS_DICTIONARY_H
#define COLOR_END
#define YELLOW_START
TRANSFORM_TYPE & lv_T_frame1_frame2(const std::string &frame1, const std::string &frame2)
std::map< std::pair< std::string, std::string >, TRANSFORM_TYPE > T_frame1_frame2_map_
TransformsDictionary(const TransformsDictionary &other)
void set_T_frame1_frame2(const std::string &frame1, const std::string &frame2, const TRANSFORM_TYPE T_frame1_frame2)
bool isFramePairInDictionary(const std::string &frame1, const std::string &frame2)
std::map< std::pair< std::string, std::string >, TRANSFORM_TYPE > & getTransformsMap()
TransformsDictionary(TRANSFORM_TYPE identityObject)
const std::map< std::pair< std::string, std::string >, TRANSFORM_TYPE > & getTransformsMap() const
void set_T_frame1_frame2_andInverse(const std::string &frame1, const std::string &frame2, const TRANSFORM_TYPE &T_frame1_frame2)
virtual bool removeTransform(const std::string &frame1, const std::string &frame2, TRANSFORM_TYPE &removedTransform)
const TRANSFORM_TYPE & rv_T_frame1_frame2(const std::string &frame1, const std::string &frame2) const