blob: ee621dd8afccd99e729a6ddb7946d391c2c4a110 [file] [log] [blame]
Jim Ostrowskicb8b4082024-01-21 02:23:46 -08001#ifndef FRC971_VISION_VISION_UTIL_LIB_H_
2#define FRC971_VISION_VISION_UTIL_LIB_H_
3#include <optional>
4#include <string_view>
5
Jim Ostrowski6d242d52024-04-03 20:39:03 -07006#include "Eigen/Dense"
7#include "Eigen/Geometry"
8#include "Eigen/SVD"
Jim Ostrowskicb8b4082024-01-21 02:23:46 -08009#include "opencv2/imgproc.hpp"
10
11#include "frc971/vision/calibration_generated.h"
Jim Ostrowski6d242d52024-04-03 20:39:03 -070012#include "frc971/vision/ceres/types.h"
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080013
Jim Ostrowskib974cca2024-01-28 15:07:50 -080014// Extract the CameraExtrinsics from a CameraCalibration struct
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080015namespace frc971::vision {
16std::optional<cv::Mat> CameraExtrinsics(
17 const frc971::vision::calibration::CameraCalibration *camera_calibration);
18
Jim Ostrowskib974cca2024-01-28 15:07:50 -080019// Extract the CameraIntrinsics from a CameraCalibration struct
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080020cv::Mat CameraIntrinsics(
21 const frc971::vision::calibration::CameraCalibration *camera_calibration);
22
Jim Ostrowskib974cca2024-01-28 15:07:50 -080023// Extract the CameraDistCoeffs from a CameraCalibration struct
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080024cv::Mat CameraDistCoeffs(
25 const frc971::vision::calibration::CameraCalibration *camera_calibration);
26
Jim Ostrowskib974cca2024-01-28 15:07:50 -080027// Get the camera number from a camera channel name, e.g., return 2 from
28// "/camera2". Returns nullopt if string doesn't start with "/camera" or does
29// not have a number
30std::optional<uint16_t> CameraNumberFromChannel(std::string camera_channel);
31
Jim Ostrowski33208982024-03-02 15:01:45 -080032// Return a calibration filename to save to based on the given data
33std::string CalibrationFilename(std::string calibration_folder,
34 std::string node_name, int team_number,
35 int camera_number, std::string camera_id,
36 std::string timestamp);
37
Jim Ostrowski6d242d52024-04-03 20:39:03 -070038// Utility functions for dealing with ceres::examples::Pose3d structs
39class PoseUtils {
40 public:
41 // Embeds a 3d pose into an affine transformation
42 static Eigen::Affine3d Pose3dToAffine3d(
43 const ceres::examples::Pose3d &pose3d);
44 // Inverse of above function
45 static ceres::examples::Pose3d Affine3dToPose3d(const Eigen::Affine3d &H);
46
47 // Computes pose_2 relative to pose_1. This is equivalent to (pose_1^-1 *
48 // pose_2)
49 static ceres::examples::Pose3d ComputeRelativePose(
50 const ceres::examples::Pose3d &pose_1,
51 const ceres::examples::Pose3d &pose_2);
52
53 // Computes pose_2 given a pose_1 and pose_2 relative to pose_1. This is
54 // equivalent to (pose_1 * pose_2_relative)
55 static ceres::examples::Pose3d ComputeOffsetPose(
56 const ceres::examples::Pose3d &pose_1,
57 const ceres::examples::Pose3d &pose_2_relative);
58
59 // Converts a rotation with roll, pitch, and yaw into a quaternion
60 static Eigen::Quaterniond EulerAnglesToQuaternion(const Eigen::Vector3d &rpy);
61 // Inverse of above function
62 static Eigen::Vector3d QuaternionToEulerAngles(const Eigen::Quaterniond &q);
63 // Converts a 3d rotation matrix into a rotation with roll, pitch, and yaw
64 static Eigen::Vector3d RotationMatrixToEulerAngles(const Eigen::Matrix3d &R);
65};
66
67// Compute the average of a set of quaternions
68Eigen::Vector4d QuaternionAverage(std::vector<Eigen::Vector4d> quaternions);
69
70// Compute the average pose given a list of translations (as
71// Eigen::Vector3d's) and rotations (as Eigen::Vector4d quaternions)
72// Returned as an Eigen::Affine3d pose
73// Also, compute the variance of each of these list of vectors
74Eigen::Affine3d ComputeAveragePose(
75 std::vector<Eigen::Vector3d> &translation_list,
76 std::vector<Eigen::Vector4d> &rotation_list,
77 Eigen::Vector3d *translation_variance = nullptr,
78 Eigen::Vector3d *rotation_variance = nullptr);
79
80// Helper function to compute average pose when supplied with list
81// of Eigen::Affine3d's
82Eigen::Affine3d ComputeAveragePose(
83 std::vector<Eigen::Affine3d> &pose_list,
84 Eigen::Vector3d *translation_variance = nullptr,
85 Eigen::Vector3d *rotation_variance = nullptr);
86
Jim Ostrowskicb8b4082024-01-21 02:23:46 -080087} // namespace frc971::vision
88
89#endif // FRC971_VISION_VISION_UTIL_LIB_H_