View Javadoc

1   // Copyright 2004, FreeHEP.
2   package hep.wired.heprep.projection;
3   
4   import java.util.*;
5   
6   import org.jdom.Element;
7   
8   import org.freehep.xml.io.XMLIO;
9   import org.freehep.xml.io.XMLIOManager;
10  
11  import hep.wired.services.Feature;
12  import hep.wired.services.ViewPort;
13  import hep.wired.feature.Resetable;
14  import hep.wired.heprep.services.Projection;
15  
16  /***
17   * Sequence of projections.
18   *
19   * @author Mark Donszelmann
20   * @version $Id: CompositeProjection.java 645 2005-02-27 02:23:41Z duns $
21   */
22  
23  public class CompositeProjection extends AbstractProjection implements Resetable {
24  
25      // used array for speed of projections
26      private Projection[] projections = new Projection[5];
27      private int noOfProjections = 0;
28  
29      /***
30       * Creates a composite projection. A transform called on this
31       * projection will use all added projections to transform x,y,z into u,v,w.
32       */
33      public CompositeProjection() {
34          this(null);
35      }
36  
37      /***
38       * Creates a named empty composite projection. A transform called on this
39       * projection will use all added projections to transform x,y,z into u,v,w.
40       */
41      public CompositeProjection(String name) {
42          super(name);
43      }
44  
45      /***
46       * Creates a named composite projection filled with the given array of
47       * projections. A transform called on this projection will use those 
48       * projections to transform x,y,z into u,v,w.
49       */
50      public CompositeProjection(String name, Projection[] projections) {
51          super(name);
52          this.noOfProjections = projections.length;
53          this.projections = new Projection[noOfProjections];
54          System.arraycopy(projections, 0, this.projections, 0, noOfProjections);
55      }
56  
57      public String getFormula() {
58          return "(u,v,w) = Composite"+getName()+"(x,y,z)";   
59      }
60   
61      public boolean supports(Class featureClass) {
62          for (int i=0; i<noOfProjections; i++) {
63              if (projections[i].supports(featureClass)) return true;
64          }
65          return false;
66      }
67      
68      public Feature getFeature(Class featureClass) {
69          for (int i=0; i<noOfProjections; i++) {
70              Feature f = projections[i].getFeature(featureClass);
71              if (f != null) return f;
72          } 
73          return null;
74      }
75  
76      public void setViewPort(ViewPort viewPort) {
77          super.setViewPort(viewPort);
78          for (int i=0; i<noOfProjections; i++) {
79              projections[i].setViewPort(viewPort);
80          }
81      }
82  
83      /***
84       * Adds a projection to the end of the list.
85       */
86      public void add(Projection p) {
87          if (noOfProjections >= projections.length) {
88              Projection[] old = projections;
89              projections = new Projection[projections.length + 5];
90              System.arraycopy(old, 0, projections, 0, projections.length);   
91          }
92          projections[noOfProjections] = p;
93          noOfProjections++;
94      }
95      
96      // FIXME WIRED-256 we may need move and insert...
97      /***
98       * Returns the list of projections.
99       */
100     public List/*<Projection>*/ getProjections() {
101         List list = new LinkedList();
102         for (int i=0; i<noOfProjections; i++) {
103             list.add(projections[i]);
104         }
105         return list;
106     }
107     
108     public double[] transform(double[] xyz) {
109         for (int i=0; i<noOfProjections; i++) {
110             xyz = projections[i].transform(xyz);
111         } 
112         return xyz;
113     }    
114 
115     public double[][] transform(double[][] xyz, int n) {
116         for (int i=0; i<noOfProjections; i++) {
117             xyz = projections[i].transform(xyz, n);
118         } 
119         return xyz;
120     }
121 
122     public double[] deltaTransform(double[] xyz) {
123         for (int i=0; i<noOfProjections; i++) {
124             xyz = projections[i].deltaTransform(xyz);
125         } 
126         return xyz;
127     }    
128 
129     public double[] inverseTransform(double[] uvw) throws UnsupportedOperationException {
130         for (int i=noOfProjections-1; i>=0; i--) {
131             uvw = projections[i].inverseTransform(uvw);
132         } 
133         return uvw;        
134     }
135 
136     public double[] inverseDeltaTransform(double[] uvw) throws UnsupportedOperationException {
137         for (int i=noOfProjections-1; i>=0; i--) {
138             uvw = projections[i].inverseDeltaTransform(uvw);
139         } 
140         return uvw;        
141     }
142 
143     public Projection copy() {
144         CompositeProjection cp = new CompositeProjection(getName());
145         for (int i=0; i<noOfProjections; i++) {
146             cp.add(projections[i].copy());
147         }
148         return cp;
149     }
150     
151 // Resetable
152     public Object reset(Object newStates) {
153         Object[] oldStates = new Object[noOfProjections];
154         Object[] array = (Object[])newStates;
155         
156         for (int i=0; i<noOfProjections; i++) {
157             Object state = (array != null) ? array[i] : null;
158             if (projections[i] instanceof Resetable) {
159                 oldStates[i] = ((Resetable)projections[i]).reset(state);
160             }
161         }
162         return oldStates;
163     }    
164 
165 
166 //
167 // XMLIO
168 //
169     public void save(XMLIOManager xmlioManager,
170                      org.jdom.Element nodeEl) {
171         super.save(xmlioManager, nodeEl);
172         for (int i=0; i<noOfProjections; i++) {
173             nodeEl.addContent(xmlioManager.save(projections[i]));
174         }
175     }
176 
177     public void restore(XMLIOManager xmlioManager,
178                         org.jdom.Element nodeEl) {
179         super.restore(xmlioManager, nodeEl);
180         for (Iterator i=nodeEl.getChildren().iterator(); i.hasNext(); ) {
181             Element element = (Element)i.next();
182             add((Projection)xmlioManager.restore(element));
183         }
184     }     
185 }
186