empty_ is gone

release/4.3a0
dellaert 2014-11-28 17:13:54 +01:00
parent bd342261e4
commit a89b4d2168
2 changed files with 40 additions and 25 deletions

View File

@ -44,7 +44,6 @@ public:
private: private:
bool empty_; ///< flag whether initialized or not
Eigen::Map<Fixed> map_; /// View on constructor argument, if given Eigen::Map<Fixed> map_; /// View on constructor argument, if given
// Trick from http://eigen.tuxfamily.org/dox/group__TutorialMapClass.html // Trick from http://eigen.tuxfamily.org/dox/group__TutorialMapClass.html
@ -57,25 +56,25 @@ public:
/// Default constructor acts like boost::none /// Default constructor acts like boost::none
OptionalJacobian() : OptionalJacobian() :
empty_(true), map_(NULL) { map_(NULL) {
} }
/// Constructor that will usurp data of a fixed-size matrix /// Constructor that will usurp data of a fixed-size matrix
OptionalJacobian(Fixed& fixed) : OptionalJacobian(Fixed& fixed) :
empty_(false), map_(NULL) { map_(NULL) {
usurp(fixed.data()); usurp(fixed.data());
} }
/// Constructor that will usurp data of a fixed-size matrix, pointer version /// Constructor that will usurp data of a fixed-size matrix, pointer version
OptionalJacobian(Fixed* fixedPtr) : OptionalJacobian(Fixed* fixedPtr) :
empty_(fixedPtr==NULL), map_(NULL) { map_(NULL) {
if (fixedPtr) if (fixedPtr)
usurp(fixedPtr->data()); usurp(fixedPtr->data());
} }
/// Constructor that will resize a dynamic matrix (unless already correct) /// Constructor that will resize a dynamic matrix (unless already correct)
OptionalJacobian(Eigen::MatrixXd& dynamic) : OptionalJacobian(Eigen::MatrixXd& dynamic) :
empty_(false), map_(NULL) { map_(NULL) {
dynamic.resize(Rows, Cols); // no malloc if correct size dynamic.resize(Rows, Cols); // no malloc if correct size
usurp(dynamic.data()); usurp(dynamic.data());
} }
@ -84,12 +83,12 @@ public:
/// Constructor with boost::none just makes empty /// Constructor with boost::none just makes empty
OptionalJacobian(boost::none_t none) : OptionalJacobian(boost::none_t none) :
empty_(true), map_(NULL) { map_(NULL) {
} }
/// Constructor compatible with old-style derivatives /// Constructor compatible with old-style derivatives
OptionalJacobian(const boost::optional<Eigen::MatrixXd&> optional) : OptionalJacobian(const boost::optional<Eigen::MatrixXd&> optional) :
empty_(!optional), map_(NULL) { map_(NULL) {
if (optional) { if (optional) {
optional->resize(Rows, Cols); optional->resize(Rows, Cols);
usurp(optional->data()); usurp(optional->data());
@ -100,7 +99,7 @@ public:
/// Return true is allocated, false if default constructor was used /// Return true is allocated, false if default constructor was used
operator bool() const { operator bool() const {
return !empty_; return map_.data();
} }
/// De-reference, like boost optional /// De-reference, like boost optional

View File

@ -24,21 +24,37 @@ using namespace std;
using namespace gtsam; using namespace gtsam;
//****************************************************************************** //******************************************************************************
TEST( OptionalJacobian, Constructors ) TEST( OptionalJacobian, Constructors ) {
{
Matrix23 fixed; Matrix23 fixed;
Matrix dynamic;
OptionalJacobian<2, 3> H1; OptionalJacobian<2, 3> H1;
EXPECT(!H1);
OptionalJacobian<2, 3> H2(fixed); OptionalJacobian<2, 3> H2(fixed);
EXPECT(H2);
OptionalJacobian<2, 3> H3(&fixed); OptionalJacobian<2, 3> H3(&fixed);
EXPECT(H3);
Matrix dynamic;
OptionalJacobian<2, 3> H4(dynamic); OptionalJacobian<2, 3> H4(dynamic);
EXPECT(H4);
OptionalJacobian<2, 3> H5(boost::none); OptionalJacobian<2, 3> H5(boost::none);
EXPECT(!H5);
boost::optional<Matrix&> optional(dynamic); boost::optional<Matrix&> optional(dynamic);
OptionalJacobian<2, 3> H6(optional); OptionalJacobian<2, 3> H6(optional);
EXPECT(H6);
} }
//****************************************************************************** //******************************************************************************
void test3(OptionalJacobian<2,3> H = OptionalJacobian<2,3>()) { void test(OptionalJacobian<2, 3> H = boost::none) {
if (H)
*H = Matrix23::Zero();
}
void testPtr(Matrix23* H = NULL) {
if (H) if (H)
*H = Matrix23::Zero(); *H = Matrix23::Zero();
} }
@ -49,35 +65,35 @@ TEST( OptionalJacobian, Ref2) {
expected = Matrix23::Zero(); expected = Matrix23::Zero();
// Default argument does nothing // Default argument does nothing
test3(); test();
// Fixed size, no copy // Fixed size, no copy
Matrix23 fixed1; Matrix23 fixed1;
fixed1.setOnes(); fixed1.setOnes();
test3(fixed1); test(fixed1);
EXPECT(assert_equal(expected,fixed1)); EXPECT(assert_equal(expected,fixed1));
// Fixed size, no copy, pointer style // Fixed size, no copy, pointer style
Matrix23 fixed2; Matrix23 fixed2;
fixed2.setOnes(); fixed2.setOnes();
test3(&fixed2); test(&fixed2);
EXPECT(assert_equal(expected,fixed2)); EXPECT(assert_equal(expected,fixed2));
// Empty is no longer a sign we don't want a matrix, we want it resized // Empty is no longer a sign we don't want a matrix, we want it resized
Matrix dynamic0; Matrix dynamic0;
test3(dynamic0); test(dynamic0);
EXPECT(assert_equal(expected,dynamic0)); EXPECT(assert_equal(expected,dynamic0));
// Dynamic wrong size // Dynamic wrong size
Matrix dynamic1(3, 5); Matrix dynamic1(3, 5);
dynamic1.setOnes(); dynamic1.setOnes();
test3(dynamic1); test(dynamic1);
EXPECT(assert_equal(expected,dynamic0)); EXPECT(assert_equal(expected,dynamic0));
// Dynamic right size // Dynamic right size
Matrix dynamic2(2, 5); Matrix dynamic2(2, 5);
dynamic2.setOnes(); dynamic2.setOnes();
test3(dynamic2); test(dynamic2);
EXPECT(assert_equal(dynamic2,dynamic0)); EXPECT(assert_equal(dynamic2,dynamic0));
} }