Point Cloud Library (PCL)  1.12.1-dev
real_sense_2_grabber.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2018-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <thread>
41 #include <mutex>
42 
43 #include <pcl/io/grabber.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/point_types.h>
46 
47 #include <librealsense2/rs.hpp>
48 
49 namespace pcl
50 {
51 
52  /** \brief Grabber for Intel Realsense 2 SDK devices (D400 series)
53  * \note Device width/height defaults to 424/240, the lowest resolutions for D400 devices.
54  * \note Testing on the in_hand_scanner example we found the lower default resolution allowed the app to perform adequately.
55  * \note Developers should use this resolution as a starting point and gradually increase to get the best results
56  * \author Patrick Abadi <patrickabadi@gmail.com>, Daniel Packard <pack3754@gmail.com>
57  * \ingroup io
58  */
60  {
61  public:
62  /** \brief Constructor
63  * \param[in] file_name_or_serial_number used for either loading bag file or specific device by serial number
64  * \param[in] repeat_playback whether to repeat playback when reading from file
65  */
66  RealSense2Grabber ( const std::string& file_name_or_serial_number = "", const bool repeat_playback = true );
67 
68  /** \brief virtual Destructor inherited from the Grabber interface. It never throws. */
70 
71  /** \brief Set the device options
72  * \param[in] width resolution
73  * \param[in] height resolution
74  * \param[in] fps target frames per second for the device
75  */
76  inline void
77  setDeviceOptions ( std::uint32_t width, std::uint32_t height, std::uint32_t fps = 30 )
78  {
79  device_width_ = width;
80  device_height_ = height;
81  target_fps_ = fps;
82 
83  reInitialize ();
84  }
85 
86  /** \brief Start the data acquisition. */
87  void
88  start () override;
89 
90  /** \brief Stop the data acquisition. */
91  void
92  stop () override;
93 
94  /** \brief Check if the data acquisition is still running. */
95  bool
96  isRunning () const override;
97 
98  /** \brief Obtain the number of frames per second (FPS). */
99  float
100  getFramesPerSecond () const override;
101 
102  /** \brief defined grabber name*/
103  std::string
104  getName () const override { return std::string ( "RealSense2Grabber" ); }
105 
106  //define callback signature typedefs
107  typedef void (signal_librealsense_PointXYZ) ( const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& );
108  typedef void (signal_librealsense_PointXYZI) ( const pcl::PointCloud<pcl::PointXYZI>::ConstPtr& );
109  typedef void (signal_librealsense_PointXYZRGB) ( const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& );
110  typedef void (signal_librealsense_PointXYZRGBA) ( const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr& );
111 
112  protected:
113 
114  boost::signals2::signal<signal_librealsense_PointXYZ>* signal_PointXYZ;
115  boost::signals2::signal<signal_librealsense_PointXYZI>* signal_PointXYZI;
116  boost::signals2::signal<signal_librealsense_PointXYZRGB>* signal_PointXYZRGB;
117  boost::signals2::signal<signal_librealsense_PointXYZRGBA>* signal_PointXYZRGBA;
118 
119  /** \brief Handle when a signal callback has been changed
120  */
121  void
122  signalsChanged () override;
123 
124  /** \brief the thread function
125  */
126  void
127  threadFunction ();
128 
129  /** \brief Dynamic reinitialization.
130  */
131  void
132  reInitialize ();
133 
134  /** \brief Convert a Depth image to a pcl::PointCloud<pcl::PointXYZ>
135  * \param[in] points the depth points
136  */
138  convertDepthToPointXYZ ( const rs2::points& points );
139 
140  /** \brief Convert an Infrared Depth image to a pcl::PointCloud<pcl::PointXYZI>
141  * \param[in] points the depth points
142  * \param[in] ir Infrared video frame
143  */
145  convertIntensityDepthToPointXYZRGBI ( const rs2::points& points, const rs2::video_frame& ir );
146 
147  /** \brief Convert an rgb Depth image to a pcl::PointCloud<pcl::PointXYZRGB>
148  * \param[in] points the depth points
149  * \param[in] rgb rgb video frame
150  */
152  convertRGBDepthToPointXYZRGB ( const rs2::points& points, const rs2::video_frame& rgb );
153 
154  /** \brief Convert an rgb Depth image to a pcl::PointCloud<pcl::PointXYZRGBA>
155  * \param[in] points the depth points
156  * \param[in] rgb rgb video frame
157  */
159  convertRGBADepthToPointXYZRGBA ( const rs2::points& points, const rs2::video_frame& rgb );
160 
161  /** \brief template function to convert realsense point cloud to PCL point cloud
162  * \param[in] points - realsense point cloud array
163  * \param[in] mapColorFunc dynamic function to convert individual point color or intensity values
164  */
165  template <typename PointT, typename Functor>
167  convertRealsensePointsToPointCloud ( const rs2::points& points, Functor mapColorFunc );
168 
169  /** \brief Retrieve pixel index for UV texture coordinate
170  * \param[in] texture the texture
171  * \param[in] u 2D coordinate
172  * \param[in] v 2D coordinate
173  */
174  static size_t
175  getTextureIdx (const rs2::video_frame & texture, float u, float v);
176 
177  /** \brief Retrieve RGB color from texture video frame
178  * \param[in] texture the texture
179  * \param[in] u 2D coordinate
180  * \param[in] v 2D coordinate
181  */
182  static pcl::RGB
183  getTextureColor ( const rs2::video_frame& texture, float u, float v );
184 
185  /** \brief Retrieve color intensity from texture video frame
186  * \param[in] texture the texture
187  * \param[in] u 2D coordinate
188  * \param[in] v 2D coordinate
189  */
190  static std::uint8_t
191  getTextureIntensity ( const rs2::video_frame& texture, float u, float v );
192 
193 
194  /** \brief handle to the thread */
195  std::thread thread_;
196  /** \brief Defines either a file path to a bag file or a realsense device serial number. */
198  /** \brief Repeat playback when reading from file */
200  /** \brief controlling the state of the thread. */
201  bool quit_;
202  /** \brief Is the grabber running. */
203  bool running_;
204  /** \brief Calculated FPS for the grabber. */
205  float fps_;
206  /** \brief Width for the depth and color sensor. Default 424*/
207  std::uint32_t device_width_;
208  /** \brief Height for the depth and color sensor. Default 240 */
209  std::uint32_t device_height_;
210  /** \brief Target FPS for the device. Default 30. */
211  std::uint32_t target_fps_;
212  /** \brief Declare pointcloud object, for calculating pointclouds and texture mappings */
213  rs2::pointcloud pc_;
214  /** \brief Declare RealSense pipeline, encapsulating the actual device and sensors */
215  rs2::pipeline pipe_;
216  };
217 
218 }
pcl::RealSense2Grabber::signal_PointXYZ
boost::signals2::signal< signal_librealsense_PointXYZ > * signal_PointXYZ
Definition: real_sense_2_grabber.h:114
pcl
Definition: convolution.h:46
point_types.h
pcl::RealSense2Grabber::signal_PointXYZRGB
boost::signals2::signal< signal_librealsense_PointXYZRGB > * signal_PointXYZRGB
Definition: real_sense_2_grabber.h:116
pcl::RealSense2Grabber::signal_PointXYZI
boost::signals2::signal< signal_librealsense_PointXYZI > * signal_PointXYZI
Definition: real_sense_2_grabber.h:115
pcl::RealSense2Grabber::signal_PointXYZRGBA
boost::signals2::signal< signal_librealsense_PointXYZRGBA > * signal_PointXYZRGBA
Definition: real_sense_2_grabber.h:117
pcl::RealSense2Grabber::fps_
float fps_
Calculated FPS for the grabber.
Definition: real_sense_2_grabber.h:205
pcl::RealSense2Grabber::quit_
bool quit_
controlling the state of the thread.
Definition: real_sense_2_grabber.h:201
pcl::RealSense2Grabber
Grabber for Intel Realsense 2 SDK devices (D400 series)
Definition: real_sense_2_grabber.h:59
pcl::RealSense2Grabber::device_width_
std::uint32_t device_width_
Width for the depth and color sensor.
Definition: real_sense_2_grabber.h:207
pcl::Grabber
Grabber interface for PCL 1.x device drivers.
Definition: grabber.h:59
pcl::RealSense2Grabber::setDeviceOptions
void setDeviceOptions(std::uint32_t width, std::uint32_t height, std::uint32_t fps=30)
Set the device options.
Definition: real_sense_2_grabber.h:77
pcl::RealSense2Grabber::device_height_
std::uint32_t device_height_
Height for the depth and color sensor.
Definition: real_sense_2_grabber.h:209
pcl::Functor
Base functor all the models that need non linear optimization must define their own one and implement...
Definition: sac_model.h:678
pcl::RGB
A structure representing RGB color information.
Definition: point_types.hpp:395
pcl::RealSense2Grabber::pipe_
rs2::pipeline pipe_
Declare RealSense pipeline, encapsulating the actual device and sensors.
Definition: real_sense_2_grabber.h:215
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
pcl::RealSense2Grabber::getName
std::string getName() const override
defined grabber name
Definition: real_sense_2_grabber.h:104
pcl::RealSense2Grabber::running_
bool running_
Is the grabber running.
Definition: real_sense_2_grabber.h:203
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
pcl::RealSense2Grabber::thread_
std::thread thread_
handle to the thread
Definition: real_sense_2_grabber.h:195
pcl::RealSense2Grabber::file_name_or_serial_number_
std::string file_name_or_serial_number_
Defines either a file path to a bag file or a realsense device serial number.
Definition: real_sense_2_grabber.h:197
pcl::RealSense2Grabber::repeat_playback_
bool repeat_playback_
Repeat playback when reading from file.
Definition: real_sense_2_grabber.h:199
pcl::RealSense2Grabber::pc_
rs2::pointcloud pc_
Declare pointcloud object, for calculating pointclouds and texture mappings.
Definition: real_sense_2_grabber.h:213
pcl::RealSense2Grabber::target_fps_
std::uint32_t target_fps_
Target FPS for the device.
Definition: real_sense_2_grabber.h:211
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:323