$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00036 00037 #include <laser_joint_processor/joint_image_interpolator.h> 00038 #include <ros/console.h> 00039 00040 using namespace laser_joint_processor; 00041 using namespace std; 00042 00043 bool JointImageInterpolator::interp(const std::vector <calibration_msgs::ImagePoint>& points, 00044 IplImage* image, std::vector<float>& positions, std::vector<float>& velocities) 00045 { 00046 const unsigned int N = points.size(); 00047 00048 // Do consistency checks 00049 if (image->depth != IPL_DEPTH_32F) 00050 { 00051 ROS_ERROR("Expecting input image to have depth of IPL_DEPTH_32F"); 00052 return false; 00053 } 00054 if (image->nChannels != 2) 00055 { 00056 ROS_ERROR("Expecting input image to have 2 channels. Instead had %i channels", image->nChannels); 00057 return false; 00058 } 00059 00060 // Allocate Maps 00061 vector<float> map_x_vec(N); 00062 vector<float> map_y_vec(N); 00063 CvMat map_x_mat = cvMat(N, 1, CV_32FC1, &map_x_vec[0]); 00064 CvMat map_y_mat = cvMat(N, 1, CV_32FC1, &map_y_vec[0]); 00065 00066 // Set up maps 00067 for (unsigned int i=0; i<N; i++) 00068 { 00069 map_x_vec[i] = points[i].x; 00070 map_y_vec[i] = points[i].y; 00071 } 00072 00073 // Allocate Destination Image 00074 vector<float> dest_vec(2*N); 00075 CvMat dest_mat = cvMat(N, 1, CV_32FC2, &dest_vec[0]); 00076 00077 // Perform the OpenCV interpolation 00078 cvRemap(image, &dest_mat, &map_x_mat, &map_y_mat); 00079 00080 // Copy results into output vectors 00081 positions.resize(N); 00082 velocities.resize(N); 00083 for (unsigned int i=0; i<N; i++) 00084 { 00085 positions[i] = dest_vec[2*i+0]; 00086 velocities[i] = dest_vec[2*i+1]; 00087 } 00088 00089 return true; 00090 } 00091 00092 00093 bool laser_joint_processor::interpSnapshot(const std::vector <calibration_msgs::ImagePoint>& points, 00094 const calibration_msgs::DenseLaserSnapshot& snapshot, 00095 std::vector<float>& angles, 00096 std::vector<float>& ranges) 00097 { 00098 const unsigned int N = points.size(); 00099 00100 // Check to make sure points are in range 00101 for (unsigned int i=0; i<N; i++) 00102 { 00103 if (points[i].x < 0 || 00104 points[i].x > snapshot.readings_per_scan-1 || 00105 points[i].y < 0 || 00106 points[i].y > snapshot.num_scans-1 ) 00107 { 00108 return false; 00109 } 00110 } 00111 00112 // Set up input image 00113 CvMat range_image = cvMat(snapshot.num_scans, snapshot.readings_per_scan, CV_32FC1, (void*) &snapshot.ranges[0]); 00114 00115 // Allocate Maps 00116 vector<float> map_x_vec(N); 00117 vector<float> map_y_vec(N); 00118 CvMat map_x_mat = cvMat(N, 1, CV_32FC1, &map_x_vec[0]); 00119 CvMat map_y_mat = cvMat(N, 1, CV_32FC1, &map_y_vec[0]); 00120 00121 // Set up maps 00122 for (unsigned int i=0; i<N; i++) 00123 { 00124 map_x_vec[i] = points[i].x; 00125 map_y_vec[i] = points[i].y; 00126 } 00127 00128 // Allocate Destination Image 00129 ranges.resize(N); 00130 CvMat ranges_mat = cvMat(N, 1, CV_32FC1, &ranges[0]); 00131 00132 // Perform the OpenCV interpolation 00133 cvRemap(&range_image, &ranges_mat, &map_x_mat, &map_y_mat); 00134 00135 // Do angle interp manually 00136 angles.resize(N); 00137 for (unsigned int i=0; i<N; i++) 00138 angles[i] = snapshot.angle_min + points[i].x*snapshot.angle_increment; 00139 00140 return true; 00141 } 00142 00143 00144 00145 00146