CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxTrackedCenterlineWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include <QVBoxLayout>
36 #include "cxTrackingService.h"
37 #include "cxMeshHelpers.h"
38 #include "cxToolRep3D.h"
39 #include "cxToolTracer.h"
40 //#include "cxTrackingDataToVolume.h"
41 #include "cxRecordSessionWidget.h"
42 #include "cxRecordSession.h"
43 #include "cxTool.h"
44 #include "cxLogger.h"
45 #include "cxView.h"
46 #include "cxViewService.h"
47 #include "cxRepContainer.h"
48 #include "cxAcquisitionService.h"
49 #include "cxVisServices.h"
50 
51 namespace cx
52 {
53 
55  TrackedRecordWidget(acquisitionService, parent, "Tracked centerline")
56 {
57  this->setObjectName("TrackedCenterlineWidget");
58  this->setWindowTitle("Tracked Centerline");
59  this->setToolTip("Record the tool movement as a centerline");
60 
61  mServices = services;
62  connect(mServices->getToolManager().get(), &TrackingService::stateChanged, this, &TrackedCenterlineWidget::checkIfReadySlot);
63  mLayout->addStretch();
64 
65  this->checkIfReadySlot();
66 }
67 
69 {}
70 
72 {
73  if(mServices->getToolManager()->getState()>=Tool::tsTRACKING)
74  {
75  mRecordSessionWidget->setReady(true, "<font color=green>Ready to record!</font>\n");
76  }
77  else
78  {
79  mRecordSessionWidget->setReady(false, "<font color=red>Need to start tracking.</font>\n");
80  }
81 }
82 
84 {
85 // RecordSessionPtr session = mPluginData->getRecordSession(sessionId);
86 //
87 // //get the transforms from the session
88 // TimedTransformMap transforms_prMt = this->getRecording(session);
89 // if(transforms_prMt.empty())
90 // {
91 // reportError("Could not find any tracking data from session "+sessionId+". Aborting volume tracking data generation.");
92 // return;
93 // }
94 //
95 // //visualize the tracked data as a mesh
96 // loadMeshFromToolTransforms(transforms_prMt);
97 //
98 // //convert the transforms into a binary image
99 // TrackingDataToVolume converter;
100 // int padding = 10;
101 // converter.setInput(transforms_prMt, padding);
102 // ImagePtr image_d = converter.getOutput();
103 //
104 // //extract the centerline
105 // QString savepath = patientService()->getPatientData()->getActivePatientFolder();
106 // mCenterlineAlgorithm.setInput(image_d, savepath);
107  mSessionID = sessionId;
108 // mCenterlineAlgorithm.execute();
109 // mRecordSessionWidget->setReady(false, "<font color=orange>Generating centerline... Please wait!</font>\n");
110 }
111 
113 {
114  RecordSessionPtr session = mAcquisitionService->getSession(mSessionID);
115 
116  //get the transforms from the session
117  TimedTransformMap transforms_prMt = this->getRecording(session);
118  if(transforms_prMt.empty())
119  {
120  reportError("Could not find any tracking data from session "+mSessionID+". Aborting volume tracking data generation.");
121  return;
122  }
123 
124  //visualize the tracked data as a mesh
125  loadMeshFromToolTransforms(mServices->getPatientService(), transforms_prMt);
126 
127  //convert the transforms into a binary image
128 // TrackingDataToVolume converter;
129 // int padding = 10;
130 // converter.setInput(transforms_prMt, padding);
131 // ImagePtr image_d = converter.getOutput();
132 
133  //extract the centerline
134 // QString savepath = patientService()->getPatientData()->getActivePatientFolder();
135 // mCenterlineAlgorithm.setInput(image_d, savepath);
136  mRecordSessionWidget->setReady(false, "<font color=orange>Generating centerline... Please wait!</font>\n");
137 }
138 
139 
141 {
142  this->checkIfReadySlot();
143 }
144 
146 {
147  //show preview of tool path
148  TrackingService::ToolMap tools = mServices->getToolManager()->getTools();
149  TrackingService::ToolMap::iterator toolIt = tools.begin();
150 
151  ToolRep3DPtr activeRep3D;
152  for(; toolIt != tools.end(); ++toolIt)
153  {
154  activeRep3D = mServices->visualizationService->get3DReps()->findFirst<ToolRep3D>(toolIt->second);
155  if(!activeRep3D)
156  continue;
157  activeRep3D->getTracer()->clear();
158  activeRep3D->getTracer()->start();
159  }
160 }
161 
163 {
164  //hide preview of tool path
165  TrackingService::ToolMap tools = mServices->getToolManager()->getTools();
166  TrackingService::ToolMap::iterator toolIt = tools.begin();
167 
168  ToolRep3DPtr activeRep3D;
169  for(; toolIt != tools.end(); ++toolIt)
170  {
171  activeRep3D = mServices->visualizationService->get3DReps()->findFirst<ToolRep3D>(toolIt->second);
172  if(!activeRep3D)
173  continue;
174  if (activeRep3D->getTracer()->isRunning())
175  {
176  activeRep3D->getTracer()->stop();
177  activeRep3D->getTracer()->clear();
178  }
179  }
180 }
181 TimedTransformMap TrackedCenterlineWidget::getRecording(RecordSessionPtr session)
182 {
183  TimedTransformMap retval;
184 
185  double startTime = session->getStartTime();
186  double stopTime = session->getStopTime();
187 
188  ToolPtr tool = this->findTool(startTime, stopTime);
189  if(!tool)
190  {
191  reportWarning("Found no tool with tracking data from the given session.");
192  return retval;
193  }
194  this->setTool(tool);
195  retval = tool->getSessionHistory(startTime, stopTime);
196 
197  return retval;
198 }
199 
200 ToolPtr TrackedCenterlineWidget::findTool(double startTime, double stopTime)
201 {
202  ToolPtr retval;
203 
204  SessionToolHistoryMap toolTransformMap = mServices->getToolManager()->getSessionHistory(startTime, stopTime);
205  if(toolTransformMap.size() == 1)
206  {
207  report("Found one tool("+toolTransformMap.begin()->first->getName()+") with relevant data.");
208  retval = toolTransformMap.begin()->first;
209  }
210  else if(toolTransformMap.size() > 1)
211  {
212  reportWarning("Found more than one tool with relevant data, user needs to choose which one to use for tracked centerline extraction.");
213  //TODO make the user select which tool they wanna use!!! Pop-up???
214  retval = toolTransformMap.begin()->first;
215  //TODO
216  }else if(toolTransformMap.empty())
217  {
218  reportWarning("Could not find any session history for given session.");
219  }
220  return retval;
221 }
222 }//namespace cx
void reportError(QString msg)
Definition: cxLogger.cpp:92
boost::shared_ptr< class AcquisitionService > AcquisitionServicePtr
void setTool(ToolPtr tool)
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
std::map< ToolPtr, TimedTransformMap > SessionToolHistoryMap
Display a Tool in 3D.
Definition: cxToolRep3D.h:72
boost::shared_ptr< class RecordSession > RecordSessionPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void setReady(bool val, QString text)
deprecated: use readinessChangedSlot instead.
void postProcessingSlot(QString sessionId)
void loadMeshFromToolTransforms(PatientModelServicePtr dataManager, TimedTransformMap transforms_prMt)
AcquisitionServicePtr mAcquisitionService
cxLogicManager_EXPORT AcquisitionServicePtr acquisitionService()
std::map< QString, ToolPtr > ToolMap
RecordSessionWidget * mRecordSessionWidget
void report(QString msg)
Definition: cxLogger.cpp:90
emitting tracking data
Definition: cxTool.h:83
TrackedCenterlineWidget(AcquisitionServicePtr acquisitionService, VisServicesPtr services, QWidget *parent)
ToolTracerPtr getTracer()
Definition: cxToolRep3D.cpp:91
boost::shared_ptr< class ToolRep3D > ToolRep3DPtr
std::map< double, Transform3D > TimedTransformMap
boost::shared_ptr< class Tool > ToolPtr