Point Cloud Library (PCL)  1.14.0-dev
range_image_planar.hpp
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
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;
68 
69  is_dense = false;
70 
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 
87 }
88 
89 
90 /////////////////////////////////////////////////////////////////////////
91 void
92 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 / (std::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
105 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 
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:403
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
std::vector< PointWithRange, Eigen::aligned_allocator< PointWithRange > > points
The point data.
Definition: point_cloud.h:395
PCL_EXPORTS void recalculate3DPointPositions()
Recalculate all 3D point positions according to their pixel position and range.
int image_offset_y_
Position of the top left corner of the range image compared to an image of full size (360x180 degrees...
Definition: range_image.h:776
Eigen::Affine3f to_world_system_
Inverse of to_range_image_system_.
Definition: range_image.h:769
PointWithRange unobserved_point
This point is used to be able to return a reference to a non-existing point.
Definition: range_image.h:778
void doZBuffer(const PointCloudType &point_cloud, float noise_level, float min_range, int &top, int &right, int &bottom, int &left)
Integrate the given point cloud into the current range image using a z-buffer.
Eigen::Affine3f to_range_image_system_
Inverse of to_world_system_.
Definition: range_image.h:768
static PCL_EXPORTS void getCoordinateFrameTransformation(RangeImage::CoordinateFrame coordinate_frame, Eigen::Affine3f &transformation)
Get the transformation that transforms the given coordinate frame into CAMERA_FRAME.
void calculate3DPoint(float image_x, float image_y, float range, PointWithRange &point) const
Calculate the 3D point according to the given image point and range.
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=CAMERA_FRAME, float noise_level=0.0f, float min_range=0.0f)
Create the image from an existing point cloud.
float center_y_
The principle point of the image.
float focal_length_y_reciprocal_
1/focal_length -> for internal use
virtual void getImagePoint(const Eigen::Vector3f &point, float &image_x, float &image_y, float &range) const
Get imagePoint from 3D point in world coordinates.
float focal_length_y_
The focal length of the image in pixels.
Defines all the PCL and non-PCL macros used.