use of automenta.vivisect.graph.VertexVis in project opennars by opennars.
the class AbegoTreeLayout method postUpdate.
@Override
public boolean postUpdate(AbstractGraphVis<V, E> g) {
if (finished)
return true;
float px = 0, py = 0;
float horizontalMargin = 16f;
float levelGap = 32f;
float nodeGap = 8f;
DirectedGraph<V, E> graph = (DirectedGraph<V, E>) g.getGraph();
ArrayList<V> roots = new ArrayList<V>();
for (V v : graph.vertexSet()) if (graph.inDegreeOf(v) == 0)
roots.add(v);
KruskalMinimumSpanningTree<V, E> st = new KruskalMinimumSpanningTree<V, E>(graph);
for (V r : roots) {
DefaultTreeForTreeLayout<VertexVis<V, E>> tree = new DefaultTreeForTreeLayout(g.getVertexDisplay(r));
DirectedMultigraph<V, E> subgraph = new DirectedMultigraph(DefaultEdge.class);
subgraph.addVertex(r);
// add all other vertices which are not roots
for (V v : graph.vertexSet()) {
if (!roots.contains(v))
subgraph.addVertex(v);
}
for (E e : st.getMinimumSpanningTreeEdgeSet()) {
V from = graph.getEdgeSource(e);
V to = graph.getEdgeTarget(e);
// continue;
if (!subgraph.containsVertex(from) || !subgraph.containsVertex(to))
continue;
// processed.add(from);
// processed.add(to);
subgraph.addEdge(from, to, e);
}
BreadthFirstIterator<V, E> i = new BreadthFirstIterator<V, E>(subgraph, r) {
@Override
protected void encounterVertex(V parent, E edge) {
super.encounterVertex(parent, edge);
VertexVis p = g.getVertexDisplay(parent);
Set<E> children = graph.outgoingEdgesOf(parent);
VertexVis[] cv = new VertexVis[children.size()];
int j = 0;
for (E ec : children) {
V vc = graph.getEdgeTarget(ec);
cv[j++] = g.getVertexDisplay(vc);
}
try {
tree.addChildren(p, cv);
} catch (Exception e) {
System.err.println(e);
System.err.println("vertex -> " + parent + " " + Arrays.toString(cv));
}
}
};
while (i.hasNext()) i.next();
TreeLayout<VertexVis<V, E>> treeLayout = new TreeLayout(tree, new NodeExtentProvider<VertexVis<V, E>>() {
@Override
public double getWidth(VertexVis<V, E> tn) {
return tn.getRadius();
}
@Override
public double getHeight(VertexVis<V, E> tn) {
return tn.getRadius();
}
}, new DefaultConfiguration(levelGap, nodeGap));
Map<VertexVis<V, E>, Rectangle2D.Double> bounds = treeLayout.getNodeBounds();
for (Map.Entry<VertexVis<V, E>, Rectangle2D.Double> vv : bounds.entrySet()) {
VertexVis<V, E> v = vv.getKey();
Rectangle2D.Double b = vv.getValue();
v.setPosition(px + (float) b.getCenterX(), py + (float) b.getCenterY());
}
// System.out.println(bounds);
px += treeLayout.getBounds().getWidth() + horizontalMargin;
}
finished = true;
return true;
}
use of automenta.vivisect.graph.VertexVis in project opennars by opennars.
the class FastOrganicLayout method postUpdate.
@Override
public boolean postUpdate(AbstractGraphVis<V, E> g) {
Graph<V, E> graph = g.getGraph();
if (indices == null)
indices = new HashMap<>();
else
indices.clear();
// Finds the relevant vertex for the layout
if (vertexArray == null)
vertexArray = new ArrayList();
else
vertexArray.clear();
for (V v : graph.vertexSet()) {
VertexVis vd = g.getVertexDisplay(v);
if (vd == null)
continue;
if (vd.getRadius() == 0)
continue;
vertexArray.add(vd);
}
// new mxRectangle(-100, -50, 100, 50);
mxRectangle initialBounds = null;
// ? graph.getBoundsForCells(vertexArray, false, false, true) : null;
int n = vertexArray.size();
if ((cellLocation == null) || (cellLocation.length != n)) {
dispX = new double[n];
dispY = new double[n];
cellLocation = new double[n][];
isMoveable = new boolean[n];
neighbors = new int[n][];
radius = new double[n];
radiusSquared = new double[n];
}
minDistanceLimitSquared = minDistanceLimit * minDistanceLimit;
if (forceConstant < 0.001) {
forceConstant = 0.001;
}
forceConstantSquared = forceConstant * forceConstant;
for (int i = 0; i < n; i++) {
VertexVis<V, E> vd = vertexArray.get(i);
if (cellLocation[i] == null)
cellLocation[i] = new double[2];
// Set up the mapping from array indices to cells
indices.put(vd.vertex, i);
// mxRectangle bounds = getVertexBounds(vertex);
// Set the X,Y value of the internal version of the cell to
// the center point of the vertex for better positioning
// bounds.getWidth();
double width = vd.getRadius() * 2f;
// bounds.getHeight();
double height = vd.getRadius() * 2f;
// Randomize (0, 0) locations
// TODO re-use existing location
double x, y;
if (vd == null) {
// Math.random() * 100.0;//Math.random() * 100; //bounds.getX();
x = 0;
// Math.random() * 100.0;
y = 0;
} else {
x = vd.getX();
y = vd.getY();
}
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];
// Moves cell location back to top-left from center locations used in
// algorithm, resetting the edge points is part of the transaction
dispX[i] = 0;
dispY[i] = 0;
// isVertexMovable(vertexArray[i]);
isMoveable[i] = true;
// Get lists of neighbours to all vertex, translate the cells
// obtained in indices into vertexArray and store as an array
// against the original cell index
// V v = vertexArray.get(i).getVertex();
// ProcessingGraphCanvas.VertexVis vd = displayed.get(v);
// TODO why does a vertex disappear from the graph... make this unnecessary
V v = vd.getVertex();
Set<E> edges = vd.getEdges();
if (edges != null) {
List<V> cells = new ArrayList(edges.size());
for (E e : edges) {
if (isResetEdges()) {
// graph.resetEdge(edge[k]);
}
if (isDisableEdgeStyle()) {
// setEdgeStyleEnabled(edge[k], false);
}
V source = graph.getEdgeSource(e);
V target = graph.getEdgeTarget(e);
if (source != v)
cells.add(source);
else if (target != v)
cells.add(target);
}
neighbors[i] = new int[cells.size()];
for (int j = 0; j < cells.size(); j++) {
Integer index = indices.get(cells.get(j));
// acted on by this layout
if (index != null) {
neighbors[i][j] = index.intValue();
} else // 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
{
neighbors[i][j] = i;
}
}
}
}
temperature = initialTemp;
// If max number of iterations has not been set, guess it
if (maxIterations == 0) {
maxIterations = 20.0 * Math.sqrt(n);
}
// Main iteration loop
try {
for (iteration = 0; iteration < maxIterations; iteration++) {
if (!allowedToRun) {
return false;
}
// Calculate repulsive forces on all vertex
calcRepulsion();
// Calculate attractive forces through edge
calcAttraction();
calcPositions();
reduceTemperature();
}
} catch (Exception e) {
}
double minx = 0, miny = 0, maxx = 0, maxy = 0;
for (int i = 0; i < vertexArray.size(); i++) {
VertexVis vd = vertexArray.get(i);
if (vd != null) {
// cellLocation[i][0] -= 1/2.0; //geo.getWidth() / 2.0;
// cellLocation[i][1] -= 1/2.0; //geo.getHeight() / 2.0;
float r = vd.getRadius();
double x = /*graph.snap*/
(cellLocation[i][0] - r);
double y = /*graph.snap*/
(cellLocation[i][1] - r);
vd.setPosition((float) x, (float) y);
if (i == 0) {
minx = maxx = x;
miny = maxy = y;
} else {
if (x < minx)
minx = x;
if (y < miny)
miny = y;
if (x > maxx)
maxx = x;
if (y > maxy)
maxy = y;
}
}
}
// Modifies the cloned geometries in-place. Not needed
// to clone the geometries again as we're in the same
// undoable change.
double dx = -(maxx + minx) / 2f;
double dy = -(maxy + miny) / 2f;
if (initialBounds != null) {
dx += initialBounds.getX();
dy += initialBounds.getY();
}
for (int i = 0; i < vertexArray.size(); i++) {
VertexVis vd = vertexArray.get(i);
vd.movePosition((float) dx, (float) dy);
}
return true;
}
Aggregations