コード例 #1
0
        private static void Main(string[] args)
        {
            var undirectedCompletedGraph = new UndirectedGraph <string, TaggedUndirectedEdge <string, int> >();

            var edge1 = new TaggedUndirectedEdge <string, int>("A", "B", 2);
            var edge2 = new TaggedUndirectedEdge <string, int>("A", "E", 5);
            var edge3 = new TaggedUndirectedEdge <string, int>("A", "D", 12);
            var edge4 = new TaggedUndirectedEdge <string, int>("B", "C", 4);
            var edge5 = new TaggedUndirectedEdge <string, int>("B", "D", 8);
            var edge6 = new TaggedUndirectedEdge <string, int>("C", "E", 3);
            var edge7 = new TaggedUndirectedEdge <string, int>("C", "D", 3);
            var edge8 = new TaggedUndirectedEdge <string, int>("D", "E", 10);

            undirectedCompletedGraph.AddVerticesAndEdgeRange(
                new List <TaggedUndirectedEdge <string, int> > {
                edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8
            });

            var startVertex = "B";

            var travelingSalesmanResolver = new TravelingSalesmanResolver();
            var travelingVertices         =
                travelingSalesmanResolver.ApproximateWith2Approximation(undirectedCompletedGraph, startVertex);

            Console.WriteLine($"Salesman should travel followering vertices: {string.Join(", ", travelingVertices)}");

            Console.ReadLine();
        }
コード例 #2
0
        public void Run()
        {
            var g = new UndirectedGraph <int, TaggedUndirectedEdge <int, int> >();

            var e1 = new TaggedUndirectedEdge <int, int>(1, 2, 57);
            var e2 = new TaggedUndirectedEdge <int, int>(1, 4, 65);
            var e3 = new TaggedUndirectedEdge <int, int>(2, 3, 500);
            var e4 = new TaggedUndirectedEdge <int, int>(2, 4, 1);
            var e5 = new TaggedUndirectedEdge <int, int>(3, 4, 78);
            var e6 = new TaggedUndirectedEdge <int, int>(3, 5, 200);

            g.AddVerticesAndEdge(e1);
            g.AddVerticesAndEdge(e2);
            g.AddVerticesAndEdge(e3);
            g.AddVerticesAndEdge(e4);
            g.AddVerticesAndEdge(e5);
            g.AddVerticesAndEdge(e6);

            foreach (var v in g.Edges)
            {
                Console.WriteLine(v);
            }
            Console.WriteLine("--------");
            //Func<TaggedUndirectedEdge<int, int>, double> edgeWeights = (q) =>
            //{
            //    g.Edges.SingleOrDefault(m => q == m).Tag;
            //};

            var mst = g.MinimumSpanningTreePrim(e => e.Tag).ToList();

            foreach (var v in mst)
            {
                Console.WriteLine(v);
            }
        }
コード例 #3
0
        public void Equals()
        {
            var tag1  = new TestObject(1);
            var tag2  = new TestObject(2);
            var edge1 = new TaggedUndirectedEdge <int, TestObject>(1, 2, tag1);
            var edge2 = new TaggedUndirectedEdge <int, TestObject>(1, 2, tag1);
            var edge3 = new TaggedUndirectedEdge <int, TestObject>(1, 2, tag2);
            var edge4 = new TaggedUndirectedEdge <int, TestObject>(1, 2, null);

            Assert.AreEqual(edge1, edge1);

            Assert.AreNotEqual(edge1, edge2);
            Assert.AreNotEqual(edge2, edge1);
            Assert.IsFalse(edge1.Equals(edge2));
            Assert.IsFalse(edge2.Equals(edge1));

            Assert.AreNotEqual(edge1, edge3);
            Assert.AreNotEqual(edge3, edge1);
            Assert.IsFalse(edge1.Equals(edge3));
            Assert.IsFalse(edge3.Equals(edge1));

            Assert.AreNotEqual(edge1, edge4);
            Assert.AreNotEqual(edge4, edge1);
            Assert.IsFalse(edge1.Equals(edge4));
            Assert.IsFalse(edge4.Equals(edge1));

            Assert.AreNotEqual(edge1, null);
            Assert.IsFalse(edge1.Equals(null));
        }
