Skip to content

Commit b6908be

Browse files
committed
New FastNoiseModelFactor with more efficient linearization
1 parent db6010d commit b6908be

File tree

9 files changed

+536
-39
lines changed

9 files changed

+536
-39
lines changed

gtsam/base/OptionalJacobian.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,27 @@ class OptionalJacobian {
120120
"Expected: ") +
121121
"(" + std::to_string(Rows) + ", " + std::to_string(Cols) + ")");
122122
}
123-
}
123+
}
124+
125+
/**
126+
* @brief Constructor from an Eigen::Ref *value*. Will not usurp if dimension is wrong
127+
* @note This is important so we don't overwrite someone else's memory!
128+
*/
129+
template<class MATRIX>
130+
OptionalJacobian(Eigen::Ref<MATRIX>* dynamic_optional) :
131+
map_(nullptr) {
132+
if (!dynamic_optional) return; // stay empty
133+
Eigen::Ref<MATRIX>& dynamic_ref = *dynamic_optional;
134+
if (dynamic_ref.rows() == Rows && dynamic_ref.cols() == Cols && !dynamic_ref.IsRowMajor) {
135+
usurp(dynamic_ref.data());
136+
} else {
137+
throw std::invalid_argument(
138+
std::string("OptionalJacobian called with wrong dimensions or "
139+
"storage order.\n"
140+
"Expected: ") +
141+
"(" + std::to_string(Rows) + ", " + std::to_string(Cols) + ")");
142+
}
143+
}
124144

125145
/// Constructor with std::nullopt just makes empty
126146
OptionalJacobian(std::nullopt_t /*none*/) :

gtsam/linear/GaussianConditional.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ namespace gtsam {
322322
for (auto&& key : parents()) newKeys.push_back(key);
323323

324324
// Hopefully second newAb copy below is optimized out...
325-
return std::make_shared<JacobianFactor>(newKeys, newAb, model_);
325+
return std::make_shared<JacobianFactor>(newKeys, std::move(newAb), model_);
326326
}
327327

328328
/* **************************************************************************/

gtsam/linear/JacobianFactor-inl.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,14 @@ namespace gtsam {
3434
}
3535

3636
/* ************************************************************************* */
37-
template <typename KEYS>
38-
JacobianFactor::JacobianFactor(const KEYS& keys,
39-
const VerticalBlockMatrix& augmentedMatrix,
37+
template <typename KEYS, typename MATRIX,
38+
typename = typename std::enable_if<std::is_same<
39+
std::decay_t<MATRIX>, VerticalBlockMatrix>::value>::type>
40+
JacobianFactor::JacobianFactor(KEYS&& keys, MATRIX&& augmentedMatrix,
4041
const SharedDiagonal& model)
41-
: Base(keys), Ab_(augmentedMatrix), model_(model) {
42-
checkAb(model, augmentedMatrix);
43-
}
44-
45-
/* ************************************************************************* */
46-
template <typename KEYS>
47-
JacobianFactor::JacobianFactor(const KEYS& keys,
48-
VerticalBlockMatrix&& augmentedMatrix,
49-
const SharedDiagonal& model)
50-
: Base(keys), Ab_(std::move(augmentedMatrix)), model_(model) {
42+
: Base(std::forward<KEYS>(keys)),
43+
Ab_(std::forward<MATRIX>(augmentedMatrix)),
44+
model_(model) {
5145
checkAb(model, Ab_);
5246
}
5347

gtsam/linear/JacobianFactor.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,10 @@ namespace gtsam {
145145
template<typename TERMS>
146146
JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal());
147147

148-
/** Constructor with arbitrary number keys, and where the augmented matrix
149-
* is given all together instead of in block terms.
150-
*/
151-
template <typename KEYS>
152-
JacobianFactor(const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix,
153-
const SharedDiagonal& sigmas = SharedDiagonal());
154-
155148
/** Construct with an rvalue VerticalBlockMatrix, to allow std::move. */
156-
template <typename KEYS>
157-
JacobianFactor(const KEYS& keys, VerticalBlockMatrix&& augmentedMatrix,
158-
const SharedDiagonal& model);
149+
template <typename KEYS, typename MATRIX, typename>
150+
JacobianFactor(KEYS&& keys, MATRIX&& augmentedMatrix,
151+
const SharedDiagonal& model = SharedDiagonal());
159152

160153
/**
161154
* Build a dense joint factor from all the factors in a factor graph. If a VariableSlots

gtsam/linear/NoiseModel.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ namespace gtsam {
123123
}
124124

125125
/** in-place whiten, override if can be done more efficiently */
126-
virtual void whitenInPlace(Eigen::Block<Vector>& v) const {
127-
v = whiten(v);
126+
virtual void whitenInPlace(Eigen::Block<Matrix> v) const {
127+
v = Whiten(v);
128128
}
129129

130130
/** in-place unwhiten, override if can be done more efficiently */
131-
virtual void unwhitenInPlace(Eigen::Block<Vector>& v) const {
131+
virtual void unwhitenInPlace(Eigen::Block<Matrix> v) const {
132132
v = unwhiten(v);
133133
}
134134

@@ -644,8 +644,8 @@ namespace gtsam {
644644
void WhitenInPlace(Eigen::Block<Matrix> /*H*/) const override {}
645645
void whitenInPlace(Vector& /*v*/) const override {}
646646
void unwhitenInPlace(Vector& /*v*/) const override {}
647-
void whitenInPlace(Eigen::Block<Vector>& /*v*/) const override {}
648-
void unwhitenInPlace(Eigen::Block<Vector>& /*v*/) const override {}
647+
void whitenInPlace(Eigen::Block<Matrix> /*v*/) const override {}
648+
void unwhitenInPlace(Eigen::Block<Matrix> /*v*/) const override {}
649649

650650
private:
651651
#if GTSAM_ENABLE_BOOST_SERIALIZATION

gtsam/navigation/ImuBias.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ namespace imuBias {
6666
// }
6767
/// ostream operator
6868
std::ostream& operator<<(std::ostream& os, const ConstantBias& bias) {
69-
os << "acc = " << bias.accelerometer().transpose();
70-
os << " gyro = " << bias.gyroscope().transpose();
69+
os << "acc = " << bias.accelerometer().transpose().eval();
70+
os << " gyro = " << bias.gyroscope().transpose().eval();
7171
return os;
7272
}
7373

0 commit comments

Comments
 (0)