Blame internal/ceres/visibility_based_preconditioner.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2017 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//
Packit ea1746
// Preconditioners for linear systems that arise in Structure from
Packit ea1746
// Motion problems. VisibilityBasedPreconditioner implements:
Packit ea1746
//
Packit ea1746
//  CLUSTER_JACOBI
Packit ea1746
//  CLUSTER_TRIDIAGONAL
Packit ea1746
//
Packit ea1746
// Detailed descriptions of these preconditions beyond what is
Packit ea1746
// documented here can be found in
Packit ea1746
//
Packit ea1746
// Visibility Based Preconditioning for Bundle Adjustment
Packit ea1746
// A. Kushal & S. Agarwal, CVPR 2012.
Packit ea1746
//
Packit ea1746
// http://www.cs.washington.edu/homes/sagarwal/vbp.pdf
Packit ea1746
//
Packit ea1746
// The two preconditioners share enough code that its most efficient
Packit ea1746
// to implement them as part of the same code base.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_VISIBILITY_BASED_PRECONDITIONER_H_
Packit ea1746
#define CERES_INTERNAL_VISIBILITY_BASED_PRECONDITIONER_H_
Packit ea1746
Packit ea1746
#include <set>
Packit ea1746
#include <utility>
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/collections_port.h"
Packit ea1746
#include "ceres/graph.h"
Packit ea1746
#include "ceres/internal/macros.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "ceres/preconditioner.h"
Packit ea1746
#include "ceres/sparse_cholesky.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class BlockRandomAccessSparseMatrix;
Packit ea1746
class BlockSparseMatrix;
Packit ea1746
struct CompressedRowBlockStructure;
Packit ea1746
class SchurEliminatorBase;
Packit ea1746
Packit ea1746
// This class implements visibility based preconditioners for
Packit ea1746
// Structure from Motion/Bundle Adjustment problems. The name
Packit ea1746
// VisibilityBasedPreconditioner comes from the fact that the sparsity
Packit ea1746
// structure of the preconditioner matrix is determined by analyzing
Packit ea1746
// the visibility structure of the scene, i.e. which cameras see which
Packit ea1746
// points.
Packit ea1746
//
Packit ea1746
// The key idea of visibility based preconditioning is to identify
Packit ea1746
// cameras that we expect have strong interactions, and then using the
Packit ea1746
// entries in the Schur complement matrix corresponding to these
Packit ea1746
// camera pairs as an approximation to the full Schur complement.
Packit ea1746
//
Packit ea1746
// CLUSTER_JACOBI identifies these camera pairs by clustering cameras,
Packit ea1746
// and considering all non-zero camera pairs within each cluster. The
Packit ea1746
// clustering in the current implementation is done using the
Packit ea1746
// Canonical Views algorithm of Simon et al. (see
Packit ea1746
// canonical_views_clustering.h). For the purposes of clustering, the
Packit ea1746
// similarity or the degree of interaction between a pair of cameras
Packit ea1746
// is measured by counting the number of points visible in both the
Packit ea1746
// cameras. Thus the name VisibilityBasedPreconditioner. Further, if we
Packit ea1746
// were to permute the parameter blocks such that all the cameras in
Packit ea1746
// the same cluster occur contiguously, the preconditioner matrix will
Packit ea1746
// be a block diagonal matrix with blocks corresponding to the
Packit ea1746
// clusters. Thus in analogy with the Jacobi preconditioner we refer
Packit ea1746
// to this as the CLUSTER_JACOBI preconditioner.
Packit ea1746
//
Packit ea1746
// CLUSTER_TRIDIAGONAL adds more mass to the CLUSTER_JACOBI
Packit ea1746
// preconditioner by considering the interaction between clusters and
Packit ea1746
// identifying strong interactions between cluster pairs. This is done
Packit ea1746
// by constructing a weighted graph on the clusters, with the weight
Packit ea1746
// on the edges connecting two clusters proportional to the number of
Packit ea1746
// 3D points visible to cameras in both the clusters. A degree-2
Packit ea1746
// maximum spanning forest is identified in this graph and the camera
Packit ea1746
// pairs contained in the edges of this forest are added to the
Packit ea1746
// preconditioner. The detailed reasoning for this construction is
Packit ea1746
// explained in the paper mentioned above.
Packit ea1746
//
Packit ea1746
// Degree-2 spanning trees and forests have the property that they
Packit ea1746
// correspond to tri-diagonal matrices. Thus there exist a permutation
Packit ea1746
// of the camera blocks under which the CLUSTER_TRIDIAGONAL
Packit ea1746
// preconditioner matrix is a block tridiagonal matrix, and thus the
Packit ea1746
// name for the preconditioner.
Packit ea1746
//
Packit ea1746
// Thread Safety: This class is NOT thread safe.
Packit ea1746
//
Packit ea1746
// Example usage:
Packit ea1746
//
Packit ea1746
//   LinearSolver::Options options;
Packit ea1746
//   options.preconditioner_type = CLUSTER_JACOBI;
Packit ea1746
//   options.elimination_groups.push_back(num_points);
Packit ea1746
//   options.elimination_groups.push_back(num_cameras);
Packit ea1746
//   VisibilityBasedPreconditioner preconditioner(
Packit ea1746
//      *A.block_structure(), options);
Packit ea1746
//   preconditioner.Update(A, NULL);
Packit ea1746
//   preconditioner.RightMultiply(x, y);
Packit ea1746
class VisibilityBasedPreconditioner : public BlockSparseMatrixPreconditioner {
Packit ea1746
 public:
Packit ea1746
  // Initialize the symbolic structure of the preconditioner. bs is
Packit ea1746
  // the block structure of the linear system to be solved. It is used
Packit ea1746
  // to determine the sparsity structure of the preconditioner matrix.
Packit ea1746
  //
Packit ea1746
  // It has the same structural requirement as other Schur complement
Packit ea1746
  // based solvers. Please see schur_eliminator.h for more details.
Packit ea1746
  VisibilityBasedPreconditioner(const CompressedRowBlockStructure& bs,
Packit ea1746
                                const Preconditioner::Options& options);
Packit ea1746
  virtual ~VisibilityBasedPreconditioner();
Packit ea1746
Packit ea1746
  // Preconditioner interface
Packit ea1746
  virtual void RightMultiply(const double* x, double* y) const;
Packit ea1746
  virtual int num_rows() const;
Packit ea1746
Packit ea1746
  friend class VisibilityBasedPreconditionerTest;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  virtual bool UpdateImpl(const BlockSparseMatrix& A, const double* D);
Packit ea1746
  void ComputeClusterJacobiSparsity(const CompressedRowBlockStructure& bs);
Packit ea1746
  void ComputeClusterTridiagonalSparsity(const CompressedRowBlockStructure& bs);
Packit ea1746
  void InitStorage(const CompressedRowBlockStructure& bs);
Packit ea1746
  void InitEliminator(const CompressedRowBlockStructure& bs);
Packit ea1746
  LinearSolverTerminationType Factorize();
Packit ea1746
  void ScaleOffDiagonalCells();
Packit ea1746
Packit ea1746
  void ClusterCameras(const std::vector<std::set<int> >& visibility);
Packit ea1746
  void FlattenMembershipMap(const HashMap<int, int>& membership_map,
Packit ea1746
                            std::vector<int>* membership_vector) const;
Packit ea1746
  void ComputeClusterVisibility(
Packit ea1746
      const std::vector<std::set<int> >& visibility,
Packit ea1746
      std::vector<std::set<int> >* cluster_visibility) const;
Packit ea1746
  WeightedGraph<int>* CreateClusterGraph(
Packit ea1746
      const std::vector<std::set<int> >& visibility) const;
Packit ea1746
  void ForestToClusterPairs(const WeightedGraph<int>& forest,
Packit ea1746
                            HashSet<std::pair<int, int> >* cluster_pairs) const;
Packit ea1746
  void ComputeBlockPairsInPreconditioner(const CompressedRowBlockStructure& bs);
Packit ea1746
  bool IsBlockPairInPreconditioner(int block1, int block2) const;
Packit ea1746
  bool IsBlockPairOffDiagonal(int block1, int block2) const;
Packit ea1746
Packit ea1746
  Preconditioner::Options options_;
Packit ea1746
Packit ea1746
  // Number of parameter blocks in the schur complement.
Packit ea1746
  int num_blocks_;
Packit ea1746
  int num_clusters_;
Packit ea1746
Packit ea1746
  // Sizes of the blocks in the schur complement.
Packit ea1746
  std::vector<int> block_size_;
Packit ea1746
Packit ea1746
  // Mapping from cameras to clusters.
Packit ea1746
  std::vector<int> cluster_membership_;
Packit ea1746
Packit ea1746
  // Non-zero camera pairs from the schur complement matrix that are
Packit ea1746
  // present in the preconditioner, sorted by row (first element of
Packit ea1746
  // each pair), then column (second).
Packit ea1746
  std::set<std::pair<int, int> > block_pairs_;
Packit ea1746
Packit ea1746
  // Set of cluster pairs (including self pairs (i,i)) in the
Packit ea1746
  // preconditioner.
Packit ea1746
  HashSet<std::pair<int, int> > cluster_pairs_;
Packit ea1746
  scoped_ptr<SchurEliminatorBase> eliminator_;
Packit ea1746
Packit ea1746
  // Preconditioner matrix.
Packit ea1746
  scoped_ptr<BlockRandomAccessSparseMatrix> m_;
Packit ea1746
  scoped_ptr<SparseCholesky> sparse_cholesky_;
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(VisibilityBasedPreconditioner);
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_VISIBILITY_BASED_PRECONDITIONER_H_