From 45c4baa9379f0eea5b415c5d234b860f13b474b6 Mon Sep 17 00:00:00 2001 From: Bertrand Kerautret Date: Mon, 26 Aug 2024 15:50:55 +0200 Subject: [PATCH 1/3] base tools splitMeshFromCol --- geometry3d/CMakeLists.txt | 1 + geometry3d/splitMeshFromCol.cpp | 147 ++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 geometry3d/splitMeshFromCol.cpp diff --git a/geometry3d/CMakeLists.txt b/geometry3d/CMakeLists.txt index 8b6ea44..fad8ba7 100644 --- a/geometry3d/CMakeLists.txt +++ b/geometry3d/CMakeLists.txt @@ -12,6 +12,7 @@ SET(DGTAL_TOOLS_DEVEL_SRC meshAxisCutter graph2vol trunkMeshTransform + splitMeshFromCol ) diff --git a/geometry3d/splitMeshFromCol.cpp b/geometry3d/splitMeshFromCol.cpp new file mode 100644 index 0000000..30cff85 --- /dev/null +++ b/geometry3d/splitMeshFromCol.cpp @@ -0,0 +1,147 @@ +/** + * 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 . + * + **/ + +/** + * @file + * @ingroup geometry3d + * @author Bertrand Kerautret (\c bertrand.kerautret@univ-lyon2.fr ) + * 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 Description of the tool... + + @b Usage: splitMeshFromCol [input] + + @b Allowed @b options @b are : + + @code + -h [ --help ] display this message + -i [ --input ] arg an input file... + -p [ --parameter] arg a double parameter... + @endcode + + @b Example: + + @code + splitMeshFromCol -i $DGtal/examples/samples/.... + @endcode + + @image html ressplitMeshFromCol.png "Example of result. " + + @see + @ref splitMeshFromCol.cpp + + */ +typedef DGtal::Mesh Mesh3DR; +typedef DGtal::Mesh::Index Face; + +void extractMeshFromCol(const Mesh3DR &aMesh, const DGtal::Color &colorRef, + const std::vector &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 -i ... \n"; + // parse command line using CLI------------------------------------------------------- + CLI::App app; + app.description("Your program description.\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.add_option("--parameter,-p", parameter, "a double parameter", true); + + 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 :" << outputFileName + << " param: " << parameter < aMesh(true); + aMesh << inputFileName; + trace.info() << "[done]" << std::endl; + + //Partion of mesh faces + trace.info() << "Partionning colors of the mesh " ; + std::map > mapColorFaces; + for (auto i = 0; i< aMesh.nbFaces(); i++){ + if ( mapColorFaces.count(aMesh.getFaceColor(i)) == 0){ + mapColorFaces[aMesh.getFaceColor(i)] = std::vector(); + }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 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; +} + + From 994c779c8669126876e5c62a5dd2d779c3a2127f Mon Sep 17 00:00:00 2001 From: Bertrand Kerautret Date: Mon, 26 Aug 2024 16:16:33 +0200 Subject: [PATCH 2/3] doc tools --- geometry3d/splitMeshFromCol.cpp | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/geometry3d/splitMeshFromCol.cpp b/geometry3d/splitMeshFromCol.cpp index 30cff85..ed400ca 100644 --- a/geometry3d/splitMeshFromCol.cpp +++ b/geometry3d/splitMeshFromCol.cpp @@ -45,22 +45,27 @@ using namespace DGtal; /** @page splitMeshFromCol splitMeshFromCol - @brief Description of the tool... + @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: splitMeshFromCol [input] + @b Usage: ./geometry3d/splitMeshFromCol [OPTIONS] 1 2 @b Allowed @b options @b are : @code - -h [ --help ] display this message - -i [ --input ] arg an input file... - -p [ --parameter] arg a double parameter... + 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 -i $DGtal/examples/samples/.... + splitMeshFromCol inputMeshCol.obj resBase @endcode @image html ressplitMeshFromCol.png "Example of result. " @@ -79,7 +84,7 @@ void extractMeshFromCol(const Mesh3DR &aMesh, const DGtal::Color &colorRef, resMesh.addVertex(aMesh.getVertex(i)); } for (auto i = 0; i< indColFaces.size(); i++){ - resMesh.addFace(aMesh.getFace(indColFaces[i])); + resMesh.addFace(aMesh.getFace(indColFaces[i])); } resMesh.removeIsolatedVertices(); } @@ -92,13 +97,12 @@ int main( int argc, char** argv ) std::string outputFileName; std::stringstream usage; usage << "Usage: " << argv[0] << " [input]\n" - << "Typical use example:\n \t splitMeshFromCol -i ... \n"; + << "Typical use example:\n \t splitMeshFromCol inputMeshCol.obj resBase \n"; // parse command line using CLI------------------------------------------------------- CLI::App app; - app.description("Your program description.\n" + usage.str() ); + 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.add_option("--parameter,-p", parameter, "a double parameter", true); app.get_formatter()->column_width(40); CLI11_PARSE(app, argc, argv); @@ -109,8 +113,7 @@ int main( int argc, char** argv ) trace.info() << "Starting " << argv[0] << "with input: " << inputFileName - << " and output :" << outputFileName - << " param: " << parameter < aMesh(true); aMesh << inputFileName; @@ -120,9 +123,12 @@ int main( int argc, char** argv ) trace.info() << "Partionning colors of the mesh " ; std::map > mapColorFaces; for (auto i = 0; i< aMesh.nbFaces(); i++){ - if ( mapColorFaces.count(aMesh.getFaceColor(i)) == 0){ + if ( mapColorFaces.count(aMesh.getFaceColor(i)) == 0) + { mapColorFaces[aMesh.getFaceColor(i)] = std::vector(); - }else{ + } + else + { mapColorFaces[aMesh.getFaceColor(i)].push_back(i); } } @@ -139,8 +145,6 @@ int main( int argc, char** argv ) trace.info() << "[done]" << std::endl; n++; } - - return 0; } From 37afe002c6661c15b294946f7f2c25e2853ef06f Mon Sep 17 00:00:00 2001 From: Bertrand Kerautret Date: Mon, 26 Aug 2024 16:20:26 +0200 Subject: [PATCH 3/3] update readme and changelog --- ChangeLog.md | 4 +++- README.md | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index cad6745..e8f5cf7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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)) diff --git a/README.md b/README.md index 94a51dd..f64588a 100644 --- a/README.md +++ b/README.md @@ -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.