WPILibC++ 2025.0.0-alpha-1-24-g6478ba6
Eigen.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <concepts>
8
9#include <Eigen/Core>
10#include <Eigen/SparseCore>
11#include <fmt/format.h>
12
13/**
14 * Formatter for classes derived from Eigen::DenseBase<Derived> or
15 * Eigen::SparseCompressedBase<Derived>.
16 */
17template <typename Derived, typename CharT>
18 requires std::derived_from<Derived, Eigen::DenseBase<Derived>> ||
19 std::derived_from<Derived, Eigen::SparseCompressedBase<Derived>>
20struct fmt::formatter<Derived, CharT> {
21 template <typename ParseContext>
22 constexpr auto parse(ParseContext& ctx) {
23 return m_underlying.parse(ctx);
24 }
25
26 template <typename FmtContext>
27 auto format(const Derived& mat, FmtContext& ctx) const {
28 auto out = ctx.out();
29
30 for (int row = 0; row < mat.rows(); ++row) {
31 for (int col = 0; col < mat.cols(); ++col) {
32 out = fmt::format_to(out, " ");
33 out = m_underlying.format(mat.coeff(row, col), ctx);
34 }
35
36 if (row < mat.rows() - 1) {
37 out = fmt::format_to(out, "\n");
38 }
39 }
40
41 return out;
42 }
43
44 private:
45 fmt::formatter<typename Derived::Scalar, CharT> m_underlying;
46};
auto format(const Derived &mat, FmtContext &ctx) const
Definition: Eigen.h:27
constexpr auto parse(ParseContext &ctx)
Definition: Eigen.h:22
auto format_to(OutputIt out, wformat_string< T... > fmt, T &&... args) -> OutputIt
Definition: xchar.h:142