Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkAbstractToFDeviceFactory.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 ============================================================================*/
13 #include <mitkCameraIntrinsics.h>
15 //Microservices
16 #include <usModuleContext.h>
17 #include <usGetModuleContext.h>
18 #include <usModule.h>
19 #include <usModuleResource.h>
20 #include <usModuleResourceStream.h>
21 
22 #include <tinyxml.h>
23 
24 mitk::ToFCameraDevice::Pointer mitk::AbstractToFDeviceFactory::ConnectToFDevice()
25 {
26  ToFCameraDevice::Pointer device = CreateToFCameraDevice();
27  mitk::CameraIntrinsics::Pointer cameraIntrinsics = GetCameraIntrinsics();
28  device->SetProperty("CameraIntrinsics", mitk::CameraIntrinsicsProperty::New(cameraIntrinsics));
29  m_Devices.push_back(device);
30 
31  us::ModuleContext* context = us::GetModuleContext();
32  us::ServiceProperties deviceProps;
33  deviceProps["ToFDeviceName"] = GetCurrentDeviceName();
34  m_DeviceRegistrations.insert(std::make_pair(device.GetPointer(), context->RegisterService(device.GetPointer(),deviceProps)));
35  return device;
36 }
37 
38 void mitk::AbstractToFDeviceFactory::DisconnectToFDevice(const ToFCameraDevice::Pointer& device)
39 {
40  std::map<ToFCameraDevice*,us::ServiceRegistration<ToFCameraDevice> >::iterator i = m_DeviceRegistrations.find(device.GetPointer());
41  if (i == m_DeviceRegistrations.end()) return;
42 
43  i->second.Unregister();
44  m_DeviceRegistrations.erase(i);
45 
46  m_Devices.erase(std::remove(m_Devices.begin(), m_Devices.end(), device), m_Devices.end());
47 }
48 
50 {
51  return m_Devices.size();
52 }
53 
54 mitk::CameraIntrinsics::Pointer mitk::AbstractToFDeviceFactory::GetCameraIntrinsics()
55 {
57  if (! resource.IsValid())
58  {
59  MITK_WARN << "Could not load resource '" << resource.GetName() << "'. CameraIntrinsics are invalid!";
60  }
61 
62  // Create ResourceStream from Resource
63  us::ModuleResourceStream resStream(resource);
64 
65  // Parse XML
66  TiXmlDocument xmlDocument;
67  resStream >> xmlDocument;
68 
69  //Retrieve Child Element and convert to CamerIntrinsics
70  TiXmlElement* element = xmlDocument.FirstChildElement();
71  mitk::CameraIntrinsics::Pointer intrinsics = mitk::CameraIntrinsics::New();
72  intrinsics->FromXML(element);
73 
74  return intrinsics;
75 }
76 
78 {
80  return module->GetResource("CalibrationFiles/Default_Parameters.xml");
81  MITK_WARN << "Loaded Default CameraIntrinsics. Overwrite AbstractToFDeviceFactory::GetIntrinsicsResource() if you want to define your own.";
82 }
std::vector< ToFCameraDevice::Pointer > m_Devices
m_Devices A list (vector) containing all connected devices of the respective factory.
std::string GetCurrentDeviceName() override
GetCurrentDeviceName Get the human readable name of the current device. A factory can produce many de...
virtual ToFCameraDevice::Pointer CreateToFCameraDevice()=0
CreateToFCameraDevice Create a new device of the respective factory. E.g. a "KinectFactory" creates a...
CameraIntrinsics::Pointer GetCameraIntrinsics()
Returns the CameraIntrinsics for the cameras created by this factory.
#define MITK_WARN
Definition: mitkLogMacros.h:19
vcl_size_t GetNumberOfDevices()
GetNumberOfDevices Get the number of devices produced by this factory. This function will return the ...
void DisconnectToFDevice(const ToFCameraDevice::Pointer &device)
DisconnectToFDevice Use this method to disconnect a device.
Module * GetModule() const
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
static Pointer New()
virtual us::ModuleResource GetIntrinsicsResource()
Returns the ModuleResource that contains a xml definition of the CameraIntrinsics.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
ToFCameraDevice::Pointer ConnectToFDevice()
ConnectToFDevice Use this method to connect a device.
std::map< ToFCameraDevice *, us::ServiceRegistration< ToFCameraDevice > > m_DeviceRegistrations
m_DeviceRegistrations A map containing all the pairs of device registration numbers and devices...
std::string GetName() const