//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Sat Jul  1 02:48:55 PDT 2006
// Last Modified: Mon Mar 17 23:19:09 PST 2008
// Filename:      ...sig/examples/all/plotcurve.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/plotcurve/plotcurve.cpp
// Syntax:        C++; museinfo
//
// Description:   Plot a stream of numbers.
//

#include "HumdrumFile.h"
#include "Array.h"
#include "Options.h"
#include "PixelColor.h"

#include <string.h>
#include <stdio.h>

#ifndef OLDCPP
   using namespace std;
   #include <iostream>
   #include <fstream>
#else
   #include <iostream.h>
   #include <fstream.h>
#endif

// function declarations:
void     checkOptions(Options& opts, int argc, char** argv);
void     example(void);
void     usage(const char* command);
void     printDataPlot(Array<double>& a, Array<double>& b, int height,
                                int gridlines);

// User interface variables:
Options   options;
int       column    = 0;         // used with -n option (for averaging)
int       flipq     = 0;         // flip the data from tempo to duration
int       maxheight = -1;        // used with --height option
int       gradientq = 0;         // used with -G option
double    smooth    = 0.0;       // used with -S option
int       hline     = 0;         // used with -H option
int       plotq     = 0;         // used with -P option
int       unsmoothq = 0;         // used with -U option
int       bifeature = 0;         // used with -b option
int       bgval     = 80;        // no interface to user yet

//////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv) {
   // process the command-line options
   checkOptions(options, argc, argv);

   HumdrumFile infile;
   const char* filename = "";

   if (options.getArgCount() <= 0) {
      infile.read(cin);
   } else {
      filename = options.getArg(1);
      infile.read(filename);
   }

   Array<double> a;
   //Array<double> b;

   // loadData(infile, a);

   // printDataPlot(a);

   return 0;
}


//////////////////////////////////////////////////////////////////////////


//////////////////////////////
//
// printDataPlot -- print an input data plot underneath the scape.
//

void printDataPlot(Array<double>& a, Array<double>& b, int height,
      int gridlines) {
   PixelColor pc;
   pc.setColor(255,0,0);

   int i, j;
   int rows = height;
   int cols = a.getSize() * 2;

   Array<Array<PixelColor> > plotregion;
   plotregion.setSize(rows);
   plotregion.allowGrowth(0);
   for (i=0; i<rows; i++) {
      plotregion[i].setSize(cols);
      plotregion[i].allowGrowth(0);
      for (j=0; j<cols; j++) {
         plotregion[i][j].setColor(255,255,255);
      }
   }

   // draw vertical lines if gridlines > 0:
   if (gridlines > 0) {
      for (j=0; j<cols/2; j++) {
         if ((j+1) % gridlines == 0) {
            for (i=0; i<rows; i++) {
               plotregion[i][j*2].setColor(bgval,bgval,bgval);
               plotregion[i][j*2+1].setColor(bgval,bgval,bgval);
            }
         }
      }
   }
   

   double minn = a[0];
   double maxx = a[0];
   for (i=1; i<a.getSize(); i++) {
      if (a[i] < minn) { minn = a[i]; }
      if (a[i] > maxx) { maxx = a[i]; }
   }

   double i1;
   double i2;
   int x1;
   int x2;
   int k;
   int xlow;
   int xhi;
   for (j=0; j<a.getSize(); j++) {
      i1 = (a[j] - minn)/(maxx - minn) * rows;
      i = rows - int(i1) - 1;
      if (i < 0) { i = 0; }
      if (i >= rows) { i = rows-1; }
      plotregion[i][2*j].setColor(0,0,255);
      if (j < a.getSize()-1) {
         i2 = (a[j+1] - minn)/(maxx - minn) * rows;
         x1 = rows - int(i1) - 1;
         x2 = rows - int(i2) - 1;
         if (x1 < 0) { x1 = 0; }
         if (x1 >= rows) { x1 = rows-1; }
         if (x2 < 0) { x2 = 0; }
         if (x2 >= rows) { x2 = rows-1; }
         if (x1 < x2) {
            xlow = x1;
            xhi  = x2;
         } else {
            xlow = x2;
            xhi  = x1;
         }
         for (k=xlow+1; k<xhi; k++) {
            plotregion[k][2*j+1].setColor(175,175,255);
         }
	 if (xlow == xhi) {
            plotregion[xlow][2*j+1].setColor(175,175,255);
         }
	 if (xhi-xlow == 1) {
            plotregion[x1][2*j+1].setColor(175,175,255);
         }
      }
   }

   for (i=0; i<rows; i++) {
      for (j=0; j<cols; j++) {
         cout << " ";
         plotregion[i][j].writePpm3(cout);
      }
      cout << endl;
   }

}



