35 #ifndef VP_MB_HIDDEN_FACES_H
36 #define VP_MB_HIDDEN_FACES_H
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpDebug.h>
40 #include <visp3/core/vpHomogeneousMatrix.h>
41 #include <visp3/core/vpMeterPixelConversion.h>
42 #include <visp3/core/vpPixelMeterConversion.h>
43 #include <visp3/mbt/vpMbScanLine.h>
44 #include <visp3/mbt/vpMbtPolygon.h>
47 #include <visp3/ar/vpAROgre.h>
73 std::vector<PolygonType *> Lpol;
75 unsigned int nbVisiblePolygon;
76 vpMbScanLine scanlineRender;
81 unsigned int nbRayAttempts;
82 double ratioVisibleRay;
84 std::vector<Ogre::ManualObject *> lOgrePolygons;
85 bool ogreShowConfigDialog;
89 const double &angleDisappears,
bool &changed,
bool useOgre =
false,
90 bool not_used =
false,
unsigned int width = 0,
unsigned int height = 0,
103 bool &changed,
bool useOgre,
bool not_used,
unsigned int width,
unsigned int height,
111 const bool &displayResults =
false);
115 #ifdef VISP_HAVE_OGRE
126 #ifdef VISP_HAVE_OGRE
137 #ifdef VISP_HAVE_OGRE
168 bool isAppearing(
unsigned int i) {
return Lpol[i]->isAppearing(); }
170 #ifdef VISP_HAVE_OGRE
186 bool isVisible(
unsigned int i) {
return Lpol[i]->isVisible(); }
188 #ifdef VISP_HAVE_OGRE
193 inline PolygonType *
operator[](
unsigned int i) {
return Lpol[i]; }
195 inline const PolygonType *
operator[](
unsigned int i)
const {
return Lpol[i]; }
199 #ifdef VISP_HAVE_OGRE
237 ratioVisibleRay = ratio;
238 if (ratioVisibleRay > 1.0)
239 ratioVisibleRay = 1.0;
240 if (ratioVisibleRay < 0.0)
241 ratioVisibleRay = 0.0;
264 #ifdef VISP_HAVE_OGRE
276 inline unsigned int size()
const {
return (
unsigned int)Lpol.size(); }
282 template <
class PolygonType>
285 #ifdef VISP_HAVE_OGRE
286 ogreInitialised =
false;
288 ratioVisibleRay = 1.0;
289 ogreShowConfigDialog =
false;
300 for (
unsigned int i = 0; i < Lpol.size(); i++) {
301 if (Lpol[i] !=
nullptr) {
308 #ifdef VISP_HAVE_OGRE
309 if (ogre !=
nullptr) {
322 lOgrePolygons.resize(0);
329 template <
class PolygonType>
331 : Lpol(), nbVisiblePolygon(copy.nbVisiblePolygon), scanlineRender(copy.scanlineRender)
332 #ifdef VISP_HAVE_OGRE
334 ogreBackground(copy.ogreBackground), ogreInitialised(copy.ogreInitialised), nbRayAttempts(copy.nbRayAttempts),
335 ratioVisibleRay(copy.ratioVisibleRay), ogre(nullptr), lOgrePolygons(), ogreShowConfigDialog(copy.ogreShowConfigDialog)
339 for (
unsigned int i = 0; i < copy.Lpol.size(); i++) {
340 PolygonType *poly =
new PolygonType(*copy.Lpol[i]);
341 Lpol.push_back(poly);
348 swap(first.Lpol, second.Lpol);
349 swap(first.nbVisiblePolygon, second.nbVisiblePolygon);
350 swap(first.scanlineRender, second.scanlineRender);
351 #ifdef VISP_HAVE_OGRE
352 swap(first.ogreInitialised, second.ogreInitialised);
353 swap(first.nbRayAttempts, second.nbRayAttempts);
354 swap(first.ratioVisibleRay, second.ratioVisibleRay);
355 swap(first.ogreShowConfigDialog, second.ogreShowConfigDialog);
356 swap(first.ogre, second.ogre);
357 swap(first.ogreBackground, second.ogreBackground);
364 template <
class PolygonType>
379 PolygonType *p_new =
new PolygonType;
380 p_new->index = p->index;
381 p_new->setNbPoint(p->nbpt);
382 p_new->isvisible = p->isvisible;
383 p_new->useLod = p->useLod;
384 p_new->minLineLengthThresh = p->minLineLengthThresh;
385 p_new->minPolygonAreaThresh = p->minPolygonAreaThresh;
386 p_new->setName(p->name);
387 p_new->hasOrientation = p->hasOrientation;
389 for (
unsigned int i = 0; i < p->nbpt; i++)
390 p_new->p[i] = p->p[i];
391 Lpol.push_back(p_new);
399 nbVisiblePolygon = 0;
400 for (
unsigned int i = 0; i < Lpol.size(); i++) {
401 if (Lpol[i] !=
nullptr) {
408 #ifdef VISP_HAVE_OGRE
409 if (ogre !=
nullptr) {
422 lOgrePolygons.resize(0);
424 ogreInitialised =
false;
426 ratioVisibleRay = 1.0;
439 template <
class PolygonType>
442 for (
unsigned int i = 0; i < Lpol.size(); i++) {
448 Lpol[i]->changeFrame(cMo);
449 Lpol[i]->computePolygonClipped(cam);
462 template <
class PolygonType>
464 const unsigned int &h)
466 std::vector<std::vector<std::pair<vpPoint, unsigned int> > > polyClipped(Lpol.size());
467 std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> listPolyClipped;
468 std::vector<int> listPolyIndices;
470 for (
unsigned int i = 0; i < Lpol.size(); i++) {
476 polyClipped[i].clear();
477 Lpol[i]->getPolygonClipped(polyClipped[i]);
478 if (polyClipped[i].size() != 0) {
479 listPolyClipped.push_back(&polyClipped[i]);
480 listPolyIndices.push_back(Lpol[i]->getIndex());
485 scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
498 template <
class PolygonType>
500 std::vector<std::pair<vpPoint, vpPoint> > &lines,
501 const bool &displayResults)
503 scanlineRender.queryLineVisibility(a, b, lines, displayResults);
520 template <
class PolygonType>
522 const double &angleDisappears,
bool &changed,
bool useOgre,
523 bool not_used,
unsigned int width,
unsigned int height,
526 nbVisiblePolygon = 0;
532 #ifdef VISP_HAVE_OGRE
536 vpTRACE(
"ViSP doesn't have Ogre3D, simple visibility test used");
540 for (
unsigned int i = 0; i < Lpol.size(); i++) {
542 if (computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, width, height, cam, cameraPos,
546 return nbVisiblePolygon;
565 template <
class PolygonType>
567 const double &angleDisappears,
bool &changed,
bool useOgre,
568 bool not_used,
unsigned int width,
unsigned int height,
573 unsigned int i = index;
574 Lpol[i]->changeFrame(cMo);
575 Lpol[i]->isappearing =
false;
585 if (Lpol[i]->isVisible()) {
586 bool testDisappear =
false;
589 if (!testDisappear) {
591 #ifdef VISP_HAVE_OGRE
593 ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, width, height)) || !isVisibleOgre(cameraPos, i));
597 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
601 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
609 Lpol[i]->isvisible =
false;
613 Lpol[i]->isvisible =
true;
620 bool testAppear =
true;
624 #ifdef VISP_HAVE_OGRE
626 ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, width, height)) && isVisibleOgre(cameraPos, i));
628 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
631 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
636 Lpol[i]->isvisible =
true;
642 Lpol[i]->isvisible =
false;
648 return Lpol[i]->isvisible;
662 template <
class PolygonType>
665 const double &angle,
bool &changed)
667 return setVisible(width, height, cam, cMo, angle, angle, changed);
682 template <
class PolygonType>
685 const double &angleAppears,
const double &angleDisappears,
688 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false,
true, width, height, cam);
701 template <
class PolygonType>
703 const double &angleDisappears,
bool &changed)
705 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false);
708 #ifdef VISP_HAVE_OGRE
716 ogreInitialised =
true;
719 ogre->
init(ogreBackground,
false,
true);
721 for (
unsigned int n = 0; n < Lpol.size(); n++) {
722 Ogre::ManualObject *manual = ogre->
getSceneManager()->createManualObject(Ogre::StringConverter::toString(n));
724 manual->begin(
"BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);
725 for (
unsigned int i = 0; i < Lpol[n]->nbpt; i++) {
726 manual->position((Ogre::Real)Lpol[n]->p[i].get_oX(), (Ogre::Real)Lpol[n]->p[i].get_oY(),
727 (Ogre::Real)Lpol[n]->p[i].get_oZ());
728 manual->colour(1.0, 1.0, 1.0);
735 ogre->
getSceneManager()->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
737 lOgrePolygons.push_back(manual);
749 for (
unsigned int i = 0; i < Lpol.size(); i++) {
750 if (Lpol[i]->isVisible()) {
751 lOgrePolygons[i]->setVisible(
true);
754 lOgrePolygons[i]->setVisible(
false);
756 ogre->
display(ogreBackground, cMo);
772 template <
class PolygonType>
775 const double &angleAppears,
const double &angleDisappears,
778 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true,
true, width, height, cam);
791 template <
class PolygonType>
793 const double &angleDisappears,
bool &changed)
795 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true);
806 template <
class PolygonType>
809 Ogre::Vector3 camera((Ogre::Real)cameraPos[0], (Ogre::Real)cameraPos[1], (Ogre::Real)cameraPos[2]);
810 if (!ogre->
getCamera()->isVisible(lOgrePolygons[index]->getBoundingBox())) {
811 lOgrePolygons[index]->setVisible(
false);
812 Lpol[index]->isvisible =
false;
817 bool visible =
false;
818 unsigned int nbVisible = 0;
820 for (
unsigned int i = 0; i < nbRayAttempts; i++) {
821 Ogre::Vector3 origin(0, 0, 0);
822 Ogre::Real totalFactor = 0.0f;
824 for (
unsigned int j = 0; j < Lpol[index]->getNbPoint(); j++) {
825 Ogre::Real factor = 1.0f;
827 if (nbRayAttempts > 1) {
828 int r = rand() % 101;
831 factor = ((Ogre::Real)r) / 100.0f;
834 Ogre::Vector3 tmp((Ogre::Real)Lpol[index]->getPoint(j).get_oX(), (Ogre::Real)Lpol[index]->getPoint(j).get_oY(),
835 (Ogre::Real)Lpol[index]->getPoint(j).get_oZ());
838 totalFactor += factor;
841 origin /= totalFactor;
843 Ogre::Vector3 direction = origin - camera;
844 Ogre::Real distanceCollision = direction.length();
846 direction.normalise();
847 Ogre::RaySceneQuery *mRaySceneQuery = ogre->
getSceneManager()->createRayQuery(Ogre::Ray(camera, direction));
848 mRaySceneQuery->setSortByDistance(
true);
850 Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute();
851 Ogre::RaySceneQueryResult::iterator it = result.begin();
860 if (it != result.end())
861 if (it->movable->getName().find(
"SimpleRenderable") != Ogre::String::npos)
868 if (Lpol[index]->getNbPoint() == 2 &&
869 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
870 std::numeric_limits<double>::epsilon()) +
871 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
872 std::numeric_limits<double>::epsilon()) +
873 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
874 std::numeric_limits<double>::epsilon())) >= 2)) {
875 if (it != result.end()) {
876 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
880 distance = it->distance;
884 if (distance > distanceCollision || std::fabs(distance - distanceCollision) <
893 if (it != result.end()) {
894 distance = it->distance;
895 double distancePrev = distance;
900 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;
928 if (((
double)nbVisible) / ((
double)nbRayAttempts) > ratioVisibleRay ||
929 std::fabs(((
double)nbVisible) / ((
double)nbRayAttempts) - ratioVisibleRay) <
930 ratioVisibleRay * std::numeric_limits<double>::epsilon())
936 lOgrePolygons[index]->setVisible(
true);
937 Lpol[index]->isvisible =
true;
940 lOgrePolygons[index]->setVisible(
false);
941 Lpol[index]->isvisible =
false;
944 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.