package org.bonej.wrapperPlugins;

import net.imagej.ImgPlus;
import net.imagej.axis.CalibratedAxis;
import net.imagej.ops.OpService;
import net.imglib2.img.Img;
import net.imglib2.type.logic.BitType;
import org.bonej.ops.connectivity.EulerContribution;
import org.bonej.ops.connectivity.EulerCharacteristic;
import org.bonej.utilities.AxisUtils;
import org.bonej.utilities.ElementUtil;
import org.bonej.utilities.ResultsInserter;
import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;

import static org.bonej.wrapperPlugins.CommonMessages.*;
import static org.scijava.ui.DialogPrompt.MessageType.WARNING_MESSAGE;

/**
 * A wrapper UI class for the Connectivity Ops
 *
 * @author Richard Domander 
 */
@Plugin(type = Command.class, menuPath = "Plugins>BoneJ>Connectivity", headless = true)
public class ConnectivityWrapper extends ContextCommand {
    @Parameter(initializer = "initializeImage")
    private ImgPlus inputImage;

    @Parameter
    private OpService opService;

    @Parameter
    private UIService uiService;

    @Override
    public void run() {
        final Img<BitType> bitImg = opService.convert().bit(inputImage);
        final int dimensions = inputImage.numDimensions();
        final ImgPlus<BitType> bitImgPlus = new ImgPlus<>(bitImg);
        for (int d = 0; d < dimensions; d++) {
            // Copy metadata
            final CalibratedAxis axis = (CalibratedAxis) inputImage.axis(d);
            bitImgPlus.setAxis(axis, d);
        }

        final Integer eulerCharacteristic = (Integer) opService.run(EulerCharacteristic.class, bitImgPlus);
        final Integer deltaEuler = (Integer) opService.run(EulerContribution.class, bitImgPlus, eulerCharacteristic);
        final int connectivity = 1 - eulerCharacteristic;
        final double connectivityDensity = calculateConnectivityDensity(connectivity);

        showResults(eulerCharacteristic, deltaEuler, connectivity, connectivityDensity);
    }

    //region -- Helper methods --
    private void showResults(final Integer eulerCharacteristic, final Integer deltaEuler, final int connectivity,
            final double connectivityDensity) {
        final String label = inputImage.getName();
        final String unitHeader = WrapperUtils.getUnitHeader(inputImage, "³");

        if (unitHeader.isEmpty()) {
            uiService.showDialog(BAD_CALIBRATION, WARNING_MESSAGE);
        }

        final ResultsInserter inserter = ResultsInserter.getInstance();
        inserter.setMeasurementInFirstFreeRow(label, "Euler char. (χ)", eulerCharacteristic);
        inserter.setMeasurementInFirstFreeRow(label, "Contribution (Δχ)", deltaEuler);
        inserter.setMeasurementInFirstFreeRow(label, "Connectivity", connectivity);
        inserter.setMeasurementInFirstFreeRow(label, "Conn. density " + unitHeader, connectivityDensity);
        inserter.updateResults();
    }

    private double calculateConnectivityDensity(final int connectivity) {
        final double elements = AxisUtils.spatialSpaceSize(inputImage);
        final double elementSize = AxisUtils.calibratedSpatialElementSize(inputImage);
        return connectivity / (elements * elementSize);
    }

    @SuppressWarnings("unused")
    private void initializeImage() {
        if (inputImage == null) {
            cancel(NO_IMAGE_OPEN);
            return;
        }

        if (AxisUtils.countSpatialDimensions(inputImage) < 3) {
            cancel(NOT_3D_IMAGE);
            return;
        }

        if (!ElementUtil.isColorsBinary(inputImage)) {
            cancel(NOT_BINARY);
        }
    }
    //endregion
}

@@ -1601,31 +1601,30 @@
       final 
-Integer
+double
  eulerCharac
@@ -1716,37 +1716,39 @@
 nal 
-Integer deltaEuler = (Integer
+double edgeCorrection = (Double
 ) op
@@ -1794,37 +1794,16 @@
 tImgPlus
-, eulerCharacteristic
 );%0A     
@@ -1807,27 +1807,30 @@
       final 
-int
+double
  connectivit
@@ -1985,28 +1985,49 @@
 teristic
-, deltaEuler
+ + edgeCorrection, edgeCorrection
 , connec
@@ -2132,23 +2132,22 @@
 s(final 
-Integer
+double
  eulerCh
@@ -2166,23 +2166,22 @@
 , final 
-Integer
+double
  deltaEu
@@ -2187,27 +2187,30 @@
 uler, final 
-int
+double
  connectivit
@@ -2726,24 +2726,27 @@
 label, %22
-Contribu
+Edge correc
 tion (%CE%94%CF%87
@@ -3047,19 +3047,22 @@
 y(final 
-int
+double
  connect

Fix results