Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
vpCylinder.cpp
1
/*
2
* ViSP, open source Visual Servoing Platform software.
3
* Copyright (C) 2005 - 2025 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
* Cylinder feature.
32
*/
33
34
#include <visp3/core/vpCylinder.h>
35
#include <visp3/core/vpFeatureDisplay.h>
36
37
BEGIN_VISP_NAMESPACE
38
void
vpCylinder::init
()
39
{
40
oP
.resize(7);
41
cP
.resize(7);
42
43
p
.resize(4);
44
}
45
61
void
vpCylinder::setWorldCoordinates
(
const
vpColVector
&o_P) { this->
oP
= o_P; }
62
77
void
vpCylinder::setWorldCoordinates
(
double
oA,
double
oB,
double
oC,
double
oX,
double
oY,
double
oZ,
double
R)
78
{
79
oP
[0] = oA;
80
oP
[1] = oB;
81
oP
[2] = oC;
82
oP
[3] = oX;
83
oP
[4] = oY;
84
oP
[5] = oZ;
85
oP
[6] = R;
86
}
87
91
vpCylinder::vpCylinder
() {
init
(); }
92
107
vpCylinder::vpCylinder
(
const
vpColVector
&o_P)
108
{
109
init
();
110
setWorldCoordinates
(o_P);
111
}
112
129
vpCylinder::vpCylinder
(
double
oA,
double
oB,
double
oC,
double
oX,
double
oY,
double
oZ,
double
R)
130
{
131
init
();
132
setWorldCoordinates
(oA, oB, oC, oX, oY, oZ, R);
133
}
134
158
void
vpCylinder::projection
() {
projection
(
cP
,
p
); }
159
188
void
vpCylinder::projection
(
const
vpColVector
&cP_,
vpColVector
&p_)
const
189
{
190
p_.
resize
(4,
false
);
191
192
double
co, si, e, x0, y0, z0;
193
double
A, B, C, X0, Y0, Z0, R;
194
double
s, a, b, c, zero;
195
196
A = cP_[0];
197
B = cP_[1];
198
C = cP_[2];
199
X0 = cP_[3];
200
Y0 = cP_[4];
201
Z0 = cP_[5];
202
R = cP_[6];
203
zero = A * X0 + B * Y0 + C * Z0;
// should be zero for a good reprensetation of the cylinder
204
205
s = X0 * X0 + Y0 * Y0 + Z0 * Z0 - R * R - zero * zero;
206
if
(s < 0) {
207
throw
vpException
(
vpException::fatalError
,
"The camera is inside the cylinder with s=%f!"
, s);
208
}
209
s = 1.0 / sqrt(s);
210
a = X0 - A * zero;
//(1-A*A)*X0 - A*B*Y0 - A*C*Z0;
211
b = Y0 - B * zero;
// - A*B*X0 + (1-B*B)*Y0 - B*C*Z0;
212
c = Z0 - C * zero;
//- A*C*X0 - B*C*Y0 + (1-C*C)*Z0;
213
x0 = C * Y0 - B * Z0;
214
y0 = A * Z0 - C * X0;
215
z0 = B * X0 - A * Y0;
216
217
// rho1 / theta1
218
co = R * a * s - x0;
219
si = R * b * s - y0;
220
e = sqrt(co * co + si * si);
221
p_[0] = -(R * c * s - z0) / e;
// rho1
222
p_[1] = atan2(si, co);
// theta 1
223
224
// rho2 / theta2
225
co = R * a * s + x0;
226
si = R * b * s + y0;
227
e = sqrt(co * co + si * si);
228
p_[2] = -(R * c * s + z0) / e;
// rho2
229
p_[3] = atan2(si, co);
// theta2
230
}
231
241
void
vpCylinder::changeFrame
(
const
vpHomogeneousMatrix
&cMo) {
changeFrame
(cMo,
cP
); }
242
253
void
vpCylinder::changeFrame
(
const
vpHomogeneousMatrix
&cMo,
vpColVector
&cP_)
const
254
{
255
cP_.
resize
(7,
false
);
256
257
double
X1, Y1, Z1;
258
double
X2, Y2, Z2;
259
double
s, a, b, c;
260
261
double
oA, oB, oC, oX0, oY0, oZ0;
262
oA =
oP
[0];
263
oB =
oP
[1];
264
oC =
oP
[2];
265
oX0 =
oP
[3];
266
oY0 =
oP
[4];
267
oZ0 =
oP
[5];
268
269
X1 = cMo[0][0] * oA + cMo[0][1] * oB + cMo[0][2] * oC;
270
Y1 = cMo[1][0] * oA + cMo[1][1] * oB + cMo[1][2] * oC;
271
Z1 = cMo[2][0] * oA + cMo[2][1] * oB + cMo[2][2] * oC;
272
s = sqrt(X1 * X1 + Y1 * Y1 + Z1 * Z1);
273
a = X1 / s;
274
b = Y1 / s;
275
c = Z1 / s;
276
277
// set axis coordinates in camera frame
278
cP_[0] = a;
279
cP_[1] = b;
280
cP_[2] = c;
281
282
X2 = cMo[0][3] + cMo[0][0] * oX0 + cMo[0][1] * oY0 + cMo[0][2] * oZ0;
283
Y2 = cMo[1][3] + cMo[1][0] * oX0 + cMo[1][1] * oY0 + cMo[1][2] * oZ0;
284
Z2 = cMo[2][3] + cMo[2][0] * oX0 + cMo[2][1] * oY0 + cMo[2][2] * oZ0;
285
286
// adding the constraint X0 is the nearest point to the origin (A^T . X0 =
287
// 0) using the projection operator (I - AA^T) orthogonal to A
288
cP_[3] = (1 - a * a) * X2 - a * b * Y2 - a * c * Z2;
289
cP_[4] = -a * b * X2 + (1 - b * b) * Y2 - b * c * Z2;
290
cP_[5] = -a * c * X2 - b * c * Y2 + (1 - c * c) * Z2;
291
292
/* old version for the same onstraint
293
if ( fabs(a) > 0.25 )
294
{
295
double xx, yy, zz, xx1, yy1;
296
297
xx1 = a*Y2 - b*X2;
298
yy1 = a*Z2 - c*X2;
299
xx = -( b*xx1 + c*yy1);
300
yy = (( a*a + c*c ) * xx1 - b*c*yy1 ) /a;
301
zz = ( -b*c*xx1 + ( a*a + b*b )*yy1) /a;
302
303
// set point coordinates in camera frame
304
_cP[3] = xx ;
305
_cP[4] = yy ;
306
_cP[5] = zz ;
307
}
308
else if ( fabs(b) >0.25 )
309
{
310
double xx, yy, zz, xx1, yy1;
311
312
xx1 = a*Y2 - b*X2;
313
yy1 = c*Y2 - b*Z2;
314
xx = - (( b*b + c*c ) * xx1 - a*c*yy1 ) /b;
315
yy = a*xx1 + c*yy1;
316
zz = - ( -a*c*xx1 + (a*a + b*b) * yy1 ) /b;
317
318
319
// set point coordinates in camera frame
320
_cP[3] = xx ;
321
_cP[4] = yy ;
322
_cP[5] = zz ;
323
}
324
else
325
{
326
double xx, yy, zz, xx1, yy1;
327
328
xx1 = a*Z2 - c*X2;
329
yy1 = b*Z2 - c*Y2;
330
xx = (-( b*b + c*c ) * xx1 - a*c*yy1 ) /c;
331
yy = ( a*b*xx1 - ( a*a + c*c )*yy1) /c;
332
zz = a*xx1 + b*yy1;
333
334
// set point coordinates in camera frame
335
_cP[3] = xx ;
336
_cP[4] = yy ;
337
_cP[5] = zz ;
338
}
339
*/
340
// radius
341
cP_[6] =
oP
[6];
342
}
343
348
double
vpCylinder::computeZ
(
double
x,
double
y)
const
349
{
350
double
A = x * x + y * y + 1 - ((
getA
() * x +
getB
() * y +
getC
()) * (
getA
() * x +
getB
() * y +
getC
()));
351
double
B = (x *
getX
() + y *
getY
() +
getZ
());
352
double
C =
getX
() *
getX
() +
getY
() *
getY
() +
getZ
() *
getZ
() -
getR
() *
getR
();
353
354
return
(B - std::sqrt(B * B - A * C)) / A;
355
}
356
358
vpCylinder
*
vpCylinder::duplicate
()
const
359
{
360
vpCylinder
*feature =
new
vpCylinder
(*
this
);
361
return
feature;
362
}
363
375
376
void
vpCylinder::display
(
const
vpImage<unsigned char>
&I,
const
vpHomogeneousMatrix
&cMo,
const
vpCameraParameters
&cam,
377
const
vpColor
&color,
unsigned
int
thickness)
378
{
379
380
vpColVector
_cP(7), _p(4);
381
changeFrame
(cMo, _cP);
382
projection
(_cP, _p);
383
vpFeatureDisplay::displayCylinder
(_p[0], _p[1], _p[2], _p[3], cam, I, color, thickness);
384
}
385
397
398
void
vpCylinder::display
(
const
vpImage<vpRGBa>
&I,
const
vpHomogeneousMatrix
&cMo,
const
vpCameraParameters
&cam,
399
const
vpColor
&color,
unsigned
int
thickness)
400
{
401
402
vpColVector
_cP(7), _p(4);
403
changeFrame
(cMo, _cP);
404
projection
(_cP, _p);
405
vpFeatureDisplay::displayCylinder
(_p[0], _p[1], _p[2], _p[3], cam, I, color, thickness);
406
}
407
416
void
vpCylinder::display
(
const
vpImage<unsigned char>
&I,
const
vpCameraParameters
&cam,
const
vpColor
&color,
417
unsigned
int
thickness)
418
{
419
vpFeatureDisplay::displayCylinder
(
p
[0],
p
[1],
p
[2],
p
[3], cam, I, color, thickness);
420
}
421
430
void
vpCylinder::display
(
const
vpImage<vpRGBa>
&I,
const
vpCameraParameters
&cam,
const
vpColor
&color,
431
unsigned
int
thickness)
432
{
433
vpFeatureDisplay::displayCylinder
(
p
[0],
p
[1],
p
[2],
p
[3], cam, I, color, thickness);
434
}
435
END_VISP_NAMESPACE
vpCameraParameters
Generic class defining intrinsic camera parameters.
Definition
vpCameraParameters.h:311
vpColVector
Implementation of column vector and the associated operations.
Definition
vpColVector.h:191
vpColVector::resize
void resize(unsigned int i, bool flagNullify=true)
Definition
vpColVector.h:1159
vpColor
Class to define RGB colors available for display functionalities.
Definition
vpColor.h:157
vpCylinder::getZ
double getZ() const
Definition
vpCylinder.h:183
vpCylinder::getB
double getB() const
Definition
vpCylinder.h:163
vpCylinder::init
void init() VP_OVERRIDE
Definition
vpCylinder.cpp:38
vpCylinder::display
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, unsigned int thickness=1) VP_OVERRIDE
Definition
vpCylinder.cpp:416
vpCylinder::getX
double getX() const
Definition
vpCylinder.h:173
vpCylinder::getY
double getY() const
Definition
vpCylinder.h:178
vpCylinder::changeFrame
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition
vpCylinder.cpp:253
vpCylinder::getA
double getA() const
Definition
vpCylinder.h:158
vpCylinder::vpCylinder
vpCylinder()
Definition
vpCylinder.cpp:91
vpCylinder::projection
void projection() VP_OVERRIDE
Definition
vpCylinder.cpp:158
vpCylinder::getR
double getR() const
Definition
vpCylinder.h:188
vpCylinder::duplicate
vpCylinder * duplicate() const VP_OVERRIDE
For memory issue (used by the vpServo class only).
Definition
vpCylinder.cpp:358
vpCylinder::getC
double getC() const
Definition
vpCylinder.h:168
vpCylinder::computeZ
double computeZ(double x, double y) const
Definition
vpCylinder.cpp:348
vpCylinder::setWorldCoordinates
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition
vpCylinder.cpp:61
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::fatalError
@ fatalError
Fatal error.
Definition
vpException.h:72
vpFeatureDisplay::displayCylinder
static void displayCylinder(double rho1, double theta1, double rho2, double theta2, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
Definition
vpFeatureDisplay.cpp:122
vpForwardProjection::oP
vpColVector oP
Definition
vpForwardProjection.h:205
vpHomogeneousMatrix
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition
vpHomogeneousMatrix.h:221
vpImage
Definition of the vpImage class member functions.
Definition
vpImage.h:131
vpTracker::cP
vpColVector cP
Definition
vpTracker.h:73
vpTracker::p
vpColVector p
Definition
vpTracker.h:69
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
core
src
tracking
forward-projection
vpCylinder.cpp
Generated by
1.17.0