/* 0015vert.c  wing in wind tunnel, plot, get left and right grid  */
/*             read file  0015a**.dat  ** is angle of attack       */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
#include <math.h>

static double xmin = -1.0; /* data, not plot area */
static double xmax =  1.0;
static double ymin = -1.0;
static double ymax =  1.0;
static double xmins;
static double xmaxs;
static double xtop, ytop, xbot, ybot; /* special points */
static double grid = 0.1; /* grid spacing, not around wing */
static int width = 600;
static int height = 600; /* upper part for title */
#define npts 100
static double xlg[npts]; /* lower and left points */
static double xrg[npts]; /* upper and right points */
static double yg[npts];
static int nxl, nxr, ny;
static int i1yg, i2yg, i1xlg, i2xlg, i1xrg, i2xrg; /* inside indices */
#define outpts 100
static double leftp[outpts]; /* read in data */
static double rightp[outpts];
static double ygrid[outpts];
static int npts_lr;
static double angle_deg = 22.0; /* may be read in */
static char angle_ch[] = "22";
static char fname[] = "0015a22.dat";
static FILE* fout;

#undef min
#define min(a,b) ((a)<(b)?(a):(b))
#undef max
#define max(a,b) ((a)>(b)?(a):(b))

void build_grid()
{
  int i, j;
  double x, y;
  i1yg = 0;
  i2yg = 0;
  i1xlg = 0;
  i2xlg = 0;
  i1xrg = 0;
  i2xrg = 0;

  ny = 0;
  for(y=ymin; y<=ymax; y+=grid)
  {
    if(y<ybot)
    { 
      yg[ny] = y;
      ny++;
    }
    if(y>ybot && y<ytop && i1yg==0)
    {
      i1yg = ny;
      for(j=0; j<npts_lr; j++)
      {
        yg[ny] = ygrid[j];
        ny++;
      }
      i2yg = ny-1;
    }
    if(y>ytop)
    { 
      yg[ny] = y;
      ny++;
    }
  } /* end yg gen */

  nxl = 0;
  for(x=xmin; x<=xmax; x+=grid)
  {
    if(x<xbot)
    { 
      xlg[nxl] = x;
      nxl++;
    }
    if(x>xbot && x<xtop && i1xlg==0)
    {
      i1xlg = nxl;
      for(j=npts_lr-1; j>=0; j--) /* reverse increase */
      {
        xlg[nxl] = leftp[j];
        nxl++;
      }
      i2xlg = nxl-1;
    }
    if(x>xtop)
    { 
      xlg[nxl] = x;
      nxl++;
    }
  } /* end xlg gen */

  nxr = 0;
  for(x=xmin; x<=xmax; x+=grid)
  {
    if(x<xbot)
    { 
      xrg[nxr] = x;
      nxr++;
    }
    if(x>xbot && x<xtop && i1xrg==0)
    {
      i1xrg = nxr;
      for(j=npts_lr-1; j>=0; j--) /* reverse increase */
      {
        xrg[nxr] = rightp[j];
        nxr++;
      }
      i2xrg = nxr-1;
    }
    if(x>xtop)
    { 
      xrg[nxr] = x;
      nxr++;
    }
  } /* end xlg gen */
  printf("yg[0]  to i1yg =%d to i2yg =%d to ny =%d \n", i1yg,  i2yg,  ny);
  printf("xlg[0] to i1xlg=%d to i2xlg=%d to nxl=%d \n", i1xlg, i2xlg, nxl);
  printf("xrg[0] to i1xrg=%d to i2xrg=%d to nxr=%d \n", i1xrg, i2xrg, nxr);
} /* end build grid */

void draw_lr() /* left/right grid */
{
  int j, i;

  glColor3f(0.0, 0.0, 0.0);
  for(j=0; j<ny; j++)
  {
    if(j<i1yg) /* all the way across */
    {
      for(i=0; i<nxl; i++)
      {
        glBegin(GL_POINTS);
          glVertex2f(xlg[i],yg[j]);
        glEnd();
      }
    }
    if(j>i2yg) /* all the way across */
    {
      for(i=0; i<nxr; i++)
      {
        glBegin(GL_POINTS);
          glVertex2f(xrg[i],yg[j]);
        glEnd();
      }
    }
    if(j>=i1yg && j<=i2yg) /* part way */
    {  
      for(i=0; i<nxl; i++)
      {
        if((i<i1xlg || xlg[i]<=leftp[npts_lr-1-(i-i1xlg)]) && i<i2xlg)
	{
          if(i>=i1xlg) printf("xlg[%d]=%f, leftp[%d]=%f \n",
                i, xlg[i], npts_lr-1-(j-i1yg), leftp[npts_lr-1-(j-i1yg)]);
          glBegin(GL_POINTS);
            glVertex2f(xlg[i],yg[j]);
          glEnd();
	}
      }
      for(i=0; i<nxr; i++)
      {
        if((i>=i1xrg && xrg[i]>=rightp[npts_lr-1-(j-i1yg)]) || i>i2xrg)
	{
          if(i<=i2xrg) printf("xrg[%d]=%f, rightp[%d]=%f \n",
                i, xrg[i], npts_lr-1-(i-i1xrg), rightp[npts_lr-1-(j-i1yg)]);
          glBegin(GL_POINTS);
            glVertex2f(xrg[i],yg[j]);
          glEnd();
	}
      }
    }
  }

} /* end draw_lr */

