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