ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/src/rt/RcontribSimulManager.cpp
(Generate patch)

Comparing src/rt/RcontribSimulManager.cpp (file contents):
Revision 2.3 by greg, Wed Oct 30 01:38:21 2024 UTC vs.
Revision 2.13 by greg, Thu Oct 16 23:14:31 2025 UTC

# Line 24 | Line 24 | extern const char      HDRSTR[];
24   extern const char       BIGEND[];
25   extern const char       FMTSTR[];
26  
27 int     contrib = 0;                    // computing contributions?
28
29 int     xres = 0;                       // horizontal (scan) size
30 int     yres = 0;                       // vertical resolution
31
27   // new/exclusive, overwrite if exists, or recover data
28   int     RSDOflags[] = {RDSwrite|RDSexcl|RDSextend, RDSwrite|RDSextend,
29                                  RDSread|RDSwrite};
# Line 40 | Line 35 | static const char      ROWZEROSTR[] = "NROWS=00000000000000
35   struct RcontribMod {
36          RcontribOutput *        opl;            // pointer to first output channel
37          char *                  params;         // parameters string
38 <        EPNODE *                binv;           // bin expression
38 >        EPNODE *                binv;           // bin expression (NULL if 1 bin)
39          int                     nbins;          // bin count this modifier
40          int                     coffset;        // column offset in bytes
41          DCOLORV                 cbin[1];        // bin accumulator (extends struct)
# Line 57 | Line 52 | struct RowAssignment {
52          uint32                  ac;             // accumulation count
53   };
54  
55 + // Get format identifier
56 + const char *
57 + formstr(int f)
58 + {
59 +        switch (f) {
60 +        case 'a': return("ascii");
61 +        case 'f': return("float");
62 +        case 'd': return("double");
63 +        case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT);
64 +        }
65 +        return("unknown");
66 + }
67 +
68   // Our default data share function
69   RdataShare *
70   defDataShare(const char *name, RCOutputOp op, size_t siz)
# Line 68 | Line 76 | defDataShare(const char *name, RCOutputOp op, size_t s
76   RcontribMod *
77   NewRcMod(const char *prms, const char *binexpr, int ncbins)
78   {
71        if (ncbins <= 0) return NULL;
79          if (!prms) prms = "";
80 <        if (!binexpr | (ncbins == 1))
81 <                binexpr = "0";
82 <
80 >        if ((ncbins > 1) & !binexpr) {
81 >                error(USER, "missing bin expression");
82 >                return NULL;
83 >        }
84 >        if (ncbins < 1) ncbins = 1;
85 >        
86          RcontribMod *   mp = (RcontribMod *)ecalloc(1, sizeof(RcontribMod) +
87                                                  sizeof(DCOLORV)*(NCSAMP*ncbins-1) +
88                                                  strlen(prms)+1);
89  
90 +        if (binexpr) {                          // get/check bin expression
91 +                mp->binv = eparse(const_cast<char *>(binexpr));
92 +                if (mp->binv->type == NUM) {    // constant expression (0)?
93 +                        if ((int)evalue(mp->binv) != 0) {
94 +                                sprintf(errmsg, "illegal non-zero constant for bin (%s)",
95 +                                                binexpr);
96 +                                error(USER, errmsg);
97 +                        }
98 +                        if (ncbins > 1) {
99 +                                sprintf(errmsg, "bad bin count (%d should be 1)", ncbins);
100 +                                error(USER, errmsg);
101 +                        }
102 +                        epfree(mp->binv, true);
103 +                        mp->binv = NULL;
104 +                        prms = "";
105 +                        ncbins = 1;
106 +                }
107 +        }
108          mp->params = strcpy((char *)(mp->cbin + ncbins*NCSAMP), prms);
81        mp->binv = eparse(const_cast<char *>(binexpr));
109          mp->nbins = ncbins;
110          return mp;
111   }
# Line 88 | Line 115 | void
115   FreeRcMod(void *p)
116   {
117          if (!p) return;
118 <        epfree((*(RcontribMod *)p).binv, true);
118 >        EPNODE *        bep = (*(RcontribMod *)p).binv;
119 >        if (bep) epfree(bep, true);
120          efree(p);
121   }
122  
# Line 135 | Line 163 | RcontribSimulManager::RctCall(RAY *r, void *cd)
163          if (!mp)
164                  return 0;               // not in our modifier list
165  
166 <        worldfunc(RCCONTEXT, r);        // compute bin #
167 <        set_eparams(mp->params);
168 <        double          bval = evalue(mp->binv);
169 <        if (bval <= -.5)
170 <                return 0;       // silently ignore negative bin index
171 <        DCOLORV *       dvp = (*mp)[int(bval + .5)];
166 >        int                     bi = 0; // get bin index
167 >        if (mp->binv) {
168 >                worldfunc(RCCONTEXT, r);
169 >                set_eparams(mp->params);
170 >                double          bval = evalue(mp->binv);
171 >                if (bval <= -.5)
172 >                        return 0;       // silently ignore negative bin index
173 >                bi = int(bval + .5);
174 >        }
175 >        DCOLORV *       dvp = (*mp)[bi];
176          if (!dvp) {
177 <                sprintf(errmsg, "bad bin number for '%s' (%.1f ignored)", mname, bval);
177 >                sprintf(errmsg, "bad bin number for '%s' (%d ignored)", mname, bi);
178                  error(WARNING, errmsg);
179                  return 0;
180          }
181          SCOLOR          contr;
182          raycontrib(contr, r, PRIMARY);          // compute coefficient
183 <        if (contrib)
183 >        if (rcp->HasFlag(RCcontrib))
184                  smultscolor(contr, r->rcol);    // -> value contribution
185 +
186          for (int i = 0; i < NCSAMP; i++)
187                  *dvp++ += contr[i];             // accumulate color/spectrum
188          return 1;
# Line 178 | Line 211 | bool
211   RcontribSimulManager::AddModifier(const char *modn, const char *outspec,
212                                  const char *prms, const char *binval, int bincnt)
213   {
214 <        if (!modn | !outspec | (bincnt <= 0) || !*modn | !*outspec) {
214 >        if (!modn | !outspec || !*modn | !*outspec) {
215                  error(WARNING, "ignoring bad call to AddModifier()");
216                  return false;
217          }
218 +        if (*outspec == '!') {
219 +                error(USER, "command output not supported by RcontribSimulManager");
220 +                return false;
221 +        }
222          if (!nChan) {                           // initial call?
223 +                if ((xres < 0) | (yres <= 0)) {
224 +                        error(USER, "xres, yres must be set before first modifier");
225 +                        return false;
226 +                }
227                  if (!SetDataFormat(dtyp))
228                          return false;
229                  nChan = NCSAMP;
230          } else if (nChan != NCSAMP) {
231 <                error(INTERNAL, "number of spectral channels must be fixed");
231 >                error(USER, "# spectral channels must be fixed in AddModifier()");
232                  return false;
233          }
234          if (Ready()) {
# Line 210 | Line 251 | RcontribSimulManager::AddModifier(const char *modn, co
251          const int       binndx = hasFormat(outspec, "diouxX");
252          int             bin0 = 0;
253          char            fnbuf[512];
254 <        if (!modndx | (modndx > binndx))
254 >        if (!modndx || (binndx > 0) & (modndx > binndx))
255                  sprintf(fnbuf, outspec, bin0, modn);
256          else
257                  sprintf(fnbuf, outspec, modn, bin0);
# Line 239 | Line 280 | RcontribSimulManager::AddModifier(const char *modn, co
280          mp->opl = op;                   // first (maybe only) output channel
281          if (modndx)                     // remember modifier if part of name
282                  op->omod = lp->key;
283 <        if (binndx > 0) {               // append output image/bin list
283 >        if (binndx) {                   // append output image/bin list
284                  op->rowBytes += dsiz;
285                  op->obin = bin0;
286                  for (bincnt = 1; bincnt < mp->nbins; bincnt++) {
287 <                        if (!modndx | (modndx > binndx))
287 >                        if (!modndx || (binndx > 0) &  (modndx > binndx))
288                                  sprintf(fnbuf, outspec, bin0+bincnt, modn);
289                          else
290                                  sprintf(fnbuf, outspec, modn, bin0+bincnt);
# Line 262 | Line 303 | RcontribSimulManager::AddModifier(const char *modn, co
303                          op->rowBytes += dsiz;
304                  }
305          } else                          // else send all results to this channel
306 <                op->rowBytes += bincnt*dsiz;
306 >                op->rowBytes += mp->nbins*dsiz;
307          return true;
308   }
309  
# Line 283 | Line 324 | RcontribSimulManager::AddModFile(const char *modfn, co
324          }
325          char            mod[MAXSTR];
326          while (fgetword(mod, sizeof(mod), fp))
327 <                if (!AddModifier(mod, outspec, prms, binval, bincnt))
327 >                if (!AddModifier(mod, outspec, prms, binval, bincnt)) {
328 >                        fclose(fp);
329                          return false;
330 +                }
331          fclose(fp);
332          return true;
333   }
334  
335 + // call-back to check if modifier has been loaded
336 + static int
337 + checkModExists(const LUENT *lp, void *p)
338 + {
339 +        OBJECT  mod = modifier(lp->key);
340 +
341 +        if ((mod != OVOID) & (mod < nsceneobjs))
342 +                return 1;
343 +
344 +        sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
345 +        error(WARNING, errmsg);
346 +        return 0;
347 + }
348 +
349   // Prepare output channels and return # completed rows
350   int
351   RcontribSimulManager::PrepOutput()
352   {
353          if (!outList || !RtraceSimulManager::Ready()) {
354 <                error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
354 >                error(INTERNAL, "PrepOutput() called before octree & modifiers set");
355                  return -1;
356          }
357 +        if (!cdsF) {
358 +                error(INTERNAL, "missing RdataShare constructor call (cdsF)");
359 +                return -1;
360 +        }
361 +        if (lu_doall(&modLUT, checkModExists, NULL) < 0)
362 +                return -1;
363 +
364 +        outList->nRows = yres * (xres + !xres); // all outputs have same #rows
365          int     remWarnings = 20;
366          for (RcontribOutput *op = outList; op; op = op->next) {
367                  if (op->rData) {
368                          error(INTERNAL, "output channel already open in PrepOutput()");
369                          return -1;
370                  }
371 <                op->nRows = yres * (xres + !xres);
371 >                op->nRows = outList->nRows;
372                  op->rData = (*cdsF)(op->ofname, outOp,
373                                          GetHeadLen()+1024 + op->nRows*op->rowBytes);
374                  freeqstr(op->ofname); op->ofname = NULL;
# Line 313 | Line 378 | RcontribSimulManager::PrepOutput()
378                                  return -1;
379                          if (rd >= op->nRows) {
380                                  if (remWarnings >= 0) {
381 <                                        sprintf(errmsg, "recovered output '%s' already done",
381 >                                        sprintf(errmsg, "recovered output '%s' is complete",
382                                                          op->GetName());
383 <                                        error(WARNING, remWarnings ? errmsg : "etc...");
319 <                                        remWarnings--;
383 >                                        error(WARNING, --remWarnings ? errmsg : "etc...");
384                                  }
385                                  rd = op->nRows;
386                          }
# Line 358 | Line 422 | RcontribOutput::NewHeader(const RcontribSimulManager *
422          strcpy(hdr+begData, ROWZEROSTR);
423          rowCountPos = begData+LNROWSTR;
424          begData += sizeof(ROWZEROSTR)-1;
425 <        if (!xres | (rowBytes > esiz)) {
425 >        if (!rcp->xres | (rowBytes > esiz)) {
426                  sprintf(hdr+begData, "NCOLS=%d\n", int(rowBytes/esiz));
427                  begData += strlen(hdr+begData);
428          }
# Line 396 | Line 460 | RcontribOutput::NewHeader(const RcontribSimulManager *
460                          hdr[begData++] = ' ';
461          hdr[begData++] = '\n';          // EOL for data format
462          hdr[begData++] = '\n';          // end of nominal header
463 <        if ((xres > 0) & (rowBytes == esiz)) {  // tack on resolution string?
464 <                sprintf(hdr+begData, PIXSTDFMT, yres, xres);
463 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // tack on resolution string?
464 >                sprintf(hdr+begData, PIXSTDFMT, rcp->yres, rcp->xres);
465                  begData += strlen(hdr+begData);
466          }
467          return rData->ReleaseMemory(hdr, RDSwrite);
# Line 451 | Line 515 | RcontribOutput::CheckHeader(const RcontribSimulManager
515          }
516                                                  // check #columns
517          if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
518 <                        !xres | (rowBytes > esiz)) {
518 >                        !rcp->xres | (rowBytes > esiz)) {
519                  sprintf(errmsg, "expected NCOLS=%d in '%s'",
520                                  int(rowBytes/esiz), GetName());
521                  error(USER, errmsg);
# Line 466 | Line 530 | RcontribOutput::CheckHeader(const RcontribSimulManager
530          rowCountPos = cp - hdr;
531          int     rlast = atoi(cp);
532          begData++;                              // advance past closing EOL
533 <        if ((xres > 0) & (rowBytes == esiz)) {  // check/skip resolution string?
533 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // check/skip resolution string?
534                  char    rbuf[64];
535 <                sprintf(rbuf, PIXSTDFMT, yres, xres);
535 >                sprintf(rbuf, PIXSTDFMT, rcp->yres, rcp->xres);
536                  int     rlen = strlen(rbuf);
537                  if (strncmp(rbuf, hdr+begData, rlen)) {
538                          sprintf(errmsg, "bad resolution string in '%s'", GetName());
# Line 542 | Line 606 | putModContrib(const LUENT *lp, void *p)
606                  }
607                  } break;
608          default:
609 <                error(CONSISTENCY, "unsupported output type in sendModContrib()");
609 >                error(CONSISTENCY, "unsupported output type in putModContrib()");
610                  return -1;
611          }
612                                                  // clear for next tally
# Line 673 | Line 737 | RcontribSimulManager::RunChild()
737                          error(CONSISTENCY, "bad accumulator count in child");
738                          exit(1);
739                  }
740 <                if (rass.ac > accum)
741 <                        vecList = (FVECT *)erealloc(vecList,
742 <                                                sizeof(FVECT)*2*rass.ac);
740 >                if (rass.ac > accum) {
741 >                        efree(vecList);
742 >                        vecList = (FVECT *)emalloc(sizeof(FVECT)*2*rass.ac);
743 >                }
744                  accum = rass.ac;
745                  rInPos = rass.row;
746  
# Line 766 | Line 831 | RcontribSimulManager::SetThreadCount(int nt)
831                  return 0;
832          }
833          if (nt < 0)
834 <                return nkids;
834 >                return NThreads();
835          if (!nt) nt = GetNCores();
836          int     status = 0;
837          if (nt == 1)
# Line 779 | Line 844 | RcontribSimulManager::SetThreadCount(int nt)
844                  sprintf(errmsg, "non-zero (%d) status from child", status);
845                  error(WARNING, errmsg);
846          }
847 <        return nkids;
847 >        return NThreads();
848   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines