Visual Servoing Platform  version 3.1.0
vpMbHiddenFaces.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Generic model based tracker. This class declares the methods to implement
33  *in order to have a model based tracker.
34  *
35  * Authors:
36  * Romain Tallonneau
37  * Aurelien Yol
38  *
39  *****************************************************************************/
40 #pragma once
41 
42 #ifndef vpMbHiddenFaces_HH
43 #define vpMbHiddenFaces_HH
44 
45 #include <visp3/core/vpHomogeneousMatrix.h>
46 #include <visp3/core/vpMeterPixelConversion.h>
47 #include <visp3/core/vpPixelMeterConversion.h>
48 #include <visp3/mbt/vpMbScanLine.h>
49 #include <visp3/mbt/vpMbtPolygon.h>
50 
51 #ifdef VISP_HAVE_OGRE
52 #include <visp3/ar/vpAROgre.h>
53 #endif
54 
55 #include <limits>
56 #include <vector>
57 
58 template <class PolygonType> class vpMbHiddenFaces;
59 
60 template <class PolygonType> void swap(vpMbHiddenFaces<PolygonType> &first, vpMbHiddenFaces<PolygonType> &second);
61 
70 template <class PolygonType = vpMbtPolygon> class vpMbHiddenFaces
71 {
72 private:
74  std::vector<PolygonType *> Lpol;
76  unsigned int nbVisiblePolygon;
77  vpMbScanLine scanlineRender;
78 
79 #ifdef VISP_HAVE_OGRE
80  vpImage<unsigned char> ogreBackground;
81  bool ogreInitialised;
82  unsigned int nbRayAttempts;
83  double ratioVisibleRay;
84  vpAROgre *ogre;
85  std::vector<Ogre::ManualObject *> lOgrePolygons;
86  bool ogreShowConfigDialog;
87 #endif
88 
89  unsigned int setVisiblePrivate(const vpHomogeneousMatrix &cMo, const double &angleAppears,
90  const double &angleDisappears, bool &changed, bool useOgre = false,
91  bool not_used = false, const vpImage<unsigned char> &I = vpImage<unsigned char>(),
92  const vpCameraParameters &cam = vpCameraParameters());
93 
94 public:
97  vpMbHiddenFaces(const vpMbHiddenFaces &copy);
99  friend void swap<>(vpMbHiddenFaces &first, vpMbHiddenFaces &second);
100 
101  void addPolygon(PolygonType *p);
102 
103  bool computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
104  bool &changed, bool useOgre, bool not_used, const vpImage<unsigned char> &I,
105  const vpCameraParameters &cam, const vpTranslationVector &cameraPos, unsigned int index);
106 
108 
109  void computeScanLineRender(const vpCameraParameters &cam, const unsigned int &w, const unsigned int &h);
110 
111  void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector<std::pair<vpPoint, vpPoint> > &lines,
112  const bool &displayResults = false);
113 
114  vpMbScanLine &getMbScanLineRenderer() { return scanlineRender; }
115 
116 #ifdef VISP_HAVE_OGRE
117  void displayOgre(const vpHomogeneousMatrix &cMo);
118 #endif
119 
125  std::vector<PolygonType *> &getPolygon() { return Lpol; }
126 
127 #ifdef VISP_HAVE_OGRE
128  void initOgre(const vpCameraParameters &cam = vpCameraParameters());
129 #endif
130 
136  unsigned int getNbVisiblePolygon() const { return nbVisiblePolygon; }
137 
138 #ifdef VISP_HAVE_OGRE
139 
148  unsigned int getNbRayCastingAttemptsForVisibility() { return nbRayAttempts; }
149 
155  vpAROgre *getOgreContext() { return ogre; }
156 
166  double getGoodNbRayCastingAttemptsRatio() { return ratioVisibleRay; }
167 #endif
168 
169  bool isAppearing(const unsigned int i) { return Lpol[i]->isAppearing(); }
170 
171 #ifdef VISP_HAVE_OGRE
172 
177  bool isOgreInitialised() { return ogreInitialised; }
178 #endif
179 
187  bool isVisible(const unsigned int i) { return Lpol[i]->isVisible(); }
188 
189 #ifdef VISP_HAVE_OGRE
190  bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index);
191 #endif
192 
194  inline PolygonType *operator[](const unsigned int i) { return Lpol[i]; }
196  inline const PolygonType *operator[](const unsigned int i) const { return Lpol[i]; }
197 
198  void reset();
199 
200 #ifdef VISP_HAVE_OGRE
201 
211  void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
212  {
213  ogreBackground = vpImage<unsigned char>(h, w, 0);
214  }
215 
225  void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts) { nbRayAttempts = attempts; }
226 
236  void setGoodNbRayCastingAttemptsRatio(const double &ratio)
237  {
238  ratioVisibleRay = ratio;
239  if (ratioVisibleRay > 1.0)
240  ratioVisibleRay = 1.0;
241  if (ratioVisibleRay < 0.0)
242  ratioVisibleRay = 0.0;
243  }
254  inline void setOgreShowConfigDialog(const bool showConfigDialog) { ogreShowConfigDialog = showConfigDialog; }
255 #endif
256 
257  unsigned int setVisible(const vpImage<unsigned char> &I, const vpCameraParameters &cam,
258  const vpHomogeneousMatrix &cMo, const double &angle, bool &changed);
259  unsigned int setVisible(const vpImage<unsigned char> &I, const vpCameraParameters &cam,
260  const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
261  bool &changed);
262  unsigned int setVisible(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
263  bool &changed);
264 
265 #ifdef VISP_HAVE_OGRE
266  unsigned int setVisibleOgre(const vpImage<unsigned char> &I, const vpCameraParameters &cam,
267  const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
268  bool &changed);
269  unsigned int setVisibleOgre(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
270  bool &changed);
271 #endif
272 
277  inline unsigned int size() const { return (unsigned int)Lpol.size(); }
278 };
279 
283 template <class PolygonType>
284 vpMbHiddenFaces<PolygonType>::vpMbHiddenFaces() : Lpol(), nbVisiblePolygon(0), scanlineRender()
285 {
286 #ifdef VISP_HAVE_OGRE
287  ogreInitialised = false;
288  nbRayAttempts = 1;
289  ratioVisibleRay = 1.0;
290  ogreShowConfigDialog = false;
291  ogre = new vpAROgre();
292  ogreBackground = vpImage<unsigned char>(480, 640, 0);
293 #endif
294 }
295 
300 {
301  for (unsigned int i = 0; i < Lpol.size(); i++) {
302  if (Lpol[i] != NULL) {
303  delete Lpol[i];
304  }
305  Lpol[i] = NULL;
306  }
307  Lpol.resize(0);
308 
309 #ifdef VISP_HAVE_OGRE
310  if (ogre != NULL) {
311  delete ogre;
312  ogre = NULL;
313  }
314 
315  // This is already done by calling "delete ogre"
316  // for(unsigned int i = 0 ; i < lOgrePolygons.size() ; i++){
317  // if (lOgrePolygons[i]!=NULL){
318  // delete lOgrePolygons[i];
319  // }
320  // lOgrePolygons[i] = NULL;
321  // }
322 
323  lOgrePolygons.resize(0);
324 #endif
325 }
326 
330 template <class PolygonType>
332  : Lpol(), nbVisiblePolygon(copy.nbVisiblePolygon), scanlineRender(copy.scanlineRender)
333 #ifdef VISP_HAVE_OGRE
334  ,
335  ogreBackground(copy.ogreBackground), ogreInitialised(copy.ogreInitialised), nbRayAttempts(copy.nbRayAttempts),
336  ratioVisibleRay(copy.ratioVisibleRay), ogre(NULL), lOgrePolygons(), ogreShowConfigDialog(copy.ogreShowConfigDialog)
337 #endif
338 {
339  // Copy the list of polygons
340  for (unsigned int i = 0; i < copy.Lpol.size(); i++) {
341  PolygonType *poly = new PolygonType(*copy.Lpol[i]);
342  Lpol.push_back(poly);
343  }
344 }
345 
346 template <class PolygonType> void swap(vpMbHiddenFaces<PolygonType> &first, vpMbHiddenFaces<PolygonType> &second)
347 {
348  using std::swap;
349  swap(first.Lpol, second.Lpol);
350  swap(first.nbVisiblePolygon, second.nbVisiblePolygon);
351  swap(first.scanlineRender, second.scanlineRender);
352 #ifdef VISP_HAVE_OGRE
353  swap(first.ogreInitialised, second.ogreInitialised);
354  swap(first.nbRayAttempts, second.nbRayAttempts);
355  swap(first.ratioVisibleRay, second.ratioVisibleRay);
356  swap(first.ogreShowConfigDialog, second.ogreShowConfigDialog);
357  swap(first.ogre, second.ogre);
358  swap(first.ogreBackground, second.ogreBackground);
359 #endif
360 }
361 
365 template <class PolygonType>
367 {
368  swap(*this, other);
369 
370  return *this;
371 }
372 
378 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::addPolygon(PolygonType *p)
379 {
380  PolygonType *p_new = new PolygonType;
381  p_new->index = p->index;
382  p_new->setNbPoint(p->nbpt);
383  p_new->isvisible = p->isvisible;
384  p_new->useLod = p->useLod;
385  p_new->minLineLengthThresh = p->minLineLengthThresh;
386  p_new->minPolygonAreaThresh = p->minPolygonAreaThresh;
387  p_new->setName(p->name);
388  p_new->hasOrientation = p->hasOrientation;
389 
390  for (unsigned int i = 0; i < p->nbpt; i++)
391  p_new->p[i] = p->p[i];
392  Lpol.push_back(p_new);
393 }
394 
398 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::reset()
399 {
400  nbVisiblePolygon = 0;
401  for (unsigned int i = 0; i < Lpol.size(); i++) {
402  if (Lpol[i] != NULL) {
403  delete Lpol[i];
404  }
405  Lpol[i] = NULL;
406  }
407  Lpol.resize(0);
408 
409 #ifdef VISP_HAVE_OGRE
410  if (ogre != NULL) {
411  delete ogre;
412  ogre = NULL;
413  }
414 
415  // This is already done by calling "delete ogre"
416  // for(unsigned int i = 0 ; i < lOgrePolygons.size() ; i++){
417  // if (lOgrePolygons[i]!=NULL){
418  // delete lOgrePolygons[i];
419  // }
420  // lOgrePolygons[i] = NULL;
421  // }
422 
423  lOgrePolygons.resize(0);
424 
425  ogreInitialised = false;
426  nbRayAttempts = 1;
427  ratioVisibleRay = 1.0;
428  ogre = new vpAROgre();
429  ogreBackground = vpImage<unsigned char>(480, 640);
430 #endif
431 }
432 
440 template <class PolygonType>
442 {
443  for (unsigned int i = 0; i < Lpol.size(); i++) {
444  // For fast result we could just clip visible polygons.
445  // However clipping all of them gives us the possibility to return more
446  // information in the scanline visibility results
447  // if(Lpol[i]->isVisible())
448  {
449  Lpol[i]->changeFrame(cMo);
450  Lpol[i]->computePolygonClipped(cam);
451  }
452  }
453 }
454 
463 template <class PolygonType>
465  const unsigned int &h)
466 {
467  std::vector<std::vector<std::pair<vpPoint, unsigned int> > > polyClipped(Lpol.size());
468  std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> listPolyClipped;
469  std::vector<int> listPolyIndices;
470 
471  for (unsigned int i = 0; i < Lpol.size(); i++) {
472  // For fast result we could just use visible polygons.
473  // However using all of them gives us the possibility to return more
474  // information in the scanline visibility results
475  // if(Lpol[i]->isVisible())
476  {
477  polyClipped[i].clear();
478  Lpol[i]->getPolygonClipped(polyClipped[i]);
479  if (polyClipped[i].size() != 0) {
480  listPolyClipped.push_back(&polyClipped[i]);
481  listPolyIndices.push_back(Lpol[i]->getIndex());
482  }
483  }
484  }
485 
486  scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
487 }
488 
500 template <class PolygonType>
502  std::vector<std::pair<vpPoint, vpPoint> > &lines,
503  const bool &displayResults)
504 {
505  scanlineRender.queryLineVisibility(a, b, lines, displayResults);
506 }
507 
522 template <class PolygonType>
523 unsigned int vpMbHiddenFaces<PolygonType>::setVisiblePrivate(const vpHomogeneousMatrix &cMo, const double &angleAppears,
524  const double &angleDisappears, bool &changed, bool useOgre,
525  bool not_used, const vpImage<unsigned char> &I,
526  const vpCameraParameters &cam)
527 {
528  nbVisiblePolygon = 0;
529  changed = false;
530 
531  vpTranslationVector cameraPos;
532 
533  if (useOgre) {
534 #ifdef VISP_HAVE_OGRE
535  cMo.inverse().extract(cameraPos);
536  ogre->renderOneFrame(ogreBackground, cMo);
537 #else
538  vpTRACE("ViSP doesn't have Ogre3D, simple visibility test used");
539 #endif
540  }
541 
542  for (unsigned int i = 0; i < Lpol.size(); i++) {
543  // std::cout << "Calling poly: " << i << std::endl;
544  if (computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, I, cam, cameraPos, i))
545  nbVisiblePolygon++;
546  }
547  return nbVisiblePolygon;
548 }
549 
566 template <class PolygonType>
567 bool vpMbHiddenFaces<PolygonType>::computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears,
568  const double &angleDisappears, bool &changed, bool useOgre,
569  bool not_used, const vpImage<unsigned char> &I,
570  const vpCameraParameters &cam,
571  const vpTranslationVector &cameraPos, unsigned int index)
572 {
573  (void)not_used;
574  unsigned int i = index;
575  Lpol[i]->changeFrame(cMo);
576  Lpol[i]->isappearing = false;
577 
578  // Commented because we need to compute visibility
579  // even when dealing with line in level of detail case
580  /*if(Lpol[i]->getNbPoint() <= 2)
581  {
582  Lpol[i]->isvisible = true;
583  }
584  else*/ {
585  if (Lpol[i]->isVisible()) {
586  bool testDisappear = false;
587  // unsigned int nbCornerInsidePrev = 0;
588 
589  if (!testDisappear) {
590  if (useOgre)
591 #ifdef VISP_HAVE_OGRE
592  testDisappear = ((!Lpol[i]->isVisible(cMo, angleDisappears, true, cam, I)) || !isVisibleOgre(cameraPos, i));
593 #else
594  {
595  (void)cameraPos; // Avoid warning
596  testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears, false, cam, I));
597  }
598 #endif
599  else
600  testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears, false, cam, I));
601  }
602 
603  // test if the face is still visible
604  if (testDisappear) {
605  // std::cout << "Face " << i << " disappears" <<
606  // std::endl;
607  changed = true;
608  Lpol[i]->isvisible = false;
609  } else {
610  // nbVisiblePolygon++;
611  Lpol[i]->isvisible = true;
612 
613  // if(nbCornerInsidePrev > Lpol[i]->getNbCornerInsidePrevImage())
614  // changed = true;
615  }
616  } else {
617  bool testAppear = true;
618 
619  if (testAppear) {
620  if (useOgre)
621 #ifdef VISP_HAVE_OGRE
622  testAppear = ((Lpol[i]->isVisible(cMo, angleAppears, true, cam, I)) && isVisibleOgre(cameraPos, i));
623 #else
624  testAppear = (Lpol[i]->isVisible(cMo, angleAppears, false, cam, I));
625 #endif
626  else
627  testAppear = (Lpol[i]->isVisible(cMo, angleAppears, false, cam, I));
628  }
629 
630  if (testAppear) {
631  // std::cout << "Face " << i << " appears" << std::endl;
632  Lpol[i]->isvisible = true;
633  changed = true;
634  // nbVisiblePolygon++;
635  } else {
636  // std::cout << "Problem" << std::endl;
637  Lpol[i]->isvisible = false;
638  }
639  }
640  }
641  // std::cout << "Nombre de polygones visibles: " << nbVisiblePolygon <<
642  // std::endl;
643  return Lpol[i]->isvisible;
644 }
645 
657 template <class PolygonType>
659  const vpHomogeneousMatrix &cMo, const double &angle,
660  bool &changed)
661 {
662  return setVisible(I, cam, cMo, angle, angle, changed);
663 }
664 
677 template <class PolygonType>
679  const vpHomogeneousMatrix &cMo, const double &angleAppears,
680  const double &angleDisappears, bool &changed)
681 {
682  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, false, true, I, cam);
683 }
684 
696 template <class PolygonType>
697 unsigned int vpMbHiddenFaces<PolygonType>::setVisible(const vpHomogeneousMatrix &cMo, const double &angleAppears,
698  const double &angleDisappears, bool &changed)
699 {
700  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, false);
701 }
702 
703 #ifdef VISP_HAVE_OGRE
704 
709 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::initOgre(const vpCameraParameters &cam)
710 {
711  ogreInitialised = true;
712  ogre->setCameraParameters(cam);
713  ogre->setShowConfigDialog(ogreShowConfigDialog);
714  ogre->init(ogreBackground, false, true);
715 
716  for (unsigned int n = 0; n < Lpol.size(); n++) {
717  Ogre::ManualObject *manual = ogre->getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
718 
719  manual->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
720  for (unsigned int i = 0; i < Lpol[n]->nbpt; i++) {
721  manual->position((Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(),
722  (Ogre::Real)Lpol[n]->p[i].get_oZ());
723  manual->colour(1.0, 1.0, 1.0);
724  manual->index(i);
725  }
726 
727  manual->index(0);
728  manual->end();
729 
730  ogre->getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
731 
732  lOgrePolygons.push_back(manual);
733  }
734 }
735 
741 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::displayOgre(const vpHomogeneousMatrix &cMo)
742 {
743  if (ogreInitialised && !ogre->isWindowHidden()) {
744  for (unsigned int i = 0; i < Lpol.size(); i++) {
745  if (Lpol[i]->isVisible()) {
746  lOgrePolygons[i]->setVisible(true);
747  } else
748  lOgrePolygons[i]->setVisible(false);
749  }
750  ogre->display(ogreBackground, cMo);
751  }
752 }
753 
766 template <class PolygonType>
768  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
769  const double &angleAppears, const double &angleDisappears,
770  bool &changed)
771 {
772  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, true, true, I, cam);
773 }
774 
786 template <class PolygonType>
787 unsigned int vpMbHiddenFaces<PolygonType>::setVisibleOgre(const vpHomogeneousMatrix &cMo, const double &angleAppears,
788  const double &angleDisappears, bool &changed)
789 {
790  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, true);
791 }
792 
801 template <class PolygonType>
802 bool vpMbHiddenFaces<PolygonType>::isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
803 {
804  Ogre::Vector3 camera((Ogre::Real)cameraPos[0], (Ogre::Real)cameraPos[1], (Ogre::Real)cameraPos[2]);
805  if (!ogre->getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())) {
806  lOgrePolygons[index]->setVisible(false);
807  Lpol[index]->isvisible = false;
808  return false;
809  }
810 
811  // Get the center of gravity
812  bool visible = false;
813  unsigned int nbVisible = 0;
814 
815  for (unsigned int i = 0; i < nbRayAttempts; i++) {
816  Ogre::Vector3 origin(0, 0, 0);
817  Ogre::Real totalFactor = 0.0f;
818 
819  for (unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
820  Ogre::Real factor = 1.0f;
821 
822  if (nbRayAttempts > 1) {
823  int r = rand() % 101;
824 
825  if (r != 0)
826  factor = ((Ogre::Real)r) / 100.0f;
827  }
828 
829  Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(),
830  (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
831  tmp *= factor;
832  origin += tmp;
833  totalFactor += factor;
834  }
835 
836  origin /= totalFactor;
837 
838  Ogre::Vector3 direction = origin - camera;
839  Ogre::Real distanceCollision = direction.length();
840 
841  direction.normalise();
842  Ogre::RaySceneQuery *mRaySceneQuery = ogre->getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
843  mRaySceneQuery->setSortByDistance(true);
844 
845  Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
846  Ogre::RaySceneQueryResult::iterator it = result.begin();
847 
848  // while(it != result.end()){
849  // std::cout << it->movable->getName() << "(" << it->distance<< ") :
850  // " << std::flush; it++;
851  // }
852  // std::cout << std::endl;
853  // it = result.begin();
854 
855  if (it != result.end())
856  if (it->movable->getName().find("SimpleRenderable") !=
857  Ogre::String::npos) // Test if the ogreBackground is intersect in
858  // first
859  ++it;
860 
861  double distance;
862  // In a case of a two-axis aligned segment, ray collision is not always
863  // working.
864  if (Lpol[index]->getNbPoint() == 2 &&
865  (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
866  std::numeric_limits<double>::epsilon()) +
867  (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
868  std::numeric_limits<double>::epsilon()) +
869  (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
870  std::numeric_limits<double>::epsilon())) >= 2)) {
871  if (it != result.end()) {
872  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
873  nbVisible++;
874  } else {
875  distance = it->distance;
876  // Cannot use epsilon for comparison as ray lenght is slightly
877  // different from the collision distance returned by
878  // Ogre::RaySceneQueryResult.
879  if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
880  1e-6 /*std::fabs(distance) * std::numeric_limits<double>::epsilon()*/)
881  nbVisible++;
882  }
883  } else
884  nbVisible++; // Collision not detected but present.
885  } else {
886  if (it != result.end()) {
887  distance = it->distance;
888  double distancePrev = distance;
889 
890  // std::cout << "For " << Ogre::StringConverter::toString(index) << ":
891  // " << it->movable->getName() << " / " << std::flush;
892 
893  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
894  nbVisible++;
895  } else {
896  ++it;
897  while (it != result.end()) {
898  distance = it->distance;
899 
900  if (std::fabs(distance - distancePrev) <
901  1e-6 /*std::fabs(distance) * std::numeric_limits<double>::epsilon()*/) {
902  // std::cout << it->movable->getName() << " / " << std::flush;
903  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
904  nbVisible++;
905  break;
906  }
907  ++it;
908  distancePrev = distance;
909  } else
910  break;
911  }
912  }
913  }
914  }
915 
916  ogre->getSceneManager()->destroyQuery(mRaySceneQuery);
917  }
918 
919  if (((double)nbVisible) / ((double)nbRayAttempts) > ratioVisibleRay ||
920  std::fabs(((double)nbVisible) / ((double)nbRayAttempts) - ratioVisibleRay) <
921  ratioVisibleRay * std::numeric_limits<double>::epsilon())
922  visible = true;
923  else
924  visible = false;
925 
926  if (visible) {
927  lOgrePolygons[index]->setVisible(true);
928  Lpol[index]->isvisible = true;
929  } else {
930  lOgrePolygons[index]->setVisible(false);
931  Lpol[index]->isvisible = false;
932  }
933 
934  return Lpol[index]->isvisible;
935 }
936 #endif // VISP_HAVE_OGRE
937 
938 #endif // vpMbHiddenFaces
vpMbHiddenFaces & operator=(vpMbHiddenFaces other)
unsigned int size() const
bool isAppearing(const unsigned int i)
Implementation of the polygons management for the model-based trackers.
Ogre::Camera * getCamera()
Definition: vpAROgre.h:143
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setShowConfigDialog(const bool showConfigDialog)
Definition: vpAROgre.h:258
void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
PolygonType * operator[](const unsigned int i)
operator[] as modifier.
void setOgreShowConfigDialog(const bool showConfigDialog)
vpMbScanLine & getMbScanLineRenderer()
vpHomogeneousMatrix inverse() const
const PolygonType * operator[](const unsigned int i) const
operator[] as reader.
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:90
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:584
void extract(vpRotationMatrix &R) const
unsigned int setVisibleOgre(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:163
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:618
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
Class that defines what is a point.
Definition: vpPoint.h:58
void computeClippedPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
unsigned int setVisible(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angle, bool &changed)
void initOgre(const vpCameraParameters &cam=vpCameraParameters())
bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
vpAROgre * getOgreContext()
unsigned int getNbVisiblePolygon() const
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:115
double getGoodNbRayCastingAttemptsRatio()
friend void swap(vpMbHiddenFaces &first, vpMbHiddenFaces &second)
#define vpTRACE
Definition: vpDebug.h:416
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:652
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
Generic class defining intrinsic camera parameters.
std::vector< PolygonType * > & getPolygon()
void addPolygon(PolygonType *p)
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
bool isWindowHidden()
Definition: vpAROgre.h:173
bool isVisible(const unsigned int i)
unsigned int getNbRayCastingAttemptsForVisibility()
void displayOgre(const vpHomogeneousMatrix &cMo)
void computeScanLineRender(const vpCameraParameters &cam, const unsigned int &w, const unsigned int &h)
Class that consider the case of a translation vector.
bool computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed, bool useOgre, bool not_used, const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpTranslationVector &cameraPos, unsigned int index)