Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / canonical_views_clustering_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: Sameer Agarwal (sameeragarwal@google.com)
30 //         David Gallup (dgallup@google.com)
31
32 #include "ceres/canonical_views_clustering.h"
33
34 #include "ceres/collections_port.h"
35 #include "ceres/graph.h"
36 #include "gtest/gtest.h"
37
38 namespace ceres {
39 namespace internal {
40
41 const int kVertexIds[] = {0, 1, 2, 3};
42 class CanonicalViewsTest : public ::testing::Test {
43  protected:
44   virtual void SetUp() {
45     // The graph structure is as follows.
46     //
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]);
53     }
54     // Create self edges.
55     // CanonicalViews requires that every view "sees" itself.
56     for (int i = 0; i < 4; ++i) {
57       graph_.AddEdge(i, i, 1.0);
58     }
59
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
64       // edges.
65       graph_.AddEdge(kVertexIds[i], kVertexIds[i + 1], kEdgeWeights[i]);
66     }
67   }
68
69   void ComputeClustering() {
70     ComputeCanonicalViewsClustering(options_, graph_, &centers_, &membership_);
71   }
72
73   WeightedGraph<int> graph_;
74
75   CanonicalViewsClusteringOptions options_;
76   std::vector<int> centers_;
77   HashMap<int, int> membership_;
78 };
79
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;
85   ComputeClustering();
86
87   // 2 canonical views.
88   EXPECT_EQ(centers_.size(), 2);
89   EXPECT_EQ(centers_[0], kVertexIds[1]);
90   EXPECT_EQ(centers_[1], kVertexIds[3]);
91
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);
97 }
98
99 // Increases size penalty so the second canonical view won't be
100 // chosen.
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;
106   ComputeClustering();
107
108   // 1 canonical view.
109   EXPECT_EQ(centers_.size(), 1);
110   EXPECT_EQ(centers_[0], kVertexIds[1]);
111 }
112
113
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;
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.
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;
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