
C ++********************************************************************
C                                                                      *
C  PREPCUB_Q_N.F                REWRITTEN        SEP 2003 PAWEL PENCZYK                                                                   *
C                               LDPX,LDPY,LDPZ   FEB 2005 ArDean Leith                                                        *
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   PREPCUB_Q_N(N,NN,IPCUBE,RI,MD,LDPX,LDPY,LDPZ)                                     *
C                                                                      *
C  PURPOSE:  CREATES IPCUBE & BCKE                                           
C                                                                      *
C  PARAMETERS:   BCKE          VOLUME                             RET.
C                BUF           IO BUFFER                          SENT
C                NN            NO. OF ROWS                        RET.
C                IPCUBE                                           RET.
C                NMAT                                             RET.
C                RI            RADIUS                             SENT
C                INPIC         INPUT UNIT                         SENT
C
C  NOTES:                                                              *
C           
C     IPCUBE:    1 - BEGINNING
C                2 - LENGTH
C                3 - IX
C                4 - IY
C                5 - IZ
C
C IMAGE_PROCESSING_ROUTINE                                             *
C
C23456789012345678901234567890123456789012345678901234567890123456789012
C***********************************************************************

        SUBROUTINE PREPCUB_Q_N(N,NN,IPCUBE,RI,MD,LDPX,LDPY,LDPZ)

        INTEGER            IPCUBE(5,NN)
        LOGICAL         :: FIRST,MD

        R  = RI*RI

        NN = 0

        DO I1=1,N
           T=I1-LDPZ
           XX=T*T

           DO I2=1,N
              T     = I2 - LDPY
              YY    = T * T + XX
              FIRST = .TRUE.

              DO I3=1,N
                 T  = I3 - LDPX
                 RC = T * T + YY
                 IF (FIRST)  THEN
C                   FIRST PIXEL ON THIS LINE,
                    IF (RC .GT. R)  CYCLE
                    FIRST = .FALSE.

                    NN    = NN+1
                    IF (MD) THEN
                       IPCUBE(1,NN) = I3
                       IPCUBE(2,NN) = I3
                       IPCUBE(3,NN) = I3-LDPX
                       IPCUBE(4,NN) = I2-LDPY
                       IPCUBE(5,NN) = I1-LDPZ
                   ENDIF
                 ELSE
C                   SECOND OR LATER PIXEL ON THIS LINE,
                    IF (MD) THEN
                       IF (RC .LE. R)  IPCUBE(2,NN)=I3
                    ENDIF
                ENDIF
              ENDDO
           ENDDO
        ENDDO

        END
