Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[voctree] fix possible overflow in unsigned subtraction #786

Merged
merged 7 commits into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aliceVision/voctree/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void Database::find( const SparseHistogram& query, std::size_t N, std::vector<Do
{
// for each document/image in the database compute the distance between the
// histograms of the query image and the others
float distance = sparseDistance(query, document.second, distanceMethod, word_weights_);
const float distance = sparseDistance(query, document.second, distanceMethod, word_weights_);
acc(DocMatch(document.first, distance));
}

Expand Down
63 changes: 34 additions & 29 deletions src/aliceVision/voctree/VocabularyTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

#include "VocabularyTree.hpp"

#include <algorithm>

namespace aliceVision {
namespace voctree {

float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const std::string &distanceMethod, const std::vector<float>& word_weights)
{

float distance = 0.0f;
float epsilon = 0.001;
float distance{0.0f};
const float epsilon{0.001f};

SparseHistogram::const_iterator i1 = v1.begin(), i1e = v1.end();
SparseHistogram::const_iterator i2 = v2.begin(), i2e = v2.end();
auto i1 = v1.cbegin();
auto i1e = v1.cend();
auto i2 = v2.cbegin();
auto i2e = v2.cend();

if(distanceMethod.compare("classic") == 0)
if(distanceMethod == "classic")
{
while(i1 != i1e && i2 != i2e)
{
Expand All @@ -34,7 +38,8 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
else
{
distance += fabs(i1->second.size() - i2->second.size());
const auto val = std::minmax(i1->second.size(), i2->second.size());
distance += static_cast<float>(val.second - val.first);
++i1;
++i2;
}
Expand All @@ -53,11 +58,11 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
}

else if(distanceMethod.compare("commonPoints") == 0)
else if(distanceMethod == "commonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
float score{0.f};
float N1{0.f};
float N2{0.f};

while(i1 != i1e && i2 != i2e)
{
Expand Down Expand Up @@ -96,11 +101,11 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
distance = - score;
}

else if(distanceMethod.compare("strongCommonPoints") == 0)
else if(distanceMethod == "strongCommonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
float score{0.f};
float N1{0.f};
float N2{0.f};

while(i1 != i1e && i2 != i2e)
{
Expand All @@ -116,7 +121,7 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
else
{
if( ( fabs(i1->second.size() - 1.0) < epsilon ) && ( fabs(i2->second.size() - 1.0) < epsilon) )
if( ( fabs(i1->second.size() - 1.f) < epsilon ) && ( fabs(i2->second.size() - 1.f) < epsilon) )
{
score += 1;
N1 += 1;
Expand All @@ -142,11 +147,11 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
distance = - score;
}

else if(distanceMethod.compare("weightedStrongCommonPoints") == 0)
else if(distanceMethod == "weightedStrongCommonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
float score{0.f};
float N1{0.f};
float N2{0.f};

while(i1 != i1e && i2 != i2e)
{
Expand All @@ -160,7 +165,7 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
N1 += i1->second.size()*word_weights[i1->first];
++i1;
}
if( ( fabs(i1->second.size() - 1.0) < epsilon ) && ( fabs(i2->second.size() - 1.0) < epsilon) )
if( ( fabs(i1->second.size() - 1.f) < epsilon ) && ( fabs(i2->second.size() - 1.f) < epsilon) )
{
score += word_weights[i1->first];
N1 += word_weights[i1->first];
Expand All @@ -185,12 +190,12 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
distance = - score;
}

else if(distanceMethod.compare("inversedWeightedCommonPoints") == 0)
else if(distanceMethod == "inversedWeightedCommonPoints")
{
double score = 0.0;
double N1 = 0.0;
double N2 = 0.0;
std::map<int,int> compteur;
float score{0.f};
float N1{0.f};
float N2{0.f};
std::map<int,int> counter;

while(i1 != i1e && i2 != i2e)
{
Expand All @@ -206,7 +211,7 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const
}
else
{
compteur[i1->first] += std::min(i1->second.size(), i2->second.size());
counter[i1->first] += std::min(i1->second.size(), i2->second.size());
N1 += i1->second.size() / word_weights[i1->first];
N2 += i2->second.size() / word_weights[i2->first];
++i1;
Expand All @@ -222,12 +227,12 @@ float sparseDistance(const SparseHistogram& v1, const SparseHistogram& v2, const

while(i2 != i2e)
{
N2 += i2->second.size() / word_weights[i2->first];;
N2 += i2->second.size() / word_weights[i2->first];
++i2;
}

for(auto iCompteur = compteur.begin(); iCompteur != compteur.end(); iCompteur++)
score += (1.0/iCompteur->second) * word_weights[iCompteur->first];
for(const auto elem : counter)
score += (1.f/ elem.second) * word_weights[elem.first];

distance = - score;
}
Expand Down