2021-06-05 21:10:41 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
2023-06-07 09:50:09 -07:00
|
|
|
#include <wpi/concepts.h>
|
2021-06-05 21:10:41 -07:00
|
|
|
|
|
|
|
|
#include "Eigen/Core"
|
2022-09-02 01:00:05 -07:00
|
|
|
#include "Eigen/SparseCore"
|
2021-06-05 21:10:41 -07:00
|
|
|
|
2021-10-14 18:09:38 -07:00
|
|
|
/**
|
2023-05-30 23:35:15 -07:00
|
|
|
* Formatter for classes derived from Eigen::MatrixBase<Derived> or
|
|
|
|
|
* Eigen::SparseCompressedBase<Derived>.
|
2021-10-14 18:09:38 -07:00
|
|
|
*/
|
2023-05-30 23:35:15 -07:00
|
|
|
template <typename Derived, typename CharT>
|
2023-06-07 09:50:09 -07:00
|
|
|
requires std::derived_from<Derived, Eigen::MatrixBase<Derived>> ||
|
|
|
|
|
std::derived_from<Derived, Eigen::SparseCompressedBase<Derived>>
|
|
|
|
|
struct fmt::formatter<Derived, CharT> {
|
2021-06-05 21:10:41 -07:00
|
|
|
constexpr auto parse(fmt::format_parse_context& ctx) {
|
2023-05-30 23:35:15 -07:00
|
|
|
return m_underlying.parse(ctx);
|
2021-06-05 21:10:41 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 23:35:15 -07:00
|
|
|
auto format(const Derived& mat, fmt::format_context& ctx) const {
|
2021-06-05 21:10:41 -07:00
|
|
|
auto out = ctx.out();
|
|
|
|
|
|
2023-05-30 23:35:15 -07:00
|
|
|
for (int row = 0; row < mat.rows(); ++row) {
|
|
|
|
|
for (int col = 0; col < mat.cols(); ++col) {
|
|
|
|
|
out = fmt::format_to(out, " ");
|
|
|
|
|
out = m_underlying.format(mat.coeff(row, col), ctx);
|
2022-09-02 01:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 23:35:15 -07:00
|
|
|
if (row < mat.rows() - 1) {
|
2021-06-05 21:10:41 -07:00
|
|
|
out = fmt::format_to(out, "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
2023-05-30 23:35:15 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
fmt::formatter<typename Derived::Scalar, CharT> m_underlying;
|
2021-06-05 21:10:41 -07:00
|
|
|
};
|