ViSP  2.10.0
vpSphere.cpp
1 /****************************************************************************
2  *
3  * $Id: vpSphere.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  * 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() ;
84  setWorldCoordinates(oP_) ;
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  s = r*r - y0*y0 -z0*z0;
123 
124 // k0 = (r*r - x0*x0 -z0*z0)/s;
125 // k1 = (x0*y0)/s;
126 // k2 = (x0*z0)/s;
127 // k3 = (y0*z0)/s;
128 // k4 = (r*r - x0*x0 -y0*y0)/s;
129 
130  if ((s = z0*z0 - r*r) < 0.0)
131  {
132  vpERROR_TRACE("sphere derriere le plan image\n");
133  }
134 
135  p_[0] = x0*z0/s ; //x
136  p_[1] = y0*z0/s ; //y
137 
138  if (fabs(x0) > 1e-6)
139  {
140  // vpERROR_TRACE(" %f",r) ;
141  e = y0/x0;
142  b = r/sqrt(s);
143  if ((a = x0*x0 + y0*y0 + z0*z0 - r*r) < 0.0)
144  {
145  vpERROR_TRACE("sphere derriere le plan image\n");
146  }
147  a = r*sqrt(a)/s;
148  if (fabs(e) <= 1.0)
149  {
150  E = e;
151  A = a;
152  B = b;
153  }
154  else
155  {
156  E = -1.0/e;
157  A = b;
158  B = a;
159  }
160  }
161  else
162  {
163  // vpERROR_TRACE(" %f",r) ;
164  E = 0.0;
165  A = r/sqrt(s);
166  B = r*sqrt(y0*y0+z0*z0-r*r)/s;
167  }
168 
169  p_[2] = ( A*A + B*B * E*E) / (1.0 + E*E); // mu20
170  p_[3] = ( A*A - B*B) * E / (1.0 + E*E); // mu11
171  p_[4] = ( B*B + A*A * E*E) / (1.0 + E*E); // mu02
172 
173  // vpERROR_TRACE(" %f",r) ;
174 
175  // std::cout << p.t() ;
176 }
178 void
180 {
181  changeFrame(cMo,cP) ;
182 }
183 
185 void
187 {
188  double x0, y0, z0; //variables intermediaires
189 
190  x0 = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2] + cMo[0][3];
191  y0 = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2] + cMo[1][3];
192  z0 = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2] + cMo[2][3];
193 
194  cP_[3] = oP[3];
195 
196  cP_[0] = x0 ;
197  cP_[1] = y0 ;
198  cP_[2] = z0 ;
199 }
200 
203 {
204  vpSphere *feature = new vpSphere(*this) ;
205  return feature ;
206 }
207 
208 
209 
210 // non destructive wrt. cP and p
212  const vpHomogeneousMatrix &cMo,
213  const vpCameraParameters &cam,
214  const vpColor &color,
215  const unsigned int thickness)
216 {
217  vpColVector _cP, _p ;
218  changeFrame(cMo,_cP) ;
219  projection(_cP,_p) ;
220  vpFeatureDisplay::displayEllipse(_p[0],_p[1],_p[2],_p[3], _p[4],
221  cam, I, color, thickness) ;
222 
223 }
224 
225 
226 
228  const vpCameraParameters &cam,
229  const vpColor &color,
230  const unsigned int thickness)
231 {
232  vpFeatureDisplay::displayEllipse(p[0],p[1],p[2],p[3], p[4],
233  cam, I, color, thickness) ;
234 }
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:395
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
Perspective projection of the circle.
Definition: vpSphere.cpp:186
vpSphere * duplicate() const
for memory issue (used by the vpServo class only)
Definition: vpSphere.cpp:202
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:227
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:98