--- ray/src/rt/RcontribSimulManager.cpp 2024/10/30 01:38:21 2.3 +++ ray/src/rt/RcontribSimulManager.cpp 2024/12/10 00:38:59 2.10 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: RcontribSimulManager.cpp,v 2.3 2024/10/30 01:38:21 greg Exp $"; +static const char RCSid[] = "$Id: RcontribSimulManager.cpp,v 2.10 2024/12/10 00:38:59 greg Exp $"; #endif /* * RcontribSimulManager.cpp @@ -24,11 +24,6 @@ extern const char HDRSTR[]; extern const char BIGEND[]; extern const char FMTSTR[]; -int contrib = 0; // computing contributions? - -int xres = 0; // horizontal (scan) size -int yres = 0; // vertical resolution - // new/exclusive, overwrite if exists, or recover data int RSDOflags[] = {RDSwrite|RDSexcl|RDSextend, RDSwrite|RDSextend, RDSread|RDSwrite}; @@ -40,7 +35,7 @@ static const char ROWZEROSTR[] = "NROWS=00000000000000 struct RcontribMod { RcontribOutput * opl; // pointer to first output channel char * params; // parameters string - EPNODE * binv; // bin expression + EPNODE * binv; // bin expression (NULL if 1 bin) int nbins; // bin count this modifier int coffset; // column offset in bytes DCOLORV cbin[1]; // bin accumulator (extends struct) @@ -57,6 +52,19 @@ struct RowAssignment { uint32 ac; // accumulation count }; +// Get format identifier +const char * +formstr(int f) +{ + switch (f) { + case 'a': return("ascii"); + case 'f': return("float"); + case 'd': return("double"); + case 'c': return(NCSAMP==3 ? COLRFMT : SPECFMT); + } + return("unknown"); +} + // Our default data share function RdataShare * defDataShare(const char *name, RCOutputOp op, size_t siz) @@ -68,17 +76,36 @@ defDataShare(const char *name, RCOutputOp op, size_t s RcontribMod * NewRcMod(const char *prms, const char *binexpr, int ncbins) { - if (ncbins <= 0) return NULL; if (!prms) prms = ""; - if (!binexpr | (ncbins == 1)) - binexpr = "0"; - + if ((ncbins > 1) & !binexpr) { + error(USER, "missing bin expression"); + return NULL; + } + if (ncbins < 1) ncbins = 1; + RcontribMod * mp = (RcontribMod *)ecalloc(1, sizeof(RcontribMod) + sizeof(DCOLORV)*(NCSAMP*ncbins-1) + strlen(prms)+1); + if (binexpr) { // check bin expression + mp->binv = eparse(const_cast(binexpr)); + if (mp->binv->type == NUM) { // constant expression (0)? + if ((int)(evalue(mp->binv) + .5) > 0) { + sprintf(errmsg, "illegal positive constant for bin (%s)", + binexpr); + error(USER, errmsg); + } + if (ncbins > 1) { + sprintf(errmsg, "bad bin count (%d should be 1)", ncbins); + error(USER, errmsg); + } + epfree(mp->binv, true); + mp->binv = NULL; + prms = ""; + ncbins = 1; + } + } mp->params = strcpy((char *)(mp->cbin + ncbins*NCSAMP), prms); - mp->binv = eparse(const_cast(binexpr)); mp->nbins = ncbins; return mp; } @@ -88,7 +115,8 @@ void FreeRcMod(void *p) { if (!p) return; - epfree((*(RcontribMod *)p).binv, true); + EPNODE * bep = (*(RcontribMod *)p).binv; + if (bep) epfree(bep, true); efree(p); } @@ -135,20 +163,24 @@ RcontribSimulManager::RctCall(RAY *r, void *cd) if (!mp) return 0; // not in our modifier list - worldfunc(RCCONTEXT, r); // compute bin # - set_eparams(mp->params); - double bval = evalue(mp->binv); - if (bval <= -.5) - return 0; // silently ignore negative bin index - DCOLORV * dvp = (*mp)[int(bval + .5)]; + int bi = 0; // get bin index + if (mp->binv) { + worldfunc(RCCONTEXT, r); + set_eparams(mp->params); + double bval = evalue(mp->binv); + if (bval <= -.5) + return 0; // silently ignore negative bin index + bi = int(bval + .5); + } + DCOLORV * dvp = (*mp)[bi]; if (!dvp) { - sprintf(errmsg, "bad bin number for '%s' (%.1f ignored)", mname, bval); + sprintf(errmsg, "bad bin number for '%s' (%d ignored)", mname, bi); error(WARNING, errmsg); return 0; } SCOLOR contr; raycontrib(contr, r, PRIMARY); // compute coefficient - if (contrib) + if (rcp->HasFlag(RCcontrib)) smultscolor(contr, r->rcol); // -> value contribution for (int i = 0; i < NCSAMP; i++) *dvp++ += contr[i]; // accumulate color/spectrum @@ -178,16 +210,20 @@ bool RcontribSimulManager::AddModifier(const char *modn, const char *outspec, const char *prms, const char *binval, int bincnt) { - if (!modn | !outspec | (bincnt <= 0) || !*modn | !*outspec) { + if (!modn | !outspec || !*modn | !*outspec) { error(WARNING, "ignoring bad call to AddModifier()"); return false; } if (!nChan) { // initial call? + if ((xres < 0) | (yres <= 0)) { + error(USER, "xres, yres must be set before first modifier"); + return false; + } if (!SetDataFormat(dtyp)) return false; nChan = NCSAMP; } else if (nChan != NCSAMP) { - error(INTERNAL, "number of spectral channels must be fixed"); + error(USER, "number of spectral channels must be fixed"); return false; } if (Ready()) { @@ -210,7 +246,7 @@ RcontribSimulManager::AddModifier(const char *modn, co const int binndx = hasFormat(outspec, "diouxX"); int bin0 = 0; char fnbuf[512]; - if (!modndx | (modndx > binndx)) + if (!modndx || (binndx > 0) & (modndx > binndx)) sprintf(fnbuf, outspec, bin0, modn); else sprintf(fnbuf, outspec, modn, bin0); @@ -239,11 +275,11 @@ RcontribSimulManager::AddModifier(const char *modn, co mp->opl = op; // first (maybe only) output channel if (modndx) // remember modifier if part of name op->omod = lp->key; - if (binndx > 0) { // append output image/bin list + if (binndx) { // append output image/bin list op->rowBytes += dsiz; op->obin = bin0; for (bincnt = 1; bincnt < mp->nbins; bincnt++) { - if (!modndx | (modndx > binndx)) + if (!modndx || (binndx > 0) & (modndx > binndx)) sprintf(fnbuf, outspec, bin0+bincnt, modn); else sprintf(fnbuf, outspec, modn, bin0+bincnt); @@ -262,7 +298,7 @@ RcontribSimulManager::AddModifier(const char *modn, co op->rowBytes += dsiz; } } else // else send all results to this channel - op->rowBytes += bincnt*dsiz; + op->rowBytes += mp->nbins*dsiz; return true; } @@ -289,6 +325,18 @@ RcontribSimulManager::AddModFile(const char *modfn, co return true; } +// call-back to check if modifier has been loaded +static int +checkModExists(const LUENT *lp, void *p) +{ + if (modifier(lp->key) != OVOID) + return 1; + + sprintf(errmsg, "tracked modifier '%s' not found in main scene", lp->key); + error(WARNING, errmsg); + return 0; +} + // Prepare output channels and return # completed rows int RcontribSimulManager::PrepOutput() @@ -297,6 +345,13 @@ RcontribSimulManager::PrepOutput() error(INTERNAL, "PrepOutput() called before octree & modifiers assigned"); return -1; } + if (!cdsF) { + error(INTERNAL, "missing RdataShare constructor call (*cdsF)"); + return -1; + } + if (lu_doall(&modLUT, checkModExists, NULL) < 0) + return -1; + int remWarnings = 20; for (RcontribOutput *op = outList; op; op = op->next) { if (op->rData) { @@ -358,7 +413,7 @@ RcontribOutput::NewHeader(const RcontribSimulManager * strcpy(hdr+begData, ROWZEROSTR); rowCountPos = begData+LNROWSTR; begData += sizeof(ROWZEROSTR)-1; - if (!xres | (rowBytes > esiz)) { + if (!rcp->xres | (rowBytes > esiz)) { sprintf(hdr+begData, "NCOLS=%d\n", int(rowBytes/esiz)); begData += strlen(hdr+begData); } @@ -396,8 +451,8 @@ RcontribOutput::NewHeader(const RcontribSimulManager * hdr[begData++] = ' '; hdr[begData++] = '\n'; // EOL for data format hdr[begData++] = '\n'; // end of nominal header - if ((xres > 0) & (rowBytes == esiz)) { // tack on resolution string? - sprintf(hdr+begData, PIXSTDFMT, yres, xres); + if ((rcp->xres > 0) & (rowBytes == esiz)) { // tack on resolution string? + sprintf(hdr+begData, PIXSTDFMT, rcp->yres, rcp->xres); begData += strlen(hdr+begData); } return rData->ReleaseMemory(hdr, RDSwrite); @@ -451,7 +506,7 @@ RcontribOutput::CheckHeader(const RcontribSimulManager } // check #columns if (((cp = findArgs(hdr, "NCOLS=", begData)) && atoi(cp)*esiz != rowBytes) || - !xres | (rowBytes > esiz)) { + !rcp->xres | (rowBytes > esiz)) { sprintf(errmsg, "expected NCOLS=%d in '%s'", int(rowBytes/esiz), GetName()); error(USER, errmsg); @@ -466,9 +521,9 @@ RcontribOutput::CheckHeader(const RcontribSimulManager rowCountPos = cp - hdr; int rlast = atoi(cp); begData++; // advance past closing EOL - if ((xres > 0) & (rowBytes == esiz)) { // check/skip resolution string? + if ((rcp->xres > 0) & (rowBytes == esiz)) { // check/skip resolution string? char rbuf[64]; - sprintf(rbuf, PIXSTDFMT, yres, xres); + sprintf(rbuf, PIXSTDFMT, rcp->yres, rcp->xres); int rlen = strlen(rbuf); if (strncmp(rbuf, hdr+begData, rlen)) { sprintf(errmsg, "bad resolution string in '%s'", GetName()); @@ -766,7 +821,7 @@ RcontribSimulManager::SetThreadCount(int nt) return 0; } if (nt < 0) - return nkids; + return NThreads(); if (!nt) nt = GetNCores(); int status = 0; if (nt == 1) @@ -779,5 +834,5 @@ RcontribSimulManager::SetThreadCount(int nt) sprintf(errmsg, "non-zero (%d) status from child", status); error(WARNING, errmsg); } - return nkids; + return NThreads(); }