File: classifyvolume.c

package info (click to toggle)
volpack 1.0b3-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,948 kB
  • sloc: ansic: 12,201; sh: 9,078; makefile: 91; csh: 76
file content (185 lines) | stat: -rw-r--r-- 5,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * classifyvolume.c
 *
 * Create a classified volume from the brainsmall data set.
 *
 * Copyright (c) 1994 The Board of Trustees of The Leland Stanford
 * Junior University.  All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice and this permission notice appear in
 * all copies of this software and that you do not sell the software.
 * Commercial licensing is available by contacting the author.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Author:
 *    Phil Lacroute
 *    Computer Systems Laboratory
 *    Electrical Engineering Dept.
 *    Stanford University
 */

/*
 * $Date: 1994/12/31 19:53:03 $
 * $Revision: 1.5 $
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "volume.h"

int
main(int argc, char **argv)
{
    vpContext *vpc;	/* rendering context */
    int volume_fd;	/* file descriptor for volume (input) */
    int octree_fd;	/* file descriptor for octree (input) */
    int density_fd;	/* file descriptor for raw volume data (input) */
    int output_fd;	/* file descriptor for classified volume (output) */
    int use_rawdata;	/* if true, use raw data instead of volume */
    int use_octree;	/* if true, use octree with the volume */
    unsigned char *density;	/* buffer for density data */
    unsigned density_size;/* size of density data */
    float density_ramp[DENSITY_MAX+1];	/* opacity as a function of density */
    float gradient_ramp[GRADIENT_MAX+1];/* opacity as a function 
					   of gradient magnitude */

    /* check command-line arguments */
    use_octree = 0;
    use_rawdata = 0;
    if (argc > 1) {
	if (!strcmp(argv[1], "-octree"))
	    use_octree = 1;
	else if (!strcmp(argv[1], "-rawdata"))
	    use_rawdata = 1;
	else {
	    fprintf(stderr, "Usage: %s [-octree | -rawdata]\n", argv[0]);
	    exit(1);
	}
    }

    /* create a context */
    vpc = vpCreateContext();

    /* load input data: either raw data, or an unclassified volume with no
       octree, or an unclassified volume with an octree */
    if (use_rawdata) {
	/* describe the layout of the volume */
	vpSetVolumeSize(vpc, BRAIN_XLEN, BRAIN_YLEN, BRAIN_ZLEN);
	vpSetVoxelSize(vpc, BYTES_PER_VOXEL, VOXEL_FIELDS,
		       SHADE_FIELDS, CLSFY_FIELDS);
	vpSetVoxelField(vpc, NORMAL_FIELD, NORMAL_SIZE, NORMAL_OFFSET,
			NORMAL_MAX);
	vpSetVoxelField(vpc, DENSITY_FIELD, DENSITY_SIZE, DENSITY_OFFSET,
			DENSITY_MAX);
	vpSetVoxelField(vpc, GRADIENT_FIELD, GRADIENT_SIZE, GRADIENT_OFFSET,
			GRADIENT_MAX);

	/* allocate space for the raw data */
	density_size = BRAIN_XLEN * BRAIN_YLEN * BRAIN_ZLEN;
	density = malloc(density_size);
	if (density == NULL) {
	    fprintf(stderr, "out of memory\n");
	    exit(1);
	}

	/* load the raw data */
	if ((density_fd = open(BRAIN_FILE, 0)) < 0) {
	    perror("open");
	    fprintf(stderr, "could not open %s\n", BRAIN_FILE);
	    exit(1);
	}
	if (lseek(density_fd, BRAIN_HEADER, 0) < 0) {
	    perror("seek");
	    fprintf(stderr, "could not read data from %s\n", BRAIN_FILE);
	    exit(1);
	}
	if (read(density_fd, density, density_size) != density_size) {
	    perror("read");
	    fprintf(stderr, "could not read data from %s\n", BRAIN_FILE);
	    exit(1);
	}
	close(density_fd);
    } else {
	/* load the unclassified volume data */
	if ((volume_fd = open(VOLUME_FILE, 0)) < 0) {
	    perror("open");
	    fprintf(stderr, "could not open %s\n", VOLUME_FILE);
	    exit(1);
	}
	if (vpLoadRawVolume(vpc, volume_fd) != VP_OK) {
	    fprintf(stderr, "VolPack error: %s\n",
		    vpGetErrorString(vpGetError(vpc)));
	    fprintf(stderr, "could not load the volume from file %s\n",
		    VOLUME_FILE);
	    exit(1);
	}
	close(volume_fd);
    }

    /* set the classification function */
    vpRamp(density_ramp, sizeof(float), DENSITY_RAMP_POINTS, DensityRampX,
	   DensityRampY);
    vpSetClassifierTable(vpc, DENSITY_PARAM, DENSITY_FIELD, density_ramp,
			 sizeof(density_ramp));
    vpRamp(gradient_ramp, sizeof(float), GRADIENT_RAMP_POINTS, GradientRampX,
	   GradientRampY);
    vpSetClassifierTable(vpc, GRADIENT_PARAM, GRADIENT_FIELD,
			 gradient_ramp, sizeof(gradient_ramp));
    vpSetd(vpc, VP_MIN_VOXEL_OPACITY, 0.05);

    /* load the octree */
    if (use_octree) {
	/* load the octree */
	if ((octree_fd = open(OCTREE_FILE, 0)) < 0) {
	    perror("open");
	    fprintf(stderr, "could not open %s\n", OCTREE_FILE);
	    exit(1);
	}
	if (vpLoadMinMaxOctree(vpc, octree_fd) != VP_OK) {
	    fprintf(stderr, "VolPack error: %s\n",
		    vpGetErrorString(vpGetError(vpc)));
	    fprintf(stderr, "could not load the octree from file %s\n",
		    OCTREE_FILE);
	    exit(1);
	}
	close(octree_fd);
    }

    /* classify */
    if (use_rawdata) {
	if (vpClassifyScalars(vpc, density, density_size, DENSITY_FIELD,
			      GRADIENT_FIELD, NORMAL_FIELD) != VP_OK) {
	    fprintf(stderr, "VolPack error: %s\n",
		    vpGetErrorString(vpGetError(vpc)));
	    exit(1);
	}
    } else {
	if (vpClassifyVolume(vpc) != VP_OK) {
	    fprintf(stderr, "VolPack error: %s\n",
		    vpGetErrorString(vpGetError(vpc)));
	    exit(1);
	}
    }

    /* store the classified volume */
    if ((output_fd = creat(CLVOLUME_FILE, 0644)) < 0) {
	perror("open");
	fprintf(stderr, "could not open %s\n", CLVOLUME_FILE);
	exit(1);
    }
    if (vpStoreClassifiedVolume(vpc, output_fd) != VP_OK) {
	fprintf(stderr, "VolPack error: %s\n",
		vpGetErrorString(vpGetError(vpc)));
	exit(1);
    }
    close(output_fd);

    return(0);
}