blob: 0593d6536474a66a38faddd23e53ef83297a638b [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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: Sameer Agarwal (sameeragarwal@google.com)
30// David Gallup (dgallup@google.com)
31
32#include "ceres/canonical_views_clustering.h"
33
34#include <unordered_map>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include "ceres/graph.h"
37#include "gtest/gtest.h"
38
39namespace ceres {
40namespace internal {
41
42const int kVertexIds[] = {0, 1, 2, 3};
43class CanonicalViewsTest : public ::testing::Test {
44 protected:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080045 void SetUp() final {
Austin Schuh70cc9552019-01-21 19:46:48 -080046 // The graph structure is as follows.
47 //
48 // Vertex weights: 0 2 2 0
49 // V0-----V1-----V2-----V3
50 // Edge weights: 0.8 0.9 0.3
51 const double kVertexWeights[] = {0.0, 2.0, 2.0, -1.0};
52 for (int i = 0; i < 4; ++i) {
53 graph_.AddVertex(i, kVertexWeights[i]);
54 }
55 // Create self edges.
56 // CanonicalViews requires that every view "sees" itself.
57 for (int i = 0; i < 4; ++i) {
58 graph_.AddEdge(i, i, 1.0);
59 }
60
61 // Create three edges.
62 const double kEdgeWeights[] = {0.8, 0.9, 0.3};
63 for (int i = 0; i < 3; ++i) {
64 // The graph interface is directed, so remember to create both
65 // edges.
66 graph_.AddEdge(kVertexIds[i], kVertexIds[i + 1], kEdgeWeights[i]);
67 }
68 }
69
70 void ComputeClustering() {
71 ComputeCanonicalViewsClustering(options_, graph_, &centers_, &membership_);
72 }
73
74 WeightedGraph<int> graph_;
75
76 CanonicalViewsClusteringOptions options_;
77 std::vector<int> centers_;
78 std::unordered_map<int, int> membership_;
79};
80
81TEST_F(CanonicalViewsTest, ComputeCanonicalViewsTest) {
82 options_.min_views = 0;
83 options_.size_penalty_weight = 0.5;
84 options_.similarity_penalty_weight = 0.0;
85 options_.view_score_weight = 0.0;
86 ComputeClustering();
87
88 // 2 canonical views.
89 EXPECT_EQ(centers_.size(), 2);
90 EXPECT_EQ(centers_[0], kVertexIds[1]);
91 EXPECT_EQ(centers_[1], kVertexIds[3]);
92
93 // Check cluster membership.
94 EXPECT_EQ(FindOrDie(membership_, kVertexIds[0]), 0);
95 EXPECT_EQ(FindOrDie(membership_, kVertexIds[1]), 0);
96 EXPECT_EQ(FindOrDie(membership_, kVertexIds[2]), 0);
97 EXPECT_EQ(FindOrDie(membership_, kVertexIds[3]), 1);
98}
99
100// Increases size penalty so the second canonical view won't be
101// chosen.
102TEST_F(CanonicalViewsTest, SizePenaltyTest) {
103 options_.min_views = 0;
104 options_.size_penalty_weight = 2.0;
105 options_.similarity_penalty_weight = 0.0;
106 options_.view_score_weight = 0.0;
107 ComputeClustering();
108
109 // 1 canonical view.
110 EXPECT_EQ(centers_.size(), 1);
111 EXPECT_EQ(centers_[0], kVertexIds[1]);
112}
113
Austin Schuh70cc9552019-01-21 19:46:48 -0800114// Increases view score weight so vertex 2 will be chosen.
115TEST_F(CanonicalViewsTest, ViewScoreTest) {
116 options_.min_views = 0;
117 options_.size_penalty_weight = 0.5;
118 options_.similarity_penalty_weight = 0.0;
119 options_.view_score_weight = 1.0;
120 ComputeClustering();
121
122 // 2 canonical views.
123 EXPECT_EQ(centers_.size(), 2);
124 EXPECT_EQ(centers_[0], kVertexIds[1]);
125 EXPECT_EQ(centers_[1], kVertexIds[2]);
126}
127
128// Increases similarity penalty so vertex 2 won't be chosen despite
129// it's view score.
130TEST_F(CanonicalViewsTest, SimilarityPenaltyTest) {
131 options_.min_views = 0;
132 options_.size_penalty_weight = 0.5;
133 options_.similarity_penalty_weight = 3.0;
134 options_.view_score_weight = 1.0;
135 ComputeClustering();
136
137 // 2 canonical views.
138 EXPECT_EQ(centers_.size(), 1);
139 EXPECT_EQ(centers_[0], kVertexIds[1]);
140}
141
142} // namespace internal
143} // namespace ceres