/*******************************************************************************
*
* McXtrace, X-ray tracing package
*         Copyright 1997-2002, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Slit
*
* %I
* Written by: Erik Knudsen
* Date: June 16, 2009
* Origin: Risoe
* Release: McXtrace 0.1
*
* Rectangular/circular slit, duplicated
*
* %Description
* Based on Slit-comp by Kim Lefmann and Henrik Roennow
* A simple rectangular or circular slit. You may either
* specify the radius (circular shape), which takes precedence,
* or the rectangular bounds. The slits are separated with 'd', and arranged along X.
* No transmission around the slit is allowed.
* If cutting option is used, low-weight x-rays are ABSORBED
*
* Example: Slit_N(xwidth=0.01, yheight=0.01, d=0.02)
*          Slit_N(radius=0.01, cut=1e-10, d=0.03)
*
* %Parameters
* INPUT PARAMETERS
*
* radius:   [m] Radius of slit in the z=0 plane, centered at Origo
* xwidth:   [m] Width of slit. Overrides xmin,xmax.
* yheight:  [m] Height of slit. Overrides ymin,ymax.
* N:        [1] Number of slit openings along X
* d:        [m] Separation of slits
*
* Optional parameters:
* cut:  Lower limit for allowed weight (1)
*
* %End
*******************************************************************************/


DEFINE COMPONENT Slit_N
SETTING PARAMETERS (radius=0, cut=0, xwidth=0, yheight=0, N=2, d=0)

DECLARE
%{
  double xmax;
  double ymax;
  double xmin;
  double ymin;
%}

INITIALIZE
%{
  if (xwidth)  { xmax=xwidth/2;  xmin=-xmax; }
  if (yheight) { ymax=yheight/2; ymin=-ymax; }
  if (xmin == 0 && xmax == 0 && ymin == 0 && ymax == 0 && radius == 0)
    { fprintf(stderr,"Slit_N: %s: Error: give geometry\n", NAME_CURRENT_COMP); exit(-1); }
  if ( (radius && d<2*radius) || d<xwidth){
      fprintf(stderr,"Slit_N: %s: Error: Separation of slits less than slit width\n",NAME_CURRENT_COMP);exit(-1);}


%}

TRACE
%{
  PROP_Z0;
  double xx=-((N-1)/2.0)*d;
  int i=0;
  while(x>xx && i<N){
    i++;
    xx+=d;
  }
  if( fabs(x-xx)>fabs(x-(xx-d))){
    i--;
    xx-=d;
  }
  xx=x-xx;

  if (((radius == 0) && (xx<xmin || xx>xmax || y<ymin || y>ymax))
      || ((radius != 0) && (xx*xx + y*y > radius*radius)))
    ABSORB;
  else
    if (p < cut)
      ABSORB;
    else
      SCATTER;
%}

MCDISPLAY
%{
  int i;
  
  for(i=0;i<N;i++){
    double xx;
    xx=(-(N-1)/2.0 +i)*d;
    if (radius == 0) {
      double xw, yh;
      xw = (xmax - xmin)/2.0;
      yh = (ymax - ymin)/2.0;
      multiline(3, xmin+xx-xw, (double)ymax, 0.0,
          (double)xmin+xx, (double)ymax, 0.0,
          (double)xmin+xx, ymax+yh, 0.0);
      multiline(3, xmax+xx+xw, (double)ymax, 0.0,
          (double)xmax+xx, (double)ymax, 0.0,
          (double)xmax+xx, ymax+yh, 0.0);
      multiline(3, xmin+xx-xw, (double)ymin, 0.0,
          (double)xmin+xx, (double)ymin, 0.0,
          (double)xmin+xx, ymin-yh, 0.0);
      multiline(3, xmax+xx+xw, (double)ymin, 0.0,
          (double)xmax+xx, (double)ymin, 0.0,
          (double)xmax+xx, ymin-yh, 0.0);
    } else {
      circle("xy",xx,0,0,radius);
    }
  }
%}

END
