parent
5749565e52
commit
f8eece464d
|
|
@ -259,98 +259,97 @@ namespace gtsam {
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
template<>
|
||||
inline bool Values::filterHelper<Value>(const boost::function<bool(Key)> filter,
|
||||
const ConstKeyValuePair& key_value) {
|
||||
// Filter and check the type
|
||||
return filter(key_value.key);
|
||||
}
|
||||
template<>
|
||||
inline bool Values::filterHelper<Value>(const boost::function<bool(Key)> filter,
|
||||
const ConstKeyValuePair& key_value) {
|
||||
// Filter and check the type
|
||||
return filter(key_value.key);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
namespace internal {
|
||||
namespace internal {
|
||||
|
||||
// Check the type and throw exception if incorrect
|
||||
// Generic version, partially specialized below for various Eigen Matrix types
|
||||
template <typename ValueType>
|
||||
struct handle {
|
||||
ValueType operator()(Key j, const Value* const pointer) {
|
||||
try {
|
||||
// value returns a const ValueType&, and the return makes a copy !!!!!
|
||||
return dynamic_cast<const GenericValue<ValueType>&>(*pointer).value();
|
||||
} catch (std::bad_cast&) {
|
||||
throw ValuesIncorrectType(j, typeid(*pointer), typeid(ValueType));
|
||||
}
|
||||
}
|
||||
};
|
||||
// Check the type and throw exception if incorrect
|
||||
// Generic version, partially specialized below for various Eigen Matrix types
|
||||
template <typename ValueType>
|
||||
struct handle {
|
||||
ValueType operator()(Key j, const Value* const pointer) {
|
||||
try {
|
||||
// value returns a const ValueType&, and the return makes a copy !!!!!
|
||||
return dynamic_cast<const GenericValue<ValueType>&>(*pointer).value();
|
||||
} catch (std::bad_cast&) {
|
||||
throw ValuesIncorrectType(j, typeid(*pointer), typeid(ValueType));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename MatrixType, bool isDynamic>
|
||||
struct handle_matrix;
|
||||
template <typename MatrixType, bool isDynamic>
|
||||
struct handle_matrix;
|
||||
|
||||
// Handle dynamic matrices
|
||||
template <int M, int N>
|
||||
struct handle_matrix<Eigen::Matrix<double, M, N>, true> {
|
||||
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
|
||||
try {
|
||||
// value returns a const Matrix&, and the return makes a copy !!!!!
|
||||
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
|
||||
} catch (std::bad_cast&) {
|
||||
// If a fixed matrix was stored, we end up here as well.
|
||||
throw ValuesIncorrectType(j, typeid(*pointer), typeid(Eigen::Matrix<double, M, N>));
|
||||
}
|
||||
}
|
||||
};
|
||||
// Handle dynamic matrices
|
||||
template <int M, int N>
|
||||
struct handle_matrix<Eigen::Matrix<double, M, N>, true> {
|
||||
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
|
||||
try {
|
||||
// value returns a const Matrix&, and the return makes a copy !!!!!
|
||||
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
|
||||
} catch (std::bad_cast&) {
|
||||
// If a fixed matrix was stored, we end up here as well.
|
||||
throw ValuesIncorrectType(j, typeid(*pointer), typeid(Eigen::Matrix<double, M, N>));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Handle fixed matrices
|
||||
template <int M, int N>
|
||||
struct handle_matrix<Eigen::Matrix<double, M, N>, false> {
|
||||
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
|
||||
try {
|
||||
// value returns a const MatrixMN&, and the return makes a copy !!!!!
|
||||
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
|
||||
} catch (std::bad_cast&) {
|
||||
Matrix A;
|
||||
try {
|
||||
// Check if a dynamic matrix was stored
|
||||
A = handle_matrix<Eigen::MatrixXd, true>()(j, pointer); // will throw if not....
|
||||
} catch (const ValuesIncorrectType&) {
|
||||
// Or a dynamic vector
|
||||
A = handle_matrix<Eigen::VectorXd, true>()(j, pointer); // will throw if not....
|
||||
}
|
||||
// Yes: check size, and throw if not a match
|
||||
if (A.rows() != M || A.cols() != N)
|
||||
throw NoMatchFoundForFixed(M, N, A.rows(), A.cols());
|
||||
else
|
||||
return A; // copy but not malloc
|
||||
}
|
||||
}
|
||||
};
|
||||
// Handle fixed matrices
|
||||
template <int M, int N>
|
||||
struct handle_matrix<Eigen::Matrix<double, M, N>, false> {
|
||||
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
|
||||
try {
|
||||
// value returns a const MatrixMN&, and the return makes a copy !!!!!
|
||||
return dynamic_cast<const GenericValue<Eigen::Matrix<double, M, N>>&>(*pointer).value();
|
||||
} catch (std::bad_cast&) {
|
||||
Matrix A;
|
||||
try {
|
||||
// Check if a dynamic matrix was stored
|
||||
A = handle_matrix<Eigen::MatrixXd, true>()(j, pointer); // will throw if not....
|
||||
} catch (const ValuesIncorrectType&) {
|
||||
// Or a dynamic vector
|
||||
A = handle_matrix<Eigen::VectorXd, true>()(j, pointer); // will throw if not....
|
||||
}
|
||||
// Yes: check size, and throw if not a match
|
||||
if (A.rows() != M || A.cols() != N)
|
||||
throw NoMatchFoundForFixed(M, N, A.rows(), A.cols());
|
||||
else
|
||||
return A; // copy but not malloc
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Handle matrices
|
||||
template <int M, int N>
|
||||
struct handle<Eigen::Matrix<double, M, N>> {
|
||||
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
|
||||
return handle_matrix<Eigen::Matrix<double, M, N>,
|
||||
(M == Eigen::Dynamic || N == Eigen::Dynamic)>()(j, pointer);
|
||||
}
|
||||
};
|
||||
// Handle matrices
|
||||
template <int M, int N>
|
||||
struct handle<Eigen::Matrix<double, M, N>> {
|
||||
Eigen::Matrix<double, M, N> operator()(Key j, const Value* const pointer) {
|
||||
return handle_matrix<Eigen::Matrix<double, M, N>,
|
||||
(M == Eigen::Dynamic || N == Eigen::Dynamic)>()(j, pointer);
|
||||
}
|
||||
};
|
||||
|
||||
} // internal
|
||||
} // internal
|
||||
|
||||
/* *************************************************************************
|
||||
*/
|
||||
template <typename ValueType>
|
||||
const ValueType Values::at(Key j) const {
|
||||
// Find the item
|
||||
KeyValueMap::const_iterator item = values_.find(j);
|
||||
/* ************************************************************************* */
|
||||
template <typename ValueType>
|
||||
const ValueType Values::at(Key j) const {
|
||||
// Find the item
|
||||
KeyValueMap::const_iterator item = values_.find(j);
|
||||
|
||||
// Throw exception if it does not exist
|
||||
if (item == values_.end()) throw ValuesKeyDoesNotExist("at", j);
|
||||
// Throw exception if it does not exist
|
||||
if (item == values_.end()) throw ValuesKeyDoesNotExist("at", j);
|
||||
|
||||
// Check the type and throw exception if incorrect
|
||||
// h() split in two lines to avoid internal compiler error (MSVC2017)
|
||||
auto h = internal::handle<ValueType>();
|
||||
return h(j, item->second);
|
||||
// Check the type and throw exception if incorrect
|
||||
// h() split in two lines to avoid internal compiler error (MSVC2017)
|
||||
auto h = internal::handle<ValueType>();
|
||||
return h(j, item->second);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
|||
Loading…
Reference in New Issue