Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpFeatureLine.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 line visual feature.
32 */
33
38
39#include <visp3/visual_features/vpBasicFeature.h>
40#include <visp3/visual_features/vpFeatureLine.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// simple math function (round)
50#include <visp3/core/vpMath.h>
51
52// Display Issue
53
54// Meter/pixel conversion
55#include <visp3/core/vpCameraParameters.h>
56
57// Color / image / display
58#include <visp3/core/vpColor.h>
59#include <visp3/core/vpImage.h>
60
61#include <visp3/core/vpFeatureDisplay.h>
62
63/*
64attributes and members directly related to the vpBasicFeature needs
65other functionalities ar useful but not mandatory
66*/
67
73{
74 // feature dimension
75 dim_s = 2;
76 nbParameters = 6;
77
78 // memory allocation
79 // x cos(theta) + y sin(theta) - rho = 0
80 // s[0] = rho
81 // s[1] = theta
82 s.resize(dim_s);
83 if (flags == nullptr)
84 flags = new bool[nbParameters];
85 for (unsigned int i = 0; i < nbParameters; i++)
86 flags[i] = false;
87
88 A = B = C = D = 0.0;
89}
90
94vpFeatureLine::vpFeatureLine() : A(0), B(0), C(0), D(0) { init(); }
95
103void vpFeatureLine::setRhoTheta(double rho, double theta)
104{
105 s[0] = rho;
106 s[1] = theta;
107 for (int i = 0; i < 2; i++)
108 flags[i] = true;
109}
110
125void vpFeatureLine::setABCD(double A_, double B_, double C_, double D_)
126{
127 this->A = A_;
128 this->B = B_;
129 this->C = C_;
130 this->D = D_;
131 for (unsigned int i = 2; i < nbParameters; i++)
132 flags[i] = true;
133}
134
187{
188 vpMatrix L;
189
191 for (unsigned int i = 0; i < nbParameters; i++) {
192 if (flags[i] == false) {
193 switch (i) {
194 case 0:
195 vpTRACE("Warning !!! The interaction matrix is computed but rho "
196 "was not set yet");
197 break;
198 case 1:
199 vpTRACE("Warning !!! The interaction matrix is computed but theta "
200 "was not set yet");
201 break;
202 case 2:
203 vpTRACE("Warning !!! The interaction matrix is computed but A was "
204 "not set yet");
205 break;
206 case 3:
207 vpTRACE("Warning !!! The interaction matrix is computed but B was "
208 "not set yet");
209 break;
210 case 4:
211 vpTRACE("Warning !!! The interaction matrix is computed but C was "
212 "not set yet");
213 break;
214 case 5:
215 vpTRACE("Warning !!! The interaction matrix is computed but D was "
216 "not set yet");
217 break;
218 default:
219 vpTRACE("Problem during the reading of the variable flags");
220 }
221 }
222 }
223 resetFlags();
224 }
225 double rho = s[0];
226 double theta = s[1];
227
228 double co = cos(theta);
229 double si = sin(theta);
230
231 if (fabs(D) < 1e-6) {
232 vpERROR_TRACE("Incorrect plane coordinates D is null, D = %f", D);
233
234 throw(vpFeatureException(vpFeatureException::badInitializationError, "Incorrect plane coordinates D"));
235 }
236
237 double lambda_theta = (A * si - B * co) / D;
238 double lambda_rho = (C + rho * A * co + rho * B * si) / D;
239
240 if (vpFeatureLine::selectRho() & select) {
241 vpMatrix Lrho(1, 6);
242
243 Lrho[0][0] = co * lambda_rho;
244 Lrho[0][1] = si * lambda_rho;
245 Lrho[0][2] = -rho * lambda_rho;
246 Lrho[0][3] = si * (1.0 + rho * rho);
247 Lrho[0][4] = -co * (1.0 + rho * rho);
248 Lrho[0][5] = 0.0;
249
250 L.stack(Lrho);
251 }
252
253 if (vpFeatureLine::selectTheta() & select) {
254 vpMatrix Ltheta(1, 6);
255
256 Ltheta[0][0] = co * lambda_theta;
257 Ltheta[0][1] = si * lambda_theta;
258 Ltheta[0][2] = -rho * lambda_theta;
259 Ltheta[0][3] = -rho * co;
260 Ltheta[0][4] = -rho * si;
261 Ltheta[0][5] = -1.0;
262
263 L.stack(Ltheta);
264 }
265 return L;
266}
267
306vpColVector vpFeatureLine::error(const vpBasicFeature &s_star, unsigned int select)
307{
308 vpColVector e(0);
309
310 try {
311 if (vpFeatureLine::selectRho() & select) {
312 vpColVector erho(1);
313 erho[0] = s[0] - s_star[0];
314
315 e = vpColVector::stack(e, erho);
316 }
317
318 if (vpFeatureLine::selectTheta() & select) {
319
320 double err = s[1] - s_star[1];
321 while (err < -M_PI)
322 err += 2 * M_PI;
323 while (err > M_PI)
324 err -= 2 * M_PI;
325
326 vpColVector etheta(1);
327 etheta[0] = err;
328 e = vpColVector::stack(e, etheta);
329 }
330 }
331 catch (...) {
332 throw;
333 }
334
335 return e;
336}
337
357
358void vpFeatureLine::print(unsigned int select) const
359{
360
361 std::cout << "Line:\t " << A << "X+" << B << "Y+" << C << "Z +" << D << "=0" << std::endl;
362 if (vpFeatureLine::selectRho() & select)
363 std::cout << " \trho=" << s[0];
364 if (vpFeatureLine::selectTheta() & select)
365 std::cout << " \ttheta=" << s[1];
366 std::cout << std::endl;
367}
368
383vpFeatureLine &vpFeatureLine::buildFrom(const double &rho, const double &theta)
384{
385 s[0] = rho;
386 s[1] = theta;
387 for (int i = 0; i < 2; i++) {
388 flags[i] = true;
389 }
390 return *this;
391}
392
421vpFeatureLine &vpFeatureLine::buildFrom(const double &rho, const double &theta, const double &A_, const double &B_, const double &C_, const double &D_)
422{
423 s[0] = rho;
424 s[1] = theta;
425 this->A = A_;
426 this->B = B_;
427 this->C = C_;
428 this->D = D_;
429 for (unsigned int i = 0; i < nbParameters; ++i) {
430 flags[i] = true;
431 }
432 return *this;
433}
434
446{
447 vpFeatureLine *feature = new vpFeatureLine;
448 return feature;
449}
450
462 unsigned int thickness) const
463{
464 try {
465 double rho, theta;
466 rho = getRho();
467 theta = getTheta();
468
469 vpFeatureDisplay::displayLine(rho, theta, cam, I, color, thickness);
470
471 }
472 catch (...) {
473 vpERROR_TRACE("Error caught");
474 throw;
475 }
476}
477
488void vpFeatureLine::display(const vpCameraParameters &cam, const vpImage<vpRGBa> &I, const vpColor &color,
489 unsigned int thickness) const
490{
491 try {
492 double rho, theta;
493 rho = getRho();
494 theta = getTheta();
495
496 vpFeatureDisplay::displayLine(rho, theta, cam, I, color, thickness);
497
498 }
499 catch (...) {
500 vpERROR_TRACE("Error caught");
501 throw;
502 }
503}
504
523unsigned int vpFeatureLine::selectRho() { return FEATURE_LINE[0]; }
524
544unsigned int vpFeatureLine::selectTheta() { return FEATURE_LINE[1]; }
545END_VISP_NAMESPACE
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 displayLine(double rho, double theta, 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 init() VP_OVERRIDE
void setRhoTheta(double rho, double theta)
double getTheta() const
static unsigned int selectRho()
vpMatrix interaction(unsigned int select=FEATURE_ALL) VP_OVERRIDE
vpFeatureLine & buildFrom(const double &rho, const double &theta)
vpFeatureLine * duplicate() const VP_OVERRIDE
void setABCD(double A, double B, double C, double D)
static unsigned int selectTheta()
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
double getRho() const
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const VP_OVERRIDE
vpColVector error(const vpBasicFeature &s_star, 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