File: testEigenConversion.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (349 lines) | stat: -rw-r--r-- 11,848 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Test conversion between ViSP and Eigen type.
 *
*****************************************************************************/

/*!
  \example testEigenConversion.cpp

  Test conversion between ViSP and Eigen type.
*/

#include <visp3/core/vpConfig.h>
#include <visp3/core/vpEigenConversion.h>

#if defined(VISP_HAVE_EIGEN3) && defined(VISP_HAVE_CATCH2)
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>

namespace
{
template <typename Type> std::ostream &operator<<(std::ostream &os, const Eigen::Quaternion<Type> &q)
{
  return os << "qw: " << q.w() << " ; qx: " << q.x() << " ; qy: " << q.y() << " ; qz: " << q.z();
}

template <typename Type> std::ostream &operator<<(std::ostream &os, const Eigen::AngleAxis<Type> &aa)
{
  return os << "angle: " << aa.angle() << " ; axis: " << aa.axis()(0) << " ; " << aa.axis()(1) << " ; " << aa.axis()(2)
            << " ; thetau: " << aa.angle() * aa.axis()(0) << " ; " << aa.angle() * aa.axis()(1) << " ; "
            << aa.angle() * aa.axis()(2);
}
} // namespace

TEST_CASE("vpMatrix <--> Eigen::MatrixXd/Matrix3Xd conversion", "[eigen_conversion]")
{
  vpMatrix visp_m(3, 4);
  for (unsigned int i = 0; i < visp_m.size(); i++) {
    visp_m.data[i] = i;
  }

  {
    Eigen::MatrixXd eigen_m;
    vp::visp2eigen(visp_m, eigen_m);
    std::cout << "Eigen MatrixXd:\n" << eigen_m << std::endl;

    vpMatrix visp_m2;
    vp::eigen2visp(eigen_m, visp_m2);
    std::cout << "ViSP vpMatrix:\n" << visp_m2 << std::endl;

    REQUIRE(visp_m == visp_m2);
    std::cout << std::endl;
  }
  {
    Eigen::Matrix3Xd eigen_m;
    vp::visp2eigen(visp_m, eigen_m);
    std::cout << "Eigen Matrix3Xd:\n" << eigen_m << std::endl;

    vpMatrix visp_m2;
    vp::eigen2visp(eigen_m, visp_m2);
    std::cout << "ViSP vpMatrix:\n" << visp_m2 << std::endl;

    REQUIRE(visp_m == visp_m2);
    std::cout << std::endl;
  }
}

TEST_CASE("Eigen::MatrixXd <--> vpMatrix conversion", "[eigen_conversion]")
{
  Eigen::MatrixXd eigen_m(3, 5);
#if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
  for (Eigen::DenseIndex i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::DenseIndex j = 0; j < eigen_m.cols(); j++) {
#else
  for (Eigen::Index i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::Index j = 0; j < eigen_m.cols(); j++) {
#endif
      eigen_m(i, j) = static_cast<double>(i * eigen_m.cols() + j);
    }
  }
  std::cout << "Eigen Matrix (row major: " << eigen_m.IsRowMajor << "):\n" << eigen_m << std::endl;

  vpMatrix visp_m;
  vp::eigen2visp(eigen_m, visp_m);
  std::cout << "ViSP vpMatrix:\n" << visp_m << std::endl;

  Eigen::MatrixXd eigen_m2;
  vp::visp2eigen(visp_m, eigen_m2);
  std::cout << "Eigen MatrixXd (row major: " << eigen_m2.IsRowMajor << "):\n" << eigen_m2 << std::endl;

  vpMatrix visp_m2;
  vp::eigen2visp(eigen_m2, visp_m2);
  REQUIRE(visp_m == visp_m2);
  std::cout << std::endl;
}

TEST_CASE("Eigen::MatrixX4d <--> vpMatrix conversion", "[eigen_conversion]")
{
  Eigen::MatrixX4d eigen_m(2, 4);
#if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
  for (Eigen::DenseIndex i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::DenseIndex j = 0; j < eigen_m.cols(); j++) {
#else
  for (Eigen::Index i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::Index j = 0; j < eigen_m.cols(); j++) {
#endif
      eigen_m(i, j) = static_cast<double>(i * eigen_m.cols() + j);
    }
  }
  std::cout << "Eigen MatrixX4d (row major: " << eigen_m.IsRowMajor << "):\n" << eigen_m << std::endl;

  vpMatrix visp_m;
  vp::eigen2visp(eigen_m, visp_m);
  std::cout << "ViSP vpMatrix:\n" << visp_m << std::endl;

  Eigen::MatrixX4d eigen_m2;
  vp::visp2eigen(visp_m, eigen_m2);
  std::cout << "Eigen MatrixX4d (row major: " << eigen_m2.IsRowMajor << "):\n" << eigen_m2 << std::endl;

  vpMatrix visp_m2;
  vp::eigen2visp(eigen_m2, visp_m2);
  REQUIRE(visp_m == visp_m2);
  std::cout << std::endl;
}

