ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/face.c
Revision: 2.5
Committed: Fri Mar 22 09:01:16 1996 UTC (28 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +4 -2 lines
Log Message:
slight efficiency improvement

File Contents

# Content
1 /* Copyright (c) 1995 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 #ifdef SMLFLT
31 #define VERTEPS 1e-2 /* allowed vertex error */
32 #else
33 #define VERTEPS 1e-4 /* allowed vertex error */
34 #endif
35
36
37 FACE *
38 getface(o) /* get arguments for a face */
39 OBJREC *o;
40 {
41 double d1;
42 int badvert;
43 FVECT v1, v2, v3;
44 register FACE *f;
45 register int i;
46
47 if ((f = (FACE *)o->os) != NULL)
48 return(f); /* already done */
49
50 f = (FACE *)malloc(sizeof(FACE));
51 if (f == NULL)
52 error(SYSTEM, "out of memory in makeface");
53
54 if (o->oargs.nfargs < 9 || o->oargs.nfargs % 3)
55 objerror(o, USER, "bad # arguments");
56
57 o->os = (char *)f; /* save face */
58
59 f->va = o->oargs.farg;
60 f->nv = o->oargs.nfargs / 3;
61 /* check for last==first */
62 if (dist2(VERTEX(f,0),VERTEX(f,f->nv-1)) <= FTINY*FTINY)
63 f->nv--;
64 /* compute area and normal */
65 f->norm[0] = f->norm[1] = f->norm[2] = 0.0;
66 v1[0] = VERTEX(f,1)[0] - VERTEX(f,0)[0];
67 v1[1] = VERTEX(f,1)[1] - VERTEX(f,0)[1];
68 v1[2] = VERTEX(f,1)[2] - VERTEX(f,0)[2];
69 for (i = 2; i < f->nv; i++) {
70 v2[0] = VERTEX(f,i)[0] - VERTEX(f,0)[0];
71 v2[1] = VERTEX(f,i)[1] - VERTEX(f,0)[1];
72 v2[2] = VERTEX(f,i)[2] - VERTEX(f,0)[2];
73 fcross(v3, v1, v2);
74 f->norm[0] += v3[0];
75 f->norm[1] += v3[1];
76 f->norm[2] += v3[2];
77 VCOPY(v1, v2);
78 }
79 f->area = normalize(f->norm);
80 if (f->area == 0.0) {
81 objerror(o, WARNING, "zero area"); /* used to be fatal */
82 f->offset = 0.0;
83 f->ax = 0;
84 return(f);
85 }
86 f->area *= 0.5;
87 /* compute offset */
88 badvert = 0;
89 f->offset = DOT(f->norm, VERTEX(f,0));
90 for (i = 1; i < f->nv; i++) {
91 d1 = DOT(f->norm, VERTEX(f,i));
92 badvert += fabs(d1 - f->offset/i) > VERTEPS;
93 f->offset += d1;
94 }
95 f->offset /= (double)f->nv;
96 if (badvert)
97 objerror(o, WARNING, "non-planar vertex");
98 /* find axis */
99 f->ax = fabs(f->norm[0]) > fabs(f->norm[1]) ? 0 : 1;
100 if (fabs(f->norm[2]) > fabs(f->norm[f->ax]))
101 f->ax = 2;
102
103 return(f);
104 }
105
106
107 freeface(o) /* free memory associated with face */
108 OBJREC *o;
109 {
110 if (o->os == NULL)
111 return;
112 free(o->os);
113 o->os = NULL;
114 }
115
116
117 inface(p, f) /* determine if point is in face */
118 FVECT p;
119 FACE *f;
120 {
121 int ncross, n;
122 double x, y;
123 register int xi, yi;
124 register FLOAT *p0, *p1;
125
126 xi = (f->ax+1)%3;
127 yi = (f->ax+2)%3;
128 x = p[xi];
129 y = p[yi];
130 n = f->nv;
131 p0 = f->va + 3*(n-1); /* connect last to first */
132 p1 = f->va;
133 ncross = 0;
134 /* positive x axis cross test */
135 while (n--) {
136 if ((p0[yi] > y) ^ (p1[yi] > y))
137 if (p0[xi] > x && p1[xi] > x)
138 ncross++;
139 else if (p0[xi] > x || p1[xi] > x)
140 ncross += (p1[yi] > p0[yi]) ^
141 ((p0[yi]-y)*(p1[xi]-x) >
142 (p0[xi]-x)*(p1[yi]-y));
143 p0 = p1;
144 p1 += 3;
145 }
146 return(ncross & 01);
147 }