CortidQCT  1.2.2.52
EigenAdaptors.h
Go to the documentation of this file.
1 
12 #pragma once
13 
14 #include <Eigen/Core>
15 #include <gsl/gsl>
16 
17 #include <array>
18 
19 namespace CortidQCT {
20 namespace Internal {
21 
22 namespace Adaptor {
23 
24 namespace Detail_ {
25 
26 template <class T, class I>
27 constexpr inline auto subscript(T &&x,
28  I i) noexcept(noexcept(std::declval<T>()[i])) {
29  return x[i];
30 }
31 
32 template <class Derived, class I>
33 constexpr inline auto
34 subscript(Eigen::MatrixBase<Derived> const &x,
35  I i) noexcept(noexcept(x(std::declval<Eigen::Index>()))) {
36  return x(gsl::narrow_cast<Eigen::Index>(i));
37 }
38 
39 template <class Derived, class I>
40 constexpr inline auto
41 subscript(Eigen::MatrixBase<Derived> &&x,
42  I i) noexcept(noexcept(x(std::declval<Eigen::Index>()))) {
43  return x(gsl::narrow_cast<Eigen::Index>(i));
44 }
45 
46 template <class T, std::size_t N> struct ConstructionHelper {
47  template <class P, class... Args>
48  inline constexpr T operator()(P &&param, Args &&... args) const
49  noexcept(noexcept(ConstructionHelper<T, N - 1>{}(
50  std::forward<P>(param), subscript(param, N - 1),
51  std::forward<Args>(args)...))) {
52  return ConstructionHelper<T, N - 1>{}(std::forward<P>(param),
53  subscript(param, N - 1),
54  std::forward<Args>(args)...);
55  }
56 };
57 
58 template <class T> struct ConstructionHelper<T, 0> {
59  template <class P, class... Args>
60  inline constexpr T operator()(P &&, Args &&... args) const
61  noexcept(noexcept(T{std::forward<Args>(args)...})) {
62  return T{std::forward<Args>(args)...};
63  }
64 };
65 
66 template <class T, std::size_t N>
67 struct ConstructionHelper<std::array<T, N>, 0> {
68  template <class P, class... Args>
69  inline constexpr std::array<T, N> operator()(P &&, Args &&... args) const
70  noexcept(noexcept(std::array<T, N>{{std::forward<Args>(args)...}})) {
71  return std::array<T, N>{{std::forward<Args>(args)...}};
72  }
73 };
74 
75 } // namespace Detail_
76 
78 template <class T, std::size_t N>
79 inline Eigen::Matrix<T, static_cast<Eigen::Index>(N), 1>
80 vec(std::array<T, N> const &arr) {
81  using Result = Eigen::Matrix<T, static_cast<Eigen::Index>(N), 1>;
83 }
84 
86 template <class Derived>
87 inline std::array<typename Derived::Scalar,
88  static_cast<std::size_t>(Derived::RowsAtCompileTime)>
89 arr(Eigen::MatrixBase<Derived> const &vec) noexcept {
90  static_assert(Derived::RowsAtCompileTime != Eigen::Dynamic &&
91  Derived::ColsAtCompileTime == 1,
92  "Conversion only available for fix sized vectors");
93 
94  using T = typename Derived::Scalar;
95  auto constexpr N = static_cast<std::size_t>(Derived::RowsAtCompileTime);
96  using Result = std::array<T, N>;
98 }
99 
100 template <class T>
101 inline Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> const>
102 map(std::vector<T> const &v) {
103  return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> const>{
104  v.data(), gsl::narrow_cast<Eigen::Index>(v.size())};
105 }
106 
107 template <class T>
108 inline Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>> map(std::vector<T> &v) {
109  return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1>>{
110  v.data(), gsl::narrow_cast<Eigen::Index>(v.size())};
111 }
112 
113 template <std::size_t N, class T>
114 inline Eigen::Map<
115  Eigen::Matrix<T, static_cast<Eigen::Index>(N), Eigen::Dynamic> const>
116 map(std::vector<std::array<T, N>> const &m) {
117  return Eigen::Map<
118  Eigen::Matrix<T, static_cast<Eigen::Index>(N), Eigen::Dynamic> const>{
119  reinterpret_cast<T const *>(m.data()), gsl::narrow_cast<Eigen::Index>(N),
120  gsl::narrow_cast<Eigen::Index>(m.size())};
121 }
122 
123 template <std::size_t N, class T>
124 inline Eigen::Map<
125  Eigen::Matrix<T, static_cast<Eigen::Index>(N), Eigen::Dynamic>>
126 map(std::vector<std::array<T, N>> &m) {
127  return Eigen::Map<
128  Eigen::Matrix<T, static_cast<Eigen::Index>(N), Eigen::Dynamic>>{
129  reinterpret_cast<T *>(m.data()), gsl::narrow_cast<Eigen::Index>(N),
130  gsl::narrow_cast<Eigen::Index>(m.size())};
131 }
132 
133 } // namespace Adaptor
134 } // namespace Internal
135 } // namespace CortidQCT
Name namespace for CortidQCT library.
Definition: CortidQCT.h:23
Definition: ColorToLabelMap.h:27
Eigen::Matrix< T, static_cast< Eigen::Index >N), 1 > vec(std::array< T, N > const &arr)
Converts an std::array based vector into an Eigen vector.
Definition: EigenAdaptors.h:80