ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/face.c
Revision: 1.3
Committed: Fri Jul 28 14:27:44 1989 UTC (34 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +2 -1 lines
Log Message:
minor bug fix for zero area faces

File Contents

# Content
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 * face.c - routines dealing with polygonal faces.
9 *
10 * 8/30/85
11 */
12
13 #include "standard.h"
14
15 #include "object.h"
16
17 #include "face.h"
18
19 /*
20 * A face is given as a list of 3D vertices. The normal
21 * direction and therefore the surface orientation is determined
22 * by the ordering of the vertices. Looking in the direction opposite
23 * the normal (at the front of the face), the vertices will be
24 * listed in counter-clockwise order.
25 * There is no checking done to insure that the edges do not cross
26 * one another. This was considered too expensive and should be unnecessary.
27 * The last vertex is automatically connected to the first.
28 */
29
30 #define VERTEPS 1e-4 /* allowed vertex error */
31
32
33 FACE *
34 getface(o) /* get arguments for a face */
35 OBJREC *o;
36 {
37 double fabs();
38 double d1;
39 int badvert;
40 FVECT v1, v2, v3;
41 register FACE *f;
42 register int i;
43
44 if ((f = (FACE *)o->os) != NULL)
45 return(f); /* already done */
46
47 f = (FACE *)malloc(sizeof(FACE));
48 if (f == NULL)
49 error(SYSTEM, "out of memory in makeface");
50
51 if (o->oargs.nfargs < 9 || o->oargs.nfargs % 3)
52 objerror(o, USER, "bad # arguments");
53
54 o->os = (char *)f; /* save face */
55
56 f->va = o->oargs.farg;
57 f->nv = o->oargs.nfargs / 3;
58 /* compute area and normal */
59 f->norm[0] = f->norm[1] = f->norm[2] = 0.0;
60 v1[0] = v1[1] = v1[2] = 0.0;
61 for (i = 1; i < f->nv; i++) {
62 v2[0] = VERTEX(f,i)[0] - VERTEX(f,0)[0];
63 v2[1] = VERTEX(f,i)[1] - VERTEX(f,0)[1];
64 v2[2] = VERTEX(f,i)[2] - VERTEX(f,0)[2];
65 fcross(v3, v1, v2);
66 f->norm[0] += v3[0];
67 f->norm[1] += v3[1];
68 f->norm[2] += v3[2];
69 VCOPY(v1, v2);
70 }
71 f->area = normalize(f->norm);
72 if (f->area == 0.0) {
73 objerror(o, WARNING, "zero area"); /* used to be fatal */
74 f->offset = 0.0;
75 f->ax = 0;
76 return(f);
77 }
78 f->area *= 0.5;
79 /* compute offset */
80 badvert = 0;
81 f->offset = DOT(f->norm, VERTEX(f,0));
82 for (i = 1; i < f->nv; i++) {
83 d1 = DOT(f->norm, VERTEX(f,i));
84 badvert += fabs(d1 - f->offset/i) > VERTEPS;
85 f->offset += d1;
86 }
87 f->offset /= f->nv;
88 if (badvert)
89 objerror(o, WARNING, "non-planar vertex");
90 /* find axis */
91 f->ax = fabs(f->norm[0]) > fabs(f->norm[1]) ? 0 : 1;
92 if (fabs(f->norm[2]) > fabs(f->norm[f->ax]))
93 f->ax = 2;
94
95 return(f);
96 }
97
98
99 freeface(o) /* free memory associated with face */
100 OBJREC *o;
101 {
102 free(o->os);
103 o->os = NULL;
104 }
105
106
107 inface(p, f) /* determine if point is in face */
108 FVECT p;
109 FACE *f;
110 {
111 int ncross, n;
112 double x, y;
113 register int xi, yi;
114 register double *p0, *p1;
115
116 xi = (f->ax+1)%3;
117 yi = (f->ax+2)%3;
118 x = p[xi];
119 y = p[yi];
120 n = f->nv;
121 p0 = f->va + 3*(n-1); /* connect last to first */
122 p1 = f->va;
123 ncross = 0;
124 /* positive x axis cross test */
125 while (n--) {
126 if ((p0[yi] > y) ^ (p1[yi] > y))
127 if (p0[xi] > x && p1[xi] > x)
128 ncross++;
129 else if (p0[xi] > x || p1[xi] > x)
130 ncross += (p1[yi] > p0[yi]) ^
131 ((p0[yi]-y)*(p1[xi]-x) >
132 (p0[xi]-x)*(p1[yi]-y));
133 p0 = p1;
134 p1 += 3;
135 }
136 return(ncross & 01);
137 }