RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
VectorProto.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.ejml.simple.SimpleMatrix;
8import org.wpilib.math.linalg.Vector;
9import org.wpilib.math.linalg.proto.detail.ProtobufVector;
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 final class VectorProto<R extends Num> implements Protobuf<Vector<R>, ProtobufVector> {
16 private final Nat<R> m_rows;
17
23 public VectorProto(Nat<R> rows) {
24 m_rows = rows;
25 }
26
27 @Override
28 public Class<Vector<R>> getTypeClass() {
29 @SuppressWarnings("unchecked")
30 var clazz = (Class<Vector<R>>) (Class<?>) Vector.class;
31 return clazz;
32 }
33
34 @Override
35 public Descriptor getDescriptor() {
37 }
38
39 @Override
43
44 @Override
45 public Vector<R> unpack(ProtobufVector msg) {
46 if (msg.getRows().length() != m_rows.getNum()) {
47 throw new IllegalArgumentException(
48 "Tried to unpack msg "
49 + msg
50 + " with "
51 + msg.getRows().length()
52 + " rows into Vector with "
53 + m_rows.getNum()
54 + " rows");
55 }
56 var storage = new SimpleMatrix(Protobuf.unpackArray(msg.getRows()));
57 return new Vector<R>(storage);
58 }
59
60 @Override
61 public void pack(ProtobufVector msg, Vector<R> value) {
62 Protobuf.packArray(msg.getMutableRows(), value.getData());
63 }
64}
static< T, MessageType extends ProtoMessage< MessageType > > T[] unpackArray(RepeatedMessage< MessageType > msg, Protobuf< T, MessageType > proto)