From 41585f02d9abd0df5aff96b3ad1d5698b444685d Mon Sep 17 00:00:00 2001 From: pmoulon Date: Thu, 21 Jan 2016 15:45:59 +0100 Subject: [PATCH] [features] IO. Handle better missing file & error reporting. #454 --- src/openMVG/features/descriptor.hpp | 17 ++++++--- src/openMVG/features/feature.hpp | 8 +++-- src/openMVG/features/features_test.cpp | 48 ++++++++++++++++++-------- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/openMVG/features/descriptor.hpp b/src/openMVG/features/descriptor.hpp index d3128a1802..2216ab7802 100644 --- a/src/openMVG/features/descriptor.hpp +++ b/src/openMVG/features/descriptor.hpp @@ -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(fileIn), std::istream_iterator(), std::back_inserter(vec_desc)); - bool bOk = !fileIn.bad(); + const bool bOk = !fileIn.bad(); fileIn.close(); return bOk; } @@ -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(file,"\n")); - bool bOk = file.good(); + const bool bOk = file.good(); file.close(); return bOk; } @@ -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)); @@ -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; } @@ -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)); @@ -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; } diff --git a/src/openMVG/features/feature.hpp b/src/openMVG/features/feature.hpp index efd6472d5c..76d25ac5ef 100644 --- a/src/openMVG/features/feature.hpp +++ b/src/openMVG/features/feature.hpp @@ -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(fileIn), std::istream_iterator(), std::back_inserter(vec_feat)); - bOk = !fileIn.bad(); + const bool bOk = !fileIn.bad(); fileIn.close(); return bOk; } @@ -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(file,"\n")); bool bOk = file.good(); diff --git a/src/openMVG/features/features_test.cpp b/src/openMVG/features/features_test.cpp index a13796b7e6..8928b6fbbb 100644 --- a/src/openMVG/features/features_test.cpp +++ b/src/openMVG/features/features_test.cpp @@ -17,12 +17,33 @@ 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 Feats_T; +// Define a descriptor and a container of descriptors +static const int DESC_LENGTH = 128; +typedef Descriptor Desc_T; +typedef std::vector 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) { @@ -30,11 +51,11 @@ TEST(featureIO, ASCII) { } //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) { @@ -45,12 +66,9 @@ TEST(featureIO, ASCII) { } } -//-- Test descriptors - -static const int DESC_LENGTH = 128; -typedef Descriptor Desc_T; -typedef std::vector Descs_T; - +//-- +//-- Descriptors interface test +//-- TEST(descriptorIO, ASCII) { // Create an input series of descriptor Descs_T vec_descs; @@ -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) { @@ -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) {