Visual Servoing Platform  version 3.0.0
vpSphere.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Sphere feature.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
39 #include <visp3/core/vpSphere.h>
40 #include <visp3/core/vpFeatureDisplay.h>
41 
42 
43 void
45 {
46 
47  oP.resize(4) ;
48  cP.resize(4) ;
49 
50  p.resize(5) ;
51 }
52 
53 void
55 {
56  this->oP = oP_ ;
57 }
58 
59 void
60 vpSphere::setWorldCoordinates(const double X0, const double Y0,
61  const double Z0, const double R)
62 {
63  oP[0] = X0 ;
64  oP[1] = Y0 ;
65  oP[2] = Z0 ;
66  oP[3] = R ;
67 }
68 
69 
70 
72 {
73  init() ;
74 }
75 
76 
78 {
79  init() ;
80  setWorldCoordinates(oP_) ;
81 }
82 
83 vpSphere::vpSphere(const double X0, const double Y0,
84  const double Z0, const double R)
85 {
86  init() ;
87  setWorldCoordinates(X0, Y0, Z0, R) ;
88 }
89 
91 {
92 }
93 
94 
96 void
98 {
99  projection(cP,p) ;
100 }
101 
103 void
105 {
106  double x0, y0, z0; //variables intermediaires
107 // double k0, k1, k2, k3, k4; //variables intermediaires
108  double E, A, B; //variables intermediaires
109 
110  //calcul des parametres M20, M11, M02 de l'ellipse
111  double s, a, b, r, e; //variables intermediaires
112  r = cP_[3];
113 
114  x0 = cP_[0] ;
115  y0 = cP_[1] ;
116  z0 = cP_[2] ;
117 
118  s = r*r - y0*y0 -z0*z0;
119 
120 // k0 = (r*r - x0*x0 -z0*z0)/s;
121 // k1 = (x0*y0)/s;
122 // k2 = (x0*z0)/s;
123 // k3 = (y0*z0)/s;
124 // k4 = (r*r - x0*x0 -y0*y0)/s;
125 
126  if ((s = z0*z0 - r*r) < 0.0)
127  {
128  vpERROR_TRACE("sphere derriere le plan image\n");
129  }
130 
131  p_[0] = x0*z0/s ; //x
132  p_[1] = y0*z0/s ; //y
133 
134  if (fabs(x0) > 1e-6)
135  {
136  // vpERROR_TRACE(" %f",r) ;
137  e = y0/x0;
138  b = r/sqrt(s);
139  if ((a = x0*x0 + y0*y0 + z0*z0 - r*r) < 0.0)
140  {
141  vpERROR_TRACE("sphere derriere le plan image\n");
142  }
143  a = r*sqrt(a)/s;
144  if (fabs(e) <= 1.0)
145  {
146  E = e;
147  A = a;
148  B = b;
149  }
150  else
151  {
152  E = -1.0/e;
153  A = b;
154  B = a;
155  }
156  }
157  else
158  {
159  // vpERROR_TRACE(" %f",r) ;
160  E = 0.0;
161  A = r/sqrt(s);
162  B = r*sqrt(y0*y0+z0*z0-r*r)/s;
163  }
164 
165  p_[2] = ( A*A + B*B * E*E) / (1.0 + E*E); // mu20
166  p_[3] = ( A*A - B*B) * E / (1.0 + E*E); // mu11
167  p_[4] = ( B*B + A*A * E*E) / (1.0 + E*E); // mu02
168 
169  // vpERROR_TRACE(" %f",r) ;
170 
171  // std::cout << p.t() ;
172 }
174 void
176 {
177  changeFrame(cMo,cP) ;
178 }
179 
181 void
183 {
184  double x0, y0, z0; //variables intermediaires
185 
186  x0 = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2] + cMo[0][3];
187  y0 = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2] + cMo[1][3];
188  z0 = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2] + cMo[2][3];
189 
190  cP_[3] = oP[3];
191 
192  cP_[0] = x0 ;
193  cP_[1] = y0 ;
194  cP_[2] = z0 ;
195 }
196 
199 {
200  vpSphere *feature = new vpSphere(*this) ;
201  return feature ;
202 }
203 
204 
205 
206 // non destructive wrt. cP and p
208  const vpHomogeneousMatrix &cMo,
209  const vpCameraParameters &cam,
210  const vpColor &color,
211  const unsigned int thickness)
212 {
213  vpColVector _cP, _p ;
214  changeFrame(cMo,_cP) ;
215  projection(_cP,_p) ;
216  vpFeatureDisplay::displayEllipse(_p[0],_p[1],_p[2],_p[3], _p[4],
217  cam, I, color, thickness) ;
218 
219 }
220 
221 
222 
224  const vpCameraParameters &cam,
225  const vpColor &color,
226  const unsigned int thickness)
227 {
228  vpFeatureDisplay::displayEllipse(p[0],p[1],p[2],p[3], p[4],
229  cam, I, color, thickness) ;
230 }
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)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP)
Perspective projection of the circle.
Definition: vpSphere.cpp:182
vpSphere * duplicate() const
for memory issue (used by the vpServo class only)
Definition: vpSphere.cpp:198
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
virtual ~vpSphere()
Definition: vpSphere.cpp:90
Class that defines what is a sphere.
Definition: vpSphere.h:60
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:54
vpColVector cP
Definition: vpTracker.h:77
void projection()
perspective projection of the sphere
Definition: vpSphere.cpp:97
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, const unsigned int thickness=1)
Definition: vpSphere.cpp:223
Generic class defining intrinsic camera parameters.
vpSphere()
Definition: vpSphere.cpp:71
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void init()
Definition: vpSphere.cpp:44
vpColVector p
Definition: vpTracker.h:73
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:217