private String transformationStr(Transformation trf) {
   NumberFormat fmt = FloatFormat.getInstance();
   Matrix matrix = trf.matrix();
   Vector vector = trf.vector();
   return "matrix <"
       + fmt.format(matrix.get(Transformation.X, Transformation.X))
       + ","
       + fmt.format(matrix.get(Transformation.Z, Transformation.X))
       + ","
       + fmt.format(matrix.get(Transformation.Y, Transformation.X))
       + ","
       + fmt.format(matrix.get(Transformation.X, Transformation.Z))
       + ","
       + fmt.format(matrix.get(Transformation.Z, Transformation.Z))
       + ","
       + fmt.format(matrix.get(Transformation.Y, Transformation.Z))
       + ","
       + fmt.format(matrix.get(Transformation.X, Transformation.Y))
       + ","
       + fmt.format(matrix.get(Transformation.Z, Transformation.Y))
       + ","
       + fmt.format(matrix.get(Transformation.Y, Transformation.Y))
       + ","
       + fmt.format(vector.getX())
       + ","
       + fmt.format(vector.getZ())
       + ","
       + fmt.format(vector.getY())
       + ">";
 }
 private String vectorStr(Vector v) {
   NumberFormat fmt = FloatFormat.getInstance();
   return "<"
       + fmt.format(v.getX())
       + ","
       + fmt.format(v.getZ())
       + ","
       + fmt.format(v.getY())
       + ">";
 }
Beispiel #3
0
  private void drawLine(Graphics g, Vector p, Vector q) {
    // FIXME: maybe eliminate class instantiations
    // from this method for performance reasons:
    // use static point arrays for transformations

    Vector u = rotation.apply(p.sub(origin));
    Vector v = rotation.apply(q.sub(origin));

    Point2D.Double from = new Point2D.Double(u.getX(), u.getZ());
    Point2D.Double to = new Point2D.Double(v.getX(), v.getZ());

    Point ifrom = new Point();
    Point ito = new Point();

    transform.transform(from, ifrom);
    transform.transform(to, ito);

    //		g.drawLine(xInt(u.getX()),yInt(u.getZ()),
    //				xInt(v.getX()),yInt(v.getZ()));

    g.drawLine(ifrom.x, ifrom.y, ito.x, ito.y);
  }
Beispiel #4
0
  private void initTransform(Graphics g) throws Exception {
    // Perform transformation
    transform = new AffineTransform();
    double dw = 1;
    double minw = 0;
    double dh = 0;
    double minh = 0;
    double scale;
    double x;
    double y;
    // double abs;
    final int margin = 5;

    int showLevel = previewTree.getShowLevel();

    class FindAStem extends DefaultTreeTraversal {
      Stem found = null;
      int level;

      public FindAStem(int level) {
        this.level = level;
      }

      public Stem getFound() {
        return found;
      }

      public boolean enterStem(Stem stem) {
        if (found == null && stem.stemlevel < level) return true; // look further
        else if (found != null || stem.stemlevel > level) return false; // found a stem or too deep
        else if (stem.stemlevel == level) found = stem;

        return true;
      }

      public boolean leaveTree(Tree tree) {
        return (found != null);
      }
    }

    if (showLevel < 1) {
      setOrigin(new Vector());

      //////////// FRONT view
      if (perspective == PERSPECTIVE_FRONT) {
        // get width and height of the tree
        dw = previewTree.getWidth() * 2;
        dh = previewTree.getHeight();
        minh = 0;
        minw = -dw / 2;
        ///////////////// TOP view
      } else {
        // get width of the tree
        dw = previewTree.getWidth() * 2;
        minw = -dw / 2;
      }

    } else {

      // find stem which to show
      /*			Enumeration e = previewTree.allStems(showLevel-1);
      if (! e.hasMoreElements()) throw new Exception("Stem not found");
      Stem stem = (Stem)e.nextElement();
      */
      Stem aStem = null;
      FindAStem stemFinder = new FindAStem(showLevel - 1);
      if (previewTree.traverseTree(stemFinder)) {
        aStem = stemFinder.getFound();
      }

      if (aStem != null) {
        Vector diag = aStem.getMaxPoint().sub(aStem.getMinPoint());
        Vector orig = aStem.getTransformation().getT();
        setOrigin(new Vector(orig.getX(), orig.getY(), 0));
        Vector max = aStem.getMaxPoint();
        Vector min = aStem.getMinPoint();

        // get greatest distance from orig
        x = Math.max(Math.abs(min.getX() - orig.getX()), Math.abs(max.getX() - orig.getX()));
        y = Math.max(Math.abs(min.getY() - orig.getY()), Math.abs(max.getY() - orig.getY()));

        dw = Math.sqrt(x * x + y * y) * 2;
        minw = -dw / 2;

        dh = diag.getZ();
        minh = min.getZ();
      }
      // DEBUG
      //			System.err.println("O: "+ orig +" min: "+min+" max: "+max);
      //			System.err.println("maxX: "+x+" maxY: "+y);
      //			System.err.println("dg: "+diag+" dw: "+dw+" minw: "+minw+" dh: "+dh+" minh: "+minh);
    }

    //////////// FRONT view
    if (perspective == PERSPECTIVE_FRONT) {

      // how much to scale for fitting into view?
      scale = Math.min((getHeight() - 2 * margin) / dh, (getWidth() - 2 * margin) / dw);

      if (previewTree.params.debug) System.err.println("scale: " + scale);

      // shift to mid point of the view
      transform.translate(getWidth() / 2, getHeight() / 2);
      // scale to image height
      transform.scale(scale, -scale);
      // shift mid of the tree to the origin of the image
      transform.translate(-minw - dw / 2, -minh - dh / 2);

      ///////////////// TOP view
    } else {

      // how much to scale for fitting into view?
      scale = Math.min((getHeight() - 2 * margin) / dw, (getWidth() - 2 * margin) / dw);

      // shift to mid point of the view
      transform.translate(getWidth() / 2, getHeight() / 2);
      // scale to image height
      transform.scale(scale, -scale);
      // shift mid of the stem to the origin of the image
      transform.translate(-minw - dw / 2, -minw - dw / 2);
    }

    // DEBUG
    Point p = new Point();
    transform.transform(new Point2D.Double(0.0, 0.0), p);
    if (previewTree.params.debug) {
      System.err.println("width: " + minw + "+" + dw);
      System.err.println("height: " + minh + "+" + dh);
      System.err.println("Origin at: " + p);
      System.err.println("view: " + getWidth() + "x" + getHeight());
    }
  }