00001 #include <ros/console.h>
00002 #include <blort/Tracker/TextureTracker.h>
00003 #include <blort/TomGine/tgLighting.h>
00004 #include <blort/TomGine/tgMaterial.h>
00005 #include <blort/TomGine/tgError.h>
00006
00007
00008 using namespace std;
00009 using namespace Tracking;
00010 using namespace TomGine;
00011
00012
00013
00014
00015 void TextureTracker::model_processing(ModelEntry* modelEntry){
00016
00017 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00018
00019
00020 if(modelEntry->model.getTextured()){
00021 glDisable(GL_DEPTH_TEST);
00022 glDepthMask(0);
00023 m_ip->render(m_tex_frame);
00024 glDepthMask(1);
00025 glEnable(GL_DEPTH_TEST);
00026 }
00027
00028
00029 m_cam_perspective.Activate();
00030 m_lighting.Activate();
00031 modelEntry->pose.Activate();
00032 modelEntry->model.restoreTexture();
00033 modelEntry->model.drawPass();
00034
00035
00036 mat4 modelview, projection;
00037 glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
00038 glGetFloatv(GL_PROJECTION_MATRIX, projection);
00039 modelEntry->modelviewprojection = projection * modelview;
00040
00041
00042 m_shadeCompare->bind();
00043 m_shadeCompare->setUniform("modelviewprojection", modelEntry->modelviewprojection, GL_FALSE);
00044 m_shadeCompare->unbind();
00045 m_shadeConfidenceMM->bind();
00046 m_shadeConfidenceMM->setUniform("modelviewprojection", modelEntry->modelviewprojection, GL_FALSE);
00047 m_shadeConfidenceMM->unbind();
00048 modelEntry->pose.Deactivate();
00049 m_lighting.Deactivate();
00050
00051
00052 m_tex_model->copyTexImage2D(params.camPar.width, params.camPar.height);
00053
00054
00055 glDisable(GL_DEPTH_TEST);
00056 glDepthMask(0);
00057
00058
00059 if(modelEntry->model.getTextured()){
00060 m_ip->gauss(m_tex_model, m_tex_model_ip[0]);
00061 m_ip->sobel(m_tex_model_ip[0], m_tex_model_ip[0], params.model_sobel_th, true);
00062 }else{
00063 m_ip->sobel(m_tex_model, m_tex_model_ip[0], params.model_sobel_th, true);
00064 }
00065
00066 if(modelEntry->mask_geometry_edges){
00067 Texture mask;
00068 this->computeModelEdgeMask(modelEntry, mask);
00069 m_ip->thinning(m_tex_model_ip[0], m_tex_model_ip[0], &mask);
00070 }else{
00071 m_ip->thinning(m_tex_model_ip[0], m_tex_model_ip[0]);
00072 }
00073
00074 for(unsigned i=1; i<params.num_spreadings; i++)
00075 m_ip->spreading(m_tex_model_ip[i-1], m_tex_model_ip[i]);
00076
00077 glDepthMask(1);
00078 }
00079
00080
00081 void TextureTracker::particle_filtering(ModelEntry* modelEntry){
00082 int num_particles;
00083 m_cam_perspective.Activate();
00084
00085
00086 TomGine::tgVector3 vCam = m_cam_perspective.GetPos();
00087 TomGine::tgVector3 vObj = TomGine::tgVector3(modelEntry->pose.t.x, modelEntry->pose.t.y, modelEntry->pose.t.z);
00088 modelEntry->vCam2Model = vObj - vCam;
00089 modelEntry->predictor->setCamViewVector(modelEntry->vCam2Model);
00090
00091 Particle variation = params.variation;
00092
00093 for(unsigned i=0; i<modelEntry->num_recursions; i++){
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 int j = (int)params.num_spreadings - (int)i;
00105 if(j > 1){
00106 params.m_spreadlvl = j - 1;
00107 }else{
00108 params.m_spreadlvl = 1;
00109 }
00110 float varred_t = 1.0f;
00111 float varred_q = 1.0f;
00112 float varred_z = 1.0f;
00113 if(modelEntry->num_recursions > 1){
00114 varred_t = 1.0f - 0.7f * float(i)/(modelEntry->num_recursions - 1);
00115 varred_q = 1.0f - 0.8f * float(i)/(modelEntry->num_recursions - 1);
00116 varred_z = 1.0f - 0.8f * float(i)/(modelEntry->num_recursions - 1);
00117 }
00118 variation.t = params.variation.t * varred_t;
00119 variation.q = params.variation.q * varred_q;
00120 variation.z = params.variation.z * varred_z;
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 modelEntry->distribution.updateLikelihood(modelEntry->model, m_shadeCompare, 1, params.convergence, m_showparticles);
00146
00147
00148
00149
00150
00151
00152
00153
00154 num_particles = modelEntry->num_particles;
00155
00156
00157
00158 modelEntry->predictor->resample(modelEntry->distribution, num_particles, variation, (i==0));
00159
00160
00161 modelEntry->predictor->updateTime(0.0);
00162 }
00163 tgCheckError("TextureTracker::particle_filtering");
00164
00165 modelEntry->pose_prev = modelEntry->pose;
00166 modelEntry->pose = modelEntry->distribution.getMean();
00167
00168 }
00169
00170
00171
00172
00173
00174 TextureTracker::TextureTracker(){
00175 m_lock = false;
00176 m_showparticles = false;
00177 m_showmodel = 0;
00178 m_draw_edges = false;
00179 m_tracker_initialized = false;
00180 m_drawimage = false;
00181 }
00182
00183 TextureTracker::~TextureTracker(){
00184 delete(m_tex_model);
00185 for(unsigned i=0; i<params.num_spreadings; i++){
00186 delete(m_tex_model_ip[i]);
00187 }
00188 }
00189
00190
00191 bool TextureTracker::initInternal(){
00192
00193 int id;
00194
00195
00196 if((id = g_Resources->AddShader("texEdgeTest", "texEdgeTest.vert", "texEdgeTest.frag")) == -1)
00197 exit(1);
00198 m_shadeTexEdgeTest = g_Resources->GetShader(id);
00199
00200 if((id = g_Resources->AddShader("texColorTest", "texColorTest.vert", "texColorTest.frag")) == -1)
00201 exit(1);
00202 m_shadeTexColorTest = g_Resources->GetShader(id);
00203
00204 if((id = g_Resources->AddShader("mmConfidence", "mmConfidence.vert", "mmConfidence.frag")) == -1)
00205 exit(1);
00206 m_shadeConfidenceMM = g_Resources->GetShader(id);
00207
00208 m_shadeCompare = m_shadeTexEdgeTest;
00209
00210
00211 m_tex_model = new Texture();
00212 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00213
00214 m_tex_model_ip.push_back( new Texture() );
00215 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00216 for(int i=0; i<(int)params.num_spreadings-1; i++){
00217 m_tex_model_ip.push_back( new Texture() );
00218 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00219 }
00220
00221
00222 float w = (float)params.camPar.width;
00223 float h = (float)params.camPar.height;
00224
00225 GLfloat offX[9] = { -1.0f/w, 0.0f, 1.0f/w,
00226 -1.0f/w, 0.0f, 1.0f/w,
00227 -1.0f/w, 0.0f, 1.0f/w };
00228 GLfloat offY[9] = { 1.0f/h, 1.0f/h, 1.0f/h,
00229 0.0f, 0.0f, 0.0f,
00230 -1.0f/h,-1.0f/h, -1.0f/h };
00231
00232 m_shadeTexEdgeTest->bind();
00233 m_shadeTexEdgeTest->setUniform("tex_model_edge", 0);
00234 m_shadeTexEdgeTest->setUniform("tex_frame_edge", 1);
00235 m_shadeTexEdgeTest->setUniform("fTol", params.edge_tolerance);
00236 m_shadeTexEdgeTest->setUniform( "mOffsetX", mat3(offX), GL_FALSE );
00237 m_shadeTexEdgeTest->setUniform( "mOffsetY", mat3(offY), GL_FALSE );
00238 m_shadeTexEdgeTest->setUniform("drawcolor", vec4(1.0f,0.0f,0.0f,0.0f));
00239 m_shadeTexEdgeTest->setUniform("kernelsize", (GLint)params.kernel_size);
00240 m_shadeTexEdgeTest->unbind();
00241
00242 m_shadeTexColorTest->bind();
00243 m_shadeTexColorTest->setUniform("tex_model_color", 2);
00244 m_shadeTexColorTest->setUniform("tex_frame_color", 3);
00245 m_shadeTexColorTest->setUniform("fTol", params.edge_tolerance);
00246 m_shadeTexColorTest->setUniform( "mOffsetX", mat3(offX), GL_FALSE );
00247 m_shadeTexColorTest->setUniform( "mOffsetY", mat3(offY), GL_FALSE );
00248 m_shadeTexColorTest->setUniform("drawcolor", vec4(0.0f,0.0f,1.0f,0.0f));
00249 m_shadeTexColorTest->setUniform("kernelsize", (GLint)params.kernel_size);
00250 m_shadeTexColorTest->unbind();
00251
00252 m_shadeConfidenceMM->bind();
00253 m_shadeConfidenceMM->setUniform("tex_model_color", 2);
00254 m_shadeConfidenceMM->setUniform("tex_frame_color", 3);
00255 m_shadeConfidenceMM->setUniform("fTol", params.edge_tolerance);
00256 m_shadeConfidenceMM->setUniform( "mOffsetX", mat3(offX), GL_FALSE );
00257 m_shadeConfidenceMM->setUniform( "mOffsetY", mat3(offY), GL_FALSE );
00258 m_shadeConfidenceMM->setUniform("drawcolor", vec4(0.0f,0.0f,1.0f,0.0f));
00259 m_shadeConfidenceMM->setUniform("kernelsize", (GLint)params.kernel_size);
00260 m_shadeConfidenceMM->unbind();
00261
00262 tgLight light;
00263 tgVector3 cam_f = m_cam_perspective.GetF();
00264 light.ambient = vec4(0.4f,0.4f,0.4f,1.0f);
00265 light.diffuse = vec4(1.0f,1.0f,1.0f,1.0f);
00266 light.specular = vec4(1.0f,1.0f,1.0f,1.0f);
00267 light.position = vec4(-cam_f.x, -cam_f.y, -cam_f.z, 1.0f);
00268 m_lighting.ApplyLight(light,0);
00269
00270 return true;
00271 }
00272
00273 float TextureTracker::evaluateParticle(ModelEntry* modelEntry)
00274 {
00275 return evaluateParticle(modelEntry, m_shadeCompare);
00276 }
00277
00278
00279 float TextureTracker::evaluateParticle(ModelEntry* modelEntry, Shader* shader){
00280 unsigned int queryMatches;
00281 unsigned int queryEdges;
00282 int v, d;
00283 float c;
00284
00285 glGenQueriesARB(1, &queryMatches);
00286 glGenQueriesARB(1, &queryEdges);
00287
00288 m_cam_perspective.Activate();
00289
00290 glColorMask(0,0,0,0); glDepthMask(0);
00291 glColor3f(1.0,1.0,1.0);
00292
00293 m_tex_model_ip[params.m_spreadlvl]->bind(0);
00294 m_tex_frame_ip[params.m_spreadlvl]->bind(1);
00295 m_tex_model->bind(2);
00296 m_tex_frame->bind(3);
00297
00298
00299 shader->bind();
00300 shader->setUniform("analyze", false);
00301
00302 modelEntry->pose.Activate();
00303
00304
00305 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queryEdges);
00306 shader->setUniform("compare", false);
00307 if(m_showparticles)
00308 glColorMask(1,1,1,1);
00309 modelEntry->model.drawTexturedFaces();
00310 modelEntry->model.drawUntexturedFaces();
00311 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
00312
00313 glColorMask(0,0,0,0);
00314
00315
00316 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queryMatches);
00317 shader->setUniform("compare", true);
00318 shader->setUniform("textured", true);
00319 modelEntry->model.drawTexturedFaces();
00320 shader->setUniform("textured", false);
00321 modelEntry->model.drawUntexturedFaces();
00322 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
00323
00324 modelEntry->pose.Deactivate();
00325
00326 shader->unbind();
00327
00328 glGetQueryObjectivARB(queryMatches, GL_QUERY_RESULT_ARB, &d);
00329 glGetQueryObjectivARB(queryEdges, GL_QUERY_RESULT_ARB, &v);
00330 glColorMask(1,1,1,1); glDepthMask(1);
00331 glDeleteQueriesARB(1, &queryMatches);
00332 glDeleteQueriesARB(1, &queryEdges);
00333
00334 c = modelEntry->distribution.confidenceFunction(d,v);
00335
00336
00337
00338 return c;
00339 }
00340
00341
00342 void TextureTracker::image_processing(unsigned char* image, GLenum format){
00343
00344
00345 m_tex_frame->load(image, params.camPar.width, params.camPar.height, format);
00346
00347
00348 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00349 glDisable(GL_DEPTH_TEST);
00350 glDepthMask(0);
00351 glColor3f(1.0,1.0,1.0);
00352
00353 m_ip->flipUpsideDown(m_tex_frame, m_tex_frame);
00354
00355 m_ip->gauss(m_tex_frame, m_tex_frame_ip[0]);
00356 m_ip->sobel(m_tex_frame_ip[0], m_tex_frame_ip[0], params.image_sobel_th, true);
00357 m_ip->thinning(m_tex_frame_ip[0], m_tex_frame_ip[0]);
00358 for(unsigned i=1; i<params.num_spreadings; i++)
00359 m_ip->spreading(m_tex_frame_ip[i-1], m_tex_frame_ip[i]);
00360
00361 glDepthMask(1);
00362 }
00363
00364
00365 void TextureTracker::image_processing(unsigned char* image, const TomGine::tgModel &m, const tgPose &p, GLenum format){
00366
00367
00368 m_tex_frame->load(image, params.camPar.width, params.camPar.height, format);
00369
00370
00371 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00372 glDisable(GL_DEPTH_TEST);
00373 glDepthMask(0);
00374 glColor3f(1.0,1.0,1.0);
00375
00376 m_ip->flipUpsideDown(m_tex_frame, m_tex_frame);
00377
00378
00379 glDepthMask(1);
00380 glEnable(GL_DEPTH_TEST);
00381 drawModel(m,p);
00382 glDisable(GL_DEPTH_TEST);
00383 glDepthMask(0);
00384 m_tex_frame->copyTexImage2D(params.camPar.width, params.camPar.height);
00385
00386 m_ip->gauss(m_tex_frame, m_tex_frame_ip[0]);
00387 m_ip->sobel(m_tex_frame_ip[0], m_tex_frame_ip[0], params.image_sobel_th, true);
00388 m_ip->thinning(m_tex_frame_ip[0], m_tex_frame_ip[0]);
00389 for(unsigned i=1; i<params.num_spreadings; i++)
00390 m_ip->spreading(m_tex_frame_ip[i-1], m_tex_frame_ip[i]);
00391
00392 glDepthMask(1);
00393 }
00394
00395 void TextureTracker::image_processing(unsigned char* image, int model_id, const tgPose &p, GLenum format){
00396
00397
00398 m_tex_frame->load(image, params.camPar.width, params.camPar.height, format);
00399
00400
00401 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00402 glDisable(GL_DEPTH_TEST);
00403 glDepthMask(0);
00404 glColor3f(1.0,1.0,1.0);
00405
00406 m_ip->flipUpsideDown(m_tex_frame, m_tex_frame);
00407
00408
00409
00410
00411 ModelEntryList::iterator it = m_modellist.begin();
00412 while(it != m_modellist.end()){
00413 if(model_id==(*it)->id){
00414 m_cam_perspective.Activate();
00415 glClear(GL_DEPTH_BUFFER_BIT);
00416 m_lighting.Activate();
00417 glEnable(GL_DEPTH_TEST);
00418 glDepthMask(1);
00419 p.Activate();
00420
00421
00422 (*it)->model.drawPass();
00423
00424 p.Deactivate();
00425 glDepthMask(0);
00426 glDisable(GL_DEPTH_TEST);
00427 m_lighting.Deactivate();
00428 glClear(GL_DEPTH_BUFFER_BIT);
00429 }
00430 ++it;
00431 }
00432 m_tex_frame->copyTexImage2D(params.camPar.width, params.camPar.height);
00433
00434 m_ip->gauss(m_tex_frame, m_tex_frame_ip[0]);
00435 m_ip->sobel(m_tex_frame_ip[0], m_tex_frame_ip[0], params.image_sobel_th, true);
00436 m_ip->thinning(m_tex_frame_ip[0], m_tex_frame_ip[0]);
00437 for(unsigned i=1; i<params.num_spreadings; i++)
00438 m_ip->spreading(m_tex_frame_ip[i-1], m_tex_frame_ip[i]);
00439
00440 glDepthMask(1);
00441 }
00442
00443 bool TextureTracker::track()
00444 {
00445 std::vector<bool> tracking_objects(m_modellist.size(), true);
00446 return track(tracking_objects);
00447 }
00448
00449 bool TextureTracker::track(const std::vector<bool> & tracking_objects)
00450 {
00451 if(!m_tracker_initialized)
00452 {
00453 ROS_ERROR("[TextureTracker::track()] Error tracker not initialised!\n");
00454 return false;
00455 }
00456
00457
00458 for(unsigned i=0; i<m_modellist.size(); i++)
00459 {
00460 if(tracking_objects[i])
00461 {
00462 track(m_modellist[i]);
00463 }
00464 }
00465
00466 tgCheckError("TextureTracker::track()");
00467 return true;
00468 }
00469
00470 bool TextureTracker::track(ModelEntry *modelEntry)
00471 {
00472
00473 model_processing(modelEntry);
00474 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00475
00476 if(m_drawimage)
00477 drawImage(NULL);
00478
00479
00480 if(!modelEntry->lock){
00481 particle_filtering(modelEntry);
00482 if(!m_cam_perspective.GetFrustum()->PointInFrustum(modelEntry->pose.t.x, modelEntry->pose.t.y, modelEntry->pose.t.z))
00483 reset();
00484 }else{
00485 modelEntry->pose.c = evaluateParticle(modelEntry, m_shadeCompare);
00486 }
00487
00488 modelEntry->confidence_edge = evaluateParticle(modelEntry, m_shadeTexEdgeTest);
00489 modelEntry->confidence_color = evaluateParticle(modelEntry, m_shadeTexColorTest);
00490
00491 modelEntry->filter_pose();
00492 modelEntry->evaluate_states(params.variation, params.num_recursions,
00493 params.c_th_base, params.c_th_min, params.c_th_fair,
00494 params.c_mv_not, params.c_mv_slow, params.c_th_lost);
00495
00496 m_ftime = (float)m_timer.Update();
00497 return true;
00498 }
00499
00500 bool TextureTracker::track(int id)
00501 {
00502 if(!m_tracker_initialized)
00503 {
00504 ROS_ERROR("[TextureTracker::track(int)] Error tracker not initialised!\n");
00505 return false;
00506 }
00507
00508 for(unsigned i=0; i<m_modellist.size(); i++)
00509 {
00510 if(m_modellist[i]->id == id)
00511 {
00512 track(m_modellist[i]);
00513 tgCheckError("TextureTracker::track(int)");
00514 return true;
00515 }
00516 }
00517
00518 return false;
00519 }
00520
00521
00522 void TextureTracker::textureFromImage(bool use_num_pixels){
00523 std::vector<tgVertex> vertices;
00524
00525 Parameter tmpParams = params;
00526
00527 params.num_particles = 500;
00528 params.num_recursions = 10;
00529 params.m_spreadlvl = 0;
00530 params.variation = params.variation * 0.1;
00531 setKernelSize(0);
00532
00533 track();
00534
00535 params = tmpParams;
00536 setKernelSize(params.kernel_size);
00537
00538 for(unsigned i=0; i<m_modellist.size(); i++){
00539
00540 m_cam_perspective.Activate();
00541 vector<unsigned> faceUpdateList = m_modellist[i]->model.getFaceUpdateList(m_modellist[i]->pose,
00542 vec3(m_modellist[i]->vCam2Model.x, m_modellist[i]->vCam2Model.y, m_modellist[i]->vCam2Model.z),
00543 params.minTexGrabAngle,
00544 use_num_pixels);
00545
00546 if(!faceUpdateList.empty()){
00547 vertices.clear();
00548 m_modellist[i]->model.textureFromImage( m_tex_frame,
00549 params.camPar.width, params.camPar.height,
00550 m_modellist[i]->pose,
00551 vec3(m_modellist[i]->vCam2Model.x, m_modellist[i]->vCam2Model.y, m_modellist[i]->vCam2Model.z),
00552 params.minTexGrabAngle,
00553 faceUpdateList,
00554 vertices,
00555 &m_cam_perspective);
00556 }
00557 faceUpdateList.clear();
00558 }
00559 }
00560
00561
00562 void TextureTracker::textureFromImage(int id, const TomGine::tgPose &pose, bool use_num_pixels){
00563 std::vector<tgVertex> vertices;
00564
00565 ModelEntry* modelEntry = 0;
00566 ModelEntryList::iterator it = m_modellist.begin();
00567 while(it != m_modellist.end()){
00568 if(id==(*it)->id){
00569 modelEntry = (*it);
00570 }
00571 ++it;
00572 }
00573
00574 modelEntry->pose = pose;
00575
00576 m_cam_perspective.Activate();
00577 vector<unsigned> faceUpdateList = modelEntry->model.getFaceUpdateList(modelEntry->pose,
00578 vec3(modelEntry->vCam2Model.x, modelEntry->vCam2Model.y, modelEntry->vCam2Model.z),
00579 params.minTexGrabAngle,
00580 use_num_pixels);
00581
00582 if(!faceUpdateList.empty()){
00583 vertices.clear();
00584
00585 modelEntry->model.textureFromImage(m_tex_frame,
00586 params.camPar.width, params.camPar.height,
00587 modelEntry->pose,
00588 vec3(modelEntry->vCam2Model.x, modelEntry->vCam2Model.y, modelEntry->vCam2Model.z),
00589 params.minTexGrabAngle,
00590 faceUpdateList,
00591 vertices,
00592 &m_cam_perspective);
00593 }
00594 faceUpdateList.clear();
00595 }
00596
00597 void TextureTracker::untextureModels(){
00598 for(unsigned i=0; i<m_modellist.size(); i++){
00599 m_modellist[i]->model.releasePassList();
00600 }
00601 }
00602
00603
00604 void TextureTracker::drawResult(float linewidth){
00605
00606 m_cam_perspective.Activate();
00607 m_lighting.Activate();
00608
00609 glEnable(GL_DEPTH_TEST);
00610 glClear(GL_DEPTH_BUFFER_BIT);
00611
00612 for(unsigned i=0; i<m_modellist.size(); i++){
00613
00614 {
00615 drawModelEntry(m_modellist[i], linewidth);
00616 }
00617 }
00618
00619
00620
00621
00622
00623 m_lighting.Deactivate();
00624 tgCheckError("TextureTracker::drawResult B");
00625 }
00626
00627
00628 void TextureTracker::drawResult(int id, float linewidth){
00629
00630 m_cam_perspective.Activate();
00631 m_lighting.Activate();
00632
00633 glEnable(GL_DEPTH_TEST);
00634 glClear(GL_DEPTH_BUFFER_BIT);
00635
00636 for(unsigned i=0; i<m_modellist.size(); i++)
00637 {
00638 if(m_modellist[i]->id == id)
00639 {
00640 drawModelEntry(m_modellist[i], linewidth);
00641 break;
00642 }
00643 }
00644
00645
00646
00647
00648
00649 m_lighting.Deactivate();
00650 tgCheckError("TextureTracker::drawResult B");
00651 }
00652
00653 void TextureTracker::drawModelEntry(ModelEntry* modelEntry, float linewidth){
00654
00655 modelEntry->pose.Activate();
00656
00657 switch(m_showmodel){
00658 case 0:
00659 modelEntry->model.restoreTexture();
00660 modelEntry->model.drawPass(false);
00661 break;
00662 case 1:
00663 m_lighting.Deactivate();
00664
00665
00666
00667 if(linewidth<1.0f)
00668 linewidth = 1.0f;
00669 glLineWidth(linewidth);
00670 modelEntry->model.drawEdges();
00671 glColor3f(1.0,1.0,1.0);
00672 break;
00673 case 2:
00674 m_tex_model_ip[params.m_spreadlvl]->bind(0);
00675 m_tex_frame_ip[params.m_spreadlvl]->bind(1);
00676 m_tex_model->bind(2);
00677 m_tex_frame->bind(3);
00678 m_shadeCompare->bind();
00679 m_shadeCompare->setUniform("analyze", true);
00680 m_shadeCompare->setUniform("compare", true);
00681 m_shadeCompare->setUniform("textured", true);
00682 modelEntry->model.drawTexturedFaces();
00683 m_shadeCompare->setUniform("textured", false);
00684 modelEntry->model.drawUntexturedFaces();
00685 m_shadeCompare->unbind();
00686 break;
00687 case 3:
00688 break;
00689 default:
00690 m_showmodel = 0;
00691 break;
00692 }
00693
00694
00695
00696 modelEntry->pose.Deactivate();
00697 }
00698
00699 void TextureTracker::drawTrackerModel(int id, const TomGine::tgPose &p, float linewidth)
00700 {
00701 m_cam_perspective.Activate();
00702 ModelEntryList::iterator it = m_modellist.begin();
00703 while(it != m_modellist.end()){
00704 if(id==(*it)->id){
00705 ModelEntry* modelEntry = (*it);
00706 p.Activate();
00707 switch(m_showmodel){
00708 case 0:
00709 modelEntry->model.restoreTexture();
00710 modelEntry->model.drawPass(false);
00711 break;
00712 case 1:
00713 m_lighting.Deactivate();
00714
00715
00716
00717 if(linewidth<1.0f)
00718 linewidth = 1.0f;
00719 glLineWidth(linewidth);
00720 modelEntry->model.drawEdges();
00721 glColor3f(1.0,1.0,1.0);
00722 break;
00723 case 2:
00724 case 3:
00725 break;
00726 default:
00727 m_showmodel = 0;
00728 break;
00729 }
00730 p.Deactivate();
00731 }
00732 ++it;
00733 }
00734 }
00735
00736 void TextureTracker::evaluatePDF(int id,
00737 float x_min, float y_min,
00738 float x_max, float y_max,
00739 int res,
00740 const char* meshfile, const char* xfile)
00741 {
00742 ModelEntryList::iterator it = m_modellist.begin();
00743 while(it != m_modellist.end()){
00744 if(id==(*it)->id){
00745 vector<float> pdf;
00746 pdf = getPDFxy((*it), x_min, y_min, x_max, y_max, res);
00747 savePDF(pdf, x_min, y_min, x_max, y_max, res, meshfile, xfile);
00748 return;
00749 }
00750 ++it;
00751 }
00752 }
00753
00754
00755 vector<float> TextureTracker::getPDFxy(ModelEntry* modelEntry,
00756 float x_min, float y_min,
00757 float x_max, float y_max,
00758 int res)
00759 {
00760 int i = 0;
00761 ROS_DEBUG("Evaluating PDF constrained to x,y movement only");
00762 float x_step = (x_max-x_min) / res;
00763 float y_step = (y_max-y_min) / res;
00764 float scale = 0.1f;
00765
00766 x_min = (modelEntry->pose.t.x += x_min);
00767 y_min = (modelEntry->pose.t.y += y_min);
00768
00769 vector<float> vPDF;
00770 vPDF.assign(res*res, 0.0);
00771
00772 TomGine::tgFrustum* frustum = m_cam_perspective.GetFrustum();
00773
00774
00775 float p;
00776 i=0;
00777 modelEntry->pose.t.y = y_min;
00778 for(int n=0; n<res; n++){
00779 modelEntry->pose.t.x = x_min;
00780 for(int m=0; m<res; m++){
00781 if(frustum->PointInFrustum(modelEntry->pose.t.x, modelEntry->pose.t.y, modelEntry->pose.t.z)){
00782 p = evaluateParticle(modelEntry, m_shadeTexEdgeTest) * scale;
00783 }else{
00784 p = 0.0;
00785 }
00786 vPDF[i] = p;
00787
00788 i++;
00789 modelEntry->pose.t.x += x_step;
00790 }
00791 modelEntry->pose.t.y += y_step;
00792 }
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808 return vPDF;
00809 }
00810
00811
00812 void TextureTracker::savePDF(vector<float> vPDFMap,
00813 float x_min, float y_min,
00814 float x_max, float y_max,
00815 unsigned res,
00816 const char* meshfile, const char* xfile)
00817 {
00818 unsigned i,d,x,y;
00819
00820 float x_step = (x_max-x_min) / res;
00821 float y_step = (y_max-y_min) / res;
00822
00823 vector<vec3> vertexlist;
00824 vector<vec3> normallist;
00825 vector<vec2> texcoordlist;
00826 vector<unsigned int> indexlist;
00827
00828 for(y=0; y<res; y++){
00829 for(x=0; x<res; x++){
00830 vec3 v[5], n;
00831 vec2 tc;
00832 d=0;
00833
00834 v[0].x = x_min + float(x)*x_step;
00835 v[0].y = y_min + float(y)*y_step;
00836 v[0].z = vPDFMap[y*res+x];
00837
00838 if(x<res-1){
00839 v[1].x = x_min + float(x+1)*x_step;
00840 v[1].y = y_min + float(y)*y_step;
00841 v[1].z = vPDFMap[y*res+(x+1)];
00842 n += (v[1]-v[0]);
00843 d++;
00844 }
00845
00846 if(y<res-1){
00847 v[2].x = x_min + float(x)*x_step;
00848 v[2].y = y_min + float(y+1)*y_step;
00849 v[2].z = vPDFMap[(y+1)*res+x];
00850 n += (v[2]-v[0]);
00851 d++;
00852 }
00853
00854 if(x>0){
00855 v[3].x = x_min + float(x-1)*x_step;
00856 v[3].y = y_min + float(y)*y_step;
00857 v[3].z = vPDFMap[y*res+(x-1)];
00858 n += (v[3]-v[0]);
00859 d++;
00860 }
00861
00862 if(y>0){
00863 v[4].x = x_min + float(x)*x_step;
00864 v[4].y = y_min + float(y-1)*y_step;
00865 v[4].z = vPDFMap[(y-1)*res+x];
00866 n += (v[4]-v[0]);
00867 d++;
00868 }
00869
00870 n = n * (1.0f/float(d));
00871
00872 tc.x = float(x)/res;
00873 tc.y = float(y)/res;
00874
00875 vertexlist.push_back(v[0]);
00876 normallist.push_back(n);
00877 texcoordlist.push_back(tc);
00878
00879 if(x<res-1 && y<res-1){
00880 indexlist.push_back(y*res+x);
00881 indexlist.push_back(y*res+(x+1));
00882 indexlist.push_back((y+1)*res+(x+1));
00883
00884 indexlist.push_back(y*res+x);
00885 indexlist.push_back((y+1)*res+(x+1));
00886 indexlist.push_back((y+1)*res+x);
00887 }
00888 }
00889 }
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908 if(meshfile){
00909 FILE* fd = fopen(meshfile,"w");
00910 fprintf(fd, "ply\nformat ascii 1.0\n");
00911 fprintf(fd, "element vertex %d\n", res*res);
00912 fprintf(fd, "property float x\n");
00913 fprintf(fd, "property float y\n");
00914 fprintf(fd, "property float z\n");
00915 fprintf(fd, "property float nx\n");
00916 fprintf(fd, "property float ny\n");
00917 fprintf(fd, "element face %d\n", (res-1)*(res-1)*2);
00918 fprintf(fd, "property list uchar uint vertex_indices\n");
00919 fprintf(fd, "end_header\n");
00920
00921 for(i=0; i<vertexlist.size(); i++){
00922 vec3 v = vertexlist[i];
00923 vec3 n = normallist[i];
00924 fprintf(fd, "%f %f %f %f %f\n", v.x, v.y, v.z, n.x, n.y);
00925 }
00926
00927 for(i=0; i<indexlist.size(); i+=3){
00928 fprintf(fd, "3 %d %d %d\n", indexlist[i], indexlist[i+1], indexlist[i+2]);
00929 }
00930
00931 ROS_DEBUG(" output written to '%s'", meshfile);
00932
00933 fclose(fd);
00934 }
00935
00936 if(xfile){
00937 FILE* fd2 = fopen(xfile,"w");
00938 y = res>>1;
00939 for(x=0; x<res; x++){
00940 fprintf(fd2, "%f\n", vPDFMap[y*res+x]);
00941 }
00942 ROS_DEBUG(" output written to '%s'", xfile);
00943 fclose(fd2);
00944 }
00945 }
00946
00947
00948 cv::Mat TextureTracker::getModelTexture()
00949 {
00950 return m_tex_model->toCvMat();
00951 }