CARLA
 
载入中...
搜索中...
未找到
geom/Vector2D.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#include "carla/MsgPack.h"
10
11#include <cmath>
12#include <limits>
13
14namespace carla {
15namespace geom { ///定义两个嵌套的命名空间:carla和geom。
16
17 class Vector2D { ///定义一个名为Vector2D的公共类,用于表示二维向量。
18 public:
19
20 // =========================================================================
21 // -- 公开数据成员 --------------------------------------------------
22 // =========================================================================
23
24 float x = 0.0f;
25
26 float y = 0.0f; // 向量的两个分量,x和y
27
28 // =========================================================================
29 // -- 构造函数 ---------------------------------------------------------
30 // =========================================================================
31
32 Vector2D() = default; ///定义一个默认构造函数。
33
34 Vector2D(float ix, float iy) // 构造函数,接受两个参数初始化x和y
35 : x(ix),
36 y(iy) {}
37
38 // =========================================================================
39 // -- 其他方法 --------------------------------------------------------
40 // =========================================================================
41
42 float SquaredLength() const { // 计算向量的长度的平方,避免开方运算,提高效率
43 return x * x + y * y;
44 }
45
46 float Length() const { // 计算向量的长度
47 return std::sqrt(SquaredLength());
48 }
49
50 Vector2D MakeUnitVector() const { // 将向量转换为单位向量,要求向量长度不为零
51 const float len = Length();
52 DEVELOPMENT_ASSERT(len > 2.0f * std::numeric_limits<float>::epsilon());
53 const float k = 1.0f / len;
54 return Vector2D(x * k, y * k);
55 }
56
57 // =========================================================================
58 // -- 算术运算符 -------------------------------------------------
59 // =========================================================================
60
61 Vector2D &operator+=(const Vector2D &rhs) { // 重载+=运算符,实现向量的加法
62 x += rhs.x;
63 y += rhs.y;
64 return *this;
65 }
66
67 friend Vector2D operator+(Vector2D lhs, const Vector2D &rhs) { // 重载+运算符,实现向量的加法
68 lhs += rhs;
69 return lhs;
70 }
71
72 Vector2D &operator-=(const Vector2D &rhs) { // 重载-=运算符,实现向量的减法
73 x -= rhs.x;
74 y -= rhs.y;
75 return *this;
76 }
77
78 friend Vector2D operator-(Vector2D lhs, const Vector2D &rhs) { // 重载-运算符,实现向量的减法
79 lhs -= rhs;
80 return lhs;
81 }
82
83 Vector2D &operator*=(float rhs) { // 重载*=运算符,实现向量与标量的乘法
84 x *= rhs;
85 y *= rhs;
86 return *this;
87 }
88
89 friend Vector2D operator*(Vector2D lhs, float rhs) { // 重载*运算符,实现向量与标量的乘法
90 lhs *= rhs;
91 return lhs;
92 }
93
94 friend Vector2D operator*(float lhs, Vector2D rhs) { // 重载*运算符,实现标量与向量的乘法
95 rhs *= lhs;
96 return rhs;
97 }
98
99 Vector2D &operator/=(float rhs) { // 重载/=运算符,实现向量与标量的除法
100 x /= rhs;
101 y /= rhs;
102 return *this;
103 }
104
105 friend Vector2D operator/(Vector2D lhs, float rhs) { // 重载/运算符,实现向量与标量的除法
106 lhs /= rhs;
107 return lhs;
108 }
109
110 friend Vector2D operator/(float lhs, Vector2D rhs) { // 重载/运算符,实现标量与向量的除法
111 rhs /= lhs;
112 return rhs;
113 }
114
115 // =========================================================================
116 // -- 比较运算符 -------------------------------------------------
117 // =========================================================================
118
119 bool operator==(const Vector2D &rhs) const { // 重载==运算符,比较两个向量是否相等
120 return (x == rhs.x) && (y == rhs.y);
121 }
122
123 bool operator!=(const Vector2D &rhs) const { // 重载!=运算符,比较两个向量是否不相等
124 return !(*this == rhs);
125 }
126
127 // =========================================================================
128 // -- 转换为UE4类型 ---------------------------------------------
129 // =========================================================================
130
131#ifdef LIBCARLA_INCLUDED_FROM_UE4
132
133 /// 将向量从厘米转换为米
135 return *this * 1e-2f;
136 }
137
138 /// 将向量从米转换为厘米
140 return *this * 1e2f;
141 }
142
143 /// 将向量转换为UE4引擎的FVector2D类型
144 FVector2D ToFVector2D() const {
145 return FVector2D{x, y};
146 }
147
148#endif // LIBCARLA_INCLUDED_FROM_UE4
149
150 MSGPACK_DEFINE_ARRAY(x, y) // 序列化向量数据,用于消息打包
151 };
152
153} // namespace geom
154} // namespace carla
#define DEVELOPMENT_ASSERT(pred)
Definition Debug.h:89
定义两个嵌套的命名空间:carla和geom。
Vector2D ToMeters() const
将向量从厘米转换为米
friend Vector2D operator/(float lhs, Vector2D rhs)
friend Vector2D operator-(Vector2D lhs, const Vector2D &rhs)
bool operator==(const Vector2D &rhs) const
Vector2D & operator*=(float rhs)
friend Vector2D operator+(Vector2D lhs, const Vector2D &rhs)
Vector2D(float ix, float iy)
定义一个默认构造函数。
friend Vector2D operator/(Vector2D lhs, float rhs)
Vector2D & operator+=(const Vector2D &rhs)
FVector2D ToFVector2D() const
将向量转换为UE4引擎的FVector2D类型
Vector2D MakeUnitVector() const
float SquaredLength() const
bool operator!=(const Vector2D &rhs) const
float x
定义一个名为Vector2D的公共类,用于表示二维向量。
friend Vector2D operator*(Vector2D lhs, float rhs)
Vector2D ToCentimeters() const
将向量从米转换为厘米
Vector2D & operator/=(float rhs)
friend Vector2D operator*(float lhs, Vector2D rhs)
Vector2D & operator-=(const Vector2D &rhs)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139