diff --git a/ChangeLog.md b/ChangeLog.md index d5db097696..2a09f1059f 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -98,6 +98,11 @@ Action instead of Appeveyor (David Coeurjolly, [#1689](https://github.com/DGtal-team/DGtal/pull/1689)) +- *IO* + - Add reading material indices in SurfaceMeshReader::readOBJ + (Jacques-Olivier Lachaud, [#1677](https://github.com/DGtal-team/DGtal/pull/1677)) + + ## Bug fixes - *General* - Missing `boost/next_prior.hpp` includes in ReverseIterator, Melkman and Convex diff --git a/src/DGtal/io/readers/SurfaceMeshReader.h b/src/DGtal/io/readers/SurfaceMeshReader.h index 31e01f293b..e9da5ab597 100644 --- a/src/DGtal/io/readers/SurfaceMeshReader.h +++ b/src/DGtal/io/readers/SurfaceMeshReader.h @@ -73,7 +73,8 @@ namespace DGtal typedef typename SurfaceMesh::Index Index; typedef typename SurfaceMesh::Vertices Vertices; typedef typename SurfaceMesh::Faces Faces; - + typedef std::vector Materials; + /// Checks that every index in \a indices are different from the others. /// @param indices a vector of integer indices /// @return 'true' iff the integer indices are all pairwise different. @@ -97,6 +98,18 @@ namespace DGtal /// created mesh is ok. static bool readOBJ( std::istream & input, SurfaceMesh & smesh ); + + /// Reads an input file as an OBJ file format and outputs the + /// corresponding surface mesh. + /// + /// @param[in,out] input the input stream where the OBJ file is read. + /// @param[out] smesh the output surface mesh. + /// @param[out] materials a vector containing the material of each face (an index). + /// + /// @return 'true' if both reading the input stream was ok and the + /// created mesh is ok. + static + bool readOBJ( std::istream & input, SurfaceMesh & smesh, Materials& materials ); }; } // namespace DGtal diff --git a/src/DGtal/io/readers/SurfaceMeshReader.ih b/src/DGtal/io/readers/SurfaceMeshReader.ih index 9109d3706a..9749cef630 100644 --- a/src/DGtal/io/readers/SurfaceMeshReader.ih +++ b/src/DGtal/io/readers/SurfaceMeshReader.ih @@ -65,6 +65,15 @@ template bool DGtal::SurfaceMeshReader:: readOBJ( std::istream & input, SurfaceMesh & smesh ) +{ + Materials materials; + return readOBJ( input, smesh, materials ); +} +//----------------------------------------------------------------------------- +template +bool +DGtal::SurfaceMeshReader:: +readOBJ( std::istream & input, SurfaceMesh & smesh, Materials& materials ) { std::vector vertices; std::vector normals; @@ -77,6 +86,7 @@ readOBJ( std::istream & input, SurfaceMesh & smesh ) RealVector n; std::getline( input, linestr ); Index l = 0; + int mat = 0; // current material for ( ; input.good() && ! input.eof(); std::getline( input, linestr ), l++ ) { if ( linestr.empty() ) continue; // skip empty line @@ -89,6 +99,11 @@ readOBJ( std::istream & input, SurfaceMesh & smesh ) } else if ( keyword == "vn" ) { lineinput >> n[ 0 ] >> n[ 1 ] >> n[ 2 ]; normals.push_back( n ); + } else if ( keyword == "usemtl" ) { + std::string strmat; + std::operator>>( lineinput, strmat ); // lineinput >> keyword; + auto matinfo = split( strmat, '_' ); + mat = matinfo.size() >= 2 ? std::stoi( matinfo[ 1 ] ) : 0; } else if ( keyword == "f" ) { std::vector< Index > face, face_normals; while ( ! lineinput.eof() ) { @@ -109,6 +124,7 @@ readOBJ( std::istream & input, SurfaceMesh & smesh ) { faces.push_back( face ); faces_normals_idx.push_back( face_normals ); + materials.push_back( mat ); } } // Weird: necessary to clear them.