ViSP  2.8.0
vpMbHiddenFaces.h
1 /****************************************************************************
2  *
3  * $Id: vpMbTracker.h 4004 2012-11-23 17:34:44Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Generic model based tracker. This class declares the methods to implement in
35  * order to have a model based tracker.
36  *
37  * Authors:
38  * Romain Tallonneau
39  * AurĂ©lien Yol
40  *
41  *****************************************************************************/
42 #pragma once
43 
44 #ifndef vpMbHiddenFaces_HH
45 #define vpMbHiddenFaces_HH
46 
47 #include <visp/vpHomogeneousMatrix.h>
48 #include <visp/vpMeterPixelConversion.h>
49 #include <visp/vpPixelMeterConversion.h>
50 #include <visp/vpMbtPolygon.h>
51 
52 #ifdef VISP_HAVE_OGRE
53  #include <visp/vpAROgre.h>
54 #endif
55 
56 #include <vector>
57 
66 template<class PolygonType = vpMbtPolygon>
68 {
69  private:
71  std::vector<PolygonType *> Lpol ;
73  unsigned int nbVisiblePolygon;
74 
75 #ifdef VISP_HAVE_OGRE
76  vpImage<unsigned char> ogreBackground;
77  bool ogreInitialised;
78  vpAROgre *ogre;
79  std::vector< Ogre::ManualObject* > lOgrePolygons;
80 #endif
81 
82  unsigned int setVisiblePrivate(const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears,
83  bool &changed,
84  bool useOgre = false, bool testRoi = false,
87  ) ;
88 
89 
90  public :
91  vpMbHiddenFaces() ;
93 
94  void addPolygon(PolygonType *p) ;
95 
96 #ifdef VISP_HAVE_OGRE
97  void displayOgre(const vpHomogeneousMatrix &_cMo);
98 #endif
99 
105  std::vector<PolygonType*>& getPolygon() {return Lpol;}
106 
107 #ifdef VISP_HAVE_OGRE
109 #endif
110 
116  unsigned int getNbVisiblePolygon() const {return nbVisiblePolygon;}
117 
118 #ifdef VISP_HAVE_OGRE
119 
124  vpAROgre* getOgreContext(){return ogre;}
125 #endif
126 
127  bool isAppearing(const unsigned int i){ return Lpol[i]->isAppearing(); }
128 
129 
130 #ifdef VISP_HAVE_OGRE
131 
136  bool isOgreInitialised() { return ogreInitialised; }
137 #endif
138 
146  bool isVisible(const unsigned int i){ return Lpol[i]->isVisible(); }
147 
148 #ifdef VISP_HAVE_OGRE
149  bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index);
150 #endif
151 
153  inline PolygonType* operator[](const unsigned int i) { return Lpol[i];}
155  inline const PolygonType* operator[](const unsigned int i) const { return Lpol[i];}
156 
157  void reset();
158 
159 #ifdef VISP_HAVE_OGRE
160 
169  void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w) { ogreBackground.resize(h,w); }
170 #endif
171 
172  unsigned int setVisible(const vpImage<unsigned char>& _I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angle, bool &changed) ;
173  unsigned int setVisible(const vpImage<unsigned char>& _I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed) ;
174  unsigned int setVisible(const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed) ;
175 
176 #ifdef VISP_HAVE_OGRE
177  unsigned int setVisibleOgre(const vpImage<unsigned char>& _I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed) ;
178  unsigned int setVisibleOgre(const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed) ;
179 #endif
180 
185  inline unsigned int size() const { return (unsigned int)Lpol.size(); }
186 
187 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
188  bool depthTest;
190 
201  vp_deprecated bool getDepthTest(){return depthTest;}
209  vp_deprecated void setDepthTest(const bool &d){depthTest = d;}
210  unsigned int setVisible(const vpHomogeneousMatrix &_cMo) ;
211 #endif
212 } ;
213 
217 template<class PolygonType>
219 {
220 #ifdef VISP_HAVE_OGRE
221  ogreInitialised = false;
222  ogre = new vpAROgre();
223  ogre->setShowConfigDialog(false);
224  ogreBackground = vpImage<unsigned char>(480, 640);
225 #endif
226 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
227  depthTest = false;
228 #endif
229 }
230 
231 
235 template<class PolygonType>
237 {
238  for(unsigned int i = 0 ; i < Lpol.size() ; i++){
239  if (Lpol[i]!=NULL){
240  delete Lpol[i] ;
241  }
242  Lpol[i] = NULL ;
243  }
244  Lpol.resize(0);
245 
246 #ifdef VISP_HAVE_OGRE
247  delete ogre;
248 #endif
249 }
250 
256 template<class PolygonType>
257 void
259 {
260  PolygonType *p_new = new PolygonType;
261  p_new->index = p->index;
262  p_new->setNbPoint(p->nbpt);
263  p_new->isvisible = p->isvisible;
264  for(unsigned int i = 0; i < p->nbpt; i++)
265  p_new->p[i]= p->p[i];
266  Lpol.push_back(p_new);
267 }
268 
272 template<class PolygonType>
273 void
275 {
276  nbVisiblePolygon = 0;
277  for(unsigned int i = 0 ; i < Lpol.size() ; i++){
278  if (Lpol[i]!=NULL){
279  delete Lpol[i] ;
280  }
281  Lpol[i] = NULL ;
282  }
283  Lpol.resize(0);
284 }
285 
300 template<class PolygonType>
301 unsigned int
302 vpMbHiddenFaces<PolygonType>::setVisiblePrivate(const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears,
303  bool &changed, bool useOgre, bool testRoi,
304  const vpImage<unsigned char> &_I,
305  const vpCameraParameters &_cam
306  )
307 {
308  nbVisiblePolygon = 0;
309  changed = false;
310 
311  vpTranslationVector cameraPos;
312 
313  if(useOgre){
314 #ifdef VISP_HAVE_OGRE
315  _cMo.inverse().extract(cameraPos);
316  ogre->renderOneFrame(ogreBackground, _cMo);
317 #else
318  vpTRACE("ViSP doesn't have Ogre3D, simple visibility test used");
319 #endif
320  }
321 
322  for (unsigned int i = 0; i < Lpol.size(); i += 1){
323  Lpol[i]->changeFrame(_cMo);
324  Lpol[i]->isappearing = false;
325 
326  if(Lpol[i]->isVisible())
327  {
328  bool testDisappear = false;
329  unsigned int nbCornerInsidePrev = 0;
330 
331  if(testRoi){
332  nbCornerInsidePrev = Lpol[i]->getNbCornerInsidePrevImage();
333  if(Lpol[i]->getNbCornerInsideImage(_I, _cam) == 0)
334  testDisappear = true;
335  }
336 
337  if(!testDisappear){
338  if(useOgre)
339 #ifdef VISP_HAVE_OGRE
340  testDisappear = ((!Lpol[i]->isVisible(_cMo, angleDisappears, true)) || !isVisibleOgre(cameraPos,i));
341 #else
342  testDisappear = (!Lpol[i]->isVisible(_cMo, angleDisappears));
343 #endif
344  else
345  testDisappear = (!Lpol[i]->isVisible(_cMo, angleDisappears));
346  }
347 
348  // test if the face is still visible
349  if(testDisappear){
350 // std::cout << "Face " << i << " disappears" << std::endl;
351  changed = true;
352  Lpol[i]->isvisible = false;
353  }
354  else {
355  nbVisiblePolygon++;
356  Lpol[i]->isvisible = true;
357 
358  if(nbCornerInsidePrev > Lpol[i]->getNbCornerInsidePrevImage())
359  changed = true;
360  }
361  }
362  else
363  {
364  bool testAppear = true;
365 
366  if(testRoi && Lpol[i]->getNbCornerInsideImage(_I, _cam) == 0)
367  testAppear = false;
368 
369  if(testAppear){
370  if(useOgre)
371 #ifdef VISP_HAVE_OGRE
372  testAppear = ((Lpol[i]->isVisible(_cMo, angleAppears, true)) && isVisibleOgre(cameraPos,i));
373 #else
374  testAppear = (Lpol[i]->isVisible(_cMo, angleAppears));
375 #endif
376  else
377  testAppear = (Lpol[i]->isVisible(_cMo, angleAppears));
378  }
379 
380  if(testAppear){
381 // std::cout << "Face " << i << " appears" << std::endl;
382  Lpol[i]->isvisible = true;
383  changed = true;
384  nbVisiblePolygon++;
385  }
386  else
387  Lpol[i]->isvisible = false;
388  }
389  }
390 
391 // std::cout << "Nombre de polygones visibles: " << nbVisiblePolygon << std::endl;
392  return nbVisiblePolygon;
393 }
394 
406 template<class PolygonType>
407 unsigned int
408 vpMbHiddenFaces<PolygonType>::setVisible(const vpImage<unsigned char>& _I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angle, bool &changed)
409 {
410  return setVisible(_I, _cam, _cMo, angle, angle, changed);
411 }
412 
425 template<class PolygonType>
426 unsigned int
427 vpMbHiddenFaces<PolygonType>::setVisible(const vpImage<unsigned char>& _I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
428 {
429  return setVisiblePrivate(_cMo,angleAppears,angleDisappears,changed,false,true,_I,_cam);
430 }
431 
442 template<class PolygonType>
443 unsigned int
444 vpMbHiddenFaces<PolygonType>::setVisible(const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
445 {
446  return setVisiblePrivate(_cMo,angleAppears,angleDisappears,changed,false);
447 }
448 
449 #ifdef VISP_HAVE_OGRE
450 
455 template<class PolygonType>
456 void
458 {
459  ogreInitialised = true;
460  ogre->setCameraParameters(_cam);
461  ogre->init(ogreBackground, false, true);
462 
463  for(unsigned int n = 0 ; n < Lpol.size(); n++){
464  Ogre::ManualObject* manual = ogre->getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
465 
466  manual->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
467  for(unsigned int i = 0; i < Lpol[n]->nbpt; i++){
468  manual->position( (Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(), (Ogre::Real)Lpol[n]->p[i].get_oZ());
469  manual->colour(1.0, 1.0, 1.0);
470  manual->index(i);
471  }
472 
473  manual->index(0);
474  manual->end();
475 
476  ogre->getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
477 
478  lOgrePolygons.push_back(manual);
479  }
480 }
481 
487 template<class PolygonType>
488 void
490 {
491  if(ogreInitialised && !ogre->isWindowHidden()){
492  for(unsigned int i = 0 ; i < Lpol.size() ; i++){
493  if(Lpol[i]->isVisible()){
494  lOgrePolygons[i]->setVisible(true);
495  }
496  else
497  lOgrePolygons[i]->setVisible(false);
498  }
499  ogre->display(ogreBackground, _cMo);
500  }
501 }
502 
515 template<class PolygonType>
516 unsigned int
517 vpMbHiddenFaces<PolygonType>::setVisibleOgre(const vpImage<unsigned char>& _I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
518 {
519  return setVisiblePrivate(_cMo,angleAppears,angleDisappears,changed,true,true,_I,_cam);
520 }
521 
532 template<class PolygonType>
533 unsigned int
534 vpMbHiddenFaces<PolygonType>::setVisibleOgre(const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
535 {
536  return setVisiblePrivate(_cMo,angleAppears,angleDisappears,changed,true);
537 }
538 
547 template<class PolygonType>
548 bool
549 vpMbHiddenFaces<PolygonType>::isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
550 {
551 // std::cout << "visible" << std::endl;
552  // A line is always visible
553  if(Lpol[index]->getNbPoint() <= 2){
554  lOgrePolygons[index]->setVisible(true);
555  Lpol[index]->isvisible = true;
556  return true;
557  }
558 
559  Ogre::Vector3 camera((Ogre::Real)cameraPos[0],(Ogre::Real)cameraPos[1],(Ogre::Real)cameraPos[2]);
560  if(!ogre->getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())){
561  lOgrePolygons[index]->setVisible(false);
562  Lpol[index]->isvisible = false;
563  return false;
564  }
565 
566  //Get the center of gravity
567  Ogre::Vector3 origin(0,0,0);
568  for(unsigned int j = 0 ; j < Lpol[index]->getNbPoint() ; j++){
569  Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(), (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
570  origin += tmp;
571  }
572  origin /= (Ogre::Real)Lpol[index]->getNbPoint();
573  Ogre::Vector3 direction = origin - camera;
574 
575  Ogre::RaySceneQuery *mRaySceneQuery = ogre->getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
576  mRaySceneQuery->setSortByDistance(true);
577 
578  Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
579  Ogre::RaySceneQueryResult::iterator it = result.begin();
580 
581  bool visible = false;
582  double distance, distancePrev;
583  if(it != result.end()){
584  if(it->movable->getName().find("SimpleRenderable") != Ogre::String::npos) //Test if the ogreBackground is intersect in first
585  it++;
586 
587  if(it != result.end()){
588  distance = it->distance;
589  distancePrev = distance;
590  if(it->movable->getName() == Ogre::StringConverter::toString(index)){
591  visible = true;
592  }
593  else{
594  it++;
595  while(!visible && it != result.end()){
596  distance = it->distance;
597  if(distance == distancePrev){
598  if(it->movable->getName() == Ogre::StringConverter::toString(index)){
599  visible = true;
600  break;
601  }
602  it++;
603  distancePrev = distance;
604  }
605  else
606  break;
607  }
608  }
609  }
610  }
611 
612  if(visible){
613  lOgrePolygons[index]->setVisible(true);
614  Lpol[index]->isvisible = true;
615  }
616  else{
617  lOgrePolygons[index]->setVisible(false);
618  Lpol[index]->isvisible = false;
619  }
620 
621  ogre->getSceneManager()->destroyQuery(mRaySceneQuery);
622 
623  return Lpol[index]->isvisible;
624 }
625 #endif //VISP_HAVE_OGRE
626 
627 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
628 
637 template<class PolygonType>
638 unsigned int
640 {
641  nbVisiblePolygon = 0 ;
642 
643  for(unsigned int i = 0 ; i < Lpol.size() ; i++){
644  if (Lpol[i]->isVisible(_cMo, depthTest)){
645  nbVisiblePolygon++;
646  }
647  }
648  return nbVisiblePolygon ;
649 }
650 #endif //VISP_BUILD_DEPRECATED_FUNCTIONS
651 
652 #endif // vpMbHiddenFaces
653 
const PolygonType * operator[](const unsigned int i) const
operator[] as reader.
bool isAppearing(const unsigned int i)
Implementation of the polygons management for the model-based trackers.
Ogre::Camera * getCamera()
Definition: vpAROgre.h:142
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
unsigned int getNbVisiblePolygon() const
void setShowConfigDialog(const bool showConfigDialog)
Definition: vpAROgre.h:221
void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
PolygonType * operator[](const unsigned int i)
operator[] as modifier.
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:535
void initOgre(vpCameraParameters _cam=vpCameraParameters())
Implementation of an augmented reality viewer.
Definition: vpAROgre.h:90
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:561
vp_deprecated void setDepthTest(const bool &d)
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:148
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:597
bool depthTest
Boolean specifying if a polygon has to be entirely in front of the camera or not. ...
unsigned int setVisibleOgre(const vpImage< unsigned char > &_I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
vpAROgre * getOgreContext()
unsigned int setVisible(const vpImage< unsigned char > &_I, const vpCameraParameters &_cam, const vpHomogeneousMatrix &_cMo, const double &angle, bool &changed)
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:131
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:637
Generic class defining intrinsic camera parameters.
void extract(vpRotationMatrix &R) const
void addPolygon(PolygonType *p)
unsigned int size() const
vp_deprecated bool getDepthTest()
vpHomogeneousMatrix inverse() const
bool isWindowHidden()
Definition: vpAROgre.h:158
bool isVisible(const unsigned int i)
void displayOgre(const vpHomogeneousMatrix &_cMo)
Class that consider the case of a translation vector.
std::vector< PolygonType * > & getPolygon()