ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhcopy.c
(Generate patch)

Comparing ray/src/hd/rhcopy.c (file contents):
Revision 3.34 by greg, Wed Nov 16 00:12:49 2022 UTC vs.
Revision 3.38 by greg, Tue Dec 19 20:49:05 2023 UTC

# Line 51 | Line 51 | typedef struct {
51   } RAYPAR;
52  
53   static int openholo(char *fname, int append);
54 < static void addray(RAYPAR *rp);
54 > static int addray(RAYPAR *rp);
55   static int readval(RREAL *v, int n, FILE *fp);
56   static void readrays(FILE *fp);
57   static int writeval(RREAL *v, int n, FILE *fp);
# Line 228 | Line 228 | openholo(              /* open existing holodeck file for i/o */
228          return(n);
229   }
230  
231 < void
231 > int
232   addray(         /* add a ray to our output holodeck */
233          RAYPAR *rp
234   )
# Line 241 | Line 241 | addray(                /* add a ray to our output holodeck */
241          double  d0, d1;
242          unsigned        dc;
243          RAYVAL  *rv;
244 +        int     nsects = 0;
245                                  /* check each output section */
246          for (sn = nholosects; sn--; ) {
247                  hp = hdlist[sn];
# Line 271 | Line 272 | addray(                /* add a ray to our output holodeck */
272                  rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1];
273                  rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1];
274                  copycolr(rv->v, rp->cv);
275 +                ++nsects;
276          }
277 +        return nsects;
278   }
279  
280   /* Read n-vector from file stream */
# Line 325 | Line 328 | readval(RREAL *v, int n, FILE *fp)
328   static void
329   readrays(FILE *fp)
330   {
331 +        unsigned long   nread=0, ngood=0;
332 +
333          if (iofmt != 'a')
334                  SET_FILE_BINARY(fp);
335   #ifdef getc_unlocked
# Line 379 | Line 384 | readrays(FILE *fp)
384                          if (!flags)     /* got nothing, so may be normal EOF */
385                                  return;
386                  }
387 +                ++nread;
388                  if (flags & (BAD_DIR|BAD_LEN))
389                          continue;       /* just a bad ray is all -- skip */
390                  if (!(flags & GOT_VAL))
# Line 396 | Line 402 | readrays(FILE *fp)
402                          } else
403                                  goto missingData;
404                  }
405 <                addray(&ryp);           /* add our ray to holodeck */
405 >                ngood += (addray(&ryp) > 0);    /* add our ray to holodeck */
406          }
407          return;
408   missingData:
409 <        sprintf(errmsg, "insufficient data in -i%s", rspec);
409 >        sprintf(errmsg, "insufficient data or read error with -i%s after %lu rays read (%lu used)",
410 >                        rspec, nread, ngood);
411          error(USER, errmsg);
412   }
413  
# Line 478 | Line 485 | write_ray(RAYPAR *rp, FILE *fp)
485          return 0;                       /* write error? */
486   }
487  
488 + static BEAMI    *beamdir;
489 +
490 + static int
491 + bpcmp(                  /* compare beam positions on disk */
492 +        const void      *b1p,
493 +        const void      *b2p
494 + )
495 + {
496 +        off_t   pdif = beamdir[*(int *)b1p].fo - beamdir[*(int *)b2p].fo;
497 +
498 +        if (pdif > 0L) return(1);
499 +        if (pdif < 0L) return(-1);
500 +        return(0);
501 + }
502 +
503   /* Write all rays from holodeck to stream */
504   static void
505   writerays(FILE *fp)
# Line 498 | Line 520 | writerays(FILE *fp)
520   #endif
521          for (sn = 0; sn < nholosects; sn++) {   /* write each holodeck section */
522                  HOLO    *hp = hdlist[sn];
523 <                for (bi = nbeams(hp); bi > 0; bi--) {
524 <                        BEAM    *bp = hdgetbeam(hp, bi);
523 >                int     nb = nbeams(hp);        /* sort beams by file location */
524 >                int     *bq = (int *)malloc(nb*sizeof(int));
525 >                if (!bq)
526 >                        error(SYSTEM, "out of memory in writerays()");
527 >                for (bi = nb; bi--; ) bq[bi] = bi+1;
528 >                beamdir = hp->bi;
529 >                qsort(bq, nb, sizeof(*bq), bpcmp);
530 >                for (bi = 0; bi < nb; bi++) {
531 >                        BEAM    *bp = hdgetbeam(hp, bq[bi]);
532                          if (!bp)                /* empty beam? */
533                                  continue;
534 <                        hdbcoord(gc, hp, bi);   /* else write rays */
534 >                        hdbcoord(gc, hp, bq[bi]);
535                          rv = hdbray(bp);
536                          for (k = bp->nrm; k--; rv++) {
537                                  ryp.d = hdray(ryp.ro, ryp.rd, hp, gc, rv->r);
# Line 512 | Line 541 | writerays(FILE *fp)
541                                          ryp.d = 0.;
542                                  ryp.d = hddepth(hp, rv->d) - ryp.d;
543                                  copycolr(ryp.cv, rv->v);
544 <                                if (!write_ray(&ryp, fp))
544 >                                if (!write_ray(&ryp, fp)) {
545 >                                        free(bq);
546                                          goto writError;
547 +                                }
548                          }
549 <                        hdfreebeam(hp, bi);
549 >                        hdfreebeam(hp, bq[bi]);
550                  }
551 +                free(bq);
552          }
553          if (fflush(fp) != EOF)
554                  return;
# Line 524 | Line 556 | writError:
556          error(SYSTEM, "error writing holodeck rays");
557   }
558  
527 static BEAMI    *beamdir;
528
559   static int
530 bpcmp(                  /* compare beam positions on disk */
531        const void      *b1p,
532        const void      *b2p
533 )
534 {
535        off_t   pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo;
536
537        if (pdif > 0L) return(1);
538        if (pdif < 0L) return(-1);
539        return(0);
540 }
541
542 static int
560   addclump(               /* transfer the given clump and free */
561          HOLO    *hp,
562          int     *bq,
# Line 554 | Line 571 | addclump(              /* transfer the given clump and free */
571          BEAM    *bp;
572                                          /* sort based on file position */
573          beamdir = hp->bi;
574 <        qsort((char *)bq, nb, sizeof(*bq), bpcmp);
574 >        qsort(bq, nb, sizeof(*bq), bpcmp);
575                                          /* transfer each beam */
576          for (i = 0; i < nb; i++) {
577                  bp = hdgetbeam(hp, bq[i]);
# Line 736 | Line 753 | addpicz(               /* add a picture + depth-buffer */
753  
754   void
755   eputs(                  /* put error message to stderr */
756 <        char  *s
756 >        const char  *s
757   )
758   {
759          static int  midline = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines