42 #ifndef vpMbHiddenFaces_HH 43 #define vpMbHiddenFaces_HH 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> 52 #include <visp3/ar/vpAROgre.h> 74 std::vector<PolygonType *> Lpol;
76 unsigned int nbVisiblePolygon;
77 vpMbScanLine scanlineRender;
82 unsigned int nbRayAttempts;
83 double ratioVisibleRay;
85 std::vector<Ogre::ManualObject *> lOgrePolygons;
86 bool ogreShowConfigDialog;
90 const double &angleDisappears,
bool &changed,
bool useOgre =
false,
112 const bool &displayResults =
false);
116 #ifdef VISP_HAVE_OGRE 127 #ifdef VISP_HAVE_OGRE 138 #ifdef VISP_HAVE_OGRE 169 bool isAppearing(
const unsigned int i) {
return Lpol[i]->isAppearing(); }
171 #ifdef VISP_HAVE_OGRE 187 bool isVisible(
const unsigned int i) {
return Lpol[i]->isVisible(); }
189 #ifdef VISP_HAVE_OGRE 194 inline PolygonType *
operator[](
const unsigned int i) {
return Lpol[i]; }
196 inline const PolygonType *
operator[](
const unsigned int i)
const {
return Lpol[i]; }
200 #ifdef VISP_HAVE_OGRE 238 ratioVisibleRay = ratio;
239 if (ratioVisibleRay > 1.0)
240 ratioVisibleRay = 1.0;
241 if (ratioVisibleRay < 0.0)
242 ratioVisibleRay = 0.0;
265 #ifdef VISP_HAVE_OGRE 277 inline unsigned int size()
const {
return (
unsigned int)Lpol.size(); }
283 template <
class PolygonType>
286 #ifdef VISP_HAVE_OGRE 287 ogreInitialised =
false;
289 ratioVisibleRay = 1.0;
290 ogreShowConfigDialog =
false;
301 for (
unsigned int i = 0; i < Lpol.size(); i++) {
302 if (Lpol[i] != NULL) {
309 #ifdef VISP_HAVE_OGRE 323 lOgrePolygons.resize(0);
330 template <
class PolygonType>
332 : Lpol(), nbVisiblePolygon(copy.nbVisiblePolygon), scanlineRender(copy.scanlineRender)
333 #ifdef VISP_HAVE_OGRE
335 ogreBackground(copy.ogreBackground), ogreInitialised(copy.ogreInitialised), nbRayAttempts(copy.nbRayAttempts),
336 ratioVisibleRay(copy.ratioVisibleRay), ogre(NULL), lOgrePolygons(), ogreShowConfigDialog(copy.ogreShowConfigDialog)
340 for (
unsigned int i = 0; i < copy.Lpol.size(); i++) {
341 PolygonType *poly =
new PolygonType(*copy.Lpol[i]);
342 Lpol.push_back(poly);
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);
365 template <
class PolygonType>
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;
390 for (
unsigned int i = 0; i < p->nbpt; i++)
391 p_new->p[i] = p->p[i];
392 Lpol.push_back(p_new);
400 nbVisiblePolygon = 0;
401 for (
unsigned int i = 0; i < Lpol.size(); i++) {
402 if (Lpol[i] != NULL) {
409 #ifdef VISP_HAVE_OGRE 423 lOgrePolygons.resize(0);
425 ogreInitialised =
false;
427 ratioVisibleRay = 1.0;
440 template <
class PolygonType>
443 for (
unsigned int i = 0; i < Lpol.size(); i++) {
449 Lpol[i]->changeFrame(cMo);
450 Lpol[i]->computePolygonClipped(cam);
463 template <
class PolygonType>
465 const unsigned int &h)
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;
471 for (
unsigned int i = 0; i < Lpol.size(); i++) {
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());
486 scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
500 template <
class PolygonType>
502 std::vector<std::pair<vpPoint, vpPoint> > &lines,
503 const bool &displayResults)
505 scanlineRender.queryLineVisibility(a, b, lines, displayResults);
522 template <
class PolygonType>
524 const double &angleDisappears,
bool &changed,
bool useOgre,
528 nbVisiblePolygon = 0;
534 #ifdef VISP_HAVE_OGRE 538 vpTRACE(
"ViSP doesn't have Ogre3D, simple visibility test used");
542 for (
unsigned int i = 0; i < Lpol.size(); i++) {
544 if (
computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, I, cam, cameraPos, i))
547 return nbVisiblePolygon;
568 template <
class PolygonType>
570 const double &angleDisappears,
bool &changed,
bool useOgre,
576 unsigned int i = index;
577 Lpol[i]->changeFrame(cMo);
578 Lpol[i]->isappearing =
false;
588 bool testDisappear =
false;
591 if (!testDisappear) {
593 #ifdef VISP_HAVE_OGRE 594 testDisappear = ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, I)) || !
isVisibleOgre(cameraPos, i));
598 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
602 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
610 Lpol[i]->isvisible =
false;
613 Lpol[i]->isvisible =
true;
619 bool testAppear =
true;
623 #ifdef VISP_HAVE_OGRE 624 testAppear = ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, I)) &&
isVisibleOgre(cameraPos, i));
626 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
629 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
634 Lpol[i]->isvisible =
true;
639 Lpol[i]->isvisible =
false;
645 return Lpol[i]->isvisible;
659 template <
class PolygonType>
664 return setVisible(I, cam, cMo, angle, angle, changed);
679 template <
class PolygonType>
682 const double &angleDisappears,
bool &changed)
684 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false,
true, I, cam);
698 template <
class PolygonType>
700 const double &angleDisappears,
bool &changed)
702 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false);
705 #ifdef VISP_HAVE_OGRE 713 ogreInitialised =
true;
716 ogre->
init(ogreBackground,
false,
true);
718 for (
unsigned int n = 0; n < Lpol.size(); n++) {
719 Ogre::ManualObject *manual = ogre->
getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
721 manual->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
722 for (
unsigned int i = 0; i < Lpol[n]->nbpt; i++) {
723 manual->position((Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(),
724 (Ogre::Real)Lpol[n]->p[i].get_oZ());
725 manual->colour(1.0, 1.0, 1.0);
732 ogre->
getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
734 lOgrePolygons.push_back(manual);
746 for (
unsigned int i = 0; i < Lpol.size(); i++) {
748 lOgrePolygons[i]->setVisible(
true);
750 lOgrePolygons[i]->setVisible(
false);
752 ogre->
display(ogreBackground, cMo);
768 template <
class PolygonType>
771 const double &angleAppears,
const double &angleDisappears,
774 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true,
true, I, cam);
788 template <
class PolygonType>
790 const double &angleDisappears,
bool &changed)
792 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true);
803 template <
class PolygonType>
806 Ogre::Vector3 camera((Ogre::Real)cameraPos[0], (Ogre::Real)cameraPos[1], (Ogre::Real)cameraPos[2]);
807 if (!ogre->
getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())) {
808 lOgrePolygons[index]->setVisible(
false);
809 Lpol[index]->isvisible =
false;
814 bool visible =
false;
815 unsigned int nbVisible = 0;
817 for (
unsigned int i = 0; i < nbRayAttempts; i++) {
818 Ogre::Vector3 origin(0, 0, 0);
819 Ogre::Real totalFactor = 0.0f;
821 for (
unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
822 Ogre::Real factor = 1.0f;
824 if (nbRayAttempts > 1) {
825 int r = rand() % 101;
828 factor = ((Ogre::Real)r) / 100.0f;
831 Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(),
832 (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
835 totalFactor += factor;
838 origin /= totalFactor;
840 Ogre::Vector3 direction = origin - camera;
841 Ogre::Real distanceCollision = direction.length();
843 direction.normalise();
844 Ogre::RaySceneQuery *mRaySceneQuery = ogre->
getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
845 mRaySceneQuery->setSortByDistance(
true);
847 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
848 Ogre::RaySceneQueryResult::iterator it = result.begin();
857 if (it != result.end())
858 if (it->movable->getName().find(
"SimpleRenderable") !=
866 if (Lpol[index]->getNbPoint() == 2 &&
867 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
868 std::numeric_limits<double>::epsilon()) +
869 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
870 std::numeric_limits<double>::epsilon()) +
871 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
872 std::numeric_limits<double>::epsilon())) >= 2)) {
873 if (it != result.end()) {
874 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
877 distance = it->distance;
881 if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
888 if (it != result.end()) {
889 distance = it->distance;
890 double distancePrev = distance;
895 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
899 while (it != result.end()) {
900 distance = it->distance;
902 if (std::fabs(distance - distancePrev) <
905 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
910 distancePrev = distance;
921 if (((
double)nbVisible) / ((
double)nbRayAttempts) > ratioVisibleRay ||
922 std::fabs(((
double)nbVisible) / ((
double)nbRayAttempts) - ratioVisibleRay) <
923 ratioVisibleRay * std::numeric_limits<double>::epsilon())
929 lOgrePolygons[index]->setVisible(
true);
930 Lpol[index]->isvisible =
true;
932 lOgrePolygons[index]->setVisible(
false);
933 Lpol[index]->isvisible =
false;
936 return Lpol[index]->isvisible;
938 #endif // VISP_HAVE_OGRE 940 #endif // vpMbHiddenFaces vpMbHiddenFaces & operator=(vpMbHiddenFaces other)
const PolygonType * operator[](const unsigned int i) const
operator[] as reader.
bool isAppearing(const unsigned int i)
virtual ~vpMbHiddenFaces()
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 using Ogre3D 3rd party.
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()
friend void swap(vpMbHiddenFaces &first, vpMbHiddenFaces &second)
void setCameraParameters(const vpCameraParameters &cameraP)
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
Generic class defining intrinsic camera parameters.
std::vector< PolygonType * > & getPolygon()
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)