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.1 by gregl, Thu Dec 18 09:33:42 1997 UTC vs.
Revision 3.18 by schorsch, Mon Jul 21 22:30:18 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Copy data into a holodeck file
6   */
7  
8   #include "holo.h"
9   #include "view.h"
13 #include "resolu.h"
10  
11 + #ifndef BKBSIZE
12 + #define BKBSIZE         256             /* beam clump size (kilobytes) */
13 + #endif
14 +
15 + int     checkdepth = 1;         /* check depth (!-d option)? */
16 + int     checkrepeats = 0;       /* check for repeats (-u option)? */
17   int     frompicz;               /* input from pictures & depth-buffers? */
18   int     noutsects;              /* number of output sections */
19 < int     obstructions = -1;      /* interior obstructions allowed? */
19 > char    obstr, unobstr;         /* flag pointer values */
20  
21   char    *progname;              /* global argv[0] */
22  
# Line 26 | Line 28 | char   *argv[];
28          int     i;
29  
30          progname = argv[0];
31 <        if (argc < 4)
31 >        frompicz = -1;
32 >        for (i = 2; i < argc && argv[i][0] == '-'; i++)
33 >                switch (argv[i][1]) {
34 >                case 'u':
35 >                        checkrepeats = 1;
36 >                        break;
37 >                case 'd':
38 >                        checkdepth = 0;
39 >                        break;
40 >                case 'h':
41 >                        frompicz = 0;
42 >                        break;
43 >                case 'p':
44 >                        frompicz = 1;
45 >                        break;
46 >                default:
47 >                        goto userr;
48 >                }
49 >        if (i >= argc || frompicz < 0)
50                  goto userr;
51 <        if (!strcmp(argv[2], "-h"))
32 <                frompicz = 0;
33 <        else if (!strcmp(argv[2], "-pz"))
34 <                frompicz = 1;
35 <        else
51 >        if (frompicz && (argc-i)%2)
52                  goto userr;
37        if (frompicz && (argc-3)%2)
38                goto userr;
53          noutsects = openholo(argv[1], 1);
54 <        if (frompicz)
55 <                for (i = 3; i < argc; i += 2)
54 >        if (frompicz) {
55 >                for ( ; i < argc; i += 2)
56                          addpicz(argv[i], argv[i+1]);
57 <        else
58 <                for (i = 3; i < argc; i++)
57 >        } else {
58 >                if (BKBSIZE*1024*1.5 > hdcachesize)
59 >                        hdcachesize = BKBSIZE*1024*1.5;
60 >                for ( ; i < argc; i++)
61                          addholo(argv[i]);
62 +        }
63          quit(0);
64   userr:
65 <        fprintf(stderr, "Usage: %s output.hdk -h inp1.hdk ..\n", progname);
49 <        fprintf(stderr, "   Or: %s output.hdk -pz inp1.pic inp1.zbf ..\n",
65 >        fprintf(stderr, "Usage: %s output.hdk [-u][-d] -h inp1.hdk ..\n",
66                          progname);
67 +        fprintf(stderr, "   Or: %s output.hdk [-u][-d] -p inp1.pic inp1.zbf ..\n",
68 +                        progname);
69          exit(1);
70   }
71  
72  
73 < holheadline(s, bf)              /* check holodeck header line */
73 > #define H_BADF  01
74 > #define H_OBST  02
75 > #define H_OBSF  04
76 >
77 > int
78 > holheadline(s, hf)              /* check holodeck header line */
79   register char   *s;
80 < int     *bf;
80 > int     *hf;
81   {
82          char    fmt[32];
83  
84          if (formatval(fmt, s)) {
85 <                *bf = strcmp(fmt, HOLOFMT);
86 <                return;
85 >                if (strcmp(fmt, HOLOFMT))
86 >                        *hf |= H_BADF;
87 >                else
88 >                        *hf &= ~H_BADF;
89 >                return(0);
90          }
91          if (!strncmp(s, "OBSTRUCTIONS=", 13)) {
92                  s += 13;
93                  while (*s == ' ') s++;
94                  if (*s == 't' | *s == 'T')
95 <                        obstructions = 1;
95 >                        *hf |= H_OBST;
96                  else if (*s == 'f' | *s == 'F')
97 <                        obstructions = 0;
97 >                        *hf |= H_OBSF;
98                  else
99                          error(WARNING, "bad OBSTRUCTIONS value in holodeck");
100 <                return;
100 >                return(0);
101          }
102 +        return(0);
103   }
104  
78
105   int
106   openholo(fname, append)         /* open existing holodeck file for i/o */
107   char    *fname;
108   int     append;
109   {
84        extern long     ftell();
110          FILE    *fp;
111          int     fd;
112 <        int     badfmt = 0;
113 <        int4    nextloc;
112 >        int     hflags = 0;
113 >        long    nextloc;
114          int     n;
115                                          /* open holodeck file */
116          if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) {
# Line 94 | Line 119 | int    append;
119                  error(SYSTEM, errmsg);
120          }
121                                          /* check header and magic number */
122 <        if (append)
123 <                badfmt |= getheader(fp, holheadline, &badfmt) < 0;
99 <        else
100 <                badfmt = checkheader(fp, HOLOFMT, NULL) < 0;
101 <        if (badfmt || getw(fp) != HOLOMAGIC) {
122 >        if (getheader(fp, holheadline, (char *)&hflags) < 0 ||
123 >                        hflags&H_BADF || getw(fp) != HOLOMAGIC) {
124                  sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
125                  error(USER, errmsg);
126          }
# Line 106 | Line 128 | int    append;
128          nextloc = ftell(fp);                    /* get stdio position */
129          fclose(fp);                             /* done with stdio */
130          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
131 <                lseek(fd, (long)nextloc, 0);
131 >                lseek(fd, (off_t)nextloc, 0);
132                  read(fd, (char *)&nextloc, sizeof(nextloc));
133 <                hdinit(fd, NULL);
133 >                hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
134 >                                hflags&H_OBSF ? &unobstr : (char *)NULL;
135          }
136          return(n);
137   }
138  
139 + #undef H_BADF
140 + #undef H_OBST
141 + #undef H_OBSF
142  
143 +
144   addray(ro, rd, d, cv)           /* add a ray to our output holodeck */
145   FVECT   ro, rd;
146   double  d;
147   COLR    cv;
148   {
149 <        int     sn;
149 >        int     sn, bi, n;
150          register HOLO   *hp;
151          GCOORD  gc[2];
152          BYTE    rr[2][2];
153 +        BEAM    *bp;
154          double  d0, d1;
155 +        unsigned        dc;
156          register RAYVAL *rv;
157                                  /* check each output section */
158          for (sn = noutsects; sn--; ) {
# Line 131 | Line 160 | COLR   cv;
160                  d0 = hdinter(gc, rr, &d1, hp, ro, rd);
161                  if (d <= d0 || d1 < -0.001)
162                          continue;       /* missed section */
163 <                if (obstructions > 0 && d0 < -0.001)
164 <                        continue;       /* ray starts too late */
165 <                if (!obstructions && d < 0.999*d1)
166 <                        continue;       /* ray ends too soon */
167 <                                        /* should we check for duplicates? */
168 <                rv = hdnewrays(hp, hdbindex(hp, gc), 1);
163 >                if (checkdepth) {               /* check depth */
164 >                        if (hp->priv == &obstr && d0 < -0.001)
165 >                                continue;       /* ray starts too late */
166 >                        if (hp->priv == &unobstr && d < 0.999*d1)
167 >                                continue;       /* ray ends too soon */
168 >                }
169 >                dc = hdcode(hp, d-d0);
170 >                bi = hdbindex(hp, gc);          /* check for duplicates */
171 >                if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) {
172 >                        for (n = bp->nrm, rv = hdbray(bp); n--; rv++)
173 >                                if ((hp->priv != NULL || rv->d == dc) &&
174 >                                                rv->r[0][0] == rr[0][0] &&
175 >                                                rv->r[0][1] == rr[0][1] &&
176 >                                                rv->r[1][0] == rr[1][0] &&
177 >                                                rv->r[1][1] == rr[1][1])
178 >                                        break;
179 >                        if (n >= 0)
180 >                                continue;       /* found a matching ray */
181 >                }
182 >                rv = hdnewrays(hp, bi, 1);
183 >                rv->d = dc;
184                  rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1];
185                  rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1];
186                  copycolr(rv->v, cv);
143                rv->d = hdcode(hp, d-d0);
187          }
188   }
189  
190  
191 < addholo(hdf)                    /* add a holodeck file */
192 < char    *hdf;
191 > static BEAMI    *beamdir;
192 >
193 > static int
194 > bpcmp(b1p, b2p)                 /* compare beam positions on disk */
195 > int     *b1p, *b2p;
196   {
197 <        int     fd;
198 <        register HOLO   *hp;
199 <        register BEAM   *bp;
197 >        register off_t  pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
198 >
199 >        if (pdif > 0L) return(1);
200 >        if (pdif < 0L) return(-1);
201 >        return(0);
202 > }
203 >
204 > static int
205 > addclump(hp, bq, nb)            /* transfer the given clump and free */
206 > HOLO    *hp;
207 > int     *bq, nb;
208 > {
209          GCOORD  gc[2];
210          FVECT   ro, rd;
211          double  d;
212 <        int     i, j;
212 >        int     i;
213          register int    k;
214 +        register BEAM   *bp;
215 +                                        /* sort based on file position */
216 +        beamdir = hp->bi;
217 +        qsort((char *)bq, nb, sizeof(*bq), bpcmp);
218 +                                        /* transfer each beam */
219 +        for (i = 0; i < nb; i++) {
220 +                bp = hdgetbeam(hp, bq[i]);
221 +                hdbcoord(gc, hp, bq[i]);
222 +                                                /* add each ray to output */
223 +                for (k = bp->nrm; k--; ) {
224 +                        d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r);
225 +                        if (hp->priv == &unobstr)
226 +                                VSUM(ro, ro, rd, d);
227 +                        else
228 +                                d = 0.;
229 +                        d = hddepth(hp, hdbray(bp)[k].d) - d;
230 +                        addray(ro, rd, d, hdbray(bp)[k].v);
231 +                }
232 +                hdfreebeam(hp, bq[i]);          /* free the beam */
233 +        }
234 +        return(0);
235 + }
236  
237 <        openholo(hdf, 0);               /* open the holodeck for reading */
237 > addholo(hdf)                    /* add a holodeck file */
238 > char    *hdf;
239 > {
240 >        int     fd;
241 >                                        /* open the holodeck for reading */
242 >        openholo(hdf, 0);
243          fd = hdlist[noutsects]->fd;     /* remember the file handle */
244 <        while ((hp = hdlist[noutsects]) != NULL) {      /* load each section */
245 <                for (j = nbeams(hp); j > 0; j--)        /* load each beam */
246 <                        if ((bp = hdgetbeam(hp, j)) != NULL) {
247 <                                hdbcoord(gc, hp, j);
166 <                                for (k = bp->nrm; k--; ) {
167 <                                        hdray(ro, rd, hp, gc, hdbray(bp)[k].r);
168 <                                        d = hddepth(hp, hdbray(bp)[k].d);
169 <                                        addray(ro, rd, d, hdbray(bp)[k].v);
170 <                                }
171 <                                hdfreebeam(hp, j);      /* free the beam */
172 <                        }
173 <                hddone(hp);                             /* free the section */
244 >        while (hdlist[noutsects] != NULL) {     /* load each section */
245 >                                                        /* clump the beams */
246 >                clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
247 >                hddone(hdlist[noutsects]);              /* free the section */
248          }
249 <        close(fd);                      /* close the file */
249 >        close(fd);                      /* close input file */
250 >        hdflush(NULL);                  /* flush output */
251   }
252  
253  
# Line 185 | Line 260 | struct phead {
260   };
261  
262  
263 + int
264   picheadline(s, ph)              /* process picture header line */
265   char    *s;
266   struct phead    *ph;
# Line 193 | Line 269 | struct phead   *ph;
269  
270          if (formatval(fmt, s)) {
271                  ph->badfmt = strcmp(fmt, COLRFMT);
272 <                return;
272 >                return(0);
273          }
274          if (isprims(s)) {
275                  ph->altprims++;         /* don't want to deal with this */
276 <                return;
276 >                return(0);
277          }
278          if (isexpos(s)) {
279                  ph->expos *= exposval(s);
280 <                return;
280 >                return(0);
281          }
282          if (isview(s)) {
283                  ph->gotview += sscanview(&ph->vw, s);
284 <                return;
284 >                return(0);
285          }
286 +        return(0);
287   }
288  
289  
# Line 221 | Line 298 | char   *pcf, *zbf;
298          int     eshft;
299          double  emult;
300          RESOLU  prs;
301 <        FLOAT   vl[2];
301 >        RREAL   vl[2];
302          FVECT   ro, rd;
303          double  aftd;
304          COLOR   ctmp;
# Line 237 | Line 314 | char   *pcf, *zbf;
314                  error(SYSTEM, pcf);
315          }
316                                  /* load picture header */
317 <        copystruct(&phd.vw, &stdview);
317 >        phd.vw = stdview;
318          phd.expos = 1.0;
319          phd.badfmt = phd.gotview = phd.altprims = 0;
320 <        if (getheader(pfp, picheadline, &phd) < 0 ||
320 >        if (getheader(pfp, picheadline, (char *)&phd) < 0 ||
321                          phd.badfmt || !fgetsresolu(&prs, pfp)) {
322                  sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
323                  error(USER, errmsg);
# Line 305 | Line 382 | char   *pcf, *zbf;
382                          addray(ro, rd, (double)zscn[i], cscn[i]);
383                  }
384          }
385 +                                /* write output and free beams */
386 +        hdflush(NULL);
387                                  /* clean up */
388 <        free((char *)cscn);
389 <        free((char *)zscn);
388 >        free((void *)cscn);
389 >        free((void *)zscn);
390          fclose(pfp);
391          close(zfd);
392   }
393  
394  
395 + void
396   eputs(s)                        /* put error message to stderr */
397   register char  *s;
398   {
# Line 332 | Line 412 | register char  *s;
412   }
413  
414  
415 + void
416   quit(code)                      /* exit the program gracefully */
417   int     code;
418   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines