public static void AddInput(String path, PointPattern pointPattern) { try { if (File.Exists(path)) { string jsonFile; using (var reader = new StreamReader(path)) { jsonFile = reader.ReadToEnd(); } var list = JsonConvert.DeserializeObject <List <PointPattern> >(jsonFile); list.Add(pointPattern); File.Delete(path); jsonFile = JsonConvert.SerializeObject(list); WriteToFile(path, jsonFile); } else { var patterns = new PointPattern[] { pointPattern }; string JsonFile = JsonConvert.SerializeObject(patterns); WriteToFile(path, JsonFile); } } catch { throw; } }
/// <summary> /// Compares a points of a single gesture, to the points in a single saved gesture, and returns a accuracy probability. /// </summary> /// <param name="compareTo">Learned PointPattern from PointPatternSet to compare gesture points to.</param> /// <param name="points">Points of the current gesture being analyzed.</param> /// <returns>Returns the accuracy probability of the learned PointPattern to the current gesture.</returns> public PointPatternMatchResult GetPointPatternMatchResult(PointPattern compareTo, Point[] points) { //compare points // Ensure we have at least 2 points or recognition will fail as we are unable to interpolate between a single point. if (points.Length < 2) { throw new ArgumentOutOfRangeException("hello" /*nameof(points)*/); } // We'll use an array of doubles that matches the number of interpolation points to hold // the dot products of each angle comparison. var dotProducts = new double[Precision]; var myDistance = new double[Precision]; // We'll need to interpolate the incoming points array and the points of the learned gesture. // We do this for each comparison so that we can change the precision at any time and not lose // or original learned gesture to multiple interpolations. var interpolatedCompareTo = PointPatternMath.GetInterpolatedPointArray(compareTo.Points, Precision); var interpolatedPointArray = PointPatternMath.GetInterpolatedPointArray(points, Precision); int ij = 0; float totalDistance = 0; foreach (Point p in interpolatedCompareTo) { Vector2 v1 = new Vector2((float)p.X, (float)p.Y); Vector2 v2 = new Vector2((float)interpolatedPointArray[ij].X, (float)interpolatedPointArray[ij].Y); float d = Vector2.Distance(v1, v2); myDistance[ij] = Vector2.Distance(v1, v2); totalDistance = totalDistance + d; ij++; } // Debug.Log("distance is " + totalDistance); // Next we'll get an array of angles for each interpolated point in the learned and current gesture. // We'll get the same number of angles corresponding to the total number of interpolated points. var anglesCompareTo = PointPatternMath.GetPointArrayAngles(interpolatedCompareTo); var angles = PointPatternMath.GetPointArrayAngles(interpolatedPointArray); double averageDistance = myDistance.Average(); // Now that we have angles for each gesture, we'll get the dot product of every angle equal to // the total number of interpolation points. for (var i = 0; i <= anglesCompareTo.Length - 1; i++) { dotProducts[i] = PointPatternMath.GetDotProduct(anglesCompareTo[i], angles[i]); } // Convert average dot product to probability since we're using the deviation // of the average of the dot products of every interpolated point in a gesture. var probability = PointPatternMath.GetProbabilityFromDotProduct(dotProducts.Average()); // Return PointPatternMatchResult object that holds the results of comparison. return(new PointPatternMatchResult(compareTo.Name, probability, 1, totalDistance, averageDistance)); }
// void savePattern(string word) { Point[] mypoints = new Point[setPattern.Count]; mypoints = setPattern.ToArray(); PointPattern pa = new PointPattern(); pa.Name = word; pa.Points = mypoints; setPattern.Clear(); PointManager.AddInput(path, pa); Debug.Log("shoooo"); }
// Update is called once per frame void Update() { float step = 2.0f * Time.deltaTime; // mycoroutine = move(p); //move(p); // move2(p); // transform.position = Vector3.MoveTowards(transform.position, p, step); //transform.position = Vector3.MoveTowards(transform.position, t, step); // string mystring = currentWord; mycoroutine = move2(); //char[] chars = mystring.ToCharArray(); // int l = chars.Length; // transform.position = h; if (!start) { getWord(); //target = p; start = true; count = 0; findTarget(count); // yield return new WaitForSeconds(5); // StopAllCoroutines(); // StartCoroutine(mycoroutine); transform.position = target; } // Debug.Log("Current word is " + currentWord[count]+ target); transform.position = Vector3.MoveTowards(transform.position, target, step); Debug.Log(transform.position + " and " + target); Debug.Log(Vector3.Distance(transform.position, target)); //set points point.X = transform.position.x; //xpos;// mycursor.transform.position.x; point.Y = transform.position.y; // setPattern.Add(point); if ((Vector3.Distance(transform.position, target) < 0.2f)) { // Swap the position of the cylinder. //x.transform.position *= -1.0f; // yield return new WaitForSeconds(5); Debug.Log("h"); //switch (mystring[count]) //{ // case 'h': // target = h; // // transform.position = Vector3.MoveTowards(transform.position, h, step); // Debug.Log("h"); // break; // case 'i': // target = i; // Debug.Log("i"); // break; // case 'e': // target = e; // Debug.Log("e"); // break; // default: // break; //} if (count > currentWord.Length - 1) { start = false; Point[] mypoints = new Point[setPattern.Count]; mypoints = setPattern.ToArray(); PointPattern pa = new PointPattern(); pa.Name = mycurrentWord; pa.Points = mypoints; setPattern.Clear(); PointManager.AddInput(path, pa); } if (start) { findTarget(count); } count++; } //if (count >= currentWord.Length) //{ // start = false; //} // mycoroutine = move(chars, count, l); //StartCoroutine(mycoroutine); // //foreach (char c in mystring) //{ // switch (c) { // case 'h': // target = h; // // transform.position = Vector3.MoveTowards(transform.position, h, step); // Debug.Log("h"); // break; // case 'i': // target = i; // Debug.Log("i"); // break; // case 'e': // target = e; // Debug.Log("e"); // break; // default: // break; // } // move(target); //} }
// Update is called once per frame void Update() { /*check force*/ string cmd = CheckForRecievedData(); try { print(cmd); // print("tafe"); if (cmd.StartsWith("F")) //Got a force { // print("hey"); force = ParseFSRData(cmd); print(force); if (force > 300 && force < 490) //positoon soft touch { positioning = true; gettingPoints = false; } else if (force > 500) { positioning = false; gettingPoints = true; //while start //while (typing) //{ //p.X = mycursor.transform.position.x; //p.Y = mycursor.transform.position.y; // }//end while } else { gettingPoints = false; positioning = false; } } // else { force = 0; typing = false; positioning = false; } } catch (System.Exception) { Debug.Log("hie"); } /*position*/ if (positioning) { this.GetComponent <Renderer>().material.SetColor("_Color", Color.red); this.GetComponent <TrailRenderer>().widthMultiplier = 0.0f; this.GetComponent <TrailRenderer>().time = 0.01f; xpos = Input.GetAxis("Mouse X"); ypos = Input.GetAxis("Mouse Y"); displac.x = xpos * 0.3f; displac.y = ypos * 0.3f; displac.z = 0f; //float minX = -5.5f; //float maxX = 5.5f; //float minY = -2.4f; //float maxY = 2.4f; // displac.x = Mathf.Clamp(displac.x, minX, maxX); //displac.y = Mathf.Clamp(displac.y, minY, maxY); Vector3 position = this.transform.position; if (this.transform.position.x + displac.x < -5.5) { position.x = -5.5f; } else if (this.transform.position.x + displac.x > 5.5) { position.x = 5.5f; } else { position.x += displac.x; } if (this.transform.position.y + displac.y < -2.4) { position.y = -2.4f; } else if (this.transform.position.y + displac.y > 2.4) { position.y = 2.4f; } else { position.y += displac.y; } // position.y += displac.y; position.z += displac.z; try { this.transform.position = position; } catch (System.Exception) { } } /*get points */ if (gettingPoints) { this.GetComponent <Renderer>().material.SetColor("_Color", Color.green); this.GetComponent <TrailRenderer>().widthMultiplier = 0.1f; this.GetComponent <TrailRenderer>().time = 2f; xpos = Input.GetAxis("Mouse X"); ypos = Input.GetAxis("Mouse Y"); displac.x = xpos * 0.3f; displac.y = ypos * 0.3f; displac.z = 0f; Vector3 position = this.transform.position; if (this.transform.position.x + displac.x < -5.5) { position.x = -5.5f; } else if (this.transform.position.x + displac.x > 5.5) { position.x = 5.5f; } else { position.x += displac.x; } if (this.transform.position.y + displac.y < -2.4) { position.y = -2.4f; } else if (this.transform.position.y + displac.y > 2.4) { position.y = 2.4f; } else { position.y += displac.y; } // position.y += displac.y; position.z += displac.z; try { this.transform.position = position; } catch (System.Exception) { } p.X = this.transform.position.x; //xpos;// mycursor.transform.position.x; p.Y = this.transform.position.y; // ypos;// mycursor.transform.position.y; setPattern.Add(p); // drawingPattern[i] = p; i = i + 1; Debug.Log(i); } /*write word to json file*/ if (!gettingPoints && setPattern.Count > 0) { //change list to array Point[] mypoints = new Point[setPattern.Count]; mypoints = setPattern.ToArray(); PointPattern pa = new PointPattern(); pa.Name = "myword"; pa.Points = mypoints; setPattern.Clear(); PointManager.AddInput(path, pa); Debug.Log("shoooo"); } // Create a Point Pattern object // This is dependent on your inputs // Im using a default constructor in this case, Debug.Log("yes yebo"); //var pattern = new PointPattern(); //pattern.Name = "Falesto"; //pattern.Points = new Point[] { new Point { X = 100, Y = 300 }, new Point { X = 400, Y = 300 } }; //PointManager.AddInput(path, pattern); // Point Mannager is a static class with static methods. //PointManager.AddInput(path, pattern); // PointManager.ReadFile(path); }