KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > distortion > FisheyeDistortion


1 package prefuse.action.distortion;
2
3 import java.awt.geom.Point2D JavaDoc;
4 import java.awt.geom.Rectangle2D JavaDoc;
5
6
7 /**
8  * <p>
9  * Computes a graphical fisheye distortion of a graph view. This distortion
10  * allocates more space to items near the layout anchor and less space to
11  * items further away, magnifying space near the anchor and demagnifying
12  * distant space in a continuous fashion.
13  * </p>
14  *
15  * <p>
16  * For more details on this form of transformation, see Manojit Sarkar and
17  * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
18  * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
19  * online at <a HREF="http://citeseer.ist.psu.edu/sarkar92graphical.html">
20  * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
21  * </p>
22  *
23  * @author <a HREF="http://jheer.org">jeffrey heer</a>
24  */

25 public class FisheyeDistortion extends Distortion {
26
27     private double dx, dy; // distortion factors
28
private double sz = 3.0; // size factor
29

30     /**
31      * Create a new FisheyeDistortion with default distortion factor.
32      */

33     public FisheyeDistortion() {
34         this(4);
35     }
36     
37     /**
38      * Create a new FisheyeDistortion with the given distortion factor
39      * for use along both the x and y directions.
40      * @param dfactor the distortion factor (same for both axes)
41      */

42     public FisheyeDistortion(double dfactor) {
43         this(dfactor, dfactor);
44     }
45     
46     /**
47      * Create a new FisheyeDistortion with the given distortion factors
48      * along the x and y directions.
49      * @param xfactor the distortion factor along the x axis
50      * @param yfactor the distortion factor along the y axis
51      */

52     public FisheyeDistortion(double xfactor, double yfactor) {
53         super();
54         dx = xfactor;
55         dy = yfactor;
56         m_distortX = dx > 0;
57         m_distortY = dy > 0;
58     }
59     
60     /**
61      * Returns the distortion factor for the x-axis.
62      * @return returns the distortion factor for the x-axis.
63      */

64     public double getXDistortionFactor() {
65         return dx;
66     }
67
68     /**
69      * Sets the distortion factor for the x-axis.
70      * @param d The distortion factor to set.
71      */

72     public void setXDistortionFactor(double d) {
73         dx = d;
74         m_distortX = dx > 0;
75     }
76     
77     /**
78      * Returns the distortion factor for the y-axis.
79      * @return returns the distortion factor for the y-axis.
80      */

81     public double getYDistortionFactor() {
82         return dy;
83     }
84
85     /**
86      * Sets the distortion factor for the y-axis.
87      * @param d The distortion factor to set.
88      */

89     public void setYDistortionFactor(double d) {
90         dy = d;
91         m_distortY = dy > 0;
92     }
93     
94     /**
95      * @see prefuse.action.distortion.Distortion#distortX(double, java.awt.geom.Point2D, java.awt.geom.Rectangle2D)
96      */

97     protected double distortX(double x, Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds) {
98         return fisheye(x,anchor.getX(),dx,bounds.getMinX(),bounds.getMaxX());
99     }
100     
101     /**
102      * @see prefuse.action.distortion.Distortion#distortY(double, java.awt.geom.Point2D, java.awt.geom.Rectangle2D)
103      */

104     protected double distortY(double y, Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds) {
105         return fisheye(y,anchor.getY(),dy,bounds.getMinY(),bounds.getMaxY());
106     }
107     
108     /**
109      * @see prefuse.action.distortion.Distortion#distortSize(java.awt.geom.Rectangle2D, double, double, java.awt.geom.Point2D, java.awt.geom.Rectangle2D)
110      */

111     protected double distortSize(Rectangle2D JavaDoc bbox, double x, double y,
112             Point2D JavaDoc anchor, Rectangle2D JavaDoc bounds)
113     {
114         if ( !m_distortX && !m_distortY ) return 1.;
115         double fx=1, fy=1;
116
117         if ( m_distortX ) {
118             double ax = anchor.getX();
119             double minX = bbox.getMinX(), maxX = bbox.getMaxX();
120             double xx = (Math.abs(minX-ax) > Math.abs(maxX-ax) ? minX : maxX);
121             if ( xx < bounds.getMinX() || xx > bounds.getMaxX() )
122                 xx = (xx==minX ? maxX : minX);
123             fx = fisheye(xx,ax,dx,bounds.getMinX(),bounds.getMaxX());
124             fx = Math.abs(x-fx)/bbox.getWidth();
125         }
126
127         if ( m_distortY ) {
128             double ay = anchor.getY();
129             double minY = bbox.getMinY(), maxY = bbox.getMaxY();
130             double yy = (Math.abs(minY-ay) > Math.abs(maxY-ay) ? minY : maxY);
131             if ( yy < bounds.getMinY() || yy > bounds.getMaxY() )
132                 yy = (yy==minY ? maxY : minY);
133             fy = fisheye(yy,ay,dy,bounds.getMinY(),bounds.getMaxY());
134             fy = Math.abs(y-fy)/bbox.getHeight();
135         }
136         
137         double sf = (!m_distortY ? fx : (!m_distortX ? fy : Math.min(fx,fy)));
138         if (Double.isInfinite(sf) || Double.isNaN(sf)) {
139             return 1.;
140         } else {
141             return sz*sf;
142         }
143     }
144     
145     private double fisheye(double x, double a, double d, double min, double max) {
146         if ( d != 0 ) {
147             boolean left = x<a;
148             double v, m = (left ? a-min : max-a);
149             if ( m == 0 ) m = max-min;
150             v = Math.abs(x - a) / m;
151             v = (d+1)/(d+(1/v));
152             return (left?-1:1)*m*v + a;
153         } else {
154             return x;
155         }
156     }
157     
158 } // end of class FisheyeDistortion
159
Popular Tags