From 22ec6d6ed137fa80c33ead1450ed06dc28de0003 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Thu, 3 Apr 2008 07:48:54 +0000
Subject: [PATCH] added Element+ElementNode support in MSH format

---
 Post/PViewDataGModel.cpp   |  6 ++---
 Post/PViewDataGModel.h     |  5 ++--
 Post/PViewDataGModelIO.cpp | 51 +++++++++++++++++++++++++-------------
 Post/PViewIO.cpp           | 21 +++++++++++-----
 4 files changed, 54 insertions(+), 29 deletions(-)

diff --git a/Post/PViewDataGModel.cpp b/Post/PViewDataGModel.cpp
index 34a152b295..b4def5cb32 100644
--- a/Post/PViewDataGModel.cpp
+++ b/Post/PViewDataGModel.cpp
@@ -1,4 +1,4 @@
-// $Id: PViewDataGModel.cpp,v 1.46 2008-04-02 16:30:29 geuzaine Exp $
+// $Id: PViewDataGModel.cpp,v 1.47 2008-04-03 07:48:54 geuzaine Exp $
 //
 // Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
 //
@@ -27,8 +27,8 @@
 #include "Numeric.h"
 #include "Message.h"
 
-PViewDataGModel::PViewDataGModel() 
-  : _min(VAL_INF), _max(-VAL_INF), _type(NodeData)
+PViewDataGModel::PViewDataGModel(DataType type) 
+  : _min(VAL_INF), _max(-VAL_INF), _type(type)
 {
 }
 
