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
00030 #include "dbasePlannerDlg.h"
00031 #include <algorithm>
00032
00033 #include "body.h"
00034 #include "robot.h"
00035 #include "world.h"
00036 #include "graspit_db_model.h"
00037 #include "graspit_db_grasp.h"
00038 #include "searchState.h"
00039
00040 #include "graspitGUI.h"
00041 #include "mainWindow.h"
00042
00043 #include "DBPlanner/caching_neighbor_finder.h"
00044 #include "DBPlanner/caching_aligner.h"
00045 #include "graspit_db_planner.h"
00046
00047 #include "debug.h"
00048
00049 using std::string;
00050
00051
00052 bool biggerInTestScores(db_planner::Grasp* g1, db_planner::Grasp* g2){
00053 return static_cast<GraspitDBGrasp*>(g1)->getTestAverageScore() > static_cast<GraspitDBGrasp*>(g2)->getTestAverageScore();
00054 }
00055
00056
00057 bool biggerInEpsilonQuality(db_planner::Grasp* g1, db_planner::Grasp* g2){
00058 return static_cast<GraspitDBGrasp*>(g1)->getPreGraspPlanningState()->getEpsilonQuality() >
00059 static_cast<GraspitDBGrasp*>(g2)->getPreGraspPlanningState()->getEpsilonQuality();
00060 }
00061
00062
00063 void DBasePlannerDlg::init(){
00064
00065 std::vector<string> distFuncList;
00066 if(!mDBMgr->DistanceFunctionList(&distFuncList)){
00067 DBGA("Distance function names retrival failed");
00068 return;
00069 }
00070 initializeDistanceComboBox(distFuncList);
00071
00072 mGraspRanker = new db_planner::GraspRanker("Talk to Corey");
00073
00074 setGroupBoxEnabled(true, false, false, false, false);
00075 }
00076
00077 void DBasePlannerDlg::destroy(){
00078
00079 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mOriginalGrasps);
00080 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mTestedGrasps);
00081 }
00082
00083 void DBasePlannerDlg::exitButton_clicked(){
00084
00085 show3DObject();
00086 QDialog::accept();
00087 }
00088
00089 void DBasePlannerDlg::getNeighborButton_clicked(){
00090
00091 mNeighbors.clear();
00092 mNeighborFinder = new db_planner::CachingNeighborFinder(*mDBMgr, false, distanceFunctionComboBox->currentText().toStdString());
00093 mNeighborFinder->Find(*static_cast<GraspitDBModel*>(mPlanningModel), numOfNeighborsSpinBox->value(), &mNeighbors);
00094 if(mNeighbors.empty()) return;
00095
00096 updateNeighborList();
00097
00098 updateModelImage(mNeighbors[neighborComboBox->currentIndex()].first);
00099
00100 setGroupBoxEnabled(true, true, true, false, false);
00101 }
00102
00103 void DBasePlannerDlg::executeButton_clicked(){
00104 if(graspRangeComboBox->currentText() == QString("Test current")){
00105 if(executionTypeComboBox->currentText() == QString("Static"))
00106 mPlanner->testCurrentGrasp(GraspitDBPlanner::STATIC);
00107 else{
00108 GraspitDBPlanner::DynamicCode dynCode ;
00109 mPlanner->testCurrentGrasp(GraspitDBPlanner::DYNAMIC, &dynCode);
00110 }
00111 }
00112 else if(graspRangeComboBox->currentText() == QString("Test all")){
00113 if(executionTypeComboBox->currentText() == QString("Static"))
00114 mPlanner->testGrasps(GraspitDBPlanner::STATIC, mOriginalGrasps, &mTestedGrasps);
00115 else{
00116 mPlanner->testGrasps(GraspitDBPlanner::DYNAMIC, mOriginalGrasps, &mTestedGrasps);
00117 }
00118 std::sort(mTestedGrasps.begin(), mTestedGrasps.end(), biggerInEpsilonQuality);
00119 mCurrentTestedGrasp = 0;
00120 }
00121 else{
00122 DBGA("Execution failed\n");
00123 return;
00124 }
00125 float eq = 0, vq = 0;
00126 if(graspRangeComboBox->currentText() == QString("Test current"))
00127 mPlanner->computeQuality(eq, vq);
00128 testedEpsilonQualityLabel->setText(QString("Epsilon Quality: ") + QString::number(eq));
00129 testedVolumeQualityLabel->setText(QString("Volume Quality: ") + QString::number(vq));
00130 }
00131
00132
00133 void DBasePlannerDlg::retrieveGraspsButton_clicked(){
00134
00135 deleteVectorElements<db_planner::Grasp*, GraspitDBGrasp*>(mOriginalGrasps);
00136 mOriginalGrasps.clear();
00137 mCurrentOriginalGrasp = 0;
00138 mCurrentTestedGrasp = 0;
00139
00140 for(int i = 0; i < (int)mNeighbors.size(); ++i){
00141 std::vector<db_planner::Grasp*> grasps;
00142 if(!mDBMgr->GetGrasps(*(mNeighbors[i].first), GraspitDBGrasp::getHandDBName(mHand).toStdString(), &grasps)){
00143 DBGA("Cannot retrieve grasps from neighbor:" << mNeighbors[i].first->ModelName().c_str());
00144 return;
00145 }
00146 mOriginalGrasps.insert(mOriginalGrasps.end(), grasps.begin(), grasps.end());
00147 grasps.clear();
00148 }
00149 updateOriginalGraspInfo();
00150 }
00151
00152 void DBasePlannerDlg::rankGraspsButton_clicked(){
00153
00154 if(mAligner)
00155 delete mAligner;
00156 mAligner = new db_planner::CachingAligner(*mDBMgr, false, false, alignmentMethodComboBox->currentText().toStdString());
00157
00158 mPlanner = new GraspitDBPlanner(mHand, mPlanningModel, mDBMgr, mAligner);
00159 if(mOriginalGrasps.size() == 0){
00160 DBGA("No grasps to rank");
00161 return;
00162 }
00163
00164 std::vector<db_planner::Model*> neighborList;
00165 for(size_t i = 0; i < mNeighbors.size(); ++i){
00166 neighborList.push_back(mNeighbors[i].first);
00167 }
00168
00169 if(rankingMethodComboBox->currentText() == QString("Across neighbors")){
00170 mPlanner->crossCorrelate(neighborList, mOriginalGrasps);
00171 std::stable_sort(mOriginalGrasps.begin(), mOriginalGrasps.end(), biggerInTestScores);
00172 }
00173
00174 updateOriginalGraspInfo();
00175
00176 setGroupBoxEnabled(true, true, true, true, true);
00177
00178 mCurrentOriginalGrasp = 0;
00179 if(mCurrentOriginalGrasp < (int)mOriginalGrasps.size())
00180 showGrasp(mOriginalGrasps[mCurrentOriginalGrasp]);
00181 updateOriginalGraspInfo();
00182 }
00183
00184
00185 void DBasePlannerDlg::previousGraspButton_clicked(){
00186 if(originalGraspRadioButton->isChecked()){
00187 previousGrasp(mCurrentOriginalGrasp, mOriginalGrasps);
00188 updateOriginalGraspInfo();
00189 }else if(testedGraspRadioButton->isChecked()){
00190 previousGrasp(mCurrentTestedGrasp, mTestedGrasps);
00191 updateTestedGraspInfo();
00192 }
00193 testedEpsilonQualityLabel->setText("Epsilon Quality: -1.0");
00194 testedVolumeQualityLabel->setText("Volume Quality: -1.0");
00195 show3DObject(seeNeighborCheckBox->isChecked());
00196 }
00197
00198
00199 void DBasePlannerDlg::nextGraspButton_clicked(){
00200 if(originalGraspRadioButton->isChecked()){
00201 nextGrasp(mCurrentOriginalGrasp, mOriginalGrasps);
00202 updateOriginalGraspInfo();
00203 }else if(testedGraspRadioButton->isChecked()){
00204 nextGrasp(mCurrentTestedGrasp, mTestedGrasps);
00205 updateTestedGraspInfo();
00206 }
00207 testedEpsilonQualityLabel->setText("Epsilon Quality: -1.0");
00208 testedVolumeQualityLabel->setText("Volume Quality: -1.0");
00209 show3DObject(seeNeighborCheckBox->isChecked());
00210 }
00211
00212
00213 void DBasePlannerDlg::createGWSButton_clicked(){
00214 graspItGUI->getMainWindow()->graspCreateProjection();
00215 }
00216
00217
00218 void DBasePlannerDlg::initializeDistanceComboBox(std::vector<string> entries){
00219 for(int i = 0; i < (int)entries.size(); ++i){
00220 distanceFunctionComboBox->insertItem(QString(entries[i].c_str()));
00221 }
00222 }
00223
00224
00225 void DBasePlannerDlg::updateOriginalGraspInfo(){
00226 graspIndexLabel->setText(QString::number(mCurrentOriginalGrasp + 1) + QString("/") + QString::number(mOriginalGrasps.size()));
00227 if(mOriginalGrasps.empty()) return;
00228 originalEpsilonQualityLabel->setText(QString("Epsilon Quality: ") + QString::number(mOriginalGrasps[mCurrentOriginalGrasp]->EpsilonQuality()));
00229 originalVolumeQualityLabel->setText(QString("Volume Quality: ") + QString::number(mOriginalGrasps[mCurrentOriginalGrasp]->VolumeQuality()));
00230 int i;
00231 for( i = 0; i < (int)mNeighbors.size(); ++i){
00232 if(mNeighbors[i].first->ModelName() == mOriginalGrasps[mCurrentOriginalGrasp]->SourceModel().ModelName())
00233 break;
00234 }
00235 if(i < (int)mNeighbors.size())
00236 neighborComboBox->setCurrentIndex(i);
00237 }
00238
00239
00240 void DBasePlannerDlg::updateTestedGraspInfo(){
00241 graspIndexLabel->setText(QString::number(mCurrentTestedGrasp + 1) + QString("/") + QString::number(mTestedGrasps.size()));
00242 if(mTestedGrasps.empty()) return;
00243 float eq, vq;
00244 mPlanner->computeQuality(eq, vq);
00245 originalEpsilonQualityLabel->setText(QString("Epsilon Quality: ") + QString::number(eq));
00246 originalVolumeQualityLabel->setText(QString("Volume Quality: ") + QString::number(vq));
00247 int i;
00248 for(i = 0; i < (int)mNeighbors.size(); ++i){
00249 if(mNeighbors[i].first->ModelName() == mTestedGrasps[mCurrentTestedGrasp]->SourceModel().ModelName())
00250 break;
00251 }
00252 if(i < (int)mNeighbors.size())
00253 neighborComboBox->setCurrentIndex(i);
00254 }
00255
00256
00257 void DBasePlannerDlg::previousGrasp(int& i, std::vector<db_planner::Grasp*> graspList) {
00258 if (graspList.empty()) return;
00259 i --;
00260 if (i < 0) i = graspList.size() - 1;
00261 showGrasp(graspList[i]);
00262 }
00263
00264
00265 void DBasePlannerDlg::nextGrasp(int& i, std::vector<db_planner::Grasp*> graspList){
00266 if (graspList.empty()) return;
00267 i ++;
00268 if (i == graspList.size()) i = 0;
00269 showGrasp(graspList[i]);
00270 }
00271
00272
00273
00274
00275
00276 void DBasePlannerDlg::showGrasp(db_planner::Grasp* grasp){
00277 if(!mAligner){
00278 DBGA("Aligner is not available\n");
00279 return;
00280 }
00281 if (!grasp) return;
00282 GraspitDBGrasp *g = new GraspitDBGrasp(*static_cast<GraspitDBGrasp*>(grasp));
00283 if(!testedGraspRadioButton->isChecked()){
00284 float elmts[16];
00285 if(mAligner->Align(g->SourceModel(), *mPlanningModel, elmts))
00286 g->Transform(elmts);
00287 }
00288 static_cast<GraspitDBModel*>(mPlanningModel)->getGraspableBody()->setTran(transf::IDENTITY);
00289 g->getPreGraspPlanningState()->execute();
00290 if(mHand->isA("Barrett") && testedGraspRadioButton->isChecked()){
00291 graspItGUI->getIVmgr()->getWorld()->getCurrentHand()->autoGrasp(true);
00292 }
00293 mHand->getWorld()->findAllContacts();
00294 mHand->getWorld()->updateGrasps();
00295 }
00296
00297
00298 void DBasePlannerDlg::updateNeighborList(){
00299 neighborComboBoxInReconstruction = true;
00300 neighborComboBox->clear();
00301 for(int i = 0; i < (int)mNeighbors.size(); ++i){
00302 neighborComboBox->insertItem(mNeighbors[i].first->ModelName().c_str());
00303 }
00304 neighborComboBoxInReconstruction = false;
00305 }
00306
00307
00308 void DBasePlannerDlg::updateModelImage(db_planner::Model* model){
00309 QString psbModelThumbPath = QString(model->ThumbnailPath().c_str());
00310 if(mModelScene) delete mModelScene;
00311 mModelScene = new QGraphicsScene;
00312 QPixmap lPixmap;
00313 lPixmap.load(psbModelThumbPath);
00314 mModelScene->addPixmap(lPixmap);
00315 this->objectGraph->setScene(mModelScene);
00316 this->objectGraph->show();
00317 }
00318
00319
00320
00321
00322 void DBasePlannerDlg::modelChanged(){
00323 if(neighborComboBoxInReconstruction)
00324 return;
00325 updateModelImage(mNeighbors[neighborComboBox->currentIndex()].first);
00326 }
00327
00328
00329 void DBasePlannerDlg::neighborCheckBoxChanged(){
00330 show3DObject(seeNeighborCheckBox->isChecked());
00331 executeButton->setEnabled(!seeNeighborCheckBox->isChecked());
00332 }
00333
00334
00335 void DBasePlannerDlg::originalGraspRadioButton_clicked(){
00336 if(mCurrentOriginalGrasp < (int)mOriginalGrasps.size())
00337 showGrasp(mOriginalGrasps[mCurrentOriginalGrasp]);
00338 updateOriginalGraspInfo();
00339 }
00340
00341
00342 void DBasePlannerDlg::testedGraspRadioButton_clicked(){
00343 if(mCurrentTestedGrasp < (int) mTestedGrasps.size())
00344 showGrasp(mTestedGrasps[mCurrentTestedGrasp]);
00345 updateTestedGraspInfo();
00346 }
00347
00348
00349
00350
00351
00352 void DBasePlannerDlg::show3DObject(bool isNbr){
00353 db_planner::Model* m = mPlanningModel;
00354 if(isNbr){
00355 GraspitDBModel* dbm = static_cast<GraspitDBModel*>(mNeighbors[neighborComboBox->currentIndex()].first);
00356
00357 if (!dbm->geometryLoaded()) {
00358
00359 static_cast<GraspitDBModel*>(dbm)->load(mHand->getWorld());
00360 }
00361 m = dbm;
00362 }
00363 if(m == mModelShown)
00364 return;
00365 if(isNbr){
00366
00367 transf tr = transf::IDENTITY;
00368 float elmts[16];
00369 if(mAligner->Align(*m, *mPlanningModel, elmts)){
00370 mat3 mat;
00371 vec3 v(elmts[3],elmts[7],elmts[11]);
00372 mat[0] = elmts[0];
00373 mat[1] = elmts[1];
00374 mat[2] = elmts[2];
00375 mat[3] = elmts[4];
00376 mat[4] = elmts[5];
00377 mat[5] = elmts[6];
00378 mat[6] = elmts[8];
00379 mat[7] = elmts[9];
00380 mat[8] = elmts[10];
00381 tr.set(mat,v);
00382 }
00383 static_cast<GraspitDBModel*>(m)->getGraspableBody()->setTran( tr );
00384 }
00385
00386 mHand->getWorld()->destroyElement(static_cast<GraspitDBModel*>(mModelShown)->getGraspableBody(), false);
00387 static_cast<GraspitDBModel*>(m)->getGraspableBody()->addToIvc();
00388
00389
00390
00391 mHand->getWorld()->addBody(static_cast<GraspitDBModel*>(m)->getGraspableBody());
00392 mModelShown = m;
00393 }
00394
00395
00396 void DBasePlannerDlg::setGroupBoxEnabled(bool neighborGenerator, bool alignment, bool ranking, bool grasp, bool execute){
00397 neighborGeneratorGroup->setEnabled(neighborGenerator);
00398 alignmentMethodGroup->setEnabled(alignment);
00399 graspRankingGroup->setEnabled(ranking);
00400 graspsGroup->setEnabled(grasp);
00401 executionGroup->setEnabled(execute);
00402 }
00403
00404
00405 void DBasePlannerDlg::alignmentChanged(){
00406 setGroupBoxEnabled(true, true, true, false, false);
00407 }
00408
00409
00410 template <class vectorType, class treatAsType>
00411 inline void DBasePlannerDlg::deleteVectorElements(std::vector<vectorType>& v){
00412 for(size_t i = 0; i < v.size(); ++i){
00413 delete (treatAsType)v[i];
00414 }
00415 v.clear();
00416 }