Beispiel #1
0
        public static GeneralPolygon2d ToPolygon(this List <Dataline> list, ref Frame3f frame)
        {
            List <VertexLookup> VertexTable = list[0].VertexTable;

            Vector3d[] vertices = new Vector3d[VertexTable.Count];
            for (int j = 0; j < VertexTable.Count; j++)
            {
                vertices[j] = VertexTable.Find(item => item.Vertex == j).Com.transform.position;
            }
            OrthogonalPlaneFit3 orth = new OrthogonalPlaneFit3(vertices);

            frame = new Frame3f(orth.Origin, orth.Normal);
            GeneralPolygon2d poly = new GeneralPolygon2d(new Polygon2d());

            for (int i = 0; i < list.Count; i++)
            {
                VertexTable = list[i].VertexTable;
                vertices    = new Vector3d[VertexTable.Count];
                for (int j = 0; j < VertexTable.Count; j++)
                {
                    vertices[j] = VertexTable.Find(item => item.Vertex == j).Com.transform.position;
                }
                List <Vector2d> vertices2d = new List <Vector2d>();
                foreach (Vector3d v in vertices)
                {
                    Vector2f vertex = frame.ToPlaneUV((Vector3f)v, 3);
                    if (i != 0 && !poly.Outer.Contains(vertex))
                    {
                        break;
                    }
                    vertices2d.Add(vertex);
                }
                Polygon2d p2d = new Polygon2d(vertices2d);
                if (i == 0)
                {
                    p2d = new Polygon2d(vertices2d);
                    p2d.Reverse();
                    poly.Outer = p2d;
                }
                else
                {
                    try {
                        poly.AddHole(p2d, true, true);
                    } catch {
                        p2d.Reverse();
                        poly.AddHole(p2d, true, true);
                    }
                }
            }
            return(poly);
        }
Beispiel #2
0
        protected virtual Polygon2d make_thickened_path(PolyLine2d path, double width)
        {
            PolyLine2d pos = new PolyLine2d(path), neg = new PolyLine2d(path);

            pos.VertexOffset(width / 2);
            neg.VertexOffset(-width / 2); neg.Reverse();
            pos.AppendVertices(neg);
            Polygon2d poly = new Polygon2d(pos.Vertices);

            if (poly.IsClockwise)
            {
                poly.Reverse();
            }
            return(poly);
        }
Beispiel #3
0
        public static void TestFill()
        {
            Window window = new Window("TestFill");

            window.SetDefaultSize(600, 600);
            window.SetPosition(WindowPosition.Center);

            DebugViewCanvas view = new DebugViewCanvas();

            GeneralPolygon2d poly = new GeneralPolygon2d(
                Polygon2d.MakeCircle(20, 32));

            Polygon2d hole = Polygon2d.MakeCircle(15, 32);

            hole.Reverse();
            hole.Translate(2 * Vector2d.AxisX);
            poly.AddHole(hole);

            view.AddPolygon(poly, Colorf.Black);

            double spacing = 0.5;

            double[] offsets = new double[] { 5 };

            foreach (double offset in offsets)
            {
                DGraph2            graph = TopoOffset2d.QuickCompute(poly, offset, spacing);
                DGraph2Util.Curves c     = DGraph2Util.ExtractCurves(graph);
                //view.AddGraph(graph, Colorf.Red);

                //DGraph2 perturbGraph = perturb_fill(graph, poly, 5.0f, spacing);
                DGraph2 perturbGraph = perturb_fill_2(graph, poly, 1.0f, spacing);
                //DGraph2Util.Curves c2 = DGraph2Util.ExtractCurves(perturbGraph);
                view.AddGraph(perturbGraph, Colorf.Orange);
            }

            window.Add(view);
            window.ShowAll();

            Active = view;
        }
Beispiel #4
0
        /// <summary>
        /// 找到线圈南侧的点
        /// </summary>
        /// <returns></returns>
        public static List <Vector2d> SourthVertices(this Polygon2d polygon2d)
        {
            if (polygon2d.IsClockwise) // 为true则是顺时针,
            {
                polygon2d.Reverse();   // 强制所有线圈为逆时针方向
            }

            // 1 找到线圈点集的最左、最右,同时偏下的角点
            Vector2d leftDownPoint  = polygon2d.MaxLeftMaxDownPoint();
            Vector2d rightDownPoint = polygon2d.MaxRightMaxDownPoint();

            // 2 基于点进行重新排序
            List <Vector2d> path_Vector2d = polygon2d.ReSortPolygon2dByPoint(leftDownPoint).Vertices.ToList();

            int leftDownIndex  = path_Vector2d.IndexOf(leftDownPoint);
            int rightDownIndex = path_Vector2d.IndexOf(rightDownPoint);

            int length = rightDownIndex - leftDownIndex + 1;

            return(path_Vector2d.GetRange(leftDownIndex, length));
        }
