ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/clip.c
Revision: 2.2
Committed: Sun Dec 19 22:49:28 1993 UTC (30 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +7 -1 lines
Log Message:
fixed 1 in 1e9 bug caused by point on boundary of cube

File Contents

# User Rev Content
1 greg 2.2 /* Copyright (c) 1993 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * clip.c - routine to clip 3D line segments to a box.
9     *
10     * 8/28/85
11     */
12    
13 greg 1.2 #include "fvect.h"
14    
15 greg 1.1 #include "plocate.h"
16    
17 greg 2.2 #define MAXITER 6 /* maximum possible number of iterations */
18 greg 1.1
19 greg 2.2
20 greg 1.1 clip(ep1, ep2, min, max) /* clip a line segment to a box */
21 greg 1.2 FLOAT *ep1, *ep2;
22     FVECT min, max;
23 greg 1.1 {
24 greg 2.2 int itlim = MAXITER;
25 greg 1.1 int loc1, loc2;
26     int accept;
27 greg 1.2 FLOAT *dp;
28 greg 1.1 double d;
29     register int i, j;
30    
31     /*
32     * The Cohen-Sutherland algorithm is used to determine
33     * what part (if any) of the given line segment is contained
34     * in the box specified by the min and max vectors.
35     * The routine returns non-zero if any segment is left.
36     */
37    
38     loc1 = plocate(ep1, min, max);
39     loc2 = plocate(ep2, min, max);
40    
41     /* check for trivial accept and reject */
42     /* trivial accept is both points inside */
43     /* trivial reject is both points to one side */
44    
45     while (!((accept = !(loc1 | loc2)) || (loc1 & loc2))) {
46    
47 greg 2.2 if (itlim-- <= 0) /* past theoretical limit? */
48     return(0); /* quit fooling around */
49    
50 greg 1.1 if (!loc1) { /* make sure first point is outside */
51     dp = ep1; ep1 = ep2; ep2 = dp;
52     i = loc1; loc1 = loc2; loc2 = i;
53     }
54    
55     for (i = 0; i < 3; i++) { /* chop segment */
56    
57     if (loc1 & position(i) & BELOW) {
58     d = (min[i] - ep1[i])/(ep2[i] - ep1[i]);
59     ep1[i] = min[i];
60     } else if (loc1 & position(i) & ABOVE) {
61     d = (max[i] - ep1[i])/(ep2[i] - ep1[i]);
62     ep1[i] = max[i];
63     } else
64     continue;
65    
66     for (j = 0; j < 3; j++)
67     if (j != i)
68     ep1[j] += (ep2[j] - ep1[j])*d;
69     break;
70     }
71     loc1 = plocate(ep1, min, max);
72     }
73     return(accept);
74     }