Go to the documentation of this file.00001 #include <boost/filesystem.hpp>
00002 #include <fstream>
00003 #include <libgen.h>
00004 #include <stdexcept>
00005 #include <stdio.h>
00006 #include <yaml-cpp/yaml.h>
00007
00008 #include <bwi_planning_common/structures.h>
00009 #include <tf/transform_datatypes.h>
00010
00011
00012 #include <SDL/SDL_image.h>
00013
00014 #ifdef HAVE_NEW_YAMLCPP
00015 namespace YAML {
00016
00017
00018 template<typename T>
00019 void operator >> (const YAML::Node& node, T& i)
00020 {
00021 i = node.as<T>();
00022 }
00023 }
00024 #endif
00025
00026 namespace bwi_planning_common {
00027
00028 void readLocationFile(const std::string& filename,
00029 std::vector<std::string>& locations, std::vector<int32_t>& location_map) {
00030
00031 if (!boost::filesystem::exists(filename)) {
00032 throw std::runtime_error("Location file does not exist: " + filename);
00033 }
00034 std::ifstream fin(filename.c_str());
00035
00036 YAML::Node doc;
00037 #ifdef HAVE_NEW_YAMLCPP
00038 doc = YAML::Load(fin);
00039 #else
00040 YAML::Parser parser(fin);
00041 parser.GetNextDocument(doc);
00042 #endif
00043
00044 locations.clear();
00045 const YAML::Node &loc_node = doc["locations"];
00046 for (size_t i = 0; i < loc_node.size(); i++) {
00047 std::string location;
00048 loc_node[i] >> location;
00049 locations.push_back(location);
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059 std::string mapfname;
00060 doc["data"] >> mapfname;
00061 if(mapfname.size() == 0) {
00062 std::string errmsg = "FATAL: The data tag cannot be an empty string.";
00063 throw std::runtime_error(errmsg);
00064 }
00065 if(mapfname[0] != '/') {
00066
00067 char* fname_copy = strdup(filename.c_str());
00068 mapfname = std::string(dirname(fname_copy)) + '/' + mapfname;
00069 free(fname_copy);
00070 }
00071
00072 SDL_Surface* img;
00073
00074 if(!(img = IMG_Load(mapfname.c_str())))
00075 {
00076 std::string errmsg = std::string("failed to open image file \"") + mapfname + std::string("\"");
00077 throw std::runtime_error(errmsg);
00078 }
00079
00080
00081 int rowstride = img->pitch;
00082 int n_channels = img->format->BytesPerPixel;
00083 int avg_channels = (n_channels == 3 || n_channels == 1) ? n_channels : n_channels - 1;
00084
00085
00086 unsigned char* pixels = (unsigned char*)(img->pixels);
00087 location_map.resize(img->h * img->w);
00088
00089 for(int j = 0; j < img->h; j++)
00090 {
00091 for (int i = 0; i < img->w; i++)
00092 {
00093
00094 unsigned char* p = pixels + j*rowstride + i*n_channels;
00095 int color_sum = 0;
00096 for(int k = 0; k < avg_channels; k++)
00097 color_sum += *(p + (k));
00098 int color_avg = color_sum / (double)avg_channels;
00099
00100 int location_idx = (color_avg != 255) ? color_avg : -1;
00101
00102 location_map[(img->h - j - 1) * img->w + i] = color_avg;
00103 }
00104
00105 }
00106
00107 }
00108
00109 void readDoorFile(const std::string& filename, std::vector<Door>& doors) {
00110
00111 if (!boost::filesystem::exists(filename)) {
00112 throw std::runtime_error("Door file does not exist: " + filename);
00113 }
00114
00115 std::ifstream fin(filename.c_str());
00116
00117 YAML::Node doc;
00118 #ifdef HAVE_NEW_YAMLCPP
00119 doc = YAML::Load(fin);
00120 #else
00121 YAML::Parser parser(fin);
00122 parser.GetNextDocument(doc);
00123 #endif
00124
00125 doors.clear();
00126 for (size_t i = 0; i < doc.size(); i++) {
00127 Door door;
00128 const YAML::Node &approach_node = doc[i]["approach"];
00129 doc[i]["door_corner_pt_1"][0] >> door.door_corners[0].x;
00130 doc[i]["door_corner_pt_1"][1] >> door.door_corners[0].y;
00131 doc[i]["door_corner_pt_2"][0] >> door.door_corners[1].x;
00132 doc[i]["door_corner_pt_2"][1] >> door.door_corners[1].y;
00133 for (size_t j = 0; j < 2; ++j) {
00134 approach_node[j]["from"] >> door.approach_names[j];
00135 approach_node[j]["point"][0] >> door.approach_points[j].x;
00136 approach_node[j]["point"][1] >> door.approach_points[j].y;
00137 approach_node[j]["point"][2] >> door.approach_yaw[j];
00138 door.approach_yaw[j] += M_PI;
00139 }
00140 doc[i]["name"] >> door.name;
00141
00142 door.door_center = 0.5 * (door.door_corners[0] + door.door_corners[1]);
00143 door.width = cv::norm(door.door_corners[0] - door.door_corners[1]);
00144 doors.push_back(door);
00145 }
00146 }
00147
00148 bool readObjectApproachFile(const std::string& filename,
00149 std::map<std::string, geometry_msgs::Pose>& object_approach_map) {
00150
00151 object_approach_map.clear();
00152 if (!boost::filesystem::exists(filename)) {
00153
00154 return false;
00155 }
00156
00157 std::ifstream fin(filename.c_str());
00158
00159 YAML::Node doc;
00160 #ifdef HAVE_NEW_YAMLCPP
00161 doc = YAML::Load(fin);
00162 #else
00163 YAML::Parser parser(fin);
00164 parser.GetNextDocument(doc);
00165 #endif
00166
00167 for (size_t i = 0; i < doc.size(); i++) {
00168 std::string name;
00169 geometry_msgs::Pose pose;
00170 float yaw;
00171 doc[i]["name"] >> name;
00172 doc[i]["point"][0] >> pose.position.x;
00173 doc[i]["point"][1] >> pose.position.y;
00174 pose.position.z = 0;
00175 doc[i]["point"][2] >> yaw;
00176 yaw += M_PI;
00177 tf::quaternionTFToMsg(
00178 tf::createQuaternionFromYaw(yaw), pose.orientation);
00179 object_approach_map[name] = pose;
00180 }
00181
00182 fin.close();
00183
00184 return true;
00185 }
00186
00187 size_t resolveDoor(const std::string& door, const std::vector<Door>& doors) {
00188
00189 for (size_t i = 0; i < doors.size(); ++i) {
00190 if (doors[i].name == door) {
00191 return i;
00192 }
00193 }
00194
00195 return bwi_planning_common::NO_DOOR_IDX;
00196 }
00197
00198 }