public void TestStandardMerge() { Shape shape1 = Shapes.newValidShape(); Shape shape2 = Shapes.newValidShape(); Shape shape3 = Shapes.newValidShape(); Sketch.Sketch sketch = new Sketch.Sketch(); sketch.CheckConsistency(); sketch.AddShape(shape1); sketch.AddShape(shape2); sketch.AddShape(shape3); sketch.connectShapes(shape1, shape3); sketch.connectShapes(shape2, shape3); Assert.IsTrue(sketch.AreConnected(shape1, shape3), "Shape 1 is not connected to shape 3"); Assert.IsTrue(sketch.AreConnected(shape2, shape3), "Shape 2 is not connected to shape 3"); sketch.CheckConsistency(); Assert.IsTrue(sketch.containsShape(shape1), "Sketch does not contain shape 1"); Assert.IsTrue(sketch.containsShape(shape2), "Sketch does not contain shape 2"); sketch.mergeShapes(shape1, shape2); Assert.IsTrue(sketch.AreConnected(shape1, shape3), "Shape 1 is not connected to shape 3 (after merge)"); Assert.IsFalse(sketch.AreConnected(shape1, shape1), "Shape 1 is connected to itself (after merge)"); Assert.IsFalse(sketch.AreConnected(shape1, shape2), "Shape 1 is connected to shape 2 (after merge)"); Assert.IsTrue(sketch.containsShape(shape1), "Sketch does not contain shape 1 (after merge)"); Assert.IsFalse(sketch.containsShape(shape2), "Sketch contains shape 2 (after merge)"); }
public static Sketch.Sketch newValidSketch() { Sketch.Sketch sketch = new Sketch.Sketch(); sketch.AddShape(Shapes.newValidShape()); sketch.AddShape(Shapes.newValidShape()); sketch.AddShape(Shapes.newValidShape()); return(sketch); }
/// <summary> /// Add "strokes" connecting the middle of substrokes adjacent in the graph. /// </summary> /// <param name="sketch">sketch to modify</param> /// <param name="graph">neighborhood graph</param> private static void AddDebuggingCycles(ref Sketch.Sketch sketch, NeighborhoodMap graph) { Dictionary <Substroke, Sketch.Point> midpoints = new Dictionary <Substroke, Sketch.Point>(); foreach (Substroke s in sketch.Substrokes) { midpoints.Add(s, s.PointsL[s.PointsL.Count / 2]); } foreach (Substroke s in graph.Substrokes) { foreach (Neighbor n in graph[s]) { if (!graph.Edge(n.neighbor, s)) { continue; } Substroke sub = new Substroke(new Sketch.Point[] { midpoints[s], midpoints[n.neighbor] }, new XmlStructs.XmlShapeAttrs(true)); sub.XmlAttrs.Time = 0; sub.XmlAttrs.Name = "substroke"; sub.XmlAttrs.Type = "substroke"; Stroke str = new Stroke(sub); str.XmlAttrs.Time = 0; str.XmlAttrs.Name = "stroke"; str.XmlAttrs.Type = "stroke"; Shape xor = new Shape(new List <Substroke>(new Substroke[] { sub }), new XmlStructs.XmlShapeAttrs(true)); xor.XmlAttrs.Type = "BUBBLE"; xor.XmlAttrs.Name = "Shape"; xor.XmlAttrs.Time = 0; sketch.AddStroke(str); sketch.AddShape(xor); } } }
/// <summary> /// an attempt to find labels based on what didn't have connections in certain directions. /// didn't work very well. /// </summary> /// <param name="sketch"></param> /// <param name="graph"></param> private static void AddLabelShapes(ref Sketch.Sketch sketch, NeighborhoodMap graph) { foreach (Substroke s in graph.Substrokes) { Sketch.Point a = s.PointsL[0]; Sketch.Point b = s.PointsL[s.PointsL.Count - 1]; Sketch.Point m = s.PointsL[s.PointsL.Count / 2]; double dir = 0d; int x = 0; bool found = true; if (graph[s].Count > 1) { bool N, E, W, S; N = E = W = S = false; foreach (Neighbor n in graph[s]) { //dir = direction(m, n.neighbor.PointsL[n.neighbor.PointsL.Count/2]); dir = NeighborhoodMap.direction(m, n.dest); if (dir > -Math.PI / 3 && dir < Math.PI / 3) { E = true; } if (dir > Math.PI / 6 && dir < 5 * Math.PI / 6) { N = true; } if (dir > 2 * Math.PI / 3 || dir < -2 * Math.PI / 3) { W = true; } if (dir > -5 * Math.PI / 6 && dir < Math.PI / 6) { S = true; } } found = !((N && S) || (E && W)); } if (found) { Shape label = new Shape(new List <Substroke>(new Substroke[] { s }), new XmlStructs.XmlShapeAttrs(true)); label.XmlAttrs.Type = "Label"; label.XmlAttrs.Name = "shape"; label.XmlAttrs.Time = 1; sketch.AddShape(label); } } }
public void write() { foreach (KeyValuePair <string, List <Shape> > kvp in _sps) { int count = 0; foreach (Shape sp in kvp.Value) { Sketch.Sketch sketch = new Sketch.Sketch(); sketch.AddShape(sp); foreach (Substroke s in sp.SubstrokesL) { sketch.AddStroke(s.ParentStroke); } ConverterXML.SaveToXML maker = new ConverterXML.SaveToXML(sketch); maker.WriteXML(String.Format("{0}\\{1}_{2}.xml", _dir, kvp.Key, count)); ++count; } } }
/// <summary> /// Add "strokes" connecting the middle of substrokes adjacent in the graph. /// ID labels absed on /// </summary> /// <param name="sketch">sketch to modify</param> /// <param name="graph">neighborhood graph</param> private static void AddDebuggingLines2(ref Sketch.Sketch sketch, NeighborhoodMap graph) { string[] labels = { "AND", "OR", "NOT", "NAND", "NOR", "XOR", "Wire", "BUBBLE", "Label" }; List <List <Substroke> > components = graph.connectedComponents(); #region midpoints Dictionary <Substroke, Sketch.Point> midpoints = new Dictionary <Substroke, Sketch.Point>(); foreach (Substroke s in sketch.Substrokes) { midpoints.Add(s, s.PointsL[s.PointsL.Count / 2]); } #endregion double bbminx, bbmaxx, bbminy, bbmaxy; double bbarea = area(graph.Substrokes, out bbminx, out bbmaxx, out bbminy, out bbmaxy); double THRESH = Math.Min(bbmaxx - bbminx, bbmaxy - bbminy) / 5; foreach (List <Substroke> group in components) { Shape xyz = new Shape(group, new XmlStructs.XmlShapeAttrs(true)); xyz.XmlAttrs.Name = "Shape"; xyz.XmlAttrs.Time = 1; double minx, maxx, miny, maxy; double a = area(group, out minx, out maxx, out miny, out maxy); // check if it is an outer group and relatively small if ((minx - THRESH <= bbminx || maxx + THRESH >= bbmaxx || miny - THRESH <= bbminy || maxy + THRESH >= bbmaxy) && a <= bbarea / 40.0) { xyz.XmlAttrs.Type = "Label"; Console.Write("label!"); } else { xyz.XmlAttrs.Type = "Wire"; } sketch.AddShape(xyz); } #region old /* * foreach (Substroke s in graph.Substrokes) * { * * foreach (Neighbor n in graph[s]) * { * * Substroke sub = new Substroke(new Sketch.Point[] { midpoints[s], midpoints[n.neighbor] }, * new XmlStructs.XmlShapeAttrs(true)); * sub.XmlAttrs.Time = 0; * sub.XmlAttrs.Name = "substroke"; * sub.XmlAttrs.Type = "substroke"; * Stroke str = new Stroke(sub); * str.XmlAttrs.Time = 0; * str.XmlAttrs.Name = "stroke"; * str.XmlAttrs.Type = "stroke"; * Shape xor = new Shape(new List<Substroke>(new Substroke[] { sub }), * new XmlStructs.XmlShapeAttrs(true)); * xor.XmlAttrs.Type = thislabel; * xor.XmlAttrs.Name = "Shape"; * xor.XmlAttrs.Time = 0; * sketch.AddStroke(str); * sketch.AddShape(xor); * } * } * */ #endregion }