001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.targeting.proto;
019
020import edu.wpi.first.util.protobuf.Protobuf;
021import java.util.ArrayList;
022import java.util.List;
023import org.photonvision.proto.Photon.ProtobufTargetCorner;
024import org.photonvision.targeting.TargetCorner;
025import us.hebi.quickbuf.Descriptors.Descriptor;
026import us.hebi.quickbuf.RepeatedMessage;
027
028public class TargetCornerProto implements Protobuf<TargetCorner, ProtobufTargetCorner> {
029    @Override
030    public Class<TargetCorner> getTypeClass() {
031        return TargetCorner.class;
032    }
033
034    @Override
035    public Descriptor getDescriptor() {
036        return ProtobufTargetCorner.getDescriptor();
037    }
038
039    @Override
040    public ProtobufTargetCorner createMessage() {
041        return ProtobufTargetCorner.newInstance();
042    }
043
044    @Override
045    public TargetCorner unpack(ProtobufTargetCorner msg) {
046        return new TargetCorner(msg.getX(), msg.getY());
047    }
048
049    public List<TargetCorner> unpack(RepeatedMessage<ProtobufTargetCorner> msg) {
050        ArrayList<TargetCorner> corners = new ArrayList<>(msg.length());
051        for (ProtobufTargetCorner corner : msg) {
052            corners.add(unpack(corner));
053        }
054        return corners;
055    }
056
057    @Override
058    public void pack(ProtobufTargetCorner msg, TargetCorner value) {
059        msg.setX(value.x).setY(value.y);
060    }
061
062    public void pack(RepeatedMessage<ProtobufTargetCorner> msg, List<TargetCorner> value) {
063        var corners = msg.reserve(value.size());
064        for (TargetCorner targetCorner : value) {
065            var corner = corners.next();
066            pack(corner, targetCorner);
067        }
068    }
069}