From 5d079489ff814ca13008a46e642c5ead4c2463cc Mon Sep 17 00:00:00 2001
From: Gaetan Bricteux <gaetan.bricteux@uclouvain.be>
Date: Wed, 2 Jun 2010 09:59:38 +0000
Subject: [PATCH] display scalar polygon

---
 Geo/GFace.cpp                 |  2 ++
 Geo/GModelIO_Mesh.cpp         | 55 +++++++++++++++++------------------
 Geo/MElement.cpp              | 18 +++++++-----
 Geo/MElement.h                |  3 +-
 Geo/MElementCut.cpp           | 46 +++++++++++++++++++++--------
 Post/PViewVertexArrays.cpp    |  8 ++---
 contrib/mpeg_encode/param.cpp |  4 +--
 7 files changed, 82 insertions(+), 54 deletions(-)

diff --git a/Geo/GFace.cpp b/Geo/GFace.cpp
index 8271e449cc..bd895b65ba 100644
--- a/Geo/GFace.cpp
+++ b/Geo/GFace.cpp
@@ -1133,8 +1133,10 @@ bool GFace::fillPointCloud(double maxDist, std::vector<SPoint3> *points,
 
 void GFace::lloyd(int nbiter, int infn)
 {
+#if defined(HAVE_MESH)
   lloydAlgorithm algo(nbiter, infn);
   algo(this);
+#endif
 }
 
 void GFace::replaceEdges (std::list<GEdge*> &new_edges)
diff --git a/Geo/GModelIO_Mesh.cpp b/Geo/GModelIO_Mesh.cpp
index 956a7f85bb..9182b5ecc0 100644
--- a/Geo/GModelIO_Mesh.cpp
+++ b/Geo/GModelIO_Mesh.cpp
@@ -83,10 +83,11 @@ static bool getVertices(int num, int *indices, std::vector<MVertex*> &vec,
 static MElement *createElementMSH(GModel *m, int num, int typeMSH, int physical,
                                   int reg, int part, std::vector<MVertex*> &v,
                                   std::map<int, std::vector<MElement*> > elements[10],
-                                  std::map<int, std::map<int, std::string> > physicals[4])
+                                  std::map<int, std::map<int, std::string> > physicals[4],
+                                  bool owner=false, MElement *parent=0)
 {
   MElementFactory factory;
-  MElement *e = factory.create(typeMSH, v, num, part);
+  MElement *e = factory.create(typeMSH, v, num, part, owner, parent);
 
   if(!e){
     Msg::Error("Unknown type of element %d", typeMSH);
@@ -140,21 +141,19 @@ static MElement *getParent(int parentNum, std::map<int, std::vector<MElement*> >
   return NULL;
 }
 
-static MElement *getParent(int parentNum, int dim,
+static MElement *getParent(int parentNum, int type,
                            std::map<int, std::vector<MElement*> > elements[10])
 {
   MElement *parent = NULL;
-  switch(dim){
-  case 0 :
-    return getParent(parentNum, elements[0]);
-  case 1 :
+  switch(type){
+  case MSH_LIN_C :
     return getParent(parentNum, elements[1]);
-  case 2 :
+  case MSH_POLYG_ :
     if((parent = getParent(parentNum, elements[2]))) return parent;
     if((parent = getParent(parentNum, elements[3]))) return parent;
     if((parent = getParent(parentNum, elements[8]))) return parent;
     return parent;
-  case 3 :
+  case MSH_POLYH_ :
     if((parent = getParent(parentNum, elements[4]))) return parent;
     if((parent = getParent(parentNum, elements[5]))) return parent;
     if((parent = getParent(parentNum, elements[6]))) return parent;
@@ -380,25 +379,25 @@ int GModel::readMSH(const std::string &name)
           else{
             if(!getVertices(numVertices, indices, vertexMap, vertices)) return 0;
           }
-          MElement *e = createElementMSH(this, num, type, physical, elementary,
-                                         partition, vertices, elements, physicals);
-          for(unsigned int j = 0; j < ghosts.size(); j++)
-            _ghostCells.insert(std::pair<MElement*, short>(e, ghosts[j]));
+          MElement *p = 0;
+          bool own = false;
           if(parent) {
-            MElement *p = 0;
             if(parents.find(parent) == parents.end()){
-              p = getParent(parent, e->getDim(), elements);
+              p = getParent(parent, type, elements);
               if(p) parents[parent] = p;
               else Msg::Error("Parent element %d not found", parent);
             }
             else p = parents.find(parent)->second;
             if(parentsOwned.find(p) == parentsOwned.end()) {
-              e->setParent(p, true);
+              own = true;
               parentsOwned.insert(p);
             }
-            else 
-              e->setParent(p, false);
           }
+          MElement *e = createElementMSH(this, num, type, physical, elementary,
+                                         partition, vertices, elements, physicals,
+                                         own, p);
+          for(unsigned int j = 0; j < ghosts.size(); j++)
+            _ghostCells.insert(std::pair<MElement*, short>(e, ghosts[j]));
           if(numElements > 100000)
             Msg::ProgressMeter(i + 1, numElements, "Reading elements");
           delete [] indices;
@@ -437,26 +436,26 @@ int GModel::readMSH(const std::string &name)
             else{
               if(!getVertices(numVertices, indices, vertexMap, vertices)) return 0;
             }
-            MElement *e = createElementMSH(this, num, type, physical, elementary,
-                                           partition, vertices, elements, physicals);
-            if(numPartitions > 1)
-              for(int j = 0; j < numPartitions - 1; j++)
-                _ghostCells.insert(std::pair<MElement*, short>(e, -data[5 + j]));
+            MElement *p = 0;
+            bool own = false;
             if(parent) {
-              MElement *p = 0;
               if(parents.find(parent) == parents.end()){
-                p = getParent(parent, e->getDim(), elements);
+                p = getParent(parent, type, elements);
                 if(p) parents[parent] = p;
                 else Msg::Error("Parent element %d not found", parent);
               }
               else p = parents.find(parent)->second;
               if(parentsOwned.find(p) == parentsOwned.end()) {
-                e->setParent(p, true);
+                own = true;
                 parentsOwned.insert(p);
               }
-              else 
-                e->setParent(p, false);
             }
+            MElement *e = createElementMSH(this, num, type, physical, elementary,
+                                           partition, vertices, elements, physicals,
+                                           own, p);
+            if(numPartitions > 1)
+              for(int j = 0; j < numPartitions - 1; j++)
+                _ghostCells.insert(std::pair<MElement*, short>(e, -data[5 + j]));
             if(numElements > 100000)
               Msg::ProgressMeter(numElementsPartial + i + 1, numElements,
                                  "Reading elements");
diff --git a/Geo/MElement.cpp b/Geo/MElement.cpp
index 2ab32d7323..5d0fdc7ef7 100644
--- a/Geo/MElement.cpp
+++ b/Geo/MElement.cpp
@@ -904,8 +904,8 @@ MElement *MElement::copy(int &num, std::map<int, MVertex*> &vertexMap,
       }
     }
   }
-  MElementFactory factory;
-  MElement *newEl = factory.create(eType, vmv, getNum(), _partition);
+
+  MElement *parent=0;
   if(eParent && !getDomain(0) && !getDomain(1)) {
     std::map<MElement*, MElement*>::iterator it = newParents.find(eParent);
     MElement *newParent;
@@ -915,8 +915,12 @@ MElement *MElement::copy(int &num, std::map<int, MVertex*> &vertexMap,
     }
     else
       newParent = it->second;
-    newEl->setParent(newParent, ownsParent());
+    parent = newParent;
   }
+  
+  MElementFactory factory;
+  MElement *newEl = factory.create(eType, vmv, getNum(), _partition, ownsParent(), parent);
+
   for(int i = 0; i < 2; i++) {
     MElement *dom = getDomain(i);
     if(!dom) continue;
@@ -934,7 +938,7 @@ MElement *MElement::copy(int &num, std::map<int, MVertex*> &vertexMap,
 }
 
 MElement *MElementFactory::create(int type, std::vector<MVertex*> &v,
-                                  int num, int part)
+                                  int num, int part, bool owner, MElement *parent)
 {
   switch (type) {
   case MSH_PNT:    return new MPoint(v, num, part);
@@ -949,7 +953,7 @@ MElement *MElementFactory::create(int type, std::vector<MVertex*> &v,
   case MSH_LIN_10: return new MLineN(v, num, part);
   case MSH_LIN_11: return new MLineN(v, num, part);
   case MSH_LIN_B:  return new MLineBorder(v, num, part);
-  case MSH_LIN_C:  return new MLineChild(v, num, part);
+  case MSH_LIN_C:  return new MLineChild(v, num, part, owner, parent);
   case MSH_TRI_3:  return new MTriangle(v, num, part);
   case MSH_TRI_6:  return new MTriangle6(v, num, part);
   case MSH_TRI_9:  return new MTriangleN(v, 3, num, part);
@@ -975,7 +979,7 @@ MElement *MElementFactory::create(int type, std::vector<MVertex*> &v,
   case MSH_QUA_81: return new MQuadrangleN(v, 8, num, part);
   case MSH_QUA_100:return new MQuadrangleN(v, 9, num, part);
   case MSH_QUA_121:return new MQuadrangleN(v, 10, num, part);
-  case MSH_POLYG_: return new MPolygon(v, num, part);
+  case MSH_POLYG_: return new MPolygon(v, num, part, owner, parent);
   case MSH_POLYG_B:return new MPolygonBorder(v, num, part);
   case MSH_TET_4:  return new MTetrahedron(v, num, part);
   case MSH_TET_10: return new MTetrahedron10(v, num, part);
@@ -993,7 +997,7 @@ MElement *MElementFactory::create(int type, std::vector<MVertex*> &v,
   case MSH_TET_35: return new MTetrahedronN(v, 4, num, part);
   case MSH_TET_52: return new MTetrahedronN(v, 5, num, part);
   case MSH_TET_56: return new MTetrahedronN(v, 5, num, part);
-  case MSH_POLYH_: return new MPolyhedron(v, num, part);
+  case MSH_POLYH_: return new MPolyhedron(v, num, part, owner, parent);
   default:         return 0;
   }
 }
diff --git a/Geo/MElement.h b/Geo/MElement.h
index ff1165a37c..1d392e94d8 100644
--- a/Geo/MElement.h
+++ b/Geo/MElement.h
@@ -315,7 +315,8 @@ class MElementLessThanLexicographic{
 
 class MElementFactory{
  public:
-  MElement *create(int type, std::vector<MVertex*> &v, int num=0, int part=0);
+  MElement *create(int type, std::vector<MVertex*> &v, int num=0, int part=0,
+                   bool owner=false, MElement *parent=0);
 };
 
 // Traits of various elements based on the dimension.  These generally define
diff --git a/Geo/MElementCut.cpp b/Geo/MElementCut.cpp
index dedd10a21a..fe08598747 100644
--- a/Geo/MElementCut.cpp
+++ b/Geo/MElementCut.cpp
@@ -153,30 +153,52 @@ void MPolygon::_initVertices()
 {
   if(_parts.size() == 0) return;
 
+  // reorient the parts 
+  SVector3 n;
+  if(_orig) n = _orig->getFace(0).normal();
+  else n = _parts[0]->getFace(0).normal();
+  for(unsigned int i = 0; i < _parts.size(); i++) {
+    SVector3 ni = _parts[i]->getFace(0).normal();
+    if(dot(n, ni) < 0.) _parts[i]->revert();
+  }
+
+  std::vector<MEdge> edg;
   for(unsigned int i = 0; i < _parts.size(); i++) {
     for(int j = 0; j < 3; j++) {
       int k;
-      for(k = _edges.size() - 1; k >= 0; k--)
-        if(_parts[i]->getEdge(j) == _edges[k])
+      for(k = edg.size() - 1; k >= 0; k--)
+        if(_parts[i]->getEdge(j) == edg[k])
           break;
       if(k < 0)
-        _edges.push_back(_parts[i]->getEdge(j));
+        edg.push_back(_parts[i]->getEdge(j));
       else
-        _edges.erase(_edges.begin() + k);
+        edg.erase(edg.begin() + k);
     }
   }
 
-  for(unsigned int i = 0; i < _edges.size(); i++) {
-    for(int j = 0; j < 2; j++) {
-      int k;
-      for(k = _vertices.size() - 1; k >= 0; k--)
-        if(_edges[i].getVertex(j) == _vertices[k])
-          break;
-      if(k < 0)
-        _vertices.push_back(_edges[i].getVertex(j));
+  // organize edges to get vertices in rotating order
+  _edges.push_back(edg[0]);
+  edg.erase(edg.begin());
+  while(edg.size()) {
+    for(unsigned int i = 0; i < edg.size(); i++) {
+      MVertex *v = _edges[_edges.size() - 1].getVertex(1);
+      if(edg[i].getVertex(0) == v) {
+        _edges.push_back(edg[i]);
+        edg.erase(edg.begin() + i);
+        break;
+      }
+      if(edg[i].getVertex(1) == v) {
+        _edges.push_back(MEdge(edg[i].getVertex(1), edg[i].getVertex(0)));
+        edg.erase(edg.begin() + i);
+        break;
+      }
     }
   }
 
+  for(unsigned int i = 0; i < _edges.size(); i++) {
+    _vertices.push_back(_edges[i].getVertex(0));
+  }
+
   // innerVertices
   for(unsigned int i = 0; i < _parts.size(); i++) {
     for(int j = 0; j < 3; j++) {
diff --git a/Post/PViewVertexArrays.cpp b/Post/PViewVertexArrays.cpp
index 6a1bdbb23d..401ae4d184 100644
--- a/Post/PViewVertexArrays.cpp
+++ b/Post/PViewVertexArrays.cpp
@@ -621,7 +621,7 @@ static void addScalarPolygon(PView *p, double xyz[PVIEW_NMAX][3],
                              double val[PVIEW_NMAX][9], bool pre, int numNodes)
 {
   if(numNodes == 3) addScalarTriangle(p, xyz, val, pre);
-  if(numNodes == 4) {addScalarQuadrangle(p, xyz, val, pre, 0, 1, 2, 3); addScalarQuadrangle(p, xyz, val, pre, 1, 2, 3, 0);}
+  if(numNodes == 4) addScalarQuadrangle(p, xyz, val, pre);
   
 }
 
@@ -853,7 +853,7 @@ static void addVectorElement(PView *p, int ient, int iele, int numNodes,
     double min = opt->tmpMin, max = opt->tmpMax;
     opt->tmpMin = opt->externalMin;
     opt->tmpMax = opt->externalMax;
-    addScalarElement(p, type, xyz, val2, pre);
+    addScalarElement(p, type, xyz, val2, pre, numNodes);
     opt->tmpMin = min;
     opt->tmpMax = max;
 
@@ -948,7 +948,7 @@ static void addTensorElement(PView *p, int numNodes, int type,
   if(opt->tensorType == PViewOptions::VonMises){
     for(int i = 0; i < numNodes; i++)
       val[i][0] = ComputeVonMises(val[i]);
-    addScalarElement(p, type, xyz, val, pre);
+    addScalarElement(p, type, xyz, val, pre, numNodes);
   }
 }
 
@@ -1013,7 +1013,7 @@ static void addElementsInArrays(PView *p, bool preprocessNormalsOnly)
         opt->tmpBBox += SPoint3(xyz[j][0], xyz[j][1], xyz[j][2]);
 
       if(opt->showElement && !data->useGaussPoints()) 
-        addOutlineElement(p, type, xyz, preprocessNormalsOnly);
+        addOutlineElement(p, type, xyz, preprocessNormalsOnly, numNodes);
       
       if(opt->intervalsType != PViewOptions::Numeric){
         if(data->useGaussPoints()){
diff --git a/contrib/mpeg_encode/param.cpp b/contrib/mpeg_encode/param.cpp
index c4a3785719..e7cf412c30 100644
--- a/contrib/mpeg_encode/param.cpp
+++ b/contrib/mpeg_encode/param.cpp
@@ -529,7 +529,7 @@ ReadParamFile(char *fileName,
         optionSeen[OPTION_IO_CONVERT] = TRUE;
       } else if ( strncmp(input, "IQTABLE", 7) == 0 ) {
         for ( row = 0; row < 8; row ++ ) {
-          fgets(input, 256, fpointer);
+          if(!fgets(input, 256, fpointer)) return FALSE;
           charPtr = input;
           if (8!=sscanf(charPtr,"%d %d %d %d %d %d %d %d",
                         &qtable[row*8+0],  &qtable[row*8+1],
@@ -566,7 +566,7 @@ ReadParamFile(char *fileName,
     case 'N':
       if ( strncmp(input, "NIQTABLE", 8) == 0 ) {
         for ( row = 0; row < 8; row ++ ) {
-          fgets(input, 256, fpointer);
+          if(!fgets(input, 256, fpointer)) return FALSE;
           charPtr = input;
           if (8!=sscanf(charPtr,"%d %d %d %d %d %d %d %d",
                         &niqtable[row*8+0],  &niqtable[row*8+1],
-- 
GitLab