GraspItDatabaseManager.cpp
Go to the documentation of this file.
00001 
00022 #include <grasp_planning_graspit/GraspItDatabaseManager.h>
00023 #include <grasp_planning_graspit/LogBinding.h>
00024 
00025 #include <graspit/robot.h>
00026 
00027 #include <string>
00028 
00029 using GraspIt::GraspItDatabaseManager;
00030 
00031 int GraspItDatabaseManager::loadRobotToWorld(const std::string& robotName, const EigenTransform& transform)
00032 {
00033     Robot * robot = getRobotFromDatabase(robotName);
00034     if (!robot)
00035     {
00036         PRINTERROR("Robot " << robotName << " does not exist in database");
00037         return -1;
00038     }
00039 
00040     PRINTMSG("Adding robot...");
00041     int ret = addRobot(robot, transform);
00042     if (ret != 0)
00043     {
00044         PRINTERROR("Could not add robot to GraspIt world. Error code " << ret);
00045         return -2;
00046     }
00047     return 0;
00048 }
00049 
00050 int GraspItDatabaseManager::loadObjectToWorld(const std::string& objectName, const EigenTransform& transform)
00051 {
00052     Body * object = getObjectFromDatabase(objectName);
00053     if (!object)
00054     {
00055         PRINTERROR("Object " << objectName << " does not exist in database");
00056         return -1;
00057     }
00058 
00059     PRINTMSG("Adding object...");
00060     int ret = addBody(object, transform);
00061     if (ret != 0)
00062     {
00063         PRINTERROR("Could not add object to GraspIt world. Error code " << ret);
00064         return -2;
00065     }
00066     return 0;
00067 }
00068 
00069 int GraspItDatabaseManager::loadToWorld(const int modelID, const EigenTransform& transform)
00070 {
00071     int mType = getModelType(modelID);
00072     if (mType < 0)
00073     {
00074         PRINTERROR("Model " << modelID << " does not exist in database.");
00075         return -1;
00076     }
00077     if (mType == 1)  // is a robot
00078     {
00079         Robot * robot = getRobotFromDatabase(modelID);
00080         if (!robot)
00081         {
00082             PRINTERROR("Robot ID=" << modelID << " could not be retrieved.");
00083             return -1;
00084         }
00085 
00086         // PRINTMSG("Adding robot...");
00087         int ret = addRobot(robot, transform);
00088         if (ret != 0)
00089         {
00090             PRINTERROR("Could not add robot to GraspIt world. Error code " << ret);
00091             return -2;
00092         }
00093     }
00094     else     // is an object
00095     {
00096         Body * object = getObjectFromDatabase(modelID);
00097         if (!object)
00098         {
00099             PRINTERROR("Object ID=" << modelID << " could not be retrieved.");
00100             return -1;
00101         }
00102 
00103         // PRINTMSG("Adding object...");
00104         int ret = addBody(object, transform);
00105         if (ret != 0)
00106         {
00107             PRINTERROR("Could not add object to GraspIt world. Error code " << ret);
00108             return -2;
00109         }
00110     }
00111     return 0;
00112 }
00113 
00114 
00115 int GraspItDatabaseManager::unloadRobotFromWorld(const std::string& robotName)
00116 {
00117     int id = -1;
00118     if (!getRobotModelID(robotName, id))
00119     {
00120         PRINTERROR("Robot " << robotName << " does not exist in database.");
00121         return -2;
00122     }
00123     return unloadFromWorld(id);
00124 }
00125 
00126 int GraspItDatabaseManager::unloadObjectFromWorld(const std::string& objectName)
00127 {
00128     int id = -1;
00129     if (!getObjectModelID(objectName, id))
00130     {
00131         PRINTERROR("Object " << objectName << " does not exist in database.");
00132         return -2;
00133     }
00134     return unloadFromWorld(id);
00135 }
00136 
00137 int GraspItDatabaseManager::unloadFromWorld(const int modelID)
00138 {
00139     int mType = getModelType(modelID);
00140     if (mType < 0)
00141     {
00142         PRINTERROR("Model " << modelID << " does not exist in database.");
00143         return -2;
00144     }
00145     WorldElement * elem = NULL;
00146     if (mType == 1)  // is a robot
00147     {
00148         Robot * robot = getRobotFromDatabase(modelID);
00149         if (!robot)
00150         {
00151             PRINTERROR("Robot ID=" << modelID << " could not be retrieved from the database.");
00152             return -2;
00153         }
00154         if (!isRobotLoaded(robot))
00155         {
00156             PRINTMSG("Robot " << modelID << " is not loaded GraspIt world.");
00157             return -1;
00158         }
00159         elem = robot;
00160         PRINTMSG("Removing robot " << modelID);
00161     }
00162     else   // is an object
00163     {
00164         Body * object = getObjectFromDatabase(modelID);
00165         if (!object)
00166         {
00167             PRINTERROR("Object ID=" << modelID << " could not be retrieved from the database.");
00168             return -2;
00169         }
00170         if (!isObjectLoaded(object))
00171         {
00172             PRINTMSG("Object " << modelID << " is not loaded GraspIt world.");
00173             return -1;
00174         }
00175         elem = object;
00176         PRINTMSG("Removing object " << modelID);
00177     }
00178 
00179     if (!removeElement(elem, false))
00180     {
00181         PRINTERROR("Could not remove model " << modelID << " from GraspIt world.");
00182         return -3;
00183     }
00184     return 0;
00185 }
00186 
00187 
00188 
00189 
00190 
00191 bool GraspItDatabaseManager::saveLoadedWorld(const std::string& filename, const bool asInventor, const bool createDir)
00192 {
00193     if (asInventor)
00194     {
00195         if (!getGraspItSceneManager()->saveInventorWorld(filename, createDir))
00196         {
00197             PRINTERROR("Could not save inventor world in " << filename);
00198             if (!createDir) PRINTERROR("Does the directory exist?");
00199             return false;
00200         }
00201         // PRINTMSG("Saved inventor world in "<<filename);
00202     }
00203     else
00204     {
00205         if (!getGraspItSceneManager()->saveGraspItWorld(filename, createDir))
00206         {
00207             PRINTERROR("Could not save graspit world in " << filename);
00208             if (!createDir) PRINTERROR("Does the directory exist?");
00209             return false;
00210         }
00211         // PRINTMSG("Saved graspit world in "<<filename);
00212     }
00213     return true;
00214 }
00215 
00216 
00223 int GraspItDatabaseManager::isModelLoaded(const int modelID) const
00224 {
00225     std::string name;
00226     bool isRobot;
00227     if (!getModelNameAndType(modelID, name, isRobot))
00228     {
00229         PRINTERROR("Could not find model " << modelID << " in database");
00230     }
00231     if (isRobot)
00232     {
00233         return readGraspItSceneManager()->isRobotLoaded(name) ? 1 : 0;
00234     }
00235     return readGraspItSceneManager()->isObjectLoaded(name) ? 1 : 0;
00236 }
00237 


grasp_planning_graspit
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:36