Autoware.Auto
bounding_box_common.hpp
Go to the documentation of this file.
1 // Copyright 2017-2019 the Autoware Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Co-developed by Tier IV, Inc. and Apex.AI, Inc.
16 
19 
20 #ifndef GEOMETRY__BOUNDING_BOX__BOUNDING_BOX_COMMON_HPP_
21 #define GEOMETRY__BOUNDING_BOX__BOUNDING_BOX_COMMON_HPP_
22 
23 #include <autoware_auto_perception_msgs/msg/bounding_box.hpp>
24 #include <autoware_auto_perception_msgs/msg/detected_object.hpp>
25 #include <autoware_auto_perception_msgs/msg/shape.hpp>
27 #include <geometry/common_2d.hpp>
28 #include <geometry_msgs/msg/pose.hpp>
29 
30 #include <array>
31 #include <limits>
32 #include <vector>
33 
34 namespace autoware
35 {
36 namespace common
37 {
38 namespace geometry
39 {
41 namespace bounding_box
42 {
44 
51 template<typename IT>
52 void compute_height(const IT begin, const IT end, BoundingBox & box)
53 {
54  float32_t max_z = -std::numeric_limits<float32_t>::max();
55  float32_t min_z = std::numeric_limits<float32_t>::max();
56  for (auto it = begin; it != end; ++it) {
57  const float32_t z = point_adapter::z_(*it);
58  if (z <= min_z) {
59  min_z = z;
60  }
61  if (z >= max_z) {
62  max_z = z;
63  }
64  }
65  box.centroid.z = (max_z + min_z) * 0.5F;
66  for (auto & corner : box.corners) {
67  corner.z = box.centroid.z;
68  }
69  box.size.z = (max_z - min_z);
70 }
71 
78 template<typename IT>
79 void compute_height(const IT begin, const IT end, autoware_auto_perception_msgs::msg::Shape & shape)
80 {
81  float32_t max_z = -std::numeric_limits<float32_t>::max();
82  float32_t min_z = std::numeric_limits<float32_t>::max();
83  for (auto it = begin; it != end; ++it) {
84  const float32_t z = point_adapter::z_(*it);
85  if (z <= min_z) {
86  min_z = z;
87  }
88  if (z >= max_z) {
89  max_z = z;
90  }
91  }
92  for (auto & corner : shape.polygon.points) {
93  corner.z = min_z;
94  }
95  shape.height = max_z - min_z;
96 }
97 
98 namespace details
99 {
100 
102 template<typename T>
103 using base_type = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
104 
106 template<typename PointT>
107 using Point4 = std::array<PointT, 4U>;
108 
111 template<typename>
112 struct array_size;
113 template<typename T, std::size_t N>
114 struct array_size<std::array<T, N>>
115 {
116  static std::size_t const size = N;
117 };
118 static_assert(
119  array_size<base_type<decltype(BoundingBox::corners)>>::size == 4U,
120  "Bounding box does not have the right number of corners");
121 
125 void GEOMETRY_PUBLIC size_2d(
126  const decltype(BoundingBox::corners) & corners,
127  geometry_msgs::msg::Point32 & ret);
131 void GEOMETRY_PUBLIC finalize_box(
132  const decltype(BoundingBox::corners) & corners, BoundingBox & box);
133 
141 template<typename IT, typename PointT>
143  decltype(BoundingBox::corners) & corners,
144  const Point4<IT> & supports,
145  const Point4<PointT> & directions)
146 {
147  for (uint32_t idx = 0U; idx < corners.size(); ++idx) {
148  const uint32_t jdx = (idx != 3U) ? (idx + 1U) : 0U;
149  const auto pt =
150  intersection_2d(*supports[idx], directions[idx], *supports[jdx], directions[jdx]);
151  // TODO(c.ho) handle error
152  point_adapter::xr_(corners[idx]) = point_adapter::x_(pt);
153  point_adapter::yr_(corners[idx]) = point_adapter::y_(pt);
154  }
155 }
156 // TODO(c.ho) type trait enum base
157 
161 autoware_auto_perception_msgs::msg::Shape GEOMETRY_PUBLIC make_shape(const BoundingBox & box);
162 
166 geometry_msgs::msg::Pose GEOMETRY_PUBLIC make_pose(const BoundingBox & box);
167 
171 autoware_auto_perception_msgs::msg::DetectedObject GEOMETRY_PUBLIC make_detected_object(
172  const BoundingBox & box);
173 
180 std::vector<geometry_msgs::msg::Point32> GEOMETRY_PUBLIC get_transformed_corners(
181  const autoware_auto_perception_msgs::msg::Shape & shape_msg,
182  const geometry_msgs::msg::Point & centroid,
183  const geometry_msgs::msg::Quaternion & orientation);
184 
185 } // namespace details
186 } // namespace bounding_box
187 } // namespace geometry
188 } // namespace common
189 } // namespace autoware
190 
191 #endif // GEOMETRY__BOUNDING_BOX__BOUNDING_BOX_COMMON_HPP_
autoware::common::geometry::point_adapter::x_
auto x_(const PointT &pt)
Gets the x value for a point.
Definition: common_2d.hpp:50
autoware::common::geometry::bounding_box::details::compute_corners
void compute_corners(decltype(BoundingBox::corners) &corners, const Point4< IT > &supports, const Point4< PointT > &directions)
given support points and two orthogonal directions, compute corners of bounding box
Definition: bounding_box_common.hpp:142
visibility_control.hpp
common_2d.hpp
This file includes common functionality for 2D geometry, such as dot products.
BoundingBox
autoware_auto_perception_msgs::msg::BoundingBox BoundingBox
Definition: tf2_autoware_auto_msgs.hpp:39
autoware::common::geometry::bounding_box::details::make_shape
autoware_auto_perception_msgs::msg::Shape GEOMETRY_PUBLIC make_shape(const BoundingBox &box)
Copy vertices of the given box into a Shape type.
Definition: bounding_box.cpp:70
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::common::geometry::point_adapter::yr_
auto & yr_(PointT &pt)
Gets a reference to the y value for a point.
Definition: common_2d.hpp:114
autoware::common::geometry::bounding_box::details::make_detected_object
autoware_auto_perception_msgs::msg::DetectedObject GEOMETRY_PUBLIC make_detected_object(const BoundingBox &box)
Fill DetectedObject type with contents from a BoundingBox type.
Definition: bounding_box.cpp:100
autoware::common::types::float32_t
float float32_t
Definition: types.hpp:45
autoware::common::geometry::point_adapter::xr_
auto & xr_(PointT &pt)
Gets a reference to the x value for a point.
Definition: common_2d.hpp:98
autoware::common::geometry::bounding_box::details::get_transformed_corners
std::vector< geometry_msgs::msg::Point32 > GEOMETRY_PUBLIC get_transformed_corners(const autoware_auto_perception_msgs::msg::Shape &shape_msg, const geometry_msgs::msg::Point &centroid, const geometry_msgs::msg::Quaternion &orientation)
Transform corners from object-local coordinates using the given centroid and orientation.
Definition: bounding_box.cpp:126
autoware::common::geometry::intersection_2d
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
autoware::common::geometry::bounding_box::details::array_size
Helper struct for compile time introspection of array size from stackoverflow.com/questions/16866033/...
Definition: bounding_box_common.hpp:112
autoware::common::geometry::point_adapter::y_
auto y_(const PointT &pt)
Gets the y value for a point.
Definition: common_2d.hpp:66
Point
geometry_msgs::msg::Point32 Point
Definition: cluster2d.cpp:28
autoware::common::geometry::bounding_box::details::base_type
typename std::remove_cv< typename std::remove_reference< T >::type >::type base_type
Templated alias for getting a type without CV or reference qualification.
Definition: bounding_box_common.hpp:103
autoware::common::geometry::bounding_box::details::size_2d
void GEOMETRY_PUBLIC size_2d(const decltype(BoundingBox::corners) &corners, geometry_msgs::msg::Point32 &ret)
Compute length, width, area of bounding box.
Definition: bounding_box.cpp:41
autoware::common::geometry::bounding_box::details::Point4
std::array< PointT, 4U > Point4
Templated alias for an array of 4 objects of type PointT.
Definition: bounding_box_common.hpp:107
autoware::common::geometry::point_adapter::z_
auto z_(const PointT &pt)
Gets the z value for a point.
Definition: common_2d.hpp:82
autoware::common::geometry::bounding_box::compute_height
void compute_height(const IT begin, const IT end, BoundingBox &box)
Computes height of bounding box given a full list of points.
Definition: bounding_box_common.hpp:52
autoware::common::geometry::bounding_box::details::make_pose
geometry_msgs::msg::Pose GEOMETRY_PUBLIC make_pose(const BoundingBox &box)
Copy centroid and orientation info of the box into Pose type.
autoware::common::geometry::bounding_box::details::finalize_box
void GEOMETRY_PUBLIC finalize_box(const decltype(BoundingBox::corners) &corners, BoundingBox &box)
Computes centroid and orientation of a box given corners.
Definition: bounding_box.cpp:57
get_open_port.end
end
Definition: scripts/get_open_port.py:23
autoware::common::geometry::bounding_box::BoundingBox
autoware_auto_perception_msgs::msg::BoundingBox BoundingBox
Definition: bounding_box_common.hpp:43