ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Eigen.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #ifndef CLOUDVIEWER_EIGEN_H
9 #define CLOUDVIEWER_EIGEN_H
10 
11 #include "CVCoreLib.h"
12 
13 #ifdef _MSVC_LANG
14 #define CPP_VERSION _MSVC_LANG
15 #else
16 #define CPP_VERSION __cplusplus
17 #endif
18 
19 // EIGEN
20 // Ensure EIGEN_HAS_CXX17_OVERALIGN is set before including Eigen headers
21 // This allows C++17 std::allocator's automatic alignment handling even when
22 // EIGEN_MAX_ALIGN_BYTES=0 (for PCL compatibility)
23 #ifndef EIGEN_HAS_CXX17_OVERALIGN
24 #ifdef CPP_VERSION
25 #if CPP_VERSION >= 201703L
26 #define EIGEN_HAS_CXX17_OVERALIGN 1
27 #endif
28 #endif
29 #endif
30 
31 #include <Eigen/Core>
32 #include <Eigen/Geometry>
33 #include <Eigen/StdVector>
34 
35 // SYSTEM
36 #include <memory>
37 #include <tuple>
38 #include <unordered_map>
39 #include <utility>
40 #include <vector>
41 
42 #if !EIGEN_VERSION_AT_LEAST(3, 4, 0) || CPP_VERSION < 201703L
43 
44 #include <initializer_list>
45 #ifndef EIGEN_ALIGNED_ALLOCATOR
46 #define EIGEN_ALIGNED_ALLOCATOR Eigen::aligned_allocator
47 #endif
48 
49 // Equivalent to EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION but with support for
50 // initializer lists, which is a C++11 feature and not supported by the Eigen.
51 // The initializer list extension is inspired by Theia and StackOverflow code.
52 #define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION_CUSTOM(...) \
53  namespace std { \
54  template <> \
55  class vector<__VA_ARGS__, std::allocator<__VA_ARGS__>> \
56  : public vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__>> { \
57  typedef vector<__VA_ARGS__, EIGEN_ALIGNED_ALLOCATOR<__VA_ARGS__>> \
58  vector_base; \
59  \
60  public: \
61  typedef __VA_ARGS__ value_type; \
62  typedef vector_base::allocator_type allocator_type; \
63  typedef vector_base::size_type size_type; \
64  typedef vector_base::iterator iterator; \
65  explicit vector(const allocator_type& a = allocator_type()) \
66  : vector_base(a) {} \
67  template <typename InputIterator> \
68  vector(InputIterator first, \
69  InputIterator last, \
70  const allocator_type& a = allocator_type()) \
71  : vector_base(first, last, a) {} \
72  vector(const vector& c) : vector_base(c) {} \
73  explicit vector(size_type num, const value_type& val = value_type()) \
74  : vector_base(num, val) {} \
75  vector(iterator start, iterator end) : vector_base(start, end) {} \
76  vector& operator=(const vector& x) { \
77  vector_base::operator=(x); \
78  return *this; \
79  } \
80  vector(initializer_list<__VA_ARGS__> list) \
81  : vector_base(list.begin(), list.end()) {} \
82  }; \
83  } // namespace std
84 
98 
99 #endif
100 
101 #undef CPP_VERSION
102 
103 namespace Eigen {
104 
106 typedef Eigen::Matrix<double, 6, 6> Matrix6d;
107 typedef Eigen::Matrix<double, 6, 1> Vector6d;
108 typedef Eigen::Matrix<uint8_t, 3, 1> Vector3uint8;
109 
112 typedef Eigen::Matrix<double, 6, 6, Eigen::DontAlign> Matrix6d_u;
113 typedef Eigen::Matrix<double, 4, 4, Eigen::DontAlign> Matrix4d_u;
114 typedef Eigen::Matrix<double, 3, 1, Eigen::DontAlign> Vector3d_u;
115 typedef Eigen::Matrix<float, 3, 1, Eigen::DontAlign> Vector3f_u;
116 typedef Eigen::Matrix<double, 4, 1, Eigen::DontAlign> Vector4d_u;
117 typedef Eigen::Matrix<float, 4, 1, Eigen::DontAlign> Vector4f_u;
118 
119 } // namespace Eigen
120 
121 namespace cloudViewer {
122 
123 namespace utility {
124 
125 using Matrix4d_allocator = Eigen::aligned_allocator<Eigen::Matrix4d>;
126 using Matrix6d_allocator = Eigen::aligned_allocator<Eigen::Matrix6d>;
127 using Vector2d_allocator = Eigen::aligned_allocator<Eigen::Vector2d>;
128 using Vector3uint8_allocator = Eigen::aligned_allocator<Eigen::Vector3uint8>;
129 using Vector4i_allocator = Eigen::aligned_allocator<Eigen::Vector4i>;
130 using Vector4d_allocator = Eigen::aligned_allocator<Eigen::Vector4d>;
131 using Vector6d_allocator = Eigen::aligned_allocator<Eigen::Vector6d>;
132 
134 Eigen::Matrix3d CV_CORE_LIB_API SkewMatrix(const Eigen::Vector3d& vec);
135 
139 Eigen::Matrix4d CV_CORE_LIB_API
141 
148 TransformMatrix4dToVector6d(const Eigen::Matrix4d& input);
149 
151 std::tuple<bool, Eigen::VectorXd> CV_CORE_LIB_API
152 SolveLinearSystemPSD(const Eigen::MatrixXd& A,
153  const Eigen::VectorXd& b,
154  bool prefer_sparse = false,
155  bool check_symmetric = false,
156  bool check_det = false,
157  bool check_psd = false);
158 
162 std::tuple<bool, Eigen::Matrix4d> CV_CORE_LIB_API
164  const Eigen::Vector6d& JTr);
165 
169 std::tuple<bool, std::vector<Eigen::Matrix4d, Matrix4d_allocator>>
171  const Eigen::MatrixXd& JTJ, const Eigen::VectorXd& JTr);
172 
178 template <typename MatType, typename VecType>
179 std::tuple<MatType, VecType, double> CV_CORE_LIB_API
180 ComputeJTJandJTr(std::function<void(int, VecType&, double&, double&)> f,
181  int iteration_num,
182  bool verbose = true);
183 
189 template <typename MatType, typename VecType>
190 std::tuple<MatType, VecType, double> CV_CORE_LIB_API ComputeJTJandJTr(
191  std::function<
192  void(int,
193  std::vector<VecType, Eigen::aligned_allocator<VecType>>&,
194  std::vector<double>&,
195  std::vector<double>&)> f,
196  int iteration_num,
197  bool verbose = true);
198 
199 Eigen::Matrix3d CV_CORE_LIB_API RotationMatrixX(double radians);
200 Eigen::Matrix3d CV_CORE_LIB_API RotationMatrixY(double radians);
201 Eigen::Matrix3d CV_CORE_LIB_API RotationMatrixZ(double radians);
202 
205 Eigen::Vector3uint8 CV_CORE_LIB_API ColorToUint8(const Eigen::Vector3d& color);
207 Eigen::Vector3d CV_CORE_LIB_API ColorToDouble(uint8_t r, uint8_t g, uint8_t b);
208 Eigen::Vector3d CV_CORE_LIB_API ColorToDouble(const Eigen::Vector3uint8& rgb);
209 
211 template <typename IdxType>
212 Eigen::Matrix3d CV_CORE_LIB_API
213 ComputeCovariance(const std::vector<Eigen::Vector3d>& points,
214  const std::vector<IdxType>& indices);
215 
217 template <typename IdxType>
218 std::tuple<Eigen::Vector3d, Eigen::Matrix3d> CV_CORE_LIB_API
219 ComputeMeanAndCovariance(const std::vector<Eigen::Vector3d>& points,
220  const std::vector<IdxType>& indices);
221 
228 template <typename RealType, typename IdxType>
229 std::tuple<Eigen::Vector3d, Eigen::Matrix3d> CV_CORE_LIB_API
230 ComputeMeanAndCovariance(const RealType* const points,
231  const std::vector<IdxType>& indices);
232 
233 } // namespace utility
234 } // namespace cloudViewer
235 
236 #endif // CLOUDVIEWER_EIGEN_H
#define CV_CORE_LIB_API
Definition: CVCoreLibWin.h:15
#define EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION_CUSTOM(...)
Definition: Eigen.h:52
int points
math::float4 color
Definition: Eigen.h:103
Eigen::Matrix< double, 6, 1 > Vector6d
Definition: Eigen.h:107
Eigen::Matrix< double, 4, 1, Eigen::DontAlign > Vector4d_u
Definition: Eigen.h:116
Eigen::Matrix< double, 6, 6, Eigen::DontAlign > Matrix6d_u
Definition: Eigen.h:112
Eigen::Matrix< double, 4, 4, Eigen::DontAlign > Matrix4d_u
Definition: Eigen.h:113
Eigen::Matrix< float, 4, 1, Eigen::DontAlign > Vector4f_u
Definition: Eigen.h:117
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vector3f_u
Definition: Eigen.h:115
Eigen::Matrix< double, 6, 6 > Matrix6d
Extending Eigen namespace by adding frequently used matrix type.
Definition: Eigen.h:106
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vector3d_u
Definition: Eigen.h:114
Eigen::Matrix< uint8_t, 3, 1 > Vector3uint8
Definition: Eigen.h:108
std::tuple< Eigen::Vector3d, Eigen::Matrix3d > ComputeMeanAndCovariance(const std::vector< Eigen::Vector3d > &points, const std::vector< IdxType > &indices)
Function to compute the mean and covariance matrix of a set of points.
Definition: Eigen.cpp:321
std::tuple< bool, std::vector< Eigen::Matrix4d, Matrix4d_allocator > > SolveJacobianSystemAndObtainExtrinsicMatrixArray(const Eigen::MatrixXd &JTJ, const Eigen::VectorXd &JTr)
Definition: Eigen.cpp:119
Eigen::Matrix3d RotationMatrixX(double radians)
Definition: Eigen.cpp:248
Eigen::aligned_allocator< Eigen::Vector4i > Vector4i_allocator
Definition: Eigen.h:129
Eigen::aligned_allocator< Eigen::Vector6d > Vector6d_allocator
Definition: Eigen.h:131
std::tuple< bool, Eigen::VectorXd > SolveLinearSystemPSD(const Eigen::MatrixXd &A, const Eigen::VectorXd &b, bool prefer_sparse=false, bool check_symmetric=false, bool check_det=false, bool check_psd=false)
Function to solve Ax=b.
Definition: Eigen.cpp:21
Eigen::Matrix3d RotationMatrixY(double radians)
Definition: Eigen.cpp:255
Eigen::Matrix3d ComputeCovariance(const std::vector< Eigen::Vector3d > &points, const std::vector< IdxType > &indices)
Function to compute the covariance matrix of a set of points.
Definition: Eigen.cpp:287
Eigen::Vector6d TransformMatrix4dToVector6d(const Eigen::Matrix4d &input)
Definition: Eigen.cpp:88
Eigen::aligned_allocator< Eigen::Matrix4d > Matrix4d_allocator
Definition: Eigen.h:125
Eigen::Matrix3d SkewMatrix(const Eigen::Vector3d &vec)
Genretate a skew-symmetric matrix from a vector 3x1.
Definition: Eigen.cpp:414
Eigen::Vector3uint8 ColorToUint8(const Eigen::Vector3d &color)
Definition: Eigen.cpp:269
Eigen::aligned_allocator< Eigen::Vector4d > Vector4d_allocator
Definition: Eigen.h:130
Eigen::Matrix4d TransformVector6dToMatrix4d(const Eigen::Vector6d &input)
Definition: Eigen.cpp:76
Eigen::Vector3d ColorToDouble(uint8_t r, uint8_t g, uint8_t b)
Color conversion from uint8_t 0-255 to double [0,1].
Definition: Eigen.cpp:278
Eigen::Matrix3d RotationMatrixZ(double radians)
Definition: Eigen.cpp:262
Eigen::aligned_allocator< Eigen::Vector2d > Vector2d_allocator
Definition: Eigen.h:127
std::tuple< bool, Eigen::Matrix4d > SolveJacobianSystemAndObtainExtrinsicMatrix(const Eigen::Matrix6d &JTJ, const Eigen::Vector6d &JTr)
Definition: Eigen.cpp:105
std::tuple< MatType, VecType, double > ComputeJTJandJTr(std::function< void(int, VecType &, double &, double &)> f, int iteration_num, bool verbose=true)
Definition: Eigen.cpp:148
Eigen::aligned_allocator< Eigen::Matrix6d > Matrix6d_allocator
Definition: Eigen.h:126
Eigen::aligned_allocator< Eigen::Vector3uint8 > Vector3uint8_allocator
Definition: Eigen.h:128
Generic file read and write utility for python interface.