/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2007, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Derotator
*
* %I
*
* Date: June 20th 2013
* Written by: Emmanuel Farhi
* Origin: <a href="http://www.ill.fr">ILL</a>
*
* The counterpart of the Rotator component.
*
* %Description
* A component which stops the rotative frame set by the Rotator component.
* Its position should better coincide with the Rotator instance.
* Components preceding the Derotator are rotating, all following are steady.
*
* Example:
*   R=Rotator(nu=14, phase=0)
*   ...
*   DR=Derotator(rotator="R")
*   AT (0,0,0) RELATIVE R
*
* %Parameters
* INPUT PARAMETERS:
* rotator: [string]  the name of the Rotator component used to initiate the rotation
*
* CALCULATED PARAMETERS:
* angle: [deg]          rotation angle
*
* %End
*******************************************************************************/

DEFINE COMPONENT Derotator
SETTING PARAMETERS (string rotator)
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */

TRACE
%{
  Rotation R;

  /* Name of Rotation uservar */
  char* Rot_varptr = *COMP_GETPAR3(Rotator, rotator, rot_var);

  /* Invert rotation matrix for use in derotation */
  rot_transpose(*(Rotation *) particle_getvar_void(_particle, Rot_varptr, NULL), R);

  /* apply rotation to centered coordinates */
  Coords tmp = coords_set(x,y,z);
  coords_get(rot_apply(R, tmp), &x, &y, &z);
  /* rotate speed */
  tmp = coords_set(vx,vy,vz);
  coords_get(rot_apply(R, tmp), &vx, &vy, &vz);
%}

END
