source: trunk/source/geometry/solids/CSG/test/testSurfaceInOut.cc @ 1316

Last change on this file since 1316 was 1316, checked in by garnier, 14 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 34.1 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// Test File for tracking functions on a solid surface
28//
29// o Basic asserts on each function +
30//   awkward cases for tracking / geom algorithms
31//
32// o Add tests on dicovering bugs in G4Sphere.cc...
33//
34// History:
35//
36// 11.11.04 V.Grichine creation for box, tubs, cons, sphere, orb, torus
37// 07.06.05 J.Apostolakis:  revised to test each solid in turn,  use argc/argv
38
39// Notes:  (J.A. June 2005)
40//  - To call it use :  testSurfaceNormal  [no_points] [no_directions_each]
41//  - Old 'main' is now a test function,  new main loops over each solid type
42
43#include "G4ios.hh"
44#include <assert.h>
45#include <cmath>
46#include "globals.hh"
47#include "geomdefs.hh"
48#include "Randomize.hh"
49#include "G4GeometryTolerance.hh"
50
51#include "ApproxEqual.hh"
52
53#include "G4ThreeVector.hh"
54#include "G4RotationMatrix.hh"
55#include "G4AffineTransform.hh"
56#include "G4VoxelLimits.hh"
57
58#include "G4Box.hh"
59#include "G4Orb.hh"
60#include "G4Tubs.hh"
61#include "G4Sphere.hh"
62#include "G4Cons.hh"
63#include "G4Hype.hh"
64#include "G4Para.hh"
65#include "G4Torus.hh"
66#include "G4Trd.hh"
67
68
69
70///////////////////////////////////////////////////////////////////////////
71//
72//
73
74
75//const G4double kApproxEqualTolerance = kCarTolerance;
76
77// Return true if the double check is approximately equal to target
78//
79// Process:
80//
81// Return true is difference < kApproxEqualTolerance
82
83//G4bool ApproxEqual(const G4double check,const G4double target)
84//{
85//    return (std::fabs(check-target)<kApproxEqualTolerance) ? true : false ;
86//}
87
88// Return true if the 3vector check is approximately equal to target
89//G4bool ApproxEqual(const G4ThreeVector& check, const G4ThreeVector& target)
90//{
91//    return (ApproxEqual(check.x(),target.x())&&
92//         ApproxEqual(check.y(),target.y())&&
93//          ApproxEqual(check.z(),target.z()))? true : false;
94//}
95
96
97
98
99///////////////////////////////////////////////////////////////////
100//
101// Dave's auxiliary function
102
103const G4String OutputInside(const EInside a)
104{
105        switch(a) 
106        {
107                case kInside:  return "Inside"; 
108                case kOutside: return "Outside";
109                case kSurface: return "Surface";
110        }
111        return "????";
112}
113
114
115/////////////////////////////////////////////////////////////////////////////
116//
117// Random unit direction vector
118
119G4ThreeVector GetRandomUnitVector()
120{
121  G4double cosTheta, sinTheta, phi, vx, vy, vz;
122
123  cosTheta = -1. + 2.*G4UniformRand();
124  if( cosTheta > 1.)  cosTheta = 1.;
125  if( cosTheta < -1.) cosTheta = -1.;
126  sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
127 
128  phi = 2*pi*G4UniformRand();
129
130  vx = sinTheta*std::cos(phi);
131  vy = sinTheta*std::sin(phi);
132  vz = cosTheta;
133
134  return G4ThreeVector(vx,vy,vz);
135}
136
137/////////////////////////////////////////////////////////////////////////////
138//
139// Random  vector on box surface
140
141G4ThreeVector GetVectorOnBox( G4Box& box )
142{
143  G4double rand, a, b, c, px, py, pz;
144  G4double part = 1./6.;
145
146  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
147
148  a = box.GetXHalfLength();
149  b = box.GetYHalfLength();
150  c = box.GetZHalfLength();
151 
152  rand = G4UniformRand();
153
154  if      ( rand < part )
155  {
156    px = -a - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
157    py = -b - 0.5*kCarTolerance + (2.*b + kCarTolerance)*G4UniformRand(); 
158    pz = -c - 0.5*kCarTolerance + (2.*c + kCarTolerance)*G4UniformRand(); 
159  }
160  else if ( rand < 2*part )
161  {
162    px =  a - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
163    py = -b - 0.5*kCarTolerance + (2.*b + kCarTolerance)*G4UniformRand(); 
164    pz = -c - 0.5*kCarTolerance + (2.*c + kCarTolerance)*G4UniformRand(); 
165  }
166  else if ( rand < 3*part )
167  {
168    px = -a - 0.5*kCarTolerance + (2.*a + kCarTolerance)*G4UniformRand(); 
169    py = -b - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
170    pz = -c - 0.5*kCarTolerance + (2.*c + kCarTolerance)*G4UniformRand(); 
171  }
172  else if ( rand < 4*part )
173  {
174    px = -a - 0.5*kCarTolerance + (2.*a + kCarTolerance)*G4UniformRand(); 
175    py =  b - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
176    pz = -c - 0.5*kCarTolerance + (2.*c + kCarTolerance)*G4UniformRand(); 
177  }
178  else if ( rand < 5*part )
179  {
180    px = -a - 0.5*kCarTolerance + (2.*a + kCarTolerance)*G4UniformRand(); 
181    py = -b - 0.5*kCarTolerance + (2.*b + kCarTolerance)*G4UniformRand(); 
182    pz = -c - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
183  }
184  else 
185  {
186    px = -a - 0.5*kCarTolerance + (2.*a + kCarTolerance)*G4UniformRand(); 
187    py = -b - 0.5*kCarTolerance + (2.*b + kCarTolerance)*G4UniformRand(); 
188    pz =  c - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
189  }
190
191  return G4ThreeVector(px,py,pz);
192}
193
194/////////////////////////////////////////////////////////////////////////////
195//
196// Random vector on orb surface
197
198G4ThreeVector GetVectorOnOrb(G4Orb& orb)
199{
200  G4double cosTheta, sinTheta, phi, radius, px, py, pz;
201
202  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
203
204  radius = orb.GetRadius();
205  radius += -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
206
207  cosTheta = -1. + 2.*G4UniformRand();
208  if( cosTheta > 1.)  cosTheta = 1.;
209  if( cosTheta < -1.) cosTheta = -1.;
210  sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
211 
212  phi = 2*pi*G4UniformRand();
213
214  px = radius*sinTheta*std::cos(phi);
215  py = radius*sinTheta*std::sin(phi);
216  pz = radius*cosTheta;
217
218  return G4ThreeVector(px,py,pz);
219}
220
221/////////////////////////////////////////////////////////////////////////////
222//
223// Random vector on sphere surface
224
225G4ThreeVector GetVectorOnSphere(G4Sphere& sphere)
226{
227  G4double cosTheta, sinTheta, phi, radius, px, py, pz;
228  G4double part = 1./6.;
229  G4double rand = G4UniformRand();
230
231  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
232  G4double kAngTolerance = G4GeometryTolerance::GetInstance()->GetAngularTolerance();
233
234  G4double pRmin  = sphere.GetInsideRadius();
235  G4double pRmax  = sphere.GetOuterRadius();
236  G4double phi1   = sphere.GetStartPhiAngle();
237  G4double phi2   = phi1 + sphere.GetDeltaPhiAngle();
238  G4double theta1 = sphere.GetStartThetaAngle();
239  G4double theta2 = theta1 + sphere.GetDeltaThetaAngle();
240
241  if      ( rand < part ) // Rmax
242  {
243    radius   = pRmax -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
244
245    cosTheta = std::cos(theta2+0.5*kAngTolerance) + 
246              (std::cos(theta1-0.5*kAngTolerance)-std::cos(theta2+0.5*kAngTolerance))*G4UniformRand();
247    if( cosTheta > 1.)  cosTheta = 1.;
248    if( cosTheta < -1.) cosTheta = -1.;
249    sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
250 
251    phi      = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
252  }
253  else if ( rand < 2*part )  // Rmin
254  {
255    radius   = pRmin -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
256
257    cosTheta = std::cos(theta2+0.5*kAngTolerance) + 
258              (std::cos(theta1-0.5*kAngTolerance)-std::cos(theta2+0.5*kAngTolerance))*G4UniformRand();
259    if( cosTheta > 1.)  cosTheta = 1.;
260    if( cosTheta < -1.) cosTheta = -1.;
261    sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
262 
263    phi      = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
264  }
265  else if ( rand < 3*part )  // phi1
266  {
267    radius   = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
268
269    cosTheta = std::cos(theta2+0.5*kAngTolerance) + 
270              (std::cos(theta1-0.5*kAngTolerance)-std::cos(theta2+0.5*kAngTolerance))*G4UniformRand();
271    if( cosTheta > 1.)  cosTheta = 1.;
272    if( cosTheta < -1.) cosTheta = -1.;
273    sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
274 
275    phi      = phi1 -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
276  }
277  else if ( rand < 4*part )  // phi2
278  {
279    radius   = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
280
281    cosTheta = std::cos(theta2+0.5*kAngTolerance) + 
282              (std::cos(theta1-0.5*kAngTolerance)-std::cos(theta2+0.5*kAngTolerance))*G4UniformRand();
283    if( cosTheta > 1.)  cosTheta = 1.;
284    if( cosTheta < -1.) cosTheta = -1.;
285    sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
286 
287    phi      = phi2 -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
288  }
289  else if ( rand < 5*part )  // theta1
290  {
291    radius   = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
292
293    cosTheta = std::cos(theta1+0.5*kAngTolerance) + 
294              (std::cos(theta1-0.5*kAngTolerance)-std::cos(theta1+0.5*kAngTolerance))*G4UniformRand();
295    if( cosTheta > 1.)  cosTheta = 1.;
296    if( cosTheta < -1.) cosTheta = -1.;
297    sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
298 
299    phi      = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
300  }
301  else // theta2
302  {
303    radius   = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
304
305    cosTheta = std::cos(theta2+0.5*kAngTolerance) + 
306              (std::cos(theta2-0.5*kAngTolerance)-std::cos(theta2+0.5*kAngTolerance))*G4UniformRand();
307    if( cosTheta > 1.)  cosTheta = 1.;
308    if( cosTheta < -1.) cosTheta = -1.;
309    sinTheta = std::sqrt( 1. - cosTheta*cosTheta );
310 
311    phi      = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
312  }
313
314  px = radius*sinTheta*std::cos(phi);
315  py = radius*sinTheta*std::sin(phi);
316  pz = radius*cosTheta;
317
318  return G4ThreeVector(px,py,pz);
319}
320
321/////////////////////////////////////////////////////////////////////////////
322//
323// Random vector on tubs surface
324
325G4ThreeVector GetVectorOnTubs(G4Tubs& tubs)
326{
327  G4double phi, radius, px, py, pz;
328  G4double part = 1./6.;
329  G4double rand = G4UniformRand();
330
331  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
332  G4double kAngTolerance = G4GeometryTolerance::GetInstance()->GetAngularTolerance();
333
334  G4double pRmin = tubs.GetInnerRadius   ();
335  G4double pRmax = tubs.GetOuterRadius   ();
336  G4double tubsZ = tubs.GetZHalfLength   ();
337  G4double phi1  = tubs.GetStartPhiAngle ();
338  G4double phi2  = phi1 + tubs.GetDeltaPhiAngle ();
339
340
341  if      ( rand < part ) // Rmax
342  {
343    radius = pRmax -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
344    pz     = -tubsZ - 0.5*kCarTolerance + (2*tubsZ + kCarTolerance)*G4UniformRand(); 
345    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
346  }
347  else if ( rand < 2*part )  // Rmin
348  {
349    radius = pRmin -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
350    pz     = -tubsZ - 0.5*kCarTolerance + (2*tubsZ + kCarTolerance)*G4UniformRand();   
351    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
352  }
353  else if ( rand < 3*part )  // phi1
354  {
355    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
356    pz     = -tubsZ - 0.5*kCarTolerance + (2*tubsZ + kCarTolerance)*G4UniformRand();   
357    phi    = phi1 -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
358  }
359  else if ( rand < 4*part )  // phi2
360  {
361    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
362    pz     = -tubsZ - 0.5*kCarTolerance + (2*tubsZ + kCarTolerance)*G4UniformRand();   
363    phi    = phi2 -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
364  }
365  else if ( rand < 5*part )  // -fZ
366  {
367    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
368
369    pz     = -tubsZ - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();   
370 
371    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
372  }
373  else // fZ
374  {
375    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
376    pz     = tubsZ - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();     
377    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
378  }
379
380  px = radius*std::cos(phi);
381  py = radius*std::sin(phi);
382 
383  return G4ThreeVector(px,py,pz);
384}
385
386/////////////////////////////////////////////////////////////////////////////
387//
388// Random vector on cons surface
389
390G4ThreeVector GetVectorOnCons(G4Cons& cons)
391{
392  G4double phi, pRmin, pRmax, radius, px, py, pz;
393  G4double part = 1./6.;
394  G4double rand = G4UniformRand();
395
396  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
397  G4double kAngTolerance = G4GeometryTolerance::GetInstance()->GetAngularTolerance();
398
399  G4double pRmin1 = cons.GetInnerRadiusMinusZ   ();
400  G4double pRmax1 = cons.GetOuterRadiusMinusZ   ();
401  G4double pRmin2 = cons.GetInnerRadiusPlusZ   ();
402  G4double pRmax2 = cons.GetOuterRadiusPlusZ   ();
403  G4double consZ  = cons.GetZHalfLength   ();
404  G4double phi1   = cons.GetStartPhiAngle ();
405  G4double phi2   = phi1 + cons.GetDeltaPhiAngle ();
406  G4double tgMin  = (pRmin2 - pRmin1)/(2.*consZ);
407  G4double tgMax  = (pRmax2 - pRmax1)/(2.*consZ);
408
409  if      ( rand < part ) // Rmax
410  {
411    pz     = -consZ - 0.5*kCarTolerance + (2*consZ + kCarTolerance)*G4UniformRand();
412    pRmax  = pRmax1 + tgMax*(pz+consZ); 
413    radius = pRmax -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
414    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
415  }
416  else if ( rand < 2*part )  // Rmin
417  {
418    pz     = -consZ - 0.5*kCarTolerance + (2*consZ + kCarTolerance)*G4UniformRand();   
419    pRmin  = pRmin1 + tgMin*(pz+consZ); 
420    radius = pRmin -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
421    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
422  }
423  else if ( rand < 3*part )  // phi1
424  {
425    pz     = -consZ - 0.5*kCarTolerance + (2*consZ + kCarTolerance)*G4UniformRand();   
426    pRmax  = pRmax1 + tgMax*(pz+consZ); 
427    pRmin  = pRmin1 + tgMin*(pz+consZ); 
428    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
429    phi    = phi1 -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
430  }
431  else if ( rand < 4*part )  // phi2
432  {
433    pz     = -consZ - 0.5*kCarTolerance + (2*consZ + kCarTolerance)*G4UniformRand();   
434    pRmax  = pRmax1 + tgMax*(pz+consZ); 
435    pRmin  = pRmin1 + tgMin*(pz+consZ); 
436    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
437    phi    = phi2 -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
438  }
439  else if ( rand < 5*part )  // -fZ
440  {
441    pz     = -consZ - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();   
442    radius = pRmin1 - 0.5*kCarTolerance + (pRmax1-pRmin1+kCarTolerance)*G4UniformRand();   
443    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
444  }
445  else // fZ
446  {
447    pz     = consZ - 0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();     
448    radius = pRmin2 - 0.5*kCarTolerance + (pRmax2-pRmin2+kCarTolerance)*G4UniformRand(); 
449    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
450  }
451
452  px = radius*std::cos(phi);
453  py = radius*std::sin(phi);
454 
455  return G4ThreeVector(px,py,pz);
456}
457
458/////////////////////////////////////////////////////////////////////////////
459//
460// Random vector on torus surface
461
462G4ThreeVector GetVectorOnTorus(G4Torus& torus)
463{
464  G4double phi, alpha, radius, px, py, pz;
465  G4double part = 1./4.;
466  G4double rand = G4UniformRand();
467
468  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
469  G4double kAngTolerance = G4GeometryTolerance::GetInstance()->GetAngularTolerance();
470
471  G4double pRmin = torus.GetRmin();
472  G4double pRmax = torus.GetRmax();
473  G4double pRtor = torus.GetRtor();
474  G4double phi1  = torus.GetSPhi();
475  G4double phi2  = phi1 + torus.GetDPhi ();
476
477 if      ( rand < part ) // Rmax
478  {
479    radius = pRmax -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand();
480    alpha = twopi*G4UniformRand(); 
481    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
482  }
483  else if ( rand < 2*part )  // Rmin
484  {
485    radius = pRmin -0.5*kCarTolerance + (kCarTolerance)*G4UniformRand(); 
486    alpha = twopi*G4UniformRand(); 
487    phi    = phi1 - 0.5*kAngTolerance + (phi2 - phi1 + kAngTolerance)*G4UniformRand();
488  }
489  else if ( rand < 3*part )  // phi1
490  {
491    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
492    alpha = twopi*G4UniformRand(); 
493    //  pz     = -pRtor - 0.5*kCarTolerance + (2*pRtor + kCarTolerance)*G4UniformRand();   
494    phi    = phi1 -0.5*kAngTolerance + (kAngTolerance)*G4UniformRand();
495  }
496  else   // phi2
497  {
498    radius = pRmin - 0.5*kCarTolerance + (pRmax-pRmin+kCarTolerance)*G4UniformRand(); 
499    alpha = twopi*G4UniformRand(); 
500    phi    = phi2 -0.5*kAngTolerance + (kAngTolerance)*G4UniformRand();
501  }
502  px = (pRtor+radius*std::cos(alpha))*std::cos(phi);
503  py = (pRtor+radius*std::cos(alpha))*std::sin(phi);
504  pz = radius*std::sin(alpha);
505
506 
507  return G4ThreeVector(px,py,pz);
508}
509
510
511enum Esolid {kBox, kOrb, kSphere, kCons, kTubs, kTorus, kPara, kTrapezoid, kTrd};
512
513
514//////////////////////////////////////////////////////////////////////
515//
516// Main executable function
517
518int main(int argc, char** argv)
519{
520  int test_one_solid( Esolid, int, int ); 
521  int no_points= 10000;
522  int dirs_per_point= 1000; 
523
524  G4cout << "Usage: testSurfaceInOut [ no_surface_points ]  [ no_directions_each ] " 
525         << G4endl << G4endl;
526  int points_in=0, dirs_in=0; 
527  if( argc >= 2 ){
528       points_in = atoi(argv[1]);
529  } 
530  if( argc >= 3 ){
531       dirs_in = atoi(argv[2]);
532  } 
533
534  if( points_in > 0 ) { no_points= points_in; } 
535  if( dirs_in > 0 ) { dirs_per_point = dirs_in; } 
536
537  G4cout << "Testing each solid with " << no_points << " surface points and " 
538         << dirs_per_point  << " directions each. " << G4endl;
539
540  Esolid useCase; 
541  G4cout<< "To test Box." << G4endl;
542  test_one_solid( useCase= kBox,  no_points, dirs_per_point ); 
543
544  G4cout<< "To test Tubs." << G4endl;
545  test_one_solid( useCase= kTubs,  no_points, dirs_per_point ); 
546
547  G4cout<< "To test Sphere." << G4endl;
548  test_one_solid( useCase= kSphere,  no_points, dirs_per_point ); 
549
550  G4cout<< "To test Orb." << G4endl;
551  test_one_solid( useCase= kOrb,  no_points, dirs_per_point ); 
552
553  G4cout<< "To test Cons." << G4endl;
554  test_one_solid( useCase= kCons,  no_points, dirs_per_point ); 
555
556  G4cout<< "To test Para." << G4endl;
557  test_one_solid( useCase= kPara,  no_points, dirs_per_point ); 
558 
559  G4cout<< "To test Trapezoid." << G4endl;
560  test_one_solid( useCase= kTrapezoid,  no_points, dirs_per_point ); 
561
562  G4cout<< "To test Trd." << G4endl;
563  test_one_solid( useCase= kTrd,  no_points, dirs_per_point ); 
564
565  return 0; 
566}
567
568//
569
570int test_one_solid ( Esolid useCase,  int num_points, int directions_per_point )
571{
572  G4int i,j;
573  G4int iMax=  num_points, jMax= directions_per_point;
574  G4int iCheck=iMax/25;
575  G4cout << "  Reporting every  " << iCheck <<  " points. " << G4endl; 
576
577  G4double distIn, distOut;
578
579  G4double kCarTolerance = G4GeometryTolerance::GetInstance()->GetSurfaceTolerance();
580
581  G4double Rtor = 100 ;
582  G4double Rmax = Rtor*0.9 ;
583  G4double Rmin = Rtor*0.1 ;
584
585  EInside surfaceP;
586  G4ThreeVector norm,*pNorm;
587  G4bool *pgoodNorm, goodNorm, calcNorm=true;
588
589  pNorm=&norm;
590  pgoodNorm=&goodNorm;
591
592  G4cout.precision(20);
593
594  // Boxes for test
595
596  G4Box b1("Test Box #1",20,30,40);
597  G4Box b2("Test Box #2",10,10,10);
598  G4Box box3("BABAR Box",0.14999999999999999, 
599                           24.707000000000001, 
600                           22.699999999999999) ;
601
602  // orbs for test
603
604  G4Orb  o1("Solid G4Orb",50);
605  G4Orb  o10("s10",0.018*mm);
606  // G4Orb* solidO1= new G4Orb("O1", 2.7*cm);
607
608
609  // spheres for test
610
611  //  G4Sphere s5("Patch (phi/theta seg)",45,50,-pi/4,halfpi,pi/4,halfpi);
612  //  G4Sphere s5("Patch (phi/theta seg)",45,50,-pi/4.,pi/4.,pi/4,pi/4.);
613  G4Sphere s5("Patch (phi/theta seg)",45,50,-pi/4.,pi/4.,pi/2,pi/4.);
614
615  G4Sphere s6("John example",300,500,0,5.76,0,pi) ; 
616  G4Sphere s7("sphere7",1400.,1550.,0.022321428571428572,0.014642857142857141,
617                          1.5631177553663251,0.014642857142857141    );
618  G4Sphere s8("sphere",278.746*mm, 280.0*mm, 0.0*degree, 360.0*degree,
619                                               0.0*degree, 90.0*degree);
620
621  G4Sphere b216("b216", 1400.0, 1550.0, 
622                  0.022321428571428572, 
623                  0.014642857142857141,
624                  1.578117755366325,
625                  0.014642857142867141);
626
627  G4Sphere s9("s9",0*mm,410*mm,0*degree,360*degree,90*degree,90*degree);
628
629  G4Sphere b402("b402", 475*mm, 480*mm, 
630               0*degree,360*degree,17.8*degree,144.4*degree);
631   
632
633  G4Sphere s10("s10",0*mm,0.018*mm,0*degree,360*degree,0*degree,180*degree);
634
635
636  G4Sphere s11("s11",5000000.*mm,
637                    3700000000.*mm,
638                   0*degree,360*degree,0*degree,180*degree);
639
640
641  G4Sphere sAlex("sAlex",500.*mm,
642                    501.*mm,
643                   0*degree,360*degree,0*degree,180*degree);
644
645  G4Sphere sLHCB("sLHCB",8600*mm, 8606*mm, 
646    -1.699135525184141*degree,
647    3.398271050368263*degree,88.52855940538514*degree,2.942881189229715*degree );
648
649  G4Sphere spAroundX("SpAroundX",  10.*mm, 1000.*mm, -1.0*degree, 
650                                                     2.0*degree, 
651                                        0.*degree, 180.0*degree );
652
653
654  // G4Tubs t4("Hole Sector #4",45*mm,50*mm,50*mm,halfpi,halfpi);
655  G4Tubs t4("Hole Sector #4",45*mm,50*mm,50*mm,-halfpi,halfpi);
656
657  G4Cons c4("Hollow Cut Cone",50,100,50,200,50,-pi/6,pi/3);
658  G4Cons c5("Hollow Cut Cone",25,50,75,150,50,0,3*halfpi);
659   
660  G4Torus torus1("torus1",10.,15.,20.,0.,3*halfpi);
661  G4Torus tor2("Hole cutted Torus #2",Rmin,Rmax,Rtor,pi/4,halfpi);
662  G4Torus torn2("tn2",Rmin,Rmax,Rtor,halfpi,halfpi);
663  G4Torus torn3("tn3",Rmin,Rmax,Rtor,halfpi,3*halfpi);
664
665
666  // Check of some use cases shown zero-zero problems
667
668  G4ThreeVector pCheck, vCheck;
669
670  // pCheck = G4ThreeVector( 41.418613476008794, -16.893662525384702, 4.9094423552800466 );
671  // vCheck = G4ThreeVector( 0.34553222148699703, 0.91172822040596313, 0.22216916084289551 );
672  // distIn  = s5.DistanceToIn(pCheck,vCheck);
673  // distOut = s5.DistanceToOut(pCheck,vCheck,calcNorm,pgoodNorm,pNorm);
674
675  // pCheck = G4ThreeVector( 43.169180219772784, -11.564259048580507, 5.2621090605480623 );
676  // surfaceP = s5.Inside(pCheck);
677
678  pCheck = G4ThreeVector( -51.189087930597751, -17.382942686173514, -45.939946175080983 );
679  vCheck = G4ThreeVector( 0.44410267104120671, -0.88563345532911941, -0.13574314117431641 );
680  distIn  = c5.DistanceToIn(pCheck,vCheck);
681  distOut = c5.DistanceToOut(pCheck,vCheck,calcNorm,pgoodNorm,pNorm); 
682
683  pCheck = G4ThreeVector(-41.407491890396564, -31.155805955816909, -48.18046093035241 );
684  vCheck = G4ThreeVector(0.79040557001853884, -0.52472467107317944, -0.31610608100891113 );
685  distIn  = c5.DistanceToIn(pCheck,vCheck);
686  distOut = c5.DistanceToOut(pCheck,vCheck,calcNorm,pgoodNorm,pNorm); 
687
688  pCheck = G4ThreeVector(-66.68328490196707, -47.460245099793099, -18.151754141035401 );
689  vCheck = G4ThreeVector(-0.066679791594195931, 0.88577693677046576, -0.45929622650146484 );
690  distIn  = c5.DistanceToIn(pCheck,vCheck);
691  distOut = c5.DistanceToOut(pCheck,vCheck,calcNorm,pgoodNorm,pNorm); 
692
693
694#ifdef NDEBUG
695    G4Exception("FAIL: *** Assertions must be compiled in! ***");
696#endif
697
698 
699  // Check box tracking function
700
701  switch (useCase)
702  {
703
704    case kBox:
705    G4cout<<"Testing G4Box:"<<G4endl<<G4endl;
706    for(i=0;i<iMax;i++)
707    {
708      G4ThreeVector p = GetVectorOnBox(b1);
709   
710      surfaceP = b1.Inside(p);
711      if(surfaceP != kSurface)
712      {
713        G4cout<<"p is out of surface: "<<G4endl;
714        G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl<<G4endl;
715
716      }
717      else
718      {
719        G4ThreeVector origin_norm = b1.SurfaceNormal(p);
720
721        for(j=0;j<jMax;j++)
722        {
723          G4ThreeVector v = GetRandomUnitVector();
724
725          distIn  = b1.DistanceToIn(p,v);
726          distOut = b1.DistanceToOut(p,v,calcNorm,pgoodNorm,pNorm); 
727
728          if( distIn < kCarTolerance && distOut < kCarTolerance )
729          {
730            G4cout<<" distIn < kCarTolerance && distOut < kCarTolerance"<<G4endl;
731            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
732            G4cout<<"location p: "; // << G4endl;
733            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
734            G4cout<<" direction v: "; // << G4endl;
735            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
736          }
737          else if(distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance)
738          {
739            G4cout<<" distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance"<<G4endl;
740            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
741            G4cout<<"location p: ";  // << G4endl;
742            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
743            G4cout<<" direction v: ";  // << G4endl;
744            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl;
745            G4cout<<" dot prod v.norm(orig)= " << v.dot( origin_norm ) << G4endl;
746            G4cout <<G4endl;
747          }     
748        }
749      }
750    }
751    break;
752   
753    case kOrb:
754      G4cout<<"Testing G4Orb:"<<G4endl<<G4endl;
755    for(i=0;i<iMax;i++)
756    {
757      G4ThreeVector p = GetVectorOnOrb(o1);
758   
759      surfaceP = o1.Inside(p);
760      if(surfaceP != kSurface)
761      {
762        G4cout<<"p is out of surface: "<<G4endl;
763        G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl<<G4endl;
764
765      }
766      else
767      {
768        for(j=0;j<jMax;j++)
769        {
770          G4ThreeVector v = GetRandomUnitVector();
771
772          distIn  = o1.DistanceToIn(p,v);
773          distOut = o1.DistanceToOut(p,v,calcNorm,pgoodNorm,pNorm); 
774
775          if( distIn < kCarTolerance && distOut < kCarTolerance )
776          {
777            G4cout<<" distIn < kCarTolerance && distOut < kCarTolerance"<<G4endl;
778            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
779            G4cout<<"location p: "<<G4endl;
780            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
781            G4cout<<" direction v: "<<G4endl;
782            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
783          }
784          else if(distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance)
785          {
786            G4cout<<" distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance"<<G4endl;
787            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
788            G4cout<<"location p: "<<G4endl;
789            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
790            G4cout<<" direction v: "<<G4endl;
791            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl; 
792            G4cout<<" p.v/|p|= " << p.dot(v)/p.mag() <<G4endl;
793          }     
794        }
795      }
796    }
797    break;
798
799    case kSphere:
800      G4cout<<"Testing one cutted G4Sphere:" << s5 <<G4endl<<G4endl;
801    for(i=0;i<iMax;i++)
802    {
803      if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl;
804      G4ThreeVector p = GetVectorOnSphere(s5);     
805      surfaceP = s5.Inside(p);
806      if(surfaceP != kSurface)
807      {
808        G4cout<<"p is out of surface: "<<G4endl;
809        G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl<<G4endl;
810
811      }
812      else
813      {
814        for(j=0;j<jMax;j++)
815        {
816          G4ThreeVector v = GetRandomUnitVector();
817
818          distIn  = s5.DistanceToIn(p,v);
819          distOut = s5.DistanceToOut(p,v,calcNorm,pgoodNorm,pNorm); 
820
821          //  if( distIn < kCarTolerance && distOut < kCarTolerance )
822          if( distIn == 0. && distOut == 0. )
823          {
824            G4cout<<" distIn < kCarTolerance && distOut < kCarTolerance"<<G4endl;
825            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
826            G4cout<<"location p: "<<G4endl;
827            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
828            G4cout<<" direction v: "<<G4endl;
829            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
830          }
831          else if(distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance)
832          {
833            /*
834            G4cout<<" distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance"<<G4endl;
835            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
836            G4cout<<"location p: "<<G4endl;
837            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
838            G4cout<<" direction v: "<<G4endl;
839            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
840            */
841          }     
842        }
843      }
844    }
845    break;
846
847    case kTubs:
848      G4cout<<"Testing all cutted G4Tubs:"<<G4endl<<G4endl;
849    for(i=0;i<iMax;i++)
850    {
851      if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl;
852      G4ThreeVector p = GetVectorOnTubs(t4);     
853      surfaceP = t4.Inside(p);
854      if(surfaceP != kSurface)
855      {
856        G4cout<<"p is out of surface: "<<G4endl;
857        G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl<<G4endl;
858
859      }
860      else
861      {
862        for(j=0;j<jMax;j++)
863        {
864          G4ThreeVector v = GetRandomUnitVector();
865
866          distIn  = t4.DistanceToIn(p,v);
867          distOut = t4.DistanceToOut(p,v,calcNorm,pgoodNorm,pNorm); 
868
869          //  if( distIn < kCarTolerance && distOut < kCarTolerance )
870          if( distIn == 0. && distOut == 0. )
871          {
872            G4cout<<" distIn < kCarTolerance && distOut < kCarTolerance"<<G4endl;
873            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
874            G4cout<<"location p: "<<G4endl;
875            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
876            G4cout<<" direction v: "<<G4endl;
877            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
878          }
879          else if(distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance)
880          {
881            /*
882            G4cout<<" distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance"<<G4endl;
883            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
884            G4cout<<"location p: "<<G4endl;
885            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
886            G4cout<<" direction v: "<<G4endl;
887            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
888            */
889          }     
890        }
891      }
892    }
893    break;
894
895    case kCons:
896      G4cout<<"Testing all cutted G4Cons:"<<G4endl<<G4endl;
897    for(i=0;i<iMax;i++)
898    {
899      if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl;
900      G4ThreeVector p = GetVectorOnCons(c5);     
901      surfaceP = c5.Inside(p);
902      if(surfaceP != kSurface)
903      {
904        G4cout<<"p is out of surface: "<<G4endl;
905        G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl<<G4endl;
906
907      }
908      else
909      {
910        for(j=0;j<jMax;j++)
911        {
912          G4ThreeVector v = GetRandomUnitVector();
913
914          distIn  = c5.DistanceToIn(p,v);
915          distOut = c5.DistanceToOut(p,v,calcNorm,pgoodNorm,pNorm); 
916
917          //  if( distIn < kCarTolerance && distOut < kCarTolerance )
918          if( distIn == 0. && distOut == 0. )
919          {
920            G4cout<<" distIn < kCarTolerance && distOut < kCarTolerance"<<G4endl;
921            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
922            G4cout<<"location p: "<<G4endl;
923            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
924            G4cout<<" direction v: "<<G4endl;
925            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
926          }
927          else if(distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance)
928          {
929            /*
930            G4cout<<" distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance"<<G4endl;
931            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
932            G4cout<<"location p: "<<G4endl;
933            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
934            G4cout<<" direction v: "<<G4endl;
935            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
936            */
937          }     
938        }
939      }
940    }
941    break;
942    case kTorus:
943      G4cout<<"Testing all cutted G4Torus:"<<G4endl<<G4endl;
944    for(i=0;i<iMax;i++)
945    {
946      if(i%iCheck == 0) G4cout<<"i = "<<i<<G4endl;
947      G4ThreeVector p = GetVectorOnTorus(torus1);     
948      surfaceP = torus1.Inside(p);
949      if(surfaceP != kSurface)
950      {
951        G4cout<<"p is out of surface: "<<G4endl;
952        G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl<<G4endl;
953
954      }
955      else
956      {
957        for(j=0;j<jMax;j++)
958        {
959          G4ThreeVector v = GetRandomUnitVector();
960
961          distIn  = torus1.DistanceToIn(p,v);
962          distOut = torus1.DistanceToOut(p,v,calcNorm,pgoodNorm,pNorm); 
963
964          //  if( distIn < kCarTolerance && distOut < kCarTolerance )
965          if( distIn == 0. && distOut == 0. )
966          {
967            G4cout<<" distIn < kCarTolerance && distOut < kCarTolerance"<<G4endl;
968            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
969            G4cout<<"location p: "<<G4endl;
970            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
971            G4cout<<" direction v: "<<G4endl;
972            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
973          }
974          else if(distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance)
975          {
976            /*
977            G4cout<<" distIn > 100000*kCarTolerance && distOut > 100*kCarTolerance"<<G4endl;
978            G4cout<<"distIn = "<<distIn<<";  distOut = "<<distOut<<G4endl;
979            G4cout<<"location p: "<<G4endl;
980            G4cout<<"( "<<p.x()<<", "<<p.y()<<", "<<p.z()<<" ); "<<G4endl;
981            G4cout<<" direction v: "<<G4endl;
982            G4cout<<"( "<<v.x()<<", "<<v.y()<<", "<<v.z()<<" ); "<<G4endl<<G4endl;
983            */
984          }     
985        }
986      }
987    }
988    break;
989
990    default:
991      G4cout<<"A test is not implemented for this case: " << useCase <<G4endl<<G4endl;
992
993    break;   
994  }
995  return 0;
996}
997
Note: See TracBrowser for help on using the repository browser.