blob: 359eb93e14ff565d9d353d49114365d6c9cdab11 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// 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 <vector>
32#include "gtest/gtest.h"
33#include "ceres/block_random_access_dense_matrix.h"
34#include "ceres/internal/eigen.h"
35
36namespace ceres {
37namespace internal {
38
39TEST(BlockRandomAccessDenseMatrix, GetCell) {
40 vector<int> blocks;
41 blocks.push_back(3);
42 blocks.push_back(4);
43 blocks.push_back(5);
44 const int num_rows = 3 + 4 + 5;
45 BlockRandomAccessDenseMatrix m(blocks);
46 EXPECT_EQ(m.num_rows(), num_rows);
47 EXPECT_EQ(m.num_cols(), num_rows);
48
49 int row_idx = 0;
50 for (int i = 0; i < blocks.size(); ++i) {
51 int col_idx = 0;
52 for (int j = 0; j < blocks.size(); ++j) {
53 int row;
54 int col;
55 int row_stride;
56 int col_stride;
57 CellInfo* cell =
58 m.GetCell(i, j, &row, &col, &row_stride, &col_stride);
59
60 EXPECT_TRUE(cell != NULL);
61 EXPECT_EQ(row, row_idx);
62 EXPECT_EQ(col, col_idx);
63 EXPECT_EQ(row_stride, 3 + 4 + 5);
64 EXPECT_EQ(col_stride, 3 + 4 + 5);
65 col_idx += blocks[j];
66 }
67 row_idx += blocks[i];
68 }
69}
70
71TEST(BlockRandomAccessDenseMatrix, WriteCell) {
72 vector<int> blocks;
73 blocks.push_back(3);
74 blocks.push_back(4);
75 blocks.push_back(5);
76 const int num_rows = 3 + 4 + 5;
77
78 BlockRandomAccessDenseMatrix m(blocks);
79
80 // Fill the cell (i,j) with (i + 1) * (j + 1)
81 for (int i = 0; i < blocks.size(); ++i) {
82 for (int j = 0; j < blocks.size(); ++j) {
83 int row;
84 int col;
85 int row_stride;
86 int col_stride;
87 CellInfo* cell = m.GetCell(
88 i, j, &row, &col, &row_stride, &col_stride);
89 MatrixRef(cell->values, row_stride, col_stride).block(
90 row, col, blocks[i], blocks[j]) =
91 (i+1) * (j+1) * Matrix::Ones(blocks[i], blocks[j]);
92 }
93 }
94
95 // Check the values in the array are correct by going over the
96 // entries of each block manually.
97 int row_idx = 0;
98 for (int i = 0; i < blocks.size(); ++i) {
99 int col_idx = 0;
100 for (int j = 0; j < blocks.size(); ++j) {
101 // Check the values of this block.
102 for (int r = 0; r < blocks[i]; ++r) {
103 for (int c = 0; c < blocks[j]; ++c) {
104 int pos = row_idx * num_rows + col_idx;
105 EXPECT_EQ(m.values()[pos], (i + 1) * (j + 1));
106 }
107 }
108 col_idx += blocks[j];
109 }
110 row_idx += blocks[i];
111 }
112}
113
114} // namespace internal
115} // namespace ceres