utils.hpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  The MIT License (MIT)
00003 
00004  Copyright (c) <2013> <Vojtech Novak>
00005 
00006  Permission is hereby granted, free of charge, to any person obtaining a copy
00007  of this software and associated documentation files (the "Software"), to deal
00008  in the Software without restriction, including without limitation the rights
00009  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  copies of the Software, and to permit persons to whom the Software is
00011  furnished to do so, subject to the following conditions:
00012 
00013  The above copyright notice and this permission notice shall be included in
00014  all copies or substantial portions of the Software.
00015 
00016  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  THE SOFTWARE.
00023  *********************************************************************/
00024 
00025 #ifndef TEXT_LOCATOR_UTILS_HPP_
00026 #define TEXT_LOCATOR_UTILS_HPP_
00027 #include <string>
00028 #include <fstream>
00029 #include <boost/algorithm/string.hpp>
00030 #include <ccv.h>
00031 #include <iostream>
00032 #include <stdio.h>
00033 #include <vector>
00034 #include <pwd.h>
00035 #include <fstream>
00036 
00037 namespace ros_text_locator {
00038 
00039 inline std::pair<int, int> getTargetCoordinate(int i,
00040                 std::vector<Text2D>& text2d) {
00041         std::pair<int, int> ret;
00042         ret.first = -1;
00043         ret.second = -1;
00044         if (i == -1) {
00045                 return ret;
00046         }
00047         ret.first = (text2d.at(i).x2 - text2d.at(i).x) / 2 + text2d.at(i).x;
00048         ret.second = text2d.at(i).y;
00049         //ret.second = (text2d.at(i).y2 - text2d.at(i).y) / 2 + text2d.at(i).y;
00050         //std::cout << "coordinates: x: " << ret.first << " y: " << ret.second << std::endl;
00051         return ret;
00052 }
00053 
00054 inline std::string &trim(std::string &s) {
00055         boost::algorithm::trim(s);
00056         return s;
00057 }
00058 
00059 inline std::string readFile(std::string file) {
00060         std::ifstream in(file.c_str());
00061         std::string contents((std::istreambuf_iterator<char>(in)),
00062                         std::istreambuf_iterator<char>());
00063         return contents;
00064 }
00065 
00066 inline std::pair<int, int> openni2ModeToRes(int mode) {
00067         std::pair<int, int> ret;
00068         switch (mode) {
00069         case 1:
00070         case 2:
00071                 ret.first = 1280;
00072                 ret.second = 1024;
00073                 break;
00074         case 3:
00075         case 4:
00076                 ret.first = 1280;
00077                 ret.second = 720;
00078                 break;
00079         case 5:
00080         case 6:
00081                 ret.first = 640;
00082                 ret.second = 480;
00083                 break;
00084         case 7:
00085         case 8:
00086         case 9:
00087                 ret.first = 320;
00088                 ret.second = 240;
00089                 break;
00090         case 10:
00091         case 11:
00092         case 12:
00093                 ret.first = 160;
00094                 ret.second = 120;
00095                 break;
00096         default:
00097                 ret.first = -1;
00098                 ret.second = -1;
00099                 break;
00100         }
00101         return ret;
00102 }
00103 
00104 inline std::pair<int, int> openniModeToRes(int mode) {
00105         std::pair<int, int> ret;
00106         switch (mode) {
00107         case 1:
00108                 ret.first = 1280;
00109                 ret.second = 1024;
00110                 break;
00111         case 2:
00112         case 3:
00113                 ret.first = 640;
00114                 ret.second = 480;
00115                 break;
00116         case 4:
00117         case 5:
00118         case 6:
00119                 ret.first = 320;
00120                 ret.second = 240;
00121                 break;
00122         case 7:
00123         case 8:
00124         case 9:
00125                 ret.first = 160;
00126                 ret.second = 120;
00127                 break;
00128         default:
00129                 ret.first = -1;
00130                 ret.second = -1;
00131                 break;
00132         }
00133         return ret;
00134 }
00135 
00136 inline std::string exec(char* cmd) {
00137         FILE* pipe = popen(cmd, "r");
00138         if (!pipe)
00139                 return "ERROR";
00140         char buffer[128];
00141         std::string result = "";
00142         while (!feof(pipe)) {
00143                 if (fgets(buffer, 128, pipe) != NULL)
00144                         result += buffer;
00145         }
00146         pclose(pipe);
00147         return result;
00148 }
00149 
00150 inline std::string getHome() {
00151         using namespace std;
00152         int myuid;
00153         passwd *mypasswd;
00154         myuid = getuid();
00155         mypasswd = getpwuid(myuid);
00156         return mypasswd->pw_dir;
00157 }
00158 
00159 inline int findMatch(std::string &in, std::vector<Text2D>& text2d) {
00160         for (size_t i = 0; i < text2d.size(); i++) {
00161                 if (text2d.at(i).text == in) {
00162                         std::cout << "100% match! " << std::endl;
00163                         return i;
00164                 }
00165         }
00166         return -1;
00167 }
00168 }
00169 #endif /* TEXT_LOCATOR_UTILS_HPP_ */


text_locator
Author(s): Vojtech Novak
autogenerated on Mon Oct 6 2014 07:55:19