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 "graspClusteringTask.h"
00027
00028 #include <algorithm>
00029
00030 #include "world.h"
00031 #include "robot.h"
00032 #include "graspitGUI.h"
00033 #include "matvec3D.h"
00034 #include "searchState.h"
00035 #include "DBPlanner/db_manager.h"
00036
00037 #include "graspit_db_grasp.h"
00038
00039 #include "debug.h"
00040
00041 GraspClusteringTask::GraspClusteringTask(TaskDispatcher *disp, db_planner::DatabaseManager *mgr,
00042 db_planner::TaskRecord rec) : Task (disp, mgr, rec)
00043 {
00044
00045 }
00046
00051 void GraspClusteringTask::start()
00052 {
00053
00054 if (!mDBMgr->GetPlanningTaskRecord(mRecord.taskId, &mPlanningTask)) {
00055 DBGA("Failed to get planning record for task");
00056 mStatus = ERROR;
00057 return;
00058 }
00059
00060 World *world = graspItGUI->getIVmgr()->getWorld();
00061 Hand *hand;
00062
00063
00064
00065 if (world->getCurrentHand() &&
00066 GraspitDBGrasp::getHandDBName(world->getCurrentHand()) == QString(mPlanningTask.handName.c_str())) {
00067 DBGA("Grasp Planning Task: using currently loaded hand");
00068 hand = world->getCurrentHand();
00069 } else {
00070 QString handPath = GraspitDBGrasp::getHandGraspitPath(QString(mPlanningTask.handName.c_str()));
00071 handPath = QString(getenv("GRASPIT")) + handPath;
00072 DBGA("Grasp Planning Task: loading hand from " << handPath.latin1());
00073 hand = static_cast<Hand*>(world->importRobot(handPath));
00074 if ( !hand ) {
00075 DBGA("Failed to load hand");
00076 mStatus = ERROR;
00077 return;
00078 }
00079 }
00080 mDBMgr->SetGraspAllocator(new GraspitDBGraspAllocator(hand));
00081
00082
00083 std::vector<db_planner::Grasp*> graspList;
00084 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList)){
00085 DBGA("Load grasps failed");
00086 mStatus = ERROR;
00087 while (!graspList.empty()) {
00088 delete graspList.back();
00089 graspList.pop_back();
00090 }
00091 return;
00092 }
00093
00094
00095 std::sort(graspList.begin(), graspList.end(), db_planner::Grasp::CompareEnergy);
00096
00097
00098 mStatus = DONE;
00099 int clusters = 0;
00100 DBGA("Clustering " << graspList.size() << " grasps");
00101
00102 while (!graspList.empty()) {
00103
00104 db_planner::Grasp* repGrasp = graspList.front();
00105 graspList.erase(graspList.begin());
00106
00107
00108 if (repGrasp->CompliantCopy()) {
00109 delete repGrasp;
00110 continue;
00111 }
00112
00113
00114 if (!mDBMgr->SetGraspClusterRep(repGrasp, true)) {
00115 DBGA("Failed to mark cluster rep in database");
00116 mStatus = ERROR;
00117 delete repGrasp;
00118 break;
00119 }
00120 clusters++;
00121
00122
00123 int cloud=0;
00124 std::vector<db_planner::Grasp*>::iterator it = graspList.begin();
00125 while(it!=graspList.end()) {
00126
00127
00128 if ( !(*it)->CompliantCopy() &&
00129 clusterGrasps(static_cast<GraspitDBGrasp*>(repGrasp), static_cast<GraspitDBGrasp*>(*it)) ) {
00130 (*it)->SetClusterRep(false);
00131
00132 if (!mDBMgr->SetGraspClusterRep(*it, false)) {
00133 DBGA("Failed to mark non-cluster rep in database");
00134 mStatus = ERROR;
00135 break;
00136 }
00137 cloud++;
00138 delete *it;
00139 it = graspList.erase(it);
00140 } else {
00141 it++;
00142 }
00143
00144 }
00145 DBGA(" Marked cluster of size " << cloud);
00146 delete repGrasp;
00147 if (mStatus == ERROR) break;
00148 }
00149 while (!graspList.empty()) {
00150 delete graspList.back();
00151 graspList.pop_back();
00152 }
00153 DBGA("Successfully marked " << clusters << " clusters");
00154 }
00155
00163 bool GraspClusteringTask::clusterGrasps(const GraspitDBGrasp *g1, const GraspitDBGrasp *g2)
00164 {
00165
00166 double DISTANCE_THRESHOLD = 20;
00167
00168 double ANGULAR_THRESHOLD = 0.52;
00169
00170 transf t1 = g1->getHand()->getApproachTran() * g1->getFinalGraspPlanningState()->getTotalTran();
00171 transf t2 = g2->getHand()->getApproachTran() * g2->getFinalGraspPlanningState()->getTotalTran();
00172
00173 vec3 dvec = t1.translation() - t2.translation();
00174 double d = dvec.len();
00175 if (d > DISTANCE_THRESHOLD) return false;
00176
00177 Quaternion qvec = t1.rotation() * t2.rotation().inverse();
00178 vec3 axis; double angle;
00179 qvec.ToAngleAxis(angle,axis);
00180 if (angle > M_PI) angle -= 2*M_PI;
00181 if (angle < -M_PI) angle += 2*M_PI;
00182 if (fabs(angle) > ANGULAR_THRESHOLD) return false;
00183
00184 return true;
00185 }
00186