UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
xyzrgb.hpp
1
42#ifndef UFO_CLOUD_IO_XYZRGB_HPP
43#define UFO_CLOUD_IO_XYZRGB_HPP
44
45// UFO
46#include <ufo/cloud/point_cloud.hpp>
47#include <ufo/io/cloud_properties.hpp>
48#include <ufo/io/file_handler.hpp>
49#include <ufo/numeric/vec.hpp>
50#include <ufo/vision/color.hpp>
51
52// STL
53#include <cstdio>
54#include <filesystem>
55#include <print>
56
57namespace ufo
58{
59[[nodiscard]] CloudProperties cloudPropertiesXYZRGB(std::filesystem::path const& file);
60
61template <std::size_t Dim, class T, class... Ts>
62bool readXYZRGB(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 XYZRGB] Failed to open file: {}", file.string());
68 return false;
69 }
70
71 pc.clear();
72
73 for (char line[1024]; nullptr != std::fgets(line, sizeof line, fp.get());) {
74 Vec3d p;
75 FineRGB c;
76 if (6 == std::sscanf(line, "%lf %lf %lf %f %f %f", &p.x, &p.y, &p.z, &c.red, &c.green,
77 &c.blue)) {
78 if constexpr (std::disjunction_v<is_color<Ts>...>) {
79 pc.push_back(convert<Vec<Dim, T>>(p), convert<first_color_t<Ts...>>(c));
80 } else {
81 pc.push_back(convert<Vec<Dim, T>>(p));
82 }
83 }
84 }
85
86 return true;
87}
88
89template <std::size_t Dim, class T, class... Ts>
90bool writeXYZRGB(std::filesystem::path const& file, PointCloud<Dim, T, Ts...> const& pc)
91{
92 FileHandler fp(file.c_str(), "wb");
93
94 if (!fp) {
95 std::println(stderr, "[UFO | Write XYZRGB] Failed to create file: {}", file.string());
96 return false;
97 }
98
99 std::size_t const size = pc.size();
100 auto points = view<Vec<Dim, T>>(pc);
101 auto colors = view<first_color_t<Ts...>>(pc);
102 for (std::size_t i{}; size > i; ++i) {
103 auto p = convert<Vec<3, T>>(points[i]);
104 auto c = convert<FineRGB>(colors[i]);
105 if constexpr (std::is_same_v<T, float>) {
106 std::print(fp.get(), "{:.6} {:.6} {:.6}", p.x, p.y, p.z);
107 } else if constexpr (std::is_floating_point_v<T>) {
108 std::print(fp.get(), "{:.10} {:.10} {:.10}", p.x, p.y, p.z);
109 } else {
110 std::print(fp.get(), "{} {} {}", p.x, p.y, p.z);
111 }
112 if constexpr ((is_color_v<Ts> || ...)) {
113 std::println(fp.get(), " {:.6} {:.6} {:.6}", c.red, c.green, c.blue);
114 } else {
115 std::println(fp.get(), " 0 0 0");
116 }
117 }
118
119 return true;
120}
121} // namespace ufo
122
123#endif // UFO_CLOUD_IO_XYZRGB_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