path_sampler.cc 2.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
#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"

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

#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

Z
zhuweicheng 已提交
37
PathSampler::PathSampler(const DpPolyPathConfig& config) : config_(config) {}
Y
Yifei Jiang 已提交
38

D
Dong Li 已提交
39
bool PathSampler::sample(
J
jiangyifei 已提交
40 41 42
    const ReferenceLine& reference_line,
    const ::apollo::common::TrajectoryPoint& init_point,
    std::vector<std::vector<::apollo::common::SLPoint>>* const points) {
D
Dong Li 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
  if (!points) {
    AERROR << "The provided points are null";
    return false;
  }
  ::apollo::common::SLPoint init_sl_point;
  common::math::Vec2d init_point_vec2d{init_point.path_point().x(),
                                       init_point.path_point().y()};
  if (!reference_line.get_point_in_frenet_frame(init_point_vec2d,
                                                &init_sl_point)) {
    AERROR << "Failed to get sl point from point "
           << init_point_vec2d.DebugString();
    return false;
  }
D
Dong Li 已提交
56
  const double reference_line_length =
J
jiangyifei 已提交
57
      reference_line.reference_map_line().accumulated_s().back();
L
lianglia-apollo 已提交
58
  double level_distance =
Z
zhuweicheng 已提交
59 60
      std::fmax(config_.step_length_min(),
                std::fmin(init_point.v(), config_.step_length_max()));
L
lianglia-apollo 已提交
61

Y
Yifei Jiang 已提交
62
  double accumulated_s = init_sl_point.s();
L
lianglia-apollo 已提交
63
  for (size_t i = 0;
Z
zhuweicheng 已提交
64
       i < config_.sample_level() && accumulated_s < reference_line_length;
L
lianglia-apollo 已提交
65 66 67 68 69 70
       ++i) {
    std::vector<SLPoint> level_points;
    accumulated_s += level_distance;
    double s = std::fmin(accumulated_s, reference_line_length);

    int32_t num =
Z
zhuweicheng 已提交
71
        static_cast<int32_t>(config_.sample_points_num_each_level() / 2);
L
lianglia-apollo 已提交
72
    for (int32_t j = -num; j < num + 1; ++j) {
Z
zhuweicheng 已提交
73
      double l = config_.lateral_sample_offset() * j;
L
lianglia-apollo 已提交
74 75 76
      SLPoint sl = common::util::MakeSLPoint(s, l);
      if (reference_line.is_on_road(sl)) {
        level_points.push_back(sl);
J
jiangyifei 已提交
77 78
      }
    }
D
Dong Li 已提交
79 80 81
    if (!level_points.empty()) {
      points->push_back(level_points);
    }
Y
Yifei Jiang 已提交
82
  }
D
Dong Li 已提交
83
  return true;
Y
Yifei Jiang 已提交
84
}
85 86
}  // namespace planning
}  // namespace apollo