1 |
/* Copyright (c) 1993 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 "fvect.h" |
14 |
|
15 |
#include "plocate.h" |
16 |
|
17 |
#define MAXITER 6 /* maximum possible number of iterations */ |
18 |
|
19 |
|
20 |
clip(ep1, ep2, min, max) /* clip a line segment to a box */ |
21 |
FLOAT *ep1, *ep2; |
22 |
FVECT min, max; |
23 |
{ |
24 |
int itlim = MAXITER; |
25 |
int loc1, loc2; |
26 |
int accept; |
27 |
FLOAT *dp; |
28 |
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 |
if (itlim-- <= 0) /* past theoretical limit? */ |
48 |
return(0); /* quit fooling around */ |
49 |
|
50 |
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 |
} |