forked from graspit-simulator/graspit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graspCaptureDlg.cpp
256 lines (237 loc) · 7.67 KB
/
graspCaptureDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//######################################################################
//
// GraspIt!
// Copyright (C) 2002-2009 Columbia University in the City of New York.
// All rights reserved.
//
// GraspIt! is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GraspIt! is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GraspIt!. If not, see <http://www.gnu.org/licenses/>.
//
// Author(s): Matei T. Ciocarlie
//
// $Id: graspCaptureDlg.cpp,v 1.15 2009/10/08 16:24:10 cmatei Exp $
//
//######################################################################
#include "graspCaptureDlg.h"
#include <QFileDialog>
#include "graspit/world.h"
#include "graspit/robot.h"
#include "graspit/grasp.h"
#include "graspit/quality/quality.h"
#include "graspit/quality/qualEpsilon.h"
#include "graspit/quality/qualVolume.h"
#include "graspit/EGPlanner/searchState.h"
#include "graspit/contact/contact.h"
#include "graspit/graspitCore.h"
#include "graspit/ivmgr.h"
#include "graspit/mytools.h"
#include "graspit/debug.h"
#ifdef CGDB_ENABLED
#include "graspit/DBase/DBPlanner/grasp.h"
#include "graspit/DBase/graspit_db_grasp.h"
#include "graspit/DBase/graspit_db_model.h"
#include "graspit/DBase/DBPlanner/sql_database_manager.h"
#endif
void
GraspCaptureDlg::init(World *w) {
mWorld = w;
QObject::connect(mWorld, SIGNAL(graspsUpdated()), this, SLOT(updateQuality()));
mIndicator = new QualityIndicator(this);
mIndicator->setAttribute(Qt::WA_ShowModal, false);
mIndicator->setAttribute(Qt::WA_DeleteOnClose, false);
mIndicator->show();
mQualEpsilon = NULL;
mQualVolume = NULL;
mCurrentHand = NULL;
saveToDBaseButton->setEnabled(FALSE);
if (graspitCore->getDBMgr()) {
saveToDBaseButton->setEnabled(TRUE);
} else {
QTWARNING("DBase connection not found; only Save to File possible.");
saveToDBaseButton->setEnabled(FALSE);
}
}
GraspCaptureDlg::~GraspCaptureDlg()
{
mIndicator->close();
delete mIndicator;
clearListButtonClicked();
delete mQualEpsilon;
delete mQualVolume;
}
bool
GraspCaptureDlg::checkHandSelection()
{
Hand *hand = mWorld->getCurrentHand();
if (!hand) { return false; }
if (mCurrentHand != hand) {
mCurrentHand = hand;
delete mQualEpsilon;
mQualEpsilon = new QualEpsilon(hand->getGrasp(), QString("Grasp_recorder_qm"), "L1 Norm");
delete mQualVolume;
mQualVolume = new QualVolume(hand->getGrasp(), QString("Grasp_recorder_qm"), "L1 Norm");
}
GraspableBody *body = hand->getGrasp()->getObject();
if (!body) { return false; }
return true;
}
void
GraspCaptureDlg::captureButtonClicked()
{
if (!checkHandSelection()) { return; }
if (!mWorld->noCollision()) {
DBGA("COLLISION");
return;
}
mWorld->findAllContacts();
mWorld->updateGrasps();
double quality = mQualEpsilon->evaluate();
if (!allowNonFCBox->isChecked() && quality < 0.0) {
DBGA("NON FORCE CLOSURE");
return;
}
GraspPlanningState *newState = new GraspPlanningState(mCurrentHand);
newState->setPostureType(POSE_DOF, false);
GraspableBody *body = mCurrentHand->getGrasp()->getObject();
newState->setRefTran(body->getTran());
newState->setObject(body);
newState->setEpsilonQuality(std::max(0.0, quality));
newState->setVolume(std::max(0.0, quality));
newState->saveCurrentHandState();
for (int i = 0; i < mCurrentHand->getGrasp()->getNumContacts(); i++) {
newState->getContacts()->push_back(mCurrentHand->getGrasp()->getContact(i)->getPosition());
}
mGrasps.push_back(newState);
updateNumGrasps();
if (graspitCore->getDBMgr()) {
saveToDBaseButton->setEnabled(TRUE);
} else {
saveToDBaseButton->setEnabled(FALSE);
}
}
void
GraspCaptureDlg::saveToFileButtonClicked()
{
DBGA("Foo");
if (mGrasps.empty()) {
DBGA("No recorded grasps to save");
return;
}
QString fn(QFileDialog::getSaveFileName(this, QString(),
QString(getenv("GRASPIT")), "Text Files (*.txt)"));
if (fn.isEmpty()) { return; }
if (fn.section('.', 1).isEmpty()) { fn.append(".txt"); }
FILE *fp = fopen(fn.ascii(), "a");
if (!fp) {
DBGA("Failed to open save file " << fn.ascii());
return;
}
std::list<GraspPlanningState *>::iterator it;
for (it = mGrasps.begin(); it != mGrasps.end(); it++) {
(*it)->writeToFile(fp);
}
fclose(fp);
DBGA("Grasps saved.");
}
/*! Saves the current list of grasps in the CGDB, if an interface exists.
However, when we save in the CGDB, we need more information than we
currently store in the HandObjectState.
*/
void
GraspCaptureDlg::saveToDBaseButtonClicked()
{
#ifndef CGDB_ENABLED
return;
#else
db_planner::DatabaseManager *dbMgr = graspitCore->getDBMgr();
if (!dbMgr) { return; }
std::list<GraspPlanningState *>::iterator it;
std::vector<db_planner::Grasp *> graspList;
for (it = mGrasps.begin(); it != mGrasps.end(); it++) {
GraspitDBModel *dbModel = (*it)->getObject()->getDBModel();
if (!dbModel) {
DBGA("Model not from database!");
continue;
}
db_planner::Grasp *grasp = new GraspitDBGrasp(mCurrentHand);
grasp->SetSourceModel(*(static_cast<db_planner::Model *>(dbModel)));
grasp->SetHandName(mCurrentHand->getDBName().toStdString());
grasp->SetEpsilonQuality((*it)->getEpsilonQuality());
grasp->SetVolumeQuality((*it)->getVolume());
grasp->SetEnergy(- 30 * (*it)->getEpsilonQuality() - 100 * (*it)->getVolume());
//Hard-coded source: human operator
//should have a ComboBox here where the operator can select a source from what
//options the database gives us
grasp->SetSource("HUMAN_REFINED");
std::vector<double> tempArray;
//the posture
for (int i = 0; i < (*it)->getPosture()->getNumVariables(); ++i) {
tempArray.push_back((*it)->getPosture()->getVariable(i)->getValue());
}
grasp->SetPregraspJoints(tempArray);
grasp->SetFinalgraspJoints(tempArray);
//the position
tempArray.clear();
for (int i = 0; i < (*it)->getPosition()->getNumVariables(); ++i) {
tempArray.push_back((*it)->getPosition()->getVariable(i)->getValue());
}
grasp->SetPregraspPosition(tempArray);
grasp->SetFinalgraspPosition(tempArray);
//the contacts
std::list<position> *contacts;
tempArray.clear();
contacts = (*it)->getContacts();
std::list<position>::iterator itContact;
for (itContact = contacts->begin(); itContact != contacts->end(); ++itContact) {
tempArray.push_back((*itContact).x());
tempArray.push_back((*itContact).y());
tempArray.push_back((*itContact).z());
}
grasp->SetContacts(tempArray);
//store this
graspList.push_back(grasp);
}
dbMgr->SaveGrasps(graspList);
DBGA(graspList.size() << " grasps successfully saved to database");
for (size_t i = 0; i < graspList.size(); ++i) {
delete graspList[i];
}
//just to make sure we can't click twice by mistake
clearListButtonClicked();
#endif
}
void
GraspCaptureDlg::updateNumGrasps()
{
QString num;
num.setNum((int)mGrasps.size());
recordedGraspsLabel->setText("Recorded grasps: " + num);
}
void
GraspCaptureDlg::clearListButtonClicked()
{
while (!mGrasps.empty()) {
delete mGrasps.back();
mGrasps.pop_back();
}
updateNumGrasps();
}
void
GraspCaptureDlg::updateQuality()
{
double quality = 0.0;
if (checkHandSelection()) {
quality = mQualEpsilon->evaluate();
}
mIndicator->setBar(std::max(0.0, quality));
}