10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Block structure objects are used to carry information about the
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// dense block structure of sparse matrices. The BlockSparseMatrix
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// object uses the BlockStructure objects to keep track of the matrix
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// structure and operate upon it. This allows us to use more cache
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// friendly block oriented linear algebra operations on the matrix
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// instead of accessing it one scalar entry at a time.
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_BLOCK_STRUCTURE_H_
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_BLOCK_STRUCTURE_H_
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <vector>
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/port.h"
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h"
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass BlockStructureProto;
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtypedef int16 BlockSize;
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct Block {
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Block() : size(-1), position(-1) {}
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Block(int size_, int position_) : size(size_), position(position_) {}
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  BlockSize size;
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int position;  // Position along the row/column.
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct Cell {
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Cell() : block_id(-1), position(-1) {}
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Cell(int block_id_, int position_)
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      : block_id(block_id_), position(position_) {}
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Column or row block id as the case maybe.
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int block_id;
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Where in the values array of the jacobian is this cell located.
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  int position;
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Order cell by their block_id;
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongbool CellLessThan(const Cell& lhs, const Cell& rhs);
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct CompressedList {
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Block block;
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<Cell> cells;
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtypedef CompressedList CompressedRow;
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongtypedef CompressedList CompressedColumn;
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct CompressedRowBlockStructure {
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<Block> cols;
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<CompressedRow> rows;
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct CompressedColumnBlockStructure {
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<Block> rows;
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<CompressedColumn> cols;
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Deserialize the given block structure proto to the given block structure.
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Destroys previous contents of block_structure.
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid ProtoToBlockStructure(const BlockStructureProto &proto,
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                           CompressedRowBlockStructure *block_structure);
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Serialize the given block structure to the given proto. Destroys previous
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// contents of proto.
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongvoid BlockStructureToProto(const CompressedRowBlockStructure &block_structure,
1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                           BlockStructureProto *proto);
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_INTERNAL_BLOCK_STRUCTURE_H_
106