mirror of
https://github.com/OpenCMT/uLib.git
synced 2025-12-05 23:11:31 +01:00
update
This commit is contained in:
@@ -32,41 +32,12 @@
|
|||||||
|
|
||||||
#include <vtkSmartPointer.h>
|
#include <vtkSmartPointer.h>
|
||||||
#include <vtkImageData.h>
|
#include <vtkImageData.h>
|
||||||
|
|
||||||
#include <vtkXMLImageDataWriter.h>
|
#include <vtkXMLImageDataWriter.h>
|
||||||
|
#include <vtkStringArray.h>
|
||||||
|
|
||||||
|
|
||||||
namespace uLib {
|
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)
|
void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type)
|
||||||
{
|
{
|
||||||
FILE * vtk_file = fopen(file,"wb");
|
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);
|
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)
|
int Abstract::VoxImage::ImportFromVtk(const char *file)
|
||||||
{
|
{
|
||||||
FILE * vtk_file = fopen(file, "r");
|
FILE * vtk_file = fopen(file, "r");
|
||||||
|
|||||||
@@ -56,9 +56,9 @@ public:
|
|||||||
virtual void SetDims(const Vector3i &size) = 0;
|
virtual void SetDims(const Vector3i &size) = 0;
|
||||||
|
|
||||||
void ExportToVtk(const char *file, bool density_type = 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);
|
void ExportToVtkXml(const char *file, bool density_type = 0);
|
||||||
int ImportFromVtk(const char *file);
|
int ImportFromVtk(const char *file);
|
||||||
void SaveToVtkVti (const char *file);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user