17 #include <QTextStream>
19 #include <QPushButton>
20 #include "vtkMNITagPointReader.h"
21 #include "vtkStringArray.h"
55 mViewService(viewService),
56 mPatientModelService(patientModelService),
57 mTrackingService(trackingService),
58 mSpaceProvider(spaceProvider),
59 mFileManager(filemanager)
62 connect(patientModelService.get(), SIGNAL(dataAddedOrRemoved()),
this, SIGNAL(
metricsChanged()));
71 DataPtr data = mPatientModelService->getData(uid);
72 DataMetricPtr metric = boost::dynamic_pointer_cast<DataMetric>(data);
78 return this->getAllMetrics().size();
81 std::vector<DataMetricPtr> MetricManager::getAllMetrics()
const
83 std::vector<DataMetricPtr> retval;
84 std::map<QString, DataPtr> all = mPatientModelService->getDatas();
85 for (std::map<QString, DataPtr>::iterator iter=all.begin(); iter!=all.end(); ++iter)
87 DataMetricPtr metric = boost::dynamic_pointer_cast<DataMetric>(iter->second);
89 retval.push_back(metric);
96 mSelection = selection;
101 mActiveLandmark = uid;
110 Vector3D p_r = metric->getRefCoord();;
111 this->setManualToolPosition(p_r);
114 void MetricManager::setManualToolPosition(
Vector3D p_r)
116 Transform3D rMpr = mPatientModelService->get_rMpr();
117 Vector3D p_pr = rMpr.inv().coord(p_r);
120 ToolPtr tool = mTrackingService->getManualTool();
121 Vector3D offset = tool->get_prMt().vector(
Vector3D(0, 0, tool->getTooltipOffset()));
123 p_r = rMpr.coord(p_pr);
126 mPatientModelService->setCenter(p_r);
136 p1->setCoordinate(point);
138 mPatientModelService->insertData(p1);
140 mViewService->getGroup(0)->addData(p1->getUid());
151 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
152 for (
unsigned i=0; i<args.size(); ++i)
153 d0->getArguments()->set(i, args[i]);
155 this->installNewMetric(d0);
162 this->addPointInDefaultPosition();
168 QColor color = QColor(240, 170, 255, 255);
169 Vector3D p_ref = mSpaceProvider->getActiveToolTipPoint(ref,
true);
171 DataPtr data = mPatientModelService->getData(mActiveLandmark);
174 return this->
addPoint(p_ref, ref,
"point%1", color);
176 PointMetricPtr pointMetric = boost::dynamic_pointer_cast<PointMetric>(data);
179 ref = pointMetric->getSpace();
180 p_ref = mSpaceProvider->getActiveToolTipPoint(ref,
true);
183 DataMetricPtr metric = boost::dynamic_pointer_cast<DataMetric>(data);
185 color = metric->getColor();
189 return this->
addPoint(p_ref, ref,
"point%1", color);
198 Transform3D rMt = mSpaceProvider->getActiveToolTipTransform(ref,
true);
200 frame->setSpace(ref);
201 frame->setFrame(rMt);
203 this->installNewMetric(frame);
212 Transform3D rMt = mSpaceProvider->getActiveToolTipTransform(ref,
true);
214 frame->setSpace(ref);
215 frame->setFrame(rMt);
216 frame->setToolName(mTrackingService->getActiveTool()->getName());
217 frame->setToolOffset(mTrackingService->getActiveTool()->getTooltipOffset());
219 this->installNewMetric(frame);
227 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(p1->getArguments());
228 for (
unsigned i=0; i<args.size(); ++i)
229 p1->getArguments()->set(i, args[i]);
231 this->installNewMetric(p1);
238 std::vector<DataPtr> MetricManager::refinePointArguments(std::vector<DataPtr> args,
unsigned argNo)
242 for (
unsigned i=0; i<args.size();)
244 if (args.size() <= argNo)
246 if (!mSelection.count(args[i]->getUid()))
247 args.erase(args.begin()+i);
252 while (args.size() > argNo)
253 args.erase(args.begin());
255 while (args.size() < argNo)
268 this->installNewMetric(d0);
281 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments(), 3);
282 d0->getArguments()->set(0, args[0]);
283 d0->getArguments()->set(1, args[1]);
284 d0->getArguments()->set(2, args[1]);
285 d0->getArguments()->set(3, args[2]);
287 this->installNewMetric(d0);
294 std::vector<DataMetricPtr> metrics = this->getAllMetrics();
296 std::vector<DataPtr> args;
297 for (
unsigned i=0; i<metrics.size(); ++i)
299 if (arguments->validArgument(metrics[i]))
300 args.push_back(metrics[i]);
303 if (numberOfRequiredArguments<0)
304 numberOfRequiredArguments = arguments->getCount();
305 args = this->refinePointArguments(args, numberOfRequiredArguments);
314 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
315 d0->getArguments()->set(0, args[0]);
317 this->installNewMetric(d0);
324 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
325 d0->getArguments()->set(0, args[0]);
326 d0->getArguments()->set(1, args[1]);
328 this->installNewMetric(d0);
335 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
336 d0->getArguments()->set(0, args[0]);
337 d0->getArguments()->set(1, args[1]);
339 this->installNewMetric(d0);
347 metric->setColor(prevMetric->getColor());
350 mPatientModelService->insertData(metric);
352 mViewService->getGroup(0)->addData(metric->getUid());
357 ToolPtr refTool = mTrackingService->getReferenceTool();
360 reportDebug(
"No reference tool, cannot load reference points into the pointsampler");
364 std::map<QString, Vector3D> referencePoints_s = refTool->getReferencePoints();
365 if(referencePoints_s.empty())
367 reportWarning(
"No referenceppoints in reference tool "+refTool->getName());
374 std::map<QString, Vector3D>::iterator it = referencePoints_s.begin();
375 for(; it != referencePoints_s.end(); ++it)
377 Vector3D P_ref = mSpaceProvider->get_toMfrom(sensor, ref).coord(it->second);
385 QDomDocument& doc = orderedDoc.
doc();
386 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
387 QDomElement patientNode = doc.createElement(
"patient");
388 doc.appendChild(patientNode);
389 QDomElement managersNode = doc.createElement(
"managers");
390 patientNode.appendChild(managersNode);
391 QDomElement datamanagerNode = doc.createElement(
"datamanager");
392 managersNode.appendChild(datamanagerNode);
393 std::map<QString, DataPtr> dataMap = mPatientModelService->getDatas();
395 std::map<QString, DataPtr>::iterator iter;
396 for (iter = dataMap.begin(); iter != dataMap.end(); ++iter)
398 DataMetricPtr metric = boost::dynamic_pointer_cast<DataMetric>(iter->second);
401 QDomElement dataNode = doc.createElement(
"data");
402 metric->addXml(dataNode);
403 datamanagerNode.appendChild(dataNode);
410 void MetricManager::resolveUnknownParentSpacesForPointMetrics(QDomNode dataNode, std::map<QString, QString> mapping_of_unknown_to_known_spaces,
DataPtr data)
412 QString uid = data->getUid();
413 PointMetricPtr point_metric = boost::static_pointer_cast<PointMetric>(data);
417 QString string_space = dataNode.toElement().attribute(
"space");
420 bool parent_found = mPatientModelService->getData(parentSpace.
mRefObject) !=
DataPtr();
421 if(need_parent && !parent_found)
423 if(mapping_of_unknown_to_known_spaces.find(string_space) == mapping_of_unknown_to_known_spaces.end())
428 "Select parent coordinate system of metric with uid: "+uid);
429 space_property->setSpaceProvider(mSpaceProvider);
430 QWidget* widget =
new QWidget;
431 widget->setFocusPolicy(Qt::StrongFocus);
432 QVBoxLayout *layout =
new QVBoxLayout();
433 layout->addWidget(
new QLabel(
"Select parent space for Point metric: "+uid+
"."));
436 dialog.setWindowTitle(
"Space "+string_space+
" does not exist.");
437 dialog.setLayout(layout);
438 QPushButton *okButton =
new QPushButton(tr(
"Ok"));
439 layout->addWidget(okButton);
440 connect(okButton, &QAbstractButton::clicked, &dialog, &QWidget::close);
442 CX_LOG_DEBUG() <<
"New space is now: " << space_property->getValue().mId <<
" " << space_property->getValue().mRefObject;
444 mapping_of_unknown_to_known_spaces[string_space] = new_parentspace.
toString();
447 point_metric->setSpace(parentSpace);
448 point_metric->setCoordinate(Vector3D::fromString(dataNode.toElement().attribute(
"coord")));
449 dataNode.toElement().setAttribute(
"space", parentSpace.
toString());
456 QDomElement patientNode = xml.documentElement();
458 std::map<DataPtr, QDomNode> datanodes;
460 QDomNode managersNode = patientNode.firstChildElement(
"managers");
461 QDomNode datamanagerNode = managersNode.firstChildElement(
"datamanager");
462 QDomNode dataNode = datamanagerNode.firstChildElement(
"data");
465 std::map<QString, QString> mapping_of_unknown_to_known_spaces;
467 for (; !dataNode.isNull(); dataNode = dataNode.nextSibling())
469 QDomNamedNodeMap attributes = dataNode.attributes();
470 QDomNode typeAttribute = attributes.namedItem(
"type");
471 bool isMetric =
false;
472 if(typeAttribute.isAttr())
474 isMetric = typeAttribute.toAttr().value().contains(
"Metric");
477 if (dataNode.nodeName() ==
"data" && isMetric)
480 QString uid = dataNode.toElement().attribute(
"uid");
481 if(mPatientModelService->getData(uid))
483 QString name = dataNode.toElement().attribute(
"name");
484 reportWarning(
"Metric: " + name +
", is already in the model with Uid: " + uid +
". Import skipped.");
488 DataPtr data = this->loadDataFromXMLNode(dataNode.toElement());
490 datanodes[data] = dataNode.toElement();
493 this->resolveUnknownParentSpacesForPointMetrics(dataNode, mapping_of_unknown_to_known_spaces, data);
499 for (std::map<DataPtr, QDomNode>::iterator iter = datanodes.begin(); iter != datanodes.end(); ++iter)
501 iter->first->parseXml(iter->second);
508 DataPtr MetricManager::loadDataFromXMLNode(QDomElement node)
510 QString uid = node.toElement().attribute(
"uid");
511 QString name = node.toElement().attribute(
"name");
512 QString type = node.toElement().attribute(
"type");
514 return this->createData(type, uid, name);
518 DataPtr MetricManager::createData(QString type, QString uid, QString name)
520 DataPtr data = mPatientModelService->createData(type, uid, name);
523 reportError(QString(
"Not able to create metric with name: %1, uid: %2, type: %3").arg(name).arg(uid).arg(type));
530 mPatientModelService->insertData(data);