Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
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)
54 vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint)
55 {
56  return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
57 }
58 
65 vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) { return vpImagePoint(point.y, point.x); }
66 
73 vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) { return vpImagePoint(point.y, point.x); }
74 
81 vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f)
82 {
83  vpPoint pt;
84  pt.set_oX(point3f.x);
85  pt.set_oY(point3f.y);
86  pt.set_oZ(point3f.z);
87  pt.set_oW(1.0);
88  return pt;
89 }
90 
97 vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f)
98 {
99  vpPoint pt;
100  pt.set_X(point3f.x);
101  pt.set_Y(point3f.y);
102  pt.set_Z(point3f.z);
103  pt.set_W(1.0);
104  return pt;
105 }
106 
113 vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d)
114 {
115  vpPoint pt;
116  pt.set_oX(point3d.x);
117  pt.set_oY(point3d.y);
118  pt.set_oZ(point3d.z);
119  pt.set_oW(1.0);
120  return pt;
121 }
122 
129 vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d)
130 {
131  vpPoint pt;
132  pt.set_X(point3d.x);
133  pt.set_Y(point3d.y);
134  pt.set_Z(point3d.z);
135  pt.set_W(1.0);
136  return pt;
137 }
138 
145 cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point)
146 {
147  return cv::Point2f((float)point.get_u(), (float)point.get_v());
148 }
149 
156 cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point)
157 {
158  return cv::Point2d(point.get_u(), point.get_v());
159 }
160 
169 cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point)
170 {
171  return cv::Point3f((float)point.get_X(), (float)point.get_Y(), (float)point.get_Z());
172 }
173 
182 cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point)
183 {
184  return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
185 }
186 
194 cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point)
195 {
196  return cv::Point3f((float)point.get_oX(), (float)point.get_oY(), (float)point.get_oZ());
197 }
198 
206 cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point)
207 {
208  return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
209 }
210 
217 int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) { return match.trainIdx; }
218 
224 void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) { to = keyPointToVpImagePoint(from); }
225 
231 void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) { to = point2fToVpImagePoint(from); }
232 
238 void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) { to = point2dToVpImagePoint(from); }
239 
247 void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, bool cameraFrame)
248 {
249  if (cameraFrame) {
250  to = point3fToVpCamPoint(from);
251  }
252  else {
253  to = point3fToVpObjectPoint(from);
254  }
255 }
256 
264 void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, bool cameraFrame)
265 {
266  if (cameraFrame) {
267  to = point3dToVpCamPoint(from);
268  }
269  else {
270  to = point3dToVpObjectPoint(from);
271  }
272 }
273 
279 void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to)
280 {
281  to.resize(from.size());
282  std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
283 }
284 
290 void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to)
291 {
292  to.resize(from.size());
293  std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
294 }
295 
301 void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to)
302 {
303  to.resize(from.size());
304  std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
305 }
306 
314 void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, bool cameraFrame)
315 {
316  to.resize(from.size());
317  if (cameraFrame) {
318  std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
319  }
320  else {
321  std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
322  }
323 }
324 
332 void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, bool cameraFrame)
333 {
334  to.resize(from.size());
335  if (cameraFrame) {
336  std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
337  }
338  else {
339  std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
340  }
341 }
342 
353 void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to)
354 {
355  to.resize(from.size());
356  std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
357 }
358 
364 void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) { to = vpImagePointToPoint2f(from); }
365 
371 void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) { to = vpImagePointToPoint2d(from); }
372 
380 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, bool cameraFrame)
381 {
382  if (cameraFrame) {
383  to = vpCamPointToPoint3f(from);
384  }
385  else {
386  to = vpObjectPointToPoint3f(from);
387  }
388 }
389 
397 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, bool cameraFrame)
398 {
399  if (cameraFrame) {
400  to = vpCamPointToPoint3d(from);
401  }
402  else {
403  to = vpObjectPointToPoint3d(from);
404  }
405 }
406 
412 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to)
413 {
414  to.resize(from.size());
415  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
416 }
417 
423 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to)
424 {
425  to.resize(from.size());
426  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
427 }
428 
436 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, bool cameraFrame)
437 {
438  to.resize(from.size());
439  if (cameraFrame) {
440  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
441  }
442  else {
443  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
444  }
445 }
446 
454 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, bool cameraFrame)
455 {
456  to.resize(from.size());
457  if (cameraFrame) {
458  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
459  }
460  else {
461  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
462  }
463 }
464 #elif !defined(VISP_BUILD_SHARED_LIBS)
465 // Work around to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no symbols
466 void dummy_vpConvert() { };
467 #endif
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:224
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:364
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:77
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:450
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:488
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition: vpPoint.cpp:497
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:443
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:454
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:493
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:482
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:484
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:445
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:495
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:486
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:491
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:452
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:441