scenario_simulator_v2 C++ API
traffic_source.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TRAFFIC_SIMULATOR__TRAFFIC__TRAFFIC_SOURCE_HPP_
16 #define TRAFFIC_SIMULATOR__TRAFFIC__TRAFFIC_SOURCE_HPP_
17 
18 #include <functional>
19 #include <geometry_msgs/msg/pose.hpp>
20 #include <random>
23 #include <traffic_simulator_msgs/msg/pedestrian_parameters.hpp>
24 #include <traffic_simulator_msgs/msg/vehicle_parameters.hpp>
25 
26 namespace traffic_simulator
27 {
28 namespace traffic
29 {
31 {
32 public:
33  using VehicleParameter = traffic_simulator_msgs::msg::VehicleParameters;
34 
35  using PedestrianParameter = traffic_simulator_msgs::msg::PedestrianParameters;
36 
37  using VehicleOrPedestrianParameter = std::variant<VehicleParameter, PedestrianParameter>;
38 
39  struct Validator
40  {
41  static constexpr std::size_t spawning_lanes_limit = 1000;
42 
43  const lanelet::Ids ids;
44 
45  const lanelet::Lanelets lanelets;
46 
47  explicit Validator(
48  const std::shared_ptr<hdmap_utils::HdMapUtils> &, const geometry_msgs::msg::Pose &,
49  const double source_radius, const bool include_crosswalk);
50 
54  auto operator()(const std::vector<geometry_msgs::msg::Point> &, lanelet::Id) const -> bool;
55  };
56 
58  {
60 
62 
63  bool use_random_orientation = false;
64 
65  double start_delay = 0.0;
66  };
67 
68  using Distribution =
69  std::vector<std::tuple<VehicleOrPedestrianParameter, std::string, std::string, double>>;
70 
71  template <typename Pose, typename Parameters>
72  using Spawner = std::function<void(
73  const std::string &, const Pose &, const Parameters &, const std::string &,
74  const std::string &)>;
75 
76  template <typename Spawner>
77  explicit TrafficSource(
78  const double radius, const double rate, const geometry_msgs::msg::Pose & pose,
79  const Distribution & distribution, const std::optional<int> seed, const double current_time,
80  const Configuration & configuration,
81  const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils, const Spawner & spawn)
82  : rate(rate),
83  pose(pose),
84  id(source_count_++),
85  spawn_vehicle_in_lane_coordinate(spawn),
86  spawn_pedestrian_in_lane_coordinate(spawn),
87  spawn_vehicle_in_world_coordinate(spawn),
88  spawn_pedestrian_in_world_coordinate(spawn),
89  hdmap_utils_(hdmap_utils),
90  engine_(seed ? seed.value() : std::random_device()()),
91  angle_distribution_(0.0, boost::math::constants::two_pi<double>()),
92  radius_distribution_(0.0, radius),
93  params_distribution_([&]() {
94  auto weights = std::vector<double>();
95  weights.reserve(distribution.size());
96  for ([[maybe_unused]] auto && [parameter, behavior, model3d, weight] : distribution) {
97  weights.push_back(weight);
98  }
99  return std::discrete_distribution<std::size_t>(weights.begin(), weights.end());
100  }()),
101  start_execution_time_(
103  (std::isnan(current_time) ? 0.0 : current_time) + std::max(configuration.start_delay, 0.0)),
104  configuration_(configuration),
105  distribution_(distribution),
106  validate(hdmap_utils, pose, radius_distribution_.max(), false),
107  validate_considering_crosswalk(hdmap_utils, pose, radius_distribution_.max(), true)
108  {
109  }
110 
111  void execute(const double current_time, const double step_time) override;
112 
113  const double rate;
114 
115  const geometry_msgs::msg::Pose pose;
116 
117  const std::size_t id;
118 
119 private:
120  auto makeRandomPose(const bool random_orientation = false) -> geometry_msgs::msg::Pose;
121 
122  auto isPoseValid(const VehicleOrPedestrianParameter &, const geometry_msgs::msg::Pose &)
123  -> std::pair<bool, std::optional<CanonicalizedLaneletPose>>;
124 
125  static inline std::size_t source_count_ = 0;
126 
127  std::size_t entity_count_ = 0;
128 
129  const Spawner<CanonicalizedLaneletPose, VehicleParameter> spawn_vehicle_in_lane_coordinate;
130 
131  const Spawner<CanonicalizedLaneletPose, PedestrianParameter> spawn_pedestrian_in_lane_coordinate;
132 
133  const Spawner<geometry_msgs::msg::Pose, VehicleParameter> spawn_vehicle_in_world_coordinate;
134 
135  const Spawner<geometry_msgs::msg::Pose, PedestrianParameter> spawn_pedestrian_in_world_coordinate;
136 
137  const std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils_;
138 
139  std::mt19937 engine_;
140 
141  std::uniform_real_distribution<double> angle_distribution_;
142 
143  std::uniform_real_distribution<double> radius_distribution_;
144 
145  std::discrete_distribution<std::size_t> params_distribution_;
146 
147  const double start_execution_time_;
148 
149  const Configuration configuration_;
150 
151  const Distribution distribution_;
152 
154  const Validator validate, validate_considering_crosswalk;
155 };
156 } // namespace traffic
157 } // namespace traffic_simulator
158 
159 #endif // TRAFFIC_SIMULATOR__TRAFFIC__TRAFFIC_SOURCE_HPP_
Definition: traffic_module_base.hpp:36
Definition: traffic_source.hpp:31
void execute(const double current_time, const double step_time) override
Definition: traffic_source.cpp:94
std::function< void(const std::string &, const Pose &, const Parameters &, const std::string &, const std::string &)> Spawner
Definition: traffic_source.hpp:74
traffic_simulator_msgs::msg::PedestrianParameters PedestrianParameter
Definition: traffic_source.hpp:35
const double rate
Definition: traffic_source.hpp:113
std::vector< std::tuple< VehicleOrPedestrianParameter, std::string, std::string, double > > Distribution
Definition: traffic_source.hpp:69
traffic_simulator_msgs::msg::VehicleParameters VehicleParameter
Definition: traffic_source.hpp:33
std::variant< VehicleParameter, PedestrianParameter > VehicleOrPedestrianParameter
Definition: traffic_source.hpp:37
const geometry_msgs::msg::Pose pose
Definition: traffic_source.hpp:115
const std::size_t id
Definition: traffic_source.hpp:117
TrafficSource(const double radius, const double rate, const geometry_msgs::msg::Pose &pose, const Distribution &distribution, const std::optional< int > seed, const double current_time, const Configuration &configuration, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils, const Spawner &spawn)
Definition: traffic_source.hpp:77
Definition: cache.hpp:46
Definition: bounding_box.hpp:32
Definition: cache.hpp:27
Definition: api.hpp:49
std::string string
Definition: junit5.hpp:26
bool require_footprint_fitting
Definition: traffic_source.hpp:61
bool allow_spawn_outside_lane
Definition: traffic_source.hpp:59
double start_delay
Definition: traffic_source.hpp:65
bool use_random_orientation
Definition: traffic_source.hpp:63
Validator(const std::shared_ptr< hdmap_utils::HdMapUtils > &, const geometry_msgs::msg::Pose &, const double source_radius, const bool include_crosswalk)
Definition: traffic_source.cpp:26
const lanelet::Ids ids
Definition: traffic_source.hpp:43
auto operator()(const std::vector< geometry_msgs::msg::Point > &, lanelet::Id) const -> bool
whether the 2D polygon does fit inside the lanelet with the given id
Definition: traffic_source.cpp:35
const lanelet::Lanelets lanelets
Definition: traffic_source.hpp:45
static constexpr std::size_t spawning_lanes_limit
Definition: traffic_source.hpp:41