farm-ng-core
vector_space.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/common/common.h"
13 #include "sophus/linalg/cast.h"
14 #include "sophus/linalg/reduce.h"
15 
16 #include <Eigen/Core>
17 
18 #include <algorithm>
19 #include <utility>
20 #include <vector>
21 
22 namespace sophus {
23 
24 namespace details {
25 
26 // EigenDenseType may be a map or view or abstract base class or something.
27 // This is an alias for the corresponding concrete type with storage
28 template <::sophus::concepts::EigenDenseType TPoint>
29 using EigenConcreteType = std::decay_t<decltype(std::declval<TPoint>().eval())>;
30 
31 } // namespace details
32 
33 template <::sophus::concepts::ScalarType TPoint>
34 [[nodiscard]] auto zero() -> TPoint {
35  return 0;
36 }
37 
38 template <::sophus::concepts::EigenDenseType TPoint>
39 [[nodiscard]] auto zero() -> TPoint {
40  return TPoint::Zero();
41 }
42 
43 template <::sophus::concepts::ScalarType TPoint>
44 auto eval(TPoint const& p) {
45  return p;
46 }
47 
48 template <::sophus::concepts::EigenDenseType TPoint>
49 auto eval(TPoint const& p) {
50  return p.eval();
51 }
52 
53 template <::sophus::concepts::ScalarType TPoint>
54 auto allTrue(TPoint const& p) -> bool {
55  return bool(p);
56 }
57 
58 template <::sophus::concepts::EigenDenseType TPoint>
59 auto allTrue(TPoint const& p) -> bool {
60  return p.all();
61 }
62 
63 template <::sophus::concepts::ScalarType TPoint>
64 auto anyTrue(TPoint const& p) -> bool {
65  return bool(p);
66 }
67 
68 template <::sophus::concepts::EigenDenseType TPoint>
69 auto anyTrue(TPoint const& p) -> bool {
70  return p.any();
71 }
72 
73 template <::sophus::concepts::ScalarType TPoint>
74 auto isFinite(TPoint const& p) -> bool {
75  return std::isfinite(p);
76 }
77 
78 template <::sophus::concepts::EigenDenseType TPoint>
79 auto isFinite(TPoint const& p) -> bool {
80  return p.array().isFinite().all();
81 }
82 
83 template <::sophus::concepts::ScalarType TPoint>
84 auto isNan(TPoint const& p) -> bool {
85  return std::isnan(p);
86 }
87 
88 template <::sophus::concepts::PointType TPoint>
89 auto isNan(TPoint const& p) -> bool {
90  return p.array().isNaN().all();
91 }
92 
93 template <::sophus::concepts::ScalarType TPoint>
94 auto square(TPoint const& v) {
95  return v * v;
96 }
97 
98 template <::sophus::concepts::EigenDenseType TPoint>
99 auto square(TPoint const& v) {
100  return v.squaredNorm();
101 }
102 
103 template <::sophus::concepts::ScalarType TPoint>
104 auto min(TPoint const& a, TPoint const& b) -> TPoint {
105  return std::min(a, b);
106 }
107 
108 template <::sophus::concepts::EigenDenseType TPoint>
109 auto min(TPoint const& a, TPoint const& b) -> TPoint {
110  return a.cwiseMin(b);
111 }
112 
113 template <::sophus::concepts::ScalarType TPoint>
114 auto max(TPoint const& a, TPoint const& b) -> TPoint {
115  return std::max(a, b);
116 }
117 
118 template <::sophus::concepts::EigenDenseType TPoint>
119 auto max(TPoint const& a, TPoint const& b) -> TPoint {
120  return a.cwiseMax(b);
121 }
122 
123 template <::sophus::concepts::PointType TPoint>
124 auto clamp(TPoint const& val, TPoint const& lo, TPoint const& hi) -> TPoint {
125  return sophus::max(lo, sophus::min(val, hi));
126 }
127 
128 template <::sophus::concepts::ScalarType TPoint>
129 auto floor(TPoint s) {
130  using std::floor;
131  return floor(s);
132 }
133 
134 template <::sophus::concepts::EigenDenseType TPoint>
135 auto floor(TPoint p) {
136  for (auto& e : p.reshaped()) {
137  e = sophus::floor(e);
138  }
139  return p;
140 }
141 
142 template <::sophus::concepts::ScalarType TPoint>
143 auto ceil(TPoint s) {
144  using std::ceil;
145  return ceil(s);
146 }
147 
148 template <::sophus::concepts::EigenDenseType TPoint>
149 auto ceil(TPoint p) {
150  for (auto& e : p.reshaped()) {
151  e = sophus::ceil(e);
152  }
153  return p;
154 }
155 
156 template <::sophus::concepts::ScalarType TPoint>
157 auto round(TPoint s) {
158  using std::ceil;
159  return ceil(s);
160 }
161 
162 template <::sophus::concepts::EigenDenseType TPoint>
163 auto round(TPoint p) {
164  for (auto& e : p.reshaped()) {
165  e = sophus::round(e);
166  }
167  return p;
168 }
169 
170 template <::sophus::concepts::ScalarType TPoint>
171 [[nodiscard]] auto plus(TPoint p, TPoint s) {
172  p += s;
173  return p;
174 }
175 
176 template <::sophus::concepts::EigenDenseType TPoint>
177 [[nodiscard]] auto plus(TPoint p, typename TPoint::Scalar s) {
178  p.array() += s;
179  return p;
180 }
181 
182 template <::sophus::concepts::ScalarType TPoint>
183 [[nodiscard]] auto isLessEqual(TPoint const& lhs, TPoint const& rhs) -> bool {
184  return lhs <= rhs;
185 }
186 
187 template <::sophus::concepts::EigenDenseType TPoint>
188 [[nodiscard]] auto isLessEqual(TPoint const& lhs, TPoint const& rhs) -> bool {
189  return allTrue(eval(lhs.array() <= rhs.array()));
190 }
191 
192 template <::sophus::concepts::ScalarType TPoint>
193 [[nodiscard]] auto tryGetElem(TPoint const& p, size_t row, size_t col = 0)
194  -> Expected<TPoint> {
195  if (row == 0 && col == 0) {
196  return p;
197  }
198  return SOPHUS_UNEXPECTED("row ({}) and col ({}) must be 0", row, col);
199 }
200 
201 template <::sophus::concepts::EigenDenseType TPoint>
202 [[nodiscard]] auto tryGetElem(TPoint const& p, size_t row, size_t col = 0)
203  -> Expected<TPoint> {
204  if (row < p.rows() && col < p.cols()) {
205  return p(row, col);
206  }
207  return SOPHUS_UNEXPECTED(
208  "({}, {}) access of array of size {} x {}", row, col, p.rows(), p.cols());
209 }
210 
211 template <::sophus::concepts::ScalarType TPoint>
212 [[nodiscard]] auto trySetElem(TPoint& p, TPoint s, size_t row, size_t col = 0)
213  -> Expected<Success> {
214  if (row == 0 && col == 0) {
215  p = s;
216  return Success{};
217  }
218  return SOPHUS_UNEXPECTED("row ({}) and col ({}) must be 0", row, col);
219 }
220 
221 template <::sophus::concepts::EigenDenseType TPoint>
222 [[nodiscard]] auto trySetElem(
223  TPoint& p, typename TPoint::Scalar s, size_t row, size_t col = 0)
224  -> Expected<Success> {
225  if (row == 0 && col == 0) {
226  p(row, col) = s;
227  return Success{};
228  }
229  return SOPHUS_UNEXPECTED("row ({}) and col ({}) must be 0", row, col);
230 }
231 
232 template <::sophus::concepts::ScalarType TPoint>
233 [[nodiscard]] auto transpose(TPoint p) {
234  return p;
235 }
236 
237 template <::sophus::concepts::EigenDenseType TPoint>
238 [[nodiscard]] auto transpose(TPoint p) {
239  return p.transpose();
240 }
241 
242 namespace details {
243 
244 template <class TScalar, int kDim>
245 struct PointExamples;
246 
247 template <class TScalar>
248 struct PointExamples<TScalar, 1> {
249  static auto impl() {
250  std::vector<Eigen::Vector<TScalar, 1>> point_vec;
251  point_vec.push_back(Eigen::Vector<TScalar, 1>(TScalar(1)));
252  point_vec.push_back(Eigen::Vector<TScalar, 1>(TScalar(-3)));
253  point_vec.push_back(Eigen::Vector<TScalar, 1>::Zero());
254  return point_vec;
255  }
256 };
257 
258 template <class TScalar>
259 struct PointExamples<TScalar, 2> {
260  static auto impl() {
261  std::vector<Eigen::Vector<TScalar, 2>> point_vec;
262  point_vec.push_back(Eigen::Vector<TScalar, 2>(TScalar(1), TScalar(2)));
263  point_vec.push_back(Eigen::Vector<TScalar, 2>(TScalar(1), TScalar(-3)));
264  point_vec.push_back(Eigen::Vector<TScalar, 2>::Zero());
265  point_vec.push_back(Eigen::Vector<TScalar, 2>::Ones());
266  point_vec.push_back(Eigen::Vector<TScalar, 2>::UnitX());
267  point_vec.push_back(Eigen::Vector<TScalar, 2>::UnitY());
268  return point_vec;
269  }
270 };
271 
272 template <class TScalar>
273 struct PointExamples<TScalar, 3> {
274  static auto impl() {
275  std::vector<Eigen::Vector<TScalar, 3>> point_vec;
276  point_vec.push_back(
277  Eigen::Vector<TScalar, 3>(TScalar(1), TScalar(2), TScalar(0.1)));
278  point_vec.push_back(
279  Eigen::Vector<TScalar, 3>(TScalar(1), TScalar(-3), TScalar(-1)));
280  point_vec.push_back(Eigen::Vector<TScalar, 3>::Zero());
281  point_vec.push_back(Eigen::Vector<TScalar, 3>::Ones());
282  point_vec.push_back(Eigen::Vector<TScalar, 3>::UnitX());
283  point_vec.push_back(Eigen::Vector<TScalar, 3>::UnitZ());
284  return point_vec;
285  }
286 };
287 
288 template <class TScalar>
289 struct PointExamples<TScalar, 4> {
290  static auto impl() {
291  std::vector<Eigen::Vector<TScalar, 4>> point_vec;
292  point_vec.push_back(Eigen::Vector<TScalar, 4>(
293  TScalar(1), TScalar(2), TScalar(0.1), TScalar(0.1)));
294  point_vec.push_back(Eigen::Vector<TScalar, 4>(
295  TScalar(1), TScalar(-3), TScalar(-1), TScalar(0.1)));
296  point_vec.push_back(Eigen::Vector<TScalar, 4>::Zero());
297  point_vec.push_back(Eigen::Vector<TScalar, 4>::Ones());
298  point_vec.push_back(Eigen::Vector<TScalar, 4>::UnitX());
299  point_vec.push_back(Eigen::Vector<TScalar, 4>::UnitZ());
300  return point_vec;
301  }
302 };
303 
304 } // namespace details
305 
306 template <class TScalar, int kDim>
307 [[nodiscard]] auto pointExamples() {
308  return details::PointExamples<TScalar, kDim>::impl();
309 }
310 
311 } // namespace sophus
sophus::round
auto round(TPoint s)
Definition: vector_space.h:157
cast.h
sophus::clamp
auto clamp(TPoint const &val, TPoint const &lo, TPoint const &hi) -> TPoint
Definition: vector_space.h:124
reduce.h
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::min
auto min(TPoint const &a, TPoint const &b) -> TPoint
Definition: vector_space.h:104
sophus::allTrue
auto allTrue(TPoint const &p) -> bool
Definition: vector_space.h:54
sophus::pointExamples
auto pointExamples()
Definition: vector_space.h:307
sophus::isNan
auto isNan(TPoint const &p) -> bool
Definition: vector_space.h:84
sophus::anyTrue
auto anyTrue(TPoint const &p) -> bool
Definition: vector_space.h:64
sophus::isLessEqual
auto isLessEqual(TPoint const &lhs, TPoint const &rhs) -> bool
Definition: vector_space.h:183
sophus::ceil
auto ceil(TPoint s)
Definition: vector_space.h:143
sophus::zero
auto zero() -> TPoint
Definition: vector_space.h:34
SOPHUS_UNEXPECTED
#define SOPHUS_UNEXPECTED(...)
Definition: common.h:57
sophus::transpose
auto transpose(TPoint p)
Definition: vector_space.h:233
sophus::plus
auto plus(TPoint p, TPoint s)
Definition: vector_space.h:171
sophus::trySetElem
auto trySetElem(TPoint &p, TPoint s, size_t row, size_t col=0) -> Expected< Success >
Definition: vector_space.h:212
sophus::isFinite
auto isFinite(TPoint const &p) -> bool
Definition: vector_space.h:74
sophus::square
auto square(TPoint const &v)
Definition: vector_space.h:94
common.h
sophus::floor
auto floor(TPoint s)
Definition: vector_space.h:129
sophus::tryGetElem
auto tryGetElem(TPoint const &p, size_t row, size_t col=0) -> Expected< TPoint >
Definition: vector_space.h:193
sophus::max
auto max(TPoint const &a, TPoint const &b) -> TPoint
Definition: vector_space.h:114
sophus::eval
auto eval(TPoint const &p)
Definition: vector_space.h:44