diff --git a/Modules/SemanticRelations/include/mitkSemanticRelations.h b/Modules/SemanticRelations/include/mitkSemanticRelations.h index 08bf66beb9..ad260bfb08 100644 --- a/Modules/SemanticRelations/include/mitkSemanticRelations.h +++ b/Modules/SemanticRelations/include/mitkSemanticRelations.h @@ -1,589 +1,589 @@ /*=================================================================== 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 MITKSEMANTICRELATIONS_H #define MITKSEMANTICRELATIONS_H #include // semantic relations module #include "mitkControlPointManager.h" #include "mitkDICOMHelper.h" #include "mitkISemanticRelationsObservable.h" #include "mitkISemanticRelationsObserver.h" #include "mitkRelationStorage.h" #include "mitkSemanticTypes.h" // mitk core #include #include namespace mitk { /* * @brief The API provides functions to query and manipulate image relations and instances, * that are helpful during follow-up examination, like control-points (time period), * types of the images or lesions that may be visible on multiple images. * * The class is able to generate IDs from given data nodes using DICOM information. * These IDs are used to identify the corresponding instances of a specific case. * The case can also be directly identified by the given case ID. * * In the BlackSwan context the case is identified with the DICOM PatientID. * * In order for most functions to work the case ID has to be defined in the model. If not, * the functions do nothing. */ class MITKSEMANTICRELATIONS_EXPORT SemanticRelations : public ISemanticRelationsObservable { public: SemanticRelations(mitk::DataStorage::Pointer dataStorage); ~SemanticRelations(); typedef std::vector LesionVector; typedef std::vector LesionClassVector; typedef std::vector ControlpointVector; typedef std::vector InformationTypeVector; typedef std::vector DataNodeVector; /************************************************************************/ /* functions to implement the observer pattern */ /************************************************************************/ /* * @brief Adds the given concrete observer to the vector that holds all currently registered observer. * If the observer is already registered, it will not be added to the observer vector. * * @par observer The concrete observer to register. */ virtual void AddObserver(ISemanticRelationsObserver* observer) override; /* * @brief Removes the given concrete observer from the vector that holds all currently registered observer. * * @par observer The concrete observer to unregister. */ virtual void RemoveObserver(ISemanticRelationsObserver* observer) override; /************************************************************************/ /* functions to get instances / attributes */ /************************************************************************/ /* * @brief Returns a vector of all lesions that are currently available for the given case. * The lesions may be marked by a segmentation or may be empty - with no connection to a specific image / segmentation of the case data. * If no lesions are stored for the current case, an empty vector is returned. * * @par caseID The current case identifier is defined by the given string. * @return A vector of lesions. */ LesionVector GetAllLesionsOfCase(const SemanticTypes::CaseID& caseID) const; /** * @brief * * */ LesionClassVector GetAllLesionClassesOfCase(const SemanticTypes::CaseID& caseID) const; /* * @brief Returns a vector of all lesions that are valid for the given case, given a specific control point. * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A specific control point which has to be available at a returned (found) lesion: * Only those lesions are returned for which the image of the associated segmentation is linked to the given control point. * If the control point instance does not exist, an empty vector is returned. * @return A vector of control points. */ LesionVector GetAllLesionsOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const; /* * @brief Returns a vector of all lesions that are currently available for the current case and are connected to the given image (via a segmentation). * If no lesions are stored for the current case, an empty vector is returned. If no segmentation nodes are * connected with the image node, no lesions for the specific image will be found and an empty vector is returned. * * @pre The given image data node has to be valid (!nullptr). - * @throw mitk::Exception if the given image data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given image data node is invalid (==nullptr). * @pre The image node has to have associated segmentation nodes (child nodes) in order to reference a lesion. * * @par imageNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @return A vector of lesions. */ LesionVector GetAllLesionsInImage(const DataNode* imageNode) const; /* * @brief Returns the lesion that is defined by the given segmentation data. * * @pre The given segmentation data node has to be valid (!nullptr). - * @throw mitk::Exception if the given segmentation data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given segmentation data node is invalid (==nullptr). * @pre The segmentation data node has to represent a lesion. If not, the retrieved lesion will be empty, which leads to an exception. * @throw SemanticRelationException, if the segmentation does not represent an existing lesion (this can be checked via 'IsRepresentingALesion'). * * @par segmentationNode The segmentation identifier is extracted from the given data node. * @return The represented lesion. */ SemanticTypes::Lesion GetRepresentedLesion(const DataNode* segmentationNode) const; /* * @brief Check if the given segmentation refers to an existing lesion instance. * This function can be used before calling 'GetRepresentedLesion' in order to avoid a possible exception. * * @par segmentationNode The segmentation identifier is extracted from the given data node. * @return True, if the segmentation refers to an existing lesion; false otherwise. */ bool IsRepresentingALesion(const DataNode* segmentationNode) const; /* * @brief Return a vector of all segmentations that are currently available for the given case. * The segmentations may be connected / not connected to a lesion of the case. * If no segmentations are stored for the current case, an empty vector is returned. * * @pre The data storage member has to be valid (!nullptr). - * @throw mitk::Exception if the data storage member is invalid (==nullptr). + * @throw SemanticRelationException, if the data storage member is invalid (==nullptr). * * @par caseID The current case identifier is defined by the given string. * @return A vector of data nodes representing segmentations. */ mitk::SemanticRelations::DataNodeVector GetAllSegmentationsOfCase(const SemanticTypes::CaseID& caseID) const; /* * @brief Return a vector of all segmentations that define the given lesion. These segmentations don't have to be linked to the same image. * If the lesion is not referred to by any segmentation, an empty vector is returned. * * @pre The data storage member has to be valid (!nullptr). - * @throw mitk::Exception if the data storage member is invalid (==nullptr). + * @throw SemanticRelationException, if the data storage member is invalid (==nullptr). * @pre The UID of the lesion has to exist for a lesion instance. * @throw SemanticRelationException, if UID of the lesion does not exist for a lesion instance (this can be checked via 'InstanceExists'). * * @par caseID The current case identifier is defined by the given string. * @par lesion A Lesion with a UID that identifies the corresponding lesion instance. * @return A vector of data nodes representing segmentations that define the given lesion. */ DataNodeVector GetAllSegmentationsOfLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const; /* * @brief Return a vector of all images that are currently available for the given case. * * @pre The data storage member has to be valid (!nullptr). - * @throw mitk::Exception if the data storage member is invalid (==nullptr). + * @throw SemanticRelationException, if the data storage member is invalid (==nullptr). * * @par caseID The current case identifier is defined by the given string. * @return A vector of data nodes representing images. */ DataNodeVector GetAllImagesOfCase(const SemanticTypes::CaseID& caseID) const; /* * @brief Return a vector of all images that are connected to those segmentations that are linked to the given lesion. * If the lesion is not referred to by any segmentation, an empty vector is returned. * * @pre The UID of the lesion has to exist for a lesion instance. * @throw SemanticRelationException, if UID of the lesion does not exist for a lesion instance (this can be checked via 'InstanceExists'). * * @par caseID The current case identifier is defined by the given string. * @par lesion A Lesion with a UID that identifies the corresponding lesion instance. * @return A vector of data nodes representing images on which the lesions are visible. */ DataNodeVector GetAllImagesOfLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const; /* * @brief Check if the given lesion instance exists. * This function can be used before calling 'GetAllSegmentationsOfLesion' in order to avoid a possible exception. * This function can be used before calling 'AddLesionInstance' in order to avoid a possible exception. * * @par caseID The current case identifier is defined by the given string. * @par lesion A Lesion with a UID that identifies the corresponding lesion instance. * @return True, if the lesion instance exists; false otherwise. */ bool InstanceExists(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const; /* * @brief Return a vector of all control points that are valid for the given case. * * @par caseID The current case identifier is defined by the given string. * @return A vector of control points. */ ControlpointVector GetAllControlPointsOfCase(const SemanticTypes::CaseID& caseID) const; /* * @brief Return a vector of all control points that are valid for the given case, given a specific lesion * * @par caseID The current case identifier is defined by the given string. * @par lesion A specific lesion which has to be available at a returned (found) control point: * Only those control points are returned for which an associated data has a segmentation that references the given lesion. * If the lesion does not exists, an empty vector is returned. * @return A vector of control points. */ ControlpointVector GetAllControlPointsOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const; /* * @brief Return a vector of all control points that are valid for the given case, given a specific information type. * * @par caseID The current case identifier is defined by the given string. * @par informationType A specific information type which has to be available at a returned (found) control point: * Only those control points are returned for which an associated data has the given information type. * If the information type instance does not exists, an empty vector is returned. * @return A vector of control points. */ ControlpointVector GetAllControlPointsOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType) const; /* * @brief Return the control point of a data node. * If the data node is not linked to a control point or the data node refers to a non-existing control point, * a control point with an empty UID is returned. * * @pre The given data node has to be valid (!nullptr). - * @throw mitk::Exception if the given data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given data node is invalid (==nullptr). * * @par dataNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @return The control point of the given data node. */ SemanticTypes::ControlPoint GetControlPointOfData(const DataNode* dataNode) const; /* * @brief Return a vector of all data nodes that link to the given control point. * If the control point is not referred to by any data node, an empty vector is returned. * * @pre The UID of the control point has to exist for a control point instance. * @throw SemanticRelationException, if the UID of the control point does not exist for a control point instance (this can be checked via 'InstanceExists'). * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. * @return A vector of data nodes that link to the given control point. */ DataNodeVector GetAllDataOfControlPoint(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const; /* * @brief Check if the given control point instance exists. * This function can be used before calling 'GetAllDataOfControlPoint' in order to avoid a possible exception. * This function can be used before adding, linking and unlinking control points to avoid a possible exception. * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. * @return True, if the control point instance exists; false otherwise. */ bool InstanceExists(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const; /* * @brief Return a vector of all information types that are valid for the given case. * * @par caseID The current case identifier is defined by the given string. * @return A vector of information types. */ InformationTypeVector GetAllInformationTypesOfCase(const SemanticTypes::CaseID& caseID) const; /* * @brief Return a vector of all information types that are valid for the given case, given a specific control point. * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A specific control point which has to be available at a returned (found) information type: * Only those information types are returned for which an associated data is linked to the given control point. * If the control point instance does not exist, an empty vector is returned. * @return A vector of information types. */ InformationTypeVector GetAllInformationTypesOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const; /* * @brief Return the information type of the given image. * If the image does not contain any information type, an empty information type is returned. * * @pre The given image data node has to be valid (!nullptr). - * @throw mitk::Exception if the given image data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given image data node is invalid (==nullptr). * * @par imageNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @return The information type of the given data node. */ SemanticTypes::InformationType GetInformationTypeOfImage(const DataNode* imageNode) const; /* * @brief Return a vector of all data nodes that are defined as the given information type. * * @pre The information type has to exist for the given case (and is therefore used by at least one data node). * @throw SemanticRelationException, if the information type is not used by any data node (this can be checked via 'InstanceExists'). * * @par caseID The current case identifier is defined by the given string. * @par informationType An information type that identifies the corresponding information type instance. * @return A vector of data nodes that are defined as the given information type. */ DataNodeVector GetAllDataOfInformationType(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType) const; /* * @brief Return a vector of all data nodes that are defined as the given information type with the given control point. * @pre The UID of the control point has to exist for a control point instance. * The information type has to exist for the given case (and is therefore used by at least one data node). * @throw SemanticRelationException, if the UID of the control point does not exist for a control point instance (this can be checked via 'InstanceExists') or * if the information type is not used by any data node (this can be checked via 'InstanceExists') * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. * @par informationType An information type that identifies the corresponding information type instance. * @return A vector of data nodes that are defined as the given information type with the given control point. */ DataNodeVector GetFilteredData(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint, const SemanticTypes::InformationType& informationType) const; /* * @brief Check if the given information type exists. * This function can be used before calling 'GetAllDataOfInformationType' in order to avoid a possible exception. * * @par caseID The current case identifier is defined by the given string. * @par informationType An information type * @return True, if the information type exists; false otherwise. */ bool InstanceExists(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType) const; /* * @brief Return a vector of all CaseIDs that are currently available. * * @return A vector of CaseIDs as strings. */ std::vector GetAllCaseIDs() const; /************************************************************************/ /* functions to add / remove instances / attributes */ /************************************************************************/ /* * @brief Add the given image to the set of already existing images. * The date is extracted from the DICOM data of the image node and is compared to already existing control points in the semantic relations model. * The function tries to find a fitting control point or to extend an already existing control point, if the extracted control point is close to * any other, already existing control point. * Finally, the image is linked to the correct control point. * * @pre The given image data node has to be valid (!nullptr). - * @throw mitk::Exception if the given image data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given image data node is invalid (==nullptr). * * @par imageNode The current case identifier and node identifier is extracted from the given image data node, which contains DICOM information about the case and the node. */ void AddImage(const DataNode* imageNode); /* * @brief Remove the given image from the set of already existing images. * * @pre The given image data node has to be valid (!nullptr). - * @throw mitk::Exception if the given image data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given image data node is invalid (==nullptr). * * @par imageNode The current case identifier and node identifier is extracted from the given image data node, which contains DICOM information about the case and the node. */ void RemoveImage(const DataNode* imageNode); /* * @brief Add a newly created lesion to the set of already existing lesions - with no connection to a specific image / segmentation of the case data. * * @pre The UID of the lesion must not already exist for a lesion instance. * @throw SemanticRelationException, it the UID of the lesion already exists for a lesion instance (this can be checked via 'InstanceExists'). * * @par caseID The current case identifier is defined by the given string. * @par lesion The lesion instance to add. */ void AddLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion); /* * @brief Overwrite an already existing lesion instance (this may be useful to overwrite the lesion with a different lesion class). * * @pre The UID of the lesion has to exist for a lesion instance. * @throw SemanticRelationException, if the UID of the lesion does not exist for a lesion instance (this can be checked via 'InstanceExists'). * * @par caseID The current case identifier is defined by the given string. * @par lesion The lesion instance that overwrites an existing lesion. */ void OverwriteLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion); /* * @brief Add a newly created lesion to the set of already existing lesions. The lesion is added and a reference to * the lesion is added to the segmentation data. If the segmentation is already linked to a lesion, the * old linkage is overwritten (this can be checked via 'IsRepresentingALesion'). * * @pre The given segmentation data node has to be valid (!nullptr). - * @throw mitk::Exception if the given segmentation data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given segmentation data node is invalid (==nullptr). * @pre The UID of the lesion must not already exist for a lesion instance. * @throw SemanticRelationException, if the UID of the lesion already exists for a lesion instance (this can be checked via 'InstanceExists'). * * @par segmentationNode The segmentation identifier is extracted from the given data node. The segmentation node has DICOM information from its parent node. * @par lesion The lesion instance to add and link. */ void AddLesionAndLinkSegmentation(const DataNode* segmentationNode, const SemanticTypes::Lesion& lesion); /* * @brief Remove the given lesion from the set of already existing lesions. * * @pre The UID of the lesion has to exist for a lesion instance. * @throw SemanticRelationException, if the UID of the lesion does not exist for a lesion instance (this can be checked via 'InstanceExists'). * @pre The function needs to assure that no segmentation is still representing (linked to) this lesion. * @throw SemanticRelationException, if the lesion instance to remove is still linked to by any segmentation (this can be checked via 'GetAllSegmentationsOfLesion'). * * @par caseID The current case identifier is defined by the given string. * @par lesion The lesion instance to remove. */ void RemoveLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion); /* * @brief Add a segmentation instance to the set of already existing segmentations - with no connection to a specific lesion. * * @par segmentationNode The segmentation identifier is extracted from the given data node. The segmentation node has DICOM information from its parent node. * @par parentNode The node identifier of the parent node is extracted from the given parent data node. */ void AddSegmentation(const DataNode* segmentationNode, const DataNode* parentNode); /* * @brief Link the given segmentation instance to an an already existing lesion instance. If the segmentation is already linked to a lesion instance, the * old linkage is overwritten (this can be checked via 'IsRepresentingALesion'). * * @pre The given segmentation data node has to be valid (!nullptr). - * @throw mitk::Exception if the given segmentation data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given segmentation data node is invalid (==nullptr). * @pre The UID of the lesion has to exist for a lesion instance. * @throw SemanticRelationException, if the UID of the lesion does not exist for a lesion instance (this can be checked via 'InstanceExists'). * * @par segmentationNode The segmentation identifier is extracted from the given data node. The segmentation node has DICOM information from its parent node. * @par lesion The lesion instance to link. */ void LinkSegmentationToLesion(const DataNode* segmentationNode, const SemanticTypes::Lesion& lesion); /* * @brief Unlink the given segmentation instance from the linked lesion instance. * The lesion may stay unlinked to any segmentation. * * @pre The given segmentation data node has to be valid (!nullptr). - * @throw mitk::Exception if the given segmentation data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given segmentation data node is invalid (==nullptr). * * @par segmentationNode The segmentation identifier is extracted from the given data node. The segmentation node has DICOM information from its parent node. */ void UnlinkSegmentationFromLesion(const DataNode* segmentationNode); /* * @brief Remove the given segmentation from the set of already existing segmentations. * * @pre The given segmentation data node has to be valid (!nullptr). - * @throw mitk::Exception if the given segmentation data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given segmentation data node is invalid (==nullptr). * * @par segmentationNode The segmentation identifier is extracted from the given data node. The segmentation node has DICOM information from its parent node. */ void RemoveSegmentation(const DataNode* segmentationNode); /* * @brief Add a newly created control point to the set of already existing control points. A reference to the control point is added to the given data. * This function combines adding a control point and linking it, since a control point with no associated data is not allowed. * * @pre The given data node has to be valid (!nullptr). - * @throw mitk::Exception if the given data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given data node is invalid (==nullptr). * @pre The UID of the control point must not already exist for a control point instance. * @throw SemanticRelationException, if the UID of the control point already exists for a control point instance (this can be checked via 'InstanceExists'). * @pre The given control point must not already be contained in an existing control point interval. * @throw SemanticRelationException, if the given control point is already contained in an existing control point interval (this can be checked via 'CheckContainingControlPoint'). * @pre The given control point must contain the date of the given data node (if parameter 'checkConsistence = true'). * @throw SemanticRelationException, if the given control point does not contain the date of the given data node and 'checkConsistence = true' (this can be checked via 'ControlPointManager::InsideControlPoint'). * * @par dataNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @par controlPoint The control point instance to add. For a newly added control point always has "startDate = endDate". * @par checkConsistence If true, the function checks, whether the date of the data node actually lies inside the control point to link. */ void AddControlPointAndLinkData(const DataNode* dataNode, const SemanticTypes::ControlPoint& controlPoint, bool checkConsistence = true); /* * @brief Link the given data to an already existing control point and overwrite the start or end point of the control point. * * @pre The given data node has to be valid (!nullptr). - * @throw mitk::Exception if the given data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given data node is invalid (==nullptr). * @pre The UID of the control point has to exist for a control point instance. * @throw SemanticRelationException, if the UID of the control point does not exists for a control point instance (this can be checked via 'InstanceExists'). * @pre The given control point must contain the date of the given data node (if parameter 'checkConsistence = true'). * @throw SemanticRelationException, if the given control point does not contain the date of the given data node and 'checkConsistence = true' (this can be checked via 'ControlPointManager::InsideControlPoint'). * @pre The given control point must differ from the overwritten control point, but only either in the start point or in the end point. * It is not possible to overwrite a single control point from a single date and simultaneously changing both ends of the time period. * @throw SemanticRelationException, if the given control point does not differ from the overwritten control point or if the given control point differs in the start date and the end date from the overwritten control point. * @pre The given control point must not overlap with an already existing control point. * @throw SemanticRelationException, if the given control point overlaps with an already existing control point interval (this can be checked via 'CheckOverlappingControlPoint'). * * @par dataNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @par controlPoint The control point instance that overwrites an existing control point. * @par checkConsistence If true, the function checks, whether the date of the data node actually lies inside the control point to link. */ void OverwriteControlPointAndLinkData(const DataNode* dataNode, const SemanticTypes::ControlPoint& controlPoint, bool checkConsistence = true); /* * @brief Link the given data to an already existing control point. * * @pre The given data node has to be valid (!nullptr). - * @throw mitk::Exception if the given data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given data node is invalid (==nullptr). * @pre The UID of the control point has to exist for a control point instance. * @throw SemanticRelationException, if the UID of the control point does not exists for a control point instance (this can be checked via 'InstanceExists'). * @pre The given control point must contain the date of the given data node (if parameter 'checkConsistence = true'). * @throw SemanticRelationException, if the given control point does not contain the date of the given data node and 'checkConsistence = true' (this can be checked via 'ControlPointManager::InsideControlPoint'). * * @par dataNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @par controlPoint The control point instance to add link. * @par checkConsistence If true, the function checks, whether the date of the data node actually lies inside the control point to link. */ void LinkDataToControlPoint(const DataNode* dataNode, const SemanticTypes::ControlPoint& controlPoint, bool checkConsistence = true); /* * @brief Unlink the given image from the linked control point. * If data is unlinked from a control point, the function needs to check whether the control point is still linked to any other data: * - if not, the control point instance will be removed (has to be removed since a control point with no associated data is not allowed). * - if so, the function has to make sure that the control point instance is shortened to its minimum time period (e.g. moving the end point to an earlier date). * * @par dataNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. */ void UnlinkDataFromControlPoint(const DataNode* dataNode); /* * @brief Set the information type of the given image. * * @pre The given image data node has to be valid (!nullptr). - * @throw mitk::Exception if the given image data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given image data node is invalid (==nullptr). * @post If the information type instance did not exist before, it is now added. * * @par imageNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. * @par informationType An information type that identifies the corresponding information type instance. */ void AddInformationTypeToImage(const DataNode* imageNode, const SemanticTypes::InformationType& informationType); /* * @brief Remove the information type of the given image. * If the information type is removed, the function needs to check whether the information type is referenced by any other image: * - if not, the information type instance can be removed (has to be removed since an information type with no associated image is not allowed). * - if so, the information type is just removed from the given image. * * @pre The given image data node has to be valid (!nullptr). - * @throw mitk::Exception if the given image data node is invalid (==nullptr). + * @throw SemanticRelationException, if the given image data node is invalid (==nullptr). * * @par imageNode The current case identifier is extracted from the given data node, which contains DICOM information about the case. */ void RemoveInformationTypeFromImage(const DataNode* imageNode); private: // the relation storage serves as a storage accessor and can be sub-classed for custom demands std::shared_ptr m_RelationStorage; DataStorage::Pointer m_DataStorage; /* * @brief A vector that stores the currently registered observer of this observable subject. */ static std::vector m_ObserverVector; /* * @brief The SemanticRelations, as an example of an observable subject, notifies (updates) the observer with a given case ID. * The view's caseID was set before in the GUI. The parts of the view that observe changes in the semantic relations are only updated, * if the given case ID is equal to the observer's current caseID and thus the observer currently shows the semantic information of the given case. * * @par caseID The caseID that identifies the currently active patient / case. */ virtual void NotifyObserver(const mitk::SemanticTypes::CaseID& caseID) const override; /* * @brief Determine if the given control point contains images, which are connected to segmentations that represent the given lesion. * If the lesion or the control point are not correctly stored, the function returns false. * * @par caseID The current case identifier is defined by the given string. * @par lesion A Lesion with a UID that identifies the corresponding lesion instance. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. * * @return True, if the given control point contains data that is related to the given lesion; false otherwise. */ bool ControlPointContainsLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion, const SemanticTypes::ControlPoint& controlPoint) const; /* * @brief Determine if the given control point contains images, which refer to the given information type. * If the information type or the control point are not correctly stored, the function returns false. * * @par caseID The current case identifier is defined by the given string. * @par informationType An information type that identifies the corresponding information type instance. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. * * @return True, if the given control point contains data that is related to the given information type; false otherwise. */ bool ControlPointContainsInformationType(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType, const SemanticTypes::ControlPoint& controlPoint) const; /* * @brief Check if the given control point overlaps with an already existing control point. * If the UID already exists, then the given control point may overlap with the previous or next control point, * because the start point or the end point of the given control point might be modified (different to the same control point * that is stored at the moment). * If the UID does not already exists, the previous and next control point are found by comparing the dates of the already * existing control points and the given control point. * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. */ bool CheckOverlappingControlPoint(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint); /* * @brief Check if the given control point is contained within an already existing control point. * If the UID already exists, then the given control point is contained in this same control point. * However, the function does not check if the given control point is really contained (just compares UID) in this case. * If the UID does not already exist, the already existing control points are tested to see if they contain the * given control point. * * @par caseID The current case identifier is defined by the given string. * @par controlPoint A control point with a UID that identifies the corresponding control point instance. */ bool CheckContainingControlPoint(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint); }; } // namespace mitk #endif // MITKSEMANTICRELATIONS_H diff --git a/Modules/SemanticRelations/src/mitkDICOMHelper.cpp b/Modules/SemanticRelations/src/mitkDICOMHelper.cpp index 1820278a67..cc05dc88cd 100644 --- a/Modules/SemanticRelations/src/mitkDICOMHelper.cpp +++ b/Modules/SemanticRelations/src/mitkDICOMHelper.cpp @@ -1,162 +1,163 @@ /*=================================================================== 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. ===================================================================*/ // semantic relations module #include "mitkDICOMHelper.h" +#include "mitkSemanticRelationException.h" #include "mitkUIDGeneratorBoost.h" // mitk core #include // c++ #include mitk::SemanticTypes::Date GetDateFromString(const std::string& dateAsString); mitk::SemanticTypes::CaseID mitk::GetCaseIDFromDataNode(const mitk::DataNode* dataNode) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } mitk::BaseData* baseData = dataNode->GetData(); if (nullptr == baseData) { - mitkThrow() << "No valid base data."; + mitkThrowException(SemanticRelationException) << "No valid base data."; } // extract suitable DICOM tag to use as the case id // two alternatives can be used: // - DICOM tag "0x0010, 0x0010" is PatientName // - DICOM tag "0x0010, 0x0020" is PatientID // in the current implementation the PatientID (0x0010, 0x0010) is used mitk::BaseProperty* dicomTag = baseData->GetProperty(mitk::GeneratePropertyNameForDICOMTag(0x0010, 0x0010).c_str()); if (nullptr == dicomTag) { - mitkThrow() << "Not a valid DICOM property."; + mitkThrowException(SemanticRelationException) << "Not a valid DICOM property."; } std::string dicomTagAsString = dicomTag->GetValueAsString(); return dicomTagAsString; } mitk::SemanticTypes::ID mitk::GetIDFromDataNode(const mitk::DataNode* dataNode) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } mitk::BaseData* baseData = dataNode->GetData(); if (nullptr == baseData) { - mitkThrow() << "No valid base data."; + mitkThrowException(SemanticRelationException) << "No valid base data."; } // extract suitable DICOM tag to use as the data node id // DICOM tag "0x0020, 0x000e" is SeriesInstanceUID mitk::BaseProperty* dicomTag = baseData->GetProperty(mitk::GeneratePropertyNameForDICOMTag(0x0020, 0x000e).c_str()); if (nullptr == dicomTag) { - mitkThrow() << "Not a valid DICOM property."; + mitkThrowException(SemanticRelationException) << "Not a valid DICOM property."; } std::string dicomTagAsString = dicomTag->GetValueAsString(); return dicomTagAsString; } mitk::SemanticTypes::Date mitk::GetDICOMDateFromDataNode(const mitk::DataNode* dataNode) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } mitk::BaseData* baseData = dataNode->GetData(); if (nullptr == baseData) { - mitkThrow() << "No valid base data."; + mitkThrowException(SemanticRelationException) << "No valid base data."; } // extract suitable DICOM tag to use as the data node id // DICOM tag "0x0008, 0x0022" is AcquisitionDate mitk::BaseProperty* acquisitionDateProperty = baseData->GetProperty(mitk::GeneratePropertyNameForDICOMTag(0x0008, 0x0022).c_str()); if (nullptr == acquisitionDateProperty) { - mitkThrow() << "Not a valid DICOM property."; + mitkThrowException(SemanticRelationException) << "Not a valid DICOM property."; } std::string acquisitionDateAsString = acquisitionDateProperty->GetValueAsString(); return GetDateFromString(acquisitionDateAsString); } mitk::SemanticTypes::InformationType mitk::GetDICOMModalityFromDataNode(const mitk::DataNode* dataNode) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } mitk::BaseData* baseData = dataNode->GetData(); if (nullptr == baseData) { - mitkThrow() << "No valid base data."; + mitkThrowException(SemanticRelationException) << "No valid base data."; } // extract suitable DICOM tag to use as the information type // DICOM tag "0x0008, 0x0060" is Modality mitk::BaseProperty* dicomTag = baseData->GetProperty(mitk::GeneratePropertyNameForDICOMTag(0x0008, 0x0060).c_str()); if (nullptr == dicomTag) { - mitkThrow() << "Not a valid DICOM property."; + mitkThrowException(SemanticRelationException) << "Not a valid DICOM property."; } std::string dicomTagAsString = dicomTag->GetValueAsString(); return dicomTagAsString; } std::string mitk::TrimDICOM(const std::string& identifier) { if (identifier.empty()) { return identifier; } // leading whitespace std::size_t first = identifier.find_first_not_of(' '); if (std::string::npos == first) { return ""; } // trailing whitespace std::size_t last = identifier.find_last_not_of(' '); return identifier.substr(first, last - first + 1); } mitk::SemanticTypes::Date GetDateFromString(const std::string& dateAsString) { if (dateAsString.size() != 8) // string does not represent a DICOM date { return mitk::SemanticTypes::Date(); } mitk::SemanticTypes::Date date; date.UID = mitk::UIDGeneratorBoost::GenerateUID(); // date expected to be YYYYMMDD (8 characters) date.year = std::strtoul(dateAsString.substr(0, 4).c_str(), nullptr, 10); date.month = std::strtoul(dateAsString.substr(4, 2).c_str(), nullptr, 10); date.day = std::strtoul(dateAsString.substr(6, 2).c_str(), nullptr, 10); return date; } diff --git a/Modules/SemanticRelations/src/mitkSemanticRelations.cpp b/Modules/SemanticRelations/src/mitkSemanticRelations.cpp index 1dcdfabebe..4e33df43b7 100644 --- a/Modules/SemanticRelations/src/mitkSemanticRelations.cpp +++ b/Modules/SemanticRelations/src/mitkSemanticRelations.cpp @@ -1,1002 +1,1002 @@ /*=================================================================== 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. ===================================================================*/ // semantic relations module #include "mitkSemanticRelations.h" #include "mitkSemanticRelationException.h" #include "mitkNodePredicates.h" #include "mitkUIDGeneratorBoost.h" // multi label module #include // c++ #include std::vector mitk::SemanticRelations::m_ObserverVector; mitk::SemanticRelations::SemanticRelations(mitk::DataStorage::Pointer dataStorage) : m_DataStorage(dataStorage) { m_RelationStorage = std::make_shared(); m_RelationStorage->SetDataStorage(m_DataStorage); } mitk::SemanticRelations::~SemanticRelations() { // nothing here } void mitk::SemanticRelations::AddObserver(ISemanticRelationsObserver* observer) { std::vector::iterator existingObserver = std::find(m_ObserverVector.begin(), m_ObserverVector.end(), observer); if (existingObserver != m_ObserverVector.end()) { // no need to add the already existing observer return; } m_ObserverVector.push_back(observer); } void mitk::SemanticRelations::RemoveObserver(ISemanticRelationsObserver* observer) { m_ObserverVector.erase(std::remove(m_ObserverVector.begin(), m_ObserverVector.end(), observer), m_ObserverVector.end()); } /************************************************************************/ /* functions to get instances / attributes */ /************************************************************************/ mitk::SemanticRelations::LesionVector mitk::SemanticRelations::GetAllLesionsOfCase(const SemanticTypes::CaseID& caseID) const { return m_RelationStorage->GetAllLesionsOfCase(caseID); } mitk::SemanticRelations::LesionVector mitk::SemanticRelations::GetAllLesionsOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const { LesionVector allLesions = GetAllLesionsOfCase(caseID); // filter the lesions: use only those, where the associated data is connected to image data that refers to the given control point using a lambda function auto lambda = [&caseID, &controlPoint, this](const SemanticTypes::Lesion& lesion) { return !ControlPointContainsLesion(caseID, lesion, controlPoint); }; allLesions.erase(std::remove_if(allLesions.begin(), allLesions.end(), lambda), allLesions.end()); return allLesions; } mitk::SemanticRelations::LesionClassVector mitk::SemanticRelations::GetAllLesionClassesOfCase(const SemanticTypes::CaseID& caseID) const { LesionVector allLesionsOfCase = GetAllLesionsOfCase(caseID); LesionClassVector allLesionClassesOfCase; for (const auto& lesion : allLesionsOfCase) { allLesionClassesOfCase.push_back(lesion.lesionClass); } // remove duplicate entries auto lessThan = [](const SemanticTypes::LesionClass& lesionClassL, const SemanticTypes::LesionClass& lesionClassR) { return lesionClassL.UID < lesionClassR.UID; }; auto equal = [](const SemanticTypes::LesionClass& lesionClassL, const SemanticTypes::LesionClass& lesionClassR) { return lesionClassL.UID == lesionClassR.UID; }; std::sort(allLesionClassesOfCase.begin(), allLesionClassesOfCase.end(), lessThan); allLesionClassesOfCase.erase(std::unique(allLesionClassesOfCase.begin(), allLesionClassesOfCase.end(), equal), allLesionClassesOfCase.end()); return allLesionClassesOfCase; } mitk::SemanticRelations::LesionVector mitk::SemanticRelations::GetAllLesionsInImage(const DataNode* imageNode) const { if (nullptr == imageNode) { - mitkThrow() << "Not a valid image data node."; + mitkThrowException(SemanticRelationException) << "Not a valid image data node."; } if (m_DataStorage.IsNull()) { - mitkThrow() << "Not a valid data storage."; + mitkThrowException(SemanticRelationException) << "Not a valid data storage."; } LesionVector allLesionsInImage; // get child nodes of the current node with the segmentation predicate DataStorage::SetOfObjects::ConstPointer segmentationNodes = m_DataStorage->GetDerivations(imageNode, NodePredicates::GetSegmentationPredicate(), false); for (auto it = segmentationNodes->Begin(); it != segmentationNodes->End(); ++it) { DataNode* segmentationNode = it->Value(); try { SemanticTypes::Lesion representedLesion = GetRepresentedLesion(segmentationNode); allLesionsInImage.push_back(representedLesion); } catch (const SemanticRelationException&) { continue; } } return allLesionsInImage; } mitk::SemanticTypes::Lesion mitk::SemanticRelations::GetRepresentedLesion(const DataNode* segmentationNode) const { if (nullptr == segmentationNode) { - mitkThrow() << "Not a valid segmentation data node."; + mitkThrowException(SemanticRelationException) << "Not a valid segmentation data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(segmentationNode); SemanticTypes::ID segmentationID = GetIDFromDataNode(segmentationNode); SemanticTypes::Lesion representedLesion = m_RelationStorage->GetRepresentedLesion(caseID, segmentationID); if (representedLesion.UID.empty()) { mitkThrowException(SemanticRelationException) << "Could not find a represented lesion instance for the given segmentation node " << segmentationNode->GetName(); } else { return representedLesion; } } bool mitk::SemanticRelations::IsRepresentingALesion(const DataNode* segmentationNode) const { try { SemanticTypes::Lesion representedLesion = GetRepresentedLesion(segmentationNode); return true; } - catch (const SemanticRelationException&) + catch (const Exception&) { return false; } } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetAllSegmentationsOfCase(const SemanticTypes::CaseID& caseID) const { if (m_DataStorage.IsNull()) { - mitkThrow() << "Not a valid data storage."; + mitkThrowException(SemanticRelationException) << "Not a valid data storage."; } return m_RelationStorage->GetAllSegmentationsOfCase(caseID); } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetAllSegmentationsOfLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const { if (m_DataStorage.IsNull()) { - mitkThrow() << "Not a valid data storage."; + mitkThrowException(SemanticRelationException) << "Not a valid data storage."; } if (InstanceExists(caseID, lesion)) { // lesion exists, retrieve all case segmentations from the storage DataNodeVector allSegmentationsOfLesion = GetAllSegmentationsOfCase(caseID); // filter all segmentations: check for semantic relation with the given lesion using a lambda function auto lambda = [&lesion, this](DataNode::Pointer segmentation) { try { SemanticTypes::Lesion representedLesion = GetRepresentedLesion(segmentation); return lesion.UID != representedLesion.UID; } catch (const SemanticRelationException&) { return true; } }; allSegmentationsOfLesion.erase(std::remove_if(allSegmentationsOfLesion.begin(), allSegmentationsOfLesion.end(), lambda), allSegmentationsOfLesion.end()); return allSegmentationsOfLesion; } else { mitkThrowException(SemanticRelationException) << "Could not find an existing lesion instance for the given caseID " << caseID << " and lesion " << lesion.UID << "."; } } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetAllImagesOfCase(const SemanticTypes::CaseID& caseID) const { if (m_DataStorage.IsNull()) { - mitkThrow() << "Not a valid data storage."; + mitkThrowException(SemanticRelationException) << "Not a valid data storage."; } return m_RelationStorage->GetAllImagesOfCase(caseID); } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetAllImagesOfLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const { if (m_DataStorage.IsNull()) { - mitkThrow() << "Not a valid data storage."; + mitkThrowException(SemanticRelationException) << "Not a valid data storage."; } DataNodeVector allImagesOfLesion; // 1. get all segmentations that define the lesion // 2. retrieve the parent node (source) of the found segmentation node DataNodeVector allSegmentationsOfLesion = GetAllSegmentationsOfLesion(caseID, lesion); for (const auto& segmentationNode : allSegmentationsOfLesion) { // get parent node of the current segmentation node with the node predicate DataStorage::SetOfObjects::ConstPointer parentNodes = m_DataStorage->GetSources(segmentationNode, NodePredicates::GetImagePredicate(), false); for (auto it = parentNodes->Begin(); it != parentNodes->End(); ++it) { DataNode::Pointer dataNode = it->Value(); allImagesOfLesion.push_back(it->Value()); } } std::sort(allImagesOfLesion.begin(), allImagesOfLesion.end()); allImagesOfLesion.erase(std::unique(allImagesOfLesion.begin(), allImagesOfLesion.end()), allImagesOfLesion.end()); return allImagesOfLesion; } bool mitk::SemanticRelations::InstanceExists(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const { LesionVector allLesions = GetAllLesionsOfCase(caseID); // filter all lesions: check for equality with the given lesion using a lambda function auto lambda = [&lesion](const SemanticTypes::Lesion& currentLesion) { return currentLesion.UID == lesion.UID; }; const auto existingLesion = std::find_if(allLesions.begin(), allLesions.end(), lambda); if (existingLesion != allLesions.end()) { return true; } else { return false; } } mitk::SemanticRelations::ControlpointVector mitk::SemanticRelations::GetAllControlPointsOfCase(const SemanticTypes::CaseID& caseID) const { return m_RelationStorage->GetAllControlPointsOfCase(caseID); } mitk::SemanticRelations::ControlpointVector mitk::SemanticRelations::GetAllControlPointsOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) const { ControlpointVector allControlPoints = GetAllControlPointsOfCase(caseID); // filter the control points: use only those, where the associated image data has a segmentation that refers to the given lesion using a lambda function auto lambda = [&caseID, &lesion, this](const SemanticTypes::ControlPoint& controlPoint) { return !ControlPointContainsLesion(caseID, lesion, controlPoint); }; allControlPoints.erase(std::remove_if(allControlPoints.begin(), allControlPoints.end(), lambda), allControlPoints.end()); return allControlPoints; } mitk::SemanticRelations::ControlpointVector mitk::SemanticRelations::GetAllControlPointsOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType) const { ControlpointVector allControlPoints = GetAllControlPointsOfCase(caseID); // filter the control points: use only those, where the associated image data refers to the given information type using a lambda function auto lambda = [&caseID, &informationType, this](const SemanticTypes::ControlPoint& controlPoint) { return !ControlPointContainsInformationType(caseID, informationType, controlPoint); }; allControlPoints.erase(std::remove_if(allControlPoints.begin(), allControlPoints.end(), lambda), allControlPoints.end()); return allControlPoints; } mitk::SemanticTypes::ControlPoint mitk::SemanticRelations::GetControlPointOfData(const DataNode* dataNode) const { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(dataNode); SemanticTypes::ID dataNodeID = GetIDFromDataNode(dataNode); return m_RelationStorage->GetControlPointOfData(caseID, dataNodeID); } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetAllDataOfControlPoint(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const { if (InstanceExists(caseID, controlPoint)) { // control point exists, retrieve all images from the storage DataNodeVector allDataOfControlPoint = GetAllImagesOfCase(caseID); // filter all images to remove the ones with a different control point using a lambda function auto lambda = [&controlPoint, this](DataNode::Pointer imageNode) { return controlPoint.UID != GetControlPointOfData(imageNode).UID; }; allDataOfControlPoint.erase(std::remove_if(allDataOfControlPoint.begin(), allDataOfControlPoint.end(), lambda), allDataOfControlPoint.end()); return allDataOfControlPoint; } else { mitkThrowException(SemanticRelationException) << "Could not find an existing control point instance for the given caseID " << caseID << " and control point " << controlPoint.UID << "."; } } bool mitk::SemanticRelations::InstanceExists(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const { ControlpointVector allControlPoints = GetAllControlPointsOfCase(caseID); // filter all control points: check for equality with the given control point using a lambda function auto lambda = [&controlPoint](const SemanticTypes::ControlPoint& currentControlPoint) { return currentControlPoint.UID == controlPoint.UID; }; const auto existingControlPoint = std::find_if(allControlPoints.begin(), allControlPoints.end(), lambda); if (existingControlPoint != allControlPoints.end()) { return true; } else { return false; } } mitk::SemanticRelations::InformationTypeVector mitk::SemanticRelations::GetAllInformationTypesOfCase(const SemanticTypes::CaseID& caseID) const { return m_RelationStorage->GetAllInformationTypesOfCase(caseID); } mitk::SemanticRelations::InformationTypeVector mitk::SemanticRelations::GetAllInformationTypesOfCase(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) const { InformationTypeVector allInformationTypes = GetAllInformationTypesOfCase(caseID); // filter the information types: use only those, where the associated data refers to the given control point using a lambda function auto lambda = [&caseID, &controlPoint, this](const SemanticTypes::InformationType& informationType) { return !ControlPointContainsInformationType(caseID, informationType, controlPoint); }; allInformationTypes.erase(std::remove_if(allInformationTypes.begin(), allInformationTypes.end(), lambda), allInformationTypes.end()); return allInformationTypes; } mitk::SemanticTypes::InformationType mitk::SemanticRelations::GetInformationTypeOfImage(const DataNode* imageNode) const { if (nullptr == imageNode) { - mitkThrow() << "Not a valid image data node."; + mitkThrowException(SemanticRelationException) << "Not a valid image data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(imageNode); SemanticTypes::ID imageID = GetIDFromDataNode(imageNode); return m_RelationStorage->GetInformationTypeOfImage(caseID, imageID); } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetAllDataOfInformationType(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType) const { if (InstanceExists(caseID, informationType)) { // information type exists, retrieve all images from the storage DataNodeVector allDataOfInformationType = GetAllImagesOfCase(caseID); // filter all images to remove the ones with a different information type using a lambda function auto lambda = [&informationType, this](DataNode::Pointer imageNode) { return informationType != GetInformationTypeOfImage(imageNode); }; allDataOfInformationType.erase(std::remove_if(allDataOfInformationType.begin(), allDataOfInformationType.end(), lambda), allDataOfInformationType.end()); return allDataOfInformationType; } else { mitkThrowException(SemanticRelationException) << "Could not find an existing information type for the given caseID " << caseID << " and information type " << informationType; } } mitk::SemanticRelations::DataNodeVector mitk::SemanticRelations::GetFilteredData(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint, const SemanticTypes::InformationType& informationType) const { if (InstanceExists(caseID, controlPoint)) { if (InstanceExists(caseID, informationType)) { // control point exists, information type exists, retrieve all images from the storage DataNodeVector allImagesOfCase = GetAllImagesOfCase(caseID); // filter all images to remove the ones with a different control point and information type using a lambda function auto lambda = [&controlPoint, &informationType, this](DataNode::Pointer imageNode) { return (informationType != GetInformationTypeOfImage(imageNode)) || (controlPoint != GetControlPointOfData(imageNode)); }; allImagesOfCase.erase(std::remove_if(allImagesOfCase.begin(), allImagesOfCase.end(), lambda), allImagesOfCase.end()); return allImagesOfCase; } else { mitkThrowException(SemanticRelationException) << "Could not find an existing information type for the given caseID " << caseID << " and information type " << informationType; } } else { mitkThrowException(SemanticRelationException) << "Could not find an existing control point for the given caseID " << caseID << " and control point " << controlPoint.UID; } } bool mitk::SemanticRelations::InstanceExists(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType) const { InformationTypeVector allInformationTypes = GetAllInformationTypesOfCase(caseID); // filter all information types: check for equality with the given information type using a lambda function auto lambda = [&informationType](const SemanticTypes::InformationType& currentInformationType) { return currentInformationType == informationType; }; const auto existingInformationType = std::find_if(allInformationTypes.begin(), allInformationTypes.end(), lambda); if (existingInformationType != allInformationTypes.end()) { return true; } else { return false; } } std::vector mitk::SemanticRelations::GetAllCaseIDs() const { return m_RelationStorage->GetAllCaseIDs(); } /************************************************************************/ /* functions to add / remove instances / attributes */ /************************************************************************/ void mitk::SemanticRelations::AddImage(const mitk::DataNode* imageNode) { if (nullptr == imageNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } // continue with a valid data node SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(imageNode); SemanticTypes::ID nodeID = GetIDFromDataNode(imageNode); m_RelationStorage->AddCase(caseID); m_RelationStorage->AddImage(caseID, nodeID); SemanticTypes::InformationType informationType = GetDICOMModalityFromDataNode(imageNode); AddInformationTypeToImage(imageNode, informationType); // find the correct control point for this image std::vector allControlPoints = GetAllControlPointsOfCase(caseID); if (allControlPoints.empty()) { // no stored control point // create a new control point for the image data SemanticTypes::ControlPoint newControlPoint = GenerateControlPoint(imageNode); AddControlPointAndLinkData(imageNode, newControlPoint); NotifyObserver(caseID); return; } // some control points already exist - find a control point where the date of the data node fits in SemanticTypes::ControlPoint fittingControlPoint = FindFittingControlPoint(imageNode, allControlPoints); if (fittingControlPoint.UID.empty()) { // did not find a fitting control point, although some control points already exist // need to see if a close control point can be extended SemanticTypes::ControlPoint extendedControlPoint = ExtendClosestControlPoint(imageNode, allControlPoints); if (extendedControlPoint.UID.empty()) { // closest control point can not be extended // create a new control point for the image data SemanticTypes::ControlPoint newControlPoint = GenerateControlPoint(imageNode); AddControlPointAndLinkData(imageNode, newControlPoint); } else { // found a control point that was extended OverwriteControlPointAndLinkData(imageNode, extendedControlPoint); } } else { // found a fitting control point LinkDataToControlPoint(imageNode, fittingControlPoint); } NotifyObserver(caseID); } void mitk::SemanticRelations::RemoveImage(const mitk::DataNode* imageNode) { if (nullptr == imageNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } // continue with a valid data node SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(imageNode); SemanticTypes::ID nodeID = GetIDFromDataNode(imageNode); RemoveInformationTypeFromImage(imageNode); UnlinkDataFromControlPoint(imageNode); m_RelationStorage->RemoveImage(caseID, nodeID); NotifyObserver(caseID); } void mitk::SemanticRelations::AddLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) { if (InstanceExists(caseID, lesion)) { mitkThrowException(SemanticRelationException) << "The lesion " << lesion.UID << " to add already exists for the given case."; } else { m_RelationStorage->AddLesion(caseID, lesion); NotifyObserver(caseID); } } void mitk::SemanticRelations::OverwriteLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) { if (InstanceExists(caseID, lesion)) { m_RelationStorage->OverwriteLesion(caseID, lesion); NotifyObserver(caseID); } else { mitkThrowException(SemanticRelationException) << "The lesion " << lesion.UID << " to overwrite does not exist for the given case."; } } void mitk::SemanticRelations::AddLesionAndLinkSegmentation(const DataNode* segmentationNode, const SemanticTypes::Lesion& lesion) { if (nullptr == segmentationNode) { - mitkThrow() << "Not a valid segmentation data node."; + mitkThrowException(SemanticRelationException) << "Not a valid segmentation data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(segmentationNode); AddLesion(caseID, lesion); LinkSegmentationToLesion(segmentationNode, lesion); NotifyObserver(caseID); } void mitk::SemanticRelations::RemoveLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion) { if (InstanceExists(caseID, lesion)) { DataNodeVector allSegmentationsOfLesion = GetAllSegmentationsOfLesion(caseID, lesion); if (allSegmentationsOfLesion.empty()) { // no more segmentations are linked to the specific lesion // the lesion can be removed from the storage m_RelationStorage->RemoveLesion(caseID, lesion); NotifyObserver(caseID); } else { mitkThrowException(SemanticRelationException) << "The lesion " << lesion.UID << " to remove is still referred to by a segmentation node. Lesion will not be removed."; } } else { mitkThrowException(SemanticRelationException) << "The lesion " << lesion.UID << " to remove does not exist for the given case."; } } void mitk::SemanticRelations::AddSegmentation(const mitk::DataNode* segmentationNode, const mitk::DataNode* parentNode) { if (nullptr == segmentationNode) { - mitkThrow() << "Not a valid segmentation data node."; + mitkThrowException(SemanticRelationException) << "Not a valid segmentation data node."; } if (nullptr == parentNode) { - mitkThrow() << "Not a valid parent data node."; + mitkThrowException(SemanticRelationException) << "Not a valid parent data node."; } // continue with a valid data node SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(segmentationNode); SemanticTypes::ID segmentationNodeID = GetIDFromDataNode(segmentationNode); SemanticTypes::ID parentNodeID = GetIDFromDataNode(parentNode); m_RelationStorage->AddSegmentation(caseID, segmentationNodeID, parentNodeID); NotifyObserver(caseID); } void mitk::SemanticRelations::LinkSegmentationToLesion(const DataNode* segmentationNode, const SemanticTypes::Lesion& lesion) { if (nullptr == segmentationNode) { - mitkThrow() << "Not a valid segmentation data node."; + mitkThrowException(SemanticRelationException) << "Not a valid segmentation data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(segmentationNode); if (InstanceExists(caseID, lesion)) { SemanticTypes::ID segmentationID = GetIDFromDataNode(segmentationNode); m_RelationStorage->LinkSegmentationToLesion(caseID, segmentationID, lesion); NotifyObserver(caseID); } else { mitkThrowException(SemanticRelationException) << "The lesion " << lesion.UID << " to link does not exist for the given case."; } } void mitk::SemanticRelations::UnlinkSegmentationFromLesion(const DataNode* segmentationNode) { if (nullptr == segmentationNode) { - mitkThrow() << "Not a valid segmentation data node."; + mitkThrowException(SemanticRelationException) << "Not a valid segmentation data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(segmentationNode); SemanticTypes::ID segmentationID = GetIDFromDataNode(segmentationNode); m_RelationStorage->UnlinkSegmentationFromLesion(caseID, segmentationID); NotifyObserver(caseID); } void mitk::SemanticRelations::RemoveSegmentation(const mitk::DataNode* segmentationNode) { if (nullptr == segmentationNode) { - mitkThrow() << "Not a valid segmentation data node."; + mitkThrowException(SemanticRelationException) << "Not a valid segmentation data node."; } // continue with a valid data node SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(segmentationNode); SemanticTypes::ID segmentationNodeID = GetIDFromDataNode(segmentationNode); m_RelationStorage->RemoveSegmentation(caseID, segmentationNodeID); NotifyObserver(caseID); } void mitk::SemanticRelations::AddControlPointAndLinkData(const DataNode* dataNode, const SemanticTypes::ControlPoint& controlPoint, bool checkConsistence) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(dataNode); if (InstanceExists(caseID, controlPoint)) { mitkThrowException(SemanticRelationException) << "The control point " << controlPoint.UID << " to add already exists for the given case. \n Use 'LinkDataToControlPoint' instead."; } // control point does not already exist bool contained = CheckContainingControlPoint(caseID, controlPoint); if (contained) { mitkThrowException(SemanticRelationException) << "The control point " << controlPoint.UID << " to add is already contained in an existing control point."; } // control point is not already contained in an existing control point if (checkConsistence) { // to check the consistency, the control point manager checks, if the date extracted from the data node is inside the given control point bool insideControlPoint = InsideControlPoint(dataNode, controlPoint); if (!insideControlPoint) { mitkThrowException(SemanticRelationException) << "The control point " << controlPoint.UID << " to add does not contain the date of the given data node."; } } m_RelationStorage->AddControlPoint(caseID, controlPoint); LinkDataToControlPoint(dataNode, controlPoint, checkConsistence); } void mitk::SemanticRelations::OverwriteControlPointAndLinkData(const DataNode* dataNode, const SemanticTypes::ControlPoint& controlPoint, bool checkConsistence) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(dataNode); ControlpointVector allControlPoints = GetAllControlPointsOfCase(caseID); const auto existingControlPoint = std::find_if(allControlPoints.begin(), allControlPoints.end(), [&controlPoint](const SemanticTypes::ControlPoint& currentControlPoint) { return currentControlPoint.UID == controlPoint.UID; }); if (existingControlPoint != allControlPoints.end()) { // control point does already exist if (checkConsistence) { // to check the consistency, the control point manager checks, if the date extracted from the data node is inside the given control point bool insideControlPoint = InsideControlPoint(dataNode, controlPoint); if (!insideControlPoint) { mitkThrowException(SemanticRelationException) << "The overwriting control point " << controlPoint.UID << " does not contain the date of the given data node."; } } bool sameStartPoint = controlPoint.startPoint == (*existingControlPoint).startPoint; bool sameEndPoint = controlPoint.endPoint == (*existingControlPoint).endPoint; if (!sameStartPoint && !sameEndPoint) { mitkThrowException(SemanticRelationException) << "The overwriting control point " << controlPoint.UID << " differs in the start date and in the end date from the original control point."; } if (sameStartPoint && sameEndPoint) { mitkThrowException(SemanticRelationException) << "The overwriting control point " << controlPoint.UID << " does not differ from the original control point."; } bool overlapping = CheckOverlappingControlPoint(caseID, controlPoint); if (overlapping) { mitkThrowException(SemanticRelationException) << "The overwriting control point " << controlPoint.UID << " overlaps with an already existing control point"; } else { m_RelationStorage->OverwriteControlPoint(caseID, controlPoint); LinkDataToControlPoint(dataNode, controlPoint, checkConsistence); } } else { mitkThrowException(SemanticRelationException) << "The control point " << controlPoint.UID << " to overwrite does not exist for the given case. \n Use 'AddControlPointAndLinkData' instead."; } } void mitk::SemanticRelations::LinkDataToControlPoint(const DataNode* dataNode, const SemanticTypes::ControlPoint& controlPoint, bool checkConsistence) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(dataNode); if (InstanceExists(caseID, controlPoint)) { SemanticTypes::ID dataID = GetIDFromDataNode(dataNode); // control point does already exist if (checkConsistence) { // to check the consistency, the control point manager checks, if the date extracted from the data node is inside the given control point bool insideControlPoint = InsideControlPoint(dataNode, controlPoint); if (!insideControlPoint) { mitkThrowException(SemanticRelationException) << "The data to link does not lie inside the given control point " << controlPoint.UID << " ."; } } m_RelationStorage->LinkDataToControlPoint(caseID, dataID, controlPoint); } else { mitkThrowException(SemanticRelationException) << "The control point " << controlPoint.UID << " to link does not exist for the given case."; } } void mitk::SemanticRelations::UnlinkDataFromControlPoint(const DataNode* dataNode) { if (nullptr == dataNode) { - mitkThrow() << "Not a valid data node."; + mitkThrowException(SemanticRelationException) << "Not a valid data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(dataNode); SemanticTypes::ID dataID = GetIDFromDataNode(dataNode); mitk::SemanticTypes::ControlPoint controlPoint = m_RelationStorage->GetControlPointOfData(caseID, dataID); m_RelationStorage->UnlinkDataFromControlPoint(caseID, dataID); DataNodeVector allDataOfControlPoint = GetAllDataOfControlPoint(caseID, controlPoint); if (allDataOfControlPoint.empty()) { // no more data is linked to the specific control point // the control point can be removed from the storage m_RelationStorage->RemoveControlPointFromCase(caseID, controlPoint); } else { // some data is still linked to this control point // the control point can not be removed, but has to be adjusted to fit the remaining data SemanticTypes::ControlPoint adjustedControlPoint = GenerateControlPoint(allDataOfControlPoint); // set the UIDs to be the same, so that all references still work adjustedControlPoint.UID = controlPoint.UID; adjustedControlPoint.startPoint.UID = controlPoint.startPoint.UID; adjustedControlPoint.endPoint.UID = controlPoint.endPoint.UID; m_RelationStorage->OverwriteControlPoint(caseID, adjustedControlPoint); } } void mitk::SemanticRelations::AddInformationTypeToImage(const DataNode* imageNode, const SemanticTypes::InformationType& informationType) { if (nullptr == imageNode) { - mitkThrow() << "Not a valid image data node."; + mitkThrowException(SemanticRelationException) << "Not a valid image data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(imageNode); SemanticTypes::ID imageID = GetIDFromDataNode(imageNode); m_RelationStorage->AddInformationTypeToImage(caseID, imageID, informationType); } void mitk::SemanticRelations::RemoveInformationTypeFromImage(const DataNode* imageNode) { if (nullptr == imageNode) { - mitkThrow() << "Not a valid image data node."; + mitkThrowException(SemanticRelationException) << "Not a valid image data node."; } SemanticTypes::CaseID caseID = GetCaseIDFromDataNode(imageNode); SemanticTypes::ID imageID = GetIDFromDataNode(imageNode); SemanticTypes::InformationType originalInformationType = m_RelationStorage->GetInformationTypeOfImage(caseID, imageID); m_RelationStorage->RemoveInformationTypeFromImage(caseID, imageID); // check for further references to the removed information type std::vector allImageIDsVectorValue = m_RelationStorage->GetAllImageIDsOfCase(caseID); for (const auto otherImageID : allImageIDsVectorValue) { SemanticTypes::InformationType otherInformationType = m_RelationStorage->GetInformationTypeOfImage(caseID, otherImageID); if (otherInformationType == originalInformationType) { // found the information type in another image -> cannot remove the information type from the case return; } } // given information type was not referred by any other image of the case -> the information type can be removed from the case m_RelationStorage->RemoveInformationTypeFromCase(caseID, originalInformationType); } /************************************************************************/ /* private functions */ /************************************************************************/ void mitk::SemanticRelations::NotifyObserver(const mitk::SemanticTypes::CaseID& caseID) const { for (auto& observer : m_ObserverVector) { observer->Update(caseID); } } bool mitk::SemanticRelations::ControlPointContainsLesion(const SemanticTypes::CaseID& caseID, const SemanticTypes::Lesion& lesion, const SemanticTypes::ControlPoint& controlPoint) const { DataNodeVector allDataOfLesion; try { allDataOfLesion = GetAllImagesOfLesion(caseID, lesion); } catch (const SemanticRelationException&) { // error retrieving image data; lesion has to be outside the control point return false; } DataNodeVector allDataOfControlPoint; try { allDataOfControlPoint = GetAllDataOfControlPoint(caseID, controlPoint); } catch (const SemanticRelationException&) { // error retrieving control point data; lesion has to be outside the control point return false; } std::sort(allDataOfLesion.begin(), allDataOfLesion.end()); std::sort(allDataOfControlPoint.begin(), allDataOfControlPoint.end()); DataNodeVector allDataIntersection; // set intersection removes duplicated nodes, since 'allDataOfControlPoint' only contains at most one of each node std::set_intersection(allDataOfLesion.begin(), allDataOfLesion.end(), allDataOfControlPoint.begin(), allDataOfControlPoint.end(), std::back_inserter(allDataIntersection)); // if the vector of intersecting data is empty, the control point does not contain the lesion return !allDataIntersection.empty(); } bool mitk::SemanticRelations::ControlPointContainsInformationType(const SemanticTypes::CaseID& caseID, const SemanticTypes::InformationType& informationType, const SemanticTypes::ControlPoint& controlPoint) const { DataNodeVector allDataOfInformationType; try { allDataOfInformationType = GetAllDataOfInformationType(caseID, informationType); } catch (const SemanticRelationException&) { // error retrieving image data; information type has to be outside the control point return false; } DataNodeVector allDataOfControlPoint; try { allDataOfControlPoint = GetAllDataOfControlPoint(caseID, controlPoint); } catch (const SemanticRelationException&) { // error retrieving control point data; information type has to be outside the control point return false; } std::sort(allDataOfInformationType.begin(), allDataOfInformationType.end()); std::sort(allDataOfControlPoint.begin(), allDataOfControlPoint.end()); DataNodeVector allDataIntersection; std::set_intersection(allDataOfInformationType.begin(), allDataOfInformationType.end(), allDataOfControlPoint.begin(), allDataOfControlPoint.end(), std::back_inserter(allDataIntersection)); return !allDataIntersection.empty(); } bool mitk::SemanticRelations::CheckOverlappingControlPoint(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) { ControlpointVector allControlPoints = GetAllControlPointsOfCase(caseID); if (allControlPoints.empty()) { return false; } std::sort(allControlPoints.begin(), allControlPoints.end()); const auto existingControlPoint = std::find_if(allControlPoints.begin(), allControlPoints.end(), [&controlPoint](const SemanticTypes::ControlPoint& currentControlPoint) { return currentControlPoint.UID == controlPoint.UID; }); auto nextControlPoint = allControlPoints.end(); auto previousControlPoint = allControlPoints.begin(); if (existingControlPoint != allControlPoints.end()) { // case overwriting: control point already contained in the list of all existing control points nextControlPoint = std::next(existingControlPoint, 1); if (existingControlPoint != allControlPoints.begin()) { previousControlPoint = std::prev(existingControlPoint, 1); } } else { // case adding: control point not contained in the list of all existing control points nextControlPoint = std::find_if(allControlPoints.begin(), allControlPoints.end(), [&controlPoint](const SemanticTypes::ControlPoint& currentControlPoint) { return currentControlPoint.startPoint >= controlPoint.endPoint; }); if (nextControlPoint != allControlPoints.begin()) { previousControlPoint = std::prev(existingControlPoint, 1); } } // check the neighboring control points for overlap bool overlapWithNext = false; bool overlapWithPrevious = CheckForOverlap(controlPoint, *previousControlPoint); if (nextControlPoint != allControlPoints.end()) { overlapWithNext = CheckForOverlap(controlPoint, *nextControlPoint); } return overlapWithNext || overlapWithPrevious; // return true if at least one overlap is detected } bool mitk::SemanticRelations::CheckContainingControlPoint(const SemanticTypes::CaseID& caseID, const SemanticTypes::ControlPoint& controlPoint) { ControlpointVector allControlPoints = GetAllControlPointsOfCase(caseID); if (allControlPoints.empty()) { return false; } const auto existingControlPoint = std::find_if(allControlPoints.begin(), allControlPoints.end(), [&controlPoint](const SemanticTypes::ControlPoint& currentControlPoint) { return currentControlPoint.UID == controlPoint.UID; }); if (existingControlPoint != allControlPoints.end()) { // case overwriting: control point already contained in the list of all existing control points // -> duplicated control point found (regardless of the actual start point and end point) return true; } else { // case adding: control point not contained in the list of all existing control points for (const auto& existingControlPoint : allControlPoints) { bool contained = InsideControlPoint(controlPoint, existingControlPoint); if (contained) { return true; } } } return false; } diff --git a/Plugins/org.mitk.gui.qt.semanticrelations/src/internal/QmitkLesionInfoWidget.cpp b/Plugins/org.mitk.gui.qt.semanticrelations/src/internal/QmitkLesionInfoWidget.cpp index 244d71880a..4a4c4c59b9 100644 --- a/Plugins/org.mitk.gui.qt.semanticrelations/src/internal/QmitkLesionInfoWidget.cpp +++ b/Plugins/org.mitk.gui.qt.semanticrelations/src/internal/QmitkLesionInfoWidget.cpp @@ -1,1032 +1,1032 @@ /*=================================================================== 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. ===================================================================*/ // semantic relations plugin #include "QmitkLesionInfoWidget.h" #include "QmitkSemanticRelationsNodeSelectionDialog.h" // semantic relations UI module #include // semantic relations module #include #include #include #include #include "QmitkCustomVariants.h" // multi label module #include // mitk core #include // qt #include #include #include #include #include const QBrush QmitkLesionInfoWidget::DEFAULT_BACKGROUND_COLOR = QBrush(Qt::transparent); const QBrush QmitkLesionInfoWidget::SELECTED_BACKGROUND_COLOR = QBrush(Qt::green); const QBrush QmitkLesionInfoWidget::CONNECTED_BACKGROUND_COLOR = QBrush(Qt::darkGreen); QmitkLesionInfoWidget::QmitkLesionInfoWidget(mitk::DataStorage* dataStorage, QWidget* parent /*= nullptr*/) : QWidget(parent) , m_DataStorage(dataStorage) , m_SemanticRelations(std::make_unique(dataStorage)) { Init(); } QmitkLesionInfoWidget::~QmitkLesionInfoWidget() { m_SemanticRelations->RemoveObserver(this); } void QmitkLesionInfoWidget::Init() { // create GUI from the Qt Designer's .ui file m_Controls.setupUi(this); m_Controls.lesionListWidget->setContextMenuPolicy(Qt::CustomContextMenu); m_Controls.segmentationListWidget->setContextMenuPolicy(Qt::CustomContextMenu); m_Controls.imageListWidget->setContextMenuPolicy(Qt::CustomContextMenu); SetUpConnections(); m_SemanticRelations->AddObserver(this); } void QmitkLesionInfoWidget::SetUpConnections() { // connect buttons to modify semantic relations connect(m_Controls.addLesionPushButton, &QPushButton::clicked, this, &QmitkLesionInfoWidget::OnAddLesionButtonClicked); connect(m_Controls.addSegmentationPushButton, &QPushButton::clicked, this, &QmitkLesionInfoWidget::OnAddSegmentationButtonClicked); connect(m_Controls.addImagePushButton, &QPushButton::clicked, this, &QmitkLesionInfoWidget::OnAddImageButtonClicked); // connect each list widget with a custom slots connect(m_Controls.lesionListWidget, &QListWidget::currentItemChanged, this, &QmitkLesionInfoWidget::OnCurrentLesionItemChanged); connect(m_Controls.lesionListWidget, &QListWidget::itemDoubleClicked, this, &QmitkLesionInfoWidget::OnLesionItemDoubleClicked); connect(m_Controls.segmentationListWidget, &QListWidget::currentItemChanged, this, &QmitkLesionInfoWidget::OnCurrentSegmentationItemChanged); connect(m_Controls.segmentationListWidget, &QListWidget::itemDoubleClicked, this, &QmitkLesionInfoWidget::OnSegmentationItemDoubleClicked); connect(m_Controls.imageListWidget, &QListWidget::currentItemChanged, this, &QmitkLesionInfoWidget::OnCurrentImageItemChanged); connect(m_Controls.imageListWidget, &QListWidget::itemDoubleClicked, this, &QmitkLesionInfoWidget::OnImageItemDoubleClicked); // connect context menu entries connect(m_Controls.lesionListWidget, &QListWidget::customContextMenuRequested, this, &QmitkLesionInfoWidget::OnLesionListContextMenuRequested); connect(m_Controls.segmentationListWidget, &QListWidget::customContextMenuRequested, this, &QmitkLesionInfoWidget::OnSegmentationListContextMenuRequested); connect(m_Controls.imageListWidget, &QListWidget::customContextMenuRequested, this, &QmitkLesionInfoWidget::OnImageListContextMenuRequested); } void QmitkLesionInfoWidget::SetCurrentCaseID(const mitk::SemanticTypes::CaseID& caseID) { m_CaseID = caseID; Update(m_CaseID); } void QmitkLesionInfoWidget::Update(const mitk::SemanticTypes::CaseID& caseID) { if (nullptr == m_SemanticRelations) { return; } // if the case ID of updated instance is equal to the currently active caseID if (caseID == m_CaseID) { ResetLesionListWidget(); ResetSegmentationListWidget(); ResetImageListWidget(); } } ////////////////////////////////////////////////////////////////////////// // Implementation of the QT_SLOTS ////////////////////////////////////////////////////////////////////////// void QmitkLesionInfoWidget::OnAddLesionButtonClicked() { if (m_CaseID.empty()) { QMessageBox msgBox; msgBox.setWindowTitle("No case ID set."); msgBox.setText("In order to add a lesion, please specify the current case / patient."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } mitk::SemanticTypes::Lesion newLesion = mitk::GenerateNewLesion(); try { m_SemanticRelations->AddLesion(m_CaseID, newLesion); } catch (mitk::SemanticRelationException& e) { MITK_INFO << "Could not add a new lesion. " << e; } } void QmitkLesionInfoWidget::OnAddSegmentationButtonClicked() { if (m_CaseID.empty()) { QMessageBox msgBox; msgBox.setWindowTitle("No case ID set."); msgBox.setText("In order to add a segmentation, please specify the current case / patient."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } QmitkSemanticRelationsNodeSelectionDialog* dialog = new QmitkSemanticRelationsNodeSelectionDialog(this, "Select segmentations to add to the semantic relations storage", ""); dialog->SetDataStorage(m_DataStorage); dialog->setWindowTitle("Select segmentation node"); dialog->SetNodePredicate(mitk::NodePredicates::GetSegmentationPredicate()); dialog->SetSelectOnlyVisibleNodes(true); dialog->SetSelectionMode(QAbstractItemView::MultiSelection); dialog->SetCaseID(m_CaseID); int dialogReturnValue = dialog->exec(); if (QDialog::Rejected == dialogReturnValue) { return; } auto nodes = dialog->GetSelectedNodes(); for (mitk::DataNode* dataNode : nodes) { if (nullptr == dataNode) { continue; } mitk::BaseData* baseData = dataNode->GetData(); if (nullptr == baseData) { continue; } // continue with valid segmentation data // get parent node of the current segmentation node with the node predicate mitk::DataStorage::SetOfObjects::ConstPointer parentNodes = m_DataStorage->GetSources(dataNode, mitk::NodePredicates::GetImagePredicate(), false); // check for already existing, identifying base properties mitk::BaseProperty* caseIDProperty = baseData->GetProperty("DICOM.0010.0010"); mitk::BaseProperty* nodeIDProperty = baseData->GetProperty("DICOM.0020.000E"); if (nullptr == caseIDProperty || nullptr == nodeIDProperty) { MITK_INFO << "No DICOM tags for case and node identification found. Transferring DICOM tags from the parent node to the selected segmentation node."; mitk::SemanticTypes::CaseID caseID = mitk::GetCaseIDFromDataNode(parentNodes->front()); mitk::SemanticTypes::ID nodeID = mitk::GetIDFromDataNode(parentNodes->front()); // transfer DICOM tags to the segmentation node mitk::StringProperty::Pointer caseIDTag = mitk::StringProperty::New(caseID); baseData->SetProperty("DICOM.0010.0010", caseIDTag); // DICOM tag is "PatientName" // add UID to distinguish between different segmentations of the same parent node mitk::StringProperty::Pointer nodeIDTag = mitk::StringProperty::New(nodeID + mitk::UIDGeneratorBoost::GenerateUID()); baseData->SetProperty("DICOM.0020.000E", nodeIDTag); // DICOM tag is "SeriesInstanceUID" } try { m_SemanticRelations->AddSegmentation(dataNode, parentNodes->front()); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not add the selected segmentation."); msgBox.setText("The program wasn't able to correctly add the selected segmentation.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); + return; } } } void QmitkLesionInfoWidget::OnAddImageButtonClicked() { QmitkSemanticRelationsNodeSelectionDialog* dialog = new QmitkSemanticRelationsNodeSelectionDialog(this, "Select images to add to the semantic relations storage", ""); dialog->SetDataStorage(m_DataStorage); dialog->setWindowTitle("Select image node"); dialog->SetNodePredicate(mitk::NodePredicates::GetImagePredicate()); dialog->SetSelectOnlyVisibleNodes(true); dialog->SetSelectionMode(QAbstractItemView::MultiSelection); dialog->SetCaseID(m_CaseID); int dialogReturnValue = dialog->exec(); if (QDialog::Rejected == dialogReturnValue) { return; } auto nodes = dialog->GetSelectedNodes(); for (mitk::DataNode* dataNode : nodes) { if (nullptr != dataNode) { try { // add the image to the semantic relations storage m_SemanticRelations->AddImage(dataNode); mitk::SemanticTypes::CaseID caseID = mitk::GetCaseIDFromDataNode(dataNode); emit ImageAdded(caseID); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not add the selected image."); msgBox.setText("The program wasn't able to correctly add the selected images.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); + return; } } } } void QmitkLesionInfoWidget::OnCurrentLesionItemChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/) { if (nullptr == m_SemanticRelations || nullptr == current || nullptr == m_DataStorage) { return; } // only the UID is needed to identify a representing lesion m_CurrentLesion.UID = current->data(Qt::UserRole).toString().toStdString(); if (false == m_SemanticRelations->InstanceExists(m_CaseID, m_CurrentLesion)) { // no UID found; cannot create a lesion return; } if (SELECTED_BACKGROUND_COLOR == current->background() || CONNECTED_BACKGROUND_COLOR == current->background()) { DarkenBackgroundColors(); } else { ResetBackgroundColors(); } current->setBackground(SELECTED_BACKGROUND_COLOR); try { - // set background color for all corresponding segmentations of the currently selected lesion + // get all corresponding segmentations of the currently selected lesion and set the background color mitk::SemanticRelations::DataNodeVector allSegmentationsOfLesion = m_SemanticRelations->GetAllSegmentationsOfLesion(m_CaseID, m_CurrentLesion); for (const auto& segmentation : allSegmentationsOfLesion) { QList items = m_Controls.segmentationListWidget->findItems(QString::fromStdString(segmentation->GetName()), Qt::MatchExactly); for (auto& item : items) { item->setBackground(SELECTED_BACKGROUND_COLOR); } - // set background color for all corresponding images of the current segmentation - // get parent node of the current segmentation node with the node predicate + // get parent nodes of the current segmentation node and set the background color mitk::DataStorage::SetOfObjects::ConstPointer parentNodes = m_DataStorage->GetSources(segmentation, mitk::NodePredicates::GetImagePredicate(), false); for (auto it = parentNodes->Begin(); it != parentNodes->End(); ++it) { QList items = m_Controls.imageListWidget->findItems(QString::fromStdString(it->Value()->GetName()), Qt::MatchExactly); for (auto& item : items) { item->setBackground(SELECTED_BACKGROUND_COLOR); } } } } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("No segmentations retrieved"); msgBox.setText("Could not automatically retrieve all segmentations of the selected lesion.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } } void QmitkLesionInfoWidget::OnLesionItemDoubleClicked(QListWidgetItem* clickedItem) { if (nullptr == clickedItem || nullptr == m_SemanticRelations) { return; } // only the UID is needed to identify a representing lesion m_CurrentLesion.UID = clickedItem->data(Qt::UserRole).toString().toStdString(); m_CurrentLesion.name = clickedItem->text().toStdString(); } void QmitkLesionInfoWidget::OnCurrentSegmentationItemChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/) { if (nullptr == m_SemanticRelations || nullptr == current || nullptr == m_DataStorage) { return; } QString segmentationName = current->text(); m_CurrentSegmentation = m_DataStorage->GetNamedNode(segmentationName.toStdString()); if (nullptr == m_CurrentSegmentation) { return; } if (SELECTED_BACKGROUND_COLOR == current->background() || CONNECTED_BACKGROUND_COLOR == current->background()) { DarkenBackgroundColors(); } else { ResetBackgroundColors(); } current->setBackground(SELECTED_BACKGROUND_COLOR); - // set background color for all corresponding images of the current segmentation - // get parent node of the current segmentation node with the node predicate + + // get parent nodes of the current segmentation node and set the background color mitk::DataStorage::SetOfObjects::ConstPointer parentNodes = m_DataStorage->GetSources(m_CurrentSegmentation, mitk::NodePredicates::GetImagePredicate(), false); for (auto it = parentNodes->Begin(); it != parentNodes->End(); ++it) { QList items = m_Controls.imageListWidget->findItems(QString::fromStdString(it->Value()->GetName()), Qt::MatchExactly); for (auto& item : items) { item->setBackground(SELECTED_BACKGROUND_COLOR); } } + // get represented lesion of the current segmentation node and set the background color if (m_SemanticRelations->IsRepresentingALesion(m_CurrentSegmentation)) { try { - // set background color for the represented lesion of the currently selected segmentation mitk::SemanticTypes::Lesion representedLesion = m_SemanticRelations->GetRepresentedLesion(m_CurrentSegmentation); for (int i = 0; i < m_Controls.lesionListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.lesionListWidget->item(i); std::string currentLesionUID = item->data(Qt::UserRole).toString().toStdString(); if (currentLesionUID == representedLesion.UID) { item->setBackground(SELECTED_BACKGROUND_COLOR); } } } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("No lesion retrieved"); msgBox.setText("Could not automatically retrieve the represented lesion of the selected segmentation.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } } } void QmitkLesionInfoWidget::OnSegmentationItemDoubleClicked(QListWidgetItem* clickedItem) { if (nullptr == clickedItem || nullptr == m_DataStorage) { return; } QString segmentationName = clickedItem->text(); m_CurrentSegmentation = m_DataStorage->GetNamedNode(segmentationName.toStdString()); if (nullptr == m_CurrentSegmentation) { return; } mitk::LabelSetImage* labelSetImage = dynamic_cast(m_CurrentSegmentation->GetData()); if (nullptr == labelSetImage) { return; } int activeLayer = labelSetImage->GetActiveLayer(); mitk::Label* activeLabel = labelSetImage->GetActiveLabel(activeLayer); labelSetImage->UpdateCenterOfMass(activeLabel->GetValue(), activeLayer); const mitk::Point3D& centerPosition = activeLabel->GetCenterOfMassCoordinates(); if (centerPosition.GetVnlVector().max_value() > 0.0) { emit JumpToPosition(centerPosition); } } void QmitkLesionInfoWidget::OnCurrentImageItemChanged(QListWidgetItem* current, QListWidgetItem* /*previous*/) { if (nullptr == m_SemanticRelations || nullptr == current || nullptr == m_DataStorage) { return; } QString imageName = current->text(); m_CurrentImage = m_DataStorage->GetNamedNode(imageName.toStdString()); if (nullptr == m_CurrentImage) { return; } if (SELECTED_BACKGROUND_COLOR == current->background() || CONNECTED_BACKGROUND_COLOR == current->background()) { DarkenBackgroundColors(); } else { ResetBackgroundColors(); } current->setBackground(SELECTED_BACKGROUND_COLOR); - // set background color for all corresponding segmentations of the current image - // get child nodes of the current image node with the segmentation predicate + // get child nodes of the current image node and set the background color mitk::DataStorage::SetOfObjects::ConstPointer segmentationNodes = m_DataStorage->GetDerivations(m_CurrentImage, mitk::NodePredicates::GetSegmentationPredicate(), false); for (auto it = segmentationNodes->Begin(); it != segmentationNodes->End(); ++it) { QList items = m_Controls.segmentationListWidget->findItems(QString::fromStdString(it->Value()->GetName()), Qt::MatchExactly); for (auto& item : items) { item->setBackground(SELECTED_BACKGROUND_COLOR); } try { + // get represented lesion of the current segmentation node and set the background color if (m_SemanticRelations->IsRepresentingALesion(it->Value())) { - // set background color for the represented lesion of the currently selected segmentation mitk::SemanticTypes::Lesion representedLesion = m_SemanticRelations->GetRepresentedLesion(it->Value()); for (int i = 0; i < m_Controls.lesionListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.lesionListWidget->item(i); std::string currentLesionUID = item->data(Qt::UserRole).toString().toStdString(); if (currentLesionUID == representedLesion.UID) { item->setBackground(SELECTED_BACKGROUND_COLOR); } } } } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("No lesion retrieved"); msgBox.setText("Could not automatically retrieve the represented lesion of the selected segmentation.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } } } void QmitkLesionInfoWidget::OnImageItemDoubleClicked(QListWidgetItem* clickedItem) { if (nullptr == clickedItem || nullptr == m_SemanticRelations) { return; } } void QmitkLesionInfoWidget::OnLesionListContextMenuRequested(const QPoint& pos) { QListWidgetItem* currentItem = m_Controls.lesionListWidget->itemAt(pos); if (nullptr == currentItem) { // no item clicked; cannot retrieve the current lesion return; } mitk::SemanticTypes::ID selectedLesionUID; selectedLesionUID = currentItem->data(Qt::UserRole).toString().toStdString(); if (selectedLesionUID.empty()) { // no UID found; cannot create a lesion return; } QMenu* menu = new QMenu(m_Controls.lesionListWidget); QAction* linkToSegmentation = new QAction("Link to segmentation", this); linkToSegmentation->setEnabled(true); connect(linkToSegmentation, &QAction::triggered, [this, selectedLesionUID] { OnLinkToSegmentation(selectedLesionUID); }); menu->addAction(linkToSegmentation); QAction* setLesionName = new QAction("Set lesion name", this); setLesionName->setEnabled(true); connect(setLesionName, &QAction::triggered, [this, selectedLesionUID] { OnSetLesionName(selectedLesionUID); }); menu->addAction(setLesionName); QAction* setLesionClass = new QAction("Set lesion class", this); setLesionClass->setEnabled(true); connect(setLesionClass, &QAction::triggered, [this, selectedLesionUID] { OnSetLesionClass(selectedLesionUID); }); menu->addAction(setLesionClass); QAction* removeLesion = new QAction("Remove lesion", this); removeLesion->setEnabled(true); connect(removeLesion, &QAction::triggered, [this, selectedLesionUID] { OnRemoveLesion(selectedLesionUID); }); menu->addAction(removeLesion); menu->popup(QCursor::pos()); } void QmitkLesionInfoWidget::OnSegmentationListContextMenuRequested(const QPoint& pos) { QListWidgetItem* currentItem = m_Controls.segmentationListWidget->itemAt(pos); if (nullptr == currentItem) { // no item clicked; cannot retrieve the current segmentation return; } const mitk::DataNode* selectedSegmentation = m_DataStorage->GetNamedNode(currentItem->text().toStdString()); if (nullptr == selectedSegmentation) { return; } if (false == mitk::NodePredicates::GetSegmentationPredicate()->CheckNode(selectedSegmentation)) { return; } QMenu* menu = new QMenu(m_Controls.segmentationListWidget); QAction* unlinkFromLesion = new QAction("Unlink from lesion", this); unlinkFromLesion->setEnabled(true); connect(unlinkFromLesion, &QAction::triggered, [this, selectedSegmentation] { OnUnlinkFromLesion(selectedSegmentation); }); menu->addAction(unlinkFromLesion); QAction* removeSegmentation = new QAction("Remove segmentation", this); removeSegmentation->setEnabled(true); connect(removeSegmentation, &QAction::triggered, [this, selectedSegmentation] { OnRemoveSegmentation(selectedSegmentation); }); menu->addAction(removeSegmentation); menu->popup(QCursor::pos()); } void QmitkLesionInfoWidget::OnImageListContextMenuRequested(const QPoint& pos) { QListWidgetItem* currentItem = m_Controls.imageListWidget->itemAt(pos); if (nullptr == currentItem) { // no item clicked; cannot retrieve the current image return; } const mitk::DataNode* selectedImage = m_DataStorage->GetNamedNode(currentItem->text().toStdString()); if (nullptr == selectedImage) { return; } if (false == mitk::NodePredicates::GetImagePredicate()->CheckNode(selectedImage)) { return; } QMenu* menu = new QMenu(m_Controls.imageListWidget); QAction* removeImage = new QAction("Remove image", this); removeImage->setEnabled(true); connect(removeImage, &QAction::triggered, [this, selectedImage] { OnRemoveImage(selectedImage); }); menu->addAction(removeImage); menu->popup(QCursor::pos()); } void QmitkLesionInfoWidget::OnLinkToSegmentation(const mitk::SemanticTypes::ID& selectedLesionUID) { if (m_CaseID.empty()) { QMessageBox msgBox; msgBox.setWindowTitle("No case ID set."); msgBox.setText("In order to link a lesion to a segmentation, please specify the current case / patient."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } // retrieve the full lesion with its lesion class using the given lesion UID mitk::SemanticTypes::Lesion selectedLesion = mitk::GetLesionByUID(selectedLesionUID, m_SemanticRelations->GetAllLesionsOfCase(m_CaseID)); if (selectedLesion.UID.empty()) { // could not find lesion information for the selected lesion return; } QmitkSemanticRelationsNodeSelectionDialog* dialog = new QmitkSemanticRelationsNodeSelectionDialog(this, "Select segmentation to link to the selected lesion.", ""); dialog->SetDataStorage(m_DataStorage); dialog->setWindowTitle("Select segmentation node"); dialog->SetNodePredicate(mitk::NodePredicates::GetSegmentationPredicate()); dialog->SetSelectOnlyVisibleNodes(true); dialog->SetSelectionMode(QAbstractItemView::SingleSelection); dialog->SetCaseID(m_CaseID); int dialogReturnValue = dialog->exec(); if (QDialog::Rejected == dialogReturnValue) { return; } auto nodes = dialog->GetSelectedNodes(); mitk::DataNode::Pointer selectedDataNode = nullptr; if (!nodes.isEmpty()) { // only single selection allowed selectedDataNode = nodes.front(); } if (nullptr == selectedDataNode) { QMessageBox msgBox; msgBox.setWindowTitle("No valid segmentation node selected."); msgBox.setText("In order to link the selected lesion to a segmentation, please specify a valid segmentation node."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } if (false == mitk::NodePredicates::GetSegmentationPredicate()->CheckNode(selectedDataNode)) { QMessageBox msgBox; msgBox.setWindowTitle("No segmentation selected"); msgBox.setText("In order to link the selected lesion to a segmentation, please specify a valid segmentation node."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } mitk::BaseData* baseData = selectedDataNode->GetData(); if (nullptr == baseData) { QMessageBox msgBox; msgBox.setWindowTitle("No valid base data."); msgBox.setText("In order to link the selected lesion to a segmentation, please specify a valid segmentation node."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } try { m_SemanticRelations->LinkSegmentationToLesion(selectedDataNode, selectedLesion); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not link the selected lesion."); msgBox.setText("The program wasn't able to correctly link the selected lesion with the selected segmentation.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); } } void QmitkLesionInfoWidget::OnSetLesionName(const mitk::SemanticTypes::ID& selectedLesionUID) { // retrieve the full lesion with its lesion class using the given lesion UID mitk::SemanticTypes::Lesion selectedLesion = mitk::GetLesionByUID(selectedLesionUID, m_SemanticRelations->GetAllLesionsOfCase(m_CaseID)); if (selectedLesion.UID.empty()) { // could not find lesion information for the selected lesion return; } // use the lesion information to set the input text for the dialog QmitkLesionTextDialog* inputDialog = new QmitkLesionTextDialog(this); inputDialog->setWindowTitle("Set lesion name"); inputDialog->SetLineEditText(selectedLesion.name); int dialogReturnValue = inputDialog->exec(); if (QDialog::Rejected == dialogReturnValue) { return; } std::string newLesionName = inputDialog->GetLineEditText().toStdString(); selectedLesion.name = newLesionName; m_SemanticRelations->OverwriteLesion(m_CaseID, selectedLesion); } void QmitkLesionInfoWidget::OnSetLesionClass(const mitk::SemanticTypes::ID& selectedLesionUID) { // retrieve the full lesion with its lesion class using the given lesion UID mitk::SemanticTypes::Lesion selectedLesion = mitk::GetLesionByUID(selectedLesionUID, m_SemanticRelations->GetAllLesionsOfCase(m_CaseID)); if (selectedLesion.UID.empty()) { // could not find lesion information for the selected lesion return; } // use the lesion information to set the input text for the dialog QmitkLesionTextDialog* inputDialog = new QmitkLesionTextDialog(this); inputDialog->setWindowTitle("Set lesion class"); inputDialog->SetLineEditText(selectedLesion.lesionClass.classType); // prepare the completer for the dialogs input text field mitk::LesionClassVector allLesionClasses = m_SemanticRelations->GetAllLesionClassesOfCase(m_CaseID); QStringList wordList; for (const auto& lesionClass : allLesionClasses) { wordList << QString::fromStdString(lesionClass.classType); } QCompleter* completer = new QCompleter(wordList, this); completer->setCaseSensitivity(Qt::CaseInsensitive); inputDialog->GetLineEdit()->setCompleter(completer); int dialogReturnValue = inputDialog->exec(); if (QDialog::Rejected == dialogReturnValue) { return; } // retrieve the new input lesion class type and check for an already existing lesion class types std::string newLesionClassType = inputDialog->GetLineEditText().toStdString(); mitk::SemanticTypes::LesionClass existingLesionClass = mitk::FindExistingLesionClass(newLesionClassType, allLesionClasses); if (existingLesionClass.UID.empty()) { // could not find lesion class information for the new lesion class type // create a new lesion class for the selected lesion existingLesionClass = mitk::GenerateNewLesionClass(newLesionClassType); } selectedLesion.lesionClass = existingLesionClass; m_SemanticRelations->OverwriteLesion(m_CaseID, selectedLesion); } void QmitkLesionInfoWidget::OnRemoveLesion(const mitk::SemanticTypes::ID& selectedLesionUID) { if (m_CaseID.empty()) { QMessageBox msgBox; msgBox.setWindowTitle("No case ID set."); msgBox.setText("In order to remove a lesion, please specify the current case / patient."); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } // retrieve the full lesion with its lesion class using the given lesion UID mitk::SemanticTypes::Lesion selectedLesion = mitk::GetLesionByUID(selectedLesionUID, m_SemanticRelations->GetAllLesionsOfCase(m_CaseID)); if (selectedLesion.UID.empty()) { // could not find lesion information for the selected lesion return; } try { m_SemanticRelations->RemoveLesion(m_CaseID, selectedLesion); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not remove the selected lesion."); msgBox.setText("The program wasn't able to correctly remove the selected lesion from the semantic relations model.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); } } void QmitkLesionInfoWidget::OnUnlinkFromLesion(const mitk::DataNode* selectedSegmentation) { try { m_SemanticRelations->UnlinkSegmentationFromLesion(selectedSegmentation); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not unlink a lesion from the selected segmentation."); msgBox.setText("The program wasn't able to correctly unlink the lesion from the selected segmentation.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); } } void QmitkLesionInfoWidget::OnRemoveSegmentation(const mitk::DataNode* selectedSegmentation) { try { m_SemanticRelations->RemoveSegmentation(selectedSegmentation); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not remove the selected segmentation."); msgBox.setText("The program wasn't able to correctly remove the selected segmentation from the semantic relations model.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); } } void QmitkLesionInfoWidget::OnRemoveImage(const mitk::DataNode* selectedImage) { try { m_SemanticRelations->RemoveImage(selectedImage); emit ImageRemoved(selectedImage); } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("Could not remove the selected image."); msgBox.setText("The program wasn't able to correctly remove the selected image from the semantic relations model.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); } } void QmitkLesionInfoWidget::ResetLesionListWidget() { m_Controls.lesionListWidget->clear(); m_CurrentLesion.UID = ""; m_CurrentLesion.name = ""; // create lesion list widget entries with the current lesions mitk::SemanticRelations::LesionVector allLesionsOfCase = m_SemanticRelations->GetAllLesionsOfCase(m_CaseID); if (allLesionsOfCase.empty()) { m_Controls.lesionListWidget->addItem("No lesions found"); } for (const auto& lesion : allLesionsOfCase) { // store the UID as 'UserRole' data in the widget item QListWidgetItem* lesionItem = new QListWidgetItem; lesionItem->setData(Qt::UserRole, QString::fromStdString(lesion.UID)); // use the lesion UID for the item text, if the lesion name is non-existent if (lesion.name.empty()) { lesionItem->setText(QString::fromStdString(lesion.UID)); } else { lesionItem->setText(QString::fromStdString(lesion.name)); } m_Controls.lesionListWidget->addItem(lesionItem); } } void QmitkLesionInfoWidget::ResetSegmentationListWidget() { m_Controls.segmentationListWidget->clear(); m_CurrentSegmentation = nullptr; try { // create segmentation list widget entries with the current segmentations mitk::SemanticRelations::DataNodeVector allSegmentationsOfCase = m_SemanticRelations->GetAllSegmentationsOfCase(m_CaseID); if (allSegmentationsOfCase.empty()) { m_Controls.segmentationListWidget->addItem("No segmentations found"); } for (const auto& segmentation : allSegmentationsOfCase) { m_Controls.segmentationListWidget->addItem(QString::fromStdString(segmentation->GetName())); } } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("No segmentations retrieved"); msgBox.setText("Could not automatically retrieve all segmentations of the current case.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } } void QmitkLesionInfoWidget::ResetImageListWidget() { m_Controls.imageListWidget->clear(); m_CurrentImage = nullptr; try { // create image list widget entries with the current images mitk::SemanticRelations::DataNodeVector allImagesOfCase = m_SemanticRelations->GetAllImagesOfCase(m_CaseID); if (allImagesOfCase.empty()) { m_Controls.imageListWidget->addItem("No images found"); } for (const auto& image : allImagesOfCase) { m_Controls.imageListWidget->addItem(QString::fromStdString(image->GetName())); } } - catch (mitk::Exception& e) + catch (const mitk::SemanticRelationException& e) { std::stringstream exceptionMessage; exceptionMessage << e; QMessageBox msgBox; msgBox.setWindowTitle("No images retrieved"); msgBox.setText("Could not automatically retrieve all images of the current case.\n" "Reason:\n" + QString::fromStdString(exceptionMessage.str())); msgBox.setIcon(QMessageBox::Warning); msgBox.exec(); return; } } void QmitkLesionInfoWidget::ResetBackgroundColors() { // reset all lesion list widget items to original background color for (int i = 0; i < m_Controls.lesionListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.lesionListWidget->item(i); item->setBackground(DEFAULT_BACKGROUND_COLOR); } // reset all segmentation list widget items to original background color for (int i = 0; i < m_Controls.segmentationListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.segmentationListWidget->item(i); item->setBackground(DEFAULT_BACKGROUND_COLOR); } // reset all image list widget items to original background color for (int i = 0; i < m_Controls.imageListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.imageListWidget->item(i); item->setBackground(DEFAULT_BACKGROUND_COLOR); } } void QmitkLesionInfoWidget::DarkenBackgroundColors() { // reset all lesion list widget items to original background color for (int i = 0; i < m_Controls.lesionListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.lesionListWidget->item(i); if (DEFAULT_BACKGROUND_COLOR != item->background()) { item->setBackground(CONNECTED_BACKGROUND_COLOR); } } // reset all segmentation list widget items to original background color for (int i = 0; i < m_Controls.segmentationListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.segmentationListWidget->item(i); if (DEFAULT_BACKGROUND_COLOR != item->background()) { item->setBackground(CONNECTED_BACKGROUND_COLOR); } } // reset all image list widget items to original background color for (int i = 0; i < m_Controls.imageListWidget->count(); ++i) { QListWidgetItem* item = m_Controls.imageListWidget->item(i); if (DEFAULT_BACKGROUND_COLOR != item->background()) { item->setBackground(CONNECTED_BACKGROUND_COLOR); } } }