1 package org.lcsim.detector.solids;
2
3 import static java.lang.Math.PI;
4 import static java.lang.Math.cos;
5
6 import hep.physics.vec.Hep3Vector;
7 import java.text.DecimalFormat;
8 import java.text.NumberFormat;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class RightRegularPolyhedron extends AbstractSolid
25 {
26
27
28 private int _nsides;
29 private double _rmin;
30 private double _rmax;
31 private double _zmin;
32 private double _zmax;
33
34 private RegularPolygon _innerPolygon = null;
35 private RegularPolygon _outerPolygon = null;
36 private boolean _isHollow;
37
38
39
40
41
42
43
44
45
46
47
48 public RightRegularPolyhedron(String name, int nsides, double rmin, double rmax, double zmin, double zmax)
49 {
50 super(name);
51 if (nsides < 3)
52 throw new RuntimeException("Cannot make a polyhedron with less than three sides!");
53 _nsides = nsides;
54 if (rmin >= rmax)
55 throw new RuntimeException("Inner radius must be inside outer radius!");
56 _rmin = rmin;
57 _rmax = rmax;
58 if (zmin >= zmax)
59 throw new RuntimeException("Minimum z must be less than maximum z!");
60 _zmin = zmin;
61 _zmax = zmax;
62
63
64 double dPhi = 2. * PI / ((double) _nsides);
65 double r = _rmin / cos(dPhi / 2.);
66 _innerPolygon = new RegularPolygon(_nsides, r);
67 _outerPolygon = new RegularPolygon(_nsides, _rmax);
68 _isHollow = true;
69 }
70
71
72
73
74
75
76
77
78
79
80 public RightRegularPolyhedron(String name, int nsides, double radius, double zmin, double zmax)
81 {
82 super(name);
83 if (nsides < 3)
84 throw new RuntimeException("Cannot make a polyhedron with less than three sides!");
85 _nsides = nsides;
86 _rmax = radius;
87 if (zmin >= zmax)
88 throw new RuntimeException("Minimum z must be less than maximum z!");
89 _zmin = zmin;
90 _zmax = zmax;
91 _outerPolygon = new RegularPolygon(_nsides, _rmax);
92 _isHollow = false;
93 }
94
95 public double getCubicVolume()
96 {
97 return volume();
98 }
99
100
101
102
103
104
105
106 public Inside inside(Hep3Vector pos)
107 {
108
109 double z = pos.z();
110 if (z < _zmin || z > _zmax)
111 {
112 return Inside.OUTSIDE;
113 }
114
115 double x = pos.x();
116 double y = pos.y();
117 double r2 = x * x + y * y;
118 if (r2 > _rmax * _rmax)
119 return Inside.OUTSIDE;
120 if (r2 < _rmin * _rmin)
121 return Inside.OUTSIDE;
122
123
124 Inside outer = _outerPolygon.inside(pos);
125 if (outer.compareTo(Inside.OUTSIDE) == 0)
126 return Inside.OUTSIDE;
127
128
129 if (_isHollow)
130 {
131 Inside inner = _innerPolygon.inside(pos);
132 if (inner.compareTo(Inside.INSIDE) == 0)
133 return Inside.OUTSIDE;
134 }
135 return Inside.INSIDE;
136 }
137
138
139
140
141
142 public double polygonalArea()
143 {
144 double area = _outerPolygon.area();
145 if (_isHollow)
146 area -= _innerPolygon.area();
147 return area;
148 }
149
150
151
152
153
154 public boolean isHollow()
155 {
156 return _isHollow;
157 }
158
159
160
161
162
163 public double volume()
164 {
165 return polygonalArea() * (_zmax - _zmin);
166 }
167
168 @Override
169 public String toString()
170 {
171 NumberFormat formatter = new DecimalFormat("#0.00");
172
173 StringBuffer sb = new StringBuffer("RightRegularAnnularPolyhedron\n");
174 sb.append(" with " + _nsides + " sides from r= " + _rmin + " to " + _rmax + ": \n");
175 sb.append(_outerPolygon.toString() + "\n");
176 if (_isHollow)
177 sb.append(_innerPolygon.toString() + "\n");
178 sb.append("volume= " + volume() + "\n");
179 return sb.toString();
180 }
181
182
183
184
185
186 public int getNumberOfSides()
187 {
188 return _nsides;
189 }
190
191
192
193
194
195 public double getZMin()
196 {
197 return _zmin;
198 }
199
200
201
202
203
204 public double getZMax()
205 {
206 return _zmax;
207 }
208
209
210
211
212
213
214 public double getRMin()
215 {
216 return _rmin;
217 }
218
219
220
221
222
223 public double getRMax()
224 {
225 return _rmax;
226 }
227
228
229
230
231
232
233 public RegularPolygon getInnerPolygon()
234 {
235 return _innerPolygon;
236 }
237
238
239
240
241
242
243 public RegularPolygon getOuterPolygon()
244 {
245 return _outerPolygon;
246 }
247
248 private double t(Hep3Vector a, Hep3Vector b, Hep3Vector c)
249 {
250 double t = (a.y() - b.y()) * (c.x() - b.x()) - (a.x() - b.x()) * (c.y() - b.y());
251 return t;
252 }
253 }