EigenPluginsTest.cpp
Go to the documentation of this file.
1 /*
2  * EigenMatrixBaseAddonsTest.cpp
3  *
4  * Created on: Sep 23, 2015
5  * Author: Péter Fankhauser
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
10 
11 // gtest
12 #include <gtest/gtest.h>
13 
14 // Eigen
15 #include <Eigen/Core>
16 
17 using namespace std;
18 using namespace Eigen;
19 
20 TEST(EigenMatrixBaseAddons, numberOfFinites)
21 {
22  Eigen::Matrix3f matrix(Eigen::Matrix3f::Ones());
23  matrix(0, 0) = NAN;
24  matrix(1, 0) = NAN;
25  EXPECT_EQ(7, matrix.numberOfFinites());
26 
27  Matrix<double, 13, 10> matrix2;
28  matrix2.setOnes();
29  EXPECT_EQ(matrix2.rows() * matrix2.cols(), matrix2.numberOfFinites());
30 
31  Matrix<double, 13, 10> matrix3;
32  matrix3.setConstant(NAN);
33  matrix3.col(3).setConstant(0.0);
34  EXPECT_EQ(matrix3.rows(), matrix3.numberOfFinites());
35 }
36 
37 TEST(EigenMatrixBaseAddons, sumOfFinites)
38 {
39  Matrix<double, 7, 18> matrix;
40  matrix.setRandom();
41  EXPECT_NEAR(matrix.sum(), matrix.sumOfFinites(), 1e-10);
42  double finiteSum = matrix.sum() - matrix(0, 0) - matrix(1, 2) - matrix(3, 6) - matrix(6, 12);
43  matrix(0, 0) = NAN;
44  matrix(1, 2) = NAN;
45  matrix(3, 6) = NAN;
46  matrix(6, 12) = NAN;
47  EXPECT_NEAR(finiteSum, matrix.sumOfFinites(), 1e-10);
48  matrix.setConstant(NAN);
49  EXPECT_TRUE(std::isnan(matrix.sumOfFinites()));
50  matrix(5, 7) = 1.0;
51  EXPECT_NEAR(1.0, matrix.sumOfFinites(), 1e-10);
52 }
53 
54 TEST(EigenMatrixBaseAddons, meanOfFinites)
55 {
56  Eigen::Matrix3f matrix(Eigen::Matrix3f::Ones());
57  matrix(0, 0) = NAN;
58  matrix(1, 1) = NAN;
59  EXPECT_DOUBLE_EQ(1.0, matrix.meanOfFinites());
60 
61  Matrix<double, 13, 10> matrix2;
62  matrix2.setRandom();
63  EXPECT_NEAR(matrix2.mean(), matrix2.meanOfFinites(), 1e-10);
64 }
65 
66 TEST(EigenMatrixBaseAddons, minCoeffOfFinites)
67 {
68  Matrix<double, 7, 18> matrix;
69  matrix.setRandom();
70  double min = matrix.minCoeff();
71  EXPECT_NEAR(min, matrix.minCoeffOfFinites(), 1e-10);
72 
73  int i, j;
74  matrix.maxCoeff(&i, &j);
75  matrix(i, j) = NAN;
76  EXPECT_NEAR(min, matrix.minCoeffOfFinites(), 1e-10);
77 
78  matrix.setConstant(NAN);
79  EXPECT_TRUE(std::isnan(matrix.minCoeffOfFinites()));
80  matrix(i, j) = -1.0;
81  EXPECT_NEAR(-1.0, matrix.minCoeffOfFinites(), 1e-10);
82 }
83 
84 TEST(EigenMatrixBaseAddons, maxCoeffOfFinites)
85 {
86  Matrix<double, 7, 18> matrix;
87  matrix.setRandom();
88  double max = matrix.maxCoeff();
89  EXPECT_NEAR(max, matrix.maxCoeffOfFinites(), 1e-10);
90 
91  int i, j;
92  matrix.minCoeff(&i, &j);
93  matrix(i, j) = NAN;
94  EXPECT_NEAR(max, matrix.maxCoeffOfFinites(), 1e-10);
95 
96  matrix.setConstant(NAN);
97  EXPECT_TRUE(std::isnan(matrix.maxCoeffOfFinites()));
98  matrix(i, j) = -1.0;
99  EXPECT_NEAR(-1.0, matrix.maxCoeffOfFinites(), 1e-10);
100 }
101 
102 TEST(EigenMatrixBaseAddons, clamp)
103 {
104  Eigen::VectorXf vector(Eigen::VectorXf::LinSpaced(9, 1.0, 9.0));
105  Eigen::Matrix3f matrix;
106  matrix << vector.segment(0, 3), vector.segment(3, 3), vector.segment(6, 3);
107  matrix(1, 1) = NAN;
108  matrix = matrix.unaryExpr(grid_map::Clamp<float>(2.1, 7.0));
109  EXPECT_NEAR(2.1, matrix(0, 0), 1e-7);
110  EXPECT_NEAR(2.1, matrix(1, 0), 1e-7);
111  EXPECT_NEAR(3.0, matrix(2, 0), 1e-7);
112  EXPECT_TRUE(std::isnan(matrix(1, 1)));
113  EXPECT_NEAR(7.0, matrix(2, 2), 1e-7);
114 }
Scalar meanOfFinites() const
Scalar sumOfFinites() const
Scalar maxCoeffOfFinites() const
Scalar numberOfFinites() const
TEST(EigenMatrixBaseAddons, numberOfFinites)
Scalar minCoeffOfFinites() const


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Tue Jun 25 2019 20:02:08