UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
pcd.cpp
1// UFO
2#include <ufo/io/file_handler.hpp>
3#include <ufo/io/pcd.hpp>
4
5// STL
6#include <filesystem>
7#include <format>
8#include <stdexcept>
9
10namespace ufo
11{
12namespace detail
13{
14void pcdSplit(std::vector<std::string>& result, std::string const& in,
15 char const* const delimiters)
16{
17 // Taken from:
18 // https://github.com/PointCloudLibrary/pcl/blob/e5ce18fab8d4ff175eb44cc09ae24ae1a0a2e8eb/io/include/pcl/io/split.h#L25
19 auto const len = in.length();
20 std::size_t token_start = 0;
21
22 result.clear();
23 while (token_start < len) {
24 // eat leading whitespace
25 token_start = in.find_first_not_of(delimiters, token_start);
26 if (token_start == std::string::npos) {
27 return; // nothing left but white space
28 }
29
30 // find the end of the token
31 auto const token_end = in.find_first_of(delimiters, token_start);
32
33 // push token
34 if (token_end == std::string::npos) {
35 result.emplace_back(in.data() + token_start, len - token_start);
36 return;
37 } else {
38 result.emplace_back(in.data() + token_start, token_end - token_start);
39 }
40
41 // set up for next loop
42 token_start = token_end + 1;
43 }
44}
45} // namespace detail
46
47CloudProperties cloudPropertiesPCD(std::filesystem::path const& file)
48{
49 FileHandler fp(file.c_str(), "rb");
50
51 if (!fp) {
52 throw std::runtime_error(std::format(
53 "[UFO | Cloud Properties PCD] Failed to open file: {}", file.string()));
54 }
55
56 CloudProperties prop;
57
58 std::vector<std::string> st;
59 for (char* buf = fp.readline(); nullptr != buf; buf = fp.readline()) {
60 std::string line(buf);
61
62 if (line.empty()) {
63 continue;
64 }
65
66 detail::pcdSplit(st, line, "\t\r ");
67
68 std::stringstream sstream(line);
69 sstream.imbue(std::locale::classic());
70
71 std::string line_type;
72 sstream >> line_type;
73
74 if ("#" == line_type.substr(0, 1)) {
75 continue;
76 }
77
78 if (("FIELDS" == line_type.substr(0, 6)) || ("COLUMNS" == line_type.substr(0, 7))) {
79 int specified_channel_count = static_cast<int>(st.size() - 1);
80
81 for (int i = 0; i < specified_channel_count; ++i) {
82 if ("rgb" == st.at(i + 1)) {
83 prop.color = true;
84 } else if ("rgba" == st.at(i + 1)) {
85 prop.color = true;
86 prop.alpha = true;
87 } else if ("intensity" == st.at(i + 1)) {
88 prop.intensity = true;
89 } else if ("normal_x" == st.at(i + 1) || "normal_y" == st.at(i + 1) ||
90 "normal_z" == st.at(i + 1)) {
91 prop.normal = true;
92 }
93 }
94 } else if ("DATA" == line_type.substr(0, 4)) {
95 break;
96 }
97 }
98
99 return prop;
100}
101} // namespace ufo
All vision-related classes and functions.
Definition cloud.hpp:49