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