/*
    close_returns.c, by Timothy Jones, close returns plot via color map.

    Copyright (C) 2010 Timothy Jones

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Compilation on ubuntu:
// g++ close_returns.cpp -lpngwriter -lfreetype -lpng


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <pngwriter.h>
using namespace std;

int WID=1000;
int HIT=1000;

double **mmatrix(int r, int c);
double **mmatrix(int r, int c)
{

int row, col;
double **matrix;
matrix = (double **)malloc((size_t)(r * sizeof(double *)));
//assert(matrix != NULL);

for (row = 0; row < r; row++) 
   {
     matrix[row] = (double *)malloc((size_t)(c * sizeof(double)));
     //  assert(matrix[row] != NULL);
     for (col = 0; col < c; col++)  matrix[row][col] = 0;  //set all to zero
    }
 return matrix;
}

int main(int argc, char *argv[]){

  //////////////////////////////////////////////////////////
  // Open list of data to be mapped
  ifstream myfile;
  myfile.open ("first.list");
  double x,y,z;  // we will only use one of these for now
  double *xx,*yy,*zz;
  xx = (double *)malloc((size_t)(1 * sizeof(double *)));
  yy = (double *)malloc((size_t)(1 * sizeof(double *)));
  zz = (double *)malloc((size_t)(1 * sizeof(double *)));
  int p=0;
  double xmin=100000;
  double ymin=100000;
  double zmin=100000;
  double xmax=-100000;
  double ymax=-100000;
  double zmax=-100000;
  double xwid,ywid,zwid;
  int flag=0;
  while(myfile >> x >> y >> z)
     { 
       xx=(double *)realloc((double *)xx, ((size_t)((p+1) * sizeof(double))));
       yy=(double *)realloc((double *)yy, ((size_t)((p+1) * sizeof(double))));
       zz=(double *)realloc((double *)zz, ((size_t)((p+1) * sizeof(double))));
       if(flag>=10){
       xx[p]=x;
       yy[p]=y;
       zz[p]=z;
       p++;
       if(x>xmax){xmax=x;}
       if(y>ymax){ymax=y;}
       if(z>zmax){zmax=z;}
       if(x<xmin){xmin=x;}
       if(y<ymin){ymin=y;}
       if(z<zmin){zmin=z;}
       flag=0;
        }
       flag++;
     } 
  xwid=xmax-xmin;
  ywid=ymax-ymin;
  zwid=zmax-zmin;
  int numlines = p; 
  ///////////////////////////////////////////////////////////
  // Open png 
  char filename[256];
  int filenum=1000000;
  sprintf(filename,"%i.png",filenum);
  pngwriter png(WID,HIT,0,filename);

  ////////////////////////////////////////////////////////////
  // Plot close returns
  int round=(int) numlines/WID;  //How many data points per pixel !!!!!!!!!!!!!!!!!!!
  double diffr,diffg,diffb;
  int sample=0;
    for(int i=0; i<WID; i++){ 
      for(int j=0; j<HIT; j++){
        diffr=fabs(xx[i]-xx[i+j])/xwid;
        diffg=fabs(yy[i]-yy[i+j])/ywid;
        diffb=fabs(zz[i]-zz[i+j])/zwid;
	png.plot(i,HIT-j,diffr,diffg,diffb);
      }} 
    png.close();          
       
}

  