Beispiel #5
0
        public static GeneralPolygon2d ToPolygon(this List <DCurve3> list, ref Frame3f frame)
        {
            OrthogonalPlaneFit3 orth = new OrthogonalPlaneFit3(list[0].Vertices);

            frame = new Frame3f(orth.Origin, orth.Normal);
            GeneralPolygon2d poly = new GeneralPolygon2d(new Polygon2d());

            for (int i = 0; i < list.Count; i++)
            {
                List <Vector3d> vertices   = list[i].Vertices.ToList();
                List <Vector2d> vertices2d = new List <Vector2d>();
                foreach (Vector3d v in vertices)
                {
                    Vector2f vertex = frame.ToPlaneUV((Vector3f)v, 3);
                    if (i != 0 && !poly.Outer.Contains(vertex))
                    {
                        break;
                    }
                    vertices2d.Add(vertex);
                }
                Polygon2d p2d = new Polygon2d(vertices2d);
                if (i == 0)
                {
                    p2d = new Polygon2d(vertices2d);
                    p2d.Reverse();
                    poly.Outer = p2d;
                }
                else
                {
                    try {
                        poly.AddHole(p2d, true, true);
                    } catch {
                        p2d.Reverse();
                        poly.AddHole(p2d, true, true);
                    }
                }
            }
            return(poly);
        }
