Dandelion 1.1.2
A light-weight 3D builder for educational usage
载入中...
搜索中...
未找到
formatter.hpp
浏览该文件的文档.
1#ifndef DANDELION_UTILS_FORMATTER_HPP
2#define DANDELION_UTILS_FORMATTER_HPP
3
4#include <Eigen/Core>
5#include <fmt/core.h>
6
7/*!
8 * \file utils/formatter.hpp
9 * \ingroup utils
10 * \~chinese
11 * \brief 提供对 Eigen 列向量和方阵类型的格式化支持。
12 */
13
14/*!
15 * \~chinese
16 * \brief 提供对 Eigen 列向量类型的格式化支持。
17 *
18 * 这个格式化器调用 `Scalar` 类型的格式化器来格式化每个分量,
19 * 并在各分量之间添加逗号、空格,在开头和结尾添加括号。由于它直接调用 `Scalar`
20 * 类型的格式化器格式化分量,任何可用于 `Scalar` 类型的格式符也都可用于格式化列向量。
21 * 相应地,`Scalar` 类型必须可以被格式化。
22 *
23 * 格式符:与分量类型的格式符相同,例如 `Vector3f` 可以使用所有 `float`
24 * 类型的格式符,`Vector2i` 可以使用所有 `int` 类型的格式符。
25 *
26 * \tparam Scalar 向量中分量的类型
27 * \tparam n_dim 向量维数
28 */
29template<typename Scalar, int n_dim>
30struct fmt::formatter<Eigen::Matrix<Scalar, n_dim, 1>> : formatter<Scalar>
31{
32 static_assert(n_dim > 0, "Dimension of a vector must be positive");
33
34 fmt::appender format(const Eigen::Matrix<Scalar, n_dim, 1>& v, format_context& ctx) const
35 {
36 fmt::appender iter = fmt::format_to(ctx.out(), "(");
37 iter = formatter<Scalar>::format(v(0), ctx);
38 for (int i = 1; i < n_dim; ++i) {
39 iter = fmt::format_to(iter, ", ");
40 iter = formatter<Scalar>::format(v(i), ctx);
41 }
42 iter = fmt::format_to(iter, ")");
43 return iter;
44 }
45};
46
47/*!
48 * \~chinese
49 * \brief 提供对 Eigen 方阵类型的格式化支持。
50 *
51 * 这个格式化器调用 `Scalar` 类型的格式化器来格式化每个元素,
52 * 并在同一行的各元素间添加空格,在行间添加换行符。由于它直接调用 `Scalar`
53 * 类型的格式化器来格式化元素,任何可用于 `Scalar` 类型的格式符也都可用于格式化方阵。
54 * 相应地,`Scalar` 类型必须可以被格式化。
55 *
56 * 格式符:与元素类型的格式符相同,例如 `Matrix4f` 可以使用所有 `float`
57 * 类型的格式符,`Matrix3d` 可以使用所有 `double` 类型的格式符。
58 *
59 * 请注意,方阵的最后一行不会添加换行符,如果使用 `fmt::print` / `std::printf` /
60 * `std::cout` 等工具直接输出时,请自行添加换行。使用 spdlog 的日志输出则不必。
61 *
62 * 当矩阵中的元素位数不一时(例如 10 和 0),默认的格式符 `{}` 并不会对齐它们。
63 * 如果使用者希望将数字右对齐,请直接使用 fmtlib 的右对齐格式,例如:
64 * ```cpp
65 * Eigen::Matrix4f m = 10.0f * Eigen::Matrix4f::Identity();
66 * fmt::print("matrix: {:>5.1f}", m);
67 * ```
68 * 会输出
69 * ```
70 * matrix:
71 * 10.0 0.0 0.0 0.0
72 * 0.0 10.0 0.0 0.0
73 * 0.0 0.0 10.0 0.0
74 * 0.0 0.0 0.0 10.0
75 * ```
76 *
77 * \tparam Scalar 方阵中的元素类型
78 * \tparam n_dim 方阵的维数
79 */
80template<typename Scalar, int n_dim>
81struct fmt::formatter<Eigen::Matrix<Scalar, n_dim, n_dim, 0, n_dim, n_dim>> : formatter<Scalar>
82{
83 static_assert(n_dim > 0, "Dimension of a square matrix must be positive");
84
85 fmt::appender
86 format(const Eigen::Matrix<Scalar, n_dim, n_dim, 0, n_dim, n_dim>& m, format_context& ctx) const
87 {
88 fmt::appender iter = fmt::format_to(ctx.out(), "\n");
89 for (int row = 0; row < n_dim - 1; ++row) {
90 for (int column = 0; column < n_dim; ++column) {
91 iter = formatter<Scalar>::format(m(row, column), ctx);
92 iter = fmt::format_to(iter, " ");
93 }
94 iter = fmt::format_to(iter, "\n");
95 }
96 for (int column = 0; column < n_dim; ++column) {
97 iter = formatter<Scalar>::format(m(n_dim - 1, column), ctx);
98 iter = fmt::format_to(iter, " ");
99 }
100 return iter;
101 }
102};
103
104#endif // DANDELION_UTILS_FORMATTER_HPP