1 /*
2 * classifyvolume.c
3 *
4 * Create a classified volume from the brainsmall data set.
5 *
6 * Copyright (c) 1994 The Board of Trustees of The Leland Stanford
7 * Junior University. All rights reserved.
8 *
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation for any purpose is hereby granted without fee, provided
11 * that the above copyright notice and this permission notice appear in
12 * all copies of this software and that you do not sell the software.
13 * Commercial licensing is available by contacting the author.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * Author:
20 * Phil Lacroute
21 * Computer Systems Laboratory
22 * Electrical Engineering Dept.
23 * Stanford University
24 */
25
26 /*
27 * $Date: 1994/12/31 19:53:03 $
28 * $Revision: 1.5 $
29 */
30
31 #include "volume.h"
32
main(argc,argv)33 main(argc, argv)
34 int argc;
35 char **argv;
36 {
37 vpContext *vpc; /* rendering context */
38 int volume_fd; /* file descriptor for volume (input) */
39 int octree_fd; /* file descriptor for octree (input) */
40 int density_fd; /* file descriptor for raw volume data (input) */
41 int output_fd; /* file descriptor for classified volume (output) */
42 int use_rawdata; /* if true, use raw data instead of volume */
43 int use_octree; /* if true, use octree with the volume */
44 unsigned char *density; /* buffer for density data */
45 unsigned density_size;/* size of density data */
46 float density_ramp[DENSITY_MAX+1]; /* opacity as a function of density */
47 float gradient_ramp[GRADIENT_MAX+1];/* opacity as a function
48 of gradient magnitude */
49 void *malloc();
50
51 /* check command-line arguments */
52 use_octree = 0;
53 use_rawdata = 0;
54 if (argc > 1) {
55 if (!strcmp(argv[1], "-octree"))
56 use_octree = 1;
57 else if (!strcmp(argv[1], "-rawdata"))
58 use_rawdata = 1;
59 else {
60 fprintf(stderr, "Usage: %s [-octree | -rawdata]\n", argv[0]);
61 exit(1);
62 }
63 }
64
65 /* create a context */
66 vpc = vpCreateContext();
67
68 /* load input data: either raw data, or an unclassified volume with no
69 octree, or an unclassified volume with an octree */
70 if (use_rawdata) {
71 /* describe the layout of the volume */
72 vpSetVolumeSize(vpc, BRAIN_XLEN, BRAIN_YLEN, BRAIN_ZLEN);
73 vpSetVoxelSize(vpc, BYTES_PER_VOXEL, VOXEL_FIELDS,
74 SHADE_FIELDS, CLSFY_FIELDS);
75 vpSetVoxelField(vpc, NORMAL_FIELD, NORMAL_SIZE, NORMAL_OFFSET,
76 NORMAL_MAX);
77 vpSetVoxelField(vpc, DENSITY_FIELD, DENSITY_SIZE, DENSITY_OFFSET,
78 DENSITY_MAX);
79 vpSetVoxelField(vpc, GRADIENT_FIELD, GRADIENT_SIZE, GRADIENT_OFFSET,
80 GRADIENT_MAX);
81
82 /* allocate space for the raw data */
83 density_size = BRAIN_XLEN * BRAIN_YLEN * BRAIN_ZLEN;
84 density = malloc(density_size);
85 if (density == NULL) {
86 fprintf(stderr, "out of memory\n");
87 exit(1);
88 }
89
90 /* load the raw data */
91 if ((density_fd = open(BRAIN_FILE, 0)) < 0) {
92 perror("open");
93 fprintf(stderr, "could not open %s\n", BRAIN_FILE);
94 exit(1);
95 }
96 if (lseek(density_fd, BRAIN_HEADER, 0) < 0) {
97 perror("seek");
98 fprintf(stderr, "could not read data from %s\n", BRAIN_FILE);
99 exit(1);
100 }
101 if (read(density_fd, density, density_size) != density_size) {
102 perror("read");
103 fprintf(stderr, "could not read data from %s\n", BRAIN_FILE);
104 exit(1);
105 }
106 close(density_fd);
107 } else {
108 /* load the unclassified volume data */
109 if ((volume_fd = open(VOLUME_FILE, 0)) < 0) {
110 perror("open");
111 fprintf(stderr, "could not open %s\n", VOLUME_FILE);
112 exit(1);
113 }
114 if (vpLoadRawVolume(vpc, volume_fd) != VP_OK) {
115 fprintf(stderr, "VolPack error: %s\n",
116 vpGetErrorString(vpGetError(vpc)));
117 fprintf(stderr, "could not load the volume from file %s\n",
118 VOLUME_FILE);
119 exit(1);
120 }
121 close(volume_fd);
122 }
123
124 /* set the classification function */
125 vpRamp(density_ramp, sizeof(float), DENSITY_RAMP_POINTS, DensityRampX,
126 DensityRampY);
127 vpSetClassifierTable(vpc, DENSITY_PARAM, DENSITY_FIELD, density_ramp,
128 sizeof(density_ramp));
129 vpRamp(gradient_ramp, sizeof(float), GRADIENT_RAMP_POINTS, GradientRampX,
130 GradientRampY);
131 vpSetClassifierTable(vpc, GRADIENT_PARAM, GRADIENT_FIELD,
132 gradient_ramp, sizeof(gradient_ramp));
133 vpSetd(vpc, VP_MIN_VOXEL_OPACITY, 0.05);
134
135 /* load the octree */
136 if (use_octree) {
137 /* load the octree */
138 if ((octree_fd = open(OCTREE_FILE, 0)) < 0) {
139 perror("open");
140 fprintf(stderr, "could not open %s\n", OCTREE_FILE);
141 exit(1);
142 }
143 if (vpLoadMinMaxOctree(vpc, octree_fd) != VP_OK) {
144 fprintf(stderr, "VolPack error: %s\n",
145 vpGetErrorString(vpGetError(vpc)));
146 fprintf(stderr, "could not load the octree from file %s\n",
147 OCTREE_FILE);
148 exit(1);
149 }
150 close(octree_fd);
151 }
152
153 /* classify */
154 if (use_rawdata) {
155 if (vpClassifyScalars(vpc, density, density_size, DENSITY_FIELD,
156 GRADIENT_FIELD, NORMAL_FIELD) != VP_OK) {
157 fprintf(stderr, "VolPack error: %s\n",
158 vpGetErrorString(vpGetError(vpc)));
159 exit(1);
160 }
161 } else {
162 if (vpClassifyVolume(vpc) != VP_OK) {
163 fprintf(stderr, "VolPack error: %s\n",
164 vpGetErrorString(vpGetError(vpc)));
165 exit(1);
166 }
167 }
168
169 /* store the classified volume */
170 if ((output_fd = creat(CLVOLUME_FILE, 0644)) < 0) {
171 perror("open");
172 fprintf(stderr, "could not open %s\n", CLVOLUME_FILE);
173 exit(1);
174 }
175 if (vpStoreClassifiedVolume(vpc, output_fd) != VP_OK) {
176 fprintf(stderr, "VolPack error: %s\n",
177 vpGetErrorString(vpGetError(vpc)));
178 exit(1);
179 }
180 close(output_fd);
181
182 return(0);
183 }
184