View Javadoc

1   package org.lcsim.recon.tracking.spacegeom;
2   import java.io.*;
3   import static java.lang.Math.sqrt;
4   import static java.lang.Math.acos;
5   
6   /**  Describes a space point.
7    * The default Space point is at the origin.
8    * Derived classes can be used to set values in cartesian, cylindrical
9    * or spherical coordinates.  Point is set in the constructor and
10   * cannot be changed.  The methods simply return different coordinates.
11   * Transformations (translations, rotations, etc) are carried out by
12   * external functions which return new SpacePoints.
13   *
14   */
15  
16  public class SpacePoint implements Serializable, Cloneable
17  {
18      double _x;
19      double _y;
20      double _z;
21      double _xy;
22      double _xyz;
23      double _phi;
24      double _theta;
25      
26      /**
27       * Default constructor.
28       * Sets point to be the origin.
29       */
30      public SpacePoint()
31      {
32          _x = _y = _z = 0.0;
33          _xy = _xyz = 0.0;
34          _phi = _theta = 0.0;
35      }
36      
37      /**
38       *Copy constructor
39       *
40       * @param   spt SpacePoint to copy
41       */
42      public SpacePoint( SpacePoint spt )
43      {
44          _x = spt.x();
45          _y = spt.y();
46          _z = spt.z();
47          _xy = spt.rxy();
48          _xyz = spt.rxyz();
49          _phi = spt.phi();
50          _theta = spt.theta();
51      }
52      
53      /**
54       * Cartesian x
55       * @return double
56       */
57      public double x()
58      { 
59          return _x;
60      }
61      
62      /**
63       * Cartesian y
64       * @return double
65       */
66      public double y()
67      { 
68          return _y;
69      }
70      
71      /**
72       * Cartesian z
73       * @return double
74       */
75      
76      public double z()
77      { 
78          return _z;
79      }
80      
81      /**
82       * Cylindrical r
83       * @return double
84       */
85      public double rxy()
86      { 
87          return _xy;
88      }
89      
90      /**
91       * Cylindrical phi
92       * @return double
93       */
94      public double phi()
95      { 
96          return _phi;
97      }
98      
99      /**
100      * Spherical r
101      * @return double
102      */
103     public double rxyz()
104     { 
105         return _xyz;
106     }
107     
108     /**
109      * Spherical theta
110      * @return double
111      */
112     public double theta()
113     { 
114         return _theta;
115     }
116     
117     /**
118      * cos(phi)
119      * @return double
120      */
121     public double cosPhi()
122     {
123         if ( _xy!= 0. ) return _x/_xy;
124         return Math.cos(_phi);
125     }
126     
127     /**
128      * sin(phi)
129      * @return double
130      */
131     public double sinPhi()
132     {
133         if ( _xy != 0. ) return _y/_xy;
134         return Math.sin(_phi);
135     }
136     
137     /**
138      * sin(theta)
139      * @return double
140      */
141     public double sinTheta()
142     {
143         if ( _xyz != 0. ) return _xy/_xyz;
144         return Math.sin(_theta);
145     }
146     
147     /**
148      * cos(theta)
149      * @return double
150      */
151     public double cosTheta()
152     {
153         if ( _xyz != 0. ) return _z/_xyz;
154         return Math.cos(_theta);
155     }
156     
157     /**
158      * Output Stream
159      *
160      * @return  String representation of object
161      */
162     public String toString()
163     {
164         return  "SpacePoint: " + "\n" +
165                 "    x: " + x()     + "\n" +
166                 "    y: " + y()     + "\n" +
167                 "    z: " + z()     + "\n" +
168                 "  rxy: " + rxy()   + "\n" +
169                 " rxyz: " + rxyz()  + "\n" +
170                 "  phi: " + phi()   + "\n" +
171                 "theta: " + theta() + "\n" ;
172     }
173     
174     
175     /**
176      *Equality
177      *
178      * @param   spt SpacePoint to compare
179      * @return  true if objects are equal
180      */
181     public boolean equals(SpacePoint spt)
182     {
183         return ( x()==spt.x() ) &&
184                 ( y()==spt.y() ) &&
185                 ( z()==spt.z() );
186     }
187     
188     
189     /**
190      *Inequality
191      *
192      * @param   spt  SpacePoint to compare
193      * @return  true if objects are <em> not </em> equal
194      */
195     public boolean notEquals(SpacePoint spt)
196     {
197         return ! (equals(spt));
198     }
199     
200     /**
201      * Return the distance between two space points.
202      * @param spt1 SpacePoint 1
203      * @param spt2 SpacePoint 2
204      * @return Euclidean distance between points
205      */
206     public static double distance(SpacePoint spt1, SpacePoint spt2)
207     {
208         double dx = spt2.x() - spt1.x();
209         double dy = spt2.y() - spt1.y();
210         double dz = spt2.z() - spt1.z();
211         return Math.sqrt( dx*dx + dy*dy + dz*dz );
212     }
213     
214     /**
215      * Return the opening angle between two space points, assuming the point of reference is the origin
216      * @param spt1 SpacePoint 1
217      * @param spt2 SpacePoint 2
218      * @return opening angle between points
219      */
220     public static double openingAngle(SpacePoint p1, SpacePoint p2)
221     {
222         // should check on point being non-zero...
223         //recall that cos(theta) = a . b / |a|*|b|
224         double dot = p1.x()*p2.x()+p1.y()*p2.y()+p1.z()*p2.z();
225         
226         double d1 = sqrt(p1.x()*p1.x()+p1.y()*p1.y()+p1.z()*p1.z());
227         double d2 = sqrt(p2.x()*p2.x()+p2.y()*p2.y()+p2.z()*p2.z());
228        
229         return acos(dot/(d1*d2));
230     }    
231     
232     
233     /**
234      *Clone
235      *
236      * @return  a copy of this object
237      */
238     public Object clone()
239     {
240         Object o = null;
241         try
242         {
243             o = super.clone();
244         }
245         catch (CloneNotSupportedException e)
246         {
247             e.printStackTrace();
248         }
249         return o;
250     }
251  
252     // for legacy reasons
253     // long name to make it visible in code
254     /**
255      * for legacy code
256      * @deprecated
257      */
258     @Deprecated public double[] getCartesianArray() {
259         return new double[] {_x, _y, _z};
260     }
261 }
262