| 780 | } |
| 781 | |
| 782 | void mitk::MultiLabelSegmentation::MergeLabels(LabelValueType targetLabelValue, const LabelValueVectorType& sourceLabelValues, |
| 783 | OverwriteStyle overwriteStyle) |
| 784 | { |
| 785 | for (const auto& value : sourceLabelValues) |
| 786 | { |
| 787 | if (!this->ExistLabel(value)) mitkThrow() << "Cannot merge label. Source label value (" << value << ") does not exist."; |
| 788 | } |
| 789 | |
| 790 | if (!this->ExistLabel(targetLabelValue)) mitkThrow() << "Cannot merge label. Target label value (" << targetLabelValue << ") does not exist."; |
| 791 | |
| 792 | auto targetGroupID = this->GetGroupIndexOfLabel(targetLabelValue); |
| 793 | auto sourceGroupMapping = LabelSetImageHelper::SplitLabelValuesByGroup(this, sourceLabelValues); |
| 794 | |
| 795 | //////////////////////////////////////// |
| 796 | //prepare label information for transfer |
| 797 | auto targetGroupLabelValues = this->GetLabelValuesByGroup(targetGroupID); |
| 798 | |
| 799 | //remove all source labels, if they are in labels, because we want to ensure that these are not locked for the transfer |
| 800 | auto isSourceValueCheckLambda = [&](LabelValueType x) |
| 801 | { return std::find(sourceLabelValues.begin(), sourceLabelValues.end(), x) != sourceLabelValues.end(); }; |
| 802 | targetGroupLabelValues.erase(std::remove_if(targetGroupLabelValues.begin(), targetGroupLabelValues.end(), isSourceValueCheckLambda), |
| 803 | targetGroupLabelValues.end()); |
| 804 | |
| 805 | auto labels = this->GetConstLabelsByValue(targetGroupLabelValues); |
| 806 | for (const auto value : sourceLabelValues) |
| 807 | { |
| 808 | auto unlockedSourceLabel = this->GetLabel(value)->Clone(); |
| 809 | unlockedSourceLabel->SetLocked(false); |
| 810 | labels.push_back(unlockedSourceLabel); |
| 811 | } |
| 812 | |
| 813 | //////////////////////////////////////////////////////////////////// |
| 814 | //iterate through all relevant source groups and merge there content |
| 815 | for (const auto& [sourceGroupID, relevantSourceLabelValues] : sourceGroupMapping) |
| 816 | { |
| 817 | LabelValueMappingVector mapping; |
| 818 | for (const auto value : relevantSourceLabelValues) |
| 819 | { |
| 820 | mapping.emplace_back(value, targetLabelValue); |
| 821 | } |
| 822 | |
| 823 | //do the merge as a transfer operation |
| 824 | mitk::TransferLabelContent(this->GetGroupImage(sourceGroupID), this->GetGroupImage(targetGroupID), labels, |
| 825 | mitk::MultiLabelSegmentation::UNLABELED_VALUE, mitk::MultiLabelSegmentation::UNLABELED_VALUE, false, mapping, |
| 826 | MergeStyle::Merge, overwriteStyle); |
| 827 | } |
| 828 | |
| 829 | this->InvokeEvent(LabelModifiedEvent(targetLabelValue)); |
| 830 | for (const auto value : sourceLabelValues) |
| 831 | { |
| 832 | this->InvokeEvent(LabelModifiedEvent(value)); |
| 833 | } |
| 834 | |
| 835 | auto modifiedValues = sourceLabelValues; |
| 836 | modifiedValues.push_back(targetLabelValue); |
| 837 | this->InvokeEvent(LabelsChangedEvent(modifiedValues)); |
| 838 | |
| 839 | Modified(); |