public static void SavePlaneToFile(RansacPlane rp, string dir, string filename) { string fullPathToFile = Path.Combine(dir, filename); StreamWriter writer = new StreamWriter(fullPathToFile); writer.Write(rp.BestModel.ToString()); writer.Close(); }
public static RansacPlane ReadPlaneFromFile(string dir, string filename) { // Parse string fullPathToFile = Path.Combine(dir, filename); StreamReader reader = new StreamReader(fullPathToFile); //CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone(); //ci.NumberFormat.CurrencyDecimalSeparator = "."; string line; int counter = 0; float probability = 0; List <Vector3> points = new List <Vector3>(); while ((line = reader.ReadLine()) != null) { counter++; // Probability row if (counter == 1) { probability = float.Parse(line, CultureInfo.InvariantCulture); continue; } // Number of elemnts row // not use if (counter == 2) { continue; } char[] delimiter = new char[] { '\t' }; string[] row = line.Split(delimiter); Vector3 point = new Vector3(); point.X = float.Parse(row[0], CultureInfo.InvariantCulture); point.Y = float.Parse(row[1], CultureInfo.InvariantCulture); point.Z = float.Parse(row[2], CultureInfo.InvariantCulture); points.Add(point); } // Solve RansacPlane rp = new RansacPlane(); rp.Threshold = probability; rp.Estimate(points); return(rp); }
public void RansacPlaneConstructorTest2() { Point3[] points = { new Point3(1, 1, 1), new Point3(2, 3, 4), new Point3(4, 6, 9), new Point3(2, 2, 3) }; RansacPlane target = new RansacPlane(0.80, 0.9); Plane plane = target.Estimate(points); var normal = plane.Normal / plane.Normal.Max; double d = plane.DistanceToPoint(Point3.Origin); Assert.AreEqual(normal.X, 1, 1e-4); Assert.AreEqual(normal.Y, 1, 1e-4); Assert.AreEqual(normal.Z, -1, 1e-4); Assert.AreEqual(plane.Offset, -d, 1e-4); }
static void DemoStaticTest() { RansacPlane rp = new RansacPlane(); rp.Estimate(RansacPlane.GetTestA1()); System.Console.Write("A1: "); System.Console.WriteLine(rp.BestModel.ToString()); System.Console.WriteLine("==========================================================================="); rp.Estimate(RansacPlane.GetTestA2()); System.Console.Write("A2: "); System.Console.WriteLine(rp.BestModel.ToString()); System.Console.WriteLine("==========================================================================="); rp.Estimate(RansacPlane.GetTestA3()); System.Console.Write("A3: "); System.Console.WriteLine(rp.BestModel.ToString()); System.Console.WriteLine("==========================================================================="); System.Console.ReadKey(); }
public void RansacPlaneConstructorTest() { Accord.Math.Tools.SetupGenerator(0); { Point3[] points = new Point3[300]; int c = 0; for (int i = 0; i < points.Length / 3; i++) { points[c++] = new Point3((float)i, (float)0, (float)0); points[c++] = new Point3((float)0, (float)i, (float)0); points[c++] = new Point3((float)i, (float)i, (float)0); } RansacPlane target = new RansacPlane(0.80, 0.9); Plane expected = Plane.FromPoints(points[3], points[4], points[5]); Plane actual = target.Estimate(points); Assert.AreEqual(actual.A, 0); Assert.AreEqual(actual.B, 0); Assert.AreEqual(actual.C, -1); Assert.AreEqual(actual.Offset, 0); Assert.IsTrue(expected.Equals(actual, 1e-3)); } { Point3[] points = new Point3[300]; int c = 0; for (int i = 0; i < points.Length / 3; i++) { points[c++] = new Point3((float)i, (float)0, (float)50); points[c++] = new Point3((float)0, (float)i, (float)50); points[c++] = new Point3((float)i, (float)i, (float)50); } RansacPlane target = new RansacPlane(0.80, 0.9); Plane expected = Plane.FromPoints(points[6], points[7], points[8]); expected.Normalize(); Plane actual = target.Estimate(points); Assert.AreEqual(actual.A, 0); Assert.AreEqual(actual.B, 0, 1e-5); Assert.AreEqual(actual.C, -1, 1e-5); Assert.AreEqual(actual.Offset, 50, 1e-4); Assert.IsTrue(expected.Equals(actual, 1e-3)); } { Point3[] points = new Point3[10 * 10]; int c = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { double x = i + 5 * Accord.Math.Tools.Random.NextDouble(); double y = j + 5 * Accord.Math.Tools.Random.NextDouble(); double z = x + y - 1; points[c++] = new Point3((float)x, (float)z, (float)y); } } RansacPlane target = new RansacPlane(0.80, 0.9); Plane actual = target.Estimate(points); var normal = actual.Normal / actual.Normal.Max; Assert.AreEqual(normal.X, +1, 1e-5); Assert.AreEqual(normal.Y, -1, 1e-5); Assert.AreEqual(normal.Z, +1, 1e-5); } }