1// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 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_structure.h"
32#include "ceres/matrix_proto.h"
33
34namespace ceres {
35namespace internal {
36
37bool CellLessThan(const Cell& lhs, const Cell& rhs) {
38  return (lhs.block_id < rhs.block_id);
39}
40
41#ifndef CERES_NO_PROTOCOL_BUFFERS
42void ProtoToBlockStructure(const BlockStructureProto &proto,
43                           CompressedRowBlockStructure *block_structure) {
44  // Decode the column blocks.
45  block_structure->cols.resize(proto.cols_size());
46  for (int i = 0; i < proto.cols_size(); ++i) {
47    block_structure->cols[i].size = proto.cols(i).size();
48    block_structure->cols[i].position =
49        proto.cols(i).position();
50  }
51  // Decode the row structure.
52  block_structure->rows.resize(proto.rows_size());
53  for (int i = 0; i < proto.rows_size(); ++i) {
54    const CompressedRowProto &row = proto.rows(i);
55    block_structure->rows[i].block.size = row.block().size();
56    block_structure->rows[i].block.position = row.block().position();
57
58    // Copy the cells within the row.
59    block_structure->rows[i].cells.resize(row.cells_size());
60    for (int j = 0; j < row.cells_size(); ++j) {
61      const CellProto &cell = row.cells(j);
62      block_structure->rows[i].cells[j].block_id = cell.block_id();
63      block_structure->rows[i].cells[j].position = cell.position();
64    }
65  }
66}
67
68void BlockStructureToProto(const CompressedRowBlockStructure &block_structure,
69                           BlockStructureProto *proto) {
70  // Encode the column blocks.
71  for (int i = 0; i < block_structure.cols.size(); ++i) {
72    BlockProto *block = proto->add_cols();
73    block->set_size(block_structure.cols[i].size);
74    block->set_position(block_structure.cols[i].position);
75  }
76  // Encode the row structure.
77  for (int i = 0; i < block_structure.rows.size(); ++i) {
78    CompressedRowProto *row = proto->add_rows();
79    BlockProto *block = row->mutable_block();
80    block->set_size(block_structure.rows[i].block.size);
81    block->set_position(block_structure.rows[i].block.position);
82    for (int j = 0; j < block_structure.rows[i].cells.size(); ++j) {
83      CellProto *cell = row->add_cells();
84      cell->set_block_id(block_structure.rows[i].cells[j].block_id);
85      cell->set_position(block_structure.rows[i].cells[j].position);
86    }
87  }
88}
89#endif
90
91}  // namespace internal
92}  // namespace ceres
93