Exemplo n.º 1
0
        static void TestEclipsePipe()
        {
            using (var reader = System.IO.File.OpenText(@"M:\Superior Energy\Test Runs\data_4.dat"))
            {
                for (int i = 0; i < 8; i++)
                {
                    reader.ReadLine(); // skip header
                }
                var polarPoints = new List <Vector2>();
                while (true)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    var nums    = line.Split('\t');
                    var laser   = double.Parse(nums[1]);
                    var encoder = (int)double.Parse(nums[2]);

                    const int EncoderPPR = 4000; // encoder PPR is 1000, but it is a 4x subsampling quadrature encoder

                    if (encoder < 32000)
                    {
                        encoder += 65536;                   // to allow for rotating wrong way, move things first to account for counter rollover
                    }
                    encoder %= EncoderPPR;
                    if (encoder < 0)
                    {
                        encoder += EncoderPPR;
                    }

                    var theta = (2 * Math.PI * encoder) / EncoderPPR;

                    //const double LaserVoltageAtEdgeOfHolder = 1.3612; // as measured by 1-2-3 block method
                    const double LaserVoltageAtEdgeOfHolder    = 1.381;       // as empirically derived by assuming theoretical scale factor and pipe size
                    const double LaserScaleFactorInchesPerVolt = 12.0 / 10.0; // theoretical scale factor from datasheet
                    const double LaserHolderHalfWidth          = 2.3125;      // theoretical from drawing, part measures perfectly for width

                    var r = ((laser - LaserVoltageAtEdgeOfHolder) * LaserScaleFactorInchesPerVolt) + LaserHolderHalfWidth;

                    polarPoints.Add(new Vector2(theta, r));
                }

                var cartesianPoints = new List <Vector2>(polarPoints.Count);
                foreach (var polar in polarPoints)
                {
                    cartesianPoints.Add(new Vector2(polar.Y * Math.Cos(polar.X), polar.Y * Math.Sin(polar.X)));
                }

                var circle = GeometricFits.FitCircle(cartesianPoints);

                Console.WriteLine("Point Count: {0}", cartesianPoints.Count);
                Console.WriteLine();

                Console.WriteLine("Radius: {0:f3}", circle.Radius);
                Console.WriteLine("Diameter: {0:f3}", circle.Diameter);
                Console.WriteLine("Center X: {0:f3}", circle.Center.X);
                Console.WriteLine("Center Y: {0:f3}", circle.Center.Y);
                Console.WriteLine("Center Offset: {0:f3}", circle.Center.Length);
                Console.WriteLine();

                var ellipse = GeometricFits.FitEllipse(cartesianPoints);

                Console.WriteLine("Elliptical Major Diameter: {0:f3}", ellipse.MajorAxisLength);
                Console.WriteLine("Elliptical Minor Diameter: {0:f3}", ellipse.MinorAxisLength);
                Console.WriteLine("Elliptical Eccentricity: {0:f4}", ellipse.Eccentricity);

                Console.ReadLine();

                var censoredCenteredCartesian = new List <Vector2>();
                foreach (var point in cartesianPoints)
                {
                    var centered = point - circle.Center;
                    if (centered.Length > 2.95)
                    {
                        censoredCenteredCartesian.Add(point);
                    }
                }
                var circle2 = GeometricFits.FitCircle(censoredCenteredCartesian);
                Console.WriteLine("Radius: {0:f3}", circle2.Radius);
                Console.WriteLine("Diameter: {0:f3}", circle2.Diameter);
                Console.WriteLine("Center X: {0:f3}", circle2.Center.X);
                Console.WriteLine("Center Y: {0:f3}", circle2.Center.Y);
                Console.WriteLine("Center Offset: {0:f3}", circle2.Center.Length);
                Console.WriteLine();

                Console.ReadLine();

                var function = AutomationLibrary.Mathematics.Curves.CircularFunction.FromCartesianPoints(censoredCenteredCartesian);
                function = function.SavitzkyGolaySmooth(7, 31);
                var area = function.ComputeArea();

                Console.WriteLine("Circular area: {0:f2}", circle.Radius * circle.Radius * Math.PI);
                Console.WriteLine("Smoothed function area: {0:f2}", area);

                Console.ReadLine();

                using (var writer = System.IO.File.CreateText(@"M:\Superior Energy\Test Runs\data_4.csv"))
                {
                    writer.WriteLine("X, Y, R, Theta");
                    foreach (var point in cartesianPoints)
                    {
                        var centered = point - circle.Center;

                        writer.WriteLine("{0},{1},{2},{3}", centered.X, centered.Y, centered.Length, centered.AngleFromXAxis);
                    }
                }
            }
        }
