public void Test_GetSortedUnexploredList()
        {
            string chosenMap = @"
                                  S 0 0 0 D
                                  0 0 0 0 0
                                  0 0 0 0 0";

            var graph = new DijkstraGraph(new Map(chosenMap));


            var unexplored = graph.GetSortedUnexploredList();

            Assert.AreEqual(15, unexplored.Count);

            graph.Get(2, 1).Selected = true;

            unexplored = graph.GetSortedUnexploredList();

            Assert.AreEqual(14, unexplored.Count);

            graph.reset();

            unexplored = graph.GetSortedUnexploredList();

            Assert.AreEqual(15, unexplored.Count);
        }
Exemple #2
0
    public static DijkstraAlgoData StartDijkstra(DijkstraGraph graph, uint starting_node, uint ending_node)
    {
        var dad = new DijkstraAlgoData();

        dad.graph         = graph;
        dad.starting_node = starting_node;
        dad.ending_node   = ending_node;

        dad.node_cost = new uint[graph.nodes.Length];
        dad.prev_node = new uint[graph.nodes.Length];

        for (int i = 0; i < graph.nodes.Length; ++i)
        {
            dad.node_cost[i] = 1000000000;
            dad.prev_node[i] = INVALID_NODE;
        }

        dad.node_cost[starting_node] = 0;

        dad.open_list      = new uint[graph.nodes.Length];
        dad.open_list[0]   = starting_node;
        dad.open_list_size = 1;

        return(dad);
    }
        public void TestConstructorAdjacents()
        {
            string chosenMap = "S 0 0 -5 D";
            var    graph     = new DijkstraGraph(new Map(chosenMap));

            Assert.AreEqual(1, graph.Get(0, 0).Adjacents.Count);
            Assert.AreEqual(graph.Get(0, 0).Adjacents.First(), graph.Get(1, 0));
        }
        public void TestConstructor()
        {
            string chosenMap = "S 0 0 -5 D";
            var    graph     = new DijkstraGraph(new Map(chosenMap));

            Assert.AreEqual(0, graph.Nodes["0|0"].Weight);
            Assert.AreEqual(5, graph.Nodes["3|0"].Weight);
        }
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: complete command.
            MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance;

            if (p.if_painted_object_set_ == false)
            {
                RhinoApp.WriteLine("No mesh");
                return(Result.Failure);
            }
            Mesh my_mesh = p.painted_object_;

            DijkstraGraph my_graph = p.graph;
            GetObject     gcurve   = new GetObject();

            gcurve.SetCommandPrompt("Get nurbscurve");
            gcurve.GeometryFilter  = Rhino.DocObjects.ObjectType.Curve;
            gcurve.SubObjectSelect = false;
            gcurve.Get();
            if (gcurve.CommandResult() != Result.Success)
            {
                return(gcurve.CommandResult());
            }
            ObjRef      curve_objref              = gcurve.Object(0);
            RhinoObject curve_obj                 = curve_objref.Object();
            Curve       selected_curve            = curve_objref.Curve();
            NurbsCurve  nurbs_curve               = selected_curve.ToNurbsCurve();
            Guid        path_id                   = selected_curve.UserDictionary.GetGuid("PathID");
            IEnumerable <RhinoObject> path_objref = doc.Objects.GetObjectList(ObjectType.Curve);

            foreach (RhinoObject path in path_objref)
            {
                doc.Objects.Delete(path, true);
            }
            List <NurbsCurve> new_path_list = new List <NurbsCurve>();

            new_path_list = my_graph.DijkstraPath_DeletePath(path_id);

            ObjectAttributes my_attributes = new ObjectAttributes();

            my_attributes.ObjectColor      = Color.Yellow;
            my_attributes.ColorSource      = ObjectColorSource.ColorFromObject;
            my_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject;
            my_attributes.PlotWeight       = 2.0;

            //doc.Objects.Delete(my_objref1, true);

            for (int i = 0; i < new_path_list.Count; i++)
            {
                Guid PathID = new_path_list[i].UserDictionary.GetGuid("PathID");
                my_attributes.ObjectId = PathID;
                doc.Objects.Add(new_path_list[i], my_attributes);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }
Exemple #6
0
    /*
     *  nodes: List of node names.
     *  edges: a array where every 3 elements constructs an edge.
     *      First element is starting node index, second is ending node index, third is movement cost (non-negative).
     */
    public static DijkstraGraph SetupGraph(string[] nodes, uint[] edges)
    {
        var dg = new DijkstraGraph();

        dg.nodes = nodes;
        dg.edges = edges;

        return(dg);
    }
        public void TestConstructorAdjacents2()
        {
            string chosenMap = @"
                                  S 0 0 0 D
                                  0 0 0 0 0
                                  0 0 0 0 0";

            var graph = new DijkstraGraph(new Map(chosenMap));

            Assert.AreEqual(2, graph.Get(0, 0).Adjacents.Count);

            Assert.AreEqual(4, graph.Get(2, 1).Adjacents.Count);
        }
        public void HasUnvisitedTest_ReturnFalseIf_AllIsVisitedIsTrue_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited == true)
                }
            };

            // Assert
            Assert.False(graph.HasUnvisited());
        }
Exemple #9
0
        public static DijkstraGraph Make(Graph graph)
        {
            var dijkstraGraph = new DijkstraGraph();

            foreach (var node in graph.Nodes)
            {
                dijkstraGraph.Nodes.Add(new DijkstraNode(node.Name));

                dijkstraGraph.Edges.AddRange(node.Routes
                                             .Select(route => new DijkstraEdge(route.Start.Name, route.End.Name, route.Cost)).ToList());
            }

            return(dijkstraGraph);
        }
        public void HasUnvisited_ReturnsTrue_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited),
                    Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == 1)
                }
            };

            // Assert
            Assert.True(graph.HasUnvisited());
        }
        public void HasUnvisitedTest_ReturnFalseIf_AllIsVisitedTrue_Or_MarkIsInfinity_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited),
                    Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == null)
                }
            };

            // Assert
            Assert.False(graph.HasUnvisited());
        }
        /// <summary>
        /// Returns list of nodes that represents the shortest way from start to finish node in graph.
        /// </summary>
        /// <exception cref="NoRouteFromStartToFinishException"></exception>
        public List<Node> GetShortestWay(DijkstraGraph graph)
        {
            var finish = graph.FinishNode();
            if (!finish.TotalWeigth.HasValue ||
                finish.Previous == null)
            {
                throw new NoRouteFromStartToFinishException();
            }

            var route = new List<Node>();
            while (finish != null)
            {
                route.Insert(0, finish);
                finish = finish.Previous;
            }

            return route;
        }
        public void Markup_SetNodeInitialMark_Test()
        {
            // Arrange
            var graph = new DijkstraGraph
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsStart),
                    Mock.Of<DijkstraNode>(t => t.IsStart == false)
                }
            };

            // Act
            graph.Markup();

            // Assert
            Assert.AreEqual(graph.Nodes[0].TotalWeigth, 0);
            Assert.IsNull(graph.Nodes[1].TotalWeigth);
        }
        public void Test_GetSortedUnexploredListSorting()
        {
            string chosenMap = @"
                                  S 0 0 0 D
                                  0 0 0 0 0
                                  0 0 0 0 0";

            var graph = new DijkstraGraph(new Map(chosenMap));


            graph.Get(3, 2).Distance = 5;
            graph.Get(0, 2).Distance = 3;
            graph.Get(2, 0).Distance = 2;

            var unexplored = graph.GetSortedUnexploredList();

            Assert.AreEqual(graph.Get(2, 0), unexplored[0]);
            Assert.AreEqual(graph.Get(0, 2), unexplored[1]);
            Assert.AreEqual(graph.Get(3, 2), unexplored[2]);
        }
Exemple #15
0
        public Route FindRoute(int fromStationId, int toStationId)
        {
            var allStationsWithNearestStationsDistances = stationsDictionary.Values.ToList();
            var fromStation = stationsDictionary[fromStationId];
            var toStation   = stationsDictionary[toStationId];

            var routeStations = DijkstraGraph.FindShortestPath(allStationsWithNearestStationsDistances, fromStation, toStation);

            var routeNodes = new List <Node>(routeStations.Count - 1);

            for (int i = 0; i < routeStations.Count - 1; ++i)
            {
                var currentStation = routeStations[i];
                var nextStation    = routeStations[i + 1];
                var timeDistance   = currentStation.GetNearestStationDistance(nextStation.Id);
                var node           = new Node(currentStation.Id, nextStation.Id, timeDistance);
                routeNodes.Add(node);
            }

            return(new Route(routeStations, routeNodes));
        }
        /// <summary>
        /// Computes total wieghts from start node for all nodes in graph
        /// </summary>
        public Dijkstra Compute(DijkstraGraph graph)
        {
            while (graph.HasUnvisited())
            {
                var current = graph.NearestUnvisited();
                foreach (var node in graph.Linked(current))
                {
                    var weightFromCurrent = current.TotalWeigth + current.LinkWeight(node);

                    // if this way shorter
                    if (!node.TotalWeigth.HasValue || weightFromCurrent < node.TotalWeigth)
                    {
                        node.TotalWeigth = weightFromCurrent;
                        node.Previous = current;
                    }
                }

                current.IsVisited = true;
            }

            return this;
        }
 public DijkstraAlgorithm(Graph graph)
 {
     _graph = DijkstraGraphFactory.Make(graph);
 }
