Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpMbHiddenFaces.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
35 #ifndef VP_MB_HIDDEN_FACES_H
36 #define VP_MB_HIDDEN_FACES_H
37 
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpDebug.h>
40 #include <visp3/core/vpHomogeneousMatrix.h>
41 #include <visp3/core/vpMeterPixelConversion.h>
42 #include <visp3/core/vpPixelMeterConversion.h>
43 #include <visp3/mbt/vpMbScanLine.h>
44 #include <visp3/mbt/vpMbtPolygon.h>
45 
46 #ifdef VISP_HAVE_OGRE
47 #include <visp3/ar/vpAROgre.h>
48 #endif
49 
50 #include <limits>
51 #include <vector>
52 
53 BEGIN_VISP_NAMESPACE
54 template <class PolygonType> class vpMbHiddenFaces;
55 
56 // Forward declaration to have the operator in the global namespace
57 template <class PolygonType> void swap(vpMbHiddenFaces<PolygonType> &first, vpMbHiddenFaces<PolygonType> &second);
58 END_VISP_NAMESPACE
59 
60 BEGIN_VISP_NAMESPACE
69 template <class PolygonType = vpMbtPolygon> class vpMbHiddenFaces
70 {
71 private:
73  std::vector<PolygonType *> Lpol;
75  unsigned int nbVisiblePolygon;
76  vpMbScanLine scanlineRender;
77 
78 #ifdef VISP_HAVE_OGRE
79  vpImage<unsigned char> ogreBackground;
80  bool ogreInitialised;
81  unsigned int nbRayAttempts;
82  double ratioVisibleRay;
83  vpAROgre *ogre;
84  std::vector<Ogre::ManualObject *> lOgrePolygons;
85  bool ogreShowConfigDialog;
86 #endif
87 
88  unsigned int setVisiblePrivate(const vpHomogeneousMatrix &cMo, const double &angleAppears,
89  const double &angleDisappears, bool &changed, bool useOgre = false,
90  bool not_used = false, unsigned int width = 0, unsigned int height = 0,
91  const vpCameraParameters &cam = vpCameraParameters());
92 
93 public:
95  virtual ~vpMbHiddenFaces();
98  friend void swap<PolygonType>(vpMbHiddenFaces &first, vpMbHiddenFaces &second);
99 
100  void addPolygon(PolygonType *p);
101 
102  bool computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
103  bool &changed, bool useOgre, bool not_used, unsigned int width, unsigned int height,
104  const vpCameraParameters &cam, const vpTranslationVector &cameraPos, unsigned int index);
105 
107 
108  void computeScanLineRender(const vpCameraParameters &cam, const unsigned int &w, const unsigned int &h);
109 
110  void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector<std::pair<vpPoint, vpPoint> > &lines,
111  const bool &displayResults = false);
112 
113  vpMbScanLine &getMbScanLineRenderer() { return scanlineRender; }
114 
115 #ifdef VISP_HAVE_OGRE
117 #endif
118 
124  std::vector<PolygonType *> &getPolygon() { return Lpol; }
125 
126 #ifdef VISP_HAVE_OGRE
128 #endif
129 
135  unsigned int getNbVisiblePolygon() const { return nbVisiblePolygon; }
136 
137 #ifdef VISP_HAVE_OGRE
147  unsigned int getNbRayCastingAttemptsForVisibility() { return nbRayAttempts; }
148 
154  vpAROgre *getOgreContext() { return ogre; }
155 
165  double getGoodNbRayCastingAttemptsRatio() { return ratioVisibleRay; }
166 #endif
167 
168  bool isAppearing(unsigned int i) { return Lpol[i]->isAppearing(); }
169 
170 #ifdef VISP_HAVE_OGRE
176  bool isOgreInitialised() { return ogreInitialised; }
177 #endif
178 
186  bool isVisible(unsigned int i) { return Lpol[i]->isVisible(); }
187 
188 #ifdef VISP_HAVE_OGRE
189  bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index);
190 #endif
191 
193  inline PolygonType *operator[](unsigned int i) { return Lpol[i]; }
195  inline const PolygonType *operator[](unsigned int i) const { return Lpol[i]; }
196 
197  void reset();
198 
199 #ifdef VISP_HAVE_OGRE
210  void setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
211  {
212  ogreBackground = vpImage<unsigned char>(h, w, 0);
213  }
214 
224  void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts) { nbRayAttempts = attempts; }
225 
235  void setGoodNbRayCastingAttemptsRatio(const double &ratio)
236  {
237  ratioVisibleRay = ratio;
238  if (ratioVisibleRay > 1.0)
239  ratioVisibleRay = 1.0;
240  if (ratioVisibleRay < 0.0)
241  ratioVisibleRay = 0.0;
242  }
253  inline void setOgreShowConfigDialog(bool showConfigDialog) { ogreShowConfigDialog = showConfigDialog; }
254 #endif
255 
256  unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam,
257  const vpHomogeneousMatrix &cMo, const double &angle, bool &changed);
258  unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam,
259  const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
260  bool &changed);
261  unsigned int setVisible(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
262  bool &changed);
263 
264 #ifdef VISP_HAVE_OGRE
265  unsigned int setVisibleOgre(unsigned int width, unsigned int height, const vpCameraParameters &cam,
266  const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
267  bool &changed);
268  unsigned int setVisibleOgre(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears,
269  bool &changed);
270 #endif
276  inline unsigned int size() const { return (unsigned int)Lpol.size(); }
277 };
278 
282 template <class PolygonType>
283 vpMbHiddenFaces<PolygonType>::vpMbHiddenFaces() : Lpol(), nbVisiblePolygon(0), scanlineRender()
284 {
285 #ifdef VISP_HAVE_OGRE
286  ogreInitialised = false;
287  nbRayAttempts = 1;
288  ratioVisibleRay = 1.0;
289  ogreShowConfigDialog = false;
290  ogre = new vpAROgre();
291  ogreBackground = vpImage<unsigned char>(480, 640, 0);
292 #endif
293 }
294 
299 {
300  for (unsigned int i = 0; i < Lpol.size(); i++) {
301  if (Lpol[i] != nullptr) {
302  delete Lpol[i];
303  }
304  Lpol[i] = nullptr;
305  }
306  Lpol.resize(0);
307 
308 #ifdef VISP_HAVE_OGRE
309  if (ogre != nullptr) {
310  delete ogre;
311  ogre = nullptr;
312  }
313 
314  // This is already done by calling "delete ogre"
315  // for(unsigned int i = 0 ; i < lOgrePolygons.size() ; i++){
316  // if (lOgrePolygons[i]!=nullptr){
317  // delete lOgrePolygons[i];
318  // }
319  // lOgrePolygons[i] = nullptr;
320  // }
321 
322  lOgrePolygons.resize(0);
323 #endif
324 }
325 
329 template <class PolygonType>
331  : Lpol(), nbVisiblePolygon(copy.nbVisiblePolygon), scanlineRender(copy.scanlineRender)
332 #ifdef VISP_HAVE_OGRE
333  ,
334  ogreBackground(copy.ogreBackground), ogreInitialised(copy.ogreInitialised), nbRayAttempts(copy.nbRayAttempts),
335  ratioVisibleRay(copy.ratioVisibleRay), ogre(nullptr), lOgrePolygons(), ogreShowConfigDialog(copy.ogreShowConfigDialog)
336 #endif
337 {
338  // Copy the list of polygons
339  for (unsigned int i = 0; i < copy.Lpol.size(); i++) {
340  PolygonType *poly = new PolygonType(*copy.Lpol[i]);
341  Lpol.push_back(poly);
342  }
343 }
344 
345 template <class PolygonType> void swap(vpMbHiddenFaces<PolygonType> &first, vpMbHiddenFaces<PolygonType> &second)
346 {
347  using std::swap;
348  swap(first.Lpol, second.Lpol);
349  swap(first.nbVisiblePolygon, second.nbVisiblePolygon);
350  swap(first.scanlineRender, second.scanlineRender);
351 #ifdef VISP_HAVE_OGRE
352  swap(first.ogreInitialised, second.ogreInitialised);
353  swap(first.nbRayAttempts, second.nbRayAttempts);
354  swap(first.ratioVisibleRay, second.ratioVisibleRay);
355  swap(first.ogreShowConfigDialog, second.ogreShowConfigDialog);
356  swap(first.ogre, second.ogre);
357  swap(first.ogreBackground, second.ogreBackground);
358 #endif
359 }
360 
364 template <class PolygonType>
366 {
367  swap(*this, other);
368 
369  return *this;
370 }
371 
377 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::addPolygon(PolygonType *p)
378 {
379  PolygonType *p_new = new PolygonType;
380  p_new->index = p->index;
381  p_new->setNbPoint(p->nbpt);
382  p_new->isvisible = p->isvisible;
383  p_new->useLod = p->useLod;
384  p_new->minLineLengthThresh = p->minLineLengthThresh;
385  p_new->minPolygonAreaThresh = p->minPolygonAreaThresh;
386  p_new->setName(p->name);
387  p_new->hasOrientation = p->hasOrientation;
388 
389  for (unsigned int i = 0; i < p->nbpt; i++)
390  p_new->p[i] = p->p[i];
391  Lpol.push_back(p_new);
392 }
393 
397 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::reset()
398 {
399  nbVisiblePolygon = 0;
400  for (unsigned int i = 0; i < Lpol.size(); i++) {
401  if (Lpol[i] != nullptr) {
402  delete Lpol[i];
403  }
404  Lpol[i] = nullptr;
405  }
406  Lpol.resize(0);
407 
408 #ifdef VISP_HAVE_OGRE
409  if (ogre != nullptr) {
410  delete ogre;
411  ogre = nullptr;
412  }
413 
414  // This is already done by calling "delete ogre"
415  // for(unsigned int i = 0 ; i < lOgrePolygons.size() ; i++){
416  // if (lOgrePolygons[i]!=nullptr){
417  // delete lOgrePolygons[i];
418  // }
419  // lOgrePolygons[i] = nullptr;
420  // }
421 
422  lOgrePolygons.resize(0);
423 
424  ogreInitialised = false;
425  nbRayAttempts = 1;
426  ratioVisibleRay = 1.0;
427  ogre = new vpAROgre();
428  ogreBackground = vpImage<unsigned char>(480, 640);
429 #endif
430 }
431 
439 template <class PolygonType>
441 {
442  for (unsigned int i = 0; i < Lpol.size(); i++) {
443  // For fast result we could just clip visible polygons.
444  // However clipping all of them gives us the possibility to return more
445  // information in the scanline visibility results
446  // if(Lpol[i]->isVisible())
447  {
448  Lpol[i]->changeFrame(cMo);
449  Lpol[i]->computePolygonClipped(cam);
450  }
451  }
452 }
453 
462 template <class PolygonType>
464  const unsigned int &h)
465 {
466  std::vector<std::vector<std::pair<vpPoint, unsigned int> > > polyClipped(Lpol.size());
467  std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> listPolyClipped;
468  std::vector<int> listPolyIndices;
469 
470  for (unsigned int i = 0; i < Lpol.size(); i++) {
471  // For fast result we could just use visible polygons.
472  // However using all of them gives us the possibility to return more
473  // information in the scanline visibility results
474  // if(Lpol[i]->isVisible())
475  {
476  polyClipped[i].clear();
477  Lpol[i]->getPolygonClipped(polyClipped[i]);
478  if (polyClipped[i].size() != 0) {
479  listPolyClipped.push_back(&polyClipped[i]);
480  listPolyIndices.push_back(Lpol[i]->getIndex());
481  }
482  }
483  }
484 
485  scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
486 }
487 
498 template <class PolygonType>
500  std::vector<std::pair<vpPoint, vpPoint> > &lines,
501  const bool &displayResults)
502 {
503  scanlineRender.queryLineVisibility(a, b, lines, displayResults);
504 }
505 
520 template <class PolygonType>
521 unsigned int vpMbHiddenFaces<PolygonType>::setVisiblePrivate(const vpHomogeneousMatrix &cMo, const double &angleAppears,
522  const double &angleDisappears, bool &changed, bool useOgre,
523  bool not_used, unsigned int width, unsigned int height,
524  const vpCameraParameters &cam)
525 {
526  nbVisiblePolygon = 0;
527  changed = false;
528 
529  vpTranslationVector cameraPos;
530 
531  if (useOgre) {
532 #ifdef VISP_HAVE_OGRE
533  cMo.inverse().extract(cameraPos);
534  ogre->renderOneFrame(ogreBackground, cMo);
535 #else
536  vpTRACE("ViSP doesn't have Ogre3D, simple visibility test used");
537 #endif
538  }
539 
540  for (unsigned int i = 0; i < Lpol.size(); i++) {
541  // std::cout << "Calling poly: " << i << std::endl;
542  if (computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, width, height, cam, cameraPos,
543  i))
544  nbVisiblePolygon++;
545  }
546  return nbVisiblePolygon;
547 }
548 
565 template <class PolygonType>
566 bool vpMbHiddenFaces<PolygonType>::computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears,
567  const double &angleDisappears, bool &changed, bool useOgre,
568  bool not_used, unsigned int width, unsigned int height,
569  const vpCameraParameters &cam,
570  const vpTranslationVector &cameraPos, unsigned int index)
571 {
572  (void)not_used;
573  unsigned int i = index;
574  Lpol[i]->changeFrame(cMo);
575  Lpol[i]->isappearing = false;
576 
577  // Commented because we need to compute visibility
578  // even when dealing with line in level of detail case
579  /*if(Lpol[i]->getNbPoint() <= 2)
580  {
581  Lpol[i]->isvisible = true;
582  }
583  else*/
584  {
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 =
593  ((!Lpol[i]->isVisible(cMo, angleDisappears, true, cam, width, height)) || !isVisibleOgre(cameraPos, i));
594 #else
595  {
596  (void)cameraPos; // Avoid warning
597  testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears, false, cam, width, height));
598  }
599 #endif
600  else
601  testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears, false, cam, width, height));
602  }
603 
604  // test if the face is still visible
605  if (testDisappear) {
606  // std::cout << "Face " << i << " disappears" <<
607  // std::endl;
608  changed = true;
609  Lpol[i]->isvisible = false;
610  }
611  else {
612  // nbVisiblePolygon++;
613  Lpol[i]->isvisible = true;
614 
615  // if(nbCornerInsidePrev > Lpol[i]->getNbCornerInsidePrevImage())
616  // changed = true;
617  }
618  }
619  else {
620  bool testAppear = true;
621 
622  if (testAppear) {
623  if (useOgre)
624 #ifdef VISP_HAVE_OGRE
625  testAppear =
626  ((Lpol[i]->isVisible(cMo, angleAppears, true, cam, width, height)) && isVisibleOgre(cameraPos, i));
627 #else
628  testAppear = (Lpol[i]->isVisible(cMo, angleAppears, false, cam, width, height));
629 #endif
630  else
631  testAppear = (Lpol[i]->isVisible(cMo, angleAppears, false, cam, width, height));
632  }
633 
634  if (testAppear) {
635  // std::cout << "Face " << i << " appears" << std::endl;
636  Lpol[i]->isvisible = true;
637  changed = true;
638  // nbVisiblePolygon++;
639  }
640  else {
641  // std::cout << "Problem" << std::endl;
642  Lpol[i]->isvisible = false;
643  }
644  }
645  }
646  // std::cout << "Nombre de polygones visibles: " << nbVisiblePolygon <<
647  // std::endl;
648  return Lpol[i]->isvisible;
649 }
650 
662 template <class PolygonType>
663 unsigned int vpMbHiddenFaces<PolygonType>::setVisible(unsigned int width, unsigned int height,
664  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
665  const double &angle, bool &changed)
666 {
667  return setVisible(width, height, cam, cMo, angle, angle, changed);
668 }
669 
682 template <class PolygonType>
683 unsigned int vpMbHiddenFaces<PolygonType>::setVisible(unsigned int width, unsigned int height,
684  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
685  const double &angleAppears, const double &angleDisappears,
686  bool &changed)
687 {
688  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, false, true, width, height, cam);
689 }
690 
701 template <class PolygonType>
702 unsigned int vpMbHiddenFaces<PolygonType>::setVisible(const vpHomogeneousMatrix &cMo, const double &angleAppears,
703  const double &angleDisappears, bool &changed)
704 {
705  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, false);
706 }
707 
708 #ifdef VISP_HAVE_OGRE
714 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::initOgre(const vpCameraParameters &cam)
715 {
716  ogreInitialised = true;
717  ogre->setCameraParameters(cam);
718  ogre->setShowConfigDialog(ogreShowConfigDialog);
719  ogre->init(ogreBackground, false, true);
720 
721  for (unsigned int n = 0; n < Lpol.size(); n++) {
722  Ogre::ManualObject *manual = ogre->getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
723 
724  manual->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
725  for (unsigned int i = 0; i < Lpol[n]->nbpt; i++) {
726  manual->position((Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(),
727  (Ogre::Real)Lpol[n]->p[i].get_oZ());
728  manual->colour(1.0, 1.0, 1.0);
729  manual->index(i);
730  }
731 
732  manual->index(0);
733  manual->end();
734 
735  ogre->getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
736 
737  lOgrePolygons.push_back(manual);
738  }
739 }
740 
746 template <class PolygonType> void vpMbHiddenFaces<PolygonType>::displayOgre(const vpHomogeneousMatrix &cMo)
747 {
748  if (ogreInitialised && !ogre->isWindowHidden()) {
749  for (unsigned int i = 0; i < Lpol.size(); i++) {
750  if (Lpol[i]->isVisible()) {
751  lOgrePolygons[i]->setVisible(true);
752  }
753  else
754  lOgrePolygons[i]->setVisible(false);
755  }
756  ogre->display(ogreBackground, cMo);
757  }
758 }
759 
772 template <class PolygonType>
773 unsigned int vpMbHiddenFaces<PolygonType>::setVisibleOgre(unsigned int width, unsigned int height,
774  const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo,
775  const double &angleAppears, const double &angleDisappears,
776  bool &changed)
777 {
778  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, true, true, width, height, cam);
779 }
780 
791 template <class PolygonType>
792 unsigned int vpMbHiddenFaces<PolygonType>::setVisibleOgre(const vpHomogeneousMatrix &cMo, const double &angleAppears,
793  const double &angleDisappears, bool &changed)
794 {
795  return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed, true);
796 }
797 
806 template <class PolygonType>
807 bool vpMbHiddenFaces<PolygonType>::isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
808 {
809  Ogre::Vector3 camera((Ogre::Real)cameraPos[0], (Ogre::Real)cameraPos[1], (Ogre::Real)cameraPos[2]);
810  if (!ogre->getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())) {
811  lOgrePolygons[index]->setVisible(false);
812  Lpol[index]->isvisible = false;
813  return false;
814  }
815 
816  // Get the center of gravity
817  bool visible = false;
818  unsigned int nbVisible = 0;
819 
820  for (unsigned int i = 0; i < nbRayAttempts; i++) {
821  Ogre::Vector3 origin(0, 0, 0);
822  Ogre::Real totalFactor = 0.0f;
823 
824  for (unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
825  Ogre::Real factor = 1.0f;
826 
827  if (nbRayAttempts > 1) {
828  int r = rand() % 101;
829 
830  if (r != 0)
831  factor = ((Ogre::Real)r) / 100.0f;
832  }
833 
834  Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(),
835  (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
836  tmp *= factor;
837  origin += tmp;
838  totalFactor += factor;
839  }
840 
841  origin /= totalFactor;
842 
843  Ogre::Vector3 direction = origin - camera;
844  Ogre::Real distanceCollision = direction.length();
845 
846  direction.normalise();
847  Ogre::RaySceneQuery *mRaySceneQuery = ogre->getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
848  mRaySceneQuery->setSortByDistance(true);
849 
850  Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
851  Ogre::RaySceneQueryResult::iterator it = result.begin();
852 
853  // while(it != result.end()){
854  // std::cout << it->movable->getName() << "(" << it->distance<< ") :
855  // " << std::flush; it++;
856  // }
857  // std::cout << std::endl;
858  // it = result.begin();
859 
860  if (it != result.end())
861  if (it->movable->getName().find("SimpleRenderable") != Ogre::String::npos) // Test if the ogreBackground is
862  // intersect in first
863  ++it;
864 
865  double distance;
866  // In a case of a two-axis aligned segment, ray collision is not always
867  // working.
868  if (Lpol[index]->getNbPoint() == 2 &&
869  (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
870  std::numeric_limits<double>::epsilon()) +
871  (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
872  std::numeric_limits<double>::epsilon()) +
873  (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
874  std::numeric_limits<double>::epsilon())) >= 2)) {
875  if (it != result.end()) {
876  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
877  nbVisible++;
878  }
879  else {
880  distance = it->distance;
881  // Cannot use epsilon for comparison as ray length is slightly
882  // different from the collision distance returned by
883  // Ogre::RaySceneQueryResult.
884  if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
885  1e-6 /*std::fabs(distance) * std::numeric_limits<double>::epsilon()*/)
886  nbVisible++;
887  }
888  }
889  else
890  nbVisible++; // Collision not detected but present.
891  }
892  else {
893  if (it != result.end()) {
894  distance = it->distance;
895  double distancePrev = distance;
896 
897  // std::cout << "For " << Ogre::StringConverter::toString(index) << ":
898  // " << it->movable->getName() << " / " << std::flush;
899 
900  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
901  nbVisible++;
902  }
903  else {
904  ++it;
905  while (it != result.end()) {
906  distance = it->distance;
907 
908  if (std::fabs(distance - distancePrev) <
909  1e-6 /*std::fabs(distance) * std::numeric_limits<double>::epsilon()*/) {
910  // std::cout << it->movable->getName() << " / " << std::flush;
911  if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
912  nbVisible++;
913  break;
914  }
915  ++it;
916  distancePrev = distance;
917  }
918  else
919  break;
920  }
921  }
922  }
923  }
924 
925  ogre->getSceneManager()->destroyQuery(mRaySceneQuery);
926  }
927 
928  if (((double)nbVisible) / ((double)nbRayAttempts) > ratioVisibleRay ||
929  std::fabs(((double)nbVisible) / ((double)nbRayAttempts) - ratioVisibleRay) <
930  ratioVisibleRay * std::numeric_limits<double>::epsilon())
931  visible = true;
932  else
933  visible = false;
934 
935  if (visible) {
936  lOgrePolygons[index]->setVisible(true);
937  Lpol[index]->isvisible = true;
938  }
939  else {
940  lOgrePolygons[index]->setVisible(false);
941  Lpol[index]->isvisible = false;
942  }
943 
944  return Lpol[index]->isvisible;
945 }
946 
947 #endif // VISP_HAVE_OGRE
948 END_VISP_NAMESPACE
949 #endif // vpMbHiddenFaces
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:92
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:659
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:254
bool isWindowHidden()
Definition: vpAROgre.h:169
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:159
Ogre::Camera * getCamera()
Definition: vpAROgre.h:139
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:110
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:623
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:589
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:79
Class that consider the case of a translation vector.