package org.bonej.ops.thresholdFraction;

import net.imagej.ImageJ;
import net.imglib2.IterableInterval;
import org.bonej.ops.thresholdFraction.SurfaceFraction.Results;
import org.bonej.ops.thresholdFraction.SurfaceFraction.Settings;
import org.bonej.testImages.Cuboid;
import org.junit.AfterClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Tests for the {@link SurfaceFraction SurfaceFraction} class
 *
 * @author Richard Domander 
 */
public class SurfaceFractionTest {
    private final static ImageJ IMAGE_J = new ImageJ();
    private static final double DELTA = 1e-12;

    @AfterClass
    public static void oneTimeTearDown() {
        IMAGE_J.context().dispose();
    }

    /**
     * Test the volume calculation of SurfaceFraction with a 1x1x1 cube
     * <p>
     * The surface created by the marching cubes algorithm in SurfaceFraction is "in between pixels".
     * In the case of a unit cube it creates an octahedron,
     * whose vertices are in the middle of the faces of the cube.
     */
    @Test
    public void regressionTestUnitCube() throws AssertionError {

        final long width = 1L;
        final long height = 1L;
        final long depth = 1L;
        final double a = width * 0.5;
        final double b = depth * 0.5;
        final double pyramidSide = Math.sqrt(a * a + b * b);
        final double pyramidVolume = pyramidSide * pyramidSide * (height / 2.0) / 3.0;
        final double thresholdVolume = pyramidVolume * 2.0;
        // Unit cube with 1 padding on each side, so 3x3x3 stack
        final IterableInterval unitCube = (IterableInterval) IMAGE_J.op().run(Cuboid.class, null, 1, 1, 1, 1, 1, 1);
        final Settings settings = new Settings(1.0, 1.0);
        /*
         * Each voxel in the surface is "full" except for the 8 corners of the stack.
         * If the corner had two neighbors (x & y), the corner would be half a voxel.
         * Because it has three (x, y & z), a quarter of a pyramid is also added
         */
        final double totalVolume = 27.0 - 8.0 * 0.5 + pyramidVolume * 2;

        final Results results = (Results) IMAGE_J.op().run(SurfaceFraction.class, unitCube, settings);

        assertEquals("Incorrect threshold surface volume", thresholdVolume, results.thresholdSurfaceVolume, DELTA);
        assertEquals("Incorrect total surface volume", totalVolume, results.totalSurfaceVolume, DELTA);
        assertEquals("Incorrect volume ratio", thresholdVolume / totalVolume, results.ratio, DELTA);
    }
}
@@ -78,30 +78,210 @@
 t.im
-glib2.IterableInterval
+agej.ImgPlus;%0Aimport net.imglib2.FinalDimensions;%0Aimport net.imglib2.IterableInterval;%0Aimport net.imglib2.img.Img;%0Aimport net.imglib2.type.logic.BitType;%0Aimport net.imglib2.type.numeric.integer.LongType
 ;%0Aim
@@ -496,16 +496,119 @@
 .Test;%0A%0A
+import java.util.Iterator;%0Aimport java.util.stream.LongStream;%0Aimport java.util.stream.StreamSupport;%0A%0A
 import s
@@ -990,16 +990,867 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testCreateSurfaceMask() throws Exception %7B%0A        // Create a 2x2x2 test image with values from 0 to 7%0A        final Img%3CLongType%3E img = IMAGE_J.op().create().img(new FinalDimensions(2, 2, 2), new LongType());%0A        final ImgPlus%3CLongType%3E imgPlus = new ImgPlus%3C%3E(img);%0A        final Iterator%3CLong%3E longIterator = LongStream.iterate(0, i -%3E (i + 1) %25 8).iterator();%0A        imgPlus.cursor().forEachRemaining(e -%3E e.set(longIterator.next()));%0A%0A        // Create a mask from voxels whose value is between 2 and 5%0A        final Img%3CBitType%3E mask = SurfaceFraction.createSurfaceMask(IMAGE_J.op(), imgPlus, 0, 1, 2, 2.0, 5.0);%0A        final long voxels = StreamSupport.stream(mask.spliterator(), false).filter(BitType::get).count();%0A%0A        assertEquals(%22Incorrect number of foreground voxels in the mask%22, 4, voxels);%0A    %7D%0A%0A
     /**%0A
@@ -2230,22 +2230,17 @@
 ows 
-AssertionError
+Exception
  %7B%0A%0A
@@ -2954,16 +2954,16 @@
  stack.%0A
-
         
@@ -2966,16 +2966,21 @@
      * I
+IRC i
 f the co

Add unit test