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 testRoi =
false,
98 const double &angleAppears,
const double &angleDisappears,
99 bool &changed,
bool useOgre,
bool testRoi,
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 testRoi,
464 nbVisiblePolygon = 0;
470 #ifdef VISP_HAVE_OGRE
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, testRoi, I, cam, cameraPos, i))
483 return nbVisiblePolygon;
502 template<
class PolygonType>
505 const double &angleAppears,
const double &angleDisappears,
506 bool &changed,
bool useOgre,
bool ,
510 #ifdef VISP_HAVE_OGRE
516 unsigned int i = index;
517 Lpol[i]->changeFrame(cMo);
518 Lpol[i]->isappearing =
false;
527 if(Lpol[i]->isVisible())
529 bool testDisappear =
false;
530 unsigned int nbCornerInsidePrev = 0;
540 #ifdef VISP_HAVE_OGRE
541 testDisappear = ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, I)) || !isVisibleOgre(cameraPos,i));
543 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
546 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
553 Lpol[i]->isvisible =
false;
557 Lpol[i]->isvisible =
true;
559 if(nbCornerInsidePrev > Lpol[i]->getNbCornerInsidePrevImage())
565 bool testAppear =
true;
572 #ifdef VISP_HAVE_OGRE
573 testAppear = ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, I)) && isVisibleOgre(cameraPos,i));
575 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
578 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
583 Lpol[i]->isvisible =
true;
589 Lpol[i]->isvisible =
false;
594 return Lpol[i]->isvisible;
608 template<
class PolygonType>
612 return setVisible(I, cam, cMo, angle, angle, changed);
627 template<
class PolygonType>
631 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
false,
true,I,cam);
644 template<
class PolygonType>
648 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
false);
651 #ifdef VISP_HAVE_OGRE
657 template<
class PolygonType>
661 ogreInitialised =
true;
664 ogre->
init(ogreBackground,
false,
true);
666 for(
unsigned int n = 0 ; n < Lpol.size(); n++){
667 Ogre::ManualObject* manual = ogre->
getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
669 manual->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
670 for(
unsigned int i = 0; i < Lpol[n]->nbpt; i++){
671 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());
672 manual->colour(1.0, 1.0, 1.0);
679 ogre->
getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
681 lOgrePolygons.push_back(manual);
690 template<
class PolygonType>
695 for(
unsigned int i = 0 ; i < Lpol.size() ; i++){
696 if(Lpol[i]->isVisible()){
697 lOgrePolygons[i]->setVisible(
true);
700 lOgrePolygons[i]->setVisible(
false);
702 ogre->
display(ogreBackground, cMo);
718 template<
class PolygonType>
722 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
true,
true,I,cam);
735 template<
class PolygonType>
739 return setVisiblePrivate(cMo,angleAppears,angleDisappears,changed,
true);
750 template<
class PolygonType>
754 Ogre::Vector3 camera((Ogre::Real)cameraPos[0],(Ogre::Real)cameraPos[1],(Ogre::Real)cameraPos[2]);
755 if(!ogre->
getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())){
756 lOgrePolygons[index]->setVisible(
false);
757 Lpol[index]->isvisible =
false;
762 bool visible =
false;
763 unsigned int nbVisible = 0;
765 for(
unsigned int i = 0; i < nbRayAttempts; i++)
767 Ogre::Vector3 origin(0,0,0);
768 Ogre::Real totalFactor = 0.0f;
770 for(
unsigned int j = 0 ; j < Lpol[index]->getNbPoint() ; j++)
772 Ogre::Real factor = 1.0f;
774 if(nbRayAttempts > 1){
775 int r = rand() % 101;
778 factor = ((Ogre::Real)r)/100.0f;
781 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());
784 totalFactor += factor;
787 origin /= totalFactor;
789 Ogre::Vector3 direction = origin - camera;
790 Ogre::Real distanceCollision = direction.length();
792 direction.normalise();
793 Ogre::RaySceneQuery *mRaySceneQuery = ogre->
getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
794 mRaySceneQuery->setSortByDistance(
true);
796 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
797 Ogre::RaySceneQueryResult::iterator it = result.begin();
806 if(it != result.end())
807 if(it->movable->getName().find(
"SimpleRenderable") != Ogre::String::npos)
810 double distance, distancePrev;
812 if(Lpol[index]->getNbPoint() == 2 &&
813 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) < std::numeric_limits<double>::epsilon()) +
814 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) < std::numeric_limits<double>::epsilon()) +
815 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) < std::numeric_limits<double>::epsilon())) >= 2 ))
817 if(it != result.end())
819 if(it->movable->getName() == Ogre::StringConverter::toString(index)){
824 distance = it->distance;
826 if(distance > distanceCollision || std::fabs(distance - distanceCollision) < 1e-6 )
835 if(it != result.end())
837 distance = it->distance;
838 distancePrev = distance;
842 if(it->movable->getName() == Ogre::StringConverter::toString(index)){
848 while(it != result.end())
850 distance = it->distance;
852 if(std::fabs(distance - distancePrev) < 1e-6 ){
854 if(it->movable->getName() == Ogre::StringConverter::toString(index)){
859 distancePrev = distance;
872 if(((
double)nbVisible)/((
double)nbRayAttempts) > ratioVisibleRay ||
873 std::fabs(((
double)nbVisible)/((
double)nbRayAttempts) - ratioVisibleRay) < ratioVisibleRay * std::numeric_limits<double>::epsilon())
879 lOgrePolygons[index]->setVisible(
true);
880 Lpol[index]->isvisible =
true;
883 lOgrePolygons[index]->setVisible(
false);
884 Lpol[index]->isvisible =
false;
887 return Lpol[index]->isvisible;
889 #endif //VISP_HAVE_OGRE
891 #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.
Ogre::Camera * getCamera()
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned int getNbVisiblePolygon() const
void setShowConfigDialog(const bool showConfigDialog)
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.
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
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()
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
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()
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
double getGoodNbRayCastingAttemptsRatio()
void setCameraParameters(const vpCameraParameters &cameraP)
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
Generic class defining intrinsic camera parameters.
bool computeVisibility(const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed, bool useOgre, bool testRoi, const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpTranslationVector &cameraPos, unsigned int index)
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.
std::vector< PolygonType * > & getPolygon()