Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / detect_structure_test.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
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 "Eigen/Core"
32 #include "glog/logging.h"
33 #include "gtest/gtest.h"
34 #include "ceres/block_structure.h"
35 #include "ceres/detect_structure.h"
36
37 namespace ceres {
38 namespace internal {
39
40 TEST(DetectStructure, EverythingStatic) {
41   const int expected_row_block_size = 2;
42   const int expected_e_block_size = 3;
43   const int expected_f_block_size = 4;
44
45   CompressedRowBlockStructure bs;
46
47   bs.cols.push_back(Block());
48   bs.cols.back().size = 3;
49   bs.cols.back().position = 0;
50
51   bs.cols.push_back(Block());
52   bs.cols.back().size = 4;
53   bs.cols.back().position = 3;
54
55   bs.cols.push_back(Block());
56   bs.cols.back().size = 4;
57   bs.cols.back().position = 7;
58
59   {
60     bs.rows.push_back(CompressedRow());
61     CompressedRow& row = bs.rows.back();
62     row.block.size = 2;
63     row.block.position = 0;
64     row.cells.push_back(Cell(0, 0));
65     row.cells.push_back(Cell(1, 0));
66   }
67
68   {
69     bs.rows.push_back(CompressedRow());
70     CompressedRow& row = bs.rows.back();
71     row.block.size = 2;
72     row.block.position = 2;
73     row.cells.push_back(Cell(0, 0));
74     row.cells.push_back(Cell(2, 0));
75   }
76
77   int row_block_size = 0;
78   int e_block_size = 0;
79   int f_block_size = 0;
80   const int num_eliminate_blocks = 1;
81   DetectStructure(bs,
82                   num_eliminate_blocks,
83                   &row_block_size,
84                   &e_block_size,
85                   &f_block_size);
86
87   EXPECT_EQ(row_block_size, expected_row_block_size);
88   EXPECT_EQ(e_block_size, expected_e_block_size);
89   EXPECT_EQ(f_block_size, expected_f_block_size);
90 }
91
92 TEST(DetectStructure, DynamicRow) {
93   const int expected_row_block_size = Eigen::Dynamic;
94   const int expected_e_block_size = 3;
95   const int expected_f_block_size = 4;
96
97   CompressedRowBlockStructure bs;
98
99   bs.cols.push_back(Block());
100   bs.cols.back().size = 3;
101   bs.cols.back().position = 0;
102
103   bs.cols.push_back(Block());
104   bs.cols.back().size = 4;
105   bs.cols.back().position = 3;
106
107   bs.cols.push_back(Block());
108   bs.cols.back().size = 4;
109   bs.cols.back().position = 7;
110
111   {
112     bs.rows.push_back(CompressedRow());
113     CompressedRow& row = bs.rows.back();
114     row.block.size = 2;
115     row.block.position = 0;
116     row.cells.push_back(Cell(0, 0));
117     row.cells.push_back(Cell(1, 0));
118   }
119
120   {
121     bs.rows.push_back(CompressedRow());
122     CompressedRow& row = bs.rows.back();
123     row.block.size = 1;
124     row.block.position = 2;
125     row.cells.push_back(Cell(0, 0));
126     row.cells.push_back(Cell(2, 0));
127   }
128
129   int row_block_size = 0;
130   int e_block_size = 0;
131   int f_block_size = 0;
132   const int num_eliminate_blocks = 1;
133   DetectStructure(bs,
134                   num_eliminate_blocks,
135                   &row_block_size,
136                   &e_block_size,
137                   &f_block_size);
138
139   EXPECT_EQ(row_block_size, expected_row_block_size);
140   EXPECT_EQ(e_block_size, expected_e_block_size);
141   EXPECT_EQ(f_block_size, expected_f_block_size);
142 }
143
144 TEST(DetectStructure, DynamicFBlockDifferentRows) {
145   const int expected_row_block_size = 2;
146   const int expected_e_block_size = 3;
147   const int expected_f_block_size = Eigen::Dynamic;
148
149
150   CompressedRowBlockStructure bs;
151
152   bs.cols.push_back(Block());
153   bs.cols.back().size = 3;
154   bs.cols.back().position = 0;
155
156   bs.cols.push_back(Block());
157   bs.cols.back().size = 4;
158   bs.cols.back().position = 3;
159
160   bs.cols.push_back(Block());
161   bs.cols.back().size = 3;
162   bs.cols.back().position = 7;
163
164   {
165     bs.rows.push_back(CompressedRow());
166     CompressedRow& row = bs.rows.back();
167     row.block.size = 2;
168     row.block.position = 0;
169     row.cells.push_back(Cell(0, 0));
170     row.cells.push_back(Cell(1, 0));
171   }
172
173   {
174     bs.rows.push_back(CompressedRow());
175     CompressedRow& row = bs.rows.back();
176     row.block.size = 2;
177     row.block.position = 2;
178     row.cells.push_back(Cell(0, 0));
179     row.cells.push_back(Cell(2, 0));
180   }
181
182   int row_block_size = 0;
183   int e_block_size = 0;
184   int f_block_size = 0;
185   const int num_eliminate_blocks = 1;
186   DetectStructure(bs,
187                   num_eliminate_blocks,
188                   &row_block_size,
189                   &e_block_size,
190                   &f_block_size);
191
192   EXPECT_EQ(row_block_size, expected_row_block_size);
193   EXPECT_EQ(e_block_size, expected_e_block_size);
194   EXPECT_EQ(f_block_size, expected_f_block_size);
195 }
196
197 TEST(DetectStructure, DynamicEBlock) {
198   const int expected_row_block_size = 2;
199   const int expected_e_block_size = Eigen::Dynamic;
200   const int expected_f_block_size = 3;
201
202   CompressedRowBlockStructure bs;
203
204   bs.cols.push_back(Block());
205   bs.cols.back().size = 3;
206   bs.cols.back().position = 0;
207
208   bs.cols.push_back(Block());
209   bs.cols.back().size = 4;
210   bs.cols.back().position = 3;
211
212   bs.cols.push_back(Block());
213   bs.cols.back().size = 3;
214   bs.cols.back().position = 7;
215
216   {
217     bs.rows.push_back(CompressedRow());
218     CompressedRow& row = bs.rows.back();
219     row.block.size = 2;
220     row.block.position = 0;
221     row.cells.push_back(Cell(0, 0));
222     row.cells.push_back(Cell(2, 0));
223   }
224
225   {
226     bs.rows.push_back(CompressedRow());
227     CompressedRow& row = bs.rows.back();
228     row.block.size = 2;
229     row.block.position = 2;
230     row.cells.push_back(Cell(1, 0));
231     row.cells.push_back(Cell(2, 0));
232   }
233
234   int row_block_size = 0;
235   int e_block_size = 0;
236   int f_block_size = 0;
237   const int num_eliminate_blocks = 2;
238   DetectStructure(bs,
239                   num_eliminate_blocks,
240                   &row_block_size,
241                   &e_block_size,
242                   &f_block_size);
243
244   EXPECT_EQ(row_block_size, expected_row_block_size);
245   EXPECT_EQ(e_block_size, expected_e_block_size);
246   EXPECT_EQ(f_block_size, expected_f_block_size);
247 }
248
249 TEST(DetectStructure, DynamicFBlockSameRow) {
250   const int expected_row_block_size = 2;
251   const int expected_e_block_size = 3;
252   const int expected_f_block_size = Eigen::Dynamic;
253
254   CompressedRowBlockStructure bs;
255
256   bs.cols.push_back(Block());
257   bs.cols.back().size = 3;
258   bs.cols.back().position = 0;
259
260   bs.cols.push_back(Block());
261   bs.cols.back().size = 4;
262   bs.cols.back().position = 3;
263
264   bs.cols.push_back(Block());
265   bs.cols.back().size = 3;
266   bs.cols.back().position = 7;
267
268   {
269     bs.rows.push_back(CompressedRow());
270     CompressedRow& row = bs.rows.back();
271     row.block.size = 2;
272     row.block.position = 0;
273     row.cells.push_back(Cell(0, 0));
274     row.cells.push_back(Cell(1, 0));
275     row.cells.push_back(Cell(2, 0));
276   }
277
278   int row_block_size = 0;
279   int e_block_size = 0;
280   int f_block_size = 0;
281   const int num_eliminate_blocks = 1;
282   DetectStructure(bs,
283                   num_eliminate_blocks,
284                   &row_block_size,
285                   &e_block_size,
286                   &f_block_size);
287
288   EXPECT_EQ(row_block_size, expected_row_block_size);
289   EXPECT_EQ(e_block_size, expected_e_block_size);
290   EXPECT_EQ(f_block_size, expected_f_block_size);
291 }
292
293 }  // namespace internal
294 }  // namespace ceres