コード例 #4
0
        public ArrayList CurveToPoints()
        {
            ArrayList curveOfEdge = CurveOfEdges;
            ArrayList curveOfPoints;
            Pixel     lastPoint;
            Pixel     firstPoint;

            firstPoint    = null;
            lastPoint     = null;
            curveOfPoints = new ArrayList();
            if (curveOfEdge.Count == 1)
            {
                curveOfPoints.Add(((TaggedUndirectedEdge <Pixel, EdgeTag>)curveOfEdge[0]).Source);
                curveOfPoints.Add(((TaggedUndirectedEdge <Pixel, EdgeTag>)curveOfEdge[0]).Target);
            }
            else
            {
                for (int i = 0; i < curveOfEdge.Count; i++)
                {
                    TaggedUndirectedEdge <Pixel, EdgeTag> ed = (TaggedUndirectedEdge <Pixel, EdgeTag>)curveOfEdge[i];
                    if (i == 0)
                    {
                        firstPoint = ed.Source;
                        lastPoint  = ed.Target;
                    }
                    else
                    {
                        if (i == 1)
                        {
                            if (ed.Source.Equals(firstPoint))
                            {
                                firstPoint = lastPoint;
                                lastPoint  = ed.Source;
                            }
                            else if (ed.Target.Equals(firstPoint))
                            {
                                firstPoint = lastPoint;
                                lastPoint  = ed.Target;
                            }
                            curveOfPoints.Add(firstPoint);
                            curveOfPoints.Add(lastPoint);
                        }
                        if (ed.Source.Equals(lastPoint))
                        {
                            lastPoint = ed.Target;
                        }
                        else
                        {
                            lastPoint = ed.Source;
                        }
                        if (i > 0)
                        {
                            curveOfPoints.Add(lastPoint);
                        }
                    }
                }
            }

            return(curveOfPoints);
        }
コード例 #5
0
        private static void AddNewEdge(Pixel p1, Pixel p2, Color cA, Color cB, UndirectedGraph <Pixel, TaggedUndirectedEdge <Pixel, EdgeTag> > graph)
        {
            TaggedUndirectedEdge <Pixel, EdgeTag> e;
            bool b = !cA.Equals(cB);

            graph.TryGetEdge(p1, p2, out TaggedUndirectedEdge <Pixel, EdgeTag> e1);
            graph.TryGetEdge(p2, p1, out TaggedUndirectedEdge <Pixel, EdgeTag> e2);
            p1.color  = cA;
            p1.colorB = cB;
            p2.color  = cA;
            p2.colorB = cB;

            if (e1 == null && e2 == null && b) // adicionado o "b" para adicionar apenas arestas visiveis ao grafo
            {
                if (p1 < p2)
                {
                    e = new TaggedUndirectedEdge <Pixel, EdgeTag>(p1, p2, new EdgeTag(cA, cB, b));
                    graph.AddEdge(e);
                }
                if (b) //conta valencia apenas se for visivel
                {
                    p1.valence++;
                    p2.valence++;
                }
            }
        }
コード例 #6
0
        public void TagChanged()
        {
            var edge = new TaggedUndirectedEdge <int, TestObject>(1, 2, null);

            int changeCount = 0;

            edge.TagChanged += (sender, args) => ++ changeCount;

            edge.Tag = null;
            Assert.AreEqual(0, changeCount);

            var tag1 = new TestObject(1);

            edge.Tag = tag1;
            Assert.AreEqual(1, changeCount);

            edge.Tag = tag1;
            Assert.AreEqual(1, changeCount);

            var tag2 = new TestObject(2);

            edge.Tag = tag2;
            Assert.AreEqual(2, changeCount);

            edge.Tag = tag1;
            Assert.AreEqual(3, changeCount);
        }
コード例 #7
0
        public void ObjectToString()
        {
            var edge1 = new TaggedUndirectedEdge <int, TestObject>(1, 2, null);
            var edge2 = new TaggedUndirectedEdge <int, TestObject>(1, 2, new TestObject(12));

            Assert.AreEqual("1 <-> 2 (no tag)", edge1.ToString());
            Assert.AreEqual("1 <-> 2 (12)", edge2.ToString());
        }
