forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP requires changing path frame to map
Signed-off-by: Daniel Sanchez <[email protected]>
- Loading branch information
1 parent
a510fa3
commit 7ade4b3
Showing
3 changed files
with
270 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...ware_autonomous_emergency_braking/include/autoware_autonomous_emergency_braking/utils.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2024 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef AUTOWARE_AUTONOMOUS_EMERGENCY_BRAKING__UTILS_HPP_ | ||
#define AUTOWARE_AUTONOMOUS_EMERGENCY_BRAKING__UTILS_HPP_ | ||
|
||
#include <tier4_autoware_utils/geometry/boost_polygon_utils.hpp> | ||
|
||
#include <autoware_perception_msgs/msg/predicted_objects.hpp> | ||
#include <geometry_msgs/msg/point.hpp> | ||
#include <geometry_msgs/msg/pose.hpp> | ||
|
||
#include <boost/geometry/algorithms/convex_hull.hpp> | ||
#include <boost/geometry/algorithms/correct.hpp> | ||
#include <boost/geometry/algorithms/distance.hpp> | ||
#include <boost/optional.hpp> | ||
|
||
#include <map> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace autoware::motion::control::autonomous_emergency_braking::utils | ||
{ | ||
using autoware_perception_msgs::msg::PredictedObject; | ||
using autoware_perception_msgs::msg::PredictedObjects; | ||
using geometry_msgs::msg::Point; | ||
using geometry_msgs::msg::Pose; | ||
using geometry_msgs::msg::TransformStamped; | ||
using tier4_autoware_utils::Point2d; | ||
using tier4_autoware_utils::Polygon2d; | ||
|
||
inline Polygon2d convertPolygonObjectToGeometryPolygon( | ||
const Pose & current_pose, const autoware_perception_msgs::msg::Shape & obj_shape) | ||
{ | ||
Polygon2d object_polygon; | ||
tf2::Transform tf_map2obj; | ||
fromMsg(current_pose, tf_map2obj); | ||
const auto obj_points = obj_shape.footprint.points; | ||
object_polygon.outer().reserve(obj_points.size() + 1); | ||
for (const auto & obj_point : obj_points) { | ||
tf2::Vector3 obj(obj_point.x, obj_point.y, obj_point.z); | ||
tf2::Vector3 tf_obj = tf_map2obj * obj; | ||
object_polygon.outer().emplace_back(tf_obj.x(), tf_obj.y()); | ||
} | ||
object_polygon.outer().push_back(object_polygon.outer().front()); | ||
boost::geometry::correct(object_polygon); | ||
|
||
return object_polygon; | ||
} | ||
|
||
inline Polygon2d convertCylindricalObjectToGeometryPolygon( | ||
const Pose & current_pose, const autoware_perception_msgs::msg::Shape & obj_shape) | ||
{ | ||
Polygon2d object_polygon; | ||
|
||
const double obj_x = current_pose.position.x; | ||
const double obj_y = current_pose.position.y; | ||
|
||
constexpr int N = 20; | ||
const double r = obj_shape.dimensions.x / 2; | ||
object_polygon.outer().reserve(N + 1); | ||
for (int i = 0; i < N; ++i) { | ||
object_polygon.outer().emplace_back( | ||
obj_x + r * std::cos(2.0 * M_PI / N * i), obj_y + r * std::sin(2.0 * M_PI / N * i)); | ||
} | ||
|
||
object_polygon.outer().push_back(object_polygon.outer().front()); | ||
boost::geometry::correct(object_polygon); | ||
|
||
return object_polygon; | ||
} | ||
|
||
inline Polygon2d convertBoundingBoxObjectToGeometryPolygon( | ||
const Pose & current_pose, const double & base_to_front, const double & base_to_rear, | ||
const double & base_to_width) | ||
{ | ||
const auto mapped_point = [](const double & length_scalar, const double & width_scalar) { | ||
tf2::Vector3 map; | ||
map.setX(length_scalar); | ||
map.setY(width_scalar); | ||
map.setZ(0.0); | ||
map.setW(1.0); | ||
return map; | ||
}; | ||
|
||
// set vertices at map coordinate | ||
const tf2::Vector3 p1_map = std::invoke(mapped_point, base_to_front, -base_to_width); | ||
const tf2::Vector3 p2_map = std::invoke(mapped_point, base_to_front, base_to_width); | ||
const tf2::Vector3 p3_map = std::invoke(mapped_point, -base_to_rear, base_to_width); | ||
const tf2::Vector3 p4_map = std::invoke(mapped_point, -base_to_rear, -base_to_width); | ||
|
||
// transform vertices from map coordinate to object coordinate | ||
tf2::Transform tf_map2obj; | ||
tf2::fromMsg(current_pose, tf_map2obj); | ||
const tf2::Vector3 p1_obj = tf_map2obj * p1_map; | ||
const tf2::Vector3 p2_obj = tf_map2obj * p2_map; | ||
const tf2::Vector3 p3_obj = tf_map2obj * p3_map; | ||
const tf2::Vector3 p4_obj = tf_map2obj * p4_map; | ||
|
||
Polygon2d object_polygon; | ||
object_polygon.outer().reserve(5); | ||
object_polygon.outer().emplace_back(p1_obj.x(), p1_obj.y()); | ||
object_polygon.outer().emplace_back(p2_obj.x(), p2_obj.y()); | ||
object_polygon.outer().emplace_back(p3_obj.x(), p3_obj.y()); | ||
object_polygon.outer().emplace_back(p4_obj.x(), p4_obj.y()); | ||
|
||
object_polygon.outer().push_back(object_polygon.outer().front()); | ||
boost::geometry::correct(object_polygon); | ||
|
||
return object_polygon; | ||
} | ||
|
||
inline Polygon2d convertObjToPolygon(const PredictedObject & obj) | ||
{ | ||
Polygon2d object_polygon{}; | ||
if (obj.shape.type == autoware_perception_msgs::msg::Shape::CYLINDER) { | ||
object_polygon = utils::convertCylindricalObjectToGeometryPolygon( | ||
obj.kinematics.initial_pose_with_covariance.pose, obj.shape); | ||
} else if (obj.shape.type == autoware_perception_msgs::msg::Shape::BOUNDING_BOX) { | ||
const double & length_m = obj.shape.dimensions.x / 2; | ||
const double & width_m = obj.shape.dimensions.y / 2; | ||
object_polygon = utils::convertBoundingBoxObjectToGeometryPolygon( | ||
obj.kinematics.initial_pose_with_covariance.pose, length_m, length_m, width_m); | ||
} else if (obj.shape.type == autoware_perception_msgs::msg::Shape::POLYGON) { | ||
object_polygon = utils::convertPolygonObjectToGeometryPolygon( | ||
obj.kinematics.initial_pose_with_covariance.pose, obj.shape); | ||
} else { | ||
throw std::runtime_error("Unsupported shape type"); | ||
} | ||
return object_polygon; | ||
} | ||
} // namespace autoware::motion::control::autonomous_emergency_braking::utils | ||
|
||
#endif // AUTOWARE_AUTONOMOUS_EMERGENCY_BRAKING__UTILS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters