PointCloudCrafter
Loading...
Searching...
No Matches
utils.hpp
1/*
2 * Copyright 2024 2024 Markus Pielmeier, Florian Sauerbeck,
3 * Dominik Kulmer, Maximilian Leitenstern
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include <Eigen/Eigen>
21#include <builtin_interfaces/msg/time.hpp>
22#include <fstream>
23#include <geometry_msgs/msg/transform_stamped.hpp>
24#include <iostream>
25#include <sensor_msgs/msg/point_cloud2.hpp>
26#include <sensor_msgs/point_cloud2_iterator.hpp>
27#include <string>
28#include <unordered_map>
29#include <vector>
30namespace pointcloudcrafter::tools::utils
31{
32constexpr std::uint64_t BILLION = 1000000000;
33constexpr float COLORS[] = {0.0, 1.0, 0.333, 0.666};
39uint64_t timestamp_from_ros(const builtin_interfaces::msg::Time & ros)
40{
41 return static_cast<std::uint64_t>(ros.sec) * BILLION + static_cast<std::uint64_t>(ros.nanosec);
42}
48builtin_interfaces::msg::Time timestamp_to_ros(std::uint64_t stamp)
49{
50 builtin_interfaces::msg::Time ros;
51 ros.sec = static_cast<std::int32_t>(stamp / BILLION);
52 ros.nanosec = static_cast<std::uint32_t>(stamp % BILLION);
53 return ros;
54}
60Eigen::Affine3d transform2eigen(const geometry_msgs::msg::TransformStamped & transform)
61{
62 Eigen::Affine3d affine = Eigen::Affine3d::Identity();
63 const auto & t = transform.transform;
64 affine.translate(Eigen::Vector3d{t.translation.x, t.translation.y, t.translation.z});
65 affine.rotate(Eigen::Quaterniond{t.rotation.w, t.rotation.x, t.rotation.y, t.rotation.z});
66 return affine;
67}
74void transform_pointcloud2(
75 const Eigen::Affine3f & transform, const sensor_msgs::msg::PointCloud2 & pc_in,
76 sensor_msgs::msg::PointCloud2 & pc_out)
77{
78 // Make a copy first so that the headers are staying the same
79 pc_out = pc_in;
80
81 sensor_msgs::PointCloud2ConstIterator<float> it_x_in(pc_in, "x");
82 sensor_msgs::PointCloud2ConstIterator<float> it_y_in(pc_in, "y");
83 sensor_msgs::PointCloud2ConstIterator<float> it_z_in(pc_in, "z");
84 sensor_msgs::PointCloud2Iterator<float> it_x_out(pc_out, "x");
85 sensor_msgs::PointCloud2Iterator<float> it_y_out(pc_out, "y");
86 sensor_msgs::PointCloud2Iterator<float> it_z_out(pc_out, "z");
87
88 for (; it_x_in != it_x_in.end();
89 ++it_x_in, ++it_y_in, ++it_z_in, ++it_x_out, ++it_y_out, ++it_z_out) {
90 Eigen::Vector4f point_in{*it_x_in, *it_y_in, *it_z_in, 1.0f};
91 Eigen::Vector4f point_out = transform * point_in;
92 *it_x_out = point_out.x();
93 *it_y_out = point_out.y();
94 *it_z_out = point_out.z();
95 }
96}
102std::unordered_map<std::string, Eigen::Affine3d> load_transforms_from_file(
103 const std::string & filename)
104{
105 std::unordered_map<std::string, Eigen::Affine3d> transforms;
106
107 std::ifstream file(filename);
108 if (!file.is_open()) {
109 throw std::runtime_error("Cannot open file: " + filename);
110 }
111
112 std::string frameId;
113 double x, y, z;
114 double r1, r2, r3, r4, r5, r6, r7, r8, r9;
115
116 std::string line;
117 while (std::getline(file, line)) {
118 if (line.empty() || line[0] == '#') {
119 continue;
120 }
121 std::istringstream iss(line);
122
123 if (!(iss >> frameId >> r1 >> r2 >> r3 >> x >> r4 >> r5 >> r6 >> y >> r7 >> r8 >> r9 >> z)) {
124 std::cerr << "Transform not in expected format" << std::endl;
125 }
126 Eigen::Matrix3d rotation;
127 rotation << r1, r2, r3, r4, r5, r6, r7, r8, r9;
128
129 Eigen::Affine3d trafo = Eigen::Affine3d::Identity();
130 trafo.linear() = rotation;
131 trafo.translation() << x, y, z;
132
133 transforms[frameId] = trafo;
134 }
135
136 return transforms;
137}
143std::vector<Eigen::Affine3d> load_poses_from_file(const std::string & filename)
144{
145 std::vector<Eigen::Affine3d> poses;
146
147 std::ifstream file(filename);
148 if (!file.is_open()) {
149 throw std::runtime_error("Cannot open file: " + filename);
150 }
151
152 double x, y, z;
153 double r1, r2, r3, r4, r5, r6, r7, r8, r9;
154
155 std::string line;
156 while (std::getline(file, line)) {
157 if (line.empty() || line[0] == '#') {
158 continue;
159 }
160 std::istringstream iss(line);
161
162 if (!(iss >> r1 >> r2 >> r3 >> x >> r4 >> r5 >> r6 >> y >> r7 >> r8 >> r9 >> z)) {
163 std::cerr << "Pose not in expected format" << std::endl;
164 }
165 Eigen::Matrix3d rotation;
166 rotation << r1, r2, r3, r4, r5, r6, r7, r8, r9;
167
168 Eigen::Affine3d trafo = Eigen::Affine3d::Identity();
169 trafo.linear() = rotation;
170 trafo.translation() << x, y, z;
171
172 poses.push_back(trafo);
173 }
174
175 return poses;
176}
177
178} // namespace pointcloudcrafter::tools::utils