CARLA
 
载入中...
搜索中...
未找到
geom/Vector3D.h
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3// 遵循MIT许可协议,查看许可协议链接
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/MsgPack.h"
10#include <cmath>
11#include <limits>
12
13namespace carla {
14namespace geom {
15
16// 三维向量类,用于表示三维空间中的向量,包含了一系列针对该向量的操作方法
17class Vector3D {
18public:
19 // =========================================================================
20 // -- 公开数据成员 ----------------------------------------------------------
21 // =========================================================================
22
23 // x轴分量
24 float x = 0.0f;
25 // y轴分量
26 float y = 0.0f;
27 // z轴分量
28 float z = 0.0f;
29
30 // =========================================================================
31 // -- 构造函数 --------------------------------------------------------------
32 // =========================================================================
33
34 // 默认构造函数,使用默认初始化,各分量为0.0f
35 Vector3D() = default;
36
37 // 带参数的构造函数,用于初始化向量的x、y、z轴分量
38 Vector3D(float ix, float iy, float iz)
39 : x(ix),
40 y(iy),
41 z(iz) {}
42
43 // =========================================================================
44 // -- 其他方法 --------------------------------------------------------------
45 // =========================================================================
46
47 // 计算向量的长度的平方
48 float SquaredLength() const {
49 return x * x + y * y + z * z;
50 }
51
52 // 计算向量的长度(即模长),通过先求长度的平方再开方得到
53 float Length() const {
54 return std::sqrt(SquaredLength());
55 }
56
57 // 计算二维平面下向量长度的平方(只考虑x、y轴分量)
58 float SquaredLength2D() const {
59 return x * x + y * y;
60 }
61
62 // 计算二维平面下向量的长度(只考虑x、y轴分量),通过先求二维长度的平方再开方得到
63 float Length2D() const {
64 return std::sqrt(SquaredLength2D());
65 }
66
67 // 获取向量各分量取绝对值后的新向量
68 Vector3D Abs() const {
69 return Vector3D(abs(x), abs(y), abs(z));
70 }
71
72 // 将向量转换为单位向量,先获取向量长度,确保长度大于一定极小值(避免除以0等异常情况),然后各分量除以长度得到单位向量
74 const float length = Length();
75 DEVELOPMENT_ASSERT(length > 2.0f * std::numeric_limits<float>::epsilon());
76 const float k = 1.0f / length;
77 return Vector3D(x * k, y * k, z * k);
78 }
79
80 // 将向量转换为安全的单位向量,根据给定的epsilon值判断长度情况,若长度大于epsilon则按正常方式转换为单位向量,否则直接返回原向量(各分量乘以1.0f,相当于不变)
81 Vector3D MakeSafeUnitVector(const float epsilon) const {
82 const float length = Length();
83 const float k = (length > std::max(epsilon, 0.0f))? (1.0f / length) : 1.0f;
84 return Vector3D(x * k, y * k, z * k);
85 }
86
87 // =========================================================================
88 // -- 算术运算符 ------------------------------------------------------------
89 // =========================================================================
90
91 // 重载 += 运算符,实现向量与另一个向量相加并更新自身,将对应分量相加
93 x += rhs.x;
94 y += rhs.y;
95 z += rhs.z;
96 return *this;
97 }
98
99 // 重载 + 运算符,实现向量相加,通过调用 += 运算符实现,返回相加后的新向量
100 friend Vector3D operator+(Vector3D lhs, const Vector3D &rhs) {
101 lhs += rhs;
102 return lhs;
103 }
104
105 // 重载 -= 运算符,实现向量与另一个向量相减并更新自身,将对应分量相减
107 x -= rhs.x;
108 y -= rhs.y;
109 z -= rhs.z;
110 return *this;
111 }
112
113 // 重载 - 运算符,实现向量相减,通过调用 -= 运算符实现,返回相减后的新向量
114 friend Vector3D operator-(Vector3D lhs, const Vector3D &rhs) {
115 lhs -= rhs;
116 return lhs;
117 }
118
119 // 重载 -= 运算符,实现向量减去一个浮点数并更新自身,各分量减去该浮点数
120 Vector3D& operator-=(const float f) {
121 x -= f;
122 y -= f;
123 z -= f;
124 return *this;
125 }
126
127 // 重载 *= 运算符,实现向量与一个浮点数相乘并更新自身,各分量乘以该浮点数
128 Vector3D &operator*=(float rhs) {
129 x *= rhs;
130 y *= rhs;
131 z *= rhs;
132 return *this;
133 }
134
135 // 重载 * 运算符,实现向量与一个浮点数相乘,通过调用 *= 运算符实现,返回相乘后的新向量
136 friend Vector3D operator*(Vector3D lhs, float rhs) {
137 lhs *= rhs;
138 return lhs;
139 }
140
141 // 重载 * 运算符,实现浮点数与一个向量相乘,通过调用向量的 *= 运算符实现,返回相乘后的新向量
142 friend Vector3D operator*(float lhs, Vector3D rhs) {
143 rhs *= lhs;
144 return rhs;
145 }
146
147 // 重载 /= 运算符,实现向量与一个浮点数相除并更新自身,各分量除以该浮点数
148 Vector3D &operator/=(float rhs) {
149 x /= rhs;
150 y /= rhs;
151 z /= rhs;
152 return *this;
153 }
154
155 // 重载 / 运算符,实现向量与一个浮点数相除,通过调用 /= 运算符实现,返回相除后的新向量
156 friend Vector3D operator/(Vector3D lhs, float rhs) {
157 lhs /= rhs;
158 return lhs;
159 }
160
161 // 重载 / 运算符,实现浮点数与一个向量相除,通过调用向量的 /= 运算符实现,返回相除后的新向量(这里可能不符合常规数学意义,只是按照代码逻辑实现的对应操作)
162 friend Vector3D operator/(float lhs, Vector3D rhs) {
163 rhs /= lhs;
164 return rhs;
165 }
166
167 // =========================================================================
168 // -- 比较运算符 ------------------------------------------------------------
169 // =========================================================================
170
171 // 重载 == 运算符,比较两个向量是否相等,通过比较各对应分量是否相等来判断
172 bool operator==(const Vector3D &rhs) const {
173 return (x == rhs.x) && (y == rhs.y) && (z == rhs.z);
174 }
175
176 // 重载!= 运算符,比较两个向量是否不相等,通过对 == 运算符结果取反来判断
177 bool operator!=(const Vector3D &rhs) const {
178 return!(*this == rhs);
179 }
180
181 // =========================================================================
182 // -- 转换为 UE4 类型 -------------------------------------------------------
183 // =========================================================================
184
185#ifdef LIBCARLA_INCLUDED_FROM_UE4
186 // 明确删除了这两种构造函数和赋值运算符重载,目的是避免其他用户错误创建它们,因为向量与位置不同,有些向量有单位,有些没有,删除这些可发现缺少单位转换的地方
187 Vector3D(const FVector &v) = delete;
188 Vector3D& operator=(const FVector &rhs) = delete;
189
190 // 将向量的单位从厘米转换为米并返回新向量,通过各分量乘以1e-2f实现
192 return *this * 1e-2f;
193 }
194
195 // 将向量的单位从米转换为厘米并返回新向量,通过各分量乘以1e2f实现
197 return *this * 1e2f;
198 }
199
200 // 将该Vector3D类型向量转换为UE4中的FVector类型并返回
201 FVector ToFVector() const {
202 return FVector{x, y, z};
203 }
204#endif // LIBCARLA_INCLUDED_FROM_UE4
205
206 // =========================================================================
207 /// @todo 以下内容是从 MSGPACK_DEFINE_ARRAY 复制粘贴的。这是 msgpack 库中问题的解决方法。
208 /// MSGPACK_DEFINE_ARRAY 宏正在遮蔽我们的“z”变量。
209 /// https://github.com/msgpack/msgpack-c/issues/709
210 // =========================================================================
211 // 用于将该向量对象进行msgpack序列化,调用相关函数实现序列化操作
212 template <typename Packer>
213 void msgpack_pack(Packer& pk) const
214 {
215 clmdep_msgpack::type::make_define_array(x, y, z).msgpack_pack(pk);
216 }
217 // 用于将msgpack序列化的数据进行反序列化,恢复该向量对象
218 void msgpack_unpack(clmdep_msgpack::object const& o)
219 {
220 clmdep_msgpack::type::make_define_array(x, y, z).msgpack_unpack(o);
221 }
222 // 用于处理msgpack对象相关操作(可能涉及到更复杂的msgpack使用场景下的对象操作)
223 template <typename MSGPACK_OBJECT>
224 void msgpack_object(MSGPACK_OBJECT* o, clmdep_msgpack::zone& sneaky_variable_that_shadows_z) const
225 {
226 clmdep_msgpack::type::make_define_array(x, y, z).msgpack_object(o, sneaky_variable_that_shadows_z);
227 }
228 // =========================================================================
229};
230
231} // namespace geom
232} // namespace carla
#define DEVELOPMENT_ASSERT(pred)
Definition Debug.h:89
Vector3D & operator*=(float rhs)
float SquaredLength() const
Vector3D MakeUnitVector() const
Vector3D MakeSafeUnitVector(const float epsilon) const
float SquaredLength2D() const
void msgpack_object(MSGPACK_OBJECT *o, clmdep_msgpack::zone &sneaky_variable_that_shadows_z) const
FVector ToFVector() const
Vector3D(const FVector &v)=delete
friend Vector3D operator-(Vector3D lhs, const Vector3D &rhs)
Vector3D & operator+=(const Vector3D &rhs)
friend Vector3D operator/(Vector3D lhs, float rhs)
Vector3D ToCentimeters() const
bool operator!=(const Vector3D &rhs) const
Vector3D & operator-=(const float f)
void msgpack_pack(Packer &pk) const
Vector3D & operator/=(float rhs)
bool operator==(const Vector3D &rhs) const
friend Vector3D operator*(Vector3D lhs, float rhs)
Vector3D Abs() const
Vector3D ToMeters() const
void msgpack_unpack(clmdep_msgpack::object const &o)
float Length2D() const
Vector3D & operator=(const FVector &rhs)=delete
friend Vector3D operator/(float lhs, Vector3D rhs)
Vector3D(float ix, float iy, float iz)
friend Vector3D operator+(Vector3D lhs, const Vector3D &rhs)
friend Vector3D operator*(float lhs, Vector3D rhs)
Vector3D & operator-=(const Vector3D &rhs)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139