View Javadoc
1   package com.github.celldynamics.quimp.utils.graphics.svg;
2   
3   import java.io.BufferedOutputStream;
4   import java.io.File;
5   import java.io.FileOutputStream;
6   import java.io.PrintWriter;
7   
8   import com.github.celldynamics.quimp.Outline;
9   import com.github.celldynamics.quimp.OutlineHandler;
10  import com.github.celldynamics.quimp.QColor;
11  import com.github.celldynamics.quimp.Vert;
12  import com.github.celldynamics.quimp.filesystem.FileExtensions;
13  import com.github.celldynamics.quimp.geom.ExtendedVector2d;
14  
15  import ij.IJ;
16  
17  /**
18   * SVG plotter used in QuimP. Left in this form for compatibility reason.
19   * 
20   * <P>TODO This whole class should end up at SVGwritter
21   * 
22   * @author rtyson
23   *
24   */
25  public class SVGplotter {
26  
27    /**
28     * The OutlineHandler to plot.
29     */
30    OutlineHandler oh;
31  
32    /**
33     * The out file.
34     */
35    File outFile;
36  
37    /**
38     * The scale.
39     */
40    double scale;
41  
42    /**
43     * The delta T.
44     */
45    double deltaT;
46  
47    /**
48     * The channel.
49     */
50    int channel;
51  
52    /**
53     * The color with.
54     */
55    String colorWith;
56  
57    /**
58     * The color map.
59     */
60    QColor[] colorMap;
61  
62    /**
63     * Instantiates a new SV gplotter.
64     *
65     * @param o the o
66     * @param t the t
67     * @param s the s
68     * @param c the c
69     * @param out the out
70     */
71    public SVGplotter(OutlineHandler o, double t, double s, int c, File out) {
72      oh = o;
73      deltaT = t;
74      scale = s;
75      outFile = out;
76      channel = c;
77    }
78  
79    /**
80     * Plot track ER.
81     *
82     * @param c the type of plot
83     */
84    public void plotTrackER(String c) {
85      // default
86      if (c == null || c.isEmpty()) {
87        colorWith = "Speed";
88      } else {
89        colorWith = c;
90      }
91      // System.out.println("max min: " + oH.maxM + ", " + oH.minM);
92  
93      int miny = (int) Math.floor(oh.minCoor.getY()) - 10;
94      int minx = (int) Math.floor(oh.minCoor.getX()) - 10;
95      int maxy = (int) Math.ceil(oh.maxCoor.getY()) + 10;
96      int maxx = (int) Math.ceil(oh.maxCoor.getX()) + 10;
97  
98      int width = maxx - minx;
99      int height = maxy - miny;
100 
101     try {
102 
103       BufferedOutputStream out = new BufferedOutputStream(
104               new FileOutputStream(outFile.getAbsolutePath() + FileExtensions.motvecimageFileExt));
105       PrintWriter osw = new PrintWriter(out);
106 
107       osw.write("<?xml version=\"1.0\" standalone=\"no\"?>\n");
108       osw.write("<svg width=\"15cm\" height=\"15cm\" viewBox=\"" + minx + " " + miny + " " + width
109               + " " + height + "\" "
110               + "xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\">\n");
111       osw.write("\n");
112 
113       osw.write("<rect x=\"" + minx + "\" y=\"" + miny + "\" width=\"" + width + "\" height=\""
114               + height + "\" " + "style=\"fill:rgb(0,0,0);stroke-width:0;"
115               + "stroke:rgb(0,0,0)\"/>\n\n");
116 
117       double t = 0;
118       double dur = 0.2;
119       Outline o;
120 
121       // colorMap = QColor.colourMap("Summer", oH.getSize());
122       for (int i = 0; i < oh.getSize(); i++) {
123         o = oh.indexGetOutline(i);
124         t = (double) i * dur;
125         plotVerts(osw, o, t, dur);
126       }
127 
128       osw.write("</svg>\n");
129       osw.close();
130 
131     } catch (Exception e) {
132       e.printStackTrace();
133     }
134   }
135 
136   /**
137    * Plot track anim.
138    */
139   @Deprecated
140   public void plotTrackAnim() {
141 
142     // oH.minCoor.print("minCoor:");
143     // oH.maxCoor.print("maxCoor:");
144     int miny = (int) Math.floor(oh.minCoor.getY()) - 10;
145     int minx = (int) Math.floor(oh.minCoor.getX()) - 10;
146     int maxy = (int) Math.ceil(oh.maxCoor.getY()) + 10;
147     int maxx = (int) Math.ceil(oh.maxCoor.getX()) + 10;
148 
149     int width = maxx - minx;
150     int height = maxy - miny;
151 
152     try {
153 
154       BufferedOutputStream out = new BufferedOutputStream(
155               new FileOutputStream(outFile.getAbsolutePath() + "_trackAnim.svg"));
156       PrintWriter osw = new PrintWriter(out);
157 
158       osw.write("<?xml version=\"1.0\" standalone=\"no\"?>\n");
159       osw.write("<svg width=\"15cm\" height=\"15cm\" viewBox=\"" + minx + " " + miny + " " + width
160               + " " + height + "\" "
161               + "xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\">\n");
162       osw.write("\n");
163 
164       osw.write("<rect x=\"" + minx + "\" y=\"" + miny + "\" width=\"" + width + "\" height=\""
165               + height + "\" " + "style=\"fill:rgb(0,0,0);stroke-width:0;"
166               + "stroke:rgb(0,0,0)\"/>\n\n");
167 
168       Outline o;
169       double t;
170       double dur = 0.1;
171 
172       colorMap = QColor.colourMap("Summer", oh.getSize());
173       for (int i = 0; i < oh.getSize(); i++) {
174         o = oh.getStoredOutline(i);
175         t = (double) i * dur;
176         plotOutline(osw, o, colorMap[i].getColorSVG(), true, t, dur);
177       }
178 
179       osw.write("</svg>\n");
180       osw.close();
181 
182     } catch (Exception e) {
183       e.printStackTrace();
184     }
185   }
186 
187   /**
188    * Plot track.
189    *
190    * @param trackColor the track color
191    * @param increment the increment
192    */
193   public void plotTrack(String trackColor, int increment) {
194 
195     // oH.minCoor.print("minCoor:");
196     // oH.maxCoor.print("maxCoor:");
197     int miny = (int) Math.floor(oh.minCoor.getY()) - 15;
198     int minx = (int) Math.floor(oh.minCoor.getX()) - 10;
199     int maxy = (int) Math.ceil(oh.maxCoor.getY()) + 10;
200     int maxx = (int) Math.ceil(oh.maxCoor.getX()) + 10;
201 
202     int width = maxx - minx;
203     int height = maxy - miny;
204 
205     try {
206 
207       BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(
208               outFile.getAbsolutePath() + FileExtensions.trackvecimageFileExt));
209       PrintWriter osw = new PrintWriter(out);
210 
211       osw.write("<?xml version=\"1.0\" standalone=\"no\"?>\n");
212       osw.write("<svg width=\"15cm\" height=\"15cm\" viewBox=\"" + minx + " " + miny + " " + width
213               + " " + height + "\" "
214               + "xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\">\n");
215       osw.write("\n");
216 
217       osw.write("<rect x=\"" + minx + "\" y=\"" + miny + "\" width=\"" + width + "\" height=\""
218               + height + "\" " + "style=\"fill:rgb(0,0,0);stroke-width:0;"
219               + "stroke:rgb(0,0,0)\"/>\n\n");
220 
221       Outline o;
222       int colSize = (int) Math.ceil(oh.getSize() / (double) increment);
223 
224       colorMap = QColor.colourMap(trackColor, colSize);
225       int count = 0;
226       for (int i = 0; i < oh.getSize(); i += increment) {
227         o = oh.indexGetOutline(i);
228         plotOutline(osw, o, colorMap[count].getColorSVG(), false, 0, 0);
229         count++;
230       }
231 
232       int barValue = (int) Math.round((width / 5d) * scale);
233       SVGwritter.QScaleBar scaleBar = new SVGwritter.QScaleBar(
234               new ExtendedVector2d(minx + 5, miny + 8), "&#x3BC;m", barValue, scale);
235       scaleBar.thickness = 1;
236       scaleBar.colour.setRGB(1, 1, 1);
237       scaleBar.draw(osw);
238 
239       osw.write("\n</svg>\n");
240       osw.close();
241 
242     } catch (Exception e) {
243       e.printStackTrace();
244     }
245   }
246 
247   private void plotOutline(PrintWriter osw, Outline o, String colour, boolean anim, double t,
248           double dur) throws Exception {
249     Vert v = o.getHead();
250     osw.write("<polyline ");
251     if (anim) {
252       osw.write("display=\"none\" ");
253     }
254     osw.write("fill=\"none\" style=\"stroke:" + colour + ";stroke-width:" + 0.5 + "\" points=\"\n");
255     do {
256       osw.write(IJ.d2s(v.getX(), 6) + "," + IJ.d2s(v.getY(), 6) + "\n");
257       v = v.getNext();
258     } while (!v.isHead());
259     osw.write(IJ.d2s(v.getX(), 6) + "," + IJ.d2s(v.getY(), 6));
260 
261     if (anim) {
262       osw.write("\" >\n");
263       osw.write("<animate id='frame_" + IJ.d2s(t) + "' attributeName='display' values='inline;none'"
264               + " dur='" + dur + "s' fill='freeze' begin=\"" + IJ.d2s(t)
265               + "s\" repeatCount=\"1\"/>\n");
266       osw.write("</polyline>");
267     } else {
268       osw.write("\"/>\n");
269     }
270     osw.write("\n");
271   }
272 
273   private void plotVerts(PrintWriter osw, Outline o, double t, double dur) throws Exception {
274     osw.write("<g id=\"verts_1\" display=\"none\">\n");
275     Vert v = o.getHead();
276     QColor erColour;
277     do {
278       erColour = getERcolor(v);
279       osw.write("<circle cx=\"" + IJ.d2s(v.getX(), 6) + "\" cy=\"" + IJ.d2s(v.getY(), 6) + "\" "
280               + "r=\"0.7\" stroke-width=\"0\" fill=\"" + erColour.getColorSVG() + "\"/>");
281       v = v.getNext();
282     } while (!v.isHead());
283 
284     osw.write("\n");
285     osw.write("<animate id='frame_" + IJ.d2s(t) + "' attributeName='display' values='inline;none'"
286             + " dur='" + dur + "s' fill='freeze' begin=\"" + IJ.d2s(t) + "s\" repeatCount=\"1\"/>");
287     osw.write("\n</g>\n\n");
288   }
289 
290   private QColor getERcolor(Vert v) {
291     if (colorWith.matches("Speed")) {
292       return QColor.erColorMap2("rwb", v.distance, oh.migLimits[0], oh.migLimits[1]);
293     } else if (colorWith.matches("Fluorescence")) {
294       return QColor.rwbMap(v.fluores[channel].intensity, 255, 0);
295     } else if (colorWith.matches("Convexity")) {
296       return QColor.erColorMap2("rwb", v.curvatureSum, oh.curvLimits[0], oh.curvLimits[1]);
297     } else {
298       System.out.println("unknown color map: SVGplotter l:221");
299       return new QColor(1, 1, 1);
300     }
301 
302   }
303 }