Example #1
0
        private void initNodesAndConnections()
        {
            Random rand;

            _nodeMap = new Dictionary <DiagramNode, ForceItem>();

            //Use a pre-defined seed so that the "random" numbers are always the same.
            //This will ensure an even distribution, yet the layout will not change
            //each time it is rendered.
            rand = new Random(42346234);

            //Add the nodes
            foreach (DiagramNode currDiagramNode in _nodes)
            {
                ForceItem forceNode;

                forceNode          = new ForceItem();
                forceNode.Location = new float[] { rand.Next(500), rand.Next(500) };
                forceNode.Mass     = (float)1.0;

                //Add the node to our map so we can look up the force item by node
                _nodeMap.Add(currDiagramNode, forceNode);

                _simulator.addItem(forceNode);
            }

            //Add the springs/connections
            foreach (DiagramNodeConnection currConnection in _connections)
            {
                _simulator.addSpring(_nodeMap[currConnection.FirstNode], _nodeMap[currConnection.SecondNode]);
            }
        }
        /// <summary>
        /// Loads the simulator with all relevant force items and springs.
        /// </summary>
        /// <param name="fsim"> the force simulator driving this layout.</param>
        protected void InitializeSimulator(ForceSimulator fsim)
        {
            //TODO: some checks here...?

            float startX = (referrer == null ? 0f : (float)referrer.X);
            float startY = (referrer == null ? 0f : (float)referrer.Y);

            startX = float.IsNaN(startX) ? 0f : startX;
            startY = float.IsNaN(startY) ? 0f : startY;
            if (Nodes != null && Nodes.Count > 0)
            {
                foreach (INode item in Nodes)
                {
                    ForceItem fitem = Pars[item.Uid.ToString()];
                    fitem.Mass = getMassValue(item);
                    double x = item.X;
                    double y = item.Y;
                    fitem.Location[0] = (Double.IsNaN(x) ? startX : (float)x);
                    fitem.Location[1] = (Double.IsNaN(y) ? startY : (float)y);
                    fsim.addItem(fitem);
                }
            }
            if (Edges != null && Edges.Count > 0)
            {
                foreach (IEdge e in Edges)
                {
                    INode n1 = e.SourceNode;
                    if (n1 == null)
                    {
                        continue;
                    }
                    ForceItem f1 = Pars[n1.Uid.ToString()];
                    INode     n2 = e.TargetNode;
                    if (n2 == null)
                    {
                        continue;
                    }
                    ForceItem f2    = Pars[n2.Uid.ToString()];
                    float     coeff = getSpringCoefficient(e);
                    float     slen  = getSpringLength(e);
                    fsim.addSpring(f1, f2, (coeff >= 0 ? coeff : -1.0F), (slen >= 0 ? slen : -1.0F));
                }
            }
        }
        /// <summary>
        /// Loads the simulator with all relevant force items and springs.
        /// </summary>
        /// <param name="fsim"> the force simulator driving this layout.</param>
        protected void InitializeSimulator(ForceSimulator fsim)
        {
            //TODO: some checks here...?

            float startX = (referrer == null ? 0f : (float)referrer.X);
            float startY = (referrer == null ? 0f : (float)referrer.Y);

            startX = float.IsNaN(startX) ? 0f : startX;
            startY = float.IsNaN(startY) ? 0f : startY;
            foreach (node item in graph.nodes)
            {
                ForceItem fitem = Pars[item.name];
                fitem.Mass = getMassValue(item);
                double x = item.X;
                double y = item.Y;
                fitem.Location[0] = (Double.IsNaN(x) ? startX : (float)x);
                fitem.Location[1] = (Double.IsNaN(y) ? startY : (float)y);
                fsim.addItem(fitem);
            }
            foreach (arc e in graph.arcs)
            {
                node n1 = e.From;
                if (n1 == null)
                {
                    continue;
                }
                ForceItem f1 = Pars[n1.name];
                node      n2 = e.To;
                if (n2 == null)
                {
                    continue;
                }
                ForceItem f2    = Pars[n2.name];
                float     coeff = getSpringCoefficient(e);
                float     slen  = getSpringLength(e);
                fsim.addSpring(f1, f2, (coeff >= 0 ? coeff : -1.0F), (slen >= 0 ? slen : -1.0F));
            }
        }