Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkTrackingVolumeGenerator.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
15 #include "mitkConfig.h"
16 #include <vtkCubeSource.h>
17 #include <mitkTrackingTypes.h>
18 #include <mitkTrackingDevice.h>
20 #include <vtkSmartPointer.h>
21 #include <mitkSurface.h>
22 #include <mitkIOMimeTypes.h>
23 #include <mitkFileReaderRegistry.h>
24 
25 #include <usModuleContext.h>
26 #include <usGetModuleContext.h>
27 #include <usModule.h>
28 #include <usModuleResource.h>
29 #include <usModuleResourceStream.h>
30 
31 #include <mitkCoreServices.h>
32 #include <mitkIFileReader.h>
33 #include <mitkIOUtil.h>
34 #include <mitkIOMimeTypes.h>
35 #include <mitkMimeType.h>
36 #include <mitkIMimeTypeProvider.h>
37 
40 
41 #include <algorithm>
42 
43 namespace {
44 
48 mitk::Surface::Pointer LoadCoreSurface(const us::ModuleResource& usResource)
49 {
50  us::ModuleResourceStream resStream(usResource, std::ios_base::in);
51 
53  // get mime types for file (extension) and for surfaces
54  std::vector<mitk::MimeType> mimetypesForFile = mimeTypeProvider->GetMimeTypesForFile(usResource.GetResourcePath());
55  std::vector<mitk::MimeType> mimetypesForSurface = mimeTypeProvider->GetMimeTypesForCategory(mitk::IOMimeTypes::CATEGORY_SURFACES());
56 
57  // construct our candidates as the intersection of both sets because we need something that
58  // handles the type type _and_ produces a surface out of it
59  std::vector<mitk::MimeType> mimetypes;
60 
61  std::sort(mimetypesForFile.begin(), mimetypesForFile.end());
62  std::sort(mimetypesForSurface.begin(), mimetypesForSurface.end());
63  std::set_intersection(mimetypesForFile.begin(), mimetypesForFile.end(),
64  mimetypesForSurface.begin(), mimetypesForSurface.end(),
65  std::back_inserter(mimetypes));
66 
67  mitk::Surface::Pointer surface;
68  if (mimetypes.empty())
69  {
70  mitkThrow() << "No mimetype for resource stream: " << usResource.GetResourcePath();
71  return surface;
72  }
73 
74  mitk::FileReaderRegistry fileReaderRegistry;
75  std::vector<us::ServiceReference<mitk::IFileReader>> refs = fileReaderRegistry.GetReferences(mimetypes[0]);
76  if (refs.empty())
77  {
78  mitkThrow() << "No reader available for resource stream: " << usResource.GetResourcePath();
79  return surface;
80  }
81 
82  mitk::IFileReader *reader = fileReaderRegistry.GetReader(refs[0]);
83  reader->SetInput(usResource.GetResourcePath(), &resStream);
84  auto basedatas = reader->Read();
85  if (!basedatas.empty())
86  {
87  surface = dynamic_cast<mitk::Surface*>(basedatas.front().GetPointer());
88  }
89 
90  return surface;
91 }
92 
93 } // unnamed namespace
94 
95 
97 {
99 }
100 
101 void mitk::TrackingVolumeGenerator::SetTrackingDevice (mitk::TrackingDevice::Pointer tracker)
102 {
103  std::vector<us::ServiceReference<mitk::TrackingDeviceTypeCollection> > refs = us::GetModuleContext()->GetServiceReferences<mitk::TrackingDeviceTypeCollection>();
104  if (refs.empty())
105  {
106  MITK_ERROR << "No tracking device service found!";
107  }
109 
110  this->m_Data = deviceTypeCollection->GetFirstCompatibleDeviceDataForLine(tracker->GetType());
111 }
112 
114 {
115  mitk::Surface::Pointer output = this->GetOutput(); //the surface wich represents the tracking volume
116 
117  std::string filename = this->m_Data.VolumeModelLocation; // Name of the file or possibly a magic String, e.g. "cube"
118 
119  MITK_INFO << "volume: " << filename;
120 
121  // See if filename matches a magic string.
122  if (filename.compare("cube") == 0){
123  vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();
124  double bounds[6];
125  bounds[0] = bounds[2] = bounds[4] = -400.0; // initialize bounds to -400 ... +400 cube. This is the default value of the
126  bounds[1] = bounds[3] = bounds[5] = 400.0; // virtual tracking device, but it can be changed. In that case,
127  // the tracking volume polydata has to be updated manually
128  cubeSource->SetBounds(bounds);
129  cubeSource->Update();
130 
131  output->SetVtkPolyData(cubeSource->GetOutput()); //set the vtkCubeSource as polyData of the surface
132  return;
133  }
134  if (filename.compare("") == 0) // empty String means no model, return empty output
135  {
136  // initialize with empty poly data (otherwise old surfaces may be returned) => so an empty surface is returned
137  vtkSmartPointer<vtkPolyData> emptyPolyData = vtkSmartPointer<vtkPolyData>::New();
138  output->SetVtkPolyData(emptyPolyData);
139  return;
140  }
141 
142  // from here on, we assume that filename contains an actual filename and not a magic string
143 
145  us::ModuleResource moduleResource = module->GetResource(filename);
146 
147  // TODO one would want to call mitk::IOUtils::Load(moduleResource) here.
148  // However this function is not guaranteed to find a reader that loads
149  // named resource as a Surface (given the presence of alternative readers
150  // that produce another data type but has a higher ranking than the core
151  // surface reader) - see bug T22608.
152  mitk::Surface::Pointer fileoutput = LoadCoreSurface(moduleResource);
153 
154  if (fileoutput == nullptr)
155  {
156  MITK_ERROR << "Exception while casting data loaded from file: " << moduleResource.GetResourcePath();
157  output->SetVtkPolyData(vtkSmartPointer<vtkPolyData>(vtkPolyData::New()));
158  }
159  else
160  {
161  output->SetVtkPolyData(fileoutput->GetVtkPolyData());
162  }
163 }
164 
166 {
167  std::vector<us::ServiceReference<mitk::TrackingDeviceTypeCollection> > refs = us::GetModuleContext()->GetServiceReferences<mitk::TrackingDeviceTypeCollection>();
168  if (refs.empty())
169  {
170  MITK_ERROR << "No tracking device service found!";
171  }
173 
174  m_Data = deviceTypeCollection->GetFirstCompatibleDeviceDataForLine(deviceType);
175 }
176 
178 {
179  return m_Data.Line;
180 }
181 
183 {
184  m_Data= deviceData;
185 }
186 
188 {
189  return m_Data;
190 }
mitk::TrackingDeviceData GetTrackingDeviceData() const
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:28
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
#define MITK_INFO
Definition: mitkLogMacros.h:18
mitk::IFileReader * GetReader(const ReaderReference &ref, us::ModuleContext *context=us::GetModuleContext())
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void SetTrackingDevice(mitk::TrackingDevice::Pointer tracker)
Deprecated! Use set DeviceData instead. Sets the tracking device type of the volume. After doing this the tracking volume gets generated and is set to the correct dimensions in the correct coordinate system. The TV of a VirtualTrackingDevice is always a 400*400 cube.
OutputType * GetOutput()
This class is a collection for information of all Tracking Device Types (derived from abstract Tracki...
mitk::TrackingDeviceType GetTrackingDeviceType() const
void SetTrackingDeviceType(mitk::TrackingDeviceType deviceType)
Deprecated! Use set DeviceData instead. Sets the tracking device type of the volume. Warning: there are different possible volumes for some device types. In this case a default volume is chosen automatically. All tracking volumes are defined by TrackingDeviceData objects (see file mitkTrackingTypes.h) for a list.
TrackingDeviceType Line
virtual std::vector< itk::SmartPointer< BaseData > > Read()=0
Reads the specified file or input stream and returns its contents.
void * GetService(const ServiceReferenceBase &reference)
void SetTrackingDeviceData(mitk::TrackingDeviceData deviceData)
Sets the tracking device data object which will be used to generate the volume. Each tracking device ...
virtual void SetInput(const std::string &location)=0
Set the input location.
TrackingDeviceData GetFirstCompatibleDeviceDataForLine(TrackingDeviceType type)
#define mitkThrow()
Module * GetModule() const
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
std::vector< ServiceReferenceU > GetServiceReferences(const std::string &clazz, const std::string &filter=std::string())
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
std::string TrackingDeviceType
std::string GetResourcePath() const
A RAII helper class for core service objects.
static std::vector< ReaderReference > GetReferences(const MimeType &mimeType, us::ModuleContext *context=us::GetModuleContext())
The common interface for all MITK file readers.
mitk::TrackingDeviceData m_Data
Holds the current tracking device data object, which is used to generate the volume.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static std::string CATEGORY_SURFACES()