コード例 #8
0
    private void Update()
    {
        // В позиции 1: если есть "ребро 1" или "ребро 2", удалем их из графа
        //
        if (slider.value == 1)
        {
            if (AirSystem.graphAir.ContainsEdge(input.myVertexName, output1.myVertexName) || AirSystem.graphAir.ContainsEdge(input.myVertexName, output2.myVertexName))
            {
                if (AirSystem.graphAir.TryGetEdge(input.myVertexName, output1.myVertexName, out var e1))
                {
                    TurnOffGraphWalking(output1.myVertexName);
                    AirSystem.graphAir.RemoveEdge(e1);
                }


                if (AirSystem.graphAir.TryGetEdge(input.myVertexName, output2.myVertexName, out var e2))
                {
                    TurnOffGraphWalking(output2.myVertexName);
                    AirSystem.graphAir.RemoveEdge(e2);
                }
            }
        }

        // В позиции 0: Если есть "ребро 2" или нет "ребра 1", удаляем "ребро 2" и добавляем "ребро 1" в граф
        //
        if (slider.value == 0)
        {
            if (AirSystem.graphAir.ContainsEdge(input.myVertexName, output2.myVertexName) || !AirSystem.graphAir.ContainsEdge(input.myVertexName, output1.myVertexName))
            {
                var e1 = new TaggedUndirectedEdge <string, string>(input.myVertexName, output1.myVertexName, "DistributorEdge1");

                AirSystem.graphAir.AddEdge(e1);

                TurnOffGraphWalking(output2.myVertexName);

                AirSystem.graphAir.TryGetEdge(input.myVertexName, output2.myVertexName, out var e2);
                AirSystem.graphAir.RemoveEdge(e2);
            }
        }

        // В позиции 2: Если есть "ребро 1" или нет "ребра 2", удаляем "ребро 1" и добавляем "ребро 2" в граф
        //
        if (slider.value == 2)
        {
            if (AirSystem.graphAir.ContainsEdge(input.myVertexName, output1.myVertexName) || !AirSystem.graphAir.ContainsEdge(input.myVertexName, output2.myVertexName))
            {
                var e2 = new TaggedUndirectedEdge <string, string>(input.myVertexName, output2.myVertexName, "DistributorEdge2");

                AirSystem.graphAir.AddEdge(e2);

                TurnOffGraphWalking(output1.myVertexName);

                AirSystem.graphAir.TryGetEdge(input.myVertexName, output1.myVertexName, out var e1);
                AirSystem.graphAir.RemoveEdge(e1);
            }
        }
    }
コード例 #9
0
    public void AddEdge(CreateVertex v1, CreateVertex v2)
    {
        vertex1     = v1;
        vertex2     = v2;
        isConnected = true;

        var edge = new TaggedUndirectedEdge <string, string>(v1.myVertexName, v2.myVertexName, "CableEdge");

        AirSystem.graphAir.AddEdge(edge);
    }
コード例 #10
0
        private double AngleBetween2Lines(TaggedUndirectedEdge <Pixel, bool> edge1, TaggedUndirectedEdge <Pixel, bool> edge2)
        {
            double angle1 = Math.Atan2(edge1.Source.y - edge1.Target.y
                                       , edge1.Source.x - edge1.Target.x);

            double angle2 = Math.Atan2(edge2.Source.y - edge2.Target.y
                                       , edge2.Source.x - edge2.Target.x);

            return(angle1 - angle2);
        }
コード例 #11
0
        public double curveSize()
        {
            TaggedUndirectedEdge <Pixel, EdgeTag> edge = null;
            double lenght = 0;

            foreach (var e in curve)
            {
                edge = (TaggedUndirectedEdge <Pixel, EdgeTag>)e;
                Point p1 = edge.Source.getPoint();
                Point p2 = edge.Target.getPoint();
                lenght += (Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)));
            }
            return(lenght);
        }
コード例 #12
0
        public void Equals()
        {
            var tag1  = new TestObject(1);
            var tag2  = new TestObject(2);
            var edge1 = new TaggedUndirectedEdge <int, TestObject>(1, 2, tag1);
            var edge2 = new TaggedUndirectedEdge <int, TestObject>(1, 2, tag1);
            var edge3 = new TaggedUndirectedEdge <int, TestObject>(2, 1, tag1);
            var edge4 = new TaggedUndirectedEdge <int, TestObject>(1, 2, tag2);
            var edge5 = new TaggedUndirectedEdge <int, TestObject>(1, 2, null);

            Assert.AreEqual(edge1, edge1);
            Assert.AreNotEqual(edge1, edge2);
            Assert.AreNotEqual(edge1, edge3);
            Assert.AreNotEqual(edge1, edge4);
            Assert.AreNotEqual(edge1, edge5);

            Assert.AreNotEqual(edge1, null);
        }
