Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpFeatureLine.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * 2D line visual feature.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
45 #include <visp3/visual_features/vpBasicFeature.h>
46 #include <visp3/visual_features/vpFeatureLine.h>
47 
48 // Exception
49 #include <visp3/core/vpException.h>
50 #include <visp3/visual_features/vpFeatureException.h>
51 
52 // Debug trace
53 #include <visp3/core/vpDebug.h>
54 
55 // simple math function (round)
56 #include <visp3/core/vpMath.h>
57 
58 // Display Issue
59 
60 // Meter/pixel conversion
61 #include <visp3/core/vpCameraParameters.h>
62 
63 //Color / image / display
64 #include <visp3/core/vpColor.h>
65 #include <visp3/core/vpImage.h>
66 
67 
68 
69 #include <visp3/core/vpFeatureDisplay.h>
70 
71 
72 /*
73 
74 
75 
76 attributes and members directly related to the vpBasicFeature needs
77 other functionalities ar useful but not mandatory
78 
79 
80 
81 
82 
83 */
84 
88 void
90 {
91  //feature dimension
92  dim_s = 2 ;
93  nbParameters = 6;
94 
95  // memory allocation
96  // x cos(theta) + y sin(theta) - rho = 0
97  // s[0] = rho
98  // s[1] = theta
99  s.resize(dim_s) ;
100  if (flags == NULL)
101  flags = new bool[nbParameters];
102  for (unsigned int i = 0; i < nbParameters; i++) flags[i] = false;
103 
104  A = B = C = D = 0.0 ;
105 }
106 
107 
111 vpFeatureLine::vpFeatureLine() : A(0), B(0), C(0), D(0)
112 {
113  init() ;
114 }
115 
116 
123 void
124 vpFeatureLine::setRhoTheta(const double rho, const double theta)
125 {
126  s[0] = rho ;
127  s[1] = theta ;
128  for( int i = 0; i < 2; i++) flags[i] = true;
129 }
130 
131 
142 void
143 vpFeatureLine::setABCD(const double A_, const double B_,
144  const double C_, const double D_)
145 {
146  this->A = A_ ;
147  this->B = B_ ;
148  this->C = C_ ;
149  this->D = D_ ;
150  for(unsigned int i = 2; i < nbParameters; i++) flags[i] = true;
151 }
152 
153 
202 vpMatrix
203 vpFeatureLine::interaction(const unsigned int select)
204 {
205  vpMatrix L ;
206 
207  L.resize(0,6) ;
208 
210  {
211  for (unsigned int i = 0; i < nbParameters; i++)
212  {
213  if (flags[i] == false)
214  {
215  switch(i){
216  case 0:
217  vpTRACE("Warning !!! The interaction matrix is computed but rho was not set yet");
218  break;
219  case 1:
220  vpTRACE("Warning !!! The interaction matrix is computed but theta was not set yet");
221  break;
222  case 2:
223  vpTRACE("Warning !!! The interaction matrix is computed but A was not set yet");
224  break;
225  case 3:
226  vpTRACE("Warning !!! The interaction matrix is computed but B was not set yet");
227  break;
228  case 4:
229  vpTRACE("Warning !!! The interaction matrix is computed but C was not set yet");
230  break;
231  case 5:
232  vpTRACE("Warning !!! The interaction matrix is computed but D was not set yet");
233  break;
234  default:
235  vpTRACE("Problem during the reading of the variable flags");
236  }
237  }
238  }
239  resetFlags();
240  }
241  double rho = s[0] ;
242  double theta = s[1] ;
243 
244 
245  double co = cos(theta);
246  double si = sin(theta);
247 
248  if (fabs(D) < 1e-6)
249  {
250  vpERROR_TRACE("Incorrect plane coordinates D is null, D = %f",D) ;
251 
253  "Incorrect plane coordinates D")) ;
254  }
255 
256  double lambda_theta =( A*si - B*co) /D;
257  double lambda_rho = (C + rho*A*co + rho*B*si)/D;
258 
259  if (vpFeatureLine::selectRho() & select )
260  {
261  vpMatrix Lrho(1,6) ;
262 
263 
264  Lrho[0][0]= co*lambda_rho;
265  Lrho[0][1]= si*lambda_rho;
266  Lrho[0][2]= -rho*lambda_rho;
267  Lrho[0][3]= si*(1.0 + rho*rho);
268  Lrho[0][4]= -co*(1.0 + rho*rho);
269  Lrho[0][5]= 0.0;
270 
271  L = vpMatrix::stack(L,Lrho) ;
272  }
273 
274  if (vpFeatureLine::selectTheta() & select )
275  {
276  vpMatrix Ltheta(1,6) ;
277 
278  Ltheta[0][0] = co*lambda_theta;
279  Ltheta[0][1] = si*lambda_theta;
280  Ltheta[0][2] = -rho*lambda_theta;
281  Ltheta[0][3] = -rho*co;
282  Ltheta[0][4] = -rho*si;
283  Ltheta[0][5] = -1.0;
284 
285  L = vpMatrix::stack(L,Ltheta) ;
286  }
287  return L ;
288 }
289 
290 
331  const unsigned int select)
332 {
333  vpColVector e(0) ;
334 
335  try{
336  if (vpFeatureLine::selectRho() & select )
337  {
338  vpColVector erho(1) ;
339  erho[0] = s[0] - s_star[0] ;
340 
341  e = vpColVector::stack(e,erho) ;
342  }
343 
344  if (vpFeatureLine::selectTheta() & select )
345  {
346 
347  double err = s[1] - s_star[1] ;
348  while (err < -M_PI) err += 2*M_PI ;
349  while (err > M_PI) err -= 2*M_PI ;
350 
351  vpColVector etheta(1) ;
352  etheta[0] = err ;
353  e = vpColVector::stack(e,etheta) ;
354  }
355  }
356  catch(...) {
357  throw ;
358  }
359 
360  return e ;
361 }
362 
363 
384 void
385 vpFeatureLine::print(const unsigned int select ) const
386 {
387 
388  std::cout <<"Line:\t " << A <<"X+" << B <<"Y+" << C <<"Z +" << D <<"=0" <<std::endl ;;
389  if (vpFeatureLine::selectRho() & select )
390  std::cout << " \trho=" << s[0] ;
391  if (vpFeatureLine::selectTheta() & select )
392  std::cout << " \ttheta=" << s[1] ;
393  std::cout <<std::endl ;
394 }
395 
396 
411 void
412 vpFeatureLine::buildFrom(const double rho, const double theta)
413 {
414  s[0] = rho ;
415  s[1] = theta ;
416  for( int i = 0; i < 2; i++) flags[i] = true;
417 }
418 
419 
448 void vpFeatureLine::buildFrom(const double rho, const double theta,
449  const double A_, const double B_,
450  const double C_, const double D_)
451 {
452  s[0] = rho ;
453  s[1] = theta ;
454  this->A = A_ ;
455  this->B = B_ ;
456  this->C = C_ ;
457  this->D = D_ ;
458  for(unsigned int i = 0; i < nbParameters; i++) flags[i] = true;
459 }
460 
461 
473 {
474  vpFeatureLine *feature = new vpFeatureLine ;
475  return feature ;
476 }
477 
478 
479 
490 void
492  const vpImage<unsigned char> &I,
493  const vpColor &color,
494  unsigned int thickness) const
495 {
496  try{
497  double rho,theta ;
498  rho = getRho() ;
499  theta = getTheta() ;
500 
501  vpFeatureDisplay::displayLine(rho, theta, cam, I, color, thickness) ;
502 
503  }
504  catch(...)
505  {
506  vpERROR_TRACE("Error caught") ;
507  throw ;
508  }
509 }
510 
521 void
523  const vpImage<vpRGBa> &I,
524  const vpColor &color,
525  unsigned int thickness) const
526 {
527  try{
528  double rho,theta ;
529  rho = getRho() ;
530  theta = getTheta() ;
531 
532  vpFeatureDisplay::displayLine(rho, theta, cam, I, color, thickness) ;
533 
534  }
535  catch(...)
536  {
537  vpERROR_TRACE("Error caught") ;
538  throw ;
539  }
540 }
541 
558 unsigned int vpFeatureLine::selectRho() { return FEATURE_LINE[0] ; }
559 
576 unsigned int vpFeatureLine::selectTheta() { return FEATURE_LINE[1] ; }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
bool * flags
Ensure that all the parameters needed to compute the iteraction matrix are set.
void setABCD(const double A, const double B, const double C, const double D)
static unsigned int selectRho()
void stack(const double &d)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Definition: vpArray2D.h:167
void print(const unsigned int select=FEATURE_ALL) const
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
void stack(const vpMatrix &A)
Definition: vpMatrix.cpp:2981
unsigned int dim_s
Dimension of the visual feature.
double getRho() const
double getTheta() const
class that defines what is a visual feature
void buildFrom(const double rho, const double theta)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
#define vpTRACE
Definition: vpDebug.h:414
Error that can be emited by the vpBasicFeature class and its derivates.
Generic class defining intrinsic camera parameters.
Class that defines a 2D line visual feature which is composed by two parameters that are and ...
vpFeatureLine * duplicate() const
vpMatrix interaction(const unsigned int select=FEATURE_ALL)
static const unsigned int FEATURE_LINE[32]
vpBasicFeatureDeallocatorType deallocate
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
static void displayLine(double rho, double theta, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
vpColVector error(const vpBasicFeature &s_star, const unsigned int select=FEATURE_ALL)
static unsigned int selectTheta()
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
vpColVector s
State of the visual feature.
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225
void setRhoTheta(const double rho, const double theta)