forked from sammy-tri/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolic_expression_transform_test.cc
156 lines (135 loc) · 5.82 KB
/
symbolic_expression_transform_test.cc
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
#include <gtest/gtest.h>
#include "drake/common/eigen_types.h"
#include "drake/common/symbolic.h"
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/math/rotation_matrix.h"
namespace drake {
namespace symbolic {
namespace {
using math::RotationMatrixd;
class SymbolicExpressionTransformationTest : public ::testing::Test {
protected:
void SetUp() override {
R1_ = RotationMatrixd::MakeXRotation(.25 * M_PI) *
RotationMatrixd::MakeYRotation(.5 * M_PI) *
RotationMatrixd::MakeZRotation(.33 * M_PI);
R2_ = RotationMatrixd::MakeXRotation(M_PI / 2) *
RotationMatrixd::MakeYRotation(-M_PI / 2) *
RotationMatrixd::MakeZRotation(M_PI / 2);
affine_.setIdentity();
affine_.rotate(R1_.matrix());
affine_.translation() << 1.0, 2.0, 3.0;
affine_compact_.setIdentity();
affine_compact_.rotate((R1_ * R2_).matrix());
affine_compact_.translation() << -2.0, 1.0, 3.0;
isometry_.setIdentity();
isometry_.rotate(-R2_.matrix());
isometry_.translation() << 3.0, -1.0, 2.0;
projective_.setIdentity();
projective_.rotate(-(R2_ * R1_).matrix());
projective_.translation() << 2.0, 3.0, -1.0;
}
// Rotation Matrices.
RotationMatrixd R1_;
RotationMatrixd R2_;
// Transformations.
Eigen::Transform<double, 3, Eigen::Affine, Eigen::DontAlign> affine_;
Eigen::Transform<double, 3, Eigen::AffineCompact, Eigen::DontAlign>
affine_compact_;
Eigen::Transform<double, 3, Eigen::Isometry, Eigen::DontAlign> isometry_;
Eigen::Transform<double, 3, Eigen::Projective, Eigen::DontAlign> projective_;
};
// Checks if `lhs.cast<Expression>() * rhs` and `(lhs * rhs).cast<Expression>()`
// produce almost identical output. Also checks the symmetric case. See the
// following commutative diagram.
// * rhs
// lhs --------------------------> lhs * rhs
// || ||
// || cast<Expression>() || cast<Expression>()
// \/ ||
// lhs.cast<Expression>() ||
// || ||
// || * rhs ||
// \/ ? \/
// lhs.cast<Expression>() * rhs == (lhs * rhs).cast<Expression>()
//
// The exactness is not guaranteed due to the non-associativity of
// floating-point arithmetic (+, *).
template <typename LhsTransform, typename RhsTransform>
::testing::AssertionResult CheckMultiplication(const LhsTransform& lhs,
const RhsTransform& rhs) {
const auto expected = (lhs * rhs).template cast<Expression>();
const auto lhs_cast = lhs.template cast<Expression>() * rhs;
const auto rhs_cast = lhs * rhs.template cast<Expression>();
// Types should be identical.
::testing::StaticAssertTypeEq<decltype(expected), decltype(lhs_cast)>();
::testing::StaticAssertTypeEq<decltype(expected), decltype(rhs_cast)>();
// Their matrices should be almost identical.
const double absolute_tolerance{1e-15};
EXPECT_TRUE(CompareMatrices(expected.matrix(), lhs_cast.matrix(),
absolute_tolerance));
EXPECT_TRUE(CompareMatrices(expected.matrix(), rhs_cast.matrix(),
absolute_tolerance));
return ::testing::AssertionSuccess();
}
TEST_F(SymbolicExpressionTransformationTest, CheckMultiplication) {
EXPECT_TRUE(CheckMultiplication(affine_, affine_));
EXPECT_TRUE(CheckMultiplication(affine_, affine_compact_));
EXPECT_TRUE(CheckMultiplication(affine_, isometry_));
EXPECT_TRUE(CheckMultiplication(affine_, projective_));
EXPECT_TRUE(CheckMultiplication(affine_compact_, affine_));
EXPECT_TRUE(CheckMultiplication(affine_compact_, affine_compact_));
EXPECT_TRUE(CheckMultiplication(affine_compact_, isometry_));
EXPECT_TRUE(CheckMultiplication(affine_compact_, projective_));
EXPECT_TRUE(CheckMultiplication(isometry_, affine_));
EXPECT_TRUE(CheckMultiplication(isometry_, affine_compact_));
EXPECT_TRUE(CheckMultiplication(isometry_, isometry_));
EXPECT_TRUE(CheckMultiplication(isometry_, projective_));
EXPECT_TRUE(CheckMultiplication(projective_, affine_));
EXPECT_TRUE(CheckMultiplication(projective_, affine_compact_));
EXPECT_TRUE(CheckMultiplication(projective_, isometry_));
EXPECT_TRUE(CheckMultiplication(projective_, projective_));
}
TEST_F(SymbolicExpressionTransformationTest, ExpectedAnswers) {
Isometry3<Expression> isometry_expr;
const Variable x{"x"};
const Variable y{"y"};
const Variable z{"z"};
isometry_expr.setIdentity();
isometry_expr.translation().x() = x;
isometry_expr.translation().y() = y;
isometry_expr.translation().z() = z;
Isometry3<double> isometry_double;
isometry_double.setIdentity();
isometry_double.translation().x() = 1;
isometry_double.translation().y() = 2;
isometry_double.translation().z() = 3;
Eigen::Matrix3d R;
// clang-format off
R << 0, 0, -1,
0, 1, 0,
1, 0, 1;
// clang-format on
isometry_double.rotate(R);
// Expected result from Transform<Expression> * Transform<double>.
Eigen::Matrix<Expression, 4, 4> expected_expr_double;
// clang-format off
expected_expr_double << 0, 0, -1, (1 + x),
0, 1, 0, (2 + y),
1, 0, 1, (3 + z),
0, 0, 0, 1;
// clang-format on
EXPECT_EQ((isometry_expr * isometry_double).matrix(), expected_expr_double);
// Expected result from Transform<double> * Transform<Expression>.
Eigen::Matrix<Expression, 4, 4> expected_double_expr;
// clang-format off
expected_double_expr << 0, 0, -1, (1 - z),
0, 1, 0, (2 + y),
1, 0, 1, (3 + x + z),
0, 0, 0, 1;
// clang-format on
EXPECT_EQ((isometry_double * isometry_expr).matrix(), expected_double_expr);
}
} // namespace
} // namespace symbolic
} // namespace drake