C++*********************************************************************
C
C HIANG.F
C
C **********************************************************************
C=* FROM: SPIDER - MODULAR IMAGE PROCESSING SYSTEM.   AUTHOR: J.FRANK  *
C=* Copyright (C) 1985-2005  Health Research Inc.                      *
C=*                                                                    *
C=* HEALTH RESEARCH INCORPORATED (HRI),                                *   
C=* ONE UNIVERSITY PLACE, RENSSELAER, NY 12144-3455.                   *
C=*                                                                    *
C=* Email:  spider@wadsworth.org                                       *
C=*                                                                    *
C=* This program is free software; you can redistribute it and/or      *
C=* modify it under the terms of the GNU General Public License as     *
C=* published by the Free Software Foundation; either version 2 of the *
C=* License, or (at your option) any later version.                    *
C=*                                                                    *
C=* This program is distributed in the hope that it will be useful,    *
C=* but WITHOUT ANY WARRANTY; without even the implied warranty of     *
C=* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
C=* General Public License for more details.                           *
C=*                                                                    *
C=* You should have received a copy of the GNU General Public License  *
C=* along with this program; if not, write to the                      *
C=* Free Software Foundation, Inc.,                                    *
C=* 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.      *
C=*                                                                    *
C **********************************************************************
C
C    HIANG
C
C IMAGE_PROCESSING_ROUTINE
C
C23456789012345678901234567890123456789012345678901234567890123456789012
C--*********************************************************************

        SUBROUTINE  HIANG(ANG,NANG,DM,LB,LO)

        DIMENSION  ANG(3,NANG),LB(NANG),DM(9,NANG)
        DOUBLE PRECISION  CPHI,SPHI,CTHE,STHE,CPSI,SPSI
        DOUBLE PRECISION  QUADPI,DGR_TO_RAD
        PARAMETER (QUADPI = 3.141592653589793238462643383279502884197)
        PARAMETER (DGR_TO_RAD = (QUADPI/180))
C ANG(1 - PHI, ANG(2 - THETA, ANG(3 - PSI
        DO    I=1,NANG
           LB(I)=1
           if(ANG(2,i).ge.90.0)  then
              ANG(2,i)=180.0-ANG(2,i)
              ANG(1,i)=ANG(1,i)+180.0
              if(ANG(1,i).ge.360.0)  ANG(1,i)=ANG(1,i)-360.0
           endif
        ENDDO
        DO    i=1,NANG
           JJJ=INT(100.0*ANG(1,I))
           ANG(1,I)=FLOAT(JJJ)/100.0
        ENDDO
        DO    I=1,NANG-1
           if(LB(I).NE.0)  then
              do  J=I+1,NANG
                 if(LB(I).ne.0)  then
                    if(ANG(1,i).eq.ANG(1,j).and.ANG(2,i).eq.
     &                 ANG(2,j)) then
                       LB(i)=LB(i)+1
                       LB(j)=0
                    endif
                 endif
              enddo
           endif
        ENDDO
        lo=0
        DO    I=1,NANG
           if(LB(I).ne.0)  then
              lo=lo+1
              ANG(1,lo)=ANG(1,i)
              ANG(2,lo)=ANG(2,i)
C -           PSI SET TO ZERO - IRRELEVANT
              ANG(3,lo)=0.0
              LB(LO)=LB(I)
           endif
        ENDDO
C       NANG=LO
C
        DO    I=1,LO
           CPHI=DCOS(DBLE(ANG(1,I))*DGR_TO_RAD)
           SPHI=DSIN(DBLE(ANG(1,I))*DGR_TO_RAD)
           CTHE=DCOS(DBLE(ANG(2,I))*DGR_TO_RAD)
           STHE=DSIN(DBLE(ANG(2,I))*DGR_TO_RAD)
           CPSI=1.0D0
           SPSI=0.0D0
C
           DM(1,I)=CPHI*CTHE*CPSI-SPHI*SPSI
           DM(2,I)=SPHI*CTHE*CPSI+CPHI*SPSI
           DM(3,I)=-STHE*CPSI
           DM(4,I)=-CPHI*CTHE*SPSI-SPHI*CPSI
           DM(5,I)=-SPHI*CTHE*SPSI+CPHI*CPSI
           DM(6,I)=STHE*SPSI
           DM(7,I)=STHE*CPHI
           DM(8,I)=STHE*SPHI
           DM(9,I)=CTHE     
        ENDDO
        END
