ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhoptimize.c
Revision: 3.5
Committed: Thu Nov 12 15:07:49 1998 UTC (25 years, 5 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.4: +31 -8 lines
Log Message:
change clumpbeams() callback function to take clump's queue

File Contents

# Content
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Optimize holodeck for quick access.
9 *
10 * 11/4/98 Greg Ward Larson
11 */
12
13 #include "holo.h"
14
15 #include <signal.h>
16
17 #ifndef BKBSIZE
18 #define BKBSIZE 256 /* beam clump size (kilobytes) */
19 #endif
20
21 char *progname;
22 char tempfile[128];
23
24 extern char *rindex();
25 extern int quit();
26 extern long rhinitcopy();
27
28
29 main(argc, argv)
30 int argc;
31 char *argv[];
32 {
33 char *inpname, *outname;
34 int hdfd[2];
35 long nextipos, lastopos, thisopos;
36
37 progname = argv[0];
38 if (argc < 2 | argc > 3) {
39 fprintf(stderr, "Usage: %s input.hdk [output.hdk]\n", progname);
40 exit(1);
41 }
42 inpname = argv[1];
43 if (argc == 3) /* use given output file */
44 outname = argv[2];
45 else { /* else use temporary file */
46 if (access(inpname, R_OK|W_OK) < 0) { /* check permissions */
47 sprintf(errmsg, "cannot access \"%s\"", inpname);
48 error(SYSTEM, errmsg);
49 }
50 strcpy(tempfile, inpname);
51 if ((outname = rindex(tempfile, '/')) != NULL)
52 outname++;
53 else
54 outname = tempfile;
55 sprintf(outname, "rho%d.hdk", getpid());
56 outname = tempfile;
57 }
58 /* copy holodeck file header */
59 nextipos = rhinitcopy(hdfd, inpname, outname);
60 lastopos = 0L; /* copy sections one by one */
61 while (nextipos != 0L) {
62 /* set input position; get next */
63 lseek(hdfd[0], nextipos, 0);
64 read(hdfd[0], (char *)&nextipos, sizeof(nextipos));
65 /* get output position; set last */
66 thisopos = lseek(hdfd[1], 0L, 2);
67 if (lastopos > 0L) {
68 lseek(hdfd[1], lastopos, 0);
69 write(hdfd[1], (char *)&thisopos, sizeof(thisopos));
70 lseek(hdfd[1], 0L, 2);
71 }
72 lastopos = thisopos;
73 thisopos = 0L; /* write place holder */
74 write(hdfd[1], (char *)&thisopos, sizeof(thisopos));
75 /* copy holodeck section */
76 copysect(hdfd[0], hdfd[1]);
77 }
78 /* clean up */
79 close(hdfd[0]);
80 close(hdfd[1]);
81 if (outname == tempfile && rename(outname, inpname) < 0) {
82 sprintf(errmsg, "cannot rename \"%s\" to \"%s\"",
83 outname, inpname);
84 error(SYSTEM, errmsg);
85 }
86 exit(0);
87 }
88
89
90 long
91 rhinitcopy(hfd, infn, outfn) /* open files and copy header */
92 int hfd[2]; /* returned file descriptors */
93 char *infn, *outfn;
94 {
95 FILE *infp, *outfp;
96 long ifpos;
97 /* open files for i/o */
98 if ((infp = fopen(infn, "r")) == NULL) {
99 sprintf(errmsg, "cannot open \"%s\" for reading", infn);
100 error(SYSTEM, errmsg);
101 }
102 if ((outfp = fopen(outfn, "w+")) == NULL) {
103 sprintf(errmsg, "cannot open \"%s\" for writing", outfn);
104 error(SYSTEM, errmsg);
105 }
106 /* set up signal handling */
107 if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN);
108 if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGHUP, SIG_IGN);
109 if (signal(SIGTERM, quit) == SIG_IGN) signal(SIGTERM, SIG_IGN);
110 #ifdef SIGXCPU
111 if (signal(SIGXCPU, quit) == SIG_IGN) signal(SIGXCPU, SIG_IGN);
112 if (signal(SIGXFSZ, quit) == SIG_IGN) signal(SIGXFSZ, SIG_IGN);
113 #endif
114 /* copy and verify header */
115 if (checkheader(infp, HOLOFMT, outfp) < 0 || getw(infp) != HOLOMAGIC)
116 error(USER, "input not in holodeck format");
117 fputformat(HOLOFMT, outfp);
118 fputc('\n', outfp);
119 putw(HOLOMAGIC, outfp);
120 /* get descriptors and free stdio */
121 if ((hfd[0] = dup(fileno(infp))) < 0 ||
122 (hfd[1] = dup(fileno(outfp))) < 0)
123 error(SYSTEM, "dup call failed in rhinitcopy");
124 ifpos = ftell(infp);
125 fclose(infp);
126 if (fclose(outfp) == EOF)
127 error(SYSTEM, "file flushing error in rhinitcopy");
128 /* check cache size */
129 if (BKBSIZE*1024*1.5 > hdcachesize)
130 hdcachesize = BKBSIZE*1024*1.5;
131 /* return input position */
132 return(ifpos);
133 }
134
135
136 static BEAMI *beamdir;
137
138 static int
139 bpcmp(b1p, b2p) /* compare beam positions on disk */
140 int *b1p, *b2p;
141 {
142 register long pdif = beamdir[*b1p].fo - beamdir[*b2p].fo;
143
144 if (pdif > 0L) return(1);
145 if (pdif < 0L) return(-1);
146 return(0);
147 }
148
149 static HOLO *hout;
150
151 static int
152 xferclump(hp, bq, nb) /* transfer the given clump to hout and free */
153 HOLO *hp;
154 int *bq, nb;
155 {
156 register int i;
157 register BEAM *bp;
158
159 beamdir = hp->bi; /* sort based on file position */
160 qsort((char *)bq, nb, sizeof(*bq), bpcmp);
161 /* transfer and free each beam */
162 for (i = 0; i < nb; i++) {
163 bp = hdgetbeam(hp, bq[i]);
164 bcopy((char *)hdbray(bp), (char *)hdnewrays(hout,bq[i],bp->nrm),
165 bp->nrm*sizeof(RAYVAL));
166 hdfreebeam(hp, bq[i]);
167 }
168 hdflush(hout); /* write & free clump */
169 return(0);
170 }
171
172 copysect(ifd, ofd) /* copy holodeck section from ifd to ofd */
173 int ifd, ofd;
174 {
175 HOLO *hinp;
176 /* load input section directory */
177 hinp = hdinit(ifd, NULL);
178 /* create output section directory */
179 hout = hdinit(ofd, (HDGRID *)hinp);
180 /* clump the beams */
181 clumpbeams(hinp, 0, BKBSIZE*1024, xferclump);
182 /* clean up */
183 hddone(hinp);
184 hddone(hout);
185 }
186
187
188 eputs(s) /* put error message to stderr */
189 register char *s;
190 {
191 static int midline = 0;
192
193 if (!*s)
194 return;
195 if (!midline++) { /* prepend line with program name */
196 fputs(progname, stderr);
197 fputs(": ", stderr);
198 }
199 fputs(s, stderr);
200 if (s[strlen(s)-1] == '\n') {
201 fflush(stderr);
202 midline = 0;
203 }
204 }
205
206
207 quit(code) /* exit the program gracefully */
208 int code;
209 {
210 if (tempfile[0])
211 unlink(tempfile);
212 exit(code);
213 }