From 9f4823d88bb3b91f702cb7d96c33a3fae0729793 Mon Sep 17 00:00:00 2001
From: Gaetan Bricteux <gaetan.bricteux@uclouvain.be>
Date: Thu, 19 Apr 2012 08:39:02 +0000
Subject: [PATCH] plugin to cut mesh

---
 Plugin/CMakeLists.txt    |  1 +
 Plugin/CutMesh.cpp       | 59 ++++++++++++++++++++++++++++++++++++++++
 Plugin/CutMesh.h         | 32 ++++++++++++++++++++++
 Plugin/Distance.cpp      |  9 +++---
 Plugin/Plugin.h          |  6 ++++
 Plugin/PluginManager.cpp |  5 ++++
 6 files changed, 107 insertions(+), 5 deletions(-)
 create mode 100644 Plugin/CutMesh.cpp
 create mode 100644 Plugin/CutMesh.h

diff --git a/Plugin/CMakeLists.txt b/Plugin/CMakeLists.txt
index a557fd8eca..1b6c359112 100644
--- a/Plugin/CMakeLists.txt
+++ b/Plugin/CMakeLists.txt
@@ -29,6 +29,7 @@ set(SRC
   Bubbles.cpp NearToFarField.cpp
   DiscretizationError.cpp
   Scal2Vec.cpp
+  CutMesh.cpp
 )
 
 file(GLOB HDR RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
diff --git a/Plugin/CutMesh.cpp b/Plugin/CutMesh.cpp
new file mode 100644
index 0000000000..d941b77fcb
--- /dev/null
+++ b/Plugin/CutMesh.cpp
@@ -0,0 +1,59 @@
+// Gmsh - Copyright (C) 1997-2012 C. Geuzaine, J.-F. Remacle
+//
+// See the LICENSE.txt file for license information. Please report all
+// bugs and problems to <gmsh@geuz.org>.
+
+#include "CutMesh.h"
+#include "GModel.h"
+#include "gmshLevelset.h"
+
+
+StringXNumber CutMeshOptions_Number[] = {
+  {GMSH_FULLRC, "View", NULL, -1.},
+  {GMSH_FULLRC, "Split", NULL, 0.},
+  {GMSH_FULLRC, "SaveTri", NULL, 0.}
+};
+
+extern "C"
+{
+  GMSH_Plugin *GMSH_RegisterCutMeshPlugin() {
+    return new GMSH_CutMeshPlugin();
+  }
+}
+
+std::string GMSH_CutMeshPlugin::getHelp() const
+{
+  return "Plugin(CutMesh) cuts the mesh of the current GModel with "
+    "the zero value of the levelset defined with the view 'View'."
+    "Sub-elements are created in the new model (polygons in 2D and "
+    "polyhedra in 3D) and border elements are created on the zero-levelset.\n\n"
+    "If `Split' is nonzero, the plugin splits the mesh"
+    "along the edges of the cut elements in the positive side.\n\n"
+    "If 'SaveTri' is nonzero, the sub-elements are saved as simplices.\n\n"
+    "Plugin(CutMesh) creates one new GModel.";
+}
+
+int GMSH_CutMeshPlugin::getNbOptions() const
+{
+  return sizeof(CutMeshOptions_Number) / sizeof(StringXNumber);
+}
+
+StringXNumber *GMSH_CutMeshPlugin::getOption(int iopt)
+{
+  return &CutMeshOptions_Number[iopt];
+}
+
+void GMSH_CutMeshPlugin::run(){
+  
+  int iView = (int)CutMeshOptions_Number[0].def;
+  if(iView < 0)
+    iView = PView::list.size() - 1;
+
+  gLevelsetPostView *gLs = new gLevelsetPostView(iView);
+
+  int split = (int)CutMeshOptions_Number[1].def;
+  int saveTri = (int)CutMeshOptions_Number[2].def;
+  GModel *gm = GModel::current();
+  gm->buildCutGModel(gLs, !split, saveTri);
+}
+
diff --git a/Plugin/CutMesh.h b/Plugin/CutMesh.h
new file mode 100644
index 0000000000..17243ea2d5
--- /dev/null
+++ b/Plugin/CutMesh.h
@@ -0,0 +1,32 @@
+// Gmsh - Copyright (C) 1997-2012 C. Geuzaine, J.-F. Remacle
+//
+// See the LICENSE.txt file for license information. Please report all
+// bugs and problems to <gmsh@geuz.org>.
+
+#ifndef _CUTMESH_H_
+#define _CUTMESH_H_
+
+#include "Plugin.h"
+
+extern "C"
+{
+  GMSH_Plugin *GMSH_RegisterCutMeshPlugin();
+}
+
+class GMSH_CutMeshPlugin : public GMSH_MeshPlugin
+{
+ public:
+  GMSH_CutMeshPlugin(){}
+  std::string getName() const { return "CutMesh"; }
+  std::string getShortHelp() const
+  {
+    return "Cut mesh along a levelset";
+  }
+  std::string getHelp() const;
+  std::string getAuthor() const { return "G. Bricteux"; }
+  int getNbOptions() const;
+  StringXNumber* getOption(int iopt);
+  void run();
+};
+
+#endif
diff --git a/Plugin/Distance.cpp b/Plugin/Distance.cpp
index f0b7989488..40327ce80c 100644
--- a/Plugin/Distance.cpp
+++ b/Plugin/Distance.cpp
@@ -183,6 +183,10 @@ PView *GMSH_DistancePlugin::execute(PView *v)
 
   std::vector<GEntity*> _entities;
   GModel::current()->getEntities(_entities);
+  if(!_entities.size() || !_entities[_entities.size()-1]->getMeshElement(0)){
+    Msg::Error("This plugin needs a mesh !");
+    return view;
+  }
   GEntity* ge = _entities[_entities.size()-1];
   int integrationPointTetra[2];
   integrationPointTetra[0]=0;
@@ -195,11 +199,6 @@ PView *GMSH_DistancePlugin::execute(PView *v)
   int order=ge->getMeshElement(0)->getPolynomialOrder();
   int totNumNodes = totNodes+ge->getNumMeshElements()*integrationPointTetra[order-1];
 
-  if (totNumNodes ==0) {
-    Msg::Error("This plugin needs a mesh !");
-    return view;
-  }
-
   std::vector<SPoint3> pts;
   std::vector<double> distances;
   std::vector<MVertex* > pt2Vertex;
diff --git a/Plugin/Plugin.h b/Plugin/Plugin.h
index f528ce389a..fc756470a5 100644
--- a/Plugin/Plugin.h
+++ b/Plugin/Plugin.h
@@ -124,4 +124,10 @@ class GMSH_SolverPlugin : public GMSH_Plugin
   virtual bool GL_enhanceLine(int CurveId, Vertex *v1, Vertex *v2) { return false; }
 };
 
+class GMSH_MeshPlugin : public GMSH_Plugin
+{
+  inline GMSH_PLUGIN_TYPE getType() const { return GMSH_Plugin::GMSH_MESH_PLUGIN; }
+  virtual void run() {}
+};
+
 #endif
diff --git a/Plugin/PluginManager.cpp b/Plugin/PluginManager.cpp
index 7692045db9..b947f5da31 100644
--- a/Plugin/PluginManager.cpp
+++ b/Plugin/PluginManager.cpp
@@ -55,6 +55,7 @@
 #include "NearToFarField.h"
 #include "DiscretizationError.h"
 #include "Scal2Vec.h"
+#include "CutMesh.h"
 
 // for testing purposes only :-)
 #undef HAVE_DLOPEN
@@ -256,6 +257,10 @@ void PluginManager::registerDefaultPlugins()
 #if defined(HAVE_ANN)
     allPlugins.insert(std::pair<std::string, GMSH_Plugin*>
                       ("NearestNeighbor", GMSH_RegisterNearestNeighborPlugin()));
+#endif
+#if defined(HAVE_DINTEGRATION)
+    allPlugins.insert(std::pair<std::string, GMSH_Plugin*>
+                      ("CutMesh", GMSH_RegisterCutMeshPlugin()));
 #endif
   }
 
-- 
GitLab