UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
xyzi.hpp
1
42#ifndef UFO_IO_XYZI_HPP
43#define UFO_IO_XYZI_HPP
44
45// UFO
46#include <ufo/cloud/point_cloud.hpp>
47#include <ufo/core/intensity.hpp>
48#include <ufo/io/cloud_properties.hpp>
49#include <ufo/io/file_handler.hpp>
50#include <ufo/numeric/vec.hpp>
51
52// STL
53#include <cstdio>
54#include <filesystem>
55#include <print>
56
57namespace ufo
58{
59[[nodiscard]] CloudProperties cloudPropertiesXYZI(std::filesystem::path const& file);
60
61template <std::size_t Dim, class T, class... Ts>
62bool readXYZI(std::filesystem::path const& file, PointCloud<Dim, T, Ts...>& pc)
63{
64 FileHandler fp(file.c_str(), "rb");
65
66 if (!fp) {
67 std::println(stderr, "[UFO | Read XYZI] Failed to open file: {}", file.string());
68 return false;
69 }
70
71 pc.clear();
72
73 for (char* line = fp.readline(); nullptr != line; line = fp.readline()) {
74 Vec3d p;
75 double i;
76 if (4 == std::sscanf(line, "%lf %lf %lf %lf", &p.x, &p.y, &p.z, &i)) {
77 if constexpr ((is_intensity_v<Ts> || ...)) {
78 pc.push_back(convert<Vec<Dim, T>>(p), Intensity(i));
79 } else {
80 pc.push_back(convert<Vec<Dim, T>>(p));
81 }
82 }
83 }
84
85 return true;
86}
87
88template <std::size_t Dim, class T, class... Ts>
89bool writeXYZI(std::filesystem::path const& file, PointCloud<Dim, T, Ts...> const& pc)
90{
91 FileHandler fp(file.c_str(), "wb");
92
93 if (!fp) {
94 std::println(stderr, "[UFO | Write XYZI] Failed to create file: {}", file.string());
95 return false;
96 }
97
98 std::size_t const size = pc.size();
99 auto points = view<Vec<Dim, T>>(pc);
100 auto intensities = view<Intensity>(pc);
101 for (std::size_t i{}; size > i; ++i) {
102 auto p = convert<Vec<3, T>>(points[i]);
103 auto intensity = intensities[i];
104 if constexpr (std::is_same_v<T, float>) {
105 std::print(fp.get(), "{:.6} {:.6} {:.6}", p.x, p.y, p.z);
106 } else if constexpr (std::is_floating_point_v<T>) {
107 std::print(fp.get(), "{:.10} {:.10} {:.10}", p.x, p.y, p.z);
108 } else {
109 std::print(fp.get(), "{} {} {}", p.x, p.y, p.z);
110 }
111 if constexpr ((is_intensity_v<Ts> || ...)) {
112 std::println(fp.get(), " {:.6}", intensity.intensity);
113 } else {
114 std::println(fp.get(), " 0");
115 }
116 }
117
118 return true;
119}
120} // namespace ufo
121
122#endif // UFO_IO_XYZI_HPP
All vision-related classes and functions.
Definition cloud.hpp:49
constexpr To convert(Vec< Dim, U > const &v) noexcept
Converts a vector to a different Vec type, truncating or zero-padding dimensions.
Definition vec.hpp:1059