| 168 | } |
| 169 | |
| 170 | mitk::DataStorage::SetOfObjects::ConstPointer mitk::StandaloneDataStorage::GetRelations( |
| 171 | const mitk::DataNode *node, |
| 172 | const AdjacencyList &relation, |
| 173 | const NodePredicateBase *condition, |
| 174 | bool onlyDirectlyRelated) const |
| 175 | { |
| 176 | if (node == nullptr) |
| 177 | throw std::invalid_argument("invalid node"); |
| 178 | |
| 179 | /* Either read direct relations directly from adjacency list */ |
| 180 | if (onlyDirectlyRelated) |
| 181 | { |
| 182 | auto it = relation.find(node); // get parents of current node |
| 183 | if ((it == relation.cend()) || (it->second.IsNull())) // node not found in list or no set of parents |
| 184 | return SetOfObjects::ConstPointer(mitk::DataStorage::SetOfObjects::New()); // return an empty set |
| 185 | else |
| 186 | return this->FilterSetOfObjects(it->second, condition); |
| 187 | } |
| 188 | |
| 189 | /* Or traverse adjacency list to collect all related nodes */ |
| 190 | std::vector<mitk::DataNode::ConstPointer> resultset; |
| 191 | std::vector<mitk::DataNode::ConstPointer> openlist; |
| 192 | |
| 193 | /* Initialize openlist with node. this will add node to resultset, |
| 194 | but that is necessary to detect circular relations that would lead to endless recursion */ |
| 195 | openlist.push_back(node); |
| 196 | |
| 197 | while (openlist.size() > 0) |
| 198 | { |
| 199 | mitk::DataNode::ConstPointer current = openlist.back(); // get element that needs to be processed |
| 200 | openlist.pop_back(); // remove last element, because it gets processed now |
| 201 | resultset.push_back(current); // add current element to resultset |
| 202 | auto it = relation.find(current); // get parents of current node |
| 203 | if ((it == relation.cend()) // if node not found in list |
| 204 | || |
| 205 | (it->second.IsNull()) // or no set of parents available |
| 206 | || |
| 207 | (it->second->Size() == 0)) // or empty set of parents |
| 208 | continue; // then continue with next node in open list |
| 209 | else |
| 210 | for (SetOfObjects::ConstIterator parentIt = it->second->Begin(); parentIt != it->second->End(); |
| 211 | ++parentIt) // for each parent of current node |
| 212 | { |
| 213 | mitk::DataNode::ConstPointer p = parentIt.Value().GetPointer(); |
| 214 | if (!(std::find(resultset.cbegin(), resultset.cend(), p) != |
| 215 | resultset.end()) // if it is not already in resultset |
| 216 | && |
| 217 | !(std::find(openlist.cbegin(), openlist.cend(), p) != openlist.cend())) // and not already in openlist |
| 218 | openlist.push_back(p); // then add it to openlist, so that it can be processed |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* now finally copy the results to a proper SetOfObjects variable excluding the initial node and checking the condition |
| 223 | * if any is given */ |
| 224 | mitk::DataStorage::SetOfObjects::Pointer realResultset = mitk::DataStorage::SetOfObjects::New(); |
| 225 | if (condition != nullptr) |
| 226 | { |
| 227 | for (auto resultIt = resultset.cbegin(); |