Go to the documentation of this file.
17 #ifndef GEOMETRY__INTERSECTION_HPP_
18 #define GEOMETRY__INTERSECTION_HPP_
21 #include <autoware_auto_vehicle_msgs/msg/vehicle_kinematic_state.hpp>
22 #include <autoware_auto_perception_msgs/msg/bounding_box.hpp>
23 #include <autoware_auto_planning_msgs/msg/trajectory_point.hpp>
32 #include <type_traits>
51 using Point = geometry_msgs::msg::Point32;
57 using Line = std::pair<Point, Point>;
65 template<
typename Iter>
69 auto corner_list = std::list<Point>(start,
end);
70 const auto first_interior_point =
convex_hull(corner_list);
72 std::vector<Line> face_list{};
73 auto itLast = corner_list.begin();
74 auto itNext = std::next(itLast, 1);
76 face_list.emplace_back(
Line{*itLast, *itNext});
78 itNext = std::next(itNext, 1);
79 }
while ((itNext != first_interior_point) && (itNext != corner_list.end()));
81 face_list.emplace_back(
Line{*itLast, corner_list.front()});
87 template<
template<
typename ...>
class Iterable1T,
template<
typename ...>
class Iterable2T,
90 const Iterable1T<PointT> & external,
91 const Iterable2T<PointT> &
internal,
92 std::list<PointT> & result)
95 internal.begin(),
internal.
end(), std::back_inserter(result),
96 [&external](
const auto & pt) {
102 template<
template<
typename ...>
class Iterable1T,
template<
typename ...>
class Iterable2T,
105 const Iterable1T<PointT> & polygon1,
106 const Iterable2T<PointT> & polygon2,
107 std::list<PointT> & result)
112 auto get_edge = [](
const auto & list,
const auto & iterator) {
113 const auto next_it = std::next(iterator);
114 const auto & next_pt = (next_it != list.end()) ? *next_it : list.front();
115 return std::make_pair(*iterator, next_pt);
119 auto compute_eps_scale = [](
const auto & i1,
const auto & i2,
const auto val) {
120 auto get_abs_max = [](
const auto & interval) {
123 return std::max(std::fabs(val), std::max(get_abs_max(i1), get_abs_max(i2)));
127 for (
auto corner1_it = polygon1.begin(); corner1_it != polygon1.end(); ++corner1_it) {
128 const auto & edge1 = get_edge(polygon1, corner1_it);
138 for (
auto corner2_it = polygon2.begin(); corner2_it != polygon2.end(); ++corner2_it) {
140 const auto & edge2 = get_edge(polygon2, corner2_it);
141 const auto & intersection =
143 edge1.first,
minus_2d(edge1.second, edge1.first),
144 edge2.first,
minus_2d(edge2.second, edge2.first));
157 const auto max_feps_scale = std::max(
158 compute_eps_scale(edge1_x_interval, edge2_x_interval,
point_adapter::x_(intersection)),
159 compute_eps_scale(edge1_y_interval, edge2_y_interval,
point_adapter::y_(intersection))
161 const auto feps = max_feps_scale * std::numeric_limits<FloatT>::epsilon();
168 result.push_back(intersection);
170 }
catch (
const std::runtime_error &) {
192 template<
typename Iter>
193 bool intersect(
const Iter begin1,
const Iter end1,
const Iter begin2,
const Iter end2)
198 faces.reserve(faces.size() + faces_2.size());
199 faces.insert(faces.end(), faces_2.begin(), faces_2.end() );
202 for (
const auto & face : faces) {
205 auto get_position_along_line = [&normal](
auto point)
212 auto get_projected_min_max = [&get_position_along_line, &normal](Iter begin, Iter
end)
214 const auto zero_point =
Point{};
217 auto max_corners = min_corners;
219 for (
auto & point = begin; point !=
end; ++point) {
221 const auto position_along_line = get_position_along_line(point_projected);
222 min_corners = std::min(min_corners, position_along_line);
223 max_corners = std::max(max_corners, position_along_line);
225 return std::pair<float, float>{min_corners, max_corners};
229 auto minmax_1 = get_projected_min_max(begin1, end1);
230 auto minmax_2 = get_projected_min_max(begin2, end2);
233 const auto eps = std::numeric_limits<decltype(minmax_1.first)>::epsilon();
234 if (minmax_1.first > minmax_2.second + eps || minmax_2.first > minmax_1.second + eps) {
263 template<
template<
typename ...>
class Iterable1T,
template<
typename ...>
class Iterable2T,
266 const Iterable1T<PointT> & polygon1,
267 const Iterable2T<PointT> & polygon2)
269 std::list<PointT> result;
274 result.resize(
static_cast<uint32_t
>(std::distance(result.cbegin(), end_it)));
290 template<
template<
typename ...>
class Iterable1T,
template<
typename ...>
class Iterable2T,
293 const Iterable1T<PointT> & polygon1,
294 const Iterable2T<PointT> & polygon2
297 constexpr
auto eps = std::numeric_limits<float32_t>::epsilon();
300 const auto intersection_area =
303 if (intersection_area < eps) {
307 const auto polygon1_area =
309 const auto polygon2_area =
312 const auto union_area = polygon1_area + polygon2_area - intersection_area;
313 if (union_area < eps) {
314 throw std::domain_error(
"IoU is undefined for polygons with a zero union area");
317 return intersection_area / union_area;
324 #endif // GEOMETRY__INTERSECTION_HPP_
autoware_auto_planning_msgs::msg::TrajectoryPoint TrajectoryPoint
Definition: motion_testing_publisher.hpp:36
T times_2d(const T &p, const float32_t a)
The scalar multiplication operation, p * a.
Definition: common_2d.hpp:254
bool intersect(const Iter begin1, const Iter end1, const Iter begin2, const Iter end2)
Check if polyhedra defined by two given sets of points intersect.
Definition: intersection.hpp:193
T get_normal(const T &pt)
compute q s.t. p T q, or p * q = 0 This is the equivalent of a 90 degree ccw rotation
Definition: common_2d.hpp:327
std::pair< Point, Point > Line
Alias for a std::pair of two points.
Definition: intersection.hpp:57
auto x_(const PointT &pt)
Gets the x value for a point.
Definition: common_2d.hpp:50
This file implements the monotone chain algorithm to compute 2D convex hulls on linked lists of point...
void append_contained_points(const Iterable1T< PointT > &external, const Iterable2T< PointT > &internal, std::list< PointT > &result)
Append points of the polygon internal that are contained in the polygon exernal.
Definition: intersection.hpp:89
std::vector< Line > get_sorted_face_list(const Iter start, const Iter end)
Compute a sorted list of faces of a polyhedron given a list of points.
Definition: intersection.hpp:66
std::list< PointT > convex_polygon_intersection2d(const Iterable1T< PointT > &polygon1, const Iterable2T< PointT > &polygon2)
Get the intersection between two polygons. The polygons should be provided in an identical format to ...
Definition: intersection.hpp:265
T closest_line_point_2d(const T &p, const T &q, const T &r)
Compute the closest point on the line going through p-q to point r.
Definition: common_2d.hpp:378
This file includes common functionality for 2D geometry, such as dot products.
auto area_2d(const IT begin, const IT end) noexcept
Definition: common_2d.hpp:504
auto dot_2d(const T1 &pt, const T2 &q)
compute p * q = p1 * q1 + p2 * q2
Definition: common_2d.hpp:193
geometry_msgs::msg::Point32 Point
Definition: intersection.hpp:51
bool is_point_inside_polygon_2d(const IteratorType &start_it, const IteratorType &end_it, const PointType &p)
Check if the given point is inside or on the edge of the given polygon.
Definition: common_2d.hpp:545
autoware_auto_perception_msgs::msg::BoundingBox BoundingBox
Definition: tf2_autoware_auto_msgs.hpp:39
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
std::list< PointT >::const_iterator convex_hull(std::list< PointT > &list)
A static memory implementation of convex hull computation. Shuffles points around the deque such that...
Definition: convex_hull.hpp:185
static T min(const Interval &i)
The minimum bound of the interval.
Definition: interval.hpp:134
auto norm_2d(const T &pt)
get magnitude of x and y components:
Definition: common_2d.hpp:340
void append_intersection_points(const Iterable1T< PointT > &polygon1, const Iterable2T< PointT > &polygon2, std::list< PointT > &result)
Append the intersecting points between two polygons into the output list.
Definition: intersection.hpp:104
common::types::float32_t convex_intersection_over_union_2d(const Iterable1T< PointT > &polygon1, const Iterable2T< PointT > &polygon2)
Compute the intersection over union of two 2d convex polygons. If any of the polygons span a zero are...
Definition: intersection.hpp:292
float float32_t
Definition: types.hpp:45
T minus_2d(const T &p, const T &q)
Compute the 2d difference between two points, p - q.
Definition: common_2d.hpp:207
static bool contains(const Interval &i, const T &value, const T &eps=std::numeric_limits< T >::epsilon())
Test for whether a given interval contains a given value within the given epsilon.
Definition: interval.hpp:313
T intersection_2d(const T &pt, const T &u, const T &q, const T &v)
solve p + t * u = q + s * v Ref: https://stackoverflow.com/questions/563198/ whats-the-most-efficent-...
Definition: common_2d.hpp:273
static T max(const Interval &i)
The maximum bound of the interval.
Definition: interval.hpp:137
auto y_(const PointT &pt)
Gets the y value for a point.
Definition: common_2d.hpp:66
Data structure to contain scalar interval bounds.
Definition: interval.hpp:52
end
Definition: scripts/get_open_port.py:23