scenario_simulator_v2 C++ API
helper_functions.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__TEST__HELPER_FUNCTIONS_HPP_
16 #define TRAFFIC_SIMULATOR__TEST__HELPER_FUNCTIONS_HPP_
17 
18 #include <ament_index_cpp/get_package_share_directory.hpp>
23 #include <traffic_simulator_msgs/msg/entity_subtype.hpp>
24 #include <traffic_simulator_msgs/msg/entity_type.hpp>
25 
26 #include "catalogs.hpp"
27 #include "expect_eq_macros.hpp"
28 
29 constexpr auto convertDegToRad(const double deg) -> double { return deg / 180.0 * M_PI; }
30 constexpr auto convertRadToDeg(const double rad) -> double { return rad * 180.0 / M_PI; }
31 
32 auto makePoint(const double x, const double y, const double z = 0.0) -> geometry_msgs::msg::Point
33 {
34  return geometry_msgs::build<geometry_msgs::msg::Point>().x(x).y(y).z(z);
35 }
36 
37 auto makeBoundingBox(const double center_y = 0.0) -> traffic_simulator_msgs::msg::BoundingBox
38 {
39  return traffic_simulator_msgs::build<traffic_simulator_msgs::msg::BoundingBox>()
40  .center(makePoint(1.0, center_y))
41  .dimensions(geometry_msgs::build<geometry_msgs::msg::Vector3>().x(4.0).y(2.0).z(1.5));
42 }
43 
45  const double x, const double y, const double x_offset = 0.0, const double y_offset = 0.0)
46  -> traffic_simulator_msgs::msg::BoundingBox
47 {
48  return traffic_simulator_msgs::build<traffic_simulator_msgs::msg::BoundingBox>()
49  .center(geometry_msgs::build<geometry_msgs::msg::Point>().x(x_offset).y(y_offset).z(0.0))
50  .dimensions(geometry_msgs::build<geometry_msgs::msg::Vector3>().x(x).y(y).z(0.0));
51 }
52 
53 auto makeSmallBoundingBox(const double center_y = 0.0) -> traffic_simulator_msgs::msg::BoundingBox
54 {
55  return traffic_simulator_msgs::build<traffic_simulator_msgs::msg::BoundingBox>()
56  .center(makePoint(0.0, center_y))
57  .dimensions(geometry_msgs::build<geometry_msgs::msg::Vector3>().x(1.0).y(1.0).z(1.0));
58 }
59 
60 auto makeQuaternionFromYaw(const double yaw) -> geometry_msgs::msg::Quaternion
61 {
63  geometry_msgs::build<geometry_msgs::msg::Vector3>().x(0.0).y(0.0).z(yaw));
64 }
65 
66 auto makePose(const double x, const double y, const double z, const double yaw_deg)
67  -> geometry_msgs::msg::Pose
68 {
72  return geometry_msgs::build<geometry_msgs::msg::Pose>()
73  .position(geometry_msgs::build<geometry_msgs::msg::Point>().x(x).y(y).z(z))
75  geometry_msgs::build<geometry_msgs::msg::Vector3>().x(0.0).y(0.0).z(
76  convertDegToRad(yaw_deg))));
77 }
78 
79 auto makePose(
80  geometry_msgs::msg::Point position,
81  geometry_msgs::msg::Quaternion orientation = geometry_msgs::msg::Quaternion())
82  -> geometry_msgs::msg::Pose
83 {
84  return geometry_msgs::build<geometry_msgs::msg::Pose>().position(position).orientation(
85  orientation);
86 }
87 
88 auto makeHdMapUtilsSharedPointer() -> std::shared_ptr<hdmap_utils::HdMapUtils>
89 {
90  return std::make_shared<hdmap_utils::HdMapUtils>(
91  ament_index_cpp::get_package_share_directory("traffic_simulator") +
92  "/map/standard_map/lanelet2_map.osm",
93  geographic_msgs::build<geographic_msgs::msg::GeoPoint>()
94  .latitude(35.9037067912303)
95  .longitude(139.9337945139059)
96  .altitude(0.0));
97 }
98 
100  std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils, const lanelet::Id id = 120659,
101  const double s = 0.0, const double offset = 0.0)
103 {
106 }
107 
109  std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils,
111  traffic_simulator_msgs::msg::BoundingBox bbox, const double speed = 0.0,
112  const std::string name = "default_entity_name",
113  const uint8_t type = traffic_simulator_msgs::msg::EntityType::VEHICLE)
115 {
116  return traffic_simulator_msgs::build<traffic_simulator::EntityStatus>()
117  .type(traffic_simulator_msgs::build<traffic_simulator_msgs::msg::EntityType>().type(type))
118  .subtype(traffic_simulator_msgs::build<traffic_simulator_msgs::msg::EntitySubtype>().value(
119  traffic_simulator_msgs::msg::EntitySubtype::UNKNOWN))
120  .time(0.0)
121  .name(name)
122  .bounding_box(bbox)
123  .action_status(traffic_simulator::helper::constructActionStatus(speed, 0.0, 0.0, 0.0))
124  .pose(hdmap_utils->toMapPose(static_cast<traffic_simulator::LaneletPose>(pose)).pose)
125  .lanelet_pose(static_cast<traffic_simulator::LaneletPose>(pose))
126  .lanelet_pose_valid(true);
127 }
128 
130  std::shared_ptr<hdmap_utils::HdMapUtils> /* hdmap_utils */, geometry_msgs::msg::Pose pose,
131  traffic_simulator_msgs::msg::BoundingBox bbox, const double speed = 0.0,
132  const std::string name = "default_entity_name",
133  const uint8_t type = traffic_simulator_msgs::msg::EntityType::VEHICLE)
135 {
136  return traffic_simulator_msgs::build<traffic_simulator::EntityStatus>()
137  .type(traffic_simulator_msgs::build<traffic_simulator_msgs::msg::EntityType>().type(type))
138  .subtype(traffic_simulator_msgs::build<traffic_simulator_msgs::msg::EntitySubtype>().value(
139  traffic_simulator_msgs::msg::EntitySubtype::UNKNOWN))
140  .time(0.0)
141  .name(name)
142  .bounding_box(bbox)
143  .action_status(traffic_simulator::helper::constructActionStatus(speed, 0.0, 0.0, 0.0))
144  .pose(pose)
146  .lanelet_pose_valid(false);
147 }
148 
150  std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils,
152  traffic_simulator_msgs::msg::BoundingBox bbox, const double speed = 0.0,
153  const std::string name = "default_entity_name",
154  const uint8_t type = traffic_simulator_msgs::msg::EntityType::VEHICLE)
156 {
158  makeEntityStatus(hdmap_utils, canonicalized_lanelet_pose, bbox, speed, name, type),
159  canonicalized_lanelet_pose);
160 }
161 
163  std::shared_ptr<hdmap_utils::HdMapUtils> hdmap_utils, geometry_msgs::msg::Pose pose,
164  traffic_simulator_msgs::msg::BoundingBox bbox, const double matching_distance = 1.0,
165  const double speed = 0.0, const std::string name = "default_entity_name",
166  const uint8_t type = traffic_simulator_msgs::msg::EntityType::VEHICLE)
168 {
169  const auto include_crosswalk =
170  (traffic_simulator_msgs::msg::EntityType::PEDESTRIAN == type ||
171  traffic_simulator_msgs::msg::EntityType::MISC_OBJECT == type);
172  const auto canonicalized_lanelet_pose = traffic_simulator::pose::toCanonicalizedLaneletPose(
173  pose, bbox, include_crosswalk, matching_distance, hdmap_utils);
175  makeEntityStatus(hdmap_utils, pose, bbox, speed, name, type), canonicalized_lanelet_pose);
176 }
177 
178 #endif // TRAFFIC_SIMULATOR__TEST__ENTITY_HELPER_FUNCTIONS_HPP_
Definition: cache.hpp:46
auto convertEulerAngleToQuaternion(const T &v)
Definition: euler_to_quaternion.hpp:30
traffic_simulator_msgs::msg::ActionStatus constructActionStatus(double linear_vel=0, double angular_vel=0, double linear_accel=0, double angular_accel=0)
helper function for constructing action status
Definition: helper.cpp:25
LaneletPose constructLaneletPose(lanelet::Id lanelet_id, double s, double offset=0, double roll=0, double pitch=0, double yaw=0)
helper function for constructing lanelet pose
Definition: helper.cpp:40
auto toCanonicalizedLaneletPose(const geometry_msgs::msg::Pose &map_pose, const bool include_crosswalk, const std::shared_ptr< hdmap_utils::HdMapUtils > &hdmap_utils_ptr) -> std::optional< CanonicalizedLaneletPose >
Definition: pose.cpp:77
traffic_simulator_msgs::msg::LaneletPose LaneletPose
Definition: lanelet_pose.hpp:22
traffic_simulator_msgs::msg::EntityStatus EntityStatus
Definition: entity_status.hpp:25
std::string string
Definition: junit5.hpp:26
traffic_simulator::lanelet_pose::CanonicalizedLaneletPose CanonicalizedLaneletPose
Definition: test_lanelet_pose.cpp:21
auto makeSmallBoundingBox(const double center_y=0.0) -> traffic_simulator_msgs::msg::BoundingBox
Definition: helper_functions.hpp:53
auto makeHdMapUtilsSharedPointer() -> std::shared_ptr< hdmap_utils::HdMapUtils >
Definition: helper_functions.hpp:88
auto makeEntityStatus(std::shared_ptr< hdmap_utils::HdMapUtils > hdmap_utils, traffic_simulator::lanelet_pose::CanonicalizedLaneletPose pose, traffic_simulator_msgs::msg::BoundingBox bbox, const double speed=0.0, const std::string name="default_entity_name", const uint8_t type=traffic_simulator_msgs::msg::EntityType::VEHICLE) -> traffic_simulator::EntityStatus
Definition: helper_functions.hpp:108
auto makeBoundingBox(const double center_y=0.0) -> traffic_simulator_msgs::msg::BoundingBox
Definition: helper_functions.hpp:37
auto makePoint(const double x, const double y, const double z=0.0) -> geometry_msgs::msg::Point
Definition: helper_functions.hpp:32
auto makeQuaternionFromYaw(const double yaw) -> geometry_msgs::msg::Quaternion
Definition: helper_functions.hpp:60
auto makeCanonicalizedEntityStatus(std::shared_ptr< hdmap_utils::HdMapUtils > hdmap_utils, traffic_simulator::lanelet_pose::CanonicalizedLaneletPose canonicalized_lanelet_pose, traffic_simulator_msgs::msg::BoundingBox bbox, const double speed=0.0, const std::string name="default_entity_name", const uint8_t type=traffic_simulator_msgs::msg::EntityType::VEHICLE) -> traffic_simulator::entity_status::CanonicalizedEntityStatus
Definition: helper_functions.hpp:149
auto makeCustom2DBoundingBox(const double x, const double y, const double x_offset=0.0, const double y_offset=0.0) -> traffic_simulator_msgs::msg::BoundingBox
Definition: helper_functions.hpp:44
constexpr auto convertDegToRad(const double deg) -> double
Definition: helper_functions.hpp:29
auto makePose(const double x, const double y, const double z, const double yaw_deg) -> geometry_msgs::msg::Pose
Definition: helper_functions.hpp:66
auto makeCanonicalizedLaneletPose(std::shared_ptr< hdmap_utils::HdMapUtils > hdmap_utils, const lanelet::Id id=120659, const double s=0.0, const double offset=0.0) -> traffic_simulator::lanelet_pose::CanonicalizedLaneletPose
Definition: helper_functions.hpp:99
constexpr auto convertRadToDeg(const double rad) -> double
Definition: helper_functions.hpp:30