Esempio n. 1
0
        private static bool compute_plane_curves(DMesh3 mesh, DMeshAABBTree3 spatial,
                                                 double z, bool is_solid,
                                                 out Polygon2d[] loops, out PolyLine2d[] curves)
        {
            Func <Vector3d, double> planeF = (v) =>
            {
                return(v.z - z);
            };

            // find list of triangles that intersect this z-value
            PlaneIntersectionTraversal planeIntr = new PlaneIntersectionTraversal(mesh, z);

            spatial.DoTraversal(planeIntr);
            List <int> triangles = planeIntr.triangles;

            // compute intersection iso-curves, which produces a 3D graph of undirected edges
            MeshIsoCurves iso = new MeshIsoCurves(mesh, planeF)
            {
                WantGraphEdgeInfo = true
            };

            iso.Compute(triangles);
            DGraph3 graph = iso.Graph;

            if (graph.EdgeCount == 0)
            {
                loops  = new Polygon2d[0];
                curves = new PolyLine2d[0];
                return(false);
            }

            // if this is a closed solid, any open spurs in the graph are errors
            if (is_solid)
            {
                DGraph3Util.ErodeOpenSpurs(graph);
            }

            // [RMS] debug visualization
            //DGraph2 graph2 = new DGraph2();
            //Dictionary<int, int> mapV = new Dictionary<int, int>();
            //foreach (int vid in graph.VertexIndices())
            //    mapV[vid] = graph2.AppendVertex(graph.GetVertex(vid).xy);
            //foreach (int eid in graph.EdgeIndices())
            //    graph2.AppendEdge(mapV[graph.GetEdge(eid).a], mapV[graph.GetEdge(eid).b]);
            //SVGWriter svg = new SVGWriter();
            //svg.AddGraph(graph2, SVGWriter.Style.Outline("black", 0.05f));
            //foreach (int vid in graph2.VertexIndices()) {
            //    if (graph2.IsJunctionVertex(vid))
            //        svg.AddCircle(new Circle2d(graph2.GetVertex(vid), 0.25f), SVGWriter.Style.Outline("red", 0.1f));
            //    else if (graph2.IsBoundaryVertex(vid))
            //        svg.AddCircle(new Circle2d(graph2.GetVertex(vid), 0.25f), SVGWriter.Style.Outline("blue", 0.1f));
            //}
            //svg.Write(string.Format("c:\\meshes\\EXPORT_SLICE_{0}.svg", z));

            // extract loops and open curves from graph
            DGraph3Util.Curves c = DGraph3Util.ExtractCurves(graph, false, iso.ShouldReverseGraphEdge);
            loops = new Polygon2d[c.Loops.Count];
            for (int li = 0; li < loops.Length; ++li)
            {
                DCurve3 loop = c.Loops[li];
                loops[li] = new Polygon2d();
                foreach (Vector3d v in loop.Vertices)
                {
                    loops[li].AppendVertex(v.xy);
                }
            }

            curves = new PolyLine2d[c.Paths.Count];
            for (int pi = 0; pi < curves.Length; ++pi)
            {
                DCurve3 span = c.Paths[pi];
                curves[pi] = new PolyLine2d();
                foreach (Vector3d v in span.Vertices)
                {
                    curves[pi].AppendVertex(v.xy);
                }
            }

            return(true);
        }