1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: RtraceSimulManager.cpp,v 2.21 2024/11/09 00:10:49 greg Exp $"; |
3 |
#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 |
#include <unistd.h> |
13 |
#include <ctype.h> |
14 |
#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 |
Cleanup(); |
25 |
} |
26 |
if (!octn) // don't support stdin octree |
27 |
return false; |
28 |
|
29 |
NewHeader(octn); // get octree header |
30 |
ray_init((char *)octn); |
31 |
return true; |
32 |
} |
33 |
|
34 |
// 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 |
RadSimulManager::NewHeader(const char *inspec) |
50 |
{ |
51 |
if (hlen) { |
52 |
free(header); header = NULL; hlen = 0; |
53 |
} |
54 |
if (!inspec || !*inspec) |
55 |
return false; |
56 |
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 |
bool ok = (getheader(fp, add2header, this) >= 0); |
62 |
fclose(fp); |
63 |
return ok; |
64 |
} |
65 |
|
66 |
// Add a string to header (adds newline if missing) |
67 |
bool |
68 |
RadSimulManager::AddHeader(const char *str) |
69 |
{ |
70 |
if (!str) return false; |
71 |
int len = strlen(str); |
72 |
while (len && (str[len-1] == '\n') | (str[len-1] == '\r')) |
73 |
--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 |
header = (char *)malloc(len+2); |
80 |
if (!header) { |
81 |
hlen = 0; // XXX should report? |
82 |
return false; |
83 |
} |
84 |
memcpy(header+hlen, str, len); |
85 |
hlen += len; |
86 |
header[hlen++] = '\n'; // add single EOL |
87 |
header[hlen] = '\0'; |
88 |
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 |
RadSimulManager::AddHeader(int ac, char *av[]) |
110 |
{ |
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 |
if (hlen) // add to header |
119 |
header = (char *)realloc(header, hlen+len+1); |
120 |
else |
121 |
header = (char *)malloc(len+1); |
122 |
|
123 |
for (n = 0; n < ac; n++) { |
124 |
int c = check_special(av[n]); |
125 |
len = strlen(av[n]); |
126 |
if (c | !len) { // need to quote argument? |
127 |
if ((c == '"') | (c == '\n')) c = '\''; |
128 |
else c = '"'; |
129 |
header[hlen++] = c; |
130 |
strcpy(header+hlen, av[n]); |
131 |
hlen += len; |
132 |
header[hlen++] = c; |
133 |
} else { |
134 |
strcpy(header+hlen, av[n]); |
135 |
hlen += len; |
136 |
} |
137 |
header[hlen++] = ' '; |
138 |
} |
139 |
header[hlen-1] = '\n'; // terminate line |
140 |
header[hlen] = '\0'; |
141 |
return true; |
142 |
} |
143 |
|
144 |
// 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 |
// How many processors are available? |
174 |
int |
175 |
RadSimulManager::GetNCores() |
176 |
{ |
177 |
return sysconf(_SC_NPROCESSORS_ONLN); |
178 |
} |
179 |
|
180 |
// Set number of computation threads (0 => #cores) |
181 |
int |
182 |
RadSimulManager::SetThreadCount(int nt) |
183 |
{ |
184 |
if (!Ready()) |
185 |
return 0; |
186 |
|
187 |
if (nt <= 0) nt = castonly ? 1 : GetNCores(); |
188 |
|
189 |
if (nt == 1) |
190 |
ray_pclose(ray_pnprocs); |
191 |
else if (nt < ray_pnprocs) |
192 |
ray_pclose(ray_pnprocs - nt); |
193 |
else if (nt > ray_pnprocs) |
194 |
ray_popen(nt - ray_pnprocs); |
195 |
|
196 |
return NThreads(); |
197 |
} |
198 |
|
199 |
// Process a ray (in subthread), optional result |
200 |
int |
201 |
RadSimulManager::ProcessRay(RAY *r) |
202 |
{ |
203 |
if (!Ready()) return false; |
204 |
|
205 |
if (!ray_pnprocs) { // single-threaded mode? |
206 |
samplendx++; |
207 |
rayvalue(r); |
208 |
return 1; |
209 |
} |
210 |
return ray_pqueue(r); |
211 |
} |
212 |
|
213 |
// Wait for next result (or fail) |
214 |
bool |
215 |
RadSimulManager::WaitResult(RAY *r) |
216 |
{ |
217 |
if (!ray_pnprocs) |
218 |
return false; |
219 |
|
220 |
return (ray_presult(r, 0) > 0); |
221 |
} |
222 |
|
223 |
// Close octree, free data, return status |
224 |
int |
225 |
RadSimulManager::Cleanup(bool everything) |
226 |
{ |
227 |
if (ray_pnprocs < 0) |
228 |
return 0; // skip in child process |
229 |
NewHeader(); |
230 |
if (!ray_pnprocs) |
231 |
ray_done(everything); |
232 |
else |
233 |
ray_pdone(everything); |
234 |
return 0; |
235 |
} |
236 |
|
237 |
// Global pointer to simulation manager for trace call-back (only one) |
238 |
static const RtraceSimulManager * ourRTsimMan = NULL; |
239 |
|
240 |
// Call-back for trace output |
241 |
void |
242 |
RtraceSimulManager::RTracer(RAY *r) |
243 |
{ |
244 |
(*ourRTsimMan->traceCall)(r, ourRTsimMan->tcData); |
245 |
} |
246 |
|
247 |
// Call-back for FIFO output |
248 |
int |
249 |
RtraceSimulManager::Rfifout(RAY *r) |
250 |
{ |
251 |
return (*ourRTsimMan->cookedCall)(r, ourRTsimMan->ccData); |
252 |
} |
253 |
|
254 |
// Check for changes to render flags & adjust accordingly |
255 |
bool |
256 |
RtraceSimulManager::UpdateMode() |
257 |
{ |
258 |
if (!cookedCall) |
259 |
rtFlags &= ~RTdoFIFO; |
260 |
if (!traceCall) |
261 |
rtFlags &= ~RTtraceSources; |
262 |
if (rtFlags & RTimmIrrad) |
263 |
rtFlags &= ~RTlimDist; |
264 |
|
265 |
int misMatch = (rtFlags ^ curFlags) & RTmask; |
266 |
// updates based on toggled flags |
267 |
if (((misMatch & RTtraceSources) != 0) & (nsources > 0)) { |
268 |
int nt = NThreads(); |
269 |
if (nt > 1) { |
270 |
if (FlushQueue() < 0) |
271 |
return false; |
272 |
SetThreadCount(1); |
273 |
} |
274 |
int sn = nsources; |
275 |
if (rtFlags & RTtraceSources) { |
276 |
srcFollowed.NewBitMap(nsources); |
277 |
while (sn--) { |
278 |
if (source[sn].sflags & SFOLLOW) |
279 |
continue; |
280 |
source[sn].sflags |= SFOLLOW; |
281 |
srcFollowed.Set(sn); |
282 |
} |
283 |
} else { |
284 |
while (sn--) |
285 |
if (srcFollowed.Check(sn)) |
286 |
source[sn].sflags &= ~SFOLLOW; |
287 |
srcFollowed.NewBitMap(0); |
288 |
} |
289 |
if (nt > 1) SetThreadCount(nt); |
290 |
} |
291 |
if (misMatch & RTdoFIFO && FlushQueue() < 0) |
292 |
return false; |
293 |
curFlags = rtFlags; |
294 |
// update callbacks |
295 |
if (traceCall) |
296 |
trace = RTracer; |
297 |
else if (trace == RTracer) |
298 |
trace = NULL; |
299 |
if (rtFlags & RTdoFIFO) |
300 |
ray_fifo_out = Rfifout; |
301 |
else if (ray_fifo_out == Rfifout) |
302 |
ray_fifo_out = NULL; |
303 |
if ((trace != RTracer) & (ray_fifo_out != Rfifout)) { |
304 |
ourRTsimMan = NULL; |
305 |
} else if (ourRTsimMan != this) { |
306 |
if (ourRTsimMan) |
307 |
error(WARNING, "Competing top-level simulation managers?"); |
308 |
ourRTsimMan = this; |
309 |
} |
310 |
return true; |
311 |
} |
312 |
|
313 |
extern "C" int m_normal(OBJREC *m, RAY *r); |
314 |
|
315 |
// compute irradiance rather than radiance |
316 |
static void |
317 |
rayirrad(RAY *r) |
318 |
{ |
319 |
/* pretend we hit surface */ |
320 |
r->rxt = r->rot = 1e-5; |
321 |
VSUM(r->rop, r->rorg, r->rdir, r->rot); |
322 |
r->ron[0] = -r->rdir[0]; |
323 |
r->ron[1] = -r->rdir[1]; |
324 |
r->ron[2] = -r->rdir[2]; |
325 |
r->rod = 1.0; |
326 |
/* compute result */ |
327 |
r->revf = raytrace; |
328 |
m_normal(&Lamb, r); |
329 |
r->revf = rayirrad; |
330 |
} |
331 |
|
332 |
// compute first ray intersection only |
333 |
static void |
334 |
raycast(RAY *r) |
335 |
{ |
336 |
if (!localhit(r, &thescene)) { |
337 |
if (r->ro == &Aftplane) { /* clipped */ |
338 |
r->ro = NULL; |
339 |
r->rot = FHUGE; |
340 |
} else |
341 |
sourcehit(r); |
342 |
} |
343 |
} |
344 |
|
345 |
// Add ray bundle to queue w/ optional 1st ray ID |
346 |
int |
347 |
RtraceSimulManager::EnqueueBundle(const FVECT orig_direc[], int n, RNUMBER rID0) |
348 |
{ |
349 |
int nqueued = 0; |
350 |
RAY res; |
351 |
|
352 |
if (!Ready()) |
353 |
return -1; |
354 |
|
355 |
if (castonly && !cookedCall) |
356 |
error(INTERNAL, "EnqueueBundle() called in castonly mode without cookedCall"); |
357 |
|
358 |
if (!UpdateMode()) // update rendering mode if requested |
359 |
return -1; |
360 |
|
361 |
if (rID0 && curFlags&RTdoFIFO) |
362 |
error(INTERNAL, "Ray number assignment unsupported with FIFO"); |
363 |
|
364 |
while (n-- > 0) { // queue each ray |
365 |
VCOPY(res.rorg, orig_direc[0]); |
366 |
VCOPY(res.rdir, orig_direc[1]); |
367 |
res.rmax = .0; |
368 |
orig_direc += 2; |
369 |
rayorigin(&res, PRIMARY, NULL, NULL); |
370 |
res.rno = rID0 ? (lastRayID = rID0++) : ++lastRayID; |
371 |
if (curFlags & RTimmIrrad) |
372 |
res.revf = rayirrad; |
373 |
else if (castonly) |
374 |
res.revf = raycast; |
375 |
double d = normalize(res.rdir); |
376 |
bool sendRes = (cookedCall != NULL); |
377 |
if (d > .0) { // direction vector is valid? |
378 |
if (curFlags & RTlimDist) |
379 |
res.rmax = d; |
380 |
if (((curFlags&RTdoFIFO) != 0) & (ray_pnprocs > 0)) { |
381 |
if (ray_fifo_in(&res) < 0) |
382 |
return -1; |
383 |
sendRes = false; |
384 |
} else { |
385 |
int rv = ProcessRay(&res); |
386 |
if (rv < 0) |
387 |
return -1; |
388 |
sendRes &= (rv > 0); |
389 |
} |
390 |
} else if (ThreadsAvailable() < NThreads() && |
391 |
FlushQueue() < 0) |
392 |
return -1; |
393 |
// may be dummy ray |
394 |
if (sendRes && (*cookedCall)(&res, ccData) < 0) |
395 |
return -1; |
396 |
nqueued++; |
397 |
} |
398 |
return nqueued; |
399 |
} |
400 |
|
401 |
// Finish pending rays and complete callbacks |
402 |
int |
403 |
RtraceSimulManager::FlushQueue() |
404 |
{ |
405 |
if (curFlags & RTdoFIFO) { |
406 |
if (ray_pnprocs) |
407 |
return ray_fifo_flush(); |
408 |
return 0; |
409 |
} |
410 |
int nsent = 0; |
411 |
RAY res; |
412 |
|
413 |
while (WaitResult(&res)) { |
414 |
if (!cookedCall) continue; |
415 |
int rv = (*cookedCall)(&res, ccData); |
416 |
if (rv < 0) return -1; |
417 |
nsent += rv; |
418 |
} |
419 |
return nsent; |
420 |
} |