CARLA
 
载入中...
搜索中...
未找到
CubicPolynomial.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/Debug.h"
10
11#include <array>
12// 定义名为 carla 的命名空间,在这个命名空间下组织相关的代码,有助于避免命名冲突等问题
13namespace carla {
14namespace geom {
15
16 /// 定义一个三次多项式CubicPolynomial类,用于描述和计算三次多项式 f(x) = a + b * x + c * x^2 + d * x^3
18 public:
19 // 定义CubicPolynomial中使用的数值类型,这里选择为double,适用于需要高精度的数学计算
20 using value_type = double;
21
22 // =========================================================================
23 // -- 构造函数 --------------------------------------------------------------
24 // =========================================================================
25 // 默认构造函数,使用=default意味着使用编译器自动生成的默认构造函数,不执行任何操作
26 CubicPolynomial() = default;
27 // 拷贝构造函数,同样使用=default意味着使用编译器自动生成的拷贝构造函数, 它会自动复制源对象的所有成员到新对象中
28 CubicPolynomial(const CubicPolynomial &) = default;
29 // 构造函数,用于创建一个 CubicPolynomial 类的对象,接受四个参数,分别对应三次多项式的系数 a、b、c、d
30 // 例如对于三次多项式 f(x) = a + b * x + c * x^2 + d * x^3,这里传入的参数就是各项的系数,
31 // 并使用初始化列表对类的成员进行初始化,将传入的系数依次放入 _v 数组中,同时将表示偏移量的 _s 初始化为 0.0
32 //无偏移量版本的构造函数
34 const value_type &a,
35 const value_type &b,
36 const value_type &c,
37 const value_type &d)
38 : _v{ {a, b, c, d} },
39 _s(0.0) {}
40
41 //有偏移量版本的构造函数
43 const value_type &a,
44 const value_type &b,
45 const value_type &c,
46 const value_type &d,
47 const value_type &s) // lateral offset
48 : _v{ {a - b * s + c * s * s - d * s * s * s,
49 b - 2 * c * s + 3 * d * s * s,
50 c - 3 * d * s,
51 d} },
52 _s(s) {}
53
54 // =========================================================================
55 // -- 获取值 ----------------------------------------------------------------
56 // =========================================================================
57 // 获取三次多项式中系数 a 的值,返回类型为 value_type(也就是 double 类型),
58 // 该函数为 const 成员函数,表示不会修改类的成员变量,只是获取其值
59 value_type GetA() const {
60 return _v[0];
61 }
62// 获取三次多项式中系数 b 的值,返回类型为 value_type(也就是 double 类型),
63 // 该函数为 const 成员函数,表示不会修改类的成员变量,只是获取其值
64 value_type GetB() const {
65 return _v[1];
66 }
67// 获取三次多项式中系数 c 的值,返回类型为 value_type(也就是 double 类型)
68// 该函数为 const 成员函数,表示不会修改类的成员变量,只是获取其值
69 value_type GetC() const {
70 return _v[2];
71 }
72// 获取三次多项式中系数 d 的值,返回类型为 value_type(也就是 double 类型),
73// 该函数为 const 成员函数,表示不会修改类的成员变量,只是获取其值
74 value_type GetD() const {
75 return _v[3];
76 }
77// 获取表示偏移量(横向偏移量等相关概念)的 _s 的值,返回类型为 value_type(也就是 double 类型),
78// 该函数为 const 成员函数,表示不会修改类的成员变量,只是获取其值
79 value_type GetS() const {
80 return _s;
81 }
82
83 // =========================================================================
84 // -- 设置值 ---------------------------------------------------------------
85 // =========================================================================
86 // 设置三次多项式的系数以及偏移量的值,接受五个参数,分别对应调整后的系数 a、b、c、d 和偏移量 s,
87 // 按照特定的数学计算方式(与前面构造函数中根据偏移量调整系数的方式一致)更新 _v 数组中的系数值,并将传入的偏移量 s 赋值给 _s
88 void Set(
89 const value_type &a,
90 const value_type &b,
91 const value_type &c,
92 const value_type &d,
93 const value_type &s) { // lateral offset
94 _v = { a - b * s + c * s * s - d * s * s * s,
95 b - 2 * c * s + 3 * d * s * s,
96 c - 3 * d * s,
97 d };
98 _s = s;
99 }
100 // 设置三次多项式的系数的值,接受四个参数,分别对应系数 a、b、c、d,
101 // 将传入的系数依次放入 _v 数组中,并将表示偏移量的 _s 初始化为 0.0,用于在不需要考虑偏移量或者重置系数时使用
102 void Set(
103 const value_type &a,
104 const value_type &b,
105 const value_type &c,
106 const value_type &d) {
107 //将系数a,b,c,d放入 _v 数组
108 _v = {a, b, c, d};
109 //将偏移量 _s 设为 0.0
110 _s = 0.0;
111 }
112
113 // =========================================================================
114 // -- 评估方法 -------------------------------------------------------------
115 // =========================================================================
116
117 /// 评估 f(x) = a + bx + cx^2 + dx^3
119 // return _v[0] + _v[1] * (x) + _v[2] * (x * x) + _v[3] * (x * x * x);
120 return _v[0] + x * (_v[1] + x * (_v[2] + x * _v[3]));
121 }
122
123 /// 使用 df/dx = b + 2cx + 3dx^2 计算正切值
124 value_type Tangent(const value_type &x) const {
125 return _v[1] + x * (2 * _v[2] + x * 3 * _v[3]);
126 }
127
128 // =========================================================================
129 // -- 算术运算符 ------------------------------------------------------------
130 // =========================================================================
131 // 复合赋值运算符 += 的重载函数,用于实现当前的 CubicPolynomial 对象与另一个同类型对象相加,并将结果赋值给当前对象,
132 // 通过遍历系数数组 _v,将对应位置的系数相加,实现多项式的相加操作,最后返回当前对象的引用,以便支持连续的复合赋值操作(如 a += b += c; 这样的语法形式)
134 for (auto i = 0u; i < _v.size(); ++i) {
135 _v[i] += rhs._v[i];
136 }
137 return *this;
138 }
139 // 加法运算符 + 的重载函数,以友元函数的形式实现,它创建一个当前对象的副本(通过传值参数 lhs),
140 // 然后调用 += 运算符重载函数将传入的另一个对象 rhs 与之相加,最后返回相加后的结果对象,这样就实现了两个 CubicPolynomial 对象相加的操作
142 lhs += rhs;
143 return lhs;
144 }
145 // 复合赋值运算符 *= 的重载函数,用于实现当前的 CubicPolynomial 对象与一个数值(value_type 类型,也就是 double 类型)相乘,并将结果赋值给当前对象,
146 // 通过遍历系数数组 _v,将每个系数都与传入的数值 rhs 相乘,实现多项式的数乘操作,最后返回当前对象的引用,以便支持连续的复合赋值操作(如 a *= b *= c; 这样的语法形式)
148 for (auto i = 0u; i < _v.size(); ++i) {
149 _v[i] *= rhs;
150 }
151 return *this;
152 }
153 // 乘法运算符 * 的重载函数,以友元函数的形式实现,它创建一个当前对象的副本(通过传值参数 lhs),
154 // 然后调用 *= 运算符重载函数将传入的数值 rhs 与之相乘,最后返回相乘后的结果对象,这样就实现了一个 CubicPolynomial 对象与一个数值相乘的操作
156 lhs *= rhs;
157 return lhs;
158 }
159 // 乘法运算符 * 的另一种重载形式,同样以友元函数的形式实现,用于实现一个数值与一个 CubicPolynomial 对象相乘的操作,
160 // 它通过将传入的数值 lhs 与 rhs 对象调用 *= 运算符重载函数相乘,最后返回相乘后的结果对象,实现了乘法的交换律,使得数值与多项式相乘的顺序不影响结果
162 rhs *= lhs;
163 return rhs;
164 }
165
166 private:
167
168 // =========================================================================
169 // -- 私有数据成员 ----------------------------------------------------------
170 // =========================================================================
171
172 // a - 截距 elevation
173 // b - 斜率
174 // c - 垂直曲率 vertical curvature
175 // d - 曲率变化
176 std::array<value_type, 4> _v = {0.0, 0.0, 0.0, 0.0};
177
178 // s - 距离
180 };
181
182} // namespace geom
183} // namespace carla
定义一个三次多项式CubicPolynomial类,用于描述和计算三次多项式 f(x) = a + b * x + c * x^2 + d * x^3
CubicPolynomial & operator*=(const value_type &rhs)
CubicPolynomial & operator+=(const CubicPolynomial &rhs)
CubicPolynomial(const value_type &a, const value_type &b, const value_type &c, const value_type &d, const value_type &s)
void Set(const value_type &a, const value_type &b, const value_type &c, const value_type &d)
friend CubicPolynomial operator*(const value_type &lhs, CubicPolynomial rhs)
value_type Evaluate(const value_type &x) const
评估 f(x) = a + bx + cx^2 + dx^3
value_type Tangent(const value_type &x) const
使用 df/dx = b + 2cx + 3dx^2 计算正切值
void Set(const value_type &a, const value_type &b, const value_type &c, const value_type &d, const value_type &s)
friend CubicPolynomial operator+(CubicPolynomial lhs, const CubicPolynomial &rhs)
friend CubicPolynomial operator*(CubicPolynomial lhs, const value_type &rhs)
CubicPolynomial(const value_type &a, const value_type &b, const value_type &c, const value_type &d)
CubicPolynomial(const CubicPolynomial &)=default
std::array< value_type, 4 > _v
CARLA模拟器的主命名空间。
Definition Carla.cpp:139