/* ---------------------------------------------------------------------------- * GTSAM Copyright 2010, Georgia Tech Research Corporation, * Atlanta, Georgia 30332-0415 * All Rights Reserved * Authors: Frank Dellaert, et al. (see THANKS for the full author list) * See LICENSE for the license information * -------------------------------1------------------------------------------- */ /** * @file testExpression.cpp * @date September 18, 2014 * @author Frank Dellaert * @author Paul Furgale * @brief unit tests for Block Automatic Differentiation */ #include #include #include #include #include #include using namespace std; using namespace gtsam; /* ************************************************************************* */ template Point2 uncalibrate(const CAL& K, const Point2& p, boost::optional Dcal, boost::optional Dp) { return K.uncalibrate(p, Dcal, Dp); } /* ************************************************************************* */ TEST(Expression, constant) { Expression R(Rot3::identity()); Values values; Augmented a = R.augmented(values); EXPECT(assert_equal(Rot3::identity(), a.value())); JacobianMap expected; EXPECT(a.jacobians() == expected); } /* ************************************************************************* */ TEST(Expression, leaf) { Expression R(100); Values values; values.insert(100,Rot3::identity()); Augmented a = R.augmented(values); EXPECT(assert_equal(Rot3::identity(), a.value())); JacobianMap expected; expected[100] = eye(3); EXPECT(a.jacobians() == expected); } /* ************************************************************************* */ TEST(Expression, nullaryMethod) { Expression p(67); Expression norm(p, &Point3::norm); Values values; values.insert(67,Point3(3,4,5)); Augmented a = norm.augmented(values); EXPECT(a.value() == sqrt(50)); JacobianMap expected; expected[67] = (Matrix(1,3) << 3/sqrt(50),4/sqrt(50),5/sqrt(50)); EXPECT(assert_equal(expected.at(67),a.jacobians().at(67))); } /* ************************************************************************* */ TEST(Expression, test) { // Test Constant expression Expression c(0); // Create leaves Expression x(1); Expression p(2); Expression K(3); // Create expression tree Expression p_cam(x, &Pose3::transform_to, p); Expression projection(PinholeCamera::project_to_camera, p_cam); Expression uv_hat(uncalibrate, K, projection); // Check keys std::set expectedKeys; expectedKeys.insert(1); expectedKeys.insert(2); expectedKeys.insert(3); EXPECT(expectedKeys == uv_hat.keys()); } /* ************************************************************************* */ TEST(Expression, compose1) { // Create expression Expression R1(1), R2(2); Expression R3 = R1 * R2; // Check keys std::set expectedKeys; expectedKeys.insert(1); expectedKeys.insert(2); EXPECT(expectedKeys == R3.keys()); } /* ************************************************************************* */ // Test compose with arguments referring to the same rotation TEST(Expression, compose2) { // Create expression Expression R1(1), R2(1); Expression R3 = R1 * R2; // Check keys std::set expectedKeys; expectedKeys.insert(1); EXPECT(expectedKeys == R3.keys()); } /* ************************************************************************* */ // Test compose with one arguments referring to constant rotation TEST(Expression, compose3) { // Create expression Expression R1(Rot3::identity()), R2(3); Expression R3 = R1 * R2; // Check keys std::set expectedKeys; expectedKeys.insert(3); EXPECT(expectedKeys == R3.keys()); } /* ************************************************************************* */ // Test with ternary function Rot3 composeThree(const Rot3& R1, const Rot3& R2, const Rot3& R3, boost::optional H1, boost::optional H2, boost::optional H3) { // return dummy derivatives (not correct, but that's ok for testing here) if (H1) *H1 = eye(3); if (H2) *H2 = eye(3); if (H3) *H3 = eye(3); return R1 * (R2 * R3); } TEST(Expression, ternary) { // Create expression Expression A(1), B(2), C(3); Expression ABC(composeThree, A, B, C); // Check keys std::set expectedKeys; expectedKeys.insert(1); expectedKeys.insert(2); expectedKeys.insert(3); EXPECT(expectedKeys == ABC.keys()); } /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */