ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/face.c
Revision: 2.3
Committed: Fri May 14 13:42:53 1993 UTC (30 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +4 -0 lines
Log Message:
made vertex error a function of SMLFLT define

File Contents

# Content
1 /* Copyright (c) 1991 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 /* compute area and normal */
62 f->norm[0] = f->norm[1] = f->norm[2] = 0.0;
63 v1[0] = v1[1] = v1[2] = 0.0;
64 for (i = 1; i < f->nv; i++) {
65 v2[0] = VERTEX(f,i)[0] - VERTEX(f,0)[0];
66 v2[1] = VERTEX(f,i)[1] - VERTEX(f,0)[1];
67 v2[2] = VERTEX(f,i)[2] - VERTEX(f,0)[2];
68 fcross(v3, v1, v2);
69 f->norm[0] += v3[0];
70 f->norm[1] += v3[1];
71 f->norm[2] += v3[2];
72 VCOPY(v1, v2);
73 }
74 f->area = normalize(f->norm);
75 if (f->area == 0.0) {
76 objerror(o, WARNING, "zero area"); /* used to be fatal */
77 f->offset = 0.0;
78 f->ax = 0;
79 return(f);
80 }
81 f->area *= 0.5;
82 /* compute offset */
83 badvert = 0;
84 f->offset = DOT(f->norm, VERTEX(f,0));
85 for (i = 1; i < f->nv; i++) {
86 d1 = DOT(f->norm, VERTEX(f,i));
87 badvert += fabs(d1 - f->offset/i) > VERTEPS;
88 f->offset += d1;
89 }
90 f->offset /= (double)f->nv;
91 if (badvert)
92 objerror(o, WARNING, "non-planar vertex");
93 /* find axis */
94 f->ax = fabs(f->norm[0]) > fabs(f->norm[1]) ? 0 : 1;
95 if (fabs(f->norm[2]) > fabs(f->norm[f->ax]))
96 f->ax = 2;
97
98 return(f);
99 }
100
101
102 freeface(o) /* free memory associated with face */
103 OBJREC *o;
104 {
105 if (o->os == NULL)
106 return;
107 free(o->os);
108 o->os = NULL;
109 }
110
111
112 inface(p, f) /* determine if point is in face */
113 FVECT p;
114 FACE *f;
115 {
116 int ncross, n;
117 double x, y;
118 register int xi, yi;
119 register FLOAT *p0, *p1;
120
121 xi = (f->ax+1)%3;
122 yi = (f->ax+2)%3;
123 x = p[xi];
124 y = p[yi];
125 n = f->nv;
126 p0 = f->va + 3*(n-1); /* connect last to first */
127 p1 = f->va;
128 ncross = 0;
129 /* positive x axis cross test */
130 while (n--) {
131 if ((p0[yi] > y) ^ (p1[yi] > y))
132 if (p0[xi] > x && p1[xi] > x)
133 ncross++;
134 else if (p0[xi] > x || p1[xi] > x)
135 ncross += (p1[yi] > p0[yi]) ^
136 ((p0[yi]-y)*(p1[xi]-x) >
137 (p0[xi]-x)*(p1[yi]-y));
138 p0 = p1;
139 p1 += 3;
140 }
141 return(ncross & 01);
142 }