diff --git a/src/Math/VoxImage.cpp b/src/Math/VoxImage.cpp index 93a9e34..35c92b9 100644 --- a/src/Math/VoxImage.cpp +++ b/src/Math/VoxImage.cpp @@ -32,41 +32,12 @@ #include #include - #include +#include namespace uLib { -void Abstract::VoxImage::SaveToVtkVti (const char *file) -{ - Abstract::VoxImage *voxels = this; - - vtkSmartPointer image = vtkSmartPointer::New(); - image->SetDimensions(voxels->GetDims()(0), voxels->GetDims()(1), voxels->GetDims()(2)); - image->SetSpacing(voxels->GetSpacing()(0), voxels->GetSpacing()(1), voxels->GetSpacing()(2)); - image->SetOrigin(voxels->GetOrigin()(0), voxels->GetOrigin()(1), voxels->GetOrigin()(2)); - image->AllocateScalars(VTK_FLOAT, 1); - - int nx = voxels->GetDims()(0); - int ny = voxels->GetDims()(1); - int nz = voxels->GetDims()(2); - - size_t npoints = nx*ny*nz; - float *scalar = static_cast(image->GetScalarPointer()); - - for (size_t i = 0; i < npoints; i++) { - scalar[i] = static_cast(voxels->GetValue(i)); - } - - vtkSmartPointer writer = vtkSmartPointer::New(); - writer->SetFileName(file); - writer->SetInputData(image); - writer->Write(); -} - - - void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type) { FILE * vtk_file = fopen(file,"wb"); @@ -117,6 +88,57 @@ void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type) printf("%s vtk file saved\n",file); } + + +void Abstract::VoxImage::ExportToVti (const char *file, bool density_type, bool compressed) +{ + Abstract::VoxImage *voxels = this; + + vtkSmartPointer image = vtkSmartPointer::New(); + image->SetDimensions(voxels->GetDims()(0), voxels->GetDims()(1), voxels->GetDims()(2)); + image->SetSpacing(voxels->GetSpacing()(0), voxels->GetSpacing()(1), voxels->GetSpacing()(2)); + image->SetOrigin(voxels->GetOrigin()(0), voxels->GetOrigin()(1), voxels->GetOrigin()(2)); + image->AllocateScalars(VTK_FLOAT, 1); + + float norm; + if (density_type) { + norm = 1; + } else norm = 1.E6; + + int nx = voxels->GetDims()(0); + int ny = voxels->GetDims()(1); + int nz = voxels->GetDims()(2); + + size_t npoints = nx*ny*nz; + float *scalar = static_cast(image->GetScalarPointer()); + + for (size_t i = 0; i < npoints; i++) { + scalar[i] = static_cast(voxels->GetValue(i) * norm); + } + + // // Create a custom string key + // static vtkInformationStringKey* ConfigNote = + // vtkInformationStringKey::MakeKey("ConfigNote", "MyNotes"); + + // // Attach metadata + // image->GetInformation()->Set(ConfigNote, "This image was generated with method X, threshold=0.5"); + + + vtkSmartPointer writer = vtkSmartPointer::New(); + writer->SetFileName(file); + writer->SetInputData(image); + if(compressed) { + writer->SetDataModeToBinary(); + writer->SetCompressorTypeToZLib(); + } + writer->Write(); +} + + + + + + int Abstract::VoxImage::ImportFromVtk(const char *file) { FILE * vtk_file = fopen(file, "r"); diff --git a/src/Math/VoxImage.h b/src/Math/VoxImage.h index fee3a7e..15e3079 100644 --- a/src/Math/VoxImage.h +++ b/src/Math/VoxImage.h @@ -56,9 +56,9 @@ public: virtual void SetDims(const Vector3i &size) = 0; void ExportToVtk(const char *file, bool density_type = 0); + void ExportToVti (const char *file, bool density_type = 0, bool compressed = 0); void ExportToVtkXml(const char *file, bool density_type = 0); int ImportFromVtk(const char *file); - void SaveToVtkVti (const char *file); protected: