1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 //   this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 //   this list of conditions and the following disclaimer in the documentation
12 //   and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 //   used to endorse or promote products derived from this software without
15 //   specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
30 //
31 // Detailed descriptions of these preconditions beyond what is
32 // documented here can be found in
33 //
34 // Bundle Adjustment in the Large
35 // S. Agarwal, N. Snavely, S. Seitz & R. Szeliski, ECCV 2010
36 // http://www.cs.washington.edu/homes/sagarwal/bal.pdf
37 
38 #ifndef CERES_INTERNAL_SCHUR_JACOBI_PRECONDITIONER_H_
39 #define CERES_INTERNAL_SCHUR_JACOBI_PRECONDITIONER_H_
40 
41 #include <memory>
42 #include <set>
43 #include <utility>
44 #include <vector>
45 
46 #include "ceres/preconditioner.h"
47 
48 namespace ceres {
49 namespace internal {
50 
51 class BlockRandomAccessDiagonalMatrix;
52 class BlockSparseMatrix;
53 struct CompressedRowBlockStructure;
54 class SchurEliminatorBase;
55 
56 // This class implements the SCHUR_JACOBI preconditioner for Structure
57 // from Motion/Bundle Adjustment problems. Full mathematical details
58 // can be found in
59 //
60 // Bundle Adjustment in the Large
61 // S. Agarwal, N. Snavely, S. Seitz & R. Szeliski, ECCV 2010
62 // http://www.cs.washington.edu/homes/sagarwal/bal.pdf
63 //
64 // Example usage:
65 //
66 //   Preconditioner::Options options;
67 //   options.preconditioner_type = SCHUR_JACOBI;
68 //   options.elimination_groups.push_back(num_points);
69 //   options.elimination_groups.push_back(num_cameras);
70 //   SchurJacobiPreconditioner preconditioner(
71 //      *A.block_structure(), options);
72 //   preconditioner.Update(A, NULL);
73 //   preconditioner.RightMultiply(x, y);
74 //
75 class SchurJacobiPreconditioner : public BlockSparseMatrixPreconditioner {
76  public:
77   // Initialize the symbolic structure of the preconditioner. bs is
78   // the block structure of the linear system to be solved. It is used
79   // to determine the sparsity structure of the preconditioner matrix.
80   //
81   // It has the same structural requirement as other Schur complement
82   // based solvers. Please see schur_eliminator.h for more details.
83   SchurJacobiPreconditioner(const CompressedRowBlockStructure& bs,
84                             const Preconditioner::Options& options);
85   SchurJacobiPreconditioner(const SchurJacobiPreconditioner&) = delete;
86   void operator=(const SchurJacobiPreconditioner&) = delete;
87 
88   virtual ~SchurJacobiPreconditioner();
89 
90   // Preconditioner interface.
91   void RightMultiply(const double* x, double* y) const final;
92   int num_rows() const final;
93 
94  private:
95   void InitEliminator(const CompressedRowBlockStructure& bs);
96   bool UpdateImpl(const BlockSparseMatrix& A, const double* D) final;
97 
98   Preconditioner::Options options_;
99   std::unique_ptr<SchurEliminatorBase> eliminator_;
100   // Preconditioner matrix.
101   std::unique_ptr<BlockRandomAccessDiagonalMatrix> m_;
102 };
103 
104 }  // namespace internal
105 }  // namespace ceres
106 
107 #endif  // CERES_INTERNAL_SCHUR_JACOBI_PRECONDITIONER_H_
108