Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpFeaturePointPolar.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 * 2D point visual feature.
32 */
33
38
39#include <visp3/visual_features/vpBasicFeature.h>
40#include <visp3/visual_features/vpFeaturePointPolar.h>
41
42// Exception
43#include <visp3/core/vpException.h>
44#include <visp3/visual_features/vpFeatureException.h>
45
46// Debug trace
47#include <visp3/core/vpDebug.h>
48
49// math
50#include <visp3/core/vpMath.h>
51
52#include <visp3/core/vpFeatureDisplay.h>
53
54/*
55
56 attributes and members directly related to the vpBasicFeature needs
57 other functionalities ar useful but not mandatory
58
59*/
60
72{
73 // feature dimension
74 dim_s = 2;
75 nbParameters = 3;
76
77 // memory allocation
78 s.resize(dim_s);
79 if (flags == nullptr)
80 flags = new bool[nbParameters];
81 for (unsigned int i = 0; i < nbParameters; i++)
82 flags[i] = false;
83
84 // default value Z (1 meters)
85 Z = 1;
86}
87
99
106{
107 s[0] = rho;
108 flags[0] = true;
109}
110
116{
117 s[1] = theta;
118 flags[1] = true;
119}
120
126{
127 this->Z = Z_;
128 flags[2] = true;
129}
130
140void vpFeaturePointPolar::set_rhoThetaZ(double rho, double theta, double Z_)
141{
142 set_rho(rho);
143 set_theta(theta);
144 set_Z(Z_);
145
146 for (unsigned int i = 0; i < nbParameters; ++i) {
147 flags[i] = true;
148 }
149}
150
156double vpFeaturePointPolar::get_rho() const { return s[0]; }
157
163double vpFeaturePointPolar::get_theta() const { return s[1]; }
168double vpFeaturePointPolar::get_Z() const { return this->Z; }
169
245{
246 vpMatrix L;
247
248 L.resize(0, 6);
249
251 for (unsigned int i = 0; i < nbParameters; i++) {
252 if (flags[i] == false) {
253 switch (i) {
254 case 0:
255 vpTRACE("Warning !!! The interaction matrix is computed but rho "
256 "was not set yet");
257 break;
258 case 1:
259 vpTRACE("Warning !!! The interaction matrix is computed but theta "
260 "was not set yet");
261 break;
262 case 2:
263 vpTRACE("Warning !!! The interaction matrix is computed but Z was "
264 "not set yet");
265 break;
266 default:
267 vpTRACE("Problem during the reading of the variable flags");
268 }
269 }
270 }
271 resetFlags();
272 }
273
274 double rho = get_rho();
275 double theta = get_theta();
276 double Z_ = get_Z();
277
278 double c_ = cos(theta);
279 double s_ = sin(theta);
280
281 double rho2 = rho * rho;
282
283 if (fabs(rho) < 1e-6) {
284 vpERROR_TRACE("rho polar coordinate of the point is null");
285 std::cout << "rho = " << rho << std::endl;
286
287 throw(vpFeatureException(vpFeatureException::badInitializationError, "rho polar coordinate of the point is null"));
288 }
289
290 if (Z_ < 0) {
291 vpERROR_TRACE("Point is behind the camera ");
292 std::cout << "Z = " << Z_ << std::endl;
293
294 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
295 }
296
297 if (fabs(Z_) < 1e-6) {
298 vpERROR_TRACE("Point Z coordinates is null ");
299 std::cout << "Z = " << Z_ << std::endl;
300
301 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
302 }
303
304 if (vpFeaturePointPolar::selectRho() & select) {
305 vpMatrix Lrho(1, 6);
306 Lrho = 0;
307
308 Lrho[0][0] = -c_ / Z_;
309 Lrho[0][1] = -s_ / Z_;
310 Lrho[0][2] = rho / Z_;
311 Lrho[0][3] = (1 + rho2) * s_;
312 Lrho[0][4] = -(1 + rho2) * c_;
313 Lrho[0][5] = 0;
314
315 // printf("Lrho: rho %f theta %f Z %f\n", rho, theta, Z);
316 // std::cout << "Lrho: " << Lrho << std::endl;
317
318 L = vpMatrix::stack(L, Lrho);
319 }
320
321 if (vpFeaturePointPolar::selectTheta() & select) {
322 vpMatrix Ltheta(1, 6);
323 Ltheta = 0;
324
325 Ltheta[0][0] = s_ / (rho * Z_);
326 Ltheta[0][1] = -c_ / (rho * Z_);
327 Ltheta[0][2] = 0;
328 Ltheta[0][3] = c_ / rho;
329 Ltheta[0][4] = s_ / rho;
330 Ltheta[0][5] = -1;
331
332 // printf("Ltheta: rho %f theta %f Z %f\n", rho, theta, Z);
333 // std::cout << "Ltheta: " << Ltheta << std::endl;
334 L = vpMatrix::stack(L, Ltheta);
335 }
336 return L;
337}
338
381vpColVector vpFeaturePointPolar::error(const vpBasicFeature &s_star, unsigned int select)
382{
383 vpColVector e(0);
384
385 try {
386 if (vpFeaturePointPolar::selectRho() & select) {
387 vpColVector erho(1);
388 erho[0] = s[0] - s_star[0];
389
390 e = vpColVector::stack(e, erho);
391 }
392
393 if (vpFeaturePointPolar::selectTheta() & select) {
394
395 // printf("err: %f - %f = %f\n", s[1], s_star[1], s[1] -
396 // s_star[1]);
397 double err = s[1] - s_star[1];
398
399 // printf("Error: %f ", err );
400 while (err < -M_PI)
401 err += 2 * M_PI;
402 while (err > M_PI)
403 err -= 2 * M_PI;
404 // printf(" modif %f \n", err );
405
406 vpColVector etheta(1);
407 etheta[0] = err;
408 e = vpColVector::stack(e, etheta);
409 }
410 }
411 catch (...) {
412 throw;
413 }
414
415 return e;
416}
417
439void vpFeaturePointPolar::print(unsigned int select) const
440{
441
442 std::cout << "Point: Z=" << get_Z();
443 if (vpFeaturePointPolar::selectRho() & select)
444 std::cout << " rho=" << get_rho();
446 std::cout << " theta=" << get_theta();
447 std::cout << std::endl;
448}
449
450vpFeaturePointPolar &vpFeaturePointPolar::buildFrom(const double &rho, const double &theta, const double &Z_)
451{
452
453 s[0] = rho;
454 s[1] = theta;
455
456 this->Z = Z_;
457
458 if (Z < 0) {
459 vpERROR_TRACE("Point is behind the camera ");
460 std::cout << "Z = " << Z << std::endl;
461
462 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
463 }
464
465 if (fabs(Z) < 1e-6) {
466 vpERROR_TRACE("Point Z coordinates is null ");
467 std::cout << "Z = " << Z << std::endl;
468
469 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
470 }
471
472 for (unsigned int i = 0; i < nbParameters; ++i) {
473 flags[i] = true;
474 }
475 return *this;
476}
477
489 unsigned int thickness) const
490{
491 try {
492 double rho, theta;
493 rho = get_rho();
494 theta = get_theta();
495
496 double x, y;
497 x = rho * cos(theta);
498 y = rho * sin(theta);
499
500 vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness);
501 }
502 catch (...) {
503 vpERROR_TRACE("Error caught");
504 throw;
505 }
506}
507
519 unsigned int thickness) const
520{
521 try {
522 double rho, theta;
523 rho = get_rho();
524 theta = get_theta();
525
526 double x, y;
527 x = rho * cos(theta);
528 y = rho * sin(theta);
529
530 vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness);
531
532 }
533 catch (...) {
534 vpERROR_TRACE("Error caught");
535 throw;
536 }
537}
538
551{
553 return feature;
554}
555
579unsigned int vpFeaturePointPolar::selectRho() { return FEATURE_LINE[0]; }
580
604END_VISP_NAMESPACE
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition vpArray2D.h:448
vpColVector s
State of the visual feature.
unsigned int nbParameters
Number of parameters needed to compute the interaction matrix.
unsigned int dim_s
Dimension of the visual feature.
static const unsigned int FEATURE_LINE[32]
vpBasicFeatureDeallocatorType deallocate
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
void stack(double d)
Class to define RGB colors available for display functionalities.
Definition vpColor.h:157
static void displayPoint(double x, double y, const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1)
Error that can be emitted by the vpBasicFeature class and its derivates.
@ badInitializationError
Wrong feature initialization.
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
vpFeaturePointPolar & buildFrom(const double &rho, const double &theta, const double &Z)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
static unsigned int selectTheta()
void set_theta(double theta)
void init() VP_OVERRIDE
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) VP_OVERRIDE
vpFeaturePointPolar * duplicate() const VP_OVERRIDE
static unsigned int selectRho()
void set_rhoThetaZ(double rho, double theta, double Z)
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
Definition of the vpImage class member functions.
Definition vpImage.h:131
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
void stack(const vpMatrix &A)
#define vpTRACE
Definition vpDebug.h:450
#define vpERROR_TRACE
Definition vpDebug.h:423