ViSP  2.10.0
vpMbtDistanceCircle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtDistanceCircle.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Make the complete tracking of an object by using its CAD model. Circle
35  * tracking.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 
49 #include <stdlib.h>
50 #include <algorithm>
51 
52 #include <visp/vpMbtDistanceCircle.h>
53 #include <visp/vpPlane.h>
54 #include <visp/vpMeterPixelConversion.h>
55 #include <visp/vpPixelMeterConversion.h>
56 #include <visp/vpFeatureBuilder.h>
57 #include <visp/vpFeatureEllipse.h>
58 #include <visp/vpPose.h>
59 
64  : name(), index(0), cam(), me(NULL), wmean(1),
65  featureEllipse(), meEllipse(NULL),
66  circle(NULL), radius(0.), p1(NULL), p2(NULL), p3(NULL),
67  L(), error(), nbFeature(0), Reinit(false),
68  hiddenface(NULL), index_polygon(-1), isvisible(false)
69 {
70 }
71 
76 {
77  if (meEllipse != NULL) delete meEllipse;
78  if (circle != NULL) delete circle;
79  if (p1 != NULL) delete p1;
80  if (p2 != NULL) delete p2;
81  if (p3 != NULL) delete p3;
82 }
83 
89 void
90 vpMbtDistanceCircle::project(const vpHomogeneousMatrix &cMo)
91 {
92  circle->project(cMo) ;
93 }
94 
95 
106 void
107 vpMbtDistanceCircle::buildFrom(const vpPoint &_p1, const vpPoint &_p2, const vpPoint &_p3, const double r)
108 {
109  circle = new vpCircle ;
110  p1 = new vpPoint ;
111  p2 = new vpPoint ;
112  p3 = new vpPoint ;
113 
114  // Get the points
115  *p1 = _p1;
116  *p2 = _p2;
117  *p3 = _p3;
118 
119  // Get the radius
120  radius = r;
121 
122  vpPlane plane(*p1, *p2, *p3, vpPlane::object_frame);
123 
124  // Build our circle
125  circle->setWorldCoordinates(plane.getA(), plane.getB(), plane.getC(), _p1.get_oX(), _p1.get_oY(), _p1.get_oZ(), r);
126 }
127 
128 
134 void
136 {
137  me = _me ;
138  if (meEllipse != NULL)
139  {
140  meEllipse->setMe(me) ;
141  }
142 }
143 
152 bool
154 {
155  if(isvisible){
156  // Perspective projection
157  circle->changeFrame(cMo);
158 
159  try{
160  circle->projection();
161  }
162  catch(...){
163  std::cout<<"Problem when projecting circle\n";
164  return false;
165  }
166 
167  // Create the moving edges containers
168  meEllipse = new vpMbtMeEllipse;
169  meEllipse->setMe(me) ;
170 
171  //meEllipse->setDisplay(vpMeSite::RANGE_RESULT) ; // TODO only for debug
172  meEllipse->setInitRange(me->getRange()); // TODO: check because set to zero for lines
173 
174  try
175  {
176  vpImagePoint ic;
177  double mu20_p, mu11_p, mu02_p;
178  vpMeterPixelConversion::convertEllipse(cam, *circle, ic, mu20_p, mu11_p, mu02_p);
179  meEllipse->initTracking(I, ic, mu20_p, mu11_p, mu02_p);
180  }
181  catch(...)
182  {
183  //vpTRACE("the circle can't be initialized");
184  return false;
185  }
186  }
187  return true;
188 }
189 
196 void
198 {
199  if(isvisible){
200  try
201  {
202  meEllipse->track(I) ;
203  }
204  catch(...)
205  {
206  //std::cout << "Track meEllipse failed" << std::endl;
207  meEllipse->reset();
208  Reinit = true;
209  }
210 
211  // Update the number of features
212  nbFeature = (unsigned int)meEllipse->getMeList().size();
213  }
214 }
215 
216 
225 void
227 {
228  if(isvisible){
229  // Perspective projection
230  circle->changeFrame(cMo);
231 
232  try{
233  circle->projection();
234  }
235  catch(...){std::cout<<"Problem when projecting circle\n";}
236 
237  try
238  {
239 
240  vpImagePoint ic;
241  double mu20_p, mu11_p, mu02_p;
242  vpMeterPixelConversion::convertEllipse(cam, *circle, ic, mu20_p, mu11_p, mu02_p);
243  meEllipse->updateParameters(I, ic, mu20_p, mu11_p, mu02_p);
244  }
245  catch(...)
246  {
247  Reinit = true;
248  }
249  nbFeature = (unsigned int)meEllipse->getMeList().size();
250  }
251 }
252 
253 
262 void
264 {
265  if(meEllipse!= NULL)
266  delete meEllipse;
267 
268  meEllipse = NULL;
269 
270  if(initMovingEdge(I,cMo) == false)
271  Reinit = true;
272 
273  Reinit = false;
274 }
275 
276 
288 void
290  const vpCameraParameters &camera, const vpColor col, const unsigned int thickness,
291  const bool displayFullModel )
292 {
293  if(isvisible || displayFullModel){
294  // Perspective projection
295  circle->changeFrame(cMo);
296 
297  try{
298  circle->projection();
299  }
300  catch(...){std::cout<<"Cannot project the circle";}
301 
302  vpImagePoint center;
303  double mu20_p, mu11_p, mu02_p;
304  vpMeterPixelConversion::convertEllipse(camera, *circle, center, mu20_p, mu11_p, mu02_p);
305  vpDisplay::displayEllipse(I, center, mu20_p, mu11_p, mu02_p, true, col, thickness);
306  }
307 }
308 
320 void
322  const vpCameraParameters &camera, const vpColor col,
323  const unsigned int thickness, const bool displayFullModel)
324 {
325  if(isvisible || displayFullModel){
326  // Perspective projection
327  circle->changeFrame(cMo);
328 
329  try{
330  circle->projection();
331  }
332  catch(...){std::cout<<"Cannot project the circle";}
333 
334  vpImagePoint center;
335  double mu20_p, mu11_p, mu02_p;
336  vpMeterPixelConversion::convertEllipse(camera, *circle, center, mu20_p, mu11_p, mu02_p);
337  vpDisplay::displayEllipse(I, center, mu20_p, mu11_p, mu02_p, true, col, thickness);
338  }
339 }
340 
341 
352 void
354 {
355  if (meEllipse != NULL)
356  {
357  meEllipse->display(I); // display the me
358  if (vpDEBUG_ENABLE(3))
359  vpDisplay::flush(I);
360  }
361 }
362 
366 void
368 {
369  if (isvisible == true)
370  {
371  nbFeature = (unsigned int)meEllipse->getMeList().size();
372  L.resize(nbFeature, 6);
374  }
375  else
376  nbFeature = 0 ;
377 }
378 
382 void
384 {
385  if (isvisible)
386  {
387  // Perspective projection
388  circle->changeFrame(cMo) ;
389  try{
390  circle->projection();
391  }
392  catch(...){std::cout<<"Problem projection circle\n";}
393 
394  vpFeatureBuilder::create(featureEllipse, *circle);
395 
396  vpMatrix H1 = featureEllipse.interaction();
397 
398  vpRowVector H(5);
399  double x=0, y=0;
400 
401  // Get the parameters of the ellipse in the image plane
402  double xg = circle->p[0];
403  double yg = circle->p[1];
404  double mu20 = circle->p[2];
405  double mu11 = circle->p[3];
406  double mu02 = circle->p[4];
407 
408  unsigned int j = 0;
409 
410  for(std::list<vpMeSite>::const_iterator it=meEllipse->getMeList().begin(); it!=meEllipse->getMeList().end(); ++it){
411  vpPixelMeterConversion::convertPoint(cam, it->j, it->i, x, y);
412  H[0] = 2*(mu11*(y-yg)+mu02*(xg-x));
413  H[1] = 2*(mu20*(yg-y)+mu11*(x-xg));
414  H[2] = vpMath::sqr(y-yg)-mu02;
415  H[3] = 2*(yg*(x-xg)+y*xg+mu11-x*y);
416  H[4] = vpMath::sqr(x-xg)-mu20;
417 
418  for (unsigned int k=0; k<6; k++)
419  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];
420 
421  error[j] = mu02*vpMath::sqr(x) + mu20*vpMath::sqr(y) - 2*mu11*x*y
422  + 2*(mu11*yg-mu02*xg)*x + 2*(mu11*xg-mu20*yg)*y
423  + mu02*vpMath::sqr(xg) + mu20*vpMath::sqr(yg) - 2*mu11*xg*yg
424  + vpMath::sqr(mu11) - mu20*mu02;
425 
426  j++;
427  }
428  }
429 }
430 
unsigned int getRange() const
Definition: vpMe.h:236
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
static void displayEllipse(const vpImage< unsigned char > &I, const vpImagePoint &center, const double &coef1, const double &coef2, const double &coef3, bool use_centered_moments, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay.cpp:3608
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:199
unsigned int nbFeature
The number of moving edges.
vpPoint * p3
An other point on the plane containing the circle.
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
perspective projection of the circle
Definition: vpCircle.cpp:273
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Definition of the row vector class.
Definition: vpRowVector.h:73
vpMatrix L
The interaction matrix.
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
vpColVector error
The error vector.
double get_oY() const
Get the point Y coordinate in the object frame.
Definition: vpPoint.h:129
void buildFrom(const vpPoint &_p1, const vpPoint &_p2, const vpPoint &_p3, const double r)
vpMbtMeEllipse * meEllipse
Polygon describing the circle bbox.
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
#define vpDEBUG_ENABLE(level)
Definition: vpDebug.h:530
vpPoint * p2
A point on the plane containing the circle.
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
Class that defines what is a point.
Definition: vpPoint.h:65
void projection()
Definition: vpCircle.cpp:159
bool Reinit
Indicates if the circle has to be reinitialized.
bool isvisible
Indicates if the circle is visible or not.
static double sqr(double x)
Definition: vpMath.h:106
void trackMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
Generic class defining intrinsic camera parameters.
void computeInteractionMatrixError(const vpHomogeneousMatrix &cMo)
double get_oZ() const
Get the point Z coordinate in the object frame.
Definition: vpPoint.h:131
void updateMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
void reinitMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
vpCircle * circle
The circle to track.
static void convertEllipse(const vpCameraParameters &cam, const vpCircle &circle, vpImagePoint &center, double &mu20_p, double &mu11_p, double &mu02_p)
void displayMovingEdges(const vpImage< unsigned char > &I)
bool initMovingEdge(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo)
double get_oX() const
Get the point X coordinate in the object frame.
Definition: vpPoint.h:127
double getB() const
Definition: vpPlane.h:117
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
compute the interaction matrix from a subset a the possible features
double getA() const
Definition: vpPlane.h:115
double getC() const
Definition: vpPlane.h:119
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:67
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
double radius
The radius of the circle.
Class that defines what is a circle.
Definition: vpCircle.h:61
vpPoint * p1
The center of the circle.
vpColVector p
Definition: vpTracker.h:78
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor col, const unsigned int thickness=1, const bool displayFullModel=false)
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:98