00001 /*!@file BeoSub/BeoSubDB.H */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/BeoSubDB.H $ 00035 // $Id: BeoSubDB.H 6003 2005-11-29 17:22:45Z rjpeters $ 00036 // 00037 00038 #ifndef BEOSUBDB_H_DEFINED 00039 #define BEOSUBDB_H_DEFINED 00040 00041 #include "Util/Angle.H" 00042 #include "Image/Image.H" 00043 #include "Image/Pixels.H" 00044 00045 #include "BeoSub/Attitude.H" 00046 00047 #include <fstream> 00048 #include <cstdlib> 00049 #include <map> 00050 #include <list> 00051 00052 struct MappingData{ 00053 std::string itsImgFilename; 00054 Angle itsHeading, itsPitch, itsRoll; 00055 float itsXpos, itsYpos, itsZpos; 00056 float itsFballast, itsRballast, itsRthruster, itsLthruster; 00057 }; 00058 00059 typedef std::pair <std::string, MappingData> MapPair; 00060 00061 //! A BeoSub mapping database 00062 /*! Structure is as follows: 00063 IMAGE, COMPASS(heading pitch roll), ACTUATORS (ballast[front rear], thrusters[left right]), POSITION (x, y, z) 00064 00065 NOTE: zPos is based off of the depth sensor 00066 */ 00067 class BeoSubDB 00068 { 00069 public: 00070 // ###################################################################### 00071 /*! @name Constructors and Destructors */ 00072 //@{ 00073 00074 //! Default Constructor 00075 /*! This constructor reads in from a hardcoded directory all database data and reads it into a map*/ 00076 inline BeoSubDB(); 00077 00078 //! Destructor 00079 inline ~BeoSubDB(); 00080 00081 inline bool initializeDatabase(std::list<std::string> filenames, std::string destFilename); 00082 00083 inline bool loadDatabase(std::string filename); 00084 00085 inline void saveDatabase(std::map<std::string, MappingData> inDB, std::string filename); 00086 00087 inline MappingData getMappingData(std::string name); 00088 00089 inline uint getNumEntries(); 00090 00091 inline void getDirections(MappingData from, MappingData to, Attitude& directions, float& dist); 00092 00093 //@} 00094 00095 private: 00096 // ###################################################################### 00097 MappingData inData; //!< structure used to read in and store database members 00098 std::map<std::string, MappingData> itsDatabase; 00099 std::ifstream inFile; 00100 std::ofstream outFile; 00101 }; 00102 00103 00104 // ###################################################################### 00105 // ########## Inlined methods: 00106 // ###################################################################### 00107 inline BeoSubDB::BeoSubDB() 00108 { 00109 } 00110 00111 // ###################################################################### 00112 inline BeoSubDB::~BeoSubDB() 00113 { } 00114 00115 // ###################################################################### 00116 //! Returns true if files found, false otherwise 00117 inline bool BeoSubDB::initializeDatabase(std::list<std::string> filenames, std::string destFilename){ 00118 std::string tempImg; 00119 float tempHeading = 0.0, tempPitch = 0.0, tempRoll = 0.0, tempXpos = 0.0, tempYpos = 0.0, tempZpos = 0.0, tempFballast = 0.0, tempRballast = 0.0, tempLthruster = 0.0, tempRthruster = 0.0; 00120 std::list<std::string>::iterator fileIter; 00121 00122 for(fileIter = filenames.begin(); fileIter != filenames.end(); fileIter++){ 00123 inFile.open((*fileIter).c_str(), std::ifstream::in); 00124 if(inFile.fail()){ 00125 LINFO("File %s is not a valid filename. Please specify a valid filename.", (*fileIter).c_str()); 00126 return false; 00127 } 00128 00129 //read in data entry from file 00130 inFile>>tempImg; 00131 inFile>>tempHeading; 00132 inFile>>tempPitch; 00133 inFile>>tempRoll; 00134 inFile>>tempZpos; 00135 inFile>>tempFballast; 00136 inFile>>tempRballast; 00137 inFile>>tempLthruster; 00138 inFile>>tempRthruster; 00139 inFile>>tempXpos; 00140 inFile>>tempYpos; 00141 // printf("Loading-- %s: Heading %f, Pitch %f, Roll %f, Depth %f, \nFront Ballast %f, Rear Ballast %f, Left Thruster %f, Right Thruster %f, \nX Position %f, Y Position %f\n\n", tempImg, tempHeading, tempPitch, tempRoll, tempZpos, tempFballast, tempRballast, tempLthruster, tempRthruster, tempXpos, tempYpos); 00142 //convert to appropriate datatypes 00143 inData.itsImgFilename = tempImg; 00144 inData.itsHeading.setVal(tempHeading); 00145 inData.itsPitch.setVal(tempPitch); 00146 inData.itsRoll.setVal(tempRoll); 00147 inData.itsFballast = tempFballast; 00148 inData.itsRballast = tempRballast; 00149 inData.itsLthruster = tempLthruster; 00150 inData.itsRthruster = tempRthruster; 00151 inData.itsXpos = tempXpos; 00152 inData.itsYpos = tempYpos; 00153 inData.itsZpos = tempZpos; 00154 //printf("Saving-- %s: Heading %f, Pitch %f, Roll %f, Depth %f, \nFront Ballast %f, Rear Ballast %f, Left Thruster %f, Right Thruster %f, \nX Position %f, Y Position %f\n\n", inData.itsImgFilename, inData.itsHeading.getVal(), inData.itsPitch.getVal(), inData.itsRoll.getVal(), inData.itsZpos, inData.itsFballast, inData.itsRballast, inData.itsLthruster, inData.itsRthruster, inData.itsXpos, inData.itsYpos); 00155 //read entry into database map 00156 itsDatabase.insert(MapPair(inData.itsImgFilename, inData)); 00157 00158 inFile.close(); 00159 } 00160 00161 saveDatabase(itsDatabase, destFilename); 00162 return true; 00163 } 00164 00165 // ###################################################################### 00166 //! Returns true if file found, false otherwise 00167 inline bool BeoSubDB::loadDatabase(std::string filename){ 00168 std::string tempImg; 00169 float tempHeading = 0.0, tempPitch = 0.0, tempRoll = 0.0, tempXpos = 0.0, tempYpos = 0.0, tempZpos = 0.0, tempFballast = 0.0, tempRballast = 0.0, tempLthruster = 0.0, tempRthruster = 0.0; 00170 00171 inFile.open(filename.c_str(), std::ifstream::in); 00172 if(inFile.fail()){ 00173 LINFO("Database read failed. Please specify a valid filename."); 00174 return false; 00175 } 00176 00177 while(!inFile.eof()){//until all data entries are read... 00178 //read in data entry from file 00179 inFile>>tempImg; 00180 inFile>>tempHeading; 00181 inFile>>tempPitch; 00182 inFile>>tempRoll; 00183 inFile>>tempZpos; 00184 inFile>>tempFballast; 00185 inFile>>tempRballast; 00186 inFile>>tempLthruster; 00187 inFile>>tempRthruster; 00188 inFile>>tempXpos; 00189 inFile>>tempYpos; 00190 00191 //convert to appropriate datatypes 00192 inData.itsImgFilename = tempImg; 00193 inData.itsHeading = tempHeading; 00194 inData.itsPitch = tempPitch; 00195 inData.itsRoll = tempRoll; 00196 inData.itsFballast = tempFballast; 00197 inData.itsRballast = tempRballast; 00198 inData.itsLthruster = tempLthruster; 00199 inData.itsRthruster = tempRthruster; 00200 inData.itsXpos = tempXpos; 00201 inData.itsYpos = tempYpos; 00202 inData.itsZpos = tempZpos; 00203 //NOTE: may need to take care of line ends here. FIX? 00204 //read entry into databasemap 00205 itsDatabase.insert(MapPair(inData.itsImgFilename, inData)); 00206 } 00207 inFile.close(); 00208 return true; 00209 } 00210 00211 // ###################################################################### 00212 //! Saves map to file 00213 inline void BeoSubDB::saveDatabase(std::map<std::string, MappingData> inDB, std::string filename){ 00214 itsDatabase = inDB; 00215 std::map<std::string, MappingData>::iterator dbIter = inDB.begin(); 00216 outFile.open(filename.c_str(), std::ifstream::out); 00217 00218 if(outFile.fail()){ 00219 LINFO("Database file %s not found. Please specify a valid filename", filename.c_str()); 00220 return; 00221 } 00222 //printf("Database size is %d elements\n", inDB.size()); 00223 00224 while(dbIter != inDB.end()){ 00225 outFile<<((*dbIter).second).itsImgFilename<<"\t"; 00226 outFile<<((*dbIter).second).itsHeading.getVal()<<"\t"; 00227 outFile<<((*dbIter).second).itsPitch.getVal()<<"\t"; 00228 outFile<<((*dbIter).second).itsRoll.getVal()<<"\t"; 00229 outFile<<((*dbIter).second).itsZpos<<"\t"; 00230 outFile<<((*dbIter).second).itsFballast<<"\t"; 00231 outFile<<((*dbIter).second).itsRballast<<"\t"; 00232 outFile<<((*dbIter).second).itsLthruster<<"\t"; 00233 outFile<<((*dbIter).second).itsRthruster<<"\t"; 00234 outFile<<((*dbIter).second).itsXpos<<"\t"; 00235 outFile<<((*dbIter).second).itsYpos; 00236 outFile<<"\n"; 00237 00238 dbIter++; 00239 } 00240 outFile.close(); 00241 return; 00242 } 00243 00244 // ###################################################################### 00245 //! Returns map element at key "name" 00246 inline MappingData BeoSubDB::getMappingData(std::string name){ 00247 //NOTE: should have a check if the element exists in the database. However, map doesnt have a simple check for this that I see. FIX? 00248 return itsDatabase[name]; 00249 } 00250 00251 // ###################################################################### 00252 //! Returns size of the database 00253 inline uint BeoSubDB::getNumEntries(){ 00254 return itsDatabase.size(); 00255 } 00256 00257 // ###################################################################### 00258 //! This function returns by reference the attitude and distance necessary to reach a target state given the current state (represented in MappingData objects) 00259 inline void BeoSubDB::getDirections(MappingData from, MappingData to, Attitude& directions, float& dist){ 00260 float deltaX = to.itsXpos - from.itsXpos; 00261 float deltaY = to.itsYpos - from.itsYpos; 00262 00263 dist = sqrt((deltaX*deltaX) + (deltaY*deltaY)); //needed distance 00264 float angle = (atan2(deltaY, deltaX) * (180/3.14159265)); //needed heading in degrees; 00265 00266 directions.heading = angle; 00267 directions.pitch = to.itsPitch; 00268 directions.roll = to.itsRoll; 00269 directions.depth = to.itsZpos; 00270 //NOTE: what to do about time vars? nothing? 00271 } 00272 #endif 00273 00274 // ###################################################################### 00275 /* So things look consistent in everyone's emacs... */ 00276 /* Local Variables: */ 00277 /* indent-tabs-mode: nil */ 00278 /* End: */