コード例 #1
0
  /**
   * Like {@link #nodePosition(Graph,String,Point3)} but use an existing node as argument.
   *
   * @param node The node to consider.
   * @param pos A point that will receive the node position.
   */
  public static void nodePosition(Node node, Point3 pos) {
    if (node.hasAttribute("xyz") || node.hasAttribute("xy")) {
      Object o = node.getAttribute("xyz");

      if (o == null) o = node.getAttribute("xy");

      if (o != null) {
        positionFromObject(o, pos);
      }
    } else if (node.hasAttribute("x")) {
      pos.x = (double) node.getNumber("x");

      if (node.hasAttribute("y")) pos.y = (double) node.getNumber("y");

      if (node.hasAttribute("z")) pos.z = (double) node.getNumber("z");
    }

    //		if (node.hasAttribute("xyz") || node.hasAttribute("xy")) {
    //			Object o = node.getAttribute("xyz");
    //
    //			if (o == null)
    //				o = node.getAttribute("xy");
    //
    //			if (o != null && o instanceof Object[]) {
    //				Object oo[] = (Object[]) o;
    //
    //				if (oo.length > 0 && oo[0] instanceof Number) {
    //					pos.x = ((Number) oo[0]).doubleValue();
    //
    //					if (oo.length > 1)
    //						pos.y = ((Number) oo[1]).doubleValue();
    //					if (oo.length > 2)
    //						pos.z = ((Number) oo[2]).doubleValue();
    //				}
    //			}
    //		} else if (node.hasAttribute("x")) {
    //			pos.x = (double) node.getNumber("x");
    //
    //			if (node.hasAttribute("y"))
    //				pos.y = (double) node.getNumber("y");
    //
    //			if (node.hasAttribute("z"))
    //				pos.z = (double) node.getNumber("z");
    //		}
  }
コード例 #2
0
  /**
   * Compute a transformation matrix that pass from graph units (user space) to pixel units (device
   * space) so that the whole graph is visible.
   *
   * @param g2 The Swing graphics.
   */
  protected void autoFitView(Graphics2D g2) {
    double sx, sy;
    double tx, ty;
    double padXgu = getPaddingXgu() * 2;
    double padYgu = getPaddingYgu() * 2;
    double padXpx = getPaddingXpx() * 2;
    double padYpx = getPaddingYpx() * 2;

    sx = (metrics.viewport[2] - padXpx) / (metrics.size.data[0] + padXgu); // Ratio
    // along
    // X
    sy = (metrics.viewport[3] - padYpx) / (metrics.size.data[1] + padYgu); // Ratio
    // along
    // Y
    tx = metrics.lo.x + (metrics.size.data[0] / 2); // Centre of graph in X
    ty = metrics.lo.y + (metrics.size.data[1] / 2); // Centre of graph in Y

    if (sx <= 0) {
      sx =
          (metrics.viewport[2] - Math.min(metrics.viewport[2] - 1, padXpx))
              / (metrics.size.data[0] + padXgu);
    }

    if (sy <= 0) {
      sy =
          (metrics.viewport[3] - Math.min(metrics.viewport[3] - 1, padYpx))
              / (metrics.size.data[1] + padYgu);
    }

    if (sx > sy) // The least ratio.
    sx = sy;
    else sy = sx;

    g2.translate(metrics.viewport[2] / 2, metrics.viewport[3] / 2);
    if (rotation != 0) g2.rotate(rotation / (180 / Math.PI));
    g2.scale(sx, -sy);
    g2.translate(-tx, -ty);

    Tx = g2.getTransform();
    xT = new AffineTransform(Tx);
    try {
      xT.invert();
    } catch (NoninvertibleTransformException e) {
      logger.warning("Cannot inverse gu2px matrix.");
    }

    zoom = 1;

    center.set(tx, ty, 0);
    metrics.setRatioPx2Gu(sx);
    metrics.loVisible.copy(metrics.lo);
    metrics.hiVisible.copy(metrics.hi);
  }
