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

Split mesh from colors #89

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# DGtalTools-contrib 1.5 (beta)

- *Geometry3d*
- splitMeshFromCol: new simple tool to split mesh from its color face attributes.
(Bertrand Kerautret [#89](https://github.com/DGtal-team/DGtalTools-contrib/pull/89))



Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ As the previous section but in 3d, it contains actually these tools:
- obj2off: tool to convert a .obj mesh into the .off format.
- off2sdp: converts a mesh into a set of points (.sdp).
- trunkMeshTransform: tools to transform trunk mesh from input centerline and cylinder coordinates.
- splitMeshFromCol: tool to split mesh from its color face attributes.
- volFillCCSize: fills each Connected Component with a value corresponding to the number of voxels of the CC.

<table><tr>
Expand Down
1 change: 1 addition & 0 deletions geometry3d/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SET(DGTAL_TOOLS_DEVEL_SRC
meshAxisCutter
graph2vol
trunkMeshTransform
splitMeshFromCol
)


Expand Down
151 changes: 151 additions & 0 deletions geometry3d/splitMeshFromCol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/

/**
* @file
* @ingroup geometry3d
* @author Bertrand Kerautret (\c [email protected] )
* Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
*
* @date 2024/08/20
*
* Source file of the tool splitMeshFromCol
*
* This file is part of the DGtal library/DGtalTools-contrib Project.
*/

///////////////////////////////////////////////////////////////////////////////
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/io/readers/MeshReader.h"
#include "DGtal/io/writers/MeshWriter.h"
#include "CLI11.hpp"

#include "sstream"

///////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace DGtal;
///////////////////////////////////////////////////////////////////////////////


/**
@page splitMeshFromCol splitMeshFromCol

@brief Simple tools to split mesh from its color face attributes. From a color partition of the input mesh containing N different colors it exports N mesh incuding faces associated to the considered color (and it remove non used vertices.

@b Usage: ./geometry3d/splitMeshFromCol [OPTIONS] 1 2

@b Allowed @b options @b are :

@code
Positionals:
1 TEXT:FILE REQUIRED Input file
2 TEXT REQUIRED Output basename

Options:
-h,--help Print this help message and exit
-i,--input TEXT:FILE REQUIRED Input file
-o,--output TEXT REQUIRED Output basename
@endcode

@b Example:

@code
splitMeshFromCol inputMeshCol.obj resBase
@endcode

@image html ressplitMeshFromCol.png "Example of result. "

@see
@ref splitMeshFromCol.cpp

*/
typedef DGtal::Mesh<DGtal::Z3i::RealPoint> Mesh3DR;
typedef DGtal::Mesh<DGtal::Z3i::RealPoint>::Index Face;

void extractMeshFromCol(const Mesh3DR &aMesh, const DGtal::Color &colorRef,
const std::vector<Face> &indColFaces, Mesh3DR &resMesh){
// Import all vertex from input in the resulting mesh:
for (auto i = 0; i< aMesh.nbVertex(); i++){
resMesh.addVertex(aMesh.getVertex(i));
}
for (auto i = 0; i< indColFaces.size(); i++){
resMesh.addFace(aMesh.getFace(indColFaces[i]));
}
resMesh.removeIsolatedVertices();
}

int main( int argc, char** argv )
{

double parameter {1.0};
std::string inputFileName;
std::string outputFileName;
std::stringstream usage;
usage << "Usage: " << argv[0] << " [input]\n"
<< "Typical use example:\n \t splitMeshFromCol inputMeshCol.obj resBase \n";
// parse command line using CLI-------------------------------------------------------
CLI::App app;
app.description("Simple tools to split mesh from its color face attributes. From a color partition of the input mesh containing N different colors it exports N mesh incuding faces associated to the considered color (and it remove non used vertices.\n" + usage.str() );
app.add_option("--input,-i,1", inputFileName, "Input file")->required()->check(CLI::ExistingFile);
app.add_option("--output,-o,2", outputFileName, "Output basename")->required();

app.get_formatter()->column_width(40);
CLI11_PARSE(app, argc, argv);
// END parse command line using CLI ----------------------------------------------


// Some nice processing --------------------------------------------------


trace.info() << "Starting " << argv[0] << "with input: " << inputFileName
<< " and output basename :" << outputFileName <<std::endl;
trace.info() << "Reading mesh... ";
DGtal::Mesh<DGtal::Z3i::RealPoint> aMesh(true);
aMesh << inputFileName;
trace.info() << "[done]" << std::endl;

//Partion of mesh faces
trace.info() << "Partionning colors of the mesh " ;
std::map<DGtal::Color, std::vector<Face> > mapColorFaces;
for (auto i = 0; i< aMesh.nbFaces(); i++){
if ( mapColorFaces.count(aMesh.getFaceColor(i)) == 0)
{
mapColorFaces[aMesh.getFaceColor(i)] = std::vector<Face>();
}
else
{
mapColorFaces[aMesh.getFaceColor(i)].push_back(i);
}
}
trace.info() << "[done with " << mapColorFaces.size()<< " ] " << std::endl;


unsigned int n=0;
for (auto col: mapColorFaces){
DGtal::Mesh<DGtal::Z3i::RealPoint> aResMesh(true);
extractMeshFromCol(aMesh, col.first, col.second, aResMesh);
stringstream ss; ss<< outputFileName << "_"<< n << ".obj";
trace.info() << "Writing output mesh " << ss.str() ;
aResMesh >> ss.str();
trace.info() << "[done]" << std::endl;
n++;
}
return 0;
}


Loading