View Javadoc

1   package org.lcsim.recon.tracking.spacegeom;
2   
3   /** A point in 2-space. The default Space point is at the origin.
4    * Derived classes can be used to set values in cartesian or cylindrical
5    * coordinates.  Point is set in the constructor and cannot be changed.
6    * The methods simply return different coordinates.
7    * Transformations (translations, rotations, etc) are carried out by
8    * external functions which return new TwoSpacePoints.
9    *@author Norman A. Graf
10   *@version 1.0
11   */
12  public class TwoSpacePoint
13  {
14      
15      // data
16      protected double _x;
17      protected double _y;
18      protected double _xy;
19      protected double _phi;
20      
21      // static methods
22      
23      // Return if two doubles are close enough to be considered equal.
24      // This is used to compare components for operator==.
25      private static boolean equal(double x1, double x2)
26      {
27          //  	  double maxdif = 2.0*std::numeric_limits<double>::epsilon();
28          double maxdif = 5.0e-16;
29          double num = 2.0*(x2-x1);
30          double den = x2 + x1;
31          double dif = Math.abs(num/den);
32          return dif <= maxdif;
33      }
34      
35      // methods
36      
37      /** Default constructor.
38       * Sets point to be the origin with phi =  0.
39       */
40      public TwoSpacePoint( )
41      {
42          _x = _y = 0.0;
43          _xy = 0.0;
44          _phi = 0.0;
45      }
46      
47      // Cartesian x.
48      /**
49       * @return Cartesian x coordinate
50       */
51      public double x( )
52      {
53          return _x;
54      }
55      
56      // Cartesian y.
57      /**
58       * @return Cartesian y coordinate
59       */
60      public double y( )
61      {
62          return _y;
63      }
64      
65      // Cylindrical r.
66      /**
67       * @return  Cylindrical radius coordinate
68       */
69      public double rxy( )
70      {
71          return _xy;
72      }
73      
74      // Cylindrical phi.
75      /**
76       * @return Cylindrical phi coordinate
77       */
78      public double phi( )
79      {
80          return _phi;
81      }
82      
83      // cos(phi)
84      /**
85       * @return Cosine of Cylindrical phi coordinate
86       */
87      public double cosPhi( )
88      {
89          if ( _xy != 0. ) return _x/_xy;
90          return Math.cos(_phi);
91      }
92      
93      // sin(phi)
94      /**
95       * @return Sine of Cylindrical phi coordinate
96       */
97      public double sinPhi( )
98      {
99          if ( _xy != 0. ) return _y/_xy;
100         return Math.sin(_phi);
101     }
102     
103     /**
104      * @return String representation of this class
105      */
106     public String toString()
107     {
108         return "TwoSpacePoint: \n    x: " + x() + " \n"
109                 + "    y: " + y() + "\n"
110                 + "  rxy: " + rxy() +  "\n"
111                 + "  phi: " + phi();
112     }
113     
114     /** Checks object equality
115      * @param tsp  TwoSpacePoint to compare with for equality
116      * @return true of this TwoSpacePoint equals tsp
117      */
118     public boolean equals( TwoSpacePoint tsp )
119     {
120         return ( equal(x(),tsp.x()) && equal(y(),tsp.y()) );
121     }
122     
123     /** Checks object inequality
124      * @param tsp TwoSpacePoint to compare with for inequality
125      * @return true of this TwoSpacePoint does not equal tsp
126      */
127     public boolean notEquals( TwoSpacePoint tsp )
128     {
129         return !equals(tsp);
130     }
131     
132     // Return the distance between two space points.
133     /**
134      * @param tsp1 first TwoSpacePoint
135      * @param tsp2 second TwoSpacePoint
136      * @return Two diensional Euclidean distance between points
137      */
138     public static double distance(TwoSpacePoint tsp1, TwoSpacePoint tsp2 )
139     {
140         double dx = tsp2.x() - tsp1.x();
141         double dy = tsp2.y() - tsp1.y();
142         return Math.sqrt( dx*dx + dy*dy );
143     }
144     
145 }