Exemple #18
0
    // This function takes a grid where 1s are walls and 0s are empty space
    // and turns it into a graph. It does this by propgating through the grid using
    // Dijkstra to traverse the grid.
    public static DijkstraGraph SetupGrid(int width, int height, uint[] grid)
    {
        var dg = new DijkstraGraph();

        dg.nodes = new string[width * height];
        for (int i = 0; i < (width * height); ++i)
        {
            dg.nodes[i] = "" + i;
        }

        var open_list = new uint[grid.Length];

        open_list[0] = 0;
        uint open_list_size = 1;

        List <uint> edges = new List <uint>();

        while (open_list_size != 0)
        {
            var node = open_list[open_list_size - 1];
            open_list_size -= 1;

            int north = (int)node - width;
            int south = (int)node + width;

            int east = (int)node + 1;
            int west = (int)node - 1;

            if (node % width != 0)
            {
                // west is valid
                if (grid[west] == 0)
                {
                    edges.Add(node);
                    edges.Add((uint)west);
                    edges.Add(1);

                    open_list[open_list_size] = (uint)west;
                    open_list_size           += 1;
                }
            }
            if (node % width != width - 1)
            {
                // east is valid
                if (grid[east] == 0)
                {
                    edges.Add(node);
                    edges.Add((uint)east);
                    edges.Add(1);

                    open_list[open_list_size] = (uint)east;
                    open_list_size           += 1;
                }
            }
            if (north >= 0)
            {
                // north is valid
                if (grid[north] == 0)
                {
                    edges.Add(node);
                    edges.Add((uint)north);
                    edges.Add(1);

                    open_list[open_list_size] = (uint)north;
                    open_list_size           += 1;
                }
            }
            if (south < (width * height))
            {
                // south is valid
                if (grid[south] == 0)
                {
                    edges.Add(node);
                    edges.Add((uint)south);
                    edges.Add(1);

                    open_list[open_list_size] = (uint)south;
                    open_list_size           += 1;
                }
            }

            grid[node] = 2;
        }

        dg.edges = edges.ToArray();
        return(dg);
    }
        public void NearestVisited_ThrowsIfHasNotUnvisited_Test()
        {
            // Arrange
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    Mock.Of<DijkstraNode>(t => t.IsVisited),
                    Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == null)
                }
            };

            // Assert
            Assert.Throws<GraphHasNotUnvisitedNodesException>(() => graph.NearestUnvisited());
        }
        public void NearestUnvisited_ReturnsFirstUnvisitedWithMinimalMark_Test()
        {
            // Arrange
            var node1 = Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == 3);
            var node2 = Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == 3);
            var node3 = Mock.Of<DijkstraNode>(t => t.IsVisited == false && t.TotalWeigth == 10);
            var node4 = Mock.Of<DijkstraNode>(t => t.IsVisited == true && t.TotalWeigth == 1);
            var graph = new DijkstraGraph()
            {
                Nodes = new[]
                {
                    node1, node2, node3, node4
                }
            };

            // Act
            var nearest = graph.NearestUnvisited();

            // Assert
            Assert.AreEqual(nearest, node1);
        }
