36 #ifndef vpMbHiddenFaces_HH
37 #define vpMbHiddenFaces_HH
39 #include <visp3/core/vpHomogeneousMatrix.h>
40 #include <visp3/core/vpMeterPixelConversion.h>
41 #include <visp3/core/vpPixelMeterConversion.h>
42 #include <visp3/mbt/vpMbScanLine.h>
43 #include <visp3/mbt/vpMbtPolygon.h>
46 #include <visp3/ar/vpAROgre.h>
68 std::vector<PolygonType *> Lpol;
70 unsigned int nbVisiblePolygon;
71 vpMbScanLine scanlineRender;
76 unsigned int nbRayAttempts;
77 double ratioVisibleRay;
79 std::vector<Ogre::ManualObject *> lOgrePolygons;
80 bool ogreShowConfigDialog;
84 const double &angleDisappears,
bool &changed,
bool useOgre =
false,
85 bool not_used =
false,
unsigned int width = 0,
unsigned int height = 0,
98 bool &changed,
bool useOgre,
bool not_used,
unsigned int width,
unsigned int height,
106 const bool &displayResults =
false);
110 #ifdef VISP_HAVE_OGRE
121 #ifdef VISP_HAVE_OGRE
132 #ifdef VISP_HAVE_OGRE
163 bool isAppearing(
unsigned int i) {
return Lpol[i]->isAppearing(); }
165 #ifdef VISP_HAVE_OGRE
181 bool isVisible(
unsigned int i) {
return Lpol[i]->isVisible(); }
183 #ifdef VISP_HAVE_OGRE
188 inline PolygonType *
operator[](
unsigned int i) {
return Lpol[i]; }
190 inline const PolygonType *
operator[](
unsigned int i)
const {
return Lpol[i]; }
194 #ifdef VISP_HAVE_OGRE
232 ratioVisibleRay = ratio;
233 if (ratioVisibleRay > 1.0)
234 ratioVisibleRay = 1.0;
235 if (ratioVisibleRay < 0.0)
236 ratioVisibleRay = 0.0;
259 #ifdef VISP_HAVE_OGRE
271 inline unsigned int size()
const {
return (
unsigned int)Lpol.size(); }
277 template <
class PolygonType>
280 #ifdef VISP_HAVE_OGRE
281 ogreInitialised =
false;
283 ratioVisibleRay = 1.0;
284 ogreShowConfigDialog =
false;
295 for (
unsigned int i = 0; i < Lpol.size(); i++) {
296 if (Lpol[i] !=
nullptr) {
303 #ifdef VISP_HAVE_OGRE
304 if (ogre !=
nullptr) {
317 lOgrePolygons.resize(0);
324 template <
class PolygonType>
326 : Lpol(), nbVisiblePolygon(copy.nbVisiblePolygon), scanlineRender(copy.scanlineRender)
327 #ifdef VISP_HAVE_OGRE
329 ogreBackground(copy.ogreBackground), ogreInitialised(copy.ogreInitialised), nbRayAttempts(copy.nbRayAttempts),
330 ratioVisibleRay(copy.ratioVisibleRay), ogre(nullptr), lOgrePolygons(), ogreShowConfigDialog(copy.ogreShowConfigDialog)
334 for (
unsigned int i = 0; i < copy.Lpol.size(); i++) {
335 PolygonType *poly =
new PolygonType(*copy.Lpol[i]);
336 Lpol.push_back(poly);
343 swap(first.Lpol, second.Lpol);
344 swap(first.nbVisiblePolygon, second.nbVisiblePolygon);
345 swap(first.scanlineRender, second.scanlineRender);
346 #ifdef VISP_HAVE_OGRE
347 swap(first.ogreInitialised, second.ogreInitialised);
348 swap(first.nbRayAttempts, second.nbRayAttempts);
349 swap(first.ratioVisibleRay, second.ratioVisibleRay);
350 swap(first.ogreShowConfigDialog, second.ogreShowConfigDialog);
351 swap(first.ogre, second.ogre);
352 swap(first.ogreBackground, second.ogreBackground);
359 template <
class PolygonType>
374 PolygonType *p_new =
new PolygonType;
375 p_new->index = p->index;
376 p_new->setNbPoint(p->nbpt);
377 p_new->isvisible = p->isvisible;
378 p_new->useLod = p->useLod;
379 p_new->minLineLengthThresh = p->minLineLengthThresh;
380 p_new->minPolygonAreaThresh = p->minPolygonAreaThresh;
381 p_new->setName(p->name);
382 p_new->hasOrientation = p->hasOrientation;
384 for (
unsigned int i = 0; i < p->nbpt; i++)
385 p_new->p[i] = p->p[i];
386 Lpol.push_back(p_new);
394 nbVisiblePolygon = 0;
395 for (
unsigned int i = 0; i < Lpol.size(); i++) {
396 if (Lpol[i] !=
nullptr) {
403 #ifdef VISP_HAVE_OGRE
404 if (ogre !=
nullptr) {
417 lOgrePolygons.resize(0);
419 ogreInitialised =
false;
421 ratioVisibleRay = 1.0;
434 template <
class PolygonType>
437 for (
unsigned int i = 0; i < Lpol.size(); i++) {
443 Lpol[i]->changeFrame(cMo);
444 Lpol[i]->computePolygonClipped(cam);
457 template <
class PolygonType>
459 const unsigned int &h)
461 std::vector<std::vector<std::pair<vpPoint, unsigned int> > > polyClipped(Lpol.size());
462 std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> listPolyClipped;
463 std::vector<int> listPolyIndices;
465 for (
unsigned int i = 0; i < Lpol.size(); i++) {
471 polyClipped[i].clear();
472 Lpol[i]->getPolygonClipped(polyClipped[i]);
473 if (polyClipped[i].size() != 0) {
474 listPolyClipped.push_back(&polyClipped[i]);
475 listPolyIndices.push_back(Lpol[i]->getIndex());
480 scanlineRender.drawScene(listPolyClipped, listPolyIndices, cam, w, h);
493 template <
class PolygonType>
495 std::vector<std::pair<vpPoint, vpPoint> > &lines,
496 const bool &displayResults)
498 scanlineRender.queryLineVisibility(a, b, lines, displayResults);
515 template <
class PolygonType>
517 const double &angleDisappears,
bool &changed,
bool useOgre,
518 bool not_used,
unsigned int width,
unsigned int height,
521 nbVisiblePolygon = 0;
527 #ifdef VISP_HAVE_OGRE
531 vpTRACE(
"ViSP doesn't have Ogre3D, simple visibility test used");
535 for (
unsigned int i = 0; i < Lpol.size(); i++) {
537 if (computeVisibility(cMo, angleAppears, angleDisappears, changed, useOgre, not_used, width, height, cam, cameraPos,
541 return nbVisiblePolygon;
560 template <
class PolygonType>
562 const double &angleDisappears,
bool &changed,
bool useOgre,
563 bool not_used,
unsigned int width,
unsigned int height,
568 unsigned int i = index;
569 Lpol[i]->changeFrame(cMo);
570 Lpol[i]->isappearing =
false;
580 if (Lpol[i]->isVisible()) {
581 bool testDisappear =
false;
584 if (!testDisappear) {
586 #ifdef VISP_HAVE_OGRE
588 ((!Lpol[i]->isVisible(cMo, angleDisappears,
true, cam, width, height)) || !isVisibleOgre(cameraPos, i));
592 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
596 testDisappear = (!Lpol[i]->isVisible(cMo, angleDisappears,
false, cam, width, height));
604 Lpol[i]->isvisible =
false;
608 Lpol[i]->isvisible =
true;
615 bool testAppear =
true;
619 #ifdef VISP_HAVE_OGRE
621 ((Lpol[i]->isVisible(cMo, angleAppears,
true, cam, width, height)) && isVisibleOgre(cameraPos, i));
623 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
626 testAppear = (Lpol[i]->isVisible(cMo, angleAppears,
false, cam, width, height));
631 Lpol[i]->isvisible =
true;
637 Lpol[i]->isvisible =
false;
643 return Lpol[i]->isvisible;
657 template <
class PolygonType>
660 const double &angle,
bool &changed)
662 return setVisible(width, height, cam, cMo, angle, angle, changed);
677 template <
class PolygonType>
680 const double &angleAppears,
const double &angleDisappears,
683 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
false,
true, width, height, 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++) {
745 if (Lpol[i]->isVisible()) {
746 lOgrePolygons[i]->setVisible(
true);
749 lOgrePolygons[i]->setVisible(
false);
751 ogre->
display(ogreBackground, cMo);
767 template <
class PolygonType>
770 const double &angleAppears,
const double &angleDisappears,
773 return setVisiblePrivate(cMo, angleAppears, angleDisappears, changed,
true,
true, width, height, 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") != Ogre::String::npos)
863 if (Lpol[index]->getNbPoint() == 2 &&
864 (((std::fabs(Lpol[index]->getPoint(0).get_oX() - Lpol[index]->getPoint(1).get_oX()) <
865 std::numeric_limits<double>::epsilon()) +
866 (std::fabs(Lpol[index]->getPoint(0).get_oY() - Lpol[index]->getPoint(1).get_oY()) <
867 std::numeric_limits<double>::epsilon()) +
868 (std::fabs(Lpol[index]->getPoint(0).get_oZ() - Lpol[index]->getPoint(1).get_oZ()) <
869 std::numeric_limits<double>::epsilon())) >= 2)) {
870 if (it != result.end()) {
871 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
875 distance = it->distance;
879 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)) {
900 while (it != result.end()) {
901 distance = it->distance;
903 if (std::fabs(distance - distancePrev) <
906 if (it->movable->getName() == Ogre::StringConverter::toString(index)) {
911 distancePrev = distance;
923 if (((
double)nbVisible) / ((
double)nbRayAttempts) > ratioVisibleRay ||
924 std::fabs(((
double)nbVisible) / ((
double)nbRayAttempts) - ratioVisibleRay) <
925 ratioVisibleRay * std::numeric_limits<double>::epsilon())
931 lOgrePolygons[index]->setVisible(
true);
932 Lpol[index]->isvisible =
true;
935 lOgrePolygons[index]->setVisible(
false);
936 Lpol[index]->isvisible =
false;
939 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.