C
C
C     NAME - STMAP      "STEREOSCOPIC MAPPER"
C
C             BY  BRIAN ARMBRUSTER            SYSTEMS DESIGN ENGINEERING
C                 MIKE LAPRES                 UNIVERSITY OF WATERLOO
C                 DON SHAW                    APRIL, 1974
C
C
C     PURPOSE - 'STMAP' TRANSFORMS, IN A WAY SIMILAR TO MAPPER,
C               AN OBJECT IN X-Y-Z CO-ORDINATES INTO TWO IMAGES
C               IN X-Y CO-ORDINATES ON THE VIEW PLANE. THESE 
C               IMAGES, ONCE DISPLAYED ON THE CRT, MAY BE VIEWED IN 
C               STEREOSCOPIC 3-D USING THE STEREOSCOPIC VIEWER.
C
C
C     CALLING SEQUENCE
C          
C          ---
C          INTEGER ILSTRT,ILEND,IRSTRT,IREND,I3STRT,I3END
C          REAL DIST,SEP,STPICT,OBJECT
C          ---
C          COMMON /IMAGE/ILSTRT,ILEND,IRSTRT,IREND,STPICT(4,60)
C          COMMON /BULK/I3STRT,I3END,OBJECT(6,30)
C          ---
C          <COMMENT: ROTER MUST BE CALLED>
C          ---
C          CALL STMAP(DIST,SEP)
C          ---
C
C
C     ARGUMENT DESCRIPTION
C
C       DIST      )
C       BULK      )
C       I3STRT    )  EXACTLY AS IN MAPPER
C       I3END     )
C       OBJECT    )  
C
C       SEP        THE DISTANCE BETWEEN THE LEFT AND RIGHT EYE.
C
C       IMAGE      THE NAMED COMMON AREA INTO WHICH 'STMAP'
C                  PLACES THE TRANSFORMED 2-DIMENSIONAL DATA.
C
C       ILSTRT     SPECIFIED BY USER: THE LINE NUMBER IN 
C                  'STPICT' WHERE 'STMAP' IS TO BEGIN PLACING
C                  DATA (FIRST LINE OF THE LEFT IMAGE).
C
C       ILEND      RETURNED BY 'STMAP': THE LINE NUMBER OF THE 
C                  LAST LINE IN THE LEFT IMAGE.
C
C       IRSTRT     RETURNED BY 'STMAP': THE LINE NUMBER OF THE 
C                  FIRST LINE IN THE RIGHT IMAGE.
C       
C       IREND      RETURNED BY 'STMAP': THE LINE NUMBER OF THE
C                  LAST LINE IN THE RIGHT IMAGE.
C
C       STPICT     THE (4 X N) ARRAY INTO WHICH 'STMAP' PLACES
C                  THE TRANSFORMED IMAGES (BOTH LEFT & RIGHT).
C
C
C
      SUBROUTINE STMAP(DIST,SEP)
      INTEGER L,R,RL
C
C TRANSFORMATION MATRIX PRODUCED BY ROTER
      COMMON/GRXPQK/S11,S12,S13,S21,S22,S23,S31,S32,S33,T11,T21,T31
C
C 3-D OBJECT DATA
      COMMON/BULK/I3STRT,I3END,OB(6,60)
C
C 2-D STEREOSCOPIC DATA
      COMMON/IMAGE/ILSTRT,ILEND,IRSTRT,IREND,STPICT(4,120)
C
      DATA BOUND/1.0E75/
C
C INITIALIZE LINE COUNTERS
      L=ILSTRT
      IRSTRT=L+I3END-I3STRT+1
      R=IRSTRT
C
      EY=SEP/2.0
      ED=EY*DIST
C
      DO 100 J=I3STRT,I3END                                                     
