diff --git a/wpimath/src/main/native/include/frc/fmt/Eigen.h b/wpimath/src/main/native/include/frc/fmt/Eigen.h index c6b2ee6afe..0c915abb57 100644 --- a/wpimath/src/main/native/include/frc/fmt/Eigen.h +++ b/wpimath/src/main/native/include/frc/fmt/Eigen.h @@ -4,120 +4,45 @@ #pragma once +#include + #include #include "Eigen/Core" #include "Eigen/SparseCore" /** - * Formatter for Eigen::Matrix. - * - * @tparam Rows Number of rows. - * @tparam Cols Number of columns. - * @tparam Args Defaulted template arguments to Eigen::Matrix<>. + * Formatter for classes derived from Eigen::MatrixBase or + * Eigen::SparseCompressedBase. */ -template -struct fmt::formatter> { - /** - * Storage for format specifier. - */ - char presentation = 'f'; - - /** - * Format string parser. - * - * @param ctx Format string context. - */ +template +struct fmt::formatter< + Derived, CharT, + std::enable_if_t< + std::is_base_of_v, Derived> || + std::is_base_of_v, Derived>, + void>> { constexpr auto parse(fmt::format_parse_context& ctx) { - auto it = ctx.begin(), end = ctx.end(); - if (it != end && (*it == 'f' || *it == 'e')) { - presentation = *it++; - } - - if (it != end && *it != '}') { - throw fmt::format_error("invalid format"); - } - - return it; + return m_underlying.parse(ctx); } - /** - * Writes out a formatted matrix. - * - * @tparam FormatContext Format string context type. - * @param mat Matrix to format. - * @param ctx Format string context. - */ - template - auto format(const Eigen::Matrix& mat, - FormatContext& ctx) { + auto format(const Derived& mat, fmt::format_context& ctx) const { auto out = ctx.out(); - for (int i = 0; i < mat.rows(); ++i) { - for (int j = 0; j < mat.cols(); ++j) { - out = fmt::format_to(out, " {:f}", mat(i, j)); + + 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); } - if (i < mat.rows() - 1) { - out = fmt::format_to(out, "\n"); - } - } - - return out; - } -}; - -/** - * Formatter for Eigen::SparseMatrix. - * - * @tparam Options Union of bit flags controlling the storage scheme. - * @tparam StorageIndex The type of the indices. - */ -template -struct fmt::formatter> { - /** - * Storage for format specifier. - */ - char presentation = 'f'; - - /** - * Format string parser. - * - * @param ctx Format string context. - */ - constexpr auto parse(fmt::format_parse_context& ctx) { - auto it = ctx.begin(), end = ctx.end(); - if (it != end && (*it == 'f' || *it == 'e')) { - presentation = *it++; - } - - if (it != end && *it != '}') { - throw fmt::format_error("invalid format"); - } - - return it; - } - - /** - * Writes out a formatted matrix. - * - * @tparam FormatContext Format string context type. - * @param mat Matrix to format. - * @param ctx Format string context. - */ - template - auto format(const Eigen::SparseMatrix& mat, - FormatContext& ctx) { - auto out = ctx.out(); - for (int i = 0; i < mat.rows(); ++i) { - for (int j = 0; j < mat.cols(); ++j) { - out = fmt::format_to(out, " {:f}", mat.coeff(i, j)); - } - - if (i < mat.rows() - 1) { + if (row < mat.rows() - 1) { out = fmt::format_to(out, "\n"); } } return out; } + + private: + fmt::formatter m_underlying; }; diff --git a/wpimath/src/test/native/cpp/FormatterTest.cpp b/wpimath/src/test/native/cpp/FormatterTest.cpp index 0d8cbeff81..8961811130 100644 --- a/wpimath/src/test/native/cpp/FormatterTest.cpp +++ b/wpimath/src/test/native/cpp/FormatterTest.cpp @@ -16,14 +16,14 @@ TEST(FormatterTest, Eigen) { " 0.000000 1.000000\n" " 2.000000 3.000000\n" " 4.000000 5.000000", - fmt::format("{}", A)); + fmt::format("{:f}", A)); Eigen::MatrixXd B{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; EXPECT_EQ( " 0.000000 1.000000\n" " 2.000000 3.000000\n" " 4.000000 5.000000", - fmt::format("{}", B)); + fmt::format("{:f}", B)); Eigen::SparseMatrix C{3, 2}; std::vector> triplets; @@ -37,7 +37,7 @@ TEST(FormatterTest, Eigen) { " 0.000000 1.000000\n" " 2.000000 3.000000\n" " 4.000000 5.000000", - fmt::format("{}", C)); + fmt::format("{:f}", C)); } TEST(FormatterTest, Units) {