Coverage details for edu.uci.ics.jung.visualization.transform.AffineTransformer

LineHitsSource
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  * Created on Apr 16, 2005
9  */
10  
11 package edu.uci.ics.jung.visualization.transform;
12  
13 import java.awt.Shape;
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.GeneralPath;
16 import java.awt.geom.NoninvertibleTransformException;
17 import java.awt.geom.PathIterator;
18 import java.awt.geom.Point2D;
19  
20 import edu.uci.ics.jung.visualization.transform.shape.ShapeTransformer;
21  
22 /**
23  *
24  * Provides methods to map points from one coordinate system to
25  * another, by delegating to a wrapped AffineTransform (uniform)
26  * and its inverse.
27  *
28  * @author Tom Nelson - RABA Technologies
29  */
30 public class AffineTransformer implements Transformer, ShapeTransformer {
31  
32     protected AffineTransform inverse;
33  
34     /**
35      * the AffineTransform to use. Initialize to identity
36      *
37      */
380    protected AffineTransform transform = new AffineTransform();
39     
40     /**
41      * create an instance that does not transform points
42      *
43      */
440    public AffineTransformer() {
45         // nothing left to do
460    }
47     /**
48      * Create an instance with the supplied transform
49      */
500    public AffineTransformer(AffineTransform transform) {
510        if(transform != null)
520            this.transform = transform;
530    }
54  
55     /**
56      * @return Returns the transform.
57      */
58     public AffineTransform getTransform() {
590        return transform;
60     }
61     /**
62      * @param transform The transform to set.
63      */
64     public void setTransform(AffineTransform transform) {
650        this.transform = transform;
660    }
67     
68     /**
69      * applies the inverse transform to the supplied point
70      * @param p
71      * @return
72      */
73     public Point2D inverseTransform(Point2D p) {
74  
750        return getInverse().transform(p, null);
76     }
77     
78     public AffineTransform getInverse() {
790        if(inverse == null) {
80             try {
810                inverse = transform.createInverse();
820            } catch (NoninvertibleTransformException e) {
830                e.printStackTrace();
840            }
85         }
860        return inverse;
87     }
88     
89     /**
90      * getter for scalex
91      */
92     public double getScaleX() {
930        return transform.getScaleX();
94     }
95     
96     /**
97      * getter for scaley
98      */
99     public double getScaleY() {
1000        return transform.getScaleY();
101     }
102     
103     public double getScale() {
1040            return Math.sqrt(transform.getDeterminant());
105     }
106     
107     /**
108      * getter for shear in x axis
109      */
110     public double getShearX() {
1110        return transform.getShearX();
112     }
113     
114     /**
115      * getter for shear in y axis
116      */
117     public double getShearY() {
1180        return transform.getShearY();
119     }
120     
121     /**
122      * get the translate x value
123      */
124     public double getTranslateX() {
1250        return transform.getTranslateX();
126     }
127     
128     /**
129      * get the translate y value
130      */
131     public double getTranslateY() {
1320        return transform.getTranslateY();
133     }
134     
135  
136     
137     
138     
139     /**
140      * applies the transform to the supplied point
141      */
142     public Point2D transform(Point2D p) {
1430        if(p == null) return null;
1440        return transform.transform(p, null);
145     }
146     
147     /**
148      * transform the supplied shape from graph coordinates to
149      * screen coordinates
150      * @return the GeneralPath of the transformed shape
151      */
152     public Shape transform(Shape shape) {
1530        GeneralPath newPath = new GeneralPath();
1540        float[] coords = new float[6];
1550        for(PathIterator iterator=shape.getPathIterator(null);
1560            iterator.isDone() == false;
1570            iterator.next()) {
1580            int type = iterator.currentSegment(coords);
1590            switch(type) {
160             case PathIterator.SEG_MOVETO:
1610                Point2D p = transform(new Point2D.Float(coords[0], coords[1]));
1620                newPath.moveTo((float)p.getX(), (float)p.getY());
1630                break;
164                 
165             case PathIterator.SEG_LINETO:
1660                p = transform(new Point2D.Float(coords[0], coords[1]));
1670                newPath.lineTo((float)p.getX(), (float) p.getY());
1680                break;
169                 
170             case PathIterator.SEG_QUADTO:
1710                p = transform(new Point2D.Float(coords[0], coords[1]));
1720                Point2D q = transform(new Point2D.Float(coords[2], coords[3]));
1730                newPath.quadTo((float)p.getX(), (float)p.getY(), (float)q.getX(), (float)q.getY());
1740                break;
175                 
176             case PathIterator.SEG_CUBICTO:
1770                p = transform(new Point2D.Float(coords[0], coords[1]));
1780                q = transform(new Point2D.Float(coords[2], coords[3]));
1790                Point2D r = transform(new Point2D.Float(coords[4], coords[5]));
1800                newPath.curveTo((float)p.getX(), (float)p.getY(),
181                         (float)q.getX(), (float)q.getY(),
182                         (float)r.getX(), (float)r.getY());
1830                break;
184                 
185             case PathIterator.SEG_CLOSE:
1860                newPath.closePath();
187                 break;
188                     
189             }
190         }
1910        return newPath;
192     }
193  
194     /**
195      * transform the supplied shape from graph coordinates to
196      * screen coordinates
197      * @return the GeneralPath of the transformed shape
198      */
199     public Shape inverseTransform(Shape shape) {
2000        GeneralPath newPath = new GeneralPath();
2010        float[] coords = new float[6];
2020        for(PathIterator iterator=shape.getPathIterator(null);
2030            iterator.isDone() == false;
2040            iterator.next()) {
2050            int type = iterator.currentSegment(coords);
2060            switch(type) {
207             case PathIterator.SEG_MOVETO:
2080                Point2D p = inverseTransform(new Point2D.Float(coords[0], coords[1]));
2090                newPath.moveTo((float)p.getX(), (float)p.getY());
2100                break;
211                 
212             case PathIterator.SEG_LINETO:
2130                p = inverseTransform(new Point2D.Float(coords[0], coords[1]));
2140                newPath.lineTo((float)p.getX(), (float) p.getY());
2150                break;
216                 
217             case PathIterator.SEG_QUADTO:
2180                p = inverseTransform(new Point2D.Float(coords[0], coords[1]));
2190                Point2D q = inverseTransform(new Point2D.Float(coords[2], coords[3]));
2200                newPath.quadTo((float)p.getX(), (float)p.getY(), (float)q.getX(), (float)q.getY());
2210                break;
222                 
223             case PathIterator.SEG_CUBICTO:
2240                p = inverseTransform(new Point2D.Float(coords[0], coords[1]));
2250                q = inverseTransform(new Point2D.Float(coords[2], coords[3]));
2260                Point2D r = inverseTransform(new Point2D.Float(coords[4], coords[5]));
2270                newPath.curveTo((float)p.getX(), (float)p.getY(),
228                         (float)q.getX(), (float)q.getY(),
229                         (float)r.getX(), (float)r.getY());
2300                break;
231                 
232             case PathIterator.SEG_CLOSE:
2330                newPath.closePath();
234                 break;
235                     
236             }
237         }
2380        return newPath;
239     }
240     
241     public double getRotation() {
2420        double[] unitVector = new double[]{0,0,1,0};
2430        double[] result = new double[4];
244  
2450        transform.transform(unitVector, 0, result, 0, 2);
246  
2470        double dy = Math.abs(result[3] - result[1]);
2480        double length = Point2D.distance(result[0], result[1], result[2], result[3]);
2490        double rotation = Math.asin(dy / length);
250         
2510        if (result[3] - result[1] > 0) {
2520            if (result[2] - result[0] < 0) {
2530                rotation = Math.PI - rotation;
254             }
255         } else {
2560            if (result[2] - result[0] > 0) {
2570                rotation = 2 * Math.PI - rotation;
258             } else {
2590                rotation = rotation + Math.PI;
260             }
261         }
262  
2630        return rotation;
264     }
265  
266     public String toString() {
2670        return "Transformer using "+transform;
268     }
269  
270 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.