Skip to content

Commit 90386a5

Browse files
committed
isMatrixOfSize
1 parent 8bab363 commit 90386a5

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

gtsam/navigation/ManifoldEKF.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ManifoldEKF {
7373
n_ = traits<M>::GetDimension(X0);
7474
if constexpr (Dim == Eigen::Dynamic) {
7575
// Validate dimensions of initial covariance P0.
76-
if (P0.rows() != n_ || P0.cols() != n_) {
76+
if (!isMatrixOfSize(P0, n_, n_)) {
7777
throw std::invalid_argument(
7878
"ManifoldEKF: Initial covariance P0 dimensions (" +
7979
std::to_string(P0.rows()) + "x" + std::to_string(P0.cols()) +
@@ -115,8 +115,7 @@ class ManifoldEKF {
115115
*/
116116
void predict(const M& X_next, const Jacobian& F, const Covariance& Q) {
117117
if constexpr (Dim == Eigen::Dynamic) {
118-
if (F.rows() != n_ || F.cols() != n_ || Q.rows() != n_ ||
119-
Q.cols() != n_) {
118+
if (!isMatrixOfSize(F, n_, n_) || !isMatrixOfSize(Q, n_, n_)) {
120119
throw std::invalid_argument(
121120
"ManifoldEKF::predict: Dynamic F/Q dimensions must match state "
122121
"dimension " +
@@ -257,24 +256,32 @@ class ManifoldEKF {
257256
/// Validate inputs to update.
258257
void validateInputs(const gtsam::Vector& prediction, const Matrix& H,
259258
const gtsam::Vector& z, const Matrix& R) {
260-
const int m = static_cast<int>(prediction.size());
261-
if (static_cast<int>(z.size()) != m) {
259+
const size_t m = static_cast<size_t>(prediction.size());
260+
if (static_cast<size_t>(z.size()) != m) {
262261
throw std::invalid_argument(
263262
"ManifoldEKF::updateWithVector: prediction and z must have same "
264263
"length.");
265264
}
266-
if (H.rows() != m || H.cols() != n_) {
265+
if (!isMatrixOfSize(H, m, n_)) {
267266
throw std::invalid_argument(
268267
"ManifoldEKF::updateWithVector: H must be m x n where m = "
269268
"measurement size and n = state dimension.");
270269
}
271-
if (R.rows() != m || R.cols() != m) {
270+
if (!isMatrixOfSize(R, m, m)) {
272271
throw std::invalid_argument(
273272
"ManifoldEKF::updateWithVector: R must be m x m where m = "
274273
"measurement size.");
275274
}
276275
}
277276

277+
/// Check whether a matrix has the expected runtime dimensions.
278+
template <typename MatrixType>
279+
static bool isMatrixOfSize(const MatrixType& matrix, size_t rows,
280+
size_t cols) {
281+
return static_cast<size_t>(matrix.rows()) == rows &&
282+
static_cast<size_t>(matrix.cols()) == cols;
283+
}
284+
278285
protected:
279286
M X_; ///< Manifold state estimate.
280287
Covariance P_; ///< Covariance (Eigen::Matrix<double, Dim, Dim>).

0 commit comments

Comments
 (0)