Visual Servoing Platform  version 3.6.0 under development (2023-09-25)
vpMbtPolygon.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Romain Tallonneau
36  * Aurelien Yol
37  *
38 *****************************************************************************/
39 
40 #include <limits.h>
41 
42 #include <visp3/core/vpConfig.h>
48 #include <visp3/core/vpPolygon.h>
49 #include <visp3/mbt/vpMbtPolygon.h>
50 
55  : index(-1), isvisible(false), isappearing(false), useLod(false), minLineLengthThresh(50.0),
56  minPolygonAreaThresh(2500.0), name(""), hasOrientation(true)
57 {
58 }
59 
61  : vpPolygon3D(mbtp), index(mbtp.index), isvisible(mbtp.isvisible), isappearing(mbtp.isappearing), useLod(mbtp.useLod),
62  minLineLengthThresh(mbtp.minLineLengthThresh), minPolygonAreaThresh(mbtp.minPolygonAreaThresh), name(mbtp.name),
63  hasOrientation(mbtp.hasOrientation)
64 {
65  //*this = mbtp; // Should not be called by copy contructor to avoid multiple
66  // assignements.
67 }
68 
70 {
72  index = mbtp.index;
73  isvisible = mbtp.isvisible;
74  isappearing = mbtp.isappearing;
75  useLod = mbtp.useLod;
78  name = mbtp.name;
80 
81  return (*this);
82 }
83 
88 
105 bool vpMbtPolygon::isVisible(const vpHomogeneousMatrix &cMo, double alpha, const bool &modulo,
106  const vpCameraParameters &cam, unsigned int width, unsigned int height)
107 {
108  // std::cout << "Computing angle from MBT Face (cMo, alpha)" << std::endl;
109 
110  changeFrame(cMo);
111 
112  if (nbpt <= 2) {
113  // Level of detail (LOD)
114  if (useLod) {
115  vpCameraParameters c = cam;
116  if (clippingFlag > 3) { // Contains at least one FOV constraint
117  c.computeFov(width, height);
118  }
120  std::vector<vpImagePoint> roiImagePoints;
121  getRoiClipped(c, roiImagePoints);
122 
123  if (roiImagePoints.size() == 2) {
124  double x1 = roiImagePoints[0].get_u();
125  double y1 = roiImagePoints[0].get_v();
126  double x2 = roiImagePoints[1].get_u();
127  double y2 = roiImagePoints[1].get_v();
128  double length = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
129  // std::cout << "Index=" << index << " ; Line length=" <<
130  // length << " ; clippingFlag=" << clippingFlag << std::endl;
131  // vpTRACE("index=%d length=%f minLineLengthThresh=%f", index,
132  // length, minLineLengthThresh);
133 
134  if (length < minLineLengthThresh) {
135  isvisible = false;
136  isappearing = false;
137  return false;
138  }
139  }
140  }
141 
142  /* a line is always visible when LOD is not used */
143  isvisible = true;
144  isappearing = false;
145  return true;
146  }
147 
148  // If the polygon has no orientation, the angle check visibility is always
149  // valid. Feature mainly used for cylinders.
150  if (!hasOrientation) {
151  isvisible = true;
152  isappearing = false;
153  return true;
154  }
155 
156  // Check visibility from normal
157  // Newell's Method for calculating the normal of an arbitrary 3D polygon
158  // https://www.opengl.org/wiki/Calculating_a_Surface_Normal
159  vpColVector faceNormal(3);
160  vpColVector currentVertex, nextVertex;
161  for (unsigned int i = 0; i < nbpt; i++) {
162  currentVertex = p[i].cP;
163  nextVertex = p[(i + 1) % nbpt].cP;
164 
165  faceNormal[0] += (currentVertex[1] - nextVertex[1]) * (currentVertex[2] + nextVertex[2]);
166  faceNormal[1] += (currentVertex[2] - nextVertex[2]) * (currentVertex[0] + nextVertex[0]);
167  faceNormal[2] += (currentVertex[0] - nextVertex[0]) * (currentVertex[1] + nextVertex[1]);
168  }
169  faceNormal.normalize();
170 
171  vpColVector e4(3);
172  vpPoint pt;
173  for (unsigned int i = 0; i < nbpt; i += 1) {
174  pt.set_X(pt.get_X() + p[i].get_X());
175  pt.set_Y(pt.get_Y() + p[i].get_Y());
176  pt.set_Z(pt.get_Z() + p[i].get_Z());
177  }
178  e4[0] = -pt.get_X() / (double)nbpt;
179  e4[1] = -pt.get_Y() / (double)nbpt;
180  e4[2] = -pt.get_Z() / (double)nbpt;
181  e4.normalize();
182 
183  double angle = acos(vpColVector::dotProd(e4, faceNormal));
184 
185  // vpCTRACE << angle << "/" << vpMath::deg(angle) << "/" <<
186  // vpMath::deg(alpha) << std::endl;
187 
188  if (angle < alpha || (modulo && (M_PI - angle) < alpha)) {
189  isvisible = true;
190  isappearing = false;
191 
192  if (useLod) {
193  vpCameraParameters c = cam;
194  if (clippingFlag > 3) { // Contains at least one FOV constraint
195  c.computeFov(width, height);
196  }
198  std::vector<vpImagePoint> roiImagePoints;
199  getRoiClipped(c, roiImagePoints);
200 
201  vpPolygon roiPolygon(roiImagePoints);
202  double area = roiPolygon.getArea();
203  // std::cout << "After normal test ; Index=" << index << " ; area="
204  // << area << " ; clippingFlag="
205  // << clippingFlag << std::endl;
206  if (area < minPolygonAreaThresh) {
207  isappearing = false;
208  isvisible = false;
209  return false;
210  }
211  }
212 
213  return true;
214  }
215 
216  if (angle < alpha + vpMath::rad(1)) {
217  isappearing = true;
218  } else if (modulo && (M_PI - angle) < alpha + vpMath::rad(1)) {
219  isappearing = true;
220  } else {
221  isappearing = false;
222  }
223 
224  isvisible = false;
225  return false;
226 }
227 
228 //###################################
229 // Static functions
230 //###################################
231 
275 void vpMbtPolygon::setLod(bool use_lod) { this->useLod = use_lod; }
Generic class defining intrinsic camera parameters.
void computeFov(const unsigned int &w, const unsigned int &h)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
static double dotProd(const vpColVector &a, const vpColVector &b)
vpColVector & normalize()
Implementation of an homogeneous matrix and operations on such kind of matrices.
static double rad(double deg)
Definition: vpMath.h:116
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:65
bool isvisible
flag to specify whether the face is visible or not
Definition: vpMbtPolygon.h:71
bool hasOrientation
Definition: vpMbtPolygon.h:87
double minLineLengthThresh
Definition: vpMbtPolygon.h:79
void setLod(bool use_lod)
double minPolygonAreaThresh
Definition: vpMbtPolygon.h:82
virtual ~vpMbtPolygon()
bool isappearing
flag to specify whether the face is appearing or not
Definition: vpMbtPolygon.h:73
std::string name
Name of the polygon.
Definition: vpMbtPolygon.h:84
bool isVisible() const
Definition: vpMbtPolygon.h:113
vpMbtPolygon & operator=(const vpMbtPolygon &mbtp)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:451
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:490
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:492
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:453
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:494
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:449
Implements a 3D polygon with render functionalities like clipping.
Definition: vpPolygon3D.h:55
void changeFrame(const vpHomogeneousMatrix &cMo)
unsigned int nbpt
Number of points used to define the polygon.
Definition: vpPolygon3D.h:71
vpPoint * p
corners in the object frame
Definition: vpPolygon3D.h:76
void computePolygonClipped(const vpCameraParameters &cam=vpCameraParameters())
unsigned int clippingFlag
Clipping flag.
Definition: vpPolygon3D.h:80
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
vpPolygon3D & operator=(const vpPolygon3D &mbtp)
Definition: vpPolygon3D.cpp:70
Defines a generic 2D polygon.
Definition: vpPolygon.h:97
double getArea() const
Definition: vpPolygon.h:155
vpColVector cP
Definition: vpTracker.h:72