15 #ifndef SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__LIDAR__RAYCASTER_HPP_
16 #define SIMPLE_SENSOR_SIMULATOR__SENSOR_SIMULATION__LIDAR__RAYCASTER_HPP_
18 #include <embree4/rtcore.h>
19 #include <pcl_conversions/pcl_conversions.h>
23 #include <geometry_msgs/msg/pose.hpp>
24 #include <geometry_msgs/msg/vector3.hpp>
27 #include <sensor_msgs/msg/point_cloud2.hpp>
31 #include <unordered_map>
43 template <
typename T,
typename... Ts>
46 if (primitive_ptrs_.count(name) != 0) {
47 throw std::runtime_error(
"primitive " + name +
" already exist.");
49 auto primitive_ptr = std::make_unique<T>(std::forward<Ts>(
xs)...);
50 primitive_ptrs_.emplace(name, std::move(primitive_ptr));
52 const sensor_msgs::msg::PointCloud2
raycast(
53 const std::string & frame_id,
const rclcpp::Time & stamp,
54 const geometry_msgs::msg::Pose & origin,
double max_distance = 300,
double min_distance = 0);
57 const simulation_api_schema::LidarConfiguration & configuration,
58 double horizontal_angle_start = 0,
double horizontal_angle_end = 2 * M_PI);
61 std::vector<geometry_msgs::msg::Quaternion> getDirections(
62 const std::vector<double> & vertical_angles,
double horizontal_angle_start,
63 double horizontal_angle_end,
double horizontal_resolution);
64 std::vector<geometry_msgs::msg::Quaternion> directions_;
65 double previous_horizontal_angle_start_;
66 double previous_horizontal_angle_end_;
67 double previous_horizontal_resolution_;
68 std::vector<double> previous_vertical_angles_;
69 std::unordered_map<std::string, std::unique_ptr<primitives::Primitive>> primitive_ptrs_;
72 std::random_device seed_gen_;
73 std::default_random_engine engine_;
74 std::vector<std::string> detected_objects_;
75 std::unordered_map<unsigned int, std::string> geometry_ids_;
76 std::vector<Eigen::Matrix3d> rotation_matrices_;
78 static void intersect(
79 int thread_id,
int thread_count, RTCScene scene,
80 pcl::PointCloud<pcl::PointXYZI>::Ptr thread_cloud, geometry_msgs::msg::Pose origin,
81 std::reference_wrapper<std::set<unsigned int>> ref_thread_detected_ids,
double max_distance,
83 std::reference_wrapper<
const std::vector<Eigen::Matrix3d>> ref_rotation_matrices)
85 auto & rotation_matrices = ref_rotation_matrices.get();
86 auto & thread_detected_ids = ref_thread_detected_ids.get();
88 for (
unsigned int i = thread_id; i < rotation_matrices.size(); i += thread_count) {
89 RTCRayHit rayhit = {};
90 rayhit.ray.org_x = origin.position.x;
91 rayhit.ray.org_y = origin.position.y;
92 rayhit.ray.org_z = origin.position.z;
94 rayhit.ray.mask = 0b11111111'11111111'11111111'11111111;
95 rayhit.ray.tfar = max_distance;
96 rayhit.ray.tnear = min_distance;
97 rayhit.ray.flags =
false;
99 const auto rotation_mat = orientation_matrix * rotation_matrices.at(i);
100 rayhit.ray.dir_x = rotation_mat(0);
101 rayhit.ray.dir_y = rotation_mat(1);
102 rayhit.ray.dir_z = rotation_mat(2);
103 rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
104 rtcIntersect1(scene, &rayhit);
106 if (rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID) {
110 p.x = rotation_matrices.at(i)(0) *
distance;
111 p.y = rotation_matrices.at(i)(1) *
distance;
112 p.z = rotation_matrices.at(i)(2) *
distance;
114 thread_cloud->emplace_back(p);
115 thread_detected_ids.insert(rayhit.hit.geomID);
Definition: raycaster.hpp:38
void addPrimitive(std::string name, Ts &&... xs)
Definition: raycaster.hpp:44
~Raycaster()
Definition: raycaster.cpp:42
const sensor_msgs::msg::PointCloud2 raycast(const std::string &frame_id, const rclcpp::Time &stamp, const geometry_msgs::msg::Pose &origin, double max_distance=300, double min_distance=0)
Definition: raycaster.cpp:99
Raycaster()
Definition: raycaster.cpp:26
void setDirection(const simulation_api_schema::LidarConfiguration &configuration, double horizontal_angle_start=0, double horizontal_angle_end=2 *M_PI)
Definition: raycaster.cpp:48
const std::vector< std::string > & getDetectedObject() const
Definition: raycaster.cpp:97
auto getRotationMatrix(T quat) -> Eigen::Matrix3d
Definition: get_rotation_matrix.hpp:33
Definition: constants.hpp:19
auto distance(const geometry_msgs::Pose &pose1, const geometry_msgs::Pose &pose2)
Definition: detection_sensor.cpp:34
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26