WPILibC++ 2023.4.3-108-ge5452e3
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 <fmt/format.h>
10
11#include "Eigen/Core"
12#include "Eigen/SparseCore"
13
14/**
15 * Formatter for classes derived from Eigen::MatrixBase<Derived> or
16 * Eigen::SparseCompressedBase<Derived>.
17 */
18template <typename Derived, typename CharT>
19 requires std::derived_from<Derived, Eigen::MatrixBase<Derived>> ||
20 std::derived_from<Derived, Eigen::SparseCompressedBase<Derived>>
21struct fmt::formatter<Derived, CharT> {
22 constexpr auto parse(fmt::format_parse_context& ctx) {
23 return m_underlying.parse(ctx);
24 }
25
26 auto format(const Derived& mat, fmt::format_context& ctx) const {
27 auto out = ctx.out();
28
29 for (int row = 0; row < mat.rows(); ++row) {
30 for (int col = 0; col < mat.cols(); ++col) {
31 out = fmt::format_to(out, " ");
32 out = m_underlying.format(mat.coeff(row, col), ctx);
33 }
34
35 if (row < mat.rows() - 1) {
36 out = fmt::format_to(out, "\n");
37 }
38 }
39
40 return out;
41 }
42
43 private:
44 fmt::formatter<typename Derived::Scalar, CharT> m_underlying;
45};
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ColXpr col(Index i)
This is the const version of col().
Definition: BlockMethods.h:1097
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE RowXpr row(Index i)
This is the const version of row(). *‍/.
Definition: BlockMethods.h:1118
buffer_context< char > format_context
Definition: core.h:1851
basic_format_parse_context< char > format_parse_context
Definition: core.h:724
auto format(const Derived &mat, fmt::format_context &ctx) const
Definition: Eigen.h:26
constexpr auto parse(fmt::format_parse_context &ctx)
Definition: Eigen.h:22
auto format_to(OutputIt out, const S &fmt, Args &&... args) -> OutputIt
Definition: xchar.h:136