/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Globalization; namespace OpenTraffic.Model.Graph { public class Graph where TV : Vertex where TE : GraphEdge { public List Nodes { get; private set; } public List Links { get; private set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public Dictionary> AdjLinks { get; private set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] public Dictionary> RevAdjLinks { get; private set; } /* public void Remove(V n) { nodes.Remove(n); foreach (E e in adjLinks[n]) { for (int i = 0; i < revAdjLinks[e.Target].Count;i++) { E r = revAdjLinks[e.Target][i]; if (r.Source.id == n.id) { revAdjLinks.Remove(i); break; } } } foreach (E e in revAdjLinks[n.id]) { for (int i = 0; i < adjLinks[e.Source.id].Count; i++) { E r = adjLinks[e.Source.id][i]; if (r.Target.id == n.id) { adjLinks.Remove(i); break; } } } adjLinks.Remove(n.id); revAdjLinks.Remove(n.id); }*/ public void Add(TV n) { Nodes.Add(n); AdjLinks.Add(n, new List()); RevAdjLinks.Add(n, new List()); } public virtual void Add(TE e) { Links.Add(e); AdjLinks[e.Source].Add(e); RevAdjLinks[e.Target].Add(e); } public Graph() { Nodes = new List(); Links = new List(); AdjLinks = new Dictionary>(); RevAdjLinks = new Dictionary>(); } public void ExportToLatex(float x, float y, string fileName, Dictionary part) { System.IO.StreamWriter sw=null; try { sw = new System.IO.StreamWriter(fileName); NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat; sw.WriteLine("% ------------- GRAPH -------------------------------"); // sw.WriteLine("\\begin{graph}(" + x + "," + y + ")"); double minx = double.MaxValue; double miny = double.MaxValue; double maxx = double.MinValue; double maxy = double.MinValue; foreach (Vertex v in Nodes) { minx = Math.Min(minx, v.X); miny = Math.Min(miny, v.Y); maxx = Math.Max(maxx, v.X); maxy = Math.Max(maxy, v.Y); } double scale = Math.Max(x / (maxx - minx), y / (maxy - miny)); foreach (Vertex v in Nodes) { string color = ""; if (part != null) { int p = part[v.Id]; color = "[\\graphnodecolour(" + (1 - p) + "," + p + ",0)]"; } sw.WriteLine("\\roundnode{A" + v.Id + "}(" + ((v.X - minx) * scale).ToString(nfi) + "," + ((v.Y - miny) * scale).ToString(nfi) + ")" + color ); } foreach (TE l in Links) { if (l.Source.Id != l.Target.Id) { sw.WriteLine("\\edge{A" + l.Source.Id + "}{A" + l.Target.Id + "}"); } } // sw.WriteLine("\\end{graph}"); sw.WriteLine(); sw.Flush(); } finally { if (sw != null) { sw.Close(); } } } } }