Exemplo n.º 2
0
        static void ClowWater()
        {
            List <Vector3> p3 = new List <Vector3>();

            p3.Add(new Vector3(1.1, 0.9, 1.0));
            p3.Add(new Vector3(6.9, 7.1, 7.0));

            var line3fit = AutomationLibrary.Mathematics.Fitting.GeometricFits.FitLine(p3);

            var line3   = Line3.FromPointAndDirection(new Vector3(0.5, 0.5, 0.5), new Vector3(1, 1, 1));
            var nearest = line3.GetClosestPoint(new Vector3(27, -1.4, 19));

            List <Vector2> points = new List <Vector2>();

            using (var reader = System.IO.File.OpenText(@"C:\Users\douglas\desktop\pipe.csv"))
            {
                reader.ReadLine(); // skip header
                while (true)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    var nums   = line.Split(',');
                    var values = nums.Select(n => double.Parse(n)).ToArray();
                    points.Add(new Vector2(values[0], values[1]));
                }
            }

            var ellipse = AutomationLibrary.Mathematics.Fitting.GeometricFits.FitEllipse(points);

            var ellipseFunc       = AutomationLibrary.Mathematics.Curves.CircularFunction.FromCartesianPoints(ellipse.Center, points);
            var smoothEllipseFunc = ellipseFunc.SavitzkyGolaySmooth(3, 21);


            points.Clear();
            points.AddRange(GeneratePointsOnEllipticalArc(new Vector2(0.37, -2.4), 21, 24.26, 96.3 * Math.PI / 180.0, .020, -95.0 * Math.PI / 180.0, 97.0 * Math.PI / 180.0).Take(1000));


            var pointSet = new PointCloud2(points);

            var voronoi = AutomationLibrary.Mathematics.Geometry.Voronoi.VoronoiDiagram.ComputeForPoints(points);

            voronoi = voronoi.Filter(0); // build map of nearest points

            var centersOfInfiniteCells = new HashSet <Vector2>();

            foreach (var edge in voronoi.Edges)
            {
                if (edge.IsPartlyInfinite)
                {
                    centersOfInfiniteCells.Add(edge.LeftData);
                    centersOfInfiniteCells.Add(edge.RightData);
                }
            }

            var pointSet2 = new PointCloud2(centersOfInfiniteCells);

            var mcc = pointSet2.ComputeMinimumCircumscribingCircle();
            var mic = ComputeMaximumInscribedCircle(pointSet, voronoi, mcc);
            var lsc = GeometricFits.FitCircle(points);

            using (var writer = System.IO.File.CreateText(@"C:\users\douglas\desktop\circlepoints.csv"))
            {
                writer.WriteLine("X,Y");
                foreach (var point in pointSet)
                {
                    writer.WriteLine("{0},{1}", point.X, point.Y);
                }
            }

            Console.WriteLine("n = {0}", points.Count);
            Console.WriteLine("MIC @ ({0}), r = {1}", mic.Center, mic.Radius);
            Console.WriteLine("LSC @ ({0}), r = {1}", lsc.Center, lsc.Radius);
            Console.WriteLine("MCC @ ({0}), r = {1}", mcc.Center, mcc.Radius);

            Console.WriteLine();

            Console.WriteLine("draw.circle({0}, {1}, {2}, border='{3}')", mic.Center.X, mic.Center.Y, mic.Radius, "red");
            Console.WriteLine("draw.circle({0}, {1}, {2}, border='{3}')", lsc.Center.X, lsc.Center.Y, lsc.Radius, "blue");
            Console.WriteLine("draw.circle({0}, {1}, {2}, border='{3}')", mcc.Center.X, mcc.Center.Y, mcc.Radius, "green");

            Console.ReadLine();
        }