001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.math.trajectory.proto;
006
007import edu.wpi.first.math.proto.Trajectory.ProtobufTrajectory;
008import edu.wpi.first.math.proto.Trajectory.ProtobufTrajectoryState;
009import edu.wpi.first.math.trajectory.Trajectory;
010import edu.wpi.first.util.protobuf.Protobuf;
011import java.util.ArrayList;
012import us.hebi.quickbuf.Descriptors.Descriptor;
013
014public class TrajectoryProto implements Protobuf<Trajectory, ProtobufTrajectory> {
015  @Override
016  public Class<Trajectory> getTypeClass() {
017    return Trajectory.class;
018  }
019
020  @Override
021  public Descriptor getDescriptor() {
022    return ProtobufTrajectory.getDescriptor();
023  }
024
025  @Override
026  public Protobuf<?, ?>[] getNested() {
027    return new Protobuf<?, ?>[] {Trajectory.State.proto};
028  }
029
030  @Override
031  public ProtobufTrajectory createMessage() {
032    return ProtobufTrajectory.newInstance();
033  }
034
035  @Override
036  public Trajectory unpack(ProtobufTrajectory msg) {
037    ArrayList<Trajectory.State> states = new ArrayList<>(msg.getStates().length());
038    for (ProtobufTrajectoryState protoState : msg.getStates()) {
039      states.add(Trajectory.State.proto.unpack(protoState));
040    }
041
042    return new Trajectory(states);
043  }
044
045  @Override
046  public void pack(ProtobufTrajectory msg, Trajectory value) {
047    var states = msg.getMutableStates().reserve(value.getStates().size());
048    for (var item : value.getStates()) {
049      Trajectory.State.proto.pack(states.next(), item);
050    }
051  }
052}