• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2013 Google Inc. All rights reserved.
3 // http://code.google.com/p/ceres-solver/
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 #include "ceres/block_random_access_diagonal_matrix.h"
32 
33 #include <algorithm>
34 #include <set>
35 #include <utility>
36 #include <vector>
37 #include "ceres/internal/port.h"
38 #include "ceres/internal/scoped_ptr.h"
39 #include "ceres/triplet_sparse_matrix.h"
40 #include "ceres/types.h"
41 #include "ceres/stl_util.h"
42 #include "glog/logging.h"
43 
44 namespace ceres {
45 namespace internal {
46 
BlockRandomAccessDiagonalMatrix(const vector<int> & blocks)47 BlockRandomAccessDiagonalMatrix::BlockRandomAccessDiagonalMatrix(
48     const vector<int>& blocks)
49     : blocks_(blocks) {
50   // Build the row/column layout vector and count the number of scalar
51   // rows/columns.
52   int num_cols = 0;
53   int num_nonzeros = 0;
54   vector<int> col_layout;
55   for (int i = 0; i < blocks_.size(); ++i) {
56     col_layout.push_back(num_cols);
57     num_cols += blocks_[i];
58     num_nonzeros += blocks_[i] * blocks_[i];
59   }
60 
61   VLOG(1) << "Matrix Size [" << num_cols
62           << "," << num_cols
63           << "] " << num_nonzeros;
64 
65   tsm_.reset(new TripletSparseMatrix(num_cols, num_cols, num_nonzeros));
66   tsm_->set_num_nonzeros(num_nonzeros);
67   int* rows = tsm_->mutable_rows();
68   int* cols = tsm_->mutable_cols();
69   double* values = tsm_->mutable_values();
70 
71   int pos = 0;
72   for (int i = 0; i < blocks_.size(); ++i) {
73     const int block_size = blocks_[i];
74     layout_.push_back(new CellInfo(values + pos));
75     const int block_begin = col_layout[i];
76     for (int r = 0; r < block_size; ++r) {
77       for (int c = 0; c < block_size; ++c, ++pos) {
78         rows[pos] = block_begin + r;
79         cols[pos] = block_begin + c;
80       }
81     }
82   }
83 }
84 
85 // Assume that the user does not hold any locks on any cell blocks
86 // when they are calling SetZero.
~BlockRandomAccessDiagonalMatrix()87 BlockRandomAccessDiagonalMatrix::~BlockRandomAccessDiagonalMatrix() {
88   STLDeleteContainerPointers(layout_.begin(), layout_.end());
89 }
90 
GetCell(int row_block_id,int col_block_id,int * row,int * col,int * row_stride,int * col_stride)91 CellInfo* BlockRandomAccessDiagonalMatrix::GetCell(int row_block_id,
92                                                    int col_block_id,
93                                                    int* row,
94                                                    int* col,
95                                                    int* row_stride,
96                                                    int* col_stride) {
97   if (row_block_id != col_block_id) {
98     return NULL;
99   }
100   const int stride = blocks_[row_block_id];
101 
102   // Each cell is stored contiguously as its own little dense matrix.
103   *row = 0;
104   *col = 0;
105   *row_stride = stride;
106   *col_stride = stride;
107   return layout_[row_block_id];
108 }
109 
110 // Assume that the user does not hold any locks on any cell blocks
111 // when they are calling SetZero.
SetZero()112 void BlockRandomAccessDiagonalMatrix::SetZero() {
113   if (tsm_->num_nonzeros()) {
114     VectorRef(tsm_->mutable_values(),
115               tsm_->num_nonzeros()).setZero();
116   }
117 }
118 
119 }  // namespace internal
120 }  // namespace ceres
121