Visual Servoing Platform  version 3.4.1 under development (2021-04-20)
vpConvert.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Fabien Spindler
36  * Souriya Trinh
37  *
38  *****************************************************************************/
39 
45 #include <algorithm> // std::transform
46 #include <vector> // std::vector
47 
48 #include <visp3/core/vpConvert.h>
49 
50 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
51 
58 vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint)
59 {
60  return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
61 }
62 
69 vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) { return vpImagePoint(point.y, point.x); }
70 
77 vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) { return vpImagePoint(point.y, point.x); }
78 
85 vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f)
86 {
87  vpPoint pt;
88  pt.set_oX(point3f.x);
89  pt.set_oY(point3f.y);
90  pt.set_oZ(point3f.z);
91  pt.set_oW(1.0);
92  return pt;
93 }
94 
101 vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f)
102 {
103  vpPoint pt;
104  pt.set_X(point3f.x);
105  pt.set_Y(point3f.y);
106  pt.set_Z(point3f.z);
107  pt.set_W(1.0);
108  return pt;
109 }
110 
117 vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d)
118 {
119  vpPoint pt;
120  pt.set_oX(point3d.x);
121  pt.set_oY(point3d.y);
122  pt.set_oZ(point3d.z);
123  pt.set_oW(1.0);
124  return pt;
125 }
126 
133 vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d)
134 {
135  vpPoint pt;
136  pt.set_X(point3d.x);
137  pt.set_Y(point3d.y);
138  pt.set_Z(point3d.z);
139  pt.set_W(1.0);
140  return pt;
141 }
142 
149 cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point)
150 {
151  return cv::Point2f((float)point.get_u(), (float)point.get_v());
152 }
153 
160 cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point)
161 {
162  return cv::Point2d(point.get_u(), point.get_v());
163 }
164 
172 cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point)
173 {
174  return cv::Point3f((float)point.get_X(), (float)point.get_Y(), (float)point.get_Z());
175 }
176 
184 cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point)
185 {
186  return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
187 }
188 
196 cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point)
197 {
198  return cv::Point3f((float)point.get_oX(), (float)point.get_oY(), (float)point.get_oZ());
199 }
200 
208 cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point)
209 {
210  return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
211 }
212 
219 int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) { return match.trainIdx; }
220 
226 void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) { to = keyPointToVpImagePoint(from); }
227 
233 void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) { to = point2fToVpImagePoint(from); }
234 
240 void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) { to = point2dToVpImagePoint(from); }
241 
249 void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, bool cameraFrame)
250 {
251  if (cameraFrame) {
252  to = point3fToVpCamPoint(from);
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  } 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,
315  bool cameraFrame)
316 {
317  to.resize(from.size());
318  if (cameraFrame) {
319  std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
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,
333  bool cameraFrame)
334 {
335  to.resize(from.size());
336  if (cameraFrame) {
337  std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
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  } else {
385  to = vpObjectPointToPoint3f(from);
386  }
387 }
388 
396 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, bool cameraFrame)
397 {
398  if (cameraFrame) {
399  to = vpCamPointToPoint3d(from);
400  } else {
401  to = vpObjectPointToPoint3d(from);
402  }
403 }
404 
410 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to)
411 {
412  to.resize(from.size());
413  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
414 }
415 
421 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to)
422 {
423  to.resize(from.size());
424  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
425 }
426 
434 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, bool cameraFrame)
435 {
436  to.resize(from.size());
437  if (cameraFrame) {
438  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
439  } else {
440  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
441  }
442 }
443 
451 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, bool cameraFrame)
452 {
453  to.resize(from.size());
454  if (cameraFrame) {
455  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
456  } else {
457  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
458  }
459 }
460 #elif !defined(VISP_BUILD_SHARED_LIBS)
461 // Work arround to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no
462 // symbols
463 void dummy_vpConvert(){};
464 #endif
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:449
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:485
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:483
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:447
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:364
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:479
double get_u() const
Definition: vpImagePoint.h:262
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:490
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:451
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition: vpPoint.cpp:494
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:492
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:438
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:481
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:488
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:442
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:440
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:226
double get_v() const
Definition: vpImagePoint.h:273