コード例 #13
0
ファイル: FittingObject.cs プロジェクト: dnv0/PneumaticsApp
 private void Update()
 {
     if (!input || !output1 || !output2)
     {
         input   = transform.Find("Input").GetComponent <CreateVertex>();
         output1 = transform.Find("Output 1").GetComponent <CreateVertex>();
         output2 = transform.Find("Output 2").GetComponent <CreateVertex>();
     }
     else
     {
         if (!AirSystem.graphAir.ContainsEdge(input.myVertexName, output1.myVertexName) || !AirSystem.graphAir.ContainsEdge(input.myVertexName, output2.myVertexName))
         {
             var edge1 = new TaggedUndirectedEdge <string, string>(input.myVertexName, output1.myVertexName, "FittingEdge1");
             var edge2 = new TaggedUndirectedEdge <string, string>(input.myVertexName, output2.myVertexName, "FittingEdge2");
             AirSystem.graphAir.AddEdge(edge1);
             AirSystem.graphAir.AddEdge(edge2);
         }
     }
 }
コード例 #14
0
        private int CurveSize(TaggedUndirectedEdge <Pixel, EdgeTag> edge)
        {
            int  size    = 0;
            bool hasEdge = true;
            TaggedUndirectedEdge <Pixel, EdgeTag> nextEdge1 = edge; //a primeira direção do vertice
            TaggedUndirectedEdge <Pixel, EdgeTag> nextEdge2 = edge; //a segunda direção do vertice

            if (edge.Source.valence == 2 || edge.Target.valence == 2)
            {
                size++;
                while (hasEdge)
                {
                    hasEdge = false;
                    if (nextEdge1.Source.valence == 2 && !nextEdge1.Source.Equals(edge.Source))
                    {
                        size++;
                        foreach (var e in g.AdjacentEdges(nextEdge1.Source))
                        {
                            if (!e.Equals(nextEdge1))
                            {
                                nextEdge1 = e;
                                hasEdge   = true;
                            }
                        }
                    }

                    if (nextEdge2.Source.valence == 2 && !nextEdge1.Source.Equals(edge.Source))
                    {
                        size++;
                        foreach (var e in g.AdjacentEdges(nextEdge2.Source))
                        {
                            if (!e.Equals(nextEdge2))
                            {
                                nextEdge2 = e;
                                hasEdge   = true;
                            }
                        }
                    }
                }
            }
            return(size);
        }
コード例 #15
0
        private Pixel GetLastPixel(ArrayList curve)
        {
            TaggedUndirectedEdge <Pixel, EdgeTag> lastEdge = (TaggedUndirectedEdge <Pixel, EdgeTag>)curve[curve.Count - 1];

            if (curve.Count < 2)
            {
                return(lastEdge.Target);
            }
            else
            {
                TaggedUndirectedEdge <Pixel, EdgeTag> beforeLastEdge = (TaggedUndirectedEdge <Pixel, EdgeTag>)curve[curve.Count - 2];
                if (lastEdge.Source.Equals(beforeLastEdge.Target) || lastEdge.Source.Equals(beforeLastEdge.Source))
                {
                    return(lastEdge.Target);
                }
                else
                {
                    return(lastEdge.Source);
                }
            }
        }
コード例 #16
0
        private Pixel GetFirstPixel(ArrayList curve)
        {
            TaggedUndirectedEdge <Pixel, EdgeTag> firstEdge = (TaggedUndirectedEdge <Pixel, EdgeTag>)curve[0];

            if (curve.Count < 2)
            {
                return(firstEdge.Source);
            }
            else
            {
                TaggedUndirectedEdge <Pixel, EdgeTag> secondEdge = (TaggedUndirectedEdge <Pixel, EdgeTag>)curve[1];
                if (firstEdge.Source.Equals(secondEdge.Target) || firstEdge.Source.Equals(secondEdge.Source))
                {
                    return(firstEdge.Target);
                }
                else
                {
                    return(firstEdge.Source);
                }
            }
        }
