17 #include <QTextStream> 19 #include <QVBoxLayout> 20 #include <QPushButton> 22 #include <vtkMNITagPointReader.h> 23 #include <vtkStringArray.h> 57 mViewService(viewService),
58 mPatientModelService(patientModelService),
59 mTrackingService(trackingService),
60 mSpaceProvider(spaceProvider)
63 connect(patientModelService.get(), SIGNAL(dataAddedOrRemoved()),
this, SIGNAL(
metricsChanged()));
68 DataPtr data = mPatientModelService->getData(uid);
75 return this->getAllMetrics().size();
78 std::vector<DataMetricPtr> MetricManager::getAllMetrics()
const 80 std::vector<DataMetricPtr> retval;
81 std::map<QString, DataPtr> all = mPatientModelService->getDatas();
82 for (std::map<QString, DataPtr>::iterator iter=all.begin(); iter!=all.end(); ++iter)
86 retval.push_back(metric);
93 mSelection = selection;
98 mActiveLandmark = uid;
107 Vector3D p_r = metric->getRefCoord();;
108 this->setManualToolPosition(p_r);
111 void MetricManager::setManualToolPosition(
Vector3D p_r)
113 Transform3D rMpr = mPatientModelService->get_rMpr();
114 Vector3D p_pr = rMpr.inv().coord(p_r);
117 ToolPtr tool = mTrackingService->getManualTool();
118 Vector3D offset = tool->get_prMt().vector(
Vector3D(0, 0, tool->getTooltipOffset()));
120 p_r = rMpr.coord(p_pr);
123 mPatientModelService->setCenter(p_r);
133 p1->setCoordinate(point);
135 mPatientModelService->insertData(p1);
137 mViewService->getGroup(0)->addData(p1->getUid());
148 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
149 for (
unsigned i=0; i<args.size(); ++i)
150 d0->getArguments()->set(i, args[i]);
152 this->installNewMetric(d0);
159 this->addPointInDefaultPosition();
165 QColor color = QColor(240, 170, 255, 255);
166 Vector3D p_ref = mSpaceProvider->getActiveToolTipPoint(ref,
true);
168 DataPtr data = mPatientModelService->getData(mActiveLandmark);
171 return this->
addPoint(p_ref, ref,
"point%1", color);
176 ref = pointMetric->getSpace();
177 p_ref = mSpaceProvider->getActiveToolTipPoint(ref,
true);
186 return this->
addPoint(p_ref, ref,
"point%1", color);
195 Transform3D rMt = mSpaceProvider->getActiveToolTipTransform(ref,
true);
197 frame->setSpace(ref);
198 frame->setFrame(rMt);
200 this->installNewMetric(frame);
209 Transform3D rMt = mSpaceProvider->getActiveToolTipTransform(ref,
true);
211 frame->setSpace(ref);
212 frame->setFrame(rMt);
213 frame->setToolName(mTrackingService->getActiveTool()->getName());
214 frame->setToolOffset(mTrackingService->getActiveTool()->getTooltipOffset());
216 this->installNewMetric(frame);
224 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(p1->getArguments());
225 for (
unsigned i=0; i<args.size(); ++i)
226 p1->getArguments()->set(i, args[i]);
228 this->installNewMetric(p1);
235 std::vector<DataPtr> MetricManager::refinePointArguments(std::vector<DataPtr> args,
unsigned argNo)
239 for (
unsigned i=0; i<args.size();)
241 if (args.size() <= argNo)
243 if (!mSelection.count(args[i]->getUid()))
244 args.erase(args.begin()+i);
249 while (args.size() > argNo)
250 args.erase(args.begin());
252 while (args.size() < argNo)
265 this->installNewMetric(d0);
278 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments(), 3);
279 d0->getArguments()->set(0, args[0]);
280 d0->getArguments()->set(1, args[1]);
281 d0->getArguments()->set(2, args[1]);
282 d0->getArguments()->set(3, args[2]);
284 this->installNewMetric(d0);
291 std::vector<DataMetricPtr> metrics = this->getAllMetrics();
293 std::vector<DataPtr> args;
294 for (
unsigned i=0; i<metrics.size(); ++i)
296 if (arguments->validArgument(metrics[i]))
297 args.push_back(metrics[i]);
300 if (numberOfRequiredArguments<0)
301 numberOfRequiredArguments = arguments->getCount();
302 args = this->refinePointArguments(args, numberOfRequiredArguments);
311 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
312 d0->getArguments()->set(0, args[0]);
314 this->installNewMetric(d0);
321 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
322 d0->getArguments()->set(0, args[0]);
323 d0->getArguments()->set(1, args[1]);
325 this->installNewMetric(d0);
332 std::vector<DataPtr> args = this->getSpecifiedNumberOfValidArguments(d0->getArguments());
333 d0->getArguments()->set(0, args[0]);
334 d0->getArguments()->set(1, args[1]);
336 this->installNewMetric(d0);
344 metric->setColor(prevMetric->getColor());
347 mPatientModelService->insertData(metric);
349 mViewService->getGroup(0)->addData(metric->getUid());
354 ToolPtr refTool = mTrackingService->getReferenceTool();
357 reportDebug(
"No reference tool, cannot load reference points into the pointsampler");
361 std::map<int, Vector3D> referencePoints_s = refTool->getReferencePoints();
362 if(referencePoints_s.empty())
364 reportWarning(
"No referenceppoints in reference tool "+refTool->getName());
371 std::map<int, Vector3D>::iterator it = referencePoints_s.begin();
372 for(; it != referencePoints_s.end(); ++it)
374 Vector3D P_ref = mSpaceProvider->get_toMfrom(sensor, ref).coord(it->second);
382 QDomDocument& doc = orderedDoc.
doc();
383 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
384 QDomElement patientNode = doc.createElement(
"patient");
385 doc.appendChild(patientNode);
386 QDomElement managersNode = doc.createElement(
"managers");
387 patientNode.appendChild(managersNode);
388 QDomElement datamanagerNode = doc.createElement(
"datamanager");
389 managersNode.appendChild(datamanagerNode);
390 std::map<QString, DataPtr> dataMap = mPatientModelService->getDatas();
392 std::map<QString, DataPtr>::iterator iter;
393 for (iter = dataMap.begin(); iter != dataMap.end(); ++iter)
398 QDomElement dataNode = doc.createElement(
"data");
400 datamanagerNode.appendChild(dataNode);
407 void MetricManager::resolveUnknownParentSpacesForPointMetrics(QDomNode dataNode, std::map<QString, QString> mapping_of_unknown_to_known_spaces,
DataPtr data)
409 QString uid = data->getUid();
414 QString string_space = dataNode.toElement().attribute(
"space");
417 bool parent_found = mPatientModelService->getData(parentSpace.
mRefObject) !=
DataPtr();
418 if(need_parent && !parent_found)
420 if(mapping_of_unknown_to_known_spaces.find(string_space) == mapping_of_unknown_to_known_spaces.end())
425 "Select parent coordinate system of metric with uid: "+uid);
426 space_property->setSpaceProvider(mSpaceProvider);
427 QWidget* widget =
new QWidget;
428 widget->setFocusPolicy(Qt::StrongFocus);
429 QVBoxLayout *layout =
new QVBoxLayout();
430 layout->addWidget(
new QLabel(
"Select parent space for Point metric: "+uid+
"."));
433 dialog.setWindowTitle(
"Space "+string_space+
" does not exist.");
434 dialog.setLayout(layout);
435 QPushButton *okButton =
new QPushButton(tr(
"Ok"));
436 layout->addWidget(okButton);
437 connect(okButton, &QAbstractButton::clicked, &dialog, &QWidget::close);
439 CX_LOG_DEBUG() <<
"New space is now: " << space_property->getValue().mId <<
" " << space_property->getValue().mRefObject;
441 mapping_of_unknown_to_known_spaces[string_space] = new_parentspace.
toString();
444 point_metric->setSpace(parentSpace);
445 point_metric->setCoordinate(Vector3D::fromString(dataNode.toElement().attribute(
"coord")));
446 dataNode.toElement().setAttribute(
"space", parentSpace.
toString());
453 QDomElement patientNode = xml.documentElement();
455 std::map<DataPtr, QDomNode> datanodes;
457 QDomNode managersNode = patientNode.firstChildElement(
"managers");
458 QDomNode datamanagerNode = managersNode.firstChildElement(
"datamanager");
459 QDomNode dataNode = datamanagerNode.firstChildElement(
"data");
462 std::map<QString, QString> mapping_of_unknown_to_known_spaces;
464 for (; !dataNode.isNull(); dataNode = dataNode.nextSibling())
466 QDomNamedNodeMap attributes = dataNode.attributes();
467 QDomNode typeAttribute = attributes.namedItem(
"type");
468 bool isMetric =
false;
469 if(typeAttribute.isAttr())
471 isMetric = typeAttribute.toAttr().value().contains(
"Metric");
474 if (dataNode.nodeName() ==
"data" && isMetric)
477 QString uid = dataNode.toElement().attribute(
"uid");
478 if(mPatientModelService->getData(uid))
480 QString name = dataNode.toElement().attribute(
"name");
481 reportWarning(
"Metric: " + name +
", is already in the model with Uid: " + uid +
". Import skipped.");
485 DataPtr data = this->loadDataFromXMLNode(dataNode.toElement());
487 datanodes[data] = dataNode.toElement();
490 this->resolveUnknownParentSpacesForPointMetrics(dataNode, mapping_of_unknown_to_known_spaces, data);
496 for (std::map<DataPtr, QDomNode>::iterator iter = datanodes.begin(); iter != datanodes.end(); ++iter)
498 iter->first->parseXml(iter->second);
502 QColor MetricManager::getRandomColor()
504 QStringList colorNames = QColor::colorNames();
505 int random_int = rand() % colorNames.size();
506 QColor color(colorNames[random_int]);
507 if(color == QColor(
"black"))
508 color = getRandomColor();
513 std::vector<QString> MetricManager::dialogForSelectingVolumesForImportedMNITagFile(
int number_of_volumes, QString description)
515 std::vector<QString> data_uid;
517 QDialog selectVolumeDialog;
518 selectVolumeDialog.setWindowTitle(
"Select volume(s) related to points in MNI Tag Point file.");
520 QVBoxLayout *layout =
new QVBoxLayout();
521 QLabel *description_label =
new QLabel(description);
522 layout->addWidget(description_label);
524 std::map<int, StringPropertySelectImagePtr> selectedImageProperties;
525 for(
int i=0; i < number_of_volumes; ++i)
528 QWidget *widget =
createDataWidget(mViewService, mPatientModelService, NULL, image_property);
529 layout->addWidget(widget);
530 selectedImageProperties[i] = image_property;
533 QPushButton *okButton =
new QPushButton(tr(
"Ok"));
534 layout->addWidget(okButton);
535 connect(okButton, &QAbstractButton::clicked, &selectVolumeDialog, &QWidget::close);
536 selectVolumeDialog.setLayout(layout);
537 selectVolumeDialog.exec();
538 for(
int i=0; i < number_of_volumes; ++i)
541 data_uid.push_back(image_property->getValue());
553 reader->SetFileName(filename.toStdString().c_str());
560 int number_of_volumes = reader->GetNumberOfVolumes();
561 QString description(reader->GetComments());
562 std::vector<QString> data_uid;
563 data_uid.push_back(
"");
564 data_uid.push_back(
"");
566 data_uid = dialogForSelectingVolumesForImportedMNITagFile(number_of_volumes, description);
569 QString type =
"pointMetric";
572 vtkStringArray *labels = reader->GetLabelText();
574 for(
int i=0; i< number_of_volumes; ++i)
576 QColor color = getRandomColor();
578 vtkPoints *points = reader->GetPoints(i);
581 unsigned int number_of_points = points->GetNumberOfPoints();
584 for(
int j=0; j < number_of_points; ++j)
586 vtkStdString label = labels->GetValue(j);
587 name = QString(*label);
590 double *point = points->GetPoint(j);
591 DataPtr data = this->createData(type, uid, QString::number(j+1));
595 Vector3D vector_ras(point[0], point[1], point[2]);
600 Vector3D vector_lps = sMr.inv() * vector_ras;
602 point_metric->setCoordinate(vector_lps);
603 point_metric->setSpace(space);
604 point_metric->setColor(color);
611 DataPtr MetricManager::loadDataFromXMLNode(QDomElement node)
613 QString uid = node.toElement().attribute(
"uid");
614 QString name = node.toElement().attribute(
"name");
615 QString type = node.toElement().attribute(
"type");
617 return this->createData(type, uid, name);
621 DataPtr MetricManager::createData(QString type, QString uid, QString name)
623 DataPtr data = mPatientModelService->createData(type, uid, name);
626 reportError(QString(
"Not able to create metric with name: %1, uid: %2, type: %3").arg(name).arg(uid).arg(type));
633 mPatientModelService->insertData(data);
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
static void writeXmlFile(QDomDocument &doc, QString &filename)
pcsRAS
Right-Anterior-Superior, used by Slicer3D, ITK-Snap, nifti, MINC.
void reportError(QString msg)
void exportMetricsToXMLFile(QString &filename)
boost::shared_ptr< class DistanceMetric > DistanceMetricPtr
void addFrameButtonClickedSlot()
void addXml(QDomNode &dataNode)
adds xml information about the data and its variabels
boost::shared_ptr< class FrameMetric > FrameMetricPtr
boost::shared_ptr< class ToolMetric > ToolMetricPtr
void addSphereButtonClickedSlot()
boost::shared_ptr< class DonutMetric > DonutMetricPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< DataMetric > DataMetricPtr
cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
boost::shared_ptr< class TrackingService > TrackingServicePtr
void addCustomButtonClickedSlot()
void addAngleButtonClickedSlot()
QString timestampMilliSecondsFormat()
static SpacePropertyPtr initialize(const QString &uid, QString name, QString help, Space value=Space(), std::vector< Space > range=std::vector< Space >(), QDomNode root=QDomNode())
boost::shared_ptr< class SpaceProperty > SpacePropertyPtr
void addToolButtonClickedSlot()
csREF
the data reference space (r) using LPS (left-posterior-superior) coordinates.
boost::shared_ptr< class SphereMetric > SphereMetricPtr
DistanceMetricPtr addDistance(QString uid="distance%1")
Data class that represents a single frame (transform).
Data class representing a plane.
static CoordinateSystem reference()
MetricManager(ViewServicePtr viewService, PatientModelServicePtr patientModelService, TrackingServicePtr trackingService, SpaceProviderPtr spaceProvider)
boost::shared_ptr< class AngleMetric > AngleMetricPtr
QWidget * createDataWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
COORDINATE_SYSTEM mId
the type of coordinate system
Data class that represents a donut.
boost::shared_ptr< class PlaneMetric > PlaneMetricPtr
void loadReferencePointsSlot()
boost::shared_ptr< class Data > DataPtr
void setSelection(std::set< QString > selection)
virtual RegistrationHistoryPtr get_rMd_History()
void importMetricsFromMNITagFile(QString &filename, bool testmode=false)
Note: testmode is available to skip dialog popup for running automatic tests.
void reportWarning(QString msg)
void addPlaneButtonClickedSlot()
Data class that represents a custom.
Data class that represents a donut.
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
boost::shared_ptr< class MetricReferenceArgumentList > MetricReferenceArgumentListPtr
DataMetricPtr getMetric(QString uid)
Transform3D createTransformTranslate(const Vector3D &translation)
Identification of a Coordinate system.
Data class that represents a distance between two points, or a point and a plane. ...
boost::shared_ptr< class RegionOfInterestMetric > RegionOfInterestMetricPtr
QString mRefObject
for tool, sensor and data we need a object uid to define the coordinate system
void addPointButtonClickedSlot()
boost::shared_ptr< class StringPropertySelectImage > StringPropertySelectImagePtr
Data class that represents a single point.
void importMetricsFromXMLFile(QString &filename)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
void forceNewlineBeforeEof(QString path)
static QDomDocument readXmlFile(QString &filename)
void addDonutButtonClickedSlot()
static bool checkedRead(vtkSmartPointer< vtkAlgorithm > reader, QString filename)
PointMetricPtr addPoint(Vector3D point, CoordinateSystem space=CoordinateSystem(csREF), QString uid="point%1", QColor color=QColor(240, 170, 255, 255))
static StringPropertySelectImagePtr New(PatientModelServicePtr patientModelService)
void addROIButtonClickedSlot()
void activeMetricChanged()
void addDistanceButtonClickedSlot()
int getNumberOfMetrics() const
Base class for all Data Metrics.
boost::shared_ptr< class CustomMetric > CustomMetricPtr
vtkSmartPointer< class vtkMNITagPointReader > vtkMNITagPointReaderPtr
Data class that represents an angle between two lines.
void moveToMetric(QString uid)
static CoordinateSystem fromString(QString text)
void reportDebug(QString msg)
void setActiveUid(QString uid)
Namespace for all CustusX production code.
boost::shared_ptr< class PointMetric > PointMetricPtr
boost::shared_ptr< class Tool > ToolPtr