ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/face.c
Revision: 1.2
Committed: Tue Feb 21 14:39:54 1989 UTC (35 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +7 -7 lines
Log Message:
portability fixes for SGI

File Contents

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