Visual Servoing Platform  version 3.1.0
vpFeatureDisplay.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 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  * Interface with the image for feature display.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpFeatureDisplay.h>
41 
42 // Meter/pixel conversion
43 #include <visp3/core/vpMeterPixelConversion.h>
44 
45 // display
46 #include <visp3/core/vpDisplay.h>
47 
48 // Debug trace
49 #include <visp3/core/vpDebug.h>
50 
51 // math
52 #include <visp3/core/vpMath.h>
53 
54 #include <visp3/core/vpImagePoint.h>
55 
64 void vpFeatureDisplay::displayPoint(double x, double y, const vpCameraParameters &cam, const vpImage<unsigned char> &I,
65  const vpColor &color, unsigned int thickness)
66 {
67  try {
68  vpImagePoint ip; // pixel coordinates in float
70 
71  vpDisplay::displayCross(I, ip, 15, color, thickness);
72  } catch (...) {
73  vpERROR_TRACE("Error caught");
74  throw;
75  }
76 }
84 void vpFeatureDisplay::displayLine(double rho, double theta, const vpCameraParameters &cam,
85  const vpImage<unsigned char> &I, const vpColor &color, unsigned int thickness)
86 {
87 
88  try {
89  // x cos(theta) + y sin(theta) - rho = 0
90 
91  double rhop, thetap;
92  vpMeterPixelConversion::convertLine(cam, rho, theta, rhop, thetap);
93 
94  // u cos(thetap) + v sin(thetap) - rhop = 0
95 
96  double co = cos(thetap);
97  double si = sin(thetap);
98  double c = -rhop;
99 
100  // vpTRACE("rhop %f %f ",rhop, atan2(si,co)) ;
101  // double u1,v1,u2,v2 ;
102 
103  double a = si;
104  double b = co;
105  vpImagePoint ip1, ip2;
106 
107  if (fabs(a) < fabs(b)) {
108  ip1.set_ij(0, (-c) / b);
109  double h = I.getHeight() - 1;
110  ip2.set_ij(h, (-c - a * h) / b);
111  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
112  } else {
113  ip1.set_ij((-c) / a, 0);
114  double w = I.getWidth() - 1;
115  ip2.set_ij((-c - b * w) / a, w);
116  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
117  }
118  } catch (...) {
119  vpERROR_TRACE("Error caught");
120  throw;
121  }
122 }
131 void vpFeatureDisplay::displayCylinder(double rho1, double theta1, double rho2, double theta2,
132  const vpCameraParameters &cam, const vpImage<unsigned char> &I,
133  const vpColor &color, unsigned int thickness)
134 {
135  try {
136  displayLine(rho1, theta1, cam, I, color, thickness);
137  displayLine(rho2, theta2, cam, I, color, thickness);
138  } catch (...) {
139  vpERROR_TRACE("Error caught");
140  throw;
141  }
142 }
155 void vpFeatureDisplay::displayEllipse(double x, double y, double mu20, double mu11, double mu02,
156  const vpCameraParameters &cam, const vpImage<unsigned char> &I,
157  const vpColor &color, unsigned int thickness)
158 {
159  try {
160  unsigned int number_of_points = 45;
161  const double incr = 2 * M_PI / (double)number_of_points; // angle increment
162  unsigned int i = 0;
163 
164  double s = sqrt(vpMath::sqr(mu20 - mu02) + 4 * mu11 * mu11);
165  double a, b, e;
166 
167  // if (fabs(mu11)<1e-6) e =0 ;
168  if (fabs(mu11) < std::numeric_limits<double>::epsilon()) {
169  e = 0;
170  a = sqrt(mu20);
171  b = sqrt(mu02);
172  } else {
173  e = (mu02 - mu20 + s) / (2 * mu11);
174  a = sqrt((mu02 + mu20 + s) / 2.0);
175  b = sqrt((mu02 + mu20 - s) / 2.0);
176  }
177 
178  double e1 = atan(e);
179 
180  double k = 0.0;
181 
182  double ce = cos(e1);
183  double se = sin(e1);
184 
185  double x2 = 0;
186  double y2 = 0;
187  vpImagePoint ip1, ip2;
188 
189  for (i = 0; i < number_of_points + 2; i++) {
190  double x1 = a * cos(k); // equation of an ellipse
191  double y1 = b * sin(k); // equation of an ellipse
192  double x11 = x + ce * x1 - se * y1;
193  double y11 = y + se * x1 + ce * y1;
194 
195  vpMeterPixelConversion::convertPoint(cam, x11, y11, ip1);
196 
197  if (i > 1) {
198  ip2.set_u(x2);
199  ip2.set_v(y2);
200 
201  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
202  }
203 
204  ip2 = ip1;
205  y2 = y1;
206  x2 = x1;
207  k += incr;
208  } // end for loop
209  } catch (vpException &e) {
210  throw(e);
211  }
212 }
213 
222 void vpFeatureDisplay::displayPoint(double x, double y, const vpCameraParameters &cam, const vpImage<vpRGBa> &I,
223  const vpColor &color, unsigned int thickness)
224 {
225  try {
226  vpImagePoint ip; // pixel coordinates in float
228 
229  vpDisplay::displayCross(I, ip, 15, color, thickness);
230  } catch (...) {
231  vpERROR_TRACE("Error caught");
232  throw;
233  }
234 }
235 
243 void vpFeatureDisplay::displayLine(double rho, double theta, const vpCameraParameters &cam, const vpImage<vpRGBa> &I,
244  const vpColor &color, unsigned int thickness)
245 {
246 
247  try {
248  // x cos(theta) + y sin(theta) - rho = 0
249 
250  double rhop, thetap;
251  vpMeterPixelConversion::convertLine(cam, rho, theta, rhop, thetap);
252 
253  // u cos(thetap) + v sin(thetap) - rhop = 0
254 
255  double co = cos(thetap);
256  double si = sin(thetap);
257  double c = -rhop;
258 
259  // vpTRACE("rhop %f %f ",rhop, atan2(si,co)) ;
260  // double u1,v1,u2,v2 ;
261 
262  double a = si;
263  double b = co;
264  vpImagePoint ip1, ip2;
265 
266  if (fabs(a) < fabs(b)) {
267  ip1.set_ij(0, (-c) / b);
268  double h = I.getHeight() - 1;
269  ip2.set_ij(h, (-c - a * h) / b);
270  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
271  } else {
272  ip1.set_ij((-c) / a, 0);
273  double w = I.getWidth() - 1;
274  ip2.set_ij((-c - b * w) / a, w);
275  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
276  }
277  } catch (...) {
278  vpERROR_TRACE("Error caught");
279  throw;
280  }
281 }
290 void vpFeatureDisplay::displayCylinder(double rho1, double theta1, double rho2, double theta2,
291  const vpCameraParameters &cam, const vpImage<vpRGBa> &I, const vpColor &color,
292  unsigned int thickness)
293 {
294  try {
295  displayLine(rho1, theta1, cam, I, color, thickness);
296  displayLine(rho2, theta2, cam, I, color, thickness);
297  } catch (...) {
298  vpERROR_TRACE("Error caught");
299  throw;
300  }
301 }
302 
315 void vpFeatureDisplay::displayEllipse(double x, double y, double mu20, double mu11, double mu02,
316  const vpCameraParameters &cam, const vpImage<vpRGBa> &I, const vpColor &color,
317  unsigned int thickness)
318 {
319  try {
320  unsigned int number_of_points = 45;
321  const double incr = 2 * M_PI / (double)number_of_points; // angle increment
322  unsigned int i = 0;
323 
324  double s = sqrt(vpMath::sqr(mu20 - mu02) + 4 * mu11 * mu11);
325  double a, b, e;
326 
327  // if (fabs(mu11)<1e-6) e =0 ;
328  if (fabs(mu11) < std::numeric_limits<double>::epsilon()) {
329  e = 0;
330  a = sqrt(mu20);
331  b = sqrt(mu02);
332  } else {
333  e = (mu02 - mu20 + s) / (2 * mu11);
334  a = sqrt((mu02 + mu20 + s) / 2.0);
335  b = sqrt((mu02 + mu20 - s) / 2.0);
336  }
337 
338  double e1 = atan(e);
339 
340  double k = 0.0;
341 
342  double ce = cos(e1);
343  double se = sin(e1);
344 
345  double x2 = 0;
346  double y2 = 0;
347  vpImagePoint ip1, ip2;
348 
349  for (i = 0; i < number_of_points + 2; i++) {
350  double x1 = a * cos(k); // equation of an ellipse
351  double y1 = b * sin(k); // equation of an ellipse
352  double x11 = x + ce * x1 - se * y1;
353  double y11 = y + se * x1 + ce * y1;
354 
355  vpMeterPixelConversion::convertPoint(cam, x11, y11, ip1);
356 
357  if (i > 1) {
358  ip2.set_u(x2);
359  ip2.set_v(y2);
360 
361  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
362  }
363 
364  ip2 = ip1;
365  y2 = y1;
366  x2 = x1;
367  k += incr;
368  } // end for loop
369  } catch (vpException &e) {
370  throw(e);
371  }
372 }
static void displayEllipse(double x, double y, double mu20, double mu11, double m02, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
static void displayCylinder(double rho1, double theta1, double rho2, double theta2, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
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 ...
#define vpERROR_TRACE
Definition: vpDebug.h:393
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void displayPoint(double x, double y, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
void set_u(const double u)
Definition: vpImagePoint.h:226
static double sqr(double x)
Definition: vpMath.h:108
void set_v(const double v)
Definition: vpImagePoint.h:237
Generic class defining intrinsic camera parameters.
static void convertLine(const vpCameraParameters &cam, const double &rho_m, const double &theta_m, double &rho_p, double &theta_p)
Line coordinates conversion (rho,theta).
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
unsigned int getHeight() const
Definition: vpImage.h:178
static void displayLine(double rho, double theta, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
unsigned int getWidth() const
Definition: vpImage.h:229
void set_ij(const double ii, const double jj)
Definition: vpImagePoint.h:189