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 
39 #pragma once
40 
41 #include <pcl/common/eigen.h>
42 #include <pcl/range_image/range_image_planar.h>
43 #include <pcl/pcl_macros.h>
44 
45 namespace pcl
46 {
47 
48 /////////////////////////////////////////////////////////////////////////
49 template <typename PointCloudType> void
createFromPointCloudWithFixedSize(const PointCloudType & point_cloud,int di_width,int di_height,float di_center_x,float di_center_y,float di_focal_length_x,float di_focal_length_y,const Eigen::Affine3f & sensor_pose,CoordinateFrame coordinate_frame,float noise_level,float min_range)50 RangeImagePlanar::createFromPointCloudWithFixedSize (const PointCloudType& point_cloud,
51                                                      int di_width, int di_height,
52                                                      float di_center_x, float di_center_y,
53                                                      float di_focal_length_x, float di_focal_length_y,
54                                                      const Eigen::Affine3f& sensor_pose,
55                                                      CoordinateFrame coordinate_frame, float noise_level,
56                                                      float min_range)
57 {
58   //std::cout << "Starting to create range image from "<<point_cloud.size ()<<" points.\n";
59 
60   width = di_width;
61   height = di_height;
62   center_x_ = di_center_x;
63   center_y_ = di_center_y;
64   focal_length_x_ = di_focal_length_x;
65   focal_length_y_ = di_focal_length_y;
66   focal_length_x_reciprocal_ = 1 / focal_length_x_;
67   focal_length_y_reciprocal_ = 1 / focal_length_y_;
68 
69   is_dense = false;
70 
71   getCoordinateFrameTransformation (coordinate_frame, to_world_system_);
72   to_world_system_ = sensor_pose * to_world_system_;
73 
74   to_range_image_system_ = to_world_system_.inverse (Eigen::Isometry);
75 
76   unsigned int size = width*height;
77   points.clear ();
78   points.resize (size, unobserved_point);
79 
80   int top=height, right=-1, bottom=-1, left=width;
81   doZBuffer (point_cloud, noise_level, min_range, top, right, bottom, left);
82 
83   // Do not crop
84   //cropImage (border_size, top, right, bottom, left);
85 
86   recalculate3DPointPositions ();
87 }
88 
89 
90 /////////////////////////////////////////////////////////////////////////
91 void
calculate3DPoint(float image_x,float image_y,float range,Eigen::Vector3f & point) const92 RangeImagePlanar::calculate3DPoint (float image_x, float image_y, float range, Eigen::Vector3f& point) const
93 {
94   //std::cout << __PRETTY_FUNCTION__ << " called.\n";
95   float delta_x = (image_x+static_cast<float> (image_offset_x_)-center_x_)*focal_length_x_reciprocal_,
96         delta_y = (image_y+static_cast<float> (image_offset_y_)-center_y_)*focal_length_y_reciprocal_;
97   point[2] = range / (::sqrt (delta_x*delta_x + delta_y*delta_y + 1));
98   point[0] = delta_x*point[2];
99   point[1] = delta_y*point[2];
100   point = to_world_system_ * point;
101 }
102 
103 /////////////////////////////////////////////////////////////////////////
104 inline void
getImagePoint(const Eigen::Vector3f & point,float & image_x,float & image_y,float & range) const105 RangeImagePlanar::getImagePoint (const Eigen::Vector3f& point, float& image_x, float& image_y, float& range) const
106 {
107   Eigen::Vector3f transformedPoint = to_range_image_system_ * point;
108   if (transformedPoint[2]<=0)  // Behind the observer?
109   {
110     image_x = image_y = range = -1.0f;
111     return;
112   }
113   range = transformedPoint.norm ();
114 
115   image_x = center_x_ + focal_length_x_*transformedPoint[0]/transformedPoint[2] - static_cast<float> (image_offset_x_);
116   image_y = center_y_ + focal_length_y_*transformedPoint[1]/transformedPoint[2] - static_cast<float> (image_offset_y_);
117 }
118 
119 }  // namespace pcl
120 
121