Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpSphere.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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, r; //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  double e = y0/x0;
138  double b = r/sqrt(s);
139  double a = x0*x0 + y0*y0 + z0*z0 - r*r;
140  if (a < 0.0)
141  {
142  vpERROR_TRACE("sphere derriere le plan image\n");
143  }
144  a = r*sqrt(a)/s;
145  if (fabs(e) <= 1.0)
146  {
147  E = e;
148  A = a;
149  B = b;
150  }
151  else
152  {
153  E = -1.0/e;
154  A = b;
155  B = a;
156  }
157  }
158  else
159  {
160  // vpERROR_TRACE(" %f",r) ;
161  E = 0.0;
162  A = r/sqrt(s);
163  B = r*sqrt(y0*y0+z0*z0-r*r)/s;
164  }
165 
166  p_[2] = ( A*A + B*B * E*E) / (1.0 + E*E); // mu20
167  p_[3] = ( A*A - B*B) * E / (1.0 + E*E); // mu11
168  p_[4] = ( B*B + A*A * E*E) / (1.0 + E*E); // mu02
169 
170  // vpERROR_TRACE(" %f",r) ;
171 
172  // std::cout << p.t() ;
173 }
175 void
177 {
178  changeFrame(cMo,cP) ;
179 }
180 
182 void
184 {
185  double x0, y0, z0; //variables intermediaires
186 
187  x0 = cMo[0][0]*oP[0] + cMo[0][1]*oP[1] + cMo[0][2]*oP[2] + cMo[0][3];
188  y0 = cMo[1][0]*oP[0] + cMo[1][1]*oP[1] + cMo[1][2]*oP[2] + cMo[1][3];
189  z0 = cMo[2][0]*oP[0] + cMo[2][1]*oP[1] + cMo[2][2]*oP[2] + cMo[2][3];
190 
191  cP_[3] = oP[3];
192 
193  cP_[0] = x0 ;
194  cP_[1] = y0 ;
195  cP_[2] = z0 ;
196 }
197 
200 {
201  vpSphere *feature = new vpSphere(*this) ;
202  return feature ;
203 }
204 
205 
206 
207 // non destructive wrt. cP and p
209  const vpHomogeneousMatrix &cMo,
210  const vpCameraParameters &cam,
211  const vpColor &color,
212  const unsigned int thickness)
213 {
214  vpColVector _cP, _p ;
215  changeFrame(cMo,_cP) ;
216  projection(_cP,_p) ;
217  vpFeatureDisplay::displayEllipse(_p[0],_p[1],_p[2],_p[3], _p[4],
218  cam, I, color, thickness) ;
219 
220 }
221 
222 
223 
225  const vpCameraParameters &cam,
226  const vpColor &color,
227  const unsigned int thickness)
228 {
229  vpFeatureDisplay::displayEllipse(p[0],p[1],p[2],p[3], p[4],
230  cam, I, color, thickness) ;
231 }
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:183
vpSphere * duplicate() const
for memory issue (used by the vpServo class only)
Definition: vpSphere.cpp:199
#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:224
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:225