public synchronized boolean updateWorld() {
if (observer != null && observer.hasGraphChanged()) {
NodeModeler nodeModeler = (NodeModeler) engine.getNodeClass().getCurrentModeler();
EdgeModeler edgeModeler = (EdgeModeler) engine.getEdgeClass().getCurrentModeler();
Octree octree = engine.getOctree();
//Stats
int removedNodes = 0;
int addedNodes = 0;
int removedEdges = 0;
int addedEdges = 0;
for (int i = 0; i < nodes.length; i++) {
NodeModel node = nodes[i];
if (node != null && node.getNode().getStoreId() == -1) {
//Removed
octree.removeNode(node);
nodes[i] = null;
removedNodes++;
}
}
for (Node node : graph.getNodes()) {
int id = node.getStoreId();
if (id >= nodes.length || nodes[id] == null) {
growNodes(id);
NodeModel model = nodeModeler.initModel(node);
octree.addNode(model);
nodes[id] = model;
addedNodes++;
}
}
for (int i = 0; i < edges.length; i++) {
EdgeModel edge = edges[i];
if (edge != null && edge.getEdge().getStoreId() == -1) {
//Removed
NodeModel sourceModel = nodes[edge.getEdge().getSource().getStoreId()];
NodeModel targetModel = nodes[edge.getEdge().getTarget().getStoreId()];
if (sourceModel != null) {
sourceModel.removeEdge(edge);
}
if (targetModel != null) {
targetModel.removeEdge(edge);
}
edges[i] = null;
removedEdges++;
}
}
float minWeight = Float.MAX_VALUE;
float maxWeight = Float.MIN_VALUE;
for (Edge edge : graph.getEdges()) {
int id = edge.getStoreId();
if (id >= edges.length || edges[id] == null) {
growEdges(id);
NodeModel sourceModel = nodes[edge.getSource().getStoreId()];
NodeModel targetModel = nodes[edge.getTarget().getStoreId()];
EdgeModel model = edgeModeler.initModel(edge, sourceModel, targetModel);
sourceModel.addEdge(model);
targetModel.addEdge(model);
edges[id] = model;
addedEdges++;
}
float w = (float) edge.getWeight();
minWeight = Math.min(w, minWeight);
maxWeight = Math.max(w, maxWeight);
}
limits.setMaxWeight(maxWeight);
limits.setMinWeight(minWeight);
System.out.println("DATABRIDGE:");
System.out.println(" Removed Edges: " + removedEdges);
System.out.println(" Added Edges: " + addedEdges);
System.out.println(" Removed Nodes: " + removedNodes);
System.out.println(" Added Nodes: " + addedNodes);
return true;
} else if (observer == null) {
Octree octree = engine.getOctree();
if (!octree.isEmpty()) {
octree.clear();
}
}
return false;
}