
// VolumeTest.java
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Load a series of images representing slices of a 3D image,
   convert them to a stack, and view the stack in one of three
   ways:
   
  3D Projection
   * https://imagej.net/ij/developer/api/ij/ij/plugin/Projector.html

  Orthogonal view: 
   * https://imagej.nih.gov/ij/developer/api/ij/ij/plugin/Orthogonal_Views.html

   * https://imagej.nih.gov/ij/developer/source/ij/plugin/Orthogonal_Views.java.html


  Volume viewer:
    * https://imagej.nih.gov/ij/plugins/volume-viewer.html
    *  https://github.com/fiji/Volume_Viewer/blob/master/src/main/java/fiji/plugin/volumeviewer/Volume_Viewer.java



   Usage:
     > compile VolumeTest.java
     > run VolumeTest <proj | ortho | vol>

*/

import java.awt.*;
import java.util.*;

import ij.*;
import ij.io.*;
import ij.gui.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import ij.process.*;


public class VolumeTest
{
  private static final String IMS_DIR = "head-slices/";


  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: run VolumeTest <proj | ortho | vol>");
      return;
    }
    String volType = args[0].toLowerCase();

    ImagePlus[] imps = ImageUtils.loadImages(IMS_DIR);
    ImagePlus stack = ImagesToStack.run(imps);
    System.out.println("Stack Size: " + stack.getImageStackSize());
    stack.show();

    // remove individual images
    for (int i=0; i < imps.length; i++)
      imps[i].close();

    if (volType.equals("proj")) {
      Projector proj = new Projector();
      proj.run("");
        // arguments are ignored: set slice spacing = 3; click 'interpolate'
/*
      proj.run("projection=[Brightest Point] axis=Y-Axis slice=3 initial=0 " +
        " total=360 rotation=10 lower=1 upper=255 opacity=0 " +
        " surface=100 interior=50 interpolate");
*/
     // window title is "Projections of Stack"
        // Images --> Stacks --> 3D Project...
    }
    else if (volType.equals("ortho"))
      IJ.run(stack, "Orthogonal Views", "");
        // Image --> Stacks --> Orthogonal Views

    else if (volType.equals("vol"))
      IJ.run(stack, "Volume Viewer", 
          "display_mode=3 z-aspect=3 scale=1.5 sampling=1 " +
          " angle_x=90 angle_y=90 angle_z=0" );
        // Plugins --> 3D --> Volume Viewer
    else {
      System.out.println("Unknown volume type: " + volType);
      System.out.println("Usage: run VolumeTest <proj | ortho | vol>");
    }

    ImageUtils.waitGone(stack); 
    System.exit(0);
  } /* main */


}  // end of VolumeTest class
