CustusX  15.3.4-beta
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 
60  mServices = services;
61  connect(mServices->getToolManager().get(), &TrackingService::stateChanged, this, &TrackedCenterlineWidget::checkIfReadySlot);
62  mLayout->addStretch();
63 
64  this->checkIfReadySlot();
65 }
66 
68 {}
69 
71 {
72  return "<html>"
73  "<h3>Tracked Centerline.</h3>"
74  "<p><i>Record the tool movement as a centerline.</i></br>"
75  "</html>";
76 }
77 
79 {
80  if(mServices->getToolManager()->getState()>=Tool::tsTRACKING)
81  {
82  mRecordSessionWidget->setReady(true, "<font color=green>Ready to record!</font>\n");
83  }
84  else
85  {
86  mRecordSessionWidget->setReady(false, "<font color=red>Need to start tracking.</font>\n");
87  }
88 }
89 
91 {
92 // RecordSessionPtr session = mPluginData->getRecordSession(sessionId);
93 //
94 // //get the transforms from the session
95 // TimedTransformMap transforms_prMt = this->getRecording(session);
96 // if(transforms_prMt.empty())
97 // {
98 // reportError("Could not find any tracking data from session "+sessionId+". Aborting volume tracking data generation.");
99 // return;
100 // }
101 //
102 // //visualize the tracked data as a mesh
103 // loadMeshFromToolTransforms(transforms_prMt);
104 //
105 // //convert the transforms into a binary image
106 // TrackingDataToVolume converter;
107 // int padding = 10;
108 // converter.setInput(transforms_prMt, padding);
109 // ImagePtr image_d = converter.getOutput();
110 //
111 // //extract the centerline
112 // QString savepath = patientService()->getPatientData()->getActivePatientFolder();
113 // mCenterlineAlgorithm.setInput(image_d, savepath);
114  mSessionID = sessionId;
115 // mCenterlineAlgorithm.execute();
116 // mRecordSessionWidget->setReady(false, "<font color=orange>Generating centerline... Please wait!</font>\n");
117 }
118 
120 {
121  RecordSessionPtr session = mAcquisitionService->getSession(mSessionID);
122 
123  //get the transforms from the session
124  TimedTransformMap transforms_prMt = this->getRecording(session);
125  if(transforms_prMt.empty())
126  {
127  reportError("Could not find any tracking data from session "+mSessionID+". Aborting volume tracking data generation.");
128  return;
129  }
130 
131  //visualize the tracked data as a mesh
132  loadMeshFromToolTransforms(mServices->getPatientService(), transforms_prMt);
133 
134  //convert the transforms into a binary image
135 // TrackingDataToVolume converter;
136 // int padding = 10;
137 // converter.setInput(transforms_prMt, padding);
138 // ImagePtr image_d = converter.getOutput();
139 
140  //extract the centerline
141 // QString savepath = patientService()->getPatientData()->getActivePatientFolder();
142 // mCenterlineAlgorithm.setInput(image_d, savepath);
143  mRecordSessionWidget->setReady(false, "<font color=orange>Generating centerline... Please wait!</font>\n");
144 }
145 
146 
148 {
149  this->checkIfReadySlot();
150 }
151 
153 {
154  //show preview of tool path
155  TrackingService::ToolMap tools = mServices->getToolManager()->getTools();
156  TrackingService::ToolMap::iterator toolIt = tools.begin();
157 
158  ToolRep3DPtr activeRep3D;
159  for(; toolIt != tools.end(); ++toolIt)
160  {
161  activeRep3D = mServices->visualizationService->get3DReps()->findFirst<ToolRep3D>(toolIt->second);
162  if(!activeRep3D)
163  continue;
164  activeRep3D->getTracer()->clear();
165  activeRep3D->getTracer()->start();
166  }
167 }
168 
170 {
171  //hide preview of tool path
172  TrackingService::ToolMap tools = mServices->getToolManager()->getTools();
173  TrackingService::ToolMap::iterator toolIt = tools.begin();
174 
175  ToolRep3DPtr activeRep3D;
176  for(; toolIt != tools.end(); ++toolIt)
177  {
178  activeRep3D = mServices->visualizationService->get3DReps()->findFirst<ToolRep3D>(toolIt->second);
179  if(!activeRep3D)
180  continue;
181  if (activeRep3D->getTracer()->isRunning())
182  {
183  activeRep3D->getTracer()->stop();
184  activeRep3D->getTracer()->clear();
185  }
186  }
187 }
188 TimedTransformMap TrackedCenterlineWidget::getRecording(RecordSessionPtr session)
189 {
190  TimedTransformMap retval;
191 
192  double startTime = session->getStartTime();
193  double stopTime = session->getStopTime();
194 
195  ToolPtr tool = this->findTool(startTime, stopTime);
196  if(!tool)
197  {
198  reportWarning("Found no tool with tracking data from the given session.");
199  return retval;
200  }
201  this->setTool(tool);
202  retval = tool->getSessionHistory(startTime, stopTime);
203 
204  return retval;
205 }
206 
207 ToolPtr TrackedCenterlineWidget::findTool(double startTime, double stopTime)
208 {
209  ToolPtr retval;
210 
211  SessionToolHistoryMap toolTransformMap = mServices->getToolManager()->getSessionHistory(startTime, stopTime);
212  if(toolTransformMap.size() == 1)
213  {
214  report("Found one tool("+toolTransformMap.begin()->first->getName()+") with relevant data.");
215  retval = toolTransformMap.begin()->first;
216  }
217  else if(toolTransformMap.size() > 1)
218  {
219  reportWarning("Found more than one tool with relevant data, user needs to choose which one to use for tracked centerline extraction.");
220  //TODO make the user select which tool they wanna use!!! Pop-up???
221  retval = toolTransformMap.begin()->first;
222  //TODO
223  }else if(toolTransformMap.empty())
224  {
225  reportWarning("Could not find any session history for given session.");
226  }
227  return retval;
228 }
229 }//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:61
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
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
std::map< double, Transform3D > TimedTransformMap
boost::shared_ptr< class Tool > ToolPtr