optional jacobian with matrix references
parent
9bc7f3c6dd
commit
1756114986
|
|
@ -19,8 +19,10 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
#include <gtsam/config.h> // Configuration from CMake
|
#include <gtsam/config.h> // Configuration from CMake
|
||||||
#include <Eigen/Dense>
|
#include <Eigen/Dense>
|
||||||
|
#include <optional>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -118,8 +120,21 @@ public:
|
||||||
"Expected: ") +
|
"Expected: ") +
|
||||||
"(" + std::to_string(Rows) + ", " + std::to_string(Cols) + ")");
|
"(" + std::to_string(Rows) + ", " + std::to_string(Cols) + ")");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructor with std::nullopt just makes empty
|
||||||
|
OptionalJacobian(std::nullopt_t /*none*/) :
|
||||||
|
map_(nullptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructor compatible with old-style derivatives
|
||||||
|
OptionalJacobian(const std::optional<std::reference_wrapper<Eigen::MatrixXd>> optional) :
|
||||||
|
map_(nullptr) {
|
||||||
|
if (optional) {
|
||||||
|
optional->get().resize(Rows, Cols);
|
||||||
|
usurp(optional->get().data());
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Constructor that will usurp data of a block expression
|
/// Constructor that will usurp data of a block expression
|
||||||
/// TODO(frank): unfortunately using a Map makes usurping non-contiguous memory impossible
|
/// TODO(frank): unfortunately using a Map makes usurping non-contiguous memory impossible
|
||||||
// template <typename Derived, bool InnerPanel>
|
// template <typename Derived, bool InnerPanel>
|
||||||
|
|
@ -184,7 +199,7 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Default constructor acts like
|
/// Default constructor
|
||||||
OptionalJacobian() :
|
OptionalJacobian() :
|
||||||
pointer_(nullptr) {
|
pointer_(nullptr) {
|
||||||
}
|
}
|
||||||
|
|
@ -195,6 +210,17 @@ public:
|
||||||
/// Construct from refrence to dynamic matrix
|
/// Construct from refrence to dynamic matrix
|
||||||
OptionalJacobian(Jacobian& dynamic) : pointer_(&dynamic) {}
|
OptionalJacobian(Jacobian& dynamic) : pointer_(&dynamic) {}
|
||||||
|
|
||||||
|
/// Constructor with std::nullopt just makes empty
|
||||||
|
OptionalJacobian(std::nullopt_t /*none*/) :
|
||||||
|
pointer_(nullptr) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructor for optional matrix reference
|
||||||
|
OptionalJacobian(const std::optional<std::reference_wrapper<Eigen::MatrixXd>> optional) :
|
||||||
|
pointer_(nullptr) {
|
||||||
|
if (optional) pointer_ = &((*optional).get());
|
||||||
|
}
|
||||||
|
|
||||||
/// Return true if allocated, false if default constructor was used
|
/// Return true if allocated, false if default constructor was used
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
return pointer_!=nullptr;
|
return pointer_!=nullptr;
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,9 @@
|
||||||
#include <gtsam/base/OptionalJacobian.h>
|
#include <gtsam/base/OptionalJacobian.h>
|
||||||
#include <CppUnitLite/TestHarness.h>
|
#include <CppUnitLite/TestHarness.h>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace gtsam;
|
using namespace gtsam;
|
||||||
|
|
||||||
|
|
@ -32,6 +35,7 @@ using namespace gtsam;
|
||||||
TEST( OptionalJacobian, Constructors ) {
|
TEST( OptionalJacobian, Constructors ) {
|
||||||
Matrix23 fixed;
|
Matrix23 fixed;
|
||||||
Matrix dynamic;
|
Matrix dynamic;
|
||||||
|
std::optional<std::reference_wrapper<Matrix>> optionalRef(std::ref(dynamic));
|
||||||
|
|
||||||
OptionalJacobian<2, 3> H;
|
OptionalJacobian<2, 3> H;
|
||||||
EXPECT(!H);
|
EXPECT(!H);
|
||||||
|
|
@ -40,12 +44,17 @@ TEST( OptionalJacobian, Constructors ) {
|
||||||
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, &dynamic, true);
|
TEST_CONSTRUCTOR(2, 3, &dynamic, true);
|
||||||
|
TEST_CONSTRUCTOR(2, 3, std::nullopt, false);
|
||||||
|
TEST_CONSTRUCTOR(2, 3, optionalRef, true);
|
||||||
|
|
||||||
// Test dynamic
|
// Test dynamic
|
||||||
OptionalJacobian<-1, -1> H7;
|
OptionalJacobian<-1, -1> H7;
|
||||||
EXPECT(!H7);
|
EXPECT(!H7);
|
||||||
|
|
||||||
TEST_CONSTRUCTOR(-1, -1, dynamic, true);
|
TEST_CONSTRUCTOR(-1, -1, dynamic, true);
|
||||||
|
TEST_CONSTRUCTOR(-1, -1, std::nullopt, false);
|
||||||
|
TEST_CONSTRUCTOR(-1, -1, optionalRef, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue