WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
MatrixProto.hpp
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>
10
15
16template <int Rows, int Cols, int Options, int MaxRows, int MaxCols>
17 requires(Cols != 1)
25
26 static std::optional<
29 constexpr bool isSmall = Rows * Cols * sizeof(double) < 256;
30 using UnpackType = std::conditional_t<
33 UnpackType data;
34 data.Vec().reserve(Rows * Cols);
35 data.SetLimits(wpi::util::DecodeLimits::Fail);
37 msg.data = data.Callback();
38 if (!stream.Decode(msg)) {
39 return {};
40 }
41
42 if (msg.num_rows != Rows || msg.num_cols != Cols) {
43 return {};
44 }
45
46 auto items = data.Items();
47
48 if (items.size() != Rows * Cols) {
49 return {};
50 }
51
53 for (int i = 0; i < Rows * Cols; i++) {
54 mat(i) = items[i];
55 }
56
57 return mat;
58 }
59
60 static bool Pack(
61 OutputStream& stream,
63 std::span<const double> dataSpan{value.data(),
64 static_cast<size_t>(Rows * Cols)};
67 .num_rows = Rows,
68 .num_cols = Cols,
69 .data = data.Callback(),
70 };
71 return stream.Encode(msg);
72 }
73};
A callback method that will pack elements when called.
Definition ProtobufCallbacks.hpp:487
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.hpp:519
Class for wrapping a nanopb istream.
Definition Protobuf.hpp:55
bool Decode(typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg, unsigned int flags=0)
Decodes a protobuf.
Definition Protobuf.hpp:104
Class for wrapping a nanopb ostream.
Definition Protobuf.hpp:119
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.hpp:211
A DirectUnpackCallback backed by a std::vector.
Definition ProtobufCallbacks.hpp:382
std::vector< T > & Vec() noexcept
Gets a reference to the backing vector.
Definition ProtobufCallbacks.hpp:411
A DirectUnpackCallback backed by a SmallVector<T, N>.
Definition ProtobufCallbacks.hpp:335
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.hpp:21
@ Fail
Definition ProtobufCallbacks.hpp:30
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
wpi_proto_ProtobufMatrix MessageStruct
Definition MatrixProto.hpp:20
wpi::util::ProtoInputStream< wpi::math::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > > InputStream
Definition MatrixProto.hpp:21
wpi::util::ProtoOutputStream< wpi::math::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > > OutputStream
Definition MatrixProto.hpp:23
static bool Pack(OutputStream &stream, const wpi::math::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > &value)
Definition MatrixProto.hpp:60
static std::optional< wpi::math::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > > Unpack(InputStream &stream)
Definition MatrixProto.hpp:28
Protobuf serialization template.
Definition Protobuf.hpp:36
struct _wpi_proto_ProtobufMatrix wpi_proto_ProtobufMatrix