예제 #1
0
        private void SyncGraph()
        {
            fEdges.Clear();
            fNodes.Clear();

            var vertices = fGraph.Vertices;

            for (int i = 0, verticesCount = vertices.Count; i < verticesCount; i++)
            {
                Vertex vertex = vertices[i];

                ArborNode node = CreateNode(vertex.Sign);
                node.Pt = NewRandomPoint();

                vertex.Extensions.Add(node);
                fNodes.Add(node);
            }

            var edges = fGraph.Edges;

            for (int i = 0, edgesCount = edges.Count; i < edgesCount; i++)
            {
                Edge edge = edges[i];

                ArborNode anSrc = edge.Source.Extensions.Find <ArborNode>();
                ArborNode anTgt = edge.Target.Extensions.Find <ArborNode>();

                ArborEdge arbEdge = CreateEdge(anSrc, anTgt, 1.0f, fStiffness);
                edge.Extensions.Add(arbEdge);
                fEdges.Add(arbEdge);
            }
        }
예제 #2
0
        public ArborEdge AddEdge(string sourceSign, string targetSign, double length = 1.0f)
        {
            ArborNode src = GetNode(sourceSign);

            src = (src != null) ? src : AddNode(sourceSign);

            ArborNode tgt = GetNode(targetSign);

            tgt = (tgt != null) ? tgt : AddNode(targetSign);

            ArborEdge result = null;

            if (src != null && tgt != null)
            {
                for (int i = 0, edgesCount = fEdges.Count; i < edgesCount; i++)
                {
                    ArborEdge edge = fEdges[i];
                    if (edge.Source == src && edge.Target == tgt)
                    {
                        result = edge;
                        break;
                    }
                }
            }

            if (result == null)
            {
                result = CreateEdge(src, tgt, length, fStiffness);
                fEdges.Add(result);
            }

            return(result);
        }
예제 #3
0
        public void Test_Common()
        {
            var node1 = new ArborNode("x1");
            var node2 = new ArborNode("x2");

            var edge = new ArborEdge(node1, node2, 11.0f, 22.0f, true);

            Assert.IsNotNull(edge);

            Assert.Throws(typeof(ArgumentNullException), () => { new ArborEdge(null, node2, 11.0f, 22.0f, true); });
            Assert.Throws(typeof(ArgumentNullException), () => { new ArborEdge(node1, null, 11.0f, 22.0f, true); });

            var vertex1       = new Vertex();
            var vertex2       = new Vertex();
            var extensibleObj = new Edge(vertex1, vertex2, 1, null);

            edge.Attach(extensibleObj);
            edge.Detach(extensibleObj);

            Assert.AreEqual(node1, edge.Source);
            Assert.AreEqual(node2, edge.Target);
            Assert.AreEqual(11.0f, edge.Length);
            Assert.AreEqual(22.0f, edge.Stiffness);
            Assert.AreEqual(true, edge.Directed);
        }
예제 #4
0
        private void ApplySprings()
        {
            for (int i = 0, edgesCount = fEdges.Count; i < edgesCount; i++)
            {
                ArborEdge edge = fEdges[i];

                ArborPoint s = edge.Target.Pt.Sub(edge.Source.Pt);
                ArborPoint r = s.Normalize();
                double     q = edge.Stiffness * (edge.Length - s.Magnitude());

                edge.Source.ApplyForce(r.Mul(q * -0.5f));
                edge.Target.ApplyForce(r.Mul(q * 0.5f));
            }
        }
예제 #5
0
        protected override void OnPaint(PaintEventArgs e)
        {
            try {
                Graphics gfx = e.Graphics;
                gfx.SmoothingMode = SmoothingMode.AntiAlias;

                for (int i = 0, edgesCount = fSystem.Edges.Count; i < edgesCount; i++)
                {
                    ArborEdge edge = fSystem.Edges[i];

                    var sourceNode = (ArborNodeEx)edge.Source;
                    var targetNode = (ArborNodeEx)edge.Target;

                    ArborPoint pt1 = fSystem.GetViewCoords(sourceNode.Pt);
                    ArborPoint pt2 = fSystem.GetViewCoords(targetNode.Pt);

                    ArborPoint tail = IntersectLineBox(pt1, pt2, sourceNode.Box);
                    ArborPoint head = (tail.IsNull()) ? ArborPoint.Null : IntersectLineBox(tail, pt2, targetNode.Box);

                    if (!head.IsNull() && !tail.IsNull())
                    {
                        gfx.DrawLine(fLinePen, (int)tail.X, (int)tail.Y, (int)head.X, (int)head.Y);
                    }
                }

                for (int i = 0, nodesCount = fSystem.Nodes.Count; i < nodesCount; i++)
                {
                    ArborNodeEx node = (ArborNodeEx)fSystem.Nodes[i];

                    node.Box = GetNodeRect(gfx, node);
                    gfx.FillRectangle(new SolidBrush(node.Color), node.Box);
                    gfx.DrawString(node.Sign, fDrawFont, fWhiteBrush, node.Box, fStrFormat);
                }

                if (fEnergyDebug)
                {
                    string energy = "max=" + fSystem.EnergyMax.ToString("0.00000") + ", mean=" + fSystem.EnergyMean.ToString("0.00000");
                    gfx.DrawString(energy, fDrawFont, fBlackBrush, 10, 10);
                }
            } catch (Exception ex) {
                Debug.WriteLine("ArborViewer.OnPaint(): " + ex.Message);
            }
        }