ViSP  2.8.0
vpCircle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCircle.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  * Visual feature circle.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
43 #include <visp/vpCircle.h>
44 
45 #include <visp/vpFeatureDisplay.h>
46 
47 void
49 {
50 
51  oP.resize(7) ;
52  cP.resize(7) ;
53 
54  p.resize(5) ;
55 }
56 
65 void
67 {
68  this->oP = oP ;
69 }
70 
83 void
84 vpCircle::setWorldCoordinates(const double A, const double B,
85  const double C,
86  const double X0, const double Y0,
87  const double Z0,
88  const double R)
89 {
90  oP[0] = A ;
91  oP[1] = B ;
92  oP[2] = C ;
93  oP[3] = X0 ;
94  oP[4] = Y0 ;
95  oP[5] = Z0 ;
96  oP[6] = R ;
97 }
98 
99 
100 
102 {
103  init() ;
104 }
105 
116 {
117  init() ;
118  setWorldCoordinates(oP) ;
119 }
120 
134 vpCircle::vpCircle(const double A, const double B,
135  const double C,
136  const double X0, const double Y0,
137  const double Z0,
138  const double R)
139 {
140  init() ;
141  setWorldCoordinates(A, B, C,
142  X0, Y0, Z0,
143  R) ;
144 }
145 
147 {
148 }
149 
150 
151 
152 
154 void
156 {
157  projection(cP,p) ;
158 }
159 
161 void
163 {
164 
165  vpColVector K(6) ;
166 
167  {
168  double A = cP[0] ;
169  double B = cP[1] ;
170  double C = cP[2] ;
171 
172  double X0 = cP[3] ;
173  double Y0 = cP[4] ;
174  double Z0 = cP[5] ;
175 
176  double r = cP[6];
177 
178  // projection
179  double s = X0*X0 + Y0*Y0 + Z0*Z0 - r*r ;
180  double det = A*X0+B*Y0+C*Z0;
181  A = A/det ;
182  B = B/det ;
183  C = C/det ;
184 
185  K[0] = 1 - 2*A*X0 + A*A*s;
186  K[1] = 1 - 2*B*Y0 + B*B*s;
187  K[2] = -A*Y0 - B*X0 + A*B*s;
188  K[3] = -C*X0 - A*Z0 + A*C*s;
189  K[4] = -C*Y0 - B*Z0 + B*C*s;
190  K[5] = 1 - 2*C*Z0 + C*C*s;
191 
192  }
193  double det = K[2]*K[2] -K[0]*K[1];
194  if (fabs(det) < 1e-8)
195  {
196  vpERROR_TRACE("division par 0") ;
198  "division par 0")) ;
199 
200  }
201 
202 
203  double xc = (K[1]*K[3]-K[2]*K[4])/det;
204  double yc = (K[0]*K[4]-K[2]*K[3])/det;
205 
206  double c = sqrt( (K[0]-K[1])*(K[0]-K[1]) + 4*K[2]*K[2] );
207  double s = 2*(K[0]*xc*xc + 2*K[2]*xc*yc + K[1]*yc*yc - K[5]);
208 
209  double A,B,E ;
210 
211  if (fabs(K[2])<1e-6)
212  {
213  E = 0.0;
214  if (K[0] > K[1])
215  {
216  A = sqrt(s/(K[0] + K[1] + c));
217  B = sqrt(s/(K[0] + K[1] - c));
218  }
219  else
220  {
221  A = sqrt(s/(K[0] + K[1] - c));
222  B = sqrt(s/(K[0] + K[1] + c));
223  }
224  }
225  else
226  {
227  E = (K[1] - K[0] + c)/(2*K[2]);
228  if ( fabs(E) > 1.0)
229  {
230  A = sqrt(s/(K[0] + K[1] + c));
231  B = sqrt(s/(K[0] + K[1] - c));
232  }
233  else
234  {
235  A = sqrt(s/(K[0] + K[1] - c));
236  B = sqrt(s/(K[0] + K[1] + c));
237  E = -1.0/E;
238  }
239  }
240 
241  det = (1.0 + vpMath::sqr(E));
242  double m20 = (vpMath::sqr(A) + vpMath::sqr(B*E)) /det ;
243  double m11 = (vpMath::sqr(A) - vpMath::sqr(B)) *E / det ;
244  double m02 = (vpMath::sqr(B) + vpMath::sqr(A*E)) / det ;
245 
246  p[0] = xc ;
247  p[1] = yc ;
248  p[2] = m20 ;
249  p[3] = m11 ;
250  p[4] = m02 ;
251 }
252 
254 void
256 {
257 
258  double A,B,C ;
259  A = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2];
260  B = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2];
261  C = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2];
262 
263  double X0,Y0,Z0 ;
264  X0 = cMo[0][3] + cMo[0][0]*oP[3] + cMo[0][1]*oP[4] + cMo[0][2]*oP[5];
265  Y0 = cMo[1][3] + cMo[1][0]*oP[3] + cMo[1][1]*oP[4] + cMo[1][2]*oP[5];
266  Z0 = cMo[2][3] + cMo[2][0]*oP[3] + cMo[2][1]*oP[4] + cMo[2][2]*oP[5];
267  double R = oP[6] ;
268 
269  cP[0] = A ;
270  cP[1] = B ;
271  cP[2] = C ;
272 
273  cP[3] = X0 ;
274  cP[4] = Y0 ;
275  cP[5] = Z0 ;
276 
277  cP[6] = R ;
278 
279  // vpTRACE("_cP :") ; std::cout << _cP.t() ;
280 
281 }
282 
284 void
286 {
287 
288  double A,B,C ;
289  A = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2];
290  B = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2];
291  C = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2];
292 
293  double X0,Y0,Z0 ;
294  X0 = cMo[0][3] + cMo[0][0]*oP[3] + cMo[0][1]*oP[4] + cMo[0][2]*oP[5];
295  Y0 = cMo[1][3] + cMo[1][0]*oP[3] + cMo[1][1]*oP[4] + cMo[1][2]*oP[5];
296  Z0 = cMo[2][3] + cMo[2][0]*oP[3] + cMo[2][1]*oP[4] + cMo[2][2]*oP[5];
297  double R = oP[6] ;
298 
299  cP[0] = A ;
300  cP[1] = B ;
301  cP[2] = C ;
302 
303  cP[3] = X0 ;
304  cP[4] = Y0 ;
305  cP[5] = Z0 ;
306 
307  cP[6] = R ;
308 
309  // vpTRACE("_cP :") ; std::cout << _cP.t() ;
310 
311 }
312 
314  const vpCameraParameters &cam,
315  const vpColor &color,
316  const unsigned int thickness)
317 {
318  vpFeatureDisplay::displayEllipse(p[0],p[1],p[2],p[3], p[4],
319  cam, I, color, thickness) ;
320 }
321 
322 // non destructive wrt. cP and p
324  const vpHomogeneousMatrix &cMo,
325  const vpCameraParameters &cam,
326  const vpColor &color,
327  const unsigned int thickness)
328 {
329  vpColVector _cP, _p ;
330  changeFrame(cMo,_cP) ;
331  projection(_cP,_p) ;
332  vpFeatureDisplay::displayEllipse(_p[0],_p[1],_p[2],_p[3], _p[4],
333  cam, I, color, thickness) ;
334 
335 }
338 {
339  vpCircle *feature = new vpCircle(*this) ;
340  return feature ;
341 }
void init()
Definition: vpCircle.cpp:48
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)
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
perspective projection of the circle
Definition: vpCircle.cpp:255
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:379
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
error that can be emited by ViSP classes.
Definition: vpException.h:75
virtual ~vpCircle()
Definition: vpCircle.cpp:146
vpColVector cP
Definition: vpTracker.h:82
void projection()
perspective projection of the circle
Definition: vpCircle.cpp:155
static double sqr(double x)
Definition: vpMath.h:106
vpCircle * duplicate() const
for memory issue (used by the vpServo class only)
Definition: vpCircle.cpp:337
Generic class defining intrinsic camera parameters.
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, const unsigned int thickness=1)
Definition: vpCircle.cpp:313
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
Class that defines what is a circle.
Definition: vpCircle.h:61
vpColVector p
Definition: vpTracker.h:78
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94