Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpLine.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  * Line feature.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
39 #include <visp3/core/vpLine.h>
40 
41 #include <visp3/core/vpDebug.h>
42 #include <visp3/core/vpMath.h>
43 
44 #include <visp3/core/vpFeatureDisplay.h>
45 
59 void
61 {
62  oP.resize(8) ;
63  cP.resize(8) ;
64  p.resize(2) ;
65 }
66 
71 {
72  init() ;
73 }
74 
75 
76 
93 void
94 vpLine::setWorldCoordinates(const double &A1, const double &B1,
95  const double &C1, const double &D1,
96  const double &A2, const double &B2,
97  const double &C2, const double &D2)
98 {
99  oP[0] = A1 ;
100  oP[1] = B1 ;
101  oP[2] = C1 ;
102  oP[3] = D1 ;
103 
104  oP[4] = A2 ;
105  oP[5] = B2 ;
106  oP[6] = C2 ;
107  oP[7] = D2 ;
108 }
109 
110 
129 void
131 {
132  if (oP_.getRows() != 8)
133  throw vpException(vpException::dimensionError, "Size of oP is not equal to 8 as it should be");
134 
135  this->oP = oP_ ;
136 }
137 
138 
160 void
162  const vpColVector &oP2)
163 {
164  if (oP1.getRows() != 4)
165  throw vpException(vpException::dimensionError, "Size of oP1 is not equal to 4 as it should be");
166 
167  if (oP2.getRows() != 4)
168  throw vpException(vpException::dimensionError, "Size of oP2 is not equal to 4 as it should be");
169 
170  for (unsigned int i=0 ; i < 4 ; i++)
171  {
172  oP[i] = oP1[i] ;
173  oP[i+4] = oP2[i] ;
174  }
175 
176 }
177 
178 
211 void
213 {
214  projection(cP,p) ;
215 }
216 
217 
235 void
237 {
238  //projection
239 
240  if (cP.getRows() != 8)
241  throw vpException(vpException::dimensionError, "Size of cP is not equal to 8 as it should be");
242 
243  double A1, A2, B1, B2, C1, C2, D1, D2;
244 
245  A1=cP_[0] ;
246  B1=cP_[1] ;
247  C1=cP_[2] ;
248  D1=cP_[3] ;
249 
250  A2=cP_[4] ;
251  B2=cP_[5] ;
252  C2=cP_[6] ;
253  D2=cP_[7] ;
254 
255  double a, b, c, s;
256  a = A2*D1 - A1*D2;
257  b = B2*D1 - B1*D2;
258  c = C2*D1 - C1*D2;
259  s = a*a+b*b;
260  if (s <= 1e-8) // seuil pas terrible
261  {
262  printf("Degenerate case: the image of the straight line is a point!\n");
263  throw vpException(vpException::fatalError, "Degenerate case: the image of the straight line is a point!");
264  }
265  s = 1.0/sqrt(s);
266 
267  double rho = -c*s ;
268  double theta = atan2( b, a);
269 
270  if (p.getRows() != 2)
271  p.resize(2);
272 
273  p_[0] = rho ;
274  p_[1] = theta ;
275 }
276 
277 
313 void
315 {
316  changeFrame(cMo,cP) ;
317 }
318 
319 
361 void
363 {
364 
365  double a1, a2, b1, b2, c1, c2, d1, d2;
366  double A1, A2, B1, B2, C1, C2, D1, D2;
367 
368  // in case of verification
369  // double x,y,z,ap1,ap2,bp1,bp2,cp1,cp2,dp1,dp2;
370 
371  if (cP.getRows() != 8)
372  cP.resize(8);
373 
374  a1=oP[0] ;
375  b1=oP[1] ;
376  c1=oP[2] ;
377  d1=oP[3] ;
378 
379  a2=oP[4] ;
380  b2=oP[5] ;
381  c2=oP[6] ;
382  d2=oP[7] ;
383 
384  A1 = cMo[0][0]*a1 + cMo[0][1]*b1 + cMo[0][2]*c1;
385  B1 = cMo[1][0]*a1 + cMo[1][1]*b1 + cMo[1][2]*c1;
386  C1 = cMo[2][0]*a1 + cMo[2][1]*b1 + cMo[2][2]*c1;
387  D1 = d1 - (cMo[0][3]*A1 + cMo[1][3]*B1 + cMo[2][3]*C1);
388 
389  A2 = cMo[0][0]*a2 + cMo[0][1]*b2 + cMo[0][2]*c2;
390  B2 = cMo[1][0]*a2 + cMo[1][1]*b2 + cMo[1][2]*c2;
391  C2 = cMo[2][0]*a2 + cMo[2][1]*b2 + cMo[2][2]*c2;
392  D2 = d2 - (cMo[0][3]*A2 + cMo[1][3]*B2 + cMo[2][3]*C2);
393 
394  // in case of verification
395  // ap1 = A1; bp1 = B1; cp1 = C1; dp1 = D1;
396  // ap2 = A2; bp2 = B2; cp2 = C2; dp2 = D2;
397 
398  // vpERROR_TRACE("A1 B1 C1 D1 %f %f %f %f ", A1, B1, C1, D1) ;
399  // vpERROR_TRACE("A2 B2 C2 D2 %f %f %f %f ", A2, B2, C2, D2) ;
400 
401  // Adding constraints on the straight line to have a unique representation
402 
403  // direction of the straight line = N1 x N2
404  a2 = B1*C2 - C1*B2;
405  b2 = C1*A2 - A1*C2;
406  c2 = A1*B2 - B1*A2;
407 
408  // Constraint D1 = 0 (the origin belongs to P1)
409  a1 = A2*D1 - A1*D2;
410  b1 = B2*D1 - B1*D2;
411  c1 = C2*D1 - C1*D2;
412 
413  if (fabs(D2) < fabs(D1)) // to be sure that D2 <> 0
414  {
415  A2 = A1;
416  B2 = B1;
417  C2 = C1;
418  D2 = D1;
419  }
420 
421  // Constraint A1^2 + B1^2 + C1^2 = 1
422  d1 = 1.0/sqrt(a1*a1 + b1*b1 + c1*c1);
423  cP_[0] = A1 = a1*d1 ;
424  cP_[1] = B1 = b1*d1 ;
425  cP_[2] = C1 = c1*d1 ;
426  cP_[3] = 0 ;
427 
428  // Constraint A1 A2 + B1 B2 + C1 C2 = 0 (P2 orthogonal to P1)
429  // N2_new = (N1 x N2) x N1_new
430  a1 = b2*C1 - c2*B1;
431  b1 = c2*A1 - a2*C1;
432  c1 = a2*B1 - b2*A1;
433 
434  // Constraint A2^2 + B2^2 + C2^2 = 1
435  d1 = 1.0/sqrt(a1*a1 + b1*b1 + c1*c1);
436  a1 *= d1 ;
437  b1 *= d1 ;
438  c1 *= d1 ;
439 
440  // D2_new = D2 / (N2^T . N2_new)
441  D2 /= (A2*a1 + B2*b1 + C2*c1);
442  A2 = a1;
443  B2 = b1;
444  C2 = c1;
445 
446  // Constraint D2 < 0
447  if (D2 > 0)
448  {
449  A2 = -A2;
450  B2 = -B2;
451  C2 = -C2;
452  D2 = -D2;
453  }
454  // vpERROR_TRACE("A1 B1 C1 D1 %f %f %f %f ", A1, B1, C1, D1) ;
455  // vpERROR_TRACE("A2 B2 C2 D2 %f %f %f %f ", A2, B2, C2, D2) ;
456 
457  cP_[4] = A2;
458  cP_[5] = B2;
459  cP_[6] = C2;
460  cP_[7] = D2;
461 
462  // in case of verification
463  /*
464  x = -A2*D2;
465  y = -B2*D2;
466  z = -C2*D2;
467  d1 = ap1*x+bp1*y+cp1*z+dp1;
468  d2 = ap2*x+bp2*y+cp2*z+dp2;
469  if ((fabs(d1) > 1e-8) || (fabs(d2) > 1e-8))
470  {
471  printf("PB in VPline: P1 : 0 = %lf, P2: 0 = %lf\n",d1,d2);
472  exit(-1);
473  }
474  d1 = A1*x+B1*y+C1*z+D1;
475  d2 = A2*x+B2*y+C2*z+D2;
476  if ((fabs(d1) > 1e-8) || (fabs(d2) > 1e-8))
477  {
478  printf("PB in VPline: Pn1 : 0 = %lf, Pn2: 0 = %lf\n",d1,d2);
479  exit(-1);
480  }
481  */
482 
483 }
484 
485 
486 
504  const vpCameraParameters &cam,
505  const vpColor &color,
506  const unsigned int thickness)
507 {
508  vpFeatureDisplay::displayLine(p[0], p[1], cam, I, color, thickness) ;
509 }
510 
511 
532 // non destructive wrt. cP and p
534  const vpHomogeneousMatrix &cMo,
535  const vpCameraParameters &cam,
536  const vpColor &color,
537  const unsigned int thickness)
538 {
539  vpColVector _cP, _p ;
540  changeFrame(cMo,_cP) ;
541  projection(_cP,_p) ;
542  vpFeatureDisplay::displayLine(_p[0],_p[1],
543  cam, I, color, thickness) ;
544 
545 }
546 
547 
559 {
560  vpLine *feature = new vpLine(*this) ;
561  return feature ;
562 }
563 
564 /*
565  * Local variables:
566  * c-basic-offset: 2
567  * End:
568  */
void init()
Definition: vpLine.cpp:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
void setWorldCoordinates(const double &A1, const double &B1, const double &C1, const double &D1, const double &A2, const double &B2, const double &C2, const double &D2)
Definition: vpLine.cpp:94
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
error that can be emited by ViSP classes.
Definition: vpException.h:73
vpColVector cP
Definition: vpTracker.h:77
Class that defines a line in the object frame, the camera frame and the image plane. All the parameters must be set in meter.
Definition: vpLine.h:105
Generic class defining intrinsic camera parameters.
vpLine()
Definition: vpLine.cpp:70
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
vpLine * duplicate() const
Definition: vpLine.cpp:558
void projection()
Definition: vpLine.cpp:212
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
Definition: vpLine.cpp:362
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)
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, const unsigned int thickness=1)
Definition: vpLine.cpp:503
vpColVector p
Definition: vpTracker.h:73
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:225