Skip to content

Commit ee19f39

Browse files
Fixed to work by pointcloud_map
Signed-off-by: Shintaro Sakoda <shintaro.sakoda@tier4.jp>
1 parent c7eb386 commit ee19f39

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

map/map_height_fitter/src/map_height_fitter.cpp

+50-34
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct MapHeightFitter::Impl
4040
void on_pcd_map(const sensor_msgs::msg::PointCloud2::ConstSharedPtr msg);
4141
void on_vector_map(const autoware_auto_mapping_msgs::msg::HADMapBin::ConstSharedPtr msg);
4242
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);
4444
std::optional<Point> fit(const Point & position, const std::string & frame);
4545

4646
tf2::BufferCore tf2_buffer_;
@@ -170,30 +170,15 @@ void MapHeightFitter::Impl::on_vector_map(
170170
map_frame_ = msg->header.frame_id;
171171
}
172172

173-
double MapHeightFitter::Impl::get_ground_height(const tf2::Vector3 & point)
173+
double MapHeightFitter::Impl::get_ground_height(const Point & point)
174174
{
175175
const auto logger = node_->get_logger();
176176

177-
const double x = point.getX();
178-
const double y = point.getY();
177+
const double x = point.x;
178+
const double y = point.y;
179179

180180
double height = INFINITY;
181181
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-
197182
// find distance d to closest point
198183
double min_dist2 = INFINITY;
199184
for (const auto & p : map_cloud_->points) {
@@ -226,40 +211,71 @@ double MapHeightFitter::Impl::get_ground_height(const tf2::Vector3 & point)
226211
lanelet::utils::query::getClosestLanelet(all_lanelets, pose, &closest_lanelet);
227212
if (!result) {
228213
RCLCPP_WARN_STREAM(logger, "failed to get closest lanelet");
229-
return point.getZ();
214+
return point.z;
230215
}
231216
height = closest_lanelet.centerline().back().z();
232217
}
233218

234-
return std::isfinite(height) ? height : point.getZ();
219+
return std::isfinite(height) ? height : point.z;
235220
}
236221

237222
std::optional<Point> MapHeightFitter::Impl::fit(const Point & position, const std::string & frame)
238223
{
239224
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+
}
241263

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);
243266

267+
// transform map_frame_ to frame
244268
try {
245269
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);
251271
} catch (tf2::TransformException & exception) {
252272
RCLCPP_WARN_STREAM(logger, "failed to lookup transform: " << exception.what());
253273
return std::nullopt;
254274
}
255275

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);
257277

258-
Point result;
259-
result.x = point.getX();
260-
result.y = point.getY();
261-
result.z = point.getZ();
262-
return result;
278+
return point;
263279
}
264280

265281
MapHeightFitter::MapHeightFitter(rclcpp::Node * node)

0 commit comments

Comments
 (0)