1 package JSci.instruments; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import javax.swing.border.*; 8 import java.text.*; 9 10 11 12 public class PTTwoDBarycentre extends ImageFilterAdapter implements ParticleTracker { 13 14 15 public static boolean INTERLACED = true; 16 17 public static int TIME_FIELD = 20; 18 19 private JComboBox crossCombo = new JComboBox(); 20 private static long startTime = System.currentTimeMillis();; 21 22 23 public String getName() { return "PT TwoD Barycentre"; } 24 25 private ParticleTrackerListener ptl = null; 26 public void setListener(ParticleTrackerListener ptl) { 27 this.ptl=ptl; 28 } 29 30 public String toString() { 31 return "Particle Tracking; INTERLACED="+INTERLACED+ 32 "; ALPHA="+PTTwoDBarycentreCross.ALPHA+ 33 "; WEIGHT_LIGHT_PART="+PTTwoDBarycentreCross.WEIGHT_LIGHT_PART+ 34 "; REGION_SPEED="+PTTwoDBarycentreCross.REGION_SPEED+ 35 "; ODD_EVEN="+PTTwoDBarycentreCross.ODD_EVEN+ 36 "; startTime="+startTime; 37 } 38 39 40 public void filter(Image f) { 41 ListModel l = crossCombo.getModel(); 42 if (l.getSize()==0) return; 43 int[] n=new int[l.getSize()]; 44 double[] x=new double[l.getSize()]; 45 double[] y=new double[l.getSize()]; 46 for (int j=0;j<l.getSize();j++) { 47 PTTwoDBarycentreCross cross = (PTTwoDBarycentreCross)l.getElementAt(j); 48 if (INTERLACED) cross.findInterlaced(f); 49 else cross.find(f); 50 n[j]=cross.getN(); 51 x[j]=cross.getX(); 52 y[j]=cross.getY(); 53 } 54 if (ptl!=null) ptl.receivePosition(f.getTimeStamp()-startTime,n,x,y,null); 55 if (!INTERLACED) return; 56 for (int j=0;j<l.getSize();j++) { 57 PTTwoDBarycentreCross cross = (PTTwoDBarycentreCross)l.getElementAt(j); 58 x[j]=cross.getX2(); 59 y[j]=cross.getY2(); 60 } 61 if (ptl!=null) ptl.receivePosition(f.getTimeStamp()+TIME_FIELD-startTime,n,x,y,null); 62 } 63 64 JComponent comp; 65 66 public Component getFilterControlComponent() { 67 if (comp!=null) return comp; 68 JPanel comp = new JPanel(); 69 comp.setLayout(new BorderLayout()); 70 JButton addButton = new JButton("Add"); 72 comp.add(BorderLayout.NORTH,addButton); 73 addButton.addActionListener(new ActionListener() { 74 public void actionPerformed(ActionEvent e) { 75 if (theROI==null) return; 76 PTTwoDBarycentreCross cross = new PTTwoDBarycentreCross((Rectangle)theROI.getShape()); 77 crossCombo.addItem(cross); 78 } 79 }); 80 JButton removeButton = new JButton("Remove"); 82 comp.add(BorderLayout.SOUTH,removeButton); 83 removeButton.addActionListener(new ActionListener() { 84 public void actionPerformed(ActionEvent e) { 85 crossCombo.removeItem(crossCombo.getSelectedItem()); 86 } 87 }); 88 crossCombo.setEditable(false); 90 comp.add(BorderLayout.EAST,crossCombo); 91 return comp; 93 } 94 95 private ROI theROI = null; 96 97 public void setROI(ROI r) { 98 theROI = r; 99 } 100 101 public static void main(String [] args) { 102 ROI r = new RectangularROI(10,10,30,30); 103 PTTwoDBarycentre f = new PTTwoDBarycentre(); 104 Player p = new Player(); 105 ImageSource fs = new SimulatedBarycentreSource(); 106 fs.setSink(f); 107 f.setSink(p); 108 p.addROI(r); 109 f.setROI(r); 110 p.start(); 111 112 final NumberFormat formatter = NumberFormat.getNumberInstance(); 113 { formatter.setMaximumFractionDigits(3); 114 formatter.setMinimumFractionDigits(1); 115 formatter.setMinimumIntegerDigits(1); 116 formatter.setMaximumIntegerDigits(4); 117 } 118 119 ParticleTrackerListener ptl=new ParticleTrackerListener() { 120 public void receivePosition(long time,int[] n,double[] x,double[] y,double[] z) { 121 System.out.print(time); 122 for (int j=0;j<n.length;j++) 123 System.out.print(" "+n[j]+" "+formatter.format(x[j])+" "+formatter.format(y[j])); 124 System.out.println(); 125 } 126 }; 127 128 f.setListener(ptl); 129 130 } 131 132 133 134 135 136 137 138 139 140 141 142 145 static class SimulatedBarycentreSource implements ImageSource,Runnable { 146 147 Dimension dim = new Dimension(130,97); 148 149 public SimulatedBarycentreSource() { 150 Thread t = new Thread (this); 151 t.setDaemon(true); 152 t.start(); 153 } 154 155 private ImageSink sink; 156 159 public void setSink(ImageSink fs) { 160 if (sink!=fs) { 161 sink=fs; 162 sink.setSource(this); 163 } 164 } 165 166 167 private static final double SIGMA = 4.0; 168 private static final double DELTAX = 4.0; 169 private static final double DELTAY = 2.0; 170 private double f(double x,double y) { 171 return 172 Math.exp(-(x*x+y*y)/(2.0*SIGMA*SIGMA))- 173 Math.exp(-((x-DELTAX)*(x-DELTAX)+(y-DELTAY)*(y-DELTAY))/(2.0*SIGMA*SIGMA)); 174 } 175 176 public void run() { 177 int num = 0; 178 while (true) { 179 final int n = num++; 180 final long t = System.currentTimeMillis(); 181 final double x0 = Math.random()*4+40.0; 182 final double y0 = Math.random()*4+50.0; 183 final byte[] im = new byte[getWidth()*getHeight()]; 184 final Dimension dim = new Dimension(getWidth(),getHeight()); 185 Image f = new Image() { 186 public byte[] getData() { return im; } 187 public Dimension getSize() { return dim; } 188 public long getTimeStamp() { return t; } 189 }; 190 191 for (int j=0;j<f.getWidth();j++) 192 for (int k=0;k<f.getHeight();k++) 193 f.getData()[j+k*f.getWidth()] = (byte) (128.0+128.0*SimulatedBarycentreSource.this.f(j-x0,k-y0)); 194 195 if (sink!=null) sink.receive(f); 196 try { Thread.sleep(100); } 197 catch (InterruptedException e) {} 198 } 199 } 200 201 202 public int getWidth() { return dim.width; } 203 204 205 public int getHeight() { return dim.height; } 206 207 208 public Dimension getSize() { return dim; } 209 210 212 public Component getControlComponent() { 213 JPanel p = new JPanel(); 214 p.add(new JLabel("Test")); 215 Border etched = BorderFactory.createEtchedBorder(); 216 Border titled = BorderFactory.createTitledBorder(etched,"source"); 217 p.setBorder(titled); 218 return p; 219 } 220 221 } 222 } 223 224 225 226 | Popular Tags |