From 1d500dc5dfdba257a2d60f2847985994b9a21f54 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Tue, 11 Sep 2007 13:54:35 +0000
Subject: [PATCH] - fixed 2d time plots - better 2d space plots when absissa
 not monotone

---
 Graphics/Graph2D.cpp | 104 ++++++++++++++++++++++++++-----------------
 Graphics/Post.cpp    |   7 +--
 2 files changed, 64 insertions(+), 47 deletions(-)

diff --git a/Graphics/Graph2D.cpp b/Graphics/Graph2D.cpp
index d64f933eff..c9c47d1e36 100644
--- a/Graphics/Graph2D.cpp
+++ b/Graphics/Graph2D.cpp
@@ -1,4 +1,4 @@
-// $Id: Graph2D.cpp,v 1.65 2007-09-10 04:47:03 geuzaine Exp $
+// $Id: Graph2D.cpp,v 1.66 2007-09-11 13:54:34 geuzaine Exp $
 //
 // Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 //
@@ -65,8 +65,7 @@ void Draw_Text2D()
 }
 
 static bool getGraphData(PView *p, std::vector<double> &x, double &xmin, 
-			 double &xmax, std::vector<std::vector<double> > &y, 
-			 double &ymin, double ymax) 
+			 double &xmax, std::vector<std::vector<double> > &y) 
 {
   PViewData *data = p->getData();
   PViewOptions *opt = p->getOptions();
@@ -84,13 +83,15 @@ static bool getGraphData(PView *p, std::vector<double> &x, double &xmin,
   if(!numy) return false;
   y.resize(numy);
 
+  bool space = (opt->Type == PViewOptions::Plot2DSpace);
+
+  numy = 0;
   for(int i = 0; i < data->getNumElements(); i++){
     int dim = data->getDimension(i);
     if(dim < 2){
       int numNodes = data->getNumNodes(i);
       int numComp = data->getNumComponents(i);
-      for(int ts = 0; ts < data->getNumTimeSteps(); ts++){
-	if(opt->Type == PViewOptions::Plot2DSpace) ts = opt->TimeStep;
+      for(int ts = space ? opt->TimeStep : 0; ts < opt->TimeStep + 1; ts++){
 	for(int j = 0; j < numNodes; j++){
 	  double val[9], xyz[3];
 	  data->getNode(i, j, xyz[0], xyz[1], xyz[2]);
@@ -98,33 +99,47 @@ static bool getGraphData(PView *p, std::vector<double> &x, double &xmin,
 	    data->getValue(i, j, k, ts, val[k]);
 	  double vx = ComputeScalarRep(3, xyz);
 	  double vy = ComputeScalarRep(numComp, val);
-	  if(opt->Type == PViewOptions::Plot2DTime){
-	    x.push_back(ts);
-	    y[i].push_back(vy);
-	  }
-	  else if(opt->Type == PViewOptions::Plot2DSpace){
+	  if(space){
 	    x.push_back(vx);
 	    y[0].push_back(vy);
 	  }
+	  else{
+	    if(!numy) x.push_back(ts);
+	    y[numy].push_back(vy);
+	  }
 	}
-	if(opt->Type == PViewOptions::Plot2DSpace) break;
       }
+      numy++;
     }
   }
 
   if(x.empty()) return false;
 
-  xmin = xmax = x[0];
-  ymin = ymax = y[0][0];
-  for(unsigned int i = 1; i < x.size(); i++){
-    xmin = std::min(xmin, x[i]);
-    xmax = std::max(xmax, x[i]);
-    for(unsigned int j = 0; j < y.size(); j++){
-      ymin = std::min(ymin, y[j][i]);
-      ymax = std::max(ymax, y[j][i]);
+  if(space){
+    bool monotone = true;
+    for(unsigned int i = 1; i < x.size(); i++){
+      if(x[i] < x[i - 1]){
+	monotone = false;
+	break;
+      }
+    }
+    if(monotone){ // use the "coordinate"
+      xmin = xmax = x[0];
+      for(unsigned int i = 1; i < x.size(); i++){
+	xmin = std::min(xmin, x[i]);
+	xmax = std::max(xmax, x[i]);
+      }
+    }
+    else{ // just use an index
+      for(unsigned int i = 0; i < x.size(); i++) x[i] = i;
+      xmin = 0;
+      xmax = x.size() - 1;
     }
   }
-
+  else{
+    xmin = 0;
+    xmax = data->getNumTimeSteps() - 1;
+  }
   return true;
 }
 
@@ -155,7 +170,7 @@ static void drawGraphAxes(PView *p, double xleft, double ytop, double width,
   glVertex2d(xleft, ytop);
   glVertex2d(xleft, ytop - height);
   glVertex2d(xleft + width, ytop - height);
-  if(opt->Axes > 1) {
+  if(opt->Axes > 1){
     glVertex2d(xleft + width, ytop);
     glVertex2d(xleft, ytop);
   }
@@ -197,12 +212,12 @@ static void drawGraphAxes(PView *p, double xleft, double ytop, double width,
 	glBegin(GL_LINES);
 	glVertex2d(xleft, ytop - i * dy);
 	glVertex2d(xleft + tic, ytop - i * dy);
-	if(opt->Axes > 1) {
+	if(opt->Axes > 1){
 	  glVertex2d(xleft + width - tic, ytop - i * dy);
 	  glVertex2d(xleft + width, ytop - i * dy);
 	}
 	glEnd();
-	if(opt->Axes > 2 && i != 0 && i != nb) {
+	if(opt->Axes > 2 && i != 0 && i != nb){
 	  glEnable(GL_LINE_STIPPLE);
 	  glLineStipple(1, 0x1111);
 	  gl2psEnable(GL2PS_LINE_STIPPLE);
@@ -216,7 +231,7 @@ static void drawGraphAxes(PView *p, double xleft, double ytop, double width,
 	  gl2psLineWidth(CTX.line_width * CTX.print.eps_line_width_factor);
 	}
       }
-      if(opt->ShowScale) {
+      if(opt->ShowScale){
 	sprintf(label, opt->Format, (i == nb) ? opt->TmpMin : (opt->TmpMax - i * dv));
 	glRasterPos2d(xleft - 2 * tic, ytop - i * dy - font_a / 3.);
 	Draw_String_Right(label);
@@ -227,7 +242,7 @@ static void drawGraphAxes(PView *p, double xleft, double ytop, double width,
   // x tics and vertical grid
   if(opt->AxesTics[0] > 0){
     int nb = opt->AxesTics[0];
-    if(opt->Axes) {
+    if(opt->Axes){
       sprintf(label, opt->AxesFormat[0], - M_PI * 1.e-4);
       if((nb - 1) * gl_width(label) > width)
 	nb = (int)(width / gl_width(label)) + 1;
@@ -237,17 +252,17 @@ static void drawGraphAxes(PView *p, double xleft, double ytop, double width,
     double dx = width / (double)(nb - 1);
     double ybot = ytop - height;
     
-    for(int i = 0; i < nb; i++) {
-      if(opt->Axes) {
+    for(int i = 0; i < nb; i++){
+      if(opt->Axes){
 	glBegin(GL_LINES);
 	glVertex2d(xleft + i * dx, ybot);
 	glVertex2d(xleft + i * dx, ybot + tic);
-	if(opt->Axes > 1) {
+	if(opt->Axes > 1){
 	  glVertex2d(xleft + i * dx, ytop);
 	  glVertex2d(xleft + i * dx, ytop - tic);
 	}
 	glEnd();
-	if(opt->Axes > 2 && i != 0 && i != nb - 1) {
+	if(opt->Axes > 2 && i != 0 && i != nb - 1){
 	  glEnable(GL_LINE_STIPPLE);
 	  glLineStipple(1, 0x1111);
 	  gl2psEnable(GL2PS_LINE_STIPPLE);
@@ -276,14 +291,14 @@ static void drawGraphAxes(PView *p, double xleft, double ytop, double width,
 
 static void addGraphPoint(PView *p, double xleft, double ytop, double width, 
 			  double height, double x, double y, double xmin, 
-			  double xmax, double ymin, double ymax)
+			  double xmax, double ymin, double ymax, bool numeric)
 {
   PViewOptions *opt = p->getOptions();
 
   double px = xleft;
   if(xmin != xmax) px += (x - xmin) / (xmax - xmin) * width;
 
-  if(opt->SaturateValues) {
+  if(opt->SaturateValues){
     if(y > ymax)
       y = ymax;
     else if(y < ymin)
@@ -294,10 +309,10 @@ static void addGraphPoint(PView *p, double xleft, double ytop, double width,
   double py = ybot;
   if(ymax != ymin) py += (y - ymin) / (ymax - ymin) * height;
 
-  if(y >= ymin && y <= ymax) {
+  if(y >= ymin && y <= ymax){
     unsigned int col = opt->getColor(y, ymin, ymax, true);
     glColor4ubv((GLubyte *) &col);
-    if(opt->IntervalsType == PViewOptions::Numeric) {
+    if(numeric){
       glRasterPos2d(px + 3, py + 3);
       char label[256];
       sprintf(label, opt->Format, y);
@@ -321,6 +336,13 @@ static void drawGraphCurves(PView *p, double xleft, double ytop, double width,
   glLineWidth(opt->LineWidth);
   gl2psLineWidth(opt->LineWidth * CTX.print.eps_line_width_factor);
 
+  if(opt->IntervalsType == PViewOptions::Numeric){
+    for(unsigned int i = 0; i < y.size(); i++)
+      for(unsigned int j = 0; j < x.size(); j++)
+	addGraphPoint(p, xleft, ytop, width, height, x[j], y[i][j], 
+		      xmin, xmax, opt->TmpMin, opt->TmpMax, true);
+  }
+
   if(opt->IntervalsType == PViewOptions::Iso ||
      opt->IntervalsType == PViewOptions::Discrete ||
      opt->IntervalsType == PViewOptions::Numeric){
@@ -328,13 +350,13 @@ static void drawGraphCurves(PView *p, double xleft, double ytop, double width,
     for(unsigned int i = 0; i < y.size(); i++)
       for(unsigned int j = 0; j < x.size(); j++)
 	addGraphPoint(p, xleft, ytop, width, height, x[j], y[i][j], 
-		      xmin, xmax, opt->TmpMin, opt->TmpMax);
+		      xmin, xmax, opt->TmpMin, opt->TmpMax, false);
     glEnd();    
   }
 
   if(opt->IntervalsType == PViewOptions::Discrete ||
      opt->IntervalsType == PViewOptions::Continuous){
-    for(unsigned int i = 0; i < y.size(); i ++) {
+    for(unsigned int i = 0; i < y.size(); i++){
       if(opt->UseStipple){
 	glEnable(GL_LINE_STIPPLE);
 	glLineStipple(opt->Stipple[i % 10][0], opt->Stipple[i % 10][1]);
@@ -343,7 +365,7 @@ static void drawGraphCurves(PView *p, double xleft, double ytop, double width,
       glBegin(GL_LINE_STRIP);
       for(unsigned int j = 0; j < x.size(); j++)
 	addGraphPoint(p, xleft, ytop, width, height, x[j], y[i][j], 
-		      xmin, xmax, opt->TmpMin, opt->TmpMax);
+		      xmin, xmax, opt->TmpMin, opt->TmpMax, false);
       glEnd();
       if(opt->UseStipple){
 	glDisable(GL_LINE_STIPPLE);
@@ -373,8 +395,8 @@ static void drawGraph(PView *p, double xleft, double ytop, double width, double
   
   std::vector<double> x;
   std::vector<std::vector<double> > y;
-  double xmin, xmax, ymin, ymax;
-  if(!getGraphData(p, x, xmin, xmax, y, ymin, ymax)) return;
+  double xmin, xmax;
+  if(!getGraphData(p, x, xmin, xmax, y)) return;
   drawGraphAxes(p, xleft, ytop, width, height, xmin, xmax);
   drawGraphCurves(p, xleft, ytop, width, height, x, xmin, xmax, y);
 }
@@ -393,16 +415,16 @@ void Draw_Graph2D()
   gl_font(CTX.gl_font_enum, CTX.gl_fontsize);
   double xsep = 0., ysep = 5 * gl_height();
   char label[1024];
-  for(unsigned int i = 0; i < graphs.size(); i++) {
+  for(unsigned int i = 0; i < graphs.size(); i++){
     PViewOptions *opt = graphs[i]->getOptions();
     sprintf(label, opt->Format, -M_PI * 1.e-4);
     xsep = std::max(xsep, gl_width(label));
   }
   
-  for(unsigned int i = 0; i < graphs.size(); i++) {
+  for(unsigned int i = 0; i < graphs.size(); i++){
     PView *p = graphs[i];
     PViewOptions *opt = graphs[i]->getOptions();
-    if(!opt->AutoPosition) {
+    if(!opt->AutoPosition){
       double x = opt->Position[0], y = opt->Position[1];
       int center = Fix2DCoordinates(&x, &y);
       drawGraph(p, x - (center & 1 ? opt->Size[0] / 2. : 0), 
diff --git a/Graphics/Post.cpp b/Graphics/Post.cpp
index e7a71e2364..e476ee3a7e 100644
--- a/Graphics/Post.cpp
+++ b/Graphics/Post.cpp
@@ -1,4 +1,4 @@
-// $Id: Post.cpp,v 1.129 2007-09-10 05:13:12 geuzaine Exp $
+// $Id: Post.cpp,v 1.130 2007-09-11 13:54:35 geuzaine Exp $
 //
 // Copyright (C) 1997-2007 C. Geuzaine, J.-F. Remacle
 //
@@ -1186,11 +1186,6 @@ static int estimateNumVectors(PView *p)
   PViewOptions *opt = p->getOptions();
 
   int heuristic = data->getNumVectors();
-  if(opt->Normals)
-    heuristic += data->getNumElements(PViewData::Triangle) +
-      data->getNumElements(PViewData::Quadrangle);
-  if(opt->Tangents)
-    heuristic += data->getNumElements(PViewData::Line);
 
   return heuristic + 1000;
 }
-- 
GitLab