|
9 | 9 | #include <file_utils/rawloader.h> |
10 | 10 |
|
11 | 11 | #include <fstream> |
| 12 | +#include <array> |
12 | 13 |
|
13 | 14 | #include <volvis_utils/transferfunction1d.h> |
14 | 15 |
|
@@ -44,6 +45,15 @@ namespace vis |
44 | 45 | else if (extension.compare("syn") == 0) { |
45 | 46 | ret = readsyn(filepath); |
46 | 47 | } |
| 48 | + else if (extension.compare("nrrd") == 0) { |
| 49 | + ret = readnrrd(filepath); |
| 50 | + } |
| 51 | + else if (extension.compare("nhrd") == 0) { |
| 52 | + ret = readnhrd(filepath); |
| 53 | + } |
| 54 | + else if (extension.compare("dat") == 0) { |
| 55 | + ret = readdat(filepath); |
| 56 | + } |
47 | 57 | printf("DONE\n"); |
48 | 58 |
|
49 | 59 | return ret; |
@@ -360,6 +370,263 @@ namespace vis |
360 | 370 | return sg_ret; |
361 | 371 | } |
362 | 372 |
|
| 373 | + StructuredGridVolume* VolumeReader::readnrrd (std::string filepath) |
| 374 | + { |
| 375 | + StructuredGridVolume* sg_ret = nullptr; |
| 376 | + |
| 377 | + printf("Started -> Read Volume From .nrrd File\n"); |
| 378 | + printf(" - File .nrrd Path: %s\n", filepath.c_str()); |
| 379 | + |
| 380 | + std::string path = filepath; |
| 381 | + std::string name = filepath; |
| 382 | + int t1 = filepath.find_last_of('\\'); |
| 383 | + int t2 = filepath.find_last_of('/'); |
| 384 | + int t = glm::max(t1, t2); |
| 385 | + if (t > -1) { |
| 386 | + path = filepath.substr(0, t); |
| 387 | + name = filepath.substr(t + 1); |
| 388 | + } |
| 389 | + |
| 390 | + std::ifstream iffile(filepath.c_str()); |
| 391 | + if (iffile.is_open()) { |
| 392 | + std::string version; |
| 393 | + std::getline(iffile, version); |
| 394 | + |
| 395 | + std::string type; |
| 396 | + std::string dimension; |
| 397 | + std::string space; |
| 398 | + glm::ivec3 resolution; |
| 399 | + std::array<glm::vec3, 3> space_directions; |
| 400 | + glm::vec3 slicethickness(1, 1, 1); |
| 401 | + std::string kinds; |
| 402 | + std::string endian; |
| 403 | + std::string encoding; |
| 404 | + std::string space_origin; |
| 405 | + std::string data_file; |
| 406 | + |
| 407 | + // read keys |
| 408 | + while (!iffile.eof()) { |
| 409 | + std::string line; |
| 410 | + std::getline(iffile, line); |
| 411 | + |
| 412 | + std::cout << line << std::endl; |
| 413 | + |
| 414 | + if (line.find_first_of('#') == 0) continue; |
| 415 | + |
| 416 | + std::string key = line.substr(0, line.find_first_of(':')); |
| 417 | + std::string content = line.substr(line.find_first_of(":") + 1); |
| 418 | + |
| 419 | + // skip ' ' and '\t' |
| 420 | + int count_skip = 0; |
| 421 | + while (content[count_skip] == ' ' || content[count_skip] == '\t') { |
| 422 | + count_skip += 1; |
| 423 | + } |
| 424 | + content = content.substr(count_skip); |
| 425 | + |
| 426 | + if (key.find("type") == 0) { |
| 427 | + type = content; |
| 428 | + } |
| 429 | + else if (key.find("dimension") == 0) { |
| 430 | + dimension = content; |
| 431 | + } |
| 432 | + else if (key.find("space") == 0) { |
| 433 | + space = content; |
| 434 | + } |
| 435 | + else if (key.find("sizes") == 0) { |
| 436 | + std::string str_resolution = content; |
| 437 | + |
| 438 | + std::vector<int> vec_resolution; |
| 439 | + while (str_resolution.find(' ') != std::string::npos) { |
| 440 | + std::string size_l = str_resolution.substr(0, str_resolution.find(' ')); |
| 441 | + vec_resolution.push_back(std::atoi(size_l.c_str())); |
| 442 | + str_resolution = str_resolution.substr(str_resolution.find(' ') + 1); |
| 443 | + } |
| 444 | + vec_resolution.push_back(std::atoi(str_resolution.c_str())); |
| 445 | + |
| 446 | + resolution = glm::ivec3(vec_resolution[0], vec_resolution[1], vec_resolution[2]); |
| 447 | + } |
| 448 | + else if (key.find("space directions") == 0) { |
| 449 | + space_directions[0] = glm::vec3(1, 0, 0); |
| 450 | + space_directions[1] = glm::vec3(0, 1, 0); |
| 451 | + space_directions[2] = glm::vec3(0, 0, 1); |
| 452 | + } |
| 453 | + else if (key.find("kinds") == 0) { |
| 454 | + kinds = content; |
| 455 | + } |
| 456 | + else if (key.find("endian") == 0) { |
| 457 | + endian = content; |
| 458 | + } |
| 459 | + else if (key.find("encoding") == 0) { |
| 460 | + encoding = content; |
| 461 | + } |
| 462 | + else if (key.find("space origin") == 0) { |
| 463 | + space_origin = content; |
| 464 | + } |
| 465 | + else if (key.find("data file") == 0) { |
| 466 | + data_file = content; |
| 467 | + } |
| 468 | + } |
| 469 | + iffile.close(); |
| 470 | + |
| 471 | + sg_ret = new StructuredGridVolume(name, resolution.x, resolution.y, resolution.z); |
| 472 | + sg_ret->SetScale(slicethickness.x, slicethickness.y, slicethickness.z); |
| 473 | + |
| 474 | + std::string volume_data_array_file = path + "/" + data_file; |
| 475 | + |
| 476 | + int bytes_per_value = 1; |
| 477 | + if ((int)type.find("uint8") > -1) { |
| 478 | + bytes_per_value = sizeof(unsigned char); |
| 479 | + } |
| 480 | + if ((int)type.find("uint16") > -1) { |
| 481 | + bytes_per_value = sizeof(unsigned short); |
| 482 | + } |
| 483 | + |
| 484 | + SetArrayDataFromRawFile(volume_data_array_file, sg_ret, bytes_per_value); |
| 485 | + } |
| 486 | + else { |
| 487 | + printf("Finished -> Error on opening .nrrd file\n"); |
| 488 | + } |
| 489 | + |
| 490 | + return sg_ret; |
| 491 | + } |
| 492 | + |
| 493 | + StructuredGridVolume* VolumeReader::readnhrd (std::string filepath) |
| 494 | + { |
| 495 | + return readnrrd(filepath); |
| 496 | + } |
| 497 | + |
| 498 | + StructuredGridVolume* VolumeReader::readdat (std::string filepath) |
| 499 | + { |
| 500 | + StructuredGridVolume* sg_ret = nullptr; |
| 501 | + |
| 502 | + printf("Started -> Read Volume From .dat File\n"); |
| 503 | + printf(" - File .dat Path: %s\n", filepath.c_str()); |
| 504 | + |
| 505 | + std::ifstream iffile(filepath.c_str()); |
| 506 | + if (iffile.is_open()) { |
| 507 | + std::string path = filepath; |
| 508 | + std::string name = filepath; |
| 509 | + int t1 = filepath.find_last_of('\\'); |
| 510 | + int t2 = filepath.find_last_of('/'); |
| 511 | + int t = glm::max(t1, t2); |
| 512 | + if (t > -1) { |
| 513 | + path = filepath.substr(0, t); |
| 514 | + name = filepath.substr(t+1); |
| 515 | + } |
| 516 | + |
| 517 | + std::string objectfilename; |
| 518 | + std::string taggedfilename; |
| 519 | + glm::ivec3 resolution; |
| 520 | + glm::vec3 slicethickness(1.0,1.0,1.0); |
| 521 | + std::string format; |
| 522 | + int nbrtags; |
| 523 | + std::string objecttype; |
| 524 | + std::string objectmodel; |
| 525 | + std::string gridtype; |
| 526 | + std::string modality; |
| 527 | + int timestep; |
| 528 | + |
| 529 | + // read keys |
| 530 | + while (!iffile.eof()) { |
| 531 | + std::string line; |
| 532 | + std::getline(iffile, line); |
| 533 | + |
| 534 | + std::cout << line << std::endl; |
| 535 | + |
| 536 | + std::string key = line.substr(0, line.find_first_of(':')); |
| 537 | + std::string content = line.substr(line.find_first_of(":") + 1); |
| 538 | + |
| 539 | + // skip ' ' and '\t' |
| 540 | + int count_skip = 0; |
| 541 | + while (content[count_skip] == ' ' || content[count_skip] == '\t') { |
| 542 | + count_skip += 1; |
| 543 | + } |
| 544 | + content = content.substr(count_skip); |
| 545 | + |
| 546 | + if (key.find("ObjectFileName") == 0) { |
| 547 | + objectfilename = content; |
| 548 | + } |
| 549 | + else if (key.find("TaggedFileName") == 0) { |
| 550 | + taggedfilename = content; |
| 551 | + } |
| 552 | + else if (key.find("Resolution") == 0) { |
| 553 | + std::string str_resolution = content; |
| 554 | + |
| 555 | + std::vector<int> vec_resolution; |
| 556 | + while (str_resolution.find(' ') != std::string::npos) { |
| 557 | + std::string size_l = str_resolution.substr(0, str_resolution.find(' ')); |
| 558 | + vec_resolution.push_back(std::atoi(size_l.c_str())); |
| 559 | + str_resolution = str_resolution.substr(str_resolution.find(' ') + 1); |
| 560 | + } |
| 561 | + vec_resolution.push_back(std::atoi(str_resolution.c_str())); |
| 562 | + |
| 563 | + resolution = glm::ivec3(vec_resolution[0], vec_resolution[1], vec_resolution[2]); |
| 564 | + } |
| 565 | + else if (key.find("SliceThickness") == 0) { |
| 566 | + std::string str_slicethickness = content; |
| 567 | + |
| 568 | + std::vector<float> vec_thickness; |
| 569 | + while (str_slicethickness.find(' ') != std::string::npos) { |
| 570 | + std::string size_l = str_slicethickness.substr(0, str_slicethickness.find(' ')); |
| 571 | + vec_thickness.push_back(std::stof(size_l.c_str())); |
| 572 | + str_slicethickness = str_slicethickness.substr(str_slicethickness.find(' ') + 1); |
| 573 | + } |
| 574 | + vec_thickness.push_back(std::stof(str_slicethickness.c_str())); |
| 575 | + |
| 576 | + slicethickness = glm::vec3(vec_thickness[0], vec_thickness[1], vec_thickness[2]); |
| 577 | + } |
| 578 | + else if (key.find("Format") == 0) { |
| 579 | + format = content; |
| 580 | + } |
| 581 | + // TODO |
| 582 | + else if (key.find("NbrTags") == 0) { |
| 583 | + //int nbrtags; |
| 584 | + } |
| 585 | + // TODO |
| 586 | + else if (key.find("ObjectType") == 0) { |
| 587 | + //std::string objecttype; |
| 588 | + } |
| 589 | + // TODO |
| 590 | + else if (key.find("ObjectModel") == 0) { |
| 591 | + // std::string objectmodel; |
| 592 | + } |
| 593 | + // TODO |
| 594 | + else if (key.find("GridType") == 0) { |
| 595 | + //std::string gridtype; |
| 596 | + } |
| 597 | + // TODO |
| 598 | + else if (key.find("Modality") == 0) { |
| 599 | + //std::string modality; |
| 600 | + } |
| 601 | + // TODO |
| 602 | + else if (key.find("TimeStep") == 0) { |
| 603 | + //int timestep; |
| 604 | + } |
| 605 | + } |
| 606 | + iffile.close(); |
| 607 | + |
| 608 | + sg_ret = new StructuredGridVolume(name, resolution.x, resolution.y, resolution.z); |
| 609 | + sg_ret->SetScale(slicethickness.x, slicethickness.y, slicethickness.z); |
| 610 | + |
| 611 | + std::string volume_data_array_file = path + "/" + objectfilename; |
| 612 | + |
| 613 | + int bytes_per_value = 1; |
| 614 | + if ((int)format.find("UCHAR") > -1) { // 8 bits |
| 615 | + bytes_per_value = sizeof(unsigned char); |
| 616 | + } |
| 617 | + else if ((int)format.find("USHORT") > -1) { // 16 bits |
| 618 | + bytes_per_value = sizeof(unsigned short); |
| 619 | + } |
| 620 | + |
| 621 | + SetArrayDataFromRawFile(volume_data_array_file, sg_ret, bytes_per_value); |
| 622 | + } |
| 623 | + else { |
| 624 | + printf("Finished -> Error on opening .dat file\n"); |
| 625 | + } |
| 626 | + |
| 627 | + return sg_ret; |
| 628 | + } |
| 629 | + |
363 | 630 | UnstructuredGridVolume* VolumeReader::readunsvol (std::string filepath) |
364 | 631 | { |
365 | 632 | UnstructuredGridVolume* sg_ret = nullptr; |
|
0 commit comments