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,
91 bool not_used =
false,
unsigned int width = 0,
unsigned int height = 0,
104 bool &changed,
bool useOgre,
bool not_used,
unsigned int width,
unsigned int height,
112 const bool &displayResults =
false);
116 #ifdef VISP_HAVE_OGRE
127 #ifdef VISP_HAVE_OGRE
138 #ifdef VISP_HAVE_OGRE
169 bool isAppearing(
unsigned int i) {
return Lpol[i]->isAppearing(); }
171 #ifdef VISP_HAVE_OGRE
187 bool isVisible(
unsigned int i) {
return Lpol[i]->isVisible(); }
189 #ifdef VISP_HAVE_OGRE
194 inline PolygonType *
operator[](
unsigned int i) {
return Lpol[i]; }
196 inline const PolygonType *
operator[](
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,
525 bool not_used,
unsigned int width,
unsigned int height,
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, width, height, cam, cameraPos,
548 return nbVisiblePolygon;
569 template <
class PolygonType>
571 const double &angleDisappears,
bool &changed,
bool useOgre,
572 bool not_used,
unsigned int width,
unsigned int height,
577 unsigned int i = index;
578 Lpol[i]->changeFrame(cMo);
579 Lpol[i]->isappearing =
false;
589 if (Lpol[i]->isVisible()) {
590 bool testDisappear =
false;
593 if (!testDisappear) {
595 #ifdef VISP_HAVE_OGRE
597 ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, width, height)) || !isVisibleOgre(cameraPos, i));
601 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
605 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
613 Lpol[i]->isvisible =
false;
616 Lpol[i]->isvisible =
true;
622 bool testAppear =
true;
626 #ifdef VISP_HAVE_OGRE
628 ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, width, height)) && isVisibleOgre(cameraPos, i));
630 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
633 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
638 Lpol[i]->isvisible =
true;
643 Lpol[i]->isvisible =
false;
649 return Lpol[i]->isvisible;
665 template <
class PolygonType>
668 const double &angle,
bool &changed)
670 return setVisible(width, height, cam, cMo, angle, angle, changed);
687 template <
class PolygonType>
690 const double &angleAppears,
const double &angleDisappears,
693 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false,
true, width, height, cam);
707 template <
class PolygonType>
709 const double &angleDisappears,
bool &changed)
711 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false);
714 #ifdef VISP_HAVE_OGRE
722 ogreInitialised =
true;
725 ogre->
init(ogreBackground,
false,
true);
727 for (
unsigned int n = 0; n < Lpol.size(); n++) {
728 Ogre::ManualObject *manual = ogre->
getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
730 manual->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
731 for (
unsigned int i = 0; i < Lpol[n]->nbpt; i++) {
732 manual->position((Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(),
733 (Ogre::Real)Lpol[n]->p[i].get_oZ());
734 manual->colour(1.0, 1.0, 1.0);
741 ogre->
getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
743 lOgrePolygons.push_back(manual);
755 for (
unsigned int i = 0; i < Lpol.size(); i++) {
756 if (Lpol[i]->isVisible()) {
757 lOgrePolygons[i]->setVisible(
true);
759 lOgrePolygons[i]->setVisible(
false);
761 ogre->
display(ogreBackground, cMo);
779 template <
class PolygonType>
782 const double &angleAppears,
const double &angleDisappears,
785 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true,
true, width, height, cam);
799 template <
class PolygonType>
801 const double &angleDisappears,
bool &changed)
803 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true);
814 template <
class PolygonType>
817 Ogre::Vector3 camera((Ogre::Real)cameraPos[0], (Ogre::Real)cameraPos[1], (Ogre::Real)cameraPos[2]);
818 if (!ogre->
getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())) {
819 lOgrePolygons[index]->setVisible(
false);
820 Lpol[index]->isvisible =
false;
825 bool visible =
false;
826 unsigned int nbVisible = 0;
828 for (
unsigned int i = 0; i < nbRayAttempts; i++) {
829 Ogre::Vector3 origin(0, 0, 0);
830 Ogre::Real totalFactor = 0.0f;
832 for (
unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
833 Ogre::Real factor = 1.0f;
835 if (nbRayAttempts > 1) {
836 int r = rand() % 101;
839 factor = ((Ogre::Real)r) / 100.0f;
842 Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(),
843 (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
846 totalFactor += factor;
849 origin /= totalFactor;
851 Ogre::Vector3 direction = origin - camera;
852 Ogre::Real distanceCollision = direction.length();
854 direction.normalise();
855 Ogre::RaySceneQuery *mRaySceneQuery = ogre->
getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
856 mRaySceneQuery->setSortByDistance(
true);
858 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
859 Ogre::RaySceneQueryResult::iterator it = result.begin();
868 if (it != result.end())
869 if (it->movable->getName().find(
"SimpleRenderable") != Ogre::String::npos)
876 if (Lpol[index]->getNbPoint() == 2 &&
877 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
878 std::numeric_limits<double>::epsilon()) +
879 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
880 std::numeric_limits<double>::epsilon()) +
881 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
882 std::numeric_limits<double>::epsilon())) >= 2)) {
883 if (it != result.end()) {
884 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
887 distance = it->distance;
891 if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
898 if (it != result.end()) {
899 distance = it->distance;
900 double distancePrev = distance;
905 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
909 while (it != result.end()) {
910 distance = it->distance;
912 if (std::fabs(distance - distancePrev) <
915 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
920 distancePrev = distance;
931 if (((
double)nbVisible) / ((
double)nbRayAttempts) > ratioVisibleRay ||
932 std::fabs(((
double)nbVisible) / ((
double)nbRayAttempts) - ratioVisibleRay) <
933 ratioVisibleRay * std::numeric_limits<double>::epsilon())
939 lOgrePolygons[index]->setVisible(
true);
940 Lpol[index]->isvisible =
true;
942 lOgrePolygons[index]->setVisible(
false);
943 Lpol[index]->isvisible =
false;
946 return Lpol[index]->isvisible;
Implementation of an augmented reality viewer using Ogre3D 3rd party.
void setCameraParameters(const vpCameraParameters &cameraP)
void setShowConfigDialog(bool showConfigDialog)
Ogre::SceneManager * getSceneManager()
Ogre::Camera * getCamera()
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
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 ©)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Class that consider the case of a translation vector.