Visual Servoing Platform  version 3.6.1 under development (2025-01-24)
vpPoint.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 *****************************************************************************/
34 
40 #include <visp3/core/vpFeatureDisplay.h>
41 #include <visp3/core/vpPoint.h>
42 
43 BEGIN_VISP_NAMESPACE
45 {
46  const unsigned int val_3 = 3;
47  const unsigned int val_4 = 4;
48  const unsigned int index_2 = 2;
49  const unsigned int index_3 = 3;
50  p.resize(val_3);
51  p = 0;
52  p[index_2] = 1.;
53  oP.resize(val_4);
54  oP = 0.;
55  oP[index_3] = 1.;
56  cP.resize(val_4);
57  cP = 0.;
58  cP[index_3] = 1.;
59 
60  // default value Z (1 meters)
61  set_Z(1);
62 }
63 
65 
70 vpPoint::vpPoint(double oX, double oY, double oZ)
71 {
72  init();
73  setWorldCoordinates(oX, oY, oZ);
74 }
75 
87 {
88  init();
90 }
91 
102 vpPoint::vpPoint(const std::vector<double> &oP_)
103 {
104  init();
105  setWorldCoordinates(oP_);
106 }
107 
113 void vpPoint::setWorldCoordinates(double oX, double oY, double oZ)
114 {
115  const unsigned int index_0 = 0;
116  const unsigned int index_1 = 1;
117  const unsigned int index_2 = 2;
118  const unsigned int index_3 = 3;
119  oP[index_0] = oX;
120  oP[index_1] = oY;
121  oP[index_2] = oZ;
122  oP[index_3] = 1;
123 }
124 
136 {
137  const unsigned int index_0 = 0;
138  const unsigned int index_1 = 1;
139  const unsigned int index_2 = 2;
140  const unsigned int index_3 = 3;
141  const unsigned int val_3 = 3;
142  const unsigned int val_4 = 4;
143 
144  if (oP_.size() == val_3) {
145  oP[index_0] = oP_[index_0];
146  oP[index_1] = oP_[index_1];
147  oP[index_2] = oP_[index_2];
148  oP[index_3] = 1.;
149  }
150  else if (oP_.size() == val_4) {
151  oP[index_0] = oP_[index_0];
152  oP[index_1] = oP_[index_1];
153  oP[index_2] = oP_[index_2];
154  oP[index_3] = oP_[index_3];
155  oP /= oP[index_3];
156  }
157  else {
158  throw(vpException(vpException::dimensionError, "Cannot initialize vpPoint from vector with size %d", oP_.size()));
159  }
160 }
161 
172 void vpPoint::setWorldCoordinates(const std::vector<double> &oP_)
173 {
174  const unsigned int index_0 = 0;
175  const unsigned int index_1 = 1;
176  const unsigned int index_2 = 2;
177  const unsigned int index_3 = 3;
178  const unsigned int val_3 = 3;
179  const unsigned int val_4 = 4;
180 
181  if (oP_.size() == val_3) {
182  oP[index_0] = oP_[index_0];
183  oP[index_1] = oP_[index_1];
184  oP[index_2] = oP_[index_2];
185  oP[index_3] = 1.;
186  }
187  else if (oP_.size() == val_4) {
188  oP[index_0] = oP_[index_0];
189  oP[index_1] = oP_[index_1];
190  oP[index_2] = oP_[index_2];
191  oP[index_3] = oP_[index_3];
192  oP /= oP[index_3];
193  }
194  else {
195  throw(vpException(vpException::dimensionError, "Cannot initialize vpPoint from vector with size %d", oP_.size()));
196  }
197 }
198 
200 void vpPoint::getWorldCoordinates(double &oX, double &oY, double &oZ)
201 {
202  const unsigned int index_0 = 0;
203  const unsigned int index_1 = 1;
204  const unsigned int index_2 = 2;
205  oX = oP[index_0];
206  oY = oP[index_1];
207  oZ = oP[index_2];
208 }
209 
217 
224 void vpPoint::getWorldCoordinates(std::vector<double> &oP_)
225 {
226  oP_.resize(oP.size());
227  unsigned int oP_size = oP.size();
228  for (unsigned int i = 0; i < oP_size; ++i) {
229  oP_[i] = oP[i];
230  }
231 }
232 
240 
249 void vpPoint::projection(const vpColVector &v_cP, vpColVector &v_p) const
250 {
251  const unsigned int val_3 = 3;
252  const unsigned int index_0 = 0;
253  const unsigned int index_1 = 1;
254  const unsigned int index_2 = 2;
255  v_p.resize(val_3, false);
256 
257  v_p[index_0] = v_cP[index_0] / v_cP[index_2];
258  v_p[index_1] = v_cP[index_1] / v_cP[index_2];
259  v_p[index_2] = 1;
260 }
261 
271 {
272  const unsigned int val_4 = 4;
273  const unsigned int index_0 = 0;
274  const unsigned int index_1 = 1;
275  const unsigned int index_2 = 2;
276  const unsigned int index_3 = 3;
277  v_cP.resize(val_4, false);
278 
279  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]);
280  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]);
281  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]);
282  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]);
283 
284  double d = 1 / v_cP[index_3];
285  v_cP[index_0] *= d;
286  v_cP[index_1] *= d;
287  v_cP[index_2] *= d;
288  v_cP[index_3] *= d;
289 }
290 
300 {
301  const unsigned int index_0 = 0;
302  const unsigned int index_1 = 1;
303  const unsigned int index_2 = 2;
304  const unsigned int index_3 = 3;
305  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]);
306  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]);
307  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]);
308  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]);
309 
310  double d = 1. / W;
311  cP[index_0] = X * d;
312  cP[index_1] = Y * d;
313  cP[index_2] = Z * d;
314  cP[index_3] = 1.;
315 }
316 
319 {
320  vpPoint *feature = new vpPoint(*this);
321  return feature;
322 }
323 
336  const vpColor &color, unsigned int thickness)
337 {
338  vpColVector v_cP, v_p;
339  changeFrame(cMo, v_cP);
340  const unsigned int index_2 = 2;
341 
342  if (v_cP[index_2] < 0) { // no display if point is behind the camera
343  return;
344  }
345 
346  vpPoint::projection(v_cP, v_p);
347  vpFeatureDisplay::displayPoint(v_p[0], v_p[1], cam, I, color, thickness);
348 }
349 
362  const vpColor &color, unsigned int thickness)
363 {
364  vpColVector v_cP, v_p;
365  changeFrame(cMo, v_cP);
366  const unsigned int index_2 = 2;
367 
368  if (v_cP[index_2] < 0) { // no display if point is behind the camera
369  return;
370  }
371 
372  vpPoint::projection(v_cP, v_p);
373  vpFeatureDisplay::displayPoint(v_p[0], v_p[1], cam, I, color, thickness);
374 }
375 
384 void vpPoint::display(const vpImage<unsigned char> &I, const vpCameraParameters &cam, const vpColor &color,
385  unsigned int thickness)
386 {
387  vpFeatureDisplay::displayPoint(p[0], p[1], cam, I, color, thickness);
388 }
389 
398 void vpPoint::display(const vpImage<vpRGBa> &I, const vpCameraParameters &cam, const vpColor &color,
399  unsigned int thickness)
400 {
401  vpFeatureDisplay::displayPoint(p[0], p[1], cam, I, color, thickness);
402 }
403 
404 // Get coordinates
406 double vpPoint::get_X() const { const unsigned int index_0 = 0; return cP[index_0]; }
408 double vpPoint::get_Y() const { const unsigned int index_1 = 1; return cP[index_1]; }
410 double vpPoint::get_Z() const { const unsigned int index_2 = 2; return cP[index_2]; }
412 double vpPoint::get_W() const { const unsigned int index_3 = 3; return cP[index_3]; }
413 
415 double vpPoint::get_oX() const { const unsigned int index_0 = 0; return oP[index_0]; }
417 double vpPoint::get_oY() const { const unsigned int index_1 = 1; return oP[index_1]; }
419 double vpPoint::get_oZ() const { const unsigned int index_2 = 2; return oP[index_2]; }
421 double vpPoint::get_oW() const { const unsigned int index_3 = 3; return oP[index_3]; }
422 
424 double vpPoint::get_x() const { const unsigned int index_0 = 0; return p[index_0]; }
426 double vpPoint::get_y() const { const unsigned int index_1 = 1; return p[index_1]; }
428 double vpPoint::get_w() const { const unsigned int index_2 = 2; return p[index_2]; }
429 
439 {
440  const unsigned int index_0 = 0;
441  const unsigned int index_1 = 1;
442  const unsigned int index_2 = 2;
443  double d = 1 / cP[index_2];
444  p[index_0] = cP[index_0] * d;
445  p[index_1] = cP[index_1] * d;
446  p[index_2] = 1;
447 }
448 
450 void vpPoint::set_X(double cX) { const unsigned int index_0 = 0; cP[index_0] = cX; }
452 void vpPoint::set_Y(double cY) { const unsigned int index_1 = 1; cP[index_1] = cY; }
454 void vpPoint::set_Z(double cZ) { const unsigned int index_2 = 2; cP[index_2] = cZ; }
456 void vpPoint::set_W(double cW) { const unsigned int index_3 = 3; cP[index_3] = cW; }
457 
459 void vpPoint::set_oX(double oX) { const unsigned int index_0 = 0; oP[index_0] = oX; }
461 void vpPoint::set_oY(double oY) { const unsigned int index_1 = 1; oP[index_1] = oY; }
463 void vpPoint::set_oZ(double oZ) { const unsigned int index_2 = 2; oP[index_2] = oZ; }
465 void vpPoint::set_oW(double oW) { const unsigned int index_3 = 3; oP[index_3] = oW; }
466 
468 void vpPoint::set_x(double x) { const unsigned int index_0 = 0; p[index_0] = x; }
470 void vpPoint::set_y(double y) { const unsigned int index_1 = 1; p[index_1] = y; }
472 void vpPoint::set_w(double w) { const unsigned int index_2 = 2; p[index_2] = w; }
473 
474 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpPoint & /* vpp */) { return (os << "vpPoint"); }
475 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
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.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
double get_oW() const
Get the point oW coordinate in the object frame.
Definition: vpPoint.cpp:421
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:415
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:428
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:468
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:456
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition: vpPoint.cpp:465
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:426
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:408
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:419
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:461
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:450
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:424
double get_W() const
Get the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:412
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:452
void init() VP_OVERRIDE
Basic construction.
Definition: vpPoint.cpp:44
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:410
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, unsigned int thickness=1) VP_OVERRIDE
Definition: vpPoint.cpp:384
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:463
vpColVector getWorldCoordinates(void)
Definition: vpPoint.cpp:239
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:454
vpPoint * duplicate() const VP_OVERRIDE
For memory issue (used by the vpServo class only).
Definition: vpPoint.cpp:318
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:459
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:417
void projection() VP_OVERRIDE
Definition: vpPoint.cpp:438
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
Definition: vpPoint.cpp:270
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:406
vpPoint()
Basic constructor.
Definition: vpPoint.cpp:64
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:470
void set_w(double w)
Set the point w coordinate in the image plane.
Definition: vpPoint.cpp:472
vpColVector cP
Definition: vpTracker.h:73
vpColVector p
Definition: vpTracker.h:69