C
C CALCULATE ROTATED POINTS OF ONE OBJECT LINE
      TEMPX=OB(1,J)*S11+OB(2,J)*S12+OB(3,J)*S13+T11                            
      TEMPY=OB(1,J)*S21+OB(2,J)*S22+OB(3,J)*S23+T21                            
      TEMPZ=OB(1,J)*S31+OB(2,J)*S32+OB(3,J)*S33+T31                            
      TEMPX2=OB(4,J)*S11+OB(5,J)*S12+OB(6,J)*S13+T11                            
      TEMPY2=OB(4,J)*S21+OB(5,J)*S22+OB(6,J)*S23+T21                            
      TEMPZ2=OB(4,J)*S31+OB(5,J)*S32+OB(6,J)*S33+T31                            
C
C CHECK IF ALL POINTS ARE IN FRONT OF EYE
      IF(TEMPZ)3,3,1                                                            
3     IF(TEMPZ2)100,100,2                                                       
C
C IF POINT 1 IS BEHIND THE EYE & POINT 2 IS IN FRONT 
C   THE TWO POINTS ARE EXCHANGED
2     BIN=TEMPX                                                                 
      TEMPX=TEMPX2                                                              
      TEMPX2=BIN                                                                
      BIN=TEMPY                                                                 
      TEMPY=TEMPY2                                                              
      TEMPY2=BIN                                                                
      BIN=TEMPZ                                                                 
      TEMPZ=TEMPZ2                                                              
      TEMPZ2=BIN                                                                
C
C CALCULATION OF POINT 1 ON PICTURE PLANE
1     DX=DIST*TEMPX                                                             
      EZ=EY*TEMPZ                                                             
      STPICT(1,L)=(DX+ED-EZ)/TEMPZ                                         
      STPICT(2,L)=DIST*TEMPY/TEMPZ                                             
      STPICT(1,R)=(DX-ED+EZ)/TEMPZ                                       
      STPICT(2,R)=STPICT(2,L)                                               
C
      IF(TEMPZ2)5,5,4
C 
C CALCULATION OF POINT 2 IF Z>0
4     DX2=DIST*TEMPX2
      EZ2=EY*TEMPZ2
      STPICT(3,L)=(DX2+ED-EZ2)/TEMPZ2                                      
      STPICT(4,L)=DIST*TEMPY2/TEMPZ2                                            
      STPICT(3,R)=(DX2-ED+EZ2)/TEMPZ2                                    
      STPICT(4,R)=STPICT(4,L)
      GOTO 101
C
C CALCULATION OF POINT 2 IF Z<0
5     XT=TEMPX2*TEMPZ-TEMPX*TEMPZ2                                              
      YT=TEMPY2*TEMPZ-TEMPY*TEMPZ2                                              
      EYZ12=EY*(TEMPZ-TEMPZ2)                                                    
      XT=XT+EYZ12                                                               
      RL=R-L
      DO 98 KK=L,R,RL                                                           
      B =BOUND
      IF(ABS(XT).GT.ABS(YT)) GO TO 7                                            
      IF(YT)8,9,10                                                              
8     B=-B                                                                      
10    STPICT(4,KK)=B                                                             
      STPICT(3,KK)=STPICT(1,KK)+(B-STPICT(2,KK))*XT/YT                             
      GO TO 99                                                                 
9     STPICT(3,KK)=STPICT(1,KK)                                                   
      STPICT(4,KK)=STPICT(2,KK)                                                   
      GOTO 99                                                                  
7     IF(XT)11,9,12                                                             
11    B=-B                                                                      
12    STPICT(3,KK)=B                                                             
      STPICT(4,KK)=STPICT(2,KK)+(B-STPICT(1,KK))*YT/XT                               
99    XT=XT-2.0*EYZ12                                                             
98    CONTINUE
C
C INCREMENT LINE COUNTERS
101   L=L+1
      R=R+1
100   CONTINUE                                                                  
C
C RETURN NUMBERS OF LAST LINES
      ILEND=L-1
      IREND=R-1
      RETURN                                                                    
      END                                                                       
