View Javadoc

1   // Copyright 2004, FreeHEP.
2   package hep.wired.viewport;
3   
4   import java.awt.geom.*;
5   
6   import org.jdom.DataConversionException;
7   
8   import org.freehep.xml.io.XMLIO;
9   import org.freehep.xml.io.XMLIOManager;
10  
11  import hep.wired.services.ViewPort;
12  
13  /***
14   * Defines a Rectangular Viewport using x0, y0, width and height.
15   *
16   * Applies the viewport functions:
17   * <PRE>
18   *      u = (x + 1) * width / 2 + x0;
19   *      v = height - (y + 1) * height / 2 + y0;
20   *      w = (z + 1) * width / 2 + x0;   // NOTE: this is done to be able to calculate bounding boxes in Z.
21   * </PRE>
22   * given rectangle(x0, y0, width, height).
23   *
24   * This projects a viewport of ([-1,1],[-1,1]) onto ([x0,y0+height],[x0+width,y0]).
25   *
26   * @author Mark Donszelmann
27   * @version $Id: RectangularViewPort.java 561 2004-12-07 19:23:50Z duns $
28   */
29  
30  public class RectangularViewPort extends Rectangle2D.Double implements ViewPort {
31  
32      private int componentWidth, componentHeight;
33  
34      /***
35       * Creates a rectangular viewport of (0, 0, 800, 600).
36       */
37      public RectangularViewPort() {
38          this(0, 0, 800, 600, 800, 600);
39      }
40  
41      /***
42       * Creates a rectangular viewport from given rectangle.
43       */
44      public RectangularViewPort(Rectangle2D r, int componentWidth, int componentHeight) {
45          super(r.getX(), r.getY(), r.getWidth(), r.getWidth());
46          init(componentWidth, componentHeight);
47      }
48      
49      /***
50       * Creates a rectangular viewport from given parameters.
51       */
52      public RectangularViewPort(double x, double y, double width, double height, int componentWidth, int componentHeight) {
53          super(x, y, width, height);
54          init(componentWidth, componentHeight);        
55      }
56      
57      private void init(int componentWidth, int componentHeight) {
58          this.componentWidth = componentWidth;
59          this.componentHeight = componentHeight;
60      }
61      
62      public int getComponentWidth() {
63          return componentWidth;
64      }
65      
66      public int getComponentHeight() {
67          return componentHeight;
68      }
69  
70      public String getID() {
71          return getName();
72      }
73      
74      public String getName() {
75          return "RectangularViewPort";
76      }        
77  
78      public ViewPort copy() {
79          return new RectangularViewPort(this, getComponentWidth(), getComponentHeight());
80      }
81      
82      public double[] transform(double[] xyz) {
83          xyz[U] = (xyz[X]+1) * getWidth() / 2.0 + getX();
84          xyz[V] = getHeight() - (xyz[Y]+1) * getHeight() / 2.0 + getY();
85          xyz[W] = (xyz[Z]+1) * getWidth() / 2.0 + getX();
86          return xyz;
87      }
88      
89      public double[][] transform(double[][] xyz, int n) {
90          for (int i=0; i<n; i++) {
91              xyz[U][i] = (xyz[X][i]+1) * getWidth() / 2.0 + getX();
92              xyz[V][i] = getHeight() - (xyz[Y][i]+1) * getHeight() / 2.0 + getY();
93              xyz[W][i] = (xyz[Z][i]+1) * getWidth() / 2.0 + getX();
94          }
95          return xyz;
96      }
97  
98      public double[] deltaTransform(double[] xyz) {
99          xyz[U] *= getWidth() / 2.0;  
100         xyz[V] *= getHeight() / 2.0;       
101         xyz[W] *= getWidth() / 2.0;  
102         return xyz;
103     }
104     
105     public double[] inverseTransform(double[] uvw) {
106         uvw[X] = 2*(uvw[U]-getX()) / getWidth() - 1;
107         uvw[Y] = 2*(getY()-uvw[V]) / getHeight() + 1;
108         uvw[Z] = 2*(uvw[W]-getX()) / getWidth() - 1;
109         return uvw;
110     }
111         
112     public double[] inverseDeltaTransform(double[] uvw) {
113         uvw[X] = 2*uvw[U] / getWidth();
114         uvw[Y] = 2*uvw[V] / getHeight();
115         uvw[Z] = 2*uvw[W] / getWidth();
116         return uvw;
117     }
118         
119     public String toString() {
120         return getClass()+": "+super.toString();
121     }
122 
123 //
124 // XMLIO
125 //
126     public void save(XMLIOManager xmlioManager,
127                      org.jdom.Element nodeEl) {
128         nodeEl.setAttribute("x0", java.lang.Double.toString(getX()));
129         nodeEl.setAttribute("y0", java.lang.Double.toString(getY()));
130         nodeEl.setAttribute("width", java.lang.Double.toString(getWidth()));
131         nodeEl.setAttribute("height", java.lang.Double.toString(getHeight()));
132         nodeEl.setAttribute("componentWidth", Integer.toString(getComponentWidth()));
133         nodeEl.setAttribute("componentHeight", Integer.toString(getComponentHeight()));
134     }
135 
136     public void restore(XMLIOManager xmlioManager,
137                         org.jdom.Element nodeEl) {
138         try {
139             setRect(nodeEl.getAttribute("x0").getDoubleValue(),
140                     nodeEl.getAttribute("y0").getDoubleValue(),
141                     nodeEl.getAttribute("width").getDoubleValue(),
142                     nodeEl.getAttribute("height").getDoubleValue()
143             );
144             componentWidth = nodeEl.getAttribute("componentWidth").getIntValue();
145             componentHeight = nodeEl.getAttribute("componentHeight").getIntValue();
146         } catch (DataConversionException dce) {
147             throw new IllegalArgumentException(getClass()+": "+dce.getMessage());
148         }
149     }
150 }