📄 treeedge.java
字号:
/* Copyright (c) 2002 Compaq Computer Corporation SOFTWARE RELEASE Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the names of Compaq Research, Compaq Computer Corporation nor the names of its contributors may be used to endorse or promote products derived from this Software without specific prior written permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL COMPAQ COMPUTER CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/package AccordionTreeDrawer;import java.awt.Color;import java.io.IOException;import java.util.*;import gl4java.*;import gl4java.GLFunc;import AccordionDrawer.*;/** * A class representing an edge of a (phylognenetic) tree * * A TreeEdge represents a horizontal or vertical line segment used in drawing a tree. * * <Pre> * a--- firstChild * | * | * --- . * n | . * o---|c . * d --- * e | * | * b--- lastChild * * </Pre> * * In this figure (node,c), (a,b), (firstChild,a) and (lastChild,b) are line segments * Each of them is represented by a TreeEdge * (node,c) corresponds to a horizontal TreeEdge associated with TreeNode node * (a,b) corresponds to a vertical TreeEdge associated with TreeNode node * * @author Tamara Munzner, Serdar Tasiran, Li Zhang, Yunhong Zhou * @version * @see AccordionDrawer.Tree * @see AccordionDrawer.TreeNode * @see AccordionDrawer.GridCell */public class TreeEdge { public TreeNode node; /** * Whether this TreeEdge is horizontal */ public boolean horiz; public TreeEdge(TreeNode n, boolean horizontal) { node = n; horiz = horizontal; // from me to parent I'm horizontal } // public TreeNode getNode()// {// return node;// } private GridCell getCell() { return node.getCell(); } public AccordionTreeDrawer getDrawer() { return (AccordionTreeDrawer)getCell().drawer; }// /**// * clean the tree edge// * @see TreeNode.close// *// */// public void close(){// node=null;// } // protected void finalize() throws Throwable// { // try// {// close();// }// finally// {// super.finalize();// }// } // public int getKey() { return node.getKey(); }// public String getName() { return node.getName(); } // never used?// public GridCell getCell() { return node.getCell();}// public void setCell(GridCell c) { node.setCell(c);} // these will return splitLine indices public int getMin() { return node.getMin(); } public int getMax() { return node.getMax(); } public void setPositions(float[] start, float[] end)//, int direction) { AccordionTreeDrawer atd = getDrawer(); int X = AccordionDrawer.X, Y = AccordionDrawer.Y; GridCell c = getCell(); double[] minStuck = {atd.getSplitLine(Y).getMinStuckValue(), atd.getSplitLine(Y).getMinStuckValue()}; double[] rangeSize = { atd.getSplitLine(X).getMaxStuckValue() - minStuck[X], atd.getSplitLine(Y).getMaxStuckValue() - minStuck[Y]}; if (horiz) { start[X] = (float)c.getMin(X); // X is over the range end[X] = (float)c.getMax(X); start[Y] = end[Y] = (float)node.getMidY(); } else { start[X] = end[X] = (float)c.getMax(X); start[Y] = (float)node.getMinY(); end[Y] = (float)node.getMaxY(); } // draw in reverse direction// if (direction == AccordionDrawer.RIGHT)// {// start[X] = (float)(rangeSize[X] - start[X] + minStuck[X]);// end[X] = (float)(rangeSize[X] - end[X] + minStuck[X]);// } //TODO: fix drawing in different directions// //dawn in up direction// else if (direction == AccordionDrawer.UP)// {// float temp= start[0];// start[0]= start[1];// start[1] = 1- temp;// temp= end[0];// end[0]= end[1];// end[1] =1- temp;// }// // draw in down direction// else if (direction == AccordionDrawer.DOWN)// {// float temp= start[1];// start[1]= start[0];// start[0] =1- temp;// temp= end[1];// end[1]= end[0];// end[0] =1- temp;// } } /** * Draws this TreeEdge inside the GridCell to which it is * attached, and its label if appropriate. * * Find font size to use for drawing the label, by checking for * occlusions. Don't draw it all if it's occluded even at the * smallest size. * * @author Tamara Munzner, Li Zhang * * @param col The color to draw this node in * @param plane Z depth at which to draw label * * @see AccordionDrawer.GridCell */ private Color setGLColor(AccordionTreeDrawer atd, Color col) { float rgbcol[] = new float[3]; Color newrgb; if (atd.dimcolors || atd.dimbrite) { col.getRGBColorComponents(rgbcol); float hsbcol[] = Color.RGBtoHSB((int) (rgbcol[0] * 255), (int) (rgbcol[1] * 255), (int) (rgbcol[2] * 255), (new float[3])); float howdim; // H stays the same // if started out greyscale, don't crank up S if (hsbcol[1] > 0f && atd.dimcolors) { // depends on size of your subtended subtree screen area InteractionBox b = atd.makeBox(node); // box around subtree if (null == b) System.out.println("interaction box null"); // howdim base: one pixel 0, fullscreen 1.0 howdim = (float) (b.getMax(AccordionDrawer.Y) - b .getMin(AccordionDrawer.Y)) / (float) atd.getWinMax(1); //screen coords // small dynamic range:, 0%=.5, 20% and up =1.0 howdim = .5f + 40 * howdim; howdim = (howdim > 1.0) ? 1.0f : howdim; hsbcol[1] = (float) howdim; } if (atd.dimbrite && col == atd.getObjectColor()) { // depends on depth in tree int treeheight = atd.tree.getHeight(); // howdim ranges from 0 at root to 1.0 at leaf howdim = (node.height - 1.0f) / (treeheight - 1.0f); // compress the range hsbcol[2] = (float) (0.2 + howdim * .7); } newrgb = Color.getHSBColor(hsbcol[0], hsbcol[1], hsbcol[2]); } else { newrgb = col; } return newrgb; } public void drawInCell(Color col, double plane) { // System.out.println("Drawing: " + node.getCell()); int X = horiz ? 0 : 1; int Y = horiz ? 1 : 0; AccordionTreeDrawer atd = (AccordionTreeDrawer) node.cell.drawer; boolean isBase = false; if (col == null) { col = atd.objectColor; isBase = true; } if (atd.nogeoms) return; AccordionTreeDrawer.countDrawnFrame++; GLFunc gl = (GLFunc) atd.getGraphicsStuff(); // this is not a gl call, just a reference Color drawColor = setGLColor(atd, col); // draw the line int thick = atd.getLineThickness(); if (col != atd.getObjectColor() && !atd.isDoingFlash()) thick += 2; float[] posStart = new float[2], posEnd = new float[2]; setPositions(posStart, posEnd);//, atd.drawDirection); // if (horiz) // System.out.println(node + " Set positions: Y:" + posEnd[1]); if (atd.takeSnapshot) { if ((atd.basePass && isBase) || (atd.groupPass && !isBase)) try { int cellMinPix[] = {atd.w2s(posStart[X], X), atd.w2s(posStart[Y], Y)}; int cellMaxPix[] = {atd.w2s(posEnd[X], X), atd.w2s(posEnd[Y], Y)}; atd.snapShotWriter.write(thick + " setlinewidth newpath " + cellMinPix[X] + " " + cellMinPix[Y] + " moveto " + cellMaxPix[X] + " " + cellMaxPix[Y] + " lineto " + "closepath " + "gsave " + drawColor.getRed() / 255f + " " + drawColor.getGreen() / 255f + " " + drawColor.getBlue() / 255f + " setrgbcolor " + "stroke grestore\n"); } catch (IOException ioe) { System.out.println("Error: IOException while trying to write cell to file: " + atd.snapShotWriter.toString()); } } else { atd.setColorGL(drawColor); gl.glLineWidth(thick); gl.glBegin(GLEnum.GL_LINES); gl.glVertex3d(posStart[0], posStart[1], plane); gl.glVertex3d(posEnd[0], posEnd[1], plane); gl.glEnd(); } if (horiz) { // draw the dot at the end of the horizontal section if (atd.takeSnapshot) { if ((atd.basePass && isBase) || (atd.groupPass && !isBase)) try { int cellMaxPix[] = {atd.w2s(posEnd[X], X), atd.w2s(posEnd[Y], Y)}; atd.snapShotWriter.write("newpath " + (cellMaxPix[X]-thick) + " " + (cellMaxPix[Y]-thick) + " moveto " + (cellMaxPix[X]-thick) + " " + (cellMaxPix[Y]+thick) + " lineto " + (cellMaxPix[X]+thick) + " " + (cellMaxPix[Y]+thick) + " lineto " + (cellMaxPix[X]+thick) + " " + (cellMaxPix[Y]-thick) + " lineto " + "closepath " + "gsave " + drawColor.getRed() / 255f + " " + drawColor.getGreen() / 255f + " " + drawColor.getBlue() / 255f + " setrgbcolor " + "eofill grestore\n"); } catch (IOException ioe) { System.out.println("Error: IOException while trying to write cell to file: " + atd.snapShotWriter.toString()); } } else { gl.glPointSize((float) (thick + 2.0f)); gl.glBegin(GLEnum.GL_POINTS); gl.glVertex3d(posEnd[0], posEnd[1], plane); gl.glEnd(); } } if (horiz && atd.drawlabels && node.label.length() > 0 && !atd.basePass && !atd.groupPass ) { // first, check smallest font, to see if anything at all // can fit. if it does, then do binary search to find // largest possible box that fits. after we know which box // size to use, draw it. // this algorithm means drawing order will have a radical // effect on how labels look! // we should set min to one less than the real bound. new // real bound is minFontSize+1 (because we already tested // minFontSize), so leave min set to current value of // (minFontSize+1)-1 = minFontSize int min = ((AccordionTreeDrawer)atd).minFontHeight; int max = ((AccordionTreeDrawer)atd).maxFontHeight; int mid = max; int fitsheight = min; LabelBox fits = makeLabelBox(min, -1, -1, posStart, posEnd);// System.out.println("min font size: " + min); if (!intersectLabelBox(fits)) { if (max != min) { LabelBox maxBox = makeLabelBox(max, -1, -1, posStart, posEnd); if (!intersectLabelBox(maxBox)) { fits = maxBox; fitsheight = max; } else while (min+1 < max) { mid = (int) ((min + max) / 2.0);// if (mid == max)// break; // stop testing the same value// System.out.println("testing font size: " + min + " " + mid + " " + max); LabelBox lb = makeLabelBox(mid, -1, -1, posStart, posEnd); if (intersectLabelBox(lb)) max = mid; // too big else { fits = lb; // bigger size fitsheight = mid; min = mid; } } }// if (atd.takeSnapshot)// System.out.println(node.name + " using font size: " + fitsheight); drawLabelBox(fits, fitsheight, false);// else// drawLabelBox(fits, fitsheight, false); } } } /** * Draw label of this TreeEdge at maximum size (intended for * mouseover highlighting).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -