ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhoptimize.c
Revision: 3.4
Committed: Mon Nov 9 17:11:40 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.3: +15 -164 lines
Log Message:
moved routines to clumpbeams.c and made rhcopy clump beams

File Contents

# User Rev Content
1 gwlarson 3.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 gwlarson 3.3 #include <signal.h>
16    
17 gwlarson 3.1 #ifndef BKBSIZE
18 gwlarson 3.2 #define BKBSIZE 256 /* beam clump size (kilobytes) */
19 gwlarson 3.1 #endif
20    
21     char *progname;
22 gwlarson 3.3 char tempfile[128];
23 gwlarson 3.1
24 gwlarson 3.3 extern char *rindex();
25     extern int quit();
26 gwlarson 3.1 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 gwlarson 3.3 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 gwlarson 3.1 outname++;
53     else
54 gwlarson 3.3 outname = tempfile;
55 gwlarson 3.1 sprintf(outname, "rho%d.hdk", getpid());
56 gwlarson 3.3 outname = tempfile;
57 gwlarson 3.1 }
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 gwlarson 3.3 if (outname == tempfile && rename(outname, inpname) < 0) {
82 gwlarson 3.1 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 gwlarson 3.3 /* 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 gwlarson 3.1 /* copy and verify header */
115 gwlarson 3.3 if (checkheader(infp, HOLOFMT, outfp) < 0 || getw(infp) != HOLOMAGIC)
116 gwlarson 3.1 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 gwlarson 3.4 /* check cache size */
129     if (BKBSIZE*1024*1.5 > hdcachesize)
130     hdcachesize = BKBSIZE*1024*1.5;
131 gwlarson 3.1 /* return input position */
132     return(ifpos);
133     }
134    
135    
136 gwlarson 3.4 static HOLO *hout; /* output holodeck section */
137 gwlarson 3.1
138 gwlarson 3.4 static int
139     xferbeam(bp, hb) /* transfer the given beam to hout and free */
140     register BEAM *bp;
141     HDBEAMI *hb;
142 gwlarson 3.1 {
143 gwlarson 3.4 bcopy((char *)hdbray(bp), (char *)hdnewrays(hout,hb->b,bp->nrm),
144     bp->nrm*sizeof(RAYVAL));
145     hdfreebeam(hb->h, hb->b);
146 gwlarson 3.2 return(0);
147     }
148    
149 gwlarson 3.1 copysect(ifd, ofd) /* copy holodeck section from ifd to ofd */
150     int ifd, ofd;
151     {
152 gwlarson 3.4 HOLO *hinp;
153 gwlarson 3.1 /* load input section directory */
154     hinp = hdinit(ifd, NULL);
155     /* create output section directory */
156     hout = hdinit(ofd, (HDGRID *)hinp);
157 gwlarson 3.4 /* clump the beams */
158     clumpbeams(hinp, 0, BKBSIZE*1024, xferbeam);
159     /* clean up */
160 gwlarson 3.1 hddone(hinp);
161     hddone(hout);
162     }
163    
164    
165     eputs(s) /* put error message to stderr */
166     register char *s;
167     {
168     static int midline = 0;
169    
170     if (!*s)
171     return;
172     if (!midline++) { /* prepend line with program name */
173     fputs(progname, stderr);
174     fputs(": ", stderr);
175     }
176     fputs(s, stderr);
177     if (s[strlen(s)-1] == '\n') {
178     fflush(stderr);
179     midline = 0;
180     }
181     }
182    
183    
184     quit(code) /* exit the program gracefully */
185     int code;
186     {
187 gwlarson 3.3 if (tempfile[0])
188     unlink(tempfile);
189 gwlarson 3.1 exit(code);
190     }