Visual Servoing Platform  version 3.6.1 under development (2024-04-20)
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 
60  : vpPolygon3D(mbtp), index(mbtp.index), isvisible(mbtp.isvisible), isappearing(mbtp.isappearing), useLod(mbtp.useLod),
61  minLineLengthThresh(mbtp.minLineLengthThresh), minPolygonAreaThresh(mbtp.minPolygonAreaThresh), name(mbtp.name),
62  hasOrientation(mbtp.hasOrientation)
63 {
64  //*this = mbtp; // Should not be called by copy constructor to avoid multiple assignments.
65 }
66 
68 {
70  index = mbtp.index;
71  isvisible = mbtp.isvisible;
72  isappearing = mbtp.isappearing;
73  useLod = mbtp.useLod;
76  name = mbtp.name;
78 
79  return (*this);
80 }
81 
98 bool vpMbtPolygon::isVisible(const vpHomogeneousMatrix &cMo, double alpha, const bool &modulo,
99  const vpCameraParameters &cam, unsigned int width, unsigned int height)
100 {
101  // std::cout << "Computing angle from MBT Face (cMo, alpha)" << std::endl;
102 
103  changeFrame(cMo);
104 
105  if (nbpt <= 2) {
106  // Level of detail (LOD)
107  if (useLod) {
108  vpCameraParameters c = cam;
109  if (clippingFlag > 3) { // Contains at least one FOV constraint
110  c.computeFov(width, height);
111  }
113  std::vector<vpImagePoint> roiImagePoints;
114  getRoiClipped(c, roiImagePoints);
115 
116  if (roiImagePoints.size() == 2) {
117  double x1 = roiImagePoints[0].get_u();
118  double y1 = roiImagePoints[0].get_v();
119  double x2 = roiImagePoints[1].get_u();
120  double y2 = roiImagePoints[1].get_v();
121  double length = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
122  // std::cout << "Index=" << index << " ; Line length=" <<
123  // length << " ; clippingFlag=" << clippingFlag << std::endl;
124  // vpTRACE("index=%d length=%f minLineLengthThresh=%f", index,
125  // length, minLineLengthThresh);
126 
127  if (length < minLineLengthThresh) {
128  isvisible = false;
129  isappearing = false;
130  return false;
131  }
132  }
133  }
134 
135  /* a line is always visible when LOD is not used */
136  isvisible = true;
137  isappearing = false;
138  return true;
139  }
140 
141  // If the polygon has no orientation, the angle check visibility is always
142  // valid. Feature mainly used for cylinders.
143  if (!hasOrientation) {
144  isvisible = true;
145  isappearing = false;
146  return true;
147  }
148 
149  // Check visibility from normal
150  // Newell's Method for calculating the normal of an arbitrary 3D polygon
151  // https://www.opengl.org/wiki/Calculating_a_Surface_Normal
152  vpColVector faceNormal(3);
153  vpColVector currentVertex, nextVertex;
154  for (unsigned int i = 0; i < nbpt; i++) {
155  currentVertex = p[i].cP;
156  nextVertex = p[(i + 1) % nbpt].cP;
157 
158  faceNormal[0] += (currentVertex[1] - nextVertex[1]) * (currentVertex[2] + nextVertex[2]);
159  faceNormal[1] += (currentVertex[2] - nextVertex[2]) * (currentVertex[0] + nextVertex[0]);
160  faceNormal[2] += (currentVertex[0] - nextVertex[0]) * (currentVertex[1] + nextVertex[1]);
161  }
162  faceNormal.normalize();
163 
164  vpColVector e4(3);
165  vpPoint pt;
166  for (unsigned int i = 0; i < nbpt; i += 1) {
167  pt.set_X(pt.get_X() + p[i].get_X());
168  pt.set_Y(pt.get_Y() + p[i].get_Y());
169  pt.set_Z(pt.get_Z() + p[i].get_Z());
170  }
171  e4[0] = -pt.get_X() / (double)nbpt;
172  e4[1] = -pt.get_Y() / (double)nbpt;
173  e4[2] = -pt.get_Z() / (double)nbpt;
174  e4.normalize();
175 
176  double angle = acos(vpColVector::dotProd(e4, faceNormal));
177 
178  // vpCTRACE << angle << "/" << vpMath::deg(angle) << "/" <<
179  // vpMath::deg(alpha) << std::endl;
180 
181  if (angle < alpha || (modulo && (M_PI - angle) < alpha)) {
182  isvisible = true;
183  isappearing = false;
184 
185  if (useLod) {
186  vpCameraParameters c = cam;
187  if (clippingFlag > 3) { // Contains at least one FOV constraint
188  c.computeFov(width, height);
189  }
191  std::vector<vpImagePoint> roiImagePoints;
192  getRoiClipped(c, roiImagePoints);
193 
194  vpPolygon roiPolygon(roiImagePoints);
195  double area = roiPolygon.getArea();
196  // std::cout << "After normal test ; Index=" << index << " ; area="
197  // << area << " ; clippingFlag="
198  // << clippingFlag << std::endl;
199  if (area < minPolygonAreaThresh) {
200  isappearing = false;
201  isvisible = false;
202  return false;
203  }
204  }
205 
206  return true;
207  }
208 
209  if (angle < alpha + vpMath::rad(1)) {
210  isappearing = true;
211  }
212  else if (modulo && (M_PI - angle) < alpha + vpMath::rad(1)) {
213  isappearing = true;
214  }
215  else {
216  isappearing = false;
217  }
218 
219  isvisible = false;
220  return false;
221 }
222 
223 //###################################
224 // Static functions
225 //###################################
226 
270 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:163
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:127
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:58
bool isvisible
flag to specify whether the face is visible or not
Definition: vpMbtPolygon.h:64
bool hasOrientation
Definition: vpMbtPolygon.h:80
double minLineLengthThresh
Definition: vpMbtPolygon.h:72
void setLod(bool use_lod)
double minPolygonAreaThresh
Definition: vpMbtPolygon.h:75
bool isappearing
flag to specify whether the face is appearing or not
Definition: vpMbtPolygon.h:66
std::string name
Name of the polygon.
Definition: vpMbtPolygon.h:77
bool isVisible() const
Definition: vpMbtPolygon.h:105
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:447
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:486
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:488
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:449
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:490
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:445
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:71