C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C**************************
C**     LRL_OVM          **
C**************************
C
CD-Routine: Determine Spot Ellipse Overlap (using mask) - LRL_OVM
C This routine determines whether two elliptical spots, with or without
C a background border, overlap using a mask procedure based on code from
C the UPDATE program by T.J. Greenhough and A.K. Shrive.
CD-end
CD-Fortran:
      LOGICAL FUNCTION LRL_OVM (SPOT_L, SPOT_W, SPOTFAC, SPOT_B, CTOF,
     +                      MM_RAST_X, MM_RAST_Y, X1, Y1, X2, Y2, ITYP)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Determine if two elliptical reflection masks overlap using
C          mask procedure. 
C
C Author:  John W. Campbell, July 1993 (based on code from the program UPDATE
C                                       of T.J. Greenhough and A.K. Shrive)
C
C Return:  .TRUE. if spots overlap, otherwise .FALSE.
C
C Arguments:
C
      REAL SPOT_L
      REAL SPOT_W
      REAL SPOTFAC
      REAL SPOT_B
      REAL CTOF
      REAL MM_RAST_X
      REAL MM_RAST_Y
      REAL X1
      REAL Y1
      REAL X2
      REAL Y2
      INTEGER ITYP
CD-Parameters:   
C SPOT_L    r (R)  Spot length in mm.
C SPOT_W    r (R)  Spot width in mm.
C SPOTFAC   r (R)  Spot expansion factor
C SPOT_B    r (R)  Background border width in rasters
C CTOF      r (R)  Crystal to plate distance in mm.
C MM_RAST_X r (R)  Conversion factor mm to x-rasters 
C MM_RAST_Y r (R)  Conversion factor mm to y-rasters
C X1        r (R)  'x' position of reference spot in mm. 
C Y1        r (R)  'y' position of reference spot in mm.
C X2        r (R)  'x' position of second spot in mm.
C Y2        r (R)  'y' position of second spot in mm.
C ITYP      i (R)  Flag = 1, Check that (spot + unused pixel + background 
C                            border) does not overlap with next spot 
C                       = 2, Check that (spot + background border) does not
C                            overlap with next spot
C                       = 3, Check overlap strictly for spot area
C
C           Returns .TRUE. if spots overlap, otherwise .FALSE.
C
CD-end
C
C====== LOCALS:
C
      INTEGER IX1
      INTEGER IY1
      INTEGER IX2
      INTEGER IY2
      INTEGER IXS
      INTEGER IXE
      INTEGER IYS
      INTEGER IYE
      INTEGER IOFF
      INTEGER IX
      INTEGER IY
      REAL CMASK
      REAL BMINOR
      REAL AMAJOR
      REAL RADIUS
      REAL AM1
      REAL BM1
      REAL AM2
      REAL BM2
      REAL STH1
      REAL CTH1
      REAL STH2
      REAL CTH2
      REAL XPRIME
      REAL YPRIME
      REAL ELLIP

C     INTEGER IX1       !X ccordinate of 1'st spot centre (rasters)
C     INTEGER IY1       !Y coordinate of 1'st spot centre (rasters)
C     INTEGER IX2       !X coordinate of 2'nd spot centre (rasters)
C     INTEGER IY2       !Y coordinate of 2'nd spot centre (rasters)
C     INTEGER IXS       !X start of box surrounding 1'st spot (rasters)
C     INTEGER IXE       !Y start of box surrounding 1'st spot (rasters)
C     INTEGER IYS       !X start of box surrounding 2'nd spot (rasters)
C     INTEGER IYE       !Y start of box surrounding 2'nd spot (rasters)
C     INTEGER IOFF      !Max. offset from centre of box (rasters)
C     INTEGER IX        !Loop x counter for box
C     INTEGER IY        !Loop y counter for box
C     REAL CMASK        !Major axis of ellipse uncorrected for radius (rasters)
C     REAL BMINOR       !Minor axis of ellipse (rasters)
C     REAL AMAJOR       !Corrected major axis of ellipse (rasters)
C     REAL RADIUS       !Radius of spot from centre (rasters)
C     REAL AM1          !1/(amajor1*amajor1)
C     REAL BM1          !1/(bminor1*bminor1)
C     REAL AM2          !1/(amajor2*amajor2)
C     REAL BM2          !1/(bminor2*bminor2)
C     REAL STH1         !sin(theta) for 1'st spot
C     REAL CTH1         !cos(theta) for 1'st spot
C     REAL STH2         !sin(theta) for 2'nd spot
C     REAL CTH2         !sin(theta) for 2'nd spot
C     REAL XPRIME       !x coordinate wrt local ellipse axes (rasters)
C     REAL YPRIME       !y coordinate wrt local ellipse axes (rasters)
C     REAL ELLIP        !Ellipse function test value
C
C
C-------------------------------------------------------------------------------
C
      LRL_OVM = .FALSE.
      CMASK = SPOT_L*MM_RAST_X/2.0
C
C====== Get basic parameters for 1'st spot
C
      IX1 = NINT(X1*MM_RAST_X)
      IY1 = NINT(Y1*MM_RAST_Y)
      RADIUS = SQRT(X1*X1 + Y1*Y1)
      AMAJOR = CMASK*(1.0+SPOTFAC*RADIUS/
     +         (CTOF))
      BMINOR = SPOT_W*MM_RAST_X/2.0
      IF (ITYP.EQ.1) THEN
         AMAJOR = AMAJOR + 1.0 + SPOT_B
         BMINOR = BMINOR + 1.0 + SPOT_B
      ELSE IF (ITYP.EQ.2) THEN
         AMAJOR = AMAJOR + SPOT_B
         BMINOR = BMINOR + SPOT_B
      END IF
      AM1 = 1.0/(AMAJOR*AMAJOR)
      BM1 = 1.0/(BMINOR*BMINOR)
      IOFF = NINT(AMAJOR+1.0)
      IXS = IX1 - IOFF
      IXE = IX1 + IOFF
      IYS = IY1 - IOFF
      IYE = IY1 + IOFF
      STH1 = Y1/RADIUS
      CTH1 = X1/RADIUS
C
C====== Get basic parameters for 2'nd spot
C
      IX2 = NINT(X2*MM_RAST_X)
      IY2 = NINT(Y2*MM_RAST_Y)
      RADIUS = SQRT(X2*X2 + Y2*Y2)
      AMAJOR = CMASK*(1.0+SPOTFAC*RADIUS/
     +         (CTOF))
      BMINOR = SPOT_W*MM_RAST_X/2.0
      AM2 = 1.0/(AMAJOR*AMAJOR)
      BM2 = 1.0/(BMINOR*BMINOR)
      STH2 = Y2/RADIUS
      CTH2 = X2/RADIUS
C
C====== Check for overlap
C
      DO 150 IX = IXS, IXE
         DO 100 IY = IYS, IYE
            XPRIME = (IX-IX1)*CTH1 + (IY-IY1)*STH1
            YPRIME = (IY-IY1)*CTH1 - (IX-IX1)*STH1
            ELLIP = XPRIME*XPRIME*AM1 + YPRIME*YPRIME*BM1
            IF (ELLIP.LE.1.0) THEN
               XPRIME = (IX-IX2)*CTH2 + (IY-IY2)*STH2
               YPRIME = (IY-IY2)*CTH2 - (IX-IX2)*STH2
               ELLIP = XPRIME*XPRIME*AM2 + YPRIME*YPRIME*BM2
               IF (ELLIP.LE.1.0) THEN
                  LRL_OVM = .TRUE.
                  RETURN
               END IF
            END IF
100      CONTINUE
150   CONTINUE
      RETURN
      END
