; just rough translation to IDL ---> don't use it yet, but it probably is OK
; this is needed for displaying target shapes

; ---> it don't need to declare formal parameters (even arrays)
; declare rm(4,4) and ignore (0,*), (*,0)
;all formal parameters do not have to be declared inside the routines

pro test
;trick to conform to FORTRAN indexing scheme. Primitive but
; works like a charm. No need to change FORTRAN code array dimensions!
rm=fltarr(3+1,3+1)
a=fltarr(3+1)
a(1)=1
a(2)=0
a(3)=0
;theta=!Pi/2. ---> there are small negative values (is this a problem ?)
;theta=20000*!Pi ---> this is clearly wrong, but it should not happen
rot2,a,theta,rm
print,rm(1:3,1:3)
stop
end


pro ROT2,A,THETA,RM
;***********************************************************************
;
; IDL notes. I am indexing like FORTRAN so in calling
; code you need to declare rm(4,4), a(4). This could be
; skipped if the algorithm was written as in vector times
; matrix form (which should be very easy to do).
;
; This subroutine finds the (3 by 3) rotation matrix for the
; given axis (vector) --- a and the rotation angle --- theta.
; On input:
; a(3)    --- axis of rotation  (vector, any length)
; theta   --- angle of rotation (in radians) around vector a
; On output:
; rm(3,3) --- rotation matrix such that new vector v2 is obtained
;             from the old vector v1 by the transformation:
;             v2 = rm \dot v1
; (If more rotations are needed: generate matrices rm with the
; help of this subroutine and (matrix) multiply them before
; performing v2 = rm \dot v1.)
;
; Reference:
; Leubner, C., 1977,  Coordinate-free  rotation operator,
; Am. J. Phys. 47, 727---729.
;
; History:
; Written by PJF, May 1989, for use in the rotation-averaged
; DDA calculations.
;
; Copyright (C) 1993, B.T. Draine and P.J. Flatau
; This code is covered by the GNU General Public License.
;***********************************************************************
; pre-calculate some stuff:
;     .. Scalar Arguments ..
;      REAL THETA
;     ..
;     .. Array Arguments ..
;      REAL A(3), RM(3,3)
;        a=fltarr(3)
;        rm=fltarr(3,3)

;     ..
;     .. Local Scalars ..
;      REAL ANORM, CT, ST
;      INTEGER I, J
;     ..
;     .. Local Arrays ..
;      REAL AHAT(3)
       ahat=fltarr(3+1)
;     ..
;     .. External Subroutines ..
;      EXTERNAL XNORM3
;     ..
;     .. Intrinsic Functions ..
;      INTRINSIC COS, SIN
;     ..
      CT = COS(THETA)
      ST = SIN(THETA)
      XNORM3,A,ANORM
      AHAT(1) = A(1)/ANORM
      AHAT(2) = A(2)/ANORM
      AHAT(3) = A(3)/ANORM
;  cos(\theta) {\bf 1}  term:
      RM(1,1) = CT
      RM(2,2) = CT
      RM(3,3) = CT
; Skew-term  a \times {\bf 1}:
      RM(1,2) = -ST*AHAT(3)
      RM(1,3) = ST*AHAT(2)
      RM(2,1) = ST*AHAT(3)
      RM(2,3) = -ST*AHAT(1)
      RM(3,1) = -ST*AHAT(2)
      RM(3,2) = ST*AHAT(1)
; aa-dyadic (outer-product):
      for i=1,3 do begin ;DO 20 I = 1, 3
          for j=1, 3 do begin ;DO 10 J = 1, 3
              RM(I,J) = RM(I,J) + (1.-CT)*AHAT(J)*AHAT(I)
      endfor ;   10     CONTINUE
      endfor ;  20 CONTINUE
      RETURN
      END
 pro XNORM3,A,XN
; auxilary for "rotation" routines
;
; Copyright (C) 1993, B.T. Draine and P.J. Flatau
; This code is covered by the GNU General Public License.
;***********************************************************************
;     .. Scalar Arguments ..
;      REAL XN
;     ..
;     .. Array Arguments ..
;      REAL A(3)
;      a=fltarr(3)
;     ..
;     .. Intrinsic Functions ..
;      INTRINSIC SQRT
;     ..
      XN = SQRT(A(1)^2+A(2)^2+A(3)^2)
      RETURN
      END
