/*-
 * #%L
 * Scenery-backed 3D visualization package for ImageJ.
 * %%
 * Copyright (C) 2016 - 2018 SciView developers.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */
package sc.iview.commands.process;

import static sc.iview.commands.MenuWeights.PROCESS;
import static sc.iview.commands.MenuWeights.PROCESS_ISOSURFACE;

import net.imagej.ImgPlus;
import net.imagej.mesh.Mesh;
import net.imagej.ops.OpService;
import net.imagej.ops.geom.geom3d.mesh.BitTypeVertexInterpolator;
import net.imglib2.img.Img;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.integer.UnsignedByteType;

import org.scijava.command.Command;
import org.scijava.plugin.Menu;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import sc.iview.SciView;

@Plugin(type = Command.class, menuRoot = "SciView", //
menu = {@Menu(label = "Process", weight = PROCESS), //
        @Menu(label = "Isosurface", weight = PROCESS_ISOSURFACE)})
public class Isosurface implements Command {

    @Parameter
    private OpService ops;

    @Parameter
    private SciView sciView;

    @Parameter
    private ImgPlus<UnsignedByteType> image;

    @Parameter
    private int isoLevel;

    @Override
    public void run() {

        Img<BitType> bitImg = ( Img<BitType> ) ops.threshold().apply( image, new UnsignedByteType( isoLevel ) );

        Mesh m = ops.geom().marchingCubes( bitImg, isoLevel, new BitTypeVertexInterpolator() );

        sciView.addMesh( m );

    }

}

@@ -271,17 +271,16 @@
  met:%0A *
- 
 %0A * 1. R
@@ -632,17 +632,16 @@
 tion.%0A *
- 
 %0A * THIS
@@ -1481,144 +1481,29 @@
 ort 
-static sc.iview.commands.MenuWeights.PROCESS;%0Aimport static sc.iview.commands.MenuWeights.PROCESS_ISOSURFACE;%0A%0Aimport net.imagej.ImgPlus
+graphics.scenery.Node
 ;%0Aim
@@ -1628,16 +1628,53 @@
 olator;%0A
+import net.imglib2.IterableInterval;%0A
 import n
@@ -1768,35 +1768,18 @@
 ric.
-integer.UnsignedByte
+Real
 Type;%0A
-%0A
 impo
@@ -1917,32 +1917,182 @@
 in;%0A
-%0Aimport sc.iview.SciView
+import org.scijava.ui.UIService;%0Aimport sc.iview.SciView;%0A%0Aimport static sc.iview.commands.MenuWeights.PROCESS;%0Aimport static sc.iview.commands.MenuWeights.PROCESS_ISOSURFACE
 ;%0A%0A@
@@ -2145,16 +2145,24 @@
 ew%22, //%0A
+        
 menu = %7B
@@ -2216,16 +2216,24 @@
         
+  
+      
 @Menu(la
@@ -2306,16 +2306,36 @@
 osurface
+%3CT extends RealType%3E
  impleme
@@ -2469,31 +2469,25 @@
 te I
-mgPlus%3CUnsignedByteType
+terableInterval%3CT
 %3E im
@@ -2523,11 +2523,14 @@
 ate 
-int
+double
  iso
@@ -2546,40 +2546,179 @@
    @
-Override%0A    public void run() %7B
+Parameter%0A    private UIService uiService;%0A%0A    @Override%0A    public void run() %7B%0A        T tmp = (T) image.firstElement().createVariable();%0A        tmp.setReal(isoLevel);
 %0A%0A  
@@ -2746,17 +2746,16 @@
 tImg = (
- 
 Img%3CBitT
@@ -2758,17 +2758,16 @@
 BitType%3E
- 
 ) ops.th
@@ -2786,49 +2786,18 @@
 ply(
- 
 image, 
-new UnsignedByteType( isoLevel ) 
+tmp
 );%0A%0A
@@ -2842,17 +2842,16 @@
 bes(
- 
 bitImg, 
 isoL
@@ -2846,24 +2846,17 @@
 bitImg, 
-isoLevel
+1
 , new Bi
@@ -2880,17 +2880,16 @@
 olator()
- 
 );%0A%0A    
@@ -2891,16 +2891,30 @@
 %0A       
+ Node scMesh =
  sciView
@@ -2922,19 +2922,17 @@
 addMesh(
- m 
+m
 );%0A%0A    

Fix isosurface to work on all RealTypes