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.utils; 019 020import edu.wpi.first.math.geometry.*; 021import org.photonvision.common.dataflow.structures.Packet; 022 023public class PacketUtils { 024 public static final int ROTATION2D_BYTE_SIZE = Double.BYTES; 025 public static final int QUATERNION_BYTE_SIZE = Double.BYTES * 4; 026 public static final int ROTATION3D_BYTE_SIZE = QUATERNION_BYTE_SIZE; 027 public static final int TRANSLATION2D_BYTE_SIZE = Double.BYTES * 2; 028 public static final int TRANSLATION3D_BYTE_SIZE = Double.BYTES * 3; 029 public static final int TRANSFORM2D_BYTE_SIZE = TRANSLATION2D_BYTE_SIZE + ROTATION2D_BYTE_SIZE; 030 public static final int TRANSFORM3D_BYTE_SIZE = TRANSLATION3D_BYTE_SIZE + ROTATION3D_BYTE_SIZE; 031 public static final int POSE2D_BYTE_SIZE = TRANSLATION2D_BYTE_SIZE + ROTATION2D_BYTE_SIZE; 032 public static final int POSE3D_BYTE_SIZE = TRANSLATION3D_BYTE_SIZE + ROTATION3D_BYTE_SIZE; 033 034 public static void packRotation2d(Packet packet, Rotation2d rotation) { 035 packet.encode(rotation.getRadians()); 036 } 037 038 public static Rotation2d unpackRotation2d(Packet packet) { 039 return new Rotation2d(packet.decodeDouble()); 040 } 041 042 public static void packQuaternion(Packet packet, Quaternion quaternion) { 043 packet.encode(quaternion.getW()); 044 packet.encode(quaternion.getX()); 045 packet.encode(quaternion.getY()); 046 packet.encode(quaternion.getZ()); 047 } 048 049 public static Quaternion unpackQuaternion(Packet packet) { 050 return new Quaternion( 051 packet.decodeDouble(), packet.decodeDouble(), packet.decodeDouble(), packet.decodeDouble()); 052 } 053 054 public static void packRotation3d(Packet packet, Rotation3d rotation) { 055 packQuaternion(packet, rotation.getQuaternion()); 056 } 057 058 public static Rotation3d unpackRotation3d(Packet packet) { 059 return new Rotation3d(unpackQuaternion(packet)); 060 } 061 062 public static void packTranslation2d(Packet packet, Translation2d translation) { 063 packet.encode(translation.getX()); 064 packet.encode(translation.getY()); 065 } 066 067 public static Translation2d unpackTranslation2d(Packet packet) { 068 return new Translation2d(packet.decodeDouble(), packet.decodeDouble()); 069 } 070 071 public static void packTranslation3d(Packet packet, Translation3d translation) { 072 packet.encode(translation.getX()); 073 packet.encode(translation.getY()); 074 packet.encode(translation.getZ()); 075 } 076 077 public static Translation3d unpackTranslation3d(Packet packet) { 078 return new Translation3d(packet.decodeDouble(), packet.decodeDouble(), packet.decodeDouble()); 079 } 080 081 public static void packTransform2d(Packet packet, Transform2d transform) { 082 packTranslation2d(packet, transform.getTranslation()); 083 packRotation2d(packet, transform.getRotation()); 084 } 085 086 public static Transform2d unpackTransform2d(Packet packet) { 087 return new Transform2d(unpackTranslation2d(packet), unpackRotation2d(packet)); 088 } 089 090 public static void packTransform3d(Packet packet, Transform3d transform) { 091 packTranslation3d(packet, transform.getTranslation()); 092 packRotation3d(packet, transform.getRotation()); 093 } 094 095 public static Transform3d unpackTransform3d(Packet packet) { 096 return new Transform3d(unpackTranslation3d(packet), unpackRotation3d(packet)); 097 } 098 099 public static void packPose2d(Packet packet, Pose2d pose) { 100 packTranslation2d(packet, pose.getTranslation()); 101 packRotation2d(packet, pose.getRotation()); 102 } 103 104 public static Pose2d unpackPose2d(Packet packet) { 105 return new Pose2d(unpackTranslation2d(packet), unpackRotation2d(packet)); 106 } 107 108 public static void packPose3d(Packet packet, Pose3d pose) { 109 packTranslation3d(packet, pose.getTranslation()); 110 packRotation3d(packet, pose.getRotation()); 111 } 112 113 public static Pose3d unpackPose3d(Packet packet) { 114 return new Pose3d(unpackTranslation3d(packet), unpackRotation3d(packet)); 115 } 116}