diff --git a/Modules/ROI/autoload/IO/src/mitkROIIO.cpp b/Modules/ROI/autoload/IO/src/mitkROIIO.cpp index 6bdce335bb..4be0e0d2b7 100644 --- a/Modules/ROI/autoload/IO/src/mitkROIIO.cpp +++ b/Modules/ROI/autoload/IO/src/mitkROIIO.cpp @@ -1,118 +1,108 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "mitkROIIO.h" #include <mitkROI.h> #include <mitkROIIOMimeTypes.h> #include <nlohmann/json.hpp> #include <filesystem> #include <fstream> +namespace +{ + mitk::Geometry3D::Pointer ReadGeometry(const nlohmann::json& jsonGeometry) + { + auto result = mitk::Geometry3D::New(); + result->ImageGeometryOn(); + + if (!jsonGeometry.is_object()) + mitkThrow() << "Geometry is expected to be a JSON object."; + + if (jsonGeometry.contains("Origin")) + result->SetOrigin(jsonGeometry["Origin"].get<mitk::Point3D>()); + + if (jsonGeometry.contains("Spacing")) + result->SetSpacing(jsonGeometry["Spacing"].get<mitk::Vector3D>()); + + if (jsonGeometry.contains("Size")) + { + auto size = jsonGeometry["Size"].get<mitk::Vector3D>(); + mitk::BaseGeometry::BoundsArrayType bounds({ 0.0, size[0], 0.0, size[1], 0.0, size[2] }); + result->SetBounds(bounds); + } + + return result; + } +} + mitk::ROIIO::ROIIO() : AbstractFileIO(ROI::GetStaticNameOfClass(), MitkROIIOMimeTypes::ROI_MIMETYPE(), "MITK ROI") { this->RegisterService(); } std::vector<mitk::BaseData::Pointer> mitk::ROIIO::DoRead() { auto *stream = this->GetInputStream(); std::ifstream fileStream; if (nullptr == stream) { auto filename = this->GetInputLocation(); if (filename.empty() || !std::filesystem::exists(filename)) mitkThrow() << "Invalid or nonexistent filename: \"" << filename << "\"!"; fileStream.open(filename); if (!fileStream.is_open()) mitkThrow() << "Could not open file \"" << filename << "\" for reading!"; stream = &fileStream; } - nlohmann::json json; + auto result = ROI::New(); try { - json = nlohmann::json::parse(*stream); - } - catch (const nlohmann::json::exception &e) - { - mitkThrow() << e.what(); - } - - if (!json.is_object()) - mitkThrow() << "Unknown file format (expected JSON object as root)!"; - - if ("MITK ROI" != json.value("FileFormat", "")) - mitkThrow() << "Unknown file format (expected \"MITK ROI\")!"; - - if (1 != json.value<int>("Version", 0)) - mitkThrow() << "Unknown file format version (expected \"1\")!"; - - auto geometry = Geometry3D::New(); + auto json = nlohmann::json::parse(*stream); - if (json.contains("Geometry")) - { - auto jsonGeometry = json["Geometry"]; + if ("MITK ROI" != json["FileFormat"].get<std::string>()) + mitkThrow() << "Unknown file format (expected \"MITK ROI\")!"; - if (!jsonGeometry.is_object()) - mitkThrow() << "Geometry is expected to be a JSON object."; + if (1 != json["Version"].get<int>()) + mitkThrow() << "Unknown file format version (expected version 1)!"; - auto geometryType = jsonGeometry.value<std::string>("Type", "Embedded"); + result->SetGeometry(ReadGeometry(json["Geometry"])); - if (geometryType != "Embedded") - mitkThrow() << "Unknown geometry type \"" << geometryType << "\"!"; - - if (jsonGeometry.contains("Origin")) - geometry->SetOrigin(jsonGeometry["Origin"].get<Point3D>()); - - if (jsonGeometry.contains("Spacing")) - geometry->SetSpacing(jsonGeometry["Spacing"].get<Vector3D>()); - } - - if (!json.contains("ROIs") || !json["ROIs"].is_array()) - mitkThrow() << "ROIs array not found!"; - - std::vector<BaseData::Pointer> results; - - try - { - for (const auto& roi : json["ROIs"]) + for (const auto& jsonROI : json["ROIs"]) { - auto result = ROI::New(); - result->SetGeometry(geometry); - results.push_back(result.GetPointer()); } } - catch (const nlohmann::json::type_error &e) + catch (const nlohmann::json::exception &e) { mitkThrow() << e.what(); } - return results; + return { result }; } void mitk::ROIIO::Write() { } mitk::ROIIO* mitk::ROIIO::IOClone() const { return new ROIIO(*this); }