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 org.photonvision.proto.Photon.ProtobufMultiTargetPNPResult;
023import org.photonvision.targeting.MultiTargetPNPResult;
024import org.photonvision.targeting.PnpResult;
025import us.hebi.quickbuf.Descriptors.Descriptor;
026import us.hebi.quickbuf.RepeatedInt;
027
028public class MultiTargetPNPResultProto
029        implements Protobuf<MultiTargetPNPResult, ProtobufMultiTargetPNPResult> {
030    @Override
031    public Class<MultiTargetPNPResult> getTypeClass() {
032        return MultiTargetPNPResult.class;
033    }
034
035    @Override
036    public Descriptor getDescriptor() {
037        return ProtobufMultiTargetPNPResult.getDescriptor();
038    }
039
040    @Override
041    public ProtobufMultiTargetPNPResult createMessage() {
042        return ProtobufMultiTargetPNPResult.newInstance();
043    }
044
045    @Override
046    public MultiTargetPNPResult unpack(ProtobufMultiTargetPNPResult msg) {
047        ArrayList<Short> fidIdsUsed = new ArrayList<>(msg.getFiducialIdsUsed().length());
048        for (var packedFidId : msg.getFiducialIdsUsed()) {
049            fidIdsUsed.add(packedFidId.shortValue());
050        }
051
052        return new MultiTargetPNPResult(PnpResult.proto.unpack(msg.getEstimatedPose()), fidIdsUsed);
053    }
054
055    @Override
056    public void pack(ProtobufMultiTargetPNPResult msg, MultiTargetPNPResult value) {
057        PnpResult.proto.pack(msg.getMutableEstimatedPose(), value.estimatedPose);
058
059        RepeatedInt idsUsed = msg.getMutableFiducialIdsUsed().reserve(value.fiducialIDsUsed.size());
060        for (int i = 0; i < value.fiducialIDsUsed.size(); i++) {
061            idsUsed.add(value.fiducialIDsUsed.get(i));
062        }
063    }
064}