View Javadoc

1   package org.lcsim.detector.solids;
2   
3   import hep.physics.vec.BasicHep3Vector;
4   import java.util.Random;
5   import junit.framework.TestCase;
6   
7   import static java.lang.Math.abs;
8   import static java.lang.Math.PI;
9   import static java.lang.Math.atan;
10  
11  /**
12   *
13   * @author Norman A. Graf
14   *
15   * @version $Id:
16   */
17  public class IsoscelesTrapezoidTest extends TestCase
18  {
19  
20      private boolean debug = false;
21  
22      public IsoscelesTrapezoidTest(String testName)
23      {
24          super(testName);
25      }
26  
27      public void testIsoscelesTrapezoid()
28      {
29          // start with a box
30          double b = 1.0;
31          double t = 1.0;
32          double height = 1.0;
33          IsoscelesTrapezoid trap1 = new IsoscelesTrapezoid(b, t, height);
34  
35          if (debug)
36              System.out.println(trap1);
37  
38          assertEquals(trap1.area(), 4.0);
39          
40          if (debug)System.out.println(trap1.baseAngle());
41          if (debug)System.out.println(trap1.topAngle());
42          assertEquals(trap1.baseAngle(), trap1.topAngle());
43          assertEquals(trap1.baseAngle(), PI/2);
44          // coverage test
45          // generate random points in the circumscribed circle
46          // ratio of inside to outside should equal the ratio of areas
47          //
48          double radius = 10;
49          double circleArea = PI * radius * radius;
50          int nmax = 1000000;
51          int nIn = 0;
52          Random rr = new Random();
53          // test a common range of polygons...
54  
55          int i = 0;
56          nIn = 0;
57          while (i < nmax)
58          {
59              double x = radius * (2. * rr.nextDouble() - 1.);
60              double y = radius * (2. * rr.nextDouble() - 1.);
61              if ((x * x + y * y) < radius * radius)
62              {
63                  ++i;
64                  BasicHep3Vector pos = new BasicHep3Vector(x, y, 0.);
65                  Inside in = trap1.inside(pos);
66                  if (Inside.INSIDE.compareTo(in) == 0)
67                      ++nIn;
68              }
69          }
70          double meas = (double) nIn / nmax;
71          double pred = trap1.area() / circleArea;
72          if (debug)
73              System.out.println("nmax= " + nmax + " nIn= " + nIn + " ratio= " + meas);
74          if (debug)
75              System.out.println("area ratio= " + pred);
76          double err = (meas - pred) / pred;
77          if (debug)
78              System.out.println("(meas-pred)/pred= " + err);
79          // 5% uncertainty
80          assertTrue(abs(err) < .05);
81  
82          // now test a non-rectangular trapezoid...
83          b = 1.0;
84          t = 2.0;
85          height = 1.0;
86          IsoscelesTrapezoid trap2 = new IsoscelesTrapezoid(b, t, height);
87  
88          assertEquals(trap2.baseAngle(), (PI/2+atan(.5)));
89          assertEquals(PI, trap2.baseAngle()+trap2.topAngle());
90  
91      }
92  }