Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpPoint.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 * Point feature.
32 */
33
38
39#include <visp3/core/vpFeatureDisplay.h>
40#include <visp3/core/vpPoint.h>
41
44{
45 const unsigned int val_3 = 3;
46 const unsigned int val_4 = 4;
47 const unsigned int index_2 = 2;
48 const unsigned int index_3 = 3;
49 p.resize(val_3);
50 p = 0;
51 p[index_2] = 1.;
52 oP.resize(val_4);
53 oP = 0.;
54 oP[index_3] = 1.;
55 cP.resize(val_4);
56 cP = 0.;
57 cP[index_3] = 1.;
58
59 // default value Z (1 meters)
60 set_Z(1);
61}
62
64
71vpPoint::vpPoint(double oX, double oY, double oZ)
72{
73 init();
74 setWorldCoordinates(oX, oY, oZ);
75}
76
88{
89 init();
91}
92
103vpPoint::vpPoint(const std::vector<double> &oP_)
104{
105 init();
107}
108
116void vpPoint::setWorldCoordinates(double oX, double oY, double oZ)
117{
118 const unsigned int index_0 = 0;
119 const unsigned int index_1 = 1;
120 const unsigned int index_2 = 2;
121 const unsigned int index_3 = 3;
122 oP[index_0] = oX;
123 oP[index_1] = oY;
124 oP[index_2] = oZ;
125 oP[index_3] = 1;
126}
127
139{
140 const unsigned int index_0 = 0;
141 const unsigned int index_1 = 1;
142 const unsigned int index_2 = 2;
143 const unsigned int index_3 = 3;
144 const unsigned int val_3 = 3;
145 const unsigned int val_4 = 4;
146
147 if (oP_.size() == val_3) {
148 oP[index_0] = oP_[index_0];
149 oP[index_1] = oP_[index_1];
150 oP[index_2] = oP_[index_2];
151 oP[index_3] = 1.;
152 }
153 else if (oP_.size() == val_4) {
154 oP[index_0] = oP_[index_0];
155 oP[index_1] = oP_[index_1];
156 oP[index_2] = oP_[index_2];
157 oP[index_3] = oP_[index_3];
158 oP /= oP[index_3];
159 }
160 else {
161 throw(vpException(vpException::dimensionError, "Cannot initialize vpPoint from vector with size %d", oP_.size()));
162 }
163}
164
175void vpPoint::setWorldCoordinates(const std::vector<double> &oP_)
176{
177 const unsigned int index_0 = 0;
178 const unsigned int index_1 = 1;
179 const unsigned int index_2 = 2;
180 const unsigned int index_3 = 3;
181 const unsigned int val_3 = 3;
182 const unsigned int val_4 = 4;
183
184 if (oP_.size() == val_3) {
185 oP[index_0] = oP_[index_0];
186 oP[index_1] = oP_[index_1];
187 oP[index_2] = oP_[index_2];
188 oP[index_3] = 1.;
189 }
190 else if (oP_.size() == val_4) {
191 oP[index_0] = oP_[index_0];
192 oP[index_1] = oP_[index_1];
193 oP[index_2] = oP_[index_2];
194 oP[index_3] = oP_[index_3];
195 oP /= oP[index_3];
196 }
197 else {
198 throw(vpException(vpException::dimensionError, "Cannot initialize vpPoint from vector with size %d", oP_.size()));
199 }
200}
201
203void vpPoint::getWorldCoordinates(double &oX, double &oY, double &oZ)
204{
205 const unsigned int index_0 = 0;
206 const unsigned int index_1 = 1;
207 const unsigned int index_2 = 2;
208 oX = oP[index_0];
209 oY = oP[index_1];
210 oZ = oP[index_2];
211}
212
220
227void vpPoint::getWorldCoordinates(std::vector<double> &oP_)
228{
229 oP_.resize(oP.size());
230 unsigned int oP_size = oP.size();
231 for (unsigned int i = 0; i < oP_size; ++i) {
232 oP_[i] = oP[i];
233 }
234}
235
243
252void vpPoint::projection(const vpColVector &v_cP, vpColVector &v_p) const
253{
254 const unsigned int val_3 = 3;
255 const unsigned int index_0 = 0;
256 const unsigned int index_1 = 1;
257 const unsigned int index_2 = 2;
258 v_p.resize(val_3, false);
259
260 v_p[index_0] = v_cP[index_0] / v_cP[index_2];
261 v_p[index_1] = v_cP[index_1] / v_cP[index_2];
262 v_p[index_2] = 1;
263}
264
274{
275 const unsigned int val_4 = 4;
276 const unsigned int index_0 = 0;
277 const unsigned int index_1 = 1;
278 const unsigned int index_2 = 2;
279 const unsigned int index_3 = 3;
280 v_cP.resize(val_4, false);
281
282 v_cP[index_0] = (cMo[index_0][index_0] * oP[index_0]) + (cMo[index_0][index_1] * oP[index_1]) + (cMo[index_0][index_2] * oP[index_2]) + (cMo[index_0][index_3] * oP[index_3]);
283 v_cP[index_1] = (cMo[index_1][index_0] * oP[index_0]) + (cMo[index_1][index_1] * oP[index_1]) + (cMo[index_1][index_2] * oP[index_2]) + (cMo[index_1][index_3] * oP[index_3]);
284 v_cP[index_2] = (cMo[index_2][index_0] * oP[index_0]) + (cMo[index_2][index_1] * oP[index_1]) + (cMo[index_2][index_2] * oP[index_2]) + (cMo[index_2][index_3] * oP[index_3]);
285 v_cP[index_3] = (cMo[index_3][index_0] * oP[index_0]) + (cMo[index_3][index_1] * oP[index_1]) + (cMo[index_3][index_2] * oP[index_2]) + (cMo[index_3][index_3] * oP[index_3]);
286
287 double d = 1 / v_cP[index_3];
288 v_cP[index_0] *= d;
289 v_cP[index_1] *= d;
290 v_cP[index_2] *= d;
291 v_cP[index_3] *= d;
292}
293
303{
304 const unsigned int index_0 = 0;
305 const unsigned int index_1 = 1;
306 const unsigned int index_2 = 2;
307 const unsigned int index_3 = 3;
308 double X = (cMo[index_0][index_0] * oP[index_0]) + (cMo[index_0][index_1] * oP[index_1]) + (cMo[index_0][index_2] * oP[index_2]) + (cMo[index_0][index_3] * oP[index_3]);
309 double Y = (cMo[index_1][index_0] * oP[index_0]) + (cMo[index_1][index_1] * oP[index_1]) + (cMo[index_1][index_2] * oP[index_2]) + (cMo[index_1][index_3] * oP[index_3]);
310 double Z = (cMo[index_2][index_0] * oP[index_0]) + (cMo[index_2][index_1] * oP[index_1]) + (cMo[index_2][index_2] * oP[index_2]) + (cMo[index_2][index_3] * oP[index_3]);
311 double W = (cMo[index_3][index_0] * oP[index_0]) + (cMo[index_3][index_1] * oP[index_1]) + (cMo[index_3][index_2] * oP[index_2]) + (cMo[index_3][index_3] * oP[index_3]);
312
313 double d = 1. / W;
314 cP[index_0] = X * d;
315 cP[index_1] = Y * d;
316 cP[index_2] = Z * d;
317 cP[index_3] = 1.;
318}
319
322{
323 vpPoint *feature = new vpPoint(*this);
324 return feature;
325}
326
339 const vpColor &color, unsigned int thickness)
340{
341 vpColVector v_cP, v_p;
342 changeFrame(cMo, v_cP);
343 const unsigned int index_2 = 2;
344
345 if (v_cP[index_2] < 0) { // no display if point is behind the camera
346 return;
347 }
348
349 vpPoint::projection(v_cP, v_p);
350 vpFeatureDisplay::displayPoint(v_p[0], v_p[1], cam, I, color, thickness);
351}
352
365 const vpColor &color, unsigned int thickness)
366{
367 vpColVector v_cP, v_p;
368 changeFrame(cMo, v_cP);
369 const unsigned int index_2 = 2;
370
371 if (v_cP[index_2] < 0) { // no display if point is behind the camera
372 return;
373 }
374
375 vpPoint::projection(v_cP, v_p);
376 vpFeatureDisplay::displayPoint(v_p[0], v_p[1], cam, I, color, thickness);
377}
378
388 unsigned int thickness)
389{
390 vpFeatureDisplay::displayPoint(p[0], p[1], cam, I, color, thickness);
391}
392
401void vpPoint::display(const vpImage<vpRGBa> &I, const vpCameraParameters &cam, const vpColor &color,
402 unsigned int thickness)
403{
404 vpFeatureDisplay::displayPoint(p[0], p[1], cam, I, color, thickness);
405}
406
407// Get coordinates
409double vpPoint::get_X() const { const unsigned int index_0 = 0; return cP[index_0]; }
411double vpPoint::get_Y() const { const unsigned int index_1 = 1; return cP[index_1]; }
413double vpPoint::get_Z() const { const unsigned int index_2 = 2; return cP[index_2]; }
415double vpPoint::get_W() const { const unsigned int index_3 = 3; return cP[index_3]; }
416
418double vpPoint::get_oX() const { const unsigned int index_0 = 0; return oP[index_0]; }
420double vpPoint::get_oY() const { const unsigned int index_1 = 1; return oP[index_1]; }
422double vpPoint::get_oZ() const { const unsigned int index_2 = 2; return oP[index_2]; }
424double vpPoint::get_oW() const { const unsigned int index_3 = 3; return oP[index_3]; }
425
427double vpPoint::get_x() const { const unsigned int index_0 = 0; return p[index_0]; }
429double vpPoint::get_y() const { const unsigned int index_1 = 1; return p[index_1]; }
431double vpPoint::get_w() const { const unsigned int index_2 = 2; return p[index_2]; }
432
442{
443 const unsigned int index_0 = 0;
444 const unsigned int index_1 = 1;
445 const unsigned int index_2 = 2;
446 double d = 1 / cP[index_2];
447 p[index_0] = cP[index_0] * d;
448 p[index_1] = cP[index_1] * d;
449 p[index_2] = 1;
450}
451
453void vpPoint::set_X(double cX) { const unsigned int index_0 = 0; cP[index_0] = cX; }
455void vpPoint::set_Y(double cY) { const unsigned int index_1 = 1; cP[index_1] = cY; }
457void vpPoint::set_Z(double cZ) { const unsigned int index_2 = 2; cP[index_2] = cZ; }
459void vpPoint::set_W(double cW) { const unsigned int index_3 = 3; cP[index_3] = cW; }
460
462void vpPoint::set_oX(double oX) { const unsigned int index_0 = 0; oP[index_0] = oX; }
464void vpPoint::set_oY(double oY) { const unsigned int index_1 = 1; oP[index_1] = oY; }
466void vpPoint::set_oZ(double oZ) { const unsigned int index_2 = 2; oP[index_2] = oZ; }
468void vpPoint::set_oW(double oW) { const unsigned int index_3 = 3; oP[index_3] = oW; }
469
471void vpPoint::set_x(double x) { const unsigned int index_0 = 0; p[index_0] = x; }
473void vpPoint::set_y(double y) { const unsigned int index_1 = 1; p[index_1] = y; }
475void vpPoint::set_w(double w) { const unsigned int index_2 = 2; p[index_2] = w; }
476
502VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpPoint &vpp)
503{
504 os << "oP=(" << vpp.get_oX() << ", " << vpp.get_oY() << ", " << vpp.get_oZ() << ", " << vpp.get_oW() << ")\n";
505 os << "cP=(" << vpp.get_X() << ", " << vpp.get_Y() << ", " << vpp.get_Z() << ", " << vpp.get_W() << ")\n";
506 os << "ip=(" << vpp.get_x() << ", " << vpp.get_y() << ", " << vpp.get_w()<< ")";
507 return os;
508}
509END_VISP_NAMESPACE
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
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
@ dimensionError
Bad dimension.
Definition vpException.h:71
static void displayPoint(double x, double y, 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
double get_oW() const
Get the point oW coordinate in the object frame.
Definition vpPoint.cpp:424
double get_oX() const
Get the point oX coordinate in the object frame.
Definition vpPoint.cpp:418
double get_w() const
Get the point w coordinate in the image plane.
Definition vpPoint.cpp:431
void set_x(double x)
Set the point x coordinate in the image plane.
Definition vpPoint.cpp:471
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition vpPoint.cpp:459
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition vpPoint.cpp:468
double get_y() const
Get the point y coordinate in the image plane.
Definition vpPoint.cpp:429
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition vpPoint.cpp:411
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition vpPoint.cpp:422
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition vpPoint.cpp:464
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition vpPoint.cpp:453
double get_x() const
Get the point x coordinate in the image plane.
Definition vpPoint.cpp:427
double get_W() const
Get the point cW coordinate in the camera frame.
Definition vpPoint.cpp:415
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition vpPoint.cpp:455
void init() VP_OVERRIDE
Basic construction.
Definition vpPoint.cpp:43
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:413
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, unsigned int thickness=1) VP_OVERRIDE
Definition vpPoint.cpp:387
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition vpPoint.cpp:466
vpColVector getWorldCoordinates(void)
Definition vpPoint.cpp:242
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:457
vpPoint * duplicate() const VP_OVERRIDE
For memory issue (used by the vpServo class only).
Definition vpPoint.cpp:321
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition vpPoint.cpp:462
double get_oY() const
Get the point oY coordinate in the object frame.
Definition vpPoint.cpp:420
void projection() VP_OVERRIDE
Definition vpPoint.cpp:441
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition vpPoint.cpp:273
double get_X() const
Get the point cX coordinate in the camera frame.
Definition vpPoint.cpp:409
vpPoint()
Basic constructor.
Definition vpPoint.cpp:63
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpPoint &vpp)
Definition vpPoint.cpp:502
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:473
void set_w(double w)
Set the point w coordinate in the image plane.
Definition vpPoint.cpp:475
vpColVector cP
Definition vpTracker.h:73
vpColVector p
Definition vpTracker.h:69