Back to index

radiance  4R0+20100331
clip.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: clip.c,v 2.5 2003/06/26 00:58:09 schorsch Exp $";
00003 #endif
00004 /*
00005  *  clip.c - routine to clip 3D line segments to a box.
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 #include  "fvect.h"
00011 
00012 #include  "plocate.h"
00013 
00014 #define MAXITER             6      /* maximum possible number of iterations */
00015 
00016 
00017 int
00018 clip(ep1, ep2, min, max)    /* clip a line segment to a box */
00019 RREAL  *ep1, *ep2;
00020 FVECT  min, max;
00021 {
00022        int  itlim = MAXITER;
00023        int  loc1, loc2;
00024        int  accept;
00025        RREAL  *dp;
00026        double  d;
00027        register int  i, j;
00028 
00029        /*
00030         *     The Cohen-Sutherland algorithm is used to determine
00031         *  what part (if any) of the given line segment is contained
00032         *  in the box specified by the min and max vectors.
00033         *     The routine returns non-zero if any segment is left.
00034         */
00035        
00036        loc1 = plocate(ep1, min, max);
00037        loc2 = plocate(ep2, min, max);
00038 
00039                      /* check for trivial accept and reject */
00040                      /* trivial accept is both points inside */
00041                      /* trivial reject is both points to one side */
00042        
00043        while (!((accept = !(loc1 | loc2)) || (loc1 & loc2))) {
00044        
00045               if (itlim-- <= 0)    /* past theoretical limit? */
00046                      return(0);    /* quit fooling around */
00047 
00048               if (!loc1) {         /* make sure first point is outside */
00049                      dp = ep1; ep1 = ep2; ep2 = dp;
00050                      i = loc1; loc1 = loc2; loc2 = i;
00051               }
00052               
00053               for (i = 0; i < 3; i++) {          /* chop segment */
00054                      
00055                      if (loc1 & position(i) & BELOW) {
00056                             d = (min[i] - ep1[i])/(ep2[i] - ep1[i]);
00057                             ep1[i] = min[i];
00058                      } else if (loc1 & position(i) & ABOVE) {
00059                             d = (max[i] - ep1[i])/(ep2[i] - ep1[i]);
00060                             ep1[i] = max[i];
00061                      } else
00062                             continue;
00063                      
00064                      for (j = 0; j < 3; j++)
00065                             if (j != i)
00066                                    ep1[j] += (ep2[j] - ep1[j])*d;
00067                      break;
00068               }
00069               loc1 = plocate(ep1, min, max);
00070        }
00071        return(accept);
00072 }