CARLA
LibCarla/source/carla/geom/BoundingBox.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/Debug.h"
10 #include "carla/MsgPack.h"
11 #include "carla/geom/Transform.h"
12 #include "carla/geom/Location.h"
13 #include "carla/geom/Vector3D.h"
14 
15 #include <array>
16 
17 #ifdef LIBCARLA_INCLUDED_FROM_UE4
18 # include "Carla/Util/BoundingBox.h"
19 #endif // LIBCARLA_INCLUDED_FROM_UE4
20 
21 namespace carla {
22 namespace geom {
23 
24  class BoundingBox {
25  public:
26 
27  BoundingBox() = default;
28 
29  // =========================================================================
30  // -- Constructors ---------------------------------------------------------
31  // =========================================================================
32 
33  explicit BoundingBox(const Location &in_location, const Vector3D &in_extent, const Rotation &in_rotation)
34  : location(in_location),
35  extent(in_extent),
36  rotation(in_rotation) {}
37 
38  explicit BoundingBox(const Location &in_location, const Vector3D &in_extent)
39  : location(in_location),
40  extent(in_extent) {}
41 
42  explicit BoundingBox(const Vector3D &in_extent)
43  : extent(in_extent) {}
44 
45  Location location; ///< Center of the BoundingBox in local space
46  Vector3D extent; ///< Half the size of the BoundingBox in local space
47  Rotation rotation; ///< Rotation of the BoundingBox in local space
48 
49  // =========================================================================
50  // -- Other methods --------------------------------------------------------
51  // =========================================================================
52 
53  /**
54  * Whether this BoundingBox contains @a in_world_point in world space.
55  * @param in_world_point the point in world space that you want to query whether it is inside or not.
56  * @param in_bbox_to_world_transform the transformation from BoundingBox space to World space.
57  */
58  bool Contains(const Location &in_world_point, const Transform &in_bbox_to_world_transform) const {
59  auto point_in_bbox_space = in_world_point;
60  in_bbox_to_world_transform.InverseTransformPoint(point_in_bbox_space);
61  point_in_bbox_space -= location;
62 
63  return point_in_bbox_space.x >= -extent.x && point_in_bbox_space.x <= extent.x &&
64  point_in_bbox_space.y >= -extent.y && point_in_bbox_space.y <= extent.y &&
65  point_in_bbox_space.z >= -extent.z && point_in_bbox_space.z <= extent.z;
66  }
67 
68  /**
69  * Returns the positions of the 8 vertices of this BoundingBox in local space.
70  */
71  std::array<Location, 8> GetLocalVertices() const {
72 
73  return {{
74  location + Location(rotation.RotateVector({-extent.x,-extent.y,-extent.z})),
75  location + Location(rotation.RotateVector({-extent.x,-extent.y, extent.z})),
76  location + Location(rotation.RotateVector({-extent.x, extent.y,-extent.z})),
77  location + Location(rotation.RotateVector({-extent.x, extent.y, extent.z})),
78  location + Location(rotation.RotateVector({ extent.x,-extent.y,-extent.z})),
79  location + Location(rotation.RotateVector({ extent.x,-extent.y, extent.z})),
80  location + Location(rotation.RotateVector({ extent.x, extent.y,-extent.z})),
81  location + Location(rotation.RotateVector({ extent.x, extent.y, extent.z}))
82  }};
83  }
84 
85  /**
86  * Returns the positions of the 8 vertices of this BoundingBox in world space.
87  * @param in_bbox_to_world_transform The Transform from this BoundingBox space to world space.
88  */
89  std::array<Location, 8> GetWorldVertices(const Transform &in_bbox_to_world_tr) const {
90  auto world_vertices = GetLocalVertices();
91  std::for_each(world_vertices.begin(), world_vertices.end(), [&in_bbox_to_world_tr](auto &world_vertex) {
92  in_bbox_to_world_tr.TransformPoint(world_vertex);
93  });
94  return world_vertices;
95  }
96 
97  // =========================================================================
98  // -- Comparison operators -------------------------------------------------
99  // =========================================================================
100 
101  bool operator==(const BoundingBox &rhs) const {
102  return (location == rhs.location) && (extent == rhs.extent) && (rotation == rhs.rotation);
103  }
104 
105  bool operator!=(const BoundingBox &rhs) const {
106  return !(*this == rhs);
107  }
108 
109  // =========================================================================
110  // -- Conversions to UE4 types ---------------------------------------------
111  // =========================================================================
112 
113 #ifdef LIBCARLA_INCLUDED_FROM_UE4
114 
115  BoundingBox(const FBoundingBox &Box)
116  : location(Box.Origin),
117  extent(1e-2f * Box.Extent.X, 1e-2f * Box.Extent.Y, 1e-2f * Box.Extent.Z),
118  rotation(Box.Rotation) {}
119 
120 #endif // LIBCARLA_INCLUDED_FROM_UE4
121 
122  MSGPACK_DEFINE_ARRAY(location, extent, rotation);
123  };
124 
125 } // namespace geom
126 } // namespace carla
std::array< Location, 8 > GetLocalVertices() const
Returns the positions of the 8 vertices of this BoundingBox in local space.
Location location
Center of the BoundingBox in local space.
BoundingBox(const Location &in_location, const Vector3D &in_extent, const Rotation &in_rotation)
Vector3D extent
Half the size of the BoundingBox in local space.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
geom::Location Location
Definition: rpc/Location.h:14
bool operator==(const BoundingBox &rhs) const
std::array< Location, 8 > GetWorldVertices(const Transform &in_bbox_to_world_tr) const
Returns the positions of the 8 vertices of this BoundingBox in world space.
FRotator Rotation
Rotation of the bounding box.
void TransformPoint(Vector3D &in_point) const
Applies this transformation to in_point (first translation then rotation).
Rotation rotation
Rotation of the BoundingBox in local space.
BoundingBox(const Location &in_location, const Vector3D &in_extent)
bool operator!=(const BoundingBox &rhs) const
FVector Origin
Origin of the bounding box relative to its owner.
void InverseTransformPoint(Vector3D &in_point) const
Applies the inverse of this transformation to in_point.
FVector Extent
Radii extent of the bounding box.
void RotateVector(Vector3D &in_point) const
Definition: Rotation.h:62
bool Contains(const Location &in_world_point, const Transform &in_bbox_to_world_transform) const
Whether this BoundingBox contains in_world_point in world space.
MSGPACK_DEFINE_ARRAY(location, extent, rotation)