Visual Servoing Platform  version 3.1.0
vpMbtPolygon.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Make the complete tracking of an object by using its CAD model
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Romain Tallonneau
37  * Eric Marchand
38  * Aurelien Yol
39  *
40  *****************************************************************************/
41 
42 #include <limits.h>
43 
44 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpPolygon.h>
51 #include <visp3/mbt/vpMbtPolygon.h>
52 
57  : index(-1), isvisible(false), isappearing(false), useLod(false), minLineLengthThresh(50.0),
58  minPolygonAreaThresh(2500.0), name(""), hasOrientation(true)
59 {
60 }
61 
63  : vpPolygon3D(mbtp), index(mbtp.index), isvisible(mbtp.isvisible), isappearing(mbtp.isappearing), useLod(mbtp.useLod),
66 {
67  //*this = mbtp; // Should not be called by copy contructor to avoid multiple
68  // assignements.
69 }
70 
72 {
74  index = mbtp.index;
75  isvisible = mbtp.isvisible;
76  isappearing = mbtp.isappearing;
77  useLod = mbtp.useLod;
80  name = mbtp.name;
82 
83  return (*this);
84 }
85 
90 
106 bool vpMbtPolygon::isVisible(const vpHomogeneousMatrix &cMo, const double alpha, const bool &modulo,
107  const vpCameraParameters &cam, const vpImage<unsigned char> &I)
108 {
109  // std::cout << "Computing angle from MBT Face (cMo, alpha)" << std::endl;
110 
111  changeFrame(cMo);
112 
113  if (nbpt <= 2) {
114  // Level of detail (LOD)
115  if (useLod) {
116  vpCameraParameters c = cam;
117  if (clippingFlag > 3) { // Contains at least one FOV constraint
118  c.computeFov(I.getWidth(), I.getHeight());
119  }
121  std::vector<vpImagePoint> roiImagePoints;
122  getRoiClipped(c, roiImagePoints);
123 
124  if (roiImagePoints.size() == 2) {
125  double x1 = roiImagePoints[0].get_u();
126  double y1 = roiImagePoints[0].get_v();
127  double x2 = roiImagePoints[1].get_u();
128  double y2 = roiImagePoints[1].get_v();
129  double length = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
130  // std::cout << "Index=" << index << " ; Line length=" <<
131  // length << " ; clippingFlag=" << clippingFlag << std::endl;
132  // vpTRACE("index=%d lenght=%f minLineLengthThresh=%f", index,
133  // length, minLineLengthThresh);
134 
135  if (length < minLineLengthThresh) {
136  isvisible = false;
137  isappearing = false;
138  return false;
139  }
140  }
141  }
142 
143  /* a line is always visible when LOD is not used */
144  isvisible = true;
145  isappearing = false;
146  return true;
147  }
148 
149  // If the polygon has no orientation, the angle check visibility is always
150  // valid. Feature mainly used for cylinders.
151  if (!hasOrientation) {
152  isvisible = true;
153  isappearing = false;
154  return true;
155  }
156 
157  // Check visibility from normal
158  // Newell's Method for calculating the normal of an arbitrary 3D polygon
159  // https://www.opengl.org/wiki/Calculating_a_Surface_Normal
160  vpColVector faceNormal(3);
161  vpColVector currentVertex, nextVertex;
162  for (unsigned int i = 0; i < nbpt; i++) {
163  currentVertex = p[i].cP;
164  nextVertex = p[(i + 1) % nbpt].cP;
165 
166  faceNormal[0] += (currentVertex[1] - nextVertex[1]) * (currentVertex[2] + nextVertex[2]);
167  faceNormal[1] += (currentVertex[2] - nextVertex[2]) * (currentVertex[0] + nextVertex[0]);
168  faceNormal[2] += (currentVertex[0] - nextVertex[0]) * (currentVertex[1] + nextVertex[1]);
169  }
170  faceNormal.normalize();
171 
172  vpColVector e4(3);
173  vpPoint pt;
174  for (unsigned int i = 0; i < nbpt; i += 1) {
175  pt.set_X(pt.get_X() + p[i].get_X());
176  pt.set_Y(pt.get_Y() + p[i].get_Y());
177  pt.set_Z(pt.get_Z() + p[i].get_Z());
178  }
179  e4[0] = -pt.get_X() / (double)nbpt;
180  e4[1] = -pt.get_Y() / (double)nbpt;
181  e4[2] = -pt.get_Z() / (double)nbpt;
182  e4.normalize();
183 
184  double angle = acos(vpColVector::dotProd(e4, faceNormal));
185 
186  // vpCTRACE << angle << "/" << vpMath::deg(angle) << "/" <<
187  // vpMath::deg(alpha) << std::endl;
188 
189  if (angle < alpha || (modulo && (M_PI - angle) < alpha)) {
190  isvisible = true;
191  isappearing = false;
192 
193  if (useLod) {
194  vpCameraParameters c = cam;
195  if (clippingFlag > 3) { // Contains at least one FOV constraint
196  c.computeFov(I.getWidth(), I.getHeight());
197  }
199  std::vector<vpImagePoint> roiImagePoints;
200  getRoiClipped(c, roiImagePoints);
201 
202  vpPolygon roiPolygon(roiImagePoints);
203  double area = roiPolygon.getArea();
204  // std::cout << "After normal test ; Index=" << index << " ; area="
205  // << area << " ; clippingFlag="
206  // << clippingFlag << std::endl;
207  if (area < minPolygonAreaThresh) {
208  isappearing = false;
209  isvisible = false;
210  return false;
211  }
212  }
213 
214  return true;
215  }
216 
217  if (angle < alpha + vpMath::rad(1)) {
218  isappearing = true;
219  } else if (modulo && (M_PI - angle) < alpha + vpMath::rad(1)) {
220  isappearing = true;
221  } else {
222  isappearing = false;
223  }
224 
225  isvisible = false;
226  return false;
227 }
228 
229 //###################################
230 // Static functions
231 //###################################
232 
279 void vpMbtPolygon::setLod(const bool use_lod) { this->useLod = use_lod; }
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
Implements a 3D polygon with render functionnalities like clipping.
Definition: vpPolygon3D.h:59
std::string name
Name of the polygon.
Definition: vpMbtPolygon.h:86
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual ~vpMbtPolygon()
bool hasOrientation
Definition: vpMbtPolygon.h:89
double getArea() const
Definition: vpPolygon.h:161
vpPoint * p
corners in the object frame
Definition: vpPolygon3D.h:81
vpMbtPolygon & operator=(const vpMbtPolygon &mbtp)
void set_X(const double X)
Set the point X coordinate in the camera frame.
Definition: vpPoint.cpp:452
Class that defines what is a point.
Definition: vpPoint.h:58
vpColVector cP
Definition: vpTracker.h:75
Defines a generic 2D polygon.
Definition: vpPolygon.h:103
void changeFrame(const vpHomogeneousMatrix &cMo)
bool isVisible() const
Definition: vpMbtPolygon.h:115
vpColVector & normalize()
void set_Z(const double Z)
Set the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:456
double minLineLengthThresh
Definition: vpMbtPolygon.h:81
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:66
void computePolygonClipped(const vpCameraParameters &cam=vpCameraParameters())
Generic class defining intrinsic camera parameters.
void set_Y(const double Y)
Set the point Y coordinate in the camera frame.
Definition: vpPoint.cpp:454
double minPolygonAreaThresh
Definition: vpMbtPolygon.h:84
vpPolygon3D & operator=(const vpPolygon3D &mbtp)
Definition: vpPolygon3D.cpp:70
static double rad(double deg)
Definition: vpMath.h:102
double get_X() const
Get the point X coordinate in the camera frame.
Definition: vpPoint.cpp:411
unsigned int nbpt
Number of points used to define the polygon.
Definition: vpPolygon3D.h:76
bool isappearing
flag to specify whether the face is appearing or not
Definition: vpMbtPolygon.h:75
unsigned int getHeight() const
Definition: vpImage.h:178
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setLod(const bool use_lod)
static double dotProd(const vpColVector &a, const vpColVector &b)
unsigned int clippingFlag
Clipping flag.
Definition: vpPolygon3D.h:85
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:415
bool isvisible
flag to specify whether the face is visible or not
Definition: vpMbtPolygon.h:73
double get_Y() const
Get the point Y coordinate in the camera frame.
Definition: vpPoint.cpp:413
unsigned int getWidth() const
Definition: vpImage.h:229
void computeFov(const unsigned int &w, const unsigned int &h)