private Node copyNode(Node node, Graph graph) { Node copy = buildNode(graph, node.getLabel()); //Copy properties (position, size and color): copy.setX(node.x()); copy.setY(node.y()); copy.setZ(node.z()); copy.setSize(node.size()); copy.setR(node.r()); copy.setG(node.g()); copy.setB(node.b()); copy.setAlpha(node.alpha()); Table nodeTable = graph.getModel().getNodeTable(); //Copy attributes: for (Column column : nodeTable) { if (!column.isReadOnly()) { copy.setAttribute(column, node.getAttribute(column)); } } graph.addNode(copy); return copy; }
public void setLabelColor(Color c) { if (c != null) { TextProperties textProps = node.getTextProperties(); textProps.setColor(c); } } }
/** * See https://github.com/gephi/gephi/issues/603 Nodes position to NaN on applied layout * * @param graphModel */ public static void ensureSafeLayoutNodePositions(GraphModel graphModel) { Graph graph = graphModel.getGraph(); NodeIterable nodesIterable = graph.getNodes(); for (Node node : nodesIterable) { if (node.x() != 0 || node.y() != 0) { nodesIterable.doBreak(); return; } } //All at 0.0, init some random positions nodesIterable = graph.getNodes(); for (Node node : nodesIterable) { node.setX((float) ((0.01 + Math.random()) * 1000) - 500); node.setY((float) ((0.01 + Math.random()) * 1000) - 500); } } }
public void readNodeData(XMLStreamReader reader, Node node) throws XMLStreamException { boolean end = false; while (reader.hasNext() && !end) { Integer eventType = reader.next(); if (eventType.equals(XMLEvent.START_ELEMENT)) { String name = reader.getLocalName(); if (ELEMENT_NODEDATA_POSITION.equalsIgnoreCase(name)) { node.setX(Float.parseFloat(reader.getAttributeValue(null, "x"))); node.setY(Float.parseFloat(reader.getAttributeValue(null, "y"))); node.setZ(Float.parseFloat(reader.getAttributeValue(null, "z"))); } else if (ELEMENT_NODEDATA_COLOR.equalsIgnoreCase(name)) { node.setR(Float.parseFloat(reader.getAttributeValue(null, "r"))); node.setG(Float.parseFloat(reader.getAttributeValue(null, "g"))); node.setB(Float.parseFloat(reader.getAttributeValue(null, "b"))); node.setAlpha(Float.parseFloat(reader.getAttributeValue(null, "a"))); } else if (ELEMENT_NODEDATA_SIZE.equalsIgnoreCase(name)) { node.setSize(Float.parseFloat(reader.getAttributeValue(0))); } } else if (eventType.equals(XMLStreamReader.END_ELEMENT)) { if (ELEMENT_NODEDATA.equalsIgnoreCase(reader.getLocalName())) { end = true; } } } }
public static void fcBiAttractor_noCollide(Node N1, Node N2, double c) { double xDist = N1.x() - N2.x(); // distance en x entre les deux noeuds double yDist = N1.y() - N2.y(); double dist = (float) Math.sqrt(xDist * xDist + yDist * yDist) - N1.size() - N2.size(); // distance (from the border of each node) if (dist > 0) { double f = attraction(c, dist); ForceVectorNodeLayoutData N1L = N1.getLayoutData(); ForceVectorNodeLayoutData N2L = N2.getLayoutData(); N1L.dx += xDist / dist * f; N1L.dy += yDist / dist * f; N2L.dx -= xDist / dist * f; N2L.dy -= yDist / dist * f; } }
private void clampPosition(NodeModel nodeModel) { //Clamp Hack to avoid nodes to be outside octree float quantum = size / 2; Node node = nodeModel.getNode(); float x = node.x(); float y = node.y(); float z = node.z(); if (x > root.posX + quantum) { node.setX(root.posX + quantum); } else if (x < root.posX - quantum) { node.setX(root.posX - quantum); } if (y > root.posY + quantum) { node.setY(root.posY + quantum); } else if (y < root.posY - quantum) { node.setY(root.posY - quantum); } if (z > root.posZ + quantum) { node.setZ(root.posZ + quantum); } else if (z < root.posZ - quantum) { node.setZ(root.posZ - quantum); } }
public static void fcUniRepulsor(Node N1, Node N2, double c) { double xDist = N1.x() - N2.x(); // distance en x entre les deux noeuds double yDist = N1.y() - N2.y(); double dist = (float) Math.sqrt(xDist * xDist + yDist * yDist); // distance tout court if (dist > 0) { double f = repulsion(c, dist); ForceVectorNodeLayoutData N2L = N2.getLayoutData(); N2L.dx -= xDist / dist * f; N2L.dy -= yDist / dist * f; } }
@Override public void mouseClick(int[] positionViewport, float[] position3d) { //Get current graph GraphController gc = Lookup.getDefault().lookup(GraphController.class); Graph graph = gc.getGraphModel().getGraph(); GraphFactory factory = gc.getGraphModel().factory(); //Add node Node node = factory.newNode(); node.setX(position3d[0]); node.setY(position3d[1]); node.setSize(10f); graph.addNode(node); } },
protected float cameraDistance(NodeModel object) { float[] cameraLocation = drawable.getCameraLocation(); double distance = Math.sqrt(Math.pow((double) object.getNode().x() - cameraLocation[0], 2d) + Math.pow((double) object.getNode().y() - cameraLocation[1], 2d) + Math.pow((double) object.getNode().z() - cameraLocation[2], 2d)); object.setCameraDistance((float) distance); return (float) distance; }
private Node buildNode(Graph graph, String label, String id) { Node newNode; if (id != null) { newNode = graph.getModel().factory().newNode(id); } else { newNode = graph.getModel().factory().newNode(); } newNode.setSize(DEFAULT_NODE_SIZE); newNode.setLabel(label); //Set random position to the node: newNode.setX((float) ((0.01 + Math.random()) * 1000) - 500); newNode.setY((float) ((0.01 + Math.random()) * 1000) - 500); return newNode; }
@Override public void mouseClick(int[] positionViewport, float[] position3d) { color = nodePencilPanel.getColor(); size = nodePencilPanel.getNodeSize(); GraphController gc = Lookup.getDefault().lookup(GraphController.class); GraphModel gm = gc.getGraphModel(); Graph graph = gm.getGraph(); Node node = gm.factory().newNode(); node.setX(position3d[0]); node.setY(position3d[1]); node.setSize(size); node.setColor(color); graph.addNode(node); } };
@Override public Edge createEdge(String id, Node source, Node target, boolean directed, Object typeLabel, Graph graph) { Edge newEdge = buildEdge(graph, id, source, target, directed, typeLabel); try { if (graph.addEdge(newEdge)) {//The edge will be created if it does not already exist. return newEdge; } } catch (Exception e) { Logger.getLogger("").log( Level.SEVERE, "Error when adding edge [id = {0}, source = {1}, target = {2}, directed = {3}, typeLabel = {4}] to the graph. Exception message: {5}", new Object[]{id, source.getId(), target.getId(), directed, typeLabel, e.getMessage()} ); } return null; }
private String getLabel(Node n, Column[] cols, GraphView view) { String str = ""; if (cols != null) { int i = 0; for (Column c : cols) { if (i++ > 0) { str += " - "; } Object val = n.getAttribute(c, view); str += val != null ? val : ""; } } if (str.isEmpty()) { str = n.getLabel(); } if (str == null) { str = ""; } return str; }
@Override public int compare(Node o1, Node o2) { Comparable a1 = (Comparable) o1.getAttribute(column); Comparable a2 = (Comparable) o2.getAttribute(column); if (a1 == null && a2 != null) { return 1; } else if (a1 != null && a2 == null) { return -1; } else if (a1 == null && a2 == null) { return 0; } else { return a2.compareTo(a1); } } });
@Override public float getCollisionDistance(double angle) { return node.size(); } }