/// <summary> /// Sets the fixed source or target terminal point on the given edge. /// </summary> /// <param name="edge">State whose terminal point should be updated.</param> /// <param name="terminal">State which represents the actual terminal.</param> /// <param name="source">Boolean that specifies if the terminal is the source.</param> /// <param name="constraint">Constraint that specifies the connection.</param> public void UpdateFixedTerminalPoint(mxCellState edge, mxCellState terminal, bool source, mxConnectionConstraint constraint) { mxPoint pt = null; if (constraint != null) { pt = graph.GetConnectionPoint(terminal, constraint); } if (pt == null && terminal == null) { mxPoint orig = edge.Origin; mxGeometry geo = graph.GetCellGeometry(edge.Cell); pt = geo.GetTerminalPoint(source); if (pt != null) { pt = new mxPoint(scale * (translate.X + pt.X + orig.X), scale * (translate.Y + pt.Y + orig.Y)); } } edge.SetAbsoluteTerminalPoint(pt, source); }
/// <summary> /// Validates the given cell state. /// </summary> public void UpdateEdgeState(mxCellState state, mxGeometry geo, mxCellState source, mxCellState target) { // This will remove edges with no terminals and no terminal points // as such edges are invalid and produce NPEs in the edge styles. // Also removes connected edges that have no visible terminals. if ((graph.Model.GetTerminal(state.Cell, true) != null && source == null) || (source == null && geo.GetTerminalPoint(true) == null) || (graph.Model.GetTerminal(state.Cell, false) != null && target == null) || (target == null && geo.GetTerminalPoint(false) == null)) { RemoveState(state.Cell, true); } else { UpdateFixedTerminalPoints(state, source, target); UpdatePoints(state, geo.Points, source, target); UpdateFloatingTerminalPoints(state, source, target); if (state.AbsolutePointCount() < 2 || state.AbsolutePoints[0] == null || state .AbsolutePoints[state.AbsolutePointCount() - 1] == null) { // This will remove edges with invalid points from the list of states in the view. // Happens if the one of the terminals and the corresponding terminal point is null. RemoveState(state.Cell, true); } else { UpdateEdgeBounds(state); state.AbsoluteOffset = GetPoint(state, geo); } } }
/// <summary> /// Executes the fast organic layout. /// </summary> /// <param name="parent"></param> public void execute(Object parent) { mxIGraphModel model = graph.Model; // Finds the relevant vertices for the layout int childCount = model.GetChildCount(parent); List <Object> tmp = new List <Object>(childCount); for (int i = 0; i < childCount; i++) { Object child = model.GetChildAt(parent, i); if (!IsCellIgnored(child)) { tmp.Add(child); } } vertexArray = tmp.ToArray(); int n = vertexArray.Length; dispX = new double[n]; dispY = new double[n]; cellLocation = new double[n][]; isMoveable = new bool[n]; neighbours = new int[n][]; radius = new double[n]; radiusSquared = new double[n]; minDistanceLimitSquared = minDistanceLimit * minDistanceLimit; if (forceConstant < 0.001) { forceConstant = 0.001; } forceConstantSquared = forceConstant * forceConstant; // Create a map of vertices first. This is required for the array of // arrays called neighbours which holds, for each vertex, a list of // ints which represents the neighbours cells to that vertex as // the indices into vertexArray for (int i = 0; i < vertexArray.Length; i++) { Object vertex = vertexArray[i]; cellLocation[i] = new double[2]; // Set up the mapping from array indices to cells indices[vertex] = i; mxGeometry bounds = model.GetGeometry(vertex); // Set the X,Y value of the internal version of the cell to // the center point of the vertex for better positioning double width = bounds.Width; double height = bounds.Height; // Randomize (0, 0) locations double x = bounds.X; double y = bounds.Y; cellLocation[i][0] = x + width / 2.0; cellLocation[i][1] = y + height / 2.0; radius[i] = Math.Min(width, height); radiusSquared[i] = radius[i] * radius[i]; } for (int i = 0; i < n; i++) { dispX[i] = 0; dispY[i] = 0; isMoveable[i] = graph.IsCellMovable(vertexArray[i]); // Get lists of neighbours to all vertices, translate the cells // obtained in indices into vertexArray and store as an array // against the orginial cell index Object[] edges = mxGraphModel.GetEdges(model, vertexArray[i]); Object[] cells = mxGraphModel.GetOpposites(model, edges, vertexArray[i], true, true); neighbours[i] = new int[cells.Length]; for (int j = 0; j < cells.Length; j++) { int?index = indices[cells[j]]; // Check the connected cell in part of the vertex list to be // acted on by this layout if (index != null) { neighbours[i][j] = (int)index; } // Else if index of the other cell doesn't correspond to // any cell listed to be acted upon in this layout. Set // the index to the value of this vertex (a dummy self-loop) // so the attraction force of the edge is not calculated else { neighbours[i][j] = i; } } } temperature = initialTemp; // If max number of iterations has not been set, guess it if (maxIterations == 0) { maxIterations = (int)(20 * Math.Sqrt(n)); } // Main iteration loop for (iteration = 0; iteration < maxIterations; iteration++) { if (!allowedToRun) { return; } // Calculate repulsive forces on all vertices calcRepulsion(); // Calculate attractive forces through edges calcAttraction(); calcPositions(); reduceTemperature(); } // Moved cell location back to top-left from center locations used in // algorithm model.BeginUpdate(); try { double?minx = null; double?miny = null; for (int i = 0; i < vertexArray.Length; i++) { Object vertex = vertexArray[i]; mxGeometry geo = model.GetGeometry(vertex); if (geo != null) { cellLocation[i][0] -= geo.Width / 2.0; cellLocation[i][1] -= geo.Height / 2.0; geo = geo.Clone(); geo.X = graph.Snap(cellLocation[i][0]); geo.Y = graph.Snap(cellLocation[i][1]); model.SetGeometry(vertex, geo); if (minx == null) { minx = geo.X; } else { minx = Math.Min((double)minx, geo.X); } if (miny == null) { miny = geo.Y; } else { miny = Math.Min((double)miny, geo.Y); } } } // Modifies the cloned geometries in-place. Not needed // to clone the geometries again as we're in the same // undoable change. if (minx != null || miny != null) { for (int i = 0; i < vertexArray.Length; i++) { Object vertex = vertexArray[i]; mxGeometry geo = model.GetGeometry(vertex); if (geo != null) { if (minx != null) { geo.X -= ((double)minx) - 1; } if (miny != null) { geo.Y -= ((double)miny) - 1; } } } } } finally { model.EndUpdate(); } }
/// <summary> /// Validates the given cell state. /// </summary> public void UpdateVertexState(mxCellState state, mxGeometry geo) { // LATER: Add support for rotation UpdateVertexLabelOffset(state); }
/// <summary> /// Updates the given cell state. /// </summary> /// <param name="state"></param> public void UpdateCellState(mxCellState state, mxCellState source, mxCellState target) { state.AbsoluteOffset.X = 0; state.AbsoluteOffset.Y = 0; state.Origin.X = 0; state.Origin.Y = 0; state.Length = 0; mxIGraphModel model = graph.Model; mxCellState pState = GetState(model.GetParent(state.Cell)); if (pState != null) { state.Origin.X += pState.Origin.X; state.Origin.Y += pState.Origin.Y; } mxPoint offset = graph.GetChildOffsetForCell(state.Cell); if (offset != null) { state.Origin.X += offset.X; state.Origin.Y += offset.Y; } mxGeometry geo = graph.GetCellGeometry(state.Cell); if (geo != null) { if (!model.IsEdge(state.Cell)) { mxPoint origin = state.Origin; offset = geo.Offset; if (offset == null) { offset = EMPTY_POINT; } if (geo.Relative && pState != null) { if (model.IsEdge(pState.Cell)) { mxPoint orig = GetPoint(pState, geo); if (orig != null) { origin.X += (orig.X / scale) - pState.Origin.X - translate.X; origin.Y += (orig.Y / scale) - pState.Origin.Y - translate.Y; } } else { origin.X += geo.X * pState.Width / scale + offset.X; origin.Y += geo.Y * pState.Height / scale + offset.Y; } } else { state.AbsoluteOffset = new mxPoint(scale * offset.X, scale * offset.Y); origin.X += geo.X; origin.Y += geo.Y; } } state.X = scale * (translate.X + state.Origin.X); state.Y = scale * (translate.Y + state.Origin.Y); state.Width = scale * geo.Width; state.Height = scale * geo.Height; if (model.IsVertex(state.Cell)) { UpdateVertexState(state, geo); } if (model.IsEdge(state.Cell)) { UpdateEdgeState(state, geo, source, target); } } }
/// <summary> /// Returns the absolute point on the edge for the given relative /// geometry as a point. The edge is represented by the given cell state. /// </summary> /// <param name="state">Represents the state of the parent edge.</param> /// <param name="geometry">Represents the relative location.</param> public mxPoint GetPoint(mxCellState state, mxGeometry geometry) { double x = state.GetCenterX(); double y = state.GetCenterY(); if (state.Segments != null && (geometry == null || geometry.Relative)) { double gx = (geometry != null) ? geometry.X / 2 : 0; int pointCount = state.AbsolutePoints.Count; double dist = (gx + 0.5) * state.Length; double[] segments = state.Segments; double segment = segments[0]; double length = 0; int index = 1; while (dist > length + segment && index < pointCount - 1) { length += segment; segment = segments[index++]; } double factor = (segment == 0) ? 0 : (dist - length) / segment; mxPoint p0 = state.AbsolutePoints[index - 1]; mxPoint pe = state.AbsolutePoints[index]; if (p0 != null && pe != null) { double gy = 0; double offsetX = 0; double offsetY = 0; if (geometry != null) { gy = geometry.Y; mxPoint offset = geometry.Offset; if (offset != null) { offsetX = offset.X; offsetY = offset.Y; } } double dx = pe.X - p0.X; double dy = pe.Y - p0.Y; double nx = (segment == 0) ? 0 : dy / segment; double ny = (segment == 0) ? 0 : dx / segment; x = p0.X + dx * factor + (nx * gy + offsetX) * scale; y = p0.Y + dy * factor - (ny * gy - offsetY) * scale; } } else if (geometry != null) { mxPoint offset = geometry.Offset; if (offset != null) { x += offset.X; y += offset.Y; } } return(new mxPoint(x, y)); }