00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00029 #include "dbaseDlg.h"
00030
00031 #include <algorithm>
00032
00033 #include <utility>
00034 #include <QFileDialog>
00035 #include <QDir>
00036 #include <QComboBox>
00037
00038 #include "graspitGUI.h"
00039 #include "ivmgr.h"
00040 #include "robot.h"
00041 #include "world.h"
00042 #include "searchState.h"
00043 #include "grasp.h"
00044 #include "graspitGUI.h"
00045 #include "mainWindow.h"
00046 #include "matvec3D.h"
00047
00048
00049 #include "DBPlanner/sql_database_manager.h"
00050 #ifdef ROS_DATABASE_MANAGER
00051 #include "DBPlanner/ros_database_manager.h"
00052 #endif
00053
00054 #include "graspit_db_model.h"
00055 #include "graspit_db_grasp.h"
00056 #include "dbasePlannerDlg.h"
00057 #include "graspCaptureDlg.h"
00058
00059
00060 #include "debug.h"
00061
00062
00063 #include "profiling.h"
00064
00069 void DBaseDlg::init()
00070 {
00071 mModelList.clear();
00072 mGraspList.clear();
00073 mGraspList.clear();
00074 browserGroup->setEnabled(FALSE);
00075 graspsGroup->setEnabled(FALSE);
00076 mDBMgr = graspItGUI->getIVmgr()->getDBMgr();
00077 if (mDBMgr) {
00078 getModelList();
00079 }
00080 sortBox->insertItem("Epsilon");
00081 sortBox->insertItem("Volume");
00082 sortBox->insertItem("Energy");
00083 }
00084
00085 void DBaseDlg::destroy()
00086 {
00087
00088
00089 delete mModelScene;
00090 }
00091
00092 void DBaseDlg::exitButton_clicked(){
00093 if (mCurrentLoadedModel) {
00094
00095 graspItGUI->getIVmgr()->getWorld()->destroyElement(mCurrentLoadedModel->getGraspableBody(), false);
00096 }
00097
00098 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mGraspList);
00099 mGraspList.clear();
00100
00101 deleteVectorElements<db_planner::Model*, GraspitDBModel*>(mModelList);
00102 QDialog::accept();
00103 }
00104
00105 bool compareNames(db_planner::Model *m1, db_planner::Model *m2)
00106 {
00107 return m1->ModelName() < m2->ModelName();
00108 }
00109
00110 void DBaseDlg::getModelList()
00111 {
00112
00113 deleteVectorElements<db_planner::Model*, GraspitDBModel*>(mModelList);
00114 mModelList.clear();
00115
00116 if(!mDBMgr->ModelList(&mModelList,db_planner::FilterList::NONE)){
00117 DBGA("Model list retrieval failed");
00118 return;
00119 }
00120
00121 std::sort(mModelList.begin(), mModelList.end(), compareNames);
00122
00123 displayModelList();
00124
00125 if(!mModelList.empty()){
00126 browserGroup->setEnabled(TRUE);
00127 connectButton->setEnabled(FALSE);
00128 }
00129 std::vector<std::string>graspTypes;
00130 if(!mDBMgr->GraspTypeList(&graspTypes)){
00131 DBGA("Grasp Types not loaded");
00132 return;
00133 }
00134
00135 displayGraspTypeList(graspTypes);
00136 }
00137
00146 void DBaseDlg::connectButton_clicked()
00147 {
00148 delete mDBMgr;
00149 Hand *h = graspItGUI->getIVmgr()->getWorld()->getCurrentHand();
00150
00151 #ifndef ROS_DATABASE_MANAGER
00152 mDBMgr = new db_planner::SqlDatabaseManager(hostLineEdit->text().toStdString(),
00153 atoi(portLineEdit->text().latin1()),
00154 usernameLineEdit->text().toStdString(),
00155 passwordLineEdit->text().toStdString(),
00156 databaseLineEdit->text().toStdString(),
00157 new GraspitDBModelAllocator(),
00158 new GraspitDBGraspAllocator(h));
00159 #else
00160 mDBMgr = new db_planner::RosDatabaseManager(hostLineEdit->text().toStdString(),
00161 portLineEdit->text().toStdString(),
00162 usernameLineEdit->text().toStdString(),
00163 passwordLineEdit->text().toStdString(),
00164 databaseLineEdit->text().toStdString(),
00165 new GraspitDBModelAllocator(),
00166 new GraspitDBGraspAllocator(h));
00167
00168 GeomGraspitDBModelAllocator* allocator = new GeomGraspitDBModelAllocator(mDBMgr);
00169 mDBMgr->SetModelAllocator(allocator);
00170 #endif
00171
00172 if (mDBMgr->isConnected()) {
00173 getModelList();
00174 } else {
00175 DBGA("DBase Browser: Connection failed");
00176 delete mDBMgr;
00177 mDBMgr = NULL;
00178 }
00179 graspItGUI->getIVmgr()->setDBMgr(mDBMgr);
00180 }
00181
00182 PROF_DECLARE(GET_GRASPS);
00183 PROF_DECLARE(GET_GRASPS_CALL);
00184
00185 void DBaseDlg::loadGraspButton_clicked(){
00186 PROF_RESET_ALL;
00187 PROF_START_TIMER(GET_GRASPS);
00188
00189 Hand *hand = graspItGUI->getIVmgr()->getWorld()->getCurrentHand();
00190 if (!hand) {
00191 DBGA("Load and select a hand before viewing grasps!");
00192 return;
00193 }
00194
00195 if(!mCurrentLoadedModel){
00196 DBGA("Load model first!");
00197 return;
00198 }
00199
00200 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mGraspList);
00201 mGraspList.clear();
00202 mCurrentFrame = 0;
00203
00204 PROF_START_TIMER(GET_GRASPS_CALL);
00205 if(!mDBMgr->GetGrasps(*mCurrentLoadedModel,GraspitDBGrasp::getHandDBName(hand).toStdString(), &mGraspList)){
00206 DBGA("Load grasps failed");
00207 mGraspList.clear();
00208 return;
00209 }
00210 PROF_STOP_TIMER(GET_GRASPS_CALL);
00211 for(std::vector<db_planner::Grasp*>::iterator it = mGraspList.begin(); it != mGraspList.end(); ){
00212 if( QString((*it)->GetSource().c_str()) == typesComboBox->currentText() ||
00213 typesComboBox->currentText() == "ALL") ++it;
00214 else{
00215 delete (*it);
00216 mGraspList.erase(it);
00217 }
00218 }
00219
00220 QString numTotal, numCurrent;
00221 numTotal.setNum(mGraspList.size());
00222 if(!mGraspList.empty()){
00223 numCurrent.setNum(mCurrentFrame + 1);
00224 graspsGroup->setEnabled(TRUE);
00225 showGrasp(0);
00226 showMarkers();
00227 } else{
00228 numCurrent.setNum(0);
00229 graspsGroup->setEnabled(FALSE);
00230 }
00231 graspIndexLabel->setText(numCurrent + "/" + numTotal);
00232 PROF_STOP_TIMER(GET_GRASPS);
00233 PROF_PRINT_ALL;
00234 }
00235
00236 void DBaseDlg::loadModelButton_clicked(){
00237 if (mCurrentLoadedModel) {
00238
00239 graspItGUI->getIVmgr()->getWorld()->destroyElement(mCurrentLoadedModel->getGraspableBody(), false);
00240 mCurrentLoadedModel = NULL;
00241 }
00242 if(mModelList.empty()){
00243 DBGA("No model loaded...");
00244 return;
00245 }
00246
00247
00248 GraspitDBModel* model = dynamic_cast<GraspitDBModel*>(mModelList[mModelMap[modelsComboBox->currentText().toStdString()]]);
00249 if(!model){
00250 DBGA("Cannot recognize the model type");
00251 return;
00252 }
00253
00254 if (!model->geometryLoaded()) {
00255
00256 if ( model->load(graspItGUI->getIVmgr()->getWorld()) != SUCCESS) {
00257 DBGA("Model load failed");
00258 return;
00259 }
00260 }
00261
00262 model->getGraspableBody()->addToIvc();
00263
00264
00265
00266 graspItGUI->getIVmgr()->getWorld()->addBody(model->getGraspableBody());
00267
00268 mCurrentLoadedModel = model;
00269
00270 model->getGraspableBody()->setTransparency(0.0);
00271 model->getGraspableBody()->setTran(transf::IDENTITY);
00272 graspsGroup->setEnabled(FALSE);
00273
00274
00275 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mGraspList);
00276 mGraspList.clear();
00277
00278 initializeGraspInfo();
00279 }
00280
00281
00282 void DBaseDlg::nextGraspButton_clicked(){
00283 nextGrasp();
00284 }
00285
00286
00287 void DBaseDlg::previousGraspButton_clicked(){
00288 previousGrasp();
00289 }
00290
00291
00292 void DBaseDlg::plannerButton_clicked(){
00293
00294 if(!mDBMgr){
00295 DBGA("No dbase manager.");
00296 return;
00297 }
00298
00299 Hand *h = graspItGUI->getIVmgr()->getWorld()->getCurrentHand();
00300 if(!h){
00301 DBGA("No hand found currently");
00302 return;
00303 }
00304
00305 if(!mCurrentLoadedModel){
00306 DBGA("No object loaded");
00307 return;
00308 }
00309
00310 DBasePlannerDlg *dlg = new DBasePlannerDlg(this, mDBMgr, mCurrentLoadedModel, h);
00311 dlg->setAttribute(Qt::WA_ShowModal, false);
00312 dlg->setAttribute(Qt::WA_DeleteOnClose, true);
00313 dlg->show();
00314
00315
00316 graspsGroup->setEnabled(FALSE);
00317 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mGraspList);
00318 initializeGraspInfo();
00319 }
00320
00321 class VisualQualityFunctor {
00322 public:
00323 enum Type{ENERGY, EPSILON, VOLUME};
00324 Type mType;
00325 double operator()(const db_planner::Grasp *grasp) {
00326 switch(mType) {
00327 case ENERGY:
00328 return grasp->Energy();
00329 case EPSILON:
00330 return grasp->EpsilonQuality();
00331 case VOLUME:
00332 return grasp->VolumeQuality();
00333 default:
00334 return 0.0;
00335 }
00336 }
00337 };
00338
00339 void DBaseDlg::sortButton_clicked()
00340 {
00341 if (mGraspList.empty()) return;
00342 VisualQualityFunctor func;
00343 if (sortBox->currentText()== "Energy") {
00344 std::sort(mGraspList.begin(), mGraspList.end(), db_planner::Grasp::CompareEnergy);
00345 func.mType = VisualQualityFunctor::ENERGY;
00346 } else if (sortBox->currentText()== "Epsilon") {
00347 std::sort(mGraspList.begin(), mGraspList.end(), db_planner::Grasp::CompareEpsilon);
00348 func.mType = VisualQualityFunctor::EPSILON;
00349 } else if (sortBox->currentText()== "Volume") {
00350 std::sort(mGraspList.begin(), mGraspList.end(), db_planner::Grasp::CompareVolume);
00351 func.mType = VisualQualityFunctor::VOLUME;
00352 } else {
00353 assert(0);
00354 }
00355 mCurrentFrame = 0;
00356 showGrasp(mCurrentFrame);
00357 if (showMarkersBox->isChecked()) {
00358 double min = func(mGraspList.front());
00359 double max = func(mGraspList.back());
00360 if (max==min) max=min+1.0;
00361 std::vector<db_planner::Grasp*>::iterator it;
00362 for (it=mGraspList.begin(); it!=mGraspList.end(); it++) {
00363 double r = (func(*it) - min) / (max - min);
00364 double g = 1-r;
00365 double b = 0.0;
00366 GraspPlanningState *state;
00367 if(showPreGraspRadioButton->isChecked()) {
00368 state = static_cast<GraspitDBGrasp*>(*it)->getPreGraspPlanningState();
00369 } else {
00370 state = static_cast<GraspitDBGrasp*>(*it)->getFinalGraspPlanningState();
00371 }
00372 state->setIVMarkerColor(r,g,b);
00373 }
00374 }
00375 }
00376
00377
00378 void DBaseDlg::createGWSButton_clicked(){
00379 graspItGUI->getMainWindow()->graspCreateProjection();
00380 }
00381
00382
00383 void DBaseDlg::modelChanged(){
00384 if(inModelConstruction) return;
00385 QString psbModelThumbPath = QString( mModelList[mModelMap[modelsComboBox->currentText().toStdString()]]->
00386 ThumbnailPath().c_str() );
00387 if(mModelScene) delete mModelScene;
00388 mModelScene = new QGraphicsScene;
00389 mModelScene->setBackgroundBrush(Qt::blue);
00390 QPixmap lPixmap;
00391 lPixmap.load(psbModelThumbPath);
00392
00393 if (lPixmap.width() > 160) {
00394 lPixmap = lPixmap.scaledToWidth(160);
00395 }
00396 if (lPixmap.height() > 120) {
00397 lPixmap = lPixmap.scaledToHeight(120);
00398 }
00399 mModelScene->addPixmap(lPixmap);
00400 this->objectGraph->setScene(mModelScene);
00401 this->objectGraph->show();
00402 }
00403
00404 void DBaseDlg::showMarkers()
00405 {
00406 std::vector<db_planner::Grasp*>::iterator it;
00407 for (it=mGraspList.begin(); it!=mGraspList.end(); it++) {
00408 GraspitDBGrasp *grasp = static_cast<GraspitDBGrasp*>(*it);
00409 if (showPreGraspRadioButton->isChecked()) {
00410 grasp->getFinalGraspPlanningState()->hideVisualMarker();
00411 if (showMarkersBox->isChecked()) {
00412 grasp->getPreGraspPlanningState()->showVisualMarker();
00413
00414 if (grasp->ClusterRep()) {
00415 grasp->getPreGraspPlanningState()->setIVMarkerColor(0,0,1);
00416 } else if (grasp->CompliantCopy()) {
00417 grasp->getPreGraspPlanningState()->setIVMarkerColor(1.0,1.0,0.0);
00418 } else {
00419 grasp->getPreGraspPlanningState()->setIVMarkerColor(0.5,0.5,0.5);
00420 }
00421 } else {
00422 grasp->getPreGraspPlanningState()->hideVisualMarker();
00423 }
00424 } else {
00425 grasp->getPreGraspPlanningState()->hideVisualMarker();
00426 if (showMarkersBox->isChecked()) {
00427 grasp->getFinalGraspPlanningState()->showVisualMarker();
00428
00429 if (grasp->ClusterRep()) {
00430 grasp->getFinalGraspPlanningState()->setIVMarkerColor(0,0,1);
00431 } else if (grasp->CompliantCopy()) {
00432 grasp->getFinalGraspPlanningState()->setIVMarkerColor(1.0,1.0,0.0);
00433 } else {
00434 grasp->getFinalGraspPlanningState()->setIVMarkerColor(0.5,0.5,0.5);
00435 }
00436 } else {
00437 grasp->getFinalGraspPlanningState()->hideVisualMarker();
00438 }
00439 }
00440 }
00441 }
00442
00443
00444 void DBaseDlg::graspTypeChanged(){
00445 showGrasp(mCurrentFrame);
00446 showMarkers();
00447 }
00448
00449
00450 void DBaseDlg::classChanged(){
00451 inModelConstruction = true;
00452 modelsComboBox->clear();
00453 for(size_t i = 0; i < mModelList.size(); ++i){
00454 if(mModelList[i]->Tags().find(classesComboBox->currentText().toStdString()) != mModelList[i]->Tags().end()
00455 || classesComboBox->currentText() == "ALL")
00456 modelsComboBox->addItem(mModelList[i]->ModelName().c_str());
00457 }
00458 inModelConstruction = false;
00459 modelChanged();
00460 }
00461
00462
00463 void DBaseDlg::displayModelList(){
00464 std::set<string> tags;
00465 mModelMap.clear();
00466 for(int i = 0; i < (int)mModelList.size(); ++i){
00467 modelsComboBox->insertItem(QString(mModelList[i]->ModelName().c_str()));
00468 tags.insert(mModelList[i]->Tags().begin(), mModelList[i]->Tags().end());
00469 mModelMap.insert(std::make_pair<std::string, int>(mModelList[i]->ModelName(), i));
00470 }
00471 classesComboBox->clear();
00472 classesComboBox->insertItem("ALL");
00473 for(std::set<string>::iterator i = tags.begin(); i != tags.end(); ++i){
00474 classesComboBox->insertItem(QString((*i).c_str()));
00475 }
00476 }
00477
00478 void DBaseDlg::displayGraspTypeList(std::vector<std::string> list){
00479 typesComboBox->clear();
00480 typesComboBox->insertItem("ALL");
00481 for(size_t i = 0; i < list.size(); ++i){
00482 typesComboBox->insertItem(QString(list[i].c_str()));
00483 }
00484 }
00485
00486
00487 void DBaseDlg::showGrasp(int i)
00488 {
00489 if (mGraspList.empty()) return;
00490 assert( i>=0 && i < (int)mGraspList.size() );
00491
00492 mCurrentLoadedModel->getGraspableBody()->setTran(transf::IDENTITY);
00493
00494 if(showPreGraspRadioButton->isChecked()){
00495 if(!static_cast<GraspitDBGrasp*>(mGraspList[i])->getPreGraspPlanningState())
00496 return;
00497 static_cast<GraspitDBGrasp*>(mGraspList[i])->getPreGraspPlanningState()->execute();
00498 }
00499 else{
00500 if(!static_cast<GraspitDBGrasp*>(mGraspList[i])->getFinalGraspPlanningState())
00501 return;
00502 static_cast<GraspitDBGrasp*>(mGraspList[i])->getFinalGraspPlanningState()->execute();
00503 if(graspItGUI->getIVmgr()->getWorld()->getCurrentHand()->isA("Barrett")){
00504 graspItGUI->getIVmgr()->getWorld()->getCurrentHand()->autoGrasp(true);
00505 }
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 graspItGUI->getIVmgr()->getWorld()->findAllContacts();
00519 graspItGUI->getIVmgr()->getWorld()->updateGrasps();
00520 mCurrentFrame = i;
00521 updateGraspInfo();
00522 }
00523
00524
00525 void DBaseDlg::nextGrasp() {
00526 if (mGraspList.empty()) return;
00527 mCurrentFrame ++;
00528 if (mCurrentFrame == mGraspList.size()) mCurrentFrame = 0;
00529 showGrasp(mCurrentFrame);
00530 }
00531
00532
00533 void DBaseDlg::previousGrasp() {
00534 if (mGraspList.empty()) return;
00535 mCurrentFrame --;
00536 if (mCurrentFrame < 0) mCurrentFrame = mGraspList.size() - 1;
00537 showGrasp(mCurrentFrame);
00538 }
00539
00540
00541 void DBaseDlg::updateGraspInfo(){
00542 QString numTotal, numCurrent;
00543 numTotal.setNum(mGraspList.size());
00544 if(!mGraspList.empty())
00545 numCurrent.setNum(mCurrentFrame + 1);
00546 else
00547 numCurrent.setNum(0);
00548 graspIndexLabel->setText(numCurrent + "/" + numTotal);
00549
00550 QString eq, vq, en, cl;
00551 eq.setNum(mGraspList[mCurrentFrame]->EpsilonQuality());
00552 vq.setNum(mGraspList[mCurrentFrame]->VolumeQuality());
00553 en.setNum(mGraspList[mCurrentFrame]->Energy());
00554 cl.setNum(mGraspList[mCurrentFrame]->Clearance());
00555
00556 epsilonQualityLabel->setText(QString("Epsilon Quality: " + eq));
00557 volumeQualityLabel->setText(QString("Volume Quality: " + vq));
00558 energyLabel->setText(QString("Energy: " + en));
00559 clearanceLabel->setText(QString("Pregrasp clearance: " + cl));
00560 }
00561
00562
00563 void DBaseDlg::initializeGraspInfo(){
00564 graspIndexLabel->setText("0/0");
00565 epsilonQualityLabel->setText(QString("Epsilon Quality: 0.0"));
00566 volumeQualityLabel->setText(QString("Volume Quality: 0.0"));
00567 }
00568
00569
00570 template <class vectorType, class treatAsType>
00571 inline void DBaseDlg::deleteVectorElements(std::vector<vectorType>& v){
00572 for(size_t i = 0; i < v.size(); ++i){
00573 delete (treatAsType)v[i];
00574 }
00575 v.clear();
00576 }