Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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)
54vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint)
55{
56 return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
57}
58
65vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) { return vpImagePoint(point.y, point.x); }
66
73vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) { return vpImagePoint(point.y, point.x); }
74
81vpPoint 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
97vpPoint 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
113vpPoint 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
129vpPoint 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
145cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point)
146{
147 return cv::Point2f((float)point.get_u(), (float)point.get_v());
148}
149
156cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point)
157{
158 return cv::Point2d(point.get_u(), point.get_v());
159}
160
169cv::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
182cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point)
183{
184 return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
185}
186
194cv::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
206cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point)
207{
208 return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
209}
210
217int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) { return match.trainIdx; }
218
224void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) { to = keyPointToVpImagePoint(from); }
225
231void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) { to = point2fToVpImagePoint(from); }
232
238void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) { to = point2dToVpImagePoint(from); }
239
247void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, bool cameraFrame)
248{
249 if (cameraFrame) {
250 to = point3fToVpCamPoint(from);
251 } else {
252 to = point3fToVpObjectPoint(from);
253 }
254}
255
263void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, bool cameraFrame)
264{
265 if (cameraFrame) {
266 to = point3dToVpCamPoint(from);
267 } else {
268 to = point3dToVpObjectPoint(from);
269 }
270}
271
277void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to)
278{
279 to.resize(from.size());
280 std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
281}
282
288void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to)
289{
290 to.resize(from.size());
291 std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
292}
293
299void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to)
300{
301 to.resize(from.size());
302 std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
303}
304
312void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, bool cameraFrame)
313{
314 to.resize(from.size());
315 if (cameraFrame) {
316 std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
317 } else {
318 std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
319 }
320}
321
329void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, bool cameraFrame)
330{
331 to.resize(from.size());
332 if (cameraFrame) {
333 std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
334 } else {
335 std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
336 }
337}
338
349void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to)
350{
351 to.resize(from.size());
352 std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
353}
354
360void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) { to = vpImagePointToPoint2f(from); }
361
367void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) { to = vpImagePointToPoint2d(from); }
368
376void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, bool cameraFrame)
377{
378 if (cameraFrame) {
379 to = vpCamPointToPoint3f(from);
380 } else {
381 to = vpObjectPointToPoint3f(from);
382 }
383}
384
392void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, bool cameraFrame)
393{
394 if (cameraFrame) {
395 to = vpCamPointToPoint3d(from);
396 } else {
397 to = vpObjectPointToPoint3d(from);
398 }
399}
400
406void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to)
407{
408 to.resize(from.size());
409 std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
410}
411
417void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to)
418{
419 to.resize(from.size());
420 std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
421}
422
430void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, bool cameraFrame)
431{
432 to.resize(from.size());
433 if (cameraFrame) {
434 std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
435 } else {
436 std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
437 }
438}
439
447void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, bool cameraFrame)
448{
449 to.resize(from.size());
450 if (cameraFrame) {
451 std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
452 } else {
453 std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
454 }
455}
456#elif !defined(VISP_BUILD_SHARED_LIBS)
457// Work around to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no
458// symbols
459void dummy_vpConvert(){};
460#endif
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
double get_u() const
double get_v() const
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:458
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition vpPoint.cpp:496
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition vpPoint.cpp:505
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition vpPoint.cpp:451
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition vpPoint.cpp:462
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition vpPoint.cpp:501
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition vpPoint.cpp:490
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition vpPoint.cpp:492
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:453
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition vpPoint.cpp:503
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:494
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition vpPoint.cpp:499
double get_oY() const
Get the point oY coordinate in the object frame.
Definition vpPoint.cpp:460
double get_X() const
Get the point cX coordinate in the camera frame.
Definition vpPoint.cpp:449