
C ++********************************************************************
C                                                                      *
C                                                                      *
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
C  PURPOSE:                                                            *
C                                                                      *
C           CALCULATES THETA ANGLE DIFFERENCE BETWEEN
C           TWO VIEWS OF APPROXIMATELY THE SAME THETA
C           returns new theta, not the difference !
C
C PARAMETERS:
C     XYPTS(2,LS,LV) = ARRAY OF POINTS TO BE CHECKED
C     IVIEW = INDEX OF IMAGE TO BE CHECKED
C     P3D(3,LS) = 3-D MODEL
C     SCALE = DIFFERENCE IN SCALE BETWEEN IMAGES
C     PSI = IN-PLANE CORRECTION
C     THETA = TILT ANGLE OF IVIEW
C OUTPUT:
C     THETA = TILT DIFFERENCE
C
C***********************************************************************

      SUBROUTINE MRTHETA(RPT,VPT,IVIEW,P3D,THETA,PTACTIVE,NUMPTS,NTPT)

      PARAMETER (LV=300)
      PARAMETER (LS=256)

      LOGICAL     PTACTIVE(LS,LV)
      INTEGER     NUMPTS(LV)
      DIMENSION   RPT(2,LS),VPT(2,LS),P3D(3,LS)
      DIMENSION   CPJ(2,LS),CXY(2,LS)

      SY = SIN(THETA)
      CY = COS(THETA)

C     COMPUTE PROJECTION WITH X-Z POINTS AND VIEW WITH X
C     SO CAN GET COMMON CENTER OF MASS X.
      DO  I=1,NUMPTS(IVIEW)
	  IF (PTACTIVE(I,IVIEW))  THEN
             CPJ(1,I) = P3D(1,I) * SY + P3D(3,I) * CY
             CPJ(2,I) = RPT(1,I)
             CXY(2,I) = VPT(1,I)
	  ENDIF
      ENDDO

      DO  I=1,NUMPTS(IVIEW)
C         COMPUTE VIEW WITH X-Z POINTS
	  IF (PTACTIVE(I,IVIEW))  THEN
            TOTD2 = CPJ(1,I)**2 + CPJ(2,I)**2
            TMPZ2 = TOTD2 - (CXY(2,I)**2)
            TMPZ  = 0.0
            IF (TMPZ2 .GT. 0.0) TMPZ = SQRT(TMPZ2)
            CXY(1,I) = SIGN(TMPZ,CPJ(1,I))
	  ENDIF
      ENDDO

      CALL MRANG2(CPJ,CXY,IVIEW,THDIF,PTACTIVE,NTPT)
      THETA = THETA - THDIF

      END

