This commit is contained in:
AndreaRigoni
2025-09-02 13:01:55 +02:00
parent b86e27a9c4
commit 01ff8a0a0d
2 changed files with 53 additions and 31 deletions

View File

@@ -32,41 +32,12 @@
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkXMLImageDataWriter.h>
#include <vtkStringArray.h>
namespace uLib {
void Abstract::VoxImage::SaveToVtkVti (const char *file)
{
Abstract::VoxImage *voxels = this;
vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::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<float*>(image->GetScalarPointer());
for (size_t i = 0; i < npoints; i++) {
scalar[i] = static_cast<float>(voxels->GetValue(i));
}
vtkSmartPointer<vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::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<vtkImageData> image = vtkSmartPointer<vtkImageData>::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<float*>(image->GetScalarPointer());
for (size_t i = 0; i < npoints; i++) {
scalar[i] = static_cast<float>(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<vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::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");

View File

@@ -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: