From 1d1461a32b9539a7788a29f35c736780b49024af Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Sat, 28 Jan 2006 01:37:48 +0000
Subject: [PATCH] new plugin ExtractElements to extract elements whose values
 are comprised in some interval

---
 Plugin/ExtractElements.cpp | 172 +++++++++++++++++++++++++++++++++++++
 Plugin/ExtractElements.h   |  43 ++++++++++
 Plugin/Makefile            |   4 +-
 Plugin/Plugin.cpp          |   5 +-
 4 files changed, 221 insertions(+), 3 deletions(-)
 create mode 100644 Plugin/ExtractElements.cpp
 create mode 100644 Plugin/ExtractElements.h

diff --git a/Plugin/ExtractElements.cpp b/Plugin/ExtractElements.cpp
new file mode 100644
index 0000000000..6eea90ec22
--- /dev/null
+++ b/Plugin/ExtractElements.cpp
@@ -0,0 +1,172 @@
+// $Id: ExtractElements.cpp,v 1.1 2006-01-28 01:37:48 geuzaine Exp $
+//
+// Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+// 
+// Please report all bugs and problems to <gmsh@geuz.org>.
+
+#include "Plugin.h"
+#include "ExtractElements.h"
+#include "List.h"
+#include "Views.h"
+#include "Context.h"
+#include "Malloc.h"
+
+extern Context_T CTX;
+
+StringXNumber ExtractElementsOptions_Number[] = {
+  {GMSH_FULLRC, "MinVal", NULL, 0.},
+  {GMSH_FULLRC, "MaxVal", NULL, 1.},
+  {GMSH_FULLRC, "TimeStep", NULL, 0.},
+  {GMSH_FULLRC, "iView", NULL, -1.}
+};
+
+extern "C"
+{
+  GMSH_Plugin *GMSH_RegisterExtractElementsPlugin()
+  {
+    return new GMSH_ExtractElementsPlugin();
+  }
+}
+
+GMSH_ExtractElementsPlugin::GMSH_ExtractElementsPlugin()
+{
+  ;
+}
+
+void GMSH_ExtractElementsPlugin::getName(char *name) const
+{
+  strcpy(name, "ExtractElements");
+}
+
+void GMSH_ExtractElementsPlugin::getInfos(char *author, char *copyright, char *help_text) const
+{
+  strcpy(author, "C. Geuzaine (geuz@geuz.org)");
+  strcpy(copyright, "DGR (www.multiphysics.com)");
+  strcpy(help_text,
+         "Plugin(ExtractElements) extracts the elements\n"
+	 "from the view `iView' whose `TimeStep'th values\n"
+	 "are comprised between `MinVal' and `MaxVal'. If\n"
+	 "`iView' < 0, the plugin is run on the current view.\n"
+	 "\n"
+	 "Plugin(ExtractElements) creates one new view.\n");
+}
+
+int GMSH_ExtractElementsPlugin::getNbOptions() const
+{
+  return sizeof(ExtractElementsOptions_Number) / sizeof(StringXNumber);
+}
+
+StringXNumber *GMSH_ExtractElementsPlugin::getOption(int iopt)
+{
+  return &ExtractElementsOptions_Number[iopt];
+}
+
+void GMSH_ExtractElementsPlugin::catchErrorMessage(char *errorMessage) const
+{
+  strcpy(errorMessage, "ExtractElements failed...");
+}
+
+static void extract(List_T *inList, int inNb, 
+		    List_T *outList, int *outNb, 
+		    int timeStep, int nbNod, int nbComp)
+{
+  double MinVal = ExtractElementsOptions_Number[0].def;
+  double MaxVal = ExtractElementsOptions_Number[1].def;
+
+  int nb = List_Nbr(inList) / inNb;
+  for(int i = 0; i < List_Nbr(inList); i += nb) {
+    double *val = (double *)List_Pointer_Fast(inList, i + 3 * nbNod + 
+					      timeStep * nbNod * nbComp);
+    double v = 0.;
+    for(int j = 0; j < nbNod*nbComp; j++)
+      v += val[j];
+    v /= (double)(nbNod*nbComp);
+    if(v >= MinVal && v < MaxVal){
+      for(int j = 0; j < nb; j++)
+	List_Add(outList, List_Pointer_Fast(inList, i + j));
+      (*outNb)++;
+    }
+  }
+}
+
+Post_View *GMSH_ExtractElementsPlugin::execute(Post_View * v)
+{
+  int step = (int)ExtractElementsOptions_Number[2].def;
+  int iView = (int)ExtractElementsOptions_Number[3].def;
+
+  if(iView < 0)
+    iView = v ? v->Index : 0;
+
+  if(!List_Pointer_Test(CTX.post.list, iView)) {
+    Msg(GERROR, "View[%d] does not exist", iView);
+    return v;
+  }
+
+  Post_View *v1 = *(Post_View **)List_Pointer(CTX.post.list, iView);
+  Post_View *v2 = BeginView(1);
+
+  if(step < 0 || step > v1->NbTimeStep-1){
+    Msg(GERROR, "Invalid time step (%d) in View[%d]: using first step instead",
+	step, v1->Num);
+    step = 0;
+  }
+
+  // points
+  extract(v1->SP, v1->NbSP, v2->SP, &v2->NbSP, step, 1, 1);
+  extract(v1->VP, v1->NbVP, v2->SP, &v2->NbSP, step, 1, 3);
+  extract(v1->TP, v1->NbTP, v2->SP, &v2->NbSP, step, 1, 9);
+  // lines			                	
+  extract(v1->SL, v1->NbSL, v2->SL, &v2->NbSL, step, 2, 1);
+  extract(v1->VL, v1->NbVL, v2->SL, &v2->NbSL, step, 2, 3);
+  extract(v1->TL, v1->NbTL, v2->SL, &v2->NbSL, step, 2, 9);
+  // triangles			                	
+  extract(v1->ST, v1->NbST, v2->ST, &v2->NbST, step, 3, 1);
+  extract(v1->VT, v1->NbVT, v2->ST, &v2->NbST, step, 3, 3);
+  extract(v1->TT, v1->NbTT, v2->ST, &v2->NbST, step, 3, 9);
+  // quadrangles		                	
+  extract(v1->SQ, v1->NbSQ, v2->SQ, &v2->NbSQ, step, 4, 1);
+  extract(v1->VQ, v1->NbVQ, v2->SQ, &v2->NbSQ, step, 4, 3);
+  extract(v1->TQ, v1->NbTQ, v2->SQ, &v2->NbSQ, step, 4, 9);
+  // tets			                	
+  extract(v1->SS, v1->NbSS, v2->SS, &v2->NbSS, step, 4, 1);
+  extract(v1->VS, v1->NbVS, v2->SS, &v2->NbSS, step, 4, 3);
+  extract(v1->TS, v1->NbTS, v2->SS, &v2->NbSS, step, 4, 9);
+  // hexas			                	
+  extract(v1->SH, v1->NbSH, v2->SH, &v2->NbSH, step, 8, 1);
+  extract(v1->VH, v1->NbVH, v2->SH, &v2->NbSH, step, 8, 3);
+  extract(v1->TH, v1->NbTH, v2->SH, &v2->NbSH, step, 8, 9);
+  // prisms			                	
+  extract(v1->SI, v1->NbSI, v2->SI, &v2->NbSI, step, 6, 1);
+  extract(v1->VI, v1->NbVI, v2->SI, &v2->NbSI, step, 6, 3);
+  extract(v1->TI, v1->NbTI, v2->SI, &v2->NbSI, step, 6, 9);
+  // pyramids			                	
+  extract(v1->SY, v1->NbSY, v2->SY, &v2->NbSY, step, 5, 1);
+  extract(v1->VY, v1->NbVY, v2->SY, &v2->NbSY, step, 5, 3);
+  extract(v1->TY, v1->NbTY, v2->SY, &v2->NbSY, step, 5, 9);
+
+  // copy time data
+  for(int i = 0; i < List_Nbr(v1->Time); i++)
+    List_Add(v2->Time, List_Pointer(v1->Time, i));
+
+  // finalize
+  char name[1024], filename[1024];
+  sprintf(name, "%s_ExtractElements", v1->Name);
+  sprintf(filename, "%s_ExtractElements.pos", v1->Name);
+  EndView(v2, 1, filename, name);
+  return v2;
+}
diff --git a/Plugin/ExtractElements.h b/Plugin/ExtractElements.h
new file mode 100644
index 0000000000..ce2d82c9ae
--- /dev/null
+++ b/Plugin/ExtractElements.h
@@ -0,0 +1,43 @@
+#ifndef _EXTRACT_ELEMENTS_H_
+#define _EXTRACT_ELEMENTS_H_
+
+// Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+// 
+// Please report all bugs and problems to <gmsh@geuz.org>.
+
+#include "Plugin.h"
+#include "List.h"
+
+extern "C"
+{
+  GMSH_Plugin *GMSH_RegisterExtractElementsPlugin();
+}
+
+class GMSH_ExtractElementsPlugin : public GMSH_Post_Plugin
+{
+ public:
+  GMSH_ExtractElementsPlugin();
+  void getName(char *name) const;
+  void getInfos(char *author, char *copyright, char *helpText) const;
+  void catchErrorMessage(char *errorMessage) const;
+  int getNbOptions() const;
+  StringXNumber* getOption(int iopt);  
+  Post_View *execute(Post_View *);
+};
+
+#endif
diff --git a/Plugin/Makefile b/Plugin/Makefile
index 1bbfb3f7d3..2e42dcf536 100644
--- a/Plugin/Makefile
+++ b/Plugin/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.88 2006-01-14 16:24:55 geuzaine Exp $
+# $Id: Makefile,v 1.89 2006-01-28 01:37:48 geuzaine Exp $
 #
 # Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 #
