CARLA
 
载入中...
搜索中...
未找到
LibCarla/source/carla/geom/BoundingBox.h
浏览该文件的文档.
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// 引入调试相关头文件
10#include "carla/Debug.h"
11// 引入消息打包相关头文件
12#include "carla/MsgPack.h"
13// 引入变换相关头文件,可能用于3D变换
15// 引入位置相关头文件,定义位置坐标
16#include "carla/geom/Location.h"
17// 引入三维向量相关头文件,表示3D空间中的向量
18#include "carla/geom/Vector3D.h"
19
20#include <array> // 引入标准数组容器头文件
21
22#ifdef LIBCARLA_INCLUDED_FROM_UE4
23// 如果是从UE4中包含的代码,启用UE4相关宏
24
25#include <compiler/enable-ue4-macros.h> // 启用UE4相关宏
26#include "Carla/Util/BoundingBox.h" // 引入碰撞框相关头文件
27#include <compiler/disable-ue4-macros.h> // 禁用UE4相关宏
28
29#endif // LIBCARLA_INCLUDED_FROM_UE4
30
31
32namespace carla {
33namespace geom {
34
35 class BoundingBox { // 边界框类,表示一个3D空间中的矩形区域。
36 public:
37
38 BoundingBox() = default;
39
40 // =========================================================================
41 // -- 构造函数 ---------------------------------------------------------
42 // =========================================================================
43
44 explicit BoundingBox(const Location &in_location, const Vector3D &in_extent, const Rotation &in_rotation)
45 : location(in_location), // 构造一个边界框,指定其中心位置、半大小和旋转。
46 extent(in_extent),
47 rotation(in_rotation) {}
48
49 explicit BoundingBox(const Location &in_location, const Vector3D &in_extent) // 仅指定位置和大小的构造函数。
50 : location(in_location),
51 extent(in_extent),
52 rotation() {}
53
54 explicit BoundingBox(const Vector3D &in_extent) // 仅指定大小的构造函数,位置和旋转默认为默认值。
55 : location(),
56 extent(in_extent),
57 rotation() {}
58
59 // 成员变量定义了边界框的中心位置、半大小和旋转。
60 Location location; ///< 边界框的中心位置(本地坐标系下)
61 Vector3D extent; ///< 边界框的半尺寸(本地坐标系下,表示在每个轴方向上的半宽、半高和半深)
62 Rotation rotation; ///< 边界框的旋转(本地坐标系下)
63
64 // =========================================================================
65 // -- 其他方法 --------------------------------------------------------
66 // =========================================================================
67
68 /**
69 * 检查给定的世界空间中的点是否在边界框内。
70 * @param in_world_point 要检查的世界空间中的点。
71 * @param in_bbox_to_world_transform 从边界框空间到世界空间的变换矩阵。
72 * @return 如果点在边界框内,返回true,否则返回false。
73 */
74 bool Contains(const Location &in_world_point, const Transform &in_bbox_to_world_transform) const {
75 auto point_in_bbox_space = in_world_point;
76 in_bbox_to_world_transform.InverseTransformPoint(point_in_bbox_space); // 将世界空间中的点转换到边界框空间
77 point_in_bbox_space -= location; // 以边界框中心为原点,计算相对位置
78
79 // 判断点是否在边界框的范围内(根据边界框的半大小和坐标轴方向)
80 return point_in_bbox_space.x >= -extent.x && point_in_bbox_space.x <= extent.x &&
81 point_in_bbox_space.y >= -extent.y && point_in_bbox_space.y <= extent.y &&
82 point_in_bbox_space.z >= -extent.z && point_in_bbox_space.z <= extent.z;
83 }
84
85 /**
86 * 返回边界框在本地空间中的8个顶点的位置。
87 * @return 边界框8个顶点的位置数组(不考虑旋转)
88 */
89 std::array<Location, 8> GetLocalVertices() const { // 定义顶点的局部位置
90
91 return {{
92 location + Location(rotation.RotateVector({-extent.x,-extent.y,-extent.z})),
93 location + Location(rotation.RotateVector({-extent.x,-extent.y, extent.z})),
94 location + Location(rotation.RotateVector({-extent.x, extent.y,-extent.z})),
95 location + Location(rotation.RotateVector({-extent.x, extent.y, extent.z})),
96 location + Location(rotation.RotateVector({ extent.x,-extent.y,-extent.z})),
97 location + Location(rotation.RotateVector({ extent.x,-extent.y, extent.z})),
98 location + Location(rotation.RotateVector({ extent.x, extent.y,-extent.z})),
99 location + Location(rotation.RotateVector({ extent.x, extent.y, extent.z}))
100 }};
101 }
102
103 /**
104 * 返回边界框在本地空间中的8个顶点的位置,但不考虑旋转。
105 * @return 边界框8个顶点的位置数组(不考虑旋转)
106 */
107 std::array<Location, 8> GetLocalVerticesNoRotation() const { // 定义顶点的局部位置,不应用旋转
108
109 return {{
118 }};
119 }
120
121 /**
122 * 返回边界框在世界空间中的8个顶点的位置。
123 * @param in_bbox_to_world_tr 从边界框空间到世界空间的变换矩阵。
124 * @return 边界框8个顶点的位置数组(转换到世界空间)
125 */
126 std::array<Location, 8> GetWorldVertices(const Transform &in_bbox_to_world_tr) const { // 获取局部顶点,然后将它们转换到世界空间
127 auto world_vertices = GetLocalVertices();
128 // 将每个局部顶点转换到世界空间
129 std::for_each(world_vertices.begin(), world_vertices.end(), [&in_bbox_to_world_tr](auto &world_vertex) {
130 in_bbox_to_world_tr.TransformPoint(world_vertex);
131 });
132 return world_vertices;
133 }
134
135 // =========================================================================
136 // -- 比较运算符 -------------------------------------------------
137 // =========================================================================
138
139 /**
140 * 比较两个边界框是否相等。
141 * @param rhs 另一个要比较的边界框。
142 * @return 如果两个边界框的中心位置、半尺寸和旋转相同,返回true;否则返回false。
143 */
144 bool operator==(const BoundingBox &rhs) const { // 判断两个边界框是否相等
145 return (location == rhs.location) && (extent == rhs.extent) && (rotation == rhs.rotation);
146 }
147
148 /**
149 * 比较两个边界框是否不相等。
150 * @param rhs 另一个要比较的边界框。
151 * @return 如果两个边界框的任何一个属性不同,返回true;否则返回false。
152 */
153 bool operator!=(const BoundingBox &rhs) const { // 判断两个边界框是否不相等
154 return !(*this == rhs);
155 }
156
157 // =========================================================================
158 // -- 转换为UE4类型 ---------------------------------------------
159 // =========================================================================
160
161#ifdef LIBCARLA_INCLUDED_FROM_UE4
162
163 /**
164 * 从UE4的边界框类型(FBoundingBox)构造一个carla::geom::BoundingBox对象。
165 * @param Box UE4中的边界框对象。
166 */
167 BoundingBox(const FBoundingBox &Box) // 从UE4的边界框类型构造一个carla::geom::BoundingBox对象。
168 : location(Box.Origin),
169 extent(1e-2f * Box.Extent.X, 1e-2f * Box.Extent.Y, 1e-2f * Box.Extent.Z),
170 rotation(Box.Rotation) {}
171
172#endif // LIBCARLA_INCLUDED_FROM_UE4
173 // 序列化边界框对象,使用MsgPack格式进行存储。
175 };
176
177} // namespace geom
178} // namespace carla
bool operator!=(const BoundingBox &rhs) const
比较两个边界框是否不相等。
Location location
边界框的中心位置(本地坐标系下)
std::array< Location, 8 > GetLocalVertices() const
返回边界框在本地空间中的8个顶点的位置。
std::array< Location, 8 > GetLocalVerticesNoRotation() const
返回边界框在本地空间中的8个顶点的位置,但不考虑旋转。
MSGPACK_DEFINE_ARRAY(location, extent, rotation)
BoundingBox(const Location &in_location, const Vector3D &in_extent)
bool Contains(const Location &in_world_point, const Transform &in_bbox_to_world_transform) const
检查给定的世界空间中的点是否在边界框内。
Vector3D extent
边界框的半尺寸(本地坐标系下,表示在每个轴方向上的半宽、半高和半深)
BoundingBox(const FBoundingBox &Box)
从UE4的边界框类型(FBoundingBox)构造一个carla::geom::BoundingBox对象。
std::array< Location, 8 > GetWorldVertices(const Transform &in_bbox_to_world_tr) const
返回边界框在世界空间中的8个顶点的位置。
BoundingBox(const Location &in_location, const Vector3D &in_extent, const Rotation &in_rotation)
bool operator==(const BoundingBox &rhs) const
比较两个边界框是否相等。
Rotation rotation
边界框的旋转(本地坐标系下)
void RotateVector(Vector3D &in_point) const
Definition Rotation.h:80
void InverseTransformPoint(Vector3D &in_point) const
将此变换的逆运算应用于 in_point 操作顺序为先进行位置上的逆平移(减去位置信息),然后再根据旋转的逆操作对向量进行旋转,最终将结果赋值回输入点
CARLA模拟器的主命名空间。
Definition Carla.cpp:139