$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, 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 Willow Garage, Inc. 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 * $Id: common_io.h 48493 2011-02-01 03:49:28Z aaldoma $ 00035 * 00036 */ 00037 00038 #ifndef VFH_CLUSTER_CLASSIFIER_COMMON_IO_H_ 00039 #define VFH_CLUSTER_CLASSIFIER_COMMON_IO_H_ 00040 00041 #include <ros/message_traits.h> 00042 #include <ros/serialization.h> 00043 #include <pcl/point_types.h> 00044 #include <pcl/point_cloud.h> 00045 #include <pcl/io/pcd_io.h> 00046 #include <boost/filesystem.hpp> 00047 #include <fstream> 00048 #include <sstream> 00049 #include "household_objects_database/database_view.h" 00050 #include "household_objects_database/database_vfh.h" 00051 #include "household_objects_database/objects_database.h" 00052 #include "household_objects_database/database_helper_classes.h" 00053 #include <pcl_ros/point_cloud.h> 00054 #include <flann/flann.h> 00055 #include <flann/io/hdf5.h> 00056 #include <fstream> 00057 #include <iostream> 00058 #include <limits> 00059 00060 namespace ser = ros::serialization; 00061 00062 namespace vfh_recognizer_db 00063 { 00064 typedef std::pair<std::string, std::vector<float> > vfh_model; 00065 typedef std::pair<std::string, std::vector<float> > vfh_model_db; 00066 00068 00073 inline bool 00074 loadHist (const boost::filesystem::path &path, vfh_model &vfh) 00075 { 00076 using namespace std; 00077 using namespace pcl; 00078 00079 int vfh_idx; 00080 // Load the file as a PCD 00081 stringstream ss; 00082 ss << path; 00083 try 00084 { 00085 sensor_msgs::PointCloud2 cloud; 00086 int version; 00087 Eigen::Vector4f origin; 00088 Eigen::Quaternionf orientation; 00089 PCDReader r; 00090 bool binary; 00091 int offset; 00092 r.readHeader (ss.str (), cloud, origin, orientation, version, binary, offset); 00093 00094 vfh_idx = getFieldIndex (cloud, "vfh"); 00095 if (vfh_idx == -1) 00096 return (false); 00097 if ((int)cloud.width * cloud.height != 1) 00098 return (false); 00099 } 00100 catch (pcl::InvalidConversionException e) 00101 { 00102 return (false); 00103 } 00104 00105 // Treat the VFH signature as a single Point Cloud 00106 PointCloud < VFHSignature308 > point; 00107 io::loadPCDFile (ss.str (), point); 00108 vfh.second.resize (308); 00109 00110 vector < sensor_msgs::PointField > fields; 00111 getFieldIndex (point, "vfh", fields); 00112 00113 for (size_t i = 0; i < fields[vfh_idx].count; ++i) 00114 { 00115 vfh.second[i] = point.points[0].histogram[i]; 00116 } 00117 vfh.first = ss.str (); 00118 return (true); 00119 } 00120 00122 00127 inline bool 00128 loadHistPlain (const boost::filesystem::path &path, vfh_model &vfh) 00129 { 00130 std::ifstream fs; 00131 std::string line, file_name; 00132 std::stringstream ss; 00133 ss << path; 00134 file_name = ss.str (); 00135 00136 // Open file 00137 fs.open (file_name.c_str ()); 00138 if (!fs.is_open () || fs.fail ()) 00139 { 00140 fprintf (stderr, "Couldn't open %s for reading!\n", file_name.c_str ()); 00141 return (false); 00142 } 00143 00144 // Get the number of lines (points) this histogram file holds 00145 int nr_samples = 0; 00146 while ((!fs.eof ()) && (getline (fs, line))) 00147 nr_samples++; 00148 00149 vfh.second.resize (nr_samples); 00150 00151 fs.clear (); 00152 fs.seekg (0, std::ios_base::beg); 00153 for (int i = 0; i < nr_samples; ++i) 00154 { 00155 getline (fs, line); 00156 if (line == "") 00157 continue; 00158 00159 vfh.second[i] = atof (line.c_str ()); 00160 } 00161 vfh.first = file_name; 00162 // Close file 00163 fs.close (); 00164 return (true); 00165 } 00166 00167 inline void 00168 convertToFLANN (const std::vector<vfh_model_db> &models, flann::Matrix<float> &data) 00169 { 00170 data.rows = models.size (); 00171 data.cols = models[0].second.size (); // number of histogram bins 00172 data.data = (float*)malloc (data.rows * data.cols * sizeof(float)); 00173 00174 for (size_t i = 0; i < data.rows; ++i) 00175 for (size_t j = 0; j < data.cols; ++j) 00176 { 00177 data.data[i * data.cols + j] = models[i].second[j]; 00178 } 00179 } 00180 } 00181 #endif // VFH_CLUSTER_CLASSIFIER_COMMON_IO_H_