farm-ng-core
cast.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2011, Hauke Strasdat
3 // Copyright (c) 2012, Steven Lovegrove
4 // Copyright (c) 2021, farm-ng, inc.
5 //
6 // Use of this source code is governed by an MIT-style
7 // license that can be found in the LICENSE file or at
8 // https://opensource.org/licenses/MIT.
9 
10 #pragma once
11 
12 #include "sophus/concepts/point.h"
13 
14 #include <Eigen/Core>
15 
16 #include <algorithm>
17 #include <utility>
18 #include <vector>
19 
20 namespace sophus {
21 
22 namespace details {
23 
24 template <class TScalar>
25 class Cast {
26  public:
27  template <class TTo>
28  static auto impl(TScalar const& s) -> TTo {
29  return static_cast<TTo>(s);
30  }
31  template <class TTo>
32  static auto implScalar(TScalar const& s) -> TTo {
33  return static_cast<TTo>(s);
34  }
35 };
36 
37 template <::sophus::concepts::EigenType TT>
38 class Cast<TT> {
39  public:
40  template <class TTo>
41  static auto impl(TT const& v) {
42  return v.template cast<typename TTo::Scalar>().eval();
43  }
44  template <class TTo>
45  static auto implScalar(TT const& v) {
46  return v.template cast<TTo>().eval();
47  }
48 };
49 
50 template <class TT>
51 class Cast<std::vector<TT>> {
52  public:
53  template <class TTo>
54  static auto impl(std::vector<TT> const& v) {
55  using ToEl = std::decay_t<decltype(*std::declval<TTo>().data())>;
56  std::vector<ToEl> r;
57  r.reserve(v.size());
58  for (auto const& el : v) {
59  r.push_back(Cast<TT>::template impl<ToEl>(el));
60  }
61  return r;
62  }
63  template <class TTo>
64  static auto implScalar(std::vector<TT> const& v) {
65  using ToEl = decltype(Cast<TT>::template implScalar<TTo>(v[0]));
66  std::vector<ToEl> r;
67  r.reserve(v.size());
68  for (auto const& el : v) {
69  r.push_back(Cast<TT>::template impl<ToEl>(el));
70  }
71  return r;
72  }
73 };
74 
75 } // namespace details
76 
77 template <class TTo, class TT>
78 auto cast(const TT& p) {
79  return details::Cast<TT>::template impl<TTo>(p);
80 }
81 
82 template <::sophus::concepts::Arithmetic TTo, class TT>
83 auto cast(const TT& p) {
84  return details::Cast<TT>::template implScalar<TTo>(p);
85 }
86 
87 } // namespace sophus
point.h
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::cast
auto cast(const TT &p)
Definition: cast.h:78