Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructor for OptionalJacobian #884

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gtsam/base/OptionalJacobian.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class OptionalJacobian {
usurp(dynamic.data());
}

/// Constructor that will resize a dynamic matrix (unless already correct)
OptionalJacobian(Eigen::MatrixXd* dynamic) :
map_(nullptr) {
dynamic->resize(Rows, Cols); // no malloc if correct size
usurp(dynamic->data());
}

#ifndef OPTIONALJACOBIAN_NOBOOST

/// Constructor with boost::none just makes empty
Expand Down
62 changes: 37 additions & 25 deletions gtsam/base/tests/testOptionalJacobian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,33 @@ using namespace std;
using namespace gtsam;

//******************************************************************************
#define TEST_CONSTRUCTOR(DIM1, DIM2, X, TRUTHY) \
{ \
OptionalJacobian<DIM1, DIM2> H(X); \
EXPECT(H == TRUTHY); \
}
TEST( OptionalJacobian, Constructors ) {
Matrix23 fixed;

OptionalJacobian<2, 3> H1;
EXPECT(!H1);

OptionalJacobian<2, 3> H2(fixed);
EXPECT(H2);

OptionalJacobian<2, 3> H3(&fixed);
EXPECT(H3);

Matrix dynamic;
OptionalJacobian<2, 3> H4(dynamic);
EXPECT(H4);
boost::optional<Matrix&> optional(dynamic);

OptionalJacobian<2, 3> H5(boost::none);
EXPECT(!H5);
OptionalJacobian<2, 3> H;
EXPECT(!H);

boost::optional<Matrix&> optional(dynamic);
OptionalJacobian<2, 3> H6(optional);
EXPECT(H6);
TEST_CONSTRUCTOR(2, 3, fixed, true);
TEST_CONSTRUCTOR(2, 3, &fixed, true);
TEST_CONSTRUCTOR(2, 3, dynamic, true);
TEST_CONSTRUCTOR(2, 3, &dynamic, true);
TEST_CONSTRUCTOR(2, 3, boost::none, false);
TEST_CONSTRUCTOR(2, 3, optional, true);

// Test dynamic
OptionalJacobian<-1, -1> H7;
EXPECT(!H7);

OptionalJacobian<-1, -1> H8(dynamic);
EXPECT(H8);

OptionalJacobian<-1, -1> H9(boost::none);
EXPECT(!H9);

OptionalJacobian<-1, -1> H10(optional);
EXPECT(H10);
TEST_CONSTRUCTOR(-1, -1, dynamic, true);
TEST_CONSTRUCTOR(-1, -1, boost::none, false);
TEST_CONSTRUCTOR(-1, -1, optional, true);
}

//******************************************************************************
Expand Down Expand Up @@ -101,6 +94,25 @@ TEST( OptionalJacobian, Fixed) {
dynamic2.setOnes();
test(dynamic2);
EXPECT(assert_equal(kTestMatrix, dynamic2));

{ // Dynamic pointer
// Passing in an empty matrix means we want it resized
Matrix dynamic0;
test(&dynamic0);
EXPECT(assert_equal(kTestMatrix, dynamic0));

// Dynamic wrong size
Matrix dynamic1(3, 5);
dynamic1.setOnes();
test(&dynamic1);
EXPECT(assert_equal(kTestMatrix, dynamic1));

// Dynamic right size
Matrix dynamic2(2, 5);
dynamic2.setOnes();
test(&dynamic2);
EXPECT(assert_equal(kTestMatrix, dynamic2));
}
}

//******************************************************************************
Expand Down