Skip to content

Commit

Permalink
Improve readability of type trait used to distinguish scalar/vector t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
DmitriyKorchemkin committed Feb 15, 2022
1 parent f5b0320 commit 574a7a7
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions sophus/ceres_local_parameterization.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#ifndef SOPHUS_CERES_LOCAL_PARAMETERIZATION_HPP
#define SOPHUS_CERES_LOCAL_PARAMETERIZATION_HPP

#include <ceres/local_parameterization.h>

namespace Sophus {

template <class T, std::size_t = sizeof(T)>
constexpr std::true_type complete(T*);
constexpr std::false_type complete(...);

template <class T>
using IsSpecialized = decltype(complete(std::declval<T*>()));

/// Type trait used to distinguish mappable vector types from scalars
///
/// We use this class to distinguish Sophus::Vector<Scalar, N> from Scalar types
/// in LieGroup<T>::Tangent
///
/// Fortunately, ceres::Jet is not mappable
template <typename T>
struct is_mappable_type_t {
template <typename U>
using EigenTraits = Eigen::internal::traits<U>;
// Eigen::Map<T> requires Eigen::internal::traits<T> type to be complete
template <typename U>
static auto map_test(U*)
-> std::integral_constant<bool, sizeof(EigenTraits<U>) ==
sizeof(EigenTraits<U>)>;
static auto map_test(...) -> std::false_type;

using type = decltype(map_test((T*)nullptr));
static constexpr bool value = type::value;
};
/// Having Eigen::internal::traits<T> specialized for T is a prerequisite for
/// constructing Eigen::Map<T> (we will call such T "mappable" type)
///
/// Fortunately, ceres::Jet is not a mappable type
template <class T>
using HasEigenTraits = IsSpecialized<Eigen::internal::traits<std::decay_t<T>>>;

template <typename T>
constexpr bool is_mappable_type_v = is_mappable_type_t<T>::value;
template <class T>
constexpr bool HasEigenTraitsV = HasEigenTraits<T>::value;

/// Helper for mapping tangent vectors (scalars) over pointers to data
template <typename T, typename E = void>
Expand All @@ -38,7 +39,7 @@ struct Mapper {
};

template <typename T>
struct Mapper<T, typename std::enable_if<is_mappable_type_v<T>>::type> {
struct Mapper<T, typename std::enable_if<HasEigenTraitsV<T>>::type> {
using Scalar = typename T::Scalar;
using Map = Eigen::Map<T>;
using ConstMap = Eigen::Map<const T>;
Expand Down

0 comments on commit 574a7a7

Please sign in to comment.