Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
vpMbHiddenFaces.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Generic model based tracker. This class declares the methods to implement
32  * in order to have a model based tracker.
33  */
34 #pragma once
35 
36 #ifndef vpMbHiddenFaces_HH
37 #define vpMbHiddenFaces_HH
38 
39 #include <visp3/core/vpHomogeneousMatrix.h>
40 #include <visp3/core/vpMeterPixelConversion.h>
41 #include <visp3/core/vpPixelMeterConversion.h>
42 #include <visp3/mbt/vpMbScanLine.h>
43 #include <visp3/mbt/vpMbtPolygon.h>
44 
45 #ifdef VISP_HAVE_OGRE
46 #include <visp3/ar/vpAROgre.h>
47 #endif
48 
49 #include <limits>
50 #include <vector>
51 
52 template <class PolygonType> class vpMbHiddenFaces;
53 
54 template <class PolygonType> void swap(vpMbHiddenFaces<PolygonType> &first, vpMbHiddenFaces<PolygonType> &second);
55 
64 template <class PolygonType = vpMbtPolygon> class vpMbHiddenFaces
65 {
66 private:
68  std::vector<PolygonType *> Lpol;
70  unsigned int nbVisiblePolygon;
71  vpMbScanLine scanlineRender;
72 
73 #ifdef VISP_HAVE_OGRE
74  vpImage<unsigned char> ogreBackground;
75  bool ogreInitialised;
76  unsigned int nbRayAttempts;
77  double ratioVisibleRay;
78  vpAROgre *ogre;
79  std::vector<Ogre::ManualObject *> lOgrePolygons;
80  bool ogreShowConfigDialog;
81 #endif
82 
83  unsigned int setVisiblePrivate(const vpHomogeneousMatrix &cMo, const double &angleAppears,
84  const double &angleDisappears, bool &changed, bool useOgre = false,
85  bool not_used = false, unsigned int width = 0, unsigned int height = 0,
86  const vpCameraParameters &cam = vpCameraParameters());
87 
88 public:
90  virtual ~vpMbHiddenFaces();
93  friend void swap<PolygonType>(vpMbHiddenFaces &first, vpMbHiddenFaces &second);
94 
95  void addPolygon(PolygonType *p);
96 
97  bool computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
98  bool &changed, bool useOgre, bool not_used, unsigned int width, unsigned int height,
99  const vpCameraParameters &cam, const vpTranslationVector &cameraPos, unsigned int index);
100 
102 
103  void computeScanLineRender(const vpCameraParameters &cam, const unsigned int &w, const unsigned int &h);
104 
105  void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector<std::pair<vpPoint, vpPoint> > &lines,
106  const bool &displayResults = false);
107 
108  vpMbScanLine &getMbScanLineRenderer() { return scanlineRender; }
109 
110 #ifdef VISP_HAVE_OGRE
112 #endif
113 
119  std::vector<PolygonType *> &getPolygon() { return Lpol; }
120 
121 #ifdef VISP_HAVE_OGRE
123 #endif
124 
130  unsigned int getNbVisiblePolygon() const { return nbVisiblePolygon; }
131 
132 #ifdef VISP_HAVE_OGRE
142  unsigned int getNbRayCastingAttemptsForVisibility() { return nbRayAttempts; }
143 
149  vpAROgre *getOgreContext() { return ogre; }
150 
160  double getGoodNbRayCastingAttemptsRatio() { return ratioVisibleRay; }
161 #endif
162 
163  bool isAppearing(unsigned int i) { return Lpol[i]->isAppearing(); }
164 
165 #ifdef VISP_HAVE_OGRE
171  bool isOgreInitialised() { return ogreInitialised; }
172 #endif
173 
181  bool isVisible(unsigned int i) { return Lpol[i]->isVisible(); }
182 
183 #ifdef VISP_HAVE_OGRE
184  bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index);
185 #endif
186 
188  inline PolygonType *operator[](unsigned int i) { return Lpol[i]; }
190  inline const PolygonType *operator[](unsigned int i) const { return Lpol[i]; }
191 
192  void reset();
193 
194 #ifdef VISP_HAVE_OGRE
205  void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
206  {
207  ogreBackground = vpImage<unsigned char>(h, w, 0);
208  }
209 
219  void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts) { nbRayAttempts = attempts; }
220 
230  void setGoodNbRayCastingAttemptsRatio(const double &ratio)
231  {
232  ratioVisibleRay = ratio;
233  if (ratioVisibleRay > 1.0)
234  ratioVisibleRay = 1.0;
235  if (ratioVisibleRay < 0.0)
236  ratioVisibleRay = 0.0;
237  }
248  inline void setOgreShowConfigDialog(bool showConfigDialog) { ogreShowConfigDialog = showConfigDialog; }
249 #endif
250 
251  unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam,
252  const vpHomogeneousMatrix &cMo, const double &angle, bool &changed);
253  unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam,
254  const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
255  bool &changed);
256  unsigned int setVisible(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
257  bool &changed);
258 
259 #ifdef VISP_HAVE_OGRE
260  unsigned int setVisibleOgre(unsigned int width, unsigned int height, const vpCameraParameters &cam,
261  const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
262  bool &changed);
263  unsigned int setVisibleOgre(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
264  bool &changed);
265 #endif
271  inline unsigned int size() const { return (unsigned int)Lpol.size(); }
272 };
273 
277 template <class PolygonType>
278 vpMbHiddenFaces<PolygonType>::vpMbHiddenFaces() : Lpol(), nbVisiblePolygon(0), scanlineRender()
279 {
280 #ifdef VISP_HAVE_OGRE
281  ogreInitialised = false;
282  nbRayAttempts = 1;
283  ratioVisibleRay = 1.0;
284  ogreShowConfigDialog = false;
285  ogre = new vpAROgre();
286  ogreBackground = vpImage<unsigned char>(480, 640, 0);
287 #endif
288 }
289 
294 {
295  for (unsigned int i = 0; i < Lpol.size(); i++) {
296  if (Lpol[i] != nullptr) {
297  delete Lpol[i];
298  }
299  Lpol[i] = nullptr;
300  }
301  Lpol.resize(0);
302 
303 #ifdef VISP_HAVE_OGRE
304  if (ogre != nullptr) {
305  delete ogre;
306  ogre = nullptr;
307  }
308 
309  // This is already done by calling "delete ogre"
310  // for(unsigned int i = 0 ; i < lOgrePolygons.size() ; i++){
311  // if (lOgrePolygons[i]!=nullptr){
312  // delete lOgrePolygons[i];
313  // }
314  // lOgrePolygons[i] = nullptr;
315  // }
316 
317  lOgrePolygons.resize(0);
318 #endif
319 }
320 
324 template <class PolygonType>
326  : Lpol(), nbVisiblePolygon(copy.nbVisiblePolygon), scanlineRender(copy.scanlineRender)
327 #ifdef VISP_HAVE_OGRE
328  ,
329  ogreBackground(copy.ogreBackground), ogreInitialised(copy.ogreInitialised), nbRayAttempts(copy.nbRayAttempts),
330  ratioVisibleRay(copy.ratioVisibleRay), ogre(nullptr), lOgrePolygons(), ogreShowConfigDialog(copy.ogreShowConfigDialog)
331 #endif
332 {
333  // Copy the list of polygons
334  for (unsigned int i = 0; i < copy.Lpol.size(); i++) {
335  PolygonType *poly = new PolygonType(*copy.Lpol[i]);
336  Lpol.push_back(poly);
337  }
338 }
339 
340 template <class PolygonType> void swap(vpMbHiddenFaces<PolygonType> &first, vpMbHiddenFaces<PolygonType> &second)
341 {
342  using std::swap;
343  swap(first.Lpol, second.Lpol);
344  swap(first.nbVisiblePolygon, second.nbVisiblePolygon);
345  swap(first.scanlineRender, second.scanlineRender);
346 #ifdef VISP_HAVE_OGRE
347  swap(first.ogreInitialised, second.ogreInitialised);
348  swap(first.nbRayAttempts, second.nbRayAttempts);
349  swap(first.ratioVisibleRay, second.ratioVisibleRay);
350  swap(first.ogreShowConfigDialog, second.ogreShowConfigDialog);
351  swap(first.ogre, second.ogre);
352  swap(first.ogreBackground, second.ogreBackground);
353 #endif
354 }
355 
359 template <class PolygonType>
361 {
362  swap(*this, other);
363 
364  return *this;
365 }
366 
372 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::addPolygon(PolygonType *p)
373 {
374  PolygonType *p_new = new PolygonType;
375  p_new->index = p->index;
376  p_new->setNbPoint(p->nbpt);
377  p_new->isvisible = p->isvisible;
378  p_new->useLod = p->useLod;
379  p_new->minLineLengthThresh = p->minLineLengthThresh;
380  p_new->minPolygonAreaThresh = p->minPolygonAreaThresh;
381  p_new->setName(p->name);
382  p_new->hasOrientation = p->hasOrientation;
383 
384  for (unsigned int i = 0; i < p->nbpt; i++)
385  p_new->p[i] = p->p[i];
386  Lpol.push_back(p_new);
387 }
388 
392 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::reset()
393 {
394  nbVisiblePolygon = 0;
395  for (unsigned int i = 0; i < Lpol.size(); i++) {
396  if (Lpol[i] != nullptr) {
397  delete Lpol[i];
398  }
399  Lpol[i] = nullptr;
400  }
401  Lpol.resize(0);
402 
403 #ifdef VISP_HAVE_OGRE
404  if (ogre != nullptr) {
405  delete ogre;
406  ogre = nullptr;
407  }
408 
409  // This is already done by calling "delete ogre"
410  // for(unsigned int i = 0 ; i < lOgrePolygons.size() ; i++){
411  // if (lOgrePolygons[i]!=nullptr){
412  // delete lOgrePolygons[i];
413  // }
414  // lOgrePolygons[i] = nullptr;
415  // }
416 
417  lOgrePolygons.resize(0);
418 
419  ogreInitialised = false;
420  nbRayAttempts = 1;
421  ratioVisibleRay = 1.0;
422  ogre = new vpAROgre();
423  ogreBackground = vpImage<unsigned char>(480, 640);
424 #endif
425 }
426 
434 template <class PolygonType>
436 {
437  for (unsigned int i = 0; i < Lpol.size(); i++) {
438  // For fast result we could just clip visible polygons.
439  // However clipping all of them gives us the possibility to return more
440  // information in the scanline visibility results
441  // if(Lpol[i]->isVisible())
442  {
443  Lpol[i]->changeFrame(cMo);
444  Lpol[i]->computePolygonClipped(cam);
445  }
446  }
447 }
448 
457 template <class PolygonType>
459  const unsigned int &h)
460 {
461  std::vector<std::vector<std::pair<vpPoint, unsigned int> > > polyClipped(Lpol.size());
462  std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> listPolyClipped;
463  std::vector<int> listPolyIndices;
464 
465  for (unsigned int i = 0; i < Lpol.size(); i++) {
466  // For fast result we could just use visible polygons.
467  // However using all of them gives us the possibility to return more
468  // information in the scanline visibility results
469  // if(Lpol[i]->isVisible())
470  {
471  polyClipped[i].clear();
472  Lpol[i]->getPolygonClipped(polyClipped[i]);
473  if (polyClipped[i].size() != 0) {
474  listPolyClipped.push_back(&polyClipped[i]);
475  listPolyIndices.push_back(Lpol[i]->getIndex());
476  }
477  }
478  }
479 
480  scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
481 }
482 
493 template <class PolygonType>
495  std::vector<std::pair<vpPoint, vpPoint> > &lines,
496  const bool &displayResults)
497 {
498  scanlineRender.queryLineVisibility(a, b, lines, displayResults);
499 }
500 
515 template <class PolygonType>
516 unsigned int vpMbHiddenFaces<PolygonType>::setVisiblePrivate(const vpHomogeneousMatrix &cMo, const double &angleAppears,
517  const double &angleDisappears, bool &changed, bool useOgre,
518  bool not_used, unsigned int width, unsigned int height,
519  const vpCameraParameters &cam)
520 {
521  nbVisiblePolygon = 0;
522  changed = false;
523 
524  vpTranslationVector cameraPos;
525 
526  if (useOgre) {
527 #ifdef VISP_HAVE_OGRE
528  cMo.inverse().extract(cameraPos);
529  ogre->renderOneFrame(ogreBackground, cMo);
530 #else
531  vpTRACE("ViSP doesn't have Ogre3D, simple visibility test used");
532 #endif
533  }
534 
535  for (unsigned int i = 0; i < Lpol.size(); i++) {
536  // std::cout << "Calling poly: " << i << std::endl;
537  if (computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, width, height, cam, cameraPos,
538  i))
539  nbVisiblePolygon++;
540  }
541  return nbVisiblePolygon;
542 }
543 
560 template <class PolygonType>
561 bool vpMbHiddenFaces<PolygonType>::computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears,
562  const double &angleDisappears, bool &changed, bool useOgre,
563  bool not_used, unsigned int width, unsigned int height,
564  const vpCameraParameters &cam,
565  const vpTranslationVector &cameraPos, unsigned int index)
566 {
567  (void)not_used;
568  unsigned int i = index;
569  Lpol[i]->changeFrame(cMo);
570  Lpol[i]->isappearing = false;
571 
572  // Commented because we need to compute visibility
573  // even when dealing with line in level of detail case
574  /*if(Lpol[i]->getNbPoint() <= 2)
575  {
576  Lpol[i]->isvisible = true;
577  }
578  else*/
579  {
580  if (Lpol[i]->isVisible()) {
581  bool testDisappear = false;
582  // unsigned int nbCornerInsidePrev = 0;
583 
584  if (!testDisappear) {
585  if (useOgre)
586 #ifdef VISP_HAVE_OGRE
587  testDisappear =
588  ((!Lpol[i]->isVisible(cMo, angleDisappears, true, cam, width, height)) || !isVisibleOgre(cameraPos, i));
589 #else
590  {
591  (void)cameraPos; // Avoid warning
592  testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears, false, cam, width, height));
593  }
594 #endif
595  else
596  testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears, false, cam, width, height));
597  }
598 
599  // test if the face is still visible
600  if (testDisappear) {
601  // std::cout << "Face " << i << " disappears" <<
602  // std::endl;
603  changed = true;
604  Lpol[i]->isvisible = false;
605  }
606  else {
607  // nbVisiblePolygon++;
608  Lpol[i]->isvisible = true;
609 
610  // if(nbCornerInsidePrev > Lpol[i]->getNbCornerInsidePrevImage())
611  // changed = true;
612  }
613  }
614  else {
615  bool testAppear = true;
616 
617  if (testAppear) {
618  if (useOgre)
619 #ifdef VISP_HAVE_OGRE
620  testAppear =
621  ((Lpol[i]->isVisible(cMo, angleAppears, true, cam, width, height)) && isVisibleOgre(cameraPos, i));
622 #else
623  testAppear = (Lpol[i]->isVisible(cMo, angleAppears, false, cam, width, height));
624 #endif
625  else
626  testAppear = (Lpol[i]->isVisible(cMo, angleAppears, false, cam, width, height));
627  }
628 
629  if (testAppear) {
630  // std::cout << "Face " << i << " appears" << std::endl;
631  Lpol[i]->isvisible = true;
632  changed = true;
633  // nbVisiblePolygon++;
634  }
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>
658 unsigned int vpMbHiddenFaces<PolygonType>::setVisible(unsigned int width, unsigned int height,
659  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
660  const double &angle, bool &changed)
661 {
662  return setVisible(width, height, cam, cMo, angle, angle, changed);
663 }
664 
677 template <class PolygonType>
678 unsigned int vpMbHiddenFaces<PolygonType>::setVisible(unsigned int width, unsigned int height,
679  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
680  const double &angleAppears, const double &angleDisappears,
681  bool &changed)
682 {
683  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, false, true, width, height, cam);
684 }
685 
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
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  }
748  else
749  lOgrePolygons[i]->setVisible(false);
750  }
751  ogre->display(ogreBackground, cMo);
752  }
753 }
754 
767 template <class PolygonType>
768 unsigned int vpMbHiddenFaces<PolygonType>::setVisibleOgre(unsigned int width, unsigned int height,
769  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
770  const double &angleAppears, const double &angleDisappears,
771  bool &changed)
772 {
773  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, true, true, width, height, cam);
774 }
775 
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") != Ogre::String::npos) // Test if the ogreBackground is
857  // intersect in first
858  ++it;
859 
860  double distance;
861  // In a case of a two-axis aligned segment, ray collision is not always
862  // working.
863  if (Lpol[index]->getNbPoint() == 2 &&
864  (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
865  std::numeric_limits<double>::epsilon()) +
866  (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
867  std::numeric_limits<double>::epsilon()) +
868  (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
869  std::numeric_limits<double>::epsilon())) >= 2)) {
870  if (it != result.end()) {
871  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
872  nbVisible++;
873  }
874  else {
875  distance = it->distance;
876  // Cannot use epsilon for comparison as ray length 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  }
884  else
885  nbVisible++; // Collision not detected but present.
886  }
887  else {
888  if (it != result.end()) {
889  distance = it->distance;
890  double distancePrev = distance;
891 
892  // std::cout << "For " << Ogre::StringConverter::toString(index) << ":
893  // " << it->movable->getName() << " / " << std::flush;
894 
895  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
896  nbVisible++;
897  }
898  else {
899  ++it;
900  while (it != result.end()) {
901  distance = it->distance;
902 
903  if (std::fabs(distance - distancePrev) <
904  1e-6 /*std::fabs(distance) * std::numeric_limits<double>::epsilon()*/) {
905  // std::cout << it->movable->getName() << " / " << std::flush;
906  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
907  nbVisible++;
908  break;
909  }
910  ++it;
911  distancePrev = distance;
912  }
913  else
914  break;
915  }
916  }
917  }
918  }
919 
920  ogre->getSceneManager()->destroyQuery(mRaySceneQuery);
921  }
922 
923  if (((double)nbVisible) / ((double)nbRayAttempts) > ratioVisibleRay ||
924  std::fabs(((double)nbVisible) / ((double)nbRayAttempts) - ratioVisibleRay) <
925  ratioVisibleRay * std::numeric_limits<double>::epsilon())
926  visible = true;
927  else
928  visible = false;
929 
930  if (visible) {
931  lOgrePolygons[index]->setVisible(true);
932  Lpol[index]->isvisible = true;
933  }
934  else {
935  lOgrePolygons[index]->setVisible(false);
936  Lpol[index]->isvisible = false;
937  }
938 
939  return Lpol[index]->isvisible;
940 }
941 #endif // VISP_HAVE_OGRE
942 
943 #endif // vpMbHiddenFaces
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:96
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:656
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:258
bool isWindowHidden()
Definition: vpAROgre.h:173
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:163
Ogre::Camera * getCamera()
Definition: vpAROgre.h:143
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:115
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:622
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:588
Generic class defining intrinsic camera parameters.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
void extract(vpRotationMatrix &R) const
Implementation of the polygons management for the model-based trackers.
bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
vpAROgre * getOgreContext()
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
virtual ~vpMbHiddenFaces()
void computeClippedPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
unsigned int setVisibleOgre(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
bool isAppearing(unsigned int i)
vpMbHiddenFaces & operator=(vpMbHiddenFaces other)
vpMbScanLine & getMbScanLineRenderer()
unsigned int size() const
unsigned int setVisibleOgre(unsigned int width, unsigned int height, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
bool isVisible(unsigned int i)
void addPolygon(PolygonType *p)
const PolygonType * operator[](unsigned int i) const
Operator[] as reader.
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
std::vector< PolygonType * > & getPolygon()
unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
unsigned int setVisible(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
unsigned int getNbVisiblePolygon() const
void initOgre(const vpCameraParameters &cam=vpCameraParameters())
unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angle, bool &changed)
double getGoodNbRayCastingAttemptsRatio()
void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
friend void swap(vpMbHiddenFaces &first, vpMbHiddenFaces &second)
PolygonType * operator[](unsigned int i)
Operator[] as modifier.
void computeScanLineRender(const vpCameraParameters &cam, const unsigned int &w, const unsigned int &h)
unsigned int getNbRayCastingAttemptsForVisibility()
void displayOgre(const vpHomogeneousMatrix &cMo)
void setOgreShowConfigDialog(bool showConfigDialog)
bool computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed, bool useOgre, bool not_used, unsigned int width, unsigned int height, const vpCameraParameters &cam, const vpTranslationVector &cameraPos, unsigned int index)
vpMbHiddenFaces(const vpMbHiddenFaces &copy)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
Class that consider the case of a translation vector.
#define vpTRACE
Definition: vpDebug.h:405