@@ -41,7 +41,7 @@ SRC = Plugin.cpp\
         DisplacementRaise.cpp\
 	StructuralSolver.cpp\
         Skin.cpp\
-        Extract.cpp\
+        Extract.cpp ExtractElements.cpp\
         DecomposeInSimplex.cpp\
         Evaluate.cpp\
         Integrate.cpp Gradient.cpp Curl.cpp Divergence.cpp\
diff --git a/Plugin/Plugin.cpp b/Plugin/Plugin.cpp
index 052ea49a60..b53d628386 100644
--- a/Plugin/Plugin.cpp
+++ b/Plugin/Plugin.cpp
@@ -1,4 +1,4 @@
-// $Id: Plugin.cpp,v 1.79 2006-01-06 00:34:33 geuzaine Exp $
+// $Id: Plugin.cpp,v 1.80 2006-01-28 01:37:48 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -40,6 +40,7 @@
 #include "CutSphere.h"
 #include "Skin.h"
 #include "Extract.h"
+#include "ExtractElements.h"
 #include "HarmonicToTime.h"
 #include "ModulusPhase.h"
 #include "Integrate.h"
@@ -188,6 +189,8 @@ void GMSH_PluginManager::registerDefaultPlugins()
 		      ("Skin", GMSH_RegisterSkinPlugin()));
     allPlugins.insert(std::pair < char *, GMSH_Plugin * >
 		      ("Extract", GMSH_RegisterExtractPlugin()));
+    allPlugins.insert(std::pair < char *, GMSH_Plugin * >
+		      ("ExtractElements", GMSH_RegisterExtractElementsPlugin()));
     allPlugins.insert(std::pair < char *, GMSH_Plugin * >
 		      ("DecomposeInSimplex", GMSH_RegisterDecomposeInSimplexPlugin()));
     allPlugins.insert(std::pair < char *, GMSH_Plugin * >
-- 
GitLab