41 #ifndef vpMbHiddenFaces_HH
42 #define vpMbHiddenFaces_HH
44 #include <visp3/core/vpHomogeneousMatrix.h>
45 #include <visp3/core/vpMeterPixelConversion.h>
46 #include <visp3/core/vpPixelMeterConversion.h>
47 #include <visp3/mbt/vpMbtPolygon.h>
48 #include <visp3/mbt/vpMbScanLine.h>
51 #include <visp3/ar/vpAROgre.h>
65 template<
class PolygonType = vpMbtPolygon>
70 std::vector<PolygonType *> Lpol ;
72 unsigned int nbVisiblePolygon;
73 vpMbScanLine scanlineRender;
78 unsigned int nbRayAttempts;
79 double ratioVisibleRay;
81 std::vector< Ogre::ManualObject* > lOgrePolygons;
82 bool ogreShowConfigDialog;
85 unsigned int setVisiblePrivate(
const vpHomogeneousMatrix &cMo,
const double &angleAppears,
const double &angleDisappears,
87 bool useOgre =
false,
bool not_used =
false,
98 const double &angleAppears,
const double &angleDisappears,
99 bool &changed,
bool useOgre,
bool not_used,
110 std::vector<std::pair<vpPoint, vpPoint> > &lines,
const bool &displayResults =
false);
114 #ifdef VISP_HAVE_OGRE
125 #ifdef VISP_HAVE_OGRE
136 #ifdef VISP_HAVE_OGRE
164 bool isAppearing(
const unsigned int i){
return Lpol[i]->isAppearing(); }
167 #ifdef VISP_HAVE_OGRE
183 bool isVisible(
const unsigned int i){
return Lpol[i]->isVisible(); }
185 #ifdef VISP_HAVE_OGRE
190 inline PolygonType*
operator[](
const unsigned int i) {
return Lpol[i];}
192 inline const PolygonType*
operator[](
const unsigned int i)
const {
return Lpol[i];}
196 #ifdef VISP_HAVE_OGRE
225 void setGoodNbRayCastingAttemptsRatio(
const double &ratio) {ratioVisibleRay = ratio;
if(ratioVisibleRay > 1.0) ratioVisibleRay = 1.0;
if(ratioVisibleRay < 0.0) ratioVisibleRay = 0.0;}
237 ogreShowConfigDialog = showConfigDialog;
245 #ifdef VISP_HAVE_OGRE
254 inline unsigned int size()
const {
return (
unsigned int)Lpol.size(); }
260 template<
class PolygonType>
262 : Lpol(), nbVisiblePolygon(0), scanlineRender()
264 #ifdef VISP_HAVE_OGRE
265 ogreInitialised =
false;
267 ratioVisibleRay = 1.0;
268 ogreShowConfigDialog =
false;
277 template<
class PolygonType>
280 for(
unsigned int i = 0 ; i < Lpol.size() ; i++){
288 #ifdef VISP_HAVE_OGRE
302 lOgrePolygons.resize(0);
311 template<
class PolygonType>
315 PolygonType *p_new =
new PolygonType;
316 p_new->index = p->index;
317 p_new->setNbPoint(p->nbpt);
318 p_new->isvisible = p->isvisible;
319 p_new->useLod = p->useLod;
320 p_new->minLineLengthThresh = p->minLineLengthThresh;
321 p_new->minPolygonAreaThresh = p->minPolygonAreaThresh;
322 p_new->setName(p->name);
323 p_new->hasOrientation = p->hasOrientation;
325 for(
unsigned int i = 0; i < p->nbpt; i++)
326 p_new->p[i]= p->p[i];
327 Lpol.push_back(p_new);
333 template<
class PolygonType>
337 nbVisiblePolygon = 0;
338 for(
unsigned int i = 0 ; i < Lpol.size() ; i++){
346 #ifdef VISP_HAVE_OGRE
360 lOgrePolygons.resize(0);
362 ogreInitialised =
false;
364 ratioVisibleRay = 1.0;
376 template<
class PolygonType>
380 for (
unsigned int i = 0; i < Lpol.size(); i++){
385 Lpol[i]->changeFrame(cMo);
386 Lpol[i]->computePolygonClipped(cam);
398 template<
class PolygonType>
401 std::vector<std::vector<std::pair<vpPoint, unsigned int> > > polyClipped(Lpol.size());
402 std::vector<std::vector<std::pair<vpPoint, unsigned int> > * > listPolyClipped;
403 std::vector<int> listPolyIndices;
405 for (
unsigned int i = 0; i < Lpol.size(); i++){
410 polyClipped[i].clear();
411 Lpol[i]->getPolygonClipped(polyClipped[i]);
412 if(polyClipped[i].size() != 0)
414 listPolyClipped.push_back(&polyClipped[i]);
415 listPolyIndices.push_back(Lpol[i]->getIndex());
420 scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
433 template<
class PolygonType>
436 std::vector<std::pair<vpPoint, vpPoint> > &lines,
437 const bool &displayResults)
439 scanlineRender.queryLineVisibility(a,b,lines,displayResults);
456 template<
class PolygonType>
459 const double &angleAppears,
const double &angleDisappears,
460 bool &changed,
bool useOgre,
bool not_used,
464 nbVisiblePolygon = 0;
470 #ifdef VISP_HAVE_OGRE
472 ogre->renderOneFrame(ogreBackground, cMo);
474 vpTRACE(
"ViSP doesn't have Ogre3D, simple visibility test used");
478 for (
unsigned int i = 0; i < Lpol.size(); i++){
480 if (computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, I, cam, cameraPos, i))
483 return nbVisiblePolygon;
502 template<
class PolygonType>
505 const double &angleAppears,
const double &angleDisappears,
506 bool &changed,
bool useOgre,
bool not_used,
514 unsigned int i = index;
515 Lpol[i]->changeFrame(cMo);
516 Lpol[i]->isappearing =
false;
525 if(Lpol[i]->isVisible())
527 bool testDisappear =
false;
528 unsigned int nbCornerInsidePrev = 0;
532 #ifdef VISP_HAVE_OGRE
533 testDisappear = ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, I)) || !isVisibleOgre(cameraPos,i));
537 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
541 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
548 Lpol[i]->isvisible =
false;
552 Lpol[i]->isvisible =
true;
554 if(nbCornerInsidePrev > Lpol[i]->getNbCornerInsidePrevImage())
560 bool testAppear =
true;
564 #ifdef VISP_HAVE_OGRE
565 testAppear = ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, I)) && isVisibleOgre(cameraPos,i));
567 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
570 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
575 Lpol[i]->isvisible =
true;
581 Lpol[i]->isvisible =
false;
586 return Lpol[i]->isvisible;
600 template<
class PolygonType>
604 return setVisible(I, cam, cMo, angle, angle, changed);
619 template<
class PolygonType>
623 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
false,
true,I,cam);
636 template<
class PolygonType>
640 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
false);
643 #ifdef VISP_HAVE_OGRE
649 template<
class PolygonType>
653 ogreInitialised =
true;
654 ogre->setCameraParameters(cam);
655 ogre->setShowConfigDialog(ogreShowConfigDialog);
656 ogre->init(ogreBackground,
false,
true);
658 for(
unsigned int n = 0 ; n < Lpol.size(); n++){
659 Ogre::ManualObject* manual = ogre->getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
661 manual->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
662 for(
unsigned int i = 0; i < Lpol[n]->nbpt; i++){
663 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());
664 manual->colour(1.0, 1.0, 1.0);
671 ogre->getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
673 lOgrePolygons.push_back(manual);
682 template<
class PolygonType>
686 if(ogreInitialised && !ogre->isWindowHidden()){
687 for(
unsigned int i = 0 ; i < Lpol.size() ; i++){
688 if(Lpol[i]->isVisible()){
689 lOgrePolygons[i]->setVisible(
true);
692 lOgrePolygons[i]->setVisible(
false);
694 ogre->display(ogreBackground, cMo);
710 template<
class PolygonType>
714 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
true,
true,I,cam);
727 template<
class PolygonType>
731 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
true);
742 template<
class PolygonType>
746 Ogre::Vector3 camera((Ogre::Real)cameraPos[0],(Ogre::Real)cameraPos[1],(Ogre::Real)cameraPos[2]);
747 if(!ogre->getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())){
748 lOgrePolygons[index]->setVisible(
false);
749 Lpol[index]->isvisible =
false;
754 bool visible =
false;
755 unsigned int nbVisible = 0;
757 for(
unsigned int i = 0; i < nbRayAttempts; i++)
759 Ogre::Vector3 origin(0,0,0);
760 Ogre::Real totalFactor = 0.0f;
762 for(
unsigned int j = 0 ; j < Lpol[index]->getNbPoint() ; j++)
764 Ogre::Real factor = 1.0f;
766 if(nbRayAttempts > 1){
767 int r = rand() % 101;
770 factor = ((Ogre::Real)r)/100.0f;
773 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());
776 totalFactor += factor;
779 origin /= totalFactor;
781 Ogre::Vector3 direction = origin - camera;
782 Ogre::Real distanceCollision = direction.length();
784 direction.normalise();
785 Ogre::RaySceneQuery *mRaySceneQuery = ogre->getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
786 mRaySceneQuery->setSortByDistance(
true);
788 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
789 Ogre::RaySceneQueryResult::iterator it = result.begin();
798 if(it != result.end())
799 if(it->movable->getName().find(
"SimpleRenderable") != Ogre::String::npos)
802 double distance, distancePrev;
804 if(Lpol[index]->getNbPoint() == 2 &&
805 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) < std::numeric_limits<double>::epsilon()) +
806 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) < std::numeric_limits<double>::epsilon()) +
807 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) < std::numeric_limits<double>::epsilon())) >= 2 ))
809 if(it != result.end())
811 if(it->movable->getName() == Ogre::StringConverter::toString(index)){
816 distance = it->distance;
818 if(distance > distanceCollision || std::fabs(distance - distanceCollision) < 1e-6 )
827 if(it != result.end())
829 distance = it->distance;
830 distancePrev = distance;
834 if(it->movable->getName() == Ogre::StringConverter::toString(index)){
840 while(it != result.end())
842 distance = it->distance;
844 if(std::fabs(distance - distancePrev) < 1e-6 ){
846 if(it->movable->getName() == Ogre::StringConverter::toString(index)){
851 distancePrev = distance;
860 ogre->getSceneManager()->destroyQuery(mRaySceneQuery);
864 if(((
double)nbVisible)/((
double)nbRayAttempts) > ratioVisibleRay ||
865 std::fabs(((
double)nbVisible)/((
double)nbRayAttempts) - ratioVisibleRay) < ratioVisibleRay * std::numeric_limits<double>::epsilon())
871 lOgrePolygons[index]->setVisible(
true);
872 Lpol[index]->isvisible =
true;
875 lOgrePolygons[index]->setVisible(
false);
876 Lpol[index]->isvisible =
false;
879 return Lpol[index]->isvisible;
881 #endif //VISP_HAVE_OGRE
883 #endif // vpMbHiddenFaces
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.
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned int getNbVisiblePolygon() const
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()
Implementation of an augmented reality viewer using Ogre3D 3rd party.
unsigned int setVisibleOgre(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
Class that defines what is a point.
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()
double getGoodNbRayCastingAttemptsRatio()
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
Generic class defining intrinsic camera parameters.
void extract(vpRotationMatrix &R) const
void addPolygon(PolygonType *p)
unsigned int size() const
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
vpHomogeneousMatrix inverse() const
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)
std::vector< PolygonType * > & getPolygon()