1 |
greg |
1.1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
|
|
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 |
|
|
|
18 |
|
|
clip(ep1, ep2, min, max) /* clip a line segment to a box */ |
19 |
greg |
1.2 |
FLOAT *ep1, *ep2; |
20 |
|
|
FVECT min, max; |
21 |
greg |
1.1 |
{ |
22 |
|
|
int loc1, loc2; |
23 |
|
|
int accept; |
24 |
greg |
1.2 |
FLOAT *dp; |
25 |
greg |
1.1 |
double d; |
26 |
|
|
register int i, j; |
27 |
|
|
|
28 |
|
|
/* |
29 |
|
|
* The Cohen-Sutherland algorithm is used to determine |
30 |
|
|
* what part (if any) of the given line segment is contained |
31 |
|
|
* in the box specified by the min and max vectors. |
32 |
|
|
* The routine returns non-zero if any segment is left. |
33 |
|
|
*/ |
34 |
|
|
|
35 |
|
|
loc1 = plocate(ep1, min, max); |
36 |
|
|
loc2 = plocate(ep2, min, max); |
37 |
|
|
|
38 |
|
|
/* check for trivial accept and reject */ |
39 |
|
|
/* trivial accept is both points inside */ |
40 |
|
|
/* trivial reject is both points to one side */ |
41 |
|
|
|
42 |
|
|
while (!((accept = !(loc1 | loc2)) || (loc1 & loc2))) { |
43 |
|
|
|
44 |
|
|
if (!loc1) { /* make sure first point is outside */ |
45 |
|
|
dp = ep1; ep1 = ep2; ep2 = dp; |
46 |
|
|
i = loc1; loc1 = loc2; loc2 = i; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
for (i = 0; i < 3; i++) { /* chop segment */ |
50 |
|
|
|
51 |
|
|
if (loc1 & position(i) & BELOW) { |
52 |
|
|
d = (min[i] - ep1[i])/(ep2[i] - ep1[i]); |
53 |
|
|
ep1[i] = min[i]; |
54 |
|
|
} else if (loc1 & position(i) & ABOVE) { |
55 |
|
|
d = (max[i] - ep1[i])/(ep2[i] - ep1[i]); |
56 |
|
|
ep1[i] = max[i]; |
57 |
|
|
} else |
58 |
|
|
continue; |
59 |
|
|
|
60 |
|
|
for (j = 0; j < 3; j++) |
61 |
|
|
if (j != i) |
62 |
|
|
ep1[j] += (ep2[j] - ep1[j])*d; |
63 |
|
|
break; |
64 |
|
|
} |
65 |
|
|
loc1 = plocate(ep1, min, max); |
66 |
|
|
} |
67 |
|
|
return(accept); |
68 |
|
|
} |