#include "SliceExport.hpp" #include #include #include #include #include #include namespace geopro::app { bool exportSliceImagePng(vtkImageData* colorImage, const std::string& path) { if (colorImage == nullptr || path.empty()) return false; vtkNew writer; writer->SetFileName(path.c_str()); writer->SetInputData(colorImage); // 已上色 RGB 的切片 2D 图(非整窗截图) writer->Write(); return writer->GetErrorCode() == 0; } bool exportSliceDat(vtkImageData* slice, const std::string& path) { if (slice == nullptr || path.empty()) return false; vtkDataArray* arr = slice->GetPointData() ? slice->GetPointData()->GetScalars() : nullptr; if (arr == nullptr) return false; int dims[3]; slice->GetDimensions(dims); const int nx = dims[0], ny = dims[1]; if (nx < 1 || ny < 1) return false; std::ofstream out(path); if (!out) return false; // 切片重采样为 2D(dims[2]=1):写成行=j、列=i 的标量网格,每格取首分量。 for (int j = 0; j < ny; ++j) { for (int i = 0; i < nx; ++i) { const vtkIdType id = static_cast(j) * nx + i; out << arr->GetComponent(id, 0) << (i + 1 < nx ? ' ' : '\n'); } } return static_cast(out); } } // namespace geopro::app