forked from pageldev/libOpenDRIVE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RefLine.cpp
123 lines (100 loc) · 3.36 KB
/
RefLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "RefLine.h"
#include "Geometries/RoadGeometry.h"
#include "Math.hpp"
#include "Utils.hpp"
#include <functional>
#include <utility>
namespace odr
{
RefLine::RefLine(double length) : length(length) {}
ConstRoadGeometrySet RefLine::get_geometries() const
{
ConstRoadGeometrySet geometries;
for (const auto& s0_geometry : this->s0_to_geometry)
geometries.insert(s0_geometry.second);
return geometries;
}
RoadGeometrySet RefLine::get_geometries()
{
RoadGeometrySet geometries;
for (const auto& s0_geometry : this->s0_to_geometry)
geometries.insert(s0_geometry.second);
return geometries;
}
std::shared_ptr<const RoadGeometry> RefLine::get_geometry(double s) const
{
if (this->s0_to_geometry.size() > 0)
{
auto target_geom_iter = this->s0_to_geometry.upper_bound(s);
if (target_geom_iter != s0_to_geometry.begin())
target_geom_iter--;
return target_geom_iter->second;
}
return nullptr;
}
Vec3D RefLine::get_xyz(double s) const
{
std::shared_ptr<const RoadGeometry> geom = this->get_geometry(s);
Vec2D pt_xy{0, 0};
if (geom)
pt_xy = geom->get_xy(s);
return Vec3D{pt_xy[0], pt_xy[1], this->elevation_profile.get(s)};
}
Vec3D RefLine::get_grad(double s) const
{
std::shared_ptr<const RoadGeometry> geom = this->get_geometry(s);
Vec2D d_xy{0, 0};
if (geom)
d_xy = geom->get_grad(s);
return Vec3D{d_xy[0], d_xy[1], this->elevation_profile.get_grad(s)};
}
double RefLine::match(double x, double y) const
{
std::function<double(double)> f_dist = [&](const double s)
{
const Vec3D pt = this->get_xyz(s);
return euclDistance(Vec2D{pt[0], pt[1]}, {x, y});
};
return golden_section_search<double>(f_dist, 0.0, length, 1e-2);
}
Line3D RefLine::get_line(double s_start, double s_end, double eps) const
{
std::set<double> s_vals = this->approximate_linear(eps, s_start, s_end);
Line3D out_line;
for (const double& s : s_vals)
out_line.push_back(this->get_xyz(s));
return out_line;
}
std::set<double> RefLine::approximate_linear(double eps, double s_start, double s_end) const
{
if ((s_start == s_end) || this->s0_to_geometry.empty())
return {};
auto s_end_geom_iter = this->s0_to_geometry.lower_bound(s_end);
auto s_start_geom_iter = this->s0_to_geometry.upper_bound(s_start);
if (s_start_geom_iter != s0_to_geometry.begin())
s_start_geom_iter--;
std::vector<double> s_vals{s_start};
for (auto s0_geom_iter = s_start_geom_iter; s0_geom_iter != s_end_geom_iter; s0_geom_iter++)
{
const std::set<double> s_vals_geom = s0_geom_iter->second->approximate_linear(eps);
if (s_vals_geom.size() < 2)
throw std::runtime_error("expected at least two sample points");
for (const double& s : s_vals_geom)
{
if (s > s_start && s < s_end)
s_vals.push_back(s);
}
if (std::next(s0_geom_iter) != s_end_geom_iter)
s_vals.pop_back();
}
std::set<double> s_vals_elevation = this->elevation_profile.approximate_linear(eps, s_start, s_end);
for (const double& s : s_vals_elevation)
{
if (s > s_start && s < s_end)
s_vals.push_back(s);
}
s_vals.push_back(s_end);
std::set<double> s_vals_set(s_vals.begin(), s_vals.end());
return s_vals_set;
}
} // namespace odr