ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2.c
Revision: 3.17
Committed: Tue Nov 24 17:38:09 1998 UTC (25 years, 5 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.16: +12 -10 lines
Log Message:
changed to RMS distance

File Contents

# Content
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Rtrace support routines for holodeck rendering
9 */
10
11 #include "rholo.h"
12 #include "paths.h"
13 #include "random.h"
14
15
16 VIEWPOINT myeye; /* target view position */
17
18
19 packrays(rod, p) /* pack ray origins and directions */
20 register float *rod;
21 register PACKET *p;
22 {
23 short packord[RPACKSIZ];
24 float packdc2[RPACKSIZ];
25 int iterleft = 3*p->nr;
26 BYTE rpos[2][2];
27 FVECT ro, rd, rp1;
28 GCOORD gc[2];
29 double d, dc2, md2, td2;
30 int i;
31 register int ii;
32
33 if (!hdbcoord(gc, hdlist[p->hd], p->bi))
34 error(CONSISTENCY, "bad beam index in packrays");
35 td2 = (myeye.rng+FTINY)*(myeye.rng+FTINY);
36 for (i = 0, md2 = 0.; i < p->nr || md2 > td2; ) {
37 rpos[0][0] = frandom() * 256.;
38 rpos[0][1] = frandom() * 256.;
39 rpos[1][0] = frandom() * 256.;
40 rpos[1][1] = frandom() * 256.;
41 d = hdray(ro, rd, hdlist[p->hd], gc, rpos);
42 if (myeye.rng > FTINY) { /* check eyepoint */
43 register int nexti;
44
45 VSUM(rp1, ro, rd, d);
46 dc2 = dist2line(myeye.vpt, ro, rp1);
47 dc2 /= (double)(p->nr*p->nr);
48 if (i == p->nr) { /* packet full */
49 nexti = packord[i-1];
50 if (!iterleft--)
51 break; /* tried enough! */
52 if (dc2 >= packdc2[nexti])
53 continue; /* worse than worst */
54 md2 -= packdc2[nexti];
55 } else
56 nexti = i++;
57 md2 += packdc2[nexti] = dc2; /* new distance */
58 for (ii = i; --ii; ) { /* insertion sort */
59 if (dc2 > packdc2[packord[ii-1]])
60 break;
61 packord[ii] = packord[ii-1];
62 }
63 packord[ii] = nexti;
64 ii = nexti; /* put it here */
65 } else
66 ii = i++;
67 if (p->offset != NULL) {
68 if (!vdef(OBSTRUCTIONS))
69 d *= frandom(); /* random offset */
70 VSUM(ro, ro, rd, d); /* advance ray */
71 p->offset[ii] = d;
72 }
73 p->ra[ii].r[0][0] = rpos[0][0];
74 p->ra[ii].r[0][1] = rpos[0][1];
75 p->ra[ii].r[1][0] = rpos[1][0];
76 p->ra[ii].r[1][1] = rpos[1][1];
77 VCOPY(rod+6*ii, ro);
78 VCOPY(rod+6*ii+3, rd);
79 }
80 #ifdef DEBUG
81 fprintf(stderr, "%f mean distance for target %f (%d iterations left)\n",
82 sqrt(md2), myeye.rng, iterleft);
83 #endif
84 }
85
86
87 donerays(p, rvl) /* encode finished ray computations */
88 register PACKET *p;
89 register float *rvl;
90 {
91 double d;
92 register int i;
93
94 for (i = 0; i < p->nr; i++) {
95 setcolr(p->ra[i].v, rvl[0], rvl[1], rvl[2]);
96 d = rvl[3];
97 if (p->offset != NULL)
98 d += p->offset[i];
99 p->ra[i].d = hdcode(hdlist[p->hd], d);
100 rvl += 4;
101 }
102 p->nc += p->nr;
103 }
104
105
106 int
107 done_rtrace() /* clean up and close rtrace calculation */
108 {
109 int status;
110 /* already closed? */
111 if (!nprocs)
112 return;
113 /* flush beam queue */
114 done_packets(flush_queue());
115 /* sync holodeck */
116 hdsync(NULL, 1);
117 /* close rtrace */
118 if ((status = end_rtrace()))
119 error(WARNING, "bad exit status from rtrace");
120 if (vdef(REPORT)) { /* report time */
121 eputs("rtrace process closed\n");
122 report(0);
123 }
124 return(status); /* return status */
125 }
126
127
128 new_rtrace() /* restart rtrace calculation */
129 {
130 char combuf[128];
131
132 if (nprocs > 0) /* already running? */
133 return;
134 starttime = time(NULL); /* reset start time and counts */
135 npacksdone = nraysdone = 0L;
136 if (vdef(TIME)) /* reset end time */
137 endtime = starttime + vflt(TIME)*3600. + .5;
138 if (vdef(RIF)) { /* rerun rad to update octree */
139 sprintf(combuf, "rad -v 0 -s -w %s", vval(RIF));
140 if (system(combuf))
141 error(WARNING, "error running rad");
142 }
143 if (start_rtrace() < 1) /* start rtrace */
144 error(WARNING, "cannot restart rtrace");
145 else if (vdef(REPORT)) {
146 eputs("rtrace process restarted\n");
147 report(0);
148 }
149 }
150
151
152 getradfile() /* run rad and get needed variables */
153 {
154 static short mvar[] = {OCTREE,EYESEP,-1};
155 static char tf1[] = TEMPLATE;
156 char tf2[64];
157 char combuf[256];
158 char *pippt;
159 register int i;
160 register char *cp;
161 /* check if rad file specified */
162 if (!vdef(RIF))
163 return(0);
164 /* create rad command */
165 mktemp(tf1);
166 sprintf(tf2, "%s.rif", tf1);
167 sprintf(combuf,
168 "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
169 vval(RIF), tf1);
170 cp = combuf;
171 while (*cp){
172 if (*cp == '|') pippt = cp;
173 cp++;
174 } /* match unset variables */
175 for (i = 0; mvar[i] >= 0; i++)
176 if (!vdef(mvar[i])) {
177 *cp++ = '|';
178 strcpy(cp, vnam(mvar[i]));
179 while (*cp) cp++;
180 pippt = NULL;
181 }
182 if (pippt != NULL)
183 strcpy(pippt, "> /dev/null"); /* nothing to match */
184 else
185 sprintf(cp, ")[ \t]*=' > %s", tf2);
186 #ifdef DEBUG
187 wputs(combuf); wputs("\n");
188 #endif
189 system(combuf); /* ignore exit code */
190 if (pippt == NULL) {
191 loadvars(tf2); /* load variables */
192 unlink(tf2);
193 }
194 rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
195 unlink(tf1); /* clean up */
196 return(1);
197 }
198
199
200 report(t) /* report progress so far */
201 time_t t;
202 {
203 static time_t seconds2go = 1000000;
204
205 if (t == 0L)
206 t = time(NULL);
207 sprintf(errmsg, "%ld packets (%ld rays) done after %.2f hours\n",
208 npacksdone, nraysdone, (t-starttime)/3600.);
209 eputs(errmsg);
210 if (seconds2go == 1000000)
211 seconds2go = vdef(REPORT) ? (long)(vflt(REPORT)*60. + .5) : 0L;
212 if (seconds2go)
213 reporttime = t + seconds2go;
214 }