ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raycalls.c
Revision: 2.31
Committed: Wed Aug 21 20:42:20 2024 UTC (8 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +1 -3 lines
Log Message:
refactor: moved memory sharing routines to preload.c with common call interface

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: raycalls.c,v 2.30 2024/05/02 15:02:08 greg Exp $";
3 #endif
4 /*
5 * raycalls.c - interface for running Radiance rendering as a library
6 *
7 * External symbols declared in ray.h
8 */
9
10 #include "copyright.h"
11
12 /*
13 * These routines are designed to aid the programmer who wishes
14 * to call Radiance as a library. Unfortunately, the system was
15 * not originally intended to be run this way, and there are some
16 * awkward limitations to contend with. The most irritating
17 * perhaps is that the global variables and functions do not have
18 * a prefix, and the symbols are a bit generic. This results in a
19 * serious invasion of the calling application's name-space, and
20 * you may need to rename either some Radiance routines or some
21 * of your routines to avoid conflicts. Another limitation is
22 * that the global variables are not gathered together into any
23 * sort of context, so it is impossible to simultaneously run
24 * this library on multiple scenes or in multiple threads.
25 * You get one scene and one thread, and if you want more, you
26 * will have to go with the process model defined in raypcalls.c.
27 * Finally, unrecoverable errors result in a call to the application-
28 * defined function quit(). The usual thing to do is to call exit().
29 * You might want to do something else instead, like
30 * call setjmp()/longjmp() to bring you back to the calling
31 * function for recovery. You may also wish to define your own
32 * wputs(s) and eputs(s) functions to output warning and error
33 * messages, respectively.
34 *
35 * With those caveats, we have attempted to make the interface
36 * as simple as we can. Global variables and their defaults
37 * are defined below, and including "ray.h" declares these
38 * along with all the routines you are likely to need. First,
39 * assign the global variable progname to your argv[0], then
40 * change the rendering parameters as you like. If you have a set
41 * of option arguments you are working from, the getrenderopt(ac,av)
42 * call should be very useful. Before tracing any rays, you
43 * must read in the octree with a call to ray_init(oct).
44 * Passing NULL for the file name causes ray_init() to read
45 * the octree from the standard input -- rarely a good idea.
46 * However, one may read an octree from a program (such as
47 * oconv) by preceding a shell command by a '!' character.
48 *
49 * To trace a ray, define a RAY object myRay and assign:
50 *
51 * myRay.rorg = ( ray origin point )
52 * myRay.rdir = ( normalized ray direction )
53 * myRay.rmax = ( maximum length, or zero for no limit )
54 *
55 * If you are rendering from a VIEW structure, this can be
56 * accomplished with a single call for the ray at (x,y):
57 *
58 * myRay.rmax = viewray(myRay.rorg, myRay.rdir, &myView, x, y);
59 *
60 * Then, trace the primary ray with:
61 *
62 * ray_trace(&myRay);
63 *
64 * The resulting contents of myRay should provide you with
65 * more than enough information about what the ray hit,
66 * the computed value, etc. For further clues of how to
67 * compute irradiance, how to get callbacks on the evaluated
68 * ray tree, etc., see the contents of rtrace.c. See
69 * also the rpmain.c, rtmain.c, and rvmain.c modules
70 * to learn more how rendering options are processed.
71 *
72 * When you are done, you may call ray_done(1) to clean
73 * up memory used by Radiance. It doesn't free everything,
74 * but it makes a valiant effort. If you call ray_done(0),
75 * it leaves data that is likely to be reused, including
76 * loaded data files and fonts. The library may be
77 * restarted at any point by calling ray_init() on a new
78 * octree.
79 *
80 * The call ray_save(rp) fills a parameter structure
81 * with the current global parameter settings, which may be
82 * restored at any time with a call to ray_restore(rp).
83 * This buffer contains no linked information, and thus
84 * may be passed between processes using write() and
85 * read() calls, so long as byte order is maintained.
86 * Calling ray_restore(NULL) restores the original
87 * default parameters, which is also retrievable with
88 * the call ray_defaults(rp). (These should be the
89 * same as the defaults for rtrace.)
90 */
91
92 #include <string.h>
93 #include <time.h>
94
95 #include "ray.h"
96 #include "source.h"
97 #include "bsdf.h"
98 #include "ambient.h"
99 #include "otypes.h"
100 #include "random.h"
101 #include "data.h"
102 #include "font.h"
103 #include "pmapray.h"
104
105 char *progname = "unknown_app"; /* caller sets to argv[0] */
106
107 char *octname; /* octree name we are given */
108
109 CUBE thescene; /* our scene */
110 OBJECT nsceneobjs; /* number of objects in our scene */
111
112 int dimlist[MAXDIM]; /* sampling dimensions */
113 int ndims = 0; /* number of sampling dimensions */
114 int samplendx = 0; /* index for this sample */
115
116 void (*trace)() = NULL; /* trace call */
117
118 void (*addobjnotify[8])() = {ambnotify, NULL};
119
120 int castonly = 0; /* only doing ray-casting? */
121
122 int do_irrad = 0; /* compute irradiance? */
123
124 int rand_samp = 1; /* pure Monte Carlo sampling? */
125
126 double dstrsrc = 0.0; /* square source distribution */
127 double shadthresh = .03; /* shadow threshold */
128 double shadcert = .75; /* shadow certainty */
129 int directrelay = 2; /* number of source relays */
130 int vspretest = 512; /* virtual source pretest density */
131 int directvis = 1; /* sources visible? */
132 double srcsizerat = .2; /* maximum ratio source size/dist. */
133
134 COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
135 COLOR salbedo = BLKCOLOR; /* global scattering albedo */
136 double seccg = 0.; /* global scattering eccentricity */
137 double ssampdist = 0.; /* scatter sampling distance */
138
139 double specthresh = .15; /* specular sampling threshold */
140 double specjitter = 1.; /* specular sampling jitter */
141
142 int backvis = 1; /* back face visibility */
143
144 int maxdepth = -10; /* maximum recursion depth */
145 double minweight = 1e-4; /* minimum ray weight */
146
147 char *ambfile = NULL; /* ambient file name */
148 COLOR ambval = BLKCOLOR; /* ambient value */
149 int ambvwt = 0; /* initial weight for ambient value */
150 double ambacc = 0.1; /* ambient accuracy */
151 int ambres = 256; /* ambient resolution */
152 int ambdiv = 1024; /* ambient divisions */
153 int ambssamp = 512; /* ambient super-samples */
154 int ambounce = 0; /* ambient bounces */
155 char *amblist[AMBLLEN+1]; /* ambient include/exclude list */
156 int ambincl = -1; /* include == 1, exclude == 0 */
157
158
159 static void
160 reset_random(void) /* re-initialize random number generator */
161 {
162 if (rand_samp) {
163 srandom((long)time(0));
164 initurand(0);
165 } else {
166 srandom(0L);
167 initurand(2048);
168 }
169 }
170
171
172 void
173 ray_init( /* initialize ray-tracing calculation */
174 char *otnm
175 )
176 {
177 if (nobjects > 0) /* free old scene data */
178 ray_done(0);
179 /* initialize object types */
180 if (ofun[OBJ_SPHERE].funp == o_default)
181 initotypes();
182 /* initialize urand */
183 reset_random();
184 /* initialize spectral sampling */
185 if (setspectrsamp(CNDX, WLPART) < 0)
186 error(USER, "unsupported spectral sampling");
187
188 octname = savqstr(otnm); /* read scene octree */
189 readoct(octname, ~(IO_FILES|IO_INFO), &thescene, NULL);
190 nsceneobjs = nobjects;
191
192 if (!castonly) { /* any actual ray traversal to do? */
193
194 ray_init_pmap(); /* PMAP: set up & load photon maps */
195
196 marksources(); /* find and mark sources */
197
198 setambient(); /* initialize ambient calculation */
199 } else
200 distantsources(); /* else mark only distant sources */
201 }
202
203
204 void
205 ray_trace( /* trace a primary ray */
206 RAY *r
207 )
208 {
209 rayorigin(r, PRIMARY, NULL, NULL);
210 samplendx++;
211 rayvalue(r); /* assumes origin and direction are set */
212 }
213
214
215 void
216 ray_done( /* free ray-tracing data */
217 int freall
218 )
219 {
220 retainfonts = 1;
221 ambdone();
222 ambnotify(OVOID);
223 freesources();
224 freeobjects(0, nobjects);
225 donesets();
226 octdone();
227 thescene.cutree = EMPTY;
228 freeqstr(octname); octname = NULL;
229 retainfonts = 0;
230 if (freall) {
231 freefont(NULL);
232 freedata(NULL);
233 SDfreeCache(NULL);
234 initurand(0);
235 }
236 if (nobjects > 0) {
237 sprintf(errmsg, "%ld objects left after call to ray_done()",
238 (long)nobjects);
239 error(WARNING, errmsg);
240 }
241
242 ray_done_pmap();
243 }
244
245
246 void
247 ray_save( /* save current parameter settings */
248 RAYPARAMS *rp
249 )
250 {
251 int i, ndx;
252
253 if (rp == NULL)
254 return;
255 rp->do_irrad = do_irrad;
256 rp->rand_samp = rand_samp;
257 rp->dstrsrc = dstrsrc;
258 rp->shadthresh = shadthresh;
259 rp->shadcert = shadcert;
260 rp->directrelay = directrelay;
261 rp->vspretest = vspretest;
262 rp->directvis = directvis;
263 rp->srcsizerat = srcsizerat;
264 copycolor(rp->cextinction, cextinction);
265 copycolor(rp->salbedo, salbedo);
266 rp->seccg = seccg;
267 rp->ssampdist = ssampdist;
268 rp->specthresh = specthresh;
269 rp->specjitter = specjitter;
270 rp->backvis = backvis;
271 rp->maxdepth = maxdepth;
272 rp->minweight = minweight;
273 if (ambfile != NULL)
274 strncpy(rp->ambfile, ambfile, sizeof(rp->ambfile)-1);
275 else
276 memset(rp->ambfile, '\0', sizeof(rp->ambfile));
277 copycolor(rp->ambval, ambval);
278 rp->ambvwt = ambvwt;
279 rp->ambacc = ambacc;
280 rp->ambres = ambres;
281 rp->ambdiv = ambdiv;
282 rp->ambssamp = ambssamp;
283 rp->ambounce = ambounce;
284 rp->ambincl = ambincl;
285 memset(rp->amblval, '\0', sizeof(rp->amblval));
286 ndx = 0;
287 for (i = 0; i < AMBLLEN && amblist[i] != NULL; i++) {
288 int len = strlen(amblist[i]);
289 if (ndx+len >= sizeof(rp->amblval))
290 break;
291 strcpy(rp->amblval+ndx, amblist[i]);
292 rp->amblndx[i] = ndx;
293 ndx += len+1;
294 }
295 while (i <= AMBLLEN)
296 rp->amblndx[i++] = -1;
297
298 /* PMAP: save photon mapping params */
299 ray_save_pmap(rp);
300 }
301
302
303 void
304 ray_restore( /* restore parameter settings */
305 RAYPARAMS *rp
306 )
307 {
308 int i;
309
310 if (rp == NULL) { /* restore defaults */
311 RAYPARAMS dflt;
312 ray_defaults(&dflt);
313 ray_restore(&dflt);
314 return;
315 }
316 /* restore saved settings */
317 do_irrad = rp->do_irrad;
318 if (!rand_samp != !rp->rand_samp) {
319 rand_samp = rp->rand_samp;
320 reset_random();
321 }
322 dstrsrc = rp->dstrsrc;
323 shadthresh = rp->shadthresh;
324 shadcert = rp->shadcert;
325 directrelay = rp->directrelay;
326 vspretest = rp->vspretest;
327 directvis = rp->directvis;
328 srcsizerat = rp->srcsizerat;
329 copycolor(cextinction, rp->cextinction);
330 copycolor(salbedo, rp->salbedo);
331 seccg = rp->seccg;
332 ssampdist = rp->ssampdist;
333 specthresh = rp->specthresh;
334 specjitter = rp->specjitter;
335 backvis = rp->backvis;
336 maxdepth = rp->maxdepth;
337 minweight = rp->minweight;
338 copycolor(ambval, rp->ambval);
339 ambvwt = rp->ambvwt;
340 ambdiv = rp->ambdiv;
341 ambssamp = rp->ambssamp;
342 ambounce = rp->ambounce;
343 /* a bit dangerous if not static */
344 for (i = 0; rp->amblndx[i] >= 0; i++)
345 amblist[i] = rp->amblval + rp->amblndx[i];
346 while (i <= AMBLLEN)
347 amblist[i++] = NULL;
348 ambincl = rp->ambincl;
349 /* update ambient calculation */
350 ambnotify(OVOID);
351 if ((thescene.cutree != EMPTY) & !castonly) {
352 int newamb = (ambfile == NULL) ? rp->ambfile[0] :
353 strcmp(ambfile, rp->ambfile) ;
354
355 if (amblist[0] != NULL)
356 for (i = 0; i < nobjects; i++)
357 ambnotify(i);
358
359 ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL;
360 if (newamb) {
361 ambres = rp->ambres;
362 ambacc = rp->ambacc;
363 setambient();
364 } else {
365 setambres(rp->ambres);
366 setambacc(rp->ambacc);
367 }
368 } else {
369 ambfile = (rp->ambfile[0]) ? rp->ambfile : (char *)NULL;
370 ambres = rp->ambres;
371 ambacc = rp->ambacc;
372 }
373
374 /* PMAP: restore photon mapping params */
375 ray_restore_pmap(rp);
376 }
377
378
379 void
380 ray_defaults( /* get default parameter values */
381 RAYPARAMS *rp
382 )
383 {
384 int i;
385
386 if (rp == NULL)
387 return;
388
389 rp->do_irrad = 0;
390 rp->rand_samp = 1;
391 rp->dstrsrc = 0.0;
392 rp->shadthresh = 0.03;
393 rp->shadcert = 0.75;
394 rp->directrelay = 2;
395 rp->vspretest = 512;
396 rp->directvis = 1;
397 rp->srcsizerat = .2;
398 setcolor(rp->cextinction, 0., 0., 0.);
399 setcolor(rp->salbedo, 0., 0., 0.);
400 rp->seccg = 0.;
401 rp->ssampdist = 0.;
402 rp->specthresh = 0.15;
403 rp->specjitter = 1.;
404 rp->backvis = 1;
405 rp->maxdepth = -10;
406 rp->minweight = 1e-4;
407 memset(rp->ambfile, '\0', sizeof(rp->ambfile));
408 setcolor(rp->ambval, 0., 0., 0.);
409 rp->ambvwt = 0;
410 rp->ambres = 256;
411 rp->ambacc = 0.1;
412 rp->ambdiv = 1024;
413 rp->ambssamp = 512;
414 rp->ambounce = 0;
415 rp->ambincl = -1;
416 memset(rp->amblval, '\0', sizeof(rp->amblval));
417 for (i = AMBLLEN+1; i--; )
418 rp->amblndx[i] = -1;
419
420 /* PMAP: restore photon mapping defaults */
421 ray_defaults_pmap(rp);
422 }