@@ -52,11 +52,11 @@ std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>
5252 }
5353
5454 if (is_left_bigger) {
55- bigger_array = left_contours;
56- smaller_array = right_contours;
55+ bigger_array = backend::cameraPixelToGroundPos ( left_contours, camera_info) ;
56+ smaller_array = backend::cameraPixelToGroundPos ( right_contours, camera_info) ;
5757 } else {
58- bigger_array = right_contours;
59- smaller_array = left_contours;
58+ bigger_array = backend::cameraPixelToGroundPos ( right_contours, camera_info) ;
59+ smaller_array = backend::cameraPixelToGroundPos ( left_contours, camera_info) ;
6060 }
6161
6262 for (int i = 0 ; i < smaller_array.size (); i++) {
@@ -79,7 +79,7 @@ std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>
7979
8080 cam_path.push_back (cv::Point2d (x, y));
8181
82- bigger_array.erase (bigger_array.begin () + lucky_index);
82+ // bigger_array.erase(bigger_array.begin() + lucky_index);
8383 }
8484 }
8585
@@ -89,8 +89,21 @@ std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>
8989 // Convert from cv types to nav::msg
9090 // too few args
9191 // TODO use tf2 to fnd the hieght
92- auto ground_points = backend::cameraPixelToGroundPos (cam_path, camera_info, 0.6 , frame_id);
93- return ground_points;
92+ // auto ground_points = backend::cameraPixelToGroundPos(cam_path, camera_info, 0.6, frame_id);
93+ nav_msgs::msg::Path msg{};
94+ std::transform (cam_path.begin (), cam_path.end (), std::back_inserter (msg.poses ),
95+ [&frame_id](const cv::Point2d& point) {
96+ geometry_msgs::msg::PoseStamped pose{};
97+ // frame = "redto0 isn't sure if we use this";
98+ // redto0 is SURE that we use this update and fix ASAP
99+ pose.header .frame_id = frame_id; // literally is "notaemptystring"
100+ pose.pose .position .x = point.x ;
101+ pose.pose .position .y = point.y ;
102+ // pose.pose.position.z = point.z;
103+
104+ return pose;
105+ });
106+ return msg;
94107 }
95108}
96109
@@ -104,9 +117,49 @@ cv::Vec3f intersectPoint(cv::Vec3f rayVector, cv::Vec3f rayPoint, cv::Vec3f plan
104117
105118// TODO: make it not die when z is too small
106119// or make z not too small
107- nav_msgs::msg::Path backend::cameraPixelToGroundPos (std::vector<cv::Point2d>& path_points,
108- const image_geometry::PinholeCameraModel& camera_info,
109- float camera_height, std::string frame_id) {
120+
121+ std::vector<cv::Point2d> backend::cameraPixelToGroundPos (std::vector<cv::Point2d>& path_points,
122+ image_geometry::PinholeCameraModel& camera_info) {
123+ // Rotation that rotates left 90 and backwards 90.
124+ // This converts from camera coordinates in OpenCV to ROS coordinates
125+ tf2::Quaternion optical_to_ros{};
126+ optical_to_ros.setRPY (-M_PI / 2 , 0.0 , -M_PI / 2 );
127+ // -- CAMERA COORDINATES --
128+ // positive x = +X TO CAMERA
129+ // positive y = STRAIGHT TO GROUND
130+ // positive z = OUT OF CAMERA
131+ // hopefully
132+ std::vector<cv::Point2d> rwpoints;
133+
134+ auto ray_point = cv::Vec3f{0 , 0.527 , 0 };
135+
136+ for (cv::Point2d& pixel : path_points) {
137+ if (!camera_info.initialized ()) {
138+ continue ;
139+ }
140+
141+ // Project pixel to camera space
142+ auto ray = camera_info.projectPixelTo3dRay (pixel);
143+ auto ray_vect = cv::Vec3f (ray.x , ray.y , ray.z );
144+ // The oak-d uses shorts in mm, sim uses f32 in m
145+ auto point_3d = intersectPoint (ray_vect, ray_point, cv::Vec3f (0 , -1 , 0 ),
146+ cv::Vec3f (0 , 0 , 0 )); // returned form ray plane function
147+
148+ // Convert from camera space to ros coordinates ("World" but wrt camera mount)
149+ tf2::Vector3 tf_vec{point_3d[0 ], point_3d[1 ], point_3d[2 ]};
150+ tf2::Vector3 world_vec = tf2::quatRotate (optical_to_ros, tf_vec);
151+
152+ cv::Point2d dvector (-world_vec.x (), -world_vec.y ());
153+
154+ rwpoints.push_back (dvector);
155+ }
156+
157+ return rwpoints;
158+ }
159+
160+ nav_msgs::msg::Path backend::cameraPixelToGroundPath (std::vector<cv::Point2d>& path_points,
161+ const image_geometry::PinholeCameraModel& camera_info,
162+ float camera_height, std::string frame_id) {
110163 // Rotation that rotates left 90 and backwards 90.
111164 // This converts from camera coordinates in OpenCV to ROS coordinates
112165 tf2::Quaternion optical_to_ros{};
0 commit comments