TEST_CASE("Eigen::Matrix<double, Dynamic, Dynamic, RowMajor> <--> vpMatrix conversion", "[eigen_conversion]")
{
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> eigen_m(3, 5);
#if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
  for (Eigen::DenseIndex i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::DenseIndex j = 0; j < eigen_m.cols(); j++) {
#else
  for (Eigen::Index i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::Index j = 0; j < eigen_m.cols(); j++) {
#endif
      eigen_m(i, j) = static_cast<double>(i * eigen_m.cols() + j);
    }
  }
  std::cout << "Eigen Matrix (RowMajor):\n" << eigen_m << std::endl;

  vpMatrix visp_m;
  vp::eigen2visp(eigen_m, visp_m);
  std::cout << "ViSP vpMatrix:\n" << visp_m << std::endl;

  Eigen::MatrixXd eigen_m2;
  vp::visp2eigen(visp_m, eigen_m2);
  std::cout << "Eigen MatrixXd (row major: " << eigen_m2.IsRowMajor << "):\n" << eigen_m2 << std::endl;

  vpMatrix visp_m2;
  vp::eigen2visp(eigen_m2, visp_m2);
  REQUIRE(visp_m == visp_m2);
  std::cout << std::endl;
}

TEST_CASE("Eigen::Matrix<double, Dynamic, Dynamic, ColMajor> <--> vpMatrix conversion", "[eigen_conversion]")
{
  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> eigen_m(3, 5);
#if (VP_VERSION_INT(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION) < 0x030300)
  for (Eigen::DenseIndex i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::DenseIndex j = 0; j < eigen_m.cols(); j++) {
#else
  for (Eigen::Index i = 0; i < eigen_m.rows(); i++) {
    for (Eigen::Index j = 0; j < eigen_m.cols(); j++) {
#endif
      eigen_m(i, j) = static_cast<double>(i * eigen_m.cols() + j);
    }
  }
  std::cout << "Eigen Matrix (ColMajor):\n" << eigen_m << std::endl;

  vpMatrix visp_m;
  vp::eigen2visp(eigen_m, visp_m);
  std::cout << "ViSP vpMatrix:\n" << visp_m << std::endl;

  Eigen::MatrixXd eigen_m2;
  vp::visp2eigen(visp_m, eigen_m2);
  std::cout << "Eigen MatrixXd (row major: " << eigen_m2.IsRowMajor << "):\n" << eigen_m2 << std::endl;

  vpMatrix visp_m2;
  vp::eigen2visp(eigen_m2, visp_m2);
  REQUIRE(visp_m == visp_m2);
  std::cout << std::endl;
}

TEST_CASE("vpHomogeneousMatrix <--> Eigen::Matrix4d conversion", "[eigen_conversion]")
{
  vpHomogeneousMatrix visp_cMo(0.1, 0.2, 0.3, 0.1, 0.2, 0.3);
  Eigen::Matrix4d eigen_cMo;
  vp::visp2eigen(visp_cMo, eigen_cMo);
  std::cout << "Eigen Matrix4d cMo:\n" << eigen_cMo << std::endl;

  vpHomogeneousMatrix visp_cMo2;
  vp::eigen2visp(eigen_cMo, visp_cMo2);
  std::cout << "ViSP  vpHomogeneousMatrix cMo:\n" << visp_cMo2 << std::endl;
  REQUIRE(visp_cMo == visp_cMo2);
  std::cout << std::endl;
}

TEST_CASE("vpHomogeneousMatrix <--> Eigen::Matrix4f + double casting conversion", "[eigen_conversion]")
{
  vpHomogeneousMatrix visp_cMo; // identity for float to double casting
  Eigen::Matrix4d eigen_cMo_tmp;
  vp::visp2eigen(visp_cMo, eigen_cMo_tmp);
  Eigen::Matrix4f eigen_cMo = eigen_cMo_tmp.cast<float>();
  std::cout << "Eigen Matrix4f cMo:\n" << eigen_cMo << std::endl;

  vpHomogeneousMatrix visp_cMo2;
  vp::eigen2visp(eigen_cMo.cast<double>(), visp_cMo2);
  std::cout << "ViSP  vpHomogeneousMatrix cMo:\n" << visp_cMo2 << std::endl;
  REQUIRE(visp_cMo == visp_cMo2);
  std::cout << std::endl;
}

TEST_CASE("vpQuaternionVector <--> Eigen::Quaternionf conversion", "[eigen_conversion]")
{
  vpQuaternionVector visp_quaternion(0, 1, 2, 3);
  Eigen::Quaternionf eigen_quaternion;
  vp::visp2eigen(visp_quaternion, eigen_quaternion);
  std::cout << "Eigen quaternion: " << eigen_quaternion << std::endl;

  vpQuaternionVector visp_quaternion2;
  vp::eigen2visp(eigen_quaternion, visp_quaternion2);
  std::cout << "ViSP quaternion: " << visp_quaternion2.t() << std::endl;
  REQUIRE(visp_quaternion == visp_quaternion2);
  std::cout << std::endl;
}

TEST_CASE("vpThetaUVector <--> Eigen::AngleAxisf conversion", "[eigen_conversion]")
{
  vpThetaUVector visp_thetau(0, 1, 2);
  Eigen::AngleAxisf eigen_angle_axis;
  vp::visp2eigen(visp_thetau, eigen_angle_axis);
  std::cout << "Eigen AngleAxis: " << eigen_angle_axis << std::endl;

  vpThetaUVector visp_thetau2;
  vp::eigen2visp(eigen_angle_axis, visp_thetau2);
  std::cout << "ViSP AngleAxis: " << visp_thetau2.t() << std::endl;
  REQUIRE(visp_thetau == visp_thetau2);
  std::cout << std::endl;
}

TEST_CASE("vpColVector <--> Eigen::VectorXd conversion", "[eigen_conversion]")
{
  vpColVector visp_col(4, 4);
  visp_col = 10;
  Eigen::VectorXd eigen_col;
  vp::visp2eigen(visp_col, eigen_col);
  std::cout << "Eigen VectorXd: " << eigen_col.transpose() << std::endl;

  vpColVector visp_col2;
  vp::eigen2visp(eigen_col, visp_col2);
  std::cout << "ViSP vpColVector: " << visp_col2.t() << std::endl;
  REQUIRE(visp_col == visp_col2);
  std::cout << std::endl;
}

TEST_CASE("vpRowVector <--> Eigen::RowVectorXd conversion", "[eigen_conversion]")
{
  vpRowVector visp_row(4, 10);
  visp_row = 10;
  Eigen::RowVectorXd eigen_row;
  vp::visp2eigen(visp_row, eigen_row);
  std::cout << "Eigen RowVectorXd: " << eigen_row << std::endl;

  vpRowVector visp_row2;
  vp::eigen2visp(eigen_row, visp_row2);
  std::cout << "ViSP vpRowVector: " << visp_row2 << std::endl;
  REQUIRE(visp_row == visp_row2);
  std::cout << std::endl;
}

TEST_CASE("Eigen::RowVector4d <--> vpRowVector conversion", "[eigen_conversion]")
{
  Eigen::RowVector4d eigen_row;
  eigen_row << 9, 8, 7, 6;
  vpRowVector visp_row;
  vp::eigen2visp(eigen_row, visp_row);
  std::cout << "ViSP vpRowVector: " << visp_row << std::endl;

  Eigen::RowVector4d eigen_row2;
  vp::visp2eigen(visp_row, eigen_row2);
  std::cout << "Eigen RowVector4d: " << eigen_row2 << std::endl;

  vpRowVector visp_row2;
  vp::eigen2visp(eigen_row2, visp_row2);
  REQUIRE(visp_row == visp_row2);
  std::cout << std::endl;
}

TEST_CASE("vpRowVector <--> Eigen::RowVector4d conversion", "[eigen_conversion]")
{
  vpRowVector visp_row(4, 10);
  visp_row = 10;
  Eigen::RowVector4d eigen_row;
  vp::visp2eigen(visp_row, eigen_row);
  std::cout << "Eigen RowVector4d: " << eigen_row << std::endl;

  vpRowVector visp_row2;
  vp::eigen2visp(eigen_row, visp_row2);
  std::cout << "ViSP vpRowVector: " << visp_row2 << std::endl;
  REQUIRE(visp_row == visp_row2);
  std::cout << std::endl;
}

int main(int argc, char *argv[])
{
  Catch::Session session; // There must be exactly one instance

  // Let Catch (using Clara) parse the command line
  session.applyCommandLine(argc, argv);

  int numFailed = session.run();

  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
  // This clamping has already been applied, so just return it here
  // You can also do any post run clean-up here
  return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif