/**************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2006, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Pol_Bfield_stop
*
* %I
* Written by: Erik B Knudsen, Peter Christiansen, and Peter Willendrup
* Date: August 2006
* Origin: RISOE
*
* Magnetic field component.
*
* %D 
*
* End of magnetic field region defined by the latest preceeding Pol_Bfield component.
*
* The component is concentric. It means that it requires a 
*
* // START MAGNETIC FIELD
* COMPONENT msf =
* Pol_Bfield(xw=0.08, yh=0.08, length=0.2, Bx=0, By=-0.678332e-4, Bz=0)
*      AT (0, 0, 0) RELATIVE armMSF
*
* // HERE CAN BE OTHER COMPONENTS INSIDE THE MAGNETIC FIELD
*
* // STOP MAGNETIC FIELD
* COMPONENT msfCp = Pol_Bfield_stop()
*      AT ("SOMEWHERE") RELATIVE armMSF
*
* In between the two components the propagation routine
* PROP_DT also handles the spin propagation.
* The current algorithm used for spin propagation is:
* SimpleNumMagnetPrecession
* in pol-lib.
* and does not handle gravity.
*
* GRAVITY: NO
* POLARISATION: YES
*
* Example: Pol_Bfield_stop()
*
* %P
* INPUT PARAMETERS:
* geometry: [str] Name of an Object File Format (OFF) or PLY file for complex field-geometry.
* xwidth: [m]               Width of opening.
* yheight: [m]              Height of opening.
* zdepth: [m]               Length of field.
* radius: [m]               Radius of field if it is cylindrical or spherical.

*
* %E
****************************************************************************/

DEFINE COMPONENT Pol_Bfield_stop
DEFINITION PARAMETERS ()
SETTING PARAMETERS (string geometry="", yheight=0, xwidth=0, zdepth=0, radius=0)
OUTPUT PARAMETERS()
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */

SHARE
%{
  %include "pol-lib"
%}
DECLARE
%{
  int shape;
%}
INITIALIZE
%{
    enum shapes {NONE=0, BOX, WINDOW, CYLINDER, SPHERE, ANY};
    /*if a start component is given, and no geometry is set, inherent geometry from start comp.*/
    if(geometry && strlen(geometry)){
        shape=ANY;
    }else if(xwidth && yheight && zdepth){
        shape=BOX;
    }else if (xwidth && yheight && !zdepth){
        shape=WINDOW;
    }else if(radius && yheight){
        shape=CYLINDER;
    }else if (radius) {
        shape=SPHERE;
    }else{
        shape=NONE;
    }
%}

TRACE
%{
    double t0,t1;
    int nofield=0;
    enum shapes {NONE=0, BOX, WINDOW, CYLINDER, SPHERE, ANY};
    /*enter through whatever object we are*/
    switch (shape){
        case BOX:
            box_intersect(&t0,&t1,x,y,z,vx,vy,vz,xwidth,yheight,zdepth);
	    if (t0>FLT_EPSILON) PROP_DT(t0);/*this is to get a hollow inside the field.*/
            else PROP_DT(t1);
            break;
        case CYLINDER:
            cylinder_intersect(&t0,&t1,x,y,z,vx,vy,vz,radius,yheight);/*this is to get a hollow inside the field.*/
            if (t0>FLT_EPSILON) PROP_DT(t0);
            else PROP_DT(t1);
            break;
        case WINDOW:
            PROP_Z0;
            /*terminate neutrons which miss the exit window*/
            if (2*x>xwidth || 2*x<-xwidth || 2*y>yheight || 2*y<-yheight){
                ABSORB;
            }
            break;
        default:
            PROP_Z0;
    }
    mcmagnet_pop(_particle);
%}

END
