$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 <pcl_ros/point_cloud.h> 00050 #include <flann/flann.h> 00051 #include <flann/io/hdf5.h> 00052 #include <fstream> 00053 #include <iostream> 00054 #include <limits> 00055 00056 namespace ser = ros::serialization; 00057 namespace fs = boost::filesystem; 00058 00059 namespace vfh_recognizer_fs 00060 { 00061 typedef std::pair<std::string, std::vector<float> > vfh_model; 00062 typedef std::pair<std::string, std::vector<float> > vfh_model_db; 00063 00065 00070 /*inline bool 00071 loadHist (const boost::filesystem::path &path, vfh_model &vfh) 00072 { 00073 using namespace std; 00074 using namespace pcl; 00075 00076 int vfh_idx; 00077 // Load the file as a PCD 00078 stringstream ss; 00079 ss << path; 00080 try 00081 { 00082 sensor_msgs::PointCloud2 cloud; 00083 int version; 00084 Eigen::Vector4f origin; 00085 Eigen::Quaternionf orientation; 00086 PCDReader r; 00087 r.readHeader (ss.str (), cloud, origin, orientation, version); 00088 00089 vfh_idx = getFieldIndex (cloud, "vfh"); 00090 if (vfh_idx == -1) 00091 return (false); 00092 if ((int)cloud.width * cloud.height != 1) 00093 return (false); 00094 } 00095 catch (pcl::InvalidConversionException e) 00096 { 00097 return (false); 00098 } 00099 00100 // Treat the VFH signature as a single Point Cloud 00101 PointCloud < VFHSignature308 > point; 00102 io::loadPCDFile (ss.str (), point); 00103 vfh.second.resize (308); 00104 00105 vector < sensor_msgs::PointField > fields; 00106 getFieldIndex (point, "vfh", fields); 00107 00108 for (size_t i = 0; i < fields[vfh_idx].count; ++i) 00109 { 00110 vfh.second[i] = point.points[0].histogram[i]; 00111 } 00112 vfh.first = ss.str (); 00113 return (true); 00114 }*/ 00115 00117 00122 /*inline bool 00123 loadHistPlain (const boost::filesystem::path &path, vfh_model &vfh) 00124 { 00125 std::ifstream fs; 00126 std::string line, file_name; 00127 std::stringstream ss; 00128 ss << path; 00129 file_name = ss.str (); 00130 00131 // Open file 00132 fs.open (file_name.c_str ()); 00133 if (!fs.is_open () || fs.fail ()) 00134 { 00135 fprintf (stderr, "Couldn't open %s for reading!\n", file_name.c_str ()); 00136 return (false); 00137 } 00138 00139 // Get the number of lines (points) this histogram file holds 00140 int nr_samples = 0; 00141 while ((!fs.eof ()) && (getline (fs, line))) 00142 nr_samples++; 00143 00144 vfh.second.resize (nr_samples); 00145 00146 fs.clear (); 00147 fs.seekg (0, std::ios_base::beg); 00148 for (int i = 0; i < nr_samples; ++i) 00149 { 00150 getline (fs, line); 00151 if (line == "") 00152 continue; 00153 00154 vfh.second[i] = atof (line.c_str ()); 00155 } 00156 vfh.first = file_name; 00157 // Close file 00158 fs.close (); 00159 return (true); 00160 }*/ 00161 00162 inline void 00163 convertToFLANN (const std::vector<vfh_model_db> &models, flann::Matrix<float> &data) 00164 { 00165 data.rows = models.size (); 00166 data.cols = models[0].second.size (); // number of histogram bins 00167 data.data = (float*)malloc (data.rows * data.cols * sizeof(float)); 00168 00169 for (size_t i = 0; i < data.rows; ++i) 00170 for (size_t j = 0; j < data.cols; ++j) 00171 { 00172 data.data[i * data.cols + j] = models[i].second[j]; 00173 } 00174 } 00175 00176 inline std::string 00177 getModelId(fs::path path) { 00178 //split path by _ and get the last number 00179 std::string path_str = path.string(); 00180 std::vector<std::string> strs; 00181 boost::split(strs, path_str, boost::is_any_of("/")); 00182 00183 std::string filename = strs[strs.size() - 1]; 00184 boost::replace_last (filename, "vfh_", ""); 00185 boost::replace_last (filename, ".pcd", ""); 00186 00187 return filename; 00188 } 00189 00190 inline bool 00191 buildTreeFromFiles (std::vector<vfh_model_db> & models, flann::Matrix<float> & data, std::string vfh_directory) 00192 { 00193 00194 fs::path full_path = vfh_directory; 00195 00196 if (!fs::exists (full_path)) 00197 { 00198 #if (!defined BOOST_FILESYSTEM_VERSION) || (BOOST_FILESYSTEM_VERSION<=2) 00199 std::cout << "\nNot found: " << full_path.native_file_string () << std::endl; 00200 #else 00201 std::cout << "\nNot found: " << full_path.string () << std::endl; 00202 #endif 00203 return 1; 00204 } 00205 00206 if (fs::is_directory (full_path)) 00207 { 00208 #if (!defined BOOST_FILESYSTEM_VERSION) || (BOOST_FILESYSTEM_VERSION<=2) 00209 std::cout << "\nIn directory: " << full_path.native_directory_string () << "lolo\n\n"; 00210 #else 00211 std::cout << "\nIn directory: " << full_path.string () << "lolo\n\n"; 00212 #endif 00213 00214 fs::directory_iterator end_iter; 00215 for (fs::directory_iterator dir_itr (full_path); dir_itr != end_iter; ++dir_itr) 00216 { 00217 #if (!defined BOOST_FILESYSTEM_VERSION) || (BOOST_FILESYSTEM_VERSION<=2) 00218 std::cout << "lolo" << (*dir_itr).string() << std::endl; 00219 #else 00220 std::cout << "lolo" << (*dir_itr).path().string() << std::endl; 00221 #endif 00222 if (!fs::is_directory(*dir_itr)) { 00223 //vfh pcd.. 00224 vfh_model_db vfh; 00225 std::cout << "lolo" << getModelId(*dir_itr) << std::endl; 00226 vfh.first = getModelId(*dir_itr); 00227 vfh.second.resize (308); 00228 pcl::PointCloud<pcl::VFHSignature308>::Ptr vfh_signaturePtr (new pcl::PointCloud<pcl::VFHSignature308> ()); 00229 #if (!defined BOOST_FILESYSTEM_VERSION) || (BOOST_FILESYSTEM_VERSION<=2) 00230 pcl::io::loadPCDFile((*dir_itr).string(), *vfh_signaturePtr); 00231 #else 00232 pcl::io::loadPCDFile((*dir_itr).path().string(), *vfh_signaturePtr); 00233 #endif 00234 memcpy (&vfh.second[0], &vfh_signaturePtr->points[0].histogram[0], 308 * sizeof(float)); 00235 models.push_back (vfh); 00236 } 00237 } 00238 } 00239 00240 std::cout << "models size:" << models.size() << std::endl; 00241 00242 convertToFLANN (models, data); 00243 return true; 00244 } 00245 } 00246 #endif // VFH_CLUSTER_CLASSIFIER_COMMON_IO_H_