Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
vpMbtDistanceCircle.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. Circle
33  * tracking.
34  *
35 *****************************************************************************/
36 
37 #include <visp3/core/vpConfig.h>
38 
44 #include <algorithm>
45 #include <stdlib.h>
46 
47 #include <visp3/core/vpMeterPixelConversion.h>
48 #include <visp3/core/vpPixelMeterConversion.h>
49 #include <visp3/core/vpPlane.h>
50 #include <visp3/mbt/vpMbtDistanceCircle.h>
51 #include <visp3/vision/vpPose.h>
52 #include <visp3/visual_features/vpFeatureBuilder.h>
53 #include <visp3/visual_features/vpFeatureEllipse.h>
54 
55 BEGIN_VISP_NAMESPACE
60  : name(), index(0), cam(), me(nullptr), wmean(1), featureEllipse(), isTrackedCircle(true), meEllipse(nullptr), circle(nullptr),
61  radius(0.), p1(nullptr), p2(nullptr), p3(nullptr), L(), error(), nbFeature(0), Reinit(false), hiddenface(nullptr),
62  index_polygon(-1), isvisible(false)
63 { }
64 
69 {
70  if (meEllipse != nullptr)
71  delete meEllipse;
72  if (circle != nullptr)
73  delete circle;
74  if (p1 != nullptr)
75  delete p1;
76  if (p2 != nullptr)
77  delete p2;
78  if (p3 != nullptr)
79  delete p3;
80 }
81 
88 void vpMbtDistanceCircle::project(const vpHomogeneousMatrix &cMo) { circle->project(cMo); }
89 
100 void vpMbtDistanceCircle::buildFrom(const vpPoint &_p1, const vpPoint &_p2, const vpPoint &_p3, double r)
101 {
102  circle = new vpCircle;
103  p1 = new vpPoint;
104  p2 = new vpPoint;
105  p3 = new vpPoint;
106 
107  // Get the points
108  *p1 = _p1;
109  *p2 = _p2;
110  *p3 = _p3;
111 
112  // Get the radius
113  radius = r;
114 
115  vpPlane plane(*p1, *p2, *p3, vpPlane::object_frame);
116 
117  // Build our circle
118  circle->setWorldCoordinates(plane.getA(), plane.getB(), plane.getC(), _p1.get_oX(), _p1.get_oY(), _p1.get_oZ(), r);
119 }
120 
127 {
128  me = _me;
129  if (meEllipse != nullptr) {
130  meEllipse->setMe(me);
131  }
132 }
133 
146  bool doNotTrack, const vpImage<bool> *mask)
147 {
148  if (isvisible) {
149  // Perspective projection
150  circle->changeFrame(cMo);
151 
152  try {
153  circle->projection();
154  }
155  catch (...) {
156  std::cout << "Problem when projecting circle\n";
157  return false;
158  }
159 
160  // Create the moving edges containers
161  meEllipse = new vpMbtMeEllipse;
162  meEllipse->setMask(*mask);
163  meEllipse->setMe(me);
164 
165  // meEllipse->setDisplay(vpMeSite::RANGE_RESULT) ; // TODO only for debug
166  meEllipse->setInitRange(me->getRange()); // TODO: check because set to zero for lines
167 
168  try {
169  vpImagePoint ic;
170  double n20_p, n11_p, n02_p;
171  vpMeterPixelConversion::convertEllipse(cam, *circle, ic, n20_p, n11_p, n02_p);
172  meEllipse->initTracking(I, ic, n20_p, n11_p, n02_p, doNotTrack);
173  }
174  catch (...) {
175  // vpTRACE("the circle can't be initialized");
176  return false;
177  }
178  }
179  return true;
180 }
181 
189 {
190  if (isvisible) {
191  try {
192  meEllipse->track(I);
193  }
194  catch (...) {
195  // std::cout << "Track meEllipse failed" << std::endl;
196  meEllipse->reset();
197  Reinit = true;
198  }
199 
200  // Update the number of features
201  nbFeature = (unsigned int)meEllipse->getMeList().size();
202  }
203 }
204 
214 {
215  if (isvisible) {
216  // Perspective projection
217  circle->changeFrame(cMo);
218 
219  try {
220  circle->projection();
221  }
222  catch (...) {
223  std::cout << "Problem when projecting circle\n";
224  }
225 
226  try {
227 
228  vpImagePoint ic;
229  double n20_p, n11_p, n02_p;
230  vpMeterPixelConversion::convertEllipse(cam, *circle, ic, n20_p, n11_p, n02_p);
231  meEllipse->updateParameters(I, ic, n20_p, n11_p, n02_p);
232  }
233  catch (...) {
234  Reinit = true;
235  }
236  nbFeature = (unsigned int)meEllipse->getMeList().size();
237  }
238 }
239 
252  const vpImage<bool> *mask)
253 {
254  if (meEllipse != nullptr)
255  delete meEllipse;
256 
257  meEllipse = nullptr;
258 
259  if (!initMovingEdge(I, cMo, false, mask))
260  Reinit = true;
261 
262  Reinit = false;
263 }
264 
277  const vpCameraParameters &camera, const vpColor &col, unsigned int thickness,
278  bool displayFullModel)
279 {
280  std::vector<double> params = getModelForDisplay(cMo, camera, displayFullModel);
281 
282  vpImagePoint center(params[0], params[1]);
283  double n20_p = params[2];
284  double n11_p = params[3];
285  double n02_p = params[4];
286  vpDisplay::displayEllipse(I, center, n20_p, n11_p, n02_p, true, col, thickness);
287 }
288 
301  const vpCameraParameters &camera, const vpColor &col, unsigned int thickness,
302  bool displayFullModel)
303 {
304  std::vector<double> params = getModelForDisplay(cMo, camera, displayFullModel);
305 
306  vpImagePoint center(params[1], params[2]);
307  double n20_p = params[3];
308  double n11_p = params[4];
309  double n02_p = params[5];
310  vpDisplay::displayEllipse(I, center, n20_p, n11_p, n02_p, true, col, thickness);
311 }
312 
317 std::vector<std::vector<double> > vpMbtDistanceCircle::getFeaturesForDisplay()
318 {
319  std::vector<std::vector<double> > features;
320 
321  if (meEllipse != nullptr) {
322  for (std::list<vpMeSite>::const_iterator it = meEllipse->getMeList().begin(); it != meEllipse->getMeList().end();
323  ++it) {
324  vpMeSite p_me = *it;
325 #if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_98)
326  std::vector<double> params = { 0, //ME
327  p_me.get_ifloat(),
328  p_me.get_jfloat(),
329  static_cast<double>(p_me.getState()) };
330 #else
331  std::vector<double> params;
332  params.push_back(0); //ME
333  params.push_back(p_me.get_ifloat());
334  params.push_back(p_me.get_jfloat());
335  params.push_back(static_cast<double>(p_me.getState()));
336 #endif
337 
338  features.push_back(params);
339  }
340  }
341 
342  return features;
343 }
344 
357  const vpCameraParameters &camera, bool displayFullModel)
358 {
359  std::vector<double> params;
360 
361  if ((isvisible && isTrackedCircle) || displayFullModel) {
362  // Perspective projection
363  circle->changeFrame(cMo);
364 
365  try {
366  circle->projection();
367  }
368  catch (...) {
369  std::cout << "Cannot project the circle";
370  }
371 
372  vpImagePoint center;
373  double n20_p, n11_p, n02_p;
374  vpMeterPixelConversion::convertEllipse(camera, *circle, center, n20_p, n11_p, n02_p);
375  params.push_back(1); // 1 for ellipse parameters
376  params.push_back(center.get_i());
377  params.push_back(center.get_j());
378  params.push_back(n20_p);
379  params.push_back(n11_p);
380  params.push_back(n02_p);
381  }
382 
383  return params;
384 }
385 
398 {
399  if (meEllipse != nullptr) {
400  meEllipse->display(I); // display the me
401  if (vpDEBUG_ENABLE(3))
402  vpDisplay::flush(I);
403  }
404 }
405 
407 {
408  if (meEllipse != nullptr) {
409  meEllipse->display(I); // display the me
410  if (vpDEBUG_ENABLE(3))
411  vpDisplay::flush(I);
412  }
413 }
414 
419 {
420  if (isvisible) {
421  nbFeature = (unsigned int)meEllipse->getMeList().size();
422  L.resize(nbFeature, 6);
424  }
425  else
426  nbFeature = 0;
427 }
428 
434 {
435  if (isvisible) {
436  // Perspective projection
437  circle->changeFrame(cMo);
438  try {
439  circle->projection();
440  }
441  catch (...) {
442  std::cout << "Problem projection circle\n";
443  }
444 
445  vpFeatureBuilder::create(featureEllipse, *circle);
446 
447  vpMatrix H1 = featureEllipse.interaction();
448 
449  vpRowVector H(5);
450  double x = 0, y = 0;
451 
452  // Get the parameters of the ellipse in the image plane
453  double xg = circle->p[0];
454  double yg = circle->p[1];
455  double n20 = circle->p[2];
456  double n11 = circle->p[3];
457  double n02 = circle->p[4];
458 
459  unsigned int j = 0;
460 
461  for (std::list<vpMeSite>::const_iterator it = meEllipse->getMeList().begin(); it != meEllipse->getMeList().end();
462  ++it) {
463  vpPixelMeterConversion::convertPoint(cam, it->m_j, it->m_i, x, y);
464  // TRO Chaumette 2004 eq 25
465  H[0] = 2 * (n11 * (y - yg) + n02 * (xg - x));
466  H[1] = 2 * (n20 * (yg - y) + n11 * (x - xg));
467  H[2] = vpMath::sqr(y - yg) - 4.0 * n02;
468  H[3] = 2 * (yg * (x - xg) + y * xg + 4.0 * n11 - x * y);
469  H[4] = vpMath::sqr(x - xg) - 4.0 * n20;
470 
471  for (unsigned int k = 0; k < 6; k++)
472  L[j][k] = H[0] * H1[0][k] + H[1] * H1[1][k] + H[2] * H1[2][k] + H[3] * H1[3][k] + H[4] * H1[4][k];
473 
474  error[j] = n02 * vpMath::sqr(x) + n20 * vpMath::sqr(y) - 2 * n11 * x * y + 2 * (n11 * yg - n02 * xg) * x +
475  2 * (n11 * xg - n20 * yg) * y + n02 * vpMath::sqr(xg) + n20 * vpMath::sqr(yg) - 2 * n11 * xg * yg +
476  4.0 * vpMath::sqr(n11) - 4.0 * n20 * n02;
477  j++;
478  }
479  }
480 }
481 END_VISP_NAMESPACE
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:362
Generic class defining intrinsic camera parameters.
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:87
void changeFrame(const vpHomogeneousMatrix &noMo, vpColVector &noP) const VP_OVERRIDE
Definition: vpCircle.cpp:262
void projection() VP_OVERRIDE
Definition: vpCircle.cpp:144
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpCircle.cpp:57
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static void displayEllipse(const vpImage< unsigned char > &I, const vpImagePoint &center, const double &coef1, const double &coef2, const double &coef3, bool use_normalized_centered_moments, const vpColor &color, unsigned int thickness=1, bool display_center=false, bool display_arc=false)
static void flush(const vpImage< unsigned char > &I)
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
compute the interaction matrix from a subset a the possible features
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_j() const
Definition: vpImagePoint.h:125
double get_i() const
Definition: vpImagePoint.h:114
static double sqr(double x)
Definition: vpMath.h:203
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
void updateMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
std::vector< std::vector< double > > getFeaturesForDisplay()
vpColVector error
The error vector.
vpPoint * p1
The center of the circle.
unsigned int nbFeature
The number of moving edges.
vpMatrix L
The interaction matrix.
void trackMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
void computeInteractionMatrixError(const vpHomogeneousMatrix &cMo)
vpCircle * circle
The circle to track.
void buildFrom(const vpPoint &_p1, const vpPoint &_p2, const vpPoint &_p3, double r)
vpPoint * p2
A point on the plane containing the circle.
bool isvisible
Indicates if the circle is visible or not.
void displayMovingEdges(const vpImage< unsigned char > &I)
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
bool Reinit
Indicates if the circle has to be reinitialized.
void reinitMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpImage< bool > *mask=nullptr)
bool initMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, bool doNotTrack, const vpImage< bool > *mask=nullptr)
double radius
The radius of the circle.
vpPoint * p3
An other point on the plane containing the circle.
std::vector< double > getModelForDisplay(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false)
vpMbtMeEllipse * meEllipse
The moving edge containers.
Performs search in a given direction(normal) for a given distance(pixels) for a given 'site'....
Definition: vpMeSite.h:68
vpMeSiteState getState() const
Definition: vpMeSite.h:283
double get_ifloat() const
Definition: vpMeSite.h:195
double get_jfloat() const
Definition: vpMeSite.h:201
Definition: vpMe.h:134
unsigned int getRange() const
Definition: vpMe.h:268
static void convertEllipse(const vpCameraParameters &cam, const vpSphere &sphere, vpImagePoint &center_p, double &n20_p, double &n11_p, double &n02_p)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:57
@ object_frame
Definition: vpPlane.h:65
double getA() const
Definition: vpPlane.h:100
double getC() const
Definition: vpPlane.h:104
double getB() const
Definition: vpPlane.h:102
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:411
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:415
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:413
Implementation of row vector and the associated operations.
Definition: vpRowVector.h:124
vpColVector p
Definition: vpTracker.h:69