Skip to content

Commit

Permalink
[wpimath] Support formatting Eigen array types (#6496)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Apr 4, 2024
1 parent fbb3669 commit d26e6d9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions wpimath/src/main/native/include/frc/fmt/Eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include <fmt/format.h>

/**
* Formatter for classes derived from Eigen::MatrixBase<Derived> or
* Formatter for classes derived from Eigen::DenseBase<Derived> or
* Eigen::SparseCompressedBase<Derived>.
*/
template <typename Derived, typename CharT>
requires std::derived_from<Derived, Eigen::MatrixBase<Derived>> ||
requires std::derived_from<Derived, Eigen::DenseBase<Derived>> ||
std::derived_from<Derived, Eigen::SparseCompressedBase<Derived>>
struct fmt::formatter<Derived, CharT> {
constexpr auto parse(fmt::format_parse_context& ctx) {
Expand Down
9 changes: 6 additions & 3 deletions wpimath/src/test/native/cpp/FormatterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ TEST(FormatterTest, Eigen) {
" 4.000000 5.000000",
fmt::format("{:f}", B));

Eigen::SparseMatrix<double> C{3, 2};
Eigen::Array2d C{0.0, 1.0};
EXPECT_EQ(" 0.000000\n 1.000000", fmt::format("{:f}", C));

Eigen::SparseMatrix<double> D{3, 2};
std::vector<Eigen::Triplet<double>> triplets;
triplets.emplace_back(0, 1, 1.0);
triplets.emplace_back(1, 0, 2.0);
triplets.emplace_back(1, 1, 3.0);
triplets.emplace_back(2, 0, 4.0);
triplets.emplace_back(2, 1, 5.0);
C.setFromTriplets(triplets.begin(), triplets.end());
D.setFromTriplets(triplets.begin(), triplets.end());
EXPECT_EQ(
" 0.000000 1.000000\n"
" 2.000000 3.000000\n"
" 4.000000 5.000000",
fmt::format("{:f}", C));
fmt::format("{:f}", D));
}

TEST(FormatterTest, Units) {
Expand Down

0 comments on commit d26e6d9

Please sign in to comment.