Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ResultsTable.java
1 package ij.measure;
2 import ij.*;
3 import ij.plugin.filter.Analyzer;
4 import ij.text.*;
5 import java.awt.*;
6 
12 public class ResultsTable {
13 
14  public static final int MAX_COLUMNS = 50;
15 
16  public static final int COLUMN_NOT_FOUND = -1;
17  public static final int COLUMN_IN_USE = -2;
18  public static final int TABLE_FULL = -3;
19 
20  public static final int AREA=0, MEAN=1, STD_DEV=2, MODE=3, MIN=4, MAX=5,
21  X_CENTROID=6, Y_CENTROID=7, X_CENTER_OF_MASS=8, Y_CENTER_OF_MASS=9,
22  PERIMETER=10, ROI_X=11, ROI_Y=12, ROI_WIDTH=13, ROI_HEIGHT=14,
23  MAJOR=15, MINOR=16, ANGLE=17, CIRCULARITY=18, FERET=19;
24 
25  private String[] headings = new String[MAX_COLUMNS];
26  private String[] defaultHeadings = {"Area","Mean","StdDev","Mode","Min","Max",
27  "X","Y","XM","YM","Perim.","BX","BY","Width","Height","Major","Minor","Angle",
28  "Circ.", "Feret"};
29  private int counter;
30  private float[][] columns = new float[MAX_COLUMNS][];
31  private String[] rowLabels;
32  private int maxRows = 100; // will be increased as needed
33  private int lastColumn = -1;
34  private StringBuffer sb;
35  private int precision = 3;
36  private String rowLabelHeading = "";
37 
39  public ResultsTable() {
40  for(int i=0; i<defaultHeadings.length; i++)
41  headings[i] = defaultHeadings[i];
42  }
43 
45  public static ResultsTable getResultsTable() {
46  return Analyzer.getResultsTable();
47  }
48 
49 
51  public synchronized void incrementCounter() {
52  counter++;
53  if (counter==maxRows) {
54  if (rowLabels!=null) {
55  String[] s = new String[maxRows*2];
56  System.arraycopy(rowLabels, 0, s, 0, maxRows);
57  rowLabels = s;
58  }
59  for (int i=0; i<MAX_COLUMNS; i++) {
60  if (columns[i]!=null) {
61  float[] tmp = new float[maxRows*2];
62  System.arraycopy(columns[i], 0, tmp, 0, maxRows);
63  columns[i] = tmp;
64  }
65  }
66  maxRows *= 2;
67  }
68  }
69 
71  public int getCounter() {
72  return counter;
73  }
74 
76  public void addValue(int column, double value) {
77  if ((column<0) || (column>=MAX_COLUMNS))
78  throw new IllegalArgumentException("Index out of range: "+column);
79  if (counter==0)
80  throw new IllegalArgumentException("Counter==0");
81  if (columns[column]==null) {
82  columns[column] = new float[maxRows];
83  if (headings[column]==null)
84  headings[column] = "---";
85  if (column>lastColumn) lastColumn = column;
86  }
87  columns[column][counter-1] = (float)value;
88  }
89 
92  public void addValue(String column, double value) {
93  int index = getColumnIndex(column);
94  if (index==COLUMN_NOT_FOUND) {
95  index = getFreeColumn(column);
96  if (index==TABLE_FULL)
97  throw new IllegalArgumentException("table is full");
98  }
99  addValue(index, value);
100  }
101 
103  public void addLabel(String columnHeading, String label) {
104  if (counter==0)
105  throw new IllegalArgumentException("Counter==0");
106  if (rowLabels==null)
107  rowLabels = new String[maxRows];
108  rowLabels[counter-1] = label;
109  if (columnHeading!=null)
110  rowLabelHeading = columnHeading;
111  }
112 
114  public void disableRowLabels() {
115  rowLabels = null;
116  }
117 
120  public float[] getColumn(int column) {
121  if ((column<0) || (column>=MAX_COLUMNS))
122  throw new IllegalArgumentException("Index out of range: "+column);
123  if (columns[column]==null)
124  return null;
125  else {
126  float[] data = new float[counter];
127  for (int i=0; i<counter; i++)
128  data[i] = columns[column][i];
129  return data;
130  }
131  }
132 
134  public boolean columnExists(int column) {
135  if ((column<0) || (column>=MAX_COLUMNS))
136  return false;
137  else
138  return columns[column]!=null;
139  }
140 
143  public int getColumnIndex(String heading) {
144  for(int i=0; i<headings.length; i++) {
145  if (headings[i]==null)
146  return COLUMN_NOT_FOUND;
147  else if (headings[i].equals(heading))
148  return i;
149  }
150  return COLUMN_NOT_FOUND;
151  }
152 
157  public int getFreeColumn(String heading) {
158  for(int i=0; i<headings.length; i++) {
159  if (headings[i]==null) {
160  columns[i] = new float[maxRows];
161  headings[i] = heading;
162  if (i>lastColumn) lastColumn = i;
163  return i;
164  }
165  if (headings[i].equals(heading))
166  return COLUMN_IN_USE;
167  }
168  return TABLE_FULL;
169  }
170 
175  public float getValue(int column, int row) {
176  if (columns[column]==null)
177  throw new IllegalArgumentException("Column not defined: "+column);
178  if (column>=MAX_COLUMNS || row>=counter)
179  throw new IllegalArgumentException("Index out of range: "+column+","+row);
180  return columns[column][row];
181  }
182 
188  public double getValue(String column, int row) {
189  if (row<0 || row>=getCounter())
190  throw new IllegalArgumentException("Row out of range");
191  int col = getColumnIndex(column);
192  if (col==COLUMN_NOT_FOUND)
193  throw new IllegalArgumentException("\""+column+"\" column not found");
194  //IJ.log("col: "+col+" "+(col==COLUMN_NOT_FOUND?"not found":""+columns[col]));
195  return getValue(col,row);
196  }
197 
203  public void setValue(String column, int row, double value) {
204  int col = getColumnIndex(column);
205  if (col==COLUMN_NOT_FOUND) {
206  col = getFreeColumn(column);
207  if (col==TABLE_FULL)
208  throw new IllegalArgumentException("Too many columns (>"+(MAX_COLUMNS-defaultHeadings.length)+")");
209  }
210  setValue(col, row, value);
211  }
212 
215  public void setValue(int column, int row, double value) {
216  if ((column<0) || (column>=MAX_COLUMNS))
217  throw new IllegalArgumentException("Column out of range: "+column);
218  if (row>=counter)
219  throw new IllegalArgumentException("row>=counter");
220  if (columns[column]==null) {
221  columns[column] = new float[maxRows];
222  if (column>lastColumn) lastColumn = column;
223  }
224  columns[column][row] = (float)value;
225  }
226 
228  public String getColumnHeadings() {
229  StringBuffer sb = new StringBuffer(200);
230  sb.append(" \t");
231  if (rowLabels!=null)
232  sb.append(rowLabelHeading + "\t");
233  String heading;
234  for (int i=0; i<=lastColumn; i++) {
235  if (columns[i]!=null) {
236  heading = headings[i];
237  if (heading==null) heading ="---";
238  sb.append(heading + "\t");
239  }
240  }
241  return new String(sb);
242  }
243 
245  public String getColumnHeading(int column) {
246  if ((column<0) || (column>=MAX_COLUMNS))
247  throw new IllegalArgumentException("Index out of range: "+column);
248  return headings[column];
249  }
250 
253  public String getRowAsString(int row) {
254  if ((row<0) || (row>=counter))
255  throw new IllegalArgumentException("Row out of range: "+row);
256  if (sb==null)
257  sb = new StringBuffer(200);
258  else
259  sb.setLength(0);
260  sb.append(Integer.toString(row+1));
261  sb.append("\t");
262  if (rowLabels!=null) {
263  if (rowLabels[row]!=null)
264  sb.append(rowLabels[row]);
265  sb.append("\t");
266  }
267  for (int i=0; i<=lastColumn; i++) {
268  if (columns[i]!=null)
269  sb.append(n(columns[i][row]));
270  }
271  return new String(sb);
272  }
273 
275  public void setHeading(int column, String heading) {
276  if ((column<0) || (column>=headings.length))
277  throw new IllegalArgumentException("Column out of range: "+column);
278  headings[column] = heading;
279  }
280 
282  public void setPrecision(int precision) {
283  this.precision = precision;
284  }
285 
286  String n(double n) {
287  String s;
288  if (Math.round(n)==n)
289  s = IJ.d2s(n,0);
290  else
291  s = IJ.d2s(n,precision);
292  return s+"\t";
293  }
294 
296  public synchronized void reset() {
297  counter = 0;
298  maxRows = 100;
299  for (int i=0; i<=lastColumn; i++) {
300  columns[i] = null;
301  if (i<defaultHeadings.length)
302  headings[i] = defaultHeadings[i];
303  else
304  headings[i] = null;
305  }
306  lastColumn = -1;
307  }
308 
311  public void show(String windowTitle) {
312  String tableHeadings = getColumnHeadings();
313  TextPanel tp;
314  if (windowTitle.equals("Results")) {
315  tp = IJ.getTextPanel();
316  if (tp==null) return;
317  IJ.setColumnHeadings(tableHeadings);
318  } else {
319  TextWindow win = new TextWindow(windowTitle, "", 300, 200);
320  tp = win.getTextPanel();
321  tp.setColumnHeadings(tableHeadings);
322  }
323  int n = getCounter();
324  if (n>0) {
325  StringBuffer sb = new StringBuffer(n*tableHeadings.length());
326  for (int i=0; i<n; i++)
327  sb.append(getRowAsString(i)+"\n");
328  tp.append(new String(sb));
329  }
330  }
331 
332  public String toString() {
333  return ("ctr="+counter+", hdr="+getColumnHeadings());
334  }
335 
336 }