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
19 #include "Carla/Util/BoundingBox.h"
21 #endif // LIBCARLA_INCLUDED_FROM_UE4
22 
23 namespace carla {
24 namespace geom {
25 
26  class BoundingBox {
27  public:
28 
29  BoundingBox() = default;
30 
31  // =========================================================================
32  // -- Constructors ---------------------------------------------------------
33  // =========================================================================
34 
35  explicit BoundingBox(const Location &in_location, const Vector3D &in_extent, const Rotation &in_rotation)
36  : location(in_location),
37  extent(in_extent),
38  rotation(in_rotation) {}
39 
40  explicit BoundingBox(const Location &in_location, const Vector3D &in_extent)
41  : location(in_location),
42  extent(in_extent),
43  rotation() {}
44 
45  explicit BoundingBox(const Vector3D &in_extent)
46  : location(),
47  extent(in_extent),
48  rotation() {}
49 
50  Location location; ///< Center of the BoundingBox in local space
51  Vector3D extent; ///< Half the size of the BoundingBox in local space
52  Rotation rotation; ///< Rotation of the BoundingBox in local space
53 
54  // =========================================================================
55  // -- Other methods --------------------------------------------------------
56  // =========================================================================
57 
58  /**
59  * Whether this BoundingBox contains @a in_world_point in world space.
60  * @param in_world_point the point in world space that you want to query whether it is inside or not.
61  * @param in_bbox_to_world_transform the transformation from BoundingBox space to World space.
62  */
63  bool Contains(const Location &in_world_point, const Transform &in_bbox_to_world_transform) const {
64  auto point_in_bbox_space = in_world_point;
65  in_bbox_to_world_transform.InverseTransformPoint(point_in_bbox_space);
66  point_in_bbox_space -= location;
67 
68  return point_in_bbox_space.x >= -extent.x && point_in_bbox_space.x <= extent.x &&
69  point_in_bbox_space.y >= -extent.y && point_in_bbox_space.y <= extent.y &&
70  point_in_bbox_space.z >= -extent.z && point_in_bbox_space.z <= extent.z;
71  }
72 
73  /**
74  * Returns the positions of the 8 vertices of this BoundingBox in local space.
75  */
76  std::array<Location, 8> GetLocalVertices() const {
77 
78  return {{
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  location + Location(rotation.RotateVector({-extent.x, extent.y, extent.z})),
83  location + Location(rotation.RotateVector({ extent.x,-extent.y,-extent.z})),
84  location + Location(rotation.RotateVector({ extent.x,-extent.y, extent.z})),
85  location + Location(rotation.RotateVector({ extent.x, extent.y,-extent.z})),
86  location + Location(rotation.RotateVector({ extent.x, extent.y, extent.z}))
87  }};
88  }
89 
90  /**
91  * Returns the positions of the 8 vertices of this BoundingBox in local space without its own rotation.
92  */
93  std::array<Location, 8> GetLocalVerticesNoRotation() const {
94 
95  return {{
96  location + Location(-extent.x,-extent.y,-extent.z),
97  location + Location(-extent.x,-extent.y, extent.z),
98  location + Location(-extent.x, extent.y,-extent.z),
99  location + Location(-extent.x, extent.y, extent.z),
100  location + Location( extent.x,-extent.y,-extent.z),
101  location + Location( extent.x,-extent.y, extent.z),
102  location + Location( extent.x, extent.y,-extent.z),
103  location + Location( extent.x, extent.y, extent.z)
104  }};
105  }
106 
107  /**
108  * Returns the positions of the 8 vertices of this BoundingBox in world space.
109  * @param in_bbox_to_world_transform The Transform from this BoundingBox space to world space.
110  */
111  std::array<Location, 8> GetWorldVertices(const Transform &in_bbox_to_world_tr) const {
112  auto world_vertices = GetLocalVertices();
113  std::for_each(world_vertices.begin(), world_vertices.end(), [&in_bbox_to_world_tr](auto &world_vertex) {
114  in_bbox_to_world_tr.TransformPoint(world_vertex);
115  });
116  return world_vertices;
117  }
118 
119  // =========================================================================
120  // -- Comparison operators -------------------------------------------------
121  // =========================================================================
122 
123  bool operator==(const BoundingBox &rhs) const {
124  return (location == rhs.location) && (extent == rhs.extent) && (rotation == rhs.rotation);
125  }
126 
127  bool operator!=(const BoundingBox &rhs) const {
128  return !(*this == rhs);
129  }
130 
131  // =========================================================================
132  // -- Conversions to UE4 types ---------------------------------------------
133  // =========================================================================
134 
135 #ifdef LIBCARLA_INCLUDED_FROM_UE4
136 
138  : location(Box.Origin),
139  extent(1e-2f * Box.Extent.X, 1e-2f * Box.Extent.Y, 1e-2f * Box.Extent.Z),
140  rotation(Box.Rotation) {}
141 
142 #endif // LIBCARLA_INCLUDED_FROM_UE4
143 
144  MSGPACK_DEFINE_ARRAY(location, extent, rotation);
145  };
146 
147 } // namespace geom
148 } // 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:133
bg::model::box< Point3D > Box
Definition: InMemoryMap.h:52
geom::Location Location
Definition: rpc/Location.h:14
bool operator==(const BoundingBox &rhs) const
std::array< Location, 8 > GetLocalVerticesNoRotation() const
Returns the positions of the 8 vertices of this BoundingBox in local space without its own rotation...
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:64
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)