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

Comparing ray/src/rt/RpictSimulManager.cpp (file contents):
Revision 2.8 by greg, Fri Aug 23 02:08:28 2024 UTC vs.
Revision 2.15 by greg, Sat Jan 25 04:57:27 2025 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   *  Created by Greg Ward on 07/11/2024.
10   */
11  
12 + #define DEBUG   1       // XXX temporary!
13 +
14   #include <ctype.h>
15   #include "platform.h"
16   #include "RpictSimulManager.h"
# Line 103 | Line 105 | RpictSimulManager::NewFrame(const VIEW &v, int xydim[2
105  
106          if (!xydim) return false;
107          if (!ap) ap = &pasp;
108 <        pvw = vw;                       // save previous view for motion blur
109 <        vw = v;
108 >        if (&v == &vw) {
109 >                pvw.type = 0;
110 >        } else {
111 >                pvw = vw;               // save previous view for motion blur
112 >                vw = v;
113 >        }
114          const char *    verr = setview(&vw);
115          if (verr) {
116                  error(WARNING, verr);
# Line 196 | Line 202 | RpictSimulManager::SetTile(const int ti[2])
202   bool
203   RpictSimulManager::ComputePixel(int x, int y)
204   {
205 +        DCHECK(doneMap.OffBitMap(x,y),
206 +                        CONSISTENCY, "illegal pixel index in ComputPixel()");
207          int     i;
208          FVECT   rodir[2];
209          double  hpos = (x+pixjitter())/TWidth();
# Line 232 | Line 240 | RpictSimulManager::ComputePixel(int x, int y)
240  
241   // Check if neighbor differences are below pixel sampling threshold
242   bool
243 < RpictSimulManager::BelowSampThresh(int x, int y, const int noff[4][2]) const
243 > RpictSimulManager::BelowSampThresh(const int x, const int y, const int noff[4][2]) const
244   {
245          SCOLOR  pval[4];
246          float   dist[4];
247          int     i, j;
248  
249          for (i = 4; i--; ) {            // get pixels from tile store
250 <                int     px = x + noff[i][0];
251 <                int     py = y + noff[i][1];
250 >                const int       px = x + noff[i][0];
251 >                const int       py = y + noff[i][1];
252                  if (!doneMap.Check(px, py) ||
253                                  !pacc.GetPixel(px, py, pval[i], &dist[i]))
254                          return false;
255          }
256 <        const bool      spectr = (pacc.NC() > 3);
257 <        for (i = 4; --i; )              // do pairwise comparisons
258 <            for (j = i; j--; ) {
259 <                if (pacc.DepthType() &&
252 <                                (dist[i] - dist[j] > maxdiff*dist[j]) |
256 >                                        // do pairwise comparisons
257 >        for (i = (pacc.DepthType() != RDTnone)*4; --i > 0; )
258 >            for (j = i; j--; )
259 >                if ((dist[i] - dist[j] > maxdiff*dist[j]) |
260                                  (dist[j] - dist[i] > maxdiff*dist[i]))
261                          return false;
262 <                if (spectr ? sbigsdiff(pval[i], pval[j], maxdiff) :
263 <                                bigdiff(pval[i], pval[j], maxdiff))
262 >        if (pacc.NC() > 3) {
263 >            for (i = 4; --i; )
264 >                for (j = i; j--; )
265 >                    if (sbigsdiff(pval[i], pval[j], maxdiff))
266                          return false;
267 <            }
268 <        return true;                    // linear interpolation OK
267 >        } else {
268 >            for (i = 4; --i; )
269 >                for (j = i; j--; )
270 >                    if (bigdiff(pval[i], pval[j], maxdiff))
271 >                        return false;
272 >        }
273 >        return true;
274   }
275  
276   // Fill an interior square patch with interpolated values
# Line 267 | Line 281 | RpictSimulManager::FillSquare(const int x, const int y
281          float   dist[4];
282          int     i, j;
283                                          // assumes 4 corners are valid!
284 <        for (i = 4; i--; )
284 >        for (i = 4; i--; ) {
285 >                DCHECK(!doneMap.Check(x+noff[i][0], y+noff[i][1]),
286 >                        CONSISTENCY, "inclusion of bad pixel in FillSquare()");
287                  pacc.GetPixel(x+noff[i][0], y+noff[i][1], pval[i], &dist[i]);
288 <
288 >        }
289          i = abs(noff[1][0]-noff[0][0]);
290          j = abs(noff[1][1]-noff[0][1]); // i==j for diamond fill
291          const int       slen =  (i > j) ? i : j;
# Line 378 | Line 394 | RpictSimulManager::RenderRect(const int x0, const int
394                  SetQuincunx(&sampMap, noff, 1<<sp2, layer&1, x0, y0);
395                  sampMap -= doneSamples; // avoid resampling pixels
396                  // Are we into adaptive sampling realm?
397 <                if (noff[0][0]*noff[0][0] + noff[0][1]*noff[0][1] < 4*psample*psample) {
397 >                if (noff[0][0]*noff[0][0] + noff[0][1]*noff[0][1] < psample*psample) {
398                          if (FlushQueue() < 0)   // need results to check thresholds
399                                  return false;
400                          ABitMap2        fillMap = sampMap;
# Line 389 | Line 405 | RpictSimulManager::RenderRect(const int x0, const int
405                          const ABitMap2  origSampMap = sampMap;
406                          for (x = 4; x--; ) {
407                                  ABitMap2        stamp = origSampMap;
408 <                                stamp.Shift(noff[x][0], noff[x][1]);
408 >                                stamp.Shift(noff[x][0] + noff[(x+1)&3][0],
409 >                                                noff[x][1] + noff[(x+1)&3][1]);
410                                  sampMap |= stamp;
411                          }               // ...but don't resample what's done
412                          sampMap -= doneSamples;
413                                          // interpolate smooth regions
414                          fillMap -= sampMap;
415 +                        fillMap -= doneSamples;
416                          for (x = y = 0; fillMap.Find(&x, &y); x++)
417                                  FillSquare(x, y, noff);
418                          doneSamples |= doneMap;
419                  }                       // compute required ray samples
420                  for (x = y = 0; sampMap.Find(&x, &y); x++)
421 <                        if (!ComputePixel(x, y))
421 >                        if (!ComputePixel(x, y)) {
422 >                                sprintf(errmsg, "ComputePixel(%d,%d) failed", x, y);
423 >                                error(WARNING, errmsg);
424                                  return false;
425 +                        }
426                  doneSamples |= sampMap; // samples now done or at least queued
427                  sp2 -= layer++ & 1;     // next denser sampling
428          }
429 <        if (FlushQueue() < 0)           // make sure we got everyone
429 >        if (FlushQueue() < 0)           // compute stragglers
430                  return false;
410        x = y = 0;
411        if (doneMap.Find(&x, &y, false)) {
412                sprintf(errmsg, "missed %ld tile pixels, e.g. (%d,%d)",
413                                (long)doneMap.Width()*doneMap.Height() -
414                                        doneMap.SumTotal(), x, y);
415                error(WARNING, errmsg);
416        }
431          if ((prCB != NULL) & (barPix == NULL))
432                  (*prCB)(100.);
433          return true;
# Line 438 | Line 452 | RpictSimulManager::RenderTile(COLORV *rp, int ystride,
452          else if (prims)
453                  pacc.SetColorSpace(RDTrgb, prims);
454  
455 <        int     x0=0, y0=0;
442 <        if (tile) {
443 <                x0 = -tile[0]*TWidth();
444 <                y0 = -tile[1]*THeight();
445 <        }
446 <        return SetTile(tile) && RenderRect(x0, y0);
455 >        return SetTile(tile) && RenderRect();
456   }
457  
458   // Same but store as common-exponent COLR or SCOLR
# Line 460 | Line 469 | RpictSimulManager::RenderTile(COLRV *bp, int ystride,
469          else if (prims)
470                  pacc.SetColorSpace(RDTrgbe, prims);
471  
472 <        int     x0=0, y0=0;
464 <        if (tile) {
465 <                x0 = -tile[0]*TWidth();
466 <                y0 = -tile[1]*THeight();
467 <        }
468 <        return SetTile(tile) && RenderRect(x0, y0);
472 >        return SetTile(tile) && RenderRect();
473   }
474  
475   // Same but also use 16-bit encoded depth buffer
# Line 482 | Line 486 | RpictSimulManager::RenderTile(COLRV *bp, int ystride,
486          else if (prims)
487                  pacc.SetColorSpace(RDTrgbe, prims);
488  
489 <        int     x0=0, y0=0;
486 <        if (tile) {
487 <                x0 = -tile[0]*TWidth();
488 <                y0 = -tile[1]*THeight();
489 <        }
490 <        return SetTile(tile) && RenderRect(x0, y0);
489 >        return SetTile(tile) && RenderRect();
490   }
491  
492   // Back to float color with 16-bit depth
# Line 504 | Line 503 | RpictSimulManager::RenderTile(COLORV *rp, int ystride,
503          else if (prims)
504                  pacc.SetColorSpace(RDTrgb, prims);
505  
506 <        int     x0=0, y0=0;
508 <        if (tile) {
509 <                x0 = -tile[0]*TWidth();
510 <                y0 = -tile[1]*THeight();
511 <        }
512 <        return SetTile(tile) && RenderRect(x0, y0);
506 >        return SetTile(tile) && RenderRect();
507   }
508  
509   // Allocate a new render bar
# Line 561 | Line 555 | RpictSimulManager::LowerBar(int v, int ytop)
555                          sizeof(COLORV)*NC*TWidth()*(THeight()-v));
556          memmove(barDepth, barDepth + TWidth()*v,
557                          sizeof(float)*TWidth()*(THeight()-v));
558 <        if (ytop < THeight()) {                 // mark what we won't do as finished
558 >        if (ytop < THeight())                   // mark what we won't do as finished
559                  doneMap.ClearRect(0, 0, TWidth(), THeight()-ytop, true);
566                memset(barPix, 0, sizeof(COLORV)*NC*TWidth()*(THeight()-ytop));
567                memset(barDepth, 0, sizeof(float)*TWidth()*(THeight()-ytop));
568        }
560          return true;
561   }
562  
# Line 588 | Line 579 | RpictSimulManager::RenderBelow(int ytop, const int vst
579                          cropview(&ptvw, 0., double(ytop-THeight())/GetHeight(),
580                                                  1., double(ytop)/GetHeight()))
581                  ptvw.type = 0;
582 <                                                // set up spectral sampling
583 <        if (setspectrsamp(CNDX, WLPART) <= 0) {
582 >                                                // update spectral sampling
583 >        int     rv = setspectrsamp(CNDX, WLPART);
584 >        if (rv < 0) {
585                  error(USER, "unsupported spectral sampling");
586                  return false;
587          }
588 +        if (!rv & (RDTcolorT(dt) != RDTscolor) & (RDTcolorT(dt) != RDTscolr))
589 +                error(WARNING, "spectral range incompatible with color output");
590          COLORV **       parr = NULL;            // set up tiny source drawing
591          float **        zarr = NULL;
592 <        if (!ptvw.type && directvis && dblur <= FTINY) {
592 >        if (!ptvw.type && directvis && (dblur <= FTINY) & (mblur <= FTINY)) {
593                  parr = new COLORV * [THeight()];
594                  zarr = new float * [THeight()];
595                  for (int n = THeight(); n-- > 0; ) {
# Line 611 | Line 605 | RpictSimulManager::RenderBelow(int ytop, const int vst
605                          (*prCB)(100.*(GetHeight()-ytop)/GetHeight());
606                  if (!RenderRect(0, THeight()-ytop))     // render this bar
607                          return false;
608 <                int     nlines = lastOut - ytop + THeight();
608 >                int     nlines = lastOut - ytop + vstep;
609                  if (nlines > ytop)
610                          nlines = ytop;
611                  else if (parr)                  // drawing sources?
# Line 675 | Line 669 | RpictSimulManager::NewOutput(FILE *pdfp[2], const char
669   {
670          pdfp[0] = pdfp[1] = NULL;
671          if (!RDTcolorT(dt))
672 <                error(INTERNAL, "botched color output type in NewOutput()");
672 >                error(INTERNAL, "missing color output type in NewOutput()");
673          if (NCSAMP == 3) {
674                  if (RDTcolorT(dt) == RDTscolr)
675                          dt = RDTnewCT(dt, prims==xyzprims ? RDTxyze : RDTrgbe);
# Line 690 | Line 684 | RpictSimulManager::NewOutput(FILE *pdfp[2], const char
684                          error(INTERNAL, "writing picture to a command not supported");
685                          return RDTnone;
686                  }
687 <                fd = open(pfname, O_WRONLY|O_CREAT|O_EXCL, 0666);
687 >                fd = open(pfname, O_RDWR|O_CREAT|O_EXCL, 0666);
688          }
689          if (fd < 0) {
690                  if ((frameNo <= 0) | (errno != EEXIST)) {
691                          sprintf(errmsg, "cannot open picture file '%s'", pfname);
692                          error(SYSTEM, errmsg);
693                  }
694 <                return RDTnone;                 // expected in parallel sequence
694 >                return RDTnone;                 // may be expected in sequence run
695          }
696          if (fd == 1)
697                  pdfp[0] = stdout;
698 <        else if (!(pdfp[0] = fdopen(fd, "w")))
698 >        else if (!(pdfp[0] = fdopen(fd, "w+")))
699                  error(SYSTEM, "failure calling fdopen()");
700          SET_FILE_BINARY(pdfp[0]);               // write picture header
701          if ((pdfp[0] != stdout) | (frameNo <= 1)) {
702                  newheader("RADIANCE", pdfp[0]);
703 <                fputs(GetHeader(), pdfp[0]);
703 >                fputs(GetHeadStr(), pdfp[0]);
704          }
705          fputs(VIEWSTR, pdfp[0]); fprintview(&vw, pdfp[0]); fputc('\n', pdfp[0]);
706          if (frameNo > 0)
# Line 752 | Line 746 | RpictSimulManager::NewOutput(FILE *pdfp[2], const char
746                  fputendian(pdfp[0]);
747                  fputformat("float", pdfp[0]);
748                  break;
749 <        default:;
749 >        default:;       // pro forma - caught this above
750          }
751 <        fputc('\n', pdfp[0]);                   // flush picture header + resolution
751 >        fputc('\n', pdfp[0]);                   // flush picture header
752          if (fflush(pdfp[0]) == EOF) {
753                  sprintf(errmsg, "cannot write header to picture '%s'", pfname);
754                  error(SYSTEM, errmsg);
# Line 766 | Line 760 | RpictSimulManager::NewOutput(FILE *pdfp[2], const char
760                  if (dfname[0] == '!')
761                          pdfp[1] = popen(dfname+1, "w");
762                  else
763 <                        pdfp[1] = fopen(dfname, "w");
763 >                        pdfp[1] = fopen(dfname, "w+");
764                  if (!pdfp[1]) {
765                          sprintf(errmsg, "cannot open depth output '%s'", dfname);
766                          error(SYSTEM, errmsg);
# Line 778 | Line 772 | RpictSimulManager::NewOutput(FILE *pdfp[2], const char
772          }
773          if (RDTdepthT(dt) == RDTdshort) {       // write header for 16-bit depth?
774                  newheader("RADIANCE", pdfp[1]);
775 <                fputs(GetHeader(), pdfp[1]);
775 >                fputs(GetHeadStr(), pdfp[1]);
776                  fputs(VIEWSTR, pdfp[1]); fprintview(&vw, pdfp[1]); fputc('\n', pdfp[1]);
777                  fputs(DEPTHSTR, pdfp[1]); fputs(dunit, pdfp[1]); fputc('\n', pdfp[1]);
778                  fputformat(DEPTH16FMT, pdfp[1]);
# Line 814 | Line 808 | RpictSimulManager::RenderFrame(const char *pfname, Ren
808          if (RDTdepthT(dt) == RDTdshort)
809                  fprtresolu(GetWidth(), GetHeight(), pdfp[1]);
810  
811 <        const int       bheight = (psample > 1) ? int(4*psample+.99) : 8;
811 >        const int       bheight = (psample > 1) ? int(8*psample+.99) : 16;
812          const int       vstep = bheight >> (psample > 1);
813  
814          NewBar(bheight);                        // render frame if we can
815          if (!RenderBelow(GetHeight(), vstep, pdfp[0], dt, pdfp[1])) {
816                  fclose(pdfp[0]);
817                  if (pdfp[1]) (dfname[0] == '!') ? pclose(pdfp[1]) : fclose(pdfp[1]);
824                Cleanup();
818                  return RDTnone;
819          }
820          NewBar();                               // clean up and return
# Line 1008 | Line 1001 | RpictSimulManager::ReopenOutput(FILE *pdfp[2], const c
1001                  pdfp[0] = NULL;
1002                  return RDTnone;
1003          }
1004 +        if (hinfo.gotview) {                    // header view overrides
1005 +                pvw = vw;
1006 +                vw = hinfo.vw;
1007 +        }
1008          if (!dfname)                            // no depth file?
1009                  return dt;
1010  
# Line 1027 | Line 1024 | RpictSimulManager::ReopenOutput(FILE *pdfp[2], const c
1024          }
1025          SET_FILE_BINARY(pdfp[1]);
1026          int     n, len = strlen(HDRSTR);
1027 <        char    buf[32];                // sniff for 16-bit header
1027 >        char    buf[32];                        // sniff for 16-bit header
1028          if (getbinary(buf, 1, len+1, pdfp[1]) < len+1) {
1029                  sprintf(errmsg, "empty depth file '%s'", dfname);
1030                  error(SYSTEM, errmsg);
# Line 1037 | Line 1034 | RpictSimulManager::ReopenOutput(FILE *pdfp[2], const c
1034          }
1035          for (n = 0; n < len; n++)
1036                  if (buf[n] != HDRSTR[n])
1037 <                        break;          // not a Radiance header
1037 >                        break;                  // not a Radiance header
1038          rewind(pdfp[1]);
1039          if ((n < len) | !isprint(buf[len]))
1040                  return RDTnewDT(dt, RDTdfloat);
1041  
1042 <        HeaderInfo      dinfo;          // thinking it's 16-bit encoded
1042 >        HeaderInfo      dinfo;                  // thinking it's 16-bit encoded
1043          if (getheader(pdfp[1], head_check, &dinfo) < 0)
1044                  sprintf(errmsg, "bad header in encoded depth file '%s'",
1045                                  dfname);
# Line 1081 | Line 1078 | RpictSimulManager::ResumeFrame(const char *pfname, con
1078          case RDTxyze:
1079                  break;
1080          case RDTscolr:
1081 <                bytesPer = hinfo.ncomp + 1;     // XXX assumes no compression
1081 >                bytesPer = NCSAMP + 1;  // XXX assumes no compression
1082                  break;
1083          case RDTrgb:
1084          case RDTxyz:
1085                  bytesPer = sizeof(float)*3;
1086                  break;
1087          case RDTscolor:
1088 <                bytesPer = sizeof(float)*hinfo.ncomp;
1088 >                bytesPer = sizeof(float)*NCSAMP;
1089                  break;
1090          default:
1091 <                sprintf(errmsg, "unknown format (%s) for '%s'", hinfo.fmt, pfname);
1091 >                sprintf(errmsg, "unknown format for '%s'", pfname);
1092                  error(USER, errmsg);
1093                  fclose(pdfp[0]);
1094                  if (pdfp[1]) fclose(pdfp[1]);
# Line 1105 | Line 1102 | RpictSimulManager::ResumeFrame(const char *pfname, con
1102                  if (pdfp[1]) fclose(pdfp[1]);
1103                  return RDTnone;
1104          }
1105 <        vw.type = 0;                            // set up new (unreferenced) frame
1109 <        frameNo = 0;
1105 >        frameNo = 0;                            // set up unreferenced frame
1106          int     hvdim[2] = {res.xr, res.yr};
1107          double  noAdj = 0;
1108 <        if (!NewFrame(hinfo.vw, hvdim, &noAdj) ||
1108 >        if (!NewFrame(vw, hvdim, &noAdj) ||
1109                          (hvdim[0] != res.xr) | (hvdim[1] != res.yr)) {
1110                  error(CONSISTENCY, "unexpected resolution change in ResumeFrame()");
1111                  fclose(pdfp[0]);
# Line 1177 | Line 1173 | RpictSimulManager::ResumeFrame(const char *pfname, con
1173                  fclose(pdfp[0]); fclose(pdfp[1]);
1174                  return RDTnone;
1175          }
1176 <        int     bheight = (psample > 1) ? int(4*psample+.99) : 8;
1176 >        int     bheight = (psample > 1) ? int(8*psample+.99) : 16;
1177          if (bheight > GetHeight()-doneScans)
1178                  bheight = GetHeight()-doneScans;
1179          int     vstep =  bheight >> (psample > 1);
# Line 1187 | Line 1183 | RpictSimulManager::ResumeFrame(const char *pfname, con
1183          if (!RenderBelow(GetHeight()-doneScans, vstep, pdfp[0], dt, pdfp[1])) {
1184                  fclose(pdfp[0]);
1185                  if (pdfp[1]) fclose(pdfp[1]);
1190                Cleanup();
1186                  return RDTnone;
1187          }
1188          NewBar();                               // close up and return success

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines