ViSP  2.8.0
vpSphere.cpp
1 /****************************************************************************
2  *
3  * $Id: vpSphere.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  * Sphere feature.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
43 #include <visp/vpSphere.h>
44 #include <visp/vpFeatureDisplay.h>
45 
46 
47 void
49 {
50 
51  oP.resize(4) ;
52  cP.resize(4) ;
53 
54  p.resize(5) ;
55 }
56 
57 void
59 {
60  this->oP = oP ;
61 }
62 
63 void
64 vpSphere::setWorldCoordinates(const double X0, const double Y0,
65  const double Z0, const double R)
66 {
67  oP[0] = X0 ;
68  oP[1] = Y0 ;
69  oP[2] = Z0 ;
70  oP[3] = R ;
71 }
72 
73 
74 
76 {
77  init() ;
78 }
79 
80 
82 {
83  init() ;
85 }
86 
87 vpSphere::vpSphere(const double X0, const double Y0,
88  const double Z0, const double R)
89 {
90  init() ;
91  setWorldCoordinates(X0, Y0, Z0, R) ;
92 }
93 
95 {
96 }
97 
98 
100 void
102 {
103  projection(cP,p) ;
104 }
105 
107 void
109 {
110  double x0, y0, z0; //variables intermediaires
111 // double k0, k1, k2, k3, k4; //variables intermediaires
112  double E, A, B; //variables intermediaires
113 
114  //calcul des parametres M20, M11, M02 de l'ellipse
115  double s, a, b, r, e; //variables intermediaires
116  r = cP[3];
117 
118  x0 = cP[0] ;
119  y0 = cP[1] ;
120  z0 = cP[2] ;
121 
122 
123  s = r*r - y0*y0 -z0*z0;
124 
125 // k0 = (r*r - x0*x0 -z0*z0)/s;
126 // k1 = (x0*y0)/s;
127 // k2 = (x0*z0)/s;
128 // k3 = (y0*z0)/s;
129 // k4 = (r*r - x0*x0 -y0*y0)/s;
130 
131  if ((s = z0*z0 - r*r) < 0.0)
132  {
133  vpERROR_TRACE("sphere derriere le plan image\n");
134  }
135 
136  p[0] = x0*z0/s ; //x
137  p[1] = y0*z0/s ; //y
138 
139  if (fabs(x0) > 1e-6)
140  {
141  // vpERROR_TRACE(" %f",r) ;
142  e = y0/x0;
143  b = r/sqrt(s);
144  if ((a = x0*x0 + y0*y0 + z0*z0 - r*r) < 0.0)
145  {
146  vpERROR_TRACE("sphere derriere le plan image\n");
147  }
148  a = r*sqrt(a)/s;
149  if (fabs(e) <= 1.0)
150  {
151  E = e;
152  A = a;
153  B = b;
154  }
155  else
156  {
157  E = -1.0/e;
158  A = b;
159  B = a;
160  }
161  }
162  else
163  {
164  // vpERROR_TRACE(" %f",r) ;
165  E = 0.0;
166  A = r/sqrt(s);
167  B = r*sqrt(y0*y0+z0*z0-r*r)/s;
168  }
169 
170  p[2] = ( A*A + B*B * E*E) / (1.0 + E*E); // mu20
171  p[3] = ( A*A - B*B) * E / (1.0 + E*E); // mu11
172  p[4] = ( B*B + A*A * E*E) / (1.0 + E*E); // mu02
173 
174  // vpERROR_TRACE(" %f",r) ;
175 
176  // std::cout << p.t() ;
177 
178 }
180 void
182 {
183  changeFrame(cMo,cP) ;
184 }
185 
187 void
189 {
190 
191  double x0, y0, z0; //variables intermediaires
192 
193 
194  x0 = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2] + cMo[0][3];
195  y0 = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2] + cMo[1][3];
196  z0 = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2] + cMo[2][3];
197 
198  cP[3] = oP[3];
199 
200  cP[0] = x0 ;
201  cP[1] = y0 ;
202  cP[2] = z0 ;
203 
204 
205 }
206 
209 {
210  vpSphere *feature = new vpSphere(*this) ;
211  return feature ;
212 }
213 
214 
215 
216 // non destructive wrt. cP and p
218  const vpHomogeneousMatrix &cMo,
219  const vpCameraParameters &cam,
220  const vpColor &color,
221  const unsigned int thickness)
222 {
223  vpColVector _cP, _p ;
224  changeFrame(cMo,_cP) ;
225  projection(_cP,_p) ;
226  vpFeatureDisplay::displayEllipse(_p[0],_p[1],_p[2],_p[3], _p[4],
227  cam, I, color, thickness) ;
228 
229 }
230 
231 
232 
234  const vpCameraParameters &cam,
235  const vpColor &color,
236  const unsigned int thickness)
237 {
238  vpFeatureDisplay::displayEllipse(p[0],p[1],p[2],p[3], p[4],
239  cam, I, color, thickness) ;
240 }
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)
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
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
perspective projection of the circle
Definition: vpSphere.cpp:188
vpSphere * duplicate() const
for memory issue (used by the vpServo class only)
Definition: vpSphere.cpp:208
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
virtual ~vpSphere()
Definition: vpSphere.cpp:94
Class that defines what is a sphere.
Definition: vpSphere.h:64
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:58
vpColVector cP
Definition: vpTracker.h:82
void projection()
perspective projection of the sphere
Definition: vpSphere.cpp:101
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, const unsigned int thickness=1)
Definition: vpSphere.cpp:233
Generic class defining intrinsic camera parameters.
vpSphere()
Definition: vpSphere.cpp:75
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void init()
Definition: vpSphere.cpp:48
vpColVector p
Definition: vpTracker.h:78
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94