Exemple #21
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: complete command.

            MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance;

            if (p.if_painted_object_set_ == false)
            {
                RhinoApp.WriteLine("No mesh");
                return(Result.Failure);
            }
            Mesh my_mesh = p.painted_object_;

            DijkstraGraph my_graph = p.graph;

            GetObject gbrep = new GetObject();

            gbrep.SetCommandPrompt("get the brep");
            gbrep.GeometryFilter  = Rhino.DocObjects.ObjectType.Brep;
            gbrep.SubObjectSelect = false;
            gbrep.Get();
            if (gbrep.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gbrep.CommandResult());
            }
            Rhino.DocObjects.ObjRef      my_objref = gbrep.Object(0);
            Rhino.DocObjects.RhinoObject my_obj    = my_objref.Object();
            if (my_obj == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
            Brep brep = my_objref.Brep();

            if (brep == null)
            {
                return(Result.Failure);
            }
            my_obj.Select(false);

            int         pin_number_1         = My_object_functions.GetPinQuantity(brep);
            List <Guid> pin_ball_guid_list_1 = new List <Guid>();

            for (int i = 0; i < pin_number_1; i++)
            {
                ObjectAttributes green_ball_attributes = new ObjectAttributes();
                green_ball_attributes.ObjectColor = Color.Green;
                green_ball_attributes.ColorSource = ObjectColorSource.ColorFromObject;

                Point3d pin_position = My_object_functions.GetPinPosition(brep, i);
                Guid    pin_id       = My_object_functions.GetPinGuid(brep, i);
                green_ball_attributes.ObjectId = pin_id;
                green_ball_attributes.UserDictionary.Set("isPin", true);
                Sphere pin_ball = new Sphere(pin_position, 2);
                //Brep pin_ball_brep = pin_ball.ToBrep();
                doc.Objects.AddSphere(pin_ball, green_ball_attributes);
                pin_ball_guid_list_1.Add(pin_id);
            }
            doc.Views.Redraw();

            GetObject g_pinball = new GetObject();

            g_pinball.SetCommandPrompt("choose the pin");
            g_pinball.GeometryFilter = ObjectType.Surface;
            //g_pinball.SetCustomGeometryFilter(PinBallGeometryFilter);
            //g_pinball.DisablePreSelect();
            g_pinball.SubObjectSelect = false;
            g_pinball.Get();
            if (g_pinball.CommandResult() != Result.Success)
            {
                return(g_pinball.CommandResult());
            }
            if (g_pinball.Object(0).Brep() == null)
            {
                return(Result.Failure);
            }

            RhinoObject selected_pin_ball    = g_pinball.Object(0).Object();
            Guid        selected_pin_ball_id = selected_pin_ball.Id;

            for (int i = 0; i < pin_ball_guid_list_1.Count; i++)
            {
                doc.Objects.Delete(pin_ball_guid_list_1[i], true);
            }

            ObjectAttributes greenyellow_ball_attributes = new ObjectAttributes();

            greenyellow_ball_attributes.ObjectColor = Color.GreenYellow;
            greenyellow_ball_attributes.ColorSource = ObjectColorSource.ColorFromObject;
            greenyellow_ball_attributes.ObjectId    = selected_pin_ball_id;
            greenyellow_ball_attributes.UserDictionary.Set("isPin", true);
            int     pin_number            = My_object_functions.FindPinNumber(brep, selected_pin_ball_id);
            Point3d selected_pin_position = My_object_functions.GetPinPosition(brep, pin_number);
            Sphere  pin_ball_new          = new Sphere(selected_pin_position, 2);

            doc.Objects.AddSphere(pin_ball_new, greenyellow_ball_attributes);


            doc.Views.Redraw();

            //MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance;
            if (p.my_objects_list.Count <= 1)
            {
                RhinoApp.WriteLine("not enough objects");
                return(Result.Failure);
            }

            List <Guid> pin_ball_guid_list_2 = new List <Guid>();

            for (int i = 0; i < p.my_objects_list.Count; i++)
            {
                Brep an_object = p.my_objects_list[i];
                if (My_object_functions.GetComponentID(brep) != My_object_functions.GetComponentID(an_object))
                {
                    int pin_quantity = My_object_functions.GetPinQuantity(an_object);
                    for (int j = 0; j < pin_quantity; j++)
                    {
                        ObjectAttributes green_ball_attributes = new ObjectAttributes();
                        green_ball_attributes.ObjectColor = Color.Green;
                        green_ball_attributes.ColorSource = ObjectColorSource.ColorFromObject;
                        Point3d pin_position = My_object_functions.GetPinPosition(an_object, j);
                        Guid    pin_id       = My_object_functions.GetPinGuid(an_object, j);
                        green_ball_attributes.ObjectId = pin_id;
                        green_ball_attributes.UserDictionary.Set("isPin", true);
                        Sphere pin_ball = new Sphere(pin_position, 2);
                        doc.Objects.AddSphere(pin_ball, green_ball_attributes);
                        pin_ball_guid_list_2.Add(pin_id);
                    }
                }
            }
            doc.Views.Redraw();

            GetObject g_pinball_2 = new GetObject();

            g_pinball_2.SetCommandPrompt("choose the pin");
            g_pinball_2.GeometryFilter = ObjectType.Surface;
            //g_pinball.SetCustomGeometryFilter(PinBallGeometryFilter);
            //g_pinball.DisablePreSelect();
            g_pinball_2.SubObjectSelect = false;
            g_pinball_2.Get();
            if (g_pinball_2.CommandResult() != Result.Success)
            {
                return(g_pinball_2.CommandResult());
            }
            if (g_pinball_2.Object(0).Brep() == null)
            {
                return(Result.Failure);
            }

            RhinoObject selected_pin_ball_2    = g_pinball_2.Object(0).Object();
            Guid        selected_pin_ball_id_2 = selected_pin_ball_2.Id;
            Brep        brep_2 = null;

            for (int i = 0; i < p.my_objects_list.Count; i++)
            {
                Brep an_object    = p.my_objects_list[i];
                int  pin_quantity = My_object_functions.GetPinQuantity(an_object);
                bool mark         = false;
                for (int j = 0; j < pin_quantity; j++)
                {
                    if (My_object_functions.GetPinGuid(an_object, j) == selected_pin_ball_id_2)
                    {
                        mark = true;
                        break;
                    }
                }
                if (mark)
                {
                    brep_2 = an_object;
                    break;
                }
            }
            if (brep_2 == null)
            {
                return(Result.Failure);
            }

            int selected_pin_number_2 = My_object_functions.FindPinNumber(brep_2, selected_pin_ball_id_2);

            for (int i = 0; i < pin_ball_guid_list_2.Count; i++)
            {
                doc.Objects.Delete(pin_ball_guid_list_2[i], true);
            }

            Point3d selected_pin_position_2 = My_object_functions.GetPinPosition(brep_2, selected_pin_number_2);

            Sphere pin_ball_new_2 = new Sphere(selected_pin_position_2, 2);

            greenyellow_ball_attributes.ObjectId = selected_pin_ball_id_2;

            doc.Objects.AddSphere(pin_ball_new_2, greenyellow_ball_attributes);

            doc.Views.Redraw();

            MeshPoint  pin_1_meshpoint = my_mesh.ClosestMeshPoint(selected_pin_position, 0);
            MeshPoint  pin_2_meshpoint = my_mesh.ClosestMeshPoint(selected_pin_position_2, 0);
            NurbsCurve d_path          = my_graph.DijkstraPath_Add(pin_1_meshpoint, selected_pin_ball_id, pin_2_meshpoint, selected_pin_ball_id_2);


            if (d_path != null)
            {
                ObjectAttributes path_attributes = new ObjectAttributes();
                path_attributes.ObjectColor      = Color.Yellow;
                path_attributes.ColorSource      = ObjectColorSource.ColorFromObject;
                path_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject;
                path_attributes.PlotWeight       = 2.0;
                path_attributes.ObjectId         = d_path.UserDictionary.GetGuid("PathID");
                doc.Objects.AddCurve(d_path, path_attributes);
            }



            doc.Objects.Delete(selected_pin_ball_id, true);
            doc.Objects.Delete(selected_pin_ball_id_2, true);

            doc.Views.Redraw();



            return(Result.Success);
        }