void display(void)
{
  double x, y, xlow, xhigh;
  char title[] = "grid for ** degree angle of attack";
  char *p;
  int i, j;

  title[9] = angle_ch[0];
  title[10] = angle_ch[1];

  /* clear window */
  glClear(GL_COLOR_BUFFER_BIT); 
  glLoadIdentity ();

  glPointSize(4.0);
  draw_lr();

  /* draw text, in its own context */
  glPushMatrix();
  glLoadIdentity ();
  glColor3f(0.0, 0.0, 0.0);
  glEnable(GL_LINE_SMOOTH);
  glTranslatef(-0.9, 1.02, 0.0);
  glScalef(0.0007, 0.0007, 0.0);
  for(p=title; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();
  
  glFlush(); 

} /* end display */

/* This routine handles mouse events */
static void mouse(int button, int state, int x, int y)
{
  float wx, wy;
  
  if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  {
    exit(0);
  }
  glutPostRedisplay();
} /* end mouse */

/* This routine handles window resizes */
void reshape(int w, int h)
{
  width = w;
  height = h;
  /* Set the transformations */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.1, 1.1, -1.1, 1.1, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glViewport(0, 0, w, h);
} /* end reshape */

static void read_lr()
{
  int i;

  fname[5] = angle_ch[0];
  fname[6] = angle_ch[1];
  printf("opening file  %s  for reading \n", fname); 
  fout = fopen(fname, "r");
  if(fout==NULL)
  {
    printf("can not open %s for reading \n", fname);
    exit(0);
  }

  for(i=0; i<100; i++)
  {
    npts_lr = i;
    if(feof(fout)) break;
    fscanf(fout, "%lf %lf %lf\n", &ygrid[i], &leftp[i], &rightp[i]);
    printf(" %f  %f  %f \n", ygrid[i], leftp[i], rightp[i]);
    if(i==0)
    {
      xtop = rightp[i];
      xbot = leftp[i];
      ytop = ygrid[i];
      ybot = ygrid[i];
    }
    else
    {
      xtop = max(xtop,rightp[i]);
      xbot = min(xbot,leftp[i]);
      ytop = max(ytop,ygrid[i]);
      ybot = min(ybot,ygrid[i]);
    }
  }
  fclose(fout);
  printf("0015vert has npts_lr=%d  Y values \n", npts_lr);
  printf("xbot=%f, xtop=%f, ybot=%f, ytop=%f \n", xbot, xtop, ybot, ytop); 
} /* end read_lr */

static void keyboard(unsigned char key, int x, int y)
{
  if(key=='s' || key=='S') /* smaller grid */
  {
  }
  glutPostRedisplay();
} /* end keyboard */

static void init()
{
  int ip;
  double x,y,x1,y1,x2,y2,x3,y3;

  printf("0015vert.c running \n");
  /* set clear color to white */
  glClearColor (1.0, 1.0, 1.0, 0.0);
  /* set fill  color to black */
  glColor3f(0.0, 0.0, 0.0);

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glOrtho(-1.1, 1.1, -1.1, 1.1, -1.0, 1.0);
  glMatrixMode (GL_MODELVIEW);
  glViewport(0, 0, width, height);
  read_lr();
  build_grid();
} /* end init */

int main(int argc, char* argv[])
{
  glutInit(&argc,argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
  glutInitWindowSize(width, height);
  glutInitWindowPosition(20,20); 
  
  glutCreateWindow(argv[0]); 
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMouseFunc(mouse);
  glutKeyboardFunc(keyboard);
  if(argc>1)
  {
    angle_ch[0] = argv[1][0]; /* use exactly 2 digits. e.g. 00 */
    angle_ch[1] = argv[1][1];
    angle_deg = (double)atoi(angle_ch);
  }  
  init();
  glutMainLoop();
  return 0;
} /* end main of 0015vert.c */


