/// <summary> /// Recalculate the connectedShapes of every shape in a given list, /// and correctly updates all related shapes. /// </summary> /// <param name="shapeList">the list of shapes to reconnect</param> /// <param name="featureSketch">the sketch the shapes belong to</param> public void recomputeConnectedShapes(IEnumerable <Sketch.Shape> shapeList, Sketch.Sketch sketch) { // clear ALL connections first foreach (Sketch.Shape shape in sketch.Shapes) { shape.ClearConnections(); } sketch.CheckConsistency(); // connect every shape foreach (Sketch.Shape shape in shapeList) { connect(shape, sketch); } // Make sure connected wires are the same shape Data.Pair <Sketch.Shape, Sketch.Shape> pair = null; while ((pair = wiresToMerge(sketch)) != null) { sketch.mergeShapes(pair.A, pair.B); sketch.connectShapes(pair.A, pair.A); } sketch.CheckConsistency(); }
public void TestXMLConversion() { const string filename = "tmp.xml"; Domain.ShapeType testType = Domain.LogicDomain.AND; Sketch.Sketch sketch1 = Sketches.newValidSketch(); foreach (Sketch.Shape shape in sketch1.Shapes) { shape.Type = testType; } ConverterXML.SaveToXML writer = new ConverterXML.SaveToXML(sketch1); writer.WriteXML(filename); ConverterXML.ReadXML reader = new ConverterXML.ReadXML(filename); Sketch.Sketch sketch2 = reader.Sketch; sketch2.CheckConsistency(); Assert.IsTrue(sketch1.Equals(sketch2), "Original sketch is not equal to the loaded sketch"); Assert.IsTrue(sketch2.Equals(sketch1), "Loaded sketch is not equal to the original"); foreach (Sketch.Shape shape in sketch2.Shapes) { Assert.AreEqual(testType, shape.Type, "Shape types are not preserved across save/load"); } }
/// <summary> /// Makes sure that every shape in the sketch has a unique name. /// </summary> public virtual void process(Sketch.Sketch sketch) { Recognizers.TextRecognizer textRecognizer = new Recognizers.TextRecognizer(); HashSet <string> alreadySeen = new HashSet <string>(); int count = 0; foreach (Sketch.Shape shape in sketch.Shapes) { if (shape.Type == LogicDomain.TEXT && shape.Name == null) { textRecognizer.recognize(shape); } if (alreadySeen.Contains(shape.Name) || shape.Name == null) { while (alreadySeen.Contains(shape.Type + "_" + count)) { count++; } shape.Name = (shape.Type + "_" + count); } alreadySeen.Add(shape.Name); } if (DEBUG) { // For debugging purposes: Console.WriteLine("Sketch contains"); foreach (Sketch.Shape shape in sketch.Shapes) { Console.WriteLine(" - " + shape.Name + ", a " + shape.Type); } } }
/// <summary> /// Executes the stages on the given sketch /// </summary> /// <param name="sketch">The sketch to play with</param> /// <param name="filename">The filename the sketch came from</param> public override void run(Sketch.Sketch sketch, string filename) { foreach (ProcessStage ps in stages) { ps.run(sketch, filename); } }
public void FeatureSketchCreationBehavior() { /* * Problem description: * When a sketch contains duplicated points, the creation of a FeatureSketch * may delete those points. */ Sketch.Sketch sketch = Sketches.newValidSketch(); // Duplicate a point in the sketch Sketch.Shape shape = sketch.Shapes[0]; List <Sketch.Point> points = new List <Sketch.Point>(shape.Substrokes[0].Points); points.Add(points[points.Count - 1]); Sketch.Substroke substroke = new Sketch.Substroke(points); sketch.AddStroke(new Sketch.Stroke(substroke)); shape.AddSubstroke(substroke); Sketch.Sketch clone = sketch.Clone(); Assert.IsTrue(sketch.Equals(clone), "Sketch is not equal to its clone"); Featurefy.FeatureSketch featureSketch = newValidFeatureSketch(new Sketch.Project(sketch)); Assert.IsTrue(sketch.Equals(clone), "FeatureSketch creation modified original sketch"); }
public void TestXMLConversion() { const string filename = "tmp.xml"; RecognitionResult type = new RecognitionResult(LogicDomain.AND, 0.9, 0.33); Sketch.Project project1 = new Sketch.Project(Sketches.newValidSketch());; foreach (Sketch.Shape shape in project1.sketch.Shapes) { type.ApplyToShape(shape); } ConverterXML.SaveToXML writer = new ConverterXML.SaveToXML(project1); writer.WriteXML(filename); ConverterXML.ReadXML reader = new ConverterXML.ReadXML(filename); Sketch.Sketch sketch2 = reader.Sketch; sketch2.CheckConsistency(); Assert.IsTrue(project1.sketch.Equals(sketch2), "Original sketch is not equal to the loaded sketch"); Assert.IsTrue(sketch2.Equals(project1.sketch), "Loaded sketch is not equal to the original"); foreach (Sketch.Shape shape in sketch2.Shapes) { Assert.AreEqual(type.Type, shape.Type, "Shape types are not preserved across save/load"); Assert.IsTrue(Math.Abs(type.Confidence - shape.Probability) < 0.0001, "Shape confidences are not preserved across save/load"); Assert.IsTrue(Math.Abs(type.Orientation - shape.Orientation) < 0.0001, "Shape orientations are not preserved across save/load"); } }
/// <summary> /// Process an ordinary sketch by first creating a feature sketch for it. /// This method calls process using the created feature sketch, then /// returns the feature sketch, in case you want to use it. The given /// ordinary sketch is modified in the process. /// </summary> /// <param name="sketch">the ordinary sketch to use</param> /// <returns>the featureSketch used during processing</returns> public FeatureSketch process(Sketch.Sketch sketch) { FeatureSketch featureSketch = FeatureSketch.MakeFeatureSketch(sketch); process(featureSketch); return(featureSketch); }
public void testWriteXML() { Sketch.Sketch sketch = new Sketch.Sketch(); sketch.XmlAttrs.Id = System.Guid.NewGuid(); Sketch.Shape shape = new Sketch.Shape(); shape.XmlAttrs.Id = System.Guid.NewGuid(); shape.XmlAttrs.Name = "shapeName"; shape.XmlAttrs.Type = "shape"; shape.XmlAttrs.Time = "111"; Sketch.Stroke stroke; Sketch.Substroke substroke; Sketch.Point point; ulong h; ulong i; ulong j; for (h = 0; h < 3; ++h) { stroke = new Sketch.Stroke(); stroke.XmlAttrs.Id = System.Guid.NewGuid(); stroke.XmlAttrs.Name = "strokeName"; stroke.XmlAttrs.Type = "stroke"; stroke.XmlAttrs.Time = h; for (i = 0; i < 5; ++i) { substroke = new Sketch.Substroke(); substroke.XmlAttrs.Id = System.Guid.NewGuid(); substroke.XmlAttrs.Name = "substrokeName"; substroke.XmlAttrs.Type = "substroke"; substroke.XmlAttrs.Time = i; for (j = 0; j < 10; ++j) { point = new Sketch.Point(); point.XmlAttrs.X = (float)random.NextDouble(); point.XmlAttrs.Y = (float)random.NextDouble(); point.XmlAttrs.Id = System.Guid.NewGuid(); point.XmlAttrs.Pressure = (ushort)(random.NextDouble() * 256); point.XmlAttrs.Name = "point" + j.ToString(); point.XmlAttrs.Time = j; substroke.AddPoint(point); } stroke.AddSubstroke(substroke); shape.AddSubstroke(substroke); } sketch.AddStroke(stroke); } sketch.AddShape(shape); sketch.Strokes[0].SplitStrokeAt(new int[] { 7, 14, 16, 21 }); ConverterXML.MakeXML xml = new ConverterXML.MakeXML(sketch); xml.WriteXML("test.xml"); }
public static Featurefy.FeatureSketch newValidFeatureSketch(Sketch.Sketch sketch) { Dictionary <string, string> files = new Dictionary <string, string>(); files["FeaturesSingle"] = "FeatureListSingle.txt"; files["FeaturesGroup"] = "FeatureListGroup.txt"; return(Featurefy.FeatureSketch.MakeFeatureSketch(sketch, files)); }
/// <summary> /// Loads all the shapes from a sketch into the associated sketch /// shapes data member. /// /// This method is currently only used by the getter "ShapeNames," /// which gets only used in SimulationManager (as of July 2010). /// Once/if SimulationManager is refactored to not need "ShapeNames" /// anymore, just get rid of this and the associated methods/data /// members because it's unecessary clutter. /// </summary> /// <param name="sketch"></param> private void mapShapesToNames(Sketch.Sketch sketch) { _associatedSketchShapes = new Dictionary <string, Sketch.Shape>(); foreach (Sketch.Shape shape in sketch.Shapes) { _associatedSketchShapes.Add(shape.Name, shape); } }
public static InkToSketchWPF.InkCanvasSketch newInkCanvasSketch() { // Manually construct the necessary data members Sketch.Sketch sketch = new Sketch.Sketch(); Featurefy.FeatureSketch featureSketch = UnitTests.FeaturefyOperations.newValidFeatureSketch(new Sketch.Project(sketch)); InkCanvas inkcanvas = new InkCanvas(); return(new InkToSketchWPF.InkCanvasSketch(inkcanvas, featureSketch)); }
private void loadCircuitIO(Sketch.Sketch sketch) { foreach (Sketch.Shape shape in sketch.Shapes) { if (shape.Type == LogicDomain.TEXT) { loadInputOrOutput(shape); } } }
private static void WriteSketch(Sketch.Sketch sketch, XmlTextWriter xmlDocument) { string[] sketchAttributeNames = sketch.XmlAttrs.getAttributeNames(); object[] sketchAttributeValues = sketch.XmlAttrs.getAttributeValues(); Sketch.Point[] points = sketch.Points; Sketch.Shape[] shapes = sketch.Shapes; Sketch.Stroke[] strokes = sketch.Strokes; Sketch.Substroke[] substrokes = sketch.Substrokes; int length; int i; xmlDocument.WriteStartElement("sketch"); // Write all the attributes length = sketchAttributeNames.Length; for (i = 0; i < length; ++i) { if (sketchAttributeValues[i] != null) { xmlDocument.WriteAttributeString(sketchAttributeNames[i], sketchAttributeValues[i].ToString()); } } // Write all the points length = points.Length; for (i = 0; i < length; ++i) { MakeXML.WritePoint(points[i], xmlDocument); } // Write all the substrokes length = substrokes.Length; for (i = 0; i < length; ++i) { MakeXML.WriteSubstroke(substrokes[i], xmlDocument); } // Write all the strokes length = strokes.Length; for (i = 0; i < length; ++i) { MakeXML.WriteStroke(strokes[i], xmlDocument); } // Write all the shapes length = shapes.Length; for (i = 0; i < length; ++i) { MakeXML.WriteShape(shapes[i], xmlDocument); } xmlDocument.WriteEndElement(); }
/// <summary> /// Tries to parse a sketch into a circuit, and /// indicates whether or not it was successful. /// /// The main functionality in circuit recognition /// here is that it both determines what are inputs /// and outputs on both the gate and circuit level, /// and organizes the results into something the /// circuit can be built off of for simulation. /// </summary> /// <param name="sketch">The sketch to parse</param> /// <returns>True if parsing was a success, /// false if there were errors</returns> public bool SuccessfullyParse(Sketch.Sketch sketch) { // We should have a cleared circuit model each time // we try to parse a sketch. resetCircuitModel(); #region Check circuit domain string shouldNotWorkReason = "There are no shapes."; Sketch.Shape shouldNotWorkShape = null; if (sketch.Shapes.Length > 0) { foreach (Sketch.Shape shape in sketch.Shapes) { if (!_domain.IsProperlyConnected(shape)) { shouldNotWorkReason = "Shape " + shape.Name + " is not properly connected"; shouldNotWorkShape = shape; break; } } } #endregion // Check to make sure the sketch was valid in the first place. // If not, do not continue. _successfulParse = CheckSketch(sketch); if (!_successfulParse) { return(false); } loadWiresAndGates(sketch); connectGatesToWires(); loadCircuitIO(sketch); connectComponentsToComponents(); // Make sure that the circuit we just made is valid _successfulParse = CheckCircuit(); if (debug) { printErrors(); } // This method call is here only to let the SimulationManager // have a mapping of shapes to their names. mapShapesToNames(sketch); // Likewise, this method is here only for mesh highlighting // to have a mapping of sketch strokes to circuit parts. mapStrokesToCircuitParts(sketch); return(_successfulParse); }
private void menuStrokeInformation_Click(object sender, EventArgs e) { Sketch.Sketch sketch = sketchPanel.Sketch; FeatureSketch sketchFeatures = new FeatureSketch(ref sketch); Microsoft.Ink.Strokes selected = sketchPanel.InkSketch.Ink.Strokes; Microsoft.Ink.Stroke s = selected[selected.Count - 1]; Sketch.Substroke ss = sketchPanel.InkSketch.GetSketchSubstrokeByInkId(s.Id); StrokeInfoForm.strokeInfoForm sif = new StrokeInfoForm.strokeInfoForm(sketchFeatures.GetFeatureStrokeByStrokeGUID(ss.Id), sketchFeatures, s, ss); sif.Show(); }
public void process(Featurefy.FeatureSketch featureSketch) { Sketch.Sketch sketch = featureSketch.Sketch; foreach (Sketch.Shape shape in sketch.Shapes) { if (shape.Type != new Domain.ShapeType()) { shape.AlreadyLabeled = true; shape.AlreadyGrouped = true; } } }
/// <summary> /// Test the validity of each labeled shape. /// </summary> /// <returns>A dictionary mapping shapes to validity.</returns> public Dictionary <Sketch.Shape, bool> TestValidity(Featurefy.FeatureSketch _featuresketch) { Sketch.Sketch sketch = _featuresketch.Sketch; Dictionary <Sketch.Shape, bool> dict = new Dictionary <Sketch.Shape, bool>(); foreach (Sketch.Shape shape in sketch.Shapes) { dict.Add(shape, _domain.IsProperlyConnected(shape)); } return(dict); }
/// <summary> /// Run the test on a single sketch. /// </summary> /// <param name="sketch">the sketch to classify</param> /// <param name="filename">the name of the file being tested</param> public override void run(Sketch.Sketch sketch, string filename) { // This method cheats. In order to compare the results of classification to the correct // classification, we actually ignore "sketch" and classify a clone of the original. // This behavior relies (somewhat) on the fact that "sketch" is a deep clone of "original" // anyway. // Record original classifications Dictionary <Sketch.Substroke, string> original_classes = new Dictionary <Sketch.Substroke, string>(); foreach (Sketch.Substroke substroke in sketch.Substrokes) { original_classes.Add(substroke, substroke.Classification); } // Run the classifier! sketch.RemoveLabels(); _pipeline.process(sketch); // Evaluate the classifier! int numSubstrokes = 0; int numCorrect = 0; foreach (Sketch.Substroke s in sketch.Substrokes) { string correct_class = original_classes[s]; // Skip unknown substrokes if (correct_class.ToLower() == "unknown") { continue; } string result_class = s.Classification; _confusion.increment(correct_class, result_class); numSubstrokes++; if (correct_class.Equals(result_class)) { numCorrect++; } } Console.WriteLine(" --> Correct classifications: " + numCorrect + "/" + numSubstrokes); // Assemble the results! _results.addResult(new string[] { filename, "" + numSubstrokes, "" + numCorrect }); }
/// <summary> /// prints information about all the shapes in a sketch including: /// 1) labeled type (if any) /// 2) midpoint of shape /// 3) assigned column (to be done) /// 4) assigned type (to be done) /// </summary> /// <param name="sk"></param> /// <returns></returns> static string printShapeData(Sketch.Sketch sk) { string text = ""; for (int i = 0; i < numShapes(sk); i++) { text += "Shape[" + i + "] labeled: " + sk.Shapes[i].XmlAttrs.Type; text += ", x,y: " + sk.Shapes[i].XmlAttrs.X + "," + sk.Shapes[i].XmlAttrs.Y; } return(text); }
void ImageRecognizer_RecognitionCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { try { List <Cluster> scored = (List <Cluster>)e.Result; rSketch = new Sketch.Sketch(); haveResults = true; } catch (Exception ex) { Console.WriteLine("ImageRecognizerCompleted: {0}", ex.Message); } }
public override void run(Sketch.Sketch sketch, string filename) { _fileNames.Add(filename); string[] parts = filename.Split(new char[] { '\\' }); string shortfilename = parts.Last(); string user = shortfilename.Split(new char[] { '_' }).First(); if (!_userSketches.ContainsKey(user)) { _userSketches.Add(user, new List <string>()); } _userSketches[user].Add(filename); }
/// <summary> /// returns an ArrayList of the types of shapes in the sketch (no repeats) /// </summary> /// <param name="sk"></param> /// <returns></returns> static ArrayList shapeTypes(Sketch.Sketch sk) { ArrayList shapeTypes = new ArrayList(); for (int i = 0; i < numShapes(sk); i++) { if (shapeTypes.Contains(sk.Shapes[i].XmlAttrs.Type) == false) { shapeTypes.Add(sk.Shapes[i].XmlAttrs.Type); } } return(shapeTypes); }
/// <summary> /// returns average length of the strokes in the sketch /// </summary> /// <param name="sk"></param> /// <returns></returns> static int avgStrokeLength(Sketch.Sketch sk) { double sumLengths = 0; for (int i = 0; i < numStrokes(sk); i++) { Featurefy.ArcLength al = new Featurefy.ArcLength(sk.Strokes[i].Points); sumLengths += al.TotalLength; } double avgLength = sumLengths / numStrokes(sk); return((int)avgLength); }
/// <summary> /// returns the percentage of shapes correctly identified /// </summary> /// <param name="sk"></param> /// <returns></returns> static double percentAccuracy(Sketch.Sketch sk) { TruthTables.TruthTable tt = new TruthTables.TruthTable(sk); double correct = 0; for (int i = 0; i < numShapes(sk); i++) { if ((string)sk.Shapes[i].XmlAttrs.Type == tt.assignType(sk.Shapes[i])) { correct++; } } return(100 * correct / numShapes(sk)); }
public override void run(Sketch.Sketch sketch, string filename) { if (_filename == null) { _filename = filename; } foreach (Sketch.Shape shape in sketch.Shapes) { if (_recognizer.canRecognize(shape.Classification)) { _gates.Add(shape); } } }
/// <summary> /// Run the test on a single sketch. /// </summary> /// <param name="sketch">the sketch to classify</param> /// <param name="filename">the name of the file being tested</param> public override void run(Sketch.Sketch sketch, string filename) { // Record original classifications Dictionary <Sketch.Substroke, string> original_classes = new Dictionary <Sketch.Substroke, string>(); foreach (Sketch.Substroke substroke in sketch.Substrokes) { original_classes.Add(substroke, substroke.Classification); } // Run the classifier! sketch.RemoveLabels(); _pipeline.process(sketch); // Evaluate the classifier! int numSubstrokes = 0; int numCorrect = 0; foreach (Sketch.Substroke s in sketch.Substrokes) { string correct_class = original_classes[s]; // Skip unknown substrokes if (correct_class.ToLower() == "unknown") { continue; } string result_class = s.Classification; _confusion.increment(correct_class, result_class); numSubstrokes++; if (correct_class.Equals(result_class)) { numCorrect++; } } Console.WriteLine(" --> Correct classifications: " + numCorrect + "/" + numSubstrokes); // Assemble the results! _results.addResult(new string[] { filename, "" + numSubstrokes, "" + numCorrect }); }
/// <summary> /// Finds all the substrokes in a shape that should be /// broken off into a separate shape (if any), and does so. /// </summary> /// <param name="shape">The shape to investigate</param> /// <returns>A list of broken off shapes NOT including the original</returns> private List <Sketch.Shape> maybeBreakOffShapes(Sketch.Shape shape, Sketch.Sketch sketch) { List <Sketch.Shape> brokenOffShapes = new List <Sketch.Shape>(); Dictionary <Guid, Dictionary <Guid, int> > adjacency = Util.makeAdjacency(sketch); List <List <Sketch.Substroke> > subShapes = findShapeSubgroups(shape, adjacency); // While the shape is composed of more than one sub-shape while (subShapes.Count > 1) { // Break off the sub-shape Sketch.Shape newShape = sketch.BreakOffShape(shape, subShapes[0]); subShapes.RemoveAt(0); brokenOffShapes.Add(newShape); } return(brokenOffShapes); }
/// <summary> /// returns an int array of the number of times each shape occurs /// </summary> /// <param name="sk"></param> /// <returns></returns> static int[] countTypes(Sketch.Sketch sk) { int[] countTypes = new int[shapeTypes(sk).Count]; for (int i = 0; i < countTypes.Length; i++) { countTypes[i] = 0; } for (int i = 0; i < numShapes(sk); i++) { int index = shapeTypes(sk).IndexOf(sk.Shapes[i].XmlAttrs.Type); countTypes[index]++; } return(countTypes); }
/// <summary> /// Recognizes the strokes of the sketch using the Ink. /// </summary> /// <param name="args">An unlabeled Sketch and a UserTriggered flag</param> /// <returns>A Labeled Sketch and </returns> public override RecognitionResult Recognize(RecognitionArgs args) { RecognitionResult result = new RecognitionResult(); result.UserTriggered = args.UserTriggered; // Only recognize when necessary if (!args.UserTriggered) { return(result); } // Run recognition and fill result Sketcher sketcher = new Sketcher(); iTool = sketcher.InkPanel.InkTool; iTool.Clusterer.ImageRecognizer.RecognitionCompleted += new RecognitionCompletedEventHandler(ImageRecognizer_RecognitionCompleted); settingsFilename = sketcher.SettingsFilename; dir = sketcher.BaseDirectory; try { iTool.Clusterer.ImageRecognizer.m_RecognitionComplete = false; // Try to use InkTool bool success = iTool.ClassifySketch(args.Sketch, settingsFilename, dir); StrokeClassifier.StrokeClassifierResult clResult = iTool.Clusterer.Classifier.Classify(); StrokeGrouper.StrokeGrouperResult grResult = iTool.Clusterer.Grouper.Group(clResult.AllClassifications); List <Cluster> initialClusters = new List <Cluster>();//iTool.Clusterer.CreateClusters(grResult); ImageRecognizerWrapper.ImageRecognizerResults imgResult = iTool.Clusterer.GetImageResults(initialClusters); //while (!iTool.Clusterer.InitialClustersDone); //iTool.Clusterer.ImageRecognizer.RecognizeST(iTool.Clusterer. rSketch = iTool.MakeSketch(imgResult.ScoredClusters); result.Sketch = rSketch; } catch (Exception e) { // Catch all other exceptions System.Windows.MessageBox.Show("General Exception from sketch recognizer component: \n" + e.Message); // Return unrecognized sketch result.Sketch = args.Sketch; } return(result); }
/// <summary> /// Constructor, creates a Sketch from the first page of the file /// </summary> /// <param name="ink">The name of the ink object</param> public ReadInk(Ink ink) { //Code block taken from ConverterJnt sketch = new Sketch.Sketch(); sketch.XmlAttrs.Id = System.Guid.NewGuid(); sketch.XmlAttrs.Units = "himetric"; //Sends Microsoft.Ink object to be converted into a sketch try { this.ConvertToSketch(ink); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.InnerException); } }