1 package org.lcsim.detector.solids;
2
3 import hep.physics.vec.BasicHep3Vector;
4 import java.util.Random;
5 import junit.framework.TestCase;
6 import static java.lang.Math.abs;
7 import static java.lang.Math.PI;
8
9
10
11
12
13
14
15 public class RightRegularPolyhedronTest extends TestCase
16 {
17
18 private boolean debug = false;
19
20 public RightRegularPolyhedronTest(String testName) {
21 super(testName);
22 }
23
24 public void testRightRegularPolyhedron()
25 {
26 String name = "testSolid";
27 for (int nsides = 4; nsides < 20; ++nsides)
28 {
29 double r1 = 1.0;
30 double r2 = 2.0;
31 double z1 = -1.;
32 double z2 = 1.0;
33
34
35
36
37
38
39 RightRegularPolyhedron p = new RightRegularPolyhedron(name, nsides, r1, r2, z1, z2);
40
41 if(debug) System.out.println(p);
42
43
44
45
46
47
48 double sphereVolume = 4 * PI * r2 * r2 * r2 / 3.;
49 double polyVolume = p.volume();
50
51 int nmax = 1000000;
52 int nIn = 0;
53 Random ran = new Random();
54 int i = 0;
55 while (i < nmax)
56 {
57
58 double x = r2 * (2. * ran.nextDouble() - 1.);
59 double y = r2 * (2. * ran.nextDouble() - 1.);
60 double z = r2 * (2. * ran.nextDouble() - 1.);
61
62 if ((x * x + y * y + z * z) < r2 * r2)
63 {
64 ++i;
65 BasicHep3Vector pos = new BasicHep3Vector(x, y, z);
66 Inside in = p.inside(pos);
67 if (Inside.INSIDE.compareTo(in) == 0)
68 ++nIn;
69 }
70 }
71 double meas = (double) nIn / nmax;
72 double pred = polyVolume / sphereVolume;
73 if (debug)
74 System.out.println("nmax= " + nmax + " nIn= " + nIn + " ratio= " + meas);
75 if (debug)
76 System.out.println("volume ratio= " + pred);
77 double err = (meas - pred) / pred;
78 if (debug)
79 System.out.println("(meas-pred)/pred= " + err);
80
81 assertTrue(abs(err) < .1);
82
83 }
84 }
85 }