-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtest_eigen.h
160 lines (136 loc) · 4.41 KB
/
test_eigen.h
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
// This file is part of KWIVER, and is distributed under the
// OSI-approved BSD 3-Clause License. See top-level LICENSE file or
// https://github.com/Kitware/kwiver/blob/master/LICENSE for details.
/**
* \file
*
* \brief Google Test utilities for Eigen types
*
* This adds some utilities that improve working with Eigen types in Google
* Test.
*/
#ifndef KWIVER_TEST_TEST_EIGEN_H_
#define KWIVER_TEST_TEST_EIGEN_H_
#include <Eigen/Core>
#include <gtest/gtest.h>
// ----------------------------------------------------------------------------
//
// Testing helper functions
//
namespace Eigen {
// ----------------------------------------------------------------------------
void
PrintTo( Vector2d const& v, ::std::ostream* os )
{
// This function exists because a) it produces better formatting, and
// b) Google Test needs an exact match or it will fall back to the generic
// value printer...
(*os) << v[0] << ", " << v[1];
}
// ----------------------------------------------------------------------------
void
PrintTo( Vector3d const& v, ::std::ostream* os )
{
// This function exists because a) it produces better formatting, and
// b) Google Test needs an exact match or it will fall back to the generic
// value printer...
(*os) << v[0] << ", " << v[1] << ", " << v[2];
}
// ----------------------------------------------------------------------------
template <typename T>
::std::ostream&
operator<<( ::std::ostream& os, Quaternion<T> const& v )
{
os << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w();
return os;
}
// ----------------------------------------------------------------------------
template <typename T>
bool operator==( Quaternion<T> const& q1, Quaternion<T> const& q2 )
{
return q1.coeffs() == q2.coeffs();
}
} // end namespace Eigen
namespace kwiver {
namespace testing {
// ----------------------------------------------------------------------------
struct matrix_comparator
{
// --------------------------------------------------------------------------
template <typename A, typename B>
bool operator()( A const& m1, B const& m2 )
{
return m1.isApprox( m2 );
}
// --------------------------------------------------------------------------
template <typename T, int M, int N>
bool operator()( Eigen::Matrix<T, M, N> const& a,
Eigen::Matrix<T, M, N> const& b,
double epsilon )
{
for ( unsigned i = 0; i < M; ++i )
{
for ( unsigned j = 0; j < N; ++j )
{
if ( std::abs( a( i, j ) - b( i, j ) ) > epsilon )
{
return false;
}
}
}
return true;
}
// --------------------------------------------------------------------------
template <typename T>
bool operator()( Eigen::Matrix<T, Eigen::Dynamic, 1> const& a,
Eigen::Matrix<T, Eigen::Dynamic, 1> const& b,
double epsilon )
{
if ( a.size() != b.size() )
{
return false;
}
for ( unsigned i = 0; i < a.size(); ++i )
{
if ( std::abs( a[i] - b[i] ) > epsilon )
{
return false;
}
}
return true;
}
// --------------------------------------------------------------------------
template <typename T>
bool operator()( Eigen::Quaternion<T> const& q1,
Eigen::Quaternion<T> const& q2 )
{
return operator()( q1.coeffs(), q2.coeffs() );
}
};
// ----------------------------------------------------------------------------
struct similar_matrix_comparator : matrix_comparator
{
template <typename T, int M, int N>
bool operator()( Eigen::Matrix<T, M, N> const& a,
Eigen::Matrix<T, M, N> const& b,
double epsilon )
{
if ( a.cwiseProduct( b ).sum() < 0.0 )
{
return matrix_comparator::operator()( a, ( b * -1.0 ).eval(), epsilon );
}
return matrix_comparator::operator()( a, b, epsilon );
}
};
// ----------------------------------------------------------------------------
static auto compare_matrices = matrix_comparator{};
static auto compare_similar_matrices = similar_matrix_comparator{};
#define EXPECT_MATRIX_EQ(a, b) \
EXPECT_PRED2(::kwiver::testing::compare_matrices, a, b)
#define EXPECT_MATRIX_NEAR(a, b, eps) \
EXPECT_PRED3(::kwiver::testing::compare_matrices, a, b, eps)
#define EXPECT_MATRIX_SIMILAR(a, b, eps) \
EXPECT_PRED3(::kwiver::testing::compare_similar_matrices, a, b, eps)
} // end namespace testing
} // end namespace kwiver
#endif