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>
22 #include <traffic_simulator_msgs/msg/pedestrian_parameters.hpp>
23 #include <traffic_simulator_msgs/msg/vehicle_parameters.hpp>
24 #include <variant>
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  explicit Validator(
46  const geometry_msgs::msg::Pose &, const double source_radius, const bool include_crosswalk);
47 
51  auto operator()(const std::vector<geometry_msgs::msg::Point> &, const lanelet::Id) const
52  -> bool;
53  };
54 
56  {
58 
60 
61  bool use_random_orientation = false;
62 
63  double start_delay = 0.0;
64  };
65 
66  using Distribution =
67  std::vector<std::tuple<VehicleOrPedestrianParameter, std::string, std::string, double>>;
68 
69  template <typename Pose, typename Parameters>
70  using Spawner = std::function<void(
71  const std::string &, const Pose &, const Parameters &, const std::string &,
72  const std::string &)>;
73 
74  template <typename Spawner>
75  explicit TrafficSource(
76  const double radius, const double rate, const geometry_msgs::msg::Pose & pose,
77  const Distribution & distribution, const std::optional<int> seed, const double current_time,
78  const Configuration & configuration, const Spawner & spawn)
79  : rate(rate),
80  pose(pose),
81  id(source_count_++),
82  spawn_vehicle_in_lane_coordinate(spawn),
83  spawn_pedestrian_in_lane_coordinate(spawn),
84  spawn_vehicle_in_world_coordinate(spawn),
85  spawn_pedestrian_in_world_coordinate(spawn),
86  engine_(seed ? seed.value() : std::random_device()()),
87  angle_distribution_(0.0, boost::math::constants::two_pi<double>()),
88  radius_distribution_(0.0, radius),
89  params_distribution_([&]() {
90  auto weights = std::vector<double>();
91  weights.reserve(distribution.size());
92  for ([[maybe_unused]] auto && [parameter, behavior, model3d, weight] : distribution) {
93  weights.push_back(weight);
94  }
95  return std::discrete_distribution<std::size_t>(weights.begin(), weights.end());
96  }()),
97  start_execution_time_(
99  (std::isnan(current_time) ? 0.0 : current_time) + std::max(configuration.start_delay, 0.0)),
100  configuration_(configuration),
101  distribution_(distribution),
102  validate(pose, radius_distribution_.max(), false),
103  validate_considering_crosswalk(pose, radius_distribution_.max(), true)
104  {
105  }
106 
107  void execute(const double current_time, const double step_time) override;
108 
109  const double rate;
110 
112 
113  const std::size_t id;
114 
115 private:
116  auto makeRandomPose(const bool random_orientation, const VehicleOrPedestrianParameter & parameter)
118 
119  auto isPoseValid(const VehicleOrPedestrianParameter &, const geometry_msgs::msg::Pose &)
120  -> std::pair<bool, std::optional<CanonicalizedLaneletPose>>;
121 
122  static inline std::size_t source_count_ = 0;
123 
124  std::size_t entity_count_ = 0;
125 
126  const Spawner<CanonicalizedLaneletPose, VehicleParameter> spawn_vehicle_in_lane_coordinate;
127 
128  const Spawner<CanonicalizedLaneletPose, PedestrianParameter> spawn_pedestrian_in_lane_coordinate;
129 
130  const Spawner<geometry_msgs::msg::Pose, VehicleParameter> spawn_vehicle_in_world_coordinate;
131 
132  const Spawner<geometry_msgs::msg::Pose, PedestrianParameter> spawn_pedestrian_in_world_coordinate;
133 
134  std::mt19937 engine_;
135 
136  std::uniform_real_distribution<double> angle_distribution_;
137 
138  std::uniform_real_distribution<double> radius_distribution_;
139 
140  std::discrete_distribution<std::size_t> params_distribution_;
141 
142  const double start_execution_time_;
143 
144  const Configuration configuration_;
145 
146  const Distribution distribution_;
147 
149  const Validator validate, validate_considering_crosswalk;
150 };
151 } // namespace traffic
152 } // namespace traffic_simulator
153 
154 #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:98
std::function< void(const std::string &, const Pose &, const Parameters &, const std::string &, const std::string &)> Spawner
Definition: traffic_source.hpp:72
traffic_simulator_msgs::msg::PedestrianParameters PedestrianParameter
Definition: traffic_source.hpp:35
const double rate
Definition: traffic_source.hpp:109
std::vector< std::tuple< VehicleOrPedestrianParameter, std::string, std::string, double > > Distribution
Definition: traffic_source.hpp:67
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:111
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 Spawner &spawn)
Definition: traffic_source.hpp:75
const std::size_t id
Definition: traffic_source.hpp:113
Definition: bounding_box.hpp:32
Definition: lanelet_wrapper.hpp:43
geometry_msgs::msg::Pose Pose
Definition: lanelet_map.hpp:30
geometry_msgs::msg::Pose Pose
Definition: lanelet_wrapper.hpp:70
Definition: api.hpp:32
std::string string
Definition: junit5.hpp:26
bool require_footprint_fitting
Definition: traffic_source.hpp:59
bool allow_spawn_outside_lane
Definition: traffic_source.hpp:57
double start_delay
Definition: traffic_source.hpp:63
bool use_random_orientation
Definition: traffic_source.hpp:61
auto operator()(const std::vector< geometry_msgs::msg::Point > &, const lanelet::Id) const -> bool
whether the 2D polygon does fit inside the lanelet with the given id
Definition: traffic_source.cpp:34
const lanelet::Ids ids
Definition: traffic_source.hpp:43
static constexpr std::size_t spawning_lanes_limit
Definition: traffic_source.hpp:41
Validator(const geometry_msgs::msg::Pose &, const double source_radius, const bool include_crosswalk)
Definition: traffic_source.cpp:28