ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp3.c
Revision: 3.3
Committed: Thu Nov 20 16:43:42 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.2: +6 -3 lines
Log Message:
fixed serious error caused by conversion from double to int

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8 gregl 3.2 * Holodeck beam support for display process
9 gregl 3.1 */
10    
11     #include "rholo.h"
12     #include "rhdisp.h"
13     #include "view.h"
14    
15    
16     int
17     npixels(vp, hr, vr, hp, bi) /* compute appropriate number to evaluate */
18     VIEW *vp;
19     int hr, vr;
20     HOLO *hp;
21     int bi;
22     {
23     GCOORD gc[2];
24     FVECT cp[4];
25     FVECT ip[4];
26     double d;
27     register int i;
28     /* compute cell corners in image */
29     if (!hdbcoord(gc, hp, bi))
30     error(CONSISTENCY, "bad beam index in npixels");
31     hdcell(cp, hp, gc+1);
32     for (i = 0; i < 4; i++) {
33     viewloc(ip[i], vp, cp[i]);
34     if (ip[i][2] < 0.)
35     return(0);
36     ip[i][0] *= (double)hr; /* scale by resolution */
37     ip[i][1] *= (double)vr;
38     }
39     /* compute quad area */
40     d = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) -
41     (ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]);
42     d += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) -
43     (ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]);
44     if (d < 0)
45     d = -d;
46     /* round off result */
47     return((int)(.5*d+.5));
48     }
49    
50    
51     /*
52     * The ray directions that define the pyramid in visit_cells() needn't
53     * be normalized, but they must be given in clockwise order as seen
54     * from the pyramid's apex (origin).
55     */
56     int
57     visit_cells(orig, pyrd, hp, vf, dp) /* visit cells within a pyramid */
58     FVECT orig, pyrd[4]; /* pyramid ray directions in clockwise order */
59     HOLO *hp;
60     int (*vf)();
61     char *dp;
62     {
63     int n = 0;
64     int inflags = 0;
65     FVECT gp, pn[4], lo, ld;
66     double po[4], lbeg, lend, d, t;
67     GCOORD gc;
68     register int i;
69     /* figure out whose side we're on */
70     hdgrid(gp, hp, orig);
71     for (i = 0; i < 3; i++) {
72     inflags |= (gp[i] > FTINY) << (i<<1);
73     inflags |= (gp[i] < hp->grid[i]-FTINY) << (i<<1 | 1);
74     }
75     /* compute pyramid planes */
76     for (i = 0; i < 4; i++) {
77     fcross(pn[i], pyrd[i], pyrd[(i+1)&03]);
78     po[i] = DOT(pn[i], orig);
79     }
80     /* traverse each wall */
81     for (gc.w = 0; gc.w < 6; gc.w++) {
82     if (!(inflags & 1<<gc.w)) /* origin on wrong side */
83     continue;
84     /* scanline algorithm */
85     for (gc.i[1] = hp->grid[((gc.w>>1)+2)%3]; gc.i[1]--; ) {
86     /* compute scanline */
87     gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0;
88     gp[((gc.w>>1)+1)%3] = 0;
89     gp[((gc.w>>1)+2)%3] = gc.i[1] + 0.5;
90     hdworld(lo, hp, gp);
91     gp[((gc.w>>1)+1)%3] = 1;
92     hdworld(ld, hp, gp);
93 gregl 3.2 ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2];
94 gregl 3.1 /* find scanline limits */
95     lbeg = 0; lend = hp->grid[((gc.w>>1)+1)%3];
96     for (i = 0; i < 4; i++) {
97     t = DOT(pn[i], lo) - po[i];
98     d = -DOT(pn[i], ld);
99 gregl 3.2 if (d > FTINY) { /* <- plane */
100 gregl 3.1 if ((t /= d) < lend)
101     lend = t;
102 gregl 3.2 } else if (d < -FTINY) { /* plane -> */
103 gregl 3.1 if ((t /= d) > lbeg)
104     lbeg = t;
105 gregl 3.3 } else if (t < 0) { /* outside */
106     lend = -1;
107     break;
108     }
109 gregl 3.1 }
110 gregl 3.3 if (lbeg >= lend)
111     continue;
112 gregl 3.1 i = lend + .5; /* visit cells on this scan */
113     for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++)
114     n += (*vf)(&gc, dp);
115     }
116     }
117     return(n);
118     }
119    
120    
121     int
122     addcell(gcp, cl) /* add a cell to a list */
123     GCOORD *gcp;
124     register int *cl;
125     {
126     copystruct((GCOORD *)(cl+1) + *cl, gcp);
127     (*cl)++;
128     return(1);
129     }
130    
131    
132     int
133     cellcmp(gcp1, gcp2) /* visit_cells() cell ordering */
134     register GCOORD *gcp1, *gcp2;
135     {
136     register int c;
137    
138     if ((c = gcp1->w - gcp2->w))
139     return(c);
140     if ((c = gcp2->i[1] - gcp1->i[1])) /* wg1 is reverse-ordered */
141     return(c);
142     return(gcp1->i[0] - gcp2->i[0]);
143     }
144    
145    
146     int *
147     getviewcells(hp, vp) /* get ordered cell list for section view */
148     register HOLO *hp;
149     VIEW *vp;
150     {
151     FVECT org, dir[4];
152     int n;
153     register int *cl;
154     /* compute view pyramid */
155     if (vp->type == VT_PAR) goto viewerr;
156     if (viewray(org, dir[0], vp, 0., 0.) < -FTINY) goto viewerr;
157     if (viewray(org, dir[1], vp, 0., 1.) < -FTINY) goto viewerr;
158     if (viewray(org, dir[2], vp, 1., 1.) < -FTINY) goto viewerr;
159     if (viewray(org, dir[3], vp, 1., 0.) < -FTINY) goto viewerr;
160     /* allocate enough list space */
161     n = 2*( hp->grid[0]*hp->grid[1] +
162     hp->grid[0]*hp->grid[2] +
163     hp->grid[1]*hp->grid[2] );
164     cl = (int *)malloc(sizeof(int) + n*sizeof(GCOORD));
165     if (cl == NULL)
166     goto memerr;
167     *cl = 0;
168     /* add cells within pyramid */
169     visit_cells(org, dir, hp, addcell, cl);
170     if (!*cl) {
171     free((char *)cl);
172     return(NULL);
173     }
174     #if 0
175 gregl 3.2 /* We're just going to free this memory in a moment, and list is
176     * sorted automatically by visit_cells(), so we don't need this.
177     */
178 gregl 3.1 if (*cl < n) { /* optimize memory use */
179     cl = (int *)realloc((char *)cl,
180     sizeof(int) + *cl*sizeof(GCOORD));
181     if (cl == NULL)
182     goto memerr;
183     }
184     /* sort the list */
185     qsort((char *)(cl+1), *cl, sizeof(GCOORD), cellcmp);
186     #endif
187     return(cl);
188     viewerr:
189     error(INTERNAL, "unusable view in getviewcells");
190     memerr:
191     error(SYSTEM, "out of memory in getviewcells");
192     }