WPILibC++ 2024.3.2
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::MatrixBase<Derived> or
15 * Eigen::SparseCompressedBase<Derived>.
16 */
17template <typename Derived, typename CharT>
18 requires std::derived_from<Derived, Eigen::MatrixBase<Derived>> ||
19 std::derived_from<Derived, Eigen::SparseCompressedBase<Derived>>
20struct fmt::formatter<Derived, CharT> {
21 constexpr auto parse(fmt::format_parse_context& ctx) {
22 return m_underlying.parse(ctx);
23 }
24
25 auto format(const Derived& mat, fmt::format_context& ctx) const {
26 auto out = ctx.out();
27
28 for (int row = 0; row < mat.rows(); ++row) {
29 for (int col = 0; col < mat.cols(); ++col) {
30 out = fmt::format_to(out, " ");
31 out = m_underlying.format(mat.coeff(row, col), ctx);
32 }
33
34 if (row < mat.rows() - 1) {
35 out = fmt::format_to(out, "\n");
36 }
37 }
38
39 return out;
40 }
41
42 private:
43 fmt::formatter<typename Derived::Scalar, CharT> m_underlying;
44};
buffer_context< char > format_context
Definition: core.h:1759
basic_format_parse_context< char > format_parse_context
Definition: core.h:722
auto format(const Derived &mat, fmt::format_context &ctx) const
Definition: Eigen.h:25
constexpr auto parse(fmt::format_parse_context &ctx)
Definition: Eigen.h:21
auto format_to(OutputIt out, const S &fmt, T &&... args) -> OutputIt
Definition: xchar.h:156