Class Util

java.lang.Object
net.imglib2.util.Util

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

    • 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:
  • Constructor Details

    • Util

      public Util()
  • Method Details

    • 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(RealLocalizable position1, RealLocalizable position2)
    • 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(List<Double> values)
    • averageFloat

      public static float averageFloat(List<Float> values)
    • min

      public static float min(List<Float> values)
    • max

      public static float max(List<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 String printCoordinates(float[] value)
    • printCoordinates

      public static String printCoordinates(double[] value)
    • printCoordinates

      public static String printCoordinates(RealLocalizable localizable)
    • printInterval

      public static String printInterval(Interval interval)
    • printCoordinates

      public static String printCoordinates(int[] value)
    • printCoordinates

      public static String printCoordinates(long[] value)
    • printCoordinates

      public static String printCoordinates(boolean[] value)
    • pow

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

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

      public static <T extends Type<T> & 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:
      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
    • getTypeFromRealInterval

      @Deprecated public static <T, F extends RealInterval & RealRandomAccessible<T>> T getTypeFromRealInterval(F rai)
      Deprecated.
      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, T type)
      Create an ArrayImgFactory if an image of the requested targetSize could be held in an ArrayImg. Otherwise return a CellImgFactory with as large as possible cell size.
      Parameters:
      targetSize - size of image that the factory should be able to create.
      type - type of the factory.
      Returns:
      an ArrayImgFactory or a CellImgFactory.
    • 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.
    • getSuitableImgFactory

      public static <T> ImgFactory<T> getSuitableImgFactory(Dimensions targetSize, T type)
      Create an appropriate ImgFactory for the requested targetSize and type. If the target size is a Img, return its ImgFactory. If the type is a NativeType, then getArrayOrCellImgFactory(Dimensions, NativeType) is used; if not, a ListImgFactory is returned.
      Parameters:
      targetSize - size of image that the factory should be able to create.
      type - type of the factory.
      Returns:
      an ArrayImgFactory, CellImgFactory or ListImgFactory as appropriate.
    • 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.
    • locationsEqual

      public static boolean locationsEqual(Localizable l1, Localizable l2)
      Determines whether the two Localizable objects have the same position, with long precision.

      At first glance, this method may appear to be unnecessary, since there is also locationsEqual(RealLocalizable, RealLocalizable), which is more general. The difference is that this method compares the positions using Localizable.getLongPosition(int), which has higher precision in integer space than RealLocalizable.getDoublePosition(int) does, which is what the locationsEqual(RealLocalizable, RealLocalizable) method uses.

      Parameters:
      l1 - The first Localizable.
      l2 - The second Localizable.
      Returns:
      True iff the positions are the same, including dimensionality.
      See Also:
    • locationsEqual

      public static boolean locationsEqual(RealLocalizable l1, RealLocalizable l2)
      Determines whether the two RealLocalizable objects have the same position, with double precision.
      Parameters:
      l1 - The first RealLocalizable.
      l2 - The second RealLocalizable.
      Returns:
      True iff the positions are the same, including dimensionality.
      See Also:
    • imagesEqual

      public static <T extends ValueEquals<U>, U> boolean imagesEqual(RandomAccessibleInterval<? extends T> a, RandomAccessibleInterval<? extends U> b)
      Checks if both images have equal intervals and content.
    • imagesEqual

      public static <T,U> boolean imagesEqual(RandomAccessibleInterval<? extends T> a, RandomAccessibleInterval<? extends U> b, 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(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, Object b)
      This method should be used in implementations of ValueEquals, to override Object.equals(Object).
      See Also:
    • combineHash

      public static int combineHash(int hash1, int hash2)