CARLA
 
载入中...
搜索中...
未找到
Math.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#include "carla/geom/Vector3D.h" // 引入三维向量相关的头文件
11
12#include <cmath> // 引入数学库
13#include <type_traits> // 引入类型特征库
14#include <utility> // 引入通用工具库
15
16namespace carla { // 定义 carla 命名空间
17namespace geom { // 定义 geom 子命名空间
18
19 class Rotation; // 前向声明 Rotation 类
20
21 class Math { // 定义 Math 类
22 public:
23
24 // 返回浮点数类型的 π 值
25 template <typename T>
26 static constexpr T Pi() {
27 static_assert(std::is_floating_point<T>::value, "type must be floating point"); // 确保 T 是浮点数类型
28 return static_cast<T>(3.14159265358979323846264338327950288); // 返回 π 的值
29 }
30
31 // 返回浮点数类型的 2π 值
32 template <typename T>
33 static constexpr T Pi2() {
34 static_assert(std::is_floating_point<T>::value, "type must be floating point"); // 确保 T 是浮点数类型
35 return static_cast<T>(static_cast<T>(2) * Pi<T>()); // 返回 2π 的值
36 }
37
38 // 将弧度转换为度数
39 template <typename T>
40 static constexpr T ToDegrees(T rad) {
41 static_assert(std::is_floating_point<T>::value, "type must be floating point"); // 确保 T 是浮点数类型
42 return rad * (T(180.0) / Pi<T>()); // 转换弧度到度数
43 }
44
45 // 将度数转换为弧度
46 template <typename T>
47 static constexpr T ToRadians(T deg) {
48 static_assert(std::is_floating_point<T>::value, "type must be floating point"); // 确保 T 是浮点数类型
49 return deg * (Pi<T>() / T(180.0)); // 转换度数到弧度
50 }
51
52 // 限制值在指定范围内
53 template <typename T>
54 static T Clamp(T a, T min = T(0), T max = T(1)) {
55 return std::min(std::max(a, min), max); // 返回限制后的值
56 }
57
58 // 计算平方值
59 template <typename T>
60 static T Square(const T &a) {
61 return a * a; // 返回 a 的平方
62 }
63
64 // 计算两个三维向量的叉积
65 static auto Cross(const Vector3D &a, const Vector3D &b) {
66 return Vector3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); // 返回叉积结果
67 }
68
69 // 计算两个三维向量的点积
70 static auto Dot(const Vector3D &a, const Vector3D &b) {
71 return a.x * b.x + a.y * b.y + a.z * b.z; // 返回点积结果
72 }
73
74 // 计算两个二维向量的点积
75 static auto Dot2D(const Vector3D &a, const Vector3D &b) {
76 return a.x * b.x + a.y * b.y; // 返回二维点积结果
77 }
78
79 // 计算两个三维点之间的距离平方
80 static auto DistanceSquared(const Vector3D &a, const Vector3D &b) {
81 return Square(b.x - a.x) + Square(b.y - a.y) + Square(b.z - a.z); // 返回距离平方
82 }
83
84 // 计算两个二维点之间的距离平方
85 static auto DistanceSquared2D(const Vector3D &a, const Vector3D &b) {
86 return Square(b.x - a.x) + Square(b.y - a.y); // 返回二维距离平方
87 }
88
89 // 计算两个三维点之间的距离
90 static auto Distance(const Vector3D &a, const Vector3D &b) {
91 return std::sqrt(DistanceSquared(a, b)); // 返回距离
92 }
93
94 // 计算两个二维点之间的距离
95 static auto Distance2D(const Vector3D &a, const Vector3D &b) {
96 return std::sqrt(DistanceSquared2D(a, b)); // 返回二维距离
97 }
98
99 // 线性插值计算
100 static float LinearLerp(float a, float b, float f) {
101 return a * (1.0f - f) + (b * f); // 返回插值结果
102 }
103
104 /// 返回两个向量之间的夹角(弧度)
105 static double GetVectorAngle(const Vector3D &a, const Vector3D &b);
106
107 /// 计算点到线段的距离
108 /// 返回一个包含:
109 /// - @b first: 从 v 到 p' 的距离,其中 p' = p 投影到线段 (w - v) 上
110 /// - @b second: 从 p 到 p' 的欧几里得距离
111 /// @param p 要计算距离的点
112 /// @param v 线段的第一个点
113 /// @param w 线段的第二个点
114 static std::pair<float, float> DistanceSegmentToPoint(
115 const Vector3D &p,
116 const Vector3D &v,
117 const Vector3D &w);
118
119 /// 计算点到弧的距离
120 /// 返回一个包含:
121 /// - @b first: 从 start_pos 到 p' 的弧长距离,其中 p' = p 投影到弧上
122 /// - @b second: 从 p 到 p' 的欧几里得距离
123 static std::pair<float, float> DistanceArcToPoint(
124 Vector3D p,
125 Vector3D start_pos,
126 float length,
127 float heading, // [radians]
128 float curvature);
129
130 // 在原点旋转二维点
131 static Vector3D RotatePointOnOrigin2D(Vector3D p, float angle);
132
133 /// 计算指向 @a rotation 的 X 轴的单位向量
134 static Vector3D GetForwardVector(const Rotation &rotation);
135
136 /// 计算指向 @a rotation 的 Y 轴的单位向量
137 static Vector3D GetRightVector(const Rotation &rotation);
138
139 /// 计算指向 @a rotation 的 Z 轴的单位向量
140 static Vector3D GetUpVector(const Rotation &rotation);
141
142 // 生成从 a 到 b 的连续整数向量
143 static std::vector<int> GenerateRange(int a, int b);
144
145 };
146} // namespace geom
147} // namespace carla
double min(double v1, double v2)
返回两个数中的较小值
Definition Simplify.h:591
static auto Cross(const Vector3D &a, const Vector3D &b)
Definition Math.h:65
static auto DistanceSquared2D(const Vector3D &a, const Vector3D &b)
Definition Math.h:85
static Vector3D GetRightVector(const Rotation &rotation)
计算指向 rotation 的 Y 轴的单位向量
Definition Math.cpp:158
static constexpr T Pi2()
Definition Math.h:33
static constexpr T ToRadians(T deg)
Definition Math.h:47
static float LinearLerp(float a, float b, float f)
Definition Math.h:100
static double GetVectorAngle(const Vector3D &a, const Vector3D &b)
返回两个向量之间的夹角(弧度)
Definition Math.cpp:16
static Vector3D GetUpVector(const Rotation &rotation)
计算指向 rotation 的 Z 轴的单位向量
Definition Math.cpp:173
static auto Distance2D(const Vector3D &a, const Vector3D &b)
Definition Math.h:95
static std::vector< int > GenerateRange(int a, int b)
Definition Math.cpp:191
static Vector3D RotatePointOnOrigin2D(Vector3D p, float angle)
Definition Math.cpp:138
static auto DistanceSquared(const Vector3D &a, const Vector3D &b)
Definition Math.h:80
static T Clamp(T a, T min=T(0), T max=T(1))
Definition Math.h:54
static auto Dot(const Vector3D &a, const Vector3D &b)
Definition Math.h:70
static Vector3D GetForwardVector(const Rotation &rotation)
计算指向 rotation 的 X 轴的单位向量
Definition Math.cpp:147
static std::pair< float, float > DistanceSegmentToPoint(const Vector3D &p, const Vector3D &v, const Vector3D &w)
计算点到线段的距离 返回一个包含:
Definition Math.cpp:24
static constexpr T Pi()
Definition Math.h:26
static T Square(const T &a)
Definition Math.h:60
static auto Distance(const Vector3D &a, const Vector3D &b)
Definition Math.h:90
static constexpr T ToDegrees(T rad)
Definition Math.h:40
static auto Dot2D(const Vector3D &a, const Vector3D &b)
Definition Math.h:75
static std::pair< float, float > DistanceArcToPoint(Vector3D p, Vector3D start_pos, float length, float heading, float curvature)
计算点到弧的距离 返回一个包含:
Definition Math.cpp:50
geom::Rotation Rotation
CARLA模拟器的主命名空间。
Definition Carla.cpp:139