Skip to content

Commit 8cf7d91

Browse files
committed
simple nrrd and dat readers
adding methods to read .dat from Visualization and Multimedia Lab - zurich and .nrrd from Open Scientific Visualization Datasets website.
1 parent 64986d0 commit 8cf7d91

File tree

4 files changed

+305
-7
lines changed

4 files changed

+305
-7
lines changed

libs/volvis_utils/datamanager.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,10 @@ namespace vis
254254
int start_name = line.find_last_of("<") + 1;
255255
int end_name = line.find_last_of(">");
256256

257-
stored_structured_datasets.push_back(DataReference(
258-
line.substr(start_path, end_path - start_path),
259-
line.substr(start_name, end_name - start_name),
260-
m_path_to_data
261-
));
257+
std::string path_to_model = line.substr(start_path, end_path - start_path);
258+
std::string model_name = line.substr(start_name, end_name - start_name);
259+
260+
stored_structured_datasets.push_back(DataReference(path_to_model, model_name.empty() ? path_to_model : model_name, m_path_to_data));
262261
}
263262
f_openmodels.close();
264263

libs/volvis_utils/dataprovider.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ namespace vis
5656

5757
std::cout << ". " << model_name << std::endl;
5858

59-
m_uiname_list_structured_grid.push_back(model_name);
59+
if (model_name.empty()) {
60+
std::string path_model = line.substr(start_path, end_path - start_path);
61+
m_uiname_list_structured_grid.push_back(path_model);
62+
}
63+
else {
64+
m_uiname_list_structured_grid.push_back(model_name);
65+
}
6066
}
6167
f_open_file.close();
6268
}
@@ -148,7 +154,6 @@ namespace vis
148154
int end_path = line.find_first_of(">");
149155

150156
std::string path_file = line.substr(start_path, end_path - start_path);
151-
152157

153158
vis::VolumeReader vr;
154159
sg = vr.ReadStructuredVolume(m_path_list_structured_grid + "/" + path_file);

libs/volvis_utils/reader.cpp

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <file_utils/rawloader.h>
1010

1111
#include <fstream>
12+
#include <array>
1213

1314
#include <volvis_utils/transferfunction1d.h>
1415

@@ -44,6 +45,15 @@ namespace vis
4445
else if (extension.compare("syn") == 0) {
4546
ret = readsyn(filepath);
4647
}
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+
}
4757
printf("DONE\n");
4858

4959
return ret;
@@ -360,6 +370,263 @@ namespace vis
360370
return sg_ret;
361371
}
362372

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+
363630
UnstructuredGridVolume* VolumeReader::readunsvol (std::string filepath)
364631
{
365632
UnstructuredGridVolume* sg_ret = nullptr;

libs/volvis_utils/reader.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ namespace vis
3636
StructuredGridVolume* readpvmold (std::string filename);
3737
StructuredGridVolume* readraw (std::string filepath);
3838
StructuredGridVolume* readsyn (std::string filepath);
39+
/**
40+
* http://teem.sourceforge.net/nrrd/format.html
41+
* -------------------------------------------
42+
* NRRD000X
43+
* <field>: <desc>
44+
* <field>: <desc>
45+
* # <comment>
46+
* ...
47+
* <field>: <desc>
48+
* <key>:=<value>
49+
* <key>:=<value>
50+
* <key>:=<value>
51+
* # <comment>
52+
*
53+
* <data><data><data><data><data><data>...
54+
* -------------------------------------------
55+
* NRRD0001: (and NRRD00.01 for circa 1998 files) original and most basic version
56+
* NRRD0002: added key/value pairs
57+
* NRRD0003: added "kinds:" field,
58+
* NRRD0004: added "thicknesses:" and "sample units" fields, general space and orientation information ("space:", "space dimension:", "space directions:", "space origin:", and "space units:" fields) , and the ability for the "data file:" field to identify multiple data files.
59+
* NRRD0005: added "measurement frame:" field (should have been figured out for NRRD0004).
60+
*/
61+
StructuredGridVolume* readnrrd (std::string filepath);
62+
// File structure from open scivis datasets, equal to nrrd
63+
StructuredGridVolume* readnhrd (std::string filepath);
64+
// File structure from zurich datasets
65+
StructuredGridVolume* readdat (std::string filepath);
3966

4067
UnstructuredGridVolume* readunsvol (std::string filepath);
4168

0 commit comments

Comments
 (0)