1
2 package hep.wired.heprep.projection;
3
4 import java.lang.reflect.*;
5 import java.util.*;
6
7 import org.freehep.xml.io.XMLIO;
8 import org.freehep.xml.io.XMLIOManager;
9
10 import hep.wired.services.Feature;
11 import hep.wired.services.ViewPort;
12 import hep.wired.heprep.services.Projection;
13
14 /***
15 * Base class for projections.
16 *
17 * @author Mark Donszelmann
18 * @version $Id: AbstractProjection.java 1901 2005-06-14 06:42:38Z duns $
19 */
20
21 public abstract class AbstractProjection implements Projection {
22
23
24 public static final String defaultProjections = "ParallelProjection.XY:ParallelProjection.ZX:ParallelProjection.ZY:ParallelProjection.XZ:ParallelProjection.YZ:FlatProjection:Rho-Z:YX-FishEye:Rho-Z-FishEye:YX-Clock";
25
26 private String name;
27 private String id;
28
29 private transient double[] uvw = new double[3];
30
31 private transient ViewPort viewPort;
32
33 /***
34 * Creates a projection with the classname as name.
35 */
36 public AbstractProjection() {
37 this(null);
38 }
39
40 /***
41 * Creates a named projection.
42 */
43 public AbstractProjection(String name) {
44 if (name == null) {
45 String className = getClass().getName();
46 int dot = className.lastIndexOf(".");
47 className = dot < 0 ? className : className.substring(dot+1);
48 this.id = className;
49 this.name = className.replace('$','-');
50 } else {
51 this.name = name;
52 this.id = name;
53 }
54 }
55
56 public String getID() {
57 return id;
58 }
59
60 public String getName() {
61 return name;
62 }
63
64 public String getFormula() {
65 return "(u,v,w) = "+getName()+"(x,y,z)";
66 }
67
68 public boolean supports(Class featureClass) {
69 return getFeature(featureClass) != null;
70 }
71
72 /***
73 * Returns feature if supported by this projection.
74 */
75 public Feature getFeature(Class featureClass) {
76 return (featureClass.isAssignableFrom(this.getClass())) ? (Feature)this : null;
77 }
78
79 public void setViewPort(ViewPort viewPort) {
80 this.viewPort = viewPort;
81 }
82
83 public ViewPort getViewPort() {
84 return viewPort;
85 }
86
87 /***
88 * Implements this method by
89 * a call to transform([]), followed by the viewPort.
90 */
91 public double[] transform(ViewPort viewPort, double[] xyz) {
92 xyz = transform(xyz);
93 xyz = viewPort.transform(xyz);
94 return xyz;
95 }
96
97 public abstract double[] transform(double[] xyz);
98
99 /***
100 * Implements this method by
101 * a call to transform([][], n), followed by running all points through the viewPort.
102 * Concrete projections should probably override this method with something more efficient.
103 */
104 public double[][] transform(ViewPort viewPort, double[][] xyz, int n) {
105 xyz = transform(xyz, n);
106 xyz = viewPort.transform(xyz, n);
107 return xyz;
108 }
109
110 /***
111 * Implements this method in terms of double[] transform(double[] xyz).
112 * Not a very efficient implementation, so concrete projections should
113 * probably override this method with something better.
114 */
115 public double[][] transform(double[][] xyz, int n) {
116 for (int i=0; i<n; i++) {
117 uvw[U] = xyz[X][i];
118 uvw[V] = xyz[Y][i];
119 uvw[W] = xyz[Z][i];
120 uvw = transform(uvw);
121 xyz[U][i] = uvw[U];
122 xyz[V][i] = uvw[V];
123 xyz[W][i] = uvw[W];
124 }
125 return xyz;
126 }
127
128 public abstract double[] deltaTransform(double[] xyz);
129 public abstract double[] inverseTransform(double[] uvw) throws UnsupportedOperationException;
130 public abstract double[] inverseDeltaTransform(double[] uvw) throws UnsupportedOperationException;
131 public abstract Projection copy();
132
133 /***
134 * Returns name of projection
135 */
136 public String toString() {
137 return getName();
138 }
139
140
141
142
143 public void save(XMLIOManager xmlioManager,
144 org.jdom.Element nodeEl) {
145 nodeEl.setAttribute("name", getName());
146 }
147
148 public void restore(XMLIOManager xmlioManager,
149 org.jdom.Element nodeEl) {
150 name = nodeEl.getAttributeValue("name");
151 }
152 }