ViSP  2.8.0
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 - 2013 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  if (vpFeatureSegment::selectAlpha() & select ){
288  // We recall that xc_ contains xc/l, yc_ contains yc/l and l_ contains 1/l
289  vpMatrix Lalpha(1,6);
290  Lalpha[0][0] = -lambda1*sin_a_*l_ ;
291  Lalpha[0][1] = lambda1*cos_a_*l_ ;
292  Lalpha[0][2] = lambda1*(xc_*sin_a_-yc_*cos_a_);
293  Lalpha[0][3] = (-xc_*sin_a_*sin_a_+yc_*cos_a_*sin_a_)/l_;
294  Lalpha[0][4] = (xc_*cos_a_*sin_a_ - yc_*cos_a_*cos_a_)/l_ ;
295  Lalpha[0][5] = -1 ;
296  L = vpMatrix::stackMatrices(L,Lalpha) ;
297  }
298  }
299  else
300  {
301  if (vpFeatureSegment::selectXc() & select ){
302  vpMatrix Lxc(1,6);
303  Lxc[0][0] = -lambda2 ;
304  Lxc[0][1] = 0. ;
305  Lxc[0][2] = lambda2*xc_ - lambda1*l_*cos_a_/4.;
306  Lxc[0][3] = xc_*yc_ + l_*l_*cos_a_*sin_a_/4. ;
307  Lxc[0][4] = -(1+xc_*xc_+l_*l_*cos_a_*cos_a_/4.) ;
308  Lxc[0][5] = yc_ ;
309  L = vpMatrix::stackMatrices(L,Lxc) ;
310  }
311 
312  if (vpFeatureSegment::selectYc() & select ){
313  vpMatrix Lyc(1,6);
314  Lyc[0][0] = 0. ;
315  Lyc[0][1] = -lambda2 ;
316  Lyc[0][2] = lambda2*yc_ - lambda1*l_*sin_a_/4.;
317  Lyc[0][3] = 1+yc_*yc_+l_*l_*sin_a_*sin_a_/4. ;
318  Lyc[0][4] = -xc_*yc_-l_*l_*cos_a_*sin_a_/4. ;
319  Lyc[0][5] = -xc_ ;
320  L = vpMatrix::stackMatrices(L,Lyc) ;
321  }
322 
323  if (vpFeatureSegment::selectL() & select ){
324  vpMatrix Ll(1,6);
325  Ll[0][0] = lambda1*cos_a_ ;
326  Ll[0][1] = lambda1*sin_a_ ;
327  Ll[0][2] = lambda2*l_-lambda1*(xc_*cos_a_+yc_*sin_a_);
328  Ll[0][3] = l_*(xc_*cos_a_*sin_a_ + yc_*(1+sin_a_*sin_a_)) ;
329  Ll[0][4] = -l_*(xc_*(1+cos_a_*cos_a_)+yc_*cos_a_*sin_a_) ;
330  Ll[0][5] = 0 ;
331  L = vpMatrix::stackMatrices(L,Ll) ;
332  }
333  if (vpFeatureSegment::selectAlpha() & select ){
334  vpMatrix Lalpha(1,6);
335  Lalpha[0][0] = -lambda1*sin_a_/l_ ;
336  Lalpha[0][1] = lambda1*cos_a_/l_ ;
337  Lalpha[0][2] = lambda1*(xc_*sin_a_-yc_*cos_a_)/l_;
338  Lalpha[0][3] = -xc_*sin_a_*sin_a_+yc_*cos_a_*sin_a_;
339  Lalpha[0][4] = xc_*cos_a_*sin_a_ - yc_*cos_a_*cos_a_ ;
340  Lalpha[0][5] = -1 ;
341  L = vpMatrix::stackMatrices(L,Lalpha) ;
342  }
343  }
344 
345  return L ;
346 }
347 
373 vpFeatureSegment::error( const vpBasicFeature &s_star, const unsigned int select )
374 {
375  vpColVector e(0) ;
376 
377  if (vpFeatureSegment::selectXc() & select ){
378  vpColVector exc(1) ;
379  exc[0] = xc_-s_star[0];
380  e = vpMatrix::stackMatrices(e,exc) ;
381  }
382 
383  if (vpFeatureSegment::selectYc() & select ){
384  vpColVector eyc(1) ;
385  eyc[0] = yc_ - s_star[1];
386  e = vpMatrix::stackMatrices(e,eyc) ;
387  }
388 
389  if (vpFeatureSegment::selectL() & select ){
390  vpColVector eL(1) ;
391  eL[0] = l_ - s_star[2];
392  e = vpMatrix::stackMatrices(e,eL) ;
393  }
394 
395  if (vpFeatureSegment::selectAlpha() & select ){
396  vpColVector eAlpha(1) ;
397  eAlpha[0] = alpha_ - s_star[3];
398  while (eAlpha[0] < -M_PI) eAlpha[0] += 2*M_PI ;
399  while (eAlpha[0] > M_PI) eAlpha[0] -= 2*M_PI ;
400  e = vpMatrix::stackMatrices(e,eAlpha) ;
401  }
402  return e ;
403 }
404 
431 void
432 vpFeatureSegment::print( const unsigned int select ) const
433 {
434  std::cout <<"vpFeatureSegment: (";
435  if (vpFeatureSegment::selectXc() & select ) {
436  if (normalized_)
437  std::cout << "xn = ";
438  else
439  std::cout << "xc = ";
440  std::cout << s[0] << "; ";
441  }
442  if (vpFeatureSegment::selectYc() & select ) {
443  if (normalized_)
444  std::cout << "yn = ";
445  else
446  std::cout << "yc = ";
447  std::cout << s[1] << "; ";
448  }
449  if (vpFeatureSegment::selectL() & select ) {
450  if (normalized_)
451  std::cout << "ln = ";
452  else
453  std::cout << "l = ";
454  std::cout << s[2] << "; ";
455  }
456  if (vpFeatureSegment::selectAlpha() & select ) {
457  std::cout << "alpha = " << vpMath::deg(s[3]) << " deg";
458  }
459  std::cout << ")" << std::endl;
460 }
461 
473 {
474  vpFeatureSegment *feature;
475 
476  feature = new vpFeatureSegment(*this) ;
477  return feature ;
478 }
479 
491 void
493  const vpImage<unsigned char> & I,
494  const vpColor &color,
495  unsigned int thickness ) const
496 {
497  double l, x, y;
498  if (normalized_) {
499  l = 1./l_;
500  x = xc_ * l;
501  y = yc_ * l;
502  }
503  else {
504  l = l_;
505  x = xc_;
506  y = yc_;
507  }
508 
509  double x1 = x - (l/2.)*cos_a_;
510  double x2 = x + (l/2.)*cos_a_;
511 
512  double y1 = y - (l/2.)*sin_a_;
513  double y2 = y + (l/2.)*sin_a_;
514  vpImagePoint ip1,ip2;
515 
516  vpMeterPixelConversion::convertPoint(cam, x1, y1, ip1);
517  vpMeterPixelConversion::convertPoint(cam, x2, y2, ip2);
518  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
519  vpDisplay::displayCircle(I, ip1, 5, color, true);
520  vpDisplay::displayCircle(I, ip2, 5, vpColor::yellow, true);
521 }
522 
533 void
535  const vpImage<vpRGBa> & I,
536  const vpColor &color,
537  unsigned int thickness ) const
538 {
539  double l, x, y;
540  if (normalized_) {
541  l = 1./l_;
542  x = xc_ * l;
543  y = yc_ * l;
544  }
545  else {
546  l = l_;
547  x = xc_;
548  y = yc_;
549  }
550 
551  double x1 = x - (l/2.)*cos_a_;
552  double x2 = x + (l/2.)*cos_a_;
553 
554  double y1 = y - (l/2.)*sin_a_;
555  double y2 = y + (l/2.)*sin_a_;
556  vpImagePoint ip1,ip2;
557 
558  vpMeterPixelConversion::convertPoint(cam, x1, y1, ip1);
559  vpMeterPixelConversion::convertPoint(cam, x2, y2, ip2);
560  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
561  vpDisplay::displayCircle(I, ip1, 5,vpColor::cyan, true);
562  vpDisplay::displayCircle(I, ip2, 5,vpColor::yellow, true);
563 }
564 
583 void vpFeatureSegment::buildFrom(const double x1, const double y1, const double Z1,
584  const double x2, const double y2, const double Z2)
585 {
586  double l = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
587  double x_c = (x1+x2)/2.;
588  double y_c = (y1+y2)/2.;
589  double alpha = atan2(y1-y2,x1-x2);
590 
591  if (normalized_) {
592  setXc( x_c / l );
593  setYc( y_c / l );
594  setL( 1/l );
595  setAlpha( alpha );
596 
597  setZ1(Z1);
598  setZ2(Z2);
599  }
600  else {
601  setXc( x_c );
602  setYc( y_c );
603  setL( l );
604  setAlpha( alpha );
605 
606  setZ1(Z1);
607  setZ2(Z2);
608  }
609 }
610 
611 
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:125
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:176
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:2263
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:175
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