UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
xyzn.hpp
1
42#ifndef UFO_CLOUD_IO_XYZN_HPP
43#define UFO_CLOUD_IO_XYZN_HPP
44
45// UFO
46#include <ufo/cloud/point_cloud.hpp>
47#include <ufo/core/normal.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 cloudPropertiesXYZN(std::filesystem::path const& file);
60
61template <std::size_t Dim, class T, class... Ts>
62bool readXYZN(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 XYZN] 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 Normal n;
76 if (6 ==
77 std::sscanf(line, "%lf %lf %lf %f %f %f", &p.x, &p.y, &p.z, &n.x, &n.y, &n.z)) {
78 if constexpr (std::disjunction_v<is_normal<Ts>...>) {
79 pc.push_back(convert<Vec<Dim, T>>(p), n);
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 writeXYZN(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 XYZN] 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 normals = view<Normal>(pc);
102 for (std::size_t i{}; size > i; ++i) {
103 auto p = convert<Vec<3, T>>(points[i]);
104 auto n = normals[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_normal_v<Ts> || ...)) {
113 std::println(fp.get(), " {:.6} {:.6} {:.6}", n.x, n.y, n.z);
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_XYZN_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