1 /*
2  * Software License Agreement (BSD License)
3  *
4  *  Point Cloud Library (PCL) - www.pointclouds.org
5  *  Copyright (c) 2010, Willow Garage, Inc.
6  *  Copyright (c) 2012-, Open Perception, Inc.
7  *
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *   * Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *   * Redistributions in binary form must reproduce the above
17  *     copyright notice, this list of conditions and the following
18  *     disclaimer in the documentation and/or other materials provided
19  *     with the distribution.
20  *   * Neither the name of the copyright holder(s) nor the names of its
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  *  POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #pragma once
39 
40 #include <pcl/range_image/range_image.h>
41 
42 namespace pcl
43 {
44   /** \brief @b RangeImagePlanar is derived from the original range image and differs from it because it's not a
45     * spherical projection, but using a projection plane (as normal cameras do), therefore being better applicable
46     * for range sensors that already provide a range image by themselves (stereo cameras, ToF-cameras), so that
47     * a conversion to point cloud and then to a spherical range image becomes unnecessary.
48     * \author Bastian Steder
49     * \ingroup range_image
50     */
51   class RangeImagePlanar : public RangeImage
52   {
53     public:
54       // =====TYPEDEFS=====
55       using BaseClass = RangeImage;
56       using Ptr = shared_ptr<RangeImagePlanar>;
57       using ConstPtr = shared_ptr<const RangeImagePlanar>;
58 
59       // =====CONSTRUCTOR & DESTRUCTOR=====
60       /** Constructor */
61       PCL_EXPORTS RangeImagePlanar ();
62       /** Destructor */
63       PCL_EXPORTS ~RangeImagePlanar ();
64 
65       /** Return a newly created RangeImagePlanar.
66        *  Reimplementation to return an image of the same type. */
67       RangeImage*
getNew()68       getNew () const override { return new RangeImagePlanar; }
69 
70       /** Copy *this to other. Derived version - also copying additional RangeImagePlanar members */
71       PCL_EXPORTS void
72       copyTo (RangeImage& other) const override;
73 
74       // =====PUBLIC METHODS=====
75       /** \brief Get a boost shared pointer of a copy of this */
76       inline Ptr
makeShared()77       makeShared () { return Ptr (new RangeImagePlanar (*this)); }
78 
79       /** \brief Create the image from an existing disparity image.
80         * \param disparity_image the input disparity image data
81         * \param di_width the disparity image width
82         * \param di_height the disparity image height
83         * \param focal_length the focal length of the primary camera that generated the disparity image
84         * \param base_line the baseline of the stereo pair that generated the disparity image
85         * \param desired_angular_resolution If this is set, the system will skip as many pixels as necessary to get as
86         *         close to this angular resolution as possible while not going over this value (the density will not be
87         *         lower than this value). The value is in radians per pixel.
88         */
89       PCL_EXPORTS void
90       setDisparityImage (const float* disparity_image, int di_width, int di_height,
91                          float focal_length, float base_line, float desired_angular_resolution=-1);
92 
93       /** Create the image from an existing depth image.
94         * \param depth_image the input depth image data as float values
95         * \param di_width the disparity image width
96         * \param di_height the disparity image height
97         * \param di_center_x the x-coordinate of the camera's center of projection
98         * \param di_center_y the y-coordinate of the camera's center of projection
99         * \param di_focal_length_x the camera's focal length in the horizontal direction
100         * \param di_focal_length_y the camera's focal length in the vertical direction
101         * \param desired_angular_resolution If this is set, the system will skip as many pixels as necessary to get as
102         *         close to this angular resolution as possible while not going over this value (the density will not be
103         *         lower than this value). The value is in radians per pixel.
104         */
105       PCL_EXPORTS void
106       setDepthImage (const float* depth_image, int di_width, int di_height, float di_center_x, float di_center_y,
107                      float di_focal_length_x, float di_focal_length_y, float desired_angular_resolution=-1);
108 
109       /** Create the image from an existing depth image.
110         * \param depth_image the input disparity image data as short values describing millimeters
111         * \param di_width the disparity image width
112         * \param di_height the disparity image height
113         * \param di_center_x the x-coordinate of the camera's center of projection
114         * \param di_center_y the y-coordinate of the camera's center of projection
115         * \param di_focal_length_x the camera's focal length in the horizontal direction
116         * \param di_focal_length_y the camera's focal length in the vertical direction
117         * \param desired_angular_resolution If this is set, the system will skip as many pixels as necessary to get as
118         *         close to this angular resolution as possible while not going over this value (the density will not be
119         *         lower than this value). The value is in radians per pixel.
120         */
121       PCL_EXPORTS void
122       setDepthImage (const unsigned short* depth_image, int di_width, int di_height, float di_center_x, float di_center_y,
123                      float di_focal_length_x, float di_focal_length_y, float desired_angular_resolution=-1);
124 
125       /** Create the image from an existing point cloud.
126         * \param point_cloud the source point cloud
127         * \param di_width the disparity image width
128         * \param di_height the disparity image height
129         * \param di_center_x the x-coordinate of the camera's center of projection
130         * \param di_center_y the y-coordinate of the camera's center of projection
131         * \param di_focal_length_x the camera's focal length in the horizontal direction
132         * \param di_focal_length_y the camera's focal length in the vertical direction
133         * \param sensor_pose the pose of the virtual depth camera
134         * \param coordinate_frame the used coordinate frame of the point cloud
135         * \param noise_level what is the typical noise of the sensor - is used for averaging in the z-buffer
136         * \param min_range minimum range to consifder points
137         */
138       template <typename PointCloudType> void
139       createFromPointCloudWithFixedSize (const PointCloudType& point_cloud,
140                                          int di_width, int di_height, float di_center_x, float di_center_y,
141                                          float di_focal_length_x, float di_focal_length_y,
142                                          const Eigen::Affine3f& sensor_pose,
143                                          CoordinateFrame coordinate_frame=CAMERA_FRAME, float noise_level=0.0f,
144                                          float min_range=0.0f);
145 
146       // Since we reimplement some of these overloaded functions, we have to do the following:
147       using RangeImage::calculate3DPoint;
148       using RangeImage::getImagePoint;
149 
150       /** \brief Calculate the 3D point according to the given image point and range
151         * \param image_x the x image position
152         * \param image_y the y image position
153         * \param range the range
154         * \param point the resulting 3D point
155         * \note Implementation according to planar range images (compared to spherical as in the original)
156         */
157       inline void
158       calculate3DPoint (float image_x, float image_y, float range, Eigen::Vector3f& point) const override;
159 
160       /** \brief Calculate the image point and range from the given 3D point
161         * \param point the resulting 3D point
162         * \param image_x the resulting x image position
163         * \param image_y the resulting y image position
164         * \param range the resulting range
165         * \note Implementation according to planar range images (compared to spherical as in the original)
166         */
167       inline void
168       getImagePoint (const Eigen::Vector3f& point, float& image_x, float& image_y, float& range) const override;
169 
170       /** Get a sub part of the complete image as a new range image.
171         * \param sub_image_image_offset_x - The x coordinate of the top left pixel of the sub image.
172         *                         This is always according to absolute 0,0 meaning -180°,-90°
173         *                         and it is already in the system of the new image, so the
174         *                         actual pixel used in the original image is
175         *                         combine_pixels* (image_offset_x-image_offset_x_)
176         * \param sub_image_image_offset_y - Same as image_offset_x for the y coordinate
177         * \param sub_image_width - width of the new image
178         * \param sub_image_height - height of the new image
179         * \param combine_pixels - shrinking factor, meaning the new angular resolution
180         *                         is combine_pixels times the old one
181         * \param sub_image - the output image
182         */
183       PCL_EXPORTS void
184       getSubImage (int sub_image_image_offset_x, int sub_image_image_offset_y, int sub_image_width,
185                    int sub_image_height, int combine_pixels, RangeImage& sub_image) const override;
186 
187       //! Get a range image with half the resolution
188       PCL_EXPORTS void
189       getHalfImage (RangeImage& half_image) const override;
190 
191       //! Getter for the focal length in X
192       inline float
getFocalLengthX()193       getFocalLengthX () const { return focal_length_x_; }
194 
195       //! Getter for the focal length in Y
196       inline float
getFocalLengthY()197       getFocalLengthY () const { return focal_length_y_; }
198 
199       //! Getter for the principal point in X
200       inline float
getCenterX()201       getCenterX () const { return center_x_; }
202 
203       //! Getter for the principal point in Y
204       inline float
getCenterY()205       getCenterY () const { return center_y_; }
206 
207 
208     protected:
209       float focal_length_x_, focal_length_y_; //!< The focal length of the image in pixels
210       float focal_length_x_reciprocal_, focal_length_y_reciprocal_;  //!< 1/focal_length -> for internal use
211       float center_x_, center_y_;      //!< The principle point of the image
212   };
213 }  // namespace end
214 
215 
216 #include <pcl/range_image/impl/range_image_planar.hpp>  // Definitions of templated and inline functions
217