common.hpp
Go to the documentation of this file.
1 
4 #ifndef SOPHUS_COMMON_HPP
5 #define SOPHUS_COMMON_HPP
6 
7 #include <cmath>
8 #include <cstdio>
9 #include <cstdlib>
10 #include <iostream>
11 #include <random>
12 #include <type_traits>
13 
14 #include <Eigen/Core>
15 
16 // following boost's assert.hpp
17 #undef SOPHUS_ENSURE
18 
19 // ENSURES are similar to ASSERTS, but they are always checked for (including in
20 // release builds). At the moment there are no ASSERTS in Sophus which should
21 // only be used for checks which are performance critical.
22 
23 #ifdef __GNUC__
24 #define SOPHUS_FUNCTION __PRETTY_FUNCTION__
25 #elif (_MSC_VER >= 1310)
26 #define SOPHUS_FUNCTION __FUNCTION__
27 #else
28 #define SOPHUS_FUNCTION "unknown"
29 #endif
30 
31 // Make sure this compiles with older versions of Eigen which do not have
32 // EIGEN_DEVICE_FUNC defined.
33 #ifndef EIGEN_DEVICE_FUNC
34 #define EIGEN_DEVICE_FUNC
35 #endif
36 
37 #define SOPHUS_FUNC EIGEN_DEVICE_FUNC
38 
39 namespace Sophus {
40 namespace details {
41 
42 // Following: http://stackoverflow.com/a/22759544
43 template <class T>
44 class IsStreamable {
45  private:
46  template <class TT>
47  static auto test(int)
48  -> decltype(std::declval<std::stringstream&>() << std::declval<TT>(),
49  std::true_type());
50 
51  template <class>
52  static auto test(...) -> std::false_type;
53 
54  public:
55  static bool const value = decltype(test<T>(0))::value;
56 };
57 
58 template <class T>
59 class ArgToStream {
60  public:
61  static void impl(std::stringstream& stream, T&& arg) {
62  stream << std::forward<T>(arg);
63  }
64 };
65 
66 inline void FormatStream(std::stringstream& stream, char const* text) {
67  stream << text;
68  return;
69 }
70 
71 // Following: http://en.cppreference.com/w/cpp/language/parameter_pack
72 template <class T, typename... Args>
73 void FormatStream(std::stringstream& stream, char const* text, T&& arg,
74  Args&&... args) {
75  static_assert(IsStreamable<T>::value,
76  "One of the args has no ostream overload!");
77  for (; *text != '\0'; ++text) {
78  if (*text == '%') {
79  ArgToStream<T&&>::impl(stream, std::forward<T>(arg));
80  FormatStream(stream, text + 1, std::forward<Args>(args)...);
81  return;
82  }
83  stream << *text;
84  }
85  stream << "\nFormat-Warning: There are " << sizeof...(Args) + 1
86  << " args unused.";
87  return;
88 }
89 
90 template <class... Args>
91 std::string FormatString(char const* text, Args&&... args) {
92  std::stringstream stream;
93  FormatStream(stream, text, std::forward<Args>(args)...);
94  return stream.str();
95 }
96 
97 inline std::string FormatString() { return std::string(); }
98 } // namespace details
99 } // namespace Sophus
100 
101 #if defined(SOPHUS_DISABLE_ENSURES)
102 
103 #define SOPHUS_ENSURE(expr, ...) ((void)0)
104 
105 #elif defined(SOPHUS_ENABLE_ENSURE_HANDLER)
106 
107 namespace Sophus {
108 void ensureFailed(char const* function, char const* file, int line,
109  char const* description);
110 }
111 
112 #define SOPHUS_ENSURE(expr, ...) \
113  ((expr) ? ((void)0) \
114  : ::Sophus::ensureFailed( \
115  SOPHUS_FUNCTION, __FILE__, __LINE__, \
116  Sophus::details::FormatString(__VA_ARGS__).c_str()))
117 #else
118 // LCOV_EXCL_START
119 
120 namespace Sophus {
121 template <class... Args>
122 SOPHUS_FUNC void defaultEnsure(char const* function, char const* file, int line,
123  char const* description, Args&&... args) {
124  std::printf("Sophus ensure failed in function '%s', file '%s', line %d.\n",
125  function, file, line);
126 #ifdef __CUDACC__
127  std::printf("%s", description);
128 #else
129  std::cout << details::FormatString(description, std::forward<Args>(args)...)
130  << std::endl;
131  std::abort();
132 #endif
133 }
134 } // namespace Sophus
135 
136 // LCOV_EXCL_STOP
137 #define SOPHUS_ENSURE(expr, ...) \
138  ((expr) ? ((void)0) \
139  : Sophus::defaultEnsure(SOPHUS_FUNCTION, __FILE__, __LINE__, \
140  ##__VA_ARGS__))
141 #endif
142 
143 namespace Sophus {
144 
145 template <class Scalar>
146 struct Constants {
147  SOPHUS_FUNC static Scalar epsilon() { return Scalar(1e-10); }
148 
149  SOPHUS_FUNC static Scalar epsilonSqrt() {
150  using std::sqrt;
151  return sqrt(epsilon());
152  }
153 
154  SOPHUS_FUNC static Scalar pi() {
155  return Scalar(3.141592653589793238462643383279502884);
156  }
157 };
158 
159 template <>
160 struct Constants<float> {
161  SOPHUS_FUNC static float constexpr epsilon() {
162  return static_cast<float>(1e-5);
163  }
164 
165  SOPHUS_FUNC static float epsilonSqrt() { return std::sqrt(epsilon()); }
166 
167  SOPHUS_FUNC static float constexpr pi() {
168  return 3.141592653589793238462643383279502884f;
169  }
170 };
171 
173 struct nullopt_t {
174  explicit constexpr nullopt_t() {}
175 };
176 
177 constexpr nullopt_t nullopt{};
178 
184 template <class T>
185 class optional {
186  public:
187  optional() : is_valid_(false) {}
188 
190 
191  optional(T const& type) : type_(type), is_valid_(true) {}
192 
193  explicit operator bool() const { return is_valid_; }
194 
195  T const* operator->() const {
196  SOPHUS_ENSURE(is_valid_, "must be valid");
197  return &type_;
198  }
199 
200  T* operator->() {
201  SOPHUS_ENSURE(is_valid_, "must be valid");
202  return &type_;
203  }
204 
205  T const& operator*() const {
206  SOPHUS_ENSURE(is_valid_, "must be valid");
207  return type_;
208  }
209 
210  T& operator*() {
211  SOPHUS_ENSURE(is_valid_, "must be valid");
212  return type_;
213  }
214 
215  private:
216  T type_;
217  bool is_valid_;
218 };
219 
220 template <bool B, class T = void>
221 using enable_if_t = typename std::enable_if<B, T>::type;
222 
223 template <class G>
225  static const bool value = std::is_unsigned<typename G::result_type>::value &&
226  std::is_unsigned<decltype(G::min())>::value &&
227  std::is_unsigned<decltype(G::max())>::value;
228 };
229 } // namespace Sophus
230 
231 #endif // SOPHUS_COMMON_HPP
Sophus::optional::operator*
T const & operator*() const
Definition: common.hpp:205
Sophus::optional::operator*
T & operator*()
Definition: common.hpp:210
Sophus::details::IsStreamable
Definition: common.hpp:44
Sophus::defaultEnsure
SOPHUS_FUNC void defaultEnsure(char const *function, char const *file, int line, char const *description, Args &&... args)
Definition: common.hpp:122
Sophus::Constants::epsilonSqrt
static SOPHUS_FUNC Scalar epsilonSqrt()
Definition: common.hpp:149
Sophus::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: common.hpp:221
SOPHUS_ENSURE
#define SOPHUS_ENSURE(expr,...)
Definition: common.hpp:137
Sophus::details::IsStreamable::value
static const bool value
Definition: common.hpp:55
Sophus::Constants< float >::pi
static SOPHUS_FUNC constexpr float pi()
Definition: common.hpp:167
Sophus::optional::optional
optional(T const &type)
Definition: common.hpp:191
Sophus::details::IsStreamable::test
static auto test(int) -> decltype(std::declval< std::stringstream & >()<< std::declval< TT >(), std::true_type())
Sophus::details::ArgToStream::impl
static void impl(std::stringstream &stream, T &&arg)
Definition: common.hpp:61
Sophus::IsUniformRandomBitGenerator::value
static const bool value
Definition: common.hpp:225
Sophus::optional::optional
optional()
Definition: common.hpp:187
Sophus::Constants::epsilon
static SOPHUS_FUNC Scalar epsilon()
Definition: common.hpp:147
Sophus::optional::is_valid_
bool is_valid_
Definition: common.hpp:217
Sophus
Definition: average.hpp:17
Sophus::nullopt_t::nullopt_t
constexpr nullopt_t()
Definition: common.hpp:174
SOPHUS_FUNC
#define SOPHUS_FUNC
Definition: common.hpp:37
Sophus::optional::optional
optional(nullopt_t)
Definition: common.hpp:189
Sophus::optional
Definition: common.hpp:185
Sophus::optional::type_
T type_
Definition: common.hpp:216
Sophus::optional::operator->
T const * operator->() const
Definition: common.hpp:195
Sophus::Constants< float >::epsilon
static SOPHUS_FUNC constexpr float epsilon()
Definition: common.hpp:161
Sophus::nullopt
constexpr nullopt_t nullopt
Definition: common.hpp:177
Sophus::ensureFailed
void ensureFailed(char const *function, char const *file, int line, char const *description)
Definition: example_ensure_handler.cpp:7
Sophus::Constants::pi
static SOPHUS_FUNC Scalar pi()
Definition: common.hpp:154
Sophus::Constants
Definition: common.hpp:146
Sophus::optional::operator->
T * operator->()
Definition: common.hpp:200
Sophus::details::ArgToStream
Definition: common.hpp:59
Sophus::IsUniformRandomBitGenerator
Definition: common.hpp:224
Sophus::details::FormatStream
void FormatStream(std::stringstream &stream, char const *text)
Definition: common.hpp:66
Sophus::Constants< float >::epsilonSqrt
static SOPHUS_FUNC float epsilonSqrt()
Definition: common.hpp:165
Sophus::nullopt_t
Nullopt type of lightweight optional class.
Definition: common.hpp:173
Sophus::details::FormatString
std::string FormatString(char const *text, Args &&... args)
Definition: common.hpp:91


sophus
Author(s): Hauke Strasdat
autogenerated on Wed Mar 2 2022 01:01:47