blob: d376b4dfa8318124ee00303732f140c52906a034 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 "ceres/ordered_groups.h"
32
33#include <cstddef>
34#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include "gtest/gtest.h"
37
Austin Schuh3de38b02024-06-25 18:25:10 -070038namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080039
40TEST(OrderedGroups, EmptyOrderedGroupBehavesCorrectly) {
41 ParameterBlockOrdering ordering;
42 EXPECT_EQ(ordering.NumGroups(), 0);
43 EXPECT_EQ(ordering.NumElements(), 0);
44 EXPECT_EQ(ordering.GroupSize(1), 0);
45 double x;
46 EXPECT_EQ(ordering.GroupId(&x), -1);
47 EXPECT_FALSE(ordering.Remove(&x));
48}
49
50TEST(OrderedGroups, EverythingInOneGroup) {
51 ParameterBlockOrdering ordering;
52 double x[3];
53 ordering.AddElementToGroup(x, 1);
54 ordering.AddElementToGroup(x + 1, 1);
55 ordering.AddElementToGroup(x + 2, 1);
56 ordering.AddElementToGroup(x, 1);
57
58 EXPECT_EQ(ordering.NumGroups(), 1);
59 EXPECT_EQ(ordering.NumElements(), 3);
60 EXPECT_EQ(ordering.GroupSize(1), 3);
61 EXPECT_EQ(ordering.GroupSize(0), 0);
62 EXPECT_EQ(ordering.GroupId(x), 1);
63 EXPECT_EQ(ordering.GroupId(x + 1), 1);
64 EXPECT_EQ(ordering.GroupId(x + 2), 1);
65
66 ordering.Remove(x);
67 EXPECT_EQ(ordering.NumGroups(), 1);
68 EXPECT_EQ(ordering.NumElements(), 2);
69 EXPECT_EQ(ordering.GroupSize(1), 2);
70 EXPECT_EQ(ordering.GroupSize(0), 0);
71
72 EXPECT_EQ(ordering.GroupId(x), -1);
73 EXPECT_EQ(ordering.GroupId(x + 1), 1);
74 EXPECT_EQ(ordering.GroupId(x + 2), 1);
75}
76
77TEST(OrderedGroups, StartInOneGroupAndThenSplit) {
78 ParameterBlockOrdering ordering;
79 double x[3];
80 ordering.AddElementToGroup(x, 1);
81 ordering.AddElementToGroup(x + 1, 1);
82 ordering.AddElementToGroup(x + 2, 1);
83 ordering.AddElementToGroup(x, 1);
84
85 EXPECT_EQ(ordering.NumGroups(), 1);
86 EXPECT_EQ(ordering.NumElements(), 3);
87 EXPECT_EQ(ordering.GroupSize(1), 3);
88 EXPECT_EQ(ordering.GroupSize(0), 0);
89 EXPECT_EQ(ordering.GroupId(x), 1);
90 EXPECT_EQ(ordering.GroupId(x + 1), 1);
91 EXPECT_EQ(ordering.GroupId(x + 2), 1);
92
93 ordering.AddElementToGroup(x, 5);
94 EXPECT_EQ(ordering.NumGroups(), 2);
95 EXPECT_EQ(ordering.NumElements(), 3);
96 EXPECT_EQ(ordering.GroupSize(1), 2);
97 EXPECT_EQ(ordering.GroupSize(5), 1);
98 EXPECT_EQ(ordering.GroupSize(0), 0);
99
100 EXPECT_EQ(ordering.GroupId(x), 5);
101 EXPECT_EQ(ordering.GroupId(x + 1), 1);
102 EXPECT_EQ(ordering.GroupId(x + 2), 1);
103}
104
105TEST(OrderedGroups, AddAndRemoveEveryThingFromOneGroup) {
106 ParameterBlockOrdering ordering;
107 double x[3];
108 ordering.AddElementToGroup(x, 1);
109 ordering.AddElementToGroup(x + 1, 1);
110 ordering.AddElementToGroup(x + 2, 1);
111 ordering.AddElementToGroup(x, 1);
112
113 EXPECT_EQ(ordering.NumGroups(), 1);
114 EXPECT_EQ(ordering.NumElements(), 3);
115 EXPECT_EQ(ordering.GroupSize(1), 3);
116 EXPECT_EQ(ordering.GroupSize(0), 0);
117 EXPECT_EQ(ordering.GroupId(x), 1);
118 EXPECT_EQ(ordering.GroupId(x + 1), 1);
119 EXPECT_EQ(ordering.GroupId(x + 2), 1);
120
121 ordering.AddElementToGroup(x, 5);
122 ordering.AddElementToGroup(x + 1, 5);
123 ordering.AddElementToGroup(x + 2, 5);
124 EXPECT_EQ(ordering.NumGroups(), 1);
125 EXPECT_EQ(ordering.NumElements(), 3);
126 EXPECT_EQ(ordering.GroupSize(1), 0);
127 EXPECT_EQ(ordering.GroupSize(5), 3);
128 EXPECT_EQ(ordering.GroupSize(0), 0);
129
130 EXPECT_EQ(ordering.GroupId(x), 5);
131 EXPECT_EQ(ordering.GroupId(x + 1), 5);
132 EXPECT_EQ(ordering.GroupId(x + 2), 5);
133}
134
135TEST(OrderedGroups, ReverseOrdering) {
136 ParameterBlockOrdering ordering;
137 double x[3];
138 ordering.AddElementToGroup(x, 1);
139 ordering.AddElementToGroup(x + 1, 2);
140 ordering.AddElementToGroup(x + 2, 2);
141
142 EXPECT_EQ(ordering.NumGroups(), 2);
143 EXPECT_EQ(ordering.NumElements(), 3);
144 EXPECT_EQ(ordering.GroupSize(1), 1);
145 EXPECT_EQ(ordering.GroupSize(2), 2);
146 EXPECT_EQ(ordering.GroupId(x), 1);
147 EXPECT_EQ(ordering.GroupId(x + 1), 2);
148 EXPECT_EQ(ordering.GroupId(x + 2), 2);
149
150 ordering.Reverse();
151
152 EXPECT_EQ(ordering.NumGroups(), 2);
153 EXPECT_EQ(ordering.NumElements(), 3);
154 EXPECT_EQ(ordering.GroupSize(3), 1);
155 EXPECT_EQ(ordering.GroupSize(2), 2);
156 EXPECT_EQ(ordering.GroupId(x), 3);
157 EXPECT_EQ(ordering.GroupId(x + 1), 2);
158 EXPECT_EQ(ordering.GroupId(x + 2), 2);
159}
160
161TEST(OrderedGroups, ReverseOrderingWithEmptyOrderedGroups) {
162 ParameterBlockOrdering ordering;
163 // This should be a no-op.
164 ordering.Reverse();
165
166 // Ensure the properties of an empty OrderedGroups still hold after Reverse().
167 EXPECT_EQ(ordering.NumGroups(), 0);
168 EXPECT_EQ(ordering.NumElements(), 0);
169 EXPECT_EQ(ordering.GroupSize(1), 0);
170 double x;
171 EXPECT_EQ(ordering.GroupId(&x), -1);
172 EXPECT_FALSE(ordering.Remove(&x));
173}
174
175TEST(OrderedGroups, BulkRemove) {
176 ParameterBlockOrdering ordering;
177 double x[3];
178 ordering.AddElementToGroup(x, 1);
179 ordering.AddElementToGroup(x + 1, 2);
180 ordering.AddElementToGroup(x + 2, 2);
181
182 std::vector<double*> elements_to_remove;
183 elements_to_remove.push_back(x);
184 elements_to_remove.push_back(x + 2);
185
186 EXPECT_EQ(ordering.Remove(elements_to_remove), 2);
187 EXPECT_EQ(ordering.NumElements(), 1);
188 EXPECT_EQ(ordering.GroupId(x), -1);
189 EXPECT_EQ(ordering.GroupId(x + 1), 2);
190 EXPECT_EQ(ordering.GroupId(x + 2), -1);
191}
192
193TEST(OrderedGroups, BulkRemoveWithNoElements) {
194 ParameterBlockOrdering ordering;
195
196 double x[3];
197 std::vector<double*> elements_to_remove;
198 elements_to_remove.push_back(x);
199 elements_to_remove.push_back(x + 2);
200
201 EXPECT_EQ(ordering.Remove(elements_to_remove), 0);
202
203 ordering.AddElementToGroup(x, 1);
204 ordering.AddElementToGroup(x + 1, 2);
205 ordering.AddElementToGroup(x + 2, 2);
206
207 elements_to_remove.clear();
208 EXPECT_EQ(ordering.Remove(elements_to_remove), 0);
209}
210
211TEST(OrderedGroups, MinNonZeroGroup) {
212 ParameterBlockOrdering ordering;
213 double x[3];
214
215 ordering.AddElementToGroup(x, 1);
216 ordering.AddElementToGroup(x + 1, 1);
217 ordering.AddElementToGroup(x + 2, 2);
218
219 EXPECT_EQ(ordering.MinNonZeroGroup(), 1);
220 ordering.Remove(x);
221
222 EXPECT_EQ(ordering.MinNonZeroGroup(), 1);
223 ordering.Remove(x + 1);
224
225 EXPECT_EQ(ordering.MinNonZeroGroup(), 2);
226 ordering.Remove(x + 2);
227
228 // No non-zero groups left.
229 EXPECT_DEATH_IF_SUPPORTED(ordering.MinNonZeroGroup(), "NumGroups");
230}
Austin Schuh3de38b02024-06-25 18:25:10 -0700231} // namespace ceres::internal