View Javadoc
1   package com.github.celldynamics.quimp;
2   
3   import java.awt.Polygon;
4   import java.io.File;
5   
6   import com.github.celldynamics.quimp.geom.ExtendedVector2d;
7   
8   import ij.IJ;
9   import ij.ImagePlus;
10  import ij.WindowManager;
11  import ij.gui.Overlay;
12  import ij.gui.PolygonRoi;
13  import ij.gui.Roi;
14  import ij.measure.Measurements;
15  import ij.plugin.PlugIn;
16  import ij.process.ImageProcessor;
17  import ij.process.ImageStatistics;
18  
19  /**
20   * Inserts an image or stack into a stack.
21   */
22  @Deprecated
23  public class Test_QuimP implements PlugIn {
24  
25    private static final int m =
26            Measurements.AREA + Measurements.INTEGRATED_DENSITY + Measurements.MEAN;
27  
28    /*
29     * (non-Javadoc)
30     * 
31     * @see ij.plugin.PlugIn#run(java.lang.String)
32     */
33    @Override
34    public void run(String arg) {
35      // testAngle();
36      // testOverlay();
37      testLineIntersect();
38      // testCutSelf();
39      // this.testClosest();
40      // this.testArea();
41  
42      // this.test3Dout();
43      // this.colorConvert();
44    }
45  
46    /**
47     * Test area.
48     */
49    void testArea() {
50      ImagePlus imp = WindowManager.getCurrentImage();
51      if (imp == null) {
52        IJ.noImage();
53        return;
54      }
55  
56      imp.setSlice(1);
57      ImageProcessor impProc = imp.getProcessor();
58  
59      Polygon outerPoly = new Polygon();
60      outerPoly.addPoint(11, 11);
61      outerPoly.addPoint(11, 110);
62      outerPoly.addPoint(110, 11);
63  
64      Polygon innerPoly = new Polygon();
65      innerPoly.addPoint(110, 110);
66      innerPoly.addPoint(140, 120);
67      innerPoly.addPoint(60, 130);
68  
69      PolygonRoi outerRoi = new PolygonRoi(outerPoly, Roi.POLYGON);
70      impProc.setRoi(outerRoi);
71  
72      ImageStatistics is = ImageStatistics.getStatistics(impProc, m, null);
73  
74      System.out.println("Area=" + is.area + ", mean=" + is.mean);
75  
76      /*
77       * //ImageProcessor orgIp = orgStack.getProcessor(i + 1);
78       * 
79       * PolygonFiller pf = new PolygonFiller(); pf.setPolygon(roi.getXCoordinates(),
80       * roi.getYCoordinates(), roi.getNCoordinates()); Rectangle b = roi.getBounds();
81       * orgIp.setRoi(poly); orgIp.setMask(pf.getMask(b.width, b.height)); ImageStatistics is =
82       * ImageStatistics.getStatistics(orgIp, m, null);
83       */
84  
85    }
86  
87    /**
88     * Test angle.
89     */
90    void testAngle() {
91  
92      Vert a, b, c;
93  
94      a = new Vert(10, 10, 1);
95      b = new Vert(15, 15, 2);
96      c = new Vert(10, 15, 3);
97  
98      ExtendedVector2d edge1 = ExtendedVector2d.vecP2P(b.getPoint(), a.getPoint());
99  
100     ExtendedVector2d edge2 = ExtendedVector2d.vecP2P(b.getPoint(), c.getPoint());
101 
102     double curvature = ExtendedVector2d.angle(edge1, edge2) * (180 / Math.PI);
103 
104     System.out.println("curv = " + curvature);
105   }
106 
107   /**
108    * Test qparams.
109    *
110    * @throws QuimpException the quimp exception
111    */
112   void testQparams() throws QuimpException {
113     QParams p =
114             new QParams(new File("/Users/rtyson/Documents/phd/tmp/smallStack/SmallStack_0.paQP"));
115     p.readParams();
116     System.out.println("" + p.nmax);
117     System.out.println("" + p.getPath());
118     System.out.println("" + p.getFileName());
119     System.out.println("" + p.getConvexFile());
120 
121     // p.setParamFile(new
122     // File("/Users/rtyson/Documents/phd/tmp/smallStack/SmallStack_NEW.paQP"));
123     // p.writeParams();
124   }
125 
126   /**
127    * Test overlay.
128    */
129   void testOverlay() {
130 
131     ImagePlus imp = IJ.getImage();
132 
133     PolygonRoi roiP = (PolygonRoi) imp.getRoi();
134 
135     Polygon p = roiP.getPolygon();
136     Polygon np = new Polygon();
137 
138     float[] xf = new float[p.npoints];
139     float[] yf = new float[p.npoints];
140 
141     for (int i = 0; i < p.npoints; i++) {
142       np.addPoint(p.xpoints[i] + 10, p.ypoints[i] + 10);
143       xf[i] = p.xpoints[i] + 10.4f;
144       yf[i] = p.ypoints[i] + 10.4f;
145     }
146 
147     Overlay ov = new Overlay();
148     PolygonRoi intRoi = new PolygonRoi(np, Roi.POLYGON);
149     // FloatRoi floRoi = new FloatRoi(xf, yf);
150     ov.add(intRoi);
151     // ov.add(floRoi);
152     imp.setOverlay(ov);
153 
154     // floRoi.setIntPoints();
155     // imp.getCanvas().getImage().setRoi(floRoi);
156   }
157 
158   private void testLineIntersect() {
159     System.out.println("Test line intersection");
160     // Vect2d a = new Vect2d(238.8343223897114,180.57548291017963);
161     // Vect2d b = new Vect2d(252.3128980477469,179.3057722538331);
162 
163     // Vect2d c = new Vect2d(248.21769647653375,383.1254314892506);
164     // Vect2d d = new Vect2d(248.22719691475632,395.7869644627188);
165 
166     ExtendedVector2dm/ExtendedVector2d.html#ExtendedVector2d">ExtendedVector2d a = new ExtendedVector2d(238, 180);
167     ExtendedVector2dm/ExtendedVector2d.html#ExtendedVector2d">ExtendedVector2d b = new ExtendedVector2d(252, 179);
168 
169     ExtendedVector2dm/ExtendedVector2d.html#ExtendedVector2d">ExtendedVector2d c = new ExtendedVector2d(248.217, 383);
170     ExtendedVector2dm/ExtendedVector2d.html#ExtendedVector2d">ExtendedVector2d d = new ExtendedVector2d(248.227, 395);
171 
172     double[] intersect = new double[2];
173 
174     int state = ExtendedVector2d.segmentIntersection(a.getX(), a.getY(), b.getX(), b.getY(),
175             c.getX(), c.getY(), d.getX(), d.getY(), intersect);
176     System.out.println("Sate: " + state);
177     if (state == -1) {
178       System.out.println("\nLines parallel");
179       System.out.println("close all;plot([" + a.getX() + "," + b.getX() + "],[" + a.getY() + ","
180               + b.getY() + "],'-ob');"); // matlab output
181       System.out.println("hold on; plot([" + c.getX() + "," + d.getX() + "],[" + c.getY() + ","
182               + d.getY() + "],'-or');");
183 
184     } else if (state == -2) {
185       System.out.println("\nLines parallel and overlap");
186       System.out.println("close all;plot([" + a.getX() + "," + b.getX() + "],[" + a.getY() + ","
187               + b.getY() + "],'-ob');"); // matlab output
188       System.out.println("hold on; plot([" + c.getX() + "," + d.getX() + "],[" + c.getY() + ","
189               + d.getY() + "],'-or');");
190       System.out.println("plot(" + intersect[0] + "," + intersect[1] + ", 'og');");
191     } else if (state == 1) {
192       System.out.println("\nLines intersect at " + intersect[0] + ", " + intersect[1]);
193       System.out.println("close all;plot([" + a.getX() + "," + b.getX() + "],[" + a.getY() + ","
194               + b.getY() + "],'-ob');"); // matlab output
195       System.out.println("hold on; plot([" + c.getX() + "," + d.getX() + "],[" + c.getY() + ","
196               + d.getY() + "],'-or');");
197       System.out.println("plot(" + intersect[0] + "," + intersect[1] + ", 'og');");
198     }
199 
200   }
201 
202   /*
203    * private void test3Dout(){
204    * 
205    * QParams qp = new QParams(new File(
206    * "/Users/rtyson/Documents/phd/tmp/test/QuimP11analysis/SmallStack_0.paQP") ); qp.readParams();
207    * OutlineHandler oH = new OutlineHandler(qp); if (!oH.readSuccess) { return; }else{
208    * System.out.println( "Contours read in. Size: " + oH.getSize());
209    * 
210    * }
211    * 
212    * int res = 150; Point3f[] coords= new Point3f[ res * oH.getSize()]; Color3f[] colors = new
213    * Color3f[res * oH.getSize()]; int coCount = 0;
214    * 
215    * Vect2d centre = oH.getOutline(1).getCentroid(); centre.setX(centre.getX()*-1);
216    * centre.setY(centre.getY()*-1);
217    * 
218    * Outline o, op; Vect2d pHead; for(int i = 0; i < oH.getSize(); i++){ o =
219    * oH.indexGetOutline(i); o.setResolutionN(res); if(i!=0){ op = oH.indexGetOutline(i-1); pHead =
220    * op.getHead().getPoint(); o.setHeadclosest(pHead); }
221    * 
222    * Vert n = o.getHead(); do{ coords[coCount] = new
223    * Point3f((float)(n.getX()+centre.getX()),(i*1.0f),(float)(n.getY()+centre. getY()));
224    * colors[coCount] = new Color3f(0.5f,(float)Math.random(),0f);
225    * 
226    * 
227    * coCount++; n = n.getPrev(); // go tuther way round to point normals outwards
228    * }while(!n.isHead()); }
229    * 
230    * int[] indices= new int[(res*4)* (oH.getSize()-1)]; System.out.println(
231    * "indeices length: "+indices.length);
232    * 
233    * int base; for (int i = 0; i < indices.length; i+=4) {
234    * 
235    * base = i / 4;
236    * 
237    * indices[i] = base; indices[i+1] = base+1; indices[i+2] = base+1+res; indices[i+3] = base +
238    * res;
239    * 
240    * if(((i-((res-1)*4)))%(4*res) == 0){ indices[i+1] = base+1-res; indices[i+2] = base+1;
241    * //System.out.print("\twraped: "); }
242    * 
243    * //System.out.println(""+indices[i] +"," +indices[i+1]+"," +indices[i+2]+"," +indices[i+3]); }
244    * 
245    * 
246    * System.out.println("coord length" + coords.length); System.out.println( "max index = "+
247    * Tool.arrayMax(indices));
248    * 
249    * /*
250    * 
251    * //biuld a simple quad Point3f[] coords={ new Point3f(-0.2f,0.2f,-0.2f), new
252    * Point3f(-0.2f,-0.2f,-0.2f), new Point3f(0.2f,-0.2f,-0.2f), new Point3f(0.2f,0.2f,-0.2f), new
253    * Point3f(-0.2f,0.2f,0.2f), new Point3f(-0.2f,-0.2f,0.2f), new Point3f(0.2f,-0.2f,0.2f), new
254    * Point3f(0.2f,0.2f,0.2f) };
255    * 
256    * int[] indices={ 0,1,2,3, 5,6,7,4, 1,5,6,2, 0,4,7,3, 1,5,4,0, 2,6,7,3 };
257    * 
258    * //QuadArray quads = new QuadArray(4,GeometryArray.COORDINATES); //quads.setCoordinates(0,
259    * coords);
260    * 
261    * //IndexedLineArray lineArray=new IndexedLineArray(coords.length,
262    * //IndexedQuadArray.COORDINATES,indices.length); //lineArray.setCoordinates(0,coords);
263    * //lineArray.setCoordinateIndices(0,indices);
264    * 
265    * IndexedQuadArray quadArray = new IndexedQuadArray(coords.length, IndexedQuadArray.COORDINATES
266    * | QuadArray.COLOR_3,indices.length); quadArray.setCoordinates(0,coords);
267    * quadArray.setCoordinateIndices(0,indices); quadArray.setColors(0, colors);
268    * quadArray.setColorIndices(0, indices);
269    * 
270    * //Appearance a = new Appearance(); //PolygonAttributes pAttr = new PolygonAttributes(1, 0,
271    * 0.1f); // a.setPolygonAttributes(pAttr);
272    * 
273    * Shape3D myShape = new Shape3D(quadArray);
274    * 
275    * BranchGroup bg = new BranchGroup(); bg.addChild(myShape); VRML97Saver saver = new
276    * VRML97Saver();
277    * 
278    * saver.setBranchGroup(bg); saver.save("/temp/cube_A.wrl");
279    * 
280    * 
281    * 
282    * }
283    */
284   // private void colorConvert(){
285   //
286   // QColor c = new QColor( 0.6d,0.2d,0.4d);
287   // int ci= c.getColorInt();
288   // System.out.println("col int: " + ci);
289   // Color3f cf = QColor.colorInt23f(ci);
290   // System.out.println("r: " + cf.x + ", g: " + cf.y + ", b: " + cf.z);
291   // }
292 }