|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: Sameer Agarwal (sameeragarwal@google.com)
|
|
Packit |
ea1746 |
// David Gallup (dgallup@google.com)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/canonical_views_clustering.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/collections_port.h"
|
|
Packit |
ea1746 |
#include "ceres/graph.h"
|
|
Packit |
ea1746 |
#include "gtest/gtest.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int kVertexIds[] = {0, 1, 2, 3};
|
|
Packit |
ea1746 |
class CanonicalViewsTest : public ::testing::Test {
|
|
Packit |
ea1746 |
protected:
|
|
Packit |
ea1746 |
virtual void SetUp() {
|
|
Packit |
ea1746 |
// The graph structure is as follows.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Vertex weights: 0 2 2 0
|
|
Packit |
ea1746 |
// V0-----V1-----V2-----V3
|
|
Packit |
ea1746 |
// Edge weights: 0.8 0.9 0.3
|
|
Packit |
ea1746 |
const double kVertexWeights[] = {0.0, 2.0, 2.0, -1.0};
|
|
Packit |
ea1746 |
for (int i = 0; i < 4; ++i) {
|
|
Packit |
ea1746 |
graph_.AddVertex(i, kVertexWeights[i]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
// Create self edges.
|
|
Packit |
ea1746 |
// CanonicalViews requires that every view "sees" itself.
|
|
Packit |
ea1746 |
for (int i = 0; i < 4; ++i) {
|
|
Packit |
ea1746 |
graph_.AddEdge(i, i, 1.0);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Create three edges.
|
|
Packit |
ea1746 |
const double kEdgeWeights[] = {0.8, 0.9, 0.3};
|
|
Packit |
ea1746 |
for (int i = 0; i < 3; ++i) {
|
|
Packit |
ea1746 |
// The graph interface is directed, so remember to create both
|
|
Packit |
ea1746 |
// edges.
|
|
Packit |
ea1746 |
graph_.AddEdge(kVertexIds[i], kVertexIds[i + 1], kEdgeWeights[i]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void ComputeClustering() {
|
|
Packit |
ea1746 |
ComputeCanonicalViewsClustering(options_, graph_, ¢ers_, &membership_);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
WeightedGraph<int> graph_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CanonicalViewsClusteringOptions options_;
|
|
Packit |
ea1746 |
std::vector<int> centers_;
|
|
Packit |
ea1746 |
HashMap<int, int> membership_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(CanonicalViewsTest, ComputeCanonicalViewsTest) {
|
|
Packit |
ea1746 |
options_.min_views = 0;
|
|
Packit |
ea1746 |
options_.size_penalty_weight = 0.5;
|
|
Packit |
ea1746 |
options_.similarity_penalty_weight = 0.0;
|
|
Packit |
ea1746 |
options_.view_score_weight = 0.0;
|
|
Packit |
ea1746 |
ComputeClustering();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// 2 canonical views.
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_.size(), 2);
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_[0], kVertexIds[1]);
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_[1], kVertexIds[3]);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Check cluster membership.
|
|
Packit |
ea1746 |
EXPECT_EQ(FindOrDie(membership_, kVertexIds[0]), 0);
|
|
Packit |
ea1746 |
EXPECT_EQ(FindOrDie(membership_, kVertexIds[1]), 0);
|
|
Packit |
ea1746 |
EXPECT_EQ(FindOrDie(membership_, kVertexIds[2]), 0);
|
|
Packit |
ea1746 |
EXPECT_EQ(FindOrDie(membership_, kVertexIds[3]), 1);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Increases size penalty so the second canonical view won't be
|
|
Packit |
ea1746 |
// chosen.
|
|
Packit |
ea1746 |
TEST_F(CanonicalViewsTest, SizePenaltyTest) {
|
|
Packit |
ea1746 |
options_.min_views = 0;
|
|
Packit |
ea1746 |
options_.size_penalty_weight = 2.0;
|
|
Packit |
ea1746 |
options_.similarity_penalty_weight = 0.0;
|
|
Packit |
ea1746 |
options_.view_score_weight = 0.0;
|
|
Packit |
ea1746 |
ComputeClustering();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// 1 canonical view.
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_.size(), 1);
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_[0], kVertexIds[1]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Increases view score weight so vertex 2 will be chosen.
|
|
Packit |
ea1746 |
TEST_F(CanonicalViewsTest, ViewScoreTest) {
|
|
Packit |
ea1746 |
options_.min_views = 0;
|
|
Packit |
ea1746 |
options_.size_penalty_weight = 0.5;
|
|
Packit |
ea1746 |
options_.similarity_penalty_weight = 0.0;
|
|
Packit |
ea1746 |
options_.view_score_weight = 1.0;
|
|
Packit |
ea1746 |
ComputeClustering();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// 2 canonical views.
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_.size(), 2);
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_[0], kVertexIds[1]);
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_[1], kVertexIds[2]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Increases similarity penalty so vertex 2 won't be chosen despite
|
|
Packit |
ea1746 |
// it's view score.
|
|
Packit |
ea1746 |
TEST_F(CanonicalViewsTest, SimilarityPenaltyTest) {
|
|
Packit |
ea1746 |
options_.min_views = 0;
|
|
Packit |
ea1746 |
options_.size_penalty_weight = 0.5;
|
|
Packit |
ea1746 |
options_.similarity_penalty_weight = 3.0;
|
|
Packit |
ea1746 |
options_.view_score_weight = 1.0;
|
|
Packit |
ea1746 |
ComputeClustering();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// 2 canonical views.
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_.size(), 1);
|
|
Packit |
ea1746 |
EXPECT_EQ(centers_[0], kVertexIds[1]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|