Go to the documentation of this file.00001
00022 #include <grasp_planning_graspit/GraspItSimpleDBManager.h>
00023 #include <grasp_planning_graspit/LogBinding.h>
00024
00025 #include <graspit/robot.h>
00026
00027 #include <map>
00028 #include <string>
00029 #include <vector>
00030
00031 using GraspIt::GraspItSimpleDBManager;
00032
00033 int GraspItSimpleDBManager::loadRobotToDatabase(const std::string& filename, const std::string& robotName,
00034 const std::vector<std::string>& jointNames)
00035 {
00036 if (robotName.empty())
00037 {
00038 PRINTERROR("You have to specify a robot name");
00039 return -5;
00040 }
00041
00042 UNIQUE_RECURSIVE_LOCK(dbMtx);
00043
00044 std::map<std::string, Robot*>::iterator it = robots.find(robotName);
00045 if (it != robots.end())
00046 {
00047 PRINTERROR("Robot with name " << robotName << " already exists in the database.");
00048 return -4;
00049 }
00050
00051 PRINTMSG("Loading robot");
00052 Robot * loadedRobot = NULL;
00053 {
00054
00055
00056
00057
00058 UNIQUE_RECURSIVE_LOCK lock = getUniqueWorldLock();
00059
00060 int ret = getGraspItSceneManager()->loadRobot(filename, robotName);
00061 if (ret != 0)
00062 {
00063 PRINTERROR("Could not load robot " << robotName);
00064 return ret;
00065 }
00066
00067
00068
00069 loadedRobot = getRobot(robotName);
00070
00071
00072
00073
00074 if (!removeElement(loadedRobot, false))
00075 {
00076 PRINTERROR("FATAL: should have been able to remove the robot. System could now be insconsistent.");
00077 return -6;
00078 }
00079 }
00080
00081
00082 if (!robots.insert(std::make_pair(robotName, loadedRobot)).second)
00083 {
00084 PRINTERROR("Failed to insert robot into the map");
00085 return -6;
00086 }
00087
00088 if (!robotJointNames.insert(std::make_pair(robotName, jointNames)).second)
00089 {
00090 PRINTERROR("Failed to insert robot joint names into the map");
00091 return -6;
00092 }
00093
00094 ++modelIdCounter;
00095 modelIDs.insert(std::make_pair(modelIdCounter, std::make_pair(robotName, true)));
00096
00097
00098 return modelIdCounter;
00099 }
00100
00101
00102
00103
00104 int GraspItSimpleDBManager::loadObjectToDatabase(const std::string& filename,
00105 const std::string& objectName, const bool asGraspable)
00106 {
00107 if (objectName.empty())
00108 {
00109 PRINTERROR("You have to specify an object name");
00110 return -5;
00111 }
00112
00113 UNIQUE_RECURSIVE_LOCK(dbMtx);
00114
00115 std::map<std::string, Body*>::iterator it = objects.find(objectName);
00116 if (it != objects.end())
00117 {
00118 PRINTERROR("Object with name " << objectName << " already exists in the database.");
00119 return -4;
00120 }
00121
00122
00123 Body * loadedObject = NULL;
00124 {
00125
00126
00127
00128
00129 UNIQUE_RECURSIVE_LOCK lock = getUniqueWorldLock();
00130
00131 int ret = getGraspItSceneManager()->loadObject(filename, objectName, asGraspable);
00132 if (ret != 0)
00133 {
00134 PRINTERROR("Could not load object " << objectName);
00135 return ret;
00136 }
00137
00138
00139
00140 loadedObject = getBody(objectName);
00141
00142
00143
00144
00145 if (!removeElement(loadedObject, false))
00146 {
00147 PRINTERROR("FATAL: should have been able to remove the object. System could now be insconsistent.");
00148 return -6;
00149 }
00150 }
00151
00152
00153 if (!objects.insert(std::make_pair(objectName, loadedObject)).second)
00154 {
00155 PRINTERROR("Failed to insert object into the map");
00156 return -6;
00157 }
00158
00159 ++modelIdCounter;
00160 modelIDs.insert(std::make_pair(modelIdCounter, std::make_pair(objectName, false)));
00161
00162
00163 return modelIdCounter;
00164 }
00165
00166
00167
00168 bool GraspItSimpleDBManager::getRobotJointNames(const std::string& robotName,
00169 std::vector<std::string>& jointNames) const
00170 {
00171 jointNames.clear();
00172
00173 UNIQUE_RECURSIVE_LOCK(dbMtx);
00174 RobotJointNamesMap::const_iterator it = robotJointNames.find(robotName);
00175 if (it == robotJointNames.end())
00176 {
00177 PRINTERROR("Joints for robot '" << robotName << "' not found in database.");
00178 return false;
00179 }
00180 jointNames.insert(jointNames.begin(), it->second.begin(), it->second.end());
00181 return true;
00182 }
00183
00184
00185
00186 Body * GraspItSimpleDBManager::getObjectFromDatabase(const std::string& objectName)
00187 {
00188 UNIQUE_RECURSIVE_LOCK(dbMtx);
00189 std::map<std::string, Body*>::iterator it = objects.find(objectName);
00190 if (it == objects.end())
00191 {
00192 PRINTERROR("Object with name " << objectName << " does not exists in the database.");
00193 return NULL;
00194 }
00195 return it->second;
00196 }
00197
00198 Robot * GraspItSimpleDBManager::getRobotFromDatabase(const std::string& robotName)
00199 {
00200 UNIQUE_RECURSIVE_LOCK(dbMtx);
00201 std::map<std::string, Robot*>::iterator it = robots.find(robotName);
00202 if (it == robots.end())
00203 {
00204 PRINTERROR("Robot with name " << robotName << " does not exists in the database.");
00205 return NULL;
00206 }
00207 return it->second;
00208 }
00209
00210
00211 Body * GraspItSimpleDBManager::getObjectFromDatabase(const int modelID)
00212 {
00213 UNIQUE_RECURSIVE_LOCK(dbMtx);
00214 std::string name;
00215 bool isRobot;
00216 if (!getModelNameAndType(modelID, name, isRobot))
00217 {
00218 PRINTERROR("Robot/Object with model ID " << modelID << " not in database.");
00219 return NULL;
00220 }
00221 if (isRobot)
00222 {
00223 PRINTERROR("Model id " << modelID << " is a robot, not an object.");
00224 return NULL;
00225 }
00226 return getObjectFromDatabase(name);
00227 }
00228
00229 Robot * GraspItSimpleDBManager::getRobotFromDatabase(const int modelID)
00230 {
00231 UNIQUE_RECURSIVE_LOCK(dbMtx);
00232 std::string name;
00233 bool isRobot;
00234 if (!getModelNameAndType(modelID, name, isRobot))
00235 {
00236 PRINTERROR("Robot/Object with model ID " << modelID << " not in database.");
00237 return NULL;
00238 }
00239 if (!isRobot)
00240 {
00241 PRINTERROR("Model id " << modelID << " is an object, not a robot.");
00242 return NULL;
00243 }
00244 return getRobotFromDatabase(name);
00245 }
00246
00247
00248
00249
00250 int GraspItSimpleDBManager::getModelType(const int modelID) const
00251 {
00252 std::string name;
00253 bool isRobot;
00254 if (!getModelNameAndType(modelID, name, isRobot))
00255 {
00256 return -1;
00257 }
00258
00259
00260 return isRobot ? 1 : 2;
00261 }
00262
00263
00264 bool GraspItSimpleDBManager::getModelNameAndType(const int modelID, std::string& name, bool& isRobot) const
00265 {
00266 UNIQUE_RECURSIVE_LOCK(dbMtx);
00267 ModelIdMap::const_iterator it = modelIDs.find(modelID);
00268 if (it == modelIDs.end())
00269 {
00270 return false;
00271 }
00272 name = it->second.first;
00273 isRobot = it->second.second;
00274 return true;
00275 }
00276
00277
00278
00279 void GraspItSimpleDBManager::getAllLoadedRobotNames(std::vector<std::string>& names) const
00280 {
00281 UNIQUE_RECURSIVE_LOCK(dbMtx);
00282 std::map<std::string, Robot*>::const_iterator it;
00283 for (it = robots.begin(); it != robots.end(); ++it)
00284 {
00285 if (readGraspItSceneManager()->isRobotLoaded(it->first))
00286 {
00287 names.push_back(it->first);
00288 }
00289 }
00290 }
00291
00292 void GraspItSimpleDBManager::getAllLoadedRobotIDs(std::vector<int>& ids) const
00293 {
00294 UNIQUE_RECURSIVE_LOCK(dbMtx);
00295 ModelIdMap::const_iterator it;
00296 for (it = modelIDs.begin(); it != modelIDs.end(); ++it)
00297 {
00298 bool isRobot = it->second.second;
00299 if (isRobot && readGraspItSceneManager()->isRobotLoaded(it->second.first))
00300 {
00301 ids.push_back(it->first);
00302 }
00303 }
00304 }
00305
00306 void GraspItSimpleDBManager::getAllLoadedObjectNames(std::vector<std::string>& names) const
00307 {
00308 UNIQUE_RECURSIVE_LOCK(dbMtx);
00309 std::map<std::string, Body*>::const_iterator it;
00310 for (it = objects.begin(); it != objects.end(); ++it)
00311 {
00312 if (readGraspItSceneManager()->isObjectLoaded(it->first))
00313 {
00314 names.push_back(it->first);
00315 }
00316 }
00317 }
00318
00319 void GraspItSimpleDBManager::getAllLoadedObjectIDs(std::vector<int>& ids) const
00320 {
00321 UNIQUE_RECURSIVE_LOCK(dbMtx);
00322 ModelIdMap::const_iterator it;
00323 for (it = modelIDs.begin(); it != modelIDs.end(); ++it)
00324 {
00325 bool isRobot = it->second.second;
00326 if (!isRobot && readGraspItSceneManager()->isRobotLoaded(it->second.first))
00327 {
00328 ids.push_back(it->first);
00329 }
00330 }
00331 }
00332
00333
00334
00335 bool GraspItSimpleDBManager::getRobotModelID(const std::string& robotName, int& id) const
00336 {
00337 UNIQUE_RECURSIVE_LOCK(dbMtx);
00338 ModelIdMap::const_iterator it;
00339 for (it = modelIDs.begin(); it != modelIDs.end(); ++it)
00340 {
00341 bool isRobot = it->second.second;
00342 if (isRobot && (it->second.first == robotName))
00343 {
00344 id = it->first;
00345 return true;
00346 }
00347 }
00348 return false;
00349 }
00350
00351 bool GraspItSimpleDBManager::getObjectModelID(const std::string& objectName, int& id) const
00352 {
00353 UNIQUE_RECURSIVE_LOCK(dbMtx);
00354 ModelIdMap::const_iterator it;
00355 for (it = modelIDs.begin(); it != modelIDs.end(); ++it)
00356 {
00357 bool isRobot = it->second.second;
00358 if (!isRobot && (it->second.first == objectName))
00359 {
00360 id = it->first;
00361 return true;
00362 }
00363 }
00364 return false;
00365 }
00366