1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
package ij.util;
import java.awt.Color;
import java.util.*;
import java.io.*;
/** This class contains static utility methods. */
public class Tools {
/** This array contains the 16 hex digits '0'-'F'. */
public static final char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/** Converts a Color to an 7 byte hex string starting with '#'. */
public static String c2hex(Color c) {
int i = c.getRGB();
char[] buf7 = new char[7];
buf7[0] = '#';
for (int pos=6; pos>=1; pos--) {
buf7[pos] = hexDigits[i&0xf];
i >>>= 4;
}
return new String(buf7);
}
/** Converts a float to an 9 byte hex string starting with '#'. */
public static String f2hex(float f) {
int i = Float.floatToIntBits(f);
char[] buf9 = new char[9];
buf9[0] = '#';
for (int pos=8; pos>=1; pos--) {
buf9[pos] = hexDigits[i&0xf];
i >>>= 4;
}
return new String(buf9);
}
public static double[] getMinMax(double[] a) {
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
double value;
for (int i=0; i<a.length; i++) {
value = a[i];
if (value<min)
min = value;
if (value>max)
max = value;
}
double[] minAndMax = new double[2];
minAndMax[0] = min;
minAndMax[1] = max;
return minAndMax;
}
public static double[] getMinMax(float[] a) {
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
double value;
for (int i=0; i<a.length; i++) {
value = a[i];
if (value<min)
min = value;
if (value>max)
max = value;
}
double[] minAndMax = new double[2];
minAndMax[0] = min;
minAndMax[1] = max;
return minAndMax;
}
/** Converts the float array 'a' to a double array. */
public static double[] toDouble(float[] a) {
int len = a.length;
double[] d = new double[len];
for (int i=0; i<len; i++)
d[i] = a[i];
return d;
}
/** Converts the double array 'a' to a float array. */
public static float[] toFloat(double[] a) {
int len = a.length;
float[] f = new float[len];
for (int i=0; i<len; i++)
f[i] = (float)a[i];
return f;
}
/** Converts carriage returns to line feeds. */
public static String fixNewLines(String s) {
char[] chars = s.toCharArray();
for (int i=0; i<chars.length; i++)
{if (chars[i]=='\r') chars[i] = '\n';}
return new String(chars);
}
/**
* Returns a double containg the value represented by the
* specified <code>String</code>.
*
* @param s the string to be parsed.
* @param defaultValue the value returned if <code>s</code>
* does not contain a parsable double
* @return The double value represented by the string argument or
* <code>defaultValue</code> if the string does not contain a parsable double
*/
public static double parseDouble(String s, double defaultValue) {
if (s==null) return defaultValue;
try {
defaultValue = Double.parseDouble(s);
} catch (NumberFormatException e) {}
return defaultValue;
}
/**
* Returns a double containg the value represented by the
* specified <code>String</code>.
*
* @param s the string to be parsed.
* @return The double value represented by the string argument or
* Double.NaN if the string does not contain a parsable double
*/
public static double parseDouble(String s) {
return parseDouble(s, Double.NaN);
}
/** Returns the number of decimal places need to display two numbers. */
public static int getDecimalPlaces(double n1, double n2) {
if (Math.round(n1)==n1 && Math.round(n2)==n2)
return 0;
else {
n1 = Math.abs(n1);
n2 = Math.abs(n2);
double n = n1<n2&&n1>0.0?n1:n2;
double diff = Math.abs(n2-n1);
if (diff>0.0 && diff<n) n = diff;
int digits = 2;
if (n<100.0) digits = 3;
if (n<0.1) digits = 4;
if (n<0.01) digits = 5;
if (n<0.001) digits = 6;
if (n<0.0001) digits = 7;
return digits;
}
}
/** Splits a string into substrings using the default delimiter set,
which is " \t\n\r" (space, tab, newline and carriage-return). */
public static String[] split(String str) {
return split(str, " \t\n\r");
}
/** Splits a string into substring using the characters
contained in the second argument as the delimiter set. */
public static String[] split(String str, String delim) {
if (delim.equals("\n"))
return splitLines(str);
StringTokenizer t = new StringTokenizer(str, delim);
int tokens = t.countTokens();
String[] strings;
if (tokens>0) {
strings = new String[tokens];
for(int i=0; i<tokens; i++)
strings[i] = t.nextToken();
} else {
strings = new String[1];
strings[0] = str;
tokens = 1;
}
return strings;
}
static String[] splitLines(String str) {
Vector v = new Vector();
try {
BufferedReader br = new BufferedReader(new StringReader(str));
String line;
while (true) {
line = br.readLine();
if (line == null) break;
v.addElement(line);
}
br.close();
} catch(Exception e) { }
String[] lines = new String[v.size()];
v.copyInto((String[])lines);
return lines;
}
//Modified from: http://stackoverflow.com/questions/951848 N.Vischer
// quicksort a[left] to a[right]
public static void quicksort(double[] a, int[] index) {
quicksort(a, index, 0, a.length-1);
}
public static void quicksort(double[] a, int[] index, int left, int right) {
if (right <= left) return;
int i = partition(a, index, left, right);
quicksort(a, index, left, i-1);
quicksort(a, index, i+1, right);
}
// partition a[left] to a[right], assumes left < right
private static int partition(double[] a, int[] index,
int left, int right) {
int i = left - 1;
int j = right;
while (true) {
while (a[++i] < a[right]) // find item on left to swap
; // a[right] acts as sentinel
while (!(a[right] >= a[--j])) // find item on right to swap (NAN TRICK)
if (j == left) break; // don't go out-of-bounds
if (i >= j) break; // check if pointers cross
exch(a, index, i, j); // swap two elements into place
}
exch(a, index, i, right); // swap with partition element
return i;
}
// exchange a[i] and a[j]
private static void exch(double[] a, int[] index, int i, int j) {
double swap = a[i];
a[i] = a[j];
a[j] = swap;
int b = index[i];
index[i] = index[j];
index[j] = b;
}
// quicksort a[left] to a[right]
public static void quicksort(String[] a, int[] index) {
quicksort(a, index, 0, a.length-1);
}
public static void quicksort(String[] a, int[] index, int left, int right) {
if (right <= left) return;
int i = partition(a, index, left, right);
quicksort(a, index, left, i-1);
quicksort(a, index, i+1, right);
}
// partition a[left] to a[right], assumes left < right
private static int partition(String[] a, int[] index,
int left, int right) {
int i = left - 1;
int j = right;
while (true) {
while (a[++i].compareToIgnoreCase( a[right])<0) // find item on left to swap
; // a[right] acts as sentinel
while (a[right].compareToIgnoreCase( a[--j])<0) // find item on right to swap
if (j == left) break; // don't go out-of-bounds
if (i >= j) break; // check if pointers cross
exch(a, index, i, j); // swap two elements into place
}
exch(a, index, i, right); // swap with partition element
return i;
}
// exchange a[i] and a[j]
private static void exch(String[] a, int[] index, int i, int j) {
String swap = a[i];
a[i] = a[j];
a[j] = swap;
int b = index[i];
index[i] = index[j];
index[j] = b;
}
}
|