ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/RtraceSimulManager.cpp
Revision: 2.24
Committed: Wed Nov 20 17:46:25 2024 UTC (5 months, 1 week ago) by greg
Branch: MAIN
CVS Tags: HEAD
Changes since 2.23: +13 -7 lines
Log Message:
fix(rxtrace,rxcontrib): Fix in -I+ option for bug pointed out by S. Wasilewski

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.24 static const char RCSid[] = "$Id: RtraceSimulManager.cpp,v 2.23 2024/11/13 18:47:01 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * RtraceSimulManager.cpp
6     *
7     * Rtrace simulation manager class implementation
8     *
9     * Created by Greg Ward on 2/2/2023.
10     */
11    
12 greg 2.5 #include <unistd.h>
13 greg 2.12 #include <ctype.h>
14 greg 2.1 #include "RtraceSimulManager.h"
15     #include "source.h"
16    
17     // Load octree and prepare renderer
18     bool
19     RadSimulManager::LoadOctree(const char *octn)
20     {
21     if (octname) { // already running?
22     if (octn && !strcmp(octn, octname))
23     return true;
24 greg 2.6 Cleanup();
25 greg 2.1 }
26 greg 2.12 if (!octn) // don't support stdin octree
27 greg 2.1 return false;
28    
29 greg 2.13 NewHeader(octn); // get octree header
30 greg 2.1 ray_init((char *)octn);
31     return true;
32     }
33    
34 greg 2.12 // callback function for loading header
35     static int
36     add2header(char *str, void *p)
37     {
38     if (isheadid(str))
39     return 0;
40     if (isformat(str))
41     return 0;
42    
43     return (*(RadSimulManager *)p).AddHeader(str);
44     }
45    
46     // Prepare header from previous input (or clear)
47     // Normally called during octree load
48     bool
49 greg 2.13 RadSimulManager::NewHeader(const char *inspec)
50 greg 2.12 {
51 greg 2.13 if (hlen) {
52     free(header); header = NULL; hlen = 0;
53 greg 2.12 }
54 greg 2.13 if (!inspec || !*inspec)
55 greg 2.12 return false;
56 greg 2.13 if (inspec[0] == '!') // save command as header?
57     return AddHeader(inspec+1);
58     // attempt to read from file
59     FILE *fp = fopen(inspec, "rb");
60     if (!fp) return false;
61 greg 2.12 bool ok = (getheader(fp, add2header, this) >= 0);
62     fclose(fp);
63     return ok;
64     }
65    
66 greg 2.13 // Add a string to header (adds newline if missing)
67 greg 2.12 bool
68     RadSimulManager::AddHeader(const char *str)
69     {
70     if (!str) return false;
71     int len = strlen(str);
72 greg 2.14 while (len && (str[len-1] == '\n') | (str[len-1] == '\r'))
73 greg 2.13 --len; // don't copy EOL(s)
74     if (!len)
75     return false;
76     if (hlen)
77     header = (char *)realloc(header, hlen+len+2);
78     else
79 greg 2.12 header = (char *)malloc(len+2);
80 greg 2.13 if (!header) {
81     hlen = 0; // XXX should report?
82     return false;
83 greg 2.12 }
84 greg 2.13 memcpy(header+hlen, str, len);
85     hlen += len;
86     header[hlen++] = '\n'; // add single EOL
87     header[hlen] = '\0';
88 greg 2.12 return true;
89     }
90    
91     // helper function to check for white-space and quotations
92     static int
93     check_special(const char *s)
94     {
95     int space_found = 0;
96    
97     while (*s) {
98     if ((*s == '"') | (*s == '\''))
99     return *s; // quotes have priority
100     if (isspace(*s))
101     space_found = *s;
102     s++;
103     }
104     return space_found;
105     }
106    
107     // Append program line to header
108     bool
109 greg 2.15 RadSimulManager::AddHeader(int ac, char *av[])
110 greg 2.12 {
111     if ((ac <= 0) | !av) return false;
112     int len = 0;
113     int n;
114     for (n = 0; n < ac; n++) {
115     if (!av[n]) return false;
116     len += strlen(av[n]) + 3;
117     }
118 greg 2.13 if (hlen) // add to header
119 greg 2.12 header = (char *)realloc(header, hlen+len+1);
120 greg 2.13 else
121 greg 2.12 header = (char *)malloc(len+1);
122 greg 2.13
123 greg 2.12 for (n = 0; n < ac; n++) {
124     int c = check_special(av[n]);
125 greg 2.13 len = strlen(av[n]);
126     if (c | !len) { // need to quote argument?
127     if ((c == '"') | (c == '\n')) c = '\'';
128 greg 2.12 else c = '"';
129     header[hlen++] = c;
130     strcpy(header+hlen, av[n]);
131 greg 2.13 hlen += len;
132 greg 2.12 header[hlen++] = c;
133     } else {
134     strcpy(header+hlen, av[n]);
135 greg 2.13 hlen += len;
136 greg 2.12 }
137     header[hlen++] = ' ';
138     }
139     header[hlen-1] = '\n'; // terminate line
140     header[hlen] = '\0';
141     return true;
142     }
143    
144 greg 2.17 // Look for specific header keyword, return value
145     const char *
146     RadSimulManager::GetHeadStr(const char *key, bool inOK) const
147     {
148     if (!key | !hlen || strchr(key, '\n'))
149     return NULL;
150     if (inOK) // skip leading spaces?
151     while (isspace(*key)) key++;
152    
153     const int klen = strlen(key);
154     if (!klen)
155     return NULL;
156     const char * cp = header;
157     while (*cp) {
158     if (inOK) { // skip leading spaces?
159     while (isspace(*cp) && *cp++ != '\n')
160     ;
161     if (cp[-1] == '\n')
162     continue;
163     }
164     if (!strncmp(cp, key, klen))
165     return cp+klen; // found it!
166    
167     while (*cp && *cp++ != '\n')
168     ;
169     }
170     return NULL;
171     }
172    
173 greg 2.5 // How many processors are available?
174 greg 2.2 int
175     RadSimulManager::GetNCores()
176     {
177 greg 2.6 return sysconf(_SC_NPROCESSORS_ONLN);
178 greg 2.2 }
179    
180 greg 2.1 // Set number of computation threads (0 => #cores)
181     int
182     RadSimulManager::SetThreadCount(int nt)
183     {
184 greg 2.23 if (!Ready()) return 0;
185 greg 2.6
186     if (nt <= 0) nt = castonly ? 1 : GetNCores();
187    
188     if (nt == 1)
189     ray_pclose(ray_pnprocs);
190     else if (nt < ray_pnprocs)
191     ray_pclose(ray_pnprocs - nt);
192     else if (nt > ray_pnprocs)
193     ray_popen(nt - ray_pnprocs);
194 greg 2.5
195 greg 2.6 return NThreads();
196 greg 2.1 }
197    
198 greg 2.4 // Process a ray (in subthread), optional result
199 greg 2.22 int
200 greg 2.4 RadSimulManager::ProcessRay(RAY *r)
201     {
202 greg 2.23 if (!Ready()) return -1;
203 greg 2.6
204     if (!ray_pnprocs) { // single-threaded mode?
205 greg 2.4 samplendx++;
206     rayvalue(r);
207 greg 2.22 return 1;
208 greg 2.4 }
209 greg 2.22 return ray_pqueue(r);
210 greg 2.4 }
211    
212     // Wait for next result (or fail)
213     bool
214     RadSimulManager::WaitResult(RAY *r)
215     {
216 greg 2.6 if (!ray_pnprocs)
217     return false;
218    
219     return (ray_presult(r, 0) > 0);
220 greg 2.4 }
221    
222 greg 2.1 // Close octree, free data, return status
223     int
224 greg 2.3 RadSimulManager::Cleanup(bool everything)
225 greg 2.1 {
226 greg 2.19 if (ray_pnprocs < 0)
227     return 0; // skip in child process
228 greg 2.13 NewHeader();
229 greg 2.6 if (!ray_pnprocs)
230 greg 2.11 ray_done(everything);
231     else
232 greg 2.5 ray_pdone(everything);
233 greg 2.1 return 0;
234     }
235    
236     // Global pointer to simulation manager for trace call-back (only one)
237     static const RtraceSimulManager * ourRTsimMan = NULL;
238    
239 greg 2.7 // Call-back for trace output
240     void
241 greg 2.1 RtraceSimulManager::RTracer(RAY *r)
242     {
243     (*ourRTsimMan->traceCall)(r, ourRTsimMan->tcData);
244     }
245    
246 greg 2.6 // Call-back for FIFO output
247     int
248     RtraceSimulManager::Rfifout(RAY *r)
249     {
250     return (*ourRTsimMan->cookedCall)(r, ourRTsimMan->ccData);
251     }
252    
253 greg 2.1 // Check for changes to render flags & adjust accordingly
254     bool
255     RtraceSimulManager::UpdateMode()
256     {
257     if (!cookedCall)
258     rtFlags &= ~RTdoFIFO;
259     if (!traceCall)
260     rtFlags &= ~RTtraceSources;
261     if (rtFlags & RTimmIrrad)
262     rtFlags &= ~RTlimDist;
263    
264 greg 2.20 int misMatch = (rtFlags ^ curFlags) & RTmask;
265 greg 2.1 // updates based on toggled flags
266 greg 2.21 if (((misMatch & RTtraceSources) != 0) & (nsources > 0)) {
267     int nt = NThreads();
268     if (nt > 1) {
269     if (FlushQueue() < 0)
270     return false;
271     SetThreadCount(1);
272     }
273 greg 2.18 int sn = nsources;
274 greg 2.1 if (rtFlags & RTtraceSources) {
275 greg 2.18 srcFollowed.NewBitMap(nsources);
276     while (sn--) {
277     if (source[sn].sflags & SFOLLOW)
278     continue;
279 greg 2.1 source[sn].sflags |= SFOLLOW;
280 greg 2.18 srcFollowed.Set(sn);
281     }
282     } else {
283     while (sn--)
284     if (srcFollowed.Check(sn))
285     source[sn].sflags &= ~SFOLLOW;
286     srcFollowed.NewBitMap(0);
287     }
288 greg 2.21 if (nt > 1) SetThreadCount(nt);
289 greg 2.1 }
290 greg 2.4 if (misMatch & RTdoFIFO && FlushQueue() < 0)
291     return false;
292 greg 2.1 curFlags = rtFlags;
293 greg 2.6 // update callbacks
294     if (traceCall)
295 greg 2.1 trace = RTracer;
296 greg 2.6 else if (trace == RTracer)
297 greg 2.1 trace = NULL;
298 greg 2.6 if (rtFlags & RTdoFIFO)
299     ray_fifo_out = Rfifout;
300     else if (ray_fifo_out == Rfifout)
301     ray_fifo_out = NULL;
302     if ((trace != RTracer) & (ray_fifo_out != Rfifout)) {
303 greg 2.1 ourRTsimMan = NULL;
304 greg 2.6 } else if (ourRTsimMan != this) {
305     if (ourRTsimMan)
306     error(WARNING, "Competing top-level simulation managers?");
307     ourRTsimMan = this;
308 greg 2.1 }
309     return true;
310     }
311    
312     extern "C" int m_normal(OBJREC *m, RAY *r);
313    
314 greg 2.6 // compute irradiance rather than radiance
315 greg 2.1 static void
316     rayirrad(RAY *r)
317     {
318 greg 2.24 /* orientation -> normal */
319     VCOPY(r->ron, r->rdir);
320     /* pretend normal incidence */
321     r->rdir[0] = -r->ron[0];
322     r->rdir[1] = -r->ron[1];
323     r->rdir[2] = -r->ron[2];
324 greg 2.1 r->rod = 1.0;
325 greg 2.24 /* counterfeit other params */
326     r->rxt = r->rot = 1e-4;
327     /* move comfortably above sample pos. */
328     VSUM(r->rop, r->rorg, r->ron, r->rot);
329     /* leap-frog for pretend origin */
330     VSUM(r->rorg, r->rop, r->ron, r->rot);
331 greg 2.1 /* compute result */
332     r->revf = raytrace;
333     m_normal(&Lamb, r);
334     r->revf = rayirrad;
335     }
336    
337 greg 2.6 // compute first ray intersection only
338 greg 2.1 static void
339     raycast(RAY *r)
340     {
341     if (!localhit(r, &thescene)) {
342     if (r->ro == &Aftplane) { /* clipped */
343     r->ro = NULL;
344     r->rot = FHUGE;
345     } else
346     sourcehit(r);
347     }
348     }
349    
350     // Add ray bundle to queue w/ optional 1st ray ID
351     int
352     RtraceSimulManager::EnqueueBundle(const FVECT orig_direc[], int n, RNUMBER rID0)
353     {
354     int nqueued = 0;
355     RAY res;
356    
357 greg 2.23 if (!Ready()) return -1;
358 greg 2.1
359     if (castonly && !cookedCall)
360 greg 2.8 error(INTERNAL, "EnqueueBundle() called in castonly mode without cookedCall");
361 greg 2.1
362     if (!UpdateMode()) // update rendering mode if requested
363     return -1;
364    
365 greg 2.9 if (rID0 && curFlags&RTdoFIFO)
366     error(INTERNAL, "Ray number assignment unsupported with FIFO");
367    
368 greg 2.1 while (n-- > 0) { // queue each ray
369     VCOPY(res.rorg, orig_direc[0]);
370     VCOPY(res.rdir, orig_direc[1]);
371 greg 2.16 res.rmax = .0;
372 greg 2.1 orig_direc += 2;
373     rayorigin(&res, PRIMARY, NULL, NULL);
374 greg 2.9 res.rno = rID0 ? (lastRayID = rID0++) : ++lastRayID;
375 greg 2.1 if (curFlags & RTimmIrrad)
376     res.revf = rayirrad;
377     else if (castonly)
378     res.revf = raycast;
379 greg 2.4 double d = normalize(res.rdir);
380     bool sendRes = (cookedCall != NULL);
381 greg 2.10 if (d > .0) { // direction vector is valid?
382 greg 2.1 if (curFlags & RTlimDist)
383     res.rmax = d;
384 greg 2.7 if (((curFlags&RTdoFIFO) != 0) & (ray_pnprocs > 0)) {
385     if (ray_fifo_in(&res) < 0)
386     return -1;
387 greg 2.4 sendRes = false;
388 greg 2.22 } else {
389     int rv = ProcessRay(&res);
390     if (rv < 0)
391     return -1;
392     sendRes &= (rv > 0);
393     }
394 greg 2.1 } else if (ThreadsAvailable() < NThreads() &&
395 greg 2.4 FlushQueue() < 0)
396 greg 2.1 return -1;
397 greg 2.6 // may be dummy ray
398     if (sendRes && (*cookedCall)(&res, ccData) < 0)
399     return -1;
400 greg 2.3 nqueued++;
401 greg 2.1 }
402     return nqueued;
403     }
404    
405     // Finish pending rays and complete callbacks
406 greg 2.4 int
407 greg 2.1 RtraceSimulManager::FlushQueue()
408     {
409 greg 2.7 if (curFlags & RTdoFIFO) {
410     if (ray_pnprocs)
411     return ray_fifo_flush();
412     return 0;
413     }
414 greg 2.4 int nsent = 0;
415     RAY res;
416    
417     while (WaitResult(&res)) {
418     if (!cookedCall) continue;
419 greg 2.6 int rv = (*cookedCall)(&res, ccData);
420     if (rv < 0) return -1;
421     nsent += rv;
422 greg 2.4 }
423     return nsent;
424 greg 2.1 }