//////////////////////////////
//
// checkOptions -- 
//

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("g|log=b", "display vertical axis on a log scale");
   opts.define("flip=b", "flip the input data from tempo to duration");
   opts.define("cd|condiff=b", "display contoured difference plots");
   opts.define("f|logfactor=d:1500.0", "factor for strenth of log scaling");
   opts.define("n|column=i:1", "which column to average");
   opts.define("a|average=b", "display data average rather than correlation");
   opts.define("x|data=b",    "display input data and then quit");
   opts.define("c|correlation=b", "display correlation data and then quit");
   opts.define("h|hue=b", "display correlations in hue coloring");
   opts.define("r|rectangle=b", "display correlations in rectangular plot");
   opts.define("p|poly=b", "poly correlation plot");
   opts.define("b|bg|background=s:255 255 255", "background color");
   opts.define("bi=b", "Binary feature correlation");
   opts.define("height=i:-1", "Maximum height scope");
   opts.define("G|gradient=b", "Gradient analysis (of A or R option)");
   opts.define("S|smooth=d:0.0", "Smoothing gain (0.0 = don't smooth)");
   opts.define("U|unsmooth=b", "apply unsmoothing");
   opts.define("H|horizontal=i:0", "hoizontal line step");
   opts.define("P|plot=i:0", "display original data underneath");

   opts.define("author=b",  "author of program"); 
   opts.define("version=b", "compilation info");
   opts.define("example=b", "example usages");   
   opts.define("help=b",  "short description");
   opts.process(argc, argv);
   
   // handle basic options:
   if (opts.getBoolean("author")) {
      cout << "Written by Craig Stuart Sapp, "
           << "craig@ccrma.stanford.edu, Jul 2006" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 13 May 2007" << endl;
      cout << "compiled: " << __DATE__ << endl;
      exit(0);
   } else if (opts.getBoolean("help")) {
      usage(opts.getCommand());
      exit(0);
   } else if (opts.getBoolean("example")) {
      example();
      exit(0);
   }

   // logq      = opts.getBoolean("log");
   // logfactor = opts.getDouble("logfactor");
   // smooth    = opts.getDouble("smooth");
   // unsmoothq = opts.getBoolean("unsmooth");
   // hline     = opts.getInteger("horizontal");
   // flipq     = opts.getBoolean("flip");
   // condiffq  = opts.getBoolean("condiff");
   // column    = opts.getInteger("column") - 1;
   // maxheight = opts.getInteger("height");
   // rampq     = opts.getBoolean("ramp");
   // plotq     = opts.getInteger("plot");
   // bifeature = opts.getBoolean("bi");
   if (plotq < 0) {
      plotq = 0;
   }
   if (column < 0) {
      column = 0;
   } else if (column > 1) {
      column = 1;
   }
   // dataq     = opts.getBoolean("data");
   // correlq   = opts.getBoolean("correlation");
   // if (opts.getBoolean("hue")) {
   //    colortype = COLOR_HUE;
   // }

   // if ((rampq || archq) && opts.getBoolean("gradient")) {
   //    gradientq = 1;
   // }
   
   // if (opts.getBoolean("rectangle"))  {
   //    plotshape = SHAPE_RECTANGLE;
   // }
}



//////////////////////////////
//
// example --
//

void example(void) {


}



//////////////////////////////
//
// usage --
//

void usage(const char* command) {

}



// md5sum: 47d7ce5ddc1702fddd3a6a2388a4085e curveplot.cpp [20110711]