ViSP  2.6.2
vpFeatureSegment.cpp
1 /****************************************************************************
2  *
3  * $Id: vpFeatureThetaU.cpp 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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  *
34  * Description:
35  * Segment visual feature.
36  *
37  * Authors:
38  * Filip Novotny
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
44 #include <visp/vpBasicFeature.h>
45 #include <visp/vpFeatureSegment.h>
46 #include <visp/vpImagePoint.h>
47 #include <visp/vpMeterPixelConversion.h>
48 #include <visp/vpMath.h>
49 #include <visp/vpDisplay.h>
50 #include <cmath>
51 
52 // Exception
53 #include <visp/vpException.h>
54 #include <visp/vpMatrixException.h>
55 
56 // Debug trace
57 #include <visp/vpDebug.h>
58 
59 
72 void
74 {
75  //feature dimension
76  dim_s = 4 ;
77  nbParameters = 6;
78 
79  // memory allocation
80  s.resize(dim_s) ;
81  if (flags == NULL)
82  flags = new bool[nbParameters];
83  for (unsigned int i = 0; i < nbParameters; i++) flags[i] = false;
84 }
85 
94 {
95  this->normalized_ = normalized;
96 
97  init();
98 }
99 
198 vpMatrix
199 vpFeatureSegment::interaction( const unsigned int select )
200 {
201 
202  vpMatrix L ;
203  L.resize(0,6) ;
204 
206  {
207  for (unsigned int i = 0; i < nbParameters; i++)
208  {
209  if (flags[i] == false)
210  {
211  switch(i){
212  case 0:
213  vpTRACE("Warning !!! The interaction matrix is computed but xc was not set yet");
214  break;
215  case 1:
216  vpTRACE("Warning !!! The interaction matrix is computed but Yc was not set yet");
217  break;
218  case 2:
219  vpTRACE("Warning !!! The interaction matrix is computed but l was not set yet");
220  break;
221  case 3:
222  vpTRACE("Warning !!! The interaction matrix is computed but alpha was not set yet");
223  break;
224  case 4:
225  vpTRACE("Warning !!! The interaction matrix is computed but Z1 was not set yet");
226  break;
227  case 5:
228  vpTRACE("Warning !!! The interaction matrix is computed but Z2 was not set yet");
229  break;
230  default:
231  vpTRACE("Problem during the reading of the variable flags");
232  }
233  }
234  }
235  }
236 
237  //This version is a simplification
238  double lambda1 = (Z1_-Z2_)/(Z1_*Z2_); // -l * lambda
239  double lambda2 = (Z1_+Z2_)/(2*Z1_*Z2_); // 1/Zm
240 
241  if (normalized_)
242  {
243  // here var xc_ contains xc/l, yc contains yc/l and l contains 1/l
244  double xn = xc_;
245  double yn = yc_;
246  double ln = l_;
247  double lambda = -lambda1 * ln;
248  double Zn_inv = lambda2 * ln;
249  double lc = cos_a_ / ln;
250  double ls = sin_a_ / ln;
251  double xnalpha = xn*cos_a_+yn*sin_a_;
252  double lnc = cos_a_ * ln;
253  double lns = sin_a_ * ln;
254 
255  if (vpFeatureSegment::selectXc() & select ){
256  vpMatrix Lxn(1,6);
257  Lxn[0][0] = -Zn_inv + lambda * xn * cos_a_;
258  Lxn[0][1] = lambda * xn * sin_a_ ;
259  Lxn[0][2] = lambda1 * (xn*xnalpha - cos_a_ /4.);
260  Lxn[0][3] = sin_a_*cos_a_/4/ln - xn*xnalpha*sin_a_/ln;
261  Lxn[0][4] = -ln*(1.+lc*lc/4.) + xn*xnalpha*cos_a_/ln ;
262  Lxn[0][5] = yn ;
263  L = vpMatrix::stackMatrices(L, Lxn) ;
264  }
265 
266  if (vpFeatureSegment::selectYc() & select ){
267  vpMatrix Lyn(1,6);
268  Lyn[0][0] = lambda*yn*cos_a_ ;
269  Lyn[0][1] = -Zn_inv + lambda*yn*sin_a_ ;
270  Lyn[0][2] = lambda1 * (yn*xnalpha - sin_a_/4.);
271  Lyn[0][3] = ln*(1+ls*ls/4.)-yn*xnalpha*sin_a_/ln ;
272  Lyn[0][4] = -sin_a_*cos_a_/4/ln + yn*xnalpha*cos_a_/ln;
273  Lyn[0][5] = -xn ;
274  L = vpMatrix::stackMatrices(L, Lyn) ;
275  }
276 
277  if (vpFeatureSegment::selectL() & select ){
278  vpMatrix Lln(1,6);
279  Lln[0][0] = lambda * lnc ;
280  Lln[0][1] = lambda * lns ;
281  Lln[0][2] = -(Zn_inv + lambda*xnalpha);
282  Lln[0][3] = -yn-xnalpha*sin_a_ ;
283  Lln[0][4] = xn + xnalpha*cos_a_ ;
284  Lln[0][5] = 0 ;
285  L = vpMatrix::stackMatrices(L, Lln) ;
286  }
287 
288  }
289  else
290  {
291  if (vpFeatureSegment::selectXc() & select ){
292  vpMatrix Lxc(1,6);
293  Lxc[0][0] = -lambda2 ;
294  Lxc[0][1] = 0. ;
295  Lxc[0][2] = lambda2*xc_ - lambda1*l_*cos_a_/4.;
296  //Lxc[0][3] = xc_*xc_ + l_*l_*cos_a_*sin_a_/4. ; // orig bug
297  Lxc[0][3] = xc_*yc_ + l_*l_*cos_a_*sin_a_/4. ;
298  Lxc[0][4] = -(1+xc_*xc_+l_*l_*cos_a_*cos_a_/4.) ;
299  Lxc[0][5] = yc_ ;
300  L = vpMatrix::stackMatrices(L,Lxc) ;
301  }
302 
303  if (vpFeatureSegment::selectYc() & select ){
304  vpMatrix Lyc(1,6);
305  Lyc[0][0] = 0. ;
306  Lyc[0][1] = -lambda2 ;
307  Lyc[0][2] = lambda2*yc_ - lambda1*l_*sin_a_/4.;
308  Lyc[0][3] = 1+yc_*yc_+l_*l_*sin_a_*sin_a_/4. ;
309  Lyc[0][4] = -xc_*yc_-l_*l_*cos_a_*sin_a_/4. ;
310  Lyc[0][5] = -xc_ ;
311  L = vpMatrix::stackMatrices(L,Lyc) ;
312  }
313 
314  if (vpFeatureSegment::selectL() & select ){
315  vpMatrix Ll(1,6);
316  Ll[0][0] = lambda1*cos_a_ ;
317  Ll[0][1] = lambda1*sin_a_ ;
318  Ll[0][2] = lambda2*l_-lambda1*(xc_*cos_a_+yc_*sin_a_);
319  Ll[0][3] = l_*(xc_*cos_a_*sin_a_ + yc_*(1+sin_a_*sin_a_)) ;
320  Ll[0][4] = -l_*(xc_*(1+cos_a_*cos_a_)+yc_*cos_a_*sin_a_) ;
321  Ll[0][5] = 0 ;
322  L = vpMatrix::stackMatrices(L,Ll) ;
323  }
324  }
325  if (vpFeatureSegment::selectAlpha() & select ){
326  vpMatrix Lalpha(1,6);
327  Lalpha[0][0] = -lambda1*sin_a_/l_ ;
328  Lalpha[0][1] = lambda1*cos_a_/l_ ;
329  Lalpha[0][2] = lambda1*(xc_*sin_a_-yc_*cos_a_)/l_;
330  Lalpha[0][3] = -xc_*sin_a_*sin_a_+yc_*cos_a_*sin_a_;
331  Lalpha[0][4] = xc_*cos_a_*sin_a_ - yc_*cos_a_*cos_a_ ;
332  Lalpha[0][5] = -1 ;
333  L = vpMatrix::stackMatrices(L,Lalpha) ;
334  }
335 
336  return L ;
337 }
338 
364 vpFeatureSegment::error( const vpBasicFeature &s_star, const unsigned int select )
365 {
366  vpColVector e(0) ;
367 
368  if (vpFeatureSegment::selectXc() & select ){
369  vpColVector exc(1) ;
370  exc[0] = xc_-s_star[0];
371  e = vpMatrix::stackMatrices(e,exc) ;
372  }
373 
374  if (vpFeatureSegment::selectYc() & select ){
375  vpColVector eyc(1) ;
376  eyc[0] = yc_ - s_star[1];
377  e = vpMatrix::stackMatrices(e,eyc) ;
378  }
379 
380  if (vpFeatureSegment::selectL() & select ){
381  vpColVector eL(1) ;
382  eL[0] = l_ - s_star[2];
383  e = vpMatrix::stackMatrices(e,eL) ;
384  }
385 
386  if (vpFeatureSegment::selectAlpha() & select ){
387  vpColVector eAlpha(1) ;
388  eAlpha[0] = alpha_ - s_star[3];
389  while (eAlpha[0] < -M_PI) eAlpha[0] += 2*M_PI ;
390  while (eAlpha[0] > M_PI) eAlpha[0] -= 2*M_PI ;
391  e = vpMatrix::stackMatrices(e,eAlpha) ;
392  }
393  return e ;
394 }
395 
422 void
423 vpFeatureSegment::print( const unsigned int select ) const
424 {
425  std::cout <<"vpFeatureSegment: (";
426  if (vpFeatureSegment::selectXc() & select ) {
427  if (normalized_)
428  std::cout << "xn = ";
429  else
430  std::cout << "xc = ";
431  std::cout << s[0] << "; ";
432  }
433  if (vpFeatureSegment::selectYc() & select ) {
434  if (normalized_)
435  std::cout << "yn = ";
436  else
437  std::cout << "yc = ";
438  std::cout << s[1] << "; ";
439  }
440  if (vpFeatureSegment::selectL() & select ) {
441  if (normalized_)
442  std::cout << "ln = ";
443  else
444  std::cout << "l = ";
445  std::cout << s[2] << "; ";
446  }
447  if (vpFeatureSegment::selectAlpha() & select ) {
448  std::cout << "alpha = " << vpMath::deg(s[3]) << " deg";
449  }
450  std::cout << ")" << std::endl;
451 }
452 
464 {
465  vpFeatureSegment *feature;
466 
467  feature = new vpFeatureSegment(*this) ;
468  return feature ;
469 }
470 
482 void
484  const vpImage<unsigned char> & I,
485  const vpColor &color,
486  unsigned int thickness ) const
487 {
488  double l, x, y;
489  if (normalized_) {
490  l = 1./l_;
491  x = xc_ * l;
492  y = yc_ * l;
493  }
494  else {
495  l = l_;
496  x = xc_;
497  y = yc_;
498  }
499 
500  double x1 = x - (l/2.)*cos_a_;
501  double x2 = x + (l/2.)*cos_a_;
502 
503  double y1 = y - (l/2.)*sin_a_;
504  double y2 = y + (l/2.)*sin_a_;
505  vpImagePoint ip1,ip2;
506 
507  vpMeterPixelConversion::convertPoint(cam, x1, y1, ip1);
508  vpMeterPixelConversion::convertPoint(cam, x2, y2, ip2);
509  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
510  vpDisplay::displayCircle(I, ip1, 5, color, true);
511  vpDisplay::displayCircle(I, ip2, 5, vpColor::yellow, true);
512 }
513 
524 void
526  const vpImage<vpRGBa> & I,
527  const vpColor &color,
528  unsigned int thickness ) const
529 {
530  double l, x, y;
531  if (normalized_) {
532  l = 1./l_;
533  x = xc_ * l;
534  y = yc_ * l;
535  }
536  else {
537  l = l_;
538  x = xc_;
539  y = yc_;
540  }
541 
542  double x1 = x - (l/2.)*cos_a_;
543  double x2 = x + (l/2.)*cos_a_;
544 
545  double y1 = y - (l/2.)*sin_a_;
546  double y2 = y + (l/2.)*sin_a_;
547  vpImagePoint ip1,ip2;
548 
549  vpMeterPixelConversion::convertPoint(cam, x1, y1, ip1);
550  vpMeterPixelConversion::convertPoint(cam, x2, y2, ip2);
551  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
552  vpDisplay::displayCircle(I, ip1, 5,vpColor::cyan, true);
553  vpDisplay::displayCircle(I, ip2, 5,vpColor::yellow, true);
554 }
555 
574 void vpFeatureSegment::buildFrom(const double x1, const double y1, const double Z1,
575  const double x2, const double y2, const double Z2)
576 {
577  double l = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
578  double x_c = (x1+x2)/2.;
579  double y_c = (y1+y2)/2.;
580  double alpha = atan2(y1-y2,x1-x2);
581 
582  if (normalized_) {
583  setXc( x_c / l );
584  setYc( y_c / l );
585  setL( 1/l );
586  setAlpha( alpha );
587 
588  setZ1(Z1);
589  setZ2(Z2);
590  }
591  else {
592  setXc( x_c );
593  setYc( y_c );
594  setL( l );
595  setAlpha( alpha );
596 
597  setZ1(Z1);
598  setZ2(Z2);
599  }
600 }
601 
602 
virtual void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
bool * flags
Ensure that all the parameters needed to compute the iteraction matrix are set.
static unsigned int selectL()
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:174
void setAlpha(const double val)
#define vpTRACE
Definition: vpDebug.h:401
static unsigned int selectYc()
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
static unsigned int selectAlpha()
unsigned int dim_s
Dimension of the visual feature.
void setL(const double val)
void setZ1(const double val)
class that defines what is a visual feature
static const vpColor cyan
Definition: vpColor.h:174
void setZ2(const double val)
Class that defines a 2D segment visual features. This class allow to consider two sets of visual feat...
Generic class defining intrinsic camera parameters.
vpFeatureSegment(bool normalized=false)
void setYc(const double val)
static vpMatrix stackMatrices(const vpMatrix &A, const vpMatrix &B)
Stack two Matrices C = [ A B ]^T.
Definition: vpMatrix.cpp:2261
void buildFrom(const double x1, const double y1, const double Z1, const double x2, const double y2, const double Z2)
vpBasicFeatureDeallocatorType deallocate
static double deg(double rad)
Definition: vpMath.h:93
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
static const vpColor yellow
Definition: vpColor.h:173
vpFeatureSegment * duplicate() const
Feature duplication.
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
static unsigned int selectXc()
vpColVector s
State of the visual feature.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94
void setXc(const double val)
void print(const unsigned int select=FEATURE_ALL) const