diff --git a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp index d2d01c9eac..2341c17577 100644 --- a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp +++ b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.cpp @@ -1,90 +1,113 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "SegmentationReworkREST.h" #include SegmentationReworkREST::SegmentationReworkREST() {} SegmentationReworkREST::SegmentationReworkREST(utility::string_t url) : mitk::RESTServer(url) { m_Listener.support(MitkRESTMethods::PUT, std::bind(&SegmentationReworkREST::HandlePut, this, std::placeholders::_1)); + m_Listener.support(MitkRESTMethods::GET, std::bind(&SegmentationReworkREST::HandleGet, this, std::placeholders::_1)); } SegmentationReworkREST::~SegmentationReworkREST() {} -void SegmentationReworkREST::SetPutCallback(std::function callback) +void SegmentationReworkREST::HandleGet(MitkRequest message) { - m_PutCallback = callback; + auto messageString = message.to_string(); + MITK_INFO << "Message GET incoming..."; + MITK_INFO << convertToUtf8(messageString); + + auto http_get_vars = web::uri::split_query(message.request_uri().query()); + + // IHE Invoke Image Display style + auto requestType = http_get_vars.at(L"requestType"); + + if (requestType == L"IMAGE_SEG") + { + auto studyUID = http_get_vars.at(L"studyUID"); + auto imageSeriesUID = http_get_vars.at(L"imageSeriesUID"); + auto segSeriesUID = http_get_vars.at(L"segSeriesUID"); + + DicomDTO dto; + dto.imageSeriesUID = convertToUtf8(imageSeriesUID); + dto.studyUID = convertToUtf8(studyUID); + dto.segSeriesUIDA = convertToUtf8(segSeriesUID); + + m_GetCallback(dto); + } } void SegmentationReworkREST::HandlePut(MitkRequest message) { auto messageString = message.to_string(); MITK_INFO << "Message PUT incoming..."; MITK_INFO << convertToUtf8(messageString); try { auto jsonMessage = message.extract_json().get(); auto messageTypeKey = jsonMessage.at(U("messageType")); if (messageTypeKey.as_string() == U("downloadData")) { auto imageStudyUIDKey = jsonMessage.at(U("studyUID")); auto imageSeriesUIDKey = jsonMessage.at(U("imageSeriesUID")); auto segSeriesUIDAKey = jsonMessage.at(U("segSeriesUIDA")); auto segSeriesUIDBKey = jsonMessage.at(U("segSeriesUIDB")); auto simScoreKey = jsonMessage.at(U("simScoreArray")); auto minSliceStartKey = jsonMessage.at(U("minSliceStart")); DicomDTO dto; dto.segSeriesUIDA = convertToUtf8(segSeriesUIDAKey.as_string()); dto.segSeriesUIDB = convertToUtf8(segSeriesUIDBKey.as_string()); dto.imageSeriesUID = convertToUtf8(imageSeriesUIDKey.as_string()); dto.studyUID = convertToUtf8(imageStudyUIDKey.as_string()); dto.minSliceStart = minSliceStartKey.as_integer(); std::vector vec; web::json::array simArray = simScoreKey.as_array(); - for (web::json::value score : simArray) { + for (web::json::value score : simArray) + { vec.push_back(score.as_double()); } dto.simScoreArray = vec; m_PutCallback(dto); emit InvokeUpdateChartWidget(); } else { message.reply(MitkRestStatusCodes::BadRequest, "Oh man, i can only deal with 'messageType' = 'downloadData'..."); } } catch (MitkJsonException &e) { MITK_ERROR << e.what() << ".. oh man, that was not expected"; message.reply(MitkRestStatusCodes::BadRequest, "oh man, that was not expected"); return; } MitkResponse response(MitkRestStatusCodes::OK); response.headers().add(U("Access-Control-Allow-Origin"), U("localhost:9000/*")); response.set_body("Sure, i got you.. have an awesome day"); message.reply(response); return; } \ No newline at end of file diff --git a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h index 98bde97924..642fca7949 100644 --- a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h +++ b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkREST.h @@ -1,53 +1,63 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef SegmentationReworkREST_h #define SegmentationReworkREST_h #include class SegmentationReworkREST : public mitk::RESTServer { Q_OBJECT public: - - struct DicomDTO { + struct DicomDTO + { std::string segSeriesUIDA; std::string segSeriesUIDB; std::string imageSeriesUID; - std::string studyUID; + std::string studyUID; std::string segInstanceUIDA; - std::string segInstanceUIDB; + std::string segInstanceUIDB; std::vector simScoreArray; int minSliceStart; }; SegmentationReworkREST(); SegmentationReworkREST(utility::string_t url); ~SegmentationReworkREST(); void HandlePut(MitkRequest message); - void SetPutCallback(std::function callback); + void HandleGet(MitkRequest message); + + void SetPutCallback(std::function callback) + { + m_PutCallback = callback; + } + void SetGetCallback(std::function callback) + { + m_GetCallback = callback; + } signals: void InvokeUpdateChartWidget(); private: - std::function m_PutCallback; + std::function m_PutCallback; + std::function m_GetCallback; }; #endif // SegmentationReworkREST_h diff --git a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.cpp b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.cpp index 290f45cdd7..f5eadbbaa4 100644 --- a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.cpp +++ b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.cpp @@ -1,202 +1,224 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Blueberry #include #include // Qmitk #include "SegmentationReworkView.h" // Qt #include #include #include // uuid class #include // generators #include #include #include const std::string SegmentationReworkView::VIEW_ID = "org.mitk.views.segmentationreworkview"; void SegmentationReworkView::SetFocus() {} void SegmentationReworkView::CreateQtPartControl(QWidget *parent) { // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); qRegisterMetaType< std::vector >("std::vector"); connect(m_Controls.buttonUpload, &QPushButton::clicked, this, &SegmentationReworkView::UploadNewSegmentation); utility::string_t port = U("2020"); utility::string_t address = U("http://127.0.0.1:"); address.append(port); m_HttpHandler = std::unique_ptr(new SegmentationReworkREST(address)); connect(m_HttpHandler.get(), &SegmentationReworkREST::InvokeUpdateChartWidget, this, &SegmentationReworkView::UpdateChartWidget); connect(this, &SegmentationReworkView::InvokeLoadData, this, &SegmentationReworkView::LoadData); m_HttpHandler->SetPutCallback(std::bind(&SegmentationReworkView::RESTPutCallback, this, std::placeholders::_1)); + m_HttpHandler->SetGetCallback(std::bind(&SegmentationReworkView::RESTGetCallback, this, std::placeholders::_1)); m_HttpHandler->Open().wait(); MITK_INFO << "Listening for requests at: " << utility::conversions::to_utf8string(address); utility::string_t pacsHost = U("http://193.174.48.78:8090/dcm4chee-arc/aets/DCM4CHEE"); m_RestClient = new mitk::RESTClient(pacsHost); } void SegmentationReworkView::RESTPutCallback(const SegmentationReworkREST::DicomDTO &dto) { SetSimilarityGraph(dto.simScoreArray, dto.minSliceStart); MITK_INFO << "Load related dicom series ..."; boost::uuids::random_generator generator; std::string folderPathSeries = m_downloadBaseDir + boost::uuids::to_string(generator()) + "/"; std::experimental::filesystem::create_directory(folderPathSeries); std::string pathSegA = m_downloadBaseDir + boost::uuids::to_string(generator()) + "/"; std::string pathSegB = m_downloadBaseDir + boost::uuids::to_string(generator()) + "/"; auto folderPathSegA = utility::conversions::to_string_t(pathSegA); auto folderPathSegB = utility::conversions::to_string_t(pathSegB); std::experimental::filesystem::create_directory(pathSegA); std::experimental::filesystem::create_directory(pathSegB); m_CurrentStudyUID = dto.studyUID; std::vector> tasks; auto imageSeriesTask = m_RestClient->WadoRS(utility::conversions::to_string_t(folderPathSeries), dto.studyUID, dto.imageSeriesUID); auto segATask = m_RestClient->WadoRS(folderPathSegA, dto.studyUID, dto.segSeriesUIDA); auto segBTask = m_RestClient->WadoRS(folderPathSegB, dto.studyUID, dto.segSeriesUIDB); tasks.push_back(imageSeriesTask); tasks.push_back(segATask); tasks.push_back(segBTask); auto joinTask = pplx::when_all(begin(tasks), end(tasks)); auto filePathList = joinTask.then([&](std::vector filePathList) { InvokeLoadData(filePathList); }); } +void SegmentationReworkView::RESTGetCallback(const SegmentationReworkREST::DicomDTO &dto) +{ + boost::uuids::random_generator generator; + + std::string folderPathSeries = m_downloadBaseDir + boost::uuids::to_string(generator()) + "/"; + std::experimental::filesystem::create_directory(folderPathSeries); + + std::string pathSeg = m_downloadBaseDir + boost::uuids::to_string(generator()) + "/"; + auto folderPathSeg = utility::conversions::to_string_t(pathSeg); + std::experimental::filesystem::create_directory(pathSeg); + + std::vector> tasks; + auto imageSeriesTask = m_RestClient->WadoRS(utility::conversions::to_string_t(folderPathSeries), dto.studyUID, dto.imageSeriesUID); + auto segATask = m_RestClient->WadoRS(folderPathSeg, dto.studyUID, dto.segSeriesUIDA); + tasks.push_back(imageSeriesTask); + tasks.push_back(segATask); + + auto joinTask = pplx::when_all(begin(tasks), end(tasks)); + auto filePathList = joinTask.then([&](std::vector filePathList) { InvokeLoadData(filePathList); }); +} + void SegmentationReworkView::LoadData(std::vector filePathList) { MITK_INFO << "Loading finished. Pushing data to data storage ..."; auto ds = GetDataStorage(); mitk::IOUtil::Load(filePathList, *ds); // reinit view mitk::RenderingManager::GetInstance()->InitializeViewsByBoundingObjects(ds); } void SegmentationReworkView::UpdateChartWidget() { m_Controls.chartWidget->Show(); } void SegmentationReworkView::SetSimilarityGraph(std::vector simScoreArray, int sliceMinStart) { std::map map; double sliceIndex = sliceMinStart; for (double score : simScoreArray) { map.insert(std::map::value_type(sliceIndex, score)); sliceIndex++; } m_Controls.chartWidget->AddData2D(map, "similarity score"); m_Controls.chartWidget->SetChartType("similarity score", QmitkChartWidget::ChartType::line); m_Controls.chartWidget->SetXAxisLabel("slice number"); m_Controls.chartWidget->SetYAxisLabel("similarity score"); } void SegmentationReworkView::OnSelectionChanged(berry::IWorkbenchPart::Pointer /*source*/, const QList &nodes) { // iterate all selected objects, adjust warning visibility foreach(mitk::DataNode::Pointer node, nodes) { if (node.IsNotNull() && dynamic_cast(node->GetData())) { m_Controls.labelWarning->setVisible(false); return; } } m_Controls.labelWarning->setVisible(true); } void SegmentationReworkView::UploadNewSegmentation() { auto filePath = U("1.2.276.0.7230010.3.1.4.296485376.8.1533635734.141264.dcm"); m_CurrentStudyUID = "1.2.840.113654.2.70.1.159145727925405623564217141386659468090"; try { m_RestClient->StowRS(filePath, m_CurrentStudyUID).wait(); } catch (const std::exception &exception) { std::cout << exception.what() << std::endl; } } void SegmentationReworkView::DoImageProcessing() { QList nodes = this->GetDataManagerSelection(); if (nodes.empty()) return; mitk::DataNode *node = nodes.front(); if (!node) { // Nothing selected. Inform the user and return QMessageBox::information(nullptr, "Template", "Please load and select an image before starting image processing."); return; } // here we have a valid mitk::DataNode // a node itself is not very useful, we need its data item (the image) mitk::BaseData *data = node->GetData(); if (data) { // test if this data item is an image or not (could also be a surface or something totally different) mitk::Image *image = dynamic_cast(data); if (image) { std::stringstream message; std::string name; message << "Performing image processing for image "; if (node->GetName(name)) { // a property called "name" was found for this DataNode message << "'" << name << "'"; } message << "."; MITK_INFO << message.str(); // actually do something here... } } } diff --git a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.h b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.h index efe5ce3c73..428dd59330 100644 --- a/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.h +++ b/Plugins/org.mitk.gui.qt.segmentation.rework/src/internal/SegmentationReworkView.h @@ -1,82 +1,83 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef SegmentationReworkView_h #define SegmentationReworkView_h #include #include #include "ui_SegmentationReworkViewControls.h" #include "SegmentationReworkRest.h" #include /** \brief SegmentationReworkView \warning This class is not yet documented. Use "git blame" and ask the author to provide basic documentation. \sa QmitkAbstractView \ingroup ${plugin_target}_internal */ class SegmentationReworkView : public QmitkAbstractView { // this is needed for all Qt objects that should have a Qt meta-object // (everything that derives from QObject and wants to have signal/slots) Q_OBJECT public: static const std::string VIEW_ID; void RESTPutCallback(const SegmentationReworkREST::DicomDTO& dto); + void RESTGetCallback(const SegmentationReworkREST::DicomDTO& dto); void UpdateChartWidget(); void LoadData(std::vector filePathList); signals: void InvokeLoadData(std::vector filePathList); protected: virtual void CreateQtPartControl(QWidget *parent) override; virtual void SetFocus() override; /// \brief called by QmitkFunctionality when DataManager's selection has changed virtual void OnSelectionChanged(berry::IWorkbenchPart::Pointer source, const QList &nodes) override; /// \brief Called when the user clicks the GUI button void DoImageProcessing(); void UploadNewSegmentation(); Ui::SegmentationReworkViewControls m_Controls; private: void SetSimilarityGraph(std::vector simScoreArray, int sliceMinStart); std::unique_ptr m_HttpHandler; mitk::RESTClient* m_RestClient; std::string m_CurrentStudyUID; // use filesystem::path later... std::string m_downloadBaseDir = "/temp/"; }; #endif // SegmentationReworkView_h