Visual Servoing Platform  version 3.3.1 under development (2020-10-25)
vpFeatureBuilderVanishingPoint.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  * Conversion between tracker and visual feature vanishing point.
33  *
34  * Authors:
35  * Odile Bourquardez
36  *
37  *****************************************************************************/
38 
44 #include <visp3/core/vpException.h>
45 #include <visp3/visual_features/vpFeatureBuilder.h>
46 #include <visp3/visual_features/vpFeatureException.h>
47 
64 void vpFeatureBuilder::create(vpFeatureVanishingPoint &s, const vpPoint &p, unsigned int select)
65 {
66  if ((vpFeatureVanishingPoint::selectX() & select) || (vpFeatureVanishingPoint::selectY() & select)) {
67  s.set_x(p.get_x());
68  s.set_y(p.get_y());
69  }
71  double x = p.get_x();
72  double y = p.get_y();
73 
74  s.setOneOverRho( 1. / sqrt(x * x + y * y) );
75  s.setAlpha( atan2(y, x) );
76  }
77 }
78 
98 void vpFeatureBuilder::create(vpFeatureVanishingPoint &s, const vpFeatureLine &L1, const vpFeatureLine &L2, unsigned int select)
99 {
100  if ((vpFeatureVanishingPoint::selectX() & select) || (vpFeatureVanishingPoint::selectY() & select)) {
101  double rho_l = L1.getRho();
102  double rho_r = L2.getRho();
103  double theta_l = L1.getTheta();
104  double theta_r = L2.getTheta();
105  double c_l = cos(theta_l);
106  double c_r = cos(theta_r);
107  double s_l = sin(theta_l);
108  double s_r = sin(theta_r);
109 
110  double min = 0.0001;
111  if (fabs(theta_r - theta_l) < min || fabs(fabs(theta_r - theta_l) - M_PI) < min ||
112  fabs(fabs(theta_r - theta_l) - 2 * M_PI) < min) {
113  vpCERROR << "There is no vanishing point : the lines are parallel in the "
114  "image plane"
115  << std::endl;
116  throw(vpFeatureException(vpFeatureException::badInitializationError, "There is no vanishing point : the lines are "
117  "parallel in the image plane"));
118  }
119 
120  double y = (rho_r * c_l - rho_l * c_r) / (-s_l * c_r + s_r * c_l);
121  double x = (rho_r * s_l - rho_l * s_r) / (-c_l * s_r + c_r * s_l);
122 
123  s.set_x(x);
124  s.set_y(y);
125  }
127  double rho_1 = L1.getRho();
128  double theta_1 = L1.getTheta();
129  double rho_2 = L2.getRho();
130  double theta_2 = L2.getTheta();
131 
132  double theta_diff = theta_1 - theta_2;
133 
134  double denom = sqrt( rho_1*rho_1 + rho_2*rho_2 - 2*rho_1*rho_2 * cos(theta_diff) );
135  double one_over_rho = sin(theta_diff) / denom;
136  double alpha = atan2( rho_1 * cos(theta_2) - rho_2 * cos(theta_1), rho_2*sin(theta_1) - rho_1 * sin(theta_2));
137 
138  s.setOneOverRho(one_over_rho);
139  s.setAlpha(alpha);
140  }
141 }
142 
163 void vpFeatureBuilder::create(vpFeatureVanishingPoint &s, const vpLine &L1, const vpLine &L2, unsigned int select)
164 {
165  vpFeatureLine l1, l2;
166  vpFeatureBuilder::create(l1, L1);
167  vpFeatureBuilder::create(l2, L2);
168 
169  vpFeatureBuilder::create(s, l1, l2, select);
170 }
171 
190  const vpImagePoint &line1_ip1, const vpImagePoint &line1_ip2,
191  const vpImagePoint &line2_ip1, const vpImagePoint &line2_ip2,
192  unsigned int select)
193 {
194  double x1 = 0, y1 = 0;
195  double x2 = 0, y2 = 0;
196 
197  // First line
198  vpPixelMeterConversion::convertPoint(cam, line1_ip1, x1, y1);
199  vpPixelMeterConversion::convertPoint(cam, line1_ip2, x2, y2);
200 
201  double theta_1 = atan2(-(x1-x2), y1 - y2);
202  double rho_1 = ( (x1+x2)*cos(theta_1) + (y1+y2)*sin(theta_1) ) / 2.;
203 
204  // Second line
205  vpPixelMeterConversion::convertPoint(cam, line2_ip1, x1, y1);
206  vpPixelMeterConversion::convertPoint(cam, line2_ip2, x2, y2);
207 
208  double theta_2 = atan2(-(x1-x2), y1 - y2);
209  double rho_2 = ( (x1+x2)*cos(theta_2) + (y1+y2)*sin(theta_2) ) / 2.;
210 
211  if ((vpFeatureVanishingPoint::selectX() & select) || (vpFeatureVanishingPoint::selectY() & select)) {
212  double min = 0.0001;
213  double theta_diff = theta_1 - theta_2;
214 
215  if (fabs(theta_diff) < min || fabs(fabs(theta_diff) - M_PI) < min || fabs(fabs(theta_diff) - 2 * M_PI) < min) {
216  throw(vpException(vpException::fatalError, "There is no vanishing point : the lines are parallel in the image plane"));
217  }
218 
219  double x = (sin(theta_1) * rho_2 - sin(theta_2)*rho_1 ) / sin(theta_diff);
220  double y = (cos(theta_2) * rho_1 - cos(theta_1)*rho_2 ) / sin(theta_diff);
221 
222  s.set_x(x);
223  s.set_y(y);
224  }
225  else if((vpFeatureVanishingPoint::selectOneOverRho() & select)) {
226  double theta_diff = theta_1 - theta_2;
227  double denom = sqrt( rho_1*rho_1 + rho_2*rho_2 - 2*rho_1*rho_2 * cos(theta_diff) );
228  double one_over_rho = sin(theta_diff) / denom;
229 // if (one_over_rho < 0) {
230 // one_over_rho = -one_over_rho;
231 // }
232  double alpha = atan2( rho_1 * cos(theta_2) - rho_2 * cos(theta_1), rho_2*sin(theta_1) - rho_1 * sin(theta_2));
233 
234  s.setOneOverRho(one_over_rho);
235  s.setAlpha(alpha);
236  }
237  else if((vpFeatureVanishingPoint::selectAtanOneOverRho() & select)) {
238  double theta_diff = theta_1 - theta_2;
239  double denom = sqrt( rho_1*rho_1 + rho_2*rho_2 - 2*rho_1*rho_2 * cos(theta_diff) );
240  double alpha = atan2( rho_1 * cos(theta_2) - rho_2 * cos(theta_1), rho_2*sin(theta_1) - rho_1 * sin(theta_2));
241  double one_over_rho = sin(theta_diff) / denom;
242  double atan_one_over_rho = atan2(sin(theta_diff), denom);
243 
244 // if (one_over_rho < 0) {
245 // one_over_rho = -one_over_rho;
246 // }
247 
248  s.setOneOverRho(one_over_rho);
249  s.setAtanOneOverRho(atan_one_over_rho);
250  s.setAlpha(alpha);
251  }
252 }
#define vpCERROR
Definition: vpDebug.h:365
static unsigned int selectAtanOneOverRho()
double getRho() const
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
static unsigned int selectX()
Select visual feature .
Class that defines a 3D line in the object frame and allows forward projection of the line in the cam...
Definition: vpLine.h:104
static unsigned int selectY()
Select visual feature .
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 ...
double getTheta() const
void setAtanOneOverRho(double atan_one_over_rho)
Set vanishing point feature value.
static unsigned int selectOneOverRho()
void setAlpha(double alpha)
Set vanishing point feature value.
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
void set_y(double y)
Set vanishing point feature value.
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:458
static unsigned int selectAlpha()
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
void set_x(double x)
Set vanishing point feature value.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setOneOverRho(double one_over_rho)
Set vanishing point feature value.