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.15 by greg, Sat Feb 22 02:07:24 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;
# Line 84 | Line 110 | int    append;
110          extern long     ftell();
111          FILE    *fp;
112          int     fd;
113 <        int     badfmt = 0;
114 <        int4    nextloc;
113 >        int     hflags = 0;
114 >        long    nextloc;
115          int     n;
116                                          /* open holodeck file */
117          if ((fp = fopen(fname, append ? "r+" : "r")) == NULL) {
# Line 94 | Line 120 | int    append;
120                  error(SYSTEM, errmsg);
121          }
122                                          /* check header and magic number */
123 <        if (append)
124 <                badfmt |= getheader(fp, holheadline, &badfmt) < 0;
99 <        else
100 <                badfmt = checkheader(fp, HOLOFMT, NULL) < 0;
101 <        if (badfmt || getw(fp) != HOLOMAGIC) {
123 >        if (getheader(fp, holheadline, (char *)&hflags) < 0 ||
124 >                        hflags&H_BADF || getw(fp) != HOLOMAGIC) {
125                  sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
126                  error(USER, errmsg);
127          }
# Line 106 | Line 129 | int    append;
129          nextloc = ftell(fp);                    /* get stdio position */
130          fclose(fp);                             /* done with stdio */
131          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
132 <                lseek(fd, (long)nextloc, 0);
132 >                lseek(fd, (off_t)nextloc, 0);
133                  read(fd, (char *)&nextloc, sizeof(nextloc));
134 <                hdinit(fd, NULL);
134 >                hdinit(fd, NULL)->priv = hflags&H_OBST ? &obstr :
135 >                                hflags&H_OBSF ? &unobstr : (char *)NULL;
136          }
137          return(n);
138   }
139  
140 + #undef H_BADF
141 + #undef H_OBST
142 + #undef H_OBSF
143  
144 +
145   addray(ro, rd, d, cv)           /* add a ray to our output holodeck */
146   FVECT   ro, rd;
147   double  d;
148   COLR    cv;
149   {
150 <        int     sn;
150 >        int     sn, bi, n;
151          register HOLO   *hp;
152          GCOORD  gc[2];
153          BYTE    rr[2][2];
154 +        BEAM    *bp;
155          double  d0, d1;
156 +        unsigned        dc;
157          register RAYVAL *rv;
158                                  /* check each output section */
159          for (sn = noutsects; sn--; ) {
# Line 131 | Line 161 | COLR   cv;
161                  d0 = hdinter(gc, rr, &d1, hp, ro, rd);
162                  if (d <= d0 || d1 < -0.001)
163                          continue;       /* missed section */
164 <                if (obstructions > 0 && d0 < -0.001)
165 <                        continue;       /* ray starts too late */
166 <                if (!obstructions && d < 0.999*d1)
167 <                        continue;       /* ray ends too soon */
168 <                                        /* should we check for duplicates? */
169 <                rv = hdnewrays(hp, hdbindex(hp, gc), 1);
164 >                if (checkdepth) {               /* check depth */
165 >                        if (hp->priv == &obstr && d0 < -0.001)
166 >                                continue;       /* ray starts too late */
167 >                        if (hp->priv == &unobstr && d < 0.999*d1)
168 >                                continue;       /* ray ends too soon */
169 >                }
170 >                dc = hdcode(hp, d-d0);
171 >                bi = hdbindex(hp, gc);          /* check for duplicates */
172 >                if (checkrepeats && (bp = hdgetbeam(hp, bi)) != NULL) {
173 >                        for (n = bp->nrm, rv = hdbray(bp); n--; rv++)
174 >                                if ((hp->priv != NULL || rv->d == dc) &&
175 >                                                rv->r[0][0] == rr[0][0] &&
176 >                                                rv->r[0][1] == rr[0][1] &&
177 >                                                rv->r[1][0] == rr[1][0] &&
178 >                                                rv->r[1][1] == rr[1][1])
179 >                                        break;
180 >                        if (n >= 0)
181 >                                continue;       /* found a matching ray */
182 >                }
183 >                rv = hdnewrays(hp, bi, 1);
184 >                rv->d = dc;
185                  rv->r[0][0] = rr[0][0]; rv->r[0][1] = rr[0][1];
186                  rv->r[1][0] = rr[1][0]; rv->r[1][1] = rr[1][1];
187                  copycolr(rv->v, cv);
143                rv->d = hdcode(hp, d-d0);
188          }
189   }
190  
191  
192 < addholo(hdf)                    /* add a holodeck file */
193 < char    *hdf;
192 > static BEAMI    *beamdir;
193 >
194 > static int
195 > bpcmp(b1p, b2p)                 /* compare beam positions on disk */
196 > int     *b1p, *b2p;
197   {
198 <        int     fd;
199 <        register HOLO   *hp;
200 <        register BEAM   *bp;
198 >        register long   pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
199 >
200 >        if (pdif > 0L) return(1);
201 >        if (pdif < 0L) return(-1);
202 >        return(0);
203 > }
204 >
205 > static int
206 > addclump(hp, bq, nb)            /* transfer the given clump and free */
207 > HOLO    *hp;
208 > int     *bq, nb;
209 > {
210          GCOORD  gc[2];
211          FVECT   ro, rd;
212          double  d;
213 <        int     i, j;
213 >        int     i;
214          register int    k;
215 +        register BEAM   *bp;
216 +                                        /* sort based on file position */
217 +        beamdir = hp->bi;
218 +        qsort((char *)bq, nb, sizeof(*bq), bpcmp);
219 +                                        /* transfer each beam */
220 +        for (i = 0; i < nb; i++) {
221 +                bp = hdgetbeam(hp, bq[i]);
222 +                hdbcoord(gc, hp, bq[i]);
223 +                                                /* add each ray to output */
224 +                for (k = bp->nrm; k--; ) {
225 +                        d = hdray(ro, rd, hp, gc, hdbray(bp)[k].r);
226 +                        if (hp->priv == &unobstr)
227 +                                VSUM(ro, ro, rd, d);
228 +                        else
229 +                                d = 0.;
230 +                        d = hddepth(hp, hdbray(bp)[k].d) - d;
231 +                        addray(ro, rd, d, hdbray(bp)[k].v);
232 +                }
233 +                hdfreebeam(hp, bq[i]);          /* free the beam */
234 +        }
235 +        return(0);
236 + }
237  
238 <        openholo(hdf, 0);               /* open the holodeck for reading */
238 > addholo(hdf)                    /* add a holodeck file */
239 > char    *hdf;
240 > {
241 >        int     fd;
242 >                                        /* open the holodeck for reading */
243 >        openholo(hdf, 0);
244          fd = hdlist[noutsects]->fd;     /* remember the file handle */
245 <        while ((hp = hdlist[noutsects]) != NULL) {      /* load each section */
246 <                for (j = nbeams(hp); j > 0; j--)        /* load each beam */
247 <                        if ((bp = hdgetbeam(hp, j)) != NULL) {
248 <                                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 */
245 >        while (hdlist[noutsects] != NULL) {     /* load each section */
246 >                                                        /* clump the beams */
247 >                clumpbeams(hdlist[noutsects], 0, BKBSIZE*1024, addclump);
248 >                hddone(hdlist[noutsects]);              /* free the section */
249          }
250 <        close(fd);                      /* close the file */
250 >        close(fd);                      /* close input file */
251 >        hdflush(NULL);                  /* flush output */
252   }
253  
254  
# Line 185 | Line 261 | struct phead {
261   };
262  
263  
264 + int
265   picheadline(s, ph)              /* process picture header line */
266   char    *s;
267   struct phead    *ph;
# Line 193 | Line 270 | struct phead   *ph;
270  
271          if (formatval(fmt, s)) {
272                  ph->badfmt = strcmp(fmt, COLRFMT);
273 <                return;
273 >                return(0);
274          }
275          if (isprims(s)) {
276                  ph->altprims++;         /* don't want to deal with this */
277 <                return;
277 >                return(0);
278          }
279          if (isexpos(s)) {
280                  ph->expos *= exposval(s);
281 <                return;
281 >                return(0);
282          }
283          if (isview(s)) {
284                  ph->gotview += sscanview(&ph->vw, s);
285 <                return;
285 >                return(0);
286          }
287 +        return(0);
288   }
289  
290  
# Line 240 | Line 318 | char   *pcf, *zbf;
318          copystruct(&phd.vw, &stdview);
319          phd.expos = 1.0;
320          phd.badfmt = phd.gotview = phd.altprims = 0;
321 <        if (getheader(pfp, picheadline, &phd) < 0 ||
321 >        if (getheader(pfp, picheadline, (char *)&phd) < 0 ||
322                          phd.badfmt || !fgetsresolu(&prs, pfp)) {
323                  sprintf(errmsg, "bad format for picture file \"%s\"", pcf);
324                  error(USER, errmsg);
# Line 305 | Line 383 | char   *pcf, *zbf;
383                          addray(ro, rd, (double)zscn[i], cscn[i]);
384                  }
385          }
386 +                                /* write output and free beams */
387 +        hdflush(NULL);
388                                  /* clean up */
389 <        free((char *)cscn);
390 <        free((char *)zscn);
389 >        free((void *)cscn);
390 >        free((void *)zscn);
391          fclose(pfp);
392          close(zfd);
393   }
394  
395  
396 + void
397   eputs(s)                        /* put error message to stderr */
398   register char  *s;
399   {
# Line 332 | Line 413 | register char  *s;
413   }
414  
415  
416 + void
417   quit(code)                      /* exit the program gracefully */
418   int     code;
419   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines