View Javadoc

1   package org.lcsim.detector.solids;
2   
3   import hep.physics.vec.BasicHep3Vector;
4   import hep.physics.vec.Hep3Vector;
5   
6   import java.text.DecimalFormat;
7   import java.text.NumberFormat;
8   
9   import static java.lang.Math.abs;
10  import static java.lang.Math.sqrt;
11  import static java.lang.Math.acos;
12  import static java.lang.Math.atan;
13  import static java.lang.Math.PI;
14  
15  /**
16   * This class represents an isosceles trapezoid
17   * The trapezoid has a flat base at the bottom.
18   * Also known as a keystone.
19   *
20   *           top
21   *    __________________       ^
22   *    \                /       |
23   *     \              /        |
24   *      \            /      Y  |
25   *       \          /          |
26   *        \________/           +------------->
27   *                                    X
28   *          bottom
29   *
30   *
31   * @author Norman A. Graf
32   *
33   * @version $Id: IsoscelesTrapezoid.java,v 1.2 2010/04/27 22:42:18 ngraf Exp $
34   */
35  public class IsoscelesTrapezoid
36  {
37  
38      private double _bottomHalf;
39      private double _topHalf;
40      private double _heightHalf;
41      private Hep3Vector[] _vertices = new Hep3Vector[4];
42      private double _area;
43      private double _baseAngle;
44      private double _topAngle;
45  
46      /**
47       * Fully qualified constructor
48       * @param bottomHalf the half-width of the base dimension
49       * @param topHalf the half-width of the top dimension
50       * @param heightHalf the half-height
51       */
52      public IsoscelesTrapezoid(double bottomHalf, double topHalf, double heightHalf)
53      {
54          _bottomHalf = bottomHalf;
55          _topHalf = topHalf;
56          _heightHalf = heightHalf;
57          _area = 2. * (_bottomHalf + _topHalf) * _heightHalf;
58          // counterclockwise starting at lower left corner
59          _vertices[0] = new BasicHep3Vector(-_bottomHalf, -_heightHalf, 0.);
60          _vertices[1] = new BasicHep3Vector(_bottomHalf, -_heightHalf, 0.);
61          _vertices[2] = new BasicHep3Vector(_topHalf, _heightHalf, 0.);
62          _vertices[3] = new BasicHep3Vector(-_topHalf, _heightHalf, 0.);
63  
64          double delta = atan((_topHalf - _bottomHalf) / (2. * _heightHalf));
65          _baseAngle = (_topHalf >_bottomHalf) ? PI / 2. + delta : PI/2.-delta;
66          _topAngle = PI-_baseAngle;
67  
68      }
69  
70      /**
71       * Return the coordinates of the trapezoid vertices.
72       * Convention is that the trapezoid has a flat on the bottom.
73       * First vertex is the beginning of this flat, and proceeds
74       * counter-clockwise.
75       * The z coordinate is always zero for these vertices.
76       * @return The list of vertices in the XY plane.
77       */
78      public Hep3Vector[] getVertices()
79      {
80          return _vertices;
81      }
82  
83      /**
84       * The area of this trapezoid
85       * @return the area of this trapezoid.
86       */
87      public double area()
88      {
89          return _area;
90      }
91  
92      /**
93       * Determines whether a point lies within or outside of this trapezoid.
94       *
95       * @param pos the point to check
96       * @return an enumeration of INSIDE or OUTSIDE ( SURFACE is reported as INSIDE )
97       */
98      public Inside inside(Hep3Vector pos)
99      {
100         //fail fast
101         double y = pos.y();
102         if (abs(y) > _heightHalf)
103             return Inside.OUTSIDE;
104 
105         // now the sides...
106         if (t(pos, _vertices[1], _vertices[2]) < 0)
107             return Inside.OUTSIDE;
108         if (t(pos, _vertices[3], _vertices[0]) < 0)
109             return Inside.OUTSIDE;
110 
111         return Inside.INSIDE;
112     }
113 
114     @Override
115     public String toString()
116     {
117         NumberFormat formatter = new DecimalFormat("#0.0000");
118 
119         StringBuffer sb = new StringBuffer("IsoscelesTrapezoid\n");
120         sb.append(" base " + 2. * _bottomHalf + " top " + 2. * _topHalf + " height " + 2. * _heightHalf + " \n");
121         sb.append(" area " + _area + " \n");
122         return sb.toString();
123     }
124 
125     public double baseAngle()
126     {
127         return _baseAngle;
128     }
129 
130     public double topAngle()
131     {
132         return _topAngle;
133     }
134 
135     private double t(Hep3Vector a, Hep3Vector b, Hep3Vector c)
136     {
137         double t = (a.y() - b.y()) * (c.x() - b.x()) - (a.x() - b.x()) * (c.y() - b.y());
138         return t;
139     }
140     
141 }