
// Heights.java
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Generate either a Surface Plot or an interactive
   3D surface plot of Heightmap.png. Uncomment the 
   function you want to use.

   A snapshot is saved in surfacePlot.png

   Usage:
     > compile Heights.java
     > run Heights

*/

import ij.*;
import ij.plugin.*;


public class Heights
{
  private static final String PLOT_FNM = "surfacePlot.png";


  public static void main(String[] args)
  {
    ImagePlus imp = new ImagePlus("Heightmap.png");
    imp.show();
  
    // uncomment one of these ...
    // surfacePlot();
    interactivePlot(imp);

    ImageUtils.waitGone("Surface Plot");
    System.exit(0);
  } /* main */


  private static void surfacePlot()
  // Analyze --> Surface Plot
  {
    SurfacePlotter sp = new SurfacePlotter();
    sp.run("polygon=100 draw_wireframe shade draw_axis smooth");
          // these options are ignored (?); could just write ""

    ImagePlus plotImp = WindowManager.getImage("Surface Plot");
    plotImp.show();

    System.out.println("Saving surface plot to " + PLOT_FNM);
    IJ.saveAs(plotImp.duplicate(), "png", PLOT_FNM);
      // use duplicate() so plotImp name isn't changed to PLOT_FNM
  }  // end of surfacePlot()



  private static void interactivePlot(ImagePlus imp)
  // https://imagej.net/ij/plugins/surface-plot-3d.html
  // Plugin --> 3D --> Interactive 3D Surface Plot
  {
    String fnm = ImageUtils.getName(imp.getTitle());

    IJ.run(imp, "Interactive 3D Surface Plot", 
                   "plotType=3 smooth=10");   // snapshot=1");
             // I cannot capture the image when the snapshot flag is set

    // wait for the user to click "Save Plot";
    // only 1 snapshot is saved
    System.out.println("Press the 'Save Plot' button to save a snap...");
    ImagePlus snapImp = null;
    while (snapImp == null) {
      IJ.wait(500);
      snapImp = WindowManager.getImage("Surface_Plot_of_" + fnm);
    }

    System.out.println("Saving surface plot to " + PLOT_FNM);
    IJ.saveAs(snapImp, "png", PLOT_FNM);
  }  // end of interactivePlot()


}  // end of Heights class
