/// <summary> /// Updates the node positions. /// </summary> private void UpdateNodePositions() { double x1 = 0, x2 = 0, y1 = 0, y2 = 0; if (Bounds != null) { x1 = Bounds.X; y1 = Bounds.Top; x2 = Bounds.Right; y2 = Bounds.Bottom; } // update positions foreach (INode item in Nodes) { ForceItem fitem = Pars[item.Uid.ToString()]; if (item.IsFixed) { // clear any force computations fitem.Force[0] = 0.0f; fitem.Force[1] = 0.0f; fitem.Velocity[0] = 0.0f; fitem.Velocity[1] = 0.0f; if (Double.IsNaN(item.X)) { setX(item, referrer, 0.0D); setY(item, referrer, 0.0D); } continue; } double x = fitem.Location[0]; double y = fitem.Location[1]; //do we need to check the bounding constraints if (mEnforceBounds && Bounds != null) { double hw = item.Rectangle.Width / 2; double hh = item.Rectangle.Height / 2; if (x + hw > x2) { x = x2 - hw; } if (x - hw < x1) { x = x1 + hw; } if (y + hh > y2) { y = y2 - hh; } if (y - hh < y1) { y = y1 + hh; } } // set the actual position setX(item, referrer, x); setY(item, referrer, y); } }
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> /// Reset the force simulation state for all nodes processed by this layout. ///</summary> public void Reset() { foreach (node item in graph.nodes) { ForceItem fitem = Pars[item.name]; if (fitem != null) { fitem.Location[0] = (float)item.X; fitem.Location[1] = (float)item.Y; fitem.Force[0] = fitem.Force[1] = 0; fitem.Velocity[0] = fitem.Velocity[1] = 0; } } }
///<summary> /// Reset the force simulation state for all nodes processed by this layout. ///</summary> public void Reset() { foreach (INode item in Nodes) { ForceItem fitem = Pars[item.Uid.ToString()]; if (fitem != null) { fitem.Location[0] = (float)item.X; fitem.Location[1] = (float)item.Y; fitem.Force[0] = fitem.Force[1] = 0; fitem.Velocity[0] = fitem.Velocity[1] = 0; } } m_lasttime = -1L; }
/// <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> /// Updates the node positions. /// </summary> private void UpdateNodePositions() { // update node positions for (int i = 0; i < graph.nodes.Count; i++) { node item = graph.nodes[i]; ForceItem fitem = Pars[item.name.ToString()]; fitem.Force[0] = 0.0f; fitem.Force[1] = 0.0f; fitem.Velocity[0] = 0.0f; fitem.Velocity[1] = 0.0f; if (Double.IsNaN(item.X)) { graph.nodes[i].X = 0.0f; graph.nodes[i].Y = 0.0f; } graph.nodes[i].X = fitem.Location[0] * (Spacing / 50); graph.nodes[i].Y = fitem.Location[1] * (Spacing / 50); } }
/// <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)); } }