Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
vpPoint.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Point feature.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpDebug.h>
37 #include <visp3/core/vpFeatureDisplay.h>
38 #include <visp3/core/vpPoint.h>
39 
46 {
47  p.resize(3);
48  p = 0;
49  p[2] = 1;
50  oP.resize(4);
51  oP = 0;
52  oP[3] = 1;
53  cP.resize(4);
54  cP = 0;
55  cP[3] = 1;
56 
57  // default value Z (1 meters)
58  set_Z(1);
59 }
60 
62 
67 vpPoint::vpPoint(double oX, double oY, double oZ)
68 {
69  init();
70  setWorldCoordinates(oX, oY, oZ);
71 }
72 
84 {
85  init();
87 }
88 
99 vpPoint::vpPoint(const std::vector<double> &oP_)
100 {
101  init();
102  setWorldCoordinates(oP_);
103 }
104 
110 void vpPoint::setWorldCoordinates(double oX, double oY, double oZ)
111 {
112  oP[0] = oX;
113  oP[1] = oY;
114  oP[2] = oZ;
115  oP[3] = 1;
116 }
117 
129 {
130  if (oP_.size() == 3) {
131  oP[0] = oP_[0];
132  oP[1] = oP_[1];
133  oP[2] = oP_[2];
134  oP[3] = 1.;
135  }
136  else if (oP_.size() == 4) {
137  oP[0] = oP_[0];
138  oP[1] = oP_[1];
139  oP[2] = oP_[2];
140  oP[3] = oP_[3];
141  oP /= oP[3];
142  }
143  else {
144  throw(vpException(vpException::dimensionError, "Cannot initialize vpPoint from vector with size %d", oP_.size()));
145  }
146 }
147 
158 void vpPoint::setWorldCoordinates(const std::vector<double> &oP_)
159 {
160  if (oP_.size() == 3) {
161  oP[0] = oP_[0];
162  oP[1] = oP_[1];
163  oP[2] = oP_[2];
164  oP[3] = 1.;
165  }
166  else if (oP_.size() == 4) {
167  oP[0] = oP_[0];
168  oP[1] = oP_[1];
169  oP[2] = oP_[2];
170  oP[3] = oP_[3];
171  oP /= oP[3];
172  }
173  else {
174  throw(vpException(vpException::dimensionError, "Cannot initialize vpPoint from vector with size %d", oP_.size()));
175  }
176 }
177 
179 void vpPoint::getWorldCoordinates(double &oX, double &oY, double &oZ)
180 {
181  oX = oP[0];
182  oY = oP[1];
183  oZ = oP[2];
184 }
185 
193 
200 void vpPoint::getWorldCoordinates(std::vector<double> &oP_)
201 {
202  oP_.resize(oP.size());
203  unsigned int oP_size = oP.size();
204  for (unsigned int i = 0; i < oP_size; ++i) {
205  oP_[i] = oP[i];
206  }
207 }
208 
216 
225 void vpPoint::projection(const vpColVector &v_cP, vpColVector &v_p) const
226 {
227  v_p.resize(3, false);
228 
229  v_p[0] = v_cP[0] / v_cP[2];
230  v_p[1] = v_cP[1] / v_cP[2];
231  v_p[2] = 1;
232 }
233 
243 {
244  v_cP.resize(4, false);
245 
246  v_cP[0] = (cMo[0][0] * oP[0]) + (cMo[0][1] * oP[1]) + (cMo[0][2] * oP[2]) + (cMo[0][3] * oP[3]);
247  v_cP[1] = (cMo[1][0] * oP[0]) + (cMo[1][1] * oP[1]) + (cMo[1][2] * oP[2]) + (cMo[1][3] * oP[3]);
248  v_cP[2] = (cMo[2][0] * oP[0]) + (cMo[2][1] * oP[1]) + (cMo[2][2] * oP[2]) + (cMo[2][3] * oP[3]);
249  v_cP[3] = (cMo[3][0] * oP[0]) + (cMo[3][1] * oP[1]) + (cMo[3][2] * oP[2]) + (cMo[3][3] * oP[3]);
250 
251  double d = 1 / v_cP[3];
252  v_cP[0] *= d;
253  v_cP[1] *= d;
254  v_cP[2] *= d;
255  v_cP[3] *= d;
256 }
257 
267 {
268  double X = (cMo[0][0] * oP[0]) + (cMo[0][1] * oP[1]) + (cMo[0][2] * oP[2]) + (cMo[0][3] * oP[3]);
269  double Y = (cMo[1][0] * oP[0]) + (cMo[1][1] * oP[1]) + (cMo[1][2] * oP[2]) + (cMo[1][3] * oP[3]);
270  double Z = (cMo[2][0] * oP[0]) + (cMo[2][1] * oP[1]) + (cMo[2][2] * oP[2]) + (cMo[2][3] * oP[3]);
271  double W = (cMo[3][0] * oP[0]) + (cMo[3][1] * oP[1]) + (cMo[3][2] * oP[2]) + (cMo[3][3] * oP[3]);
272 
273  double d = 1 / W;
274  cP[0] = X * d;
275  cP[1] = Y * d;
276  cP[2] = Z * d;
277  cP[3] = 1;
278 }
279 
280 #if 0
291 const vpPoint
292 operator*(const vpHomogeneousMatrix &aMb, const vpPoint &bP)
293 {
294  vpPoint aP;
295 
296  vpColVector v(4), v1(4);
297 
298  v[0] = bP.get_X();
299  v[1] = bP.get_Y();
300  v[2] = bP.get_Z();
301  v[3] = bP.get_W();
302 
303  v1[0] = aMb[0][0]*v[0] + aMb[0][1]*v[1]+ aMb[0][2]*v[2]+ aMb[0][3]*v[3];
304  v1[1] = aMb[1][0]*v[0] + aMb[1][1]*v[1]+ aMb[1][2]*v[2]+ aMb[1][3]*v[3];
305  v1[2] = aMb[2][0]*v[0] + aMb[2][1]*v[1]+ aMb[2][2]*v[2]+ aMb[2][3]*v[3];
306  v1[3] = aMb[3][0]*v[0] + aMb[3][1]*v[1]+ aMb[3][2]*v[2]+ aMb[3][3]*v[3];
307 
308  v1 /= v1[3];
309 
310  // v1 = M*v ;
311  aP.set_X(v1[0]);
312  aP.set_Y(v1[1]);
313  aP.set_Z(v1[2]);
314  aP.set_W(v1[3]);
315 
316  aP.set_oX(v1[0]);
317  aP.set_oY(v1[1]);
318  aP.set_oZ(v1[2]);
319  aP.set_oW(v1[3]);
320 
321  return aP;
322 }
323 
333 const vpPoint
334 operator*(const vpHomography &aHb, const vpPoint &bP)
335 {
336  vpPoint aP;
337  vpColVector v(3), v1(3);
338 
339  v[0] = bP.get_x();
340  v[1] = bP.get_y();
341  v[2] = bP.get_w();
342 
343  v1[0] = aHb[0][0]*v[0] + aHb[0][1]*v[1]+ aHb[0][2]*v[2];
344  v1[1] = aHb[1][0]*v[0] + aHb[1][1]*v[1]+ aHb[1][2]*v[2];
345  v1[2] = aHb[2][0]*v[0] + aHb[2][1]*v[1]+ aHb[2][2]*v[2];
346 
347  // v1 = M*v ;
348  aP.set_x(v1[0]);
349  aP.set_y(v1[1]);
350  aP.set_w(v1[2]);
351 
352  return aP;
353 }
354 #endif
357 {
358  vpPoint *feature = new vpPoint(*this);
359  return feature;
360 }
361 
374  const vpColor &color, unsigned int thickness)
375 {
376 
377  vpColVector v_cP, v_p;
378  changeFrame(cMo, v_cP);
379 
380  if (v_cP[2] < 0) { // no display if point is behind the camera
381  return;
382  }
383 
384  vpPoint::projection(v_cP, v_p);
385  vpFeatureDisplay::displayPoint(v_p[0], v_p[1], cam, I, color, thickness);
386 }
387 
400  const vpColor &color, unsigned int thickness)
401 {
402  vpColVector v_cP, v_p;
403  changeFrame(cMo, v_cP);
404 
405  if (v_cP[2] < 0) { // no display if point is behind the camera
406  return;
407  }
408 
409  vpPoint::projection(v_cP, v_p);
410  vpFeatureDisplay::displayPoint(v_p[0], v_p[1], cam, I, color, thickness);
411 }
412 
413 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpPoint & /* vpp */) { return (os << "vpPoint"); }
414 
423 void vpPoint::display(const vpImage<unsigned char> &I, const vpCameraParameters &cam, const vpColor &color,
424  unsigned int thickness)
425 {
426  vpFeatureDisplay::displayPoint(p[0], p[1], cam, I, color, thickness);
427 }
428 
437 void vpPoint::display(const vpImage<vpRGBa> &I, const vpCameraParameters &cam, const vpColor &color,
438  unsigned int thickness)
439 {
440  vpFeatureDisplay::displayPoint(p[0], p[1], cam, I, color, thickness);
441 }
442 
443 // Get coordinates
445 double vpPoint::get_X() const { return cP[0]; }
447 double vpPoint::get_Y() const { return cP[1]; }
449 double vpPoint::get_Z() const { return cP[2]; }
451 double vpPoint::get_W() const { return cP[3]; }
452 
454 double vpPoint::get_oX() const { return oP[0]; }
456 double vpPoint::get_oY() const { return oP[1]; }
458 double vpPoint::get_oZ() const { return oP[2]; }
460 double vpPoint::get_oW() const { return oP[3]; }
461 
463 double vpPoint::get_x() const { return p[0]; }
465 double vpPoint::get_y() const { return p[1]; }
467 double vpPoint::get_w() const { return p[2]; }
468 
478 {
479  double d = 1 / cP[2];
480  p[0] = cP[0] * d;
481  p[1] = cP[1] * d;
482  p[2] = 1;
483 }
484 
486 void vpPoint::set_X(double cX) { cP[0] = cX; }
488 void vpPoint::set_Y(double cY) { cP[1] = cY; }
490 void vpPoint::set_Z(double cZ) { cP[2] = cZ; }
492 void vpPoint::set_W(double cW) { cP[3] = cW; }
493 
495 void vpPoint::set_oX(double oX) { oP[0] = oX; }
497 void vpPoint::set_oY(double oY) { oP[1] = oY; }
499 void vpPoint::set_oZ(double oZ) { oP[2] = oZ; }
501 void vpPoint::set_oW(double oW) { oP[3] = oW; }
502 
504 void vpPoint::set_x(double x) { p[0] = x; }
506 void vpPoint::set_y(double y) { p[1] = y; }
508 void vpPoint::set_w(double w) { p[2] = w; }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:600
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector operator*(const double &x, const vpColVector &v)
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
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.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:168
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
double get_oW() const
Get the point oW coordinate in the object frame.
Definition: vpPoint.cpp:460
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:454
double get_w() const
Get the point w coordinate in the image plane.
Definition: vpPoint.cpp:467
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:504
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:492
vpPoint * duplicate() const vp_override
For memory issue (used by the vpServo class only).
Definition: vpPoint.cpp:356
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition: vpPoint.cpp:501
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:465
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:447
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:458
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:497
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:486
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:463
double get_W() const
Get the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:451
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:488
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:449
void projection() vp_override
Definition: vpPoint.cpp:477
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:499
vpColVector getWorldCoordinates(void)
Definition: vpPoint.cpp:215
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:490
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:495
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:456
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:445
void init() vp_override
Basic construction.
Definition: vpPoint.cpp:45
void display(const vpImage< unsigned char > &I, const vpCameraParameters &cam, const vpColor &color=vpColor::green, unsigned int thickness=1) vp_override
Definition: vpPoint.cpp:423
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const vp_override
Definition: vpPoint.cpp:242
vpPoint()
Basic constructor.
Definition: vpPoint.cpp:61
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:506
void set_w(double w)
Set the point w coordinate in the image plane.
Definition: vpPoint.cpp:508
vpColVector cP
Definition: vpTracker.h:71
vpColVector p
Definition: vpTracker.h:67