path_sampler.cc 3.9 KB
Newer Older
Y
Yifei Jiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

/**
 * @file sampler.cpp
 **/
J
jiangyifei 已提交
20 21
#include <algorithm>
#include <vector>
Y
Yifei Jiang 已提交
22 23 24 25 26 27

#include "modules/planning/optimizer/dp_poly_path/path_sampler.h"

namespace apollo {
namespace planning {

28 29
using apollo::common::Status;

30
PathSampler::PathSampler(const DpPolyPathConfig &config) : _config(config) {}
Y
Yifei Jiang 已提交
31

32
Status PathSampler::sample(
J
jiangyifei 已提交
33 34 35 36
    const ReferenceLine& reference_line,
    const ::apollo::common::TrajectoryPoint& init_point,
    const ::apollo::common::SLPoint& init_sl_point,
    std::vector<std::vector<::apollo::common::SLPoint>>* const points) {
Y
Yifei Jiang 已提交
37
  CHECK_NOTNULL(points);
J
jiangyifei 已提交
38 39
  double reference_line_length =
      reference_line.reference_map_line().accumulated_s().back();
Y
Yifei Jiang 已提交
40 41 42 43 44
  double step_length = init_point.v();
  step_length = std::min(step_length, _config.step_length_max());
  step_length = std::max(step_length, _config.step_length_min());
  double center_l = init_sl_point.l();
  double accumulated_s = init_sl_point.s();
J
jiangyifei 已提交
45 46 47
  for (size_t i = 0; i < _config.sample_level(); ++i) {
    std::vector<::apollo::common::SLPoint> level_points;
    if (std::abs(center_l) < _config.lateral_sample_offset()) {
Y
Yifei Jiang 已提交
48 49 50 51 52
      center_l = 0.0;
    } else {
      center_l = center_l * _config.lateral_adjust_coeff();
    }
    accumulated_s += step_length;
J
jiangyifei 已提交
53
    double level_start_l = center_l
J
jiangyifei 已提交
54 55
        - _config.lateral_sample_offset()
            * ((_config.sample_points_num_each_level() - 1) >> 1);
J
jiangyifei 已提交
56 57
    for (size_t j = 0; j < _config.sample_points_num_each_level(); ++j) {
      ::apollo::common::SLPoint sl_point;
Y
Yifei Jiang 已提交
58
      sl_point.set_s(accumulated_s);
J
jiangyifei 已提交
59
      sl_point.set_l(level_start_l + j * _config.lateral_sample_offset());
Y
Yifei Jiang 已提交
60
      if (reference_line.is_on_road(sl_point)) {
J
jiangyifei 已提交
61 62 63 64 65
        level_points.push_back(std::move(sl_point));
      }
    }
    if (level_points.empty()) {
      if (accumulated_s > reference_line_length) {
J
jiangyifei 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        ::apollo::common::SLPoint sl_point;
        sl_point.set_s(reference_line_length);
        sl_point.set_l(-_config.lateral_sample_offset());
        level_points.emplace_back(std::move(sl_point));

        ::apollo::common::SLPoint sl_point2;
        sl_point2.set_s(reference_line_length);
        sl_point2.set_l(0.0);
        level_points.emplace_back(std::move(sl_point2));

        ::apollo::common::SLPoint sl_point3;
        sl_point3.set_s(reference_line_length);
        sl_point3.set_l(_config.lateral_sample_offset());
        level_points.emplace_back(std::move(sl_point3));

J
jiangyifei 已提交
81 82 83
        points->push_back(level_points);
        return Status::OK();
      } else {
J
jiangyifei 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
        ::apollo::common::SLPoint sl_point;
        sl_point.set_s(accumulated_s);
        sl_point.set_l(-_config.lateral_sample_offset());
        level_points.emplace_back(std::move(sl_point));

        ::apollo::common::SLPoint sl_point2;
        sl_point2.set_s(accumulated_s);
        sl_point2.set_l(0.0);
        level_points.emplace_back(std::move(sl_point2));

        ::apollo::common::SLPoint sl_point3;
        sl_point3.set_s(accumulated_s);
        sl_point3.set_l(_config.lateral_sample_offset());
        level_points.emplace_back(std::move(sl_point3));
Y
Yifei Jiang 已提交
98 99 100 101
      }
    }
    points->push_back(level_points);
  }
102
  return Status::OK();
Y
Yifei Jiang 已提交
103
}
104 105
}  // namespace planning
}  // namespace apollo