
// CoordsTest.java
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Load a file of coordinates, draw it as a line graph image,
   and use the CurevFitter and Plot classes to obtain and
   display a polynomial equation for the points.

   Usage:
     > compile CoordsTest.java
     > run CoordsTest <fnm>
   e.g.
     > run CoordsTest coords2.txt
     > run CoordsTest coords1.txt

*/

import java.awt.*;
import java.util.*;

import ij.*;
import ij.gui.*;
import ij.process.*; 
import ij.measure.*; 


public class CoordsTest
{

  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: run CoordsTest <fnm>");
      return;
    }
    ArrayList<Point> coords = ImageUtils.loadCoords(args[0]);
    // System.out.println(coords);

    ImagePlus imp = ImageUtils.drawCoords(coords);
    imp.show();

    String saveFnm = ImageUtils.getName(args[0]) + ".png";
    System.out.println("Saving image to " + saveFnm);
    IJ.saveAs(imp, "png", saveFnm);

    // convert coords to plot data 
    int xMax = 0;
    int yMax = 0;
    double[] xs = new double[coords.size()];
    double[] ys = new double[xs.length];
    for (int i=0; i < coords.size(); i++) {
      Point pt = coords.get(i);
      xs[i] = pt.x;
      ys[i] = pt.y;
      if (pt.x > xMax)
        xMax = pt.x;
      if (pt.y > yMax)
        yMax = pt.y;
    }
    // System.out.println("Max values: " + xMax + ", " + yMax);

    // plot settings
    Plot plot = new Plot("Coordinates Plot","X Axis","Y Axis");
    plot.setLimits(0, xMax, 0, yMax);
    plot.setSize(600, 600);
    plot.setLineWidth(2);

    // plot the coordinates
    plot.setColor(Color.blue);
    plot.add("line", xs, ys);

    // legend settings
    plot.changeFont(new Font("Helvetica", Font.PLAIN, 18));

    // attempt curve fitting
    CurveFitter cf = new CurveFitter(xs, ys);
    cf.doFit(CurveFitter.POLY2);
    int status = cf.getStatus();

    if (status == 0)  {  // curve fitting success
      double[] params = cf.getParams();

      // plot a red fitted curve
      double[] ys2 = new double[xs.length];
      for (int i=0; i < xs.length; i++)
        ys2[i] = cf.f(CurveFitter.POLY2, params, xs[i]);
      plot.setColor(Color.red);
      plot.add("line", xs, ys2);

      // print results
      // System.out.println("Params: " + Arrays.toString(params));
      String equ = String.format("Equ: y = %.5f + %.5fx + %.5fx^2",
                               params[0], params[1], params[2]);
      System.out.println(equ);
      String r2 = String.format("R^2 = %.5f\n", cf.getRSquared());
      System.out.println(r2);

      // draw legend
      plot.setColor(Color.black);
      plot.addLabel(0.05, 0.1, equ);   // (0,0) -> (1,1)
      plot.setColor(Color.red);
      plot.drawNormalizedLine(0.05, 0.12, 0.15, 0.12);
      plot.setColor(Color.black);
      plot.addLabel(0.05, 0.2, r2);

    }
    else {  // curve fitting failed
      System.out.println("Status: " + cf.getStatusString());
      plot.setColor(Color.black);
      plot.addLabel(0.05, 0.1, cf.getStatusString());
    }

    plot.show();

    ImageUtils.waitGone("Plot"); 
    System.exit(0);
  } /* main */


}  // end of CoordsTest class
