001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 005 package graphlab.extensions.io; 006 007 import graphlab.graph.graph.EdgeModel; 008 import graphlab.graph.graph.GraphModel; 009 import graphlab.graph.graph.VertexModel; 010 import graphlab.platform.parameter.Parameter; 011 import graphlab.platform.parameter.Parametrizable; 012 import graphlab.plugins.main.saveload.core.GraphIOException; 013 import graphlab.plugins.main.saveload.core.extension.GraphWriterExtension; 014 015 import java.io.File; 016 import java.io.FileWriter; 017 import java.io.IOException; 018 import java.util.Iterator; 019 020 /** 021 * @author Pooia Ronagh 022 */ 023 public class LatexCAD implements GraphWriterExtension, Parametrizable { 024 025 public String getName() { 026 return "LatexCAD"; 027 } 028 029 public String getExtension() { 030 return "pic"; 031 } 032 033 public double min(double a, double b) { 034 if (a < b) return a; 035 else return b; 036 } 037 038 public double max(double a, double b) { 039 if (a > b) return a; 040 else return b; 041 } 042 043 @Parameter(name = "Width", description = "Width in percent.") 044 public Double X = 0.0; 045 @Parameter(name = "Height", description = "Height in percent.") 046 public Double Y = 0.0; 047 048 public void write(File file, GraphModel graph) throws GraphIOException { 049 FileWriter output = null; 050 try { 051 output = new FileWriter(file); 052 output.write("% Drawing generated by GraphLab - requires latexcad.sty which can be found in lib\\latexcad.zip, also you can get the latest version from internet\n" + 053 "% Pooya Ronagh (p.ronagh@gmail.com)\n"); 054 // X= Double.parseDouble((JOptionPane.showInputDialog(null, "Width in percent:"))); 055 // Y= Double.parseDouble((JOptionPane.showInputDialog(null, "Height in percent:"))); 056 X /= 100; 057 Y /= 100; 058 059 //set frame 060 Iterator<VertexModel> iv = graph.iterator(); 061 VertexModel v = iv.next(); 062 double minX = v.getLocation().getX(); 063 double minY = v.getLocation().getY(); 064 double maxX = v.getLocation().getX(); 065 double maxY = v.getLocation().getY(); 066 while (iv.hasNext()) { 067 v = iv.next(); 068 minX = min(minX, v.getLocation().getX()); 069 minY = min(minY, v.getLocation().getY()); 070 maxX = max(maxX, v.getLocation().getX()); 071 maxY = max(maxY, v.getLocation().getY()); 072 } 073 double Dx = maxX - minX; 074 double Dy = maxY - minY; 075 076 //set picture 077 output.write("\\vspace{1.cm}"); 078 output.write("\\begin{picture}(" + Dx * X + "," + Dy * Y + ")\n"); 079 080 //draw vertices 081 //\node[Nfill=y,fillcolor=Black,ExtNL=y,NLangle=0.0,NLdist=1.2,Nadjustdist=2.5 082 // ,Nw=5.03,Nh=4.5,Nmr=2.25](n17)(12.78,-4.47){jhgcvk} 083 084 iv = graph.iterator(); 085 int i = 0; 086 while (iv.hasNext()) { 087 v = iv.next(); 088 i = i + 1; 089 output.write("\\node"); 090 String attr = ""; 091 String disc = ""; 092 //check filled 093 attr += "Nfill=y"; 094 //check color 095 attr += ",fillcolor=Black"; 096 if (graph.isDrawVertexLabels()) { 097 attr += ",ExtNL=y,NLangle=0.0,NLdist=1.5,Nadjustdist=2.5"; 098 disc += v.getLabel(); 099 } 100 // check shape 101 attr += ",Nw=" + 2.0; 102 attr += ",Nh=" + 2.0; 103 attr += ",Nmr=1.5"; 104 105 output.write("[" + attr + "]" 106 + "(n" + v.getId() + ")(" 107 + X * v.getLocation().getX() + "," 108 + Y * v.getLocation().getY() + ")" + 109 "{" + disc + "}\n"); 110 } 111 112 //draw edges 113 Iterator<EdgeModel> ie = graph.edgeIterator(); 114 while (ie.hasNext()) { 115 EdgeModel e = ie.next(); 116 output.write("\\drawedge"); 117 String attr = ""; 118 String disc = ""; 119 //check color 120 attr += "linecolor=Black"; 121 //check width 122 attr += ",linewidth=0.5"; 123 if (graph.isDirected()) { 124 attr += ",AHangle=29.74,AHLength=2.02,AHlength=1.75"; 125 } else { 126 attr += ",AHnb=0"; 127 } 128 if (graph.isDrawEdgeLabels()) { 129 attr += ",ELdist=2.0"; 130 disc += e.getLabel(); 131 } 132 output.write("[" + attr + "]" + 133 "(n" + e.source.getId() + ",n" + e.target.getId() + ")" + 134 "{" + disc + "}\n"); 135 } 136 137 // EdgeModel.IsCurvedEdge() 138 //EdgeModel.CubicCurve.PathIterator 139 140 141 output.write("\\end{picture}"); 142 output.close(); 143 144 } catch (IOException e) { 145 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 146 } 147 } 148 149 public String getDescription() { 150 return "exports latex"; 151 } 152 153 public String checkParameters() { 154 if (X < 0 || Y < 0) 155 return "X and Y should be positive"; 156 else 157 return null; 158 } 159 }