ViSP  2.10.0
vpFeatureDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: vpFeatureDisplay.cpp 4773 2014-07-10 17:09:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Interface with the image for feature display.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 #include <visp/vpFeatureDisplay.h>
44 
45 // Meter/pixel conversion
46 #include <visp/vpMeterPixelConversion.h>
47 
48 // display
49 #include <visp/vpDisplay.h>
50 
51 // Debug trace
52 #include <visp/vpDebug.h>
53 
54 // math
55 #include <visp/vpMath.h>
56 
57 #include <visp/vpImagePoint.h>
58 
59 
60 
69 void vpFeatureDisplay::displayPoint(double x,double y,
70  const vpCameraParameters &cam,
71  const vpImage<unsigned char> &I,
72  const vpColor &color,
73  unsigned int thickness)
74 {
75  try{
76  vpImagePoint ip; // pixel coordinates in float
78 
79  vpDisplay::displayCross(I, ip, 15, color, thickness) ;
80  }
81  catch(...)
82  {
83  vpERROR_TRACE("Error caught") ;
84  throw ;
85  }
86 
87 }
95 void vpFeatureDisplay::displayLine(double rho,double theta,
96  const vpCameraParameters &cam,
97  const vpImage<unsigned char> &I,
98  const vpColor &color,
99  unsigned int thickness )
100 {
101 
102 
103  try{
104  // x cos(theta) + y sin(theta) - rho = 0
105 
106  double rhop,thetap ;
107  vpMeterPixelConversion::convertLine(cam,rho,theta,rhop,thetap) ;
108 
109  // u cos(thetap) + v sin(thetap) - rhop = 0
110 
111  double co = cos(thetap) ;
112  double si = sin(thetap) ;
113  double c = -rhop ;
114 
115  // vpTRACE("rhop %f %f ",rhop, atan2(si,co)) ;
116  // double u1,v1,u2,v2 ;
117 
118  double a = si ;
119  double b = co ;
120  vpImagePoint ip1, ip2;
121 
122  if (fabs(a) < fabs(b)) {
123  ip1.set_ij(0, (-c)/b);
124  double h = I.getHeight() - 1;
125  ip2.set_ij(h, (-c - a*h)/b);
126  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
127  }
128  else {
129  ip1.set_ij((-c)/a, 0);
130  double w = I.getWidth()-1;
131  ip2.set_ij((-c - b*w)/a, w);
132  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
133  }
134  }
135  catch(...)
136  {
137  vpERROR_TRACE("Error caught") ;
138  throw ;
139  }
140 }
149 void vpFeatureDisplay::displayCylinder(double rho1,double theta1,
150  double rho2,double theta2,
151  const vpCameraParameters &cam,
152  const vpImage<unsigned char> &I,
153  const vpColor &color,
154  unsigned int thickness)
155 {
156  try
157  {
158  displayLine(rho1, theta1, cam, I, color, thickness) ;
159  displayLine(rho2, theta2, cam, I, color, thickness) ;
160  }
161  catch(...)
162  {
163  vpERROR_TRACE("Error caught") ;
164  throw ;
165  }
166 }
179 void vpFeatureDisplay::displayEllipse(double x,double y,
180  double mu20, double mu11, double mu02,
181  const vpCameraParameters &cam,
182  const vpImage<unsigned char> &I,
183  const vpColor &color,
184  unsigned int thickness)
185 {
186  try {
187  unsigned int number_of_points = 45 ;
188  const double incr = 2 * M_PI/(double)number_of_points ; // angle increment
189  unsigned int i = 0 ;
190 
191  double s = sqrt(vpMath::sqr(mu20-mu02)+4*mu11*mu11) ;
192  double a, b, e ;
193 
194  //if (fabs(mu11)<1e-6) e =0 ;
195  if (fabs(mu11) < std::numeric_limits<double>::epsilon()) {
196  e = 0 ;
197  a = sqrt(mu20);
198  b = sqrt(mu02);
199  }
200  else {
201  e = (mu02-mu20+s)/(2*mu11) ;
202  a = sqrt( (mu02+mu20+s)/2.0) ;
203  b = sqrt( (mu02+mu20-s)/2.0) ;
204  }
205 
206  double e1 = atan(e) ;
207 
208  double k = 0.0 ;
209 
210  double ce = cos(e1) ;
211  double se = sin(e1) ;
212 
213  double x2 = 0;
214  double y2 =0;
215  vpImagePoint ip1, ip2;
216 
217  for( i = 0; i < number_of_points+2 ; i++)
218  {
219  double x1 = a *cos(k) ; // equation of an ellipse
220  double y1 = b *sin(k) ; // equation of an ellipse
221  double x11 = x + ce *x1 - se *y1 ;
222  double y11 = y + se *x1 + ce *y1 ;
223 
224  vpMeterPixelConversion::convertPoint(cam, x11, y11, ip1);
225 
226  if (i > 1) {
227  ip2.set_u( x2 );
228  ip2.set_v( y2 );
229 
230  vpDisplay::displayLine(I, ip1, ip2, color, thickness) ;
231  }
232 
233  ip2 = ip1;
234  y2 = y1 ;
235  x2 = x1 ;
236  k += incr ;
237  } // end for loop
238  }
239  catch(vpException &e)
240  {
241  throw(e);
242  }
243 }
244 
253 void vpFeatureDisplay::displayPoint(double x,double y,
254  const vpCameraParameters &cam,
255  const vpImage<vpRGBa> &I,
256  const vpColor &color,
257  unsigned int thickness)
258 {
259  try{
260  vpImagePoint ip; // pixel coordinates in float
261  vpMeterPixelConversion::convertPoint(cam, x, y, ip) ;
262 
263  vpDisplay::displayCross(I, ip, 15, color, thickness) ;
264  }
265  catch(...)
266  {
267  vpERROR_TRACE("Error caught") ;
268  throw ;
269  }
270 
271 }
272 
280 void vpFeatureDisplay::displayLine(double rho,double theta,
281  const vpCameraParameters &cam,
282  const vpImage<vpRGBa> &I,
283  const vpColor &color,
284  unsigned int thickness )
285 {
286 
287 
288  try{
289  // x cos(theta) + y sin(theta) - rho = 0
290 
291  double rhop,thetap ;
292  vpMeterPixelConversion::convertLine(cam,rho,theta,rhop,thetap) ;
293 
294  // u cos(thetap) + v sin(thetap) - rhop = 0
295 
296  double co = cos(thetap) ;
297  double si = sin(thetap) ;
298  double c = -rhop ;
299 
300  // vpTRACE("rhop %f %f ",rhop, atan2(si,co)) ;
301  // double u1,v1,u2,v2 ;
302 
303  double a = si ;
304  double b = co ;
305  vpImagePoint ip1, ip2;
306 
307  if (fabs(a) < fabs(b)) {
308  ip1.set_ij(0, (-c)/b);
309  double h = I.getHeight() - 1;
310  ip2.set_ij(h, (-c - a*h)/b);
311  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
312  }
313  else {
314  ip1.set_ij((-c)/a, 0);
315  double w = I.getWidth()-1;
316  ip2.set_ij((-c - b*w)/a, w);
317  vpDisplay::displayLine(I, ip1, ip2, color, thickness);
318  }
319  }
320  catch(...)
321  {
322  vpERROR_TRACE("Error caught") ;
323  throw ;
324  }
325 }
334 void vpFeatureDisplay::displayCylinder(double rho1, double theta1,
335  double rho2, double theta2,
336  const vpCameraParameters &cam,
337  const vpImage<vpRGBa> &I,
338  const vpColor &color,
339  unsigned int thickness)
340 {
341  try
342  {
343  displayLine(rho1, theta1, cam, I, color, thickness) ;
344  displayLine(rho2, theta2, cam, I, color, thickness) ;
345  }
346  catch(...)
347  {
348  vpERROR_TRACE("Error caught") ;
349  throw ;
350  }
351 }
352 
365 void vpFeatureDisplay::displayEllipse(double x, double y,
366  double mu20, double mu11, double mu02,
367  const vpCameraParameters &cam,
368  const vpImage<vpRGBa> &I,
369  const vpColor &color,
370  unsigned int thickness)
371 {
372  try {
373  unsigned int number_of_points = 45 ;
374  const double incr = 2 * M_PI/(double)number_of_points ; // angle increment
375  unsigned int i = 0 ;
376 
377  double s = sqrt(vpMath::sqr(mu20-mu02)+4*mu11*mu11) ;
378  double a, b, e ;
379 
380  //if (fabs(mu11)<1e-6) e =0 ;
381  if (fabs(mu11) < std::numeric_limits<double>::epsilon()) {
382  e = 0 ;
383  a = sqrt(mu20);
384  b = sqrt(mu02);
385  }
386  else {
387  e = (mu02-mu20+s)/(2*mu11) ;
388  a = sqrt( (mu02+mu20+s)/2.0) ;
389  b = sqrt( (mu02+mu20-s)/2.0) ;
390  }
391 
392  double e1 = atan(e) ;
393 
394  double k = 0.0 ;
395 
396  double ce = cos(e1) ;
397  double se = sin(e1) ;
398 
399  double x2 = 0;
400  double y2 =0;
401  vpImagePoint ip1, ip2;
402 
403  for( i = 0; i < number_of_points+2 ; i++)
404  {
405  double x1 = a *cos(k) ; // equation of an ellipse
406  double y1 = b *sin(k) ; // equation of an ellipse
407  double x11 = x + ce *x1 - se *y1 ;
408  double y11 = y + se *x1 + ce *y1 ;
409 
410  vpMeterPixelConversion::convertPoint(cam, x11, y11, ip1);
411 
412  if (i > 1) {
413  ip2.set_u( x2 );
414  ip2.set_v( y2 );
415 
416  vpDisplay::displayLine(I, ip1, ip2, color, thickness) ;
417  }
418 
419  ip2 = ip1;
420  y2 = y1 ;
421  x2 = x1 ;
422  k += incr ;
423  } // end for loop
424  }
425  catch(vpException &e)
426  {
427  throw(e);
428  }
429 }
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)
unsigned int getWidth() const
Definition: vpImage.h:161
#define vpERROR_TRACE
Definition: vpDebug.h:395
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 ...
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
error that can be emited by ViSP classes.
Definition: vpException.h:76
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:217
static double sqr(double x)
Definition: vpMath.h:106
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
void set_v(const double v)
Definition: vpImagePoint.h:228
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 displayLine(double rho, double theta, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
unsigned int getHeight() const
Definition: vpImage.h:152
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
void set_ij(const double ii, const double jj)
Definition: vpImagePoint.h:181