From 53d58d9eabd550eb9e2d3d4dd6a9109d7f7ec0a3 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Fri, 7 Dec 2012 09:32:53 +0000
Subject: [PATCH] clarified PView unique tags (now allowed to be any number
 >=0)

---
 CMakeLists.txt             |  3 +--
 Common/GmshRemote.cpp      |  4 ++--
 Common/Options.cpp         |  4 ++--
 Common/VertexArray.cpp     |  8 +++----
 Common/VertexArray.h       |  2 +-
 Plugin/NearestNeighbor.cpp |  6 ++---
 Plugin/Plugin.cpp          | 10 ++++----
 Post/PView.cpp             | 48 +++++++++++++++++++-------------------
 Post/PView.h               | 26 ++++++++++-----------
 Post/PViewData.h           |  2 ++
 Post/PViewDataIO.cpp       | 10 ++++++++
 Post/PViewDataList.cpp     | 22 -----------------
 Post/PViewDataList.h       |  4 ++--
 Post/PViewDataListIO.cpp   | 23 ++++++++++++++++++
 Post/PViewVertexArrays.cpp | 12 +++++-----
 15 files changed, 98 insertions(+), 86 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 29427a5761..868b4ddd8a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -109,8 +109,7 @@ set(GMSH_API
     Solver/crossConfTerm.h Solver/orthogonalTerm.h
     Solver/linearSystem.h Solver/linearSystemGMM.h Solver/linearSystemCSR.h 
     Solver/linearSystemFull.h Solver/elasticitySolver.h Solver/sparsityPattern.h 
-  Post/PView.h Post/PViewData.h Post/PViewDataList.h Plugin/PluginManager.h
-    Post/OctreePost.h
+  Post/PView.h Post/PViewData.h Plugin/PluginManager.h Post/OctreePost.h
   Graphics/drawContext.h
   contrib/kbipack/gmp_normal_form.h contrib/kbipack/gmp_matrix.h 
     contrib/kbipack/gmp_blas.h contrib/kbipack/mpz.h
diff --git a/Common/GmshRemote.cpp b/Common/GmshRemote.cpp
index b00fb19491..27684383f9 100644
--- a/Common/GmshRemote.cpp
+++ b/Common/GmshRemote.cpp
@@ -48,7 +48,7 @@ static void computeAndSendVertexArrays(GmshClient *client, bool compute=true)
       if(va[type]){
         int len;
         char *str = va[type]->toChar
-          (p->getNum(), data->getName(), type + 1, min, max,
+          (p->getTag(), data->getName(), type + 1, min, max,
            data->getNumTimeSteps(), data->getTime(opt->timeStep),
            data->getBoundingBox(), len);
         client->SendMessage(GmshSocket::GMSH_VERTEX_ARRAY, len, str);
@@ -85,7 +85,7 @@ static void computeAndSendVertexArrays()
       if(va[type]){
         int len;
         char *str = va[type]->toChar
-          (p->getNum(), data->getName(), type + 1, min, max,
+          (p->getTag(), data->getName(), type + 1, min, max,
            data->getNumTimeSteps(), data->getTime(opt->timeStep),
            data->getBoundingBox(), len);
         MPI_Send(&len, 1, MPI_INT, 0, MPI_GMSH_VARRAY_LEN, MPI_COMM_WORLD);
diff --git a/Common/Options.cpp b/Common/Options.cpp
index c09d0728b0..771d2e9d8d 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -1281,8 +1281,8 @@ std::string opt_view_name(OPT_ARGS_STR)
     if(FlGui::available()){
       for(int i = 0; i < (int)PView::list.size(); i++){
         if((i == num ||
-            PView::list[i]->getAliasOf() == view->getNum() ||
-            PView::list[i]->getNum() == view->getAliasOf()) &&
+            PView::list[i]->getAliasOf() == view->getTag() ||
+            PView::list[i]->getTag() == view->getAliasOf()) &&
            FlGui::instance()->onelab->getViewButton(i)) {
           FlGui::instance()->onelab->getViewButton(i)->copy_label(data->getName());
           FlGui::instance()->onelab->getViewButton(i)->redraw();
diff --git a/Common/VertexArray.cpp b/Common/VertexArray.cpp
index f1f454258a..789a449008 100644
--- a/Common/VertexArray.cpp
+++ b/Common/VertexArray.cpp
@@ -253,7 +253,7 @@ char *VertexArray::toChar(int num, std::string name, int type, double min, doubl
 }
 
 int VertexArray::decodeHeader(int length, const char *bytes, int swap,
-                              std::string &name, int &num, int &type,
+                              std::string &name, int &tag, int &type,
                               double &min, double &max, int &numSteps, double &time,
                               double &xmin, double &ymin, double &zmin,
                               double &xmax, double &ymax, double &zmax)
@@ -271,7 +271,7 @@ int VertexArray::decodeHeader(int length, const char *bytes, int swap,
   }
 
   int index = 0;
-  memcpy(&num, &bytes[index], is); index += is;
+  memcpy(&tag, &bytes[index], is); index += is;
   int ss; memcpy(&ss, &bytes[index], is); index += is;
   if(ss){
     std::vector<char> n(ss);
@@ -295,9 +295,9 @@ int VertexArray::decodeHeader(int length, const char *bytes, int swap,
 void VertexArray::fromChar(int length, const char *bytes, int swap)
 {
   std::string name;
-  int num, type, numSteps;
+  int tag, type, numSteps;
   double min, max, time, xmin, ymin, zmin, xmax, ymax, zmax;
-  int index = decodeHeader(length, bytes, swap, name, num, type, min, max,
+  int index = decodeHeader(length, bytes, swap, name, tag, type, min, max,
                            numSteps, time, xmin, ymin, zmin, xmax, ymax, zmax);
   if(!index) return;
 
diff --git a/Common/VertexArray.h b/Common/VertexArray.h
index f302c933e0..a33a1693a2 100644
--- a/Common/VertexArray.h
+++ b/Common/VertexArray.h
@@ -202,7 +202,7 @@ class VertexArray{
                int numsteps, double time, SBoundingBox3d bbox, int &len);
   void fromChar(int length, const char *bytes, int swap);
   static int decodeHeader(int length, const char *bytes, int swap,
-                          std::string &name, int &num, int &type,
+                          std::string &name, int &tag, int &type,
                           double &min, double &max, int &numSteps, double &time,
                           double &xmin, double &ymin, double &zmin,
                           double &xmax, double &ymax, double &zmax);
diff --git a/Plugin/NearestNeighbor.cpp b/Plugin/NearestNeighbor.cpp
index 999c901a7e..5665550241 100644
--- a/Plugin/NearestNeighbor.cpp
+++ b/Plugin/NearestNeighbor.cpp
@@ -43,14 +43,14 @@ StringXNumber *GMSH_NearestNeighborPlugin::getOption(int iopt)
 PView *GMSH_NearestNeighborPlugin::execute(PView *v)
 {
   int iView = (int)NearestNeighborOptions_Number[0].def;
-  
+
   PView *v1 = getView(iView, v);
   if(!v1) return v;
   PViewData *data1 = v1->getData();
 
   int totpoints = data1->getNumPoints();
   if(!totpoints){
-    Msg::Error("View[%d] contains no points", v1->getNum());
+    Msg::Error("View[%d] contains no points", iView);
     return 0;
   }
 
@@ -94,6 +94,6 @@ PView *GMSH_NearestNeighborPlugin::execute(PView *v)
 
   data1->setName(v1->getData()->getName() + "_NearestNeighbor");
   data1->finalize();
-  
+
   return v1;
 }
diff --git a/Plugin/Plugin.cpp b/Plugin/Plugin.cpp
index 968d6cf6a7..38a84d5972 100644
--- a/Plugin/Plugin.cpp
+++ b/Plugin/Plugin.cpp
@@ -45,10 +45,10 @@ std::string GMSH_Plugin::serialize()
 {
   std::ostringstream sstream;
   for(int i = 0; i < getNbOptionsStr(); i++)
-    sstream << "Plugin(" << getName() << ")." << getOptionStr(i)->str 
+    sstream << "Plugin(" << getName() << ")." << getOptionStr(i)->str
             <<  "= \"" << getOptionStr(i)->def << "\";\n";
   for(int i = 0; i < getNbOptions(); i++)
-    sstream << "Plugin(" << getName() << ")." << getOption(i)->str 
+    sstream << "Plugin(" << getName() << ")." << getOption(i)->str
             << "=" << getOption(i)->def << ";\n";
   sstream << "Plugin(" << getName() << ").Run;\n";
   return sstream.str();
@@ -59,7 +59,7 @@ PView *GMSH_PostPlugin::executeRemote(PView *view)
   int j = -1, remoteIndex = -1;
   for(unsigned int i = 0; i < PView::list.size(); i++){
     if(PView::list[i]->getData()->isRemote()) j++;
-    if(PView::list[i]->getNum() == view->getNum()){
+    if(PView::list[i]->getTag() == view->getTag()){
       remoteIndex = j;
       break;
     }
@@ -68,11 +68,11 @@ PView *GMSH_PostPlugin::executeRemote(PView *view)
     Msg::Error("Unable to determine index of remote view");
     return view;
   }
-  
+
   for(int i = 0; i < getNbOptions(); i++)
     if(std::string(getOption(i)->str) == "View")
       getOption(i)->def = remoteIndex;
-  
+
   std::string options = serialize();
   view->getData()->fillRemoteVertexArrays(options);
   return view;
diff --git a/Post/PView.cpp b/Post/PView.cpp
index 7628e36a50..6e82ac3086 100644
--- a/Post/PView.cpp
+++ b/Post/PView.cpp
@@ -14,30 +14,30 @@
 #include "adaptiveData.h"
 #include "GmshMessage.h"
 
-int PView::_globalNum = 1;
+int PView::_globalTag = 0;
 std::vector<PView*> PView::list;
 
-void PView::_init(int num)
+void PView::_init(int tag)
 {
-  if(num){
-    _num = num;
-    _globalNum = std::max(_globalNum, _num) + 1;
+  if(tag >= 0){
+    _tag = tag;
+    _globalTag = std::max(_globalTag, _tag) + 1;
   }
   else{
-    _num = _globalNum++;
+    _tag = _globalTag++;
   }
 
   _changed = true;
-  _aliasOf = 0;
+  _aliasOf = -1;
   _eye = SPoint3(0., 0., 0.);
   va_points = va_lines = va_triangles = va_vectors = va_ellipses = 0;
   normals = 0;
 
   bool replaced = false;
   for(unsigned int i = 0; i < list.size(); i++){
-    if(list[i]->getNum() == _num){
+    if(list[i]->getTag() == _tag){
       // warning: this can potentially break aliases
-      Msg::Info("Replacing View[%d]", i);
+      Msg::Info("Replacing View[%d] (tag = %d)", i, _tag);
       replaced = true;
       delete list[i];
       _index = i;
@@ -51,9 +51,9 @@ void PView::_init(int num)
   }
 }
 
-PView::PView(int num)
+PView::PView(int tag)
 {
-  _init(num);
+  _init(tag);
   _data = new PViewDataList();
   _options = new PViewOptions(*PViewOptions::reference());
   if(_options->adaptVisualizationGrid)
@@ -61,9 +61,9 @@ PView::PView(int num)
                             _options->targetError);
 }
 
-PView::PView(PViewData *data, int num)
+PView::PView(PViewData *data, int tag)
 {
-  _init(num);
+  _init(tag);
   _data = data;
   _options = new PViewOptions(*PViewOptions::reference());
   if(_options->adaptVisualizationGrid)
@@ -75,16 +75,16 @@ PView::PView(PView *ref, bool copyOptions)
 {
   _init();
 
-  if(ref->getAliasOf()){ // alias of an alias
-    PView *orig = getViewByNum(ref->getAliasOf());
-    if(orig) _aliasOf = orig->getNum();
+  if(ref->getAliasOf() >= 0){ // alias of an alias
+    PView *orig = getViewByTag(ref->getAliasOf());
+    if(orig) _aliasOf = orig->getTag();
     else{
       Msg::Warning("Original view for alias does not exist anymore");
-      _aliasOf = ref->getNum();
+      _aliasOf = ref->getTag();
     }
   }
   else
-    _aliasOf = ref->getNum();
+    _aliasOf = ref->getTag();
 
   _data = ref->getData();
   if(copyOptions)
@@ -161,17 +161,17 @@ PView::~PView()
 
   // do not delete if another view is an alias of this one
   for(unsigned int i = 0; i < list.size(); i++)
-    if(list[i]->getAliasOf() == _num)
+    if(list[i]->getAliasOf() == _tag)
       return;
 
   // do not delete if this view is an alias and 1) if the original
   // still exists, or 2) if there are other aliases to the same view
-  if(_aliasOf)
+  if(_aliasOf >= 0)
     for(unsigned int i = 0; i < list.size(); i++)
-      if(list[i]->getNum() == _aliasOf || list[i]->getAliasOf() == _aliasOf)
+      if(list[i]->getTag() == _aliasOf || list[i]->getAliasOf() == _aliasOf)
         return;
 
-  Msg::Debug("Deleting data in View[%d] (unique num = %d)", _index, _num);
+  Msg::Debug("Deleting data in View[%d] (tag = %d)", _index, _tag);
   delete _data;
 }
 
@@ -316,10 +316,10 @@ PView *PView::getViewByFileName(const std::string &name, int timeStep, int parti
   return 0;
 }
 
-PView *PView::getViewByNum(int num, int timeStep, int partition)
+PView *PView::getViewByTag(int tag, int timeStep, int partition)
 {
   for(unsigned int i = 0; i < list.size(); i++){
-    if(list[i]->getNum() == num &&
+    if(list[i]->getTag() == tag &&
        ((timeStep < 0 || !list[i]->getData()->hasTimeStep(timeStep)) ||
         (partition < 0 || !list[i]->getData()->hasPartition(timeStep, partition))))
       return list[i];
diff --git a/Post/PView.h b/Post/PView.h
index 2294a262fc..5cb61a56f2 100644
--- a/Post/PView.h
+++ b/Post/PView.h
@@ -22,14 +22,14 @@ namespace onelab{ class localNetworkClient; }
 // A post-processing view.
 class PView{
  private:
-  static int _globalNum;
-  // unique tag of the view (> 0)
-  int _num;
+  static int _globalTag;
+  // unique tag of the view (>= 0)
+  int _tag;
   // index of the view in the current view list
   int _index;
   // flag to mark that the view has changed
   bool _changed;
-  // tag of the source view if this view is an alias, zero otherwise
+  // tag of the source view if this view is an alias, -1 otherwise
   int _aliasOf;
   // eye position (for transparency sorting)
   SPoint3 _eye;
@@ -38,13 +38,13 @@ class PView{
   // the data
   PViewData *_data;
   // initialize private stuff
-  void _init(int num=0);
+  void _init(int tag=-1);
 
  public:
   // create a new view with list-based data
-  PView(int num=0);
+  PView(int tag=-1);
   // construct a new view using the given data
-  PView(PViewData *data, int num=0);
+  PView(PViewData *data, int tag=-1);
   // construct a new view, alias of the view "ref"
   PView(PView *ref, bool copyOptions=true);
   // construct a new list-based view from a simple 2D dataset
@@ -61,9 +61,9 @@ class PView{
   // default destructor
   ~PView();
 
-  // get/set global number
-  static int getGlobalNum(){ return _globalNum; }
-  static void setGlobalNum(int num){ _globalNum = num; }
+  // get/set global tag
+  static int getGlobalTag(){ return _globalTag; }
+  static void setGlobalTag(int tag){ _globalTag = tag; }
 
   // delete the vertex arrays, used to draw the view efficiently
   void deleteVertexArrays();
@@ -76,8 +76,8 @@ class PView{
   PViewData *getData(bool useAdaptiveIfAvailable=false);
   void setData(PViewData *val){ _data = val; }
 
-  // get the view number (unique and immutable)
-  int getNum(){ return _num; }
+  // get the view tag (unique and immutable)
+  int getTag(){ return _tag; }
 
   // get/set the view index (in the view list)
   int getIndex(){ return _index; }
@@ -107,7 +107,7 @@ class PView{
                               int partition=-1);
   static PView *getViewByFileName(const std::string &name, int timeStep=-1,
                                   int partition=-1);
-  static PView *getViewByNum(int num, int timeStep=-1, int partition=-1);
+  static PView *getViewByTag(int tag, int timeStep=-1, int partition=-1);
 
   // IO read routines (these are global: they can create multiple
   // views)
diff --git a/Post/PViewData.h b/Post/PViewData.h
index 140beabaf2..6f72791de3 100644
--- a/Post/PViewData.h
+++ b/Post/PViewData.h
@@ -260,6 +260,8 @@ class PViewData {
   virtual bool writeMED(const std::string &fileName);
   virtual bool toVector(std::vector<std::vector<double> > &vec);
   virtual bool fromVector(const std::vector<std::vector<double> > &vec);
+  virtual void importLists(int N[24], std::vector<double> *V[24]);
+  virtual void getListPointers(int N[24], std::vector<double> *V[24]);
 };
 
 class nameData{
diff --git a/Post/PViewDataIO.cpp b/Post/PViewDataIO.cpp
index 12a21425dc..3048fb7281 100644
--- a/Post/PViewDataIO.cpp
+++ b/Post/PViewDataIO.cpp
@@ -243,3 +243,13 @@ bool PViewData::fromVector(const std::vector<std::vector<double> > &vec)
   }
   return true;
 }
+
+void PViewData::importLists(int N[24], std::vector<double> *V[24])
+{
+  Msg::Error("importLists not available for this view data type");
+}
+
+void PViewData::getListPointers(int N[24], std::vector<double> *V[24])
+{
+  Msg::Error("getListPointers not avalailable for this view data type");
+}
diff --git a/Post/PViewDataList.cpp b/Post/PViewDataList.cpp
index 351c2edce2..37e449ed98 100644
--- a/Post/PViewDataList.cpp
+++ b/Post/PViewDataList.cpp
@@ -960,25 +960,3 @@ std::vector<double> *PViewDataList::incrementList(int numComp, int type, int num
   return 0;
 }
 
-void PViewDataList::importLists(int N[24], std::vector<double> *V[24])
-{
-  for(int i = 0; i < 24; i++){
-    std::vector<double> *list = 0;
-    int *nbe = 0, nbc, nbn;
-    _getRawData(i, &list, &nbe, &nbc, &nbn);
-    *nbe = N[i];
-    *list = *V[i]; // deep copy
-  }
-  finalize();
-}
-
-void PViewDataList::getListPointers(int N[24], std::vector<double> *V[24])
-{
-  for(int i = 0; i < 24; i++){
-    std::vector<double> *list = 0;
-    int *nbe = 0, nbc, nbn;
-    _getRawData(i, &list, &nbe, &nbc, &nbn);
-    N[i] = *nbe;
-    V[i] = list; // copy pointer only
-  }
-}
diff --git a/Post/PViewDataList.h b/Post/PViewDataList.h
index b91c022f3f..cfb9f0a121 100644
--- a/Post/PViewDataList.h
+++ b/Post/PViewDataList.h
@@ -120,8 +120,6 @@ class PViewDataList : public PViewData {
   // specific to list-based data sets
   void setOrder2(int type);
   std::vector<double> *incrementList(int numComp, int type, int numNodes = 0);
-  void importLists(int N[24], std::vector<double> *V[24]);
-  void getListPointers(int N[24], std::vector<double> *V[24]);
 
   // I/O routines
   bool readPOS(FILE *fp, double version, bool binary);
@@ -129,6 +127,8 @@ class PViewDataList : public PViewData {
                 bool append=false);
   virtual bool writeMSH(const std::string &fileName, double version=2.2, bool binary=false,
                         bool savemesh=true, bool multipleView=false);
+  virtual void importLists(int N[24], std::vector<double> *V[24]);
+  virtual void getListPointers(int N[24], std::vector<double> *V[24]);
 };
 
 #endif
diff --git a/Post/PViewDataListIO.cpp b/Post/PViewDataListIO.cpp
index be49981d8c..3d2c4b9927 100644
--- a/Post/PViewDataListIO.cpp
+++ b/Post/PViewDataListIO.cpp
@@ -642,3 +642,26 @@ bool PViewDataList::writeMSH(const std::string &fileName, double version, bool b
   fclose(fp);
   return true;
 }
+
+void PViewDataList::importLists(int N[24], std::vector<double> *V[24])
+{
+  for(int i = 0; i < 24; i++){
+    std::vector<double> *list = 0;
+    int *nbe = 0, nbc, nbn;
+    _getRawData(i, &list, &nbe, &nbc, &nbn);
+    *nbe = N[i];
+    *list = *V[i]; // deep copy
+  }
+  finalize();
+}
+
+void PViewDataList::getListPointers(int N[24], std::vector<double> *V[24])
+{
+  for(int i = 0; i < 24; i++){
+    std::vector<double> *list = 0;
+    int *nbe = 0, nbc, nbn;
+    _getRawData(i, &list, &nbe, &nbc, &nbn);
+    N[i] = *nbe;
+    V[i] = list; // copy pointer only
+  }
+}
diff --git a/Post/PViewVertexArrays.cpp b/Post/PViewVertexArrays.cpp
index 54a13ff8c0..d40a862046 100644
--- a/Post/PViewVertexArrays.cpp
+++ b/Post/PViewVertexArrays.cpp
@@ -1435,22 +1435,22 @@ void PView::fillVertexArray(onelab::localNetworkClient *remote, int length,
                             const char *bytes, int swap)
 {
   std::string name;
-  int num, type, numSteps;
+  int tag, type, numSteps;
   double min, max, time, xmin, ymin, zmin, xmax, ymax, zmax;
-  if(!VertexArray::decodeHeader(length, bytes, swap, name, num, type, min, max,
+  if(!VertexArray::decodeHeader(length, bytes, swap, name, tag, type, min, max,
                                 numSteps, time, xmin, ymin, zmin, xmax, ymax, zmax))
     return;
 
-  Msg::Debug("Filling vertex array (type %d) in view num %d", type, num);
+  Msg::Debug("Filling vertex array (type %d) in view tag %d", type, tag);
 
   SBoundingBox3d bbox(xmin, ymin, zmin, xmax, ymax, zmax);
 
-  PView *p = PView::getViewByNum(num);
+  PView *p = PView::getViewByTag(tag);
   if(!p){
-    Msg::Info("View num %d does not exist: creating new view", num);
+    Msg::Info("View tag %d does not exist: creating new view", tag);
     PViewData *data = new PViewDataRemote(remote, min, max, numSteps, time, bbox);
     data->setName(name + " (remote)");
-    p = new PView(data, num);
+    p = new PView(data, tag);
     SetBoundingBox();
   }
   else{
-- 
GitLab