|
| 1 | +// Copyright (c) 2024 Ichiro ITS |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +#ifndef GYAKUENKI_CPP__IPM__GYAKUENKI_CPP_IPM_HPP_ |
| 22 | +#define GYAKUENKI_CPP__IPM__GYAKUENKI_CPP_IPM_HPP_ |
| 23 | + |
| 24 | +#include <tf2/LinearMath/Quaternion.h> |
| 25 | +#include <tf2/exceptions.h> |
| 26 | +#include <tf2_ros/buffer.h> |
| 27 | +#include <tf2_ros/transform_listener.h> |
| 28 | + |
| 29 | +#include <geometry_msgs/msg/quaternion.hpp> |
| 30 | +#include <memory> |
| 31 | +#include <opencv2/opencv.hpp> |
| 32 | +#include <rclcpp/rclcpp.hpp> |
| 33 | + |
| 34 | +#include "gyakuenki_cpp/utils/camera_info.hpp" |
| 35 | +#include "gyakuenki_interfaces/msg/projected_object.hpp" |
| 36 | +#include "keisan/matrix.hpp" |
| 37 | +#include "ninshiki_interfaces/msg/detected_object.hpp" |
| 38 | +#include "nlohmann/json.hpp" |
| 39 | + |
| 40 | +namespace gyakuenki_cpp |
| 41 | +{ |
| 42 | + |
| 43 | +class IPM |
| 44 | +{ |
| 45 | +public: |
| 46 | + struct CameraOffset |
| 47 | + { |
| 48 | + keisan::Point3 position; |
| 49 | + keisan::Angle<double> roll; |
| 50 | + keisan::Angle<double> pitch; |
| 51 | + keisan::Angle<double> yaw; |
| 52 | + }; |
| 53 | + |
| 54 | + using DetectedObject = ninshiki_interfaces::msg::DetectedObject; |
| 55 | + using Quaternion = geometry_msgs::msg::Quaternion; |
| 56 | + |
| 57 | + IPM( |
| 58 | + const std::shared_ptr<rclcpp::Node> & node, const std::shared_ptr<tf2_ros::Buffer> & tf_buffer, |
| 59 | + const std::shared_ptr<tf2_ros::TransformListener> & tf_listener, const std::string & path); |
| 60 | + |
| 61 | + std::string config_path; |
| 62 | + void load_config(const std::string & path); |
| 63 | + void set_config(double x, double y, double z, double roll, double pitch, double yaw); |
| 64 | + void save_config(); |
| 65 | + |
| 66 | + bool object_at_bottom_of_image(const DetectedObject & detected_object); |
| 67 | + void normalize_pixel(cv::Point2d & pixel); |
| 68 | + void undistort_pixel(cv::Point2d & pixel); |
| 69 | + |
| 70 | + Quaternion tf2_to_msg(const tf2::Quaternion & tf2_quat); |
| 71 | + tf2::Quaternion msg_to_tf2(const Quaternion & msg_quat); |
| 72 | + keisan::Matrix<4, 4> quat_to_rotation_matrix(const Quaternion & q); |
| 73 | + |
| 74 | + keisan::Matrix<4, 1> point_in_camera_frame( |
| 75 | + const cv::Point2d & pixel, const keisan::Matrix<4, 4> & T, const keisan::Matrix<4, 4> & R, |
| 76 | + const std::string & object_label); |
| 77 | + |
| 78 | + cv::Point2d get_target_pixel(const DetectedObject & detected_object); |
| 79 | + cv::Point2d get_normalized_target_pixel(const DetectedObject & detected_object); |
| 80 | + |
| 81 | + gyakuenki_interfaces::msg::ProjectedObject map_object( |
| 82 | + const DetectedObject & detected_object, const std::string & output_frame, |
| 83 | + keisan::Matrix<4, 1> & Pc); |
| 84 | + |
| 85 | + const CameraOffset & get_camera_offset() const { return camera_offset; } |
| 86 | + |
| 87 | +private: |
| 88 | + rclcpp::Node::SharedPtr node; |
| 89 | + std::shared_ptr<tf2_ros::Buffer> tf_buffer; |
| 90 | + std::shared_ptr<tf2_ros::TransformListener> tf_listener; |
| 91 | + |
| 92 | + utils::CameraInfo camera_info; |
| 93 | + CameraOffset camera_offset; |
| 94 | + tf2::Quaternion rotation_offset; |
| 95 | +}; |
| 96 | + |
| 97 | +} // namespace gyakuenki_cpp |
| 98 | + |
| 99 | +#endif // GYAKUENKI_CPP__IPM__GYAKUENKI_CPP_IPM_HPP_ |
0 commit comments