Search in sources :

Example 21 with Point2D

use of java.awt.geom.Point2D in project GNS by MobilityFirst.

the class HostConfigParser method parseFile.

private void parseFile(String filename) throws HostConfigParseException {
    String confPath = getConfPath();
    if (confPath == null) {
        return;
    }
    Document doc = null;
    try {
        InputStream is = Files.newInputStream(Paths.get(confPath, folder, filename + fileExtension));
        //InputStream is = ClassLoader.getSystemResourceAsStream(filename + ".xml");
        //File fXmlFile = new File(filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(is);
    } catch (IOException | ParserConfigurationException | SAXException e) {
        throw new HostConfigParseException("Problem creating XML document " + e);
    }
    //	//optional, but recommended
    //	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    //	doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("host");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            String idString = eElement.getAttribute(ID);
            String hostname = eElement.getAttribute(HOSTNAME);
            String latString = eElement.getAttribute(LAT);
            String lonString = eElement.getAttribute(LON);
            if (idString.isEmpty() || hostname.isEmpty()) {
                throw new HostConfigParseException("Missing id or hostname attribute!");
            }
            //int id = Integer.parseInt(idString);
            Point2D location = null;
            if (!lonString.isEmpty() && !latString.isEmpty()) {
                location = new Point2D.Double(Double.parseDouble(lonString), Double.parseDouble(latString));
            }
            if (location == null) {
                InetAddress inetAddress = null;
                try {
                    inetAddress = InetAddress.getByName(hostname);
                } catch (UnknownHostException e) {
                    throw new HostConfigParseException("Problem looking up IP address " + e);
                }
                String ip = inetAddress.getHostAddress();
                // and take a guess at the location (lat, long) of this host
                location = GEOLocator.lookupIPLocation(ip);
            }
            hosts.add(new HostInfo(idString, hostname, location));
        }
    }
    keyname = getSingleElementAttribute(doc, KEYNAME, "name");
    username = getSingleElementAttribute(doc, USERNAME, "name");
    hostType = getSingleElementAttribute(doc, HOSTTYPE, "name");
    installPath = getSingleElementAttribute(doc, INSTALLPATH, "name");
    // if last character is a / remove it
    if (installPath != null && !installPath.isEmpty()) {
        installPath = installPath.trim();
        if (installPath.endsWith(System.getProperty("file.separator"))) {
            installPath = installPath.substring(0, installPath.length() - 1);
        }
    }
    String dataStoreTypeName = getSingleElementAttribute(doc, DATASTORE, "name");
    if (username == null) {
        // for backwards compatibility
        username = getSingleElementAttribute(doc, USERNAME_OLD, "name");
        if (username != null) {
            System.out.println("!!!Use of deprecated element tag " + USERNAME_OLD + ". Fix this!!!");
        }
    }
    if (keyname == null || username == null || hostType == null || dataStoreTypeName == null) {
        throw new HostConfigParseException("Missing " + KEYNAME + " or " + USERNAME + " or " + HOSTTYPE + " or " + DATASTORE + " tag");
    }
    dataStoreType = DataStoreType.valueOf(dataStoreTypeName);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Point2D(java.awt.geom.Point2D) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) InetAddress(java.net.InetAddress)

Example 22 with Point2D

use of java.awt.geom.Point2D in project opennms by OpenNMS.

the class D3TopoLayout method stepOld.