コード例 #17
0
        private static void AddNewEdge(Pixel p1, Pixel p2, Color cA, Color cB, UndirectedGraph<Pixel, TaggedUndirectedEdge<Pixel, EdgeTag>> graph)
        {
            TaggedUndirectedEdge<Pixel, EdgeTag> e;
            TaggedUndirectedEdge<Pixel, EdgeTag> e1;
            TaggedUndirectedEdge<Pixel, EdgeTag> e2;
            bool b = !cA.Equals(cB);

            graph.TryGetEdge(p1, p2, out e1);
            graph.TryGetEdge(p2, p1, out e2);
            p1.color = cA;
            p1.colorB = cB;
            p2.color = cA;
            p2.colorB = cB;

            if (e1 == null && e2 == null && b) // adicionado o "b" para adicionar apenas arestas visiveis ao grafo
            {
                e = new TaggedUndirectedEdge<Pixel, EdgeTag>(p1, p2, new EdgeTag(cA, cB, b));
                graph.AddEdge(e);
                if (b) //conta valencia apenas se for visivel
                {
                    p1.valence++;
                    p2.valence++;
                }
            }
        }
コード例 #18
0
        private double AngleBetween2Lines(TaggedUndirectedEdge<Pixel, bool> edge1, TaggedUndirectedEdge<Pixel, bool> edge2)
        {
            double angle1 = Math.Atan2(edge1.Source.y - edge1.Target.y
                                       , edge1.Source.x - edge1.Target.x);

            double angle2 = Math.Atan2(edge2.Source.y - edge2.Target.y
                            , edge2.Source.x - edge2.Target.x);

            return angle1 - angle2;
        }
コード例 #19
0
        private int CurveSize(TaggedUndirectedEdge<Pixel, EdgeTag> edge)
        {
            int size = 0;
            bool hasEdge = true;
            TaggedUndirectedEdge<Pixel, EdgeTag> nextEdge1 = edge; //a primeira direção do vertice
            TaggedUndirectedEdge<Pixel, EdgeTag> nextEdge2 = edge; //a segunda direção do vertice
            if (edge.Source.valence == 2 || edge.Target.valence == 2)
            {
                size++;
                while (hasEdge)
                {

                    hasEdge = false;
                    if (nextEdge1.Source.valence == 2 && !nextEdge1.Source.Equals(edge.Source))
                    {
                        size++;
                        foreach (var e in g.AdjacentEdges(nextEdge1.Source))
                        {
                            if (!e.Equals(nextEdge1))
                            {
                                nextEdge1 = e;
                                hasEdge = true;
                            }
                        }
                    }

                    if (nextEdge2.Source.valence == 2 && !nextEdge1.Source.Equals(edge.Source))
                    {
                        size++;
                        foreach (var e in g.AdjacentEdges(nextEdge2.Source))
                        {
                            if (!e.Equals(nextEdge2))
                            {
                                nextEdge2 = e;
                                hasEdge = true;
                            }
                        }
                    }
                }
            }
            return size;
        }
