195{
198
199 G4bool IsParallel = false ;
200 G4bool IsConverged = false ;
201
203
205
207 {
209 {
214 }
216 }
217 else
218 {
220 {
221 distance[j] = kInfinity;
223 isvalid[j] = false;
224 gxx[j].
set(kInfinity, kInfinity, kInfinity);
225 }
226 }
227
230
231#ifdef G4TWISTDEBUG
234#endif
235
237
238
239
243 G4bool tmpisvalid = false ;
244
245 std::vector<Intersection> xbuf ;
247
248
249
251
252 G4double phixz = fPhiTwist * ( p.
x() * v.
z() - p.
z() * v.
x() ) ;
253 G4double phiyz = fPhiTwist * ( p.
y() * v.
z() - p.
z() * v.
y() ) ;
254
255
256
257
259 {
260 if ( std::fabs(p.
z()) <= L )
261 {
262 phi = p.
z() * fPhiTwist /
L ;
263 u = (fDy1*(4*(-(fdeltaY*phi*v.
x()) + fPhiTwist*p.
y()*v.
x()
264 + fdeltaX*phi*v.
y() - fPhiTwist*p.
x()*v.
y())
265 + ((fDx3plus1 + fDx4plus2)*fPhiTwist
266 + 2*(fDx3minus1 + fDx4minus2)*phi)
267 *(v.
y()*std::cos(phi) - v.
x()*std::sin(phi))))
268 /(fPhiTwist*(4*fDy1* v.
x() - (fa1md1 + 4*fDy1*fTAlph)*v.
y())
269 *std::cos(phi) + fPhiTwist*(fa1md1*v.
x()
270 + 4*fDy1*(fTAlph*v.
x() + v.
y()))*std::sin(phi));
276
277 xbuf.push_back(xbuftmp) ;
278 }
279 else
280 {
281 distance[0] = kInfinity;
282 gxx[0].
set(kInfinity,kInfinity,kInfinity);
283 isvalid[0] = false ;
286 areacode[0], isvalid[0],
287 0, validate, &gp, &gv);
288 return 0;
289 }
290 }
291 else
292 {
293
295
296 c[7] = 57600*
297 fDy1*(fa1md1*phiyz +
298 fDy1*(-4*phixz + 4*fTAlph*phiyz
299 + (fDx3plus1 + fDx4plus2)*fPhiTwist*v.
z())) ;
300 c[6] = -57600*
301 fDy1*(4*fDy1*(phiyz + 2*fDz*v.
x() + fTAlph*(phixz - 2*fDz*v.
y()))
302 - 2*fDy1*(2*fdeltaX + fDx3minus1 + fDx4minus2
303 - 2*fdeltaY*fTAlph)*v.
z()
304 + fa1md1*(phixz - 2*fDz*v.
y() + fdeltaY*v.
z()));
305 c[5] = 4800*
306 fDy1*(fa1md1*(-5*phiyz - 24*fDz*v.
x() + 12*fdeltaX*v.
z()) +
307 fDy1*(20*phixz - 4*(5*fTAlph*phiyz + 24*fDz*fTAlph*v.
x()
308 + 24*fDz*v.
y()) + (48*fdeltaY + (fDx3plus1 + fDx4plus2)
309 *fPhiTwist + 48*fdeltaX*fTAlph)*v.
z()));
310 c[4] = 4800*
311 fDy1*(fa1md1*(phixz - 10*fDz*v.
y() + 5*fdeltaY*v.
z())
312 + 2*fDy1*(2*phiyz + 20*fDz*v.
x()
313 + (-10*fdeltaX + fDx3minus1 + fDx4minus2)*v.
z()
314 + 2*fTAlph*(phixz - 10*fDz*v.
y() + 5*fdeltaY*v.
z())));
315 c[3] = -96*
316 fDy1*(-(fa1md1*(phiyz + 100*fDz*v.
x() - 50*fdeltaX*v.
z()))
317 + fDy1*(4*phixz - 400*fDz*v.
y()
318 + (200*fdeltaY - (fDx3plus1 + fDx4plus2)*fPhiTwist)*v.
z()
319 - 4*fTAlph*(phiyz + 100*fDz*v.
x() - 50*fdeltaX*v.
z())));
320 c[2] = 32*
321 fDy1*(4*fDy1*(7*fTAlph*phixz + 7*phiyz - 6*fDz*v.
x() + 6*fDz*fTAlph*v.
y())
322 + 6*fDy1*(2*fdeltaX+fDx3minus1+fDx4minus2-2*fdeltaY*fTAlph)*v.
z()
323 + fa1md1*(7*phixz + 6*fDz*v.
y() - 3*fdeltaY*v.
z()));
324 c[1] = -8*
325 fDy1*(fa1md1*(-9*phiyz - 56*fDz*v.
x() + 28*fdeltaX*v.
z())
326 + 4*fDy1*(9*phixz - 9*fTAlph*phiyz - 56*fDz*fTAlph*v.
x()
327 - 56*fDz*v.
y() + 28*(fdeltaY + fdeltaX*fTAlph)*v.
z()));
328 c[0] = 72*
329 fDy1*(fa1md1*(2*fDz*v.
y() - fdeltaY*v.
z())
330 + fDy1*(-8*fDz*v.
x() + 8*fDz*fTAlph*v.
y()
331 + 4*fdeltaX*v.
z() - 4*fdeltaY*fTAlph*v.
z()));
332
333#ifdef G4TWISTDEBUG
334 G4cout <<
"coef = " << c[0] <<
" "
335 << c[1] << " "
336 << c[2] << " "
337 << c[3] << " "
338 << c[4] << " "
339 << c[5] << " "
340 << c[6] << " "
342#endif
343
346
347 for (
G4int i = 0 ; i<num ; i++ )
348 {
349 if ( si[i]==0.0 )
350 {
351#ifdef G4TWISTDEBUG
352 G4cout <<
"Solution " << i <<
" : " << srd[i] <<
G4endl ;
353#endif
354 phi = std::fmod(srd[i] , pihalf) ;
355 u = (fDy1*(4*(phiyz + 2*fDz*phi*v.
y() - fdeltaY*phi*v.
z())
356 - ((fDx3plus1 + fDx4plus2)*fPhiTwist
357 + 2*(fDx3minus1 + fDx4minus2)*phi)*v.
z()*std::sin(phi)))
358 /(fPhiTwist*v.
z()*(4*fDy1*std::cos(phi)
359 + (fa1md1 + 4*fDy1*fTAlph)*std::sin(phi)));
365
366 xbuf.push_back(xbuftmp) ;
367
368#ifdef G4TWISTDEBUG
369 G4cout <<
"solution " << i <<
" = " << phi <<
" , " << u <<
G4endl ;
370#endif
371 }
372 }
373 }
374
375 nxx = (
G4int)xbuf.size() ;
376
383
384 for ( std::size_t k = 0 ; k<xbuf.size() ; ++k )
385 {
386#ifdef G4TWISTDEBUG
387 G4cout <<
"Solution " << k <<
" : "
388 << "reconstructed phiR = " << xbuf[k].phi
389 <<
", uR = " << xbuf[k].u <<
G4endl ;
390#endif
391
392 phi = xbuf[k].phi ;
393 u = xbuf[k].u ;
394
395 IsConverged = false ;
396
397 for (
G4int i = 1 ; i<maxint ; ++i )
398 {
399 xxonsurface = SurfacePoint(phi,u) ;
400 surfacenormal = NormAng(phi,u) ;
401
403 deltaX = ( tmpxx - xxonsurface ).mag() ;
404 theta = std::fabs(std::acos(v*surfacenormal) - pihalf) ;
405 if ( theta < 0.001 )
406 {
407 factor = 50 ;
408 IsParallel = true ;
409 }
410 else
411 {
412 factor = 1 ;
413 }
414
415#ifdef G4TWISTDEBUG
416 G4cout <<
"Step i = " << i <<
", distance = " << tmpdist
417 <<
", " << deltaX <<
G4endl ;
419#endif
420
421 GetPhiUAtX(tmpxx, phi, u) ;
422
423
424#ifdef G4TWISTDEBUG
425 G4cout <<
"approximated phi = " << phi <<
", u = " << u <<
G4endl ;
426#endif
427
428 if ( deltaX <= factor*ctol ) { IsConverged = true ; break ; }
429
430 }
431
432 if ( std::fabs(tmpdist)<ctol ) { tmpdist = 0 ; }
433
434#ifdef G4TWISTDEBUG
435 G4cout <<
"refined solution " << phi <<
" , " << u <<
G4endl ;
438#endif
439
440 tmpisvalid = false ;
441
442 if ( IsConverged )
443 {
445 {
446 tmpareacode = GetAreaCode(tmpxx);
448 {
449 if (tmpdist >= 0) tmpisvalid = true;
450 }
451 }
453 {
454 tmpareacode = GetAreaCode(tmpxx, false);
456 {
457 if (tmpdist >= 0) { tmpisvalid = true; }
458 }
459 }
460 else
461 {
462 G4Exception(
"G4TwistTrapAlphaSide::DistanceToSurface()",
464 "Feature NOT implemented !");
465 }
466 }
467 else
468 {
469 tmpdist = kInfinity;
470 tmpisvalid = false ;
471 }
472
473
474
475 xbuf[k].xx = tmpxx ;
476 xbuf[k].distance = tmpdist ;
477 xbuf[k].areacode = tmpareacode ;
478 xbuf[k].isvalid = tmpisvalid ;
479
480 }
481
483
484#ifdef G4TWISTDEBUG
487#endif
488
489
490
492 xbuf.end() );
493
494
495
496
498
499 if ( nxxtmp<2 || IsParallel )
500 {
501
502#ifdef G4TWISTDEBUG
504#endif
505
506 phi = fPhiTwist/2 ;
507 u = 0 ;
508
514
515 xbuf.push_back(xbuftmp) ;
516
517#ifdef G4TWISTDEBUG
519#endif
520
521 phi = -fPhiTwist/2 ;
522 u = 0 ;
523
529
530 xbuf.push_back(xbuftmp) ;
531
532 for ( std::size_t k = nxxtmp ; k<xbuf.size() ; ++k )
533 {
534
535#ifdef G4TWISTDEBUG
536 G4cout <<
"Solution " << k <<
" : "
537 << "reconstructed phiR = " << xbuf[k].phi
538 <<
", uR = " << xbuf[k].u <<
G4endl ;
539#endif
540
541 phi = xbuf[k].phi ;
542 u = xbuf[k].u ;
543
544 IsConverged = false ;
545
546 for (
G4int i = 1 ; i<maxint ; ++i )
547 {
548 xxonsurface = SurfacePoint(phi,u) ;
549 surfacenormal = NormAng(phi,u) ;
551 deltaX = ( tmpxx - xxonsurface ).mag();
552 theta = std::fabs(std::acos(v*surfacenormal) - pihalf);
553 if ( theta < 0.001 )
554 {
555 factor = 50 ;
556 }
557 else
558 {
559 factor = 1 ;
560 }
561
562#ifdef G4TWISTDEBUG
563 G4cout <<
"Step i = " << i <<
", distance = " << tmpdist
564 <<
", " << deltaX <<
G4endl
565 <<
"X = " << tmpxx <<
G4endl ;
566#endif
567
568 GetPhiUAtX(tmpxx, phi, u) ;
569
570
571#ifdef G4TWISTDEBUG
572 G4cout <<
"approximated phi = " << phi <<
", u = " << u <<
G4endl ;
573#endif
574
575 if ( deltaX <= factor*ctol ) { IsConverged = true ; break ; }
576
577 }
578
579 if ( std::fabs(tmpdist)<ctol ) { tmpdist = 0; }
580
581#ifdef G4TWISTDEBUG
582 G4cout <<
"refined solution " << phi <<
" , " << u <<
G4endl ;
585#endif
586
587 tmpisvalid = false ;
588
589 if ( IsConverged )
590 {
592 {
593 tmpareacode = GetAreaCode(tmpxx);
595 {
596 if (tmpdist >= 0) { tmpisvalid = true; }
597 }
598 }
600 {
601 tmpareacode = GetAreaCode(tmpxx, false);
603 {
604 if (tmpdist >= 0) { tmpisvalid = true; }
605 }
606 }
607 else
608 {
609 G4Exception(
"G4TwistedBoxSide::DistanceToSurface()",
611 "Feature NOT implemented !");
612 }
613 }
614 else
615 {
616 tmpdist = kInfinity;
617 tmpisvalid = false ;
618 }
619
620
621
622 xbuf[k].xx = tmpxx ;
623 xbuf[k].distance = tmpdist ;
624 xbuf[k].areacode = tmpareacode ;
625 xbuf[k].isvalid = tmpisvalid ;
626
627 }
628 }
629
630
632
633
635 xbuf.end() );
636
637#ifdef G4TWISTDEBUG
640#endif
641
642 nxx = (
G4int)xbuf.size() ;
643
644 for (
G4int i = 0 ; i<(
G4int)xbuf.size() ; ++i )
645 {
646 distance[i] = xbuf[i].distance;
648 areacode[i] = xbuf[i].areacode ;
649 isvalid[i] = xbuf[i].isvalid ;
650
652 isvalid[i], nxx, validate, &gp, &gv);
653#ifdef G4TWISTDEBUG
654 G4cout <<
"element Nr. " << i
655 << ", local Intersection = " << xbuf[i].xx
656 << ", distance = " << xbuf[i].distance
657 << ", u = " << xbuf[i].u
658 << ", phi = " << xbuf[i].phi
659 << ", isvalid = " << xbuf[i].isvalid
661#endif
662
663 }
664
665#ifdef G4TWISTDEBUG
667 G4cout << nxx <<
" possible physical solutions found" <<
G4endl ;
668 for (
G4int k= 0 ; k< nxx ; k++ )
669 {
670 G4cout <<
"global intersection Point found: " << gxx[k] <<
G4endl ;
673 }
674#endif
675
676 return nxx ;
677}
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
G4bool DistanceSort(const Intersection &a, const Intersection &b)
G4bool EqualIntersection(const Intersection &a, const Intersection &b)
G4GLOB_DLL std::ostream G4cout
G4int FindRoots(G4double *op, G4int degree, G4double *zeror, G4double *zeroi)
G4int GetAreacode(G4int i) const
G4double GetDistance(G4int i) const
G4bool IsValid(G4int i) const
void SetCurrentStatus(G4int i, G4ThreeVector &xx, G4double &dist, G4int &areacode, G4bool &isvalid, G4int nxx, EValidate validate, const G4ThreeVector *p, const G4ThreeVector *v=nullptr)
G4ThreeVector GetXX(G4int i) const
void ResetfDone(EValidate validate, const G4ThreeVector *p, const G4ThreeVector *v=nullptr)
static const G4int sOutside
G4ThreeVector ComputeLocalDirection(const G4ThreeVector &gp) const
G4bool IsInside(G4int areacode, G4bool testbitmode=false) const
G4ThreeVector ComputeLocalPoint(const G4ThreeVector &gp) const
G4bool IsOutside(G4int areacode) const
CurrentStatus fCurStatWithV
G4double DistanceToPlaneWithV(const G4ThreeVector &p, const G4ThreeVector &v, const G4ThreeVector &x0, const G4ThreeVector &n0, G4ThreeVector &xx)
G4ThreeVector ComputeGlobalPoint(const G4ThreeVector &lp) const