public void stepOld() {
    double currentForce;
    //guass-seidel relaxation for links
    for (E e : getGraph().getEdges()) {
        Pair<V> endPoints = getGraph().getEndpoints(e);
        VertexData srcVertexData = getVertexData(endPoints.getFirst());
        VertexData targetVertexData = getVertexData(endPoints.getSecond());
        double xDelta = targetVertexData.getX() - srcVertexData.getX();
        double yDelta = targetVertexData.getY() - srcVertexData.getY();
        double l = xDelta * xDelta + yDelta * yDelta;
        if (l != 0) {
            EdgeData edgeData = getEdgeData(e);
            double lSqrt = Math.sqrt(l);
            double distance = m_alpha * edgeData.getStrength() * (lSqrt - edgeData.getDistance()) / lSqrt;
            xDelta *= distance;
            yDelta *= distance;
            currentForce = srcVertexData.getWeight() / (targetVertexData.getWeight() + srcVertexData.getWeight());
            targetVertexData.offset(-(xDelta * currentForce), -(yDelta * currentForce));
            currentForce = 1 - currentForce;
            srcVertexData.offset(xDelta * currentForce, yDelta * currentForce);
        }
    }
    //Apply gravity forces
    currentForce = m_alpha * getGravity();
    if (currentForce != 0) {
        double centerX = getSize().getWidth() / 2;
        double centerY = getSize().getHeight() / 2;
        for (V v : getGraph().getVertices()) {
            VertexData vData = getVertexData(v);
            vData.offset((centerX - vData.getX()) * currentForce, (centerY - vData.getY()) * currentForce);
        }
    }
    //Compute quad tree center of mass and apply charge force
    if (getDefaultCharge() != 0) {
        DblBoundingBox bounds = new DblBoundingBox(0, 0, getSize().getWidth(), getSize().getHeight());
        QuadTree<VertexData> quadTree = new QuadTree<VertexData>(bounds);
        for (V v : getGraph().getVertices()) {
            VertexData vData = getVertexData(v);
            quadTree.insert(vData, vData.getCharge(), vData);
        }
        for (V v : getGraph().getVertices()) {
            final VertexData vData = getVertexData(v);
            quadTree.visit(new Visitor<VertexData>() {

                @Override
                public boolean visitNode(Node<VertexData> n) {
                    if (n.isLeaf() && vData == n.getValue())
                        return true;
                    double dx = n.getX() - vData.getX();
                    double dy = n.getY() - vData.getY();
                    double dw = n.getWidth();
                    double dSquared = dx * dx + dy * dy;
                    if (dw * dw / m_thetaSquared < dSquared) {
                        double force = n.getCharge() / dSquared;
                        vData.offset(-(dx * force), -(dy * force));
                        return true;
                    }
                    if (n.isLeaf()) {
                        if (dSquared == 0) {
                            vData.offset(1, 1);
                        } else {
                            double force = n.getCharge() / dSquared;
                            vData.offset(-(dx * force), -(dy * force));
                        }
                        return true;
                    }
                    return false;
                }
            });
        }
    }
    for (V v : getGraph().getVertices()) {
        VertexData vData = getVertexData(v);
        Point2D location = transform(v);
        location.setLocation(vData.getX(), vData.getY());
    }
    m_alpha *= 0.998235;
}
Also used : DblBoundingBox(org.opennms.features.topology.api.DblBoundingBox) Point2D(java.awt.geom.Point2D)

Example 23 with Point2D

use of java.awt.geom.Point2D in project opennms by OpenNMS.

the class D3TopoLayout method initialize.

@Override
public void initialize() {
    //initialize the weights
    for (V v : getGraph().getVertices()) {
        VertexData vData = getVertexData(v);
        vData.setWeight(1);
        Point2D location = transform(v);
        vData.setLocation(location);
        vData.setPrevious(location);
    }
    //initialize the vertices that have edges with weight
    for (E e : getGraph().getEdges()) {
        Pair<V> endPoints = getGraph().getEndpoints(e);
        V v1 = endPoints.getFirst();
        V v2 = endPoints.getSecond();
        VertexData vData1 = getVertexData(v1);
        vData1.setWeight(vData1.getWeight() + 1);
        VertexData vData2 = getVertexData(v2);
        vData2.setWeight(vData2.getWeight() + 1);
    }
//Do we need to do an initial layout, we can rely on the initialized position
}
Also used : Point2D(java.awt.geom.Point2D)

Example 24 with Point2D

use of java.awt.geom.Point2D in project opennms by OpenNMS.

the class D3TopoLayout method step.

