Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpConvert.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  * Directory management.
33  *
34 *****************************************************************************/
35 
41 #include <algorithm> // std::transform
42 #include <vector> // std::vector
43 
44 #include <visp3/core/vpConvert.h>
45 
46 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
47 BEGIN_VISP_NAMESPACE
55 vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint)
56 {
57  return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
58 }
59 
66 vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) { return vpImagePoint(point.y, point.x); }
67 
74 vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) { return vpImagePoint(point.y, point.x); }
75 
82 vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f)
83 {
84  vpPoint pt;
85  pt.set_oX(point3f.x);
86  pt.set_oY(point3f.y);
87  pt.set_oZ(point3f.z);
88  pt.set_oW(1.0);
89  return pt;
90 }
91 
98 vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f)
99 {
100  vpPoint pt;
101  pt.set_X(point3f.x);
102  pt.set_Y(point3f.y);
103  pt.set_Z(point3f.z);
104  pt.set_W(1.0);
105  return pt;
106 }
107 
114 vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d)
115 {
116  vpPoint pt;
117  pt.set_oX(point3d.x);
118  pt.set_oY(point3d.y);
119  pt.set_oZ(point3d.z);
120  pt.set_oW(1.0);
121  return pt;
122 }
123 
130 vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d)
131 {
132  vpPoint pt;
133  pt.set_X(point3d.x);
134  pt.set_Y(point3d.y);
135  pt.set_Z(point3d.z);
136  pt.set_W(1.0);
137  return pt;
138 }
139 
146 cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point)
147 {
148  return cv::Point2f((float)point.get_u(), (float)point.get_v());
149 }
150 
157 cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point)
158 {
159  return cv::Point2d(point.get_u(), point.get_v());
160 }
161 
170 cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point)
171 {
172  return cv::Point3f((float)point.get_X(), (float)point.get_Y(), (float)point.get_Z());
173 }
174 
183 cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point)
184 {
185  return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
186 }
187 
195 cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point)
196 {
197  return cv::Point3f((float)point.get_oX(), (float)point.get_oY(), (float)point.get_oZ());
198 }
199 
207 cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point)
208 {
209  return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
210 }
211 
218 int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) { return match.trainIdx; }
219 
225 void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) { to = keyPointToVpImagePoint(from); }
226 
232 void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) { to = point2fToVpImagePoint(from); }
233 
239 void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) { to = point2dToVpImagePoint(from); }
240 
248 void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, bool cameraFrame)
249 {
250  if (cameraFrame) {
251  to = point3fToVpCamPoint(from);
252  }
253  else {
254  to = point3fToVpObjectPoint(from);
255  }
256 }
257 
265 void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, bool cameraFrame)
266 {
267  if (cameraFrame) {
268  to = point3dToVpCamPoint(from);
269  }
270  else {
271  to = point3dToVpObjectPoint(from);
272  }
273 }
274 
280 void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to)
281 {
282  to.resize(from.size());
283  std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
284 }
285 
291 void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to)
292 {
293  to.resize(from.size());
294  std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
295 }
296 
302 void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to)
303 {
304  to.resize(from.size());
305  std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
306 }
307 
315 void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, bool cameraFrame)
316 {
317  to.resize(from.size());
318  if (cameraFrame) {
319  std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
320  }
321  else {
322  std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
323  }
324 }
325 
333 void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, bool cameraFrame)
334 {
335  to.resize(from.size());
336  if (cameraFrame) {
337  std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
338  }
339  else {
340  std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
341  }
342 }
343 
354 void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to)
355 {
356  to.resize(from.size());
357  std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
358 }
359 
365 void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) { to = vpImagePointToPoint2f(from); }
366 
372 void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) { to = vpImagePointToPoint2d(from); }
373 
381 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, bool cameraFrame)
382 {
383  if (cameraFrame) {
384  to = vpCamPointToPoint3f(from);
385  }
386  else {
387  to = vpObjectPointToPoint3f(from);
388  }
389 }
390 
398 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, bool cameraFrame)
399 {
400  if (cameraFrame) {
401  to = vpCamPointToPoint3d(from);
402  }
403  else {
404  to = vpObjectPointToPoint3d(from);
405  }
406 }
407 
413 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to)
414 {
415  to.resize(from.size());
416  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
417 }
418 
424 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to)
425 {
426  to.resize(from.size());
427  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
428 }
429 
437 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, bool cameraFrame)
438 {
439  to.resize(from.size());
440  if (cameraFrame) {
441  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
442  }
443  else {
444  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
445  }
446 }
447 
455 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, bool cameraFrame)
456 {
457  to.resize(from.size());
458  if (cameraFrame) {
459  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
460  }
461  else {
462  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
463  }
464 }
465 END_VISP_NAMESPACE
466 #elif !defined(VISP_BUILD_SHARED_LIBS)
467 // Work around to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no symbols
468 void dummy_vpConvert() { };
469 #endif
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:225
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:365
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
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_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:411
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:452
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition: vpPoint.cpp:461
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:404
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:415
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:457
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:446
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:448
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:406
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:459
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:450
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:455
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:413
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:402