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;
566 template <
class PolygonType>
568 const double &angleDisappears,
bool &changed,
bool useOgre,
574 unsigned int i = index;
575 Lpol[i]->changeFrame(cMo);
576 Lpol[i]->isappearing =
false;
586 bool testDisappear =
false;
589 if (!testDisappear) {
591 #ifdef VISP_HAVE_OGRE 592 testDisappear = ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, I)) || !
isVisibleOgre(cameraPos, i));
596 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
600 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, I));
608 Lpol[i]->isvisible =
false;
611 Lpol[i]->isvisible =
true;
617 bool testAppear =
true;
621 #ifdef VISP_HAVE_OGRE 622 testAppear = ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, I)) &&
isVisibleOgre(cameraPos, i));
624 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
627 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, I));
632 Lpol[i]->isvisible =
true;
637 Lpol[i]->isvisible =
false;
643 return Lpol[i]->isvisible;
657 template <
class PolygonType>
662 return setVisible(I, cam, cMo, angle, angle, changed);
677 template <
class PolygonType>
680 const double &angleDisappears,
bool &changed)
682 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false,
true, I, cam);
696 template <
class PolygonType>
698 const double &angleDisappears,
bool &changed)
700 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false);
703 #ifdef VISP_HAVE_OGRE 711 ogreInitialised =
true;
714 ogre->
init(ogreBackground,
false,
true);
716 for (
unsigned int n = 0; n < Lpol.size(); n++) {
717 Ogre::ManualObject *manual = ogre->
getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
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);
730 ogre->
getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
732 lOgrePolygons.push_back(manual);
744 for (
unsigned int i = 0; i < Lpol.size(); i++) {
746 lOgrePolygons[i]->setVisible(
true);
748 lOgrePolygons[i]->setVisible(
false);
750 ogre->
display(ogreBackground, cMo);
766 template <
class PolygonType>
769 const double &angleAppears,
const double &angleDisappears,
772 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true,
true, I, cam);
786 template <
class PolygonType>
788 const double &angleDisappears,
bool &changed)
790 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true);
801 template <
class PolygonType>
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;
812 bool visible =
false;
813 unsigned int nbVisible = 0;
815 for (
unsigned int i = 0; i < nbRayAttempts; i++) {
816 Ogre::Vector3 origin(0, 0, 0);
817 Ogre::Real totalFactor = 0.0f;
819 for (
unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
820 Ogre::Real factor = 1.0f;
822 if (nbRayAttempts > 1) {
823 int r = rand() % 101;
826 factor = ((Ogre::Real)r) / 100.0f;
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());
833 totalFactor += factor;
836 origin /= totalFactor;
838 Ogre::Vector3 direction = origin - camera;
839 Ogre::Real distanceCollision = direction.length();
841 direction.normalise();
842 Ogre::RaySceneQuery *mRaySceneQuery = ogre->
getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
843 mRaySceneQuery->setSortByDistance(
true);
845 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
846 Ogre::RaySceneQueryResult::iterator it = result.begin();
855 if (it != result.end())
856 if (it->movable->getName().find(
"SimpleRenderable") !=
864 if (Lpol[index]->getNbPoint() == 2 &&
865 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
866 std::numeric_limits<double>::epsilon()) +
867 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
868 std::numeric_limits<double>::epsilon()) +
869 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
870 std::numeric_limits<double>::epsilon())) >= 2)) {
871 if (it != result.end()) {
872 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
875 distance = it->distance;
879 if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
886 if (it != result.end()) {
887 distance = it->distance;
888 double distancePrev = distance;
893 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
897 while (it != result.end()) {
898 distance = it->distance;
900 if (std::fabs(distance - distancePrev) <
903 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
908 distancePrev = distance;
919 if (((
double)nbVisible) / ((
double)nbRayAttempts) > ratioVisibleRay ||
920 std::fabs(((
double)nbVisible) / ((
double)nbRayAttempts) - ratioVisibleRay) <
921 ratioVisibleRay * std::numeric_limits<double>::epsilon())
927 lOgrePolygons[index]->setVisible(
true);
928 Lpol[index]->isvisible =
true;
930 lOgrePolygons[index]->setVisible(
false);
931 Lpol[index]->isvisible =
false;
934 return Lpol[index]->isvisible;
936 #endif // VISP_HAVE_OGRE 938 #endif // vpMbHiddenFaces vpMbHiddenFaces & operator=(vpMbHiddenFaces other)
unsigned int size() const
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.
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()
vpHomogeneousMatrix inverse() const
const PolygonType * operator[](const unsigned int i) const
operator[] as reader.
Implementation of an augmented reality viewer using Ogre3D 3rd party.
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
void extract(vpRotationMatrix &R) const
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()
unsigned int getNbVisiblePolygon() const
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 addPolygon(PolygonType *p)
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
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)