Skip to content
Snippets Groups Projects
Commit f1f84d35 authored by Norihiro Watanabe's avatar Norihiro Watanabe
Browse files

Merge pull request #34 from TomFischer/add-TIN-tools

Add tin tools
parents 677b96c4 35a2fa8b
No related branches found
No related tags found
No related merge requests found
......@@ -22,26 +22,39 @@
namespace FileIO
{
GeoLib::Surface* TINInterface::readTIN(std::string const& fname, std::vector<GeoLib::Point*> &pnt_vec, std::vector<std::string>* errors)
GeoLib::Surface* TINInterface::readTIN(std::string const& fname,
std::vector<GeoLib::Point*> &pnt_vec,
std::vector<std::string>* errors)
{
// open file
std::ifstream in(fname.c_str());
if (!in) {
WARN("readTIN(): could not open stream from %s.", fname.c_str());
if (errors) errors->push_back ("readTINFile error opening stream from " + fname);
if (errors)
errors->push_back ("readTINFile error opening stream from " + fname);
return nullptr;
}
GeoLib::Surface* sfc = new GeoLib::Surface(pnt_vec);
std::size_t id;
double p0[3], p1[3], p2[3];
while (in.good())
std::string line;
while (std::getline(in, line).good())
{
// read id
if (!(in >> id))
// allow empty lines
if (line.empty())
continue;
// parse line
std::stringstream input(line);
// read id
if (!(input >> id)) {
in.close();
delete sfc;
return nullptr;
}
// read first point
if (!(in >> p0[0] >> p0[1] >> p0[2])) {
if (!(input >> p0[0] >> p0[1] >> p0[2])) {
ERR("Could not read coords of 1st point of triangle %d.", id);
if (errors)
errors->push_back (std::string("readTIN error: ") +
......@@ -52,7 +65,7 @@ GeoLib::Surface* TINInterface::readTIN(std::string const& fname, std::vector<Geo
return nullptr;
}
// read second point
if (!(in >> p1[0] >> p1[1] >> p1[2])) {
if (!(input >> p1[0] >> p1[1] >> p1[2])) {
ERR("Could not read coords of 2nd point of triangle %d.", id);
if (errors)
errors->push_back (std::string("readTIN error: ") +
......@@ -63,7 +76,7 @@ GeoLib::Surface* TINInterface::readTIN(std::string const& fname, std::vector<Geo
return nullptr;
}
// read third point
if (!(in >> p2[0] >> p2[1] >> p2[2])) {
if (!(input >> p2[0] >> p2[1] >> p2[2])) {
ERR("Could not read coords of 3rd point of triangle %d.", id);
if (errors)
errors->push_back (std::string("readTIN error: ") +
......@@ -86,7 +99,7 @@ GeoLib::Surface* TINInterface::readTIN(std::string const& fname, std::vector<Geo
}
// determine size pnt_vec to insert the correct ids
std::size_t pnt_pos(pnt_vec.size());
std::size_t const pnt_pos(pnt_vec.size());
pnt_vec.push_back(new GeoLib::Point(p0));
pnt_vec.push_back(new GeoLib::Point(p1));
pnt_vec.push_back(new GeoLib::Point(p2));
......
......@@ -72,7 +72,74 @@ TEST_F(OGSIOVer4InterfaceTest, SimpleTIN)
boost::filesystem::remove(tin_fname);
}
TEST_F(OGSIOVer4InterfaceTest, InvalidTIN)
TEST_F(OGSIOVer4InterfaceTest, StillCorrectTINWihtAdditionalValueAtEndOfLine)
{
std::string tin_fname(BaseLib::BuildInfo::tests_tmp_path+"Surface.tin");
std::ofstream tin_out (tin_fname);
tin_out << "0 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 1.0 10\n";
tin_out << "1 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 1.0\n";
tin_out.close();
// read geometry
GeoLib::GEOObjects geometries;
std::vector<std::string> errors;
std::string geometry_name("TestGeometry");
FileIO::Legacy::readGLIFileV4(_gli_fname, &geometries, geometry_name, errors);
std::vector<GeoLib::Surface*> const*
sfcs(geometries.getSurfaceVec(geometry_name));
ASSERT_TRUE(sfcs != nullptr);
ASSERT_EQ(1u, geometries.getSurfaceVec(geometry_name)->size());
ASSERT_EQ(2u, (*geometries.getSurfaceVec(geometry_name))[0]->getNTriangles());
boost::filesystem::remove(tin_fname);
}
TEST_F(OGSIOVer4InterfaceTest, InvalidTIN_ZeroAreaTri)
{
std::string tin_fname(BaseLib::BuildInfo::tests_tmp_path+"Surface.tin");
std::ofstream tin_out (tin_fname);
tin_out << "0 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 0.0\n";
tin_out.close();
// read geometry
GeoLib::GEOObjects geometries;
std::vector<std::string> errors;
std::string geometry_name("TestGeometry");
FileIO::Legacy::readGLIFileV4(_gli_fname, &geometries, geometry_name, errors);
std::vector<GeoLib::Surface*> const*
sfcs(geometries.getSurfaceVec(geometry_name));
ASSERT_TRUE(sfcs == nullptr);
boost::filesystem::remove(tin_fname);
}
TEST_F(OGSIOVer4InterfaceTest, InvalidTIN_LineDoesNotStartWithID)
{
std::string tin_fname(BaseLib::BuildInfo::tests_tmp_path+"Surface.tin");
std::ofstream tin_out (tin_fname);
tin_out << "0 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 1.0\n";
tin_out << "a\n";
tin_out << "1 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 1.0\n";
tin_out.close();
// read geometry
GeoLib::GEOObjects geometries;
std::vector<std::string> errors;
std::string geometry_name("TestGeometry");
FileIO::Legacy::readGLIFileV4(_gli_fname, &geometries, geometry_name, errors);
std::vector<GeoLib::Surface*> const*
sfcs(geometries.getSurfaceVec(geometry_name));
ASSERT_TRUE(sfcs == nullptr);
boost::filesystem::remove(tin_fname);
}
TEST_F(OGSIOVer4InterfaceTest, InvalidTIN_PointIsMissing)
{
std::string tin_fname(BaseLib::BuildInfo::tests_tmp_path+"Surface.tin");
std::ofstream tin_out (tin_fname);
......@@ -93,7 +160,7 @@ TEST_F(OGSIOVer4InterfaceTest, InvalidTIN)
boost::filesystem::remove(tin_fname);
}
TEST_F(OGSIOVer4InterfaceTest, InvalidTIN_II)
TEST_F(OGSIOVer4InterfaceTest, InvalidTIN_CoordOfPointIsMissing)
{
std::string tin_fname(BaseLib::BuildInfo::tests_tmp_path+"Surface.tin");
std::ofstream tin_out (tin_fname);
......@@ -114,3 +181,26 @@ TEST_F(OGSIOVer4InterfaceTest, InvalidTIN_II)
boost::filesystem::remove(tin_fname);
}
TEST_F(OGSIOVer4InterfaceTest, SimpleTIN_AdditionalEmptyLinesAtEnd)
{
std::string tin_fname(BaseLib::BuildInfo::tests_tmp_path+"Surface.tin");
std::ofstream tin_out (tin_fname);
tin_out << "0 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 1.0 10\n";
tin_out << "1 0.0 0.0 0.0 1.0 0.0.0 0.0 0.0 1.0\n\n\n";
tin_out.close();
// read geometry
GeoLib::GEOObjects geometries;
std::vector<std::string> errors;
std::string geometry_name("TestGeometry");
FileIO::Legacy::readGLIFileV4(_gli_fname, &geometries, geometry_name, errors);
std::vector<GeoLib::Surface*> const*
sfcs(geometries.getSurfaceVec(geometry_name));
ASSERT_TRUE(sfcs != nullptr);
ASSERT_EQ(1u, geometries.getSurfaceVec(geometry_name)->size());
ASSERT_EQ(2u, (*geometries.getSurfaceVec(geometry_name))[0]->getNTriangles());
boost::filesystem::remove(tin_fname);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment