Example #1
0
        //private void CreateCentroidVertexGraph(TriangleNet.Mesh mesh)
        //{
        //    pointMapping = new Dictionary<PointStruct, uint>();
        //    graph = new Graph<Point, string>();

        //    pointMapping = new Dictionary<PointStruct, uint>();
        //    graph = new Graph<Point, string>();

        //    foreach (var tri in mesh.Triangles)
        //    {
        //        var vertices = new List<PointStruct>();

        //        var centroid = new PointStruct(tri.GetCentroid());

        //        if (!pointMapping.ContainsKey(centroid))
        //        {
        //            var id = graph.AddNode(new Point(centroid.X, centroid.Y));
        //            pointMapping.Add(centroid, id);
        //        }

        //        var cID = pointMapping[centroid];

        //        for (var i = 0; i < 3; i++)
        //        {
        //            var neighbor = tri.GetNeighbor(i);

        //            if (neighbor != null)
        //            {
        //                var neighborCentroid = new PointStruct(((TriangleNet.Topology.Triangle)neighbor).GetCentroid());

        //                if (!pointMapping.ContainsKey(neighborCentroid))
        //                {
        //                    var id = graph.AddNode(new Point(neighborCentroid.X, neighborCentroid.Y));
        //                    pointMapping.Add(neighborCentroid, id);
        //                }


        //                var nID = pointMapping[neighborCentroid];

        //                var dist = centroid.Distance(neighborCentroid);

        //                graph.Connect(cID, nID, (int)Math.Ceiling(dist), "");
        //            }

        //            for (var x = 0; x < 3; x++)
        //            {
        //                var iPoint = new PointStruct(tri.GetVertex(i));
        //                if (!pointMapping.ContainsKey(iPoint))
        //                {
        //                    var id = graph.AddNode(new Point(iPoint.X, iPoint.Y));
        //                    pointMapping.Add(iPoint, id);
        //                }
        //                var iID = pointMapping[iPoint];
        //                var cdist = iPoint.Distance(centroid);
        //                graph.Connect(cID, iID, (int)cdist, "");
        //                graph.Connect(iID, cID, (int)cdist, "");
        //                if (i != x)
        //                {
        //                    var xPoint = new PointStruct(tri.GetVertex(x));



        //                    if (!pointMapping.ContainsKey(xPoint))
        //                    {
        //                        var id = graph.AddNode(new Point(xPoint.X, xPoint.Y));
        //                        pointMapping.Add(xPoint, id);
        //                    }


        //                    var xID = pointMapping[xPoint];

        //                    var dist = iPoint.Distance(xPoint);



        //                    graph.Connect(iID, xID, (int)Math.Ceiling(dist), "");

        //                }
        //            }
        //        }
        //    }
        //}

        //private void CreateVertexGraph(TriangleNet.Mesh mesh)
        //{
        //    pointMapping = new Dictionary<PointStruct, uint>();
        //    graph = new Graph<Point, string>();


        //    foreach (var tri in mesh.Triangles)
        //    {
        //        var vertices = new List<PointStruct>();

        //        for (var i = 0; i < 3; i++)
        //        {
        //            for (var x = 0; x < 3; x++)
        //            {
        //                if (i != x)
        //                {
        //                    var iPoint = new PointStruct(tri.GetVertex(i));
        //                    var xPoint = new PointStruct(tri.GetVertex(x));

        //                    if (!pointMapping.ContainsKey(iPoint))
        //                    {
        //                        var id = graph.AddNode(new Point(iPoint.X, iPoint.Y));
        //                        pointMapping.Add(iPoint, id);
        //                    }

        //                    if (!pointMapping.ContainsKey(xPoint))
        //                    {
        //                        var id = graph.AddNode(new Point(xPoint.X, xPoint.Y));
        //                        pointMapping.Add(xPoint, id);
        //                    }

        //                    var iID = pointMapping[iPoint];
        //                    var xID = pointMapping[xPoint];

        //                    var dist = iPoint.Distance(xPoint);

        //                    graph.Connect(iID, xID, (int)Math.Ceiling(dist), "");
        //                }
        //            }
        //        }
        //    }
        //}

        //private void CreateCentroidGraph(TriangleNet.Mesh mesh)
        //{
        //    pointMapping = new Dictionary<PointStruct, uint>();
        //    graph = new Graph<Point, string>();


        //    foreach (var tri in mesh.Triangles)
        //    {
        //        var centroid = new PointStruct(tri.GetCentroid());

        //        for (var i = 0; i < 3; i++)
        //        {
        //            var neighbor = tri.GetNeighbor(i);

        //            if (neighbor != null)
        //            {
        //                var neighborCentroid = new PointStruct(((TriangleNet.Topology.Triangle)neighbor).GetCentroid());

        //                if (!pointMapping.ContainsKey(centroid))
        //                {
        //                    var id = graph.AddNode(new Point(centroid.X, centroid.Y));
        //                    pointMapping.Add(centroid, id);
        //                }

        //                if (!pointMapping.ContainsKey(neighborCentroid))
        //                {
        //                    var id = graph.AddNode(new Point(neighborCentroid.X, neighborCentroid.Y));
        //                    pointMapping.Add(neighborCentroid, id);
        //                }

        //                var iID = pointMapping[centroid];
        //                var xID = pointMapping[neighborCentroid];

        //                var dist = centroid.Distance(neighborCentroid);

        //                graph.Connect(iID, xID, (int)Math.Ceiling(dist), "");
        //            }
        //        }
        //    }
        //}

        private uint?GetNearestNode(PointStruct point)
        {
            KeyValuePair <GraphNode, uint>?insideNode = null;

            foreach (var node in nodeMapping)
            {
                if (node.Key.PointInNode(new PointD(point.X, point.Y)))
                {
                    insideNode = node;
                    break;
                }
            }

            if (insideNode == null)
            {
                var sortedKeys = pointMapping.Keys.OrderBy(e => e.Distance(point));

                foreach (var key in sortedKeys)
                {
                    if (!pointMap.IsOffsetInsideMap(new Point(point.X, point.Y)))
                    {
                        return(pointMapping[key]);
                    }
                    else
                    {
                        //var crossesEdge = LineCrossesEdge(new Line(new Point(point.X, point.Y), new Point(key.X, key.Y)));
                        var isInterior = pointMap.IsOffsetInterior(new Line(new Point(point.X, point.Y), new Point(key.X, key.Y)));
                        if (isInterior)
                        {
                            return(pointMapping[key]);
                        }
                    }
                }
            }
            else
            {
                return(insideNode.Value.Value);
            }

            return(null);
        }