pro MULT3,A,B,C
;***********************************************************************
; Purpose:
; Given:  A(I,J)= 3x3 matrix
;         B(J,K)= 3x3 matrix
; Returns:C(I,K)= matrix product of A and B
; This is auxilary for "rotation" routines
;
; Copyright (C) 1993, B.T. Draine and P.J. Flatau
; This code is covered by the GNU General Public License.
;***********************************************************************
; Arguments:
;      REAL A(3,3),B(3,3),C(3,3)
;        a=fltarr(3,3)
;        b=fltarr(3,3)
;        c=fltarr(3,3)

; Local variables:
;      REAL S
;      INTEGER I,J,K
;
       for j=1,3 do begin;      DO 30 J = 1, 3
          for i=1,3 do begin ;DO 20 I = 1, 3
              S = 0.
              for k=1, 3 do begin ;DO 10 K = 1, 3
                  S = S + A(I,K)*B(K,J)
       endfor;   10         CONTINUE
              C(I,J) = S
       endfor;    20     CONTINUE
       endfor;   30 CONTINUE
      RETURN
      END
pro PROD3,RM,VIN,VOUT
; Given:  RM(I,J)=3x3 matrix
;         VIN(J)=3-vector
; Returns:VOUT(I)=RM*VIN
; 
; Auxilary for "rotation" routines
;
; Copyright (C) 1993, B.T. Draine and P.J. Flatau
; This code is covered by the GNU General Public License.
;***********************************************************************
; Arguments;
;      REAL RM(3,3), VIN(3), VOUT(3)
; Local variables:
;      INTEGER I,J
;
      for i=1, 3 do begin ;DO 20 I = 1, 3
         VOUT(I) = 0.
         for j=1,3 do begin ;DO 10 J = 1, 3
            VOUT(I) = VOUT(I) + RM(I,J)*VIN(J)
      endfor ;   10    CONTINUE
      endfor ;   20 CONTINUE
      RETURN
      END
pro PROD3C,RM,CVIN,CVOUT
; Arguments:
;      REAL RM(3,3)
;      COMPLEX CVIN(3),CVOUT(3)
; Local variables:
;      INTEGER I,J
; Given:
;       RM=3x3 rotation matrix
;       CVIN=complex 3-vector
; Computes:
;       CVOUT=RM \dot CVIN
;
; Copyright (C) 1993, B.T. Draine and P.J. Flatau
; This code is covered by the GNU General Public License.
;***********************************************************************
      for i=1, 3 do begin ;DO 20 I=1,3
         CVOUT(I)=complex(0.,0.)
         for j=1,3 do begin ;DO 10 J=1,3
            CVOUT(I)=CVOUT(I)+RM(I,J)*CVIN(J)
      endfor ; 10      CONTINUE
      endfor ;  20   CONTINUE
      RETURN
      END
pro  PROD3V,RM,VIN,VOUT,NV,NVMAX
; Arguments:
;      INTEGER NV,NVMAX
;      REAL RM(3,3),VIN(3,NVMAX),VOUT(3,NVMAX)
; Local variables:
;      INTEGER I,J,N
; Given:
;       RM = 3x3 rotation matrix
;       VIN = NV 3-vectors
; Computes:
;       VOUT=RM \dot VIN for each of the NV 3-vectors
;
; Copyright (C) 1993,1996 B.T. Draine and P.J. Flatau
; History
; 96.08.09 (BTD) trivial modification to remove obsolescent construct.
; end history
; This code is covered by the GNU General Public License.
;***********************************************************************
      for i=1, 3 do begin ;     DO 20 I=1,3
        for n=1, nv  do begin ;       DO 10 N=1,NV
            VOUT(I,N)=0.
      endfor;  10      CONTINUE
      endfor; 20   CONTINUE
;
      for i=1,3 do begin ;DO 50 I=1,3
         for j=1,3 do begin ;DO 40 J=1,3
            for n=1, nv do begin; do begin DO 30 N=1,NV
               VOUT(I,N)=VOUT(I,N)+RM(I,J)*VIN(J,N)
      endfor ; 30         CONTINUE
      endfor ;  40      CONTINUE
      endfor ;  50   CONTINUE
      RETURN
      END

