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

Mesh remove isolated vertex #1709

Merged
merged 15 commits into from
Nov 16, 2023
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
- *Shapes*
- Add flips to SurfaceMesh data structure
(Jacques-Olivier Lachaud, [#1702](https://github.com/DGtal-team/DGtal/pull/1702))
- Add method to remove isolated vertices in Mesh, improve obj
material reading from potential obsolete path. (Bertrand Kerautret,
[#1709](https://github.com/DGtal-team/DGtal/issues/1709))


- *Github*
Expand Down
15 changes: 9 additions & 6 deletions src/DGtal/io/readers/MeshReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ struct MeshReader
/**
* Main method to import OFF meshes file (Geomview Object File Format)
*
* @param filename the file name to import.
* @param aMesh (return) the mesh object to be imported.
* @param invertVertexOrder used to invert (default value=false) the order of imported points (important for normal orientation).
* @param[in] filename the file name to import.
* @param[out] aMesh the mesh object to be imported.
* @param[in] invertVertexOrder used to invert (default value=false) the order of imported points (important for normal
* @param[in] onlyFaceVertex flag used to import only vertces associated to a face.
* @return an instance of the imported mesh: MeshFromPoint.
*/

static bool importOFFFile(const std::string & filename,
DGtal::Mesh<TPoint> & aMesh, bool invertVertexOrder=false);
DGtal::Mesh<TPoint> & aMesh, bool invertVertexOrder=false,
bool onlyFaceVertex=false);



Expand All @@ -134,11 +136,12 @@ struct MeshReader
///
/// @param[in] filename the input filename of the OBJ file to be read.
/// @param[out] aMesh the output mesh.
///
/// @param[in] onlyFaceVertex flag used to import only vertces associated to a face.
///
/// @return 'true' if both reading the input stream was ok and the
/// created mesh is ok.
static
bool importOBJFile(const std::string & filename, DGtal::Mesh<TPoint> & aMesh );
bool importOBJFile(const std::string & filename, DGtal::Mesh<TPoint> & aMesh, bool onlyFaceVertex=false );


/// Checks that each index in \a indices is unique.
Expand Down
34 changes: 27 additions & 7 deletions src/DGtal/io/readers/MeshReader.ih
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ inline
bool
DGtal::MeshReader<TPoint>::importOFFFile(const std::string & aFilename,
DGtal::Mesh<TPoint> & aMesh,
bool invertVertexOrder)
bool invertVertexOrder, bool onlyFaceVertex)
{
std::ifstream infile;
DGtal::IOException dgtalio;
Expand Down Expand Up @@ -199,10 +199,12 @@ DGtal::MeshReader<TPoint>::importOFFFile(const std::string & aFilename,
aMesh.addFace(aFace);
}
getline(infile, str);
str_in = std::istringstream ( str );


str_in = std::istringstream ( str );
}
if (onlyFaceVertex)
{
aMesh.removeIsolatedVertices();
}

return true;
}
Expand Down Expand Up @@ -312,7 +314,8 @@ template <typename TPoint>
inline
bool
DGtal::MeshReader<TPoint>::
importOBJFile( const std::string & filename, DGtal::Mesh<TPoint> & mesh )
importOBJFile( const std::string & filename, DGtal::Mesh<TPoint> & mesh,
bool onlyFaceVertex )
{
typedef typename Mesh<TPoint>::Index Index;
std::vector<TPoint> vertices;
Expand Down Expand Up @@ -396,6 +399,21 @@ importOBJFile( const std::string & filename, DGtal::Mesh<TPoint> & mesh )
if (is.good()){
material = MeshReader<TPoint>::readMaterial(is);
useMtllib = true;
is.close();
}else {
// Path of material is probably outdated, trying to open same directroy as source mesh.
iSep = name.find_last_of('/');
if ((int)iSep == -1)
{ // special for windows.
iSep = name.find_last_of('\\');
}
std::string pathMesh = name.substr(iSep+1,name.size());
std::ifstream is2 (path+pathMesh);
if (is2.good()){
material = MeshReader<TPoint>::readMaterial(is2);
useMtllib = true;
}
is2.close();
}
}
else if (keyword == "usemtl")
Expand Down Expand Up @@ -432,6 +450,9 @@ importOBJFile( const std::string & filename, DGtal::Mesh<TPoint> & mesh )
mesh.setFaceColor(i, colors[i]);
}
}
if (onlyFaceVertex && ! input.bad()){
mesh.removeIsolatedVertices();
}
return ( ! input.bad() );
}

Expand All @@ -450,12 +471,11 @@ DGtal::operator<< ( Mesh<TPoint> & mesh, const std::string &filename )
DGtal::MeshReader< TPoint >::importOFSFile(filename, mesh);
return true;
}
else if(extension== "obj")
else if(extension == "obj")
{
DGtal::MeshReader< TPoint >::importOBJFile(filename, mesh);
return true;
}

return false;
}

Expand Down
14 changes: 13 additions & 1 deletion src/DGtal/shapes/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,23 @@ namespace DGtal
**/
void clearFaces();


/**
* Clear all vertices of the mesh.
**/
void clearVertices();

/**
* Removed isolated vertices of input mesh that are not associated to a
* face.
**/
void removeIsolatedVertices();

/**
* Change the scale of the mesh (i.e all vertex coordinates are multiplied by a given factor aScale).
* @param[in] aScale the scale factor.
**/
void changeScale(const typename TPoint::Component aScale);
void rescale(const typename TPoint::Component aScale);

/**
* SubDivide triangular mesh if triangle area is larger than the given parameter.
Expand Down
Loading
Loading