Skip to content

Commit

Permalink
[features] IO. Handle better missing file & error reporting. alicevis…
Browse files Browse the repository at this point in the history
  • Loading branch information
pmoulon committed Jan 21, 2016
1 parent aaa87fe commit 41585f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
17 changes: 13 additions & 4 deletions src/openMVG/features/descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ static bool loadDescsFromFile(
vec_desc.clear();

std::ifstream fileIn(sfileNameDescs.c_str());
if (!fileIn.is_open())
return false;

std::copy(
std::istream_iterator<typename DescriptorsT::value_type >(fileIn),
std::istream_iterator<typename DescriptorsT::value_type >(),
std::back_inserter(vec_desc));
bool bOk = !fileIn.bad();
const bool bOk = !fileIn.bad();
fileIn.close();
return bOk;
}
Expand All @@ -156,9 +159,11 @@ static bool saveDescsToFile(
DescriptorsT & vec_desc)
{
std::ofstream file(sfileNameDescs.c_str());
if (!file.is_open())
return false;
std::copy(vec_desc.begin(), vec_desc.end(),
std::ostream_iterator<typename DescriptorsT::value_type >(file,"\n"));
bool bOk = file.good();
const bool bOk = file.good();
file.close();
return bOk;
}
Expand All @@ -174,6 +179,8 @@ static bool loadDescsFromBinFile(

vec_desc.clear();
std::ifstream fileIn(sfileNameDescs.c_str(), std::ios::in | std::ios::binary);
if (!fileIn.is_open())
return false;
//Read the number of descriptor in the file
std::size_t cardDesc = 0;
fileIn.read((char*) &cardDesc, sizeof(std::size_t));
Expand All @@ -183,7 +190,7 @@ static bool loadDescsFromBinFile(
fileIn.read((char*) (*iter).getData(),
VALUE::static_size*sizeof(typename VALUE::bin_type));
}
bool bOk = !fileIn.bad();
const bool bOk = !fileIn.bad();
fileIn.close();
return bOk;
}
Expand All @@ -197,6 +204,8 @@ static bool saveDescsToBinFile(
typedef typename DescriptorsT::value_type VALUE;

std::ofstream file(sfileNameDescs.c_str(), std::ios::out | std::ios::binary);
if (!file.is_open())
return false;
//Write the number of descriptor
const std::size_t cardDesc = vec_desc.size();
file.write((const char*) &cardDesc, sizeof(std::size_t));
Expand All @@ -205,7 +214,7 @@ static bool saveDescsToBinFile(
file.write((const char*) (*iter).getData(),
VALUE::static_size*sizeof(typename VALUE::bin_type));
}
bool bOk = file.good();
const bool bOk = file.good();
file.close();
return bOk;
}
Expand Down
8 changes: 6 additions & 2 deletions src/openMVG/features/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ static bool loadFeatsFromFile(
FeaturesT & vec_feat)
{
vec_feat.clear();
bool bOk = false;

std::ifstream fileIn(sfileNameFeats.c_str());
if (!fileIn.is_open())
return false;

std::copy(
std::istream_iterator<typename FeaturesT::value_type >(fileIn),
std::istream_iterator<typename FeaturesT::value_type >(),
std::back_inserter(vec_feat));
bOk = !fileIn.bad();
const bool bOk = !fileIn.bad();
fileIn.close();
return bOk;
}
Expand All @@ -148,6 +150,8 @@ static bool saveFeatsToFile(
FeaturesT & vec_feat)
{
std::ofstream file(sfileNameFeats.c_str());
if (!file.is_open())
return false;
std::copy(vec_feat.begin(), vec_feat.end(),
std::ostream_iterator<typename FeaturesT::value_type >(file,"\n"));
bool bOk = file.good();
Expand Down
48 changes: 33 additions & 15 deletions src/openMVG/features/features_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ using namespace openMVG::features;
using namespace std;
using std::string;

//-- Test features
static const int CARD = 12;

// Define a feature and a container of features
typedef SIOPointFeature Feature_T;
typedef std::vector<Feature_T> Feats_T;

// Define a descriptor and a container of descriptors
static const int DESC_LENGTH = 128;
typedef Descriptor<float, DESC_LENGTH> Desc_T;
typedef std::vector<Desc_T> Descs_T;

//--
//-- Features interface test
//--

static const int CARD = 12;

TEST(featureIO, NON_EXISTING_FILE) {

// Try to read a non-existing feature file
Feats_T vec_feats;
EXPECT_FALSE(loadFeatsFromFile("x.feat", vec_feats));

// Try to read a non-existing descriptor file
Descs_T vec_descs;
EXPECT_FALSE(loadDescsFromFile("x.desc", vec_descs));
EXPECT_FALSE(loadDescsFromBinFile("x.desc", vec_descs));
}

TEST(featureIO, ASCII) {
Feats_T vec_feats;
for(int i = 0; i < CARD; ++i) {
vec_feats.push_back(Feature_T(i, i*2, i*3, i*4));
}

//Save them to a file
saveFeatsToFile("tempFeats.feat", vec_feats);
EXPECT_TRUE(saveFeatsToFile("tempFeats.feat", vec_feats));

//Read the saved data and compare to input (to check write/read IO)
Feats_T vec_feats_read;
loadFeatsFromFile("tempFeats.feat", vec_feats_read);
EXPECT_TRUE(loadFeatsFromFile("tempFeats.feat", vec_feats_read));
EXPECT_EQ(CARD, vec_feats_read.size());

for(int i = 0; i < CARD; ++i) {
Expand All @@ -45,12 +66,9 @@ TEST(featureIO, ASCII) {
}
}

//-- Test descriptors

static const int DESC_LENGTH = 128;
typedef Descriptor<float, DESC_LENGTH> Desc_T;
typedef std::vector<Desc_T> Descs_T;

//--
//-- Descriptors interface test
//--
TEST(descriptorIO, ASCII) {
// Create an input series of descriptor
Descs_T vec_descs;
Expand All @@ -62,11 +80,11 @@ TEST(descriptorIO, ASCII) {
}

//Save them to a file
saveDescsToFile("tempDescs.desc", vec_descs);
EXPECT_TRUE(saveDescsToFile("tempDescs.desc", vec_descs));

//Read the saved data and compare to input (to check write/read IO)
Descs_T vec_descs_read;
loadDescsFromFile("tempDescs.desc", vec_descs_read);
EXPECT_TRUE(loadDescsFromFile("tempDescs.desc", vec_descs_read));
EXPECT_EQ(CARD, vec_descs_read.size());

for(int i = 0; i < CARD; ++i) {
Expand All @@ -88,11 +106,11 @@ TEST(descriptorIO, BINARY) {
}

//Save them to a file
saveDescsToBinFile("tempDescsBin.desc", vec_descs);
EXPECT_TRUE(saveDescsToBinFile("tempDescsBin.desc", vec_descs));

//Read the saved data and compare to input (to check write/read IO)
Descs_T vec_descs_read;
loadDescsFromBinFile("tempDescsBin.desc", vec_descs_read);
EXPECT_TRUE(loadDescsFromBinFile("tempDescsBin.desc", vec_descs_read));
EXPECT_EQ(CARD, vec_descs_read.size());

for(int i = 0; i < CARD; ++i) {
Expand Down

0 comments on commit 41585f0

Please sign in to comment.