Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpLine.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Line feature.
33  *
34  * Authors:
35  * Eric Marchand
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 
58 {
59  oP.resize(8);
60  cP.resize(8);
61  p.resize(2);
62 }
63 
68 
85 void vpLine::setWorldCoordinates(const double &A1, const double &B1, const double &C1, const double &D1,
86  const double &A2, const double &B2, const double &C2, const double &D2)
87 {
88  oP[0] = A1;
89  oP[1] = B1;
90  oP[2] = C1;
91  oP[3] = D1;
92 
93  oP[4] = A2;
94  oP[5] = B2;
95  oP[6] = C2;
96  oP[7] = D2;
97 }
98 
118 {
119  if (oP_.getRows() != 8)
120  throw vpException(vpException::dimensionError, "Size of oP is not equal to 8 as it should be");
121 
122  this->oP = oP_;
123 }
124 
147 {
148  if (oP1.getRows() != 4)
149  throw vpException(vpException::dimensionError, "Size of oP1 is not equal to 4 as it should be");
150 
151  if (oP2.getRows() != 4)
152  throw vpException(vpException::dimensionError, "Size of oP2 is not equal to 4 as it should be");
153 
154  for (unsigned int i = 0; i < 4; i++) {
155  oP[i] = oP1[i];
156  oP[i + 4] = oP2[i];
157  }
158 }
159 
194 {
195  projection(cP, p);
196 }
197 
217 {
218  // projection
219 
220  if (cP.getRows() != 8)
221  throw vpException(vpException::dimensionError, "Size of cP is not equal to 8 as it should be");
222 
223  double A1, A2, B1, B2, C1, C2, D1, D2;
224 
225  A1 = cP_[0];
226  B1 = cP_[1];
227  C1 = cP_[2];
228  D1 = cP_[3];
229 
230  A2 = cP_[4];
231  B2 = cP_[5];
232  C2 = cP_[6];
233  D2 = cP_[7];
234 
235  double a, b, c, s;
236  a = A2 * D1 - A1 * D2;
237  b = B2 * D1 - B1 * D2;
238  c = C2 * D1 - C1 * D2;
239  s = a * a + b * b;
240  if (s <= 1e-8) // seuil pas terrible
241  {
242  printf("Degenerate case: the image of the straight line is a point!\n");
243  throw vpException(vpException::fatalError, "Degenerate case: the image of the straight line is a point!");
244  }
245  s = 1.0 / sqrt(s);
246 
247  double rho = -c * s;
248  double theta = atan2(b, a);
249 
250  if (p.getRows() != 2)
251  p.resize(2);
252 
253  p_[0] = rho;
254  p_[1] = theta;
255 }
256 
294 
337 {
338 
339  double a1, a2, b1, b2, c1, c2, d1, d2;
340  double A1, A2, B1, B2, C1, C2, D1, D2;
341 
342  // in case of verification
343  // double x,y,z,ap1,ap2,bp1,bp2,cp1,cp2,dp1,dp2;
344 
345  if (cP.getRows() != 8)
346  cP.resize(8);
347 
348  a1 = oP[0];
349  b1 = oP[1];
350  c1 = oP[2];
351  d1 = oP[3];
352 
353  a2 = oP[4];
354  b2 = oP[5];
355  c2 = oP[6];
356  d2 = oP[7];
357 
358  A1 = cMo[0][0] * a1 + cMo[0][1] * b1 + cMo[0][2] * c1;
359  B1 = cMo[1][0] * a1 + cMo[1][1] * b1 + cMo[1][2] * c1;
360  C1 = cMo[2][0] * a1 + cMo[2][1] * b1 + cMo[2][2] * c1;
361  D1 = d1 - (cMo[0][3] * A1 + cMo[1][3] * B1 + cMo[2][3] * C1);
362 
363  A2 = cMo[0][0] * a2 + cMo[0][1] * b2 + cMo[0][2] * c2;
364  B2 = cMo[1][0] * a2 + cMo[1][1] * b2 + cMo[1][2] * c2;
365  C2 = cMo[2][0] * a2 + cMo[2][1] * b2 + cMo[2][2] * c2;
366  D2 = d2 - (cMo[0][3] * A2 + cMo[1][3] * B2 + cMo[2][3] * C2);
367 
368  // in case of verification
369  // ap1 = A1; bp1 = B1; cp1 = C1; dp1 = D1;
370  // ap2 = A2; bp2 = B2; cp2 = C2; dp2 = D2;
371 
372  // vpERROR_TRACE("A1 B1 C1 D1 %f %f %f %f ", A1, B1, C1, D1) ;
373  // vpERROR_TRACE("A2 B2 C2 D2 %f %f %f %f ", A2, B2, C2, D2) ;
374 
375  // Adding constraints on the straight line to have a unique representation
376 
377  // direction of the straight line = N1 x N2
378  a2 = B1 * C2 - C1 * B2;
379  b2 = C1 * A2 - A1 * C2;
380  c2 = A1 * B2 - B1 * A2;
381 
382  // Constraint D1 = 0 (the origin belongs to P1)
383  a1 = A2 * D1 - A1 * D2;
384  b1 = B2 * D1 - B1 * D2;
385  c1 = C2 * D1 - C1 * D2;
386 
387  if (fabs(D2) < fabs(D1)) // to be sure that D2 <> 0
388  {
389  A2 = A1;
390  B2 = B1;
391  C2 = C1;
392  D2 = D1;
393  }
394 
395  // Constraint A1^2 + B1^2 + C1^2 = 1
396  d1 = 1.0 / sqrt(a1 * a1 + b1 * b1 + c1 * c1);
397  cP_[0] = A1 = a1 * d1;
398  cP_[1] = B1 = b1 * d1;
399  cP_[2] = C1 = c1 * d1;
400  cP_[3] = 0;
401 
402  // Constraint A1 A2 + B1 B2 + C1 C2 = 0 (P2 orthogonal to P1)
403  // N2_new = (N1 x N2) x N1_new
404  a1 = b2 * C1 - c2 * B1;
405  b1 = c2 * A1 - a2 * C1;
406  c1 = a2 * B1 - b2 * A1;
407 
408  // Constraint A2^2 + B2^2 + C2^2 = 1
409  d1 = 1.0 / sqrt(a1 * a1 + b1 * b1 + c1 * c1);
410  a1 *= d1;
411  b1 *= d1;
412  c1 *= d1;
413 
414  // D2_new = D2 / (N2^T . N2_new)
415  D2 /= (A2 * a1 + B2 * b1 + C2 * c1);
416  A2 = a1;
417  B2 = b1;
418  C2 = c1;
419 
420  // Constraint D2 < 0
421  if (D2 > 0) {
422  A2 = -A2;
423  B2 = -B2;
424  C2 = -C2;
425  D2 = -D2;
426  }
427  // vpERROR_TRACE("A1 B1 C1 D1 %f %f %f %f ", A1, B1, C1, D1) ;
428  // vpERROR_TRACE("A2 B2 C2 D2 %f %f %f %f ", A2, B2, C2, D2) ;
429 
430  cP_[4] = A2;
431  cP_[5] = B2;
432  cP_[6] = C2;
433  cP_[7] = D2;
434 
435  // in case of verification
436  /*
437  x = -A2*D2;
438  y = -B2*D2;
439  z = -C2*D2;
440  d1 = ap1*x+bp1*y+cp1*z+dp1;
441  d2 = ap2*x+bp2*y+cp2*z+dp2;
442  if ((fabs(d1) > 1e-8) || (fabs(d2) > 1e-8))
443  {
444  printf("PB in VPline: P1 : 0 = %lf, P2: 0 = %lf\n",d1,d2);
445  exit(-1);
446  }
447  d1 = A1*x+B1*y+C1*z+D1;
448  d2 = A2*x+B2*y+C2*z+D2;
449  if ((fabs(d1) > 1e-8) || (fabs(d2) > 1e-8))
450  {
451  printf("PB in VPline: Pn1 : 0 = %lf, Pn2: 0 = %lf\n",d1,d2);
452  exit(-1);
453  }
454  */
455 }
456 
473 void vpLine::display(const vpImage<unsigned char> &I, const vpCameraParameters &cam, const vpColor &color,
474  const unsigned int thickness)
475 {
476  vpFeatureDisplay::displayLine(p[0], p[1], cam, I, color, thickness);
477 }
478 
499 // non destructive wrt. cP and p
501  const vpColor &color, const unsigned int thickness)
502 {
503  vpColVector _cP, _p;
504  changeFrame(cMo, _cP);
505  try {
506  projection(_cP, _p);
507  vpFeatureDisplay::displayLine(_p[0], _p[1], cam, I, color, thickness);
508  }
509  catch(const vpException &e) {
510  // Skip potential exception: due to a degenerate case: the image of the straight line is a point!
511  }
512 }
513 
525 {
526  vpLine *feature = new vpLine(*this);
527  return feature;
528 }
void init()
Definition: vpLine.cpp:57
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:85
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
error that can be emited by ViSP classes.
Definition: vpException.h:71
vpColVector cP
Definition: vpTracker.h:75
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:67
unsigned int getRows() const
Definition: vpArray2D.h:156
vpLine * duplicate() const
Definition: vpLine.cpp:524
void projection()
Definition: vpLine.cpp:193
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
Definition: vpLine.cpp:336
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:473
vpColVector p
Definition: vpTracker.h:71
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:244