コード例 #1
0
ファイル: TreePreview.java プロジェクト: rmichela/GiantTrees
    public boolean enterStem(Stem stem) throws TraversalException {
      stem.traverseStem(
          new StemTraversal() {
            public boolean enterStem(Stem stem) {
              return true;
            }

            public boolean leaveStem(Stem stem) {
              return true;
            }

            public boolean enterSegment(Segment seg) throws TraversalException {
              // FIXME: maybe draw rectangles instead of thin lines
              // drawStripe(g,seg.posFrom(),seg.rad1,seg.postTo(),seg.rad2());
              drawLine(g, seg.posFrom(), seg.posTo());
              return true;
            }

            public boolean leaveSegment(Segment seg) {
              return true;
            }

            public boolean visitSubsegment(Subsegment ss) {
              return true;
            }
          });

      return true;
    }
コード例 #2
0
ファイル: TreePreview.java プロジェクト: rmichela/GiantTrees
  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());
    }
  }
コード例 #3
0
  /* (non-Javadoc)
   * @see net.sourceforge.arbaro.tree.TreeTraversal#enterStem(net.sourceforge.arbaro.tree.Stem)
   */
  public boolean enterStem(Stem stem) {
    if (level >= 0 && stem.getLevel() < level) {
      return true; // look further for stems

    } else if (level >= 0 && stem.getLevel() > level) {
      return false; // go back to higher level

    } else {

      String indent = whitespace(stem.getLevel() * 2 + 4);
      NumberFormat fmt = FloatFormat.getInstance();
      Enumeration sections = stem.sections();

      if (sections.hasMoreElements()) {
        StemSection from = (StemSection) sections.nextElement();
        StemSection to = null;

        while (sections.hasMoreElements()) {
          to = (StemSection) sections.nextElement();

          w.println(
              indent
                  + "cone   { "
                  + vectorStr(from.getPosition())
                  + ", "
                  + fmt.format(from.getRadius())
                  + ", "
                  + vectorStr(to.getPosition())
                  + ", "
                  + fmt.format(to.getRadius())
                  + " }");

          // put spheres where z-directions changes
          if (!from.getZ().equals(to.getPosition().sub(from.getPosition()).normalize())) {

            w.println(
                indent
                    + "sphere { "
                    + vectorStr(from.getPosition())
                    + ", "
                    + fmt.format(from.getRadius() - 0.0001)
                    + " }");
          }

          from = to;
        }

        // put sphere at stem end
        /* FIXME? now using sections instead of segments, the spherical stem end
        *       will be made from several cones instead of one shpere ...

        			if ((to.getRadius() > 0.0001) ||
        					(lpar.nTaper>1 && lpar.nTaper<=2))
        			{
        				w.println(indent + "sphere { " + vectorStr(to.getPosition()) + ", "
        						+ fmt.format(to.getRadius()-0.0001) + " }");
        			}
        		*/
      }

      exporter.incProgressCount(AbstractExporter.STEM_PROGRESS_STEP);

      return true;
    }
  }