| 1 |
/* |
| 2 |
* *************************************************************************** |
| 3 |
* GAMER = < Geometry-preserving Adaptive MeshER > |
| 4 |
* Copyright (C) 2007-2010 -- Michael Holst and Zeyun Yu |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* *************************************************************************** |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* *************************************************************************** |
| 24 |
* File: ReadLattice.C |
| 25 |
* |
| 26 |
* Author: Johan Hake ([email protected]) |
| 27 |
* |
| 28 |
* Purpose: Provide functionality to read in Lattice data |
| 29 |
* *************************************************************************** |
| 30 |
*/ |
| 31 |
|
| 32 |
// FIXME: This is not included by maloc? |
| 33 |
#ifdef _WIN32 |
| 34 |
#include <io.h> |
| 35 |
#else |
| 36 |
#include <unistd.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <gamer/biom.h> |
| 40 |
|
| 41 |
// Read a short integer, swapping the bytes |
| 42 |
int read_short_int(FILE *fptr, short int* n) |
| 43 |
{ |
| 44 |
unsigned char *cptr, tmp; |
| 45 |
|
| 46 |
if (fread(n, sizeof(short int), 1, fptr) != 1) |
| 47 |
return(false); |
| 48 |
cptr = (unsigned char*)n; |
| 49 |
tmp = cptr[0]; |
| 50 |
cptr[0] = cptr[1]; |
| 51 |
cptr[1] =tmp; |
| 52 |
|
| 53 |
return(true); |
| 54 |
} |
| 55 |
|
| 56 |
// Read an unsigned integer from a char buffer, swapping the bytes |
| 57 |
unsigned int get_uint(unsigned char*& cptr) |
| 58 |
{ |
| 59 |
unsigned int* n; |
| 60 |
char tmp; |
| 61 |
|
| 62 |
// Get the number |
| 63 |
n = (unsigned int*) cptr; |
| 64 |
|
| 65 |
// Swap bytes |
| 66 |
tmp = cptr[0]; |
| 67 |
cptr[0] = cptr[3]; |
| 68 |
cptr[3] = tmp; |
| 69 |
tmp = cptr[1]; |
| 70 |
cptr[1] = cptr[2]; |
| 71 |
cptr[2] = tmp; |
| 72 |
|
| 73 |
// Increase the char pointer |
| 74 |
cptr += 4; |
| 75 |
|
| 76 |
return *n; |
| 77 |
} |
| 78 |
|
| 79 |
// Read an integer from a char buffer, swapping the bytes |
| 80 |
int get_int(unsigned char*& cptr) |
| 81 |
{ |
| 82 |
int* n; |
| 83 |
char tmp; |
| 84 |
|
| 85 |
// Get the number |
| 86 |
n = (int*) cptr; |
| 87 |
|
| 88 |
// Swap bytes |
| 89 |
tmp = cptr[0]; |
| 90 |
cptr[0] = cptr[3]; |
| 91 |
cptr[3] = tmp; |
| 92 |
tmp = cptr[1]; |
| 93 |
cptr[1] = cptr[2]; |
| 94 |
cptr[2] = tmp; |
| 95 |
|
| 96 |
// Increase the char pointer |
| 97 |
cptr += 4; |
| 98 |
|
| 99 |
return *n; |
| 100 |
} |
| 101 |
|
| 102 |
// Read a floating point number from a char buffer, swapping the bytes |
| 103 |
// Assume IEEE format |
| 104 |
float get_float(unsigned char*& cptr) |
| 105 |
{ |
| 106 |
float* n; |
| 107 |
char tmp; |
| 108 |
|
| 109 |
// Get the number |
| 110 |
n = (float*) cptr; |
| 111 |
|
| 112 |
// Swap bytes |
| 113 |
tmp = cptr[0]; |
| 114 |
cptr[0] = cptr[3]; |
| 115 |
cptr[3] = tmp; |
| 116 |
tmp = cptr[1]; |
| 117 |
cptr[1] = cptr[2]; |
| 118 |
cptr[2] = tmp; |
| 119 |
|
| 120 |
// Increase the char pointer |
| 121 |
cptr += 4; |
| 122 |
|
| 123 |
return *n; |
| 124 |
} |
| 125 |
|
| 126 |
// Read a double from a char buffer, swapping the bytes |
| 127 |
// Assume IEEE |
| 128 |
double get_double(unsigned char*& cptr) |
| 129 |
{ |
| 130 |
double* n; |
| 131 |
unsigned char tmp; |
| 132 |
|
| 133 |
// Get the number |
| 134 |
n = (double*) cptr; |
| 135 |
|
| 136 |
tmp = cptr[0]; |
| 137 |
cptr[0] = cptr[7]; |
| 138 |
cptr[7] = tmp; |
| 139 |
tmp = cptr[1]; |
| 140 |
cptr[1] = cptr[6]; |
| 141 |
cptr[6] = tmp; |
| 142 |
tmp = cptr[2]; |
| 143 |
cptr[2] = cptr[5]; |
| 144 |
cptr[5] =tmp; |
| 145 |
tmp = cptr[3]; |
| 146 |
cptr[3] = cptr[4]; |
| 147 |
cptr[4] = tmp; |
| 148 |
|
| 149 |
// Increase the char pointer |
| 150 |
cptr += 8; |
| 151 |
|
| 152 |
return *n; |
| 153 |
} |
| 154 |
|
| 155 |
void load_lattice_file(char* input_name, unsigned int& xdim, |
| 156 |
unsigned int& ydim, unsigned int& zdim, float*& data, |
| 157 |
double*& dim_mat, bool padd, float padd_value) |
| 158 |
{ |
| 159 |
FILE* fptr; |
| 160 |
unsigned int file_size, i, j, k, read_version, write_version, smsize, dim; |
| 161 |
unsigned int data_type, compression_type, elem_size, dsize; |
| 162 |
size_t read_size, uint_size, double_size; |
| 163 |
unsigned char* buffer; |
| 164 |
unsigned char* cptr; |
| 165 |
char* header; |
| 166 |
|
| 167 |
// Open file |
| 168 |
if ((fptr=fopen(input_name, "rb"))==NULL) {fputs("File error\n", stderr); exit(ENOENT);} |
| 169 |
|
| 170 |
// Obtain file size: |
| 171 |
fseek (fptr, 0, SEEK_END); |
| 172 |
file_size = ftell(fptr); |
| 173 |
rewind (fptr); |
| 174 |
|
| 175 |
// Allocate memory to contain the whole file: |
| 176 |
buffer = (unsigned char*) malloc (sizeof(unsigned char)*file_size); |
| 177 |
if (buffer == NULL) {fputs("Memory error\n", stderr); exit(ENOMEM);} |
| 178 |
|
| 179 |
// Copy the file into the buffer and close file |
| 180 |
read_size = fread(buffer, 1, file_size, fptr); |
| 181 |
if (read_size != file_size) {fputs("Reading error\n", stderr); exit(EIO);} |
| 182 |
fclose(fptr); |
| 183 |
|
| 184 |
printf("File size: %d\n", file_size); |
| 185 |
|
| 186 |
header = (char*)buffer; |
| 187 |
|
| 188 |
// Check header |
| 189 |
if (strncmp(header, "LATTICE*IBT*KA", 14)){ |
| 190 |
fputs("Not a Lattice file\n", stderr); |
| 191 |
exit(EIO); |
| 192 |
}; |
| 193 |
|
| 194 |
// Get the char pointer to the first data |
| 195 |
cptr = &buffer[14]; |
| 196 |
|
| 197 |
// Read 'read_version' |
| 198 |
read_version = get_uint(cptr); |
| 199 |
printf("read version: %d\n", read_version); |
| 200 |
|
| 201 |
// Read 'write_version' |
| 202 |
write_version = get_uint(cptr); |
| 203 |
printf("write version: %d\n", write_version); |
| 204 |
|
| 205 |
// Read 'smsize' |
| 206 |
smsize = get_uint(cptr); |
| 207 |
printf("smsize: %d\n", smsize); |
| 208 |
|
| 209 |
// Read 'xdim' |
| 210 |
xdim = get_uint(cptr); |
| 211 |
printf("xdim: %d\n", xdim); |
| 212 |
|
| 213 |
// Read 'ydim' |
| 214 |
ydim = get_uint(cptr); |
| 215 |
printf("ydim: %d\n", ydim); |
| 216 |
|
| 217 |
// Read 'zdim' |
| 218 |
zdim = get_uint(cptr); |
| 219 |
printf("zdim: %d\n", zdim); |
| 220 |
|
| 221 |
// Read 'dim' |
| 222 |
dim = get_uint(cptr); |
| 223 |
printf("dim: %d\n", dim); |
| 224 |
|
| 225 |
// Read 'dim mat' |
| 226 |
dim_mat = (double*) malloc (sizeof(double)*4*dim); |
| 227 |
printf("Dimension matrix"); |
| 228 |
for (i = 0; i < dim; i++){ |
| 229 |
printf("\n"); |
| 230 |
for (j = 0; j < 4; j++){ |
| 231 |
dim_mat[i*4 + j] = get_double(cptr); |
| 232 |
printf("%f, ", dim_mat[i*4 + j]); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
printf("\n"); |
| 237 |
// Read 'data_type' |
| 238 |
data_type = get_uint(cptr); |
| 239 |
printf("data_type: %d\n", data_type); |
| 240 |
|
| 241 |
// Read 'compression_type' |
| 242 |
compression_type = get_uint(cptr); |
| 243 |
printf("compression_type: %d\n", compression_type); |
| 244 |
|
| 245 |
if (compression_type){fputs("Compression type not supported\n", stderr); exit(EIO);} |
| 246 |
|
| 247 |
// Read 'elem_size' |
| 248 |
elem_size = get_uint(cptr); |
| 249 |
printf("elem_size: %d\n", elem_size); |
| 250 |
|
| 251 |
// Skip alignment |
| 252 |
cptr += 88; |
| 253 |
|
| 254 |
// Read 'dsize' |
| 255 |
dsize = get_uint(cptr); |
| 256 |
printf("dsize: %d\n", dsize); |
| 257 |
|
| 258 |
// If padd create a padding around the data of size 1 |
| 259 |
if (padd) |
| 260 |
{ |
| 261 |
xdim+=2; |
| 262 |
ydim+=2; |
| 263 |
zdim+=2; |
| 264 |
} |
| 265 |
|
| 266 |
// Allocate memory |
| 267 |
data = (float*) malloc(sizeof(float)*xdim*ydim*zdim); |
| 268 |
|
| 269 |
// Read in data dependent on the data type |
| 270 |
switch (data_type) { |
| 271 |
case 0: |
| 272 |
|
| 273 |
if (padd) |
| 274 |
{ |
| 275 |
// Initialize all data points with padd value |
| 276 |
for (i = 0; i < xdim*ydim*zdim; i++) |
| 277 |
data[i] = padd_value; |
| 278 |
|
| 279 |
for (k = 1; k < zdim-1; k++) |
| 280 |
for (j = 1; j < ydim-1; j++) |
| 281 |
for (i = 1; i < xdim-1; i++) |
| 282 |
data[IndexVect(i,j,k)] = (*cptr++ == 255) ? 0.0 : 1.0; |
| 283 |
} |
| 284 |
else |
| 285 |
for (k = 0; k < zdim; k++) |
| 286 |
for (j = 0; j < ydim; j++) |
| 287 |
for (i = 0; i < xdim; i++) |
| 288 |
data[IndexVect(i,j,k)] = (*cptr++ == 255) ? 0.0 : 1.0; |
| 289 |
|
| 290 |
break; |
| 291 |
case 4: |
| 292 |
|
| 293 |
if (padd) |
| 294 |
{ |
| 295 |
// Initialize all data points with padd value |
| 296 |
for (i = 0; i < xdim*ydim*zdim; i++) |
| 297 |
data[i] = padd_value; |
| 298 |
|
| 299 |
// Fill inside of cell with data |
| 300 |
for (k = 1; k < zdim-1; k++) |
| 301 |
for (j = 1; j < ydim-1; j++) |
| 302 |
for (i = 1; i < xdim-1; i++) |
| 303 |
data[IndexVect(i,j,k)] = get_float(cptr); |
| 304 |
} |
| 305 |
else |
| 306 |
for (k = 0; k < zdim; k++) |
| 307 |
for (j = 0; j < ydim; j++) |
| 308 |
for (i = 0; i < xdim; i++) |
| 309 |
data[IndexVect(i,j,k)] = get_float(cptr); |
| 310 |
break; |
| 311 |
default: |
| 312 |
fputs("Data type not supported\n", stderr); exit(EIO); |
| 313 |
} |
| 314 |
|
| 315 |
// Release memory |
| 316 |
free(buffer); |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
/* |
| 321 |
* *************************************************************************** |
| 322 |
* Routine: SurfaceMesh_readLattice |
| 323 |
* |
| 324 |
* Author: Johan Hake ([email protected]) |
| 325 |
* |
| 326 |
* Purpose: Read segmentation stored in a Lattice file and generate a |
| 327 |
* surface mesh |
| 328 |
* *************************************************************************** |
| 329 |
*/ |
| 330 |
SurfaceMesh* SurfaceMesh_readLattice(char* segmentation_filename, |
| 331 |
float isovalue, |
| 332 |
bool padd) |
| 333 |
{ |
| 334 |
return SurfaceMesh_readLattice(segmentation_filename, NULL, |
| 335 |
isovalue, padd); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/* |
| 340 |
* *************************************************************************** |
| 341 |
* Routine: SurfaceMesh_readLattice |
| 342 |
* |
| 343 |
* Author: Johan Hake ([email protected]) |
| 344 |
* |
| 345 |
* Purpose: Read segmentation stored in a Lattice file and generate a |
| 346 |
* surface mesh, potential using intensity values |
| 347 |
* *************************************************************************** |
| 348 |
*/ |
| 349 |
SurfaceMesh* SurfaceMesh_readLattice(char* segmentation_filename, |
| 350 |
char* intensity_filename, |
| 351 |
float isovalue, |
| 352 |
bool padd) |
| 353 |
{ |
| 354 |
SPNT *holelist; |
| 355 |
unsigned int xdim0, ydim0, zdim0, xdim1, ydim1, zdim1; |
| 356 |
float* segmentation_data; |
| 357 |
float* intensity_data; |
| 358 |
double* mat_dim = NULL; |
| 359 |
double* mat_dim_intensity = NULL; |
| 360 |
unsigned int basename_index; |
| 361 |
char* basename_end_pointer; |
| 362 |
SurfaceMesh* surfmesh; |
| 363 |
|
| 364 |
printf("Load Lattice\n"); |
| 365 |
|
| 366 |
// Check if input is a .lat file |
| 367 |
basename_end_pointer = strstr(segmentation_filename, ".lat"); |
| 368 |
if (basename_end_pointer == NULL){ |
| 369 |
printf("No \'.lat\' file\n"); |
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
// Check if file exists and are readable |
| 374 |
#ifdef _WIN32 |
| 375 |
// checks whether file has mode read-only (4) or read/write (6) |
| 376 |
if (_access(segmentation_filename, 4) && _access(segmentation_filename, 6)){ |
| 377 |
#else |
| 378 |
if (access(segmentation_filename, R_OK)){ |
| 379 |
#endif |
| 380 |
printf("Not readable\n"); |
| 381 |
return NULL; |
| 382 |
} |
| 383 |
|
| 384 |
// Load the segmented data |
| 385 |
load_lattice_file(segmentation_filename, xdim0, ydim0, zdim0, |
| 386 |
segmentation_data, mat_dim, padd, 0.0); |
| 387 |
|
| 388 |
// Check if we intensity_filename is given |
| 389 |
if (intensity_filename != NULL) |
| 390 |
{ |
| 391 |
|
| 392 |
printf("Intensity file given\n"); |
| 393 |
|
| 394 |
// Check if input is a .flat file |
| 395 |
basename_end_pointer = strstr(intensity_filename, ".flat"); |
| 396 |
if (basename_end_pointer == NULL){ |
| 397 |
printf("No \'.flat\' file\n"); |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
// Check if file exists and are readable |
| 402 |
#ifdef _WIN32 |
| 403 |
// checks whether file has mode read-only (4) or read/write (6) |
| 404 |
if (_access(intensity_filename, 4) && _access(intensity_filename, 6)){ |
| 405 |
#else |
| 406 |
if (access(intensity_filename, R_OK)){ |
| 407 |
#endif |
| 408 |
printf("Not readable\n"); |
| 409 |
return NULL; |
| 410 |
} |
| 411 |
|
| 412 |
// Load the intensity data |
| 413 |
load_lattice_file(intensity_filename, xdim1, ydim1, zdim1, intensity_data, |
| 414 |
mat_dim_intensity, padd, -1.); |
| 415 |
|
| 416 |
// Check if dimensions fit |
| 417 |
if (xdim0 != xdim1 && ydim0 != ydim1 && zdim0 != zdim1) |
| 418 |
{ |
| 419 |
printf("Dimension does not fit\n"); |
| 420 |
return NULL; |
| 421 |
} |
| 422 |
|
| 423 |
// Call the marhching cube rutine with intensity data |
| 424 |
surfmesh = SurfaceMesh_marchingCube(xdim0, ydim0, zdim0, segmentation_data, 0.5, |
| 425 |
intensity_data, isovalue, &holelist); |
| 426 |
|
| 427 |
// Centralize and scale data |
| 428 |
SurfaceMesh_centeralize(surfmesh); |
| 429 |
SurfaceMesh_scale(surfmesh, mat_dim[0], mat_dim[5], mat_dim[10]); |
| 430 |
|
| 431 |
// Free memory |
| 432 |
free(segmentation_data); |
| 433 |
free(intensity_data); |
| 434 |
free(mat_dim); |
| 435 |
free(mat_dim_intensity); |
| 436 |
|
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
|
| 441 |
// Call the marhching cube rutine without intensity data |
| 442 |
surfmesh = SurfaceMesh_marchingCube(xdim0, ydim0, zdim0, segmentation_data, |
| 443 |
isovalue, &holelist); |
| 444 |
|
| 445 |
// Centralize and scale data |
| 446 |
SurfaceMesh_centeralize(surfmesh); |
| 447 |
SurfaceMesh_scale(surfmesh, mat_dim[0], mat_dim[5], mat_dim[10]); |
| 448 |
|
| 449 |
// Free memory |
| 450 |
free(segmentation_data); |
| 451 |
free(mat_dim); |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
// Return surface mesh |
| 456 |
return surfmesh; |
| 457 |
} |
| 458 |
|