From 18f684476a4530fd987e3899a212fe337b88531e Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Thu, 17 Aug 2006 03:22:22 +0000
Subject: [PATCH] *** empty log message ***

---
 Common/Options.cpp  |  41 ++++++++++++++----
 Fltk/Callbacks.cpp  |   4 +-
 Geo/GModel.h        |   1 +
 Geo/GModelIO.cpp    | 101 ++++++++++++++++++++++++++++++++++++++++----
 Graphics/Mesh.cpp   |   3 +-
 Parser/OpenFile.cpp |   9 +++-
 6 files changed, 136 insertions(+), 23 deletions(-)

diff --git a/Common/Options.cpp b/Common/Options.cpp
index 25659af079..84cb03ce4e 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -1,4 +1,4 @@
-// $Id: Options.cpp,v 1.293 2006-08-17 00:25:00 geuzaine Exp $
+// $Id: Options.cpp,v 1.294 2006-08-17 03:22:21 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -4804,7 +4804,7 @@ double opt_mesh_color_carousel(OPT_ARGS_NUM)
 {
   if(action & GMSH_SET) {
     // vertex arrays need to be regenerated only when we color by
-    // partition or by element type
+    // element type or by partition
     if(CTX.mesh.color_carousel != (int)val && (val == 0. || val == 3.))
       CTX.mesh.changed = 1;
     CTX.mesh.color_carousel = (int)val;
@@ -7014,6 +7014,10 @@ unsigned int opt_mesh_color_points(OPT_ARGS_COL)
 unsigned int opt_mesh_color_lines(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.line != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.line = val;
   }
 #if defined(HAVE_FLTK)
@@ -7025,6 +7029,10 @@ unsigned int opt_mesh_color_lines(OPT_ARGS_COL)
 unsigned int opt_mesh_color_triangles(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.triangle != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.triangle = val;
   }
 #if defined(HAVE_FLTK)
@@ -7036,6 +7044,10 @@ unsigned int opt_mesh_color_triangles(OPT_ARGS_COL)
 unsigned int opt_mesh_color_quadrangles(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.quadrangle != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.quadrangle = val;
   }
 #if defined(HAVE_FLTK)
@@ -7047,7 +7059,10 @@ unsigned int opt_mesh_color_quadrangles(OPT_ARGS_COL)
 unsigned int opt_mesh_color_tetrahedra(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
-    if(CTX.color.mesh.tetrahedron != val) CTX.mesh.changed = 1;
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.tetrahedron != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.tetrahedron = val;
   }
 #if defined(HAVE_FLTK)
@@ -7059,7 +7074,10 @@ unsigned int opt_mesh_color_tetrahedra(OPT_ARGS_COL)
 unsigned int opt_mesh_color_hexahedra(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
-    if(CTX.color.mesh.hexahedron != val) CTX.mesh.changed = 1;
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.hexahedron != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.hexahedron = val;
   }
 #if defined(HAVE_FLTK)
@@ -7071,7 +7089,10 @@ unsigned int opt_mesh_color_hexahedra(OPT_ARGS_COL)
 unsigned int opt_mesh_color_prisms(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
-    if(CTX.color.mesh.prism != val) CTX.mesh.changed = 1;
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.prism != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.prism = val;
   }
 #if defined(HAVE_FLTK)
@@ -7083,7 +7104,10 @@ unsigned int opt_mesh_color_prisms(OPT_ARGS_COL)
 unsigned int opt_mesh_color_pyramid(OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
-    if(CTX.color.mesh.pyramid != val) CTX.mesh.changed = 1;
+    // vertex arrays need to be regenerated only when we color by
+    // element type
+    if(CTX.color.mesh.pyramid != val && CTX.mesh.color_carousel == 0)
+      CTX.mesh.changed = 1;
     CTX.color.mesh.pyramid = val;
   }
 #if defined(HAVE_FLTK)
@@ -7118,9 +7142,8 @@ unsigned int opt_mesh_color_(int i, OPT_ARGS_COL)
 {
   if(action & GMSH_SET) {
     // vertex arrays need to be regenerated only when we color by
-    // partition or by element type
-    if(CTX.color.mesh.carousel[i] != val && 
-       (CTX.mesh.color_carousel == 0 || CTX.mesh.color_carousel == 3))
+    // partition
+    if(CTX.color.mesh.carousel[i] != val && CTX.mesh.color_carousel == 3)
       CTX.mesh.changed = 1;
     CTX.color.mesh.carousel[i] = val;
   }
diff --git a/Fltk/Callbacks.cpp b/Fltk/Callbacks.cpp
index c3f0ded01d..b969f3d3ee 100644
--- a/Fltk/Callbacks.cpp
+++ b/Fltk/Callbacks.cpp
@@ -1,4 +1,4 @@
-// $Id: Callbacks.cpp,v 1.433 2006-08-16 21:55:26 geuzaine Exp $
+// $Id: Callbacks.cpp,v 1.434 2006-08-17 03:22:21 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -788,7 +788,7 @@ void file_save_as_cb(CALLBACK_ARGS)
     {"I-DEAS universal mesh (*.unv)", _save_unv},
     {"INRIA mesh (*.mesh)", _save_mesh},
     {"STL surface mesh (*.stl)", _save_stl},
-    {"VRML surface mesh (*.wrl)", _save_vrml},
+    {"VRML 1.0 surface mesh (*.wrl)", _save_vrml},
     {"   ", _save_auto},
     {"Encapsulated PostScript (*.eps)", _save_eps},
     {"GIF (*.gif)", _save_gif},
diff --git a/Geo/GModel.h b/Geo/GModel.h
index 69079ac946..d9987ed3ac 100644
--- a/Geo/GModel.h
+++ b/Geo/GModel.h
@@ -114,6 +114,7 @@ class GModel
   int writePOS(const std::string &name, double scalingFactor=1.0);
   int readSTL(const std::string &name, double tolerance=1.e-3);
   int writeSTL(const std::string &name, double scalingFactor=1.0);
+  int readVRML(const std::string &name);
   int writeVRML(const std::string &name, double scalingFactor=1.0);
   int writeUNV(const std::string &name, double scalingFactor=1.0);
   int readMESH(const std::string &name);
diff --git a/Geo/GModelIO.cpp b/Geo/GModelIO.cpp
index 8dbd5040a7..b7811ef68e 100644
--- a/Geo/GModelIO.cpp
+++ b/Geo/GModelIO.cpp
@@ -1,4 +1,4 @@
-// $Id: GModelIO.cpp,v 1.15 2006-08-15 21:22:12 geuzaine Exp $
+// $Id: GModelIO.cpp,v 1.16 2006-08-17 03:22:22 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -737,6 +737,89 @@ int GModel::writeSTL(const std::string &name, double scalingFactor)
   return 1;
 }
 
+static int skipUntil(FILE *fp, char *key)
+{
+  char buffer[256], str[256];
+  if(!fgets(buffer, sizeof(buffer), fp)) return 0;
+  sscanf(buffer, "%s", str);
+  while(strcmp(str, key)){
+    if(!fgets(buffer, sizeof(buffer), fp)) return 0;
+    sscanf(buffer, "%s", str);
+  }
+  return 1;
+}
+
+int GModel::readVRML(const std::string &name)
+{
+  FILE *fp = fopen(name.c_str(), "r");
+  if(!fp){
+    Msg(GERROR, "Unable to open file '%s'", name.c_str());
+    return 0;
+  }
+
+  // Warning: this is by NO means a real VRML/Inventor parser: it just
+  // reads the special bunch of files I have to work with.
+
+  std::map<int, MVertex*> vertices;
+  std::map<int, std::vector<MElement*> > triangles;
+  int region = 0;
+  char buffer[256], str[256];
+  while(!feof(fp)) {
+    if(!fgets(buffer, sizeof(buffer), fp)) break;
+    if(buffer[0] != '#'){ // skip comments
+      sscanf(buffer, "%s", str);
+      if(!strcmp(str, "IndexedTriangleStripSet")){
+	region++;
+	Msg(INFO, "IndexedTriangleStripSet");
+	if(!skipUntil(fp, "vertex")) break;
+	int num = 0;
+	double x, y, z;
+	if(!fgets(buffer, sizeof(buffer), fp)) break;
+	while(sscanf(buffer, "%lf %lf %lf", &x, &y, &z)){
+	  vertices[num++] = new MVertex(x, y, z);
+	  if(!fgets(buffer, sizeof(buffer), fp)) break;
+	}
+	Msg(INFO, "%d vertices", vertices.size());
+	if(!skipUntil(fp, "coordIndex")) break;
+	int index;
+	if(!fscanf(fp, "%d", &index)) break;
+	std::vector<int> indices;
+	while(fscanf(fp, " , %d", &index)){
+	  if(index == -1){
+	    if(indices.size() < 3){
+	      Msg(GERROR, "Less than 3 indices in VRML triangle strip");
+	      break;
+	    }
+	    for(unsigned int i = 2; i < indices.size(); i++){
+	      triangles[region].push_back(new MTriangle(vertices[indices[i-2]], 
+							vertices[indices[i-1]], 
+							vertices[indices[i]]));
+	    }
+	    indices.clear();
+	  }
+	  else{
+	    indices.push_back(index);
+	  }
+	}
+	if(indices.size()){
+	  Msg(GERROR, "End of VRML triangle strip not found");
+	}
+	Msg(INFO, "%d triangles", triangles[region].size());
+      }
+      else{
+	// just ignore 
+      }
+    }
+  }
+
+  storeElementsInEntities(this, TRI1, triangles);
+  associateEntityWithVertices(this);
+  storeVerticesInEntities(vertices);
+
+  fclose(fp);
+  return 1;
+}
+
 int GModel::writeVRML(const std::string &name, double scalingFactor)
 {
   FILE *fp = fopen(name.c_str(), "w");
@@ -933,19 +1016,19 @@ int GModel::readMESH(const std::string &name)
   std::map<int, std::vector<MElement*> > elements[2];
 
   while(!feof(fp)) {
-    fgets(buffer, sizeof(buffer), fp);
+    if(!fgets(buffer, sizeof(buffer), fp)) break;
     if(buffer[0] != '#'){ // skip comments
       sscanf(buffer, "%s", str);
       if(!strcmp(str, "Dimension")){
-	fgets(buffer, sizeof(buffer), fp);
+	if(!fgets(buffer, sizeof(buffer), fp)) break;
       }
       else if(!strcmp(str, "Vertices")){
-	fgets(buffer, sizeof(buffer), fp);
+	if(!fgets(buffer, sizeof(buffer), fp)) break;
 	int nbv;
 	sscanf(buffer, "%d", &nbv);
 	Msg(INFO, "%d vertices", nbv);
 	for(int i = 0; i < nbv; i++) {
-	  fgets(buffer, sizeof(buffer), fp);
+	  if(!fgets(buffer, sizeof(buffer), fp)) break;
 	  int cl;
 	  double x, y, z;
 	  sscanf(buffer, "%lf %lf %lf %d", &x, &y, &z, &cl);
@@ -953,12 +1036,12 @@ int GModel::readMESH(const std::string &name)
 	}
       }
       else if(!strcmp(str, "Triangles")){
-	fgets(buffer, sizeof(buffer), fp);
+	if(!fgets(buffer, sizeof(buffer), fp)) break;
 	int nbt;
 	sscanf(buffer, "%d", &nbt);
 	Msg(INFO, "%d triangles", nbt);
 	for(int i = 0; i < nbt; i++) {
-	  fgets(buffer, sizeof(buffer), fp);
+	  if(!fgets(buffer, sizeof(buffer), fp)) break;
 	  int n1, n2, n3, cl;
 	  sscanf(buffer, "%d %d %d %d", &n1, &n2, &n3, &cl);
 	  elements[0][cl].push_back
@@ -966,12 +1049,12 @@ int GModel::readMESH(const std::string &name)
 	}
       }
       else if(!strcmp(str, "Quadrilaterals")) {
-	fgets(buffer, sizeof(buffer), fp);
+	if(!fgets(buffer, sizeof(buffer), fp)) break;
 	int nbq;
 	sscanf(buffer, "%d", &nbq);
 	Msg(INFO, "%d quadrangles", nbq);
 	for(int i = 0; i < nbq; i++) {
-	  fgets(buffer, sizeof(buffer), fp);
+	  if(!fgets(buffer, sizeof(buffer), fp)) break;
 	  int n1, n2, n3, n4, cl;
 	  sscanf(buffer, "%d %d %d %d %d", &n1, &n2, &n3, &n4, &cl);
 	  elements[1][cl].push_back
diff --git a/Graphics/Mesh.cpp b/Graphics/Mesh.cpp
index 49cfbcbff3..30df83886a 100644
--- a/Graphics/Mesh.cpp
+++ b/Graphics/Mesh.cpp
@@ -1,4 +1,4 @@
-// $Id: Mesh.cpp,v 1.173 2006-08-17 00:25:01 geuzaine Exp $
+// $Id: Mesh.cpp,v 1.174 2006-08-17 03:22:22 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -810,6 +810,7 @@ void Draw_Mesh()
     CTX.threads_lock = 1; 
     int status = GMODEL->getMeshStatus();
     if(CTX.mesh.changed) {
+      Msg(DEBUG, "Mesh has changed: reinitializing drawing data");
       if(status >= 1)
 	std::for_each(GMODEL->firstEdge(), GMODEL->lastEdge(), initMeshGEdge());
       if(status >= 2){
diff --git a/Parser/OpenFile.cpp b/Parser/OpenFile.cpp
index afeab35ca4..6ab4dbbf02 100644
--- a/Parser/OpenFile.cpp
+++ b/Parser/OpenFile.cpp
@@ -1,4 +1,4 @@
-// $Id: OpenFile.cpp,v 1.112 2006-08-15 06:16:43 geuzaine Exp $
+// $Id: OpenFile.cpp,v 1.113 2006-08-17 03:22:22 geuzaine Exp $
 //
 // Copyright (C) 1997-2006 C. Geuzaine, J.-F. Remacle
 //
@@ -285,7 +285,12 @@ int MergeProblem(char *name, int warn_if_missing)
   if(!strcmp(ext, ".stl") || !strcmp(ext, ".STL")){
     status = GMODEL->readSTL(name, CTX.mesh.stl_distance_tol);
   }
-  else if(!strcmp(ext, ".mesh")){
+  else if(!strcmp(ext, ".wrl") || !strcmp(ext, ".WRL") || 
+	  !strcmp(ext, ".vrml") || !strcmp(ext, ".VRML") ||
+	  !strcmp(ext, ".iv") || !strcmp(ext, ".IV")){
+    status = GMODEL->readVRML(name);
+  }
+  else if(!strcmp(ext, ".mesh") || !strcmp(ext, ".MESH")){
     status = GMODEL->readMESH(name);
   }
 #if defined(HAVE_FLTK)
-- 
GitLab