Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpFeaturePoint.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/vpFeaturePoint.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
56attributes and members directly related to the vpBasicFeature needs
57other functionalities ar useful but not mandatory
58
59*/
60
66{
67 // feature dimension
68 dim_s = 2;
69 nbParameters = 3;
70
71 // memory allocation
72 s.resize(dim_s);
73 if (flags == nullptr)
74 flags = new bool[nbParameters];
75 for (unsigned int i = 0; i < nbParameters; i++)
76 flags[i] = false;
77
78 // default value Z (1 meters)
79 Z = 1;
80}
81
86
93void vpFeaturePoint::set_Z(double Z_)
94{
95 this->Z = Z_;
96 flags[2] = true;
97}
98
105double vpFeaturePoint::get_Z() const { return Z; }
106
115{
116 s[0] = x;
117 flags[0] = true;
118}
119
126double vpFeaturePoint::get_x() const { return s[0]; }
127
135{
136 s[1] = y;
137 flags[1] = true;
138}
139
146double vpFeaturePoint::get_y() const { return s[1]; }
147
159void vpFeaturePoint::set_xyZ(double x_, double y_, double Z_)
160{
161 set_x(x_);
162 set_y(y_);
163 set_Z(Z_);
164 for (unsigned int i = 0; i < nbParameters; i++)
165 flags[i] = true;
166}
167
212{
213 vpMatrix L;
214
215 L.resize(0, 6);
216
218 for (unsigned int i = 0; i < nbParameters; i++) {
219 if (flags[i] == false) {
220 switch (i) {
221 case 0:
222 vpTRACE("Warning !!! The interaction matrix is computed but x was "
223 "not set yet");
224 break;
225 case 1:
226 vpTRACE("Warning !!! The interaction matrix is computed but y was "
227 "not set yet");
228 break;
229 case 2:
230 vpTRACE("Warning !!! The interaction matrix is computed but Z was "
231 "not set yet");
232 break;
233 default:
234 vpTRACE("Problem during the reading of the variable flags");
235 }
236 }
237 }
238 resetFlags();
239 }
240
241 double x_ = get_x();
242 double y_ = get_y();
243 double Z_ = get_Z();
244
245 if (Z_ < 0) {
246 vpERROR_TRACE("Point is behind the camera ");
247 std::cout << "Z = " << Z_ << std::endl;
248
249 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
250 }
251
252 if (fabs(Z_) < 1e-6) {
253 vpERROR_TRACE("Point Z coordinates is null ");
254 std::cout << "Z = " << Z_ << std::endl;
255
256 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
257 }
258
259 if (vpFeaturePoint::selectX() & select) {
260 vpMatrix Lx(1, 6);
261 Lx = 0;
262
263 Lx[0][0] = -1 / Z_;
264 Lx[0][1] = 0;
265 Lx[0][2] = x_ / Z_;
266 Lx[0][3] = x_ * y_;
267 Lx[0][4] = -(1 + x_ * x_);
268 Lx[0][5] = y_;
269
270 L = vpMatrix::stack(L, Lx);
271 }
272
273 if (vpFeaturePoint::selectY() & select) {
274 vpMatrix Ly(1, 6);
275 Ly = 0;
276
277 Ly[0][0] = 0;
278 Ly[0][1] = -1 / Z_;
279 Ly[0][2] = y_ / Z_;
280 Ly[0][3] = 1 + y_ * y_;
281 Ly[0][4] = -x_ * y_;
282 Ly[0][5] = -x_;
283
284 L = vpMatrix::stack(L, Ly);
285 }
286 return L;
287}
288
325vpColVector vpFeaturePoint::error(const vpBasicFeature &s_star, unsigned int select)
326{
327 vpColVector e(0);
328
329 try {
330 if (vpFeaturePoint::selectX() & select) {
331 vpColVector ex(1);
332 ex[0] = s[0] - s_star[0];
333
334 e = vpColVector::stack(e, ex);
335 }
336
337 if (vpFeaturePoint::selectY() & select) {
338 vpColVector ey(1);
339 ey[0] = s[1] - s_star[1];
340 e = vpColVector::stack(e, ey);
341 }
342 }
343 catch (...) {
344 throw;
345 }
346
347 return e;
348}
349
369void vpFeaturePoint::print(unsigned int select) const
370{
371
372 std::cout << "Point: Z=" << get_Z();
373 if (vpFeaturePoint::selectX() & select)
374 std::cout << " x=" << get_x();
375 if (vpFeaturePoint::selectY() & select)
376 std::cout << " y=" << get_y();
377 std::cout << std::endl;
378}
379
392vpFeaturePoint &vpFeaturePoint::buildFrom(const double &x_, const double &y_, const double &Z_)
393{
394
395 s[0] = x_;
396 s[1] = y_;
397
398 this->Z = Z_;
399
400 if (Z_ < 0) {
401 vpERROR_TRACE("Point is behind the camera ");
402 std::cout << "Z = " << Z_ << std::endl;
403
404 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point is behind the camera "));
405 }
406
407 if (fabs(Z_) < 1e-6) {
408 vpERROR_TRACE("Point Z coordinates is null ");
409 std::cout << "Z = " << Z_ << std::endl;
410
411 throw(vpFeatureException(vpFeatureException::badInitializationError, "Point Z coordinates is null"));
412 }
413
414 for (unsigned int i = 0; i < nbParameters; ++i) {
415 flags[i] = true;
416 }
417 return *this;
418}
419
431 unsigned int thickness) const
432{
433 try {
434 double x, y;
435 x = get_x();
436 y = get_y();
437
438 vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness);
439
440 }
441 catch (...) {
442 vpERROR_TRACE("Error caught");
443 throw;
444 }
445}
446
458 unsigned int thickness) const
459{
460 try {
461 double x, y;
462 x = get_x();
463 y = get_y();
464
465 vpFeatureDisplay::displayPoint(x, y, cam, I, color, thickness);
466
467 }
468 catch (...) {
469 vpERROR_TRACE("Error caught");
470 throw;
471 }
472}
473
485{
486 vpFeaturePoint *feature = new vpFeaturePoint;
487 return feature;
488}
489
508unsigned int vpFeaturePoint::selectX() { return FEATURE_LINE[0]; }
509
528unsigned int vpFeaturePoint::selectY() { return FEATURE_LINE[1]; }
529END_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.
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
static unsigned int selectX()
void set_xyZ(double x, double y, double Z)
vpFeaturePoint * duplicate() const VP_OVERRIDE
vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL) VP_OVERRIDE
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
static unsigned int selectY()
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
void set_y(double y)
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
double get_y() const
double get_x() const
void set_x(double x)
void init() VP_OVERRIDE
double get_Z() const
void set_Z(double Z)
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