1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: Sameer Agarwal (sameeragarwal@google.com)
30 // David Gallup (dgallup@google.com)
32 #include "ceres/canonical_views_clustering.h"
34 #include "ceres/collections_port.h"
35 #include "ceres/graph.h"
36 #include "gtest/gtest.h"
41 const int kVertexIds[] = {0, 1, 2, 3};
42 class CanonicalViewsTest : public ::testing::Test {
44 virtual void SetUp() {
45 // The graph structure is as follows.
47 // Vertex weights: 0 2 2 0
48 // V0-----V1-----V2-----V3
49 // Edge weights: 0.8 0.9 0.3
50 const double kVertexWeights[] = {0.0, 2.0, 2.0, -1.0};
51 for (int i = 0; i < 4; ++i) {
52 graph_.AddVertex(i, kVertexWeights[i]);
55 // CanonicalViews requires that every view "sees" itself.
56 for (int i = 0; i < 4; ++i) {
57 graph_.AddEdge(i, i, 1.0);
60 // Create three edges.
61 const double kEdgeWeights[] = {0.8, 0.9, 0.3};
62 for (int i = 0; i < 3; ++i) {
63 // The graph interface is directed, so remember to create both
65 graph_.AddEdge(kVertexIds[i], kVertexIds[i + 1], kEdgeWeights[i]);
69 void ComputeClustering() {
70 ComputeCanonicalViewsClustering(options_, graph_, ¢ers_, &membership_);
73 WeightedGraph<int> graph_;
75 CanonicalViewsClusteringOptions options_;
76 std::vector<int> centers_;
77 HashMap<int, int> membership_;
80 TEST_F(CanonicalViewsTest, ComputeCanonicalViewsTest) {
81 options_.min_views = 0;
82 options_.size_penalty_weight = 0.5;
83 options_.similarity_penalty_weight = 0.0;
84 options_.view_score_weight = 0.0;
88 EXPECT_EQ(centers_.size(), 2);
89 EXPECT_EQ(centers_[0], kVertexIds[1]);
90 EXPECT_EQ(centers_[1], kVertexIds[3]);
92 // Check cluster membership.
93 EXPECT_EQ(FindOrDie(membership_, kVertexIds[0]), 0);
94 EXPECT_EQ(FindOrDie(membership_, kVertexIds[1]), 0);
95 EXPECT_EQ(FindOrDie(membership_, kVertexIds[2]), 0);
96 EXPECT_EQ(FindOrDie(membership_, kVertexIds[3]), 1);
99 // Increases size penalty so the second canonical view won't be
101 TEST_F(CanonicalViewsTest, SizePenaltyTest) {
102 options_.min_views = 0;
103 options_.size_penalty_weight = 2.0;
104 options_.similarity_penalty_weight = 0.0;
105 options_.view_score_weight = 0.0;
109 EXPECT_EQ(centers_.size(), 1);
110 EXPECT_EQ(centers_[0], kVertexIds[1]);
114 // Increases view score weight so vertex 2 will be chosen.
115 TEST_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;
122 // 2 canonical views.
123 EXPECT_EQ(centers_.size(), 2);
124 EXPECT_EQ(centers_[0], kVertexIds[1]);
125 EXPECT_EQ(centers_[1], kVertexIds[2]);
128 // Increases similarity penalty so vertex 2 won't be chosen despite
130 TEST_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;
137 // 2 canonical views.
138 EXPECT_EQ(centers_.size(), 1);
139 EXPECT_EQ(centers_[0], kVertexIds[1]);
142 } // namespace internal