CARLA
 
载入中...
搜索中...
未找到
Geometry.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// 包含carla/geom/Location.h头文件
10#include "carla/geom/Location.h"
11// 包含carla/geom/Math.h头文件
12#include "carla/geom/Math.h"
13// 包含carla/geom/CubicPolynomial.h头文件
15// 包含carla/geom/Rtree.h头文件
16#include "carla/geom/Rtree.h"
17
18// 定义命名空间carla,在这个命名空间下包含road和其他相关的定义
19namespace carla {
20// 定义road命名空间,在carla命名空间下,用于与道路相关的定义
21namespace road {
22// 定义element命名空间,在road命名空间下,用于道路元素相关的定义
23namespace element {
24
25 // 定义一个枚举类型GeometryType,表示几何类型,是无符号整数类型
26 enum class GeometryType : unsigned int {
27 // 直线类型
28 LINE,
29 // 弧线类型
30 ARC,
31 // 螺旋线类型
32 SPIRAL,
33 // 三次多项式(可能表示曲线)类型
34 POLY3,
35 // 带参数的三次多项式(可能表示曲线)类型
37 };
38
39 // 定义一个结构体DirectedPoint,表示有方向的点
41 // 默认构造函数,初始化location为(0, 0, 0),tangent为0
43 : location(0, 0, 0),
44 tangent(0) {}
45 // 构造函数,根据给定的geom::Location和tangent值初始化
46 DirectedPoint(const geom::Location &l, double t)
47 : location(l),
48 tangent(t) {}
49 // 构造函数,根据给定的坐标和tangent值初始化
50 DirectedPoint(float x, float y, float z, double t)
51 : location(x, y, z),
52 tangent(t) {}
53
54 // 点的坐标位置
55 geom::Location location = {0.0f, 0.0f, 0.0f};
56 // 切线方向(以弧度表示)
57 double tangent = 0.0; // [radians]
58 // 俯仰角(以弧度表示)
59 double pitch = 0.0; // [radians]
60
61 // 应用横向偏移量的函数,函数体未实现
62 void ApplyLateralOffset(float lateral_offset);
63
64 // 重载==运算符,用于比较两个DirectedPoint是否相等
65 friend bool operator==(const DirectedPoint &lhs, const DirectedPoint &rhs) {
66 return (lhs.location == rhs.location) && (lhs.tangent == rhs.tangent);
67 }
68 };
69
70 // 定义一个抽象基类Geometry,表示几何形状
71 class Geometry {
72 public:
73 // 获取几何类型的函数
75 return _type;
76 }
77 // 获取几何形状长度的函数
78 double GetLength() const {
79 return _length;
80 }
81 // 获取起始偏移量的函数
82 double GetStartOffset() const {
84 }
85 // 获取起始方向(以弧度表示)的函数
86 double GetHeading() const {
87 return _heading;
88 }
89
90 // 获取起始位置的引用
94
95 // 虚析构函数,用于多态删除对象时正确释放资源
96 virtual ~Geometry() = default;
97
98 // 纯虚函数,根据距离计算位置,需要在派生类中实现
99 virtual DirectedPoint PosFromDist(double dist) const = 0;
100
101 // 纯虚函数,计算到给定点的距离,返回一对浮点数,需要在派生类中实现
102 virtual std::pair<float, float> DistanceTo(const geom::Location &p) const = 0;
103
104 protected:
105 // 受保护的构造函数,用于初始化几何形状的基本属性
107 GeometryType type,
108 double start_offset,
109 double length,
110 double heading,
111 const geom::Location &start_pos)
112 : _type(type),
113 _length(length),
114 _start_position_offset(start_offset),
115 _heading(heading),
116 _start_position(start_pos) {}
117
118 protected:
119 GeometryType _type; // 几何类型
120 double _length; // 道路部分的长度(单位:米)
121
122 double _start_position_offset; // s - 偏移量(单位:米)
123 double _heading; // 起始方向(单位:弧度)
124
125 geom::Location _start_position; // 起始位置(单位:米)
126 };
127
128 // 定义表示直线的几何形状类,继承自Geometry类
129 class GeometryLine final : public Geometry {
130 public:
131 // 构造函数,用于初始化直线几何形状的属性
133 double start_offset,
134 double length,
135 double heading,
136 const geom::Location &start_pos)
137 : Geometry(GeometryType::LINE, start_offset, length, heading, start_pos) {}
138
139 // 重写PosFromDist函数,根据距离计算直线上的位置
140 DirectedPoint PosFromDist(double dist) const override;
141
142 // 重写DistanceTo函数,计算到给定点的距离
143 /// 返回一个包含以下内容的对(pair):
144 ///- @b first(第一个元素):从形状起点到这条直线上最近点的距离。
145 ///- @b second(第二个元素):从这条直线上最近点到 p 点的欧几里得距离。
146 /// @param p 用于计算距离的点
147 std::pair<float, float> DistanceTo(const geom::Location &p) const override {
149 p,
151 PosFromDist(_length).location);
152 }
153
154 };
155
156 // 定义表示弧线的几何形状类,继承自Geometry类
157 class GeometryArc final : public Geometry {
158 public:
159 // 构造函数,用于初始化弧线几何形状的属性,包括曲率
161 double start_offset,
162 double length,
163 double heading,
164 const geom::Location &start_pos,
165 double curv)
166 : Geometry(GeometryType::ARC, start_offset, length, heading, start_pos),
167 _curvature(curv) {}
168
169 // 重写PosFromDist函数,根据距离计算弧线上的位置
170 DirectedPoint PosFromDist(double dist) const override;
171
172 // 重写DistanceTo函数,计算到给定点的距离
173 /// 返回一个包含以下内容的数对:
174 ///- @b first(第一个元素):从形状起始点到该弧线最近点的距离。
175 ///- @b second(第二个元素):从该弧线最近点到 p 点的欧几里得距离。
176 /// @param p 为用于计算距离的点。
177 std::pair<float, float> DistanceTo(const geom::Location &p) const override {
179 p,
181 static_cast<float>(_length),
182 static_cast<float>(_heading),
183 static_cast<float>(_curvature));
184 }
185
186 // 获取曲率的函数
187 double GetCurvature() const {
188 return _curvature;
189 }
190
191 private:
192 // 弧线的曲率
194 };
195
196 // 定义表示螺旋线的几何形状类,继承自Geometry类
197 class GeometrySpiral final : public Geometry {
198 public:
199 // 构造函数,用于初始化螺旋线几何形状的属性,包括曲线起始和结束的曲率
200 // 构造函数名为GeometrySpiral,接受以下参数
202 // 起始偏移量,double类型
203 double start_offset,
204 // 螺旋线长度,double类型
205 double length,
206 // 起始方向(弧度表示),double类型
207 double heading,
208 // 起始位置,类型为const geom::Location &,表示不可修改的geom::Location类型的引用
209 const geom::Location &start_pos,
210 // 曲线起始曲率,double类型
211 double curv_s,
212 // 曲线结束曲率,double类型
213 double curv_e)
214 // 使用初始化列表初始化基类Geometry部分的属性
215 // 将GeometryType::SPIRAL作为几何类型,以及传入的起始偏移量、长度、起始方向、起始位置等参数来初始化基类部分
216 : Geometry(GeometryType::SPIRAL, start_offset, length, heading, start_pos),
217 // 初始化本类中的_curve_start成员变量,将传入的curv_s赋值给它,表示曲线起始曲率
218 _curve_start(curv_s),
219 // 初始化本类中的_curve_end成员变量,将传入的curv_e赋值给它,表示曲线结束曲率
220 _curve_end(curv_e) {}
221
222 // 获取曲线起始曲率的函数
223 double GetCurveStart() {
224 return _curve_start;
225 }
226
227 // 获取曲线结束曲率的函数
228 double GetCurveEnd() {
229 return _curve_end;
230 }
231
232 // 重写PosFromDist函数,根据距离计算螺旋线上的位置
233 DirectedPoint PosFromDist(double dist) const override;
234
235 // 重写DistanceTo函数,计算到给定点的距离(函数体未完整实现)
236 std::pair<float, float> DistanceTo(const geom::Location &) const override;
237
238 private:
239 // 曲线起始曲率
241 // 曲线结束曲率
243 };
244
245 // 定义表示三次多项式曲线的几何形状类,继承自Geometry类
246 class GeometryPoly3 final : public Geometry {
247 public:
248 // 构造函数,用于初始化三次多项式曲线几何形状的属性,包括多项式系数
249 // 构造函数名为GeometryPoly3,接受以下参数
251 // 起始偏移量,double类型
252 double start_offset,
253 // 曲线长度,double类型
254 double length,
255 // 起始方向(以弧度表示),double类型
256 double heading,
257 // 起始位置,类型为const geom::Location &,表示不可修改的geom::Location类型的引用
258 const geom::Location &start_pos,
259 // 三次多项式的系数a,double类型
260 double a,
261 // 三次多项式的系数b,double类型
262 double b,
263 // 三次多项式的系数c,double类型
264 double c,
265 // 三次多项式的系数d,double类型
266 double d)
267 // 使用初始化列表初始化基类Geometry部分的属性
268 // 将GeometryType::POLY3作为几何类型,以及传入的起始偏移量、长度、起始方向、起始位置等参数来初始化基类部分
269 : Geometry(GeometryType::POLY3, start_offset, length, heading, start_pos),
270 // 初始化本类中的_a成员变量,将传入的a赋值给它,表示三次多项式的系数a
271 _a(a),
272 // 初始化本类中的_b成员变量,将传入的b赋值给它,表示三次多项式的系数b
273 _b(b),
274 // 初始化本类中的_c成员变量,将传入的c赋值给它,表示三次多项式的系数c
275 _c(c),
276 // 初始化本类中的_d成员变量,将传入的d赋值给它,表示三次多项式的系数d
277 _d(d) {
278 // 调用_poly对象(类型为geom::CubicPolynomial)的Set函数
279 // 传入三次多项式的系数a、b、c、d来设置多项式
280 _poly.Set(a, b, c, d);
281 // 调用PreComputeSpline函数,函数体未实现,可能用于预计算样条相关的操作
283 }
284 // 获取系数a的函数
285 double Geta() const {
286 return _a;
287 }
288 // 获取系数b的函数
289 double Getb() const {
290 return _b;
291 }
292 // 获取系数c的函数
293 double Getc() const {
294 return _c;
295 }
296 // 获取系数d的函数
297 double Getd() const {
298 return _d;
299 }
300
301 // 重写PosFromDist函数,根据距离计算三次多项式曲线上的位置
302 DirectedPoint PosFromDist(double dist) const override;
303
304 // 重写DistanceTo函数,计算到给定点的距离(函数体未完整实现)
305 std::pair<float, float> DistanceTo(const geom::Location &) const override;
306
307 private:
308 // 三次多项式对象
310
311 // 多项式系数
312 double _a;
313 double _b;
314 double _c;
315 double _d;
316
317 // 定义用于R - tree的值结构体
318 struct RtreeValue {
319 double u = 0;
320 double v = 0;
321 double s = 0;
322 double t = 0;
323 };
324 // 定义R - tree类型
326 // 定义R - tree元素类型
328 // R - tree对象
330 // 预计算样条函数,函数体未实现
331 void PreComputeSpline();
332 };
333
334 // 定义表示带参数的三次多项式曲线的几何形状类,继承自Geometry类
335 class GeometryParamPoly3 final : public Geometry {
336 public:
337 // 构造函数,用于初始化带参数的三次多项式曲线几何形状的属性,包括多项式系数等
338// 定义名为GeometryParamPoly3的函数(从代码结构看很可能是一个构造函数,用于初始化对应类的对象),它接收一系列参数来构建特定的几何对象相关信息
340 double start_offset,// 起始偏移量,可能表示几何形状在某个参考方向上的起始位置的偏移数值,具体含义取决于使用场景
341 double length,// 几何形状的长度,比如可以是一段曲线、线段等几何元素沿着某个方向延伸的长度值
342 double heading,// 朝向角度,通常用于表示几何形状的方向,例如在平面坐标系中与某个基准坐标轴所成的角度
343 const geom::Location &start_pos,// 起始位置,通过geom::Location类型传入,包含了具体的坐标等位置信息,代表几何形状起始的地点
344 double aU,// 多项式U(可能是用于描述几何形状在U方向上特征的多项式系数,具体取决于所在几何模型的定义)的三次项系数
345 double bU,// 多项式U的二次项系数
346 double cU,// 多项式U一次项系数
347 double dU,// 多项式U的常数项系数
348 double aV,// 多项式V(同理,可能用于描述几何形状在V方向上特征)的三次项系数
349 double bV, // 多项式V的二次项系数
350 double cV, // 多项式V的一次项系数
351 double dV, // 多项式V的常数项系数
352 bool arcLength)
353// 一个布尔值,用于指示是否基于弧长进行相关计算(比如在涉及曲线的几何处理中,弧长相关设置会影响计算方式等)
354 // 通过初始化列表初始化基类Geometry,调用基类的构造函数,传入几何类型(这里是GeometryType::POLY3PARAM表示是特定的多项式参数定义的几何类型)、起始偏移量、长度、朝向以及起始位置等参数
355 : Geometry(GeometryType::POLY3PARAM, start_offset, length, heading, start_pos),
356// 初始化成员变量_aU,将传入的参数aU的值赋给它,以下同理,分别初始化各个对应的成员变量
357 _aU(aU),
358 _bU(bU),
359 _cU(cU),
360 _dU(dU),
361 _aV(aV),
362 _bV(bV),
363 _cV(cV),
364 _dV(dV),
365 _arcLength(arcLength) {
366 // 使用传入的系数来设置_polyU这个多项式对象(应该是类内封装用于处理U方向相关几何计算的多项式表示),调用其Set方法传入相应系数
367 _polyU.Set(aU, bU, cU, dU);
368// 同理,使用传入的系数设置_polyV这个多项式对象,用于V方向的相关几何计算
369 _polyV.Set(aV, bV, cV, dV);
370// 调用PreComputeSpline函数(应该是用于预先计算样条相关的数据或者进行一些初始化计算,为后续几何处理做准备,具体功能要看其函数实现)
372 }
373
374 // 获取系数aU的函数
375 double GetaU() const {
376 return _aU;
377 }
378 // 获取系数bU的函数
379 double GetbU() const {
380 return _bU;
381 }
382 // 获取系数cU的函数
383 double GetcU() const {
384 return _cU;
385 }
386 // 获取系数dU的函数
387 double GetdU() const {
388 return _dU;
389 }
390 // 获取系数aV的函数
391 double GetaV() const {
392 return _aV;
393 }
394 // 获取系数bV的函数
395 double GetbV() const {
396 return _bV;
397 }
398 // 获取系数cV的函数
399 double GetcV() const {
400 return _cV;
401 }
402 // 获取系数dV的函数
403 double GetdV() const {
404 return _dV;
405 }
406
407 // 重写PosFromDist函数,根据距离计算带参数的三次多项式曲线上的位置
408 DirectedPoint PosFromDist(double dist) const override;
409
410 // 重写DistanceTo函数,计算到给定点的距离(函数体未完整实现)
411 std::pair<float, float> DistanceTo(const geom::Location &) const override;
412
413 private:
414 // 用于U方向的三次多项式对象
416 // 用于V方向的三次多项式对象
418 // 多项式系数
419 double _aU;
420 double _bU;
421 double _cU;
422 double _dU;
423 double _aV;
424 double _bV;
425 double _cV;
426 double _dV;
427 // 是否为弧长相关的标志
429
430 // 定义用于R - tree的值结构体
431 struct RtreeValue {
432 double u = 0;
433 double v = 0;
434 double s = 0;
435 double t_u = 0;
436 double t_v = 0;
437 };
438 // 定义R - tree类型
440 // 定义R - tree元素类型
442 // R - tree对象
444 // 预计算样条函数,函数体未实现
445 void PreComputeSpline();
446 };
447
448} // namespace element
449} // namespace road
450} // namespace carla
定义一个三次多项式CubicPolynomial类,用于描述和计算三次多项式 f(x) = a + b * x + c * x^2 + d * x^3
void Set(const value_type &a, const value_type &b, const value_type &c, const value_type &d, const value_type &s)
static std::pair< float, float > DistanceSegmentToPoint(const Vector3D &p, const Vector3D &v, const Vector3D &w)
计算点到线段的距离 返回一个包含:
Definition Math.cpp:24
static std::pair< float, float > DistanceArcToPoint(Vector3D p, Vector3D start_pos, float length, float heading, float curvature)
计算点到弧的距离 返回一个包含:
Definition Math.cpp:50
std::pair< BSegment, std::pair< RtreeValue, RtreeValue > > TreeElement
Definition Rtree.h:95
std::pair< float, float > DistanceTo(const geom::Location &p) const override
返回一个包含以下内容的数对:
Definition Geometry.h:177
DirectedPoint PosFromDist(double dist) const override
Definition Geometry.cpp:76
GeometryArc(double start_offset, double length, double heading, const geom::Location &start_pos, double curv)
Definition Geometry.h:160
GeometryLine(double start_offset, double length, double heading, const geom::Location &start_pos)
Definition Geometry.h:132
DirectedPoint PosFromDist(double dist) const override
Definition Geometry.cpp:59
std::pair< float, float > DistanceTo(const geom::Location &p) const override
返回一个包含以下内容的对(pair):
Definition Geometry.h:147
DirectedPoint PosFromDist(double dist) const override
Definition Geometry.cpp:268
std::pair< float, float > DistanceTo(const geom::Location &) const override
Definition Geometry.cpp:303
GeometryParamPoly3(double start_offset, double length, double heading, const geom::Location &start_pos, double aU, double bU, double cU, double dU, double aV, double bV, double cV, double dV, bool arcLength)
Definition Geometry.h:339
geom::CubicPolynomial _poly
Definition Geometry.h:309
std::pair< float, float > DistanceTo(const geom::Location &) const override
Definition Geometry.cpp:212
GeometryPoly3(double start_offset, double length, double heading, const geom::Location &start_pos, double a, double b, double c, double d)
Definition Geometry.h:250
DirectedPoint PosFromDist(double dist) const override
Definition Geometry.cpp:180
std::pair< float, float > DistanceTo(const geom::Location &) const override
Definition Geometry.cpp:172
DirectedPoint PosFromDist(double dist) const override
Definition Geometry.cpp:119
GeometrySpiral(double start_offset, double length, double heading, const geom::Location &start_pos, double curv_s, double curv_e)
Definition Geometry.h:201
Geometry(GeometryType type, double start_offset, double length, double heading, const geom::Location &start_pos)
Definition Geometry.h:106
geom::Location _start_position
Definition Geometry.h:125
double GetStartOffset() const
Definition Geometry.h:82
virtual DirectedPoint PosFromDist(double dist) const =0
virtual std::pair< float, float > DistanceTo(const geom::Location &p) const =0
GeometryType GetType() const
Definition Geometry.h:74
const geom::Location & GetStartPosition()
Definition Geometry.h:91
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
void ApplyLateralOffset(float lateral_offset)
Definition Geometry.cpp:45
DirectedPoint(const geom::Location &l, double t)
Definition Geometry.h:46
DirectedPoint(float x, float y, float z, double t)
Definition Geometry.h:50
friend bool operator==(const DirectedPoint &lhs, const DirectedPoint &rhs)
Definition Geometry.h:65