diff --git a/VisualizationContainer.cxx b/VisualizationContainer.cxx index 3c1aec6..ae6d221 100644 --- a/VisualizationContainer.cxx +++ b/VisualizationContainer.cxx @@ -189,27 +189,29 @@ bool VisualizationContainer::OpenSegmentationFile(const std::string& fileName) { reader->SetFileName(fileName.c_str()); reader->Update(); - labels = reader->GetOutput(); + if (SetLabelData(reader->GetOutput())) { + LoadRegionMetadata(fileName + ".json"); + + qtWindow->UpdateRegionTable(regions); + + return true; + } } else if (extension == "nii") { vtkSmartPointer reader = vtkSmartPointer::New(); reader->SetFileName(fileName.c_str()); reader->Update(); - labels = reader->GetOutput(); - } - else { - return false; - } + if (SetLabelData(reader->GetOutput())) { + LoadRegionMetadata(fileName + ".json"); - UpdateLabels(); - LoadRegionMetadata(fileName + ".json"); + qtWindow->UpdateRegionTable(regions); - qtWindow->UpdateRegionTable(regions); - - Render(); + return true; + } + } - return true; + return false; } bool VisualizationContainer::OpenSegmentationStack(const std::vector& fileNames) { @@ -232,20 +234,16 @@ bool VisualizationContainer::OpenSegmentationStack(const std::vectorSetFileNames(names); reader->Update(); - labels = reader->GetOutput(); - } - else { - return false; - } + if (SetLabelData(reader->GetOutput())) { + LoadRegionMetadata(fileNames[0] + ".json"); - UpdateLabels(); - LoadRegionMetadata(fileNames[0] + ".json"); - - qtWindow->UpdateRegionTable(regions); + qtWindow->UpdateRegionTable(regions); - Render(); + return true; + } + } - return true; + return false; } bool VisualizationContainer::SaveSegmentationData(const std::string& fileName) { @@ -630,6 +628,38 @@ void VisualizationContainer::SetImageData(vtkImageData* imageData) { sliceView->SetImageData(data); } +bool VisualizationContainer::SetLabelData(vtkImageData* labelData) { + // Check that volumes match + int* dataDims = data->GetDimensions(); + int* labelDims = data->GetDimensions(); + + for (int i = 0; i < 3; i++) { + if (dataDims[i] != labelDims[i]) { + std::cout << dataDims[i] << " != " << labelDims[i] << std::endl; + return false; + } + } + + // Check that bounds match + double* dataBounds = data->GetBounds(); + double* labelBounds = labelData->GetBounds(); + + double epsilon = 1e-6; + + for (int i = 0; i < 6; i++) { + if (std::abs(dataBounds[i] - labelBounds[i]) > epsilon) { + std::cout << dataBounds[i] << " != " << labelBounds[i] << std::endl; + return false; + } + } + + labels = labelData; + + UpdateLabels(); + + return true; +} + void VisualizationContainer::UpdateLabels() { UpdateColors(); ExtractRegions(); diff --git a/VisualizationContainer.h b/VisualizationContainer.h index ade114c..aa8c3f7 100644 --- a/VisualizationContainer.h +++ b/VisualizationContainer.h @@ -80,6 +80,7 @@ class VisualizationContainer { enum InteractionMode interactionMode; void SetImageData(vtkImageData* imageData); + bool SetLabelData(vtkImageData* labelData); void UpdateLabels(); void UpdateColors();