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
00026 #include "eigenGraspDlg.h"
00027
00028 #include <QLayout>
00029 #include <QLabel>
00030 #include <QScrollBar>
00031 #include <QCheckBox>
00032 #include <QFileDialog>
00033
00034 #include "world.h"
00035 #include "eigenGrasp.h"
00036 #include "robot.h"
00037
00038 void EigenGraspDlg::init()
00039 {
00040 SLIDER_STEPS = 100000;
00041
00042 mSliderConversion = 0;
00043 mSlave = NULL;
00044 }
00045
00046 void EigenGraspDlg::destroy()
00047 {
00048
00049 mEigenGrasps->setRigid(false);
00050 }
00051
00052 int EigenGraspDlg::setWorld( World *w )
00053 {
00054 mWorld = w;
00055 mHand = mWorld->getCurrentHand();
00056 assert(mHand);
00057 mEigenGrasps = mHand->getEigenGrasps();
00058 if (!mEigenGrasps || mEigenGrasps->getSize() == 0) {
00059 QTWARNING("Current hand contains no eigen grasp information");
00060 return 0;
00061 }
00062 resetSlave();
00063 adjustSliders();
00064 handConfigurationChanged();
00065 QObject::connect(mHand, SIGNAL(configurationChanged()), this, SLOT(handConfigurationChanged()) );
00066 return 1;
00067 }
00068
00080 void EigenGraspDlg::eigenGraspChanged()
00081 {
00082 double* amplitudes = new double[mNumberGrasps];
00083 QString val;
00084 for (int i=0; i < mNumberGrasps; i++) {
00085 double t = mBarList[i]->value() * mSliderConversion;
00086
00087 if (t > mEigenGrasps->getGrasp(i)->mMax) {
00088 mBarList[i]->setValue( (int)(mEigenGrasps->getGrasp(i)->mMax / mSliderConversion) - 1);
00089 } else if (t < mEigenGrasps->getGrasp(i)->mMin) {
00090 mBarList[i]->setValue( (int)(mEigenGrasps->getGrasp(i)->mMin / mSliderConversion) + 1);
00091 }
00092 amplitudes[i] = mBarList[i]->value() * mSliderConversion;
00093 val.setNum(amplitudes[i],'f',2);
00094 mValueList[i]->setText(val);
00095 }
00096
00097
00098 double *dof = new double[mHand->getNumDOF()];
00099 mEigenGrasps->getDOF(amplitudes,dof);
00100
00101
00102
00103
00104
00105
00106
00107 mEigenGrasps->getAmp(amplitudes, dof);
00108
00109
00110 #ifdef EIGENGRASP_LOOSE
00111 if ( !mHand->checkSetDOFVals(dof) ) {
00112 fprintf(stderr,"All DOF values are illegal\n");
00113 }
00114 #endif
00115
00116 mHand->forceDOFVals(dof);
00117 QObject::disconnect(mHand, SIGNAL(configurationChanged()), this, SLOT(handConfigurationChanged()) );
00118 mHand->emitConfigChange();
00119 QObject::connect(mHand, SIGNAL(configurationChanged()), this, SLOT(handConfigurationChanged()) );
00120 mEigenGrasps->setMinMax();
00121
00122 delete [] amplitudes;
00123 delete [] dof;
00124 }
00125
00132 void EigenGraspDlg::fixBoxChanged()
00133 {
00134 bool fixed = false;
00135 for (int i=0; i < mNumberGrasps; i++ ) {
00136 if ( mCheckList[i]->isChecked() && mBarList[i]->isEnabled() ){
00137 double fa = mBarList[i]->value() * mSliderConversion;
00138 mEigenGrasps->fixEigenGrasp(i, fa);
00139 mBarList[i]->setEnabled(false);
00140 fixed = true;
00141 } else if ( !mCheckList[i]->isChecked() && !mBarList[i]->isEnabled() ){
00142 mEigenGrasps->unfixEigenGrasp(i);
00143 mBarList[i]->setEnabled(true);
00144 }
00145 }
00146 if (fixed) {
00147 rigidCheckBox->setChecked(true);
00148 rigidCheckBox_clicked();
00149 }
00150 }
00151
00152 void EigenGraspDlg::resetSlave()
00153 {
00154 if (mSlave) {
00155 delete mSlave;
00156 }
00157
00158 mNumberGrasps = mEigenGrasps->getSize();
00159 fileNameLabel->setText( QString("Filename: ") + mEigenGrasps->getName() );
00160
00161 mValueList.clear();
00162 mBarList.clear();
00163 mCheckList.clear();
00164
00165 mSlave = new QDialog(this);
00166 setSlaveLayout(mNumberGrasps);
00167 mSlave->show();
00168
00169 mEigenGrasps->setRigid(false);
00170 rigidCheckBox->setChecked(false);
00171
00172
00173
00174 }
00175
00176 void EigenGraspDlg::setSlaveLayout( int nGrasps )
00177 {
00178 mainLayout = new QVBoxLayout(mSlave, 5);
00179
00180 QLabel *valueLabel = new QLabel(QString("Value:"), mSlave);
00181 QLabel *amplLabel = new QLabel(QString("Amplitude:"), mSlave);
00182 QLabel *fixedLabel = new QLabel(QString("Fixed"), mSlave);
00183
00184 QHBoxLayout *fakeRow = new QHBoxLayout(mainLayout,-1);
00185 fakeRow->addSpacing(400);
00186
00187 QHBoxLayout *labelRow = new QHBoxLayout(mainLayout,-1);
00188 labelRow->addSpacing(5);
00189 labelRow->addWidget(valueLabel,0);
00190 labelRow->addWidget(amplLabel,1,Qt::AlignHCenter);
00191 labelRow->addWidget(fixedLabel,0);
00192 labelRow->addSpacing(5);
00193 mainLayout->addLayout(labelRow);
00194
00195 for (int i=0; i<nGrasps; i++) {
00196 QHBoxLayout *graspRow = new QHBoxLayout(mainLayout,10);
00197
00198 QLabel *eigenValue = new QLabel(QString("0.0"), mSlave);
00199 QScrollBar *bar = new QScrollBar(Qt::Horizontal, mSlave);
00200 bar->setRange( -SLIDER_STEPS, SLIDER_STEPS );
00201 bar->setPageStep(5000);
00202 bar->setLineStep(1000);
00203 bar->setValue(0);
00204 QCheckBox *box = new QCheckBox(mSlave);
00205
00206 graspRow->addSpacing(15);
00207 graspRow->addWidget(eigenValue,0);
00208 graspRow->addWidget(bar,1);
00209 graspRow->addWidget(box,0);
00210 graspRow->addSpacing(15);
00211
00212 mValueList.push_back(eigenValue);
00213 mBarList.push_back(bar);
00214 mCheckList.push_back(box);
00215
00216 connect(bar,SIGNAL(sliderMoved(int)), this, SLOT(eigenGraspChanged()) );
00217
00218
00219 connect(bar,SIGNAL(nextLine()), this, SLOT(eigenGraspChanged()) );
00220 connect(bar,SIGNAL(prevLine()), this, SLOT(eigenGraspChanged()) );
00221 connect(bar,SIGNAL(nextPage()), this, SLOT(eigenGraspChanged()) );
00222 connect(bar,SIGNAL(prevPage()), this, SLOT(eigenGraspChanged()) );
00223 connect(bar,SIGNAL(sliderReleased()), this, SLOT(eigenGraspChanged()) );
00224 connect(box,SIGNAL(clicked()), this, SLOT(fixBoxChanged()) );
00225 }
00226 mainLayout->addSpacing(20);
00227 }
00228
00229 void EigenGraspDlg::saveButton_clicked()
00230 {
00231 QString fn = QFileDialog::getSaveFileName(this, QString(), QString(getenv("GRASPIT"))+QString("/models/eigen"),
00232 "EigenGrasp Files (*.egr)") ;
00233 if ( !fn.isEmpty() ) {
00234 if (fn.section('.',1).isEmpty())
00235 fn.append(".egr");
00236 mEigenGrasps->writeToFile( fn.latin1() );
00237 }
00238 }
00239
00240 void EigenGraspDlg::loadButton_clicked()
00241 {
00242 QString fn = QFileDialog::getOpenFileName( this, QString(), QString(getenv("GRASPIT"))+QString("/models/eigen"),
00243 "EigenGrasp Files (*.egr)" );
00244 if (fn.isEmpty()) return;
00245 mHand->loadEigenData(fn);
00246 mEigenGrasps = mHand->getEigenGrasps();
00247 resetSlave();
00248 }
00249
00250 void EigenGraspDlg::identityButton_clicked()
00251 {
00252 mHand->useIdentityEigenData();
00253 mEigenGrasps = mHand->getEigenGrasps();
00254 resetSlave();
00255 }
00256
00257 void EigenGraspDlg::exitButton_clicked()
00258 {
00259 mEigenGrasps->setRigid(false);
00260
00261 QDialog::accept();
00262 }
00263
00269 void EigenGraspDlg::adjustSliders()
00270 {
00271 double max;
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 max = 4.0;
00291 mSliderConversion = 2 * max / SLIDER_STEPS;
00292 }
00293
00294 void EigenGraspDlg::show()
00295 {
00296 QDialog::show();
00297 }
00298
00299 void EigenGraspDlg::setAmplitudes(double *amp)
00300 {
00301 QString val;
00302 for (int i=0; i < mNumberGrasps; i++) {
00303 mBarList[i]->setValue( (int) (amp[i] / mSliderConversion) );
00304 val.setNum(amp[i],'f',2);
00305 mValueList[i]->setText(val);
00306 }
00307 }
00308
00316 void EigenGraspDlg::handConfigurationChanged()
00317 {
00318 double *amp = new double[mNumberGrasps];
00319 double *dof = new double[mHand->getNumDOF()];
00320 mHand->getDOFVals(dof);
00321 mEigenGrasps->getAmp(amp, dof);
00322 setAmplitudes(amp);
00323 mEigenGrasps->setMinMax();
00324 delete [] amp;
00325 delete [] dof;
00326 }
00327
00328
00329 void EigenGraspDlg::setOriginButton_clicked()
00330 {
00331 double *dof = new double[mHand->getNumDOF()];
00332 mHand->getDOFVals(dof);
00333 mEigenGrasps->setOrigin(dof);
00334 handConfigurationChanged();
00335 delete [] dof;
00336 }
00337
00338
00339 void EigenGraspDlg::rigidCheckBox_clicked()
00340 {
00341 if (rigidCheckBox->isChecked()) {
00342 mEigenGrasps->setRigid(true);
00343 eigenGraspChanged();
00344 } else {
00345 mEigenGrasps->setRigid(false);
00346
00347 for (int i=0; i < mNumberGrasps; i++ ) {
00348 if ( mCheckList[i]->isChecked() ) mCheckList[i]->setChecked(false);
00349 }
00350 fixBoxChanged();
00351 }
00352 }
00353
00354 void EigenGraspDlg::goToOrigin()
00355 {
00356 double* amplitudes = new double[mNumberGrasps];
00357 QString val;
00358
00359 for (int i=0; i < mNumberGrasps; i++) {
00360 mBarList[i]->setValue(0);
00361 amplitudes[i] = 0;
00362 val.setNum(0.0,'f',2);
00363 mValueList[i]->setText(val);
00364 }
00365 double *dof = new double[mHand->getNumDOF()];
00366 mEigenGrasps->getDOF(amplitudes,dof);
00367 mHand->forceDOFVals(dof);
00368 mEigenGrasps->setMinMax();
00369 adjustSliders();
00370
00371 delete [] amplitudes;
00372 delete [] dof;
00373 }
00374
00375
00376 void EigenGraspDlg::closeHandButton_clicked()
00377 {
00378
00379 }
00380
00381
00382 void EigenGraspDlg::goToOriginButton_clicked()
00383 {
00384 goToOrigin();
00385 }
00386
00387