ReUseX  0.0.1
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
fmt_formatter.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#pragma once
6#include <Eigen/Core>
7#include <Eigen/Geometry>
8#include <Eigen/src/Core/Matrix.h>
9#include <fmt/chrono.h>
10#include <fmt/color.h>
11#include <fmt/core.h>
12#include <fmt/format.h>
13#include <fmt/ostream.h>
14#include <fmt/printf.h>
15#include <fmt/ranges.h>
16#include <fmt/std.h>
17#include <opencv2/core.hpp>
18
19// template<>
20// struct fmt::formatter<Eigen::Matrix4f>{
21// // Define the parse function (optional for custom types)
22// constexpr auto parse(fmt::format_parse_context& ctx) ->
23// decltype(ctx.begin()) {
24// // No custom parsing, so just return the end of the context
25// return ctx.end();
26// };
27
28// // Define the format function
29// template <typename FormatContext>
30// auto format(const Eigen::Matrix4f& mat, FormatContext& ctx) ->
31// decltype(ctx.out()) {
32// // Convert the Eigen matrix to a string representation
33// std::string result;
34// for (int i = 0; i < mat.rows(); ++i) {
35// for (int j = 0; j < mat.cols(); ++j) {
36// result += fmt::format("{:12.6f} ", mat(i, j)); // Adjust
37// formatting as needed
38// }
39// result += '\n'; // Add a newline after each row
40// }
41// return fmt::format_to(ctx.out(), "{}", result);
42// }
43// };
44//
45//
46const Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "",
47 "\n[", "]");
48
49// Parese Eigen matrix with included formatter
50template <typename T>
51struct fmt::formatter<Eigen::WithFormat<T>> : fmt::ostream_formatter {};
52
53template <typename T>
54struct fmt::formatter<cv::Size_<T>> : fmt::ostream_formatter {};
55
56template <typename Scalar>
57struct fmt::formatter<Eigen::Quaternion<Scalar>>
58 : fmt::formatter<fmt::string_view> {
59 auto format(const Eigen::Quaternion<Scalar> &value, format_context &ctx) const
60 -> format_context::iterator {
61 return fmt::format_to(ctx.out(), "Ouat[{:.3f}X {:.3f}Y {:.3f}Z {:.3f}W]",
62 value.x(), value.y(), value.z(), value.w());
63 }
64};
65
66template <>
67struct fmt::formatter<Eigen::Matrix<float, 4, 1>>
68 : fmt::formatter<fmt::string_view> {
69
70 auto format(const Eigen::Matrix<float, 4, 1> &value,
71 format_context &ctx) const -> format_context::iterator {
72 return fmt::format_to(ctx.out(), "Vec[{:.3f},{:.3f},{:.3f},{:.3f}]",
73 value(0, 0), value(1, 0), value(2, 0), value(3, 0));
74 }
75};
76
77// template <typename Scalar> struct fmt::formatter<Eigen::MatrixX<Scalar>> {
78//
79// // The parse function can be used to parse any format options (unused here)
80// constexpr auto parse(fmt::format_parse_context &ctx) {
81// // Parsing is not needed for simple vector printing, so we return the end
82// // iterator
83// return ctx.end();
84// }
85//
86// // The format function is responsible for formatting the vector
87// template <typename FormatContext>
88// auto format(const Eigen::MatrixX<Scalar> &quat, FormatContext &ctx) {
89// return "Matrix";
90// }
91// };
92
93template <typename T>
94struct fmt::formatter<
95 T, std::enable_if_t<std::is_base_of_v<Eigen::DenseBase<T>, T>, char>>
96 : ostream_formatter {};
const Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "", "\n[", "]")
auto format(const Eigen::Matrix< float, 4, 1 > &value, format_context &ctx) const -> format_context::iterator
auto format(const Eigen::Quaternion< Scalar > &value, format_context &ctx) const -> format_context::iterator