farm-ng-core
eigen.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
18 
19 #include <Eigen/Core>
20 
21 // TODO: Move to Sophus.
22 namespace farm_ng {
23 namespace details {
24 
25 template <class Scalar, int M, int N>
26 struct CheckNear<Eigen::Matrix<Scalar, M, N>> {
27  static void impl(
28  Eigen::Matrix<Scalar, M, N> const& lhs,
29  Eigen::Matrix<Scalar, M, N> const& rhs,
30  Scalar const& thr,
31  char const* lhs_cstr,
32  char const* rhs_cstr,
33  char const* thr_cstr,
34  std::string const& file,
35  int line,
36  std::string const& func,
37  std::string const& str) {
38  using std::abs;
39  using std::min;
40 
41  Scalar max_error = 0.0;
42  int max_error_dim = -1;
43  bool absolute = true;
44  for (int i = 0; i < lhs.reshaped().rows(); ++i) {
45  auto lhs_abs = abs(lhs.reshaped()[i]);
46  auto rhs_abs = abs(rhs.reshaped()[i]);
47 
48  if (min(lhs_abs, rhs_abs) < thr) {
49  /* close to zero, we are doing an absolute comparison*/
50  Scalar err = abs(lhs.reshaped()[i] - rhs.reshaped()[i]);
51  if (err > max_error) {
52  max_error = err;
53  max_error_dim = i;
54  absolute = true;
55  }
56  } else {
57  Scalar err = abs((lhs.reshaped()[i] / rhs.reshaped()[i]) - 1.0);
58  if (err > max_error) {
59  max_error = err;
60  max_error_dim = i;
61  absolute = false;
62  }
63  }
64  }
65  if (max_error < thr) {
66  // all errors below threshold
67  return;
68  }
70  farm_ng::LogLevel::critical,
72  "ASSERT_NEAR({})]"
73  "Not true: {} near {}; has error of {} in {} (thr: "
74  "{})\n{}\n{}\n{}\nvs.\n{}\n{}\n{}",
75  absolute ? "absolute" : "relative",
76  lhs_cstr,
77  rhs_cstr,
78  max_error,
79  max_error_dim,
80  thr,
81  lhs.reshaped()[max_error_dim],
82  lhs_cstr,
83  lhs,
84  rhs.reshaped()[max_error_dim],
85  rhs_cstr,
86  rhs),
87  file,
88  line,
89  func,
90  str);
92  }
93 };
94 
95 } // namespace details
96 } // namespace farm_ng
FARM_FORMAT
#define FARM_FORMAT(...)
Formats the cstr using the libfmt library.
Definition: format.h:125
farm_ng
Definition: backtrace.cpp:102
Eigen
Definition: params.h:72
logger.h
core.event_service.str
str
Definition: event_service.py:547
sophus::min
auto min(TPoint const &a, TPoint const &b) -> TPoint
Definition: vector_space.h:104
farm_ng::StreamLogger::log
void log(LogLevel log_level, std::string const &header_text, std::string const &file, int line, std::string const &function, std::string const &message, TT &&... args)
Definition: logger.h:61
farm_ng::details::CheckNear
Definition: logger.h:311
farm_ng::defaultLogger
StreamLogger & defaultLogger()
Definition: logger.h:124
FARM_IMPL_ABORT
#define FARM_IMPL_ABORT()
Definition: format.h:62
farm_ng::details::CheckNear< Eigen::Matrix< Scalar, M, N > >::impl
static void impl(Eigen::Matrix< Scalar, M, N > const &lhs, Eigen::Matrix< Scalar, M, N > const &rhs, Scalar const &thr, char const *lhs_cstr, char const *rhs_cstr, char const *thr_cstr, std::string const &file, int line, std::string const &func, std::string const &str)
Definition: eigen.h:27
core.event_service_recorder.func
func
Definition: event_service_recorder.py:420