Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpSphere.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
34#include <visp3/core/vpFeatureDisplay.h>
35#include <visp3/core/vpSphere.h>
36
42{
43 const unsigned int val_4 = 4;
44 const unsigned int val_5 = 5;
45 oP.resize(val_4);
46 cP.resize(val_4);
47
48 p.resize(val_5);
49}
50
60void vpSphere::setWorldCoordinates(const vpColVector &oP_) { this->oP = oP_; }
61
71void vpSphere::setWorldCoordinates(double oX, double oY, double oZ, double R)
72{
73 const unsigned int index_0 = 0;
74 const unsigned int index_1 = 1;
75 const unsigned int index_2 = 2;
76 const unsigned int index_3 = 3;
77 oP[index_0] = oX;
78 oP[index_1] = oY;
79 oP[index_2] = oZ;
80 oP[index_3] = R;
81}
82
87
97{
98 init();
100}
101
111vpSphere::vpSphere(double oX, double oY, double oZ, double R)
112{
113 init();
114 setWorldCoordinates(oX, oY, oZ, R);
115}
116
125
141{
142 const unsigned int val_5 = 5;
143 p_.resize(val_5, false);
144 double x0, y0, z0;
145 double E, A, B;
146 const unsigned int index_0 = 0;
147 const unsigned int index_1 = 1;
148 const unsigned int index_2 = 2;
149 const unsigned int index_3 = 3;
150 const unsigned int index_4 = 4;
151
152 // calcul des parametres M20, M11, M02 de l'ellipse
153 double s, r;
154 r = cP_[index_3];
155
156 x0 = cP_[index_0];
157 y0 = cP_[index_1];
158 z0 = cP_[index_2];
159
160 s = (r * r) - (y0 * y0) - (z0 * z0);
161
162 if ((s = ((z0 * z0) - (r * r))) < 0.0) {
163 throw(vpException(vpException::fatalError, "Error: Sphere is behind image plane"));
164 }
165
166 p_[0] = (x0 * z0) / s; // x
167 p_[1] = (y0 * z0) / s; // y
168
169 if (fabs(x0) > 1e-6) {
170 double e = y0 / x0;
171 double b = r / sqrt(s);
172 double a = ((x0 * x0) + (y0 * y0) + (z0 * z0)) - (r * r);
173 if (a < 0.0) {
174 throw(vpException(vpException::fatalError, "Error: Sphere is behind image plane"));
175 }
176 a = (r * sqrt(a)) / s;
177 if (fabs(e) <= 1.0) {
178 E = e;
179 A = a;
180 B = b;
181 }
182 else {
183 E = -1.0 / e;
184 A = b;
185 B = a;
186 }
187 }
188 else {
189 E = 0.0;
190 A = r / sqrt(s);
191 B = (r * sqrt(((y0 * y0) + (z0 * z0)) - (r * r))) / s;
192 }
193
194 // Chaumette PhD Thesis 1990, eq 2.72 divided by 4 since n_ij = mu_ij_chaumette_thesis / 4
195 double det = 4 * (1.0 + vpMath::sqr(E));
196 double n20 = (vpMath::sqr(A) + vpMath::sqr(B * E)) / det;
197 double n11 = ((vpMath::sqr(A) - vpMath::sqr(B)) * E) / det;
198 double n02 = (vpMath::sqr(B) + vpMath::sqr(A * E)) / det;
199
200 p_[index_2] = n20;
201 p_[index_3] = n11;
202 p_[index_4] = n02;
203}
204
212
221{
222 const unsigned int val_4 = 4;
223 cP_.resize(val_4, false);
224
225 const unsigned int index_0 = 0;
226 const unsigned int index_1 = 1;
227 const unsigned int index_2 = 2;
228 const unsigned int index_3 = 3;
229
230 double x0, y0, z0; // variables intermediaires
231
232 x0 = (cMo[index_0][0] * oP[0]) + (cMo[index_0][1] * oP[1]) + (cMo[index_0][index_2] * oP[index_2]) + cMo[index_0][index_3];
233 y0 = (cMo[index_1][0] * oP[0]) + (cMo[index_1][1] * oP[1]) + (cMo[index_1][index_2] * oP[index_2]) + cMo[index_1][index_3];
234 z0 = (cMo[index_2][0] * oP[0]) + (cMo[index_2][1] * oP[1]) + (cMo[index_2][index_2] * oP[index_2]) + cMo[index_2][index_3];
235
236 cP_[index_3] = oP[index_3];
237
238 cP_[index_0] = x0;
239 cP_[index_1] = y0;
240 cP_[index_2] = z0;
241}
242
245{
246 vpSphere *feature = new vpSphere(*this);
247 return feature;
248}
249
262 const vpColor &color, unsigned int thickness)
263{
264 vpColVector v_cP, v_p;
265 changeFrame(cMo, v_cP);
266 projection(v_cP, v_p);
267 const unsigned int index_0 = 0;
268 const unsigned int index_1 = 1;
269 const unsigned int index_2 = 2;
270 const unsigned int index_3 = 3;
271 const unsigned int index_4 = 4;
272 vpFeatureDisplay::displayEllipse(v_p[index_0], v_p[index_1], v_p[index_2], v_p[index_3], v_p[index_4], cam, I, color, thickness);
273}
274
287 const vpColor &color, unsigned int thickness)
288{
289 vpColVector v_cP, v_p;
290 changeFrame(cMo, v_cP);
291 projection(v_cP, v_p);
292 const unsigned int index_0 = 0;
293 const unsigned int index_1 = 1;
294 const unsigned int index_2 = 2;
295 const unsigned int index_3 = 3;
296 const unsigned int index_4 = 4;
297 vpFeatureDisplay::displayEllipse(v_p[index_0], v_p[index_1], v_p[index_2], v_p[index_3], v_p[index_4], cam, I, color, thickness);
298}
299
309 unsigned int thickness)
310{
311 const unsigned int index_0 = 0;
312 const unsigned int index_1 = 1;
313 const unsigned int index_2 = 2;
314 const unsigned int index_3 = 3;
315 const unsigned int index_4 = 4;
316 vpFeatureDisplay::displayEllipse(p[index_0], p[index_1], p[index_2], p[index_3], p[index_4], cam, I, color, thickness);
317}
318
327void vpSphere::display(const vpImage<vpRGBa> &I, const vpCameraParameters &cam, const vpColor &color,
328 unsigned int thickness)
329{
330 const unsigned int index_0 = 0;
331 const unsigned int index_1 = 1;
332 const unsigned int index_2 = 2;
333 const unsigned int index_3 = 3;
334 const unsigned int index_4 = 4;
335 vpFeatureDisplay::displayEllipse(p[index_0], p[index_1], p[index_2], p[index_3], p[index_4], cam, I, color, thickness);
336}
337END_VISP_NAMESPACE
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
static void displayEllipse(double x, double y, double n20, double n11, double n02, 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.
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double sqr(double x)
Definition vpMath.h:203
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, unsigned int thickness=1) VP_OVERRIDE
Definition vpSphere.cpp:308
void init() VP_OVERRIDE
Definition vpSphere.cpp:41
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition vpSphere.cpp:60
void projection() VP_OVERRIDE
Definition vpSphere.cpp:124
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition vpSphere.cpp:220
vpSphere * duplicate() const VP_OVERRIDE
For memory issue (used by the vpServo class only).
Definition vpSphere.cpp:244
vpColVector cP
Definition vpTracker.h:73
vpColVector p
Definition vpTracker.h:69