@@ -40,7 +40,7 @@ struct MapHeightFitter::Impl
40
40
void on_pcd_map (const sensor_msgs::msg::PointCloud2::ConstSharedPtr msg);
41
41
void on_vector_map (const autoware_auto_mapping_msgs::msg::HADMapBin::ConstSharedPtr msg);
42
42
bool get_partial_point_cloud_map (const Point & point);
43
- double get_ground_height (const tf2::Vector3 & point);
43
+ double get_ground_height (const Point & point);
44
44
std::optional<Point > fit (const Point & position, const std::string & frame);
45
45
46
46
tf2::BufferCore tf2_buffer_;
@@ -170,30 +170,15 @@ void MapHeightFitter::Impl::on_vector_map(
170
170
map_frame_ = msg->header .frame_id ;
171
171
}
172
172
173
- double MapHeightFitter::Impl::get_ground_height (const tf2::Vector3 & point)
173
+ double MapHeightFitter::Impl::get_ground_height (const Point & point)
174
174
{
175
175
const auto logger = node_->get_logger ();
176
176
177
- const double x = point.getX () ;
178
- const double y = point.getY () ;
177
+ const double x = point.x ;
178
+ const double y = point.y ;
179
179
180
180
double height = INFINITY;
181
181
if (fit_target_ == " pointcloud_map" ) {
182
- if (cli_pcd_map_) {
183
- geometry_msgs::msg::Point position;
184
- position.x = point.getX ();
185
- position.y = point.getY ();
186
- position.z = point.getZ ();
187
- if (!get_partial_point_cloud_map (position)) {
188
- return point.getZ ();
189
- }
190
- }
191
-
192
- if (!map_cloud_) {
193
- RCLCPP_WARN_STREAM (logger, " point cloud map is not ready" );
194
- return point.getZ ();
195
- }
196
-
197
182
// find distance d to closest point
198
183
double min_dist2 = INFINITY;
199
184
for (const auto & p : map_cloud_->points ) {
@@ -226,40 +211,71 @@ double MapHeightFitter::Impl::get_ground_height(const tf2::Vector3 & point)
226
211
lanelet::utils::query::getClosestLanelet (all_lanelets, pose, &closest_lanelet);
227
212
if (!result) {
228
213
RCLCPP_WARN_STREAM (logger, " failed to get closest lanelet" );
229
- return point.getZ () ;
214
+ return point.z ;
230
215
}
231
216
height = closest_lanelet.centerline ().back ().z ();
232
217
}
233
218
234
- return std::isfinite (height) ? height : point.getZ () ;
219
+ return std::isfinite (height) ? height : point.z ;
235
220
}
236
221
237
222
std::optional<Point > MapHeightFitter::Impl::fit (const Point & position, const std::string & frame)
238
223
{
239
224
const auto logger = node_->get_logger ();
240
- tf2::Vector3 point (position.x , position.y , position.z );
225
+ Point point;
226
+ point.x = position.x ;
227
+ point.y = position.y ;
228
+ point.z = position.z ;
229
+
230
+ RCLCPP_DEBUG (logger, " original point: %.3f %.3f %.3f" , point.x , point.y , point.z );
231
+
232
+ // prepare data
233
+ if (fit_target_ == " pointcloud_map" ) {
234
+ if (!cli_pcd_map_) {
235
+ RCLCPP_WARN_STREAM (logger, " Partial map loading in pointcloud_map_loader is not enabled" );
236
+ return std::nullopt;
237
+ }
238
+ if (!get_partial_point_cloud_map (position)) {
239
+ RCLCPP_WARN_STREAM (logger, " failed to get partial point cloud map" );
240
+ return std::nullopt;
241
+ }
242
+ if (!map_cloud_) {
243
+ RCLCPP_WARN_STREAM (logger, " point cloud map is not ready" );
244
+ return std::nullopt;
245
+ }
246
+ } else if (fit_target_ == " vector_map" ) {
247
+ if (!vector_map_) {
248
+ RCLCPP_WARN_STREAM (logger, " vector map is not ready" );
249
+ return std::nullopt;
250
+ }
251
+ } else {
252
+ throw std::runtime_error (" invalid fit_target" );
253
+ }
254
+
255
+ // transform frame to map_frame_
256
+ try {
257
+ const auto stamped = tf2_buffer_.lookupTransform (frame, map_frame_, tf2::TimePointZero);
258
+ tf2::doTransform (point, point, stamped);
259
+ } catch (tf2::TransformException & exception ) {
260
+ RCLCPP_WARN_STREAM (logger, " failed to lookup transform: " << exception .what ());
261
+ return std::nullopt;
262
+ }
241
263
242
- RCLCPP_DEBUG (logger, " original point: %.3f %.3f %.3f" , point.getX (), point.getY (), point.getZ ());
264
+ // fit height on map_frame_
265
+ point.z = get_ground_height (point);
243
266
267
+ // transform map_frame_ to frame
244
268
try {
245
269
const auto stamped = tf2_buffer_.lookupTransform (map_frame_, frame, tf2::TimePointZero);
246
- tf2::Transform transform{tf2::Quaternion{}, tf2::Vector3{}};
247
- tf2::fromMsg (stamped.transform , transform);
248
- point = transform * point;
249
- point.setZ (get_ground_height (point));
250
- point = transform.inverse () * point;
270
+ tf2::doTransform (point, point, stamped);
251
271
} catch (tf2::TransformException & exception ) {
252
272
RCLCPP_WARN_STREAM (logger, " failed to lookup transform: " << exception .what ());
253
273
return std::nullopt;
254
274
}
255
275
256
- RCLCPP_DEBUG (logger, " modified point: %.3f %.3f %.3f" , point.getX () , point.getY () , point.getZ () );
276
+ RCLCPP_DEBUG (logger, " modified point: %.3f %.3f %.3f" , point.x , point.y , point.z );
257
277
258
- Point result;
259
- result.x = point.getX ();
260
- result.y = point.getY ();
261
- result.z = point.getZ ();
262
- return result;
278
+ return point;
263
279
}
264
280
265
281
MapHeightFitter::MapHeightFitter (rclcpp::Node * node)
0 commit comments