@Override
public void step() {
    double currentForce;
    //guass-seidel relaxation for links
    for (E e : getGraph().getEdges()) {
        Pair<V> endPoints = getGraph().getEndpoints(e);
        VertexData srcVertexData = getVertexData(endPoints.getFirst());
        VertexData targetVertexData = getVertexData(endPoints.getSecond());
        double xDelta = targetVertexData.getX() - srcVertexData.getX();
        double yDelta = targetVertexData.getY() - srcVertexData.getY();
        double l = xDelta * xDelta + yDelta * yDelta;
        if (l != 0) {
            EdgeData edgeData = getEdgeData(e);
            double lSqrt = Math.sqrt(l);
            double distance = m_alpha * edgeData.getStrength() * (lSqrt - edgeData.getDistance()) / lSqrt;
            //double distance = edgeData.getStrength() * (lSqrt - edgeData.getDistance()) / lSqrt;
            xDelta *= distance;
            yDelta *= distance;
            currentForce = srcVertexData.getWeight() / (targetVertexData.getWeight() + srcVertexData.getWeight());
            //currentForce = 0.5;
            targetVertexData.offset(-(xDelta * currentForce), -(yDelta * currentForce));
            currentForce = 1 - currentForce;
            srcVertexData.offset(xDelta * currentForce, yDelta * currentForce);
        }
    }
    //Apply gravity forces
    currentForce = m_alpha * getGravity();
    if (currentForce != 0) {
        double centerX = getSize().getWidth() / 2;
        double centerY = getSize().getHeight() / 2;
        for (V v : getGraph().getVertices()) {
            VertexData vData = getVertexData(v);
            vData.offset((centerX - vData.getX()) * currentForce, (centerY - vData.getY()) * currentForce);
        }
    }
    //Compute quad tree center of mass and apply charge force
    if (getDefaultCharge() != 0) {
        for (V v1 : getGraph().getVertices()) {
            VertexData vData1 = getVertexData(v1);
            for (V v2 : getGraph().getVertices()) {
                VertexData vData2 = getVertexData(v2);
                double dx = vData2.getX() - vData1.getX();
                double dy = vData2.getY() - vData1.getY();
                double d = dx * dx + dy * dy;
                if (d > 0) {
                    double k = m_alpha * vData2.getCharge() / d;
                    double px = dx * k;
                    double py = dy * k;
                    //vData1.offsetPrevious(px, py);
                    vData1.offset(px, py);
                } else {
                    //vData1.offsetPrevious(0.5-Math.random(), 0.5-Math.random());
                    vData1.offset(0.5 - Math.random(), 0.5 - Math.random());
                }
            }
        }
    }
    // position verlet integration
    for (V v : getGraph().getVertices()) {
        VertexData vData = getVertexData(v);
        double tempX = vData.getX();
        double tempY = vData.getY();
        double x = vData.getX() + (vData.getPrevious().getX() - vData.getX()) * getFriction();
        double y = vData.getY() + (vData.getPrevious().getY() - vData.getY()) * getFriction();
        vData.setLocation(x, y);
        vData.setPrevious(tempX, tempY);
        Point2D location = transform(v);
        location.setLocation(vData.getX(), vData.getY());
    }
    m_alpha *= 0.99;
}
Also used : Point2D(java.awt.geom.Point2D)

Example 25 with Point2D

use of java.awt.geom.Point2D in project opennms by OpenNMS.

the class TopoFRLayout method calcPositions.

protected synchronized void calcPositions(V v) {
    FRVertexData fvd = getFRData(v);
    if (fvd == null)
        return;
    Point2D xyd = transform(v);
    double deltaLength = fvd.norm();
    if (deltaLength <= 0.005)
        return;
    double newXDisp = fvd.getX() * percentage() / deltaLength * Math.min(deltaLength, temperature);
    if (Double.isNaN(newXDisp)) {
        throw new IllegalArgumentException("Unexpected mathematical result in FRLayout:calcPositions [xdisp]");
    }
    double newYDisp = fvd.getY() * percentage() / deltaLength * Math.min(deltaLength, temperature);
    xyd.setLocation(xyd.getX() + newXDisp, xyd.getY() + newYDisp);
    double borderWidth = getSize().getWidth() / 50.0;
    double newXPos = xyd.getX();
    if (newXPos < borderWidth) {
        newXPos = borderWidth + Math.random() * borderWidth * 2.0;
    } else if (newXPos > (getSize().getWidth() - borderWidth)) {
        newXPos = getSize().getWidth() - borderWidth - Math.random() * borderWidth * 2.0;
    }
    double newYPos = xyd.getY();
    if (newYPos < borderWidth) {
        newYPos = borderWidth + Math.random() * borderWidth * 2.0;
    } else if (newYPos > (getSize().getHeight() - borderWidth)) {
        newYPos = getSize().getHeight() - borderWidth - Math.random() * borderWidth * 2.0;
    }
    xyd.setLocation(newXPos, newYPos);
}
Also used : Point2D(java.awt.geom.Point2D)

Aggregations

Point2D (java.awt.geom.Point2D)159 Rectangle2D (java.awt.geom.Rectangle2D)18 Color (java.awt.Color)12 RadialGradientPaint (java.awt.RadialGradientPaint)10 AffineTransform (java.awt.geom.AffineTransform)10 Line2D (java.awt.geom.Line2D)9 Paint (java.awt.Paint)8 Graphics2D (java.awt.Graphics2D)7 BufferedImage (java.awt.image.BufferedImage)7 Element (org.jdom2.Element)6 BasicStroke (java.awt.BasicStroke)5 IOException (java.io.IOException)5 LinearGradientPaint (java.awt.LinearGradientPaint)4 Point (java.awt.Point)4 Rectangle (java.awt.Rectangle)4 NodeRef (dr.evolution.tree.NodeRef)3 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)3 Arc2D (java.awt.geom.Arc2D)3 Test (org.junit.Test)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)2