diff --git a/Post/PViewDataGModel.h b/Post/PViewDataGModel.h
index ae962c4ae3..0ae11911c6 100644
--- a/Post/PViewDataGModel.h
+++ b/Post/PViewDataGModel.h
@@ -143,7 +143,7 @@ class PViewDataGModel : public PViewData {
   // the type of the dataset
   DataType _type;
  public:
-  PViewDataGModel();
+  PViewDataGModel(DataType type=NodeData);
   ~PViewDataGModel();
   bool finalize();
   int getNumTimeSteps();
@@ -170,9 +170,8 @@ class PViewDataGModel : public PViewData {
   bool hasMultipleMeshes();
   bool useGaussPoints(){ return _type == GaussPointData; }
 
-  // get/set the data type
+  // get the data type
   DataType getType(){ return _type; }
-  void setType(DataType type){ _type = type; }
   // direct access to GModel entities
   GEntity *getEntity(int step, int ent);
   // direct access to value by index
diff --git a/Post/PViewDataGModelIO.cpp b/Post/PViewDataGModelIO.cpp
index a74fdf24b7..6dc741cff9 100644
--- a/Post/PViewDataGModelIO.cpp
+++ b/Post/PViewDataGModelIO.cpp
@@ -1,4 +1,4 @@
-// $Id: PViewDataGModelIO.cpp,v 1.36 2008-04-02 20:00:38 geuzaine Exp $
+// $Id: PViewDataGModelIO.cpp,v 1.37 2008-04-03 07:48:54 geuzaine Exp $
 //
 // Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
 //
@@ -32,10 +32,10 @@
 
 bool PViewDataGModel::readMSH(std::string fileName, int fileIndex, FILE *fp,
                               bool binary, bool swap, int step, double time, 
-                              int partition, int numComp, int numNodes)
+                              int partition, int numComp, int numEnt)
 {
-  Msg(INFO, "Reading step %d (time %g) partition %d: %d nodes", 
-      step, time, partition, numNodes);
+  Msg(INFO, "Reading step %d (time %g) partition %d: %d records", 
+      step, time, partition, numEnt);
 
   while(step >= (int)_steps.size())
     _steps.push_back(new stepData<double>(GModel::current(), numComp));
@@ -50,9 +50,9 @@ bool PViewDataGModel::readMSH(std::string fileName, int fileIndex, FILE *fp,
     numSteps += _steps[i]->getNumData() ? 1 : 0;
   if(numSteps > maxSteps) return true;
 
-  _steps[step]->resizeData(numNodes);
+  _steps[step]->resizeData(numEnt);
 
-  for(int i = 0; i < numNodes; i++){
+  for(int i = 0; i < numEnt; i++){
     int num;
     if(binary){
       if(fread(&num, sizeof(int), 1, fp) != 1) return false;
@@ -61,18 +61,31 @@ bool PViewDataGModel::readMSH(std::string fileName, int fileIndex, FILE *fp,
     else{
       if(fscanf(fp, "%d", &num) != 1) return false;
     }
-    double *d = _steps[step]->getData(num, true);
+    int mult = 1;
+    if(_type == ElementNodeData || _type == GaussPointData){
+      if(binary){
+	if(fread(&mult, sizeof(int), 1, fp) != 1) return false;
+	if(swap) swapBytes((char*)&mult, sizeof(int), 1);
+      }
+      else{
+	if(fscanf(fp, "%d", &mult) != 1) return false;
+      }
+    }
+    double *d = _steps[step]->getData(num, true, mult);
     if(binary){
-      if((int)fread(d, sizeof(double), numComp, fp) != numComp) return false;
-      if(swap) swapBytes((char*)d, sizeof(double), numComp);
+      if((int)fread(d, sizeof(double), numComp * mult, fp) != numComp * mult) 
+	return false;
+      if(swap) swapBytes((char*)d, sizeof(double), numComp * mult);
     }
     else{
-      for(int j = 0; j < numComp; j++)
+      for(int j = 0; j < numComp * mult; j++)
         if(fscanf(fp, "%lf", &d[j]) != 1) return false;
     }
-    double s = ComputeScalarRep(numComp, d);
-    _steps[step]->setMin(std::min(_steps[step]->getMin(), s));
-    _steps[step]->setMax(std::max(_steps[step]->getMax(), s));
+    for(int j = 0; j < mult; j++){
+      double s = ComputeScalarRep(numComp, &d[numComp *j]);
+      _steps[step]->setMin(std::min(_steps[step]->getMin(), s));
+      _steps[step]->setMax(std::max(_steps[step]->getMax(), s));
+    }
   }
 
   _partitions.insert(partition);
@@ -90,6 +103,11 @@ bool PViewDataGModel::writeMSH(std::string fileName, bool binary)
     return false;
   }
 
+  if(_type != NodeData){
+    Msg(GERROR, "Can only export node-based datasets for now");
+    return false;
+  }
+
   GModel *model = _steps[0]->getModel();
 
   binary = true;
@@ -217,9 +235,8 @@ bool PViewDataGModel::readMED(std::string fileName, int fileIndex)
   }
   else{
     med_entite_maillage ent = entType[pairs[0].first];
-    setType((ent == MED_NOEUD) ? NodeData : 
-	    (ent == MED_MAILLE) ? ElementData :
-	    ElementNodeData);
+    _type = (ent == MED_NOEUD) ? NodeData : (ent == MED_MAILLE) ? ElementData : 
+      ElementNodeData;
   }
 
   for(int step = 0; step < numSteps; step++){
@@ -263,7 +280,7 @@ bool PViewDataGModel::readMED(std::string fileName, int fileIndex)
       }
       else if(ngauss != MED_NOPG){
 	mult = ngauss;
-	setType(GaussPointData);
+	_type = GaussPointData;
       }
       _steps[step]->resizeData(numVal / mult);
 
diff --git a/Post/PViewIO.cpp b/Post/PViewIO.cpp
index dee8e9bdfe..de18c1810b 100644
--- a/Post/PViewIO.cpp
+++ b/Post/PViewIO.cpp
@@ -1,4 +1,4 @@
-// $Id: PViewIO.cpp,v 1.3 2008-03-29 10:19:43 geuzaine Exp $
+// $Id: PViewIO.cpp,v 1.4 2008-04-03 07:48:54 geuzaine Exp $
 //
 // Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
 //
@@ -145,25 +145,34 @@ bool PView::readMSH(std::string fileName, int fileIndex)
         }
       }
     }
-    else if(!strncmp(&str[1], "NodeData", 8)) {
+    else if(!strncmp(&str[1], "NodeData", 8) ||
+	    !strncmp(&str[1], "ElementData", 11) ||
+	    !strncmp(&str[1], "ElementNodeData", 15)) {
       index++;
       if(fileIndex < 0 || fileIndex == index){
+	PViewDataGModel::DataType type;
+	if(!strncmp(&str[1], "NodeData", 8))
+	  type = PViewDataGModel::NodeData;
+	else if(!strncmp(&str[1], "ElementData", 11))
+	  type = PViewDataGModel::ElementData;
+	else
+	  type = PViewDataGModel::ElementNodeData;
         // read data info
         if(!fgets(str, sizeof(str), fp)) return false;
         std::string name = extractDoubleQuotedString(str, sizeof(str));
-        int timeStep, partition, interpolationScheme, numComp, numNodes;
+        int timeStep, partition, interpolationScheme, numComp, numEnt;
         double time;
         if(!fgets(str, sizeof(str), fp)) return false;
         if(sscanf(str, "%d %lf %d %d %d %d", &timeStep, &time, &partition,
-                  &interpolationScheme, &numComp, &numNodes) != 6) return false;
+                  &interpolationScheme, &numComp, &numEnt) != 6) return false;
         // either get existing viewData, or create new one
         PView *p = getViewByName(name, timeStep, partition);
         PViewDataGModel *d = 0;
         if(p) d = dynamic_cast<PViewDataGModel*>(p->getData());
         bool create = d ? false : true;
-        if(create) d = new PViewDataGModel();
+	if(create) d = new PViewDataGModel(type);
         if(!d->readMSH(fileName, fileIndex, fp, binary, swap, timeStep, 
-                       time, partition, numComp, numNodes)){
+                       time, partition, numComp, numEnt)){
           Msg(GERROR, "Could not read data in msh file");
           if(create) delete d;
           return false;
-- 
GitLab