Beispiel #6
0
        public static void TestDGraph2()
        {
            Window window = new Window("TestDGraph2");

            window.SetDefaultSize(600, 600);
            window.SetPosition(WindowPosition.Center);

            DebugViewCanvas view = new DebugViewCanvas();

            GeneralPolygon2d poly = new GeneralPolygon2d(
                Polygon2d.MakeCircle(10, 32));

            //Polygon2d hole = Polygon2d.MakeCircle(9, 32);
            //hole.Reverse();
            //poly.AddHole(hole);

            Polygon2d hole = Polygon2d.MakeCircle(5, 32);

            hole.Translate(new Vector2d(2, 0));
            hole.Reverse();
            poly.AddHole(hole);

            Polygon2d hole2 = Polygon2d.MakeCircle(1, 32);

            hole2.Translate(-6 * Vector2d.AxisX);
            hole2.Reverse();
            poly.AddHole(hole2);

            Polygon2d hole3 = Polygon2d.MakeCircle(1, 32);

            hole3.Translate(-6 * Vector2d.One);
            hole3.Reverse();
            poly.AddHole(hole3);

            Polygon2d hole4 = Polygon2d.MakeCircle(1, 32);

            hole4.Translate(7 * Vector2d.AxisY);
            hole4.Reverse();
            poly.AddHole(hole4);

            view.AddPolygon(poly, Colorf.Black);

            double spacing = 0.2;

            //double[] offsets = new double[] { 0.5, 1, 1.5, 2, 2.5 };
            double[] offsets = new double[] { 0.2, 0.6 };

            TopoOffset2d o = new TopoOffset2d(poly)
            {
                PointSpacing = spacing
            };

            foreach (double offset in offsets)
            {
                o.Offset = offset;
                DGraph2            graph = o.Compute();
                DGraph2Util.Curves c     = DGraph2Util.ExtractCurves(graph);
                view.AddGraph(graph, Colorf.Red);
            }



            window.Add(view);
            window.ShowAll();
        }
        public static void test_splitter()
        {
            Polygon2d        poly  = Polygon2d.MakeCircle(1000, 16);
            Polygon2d        hole  = Polygon2d.MakeCircle(500, 32); hole.Reverse();
            GeneralPolygon2d gpoly = new GeneralPolygon2d(poly);

            gpoly.AddHole(hole);
            //Polygon2d poly = Polygon2d.MakeRectangle(Vector2d.Zero, 1000, 1000);

            DGraph2 graph = new DGraph2();

            graph.AppendPolygon(gpoly);

            System.Console.WriteLine("Stats before: verts {0} edges {1} ", graph.VertexCount, graph.EdgeCount);

            GraphSplitter2d splitter = new GraphSplitter2d(graph);

            splitter.InsideTestF = gpoly.Contains;

            for (int k = 0; k < poly.VertexCount; ++k)
            {
                Line2d line = new Line2d(poly[k], Vector2d.AxisY);
                splitter.InsertLine(line);
            }
            System.Console.WriteLine("Stats after 1: verts {0} edges {1} ", graph.VertexCount, graph.EdgeCount);
            for (int k = 0; k < poly.VertexCount; ++k)
            {
                Line2d line = new Line2d(poly[k], Vector2d.AxisX);
                splitter.InsertLine(line);
            }
            for (int k = 0; k < poly.VertexCount; ++k)
            {
                Line2d line = new Line2d(poly[k], Vector2d.One.Normalized);
                splitter.InsertLine(line);
            }
            for (int k = 0; k < poly.VertexCount; ++k)
            {
                Line2d line = new Line2d(poly[k], new Vector2d(1, -1).Normalized);
                splitter.InsertLine(line);
            }

            System.Console.WriteLine("Stats after: verts {0} edges {1} ", graph.VertexCount, graph.EdgeCount);


            Random r = new Random(31337);

            foreach (int vid in graph.VertexIndices())
            {
                Vector2d v = graph.GetVertex(vid);
                v += TestUtil.RandomPoints2(1, r, v, 25)[0];
                graph.SetVertex(vid, v);
            }



            SVGWriter svg = new SVGWriter();

            svg.AddGraph(graph);

            var vtx_style = SVGWriter.Style.Outline("red", 1.0f);

            foreach (int vid in graph.VertexIndices())
            {
                Vector2d v = graph.GetVertex(vid);
                svg.AddCircle(new Circle2d(v, 10), vtx_style);
            }

            svg.Write(TestUtil.GetTestOutputPath("split_graph.svg"));
        }
        public static void test_cells()
        {
            Polygon2d        outer = Polygon2d.MakeCircle(1000, 17);
            Polygon2d        hole  = Polygon2d.MakeCircle(100, 32); hole.Reverse();
            GeneralPolygon2d gpoly = new GeneralPolygon2d(outer);

            gpoly.AddHole(hole);

            DGraph2 graph = new DGraph2();

            graph.AppendPolygon(gpoly);

            GraphSplitter2d splitter = new GraphSplitter2d(graph);

            splitter.InsideTestF = gpoly.Contains;
            for (int k = 0; k < outer.VertexCount; ++k)
            {
                Line2d line = new Line2d(outer[k], Vector2d.AxisY);
                splitter.InsertLine(line);
            }
            for (int k = 0; k < outer.VertexCount; ++k)
            {
                Line2d line = new Line2d(outer[k], Vector2d.AxisX);
                splitter.InsertLine(line);
            }
            for (int k = 0; k < outer.VertexCount; ++k)
            {
                Line2d line = new Line2d(outer[k], Vector2d.One.Normalized);
                splitter.InsertLine(line);
            }
            for (int k = 0; k < outer.VertexCount; ++k)
            {
                Line2d line = new Line2d(outer[k], new Vector2d(1, -1).Normalized);
                splitter.InsertLine(line);
            }

            GraphCells2d cells = new GraphCells2d(graph);

            cells.FindCells();

            List <Polygon2d> polys = cells.ContainedCells(gpoly);

            for (int k = 0; k < polys.Count; ++k)
            {
                double offset = polys[k].IsClockwise ? 4 : 20;
                polys[k].PolyOffset(offset);
            }


            PlanarComplex cp = new PlanarComplex();

            for (int k = 0; k < polys.Count; ++k)
            {
                cp.Add(polys[k]);
            }

            // convert back to solids
            var options = PlanarComplex.FindSolidsOptions.Default;

            options.WantCurveSolids            = false;
            options.SimplifyDeviationTolerance = 0;
            var solids = cp.FindSolidRegions(options);

            SVGWriter svg = new SVGWriter();

            svg.AddGraph(graph, SVGWriter.Style.Outline("red", 5));
            for (int k = 0; k < polys.Count; ++k)
            {
                svg.AddPolygon(polys[k], SVGWriter.Style.Outline("black", 1));
            }

            svg.Write(TestUtil.GetTestOutputPath("cells_graph.svg"));
        }