Class Util


  • public class Util
    extends java.lang.Object
    A collection of general-purpose utility methods for working with ImgLib2 data structures.
    • Field Detail

      • MAX_ARRAY_SIZE

        private static final int MAX_ARRAY_SIZE
        The possible java array size is JVM dependent an actually slightly below Integer.MAX_VALUE. This is the same MAX_ARRAY_SIZE as used for example in ArrayList in OpenJDK8.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • genericArray

        @Deprecated
        public static <T> T[] genericArray​(int length)
        Deprecated.
        This does only work when T is erased to Object at call site.

        See https://github.com/imglib/imglib2/issues/253

      • log2

        public static double log2​(double value)
      • getArrayFromValue

        public static double[] getArrayFromValue​(double value,
                                                 int numDimensions)
      • getArrayFromValue

        public static float[] getArrayFromValue​(float value,
                                                int numDimensions)
      • getArrayFromValue

        public static int[] getArrayFromValue​(int value,
                                              int numDimensions)
      • getArrayFromValue

        public static long[] getArrayFromValue​(long value,
                                               int numDimensions)
      • expandArray

        public static long[] expandArray​(long[] values,
                                         int newLength)
        Expand or truncate the provided array of values to length newLength.

        If values.length < newLength then the last value is repeated. That is, the remaining elements are filled with values[values.length - 1].

        If values.length == newLength then values is returned, otherwise a new array is created.

        Parameters:
        values - values to copy
        newLength - length of expanded array
        Returns:
        an array where array.length == newLength and array[i] == values[Math.max(i, values.length)]
      • expandArray

        public static int[] expandArray​(int[] values,
                                        int newLength)
        Expand or truncate the provided array of values to length newLength.

        If values.length < newLength then the last value is repeated. That is, the remaining elements are filled with values[values.length - 1].

        If values.length == newLength then values is returned, otherwise a new array is created.

        Parameters:
        values - values to copy
        newLength - length of expanded array
        Returns:
        an array where array.length == newLength and array[i] == values[Math.max(i, values.length)]
      • distance

        public static double distance​(long[] position1,
                                      long[] position2)
      • percentile

        public static double percentile​(double[] values,
                                        double percentile)
        Computes the percentile of a collection of doubles (percentile 0.5 roughly corresponds to median)
        Parameters:
        values - - the values
        percentile - - the percentile [0...1]
        Returns:
        the corresponding value
      • averageDouble

        public static double averageDouble​(java.util.List<java.lang.Double> values)
      • averageFloat

        public static float averageFloat​(java.util.List<java.lang.Float> values)
      • min

        public static float min​(java.util.List<java.lang.Float> values)
      • max

        public static float max​(java.util.List<java.lang.Float> values)
      • average

        public static float average​(float[] values)
      • average

        public static double average​(double[] values)
      • min

        public static double min​(double[] values)
      • max

        public static double max​(double[] values)
      • median

        public static long median​(long[] values)
      • median

        public static double median​(double[] values)
      • median

        public static float median​(float[] values)
      • quicksort

        public static void quicksort​(long[] data)
      • quicksort

        public static void quicksort​(long[] data,
                                     int left,
                                     int right)
      • quicksort

        public static void quicksort​(double[] data)
      • quicksort

        public static void quicksort​(double[] data,
                                     int left,
                                     int right)
      • quicksort

        public static void quicksort​(float[] data)
      • quicksort

        public static void quicksort​(float[] data,
                                     int left,
                                     int right)
      • quicksort

        public static void quicksort​(double[] data,
                                     int[] sortAlso,
                                     int left,
                                     int right)
      • gLog

        public static double gLog​(double z,
                                  double c)
      • gLog

        public static float gLog​(float z,
                                 float c)
      • gLogInv

        public static double gLogInv​(double w,
                                     double c)
      • gLogInv

        public static double gLogInv​(float w,
                                     float c)
      • isApproxEqual

        public static boolean isApproxEqual​(float a,
                                            float b,
                                            float threshold)
      • isApproxEqual

        public static boolean isApproxEqual​(double a,
                                            double b,
                                            double threshold)
      • round

        public static int round​(float value)
      • round

        public static long round​(double value)
      • roundToInt

        public static int roundToInt​(float value)
      • roundToInt

        public static int roundToInt​(double value)
      • roundToLong

        public static long roundToLong​(float value)
      • roundToLong

        public static long roundToLong​(double value)
      • createGaussianKernel1DDouble

        public static double[] createGaussianKernel1DDouble​(double sigma,
                                                            boolean normalize)
        This method creates a gaussian kernel
        Parameters:
        sigma - Standard Derivation of the gaussian function
        normalize - Normalize integral of gaussian function to 1 or not...
        Returns:
        double[] The gaussian kernel
      • getSuggestedKernelDiameter

        public static int getSuggestedKernelDiameter​(double sigma)
      • printCoordinates

        public static java.lang.String printCoordinates​(float[] value)
      • printCoordinates

        public static java.lang.String printCoordinates​(double[] value)
      • printCoordinates

        public static java.lang.String printCoordinates​(RealLocalizable localizable)
      • printInterval

        public static java.lang.String printInterval​(Interval interval)
      • printCoordinates

        public static java.lang.String printCoordinates​(int[] value)
      • printCoordinates

        public static java.lang.String printCoordinates​(long[] value)
      • printCoordinates

        public static java.lang.String printCoordinates​(boolean[] value)
      • pow

        public static int pow​(int a,
                              int b)
      • max

        public static <T extends Type<T> & java.lang.Comparable<T>> T max​(T value1,
                                                                          T value2)
      • min

        public static <T extends Type<T> & java.lang.Comparable<T>> T min​(T value1,
                                                                          T value2)
      • long2int

        public static final int[] long2int​(long[] a)
      • int2long

        public static final long[] int2long​(int[] i)
      • safeInt

        public static int safeInt​(long value)
        Cast value to int, checking for overflow.
        Parameters:
        value - value to cast to int
        Returns:
        value cast to int
        Throws:
        java.lang.IllegalArgumentException - if value > Integer.MAX_VALUE
      • getTypeFromInterval

        @Deprecated
        public static <T,​F extends Interval & RandomAccessible<T>> T getTypeFromInterval​(F rai)
        Deprecated.
        This method has been deprecated. Use Typed.getType() instead. TODO: Cannot deprecate because rai parameter is not actually a RandomAccessibleInterval Gets an instance of T from the RandomAccessibleInterval by querying the value at the min coordinate
        Type Parameters:
        T - - the T
        Parameters:
        rai - - the RandomAccessibleInterval
        Returns:
        - an instance of T
      • getArrayOrCellImgFactory

        public static <T extends NativeType<T>> ImgFactory<T> getArrayOrCellImgFactory​(Dimensions targetSize,
                                                                                       int targetCellSize,
                                                                                       T type)
        Create an ArrayImgFactory if an image of the requested targetSize could be held in an ArrayImg. Otherwise return a CellImgFactory with cell size targetCellSize (or as large as possible if targetCellSize is too large).
        Parameters:
        targetSize - size of image that the factory should be able to create.
        targetCellSize - if a CellImgFactory is created, what should be the cell size.
        type - type of the factory.
        Returns:
        an ArrayImgFactory or a CellImgFactory.
      • ldu

        public static final int ldu​(int v)
        (Hopefully) fast floor log2 of an unsigned(!) integer value.
        Parameters:
        v - unsigned integer
        Returns:
        floor log2
      • equalIterationOrder

        public static boolean equalIterationOrder​(IterableInterval<?>... intervals)
        Checks whether n IterableInterval have the same iteration order.
      • imagesEqual

        public static <T,​U> boolean imagesEqual​(RandomAccessibleInterval<? extends T> a,
                                                      RandomAccessibleInterval<? extends U> b,
                                                      java.util.function.BiPredicate<T,​U> pixelEquals)
        Checks if both images have equal intervals and content. A predicate must be given to check if two pixels are equal.
      • min

        public static final void min​(double[] a,
                                     double[] b)
        Writes min(a,b) into a
        Parameters:
        a -
        b -
      • max

        public static final void max​(double[] a,
                                     double[] b)
        Writes max(a,b) into a
        Parameters:
        a -
        b -
      • asDoubleArray

        public static double[] asDoubleArray​(java.lang.Iterable<? extends RealType<?>> iterable)
        Returns the content of Iterable<RealType> as array of doubles.
      • asDoubleArray

        public static double[] asDoubleArray​(RandomAccessibleInterval<? extends RealType<?>> rai)
        Returns the pixels of an RandomAccessibleInterval of RealType as array of doubles. The pixels are sorted in flat iteration order.
      • valueEqualsObject

        public static <T extends ValueEquals<T>> boolean valueEqualsObject​(T a,
                                                                           java.lang.Object b)
        This method should be used in implementations of ValueEquals, to override Object.equals(Object).
        See Also:
        Object.equals(Object)
      • combineHash

        public static int combineHash​(int hash1,
                                      int hash2)