1 package prefuse.action.distortion; 2 3 import java.awt.geom.Point2D ; 4 import java.awt.geom.Rectangle2D ; 5 6 7 25 public class FisheyeDistortion extends Distortion { 26 27 private double dx, dy; private double sz = 3.0; 30 33 public FisheyeDistortion() { 34 this(4); 35 } 36 37 42 public FisheyeDistortion(double dfactor) { 43 this(dfactor, dfactor); 44 } 45 46 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 64 public double getXDistortionFactor() { 65 return dx; 66 } 67 68 72 public void setXDistortionFactor(double d) { 73 dx = d; 74 m_distortX = dx > 0; 75 } 76 77 81 public double getYDistortionFactor() { 82 return dy; 83 } 84 85 89 public void setYDistortionFactor(double d) { 90 dy = d; 91 m_distortY = dy > 0; 92 } 93 94 97 protected double distortX(double x, Point2D anchor, Rectangle2D bounds) { 98 return fisheye(x,anchor.getX(),dx,bounds.getMinX(),bounds.getMaxX()); 99 } 100 101 104 protected double distortY(double y, Point2D anchor, Rectangle2D bounds) { 105 return fisheye(y,anchor.getY(),dy,bounds.getMinY(),bounds.getMaxY()); 106 } 107 108 111 protected double distortSize(Rectangle2D bbox, double x, double y, 112 Point2D anchor, Rectangle2D 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 } | Popular Tags |