42#ifndef UFO_CLOUD_IO_OBJ_HPP
43#define UFO_CLOUD_IO_OBJ_HPP
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#include <ufo/vision/color.hpp>
60[[nodiscard]] CloudProperties cloudPropertiesOBJ(std::filesystem::path
const& file);
62template <std::size_t Dim,
class T,
class... Ts>
63bool readOBJ(std::filesystem::path
const& file, PointCloud<Dim, T, Ts...>& pc)
65 FileHandler fp(file.c_str(),
"rb");
68 std::println(stderr,
"[UFO | Read OBJ] Failed to open file: {}", file.string());
72 std::size_t num_points{};
73 std::size_t num_normals{};
74 for (
char line[1024];
nullptr != std::fgets(line,
sizeof line, fp.get());) {
78 if (
auto num_fields = std::sscanf(line,
"v %lf %lf %lf %f %f %f", &p.x, &p.y, &p.z,
79 &c.red, &c.green, &c.blue);
83 if (4 == num_fields) {
85 p /=
static_cast<double>(c.red);
88 if constexpr ((is_color_v<Ts> || ...)) {
89 if (6 == num_fields) {
90 if (pc.size() < num_points) {
91 pc.push_back(
convert<Vec<Dim, T>>(p),
convert<first_color_t<Ts...>>(c));
93 view<Vec<Dim, T>>(pc)[num_points - 1] =
convert<Vec<Dim, T>>(p);
94 view<first_color_t<Ts...>>(pc)[num_points - 1] =
95 convert<first_color_t<Ts...>>(c);
100 if (3 == num_fields || 4 == num_fields) {
101 if (pc.size() < num_points) {
102 pc.push_back(
convert<Vec<Dim, T>>(p));
104 view<Vec<Dim, T>>(pc)[num_points - 1] =
convert<Vec<Dim, T>>(p);
107 std::println(stderr,
"[UFO | Read OBJ] Unknown format of line: {}", line);
110 }
else if constexpr ((is_normal_v<Ts> || ...)) {
111 if (
auto num_fields = std::sscanf(line,
"vn %f %f %f", &n.x, &n.y, &n.z);
114 if (pc.size() < num_normals) {
117 view<Normal>(pc)[num_normals - 1] = n;
126template <std::size_t Dim,
class T,
class... Ts>
127bool writeOBJ(std::filesystem::path
const& file, PointCloud<Dim, T, Ts...>
const& pc)
129 FileHandler fp(file.c_str(),
"wb");
132 std::println(stderr,
"[UFO | Write OBJ] Failed to create file: {}", file.string());
136 std::println(fp.get(),
"Created by UFO");
138 std::size_t size = pc.size();
139 auto points = view<Vec<Dim, T>>(pc);
140 for (std::size_t i{}; size > i; ++i) {
141 auto p = convert<Vec<3, T>>(points[i]);
142 if constexpr (std::is_same_v<T, float>) {
143 std::print(fp.get(),
"v {:.6} {:.6} {:.6}", p.x, p.y, p.z);
144 }
else if constexpr (std::is_floating_point_v<T>) {
145 std::print(fp.get(),
"v {:.10} {:.10} {:.10}", p.x, p.y, p.z);
147 std::print(fp.get(),
"v {} {} {}", p.x, p.y, p.z);
149 if constexpr ((is_color_v<Ts> || ...)) {
150 auto c = convert<FineRGB>(view<first_color_t<Ts...>>(pc)[i]);
151 std::println(fp.get(),
" {:.6} {:.6} {:.6}", c.red, c.green, c.blue);
153 std::print(fp.get(),
"\n");
155 if constexpr ((is_normal_v<Ts> || ...)) {
156 auto n = view<Normal>(pc)[i];
157 std::println(fp.get(),
"vn {:.6} {:.6} {:.6}", n.x, n.y, n.z);
All vision-related classes and functions.
constexpr To convert(Vec< Dim, U > const &v) noexcept
Converts a vector to a different Vec type, truncating or zero-padding dimensions.