コード例 #20
0
        /*
         * Examinar blocos de 2x2 nodos para eliminar a maior quantidade de arestas, e remover cruzamentos ambiguos
         * 00 10
         * 01 11
         */
        internal String SolveAmbiguities()
        {
            for (int y = 0; y < Height - 1; y++)
            {
                for (int x = 0; x < Width - 1; x++)
                {
                    Pixel p00 = VertexSearch(x, y, g);
                    Pixel p10 = VertexSearch(x + 1, y, g);
                    Pixel p01 = VertexSearch(x, y + 1, g);
                    Pixel p11 = VertexSearch(x + 1, y + 1, g);
                    //Console.WriteLine("P= " + x + " " + y + " Valence =" + p00.valence);
                    TaggedUndirectedEdge <Pixel, EdgeTag> edge1 = null;
                    TaggedUndirectedEdge <Pixel, EdgeTag> edge2 = null;
                    g.TryGetEdge(p11, p00, out edge1);
                    g.TryGetEdge(p01, p10, out edge2);
                    if (edge1 != null)
                    {
                        if (edge2 != null)
                        {
                            //Identificada uma ambiguidade
                            if (Pixel.colorIsSimilar(p00.color, p01.color))
                            {
                                //Se as 4 cores são iguais remove todas as arestas internas
                                if (edge1 != null)
                                {
                                    g.RemoveEdge(edge1);
                                }
                                if (edge2 != null)
                                {
                                    g.RemoveEdge(edge2);
                                }
                            }

                            //Heuristica da ilha
                            else if (edge1.Source.valence == 1 || edge1.Target.valence == 1)
                            {
                                if (edge2 != null)
                                {
                                    g.RemoveEdge(edge2);
                                }
                            }
                            else if (edge2.Source.valence == 1 || edge2.Target.valence == 1)
                            {
                                if (edge1 != null)
                                {
                                    g.RemoveEdge(edge1);
                                }
                            }
                            else
                            {
                                //Heuristica da curva
                                if (edge1.Source.valence == 2 || edge1.Target.valence == 2 || edge2.Source.valence == 2 || edge2.Target.valence == 2)
                                {
                                    if (CurveSize(edge1) <= CurveSize(edge2))
                                    {
                                        if (edge1 != null)
                                        {
                                            g.RemoveEdge(edge1);
                                        }
                                    }
                                    else
                                    {
                                        if (edge2 != null)
                                        {
                                            g.RemoveEdge(edge2);
                                        }
                                    }
                                }
                                else
                                //Heuristica dos pixels sobrepostos
                                {
                                    Pixel p1 = VertexSearch(edge1.Source.x, edge1.Source.y, g);
                                    Pixel p2 = VertexSearch(edge2.Source.x, edge2.Source.y, g);
                                    //Pixel p3 = VertexSearch(edge1.Target.x, edge1.Target.y, g);
                                    //Pixel p4 = VertexSearch(edge2.Target.x, edge2.Target.y, g);
                                    Color c1 = p1.color;
                                    Color c2 = p2.color;
                                    int   sumC1 = 0, sumC2 = 0;
                                    //Inicializa x e y com -4 posicoes para verificar 3 pixeis em ambas direções
                                    int xs = p1.x - 4;
                                    int ys = p1.y - 4;

                                    while (xs <= p1.x + 3)
                                    {
                                        while (ys <= p1.y + 3)
                                        {
                                            Pixel pixel = VertexSearch(x, y, g);
                                            if (pixel.color == c1)
                                            {
                                                sumC1++;
                                            }
                                            else
                                            if (pixel.color == c2)
                                            {
                                                sumC2++;
                                            }
                                            ys++;
                                        }
                                        xs++;
                                    }
                                    //A cor em maior quantidade representa o fundo, e deve se manter os detalhes conectados
                                    if (sumC1 > sumC2) //Remove a cor em menor quantidade
                                    {
                                        g.RemoveEdge(edge1);
                                    }
                                    else
                                    {
                                        g.RemoveEdge(edge2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            SvgVector svg = new SvgVector();

            svg.Height = Height;
            svg.Width  = Width;
            //svg.DrawValence = true;
            return(svg.toImageSVG(g, "AmbiguitiesSolved.svg"));
        }
コード例 #21
0
        /*
         * p11 p12 p13
         * p21  p  p23
         * p31 p32 p33
         */
        internal String ImageToGraph(Bitmap b)
        {
            // FastBitmap fb = new FastBitmap(b);
            Bitmap fb = b;

            Height = b.Height;
            Width  = b.Width;


            //Le imagem orignal e adiciona vertex - nodos
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Pixel p = new Pixel(x, y, fb.GetPixel(x, y));
                    g.AddVertex(p);
                }
            }

            //Le imagem orignal e adiciona edges - arestas
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Pixel p = VertexSearch(x, y, g);

                    Pixel p11 = VertexSearch(x - 1, y - 1, g);
                    if (p11 != null && Pixel.colorIsSimilar(p11.color, p.color))
                    {
                        var e1 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p11, new EdgeTag());
                        g.AddEdge(e1);
                        p.valence++;
                        p11.valence++;
                    }

                    Pixel p12 = VertexSearch(x, y - 1, g);
                    if (p12 != null && Pixel.colorIsSimilar(p12.color, p.color))
                    {
                        var e2 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p12, new EdgeTag());
                        g.AddEdge(e2);
                        p.valence++;
                        p12.valence++;
                    }

                    Pixel p13 = VertexSearch(x + 1, y - 1, g);
                    if (p13 != null && Pixel.colorIsSimilar(p13.color, p.color))
                    {
                        if (p <= p13)
                        {
                            var e3 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p13, new EdgeTag());
                            g.AddEdge(e3);
                        }
                        else
                        {
                            var e3 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p13, p, new EdgeTag());
                            g.AddEdge(e3);
                        }
                        p.valence++;
                        p13.valence++;
                    }

                    Pixel p21 = VertexSearch(x - 1, y, g);
                    if (p21 != null && Pixel.colorIsSimilar(p21.color, p.color))
                    {
                        var e4 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p21, new EdgeTag());
                        g.AddEdge(e4);
                        p.valence++;
                        p21.valence++;
                    }
                }
            }

            SvgVector svg = new SvgVector
            {
                Height = Height,
                Width  = Width
            };

            return(svg.ToImageSVG(g, "Graph.svg"));
        }
