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 "graspit_db_model.h"
00031
00032 #include <Inventor/nodes/SoScale.h>
00033
00034 #include <string>
00035
00036 #include "mytools.h"
00037 #include "body.h"
00038 #include "world.h"
00039
00040 #include "debug.h"
00041
00042
00043 #include "DBPlanner/db_manager.h"
00044
00045 GraspitDBModel::~GraspitDBModel()
00046 {
00047 if(mGraspableBody) delete mGraspableBody;
00048 }
00049
00050 int GraspitDBModel::loadGeometry()
00051 {
00052
00053 QString filename = QString(GeometryPath().c_str());
00054 QString extension = filename.section('.',-1,-1);
00055 if (extension == "off") {
00056 DBGA("Failed to load .off geometry from file " << GeometryPath());
00057 return mGraspableBody->loadGeometryOFF(filename);
00058 } else if (extension == "iv") {
00059 DBGA("Failed to load .iv geometry from file " << GeometryPath());
00060 return mGraspableBody->loadGeometryIV(filename);
00061 } else if (extension == "ply") {
00062 DBGA("Failed to load .ply geometry from file " << GeometryPath());
00063 return mGraspableBody->loadGeometryPLY(filename);
00064 } else {
00065 DBGA("Uknown geometry file extension: " << extension.latin1());
00066 return FAILURE;
00067 }
00068 }
00069
00070 int GraspitDBModel::load(World* w)
00071 {
00072
00073 if(mGraspableBody) delete mGraspableBody;
00074
00075 mGraspableBody = new GraspableBody(w, ModelName().c_str());
00076 mGraspableBody->setDBModel(this);
00077
00078 mGraspableBody->setMaterial(w->getMaterialIdx("wood"));
00079
00080
00081
00082
00083 SoScale* scale = new SoScale();
00084 scale->scaleFactor.setValue(RescaleFactor(), RescaleFactor(), RescaleFactor());
00085 mGraspableBody->getIVGeomRoot()->addChild(scale);
00086
00087 if (loadGeometry() != SUCCESS) {
00088 mGeometryLoaded = false;
00089 return FAILURE;
00090 }
00091 mGeometryLoaded = true;
00092 mGraspableBody->addIVMat();
00093
00094
00095 mGraspableBody->setDefaultDynamicParameters();
00096
00097 double I[] = {4853.0, -1.1196, -6.5156, -1.1196, 4853.0, 47.542, -6.5156, 0.0, 2357.6};
00098 mGraspableBody->setInertiaMatrix(I);
00099 mGraspableBody->setMaxRadius(mGraspableBody->computeDefaultMaxRadius());
00100 mGraspableBody->setMass(300);
00101
00102 return SUCCESS;
00103 }
00104
00110 void GraspitDBModel::unload()
00111 {
00112 delete mGraspableBody; mGraspableBody = NULL;
00113 mGeometryLoaded = false;
00114 }
00115
00119 int GeomGraspitDBModel::loadGeometry()
00120 {
00121
00122 if (GetVertices().empty()) {
00123 if (!mManager) {
00124 DBGA("Cannot load geometry from database; missing manager");
00125 return FAILURE;
00126 }
00127 if (!mManager->LoadModelGeometry(this)) {
00128 DBGA("Manager failed to load geometry for model from database");
00129 return FAILURE;
00130 }
00131 if (GetVertices().empty() || GetTriangles().empty()) {
00132 DBGA("Empty geometry loaded from database");
00133 return FAILURE;
00134 }
00135 }
00136
00137 std::vector<position> vertices;
00138 if ( GetVertices().size() % 3 != 0 ) {
00139 DBGA("Load model geometry from database: size of vertices vector is not a multiple of 3");
00140 return FAILURE;
00141 }
00142 for (size_t i=0; i<GetVertices().size()/3; i++) {
00143 vertices.push_back( position(GetVertices().at(3*i+0), GetVertices().at(3*i+1), GetVertices().at(3*i+2)) );
00144 }
00145
00146 return mGraspableBody->loadGeometryMemory(vertices, GetTriangles());
00147 }
00148