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_msgs/msg/bounding_box.hpp>
25 #include <geometry/common_2d.hpp>
26 #include <array>
27 #include <limits>
28 
29 namespace autoware
30 {
31 namespace common
32 {
33 namespace geometry
34 {
36 namespace bounding_box
37 {
39 
46 template<typename IT>
47 void compute_height(const IT begin, const IT end, BoundingBox & box)
48 {
49  float32_t max_z = -std::numeric_limits<float32_t>::max();
50  float32_t min_z = std::numeric_limits<float32_t>::max();
51  for (auto it = begin; it != end; ++it) {
52  const float32_t z = point_adapter::z_(*it);
53  if (z <= min_z) {
54  min_z = z;
55  }
56  if (z >= max_z) {
57  max_z = z;
58  }
59  }
60  box.centroid.z = (max_z + min_z) * 0.5F;
61  for (auto & corner : box.corners) {
62  corner.z = box.centroid.z;
63  }
64  box.size.z = (max_z - min_z) * 0.5F;
65 }
66 
67 namespace details
68 {
69 
71 template<typename T>
72 using base_type = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
73 
75 template<typename PointT>
76 using Point4 = std::array<PointT, 4U>;
77 
80 template<typename>
81 struct array_size;
82 template<typename T, std::size_t N>
83 struct array_size<std::array<T, N>>
84 {
85  static std::size_t const size = N;
86 };
87 static_assert(
88  array_size<base_type<decltype(BoundingBox::corners)>>::size == 4U,
89  "Bounding box does not have the right number of corners");
90 
94 void GEOMETRY_PUBLIC size_2d(
95  const decltype(BoundingBox::corners) & corners,
96  geometry_msgs::msg::Point32 & ret);
100 void GEOMETRY_PUBLIC finalize_box(
101  const decltype(BoundingBox::corners) & corners, BoundingBox & box);
102 
110 template<typename IT, typename PointT>
112  decltype(BoundingBox::corners) & corners,
113  const Point4<IT> & supports,
114  const Point4<PointT> & directions)
115 {
116  for (uint32_t idx = 0U; idx < corners.size(); ++idx) {
117  const uint32_t jdx = (idx != 3U) ? (idx + 1U) : 0U;
118  const auto pt =
119  intersection_2d(*supports[idx], directions[idx], *supports[jdx], directions[jdx]);
120  // TODO(c.ho) handle error
121  point_adapter::xr_(corners[idx]) = point_adapter::x_(pt);
122  point_adapter::yr_(corners[idx]) = point_adapter::y_(pt);
123  }
124 }
125 // TODO(c.ho) type trait enum base
126 
127 } // namespace details
128 } // namespace bounding_box
129 } // namespace geometry
130 } // namespace common
131 } // namespace autoware
132 
133 #endif // GEOMETRY__BOUNDING_BOX__BOUNDING_BOX_COMMON_HPP_
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:55
autoware_auto_msgs::msg::BoundingBox BoundingBox
Definition: bounding_box_common.hpp:38
This file includes common functionality for 2D geometry, such as dot products.
float float32_t
Definition: types.hpp:36
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:47
auto & xr_(PointT &pt)
Gets a reference to the x value for a point.
Definition: common_2d.hpp:74
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:72
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:111
auto z_(const PointT &pt)
Gets the z value for a point.
Definition: common_2d.hpp:65
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:39
Helper struct for compile time introspection of array size from stackoverflow.com/questions/16866033/...
Definition: bounding_box_common.hpp:81
auto x_(const PointT &pt)
Gets the x value for a point.
Definition: common_2d.hpp:47
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:211
autoware_auto_msgs::msg::BoundingBox BoundingBox
Definition: tf2_autoware_auto_msgs.hpp:36
auto y_(const PointT &pt)
Gets the y value for a point.
Definition: common_2d.hpp:56
end
Definition: scripts/get_open_port.py:23
auto & yr_(PointT &pt)
Gets a reference to the y value for a point.
Definition: common_2d.hpp:83
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
std::array< PointT, 4U > Point4
Templated alias for an array of 4 objects of type PointT.
Definition: bounding_box_common.hpp:76