Exemple #22
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: complete command.

            MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance;

            if (p.if_painted_object_set_ == false)
            {
                RhinoApp.WriteLine("No mesh");
                return(Result.Failure);
            }
            Mesh my_mesh = p.painted_object_;

            DijkstraGraph my_graph = p.graph;

            GetObject gbrep1 = new GetObject();

            gbrep1.SetCommandPrompt("get the brep");
            gbrep1.GeometryFilter  = Rhino.DocObjects.ObjectType.Brep;
            gbrep1.SubObjectSelect = false;
            gbrep1.Get();
            if (gbrep1.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gbrep1.CommandResult());
            }
            Rhino.DocObjects.ObjRef      my_objref1 = gbrep1.Object(0);
            Rhino.DocObjects.RhinoObject my_obj1    = my_objref1.Object();
            if (my_obj1 == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
            Brep brep1 = my_objref1.Brep();

            if (brep1 == null)
            {
                return(Result.Failure);
            }
            my_obj1.Select(false);

            GetObject gbrep2 = new GetObject();

            gbrep2.SetCommandPrompt("get the brep");
            gbrep2.GeometryFilter  = Rhino.DocObjects.ObjectType.Brep;
            gbrep2.SubObjectSelect = false;
            gbrep2.Get();
            if (gbrep2.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gbrep2.CommandResult());
            }
            Rhino.DocObjects.ObjRef      my_objref2 = gbrep2.Object(0);
            Rhino.DocObjects.RhinoObject my_obj2    = my_objref2.Object();
            if (my_obj2 == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
            Brep brep2 = my_objref2.Brep();

            if (brep2 == null)
            {
                return(Result.Failure);
            }
            my_obj2.Select(false);

            Point3d pin_1_position = brep1.UserDictionary.GetPoint3d("CurrentPosition");
            Point3d pin_2_position = brep2.UserDictionary.GetPoint3d("CurrentPosition");
            Guid    pin_1_id       = brep1.UserDictionary.GetGuid("PinID");
            Guid    pin_2_id       = brep2.UserDictionary.GetGuid("PinID");

            MeshPoint pin_1_meshpoint = my_mesh.ClosestMeshPoint(pin_1_position, 0);
            MeshPoint pin_2_meshpoint = my_mesh.ClosestMeshPoint(pin_2_position, 0);
            Stopwatch watch           = new Stopwatch();

            watch.Start();
            NurbsCurve d_path = my_graph.DijkstraPath_Add(pin_1_meshpoint, pin_1_id, pin_2_meshpoint, pin_2_id);

            watch.Stop();
            if (d_path == null)
            {
                return(Result.Success);
            }
            RhinoApp.WriteLine("link time: {0}", watch.Elapsed);
            ObjectAttributes my_attributes = new ObjectAttributes();

            my_attributes.ObjectColor      = Color.Yellow;
            my_attributes.ColorSource      = ObjectColorSource.ColorFromObject;
            my_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject;
            my_attributes.PlotWeight       = 2.0;

            doc.Objects.AddCurve(d_path, my_attributes);
            doc.Views.Redraw();

            return(Result.Success);
        }
