source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/vrippack-0.31/src/vrip/vripFileCmds.cc @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 10.4 KB
Line 
1/*
2
3Brian Curless
4
5Computer Graphics Laboratory
6Stanford University
7
8---------------------------------------------------------------------
9
10Copyright (1997) The Board of Trustees of the Leland Stanford Junior
11University. Except for commercial resale, lease, license or other
12commercial transactions, permission is hereby given to use, copy,
13modify this software for academic purposes only.  No part of this
14software or any derivatives thereof may be used in the production of
15computer models for resale or for use in a commercial
16product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND
17CONCERNING THIS SOFTWARE.  No support is implied or provided.
18
19*/
20
21
22#include <limits.h>
23#include <stdlib.h>
24#include <math.h>
25#include <unistd.h>
26#include <sys/wait.h>
27
28#include "vrip.h"
29#include "vripFileCmds.h"
30#include "vripGlobals.h"
31#include "vripAux.h"
32
33
34int
35Vrip_WriteGridCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
36{
37    if (argc != 2 && argc != 3) {
38        interp->result = "Usage: vrip_writegrid <filename>";
39        return TCL_ERROR;
40    }
41
42    // The third argument is supposed to be something that flags us
43    //  not to transpose the grid
44
45    if (argc == 2) {   
46        // Unflip the grid
47        if (frontRLEGrid->flip) {
48            frontRLEGrid->flip = FALSE;
49            frontRLEGrid->origin[2] = -frontRLEGrid->origin[2];
50        }
51
52        // Restore to original untransposed coords
53        if (frontRLEGrid->axis != Z_AXIS) {
54           if (frontRLEGrid->axis == X_AXIS) {
55              frontRLEGrid->transposeXZ(backRLEGrid);
56              swapgrids();
57           }
58           else {
59              frontRLEGrid->transposeYZ(backRLEGrid);
60              swapgrids();
61           }
62
63           frontRLEGrid->axis = Z_AXIS;
64        }
65    }
66
67    if (!frontRLEGrid->write(argv[1])) {
68        interp->result = "vrip_writegrid: could not write file";
69        return TCL_ERROR;
70    }
71
72    return TCL_OK;
73}
74
75
76int
77Vrip_WriteGridNormCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
78{
79    if (argc != 2) {
80        interp->result = "Usage: vrip_writegridnorm <filename>";
81        return TCL_ERROR;
82    }
83
84
85    // Unflip the grid
86    if (frontRLEGridNorm->flip) {
87        frontRLEGridNorm->flip = FALSE;
88        frontRLEGridNorm->origin[2] = -frontRLEGridNorm->origin[2];
89    }
90
91    // Restore to original untransposed coords
92    if (frontRLEGridNorm->axis != Z_AXIS) {
93       if (frontRLEGridNorm->axis == X_AXIS) {
94          frontRLEGridNorm->transposeXZ(backRLEGridNorm);
95          swapgrids();
96       }
97       else {
98          frontRLEGridNorm->transposeYZ(backRLEGridNorm);
99          swapgrids();
100       }
101
102       frontRLEGridNorm->axis = Z_AXIS;
103    }
104
105
106    if (!frontRLEGridNorm->write(argv[1])) {
107        interp->result = "vrip_writegridnorm: could not write file";
108        return TCL_ERROR;
109    }
110
111    return TCL_OK;
112}
113
114
115int
116Vrip_WriteDenCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
117{
118    if (argc != 2) {
119        interp->result = "Usage: vrip_writeden <filename>";
120        return TCL_ERROR;
121    }
122   
123    theGrid->writeDen(argv[1]);
124
125    return TCL_OK;
126}
127
128
129int
130Vrip_ReadGridCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
131{
132    int maxDim;
133
134    if (argc != 2) {
135        interp->result = "Usage: vrip_readgrid <filename>";
136        return TCL_ERROR;
137    }
138
139    if (frontRLEGrid == NULL) {
140        frontRLEGrid = new OccGridRLE(50,50,50,CHUNK_SIZE);
141        backRLEGrid = new OccGridRLE(50,50,50,CHUNK_SIZE);
142    }
143   
144    if (!frontRLEGrid->read(argv[1])) {
145        interp->result = "vrip_readgrid: could not read file";
146        return TCL_ERROR;
147    }
148
149    // Don't delete - re-use!!
150
151    delete backRLEGrid;
152
153
154    backRLEGrid = new OccGridRLE(frontRLEGrid->xdim, frontRLEGrid->ydim,
155                               frontRLEGrid->zdim, CHUNK_SIZE);
156    backRLEGrid->resolution = frontRLEGrid->resolution;
157    backRLEGrid->origin[0] = frontRLEGrid->origin[0];
158    backRLEGrid->origin[1] = frontRLEGrid->origin[1];
159    backRLEGrid->origin[2] = frontRLEGrid->origin[2];
160
161    if (theDepthMap != NULL) {
162        delete theDepthMap;
163    }
164
165    maxDim = MAX(frontRLEGrid->xdim, MAX(frontRLEGrid->ydim, 
166                                         frontRLEGrid->zdim));
167    theDepthMap = new DepthMap(int(maxDim*2.2), 
168                               int(maxDim*2.2), 
169                               FALSE, DEPTH_TREE_GRANULARITY);
170
171    return TCL_OK;
172}
173
174
175
176int
177Vrip_ReadFlatGridCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
178{
179    int maxDim;
180    int xdim, ydim, zdim;
181
182    if (argc != 2) {
183        interp->result = "Usage: vrip_readflatgrid <filename>";
184        return TCL_ERROR;
185    }
186
187    if (frontRLEGrid != NULL) {
188        delete frontRLEGrid;
189    }
190
191    if (backRLEGrid != NULL) {
192        delete backRLEGrid;
193    }
194
195    FILE *fp = fopen(argv[1], "rb");
196    fread(&xdim, sizeof(int), 1, fp);
197    fread(&ydim, sizeof(int), 1, fp);
198    fread(&zdim, sizeof(int), 1, fp);
199
200    frontRLEGrid = new OccGridRLE(xdim, ydim, zdim, CHUNK_SIZE);
201
202    uchar *ucScanline = new uchar[xdim];
203    OccElement *occScanline = new OccElement[xdim];
204
205    for (int zz = 0; zz < zdim; zz++) {
206       for (int yy = 0; yy < ydim; yy++) {
207          fread(ucScanline, sizeof(uchar), xdim, fp);
208          for (int xx = 0; xx < xdim; xx++) {
209             occScanline[xx].value = (ushort)(ucScanline[xx]/255.0*USHRT_MAX);
210             occScanline[xx].totalWeight = 1;
211          }
212          frontRLEGrid->putScanline(occScanline, yy, zz);
213       }
214    }
215
216    delete ucScanline;
217    delete occScanline;
218
219    backRLEGrid = new OccGridRLE(frontRLEGrid->xdim, frontRLEGrid->ydim,
220                               frontRLEGrid->zdim, CHUNK_SIZE);
221    backRLEGrid->resolution = frontRLEGrid->resolution;
222    backRLEGrid->origin[0] = frontRLEGrid->origin[0];
223    backRLEGrid->origin[1] = frontRLEGrid->origin[1];
224    backRLEGrid->origin[2] = frontRLEGrid->origin[2];
225
226    if (theDepthMap != NULL) {
227        delete theDepthMap;
228    }
229
230    maxDim = MAX(frontRLEGrid->xdim, MAX(frontRLEGrid->ydim, 
231                                         frontRLEGrid->zdim));
232    theDepthMap = new DepthMap(int(maxDim*2.2), 
233                               int(maxDim*2.2), 
234                               FALSE, DEPTH_TREE_GRANULARITY);
235
236    return TCL_OK;
237}
238
239
240int
241Vrip_ReadRawImagesCmd(ClientData, Tcl_Interp *interp, int argc, 
242                      const char *argv[])
243{
244    int maxDim;
245    int xdim, ydim, zdim;
246    char filename[PATH_MAX], other[PATH_MAX];
247
248    if (argc != 13) {
249        interp->result = "Usage: vrip_readrawimages <root-name> <ext> <sig-digits> <image-xdim> <image-ydim> <xmin> <xmax> <ymin> <ymax> <zmin> <zmax> <downsample-rate>";
250        return TCL_ERROR;
251    }
252
253    // Need to give option to downsample
254    // Need to provide min/max thresholding into empty and unknown
255
256    int sigDigits = atoi(argv[3]);
257    int startIndex = atoi(argv[4]);
258    int oxdim = atoi(argv[4]);
259    int oydim = atoi(argv[5]);
260    int xmin = atoi(argv[6]);
261    int xmax = atoi(argv[7]);
262    int ymin = atoi(argv[8]);
263    int ymax = atoi(argv[9]);
264    int zmin = atoi(argv[10]);
265    int zmax = atoi(argv[11]);
266    int downsampleRate = atoi(argv[12]);
267
268    short *volStack = new short[oxdim*oydim*downsampleRate];
269
270    xdim = (xmax-xmin)/downsampleRate;
271    ydim = (ymax-ymin)/downsampleRate;
272    zdim = (zmax-zmin)/downsampleRate;
273
274    if (frontRLEGrid != NULL) {
275        delete frontRLEGrid;
276    }
277
278    if (backRLEGrid != NULL) {
279        delete backRLEGrid;
280    }
281
282
283    frontRLEGrid = new OccGridRLE(xdim, ydim, zdim, CHUNK_SIZE);
284
285    ushort *usScanline = new ushort[xdim];
286    OccElement *occScanline = new OccElement[xdim];
287
288
289    for (int zz = 0; zz < zdim; zz++) {
290
291       // Read slices
292       for (int oz = 0; oz < downsampleRate; oz++) {
293          sprintf(filename, "%d", zz*downsampleRate+zmin+oz);
294          strcpy(other, filename);
295          int numDigits = strlen(filename);
296          for (int i=0; i < sigDigits-numDigits; i++) {
297             sprintf(filename, "0%s", other);
298             strcpy(other, filename);
299          }
300          sprintf(filename, "%s%s.%s", argv[1], other, argv[2]);
301         
302          FILE *fp = fopen(filename, "rb");
303          fread(volStack+oxdim*oydim*oz, sizeof(ushort), oxdim*oydim, fp);
304          fclose(fp);
305       }
306
307       printf("\rSlice %d of %d...", zz, zdim);
308       fflush(stdout);
309       for (int yy = 0; yy < ydim; yy++) {
310          for (int xx = 0; xx < xdim; xx++) {
311             uint total = 0;
312             for (int oz = 0; oz < downsampleRate; oz++) {
313                for (int oy = yy*downsampleRate+ymin; 
314                     oy < (yy+1)*downsampleRate+ymin; oy++) {
315                   for (int ox = xx*downsampleRate+xmin; 
316                        ox < (xx+1)*downsampleRate+xmin; ox++) {
317                      int value = volStack[oz*oxdim*oydim+oy*oxdim+ox];
318                      value -= SHRT_MIN;
319                      total += uint(value);
320                   }
321                }
322             }
323             occScanline[xx].value = ushort(total/(downsampleRate*downsampleRate*downsampleRate));
324             occScanline[xx].totalWeight = 1;
325          }
326          frontRLEGrid->putScanline(occScanline, yy, zz);
327       }
328    }
329
330    printf("\n");
331
332    delete volStack;
333    delete usScanline;
334    delete occScanline;
335
336    backRLEGrid = new OccGridRLE(frontRLEGrid->xdim, frontRLEGrid->ydim,
337                               frontRLEGrid->zdim, CHUNK_SIZE);
338    backRLEGrid->resolution = frontRLEGrid->resolution;
339    backRLEGrid->origin[0] = frontRLEGrid->origin[0];
340    backRLEGrid->origin[1] = frontRLEGrid->origin[1];
341    backRLEGrid->origin[2] = frontRLEGrid->origin[2];
342
343    if (theDepthMap != NULL) {
344        delete theDepthMap;
345    }
346
347    maxDim = MAX(frontRLEGrid->xdim, MAX(frontRLEGrid->ydim, 
348                                         frontRLEGrid->zdim));
349    theDepthMap = new DepthMap(int(maxDim*2.2), 
350                               int(maxDim*2.2), 
351                               FALSE, DEPTH_TREE_GRANULARITY);
352
353    return TCL_OK;
354}
355
356
357int
358Vrip_WriteFlatGridCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
359{
360    int xdim, ydim, zdim, xx, yy, zz;
361    OccElement *occScanline;
362
363    /*
364    frontRLEGrid = new OccGridRLE(11,11,11,CHUNK_SIZE);
365
366    xdim = frontRLEGrid->xdim;
367    ydim = frontRLEGrid->ydim;
368    zdim = frontRLEGrid->zdim;
369
370    occScanline = new OccElement[xdim];
371
372    for (zz = 0; zz < zdim; zz++) {
373       for (yy = 0; yy < ydim; yy++) {
374          for (xx = 0; xx < xdim; xx++) {
375             if (((xx-xdim/2)*(xx-xdim/2)+(yy-ydim/2)*(yy-ydim/2)+(zz-zdim/2)*(zz-zdim/2)) > (xdim/4 * xdim/4)) {
376                occScanline[xx].value = 0;
377                occScanline[xx].totalWeight = 1;
378             } else {
379                occScanline[xx].value = 128<<8;
380                occScanline[xx].totalWeight = 1;
381             }
382          }
383          frontRLEGrid->putScanline(occScanline, yy, zz);
384       }
385    }
386    delete occScanline;
387    */
388
389    if (argc != 2) {
390        interp->result = "Usage: vrip_writeflatgrid <filename>";
391        return TCL_ERROR;
392    }
393
394    xdim = frontRLEGrid->xdim;
395    ydim = frontRLEGrid->ydim;
396    zdim = frontRLEGrid->zdim;
397
398    FILE *fp = fopen(argv[1], "wb");
399    fwrite(&xdim, sizeof(int), 1, fp);
400    fwrite(&ydim, sizeof(int), 1, fp);
401    fwrite(&zdim, sizeof(int), 1, fp);
402
403    uchar *ucScanline = new uchar[xdim];
404
405    for (zz = 0; zz < zdim; zz++) {
406       for (yy = 0; yy < ydim; yy++) {
407          occScanline = frontRLEGrid->getScanline(yy, zz);
408          for (xx = 0; xx < xdim; xx++) {
409             ucScanline[xx] = uchar(occScanline[xx].value >> 8);
410          }
411          fwrite(ucScanline, sizeof(uchar), xdim, fp);
412       }
413    }
414
415    delete ucScanline;
416
417    return TCL_OK;
418}
419
Note: See TracBrowser for help on using the repository browser.