Apollo 11.0
自动驾驶开放平台
interpolation_2d.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
17
18#include <cmath>
19
20namespace apollo {
21namespace dreamview {
22
23const double kDoubleEpsilon = 1e-6;
24
26 if (xyz.empty()) {
27 return false;
28 }
29 for (const auto& t : xyz) {
30 _xyz[std::get<0>(t)][std::get<2>(t)] = std::get<1>(t);
31 }
32 return true;
33}
34
35double Interpolation2D::Interpolate(const KeyType& xy) const {
36 double max_x = _xyz.rbegin()->first;
37 double min_x = _xyz.begin()->first;
38 if (xy.first >= max_x - kDoubleEpsilon) {
39 return InterpolateYz(_xyz.rbegin()->second, xy.second);
40 }
41 if (xy.first <= min_x + kDoubleEpsilon) {
42 return InterpolateYz(_xyz.begin()->second, xy.second);
43 }
44
45 auto itr_after = _xyz.lower_bound(xy.first);
46 auto itr_before = itr_after;
47 --itr_before;
48
49 double x_before = itr_before->first;
50 double z_before = InterpolateYz(itr_before->second, xy.second);
51 double x_after = itr_after->first;
52 double z_after = InterpolateYz(itr_after->second, xy.second);
53
54 double x_diff_before = std::fabs(xy.first - x_before);
55 double x_diff_after = std::fabs(xy.first - x_after);
56
57 return InterpolateValue(z_before, x_diff_before, z_after, x_diff_after);
58}
59
60double Interpolation2D::InterpolateYz(const std::map<double, double>& yz_table,
61 double y) const {
62 double max_y = yz_table.rbegin()->first;
63 double min_y = yz_table.begin()->first;
64 if (y >= max_y - kDoubleEpsilon) {
65 return yz_table.rbegin()->second;
66 }
67 if (y <= min_y + kDoubleEpsilon) {
68 return yz_table.begin()->second;
69 }
70
71 auto itr_after = yz_table.lower_bound(y);
72 auto itr_before = itr_after;
73 --itr_before;
74
75 double y_before = itr_before->first;
76 double z_before = itr_before->second;
77 double y_after = itr_after->first;
78 double z_after = itr_after->second;
79
80 double y_diff_before = std::fabs(y - y_before);
81 double y_diff_after = std::fabs(y - y_after);
82
83 return InterpolateValue(z_before, y_diff_before, z_after, y_diff_after);
84}
85
86double Interpolation2D::InterpolateValue(const double value_before,
87 const double dist_before,
88 const double value_after,
89 const double dist_after) const {
90 if (dist_before < kDoubleEpsilon) {
91 return value_before;
92 }
93 if (dist_after < kDoubleEpsilon) {
94 return value_after;
95 }
96 double value_gap = value_after - value_before;
97 double value_buff = value_gap * dist_before / (dist_before + dist_after);
98 return value_before + value_buff;
99}
100
101} // namespace dreamview
102} // namespace apollo
std::vector< std::tuple< double, double, double > > DataType
std::pair< double, double > KeyType
double Interpolate(const KeyType &xy) const
const double kDoubleEpsilon
class register implement
Definition arena_queue.h:37