|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, Willow Garage, Inc. |
| 3 | + * Copyright (c) 2023, Open Source Robotics Foundation, Inc. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of the Willow Garage, Inc. nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#ifndef RVIZ_COMMON__DEPTH_CLOUD_MLD_HPP_ |
| 32 | +#define RVIZ_COMMON__DEPTH_CLOUD_MLD_HPP_ |
| 33 | + |
| 34 | +#include <cmath> |
| 35 | +#include <cstdint> |
| 36 | +#include <stdexcept> |
| 37 | +#include <exception> |
| 38 | +#include <memory> |
| 39 | +#include <mutex> |
| 40 | +#include <string> |
| 41 | +#include <vector> |
| 42 | + |
| 43 | +#include <sensor_msgs/msg/image.hpp> |
| 44 | +#include <sensor_msgs/msg/camera_info.hpp> |
| 45 | +#include <sensor_msgs/msg/point_cloud2.hpp> |
| 46 | + |
| 47 | +#include <rclcpp/rclcpp.hpp> |
| 48 | + |
| 49 | +#include "rviz_common/ros_integration/ros_node_abstraction_iface.hpp" |
| 50 | +#include "rviz_common/visibility_control.hpp" |
| 51 | + |
| 52 | +// This is necessary because of using stl types here. It is completely safe, because |
| 53 | +// a) the member is not accessible from the outside |
| 54 | +// b) there are no inline functions. |
| 55 | +#ifdef _WIN32 |
| 56 | +# pragma warning(push) |
| 57 | +# pragma warning(disable:4251) |
| 58 | +#endif |
| 59 | + |
| 60 | +namespace rviz_common |
| 61 | +{ |
| 62 | +class MultiLayerDepthException : public std::exception |
| 63 | +{ |
| 64 | +public: |
| 65 | + RVIZ_COMMON_PUBLIC |
| 66 | + explicit MultiLayerDepthException(const std::string & error_msg) |
| 67 | + : std::exception(), error_msg_(error_msg) |
| 68 | + { |
| 69 | + } |
| 70 | + ~MultiLayerDepthException() throw() override |
| 71 | + { |
| 72 | + } |
| 73 | + |
| 74 | + RVIZ_COMMON_PUBLIC |
| 75 | + const char * what() const throw() override |
| 76 | + { |
| 77 | + return error_msg_.c_str(); |
| 78 | + } |
| 79 | + |
| 80 | +protected: |
| 81 | + std::string error_msg_; |
| 82 | +}; |
| 83 | + |
| 84 | +class RVIZ_COMMON_PUBLIC MultiLayerDepth final |
| 85 | +{ |
| 86 | +public: |
| 87 | + MultiLayerDepth() |
| 88 | + : shadow_time_out_(30.0), shadow_distance_(0.01f) {} |
| 89 | + |
| 90 | + virtual ~MultiLayerDepth() |
| 91 | + { |
| 92 | + } |
| 93 | + |
| 94 | + void setShadowTimeOut(double time_out); |
| 95 | + |
| 96 | + void enableOcclusionCompensation(bool occlusion_compensation); |
| 97 | + |
| 98 | + sensor_msgs::msg::PointCloud2::SharedPtr |
| 99 | + generatePointCloudFromDepth( |
| 100 | + const sensor_msgs::msg::Image::ConstSharedPtr & depth_msg, |
| 101 | + const sensor_msgs::msg::Image::ConstSharedPtr & color_msg, |
| 102 | + sensor_msgs::msg::CameraInfo::ConstSharedPtr camera_info_msg, |
| 103 | + ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node); |
| 104 | + |
| 105 | + void reset(); |
| 106 | + |
| 107 | +private: |
| 108 | + /** @brief Precompute projection matrix, initialize buffers */ |
| 109 | + void initializeConversion( |
| 110 | + const sensor_msgs::msg::Image::ConstSharedPtr & depth_msg, |
| 111 | + sensor_msgs::msg::CameraInfo::ConstSharedPtr & camera_info_msg); |
| 112 | + |
| 113 | + /** @brief Convert color data to RGBA format */ |
| 114 | + template<typename T> |
| 115 | + void convertColor( |
| 116 | + const sensor_msgs::msg::Image::ConstSharedPtr & color_msg, |
| 117 | + std::vector<uint32_t> & rgba_color_raw); |
| 118 | + |
| 119 | + /** @brief Generate single-layered depth cloud (depth only) */ |
| 120 | + template<typename T> |
| 121 | + sensor_msgs::msg::PointCloud2::SharedPtr generatePointCloudSL( |
| 122 | + const sensor_msgs::msg::Image::ConstSharedPtr & depth_msg, |
| 123 | + std::vector<uint32_t> & rgba_color_raw); |
| 124 | + |
| 125 | + /** @brief Generate multi-layered depth cloud (depth+shadow) */ |
| 126 | + template<typename T> |
| 127 | + sensor_msgs::msg::PointCloud2::SharedPtr generatePointCloudML( |
| 128 | + const sensor_msgs::msg::Image::ConstSharedPtr & depth_msg, |
| 129 | + std::vector<uint32_t> & rgba_color_raw, |
| 130 | + ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node); |
| 131 | + |
| 132 | + // Helpers to generate pointcloud2 message |
| 133 | + sensor_msgs::msg::PointCloud2::SharedPtr initPointCloud(); |
| 134 | + void finalizePointCloud( |
| 135 | + sensor_msgs::msg::PointCloud2::SharedPtr & point_cloud, |
| 136 | + std::size_t size); |
| 137 | + |
| 138 | + std::vector<float> projection_map_x_; |
| 139 | + std::vector<float> projection_map_y_; |
| 140 | + |
| 141 | + // shadow buffers |
| 142 | + std::vector<float> shadow_depth_; |
| 143 | + std::vector<double> shadow_timestamp_; |
| 144 | + std::vector<uint8_t> shadow_buffer_; |
| 145 | + |
| 146 | + // configuration |
| 147 | + bool occlusion_compensation_; |
| 148 | + double shadow_time_out_; |
| 149 | + float shadow_distance_; |
| 150 | +}; |
| 151 | +} // namespace rviz_common |
| 152 | + |
| 153 | +#ifdef _WIN32 |
| 154 | +# pragma warning(pop) |
| 155 | +#endif |
| 156 | + |
| 157 | +#endif // RVIZ_COMMON__DEPTH_CLOUD_MLD_HPP_ |
0 commit comments