UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
xyz.hpp
1
42#ifndef UFO_IO_XYZ_HPP
43#define UFO_IO_XYZ_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
51// STL
52#include <cstdio>
53#include <filesystem>
54#include <print>
55
56namespace ufo
57{
58[[nodiscard]] CloudProperties cloudPropertiesXYZ(std::filesystem::path const& file);
59
60template <std::size_t Dim, class T, class... Ts>
61bool readXYZ(std::filesystem::path const& file, PointCloud<Dim, T, Ts...>& pc)
62{
63 FileHandler fp(file.c_str(), "rb");
64
65 if (!fp) {
66 std::println(stderr, "[UFO | Read XYZ] Failed to open file: {}", file.string());
67 return false;
68 }
69
70 pc.clear();
71
72 for (char* line = fp.readline(); nullptr != line; line = fp.readline()) {
73 Vec3d p;
74 if (3 == std::sscanf(line, "%lf %lf %lf", &p.x, &p.y, &p.z)) {
75 pc.push_back(convert<Vec<Dim, T>>(p));
76 }
77 }
78
79 return true;
80}
81
82template <std::size_t Dim, class T, class... Ts>
83bool writeXYZ(std::filesystem::path const& file, PointCloud<Dim, T, Ts...> const& pc)
84{
85 FileHandler fp(file.c_str(), "wb");
86
87 if (!fp) {
88 std::println(stderr, "[UFO | Write XYZ] Failed to create file: {}", file.string());
89 return false;
90 }
91
92 for (auto points = view<Vec<Dim, T>>(pc); auto const& point : points) {
93 auto p = convert<Vec<3, T>>(point);
94 if constexpr (std::is_same_v<T, float>) {
95 std::println(fp.get(), "{:.6} {:.6} {:.6}", p.x, p.y, p.z);
96 } else if constexpr (std::is_floating_point_v<T>) {
97 std::println(fp.get(), "{:.10} {:.10} {:.10}", p.x, p.y, p.z);
98 } else {
99 std::println(fp.get(), "{} {} {}", p.x, p.y, p.z);
100 }
101 }
102
103 return true;
104}
105} // namespace ufo
106
107#endif // UFO_IO_XYZ_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