WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
MatrixProto.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 <stdexcept>
8
9#include <fmt/format.h>
12
13#include "frc/EigenCore.h"
15
16template <int Rows, int Cols, int Options, int MaxRows, int MaxCols>
17 requires(Cols != 1)
24
25 static std::optional<frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols>>
27 constexpr bool isSmall = Rows * Cols * sizeof(double) < 256;
28 using UnpackType =
29 std::conditional_t<isSmall, wpi::UnpackCallback<double, Rows * Cols>,
31 UnpackType data;
32 data.Vec().reserve(Rows * Cols);
33 data.SetLimits(wpi::DecodeLimits::Fail);
35 msg.data = data.Callback();
36 if (!stream.Decode(msg)) {
37 return {};
38 }
39
40 if (msg.num_rows != Rows || msg.num_cols != Cols) {
41 return {};
42 }
43
44 auto items = data.Items();
45
46 if (items.size() != Rows * Cols) {
47 return {};
48 }
49
51 for (int i = 0; i < Rows * Cols; i++) {
52 mat(i) = items[i];
53 }
54
55 return mat;
56 }
57
58 static bool Pack(
59 OutputStream& stream,
61 std::span<const double> dataSpan{value.data(),
62 static_cast<size_t>(Rows * Cols)};
63 wpi::PackCallback<double> data{dataSpan};
65 .num_rows = Rows,
66 .num_cols = Cols,
67 .data = data.Callback(),
68 };
69 return stream.Encode(msg);
70 }
71};
A callback method that will pack elements when called.
Definition ProtobufCallbacks.h:486
Class for wrapping a nanopb istream.
Definition Protobuf.h:55
bool Decode(typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg, unsigned int flags=0)
Decodes a protobuf.
Definition Protobuf.h:104
Class for wrapping a nanopb ostream.
Definition Protobuf.h:119
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.h:211
A DirectUnpackCallback backed by a std::vector.
Definition ProtobufCallbacks.h:381
std::vector< T > & Vec() noexcept
Gets a reference to the backing vector.
Definition ProtobufCallbacks.h:410
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
Definition wpimath.npb.h:18
uint32_t num_rows
Definition wpimath.npb.h:23
pb_callback_t data
Definition wpimath.npb.h:25
uint32_t num_cols
Definition wpimath.npb.h:24
static std::optional< frc::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > > Unpack(InputStream &stream)
Definition MatrixProto.h:26
static bool Pack(OutputStream &stream, const frc::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > &value)
Definition MatrixProto.h:58
Protobuf serialization template.
Definition Protobuf.h:36
struct _wpi_proto_ProtobufMatrix wpi_proto_ProtobufMatrix