GraspItSceneManager.cpp
Go to the documentation of this file.
00001 
00022 #include <grasp_planning_graspit/GraspItSceneManager.h>
00023 #include <grasp_planning_graspit/LogBinding.h>
00024 #include <grasp_planning_graspit/PrintHelpers.h>
00025 #include <grasp_planning_graspit/GraspItAccessor.h>
00026 #include <grasp_planning_graspit/GraspItHelpers.h>
00027 
00028 #include <string>
00029 #include <vector>
00030 #include <map>
00031 #include <exception>
00032 
00033 #include <graspit/world.h>
00034 #include <graspit/robot.h>
00035 #include <graspit/body.h>
00036 #include <graspit/grasp.h>
00037 #include <graspit/graspitCore.h>
00038 
00039 #include <Inventor/Qt/SoQt.h>
00040 #include <Inventor/actions/SoWriteAction.h>
00041 // #include <Inventor/actions/SoGetBoundingBoxAction.h>
00042 
00043 #include <boost/filesystem.hpp>
00044 
00045 using GraspIt::GraspItSceneManager;
00046 
00047 bool fileExists(const std::string& filename)
00048 {
00049     return boost::filesystem::exists(filename) && boost::filesystem::is_regular_file(filename);
00050 }
00051 
00052 bool makeDirectoryIfNeeded(const std::string& dPath)
00053 {
00054     try
00055     {
00056         boost::filesystem::path dir(dPath);
00057         boost::filesystem::path buildPath;
00058 
00059         for (boost::filesystem::path::iterator it(dir.begin()), it_end(dir.end()); it != it_end; ++it)
00060         {
00061             buildPath /= *it;
00062             // std::cout << buildPath << std::endl;
00063 
00064             if (!boost::filesystem::exists(buildPath) &&
00065                     !boost::filesystem::create_directory(buildPath))
00066             {
00067                 PRINTERROR("Could not create directory " << buildPath);
00068                 return false;
00069             }
00070         }
00071     }
00072     catch (const boost::filesystem::filesystem_error& ex)
00073     {
00074         PRINTERROR(ex.what());
00075         return false;
00076     }
00077     return true;
00078 }
00079 
00080 
00081 
00082 GraspItSceneManager::GraspItSceneManager():
00083     graspitWorld(NULL),
00084     core(NULL),
00085     initialized(false),
00086     fakeQObjectParent(NULL)
00087 {
00088 }
00089 
00090 
00091 GraspItSceneManager::~GraspItSceneManager()
00092 {
00093     PRINTMSG("GraspItSceneManager destructor");
00094 
00095     if (core)
00096     {
00097         PRINTERROR("The IVmgr should have been deleted, either by calling shutdown(), or by subclasses destructor!");
00098     }
00099 
00100     if (fakeQObjectParent)
00101     {
00102         delete fakeQObjectParent;
00103         fakeQObjectParent = NULL;
00104     }
00105 }
00106 
00107 void GraspItSceneManager::initialize()
00108 {
00109     if (initialized)
00110     {
00111         PRINTMSG("GraspItSceneManager already initialized.");
00112         return;
00113     }
00114 
00115     initializeCore();
00116     if (!core)
00117     {
00118         throw std::string("Cannot initialize world without core begin intialized");
00119     }
00120 
00121     fakeQObjectParent = new QObject();
00122 
00123     UNIQUE_RECURSIVE_LOCK lock(graspitWorldMtx);
00124     graspitWorld = createNewGraspitWorld();
00125     if (!graspitWorld)
00126     {
00127         PRINTERROR("Graspit world was initialized to NULL");
00128         throw std::string("Graspit world was initialized to NULL");
00129     }
00130 
00131     waitUntilReady();
00132 
00133     PRINTMSG("Initialized GraspItSceneManager.");
00134 
00135     initialized = true;
00136 }
00137 
00138 void GraspItSceneManager::shutdown()
00139 {
00140     if (!initialized)
00141     {
00142         PRINTMSG("GraspItSceneManager already shut down.");
00143         return;
00144     }
00145     initialized = false;
00146 
00147     // notify all registered accessors that the manager is
00148     // shutting down.
00149     registeredAccessorsMtx.lock();
00150     std::map<std::string, GraspItAccessor*>::iterator it;
00151     for (it = registeredAccessors.begin(); it != registeredAccessors.end(); ++it)
00152     {
00153         GraspItAccessor * s = it->second;
00154         s->onSceneManagerShutdown();
00155     }
00156     registeredAccessorsMtx.unlock();
00157 
00158     // destroy core
00159     destroyCore();
00160 
00161     if (core)
00162     {
00163         PRINTERROR("The IVmgr should have been deleted, either by calling shutdown(), or by subclasses destructor!");
00164         throw std::string("The IVmgr should have been deleted, either by calling shutdown(), or by subclasses destructor!");
00165     }
00166 
00167     if (fakeQObjectParent)
00168     {
00169         delete fakeQObjectParent;
00170         fakeQObjectParent = NULL;
00171     }
00172 }
00173 
00174 
00175 
00176 bool GraspItSceneManager::isInitialized() const
00177 {
00178     return initialized && isReady();
00179 }
00180 
00181 
00182 bool GraspItSceneManager::removeIdleListener(GraspItAccessor* s)
00183 {
00184     registeredAccessorsMtx.lock();
00185     PRINTMSG("Unregistering " << s->getName());
00186     std::map<std::string, GraspItAccessor*>::iterator it = registeredAccessors.find(s->getName());
00187     bool success = true;
00188     if (it == registeredAccessors.end())
00189     {
00190         PRINTMSG("INFO: Did not remove " << s->getName() << " from registered accessors because it wasn't registered");
00191         success = false;
00192     }
00193     else
00194     {
00195         registeredAccessors.erase(it);
00196     }
00197     registeredAccessorsMtx.unlock();
00198     return success;
00199 }
00200 
00201 
00202 bool GraspItSceneManager::addIdleListener(GraspItAccessor* s)
00203 {
00204     PRINTMSG("Registering " << s->getName());
00205     registeredAccessorsMtx.lock();
00206     bool success = registeredAccessors.insert(std::make_pair(s->getName(), s)).second;
00207     registeredAccessorsMtx.unlock();
00208     return success;
00209 }
00210 
00211 
00212 
00213 void GraspItSceneManager::processIdleEvent()
00214 {
00215     registeredAccessorsMtx.lock();
00216     // PRINTMSG("LOOP: Notify "<<_registeredAccessors.size());
00217     std::map<std::string, GraspItAccessor*>::iterator it;
00218     for (it = registeredAccessors.begin(); it != registeredAccessors.end(); ++it)
00219     {
00220         GraspItAccessor * s = it->second;
00221         if (s->isScheduledForIdleEvent())
00222         {
00223             s->idleEventFromSceneManager();
00224         }
00225     }
00226     registeredAccessorsMtx.unlock();
00227 }
00228 
00229 
00230 
00231 /*
00232 Old method to get camera parameters. Is now done in new GraspitCore class.
00233 
00234  **
00235  * Computes camera parameters to set for watching the current scene.
00236  * This is needed for writing a world file with meaningful camera parameters.
00237  *
00238  * Will set the camera at twice the scene's diameter (diameter of whole bounding box) distance
00239  * away from the scene center along the x axis. It look at the scene along the x axis.
00240  * The focal distance is also the distance from the camera to the scene center to keep things simple.
00241  *
00242 void GraspItSceneManager::getCameraParameters(Eigen::Vector3d & camPos, Eigen::Quaterniond& camQuat, double & fd) const
00243 {
00244     if (!isInitialized())
00245     {
00246         PRINTERROR("Not initialized");
00247         return;
00248     }
00249     // make a fake viewport since we have no GUI here
00250     SbViewportRegion vpReg;
00251     vpReg.setWindowSize(1920, 1080);
00252 
00253     // this action can be used to compute a bounding box of the scene
00254     SoGetBoundingBoxAction bboxAction(vpReg);
00255     graspitWorldMtx.lock();
00256     bboxAction.apply(graspitWorld->getIVRoot());
00257     graspitWorldMtx.unlock();
00258 
00259     // get bounding box parameters.
00260     SbBox3f bbox = bboxAction.getBoundingBox();
00261     SbVec3f min = bbox.getMin();
00262     SbVec3f max = bbox.getMax();
00263     SbVec3f center = bbox.getCenter();
00264     SbVec3f diameter = max - min;
00265     float cx, cy, cz;
00266     center.getValue(cx, cy, cz);
00267     float diamLen = diameter.length();
00268     // PRINTMSG("Bounding box: center="<<cx<<"/"<<cy<<"/"<<cz<<" diam="<<diamLen);
00269 
00270     // camera position is along the negative x axis 3 times the length of the diamenter from the bounding box center
00271     camPos = Eigen::Vector3d(cx - diamLen, cy, cz);
00272     // camera orientation is looking along positive x axis
00273     camQuat = Eigen::Quaterniond::FromTwoVectors(Eigen::Vector3d(0, 0, 1), Eigen::Vector3d(-1, 0, 0));
00274     fd = diamLen;
00275 }
00276 */
00277 
00278 bool GraspItSceneManager::saveGraspItWorld(const std::string& filename, bool createDir)
00279 {
00280     if (!isInitialized())
00281     {
00282         PRINTERROR("Not initialized");
00283         return false;
00284     }
00285 
00286     try
00287     {
00288         if (createDir && !makeDirectoryIfNeeded(getFileDirectory(filename)))
00289         {
00290             PRINTERROR("Could not create directory for file " << filename);
00291             return false;
00292         }
00293     }
00294     catch (int e)
00295     {
00296         PRINTERROR("An exception ocurred when trying to create the directory. Exception number " << e);
00297         return false;
00298     }
00299 
00300 /* Old code using obsolete getCameraParameters(). This is now done in GraspitCore.
00301     // set the camera so that the world file can be written with some correct camera parameters
00302     // if no camera parameters are set, the world file can't be opened with the original simulatur GUI.
00303     Eigen::Vector3d camPos;
00304     Eigen::Quaterniond camQuat;
00305     double fd;
00306     getCameraParameters(camPos, camQuat, fd);
00307     // core->setCamera(camPos.x(), camPos.y(), camPos.z(),
00308     //                camQuat.x(), camQuat.y(), camQuat.z(), camQuat.w(), fd);
00309 */
00310 
00311     if (graspitWorld->save(filename.c_str()) == FAILURE)
00312     {
00313         PRINTERROR("GraspIt could not save world file " << filename);
00314         return false;
00315     }
00316     return true;
00317 }
00318 
00319 
00320 bool GraspItSceneManager::saveInventorWorld(const std::string& filename, bool createDir)
00321 {
00322     if (!isInitialized())
00323     {
00324         PRINTERROR("Not initialized");
00325         return false;
00326     }
00327     try
00328     {
00329         if (createDir && !makeDirectoryIfNeeded(getFileDirectory(filename)))
00330         {
00331             PRINTERROR("Could not create directory for file " << filename);
00332             return false;
00333         }
00334     }
00335     catch (int e)
00336     {
00337         PRINTERROR("An exception ocurred when trying to create the directory. Exception number " << e);
00338         return false;
00339     }
00340 
00341     SoOutput out;
00342     if (!out.openFile(filename.c_str())) return false;
00343     out.setBinary(false);
00344     SoWriteAction write(&out);
00345     graspitWorldMtx.lock();
00346     write.apply(graspitWorld->getIVRoot());
00347     graspitWorldMtx.unlock();
00348     write.getOutput()->closeFile();
00349     return true;
00350 }
00351 
00352 
00353 
00354 bool GraspItSceneManager::saveRobotAsInventor(const std::string& filename, const std::string& robotName,
00355                                    const bool createDir, const bool forceWrite)
00356 {
00357     if (!forceWrite && fileExists(filename))
00358     {
00359         PRINTERROR("File " << filename << " already exists");
00360         return false;
00361     }
00362     if (!isInitialized())
00363     {
00364         PRINTERROR("Not initialized");
00365         return false;
00366     }
00367     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00368     if (!graspitWorld)
00369     {
00370         PRINTERROR("Cannot load " << filename << " with no initialized graspitWorld");
00371         return false;
00372     }
00373 
00374     // Check that no object with same name exists
00375     Robot * existingRobot = getRobotNoCheck(robotName);
00376     if (!existingRobot)
00377     {
00378         PRINTERROR("Robot with name " << robotName << " does not exist in world.");
00379         return false;
00380     }
00381 
00382     try
00383     {
00384         if (createDir && !makeDirectoryIfNeeded(getFileDirectory(filename)))
00385         {
00386             PRINTERROR("Could not create directory for file " << filename);
00387             return false;
00388         }
00389     }
00390     catch (int e)
00391     {
00392         PRINTERROR("An exception ocurred when trying to create the directory. Exception number " << e);
00393         return false;
00394     }
00395 
00396     SoOutput out;
00397     if (!out.openFile(filename.c_str())) return false;
00398     out.setBinary(false);
00399     SoWriteAction write(&out);
00400     write.apply(existingRobot->getIVRoot());
00401     write.getOutput()->closeFile();
00402 
00403     PRINTMSG("Saved robot IV to " << filename);
00404     return true;
00405 }
00406 
00407 
00408 bool GraspItSceneManager::saveObjectAsInventor(const std::string& filename, const std::string& name,
00409                                     const bool createDir, const bool forceWrite)
00410 {
00411     if (name.empty())
00412     {
00413         PRINTERROR("Cannot save an object without a name");
00414         return false;
00415     }
00416 
00417     if (!forceWrite && fileExists(filename))
00418     {
00419         PRINTERROR("File " << filename << " already exists");
00420         return false;
00421     }
00422     if (!isInitialized())
00423     {
00424         PRINTERROR("Not initialized");
00425         return false;
00426     }
00427 
00428     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00429     if (!graspitWorld)
00430     {
00431         PRINTERROR("Cannot load " << filename << " with no initialized graspitWorld");
00432         return false;
00433     }
00434 
00435     // Check that no object with same name exists
00436     Body * existingBody = getBodyNoCheck(name);
00437 
00438     if (!existingBody)
00439     {
00440         PRINTERROR("Body with name " << name << " is not loaded in world.");
00441         return false;
00442     }
00443 
00444     try
00445     {
00446         if (createDir && !makeDirectoryIfNeeded(getFileDirectory(filename)))
00447         {
00448             PRINTERROR("Could not create directory for file " << filename);
00449             return false;
00450         }
00451     }
00452     catch (int e)
00453     {
00454         PRINTERROR("An exception ocurred when trying to create the directory. Exception number " << e);
00455         return false;
00456     }
00457 
00458     SoOutput out;
00459     if (!out.openFile(filename.c_str())) return false;
00460     out.setBinary(false);
00461     SoWriteAction write(&out);
00462     write.apply(existingBody->getIVRoot());
00463     write.getOutput()->closeFile();
00464 
00465     PRINTMSG("Saved object IV to " << filename);
00466     return true;
00467 }
00468 
00469 
00470 std::vector<std::string> GraspItSceneManager::getRobotNames() const
00471 {
00472     std::vector<std::string> names;
00473     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00474     int numR =  graspitWorld->getNumRobots();
00475     for (int i = 0; i < numR; ++i)
00476     {
00477         const Robot * r = graspitWorld->getRobot(i);
00478         names.push_back(r->getName().toStdString());
00479     }
00480     return names; 
00481 }
00482 
00483 std::vector<std::string> GraspItSceneManager::getObjectNames(bool graspable) const
00484 {
00485     std::vector<std::string> names;
00486     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00487 
00488     int numB =  graspable ? graspitWorld->getNumGB() : graspitWorld->getNumBodies();
00489     for (int i = 0; i < numB; ++i)
00490     {
00491         if (graspable)
00492         {
00493             GraspableBody * b = graspitWorld->getGB(i);
00494             names.push_back(b->getName().toStdString());
00495         } 
00496         else
00497         {
00498             Body * b = graspitWorld->getBody(i);
00499             names.push_back(b->getName().toStdString());
00500         }
00501     }
00502     return names; 
00503 }
00504 
00505 
00506 
00507 
00508 int GraspItSceneManager::loadWorld(const std::string& filename)
00509 {
00510     if (!fileExists(filename))
00511     {
00512         PRINTERROR("File " << filename << " does not exist");
00513         return -3;
00514     }
00515     if (!isInitialized())
00516     {
00517         PRINTERROR("Not initialized");
00518         return -2;
00519     }
00520     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00521     if (!graspitWorld)
00522     {
00523         PRINTERROR("Cannot load " << filename << " with no initialized graspitWorld");
00524         return -2;
00525     }
00526 
00527     PRINTMSG("Loading graspitWorld " << filename);
00528     if (graspitWorld->load(QString(filename.c_str())) == FAILURE)
00529     {
00530         PRINTERROR("Could not load graspitWorld " << filename);
00531         return -1;
00532     }
00533     PRINTMSG("Loaded graspitWorld " << filename);
00534     return 0;
00535 }
00536 
00537 
00538 int GraspItSceneManager::loadRobot(const std::string& filename, const std::string& robotName,
00539                                    const EigenTransform& worldTransform)
00540 {
00541     if (!fileExists(filename))
00542     {
00543         PRINTERROR("File " << filename << " does not exist");
00544         return -3;
00545     }
00546     if (!isInitialized())
00547     {
00548         PRINTERROR("Not initialized");
00549         return -2;
00550     }
00551     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00552     if (!graspitWorld)
00553     {
00554         PRINTERROR("Cannot load " << filename << " with no initialized graspitWorld");
00555         return -2;
00556     }
00557 
00558     // Check that no object with same name exists
00559     Robot * existingRobot = getRobotNoCheck(robotName);
00560     if (existingRobot)
00561     {
00562         PRINTERROR("Robot with name " << robotName << " already exists in world.");
00563         return -4;
00564     }
00565 
00566     Robot * robot = graspitWorld->importRobot(QString(filename.c_str()));
00567     if (!robot)
00568     {
00569         PRINTERROR("Could not import robot from " << filename);
00570         return -1;
00571     }
00572 
00573     robot->setName(QString(robotName.c_str()));
00574 
00575     transf trans = getGraspitTransform(worldTransform);
00576     robot->setTran(trans);
00577 
00578     PRINTMSG("Loaded robot " << filename);
00579     return 0;
00580 }
00581 
00582 
00583 int GraspItSceneManager::loadObject(const std::string& filename, const std::string& name,
00584                                     const bool asGraspable, const EigenTransform& worldTransform)
00585 {
00586     if (name.empty())
00587     {
00588         PRINTERROR("Cannot load an object without a name");
00589         return -5;
00590     }
00591 
00592     if (!fileExists(filename))
00593     {
00594         PRINTERROR("File " << filename << " does not exist");
00595         return -3;
00596     }
00597     if (!isInitialized())
00598     {
00599         PRINTERROR("Not initialized");
00600         return -2;
00601     }
00602 
00603     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00604     if (!graspitWorld)
00605     {
00606         PRINTERROR("Cannot load " << filename << " with no initialized graspitWorld");
00607         return -2;
00608     }
00609 
00610     // Check that no object with same name exists
00611     Body * existingBody = getBodyNoCheck(name);
00612 
00613     if (existingBody)
00614     {
00615         PRINTERROR("Body with name " << name << " already exists in world.");
00616         return -4;
00617     }
00618 
00619     Body * object = NULL;
00620     if (asGraspable)
00621     {
00622         object = graspitWorld->importBody("GraspableBody", QString(filename.c_str()));
00623     }
00624     else
00625     {
00626         object = graspitWorld->importBody("Body", QString(filename.c_str()));
00627     }
00628     if (!object)
00629     {
00630         PRINTERROR("Could not import object from " << filename);
00631         return -1;
00632     }
00633 
00634     object->setName(QString(name.c_str()));
00635 
00636     transf trans = getGraspitTransform(worldTransform);
00637     object->setTran(trans);
00638 
00639     PRINTMSG("Loaded object from " << filename);
00640     return 0;
00641 }
00642 
00643 
00644 GraspableBody * GraspItSceneManager::getGraspableBodyNoCheck(const std::string& name)
00645 {
00646     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00647     int gb =  graspitWorld->getNumGB();
00648     for (int i = 0; i < gb; ++i)
00649     {
00650         GraspableBody * b = graspitWorld->getGB(i);
00651         if (b->getName().toStdString() == name)
00652         {
00653             return b;
00654         }
00655     }
00656     return NULL;
00657 }
00658 
00659 const GraspableBody * GraspItSceneManager::readGraspableBodyNoCheck(const std::string& name) const
00660 {
00661     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00662     int gb =  graspitWorld->getNumGB();
00663     for (int i = 0; i < gb; ++i)
00664     {
00665         GraspableBody * b = graspitWorld->getGB(i);
00666         if (b->getName().toStdString() == name)
00667         {
00668             return b;
00669         }
00670     }
00671     return NULL;
00672 }
00673 
00674 
00675 
00676 GraspableBody * GraspItSceneManager::getGraspableBody(const std::string& name)
00677 {
00678     if (!isInitialized())
00679     {
00680         PRINTERROR("Not initialized");
00681         return NULL;
00682     }
00683     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00684     if (!graspitWorld)
00685     {
00686         PRINTERROR("Graspit world is NULL");
00687         return NULL;
00688     }
00689     return getGraspableBodyNoCheck(name);
00690 }
00691 
00692 
00693 const GraspableBody * GraspItSceneManager::readGraspableBody(const std::string& name) const
00694 {
00695     if (!isInitialized())
00696     {
00697         PRINTERROR("Not initialized");
00698         return NULL;
00699     }
00700     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00701     if (!graspitWorld)
00702     {
00703         PRINTERROR("Graspit world is NULL");
00704         return NULL;
00705     }
00706     return readGraspableBodyNoCheck(name);
00707 }
00708 
00709 
00710 
00711 GraspableBody * GraspItSceneManager::getGraspableBodyNoCheck(const unsigned int i)
00712 {
00713     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00714     int gb =  graspitWorld->getNumGB();
00715     if (i > gb)
00716     {
00717         PRINTERROR("There is no " << i << "th graspable body");
00718         return NULL;
00719     }
00720     return graspitWorld->getGB(i);
00721 }
00722 
00723 
00724 GraspableBody * GraspItSceneManager::getGraspableBody(const unsigned int i)
00725 {
00726     if (!isInitialized())
00727     {
00728         PRINTERROR("Not initialized");
00729         return NULL;
00730     }
00731     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00732     if (!graspitWorld)
00733     {
00734         PRINTERROR("Graspit world is NULL");
00735         return NULL;
00736     }
00737     return getGraspableBodyNoCheck(i);
00738 }
00739 
00740 
00741 
00742 Body * GraspItSceneManager::getBodyNoCheck(const std::string& name)
00743 {
00744     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00745     int nb =  graspitWorld->getNumBodies();
00746     for (int i = 0; i < nb; ++i)
00747     {
00748         Body * b = graspitWorld->getBody(i);
00749         if (b->getName().toStdString() == name)
00750         {
00751             return b;
00752         }
00753     }
00754     return NULL;
00755 }
00756 
00757 const Body * GraspItSceneManager::readBodyNoCheck(const std::string& name) const
00758 {
00759     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00760     int nb =  graspitWorld->getNumBodies();
00761     for (int i = 0; i < nb; ++i)
00762     {
00763         const Body * b = graspitWorld->getBody(i);
00764         if (b->getName().toStdString() == name)
00765         {
00766             return b;
00767         }
00768     }
00769     return NULL;
00770 }
00771 
00772 
00773 
00774 Body * GraspItSceneManager::getBody(const std::string& name)
00775 {
00776     if (!isInitialized())
00777     {
00778         PRINTERROR("Not initialized");
00779         return NULL;
00780     }
00781     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00782     if (!graspitWorld)
00783     {
00784         PRINTERROR("Graspit world is NULL");
00785         return NULL;
00786     }
00787     return getBodyNoCheck(name);
00788 }
00789 
00790 
00791 const Body * GraspItSceneManager::readBody(const std::string& name) const
00792 {
00793     if (!isInitialized())
00794     {
00795         PRINTERROR("Not initialized");
00796         return NULL;
00797     }
00798     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00799     if (!graspitWorld)
00800     {
00801         PRINTERROR("Graspit world is NULL");
00802         return NULL;
00803     }
00804     return readBodyNoCheck(name);
00805 }
00806 
00807 
00808 
00809 Body * GraspItSceneManager::getBodyNoCheck(const unsigned int i)
00810 {
00811     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00812     int nb =  graspitWorld->getNumBodies();
00813     if (i > nb)
00814     {
00815         PRINTERROR("There is no " << i << "th body");
00816         return NULL;
00817     }
00818     return graspitWorld->getBody(i);
00819 }
00820 
00821 
00822 Body * GraspItSceneManager::getBody(const unsigned int i)
00823 {
00824     if (!isInitialized())
00825     {
00826         PRINTERROR("Not initialized");
00827         return NULL;
00828     }
00829     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00830     if (!graspitWorld)
00831     {
00832         PRINTERROR("Graspit world is NULL");
00833         return NULL;
00834     }
00835     return getBodyNoCheck(i);
00836 }
00837 
00838 
00839 
00840 
00841 const Robot * GraspItSceneManager::readRobotNoCheck(const std::string& name) const
00842 {
00843     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00844     int numR =  graspitWorld->getNumRobots();
00845     for (int i = 0; i < numR; ++i)
00846     {
00847         const Robot * b = graspitWorld->getRobot(i);
00848         if (b->getName().toStdString() == name)
00849         {
00850             return b;
00851         }
00852     }
00853     return NULL;
00854 }
00855 
00856 
00857 
00858 
00859 Robot * GraspItSceneManager::getRobotNoCheck(const std::string& name)
00860 {
00861     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00862     int numR =  graspitWorld->getNumRobots();
00863     for (int i = 0; i < numR; ++i)
00864     {
00865         Robot * b = graspitWorld->getRobot(i);
00866         if (b->getName().toStdString() == name)
00867         {
00868             return b;
00869         }
00870     }
00871     return NULL;
00872 }
00873 
00874 Robot * GraspItSceneManager::getRobot(const std::string& name)
00875 {
00876     if (!isInitialized())
00877     {
00878         PRINTERROR("Not initialized");
00879         return NULL;
00880     }
00881     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00882     if (!graspitWorld)
00883     {
00884         PRINTERROR("Graspit world is NULL");
00885         return NULL;
00886     }
00887     return getRobotNoCheck(name);
00888 }
00889 
00890 
00891 const Robot * GraspItSceneManager::readRobot(const std::string& name) const
00892 {
00893     if (!isInitialized())
00894     {
00895         PRINTERROR("Not initialized");
00896         return NULL;
00897     }
00898     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00899     if (!graspitWorld)
00900     {
00901         PRINTERROR("Graspit world is NULL");
00902         return NULL;
00903     }
00904     return readRobotNoCheck(name);
00905 }
00906 
00907 
00908 
00909 Robot * GraspItSceneManager::getRobotNoCheck(const unsigned int i)
00910 {
00911     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00912     int numR =  graspitWorld->getNumRobots();
00913     if (i > numR)
00914     {
00915         PRINTERROR("There is no " << i << "th robot");
00916         return NULL;
00917     }
00918     return graspitWorld->getRobot(i);
00919 }
00920 
00921 
00922 Robot * GraspItSceneManager::getRobot(const unsigned int i)
00923 {
00924     if (!isInitialized())
00925     {
00926         PRINTERROR("Not initialized");
00927         return NULL;
00928     }
00929     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00930     if (!graspitWorld)
00931     {
00932         PRINTERROR("Graspit world is NULL");
00933         return NULL;
00934     }
00935     return getRobotNoCheck(i);
00936 }
00937 
00938 
00939 
00940 
00941 int GraspItSceneManager::moveObject(const std::string& name, const EigenTransform& worldTransform)
00942 {
00943     if (name.empty())
00944     {
00945         PRINTERROR("Cannot move an object without a name");
00946         return -1;
00947     }
00948 
00949     if (!isInitialized())
00950     {
00951         PRINTERROR("Not initialized");
00952         return -2;
00953     }
00954     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00955     if (!graspitWorld)
00956     {
00957         PRINTERROR("Cannot move " << name << " with no initialized graspitWorld");
00958         return -2;
00959     }
00960 
00961     return moveObjectNoCheck(name, worldTransform);
00962 }
00963 
00964 int GraspItSceneManager::moveObjectNoCheck(const std::string& name, const EigenTransform& worldTransform)
00965 {
00966     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00967     // Check that no object with same name exists
00968     Body * existingBody = getBodyNoCheck(name);
00969     if (!existingBody) return -1;
00970     transf trans = getGraspitTransform(worldTransform);
00971     existingBody->setTran(trans);
00972     return 0;
00973 }
00974 
00975 
00976 bool GraspItSceneManager::removeElement(WorldElement* elem, const bool deleteInstance)
00977 {
00978     if (!isInitialized())
00979     {
00980         PRINTERROR("Not initialized");
00981         return false;
00982     }
00983     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00984     if (!graspitWorld)
00985     {
00986         PRINTERROR("Cannot remove element with no initialized graspitWorld");
00987         return false;
00988     }
00989     removeElementNoCheck(elem, deleteInstance);
00990     return true;
00991 }
00992 
00993 
00994 void GraspItSceneManager::removeElementNoCheck(WorldElement* elem, const bool deleteInstance)
00995 {
00996     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
00997 
00998     // graspitWorld->destroyElement(elem,deleteInstance);
00999     // PRINTMSG("Now the number of bodies is "<<graspitWorld->getNumBodies());
01000 
01001     // cannot use removeRobot() for the robot because this deletes the object too,
01002     // and deleteInstance may be false!
01003 
01004     Robot * robot = dynamic_cast<Robot*>(elem);
01005     if (robot)
01006     {
01007         // this is a robot so we also need to remove the link bodies which
01008         // are added in World::addRobot().
01009         // unfortunately this is not handled in World::destroyElement (or in
01010         // World::removeRobot(), so the bodies end up remaining in the scene
01011         if (robot->getBase())
01012         {
01013             graspitWorld->destroyElement(robot->getBase(), false);
01014         }
01015 
01016         for (int f = 0; f < robot->getNumChains(); f++)
01017         {
01018             for (int l = 0; l < robot->getChain(f)->getNumLinks(); l++)
01019             {
01020                 Body * link = robot->getChain(f)->getLink(l);
01021                 if (link)
01022                 {
01023                     graspitWorld->destroyElement(link, false);
01024                     // PRINTMSG("Removed link "<<link->getName().toStdString()<<" from world.");
01025                 }
01026             }
01027         }
01028     }
01029 
01030     graspitWorld->destroyElement(elem, deleteInstance);
01031 
01032     if (!deleteInstance)
01033     {
01034         // remove the World object parent and pass on the responsibility to
01035         // delete this object to fakeQObjectParent.
01036         // Explanation: The graspit World is normally QObject-parent to
01037         // all Robots and Objects. Further, all robots and objects are
01038         // managed from within in the world, so they are properly deleted in the
01039         // World destructor. However, now we have removed this registration
01040         // from the world. So instead, the Robot and Body objects will be
01041         // destroyed from within the QObject destructor, when it destroys
01042         // all children of it (this happens when World is destroyed, right
01043         // after its destructor is called).
01044         // However, the Body and Robot destructors still access to the
01045         // World and to various other objects which have been destroyed
01046         // in the World destructor already. Then there is a memory corruption.
01047         elem->setParent(fakeQObjectParent);
01048         // setting to NULL would pass reponsibility to delete to caller
01049         // instead, but only until they may re-add the element to the World.
01050         // this makes usage of the interface too complicated, so it's better
01051         // to use fakeQObjectParent and tell users of GraspItSceneManager that
01052         // they simply never have to worry about destroying objects.
01053 
01054         // elem->setParent(NULL);
01055     }
01056 }
01057 
01058 
01059 int GraspItSceneManager::removeObject(const std::string& name)
01060 {
01061     if (name.empty())
01062     {
01063         PRINTERROR("Cannot remove an object without a name");
01064         return -1;
01065     }
01066 
01067     if (!isInitialized())
01068     {
01069         PRINTERROR("Not initialized");
01070         return -2;
01071     }
01072     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01073     if (!graspitWorld)
01074     {
01075         PRINTERROR("Cannot remove " << name << " with no initialized graspitWorld");
01076         return -2;
01077     }
01078 
01079     return removeObjectNoCheck(name);
01080 }
01081 
01082 
01083 int GraspItSceneManager::removeObjectNoCheck(const std::string& name)
01084 {
01085     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01086     // Check that no object with same name exists
01087     Body * existingBody = getBodyNoCheck(name);
01088     if (!existingBody) return -1;
01089     // Options in World to delete objects:
01090     // * World::destroyElement: removes any element from the scene (robot, object, body...), and from
01091     //   robotVec/handVec/etc. Allows optionally to delete object pointed to. Also emits signals.
01092     // * World::removeElementFromSceneGraph just removes the stuff from IV tree
01093     // * World::removeRobot calls removeElementFromSceneGraph and additionally removes it
01094     //   from robotVec and handVec AND deletes the object. Does not emit signals.
01095     removeElementNoCheck(existingBody, true);
01096     return 0;
01097 }
01098 
01099 
01100 int GraspItSceneManager::removeRobot(const std::string& name)
01101 {
01102     if (name.empty())
01103     {
01104         PRINTERROR("Cannot remove an object without a name");
01105         return -1;
01106     }
01107 
01108     if (!isInitialized())
01109     {
01110         PRINTERROR("Not initialized");
01111         return -2;
01112     }
01113     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01114     if (!graspitWorld)
01115     {
01116         PRINTERROR("Cannot remove " << name << " with no initialized graspitWorld");
01117         return -2;
01118     }
01119 
01120     return removeRobotNoCheck(name);
01121 }
01122 
01123 
01124 int GraspItSceneManager::removeRobotNoCheck(const std::string& name)
01125 {
01126     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01127     // Check that no object with same name exists
01128     Robot * existingRobot = getRobotNoCheck(name);
01129     if (!existingRobot) return -1;
01130     removeElementNoCheck(existingRobot, true);
01131     return 0;
01132 }
01133 
01134 
01135 int GraspItSceneManager::moveRobot(const std::string& name, const EigenTransform& worldTransform)
01136 {
01137     if (name.empty())
01138     {
01139         PRINTERROR("Cannot move a robot without a name");
01140         return -1;
01141     }
01142 
01143     if (!isInitialized())
01144     {
01145         PRINTERROR("Not initialized");
01146         return -2;
01147     }
01148     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01149     if (!graspitWorld)
01150     {
01151         PRINTERROR("Cannot move " << name << " with no initialized graspitWorld");
01152         return -2;
01153     }
01154 
01155     return moveRobotNoCheck(name, worldTransform);
01156 }
01157 
01158 
01159 int GraspItSceneManager::moveRobotNoCheck(const std::string& name, const EigenTransform& worldTransform)
01160 {
01161     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01162     // Check that no object with same name exists
01163     Robot * existingRobot = getRobotNoCheck(name);
01164     if (!existingRobot) return -1;
01165     transf trans = getGraspitTransform(worldTransform);
01166     existingRobot->setTran(trans);
01167     return 0;
01168 }
01169 
01170 
01171 
01172 int GraspItSceneManager::setCurrentHand(const std::string& robotName)
01173 {
01174     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01175     Robot * robot = getRobot(robotName);
01176     if (!robot)
01177     {
01178         PRINTERROR("Robot " << robotName << " could not be found.");
01179         return -1;
01180     }
01181 
01182     if (!robot->inherits("Hand"))
01183     {
01184         PRINTERROR("Robot " << robotName << " is not of type Hand");
01185         return -2;
01186     }
01187 
01188     Hand * hand = dynamic_cast<Hand*>(robot);
01189     if (!hand)
01190     {
01191         PRINTERROR("Could not cast robot to Hand type");
01192         return -2;
01193     }
01194     graspitWorld->setCurrentHand(hand);
01195     return 0;
01196 }
01197 
01198 
01199 int GraspItSceneManager::setCurrentGraspableObject(const std::string& objectName)
01200 {
01201     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01202 
01203     Hand * hand = getCurrentHand();
01204     if (!hand)
01205     {
01206         PRINTERROR("No hand currently selected");
01207         return -1;
01208     }
01209 
01210     GraspableBody * gObject = getGraspableBody(objectName);
01211     if (!gObject)
01212     {
01213         PRINTERROR("No graspable object " << objectName << " found.");
01214         return -2;
01215     }
01216 
01217     hand->getGrasp()->setObjectNoUpdate(gObject);
01218     return 0;
01219 }
01220 
01221 int GraspItSceneManager::setGraspableObject(const std::string& robotName, const std::string& objectName)
01222 {
01223     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01224 
01225     Robot * robot = getRobot(robotName);
01226     if (!robot)
01227     {
01228         PRINTERROR("Robot " << robotName << " not found.");
01229         return -1;
01230     }
01231 
01232     if (!robot->inherits("Hand"))
01233     {
01234         PRINTERROR("Robot " << robotName << " is not of type Hand");
01235         return -2;
01236     }
01237 
01238     Hand * hand = dynamic_cast<Hand*>(robot);
01239     if (!hand)
01240     {
01241         PRINTERROR("Could not cast robot " << robotName << " to Hand type");
01242         return -2;
01243     }
01244 
01245     GraspableBody * gObject = getGraspableBody(objectName);
01246     if (!gObject)
01247     {
01248         PRINTERROR("No graspable object " << objectName << " found.");
01249         return -3;
01250     }
01251 
01252     hand->getGrasp()->setObjectNoUpdate(gObject);
01253     return 0;
01254 }
01255 
01256 
01257 GraspableBody * GraspItSceneManager::getCurrentGraspableBody()
01258 {
01259     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01260 
01261     Hand * hand = getCurrentHand();
01262     if (!hand)
01263     {
01264         PRINTERROR("No hand currently selected");
01265         return NULL;
01266     }
01267 
01268     return hand->getGrasp()->getObject();
01269 }
01270 
01271 
01272 const GraspableBody * GraspItSceneManager::readCurrentGraspableBody() const
01273 {
01274     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01275 
01276     const Hand * hand = readCurrentHand();
01277     if (!hand)
01278     {
01279         PRINTERROR("No hand currently selected");
01280         return NULL;
01281     }
01282 
01283     return hand->getGrasp()->getObject();
01284 }
01285 
01286 
01287 
01288 
01289 const Hand * GraspItSceneManager::readCurrentHand() const
01290 {
01291     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01292     if (graspitWorld) return graspitWorld->getCurrentHand();
01293     return NULL;
01294 }
01295 
01296 
01297 
01298 Hand * GraspItSceneManager::getCurrentHand()
01299 {
01300     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01301     if (graspitWorld) return graspitWorld->getCurrentHand();
01302     return NULL;
01303 }
01304 
01305 
01306 int GraspItSceneManager::addRobot(Robot* robot, const EigenTransform& worldTransform)
01307 {
01308     if (!robot)
01309     {
01310         PRINTERROR("Trying to add NULL robot");
01311         return -2;
01312     }
01313 
01314     std::string robotName = robot->getName().toStdString();
01315 
01316     if (robotName.empty())
01317     {
01318         PRINTERROR("Can only add robots with a name");
01319         return -3;
01320     }
01321 
01322     if (!isInitialized())
01323     {
01324         PRINTERROR("Not initialized");
01325         return -1;
01326     }
01327 
01328     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01329     if (!graspitWorld)
01330     {
01331         PRINTERROR("World not initialized");
01332         return -1;
01333     }
01334 
01335     // double-check that neither an instance with same pointer OR name is loaded
01336     if (isRobotLoaded(robot) || isRobotLoaded(robotName))
01337     {
01338         PRINTERROR("Robot " << robotName << " already exists in world (as name or same pointer).");
01339         return -4;
01340     }
01341 
01342 
01343     if (robot->getWorld() != graspitWorld)
01344     {
01345         std::stringstream s;
01346         s << "The world registered at the Robot is not the same as the current world. ";
01347         s << "Such changes to the WorldElement objects has not been considered ";
01348         s << "in the current implementation. Other things could be not initialized ";
01349         s << "properly. Cannot add this object to the world at this stage, use only ";
01350         s << "objects which have been created by the graspit world in the first place.";
01351         PRINTERROR(s.str());
01352         return -2;
01353     }
01354 
01355 
01356     // we need to add the robot links to the CollisionInterface again,
01357     // World::addRobot() does not do this.
01358     std::vector<DynamicBody *> allLinkVec;
01359     robot->getAllLinks(allLinkVec);
01360     std::vector<DynamicBody *>::iterator it;
01361     for (it = allLinkVec.begin(); it != allLinkVec.end(); ++it)
01362     {
01363         DynamicBody * link = *it;
01364         if (link)
01365         {
01366             // PRINTMSG("Adding link '"<<link->getName().toStdString()<<"' to collision environment");
01367             link->addToIvc();
01368         }
01369     }
01370     // XXX TODO we probably also have to handle Robot::getAllAttachedRobots()!!
01371 
01372     transf trans = getGraspitTransform(worldTransform);
01373     robot->setTran(trans);
01374 
01375     graspitWorld->addRobot(robot, true);
01376     // set the QObject parent to the GraspIt world so that it is handled
01377     // properly in its destructor (if it is loaded in the world, it will
01378     // automatically be destroyed in the graspit destructor anyway,
01379     // so it should also have the proper parent relationship)
01380     robot->setParent(graspitWorld);
01381 
01382     return 0;
01383 }
01384 
01385 int GraspItSceneManager::addBody(Body * body, const EigenTransform& worldTransform)
01386 {
01387     if (!body)
01388     {
01389         PRINTERROR("Trying to add NULL body");
01390         return -2;
01391     }
01392 
01393     std::string bodyName = body->getName().toStdString();
01394 
01395     if (bodyName.empty())
01396     {
01397         PRINTERROR("Can only add bodys with a name");
01398         return -3;
01399     }
01400 
01401     if (!isInitialized())
01402     {
01403         PRINTERROR("Not initialized");
01404         return -1;
01405     }
01406 
01407     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01408     if (!graspitWorld)
01409     {
01410         PRINTERROR("World not initialized");
01411         return -1;
01412     }
01413 
01414     // double-check that neither an instance with same pointer OR name is loaded
01415     if (isObjectLoaded(body) || isObjectLoaded(bodyName))
01416     {
01417         PRINTERROR("Body with name " << bodyName << " already exists in world (as name or same pointer).");
01418         return -4;
01419     }
01420 
01421     if (body->getWorld() != graspitWorld)
01422     {
01423         std::stringstream s;
01424         s << "The world registered at the Body is not the same as the current world. ";
01425         s << "Such changes to the WorldElement objects has not been considered ";
01426         s << "in the current implementation. Other things could be not initialized ";
01427         s << "properly. Cannot add this object to the world at this stage, use only ";
01428         s << "objects which have been created by the graspit world in the first place.";
01429         PRINTERROR(s.str());
01430         return -2;
01431     }
01432 
01433     transf trans = getGraspitTransform(worldTransform);
01434     body->setTran(trans);
01435 
01436     // we need to add the body to the CollisionInterface again,
01437     // World::addBody() does not do this.
01438     body->addToIvc();
01439     graspitWorld->addBody(body);
01440 
01441     // set the QObject parent to the GraspIt world so that it is handled
01442     // properly in its destructor (if it is loaded in the world, it will
01443     // automatically be destroyed in the graspit destructor anyway,
01444     // so it should also have the proper parent relationship)
01445     body->setParent(graspitWorld);
01446 
01447     return 0;
01448 }
01449 
01450 
01451 
01452 
01453 
01454 unsigned int GraspItSceneManager::getNumGraspableBodies() const
01455 {
01456     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01457     return  graspitWorld->getNumGB();
01458 }
01459 
01460 unsigned int GraspItSceneManager::getNumBodies() const
01461 {
01462     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01463     return graspitWorld->getNumBodies();
01464 }
01465 
01466 unsigned int GraspItSceneManager::getNumRobots() const
01467 {
01468     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01469     return graspitWorld->getNumRobots();
01470 }
01471 
01472 
01473 
01474 
01475 
01476 bool GraspItSceneManager::isRobotLoaded(const std::string& name) const
01477 {
01478     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01479     const Robot * r = readRobot(name);
01480     return r != NULL;
01481 }
01482 
01483 
01484 bool GraspItSceneManager::isObjectLoaded(const std::string& name) const
01485 {
01486     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01487     const Body * b = readBody(name);
01488     return b != NULL;
01489 }
01490 
01491 bool GraspItSceneManager::isRobotLoaded(const Robot * r) const
01492 {
01493     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01494     if (!r)
01495     {
01496         PRINTERROR("Cannot check for NULL robot");
01497         return false;
01498     }
01499 
01500     int numR =  graspitWorld->getNumRobots();
01501     for (int i = 0; i < numR; ++i)
01502     {
01503         const Robot * rw = graspitWorld->getRobot(i);
01504         if (rw == r)
01505         {
01506             return true;
01507         }
01508     }
01509     return false;
01510 }
01511 
01512 
01513 bool GraspItSceneManager::isObjectLoaded(const Body * b) const
01514 {
01515     UNIQUE_RECURSIVE_LOCK(graspitWorldMtx);
01516     if (!b)
01517     {
01518         PRINTERROR("Cannot check for NULL object");
01519         return false;
01520     }
01521     int nb =  graspitWorld->getNumBodies();
01522     for (int i = 0; i < nb; ++i)
01523     {
01524         const Body * rb = graspitWorld->getBody(i);
01525         if (rb == b)
01526         {
01527             return true;
01528         }
01529     }
01530     return false;
01531 }
01532 


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