-
Notifications
You must be signed in to change notification settings - Fork 172
/
DepthImageToLaserScan.cpp
162 lines (136 loc) · 6.34 KB
/
DepthImageToLaserScan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2012, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Author: Chad Rockey
*/
#include <depthimage_to_laserscan/DepthImageToLaserScan.h>
using namespace depthimage_to_laserscan;
DepthImageToLaserScan::DepthImageToLaserScan(){
}
DepthImageToLaserScan::~DepthImageToLaserScan(){
}
double DepthImageToLaserScan::magnitude_of_ray(const cv::Point3d& ray) const{
return sqrt(pow(ray.x, 2.0) + pow(ray.y, 2.0) + pow(ray.z, 2.0));
}
double DepthImageToLaserScan::angle_between_rays(const cv::Point3d& ray1, const cv::Point3d& ray2) const{
double dot_product = ray1.x*ray2.x + ray1.y*ray2.y + ray1.z*ray2.z;
double magnitude1 = magnitude_of_ray(ray1);
double magnitude2 = magnitude_of_ray(ray2);;
return acos(dot_product / (magnitude1 * magnitude2));
}
bool DepthImageToLaserScan::use_point(const float new_value, const float old_value, const float range_min, const float range_max) const{
// Check for NaNs and Infs, a real number within our limits is more desirable than these.
bool new_finite = std::isfinite(new_value);
bool old_finite = std::isfinite(old_value);
// Infs are preferable over NaNs (more information)
if(!new_finite && !old_finite){ // Both are not NaN or Inf.
if(!isnan(new_value)){ // new is not NaN, so use it's +-Inf value.
return true;
}
return false; // Do not replace old_value
}
// If not in range, don't bother
bool range_check = range_min <= new_value && new_value <= range_max;
if(!range_check){
return false;
}
if(!old_finite){ // New value is in range and finite, use it.
return true;
}
// Finally, if they are both numerical and new_value is closer than old_value, use new_value.
bool shorter_check = new_value < old_value;
return shorter_check;
}
sensor_msgs::LaserScanPtr DepthImageToLaserScan::convert_msg(const sensor_msgs::ImageConstPtr& depth_msg,
const sensor_msgs::CameraInfoConstPtr& info_msg){
// Set camera model
cam_model_.fromCameraInfo(info_msg);
// Calculate angle_min and angle_max by measuring angles between the left ray, right ray, and optical center ray
cv::Point2d raw_pixel_left(0, cam_model_.cy());
cv::Point2d rect_pixel_left = cam_model_.rectifyPoint(raw_pixel_left);
cv::Point3d left_ray = cam_model_.projectPixelTo3dRay(rect_pixel_left);
cv::Point2d raw_pixel_right(depth_msg->width-1, cam_model_.cy());
cv::Point2d rect_pixel_right = cam_model_.rectifyPoint(raw_pixel_right);
cv::Point3d right_ray = cam_model_.projectPixelTo3dRay(rect_pixel_right);
cv::Point2d raw_pixel_center(cam_model_.cx(), cam_model_.cy());
cv::Point2d rect_pixel_center = cam_model_.rectifyPoint(raw_pixel_center);
cv::Point3d center_ray = cam_model_.projectPixelTo3dRay(rect_pixel_center);
double angle_max = angle_between_rays(left_ray, center_ray);
double angle_min = -angle_between_rays(center_ray, right_ray); // Negative because the laserscan message expects an opposite rotation of that from the depth image
// Fill in laserscan message
sensor_msgs::LaserScanPtr scan_msg(new sensor_msgs::LaserScan());
scan_msg->header = depth_msg->header;
if(output_frame_id_.length() > 0){
scan_msg->header.frame_id = output_frame_id_;
}
scan_msg->angle_min = angle_min;
scan_msg->angle_max = angle_max;
scan_msg->angle_increment = (scan_msg->angle_max - scan_msg->angle_min) / (depth_msg->width - 1);
scan_msg->time_increment = 0.0;
scan_msg->scan_time = scan_time_;
scan_msg->range_min = range_min_;
scan_msg->range_max = range_max_;
// Check scan_height vs image_height
if(scan_height_/2 > cam_model_.cy() || scan_height_/2 > depth_msg->height - cam_model_.cy()){
std::stringstream ss;
ss << "scan_height ( " << scan_height_ << " pixels) is too large for the image height.";
throw std::runtime_error(ss.str());
}
// Calculate and fill the ranges
uint32_t ranges_size = depth_msg->width;
scan_msg->ranges.assign(ranges_size, std::numeric_limits<float>::quiet_NaN());
if (depth_msg->encoding == sensor_msgs::image_encodings::TYPE_16UC1)
{
convert<uint16_t>(depth_msg, cam_model_, scan_msg, scan_height_);
}
else if (depth_msg->encoding == sensor_msgs::image_encodings::TYPE_32FC1)
{
convert<float>(depth_msg, cam_model_, scan_msg, scan_height_);
}
else
{
std::stringstream ss;
ss << "Depth image has unsupported encoding: " << depth_msg->encoding;
throw std::runtime_error(ss.str());
}
return scan_msg;
}
void DepthImageToLaserScan::set_scan_time(const float scan_time){
scan_time_ = scan_time;
}
void DepthImageToLaserScan::set_range_limits(const float range_min, const float range_max){
range_min_ = range_min;
range_max_ = range_max;
}
void DepthImageToLaserScan::set_scan_height(const int scan_height){
scan_height_ = scan_height;
}
void DepthImageToLaserScan::set_output_frame(const std::string output_frame_id){
output_frame_id_ = output_frame_id;
}