コード例 #22
0
        /*
         * Detectar os ciclos dentro do grafo para transforma-los em objetos coloridos
         */
        internal String CreateShapes()
        {
            Shapes    shapes          = new Shapes();
            Shape     shape           = new Shape();
            ArrayList processedCurves = new ArrayList();

            Pixel lastPixel;
            Pixel firstPixel;
            Color color;
            bool  hasCurve;

            foreach (Curve curve in curvesC)
            {
                Curve processingCurve = curve;
                color = processingCurve.Color;
                if (!processedCurves.Contains(processingCurve))
                {
                    processedCurves.Add(processingCurve);
                    ArrayList curveArray = processingCurve.CurveOfEdges;

                    firstPixel = GetFirstPixel(curveArray);
                    lastPixel  = GetLastPixel(curveArray);
                    shape.Add(curve);
                    if (!firstPixel.Equals(lastPixel)) // se primeiro e ultimo são iguais, então o circuito já esta fechado
                    {
                        hasCurve = true;
                        while (hasCurve)
                        {
                            foreach (Curve newCurve in curvesC)                                                            // verifico todas as curvas procurando uma que comece ou termine com o ultimo pixel da pesquisada
                            {
                                if ((newCurve.Color == color && newCurve != curve && !processedCurves.Contains(newCurve))) // Precisa ser uma curva não processessada e com cor igual
                                {
                                    hasCurve = false;
                                    ArrayList newCurveArray = newCurve.CurveOfEdges;
                                    TaggedUndirectedEdge <Pixel, EdgeTag> firstNewEdge = (TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0];
                                    TaggedUndirectedEdge <Pixel, EdgeTag> lastNewEdge  = (TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[newCurveArray.Count - 1];

                                    if (firstNewEdge.Source.Equals(lastPixel) || firstNewEdge.Target.Equals(lastPixel) || lastNewEdge.Target.Equals(lastPixel) || lastNewEdge.Source.Equals(lastPixel)) // se a curva analizada possui o primeiro ou ultimo pixel igual a um pixel do lastEdge // simplificado apenas para conter um dos pixeis da ultima aresta da curva
                                    {
                                        if (firstNewEdge.Source.Equals(lastPixel) || firstNewEdge.Target.Equals(lastPixel))
                                        {
                                            if (newCurveArray.Count == 1)
                                            {
                                                if (((TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0]).Source.Equals(lastPixel))
                                                {
                                                    lastPixel = ((TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0]).Target;
                                                }
                                                else
                                                {
                                                    lastPixel = ((TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0]).Source;
                                                }
                                            }
                                            else
                                            {
                                                lastPixel = GetLastPixel(newCurveArray);
                                            }
                                        }
                                        else
                                        {
                                            if (newCurveArray.Count == 1)
                                            {
                                                if (((TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0]).Source.Equals(lastPixel))
                                                {
                                                    lastPixel = ((TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0]).Target;
                                                }
                                                else
                                                {
                                                    lastPixel = ((TaggedUndirectedEdge <Pixel, EdgeTag>)newCurveArray[0]).Source;
                                                }
                                            }
                                            else
                                            {
                                                lastPixel = GetFirstPixel(newCurveArray);
                                                newCurveArray.Reverse();
                                            }
                                        }


                                        processedCurves.Add(newCurve);
                                        if (!lastPixel.Equals(firstPixel))
                                        {
                                            shape.Add(newCurve);
                                            hasCurve = true;
                                            break;
                                        }
                                        else // terminou o grupo de curvas
                                        {
                                            shape.Add(newCurve);
                                            shapes.Add(shape);
                                            hasCurve = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        shapes.Add(shape);
                    }
                    shape = new Shape();
                }
            }

            //remove shapes iguais de cores diferentes
            ArrayList remove = new ArrayList();

            foreach (Shape shapeItem in shapes)
            {
                foreach (Shape shapeItem2 in shapes)
                {
                    if (!remove.Contains(shapeItem) && !remove.Contains(shapeItem2) && shapeItem.GetColor() != Color.Beige && shapeItem2.GetColor() != Color.Beige)
                    {
                        if (shapeItem.GetColor() != shapeItem2.GetColor() && shapeItem.IsSameShape(shapeItem2))
                        {
                            Color c = new Color();
                            //localiza o pixel de cor dentro do poligono
                            foreach (Pixel v in g.Vertices)
                            {
                                if (v.pixelInsidePolygon(shapeItem.ToPoints()))
                                {
                                    c = v.color;
                                    break;
                                }
                            }

                            if (shapeItem.GetColor() == c)
                            {
                                remove.Add(shapeItem2);
                                break;
                            }
                            else
                            if (shapeItem2.GetColor() == c)
                            {
                                remove.Add(shapeItem);
                                break;
                            }
                        }
                    }
                }
            }


            foreach (Shape item in remove)
            {
                shapes.Remove(item);
            }

            //Ordena do objeto com maior area para os de menor area
            shapes.Sort();

            SvgVector svg = new SvgVector
            {
                Height = Height * scale,
                Width  = Width * scale,
                scale  = 1
            };

            return(svg.NewImage(g, shapes, "NewImage.svg"));
        }
コード例 #23
0
        /*
         * p11 p12 p13
         * p21  p  p23
         * p31 p32 p33
         */
        internal String ImageToGraph(Bitmap b)
        {
            FastBitmap fb = new FastBitmap(b);
            Height = b.Height;
            Width = b.Width;

            //Le imagem orignal e adiciona vertex - nodos
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Pixel p = new Pixel(x, y, fb.GetPixel(x, y));
                    g.AddVertex(p);

                }
            }

            //Le imagem orignal e adiciona edges - arestas
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    Pixel p = VertexSearch(x, y, g);

                    Pixel p11 = VertexSearch(x - 1, y - 1, g);
                    if (p11 != null && Pixel.colorIsSimilar(p11.color, p.color))
                    {
                        var e1 = new TaggedUndirectedEdge<Pixel, EdgeTag>(p, p11, new EdgeTag());
                        g.AddEdge(e1);
                        p.valence++;
                        p11.valence++;
                    }

                    Pixel p12 = VertexSearch(x, y - 1, g);
                    if (p12 != null && Pixel.colorIsSimilar(p12.color, p.color))
                    {
                        var e2 = new TaggedUndirectedEdge<Pixel, EdgeTag>(p, p12, new EdgeTag());
                        g.AddEdge(e2);
                        p.valence++;
                        p12.valence++;
                    }

                    Pixel p13 = VertexSearch(x + 1, y - 1, g);
                    if (p13 != null && Pixel.colorIsSimilar(p13.color, p.color))
                    {
                        var e3 = new TaggedUndirectedEdge<Pixel, EdgeTag>(p, p13, new EdgeTag());
                        g.AddEdge(e3);
                        p.valence++;
                        p13.valence++;
                    }

                    Pixel p21 = VertexSearch(x - 1, y, g);
                    if (p21 != null && Pixel.colorIsSimilar(p21.color, p.color))
                    {
                        var e4 = new TaggedUndirectedEdge<Pixel, EdgeTag>(p, p21, new EdgeTag());
                        g.AddEdge(e4);
                        p.valence++;
                        p21.valence++;
                    }
                }
            }

            SvgVector svg = new SvgVector();
            svg.Height = Height;
            svg.Width = Width;

            return svg.toImageSVG(g, "Graph.svg");
        }