|
4 | 4 | #include <geometry_msgs/msg/pose_array.hpp> |
5 | 5 | #include <geometry_msgs/msg/pose_stamped.hpp> |
6 | 6 | #include <geometry_msgs/msg/pose_with_covariance_stamped.hpp> |
| 7 | +#include <vortex_msgs/msg/landmark_array.hpp> |
7 | 8 |
|
8 | 9 | #include <tf2/exceptions.h> |
9 | 10 | #include <tf2/time.h> |
@@ -103,6 +104,49 @@ inline void transform_pose(tf2_ros::Buffer& tf_buffer, |
103 | 104 | } |
104 | 105 | } |
105 | 106 |
|
| 107 | +/** |
| 108 | + * @brief Transform all poses in a LandmarkArray into a target frame using TF. |
| 109 | + * |
| 110 | + * Each pose in the input LandmarkArray is individually transformed using the |
| 111 | + * array header (frame ID and timestamp). The output LandmarkArray contains |
| 112 | + * only successfully transformed poses and is expressed in the target frame. |
| 113 | + * |
| 114 | + * TF does not natively support PoseArray, so each pose is internally |
| 115 | + * wrapped as a PoseStamped for transformation. |
| 116 | + * |
| 117 | + * @param tf_buffer TF buffer used for transform lookup |
| 118 | + * @param in Input PoseArray message |
| 119 | + * @param target_frame Target frame ID |
| 120 | + * @param out Output PoseArray message in the target frame |
| 121 | + * @param timeout Maximum duration to wait for each pose transform |
| 122 | + * @note This function throws tf2::TransformException on failure. |
| 123 | + * Callers are expected to handle errors via try/catch. |
| 124 | + */ |
| 125 | +inline void transform_pose(tf2_ros::Buffer& tf_buffer, |
| 126 | + const vortex_msgs::msg::LandmarkArray& in, |
| 127 | + const std::string& target_frame, |
| 128 | + vortex_msgs::msg::LandmarkArray& out, |
| 129 | + tf2::Duration timeout = tf2::durationFromSec(0.0)) { |
| 130 | + out.header.stamp = in.header.stamp; |
| 131 | + out.header.frame_id = target_frame; |
| 132 | + out.landmarks.clear(); |
| 133 | + out.landmarks.reserve(in.landmarks.size()); |
| 134 | + |
| 135 | + for (const auto& lm : in.landmarks) { |
| 136 | + vortex_msgs::msg::Landmark lm_out = lm; |
| 137 | + |
| 138 | + geometry_msgs::msg::PoseWithCovarianceStamped in_ps; |
| 139 | + geometry_msgs::msg::PoseWithCovarianceStamped out_ps; |
| 140 | + in_ps.header = in.header; |
| 141 | + in_ps.pose = lm.pose; |
| 142 | + |
| 143 | + tf_buffer.transform(in_ps, out_ps, target_frame, timeout); |
| 144 | + |
| 145 | + lm_out.pose = out_ps.pose; |
| 146 | + out.landmarks.push_back(lm_out); |
| 147 | + } |
| 148 | +} |
| 149 | + |
106 | 150 | } // namespace vortex::utils::ros_transforms |
107 | 151 |
|
108 | 152 | #endif // VORTEX_UTILS__ROS_TRANSFORMS_HPP_ |
0 commit comments