コード例 #3
0
  /**
   * Enable or disable automatic adjustment of the view to see the entire graph.
   *
   * @param on If true, automatic adjustment is enabled.
   */
  public void setAutoFitView(boolean on) {
    if (autoFit && (!on)) {
      // We go from autoFit to user view, ensure the current centre is at
      // the
      // middle of the graph, and the zoom is at one.

      zoom = 1;
      center.set(
          metrics.lo.x + (metrics.size.data[0] / 2), metrics.lo.y + (metrics.size.data[1] / 2), 0);
    }

    autoFit = on;
  }
コード例 #4
0
  /**
   * Try to convert an object to a position. The object can be an array of numbers, an array of base
   * numeric types or their object counterparts.
   *
   * @param o The object to try to convert.
   * @param pos The result.
   */
  public static void positionFromObject(Object o, Point3 pos) {
    if (o instanceof Object[]) {
      Object oo[] = (Object[]) o;

      if (oo.length > 0 && oo[0] instanceof Number) {
        pos.x = ((Number) oo[0]).doubleValue();
        if (oo.length > 1) pos.y = ((Number) oo[1]).doubleValue();
        if (oo.length > 2) pos.z = ((Number) oo[2]).doubleValue();
      }
    } else if (o instanceof Double[]) {
      Double oo[] = (Double[]) o;
      if (oo.length > 0) pos.x = oo[0];
      if (oo.length > 1) pos.y = oo[1];
      if (oo.length > 2) pos.z = oo[2];
    } else if (o instanceof Float[]) {
      Float oo[] = (Float[]) o;
      if (oo.length > 0) pos.x = oo[0];
      if (oo.length > 1) pos.y = oo[1];
      if (oo.length > 2) pos.z = oo[2];
    } else if (o instanceof Integer[]) {
      Integer oo[] = (Integer[]) o;
      if (oo.length > 0) pos.x = oo[0];
      if (oo.length > 1) pos.y = oo[1];
      if (oo.length > 2) pos.z = oo[2];
    } else if (o instanceof double[]) {
      double oo[] = (double[]) o;
      if (oo.length > 0) pos.x = oo[0];
      if (oo.length > 1) pos.y = oo[1];
      if (oo.length > 2) pos.z = oo[2];
    } else if (o instanceof float[]) {
      float oo[] = (float[]) o;
      if (oo.length > 0) pos.x = oo[0];
      if (oo.length > 1) pos.y = oo[1];
      if (oo.length > 2) pos.z = oo[2];
    } else if (o instanceof int[]) {
      int oo[] = (int[]) o;
      if (oo.length > 0) pos.x = oo[0];
      if (oo.length > 1) pos.y = oo[1];
      if (oo.length > 2) pos.z = oo[2];
    } else if (o instanceof Number[]) {
      Number oo[] = (Number[]) o;
      if (oo.length > 0) pos.x = oo[0].doubleValue();
      if (oo.length > 1) pos.y = oo[1].doubleValue();
      if (oo.length > 2) pos.z = oo[2].doubleValue();
    } else if (o instanceof Point3) {
      Point3 oo = (Point3) o;
      pos.x = oo.x;
      pos.y = oo.y;
      pos.z = oo.z;
    } else if (o instanceof Vector3) {
      Vector3 oo = (Vector3) o;
      pos.x = oo.data[0];
      pos.y = oo.data[1];
      pos.z = oo.data[2];
    } else if (o instanceof Point2) {
      Point2 oo = (Point2) o;
      pos.x = oo.x;
      pos.y = oo.y;
      pos.z = 0;
    } else if (o instanceof Vector2) {
      Vector2 oo = (Vector2) o;
      pos.x = oo.data[0];
      pos.y = oo.data[1];
      pos.z = 0;
    } else {
      System.err.printf("Do not know how to handle xyz attribute %s%n", o.getClass().getName());
    }
  }
コード例 #5
0
 /*
  * (non-Javadoc)
  *
  * @see org.graphstream.ui.swingViewer.util.Camera#setViewCenter(double,
  * double, double)
  */
 public void setViewCenter(double x, double y, double z) {
   setAutoFitView(false);
   center.set(x, y, z);
   graph.graphChanged = true;
 }