RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
MatrixProto.java
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
5package org.wpilib.math.linalg.proto;
6
7import org.wpilib.math.linalg.MatBuilder;
8import org.wpilib.math.linalg.Matrix;
9import org.wpilib.math.linalg.proto.detail.ProtobufMatrix;
10import org.wpilib.math.util.Nat;
11import org.wpilib.math.util.Num;
12import org.wpilib.util.protobuf.Protobuf;
13import us.hebi.quickbuf.Descriptors.Descriptor;
14
15public class MatrixProto<R extends Num, C extends Num>
16 implements Protobuf<Matrix<R, C>, ProtobufMatrix> {
17 private final Nat<R> m_rows;
18 private final Nat<C> m_cols;
19
26 public MatrixProto(Nat<R> rows, Nat<C> cols) {
27 m_rows = rows;
28 m_cols = cols;
29 }
30
31 @Override
32 public Class<Matrix<R, C>> getTypeClass() {
33 @SuppressWarnings("unchecked")
34 var clazz = (Class<Matrix<R, C>>) (Class<?>) Matrix.class;
35 return clazz;
36 }
37
38 @Override
39 public Descriptor getDescriptor() {
41 }
42
43 @Override
47
48 @Override
49 public Matrix<R, C> unpack(ProtobufMatrix msg) {
50 if (msg.getNumRows() != m_rows.getNum() || msg.getNumCols() != m_cols.getNum()) {
51 throw new IllegalArgumentException(
52 "Tried to unpack msg "
53 + msg
54 + " with "
55 + msg.getNumRows()
56 + " rows and "
57 + msg.getNumCols()
58 + " columns into Matrix with "
59 + m_rows.getNum()
60 + " rows and "
61 + m_cols.getNum()
62 + " columns");
63 }
64 return MatBuilder.fill(m_rows, m_cols, Protobuf.unpackArray(msg.getData()));
65 }
66
67 @Override
68 public void pack(ProtobufMatrix msg, Matrix<R, C> value) {
69 msg.setNumRows(value.getNumRows());
70 msg.setNumCols(value.getNumCols());
71 Protobuf.packArray(msg.getMutableData(), value.getData());
72 }
73}
static< R extends Num, C extends Num > Matrix< R, C > fill(Nat< R > rows, Nat< C > cols, double... data)
MatrixProto(Nat< R > rows, Nat< C > cols)
Class< Matrix< R, C > > getTypeClass()
static< T, MessageType extends ProtoMessage< MessageType > > T[] unpackArray(RepeatedMessage< MessageType > msg, Protobuf< T, MessageType > proto)