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

Comparing ray/src/rt/RcontribSimulManager.cpp (file contents):
Revision 2.2 by greg, Tue Oct 29 19:47:19 2024 UTC vs.
Revision 2.10 by greg, Tue Dec 10 00:38:59 2024 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) {                          // 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) + .5) > 0) {
94 +                                sprintf(errmsg, "illegal positive 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          for (int i = 0; i < NCSAMP; i++)
186                  *dvp++ += contr[i];             // accumulate color/spectrum
# Line 178 | Line 210 | bool
210   RcontribSimulManager::AddModifier(const char *modn, const char *outspec,
211                                  const char *prms, const char *binval, int bincnt)
212   {
213 <        if (!modn | !outspec | (bincnt <= 0) || !*modn | !*outspec) {
213 >        if (!modn | !outspec || !*modn | !*outspec) {
214                  error(WARNING, "ignoring bad call to AddModifier()");
215                  return false;
216          }
217          if (!nChan) {                           // initial call?
218 +                if ((xres < 0) | (yres <= 0)) {
219 +                        error(USER, "xres, yres must be set before first modifier");
220 +                        return false;
221 +                }
222                  if (!SetDataFormat(dtyp))
223                          return false;
224                  nChan = NCSAMP;
225          } else if (nChan != NCSAMP) {
226 <                error(INTERNAL, "number of spectral channels must be fixed");
226 >                error(USER, "number of spectral channels must be fixed");
227                  return false;
228          }
229          if (Ready()) {
# Line 210 | Line 246 | RcontribSimulManager::AddModifier(const char *modn, co
246          const int       binndx = hasFormat(outspec, "diouxX");
247          int             bin0 = 0;
248          char            fnbuf[512];
249 <        if (!modndx | (modndx > binndx))
249 >        if (!modndx || (binndx > 0) & (modndx > binndx))
250                  sprintf(fnbuf, outspec, bin0, modn);
251          else
252                  sprintf(fnbuf, outspec, modn, bin0);
# Line 239 | Line 275 | RcontribSimulManager::AddModifier(const char *modn, co
275          mp->opl = op;                   // first (maybe only) output channel
276          if (modndx)                     // remember modifier if part of name
277                  op->omod = lp->key;
278 <        if (binndx > 0) {               // append output image/bin list
278 >        if (binndx) {                   // append output image/bin list
279                  op->rowBytes += dsiz;
280                  op->obin = bin0;
281                  for (bincnt = 1; bincnt < mp->nbins; bincnt++) {
282 <                        if (!modndx | (modndx > binndx))
282 >                        if (!modndx || (binndx > 0) &  (modndx > binndx))
283                                  sprintf(fnbuf, outspec, bin0+bincnt, modn);
284                          else
285                                  sprintf(fnbuf, outspec, modn, bin0+bincnt);
# Line 262 | Line 298 | RcontribSimulManager::AddModifier(const char *modn, co
298                          op->rowBytes += dsiz;
299                  }
300          } else                          // else send all results to this channel
301 <                op->rowBytes += bincnt*dsiz;
301 >                op->rowBytes += mp->nbins*dsiz;
302          return true;
303   }
304  
# Line 289 | Line 325 | RcontribSimulManager::AddModFile(const char *modfn, co
325          return true;
326   }
327  
328 + // call-back to check if modifier has been loaded
329 + static int
330 + checkModExists(const LUENT *lp, void *p)
331 + {
332 +        if (modifier(lp->key) != OVOID)
333 +                return 1;
334 +
335 +        sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key);
336 +        error(WARNING, errmsg);
337 +        return 0;
338 + }
339 +
340   // Prepare output channels and return # completed rows
341   int
342   RcontribSimulManager::PrepOutput()
# Line 297 | Line 345 | RcontribSimulManager::PrepOutput()
345                  error(INTERNAL, "PrepOutput() called before octree & modifiers assigned");
346                  return -1;
347          }
348 +        if (!cdsF) {
349 +                error(INTERNAL, "missing RdataShare constructor call (*cdsF)");
350 +                return -1;
351 +        }
352 +        if (lu_doall(&modLUT, checkModExists, NULL) < 0)
353 +                return -1;
354 +
355          int     remWarnings = 20;
356          for (RcontribOutput *op = outList; op; op = op->next) {
357                  if (op->rData) {
# Line 358 | Line 413 | RcontribOutput::NewHeader(const RcontribSimulManager *
413          strcpy(hdr+begData, ROWZEROSTR);
414          rowCountPos = begData+LNROWSTR;
415          begData += sizeof(ROWZEROSTR)-1;
416 <        if (!xres | (rowBytes > esiz)) {
416 >        if (!rcp->xres | (rowBytes > esiz)) {
417                  sprintf(hdr+begData, "NCOLS=%d\n", int(rowBytes/esiz));
418                  begData += strlen(hdr+begData);
419          }
# Line 396 | Line 451 | RcontribOutput::NewHeader(const RcontribSimulManager *
451                          hdr[begData++] = ' ';
452          hdr[begData++] = '\n';          // EOL for data format
453          hdr[begData++] = '\n';          // end of nominal header
454 <        if ((xres > 0) & (rowBytes == esiz)) {  // tack on resolution string?
455 <                sprintf(hdr+begData, PIXSTDFMT, yres, xres);
454 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // tack on resolution string?
455 >                sprintf(hdr+begData, PIXSTDFMT, rcp->yres, rcp->xres);
456                  begData += strlen(hdr+begData);
457          }
458          return rData->ReleaseMemory(hdr, RDSwrite);
# Line 443 | Line 498 | RcontribOutput::CheckHeader(const RcontribSimulManager
498                  return -1;
499          }
500                                                  // check format
501 <        if (!(cp = findArgs(hdr, FMTSTR, begData)) || strcmp(cp, formstr(etyp))) {
501 >        if (!(cp = findArgs(hdr, FMTSTR, begData)) ||
502 >                                strncmp(cp, formstr(etyp), strlen(formstr(etyp)))) {
503                  sprintf(errmsg, "expected %s%s in '%s'", FMTSTR, formstr(etyp), GetName());
504                  error(USER, errmsg);
505                  return -1;
506          }
507                                                  // check #columns
508          if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) ||
509 <                        !xres | (rowBytes > esiz)) {
509 >                        !rcp->xres | (rowBytes > esiz)) {
510                  sprintf(errmsg, "expected NCOLS=%d in '%s'",
511                                  int(rowBytes/esiz), GetName());
512                  error(USER, errmsg);
# Line 465 | Line 521 | RcontribOutput::CheckHeader(const RcontribSimulManager
521          rowCountPos = cp - hdr;
522          int     rlast = atoi(cp);
523          begData++;                              // advance past closing EOL
524 <        if ((xres > 0) & (rowBytes == esiz)) {  // check/skip resolution string?
524 >        if ((rcp->xres > 0) & (rowBytes == esiz)) {     // check/skip resolution string?
525                  char    rbuf[64];
526 <                sprintf(rbuf, PIXSTDFMT, yres, xres);
526 >                sprintf(rbuf, PIXSTDFMT, rcp->yres, rcp->xres);
527                  int     rlen = strlen(rbuf);
528                  if (strncmp(rbuf, hdr+begData, rlen)) {
529                          sprintf(errmsg, "bad resolution string in '%s'", GetName());
# Line 632 | Line 688 | RcontribSimulManager::GetChild(bool forceWait)
688                                  FD_ISSET(kid[n].w, &writeset) |
689                                  FD_ISSET(kid[n].w, &errset)) {
690                                                  // update output row counts
691 <                        UpdateRowsDone(kidRow[n]);
691 >                        if (!FD_ISSET(kid[n].w, &errset))
692 >                                UpdateRowsDone(kidRow[n]);
693                          kidRow[n] = -1;         // flag it available
694                          pn = n;
695                  }
# Line 716 | Line 773 | RcontribSimulManager::StartKids(int n2go)
773                                  close(kid[nkids].w);
774                          free(kid); free(kidRow);
775                          kid = NULL; kidRow = NULL;
719                        rowsDone.NewBitMap(0);
776                          RunChild();             // should never return
777                          _exit(1);
778                  }
# Line 765 | Line 821 | RcontribSimulManager::SetThreadCount(int nt)
821                  return 0;
822          }
823          if (nt < 0)
824 <                return nkids;
824 >                return NThreads();
825          if (!nt) nt = GetNCores();
826          int     status = 0;
827          if (nt == 1)
# Line 778 | Line 834 | RcontribSimulManager::SetThreadCount(int nt)
834                  sprintf(errmsg, "non-zero (%d) status from child", status);
835                  error(WARNING, errmsg);
836          }
837 <        return nkids;
837 >        return NThreads();
838   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines