ViSP  2.9.0
vpCircle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCircle.cpp 4649 2014-02-07 14:57:11Z 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  * 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  vpColVector K(6) ;
165  {
166  double A = cP_[0] ;
167  double B = cP_[1] ;
168  double C = cP_[2] ;
169 
170  double X0 = cP_[3] ;
171  double Y0 = cP_[4] ;
172  double Z0 = cP_[5] ;
173 
174  double r = cP_[6];
175 
176  // projection
177  double s = X0*X0 + Y0*Y0 + Z0*Z0 - r*r ;
178  double det = A*X0+B*Y0+C*Z0;
179  A = A/det ;
180  B = B/det ;
181  C = C/det ;
182 
183  K[0] = 1 - 2*A*X0 + A*A*s;
184  K[1] = 1 - 2*B*Y0 + B*B*s;
185  K[2] = -A*Y0 - B*X0 + A*B*s;
186  K[3] = -C*X0 - A*Z0 + A*C*s;
187  K[4] = -C*Y0 - B*Z0 + B*C*s;
188  K[5] = 1 - 2*C*Z0 + C*C*s;
189 
190  }
191  double det = K[2]*K[2] -K[0]*K[1];
192  if (fabs(det) < 1e-8)
193  {
194  vpERROR_TRACE("division par 0") ;
196  "division par 0")) ;
197  }
198 
199  double xc = (K[1]*K[3]-K[2]*K[4])/det;
200  double yc = (K[0]*K[4]-K[2]*K[3])/det;
201 
202  double c = sqrt( (K[0]-K[1])*(K[0]-K[1]) + 4*K[2]*K[2] );
203  double s = 2*(K[0]*xc*xc + 2*K[2]*xc*yc + K[1]*yc*yc - K[5]);
204 
205  double A,B,E ;
206 
207  if (fabs(K[2])<1e-6)
208  {
209  E = 0.0;
210  if (K[0] > K[1])
211  {
212  A = sqrt(s/(K[0] + K[1] + c));
213  B = sqrt(s/(K[0] + K[1] - c));
214  }
215  else
216  {
217  A = sqrt(s/(K[0] + K[1] - c));
218  B = sqrt(s/(K[0] + K[1] + c));
219  }
220  }
221  else
222  {
223  E = (K[1] - K[0] + c)/(2*K[2]);
224  if ( fabs(E) > 1.0)
225  {
226  A = sqrt(s/(K[0] + K[1] + c));
227  B = sqrt(s/(K[0] + K[1] - c));
228  }
229  else
230  {
231  A = sqrt(s/(K[0] + K[1] - c));
232  B = sqrt(s/(K[0] + K[1] + c));
233  E = -1.0/E;
234  }
235  }
236 
237  det = (1.0 + vpMath::sqr(E));
238  double m20 = (vpMath::sqr(A) + vpMath::sqr(B*E)) /det ;
239  double m11 = (vpMath::sqr(A) - vpMath::sqr(B)) *E / det ;
240  double m02 = (vpMath::sqr(B) + vpMath::sqr(A*E)) / det ;
241 
242  p_[0] = xc ;
243  p_[1] = yc ;
244  p_[2] = m20 ;
245  p_[3] = m11 ;
246  p_[4] = m02 ;
247 }
248 
250 void
252 {
253 
254  double A,B,C ;
255  A = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2];
256  B = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2];
257  C = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2];
258 
259  double X0,Y0,Z0 ;
260  X0 = cMo[0][3] + cMo[0][0]*oP[3] + cMo[0][1]*oP[4] + cMo[0][2]*oP[5];
261  Y0 = cMo[1][3] + cMo[1][0]*oP[3] + cMo[1][1]*oP[4] + cMo[1][2]*oP[5];
262  Z0 = cMo[2][3] + cMo[2][0]*oP[3] + cMo[2][1]*oP[4] + cMo[2][2]*oP[5];
263  double R = oP[6] ;
264 
265  cP_[0] = A ;
266  cP_[1] = B ;
267  cP_[2] = C ;
268 
269  cP_[3] = X0 ;
270  cP_[4] = Y0 ;
271  cP_[5] = Z0 ;
272 
273  cP_[6] = R ;
274 
275  // vpTRACE("_cP :") ; std::cout << _cP.t() ;
276 
277 }
278 
280 void
282 {
283 
284  double A,B,C ;
285  A = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2];
286  B = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2];
287  C = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2];
288 
289  double X0,Y0,Z0 ;
290  X0 = cMo[0][3] + cMo[0][0]*oP[3] + cMo[0][1]*oP[4] + cMo[0][2]*oP[5];
291  Y0 = cMo[1][3] + cMo[1][0]*oP[3] + cMo[1][1]*oP[4] + cMo[1][2]*oP[5];
292  Z0 = cMo[2][3] + cMo[2][0]*oP[3] + cMo[2][1]*oP[4] + cMo[2][2]*oP[5];
293  double R = oP[6] ;
294 
295  cP[0] = A ;
296  cP[1] = B ;
297  cP[2] = C ;
298 
299  cP[3] = X0 ;
300  cP[4] = Y0 ;
301  cP[5] = Z0 ;
302 
303  cP[6] = R ;
304 
305  // vpTRACE("_cP :") ; std::cout << _cP.t() ;
306 
307 }
308 
310  const vpCameraParameters &cam,
311  const vpColor &color,
312  const unsigned int thickness)
313 {
314  vpFeatureDisplay::displayEllipse(p[0],p[1],p[2],p[3], p[4],
315  cam, I, color, thickness) ;
316 }
317 
318 // non destructive wrt. cP and p
320  const vpHomogeneousMatrix &cMo,
321  const vpCameraParameters &cam,
322  const vpColor &color,
323  const unsigned int thickness)
324 {
325  vpColVector _cP, _p ;
326  changeFrame(cMo,_cP) ;
327  projection(_cP,_p) ;
328  vpFeatureDisplay::displayEllipse(_p[0],_p[1],_p[2],_p[3], _p[4],
329  cam, I, color, thickness) ;
330 
331 }
334 {
335  vpCircle *feature = new vpCircle(*this) ;
336  return feature ;
337 }
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:251
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:395
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
error that can be emited by ViSP classes.
Definition: vpException.h:76
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:333
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:309
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