path_sampler.cc 2.4 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
#include <algorithm>
D
Dong Li 已提交
21
#include <cmath>
L
lianglia-apollo 已提交
22 23
#include <memory>
#include <vector>
Y
Yifei Jiang 已提交
24 25 26

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

L
lianglia-apollo 已提交
27 28 29 30
#include "modules/common/proto/path_point.pb.h"

#include "modules/common/util/util.h"

Y
Yifei Jiang 已提交
31 32 33
namespace apollo {
namespace planning {

L
lianglia-apollo 已提交
34 35
using Status = apollo::common::Status;
using SLPoint = apollo::common::SLPoint;
36

L
lianglia-apollo 已提交
37
PathSampler::PathSampler(const DpPolyPathConfig& config) : _config(config) {}
Y
Yifei Jiang 已提交
38

39
Status PathSampler::sample(
J
jiangyifei 已提交
40 41 42 43
    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 已提交
44
  CHECK_NOTNULL(points);
D
Dong Li 已提交
45
  const double reference_line_length =
J
jiangyifei 已提交
46
      reference_line.reference_map_line().accumulated_s().back();
L
lianglia-apollo 已提交
47 48 49 50
  double level_distance =
      std::fmax(_config.step_length_min(),
                std::fmin(init_point.v(), _config.step_length_max()));

Y
Yifei Jiang 已提交
51
  double accumulated_s = init_sl_point.s();
L
lianglia-apollo 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
  for (size_t i = 0;
       i < _config.sample_level() && accumulated_s < reference_line_length;
       ++i) {
    std::vector<SLPoint> level_points;
    accumulated_s += level_distance;
    double s = std::fmin(accumulated_s, reference_line_length);

    int32_t num =
        static_cast<int32_t>(_config.sample_points_num_each_level() / 2);
    for (int32_t j = -num; j < num + 1; ++j) {
      double l = _config.lateral_sample_offset() * j;
      SLPoint sl = common::util::MakeSLPoint(s, l);
      if (reference_line.is_on_road(sl)) {
        level_points.push_back(sl);
J
jiangyifei 已提交
66 67
      }
    }
Y
Yifei Jiang 已提交
68 69
    points->push_back(level_points);
  }
L
lianglia-apollo 已提交
70

71
  return Status::OK();
Y
Yifei Jiang 已提交
72
}
73 74
}  // namespace planning
}  // namespace apollo