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, i))
547 return nbVisiblePolygon;
568 template <
class PolygonType>
570 const double &angleDisappears,
bool &changed,
bool useOgre,
571 bool not_used,
unsigned int width,
unsigned int height,
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, width, height)) || !
isVisibleOgre(cameraPos, i));
598 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
602 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
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, width, height)) &&
isVisibleOgre(cameraPos, i));
626 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
629 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
634 Lpol[i]->isvisible =
true;
639 Lpol[i]->isvisible =
false;
645 return Lpol[i]->isvisible;
661 template <
class PolygonType>
666 return setVisible(width, height, cam, cMo, angle, angle, changed);
683 template <
class PolygonType>
686 const double &angleDisappears,
bool &changed)
688 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false,
true, width, height, cam);
702 template <
class PolygonType>
704 const double &angleDisappears,
bool &changed)
706 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false);
709 #ifdef VISP_HAVE_OGRE 717 ogreInitialised =
true;
720 ogre->
init(ogreBackground,
false,
true);
722 for (
unsigned int n = 0; n < Lpol.size(); n++) {
723 Ogre::ManualObject *manual = ogre->
getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
725 manual->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
726 for (
unsigned int i = 0; i < Lpol[n]->nbpt; i++) {
727 manual->position((Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(),
728 (Ogre::Real)Lpol[n]->p[i].get_oZ());
729 manual->colour(1.0, 1.0, 1.0);
736 ogre->
getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
738 lOgrePolygons.push_back(manual);
750 for (
unsigned int i = 0; i < Lpol.size(); i++) {
752 lOgrePolygons[i]->setVisible(
true);
754 lOgrePolygons[i]->setVisible(
false);
756 ogre->
display(ogreBackground, cMo);
774 template <
class PolygonType>
777 const double &angleAppears,
const double &angleDisappears,
780 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true,
true, width, height, cam);
794 template <
class PolygonType>
796 const double &angleDisappears,
bool &changed)
798 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true);
809 template <
class PolygonType>
812 Ogre::Vector3 camera((Ogre::Real)cameraPos[0], (Ogre::Real)cameraPos[1], (Ogre::Real)cameraPos[2]);
813 if (!ogre->
getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())) {
814 lOgrePolygons[index]->setVisible(
false);
815 Lpol[index]->isvisible =
false;
820 bool visible =
false;
821 unsigned int nbVisible = 0;
823 for (
unsigned int i = 0; i < nbRayAttempts; i++) {
824 Ogre::Vector3 origin(0, 0, 0);
825 Ogre::Real totalFactor = 0.0f;
827 for (
unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
828 Ogre::Real factor = 1.0f;
830 if (nbRayAttempts > 1) {
831 int r = rand() % 101;
834 factor = ((Ogre::Real)r) / 100.0f;
837 Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(),
838 (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
841 totalFactor += factor;
844 origin /= totalFactor;
846 Ogre::Vector3 direction = origin - camera;
847 Ogre::Real distanceCollision = direction.length();
849 direction.normalise();
850 Ogre::RaySceneQuery *mRaySceneQuery = ogre->
getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
851 mRaySceneQuery->setSortByDistance(
true);
853 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
854 Ogre::RaySceneQueryResult::iterator it = result.begin();
863 if (it != result.end())
864 if (it->movable->getName().find(
"SimpleRenderable") !=
872 if (Lpol[index]->getNbPoint() == 2 &&
873 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
874 std::numeric_limits<double>::epsilon()) +
875 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
876 std::numeric_limits<double>::epsilon()) +
877 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
878 std::numeric_limits<double>::epsilon())) >= 2)) {
879 if (it != result.end()) {
880 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
883 distance = it->distance;
887 if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
894 if (it != result.end()) {
895 distance = it->distance;
896 double distancePrev = distance;
901 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
905 while (it != result.end()) {
906 distance = it->distance;
908 if (std::fabs(distance - distancePrev) <
911 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
916 distancePrev = distance;
927 if (((
double)nbVisible) / ((
double)nbRayAttempts) > ratioVisibleRay ||
928 std::fabs(((
double)nbVisible) / ((
double)nbRayAttempts) - ratioVisibleRay) <
929 ratioVisibleRay * std::numeric_limits<double>::epsilon())
935 lOgrePolygons[index]->setVisible(
true);
936 Lpol[index]->isvisible =
true;
938 lOgrePolygons[index]->setVisible(
false);
939 Lpol[index]->isvisible =
false;
942 return Lpol[index]->isvisible;
944 #endif // VISP_HAVE_OGRE 946 #endif // vpMbHiddenFaces vpMbHiddenFaces & operator=(vpMbHiddenFaces other)
bool isVisible(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 setBackgroundSizeOgre(const unsigned int &h, const unsigned int &w)
vpMbScanLine & getMbScanLineRenderer()
Implementation of an augmented reality viewer using Ogre3D 3rd party.
bool renderOneFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Ogre::SceneManager * getSceneManager()
unsigned int setVisibleOgre(unsigned int width, unsigned int height, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angleAppears, const double &angleDisappears, bool &changed)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
void computeClippedPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
void initOgre(const vpCameraParameters &cam=vpCameraParameters())
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)
bool isVisibleOgre(const vpTranslationVector &cameraPos, const unsigned int &index)
vpAROgre * getOgreContext()
PolygonType * operator[](unsigned int i)
operator[] as modifier.
bool isAppearing(unsigned int i)
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
double getGoodNbRayCastingAttemptsRatio()
friend void swap(vpMbHiddenFaces &first, vpMbHiddenFaces &second)
const PolygonType * operator[](unsigned int i) const
operator[] as reader.
void setCameraParameters(const vpCameraParameters &cameraP)
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
Generic class defining intrinsic camera parameters.
void setOgreShowConfigDialog(bool showConfigDialog)
std::vector< PolygonType * > & getPolygon()
void extract(vpRotationMatrix &R) const
void setShowConfigDialog(bool showConfigDialog)
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
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.
unsigned int setVisible(unsigned int width, unsigned int height, const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo, const double &angle, bool &changed)