ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
matrix.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 #pragma once
9 
10 #include <Eigen/Core>
11 #include <Eigen/Dense>
12 #include <Eigen/QR>
13 
14 namespace colmap {
15 
16 // Check if the given floating point array contains a NaN value.
17 template <typename Derived>
18 inline bool IsNaN(const Eigen::MatrixBase<Derived>& x);
19 
20 // Check if the given floating point array contains infinity.
21 template <typename Derived>
22 inline bool IsInf(const Eigen::MatrixBase<Derived>& x);
23 
24 // Perform RQ decomposition on matrix. The RQ decomposition transforms a matrix
25 // A into the product of an upper triangular matrix R (also known as
26 // right-triangular) and an orthogonal matrix Q.
27 template <typename MatrixType>
28 void DecomposeMatrixRQ(const MatrixType& A, MatrixType* R, MatrixType* Q);
29 
31 // Implementation
33 
34 template <typename Derived>
35 bool IsNaN(const Eigen::MatrixBase<Derived>& x) {
36  return !(x.array() == x.array()).all();
37 }
38 
39 template <typename Derived>
40 bool IsInf(const Eigen::MatrixBase<Derived>& x) {
41  return !((x - x).array() == (x - x).array()).all();
42 }
43 
44 template <typename MatrixType>
45 void DecomposeMatrixRQ(const MatrixType& A, MatrixType* R, MatrixType* Q) {
46  const MatrixType A_flipud_transpose =
47  A.transpose().rowwise().reverse().eval();
48 
49  const Eigen::HouseholderQR<MatrixType> QR(A_flipud_transpose);
50  const MatrixType& Q0 = QR.householderQ();
51  const MatrixType& R0 = QR.matrixQR();
52 
53  *R = R0.transpose().colwise().reverse().eval();
54  *R = R->rowwise().reverse().eval();
55  for (int i = 0; i < R->rows(); ++i) {
56  for (int j = 0; j < R->cols() && (R->cols() - j) > (R->rows() - i);
57  ++j) {
58  (*R)(i, j) = 0;
59  }
60  }
61 
62  *Q = Q0.transpose().colwise().reverse().eval();
63 
64  // Make the decomposition unique by requiring that det(Q) > 0.
65  if (Q->determinant() < 0) {
66  Q->row(1) *= -1.0;
67  R->col(1) *= -1.0;
68  }
69 }
70 
71 } // namespace colmap
normal_z x
void DecomposeMatrixRQ(const MatrixType &A, MatrixType *R, MatrixType *Q)
Definition: matrix.h:45
bool IsNaN(const float x)
Definition: math.h:160
bool IsInf(const float x)
Definition: math.h:163