Class Util
- java.lang.Object
-
- net.imglib2.util.Util
-
public class Util extends java.lang.ObjectA collection of general-purpose utility methods for working with ImgLib2 data structures.
-
-
Field Summary
Fields Modifier and Type Field Description private static intMAX_ARRAY_SIZEThe possible java array size is JVM dependent an actually slightly below Integer.MAX_VALUE.
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static double[]asDoubleArray(java.lang.Iterable<? extends RealType<?>> iterable)Returns the content ofIterable<RealType>as array of doubles.static double[]asDoubleArray(RandomAccessibleInterval<? extends RealType<?>> rai)Returns the pixels of an RandomAccessibleInterval of RealType as array of doubles.static doubleaverage(double[] values)static floataverage(float[] values)static doubleaverageDouble(java.util.List<java.lang.Double> values)static floataverageFloat(java.util.List<java.lang.Float> values)static intcombineHash(int hash1, int hash2)static double[]createGaussianKernel1DDouble(double sigma, boolean normalize)This method creates a gaussian kernelstatic doubledistance(long[] position1, long[] position2)static doubledistance(RealLocalizable position1, RealLocalizable position2)static booleanequalIterationOrder(IterableInterval<?>... intervals)Checks whether nIterableIntervalhave the same iteration order.static int[]expandArray(int[] values, int newLength)Expand or truncate the provided array ofvaluesto lengthnewLength.static long[]expandArray(long[] values, int newLength)Expand or truncate the provided array ofvaluesto lengthnewLength.static <T> T[]genericArray(int length)Deprecated.static double[]getArrayFromValue(double value, int numDimensions)static float[]getArrayFromValue(float value, int numDimensions)static int[]getArrayFromValue(int value, int numDimensions)static long[]getArrayFromValue(long value, int numDimensions)static <T extends NativeType<T>>
ImgFactory<T>getArrayOrCellImgFactory(Dimensions targetSize, int targetCellSize, T type)static <T extends NativeType<T>>
ImgFactory<T>getArrayOrCellImgFactory(Dimensions targetSize, T type)static intgetSuggestedKernelDiameter(double sigma)static <T> ImgFactory<T>getSuitableImgFactory(Dimensions targetSize, T type)static <T,F extends Interval & RandomAccessible<T>>
TgetTypeFromInterval(F rai)Deprecated.static <T,F extends RealInterval & RealRandomAccessible<T>>
TgetTypeFromRealInterval(F rai)Deprecated.static doublegLog(double z, double c)static floatgLog(float z, float c)static doublegLogInv(double w, double c)static doublegLogInv(float w, float c)static <T extends ValueEquals<U>,U>
booleanimagesEqual(RandomAccessibleInterval<? extends T> a, RandomAccessibleInterval<? extends U> b)Checks if both images have equal intervals and content.static <T,U>
booleanimagesEqual(RandomAccessibleInterval<? extends T> a, RandomAccessibleInterval<? extends U> b, java.util.function.BiPredicate<T,U> pixelEquals)Checks if both images have equal intervals and content.static long[]int2long(int[] i)static booleanisApproxEqual(double a, double b, double threshold)static booleanisApproxEqual(float a, float b, float threshold)static intldu(int v)(Hopefully) fast floor log2 of an unsigned(!) integer value.static booleanlocationsEqual(Localizable l1, Localizable l2)Determines whether the twoLocalizableobjects have the same position, withlongprecision.static booleanlocationsEqual(RealLocalizable l1, RealLocalizable l2)Determines whether the twoRealLocalizableobjects have the same position, withdoubleprecision.static doublelog2(double value)static int[]long2int(long[] a)static doublemax(double[] values)static voidmax(double[] a, double[] b)Writes max(a,b) into astatic floatmax(java.util.List<java.lang.Float> values)static <T extends Type<T> & java.lang.Comparable<T>>
Tmax(T value1, T value2)static doublemedian(double[] values)static floatmedian(float[] values)static longmedian(long[] values)static doublemin(double[] values)static voidmin(double[] a, double[] b)Writes min(a,b) into astatic floatmin(java.util.List<java.lang.Float> values)static <T extends Type<T> & java.lang.Comparable<T>>
Tmin(T value1, T value2)static doublepercentile(double[] values, double percentile)Computes the percentile of a collection of doubles (percentile 0.5 roughly corresponds to median)static intpow(int a, int b)static java.lang.StringprintCoordinates(boolean[] value)static java.lang.StringprintCoordinates(double[] value)static java.lang.StringprintCoordinates(float[] value)static java.lang.StringprintCoordinates(int[] value)static java.lang.StringprintCoordinates(long[] value)static java.lang.StringprintCoordinates(RealLocalizable localizable)static java.lang.StringprintInterval(Interval interval)static voidquicksort(double[] data)static voidquicksort(double[] data, int[] sortAlso, int left, int right)static voidquicksort(double[] data, int left, int right)static voidquicksort(float[] data)static voidquicksort(float[] data, int left, int right)static voidquicksort(long[] data)static voidquicksort(long[] data, int left, int right)static longround(double value)static intround(float value)static introundToInt(double value)static introundToInt(float value)static longroundToLong(double value)static longroundToLong(float value)static intsafeInt(long value)Castvaluetoint, checking for overflow.static <T extends ValueEquals<T>>
booleanvalueEqualsObject(T a, java.lang.Object b)This method should be used in implementations ofValueEquals, to overrideObject.equals(Object).
-
-
-
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
-
-
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 ofvaluesto lengthnewLength.If
values.length < newLengththen the last value is repeated. That is, the remaining elements are filled withvalues[values.length - 1].If
values.length == newLengththenvaluesis returned, otherwise a new array is created.- Parameters:
values- values to copynewLength- length of expanded array- Returns:
- an array where
array.length == newLengthandarray[i] == values[Math.max(i, values.length)]
-
expandArray
public static int[] expandArray(int[] values, int newLength)Expand or truncate the provided array ofvaluesto lengthnewLength.If
values.length < newLengththen the last value is repeated. That is, the remaining elements are filled withvalues[values.length - 1].If
values.length == newLengththenvaluesis returned, otherwise a new array is created.- Parameters:
values- values to copynewLength- length of expanded array- Returns:
- an array where
array.length == newLengthandarray[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 valuespercentile- - 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 functionnormalize- 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)
Castvaluetoint, checking for overflow.- Parameters:
value- value to cast toint- Returns:
valuecast toint- Throws:
java.lang.IllegalArgumentException- ifvalue > Integer.MAX_VALUE
-
getTypeFromInterval
@Deprecated public static <T,F extends Interval & RandomAccessible<T>> T getTypeFromInterval(F rai)
Deprecated.This method has been deprecated. UseTyped.getType()instead. TODO: Cannot deprecate because rai parameter is not actually a RandomAccessibleInterval Gets an instance of T from theRandomAccessibleIntervalby querying the value at the min coordinate- Type Parameters:
T- - the T- Parameters:
rai- - theRandomAccessibleInterval- 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 theRandomAccessibleIntervalby querying the value at the min coordinate- Type Parameters:
T- - the T- Parameters:
rai- - theRandomAccessibleInterval- Returns:
- - an instance of T
-
getArrayOrCellImgFactory
public static <T extends NativeType<T>> ImgFactory<T> getArrayOrCellImgFactory(Dimensions targetSize, T type)
Create anArrayImgFactoryif an image of the requestedtargetSizecould be held in anArrayImg. Otherwise return aCellImgFactorywith 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
ArrayImgFactoryor aCellImgFactory.
-
getArrayOrCellImgFactory
public static <T extends NativeType<T>> ImgFactory<T> getArrayOrCellImgFactory(Dimensions targetSize, int targetCellSize, T type)
Create anArrayImgFactoryif an image of the requestedtargetSizecould be held in anArrayImg. Otherwise return aCellImgFactorywith cell sizetargetCellSize(or as large as possible iftargetCellSizeis too large).- Parameters:
targetSize- size of image that the factory should be able to create.targetCellSize- if aCellImgFactoryis created, what should be the cell size.type- type of the factory.- Returns:
- an
ArrayImgFactoryor aCellImgFactory.
-
getSuitableImgFactory
public static <T> ImgFactory<T> getSuitableImgFactory(Dimensions targetSize, T type)
Create an appropriateImgFactoryfor the requestedtargetSizeandtype. If the target size is aImg, return itsImgFactory. If the type is aNativeType, thengetArrayOrCellImgFactory(Dimensions, NativeType)is used; if not, aListImgFactoryis returned.- Parameters:
targetSize- size of image that the factory should be able to create.type- type of the factory.- Returns:
- an
ArrayImgFactory,CellImgFactoryorListImgFactoryas 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 nIterableIntervalhave the same iteration order.
-
locationsEqual
public static boolean locationsEqual(Localizable l1, Localizable l2)
Determines whether the twoLocalizableobjects have the same position, withlongprecision.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 usingLocalizable.getLongPosition(int), which has higher precision in integer space thanRealLocalizable.getDoublePosition(int)does, which is what thelocationsEqual(RealLocalizable, RealLocalizable)method uses.- Parameters:
l1- The firstLocalizable.l2- The secondLocalizable.- Returns:
- True iff the positions are the same, including dimensionality.
- See Also:
Localizable.getLongPosition(int)
-
locationsEqual
public static boolean locationsEqual(RealLocalizable l1, RealLocalizable l2)
Determines whether the twoRealLocalizableobjects have the same position, withdoubleprecision.- Parameters:
l1- The firstRealLocalizable.l2- The secondRealLocalizable.- Returns:
- True iff the positions are the same, including dimensionality.
- See Also:
RealLocalizable.getDoublePosition(int)
-
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, 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 ofIterable<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 ofValueEquals, to overrideObject.equals(Object).- See Also:
Object.equals(Object)
-
combineHash
public static int combineHash(int hash1, int hash2)
-
-