Exemple #23
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: complete command.
            MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance;

            if (p.if_painted_object_set_ == false)
            {
                RhinoApp.WriteLine("No mesh");
                return(Result.Failure);
            }
            Mesh my_mesh = p.painted_object_;

            DijkstraGraph my_graph = p.graph;

            GetObject gbrep1 = new GetObject();

            gbrep1.SetCommandPrompt("get the brep");
            gbrep1.GeometryFilter  = Rhino.DocObjects.ObjectType.Brep;
            gbrep1.SubObjectSelect = false;
            gbrep1.Get();
            if (gbrep1.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gbrep1.CommandResult());
            }
            Rhino.DocObjects.ObjRef      my_objref1 = gbrep1.Object(0);
            Rhino.DocObjects.RhinoObject my_obj1    = my_objref1.Object();
            if (my_obj1 == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
            Brep brep1 = my_objref1.Brep();

            if (brep1 == null)
            {
                return(Result.Failure);
            }
            my_obj1.Select(false);

            for (int i = 0; i < p.my_objects_list.Count; i++)
            {
                Guid brep1_id     = My_object_functions.GetComponentID(brep1);
                Guid my_object_id = My_object_functions.GetComponentID(p.my_objects_list[i]);
                if (brep1_id == my_object_id)
                {
                    p.my_objects_list.RemoveAt(i);
                }
            }

            IEnumerable <RhinoObject> path_objref = doc.Objects.GetObjectList(ObjectType.Curve);

            foreach (RhinoObject path in path_objref)
            {
                doc.Objects.Delete(path, true);
            }

            List <NurbsCurve> new_path_list = new List <NurbsCurve>();
            int pin_number = My_object_functions.GetPinQuantity(brep1);

            for (int i = 0; i < pin_number; i++)
            {
                Guid pin_id = My_object_functions.GetPinGuid(brep1, i);
                new_path_list = p.graph.DijkstraPath_DeletePin(pin_id);
            }

            ObjectAttributes my_attributes = new ObjectAttributes();

            my_attributes.ObjectColor      = Color.Yellow;
            my_attributes.ColorSource      = ObjectColorSource.ColorFromObject;
            my_attributes.PlotWeightSource = ObjectPlotWeightSource.PlotWeightFromObject;
            my_attributes.PlotWeight       = 2.0;

            doc.Objects.Delete(my_objref1, true);

            for (int i = 0; i < new_path_list.Count; i++)
            {
                Guid path_id = new_path_list[i].UserDictionary.GetGuid("PathID");
                my_attributes.ObjectId = path_id;
                doc.Objects.Add(new_path_list[i], my_attributes);
            }

            doc.Views.Redraw();

            return(Result.Success);
        }