WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
MatrixStruct.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 <fmt/format.h>
8#include <wpi/ct_string.h>
9#include <wpi/struct/Struct.h>
10
11#include "frc/EigenCore.h"
12
13template <int Rows, int Cols, int Options, int MaxRows, int MaxCols>
14 requires(Cols != 1)
16 static constexpr ct_string kTypeName =
17 wpi::Concat("Matrix__"_ct_string, wpi::NumToCtString<Rows>(),
18 "_"_ct_string, wpi::NumToCtString<Cols>());
19 static constexpr std::string_view GetTypeName() { return kTypeName; }
20 static constexpr size_t GetSize() { return Rows * Cols * 8; }
21 static constexpr ct_string kSchema =
22 wpi::Concat("double data["_ct_string, wpi::NumToCtString<Rows * Cols>(),
23 "]"_ct_string);
24 static constexpr std::string_view GetSchema() { return kSchema; }
25
27 std::span<const uint8_t> data) {
28 constexpr size_t kDataOff = 0;
32 for (int i = 0; i < Rows * Cols; i++) {
33 mat(i) = mat_data[i];
34 }
35 return mat;
36 }
37
38 static void Pack(
39 std::span<uint8_t> data,
41 constexpr size_t kDataOff = 0;
43 for (int i = 0; i < Rows * Cols; i++) {
44 mat_data[i] = value(i);
45 }
47 }
48};
49
This class is a wrapper around std::array that does compile time size checking.
Definition array.h:26
Specifies that a type is capable of raw struct serialization and deserialization.
Definition Struct.h:69
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
constexpr auto NumToCtString()
Converts any integral to a ct_string at compile-time.
Definition ct_string.h:202
constexpr auto Concat(ct_string< Char, Traits, N1 > const &s1, ct_string< Char, Traits, N > const &... s)
Concatenates multiple fixed_strings into a larger fixed_string at compile time.
Definition ct_string.h:168
void PackStructArray(std::span< uint8_t > data, const wpi::array< T, N > &arr)
Pack a serialized struct array starting at a given offset within the data.
Definition Struct.h:234
constexpr empty_array_t empty_array
Definition array.h:16
wpi::array< T, N > UnpackStructArray(std::span< const uint8_t > data)
Unpack a serialized struct array starting at a given offset within the data.
Definition Struct.h:172
static constexpr std::string_view GetSchema()
Definition MatrixStruct.h:24
static frc::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > Unpack(std::span< const uint8_t > data)
Definition MatrixStruct.h:26
static constexpr std::string_view GetTypeName()
Definition MatrixStruct.h:19
static constexpr size_t GetSize()
Definition MatrixStruct.h:20
static void Pack(std::span< uint8_t > data, const frc::Matrixd< Rows, Cols, Options, MaxRows, MaxCols > &value)
Definition MatrixStruct.h:38
Struct serialization template.
Definition Struct.h:39
Fixed length string (array of character) for compile time use.
Definition ct_string.h:29