MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkToFCameraMITKPlayerDeviceTest.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
13#include <mitkTestingMacros.h>
14#include <mitkTestFixture.h>
15#include <mitkIOUtil.h>
18
19//MicroServices
20#include <usModuleContext.h>
21#include <usGetModuleContext.h>
22#include <usServiceReference.h>
23
24class mitkToFCameraMITKPlayerDeviceTestSuite : public mitk::TestFixture
25{
26
33 CPPUNIT_TEST_SUITE_END();
34
35private:
36
37 mitk::ToFCameraMITKPlayerDevice* m_PlayerDevice;
38 std::string m_PathToDepthData;
39 float* m_DistanceArray;
40
41public:
42
43 void setUp() override
44 {
45 us::ModuleContext* context = us::GetModuleContext();
46
47 //Filter all registered devices for an ToFCameraMITKPlayerDevice via device name
48 std::string filter = "(ToFDeviceName=MITK Player)";
49 us::ServiceReference<mitk::ToFCameraDevice> serviceRefDevice = context->GetServiceReferences<mitk::ToFCameraDevice>(filter).front();
50 //Get the actual device
51 m_PlayerDevice = dynamic_cast<mitk::ToFCameraMITKPlayerDevice*>( context->GetService<mitk::ToFCameraDevice>(serviceRefDevice) );
52
53 //during the tests here, we use one special test data set located in MITK-Data
54 m_PathToDepthData = GetTestDataFilePath("ToF-Data/Kinect_Lego_Phantom_DistanceImage.nrrd");
55 m_PlayerDevice->SetProperty("DistanceImageFileName",mitk::StringProperty::New(m_PathToDepthData));
56
57 //initialize an array with the test data size
58 unsigned int numberOfPixels = 640*480;
59 m_DistanceArray = new float[numberOfPixels];
60 }
61
62 void tearDown() override
63 {
64 //Wait some time to avoid threading issues.
65 itksys::SystemTools::Delay(10);
66 //Clean up
67 if(m_PlayerDevice->IsCameraActive())
68 m_PlayerDevice->StopCamera();
69 if(m_PlayerDevice->IsCameraConnected())
70 m_PlayerDevice->DisconnectCamera();
71
72 delete[] m_DistanceArray;
73 }
74
76 {
77 CPPUNIT_ASSERT_MESSAGE("The device (player) should not be active before starting.", m_PlayerDevice->IsCameraActive()==false);
78 }
79
81 {
82 CPPUNIT_ASSERT_MESSAGE("ConnectCamera() should return true in case of success.", m_PlayerDevice->ConnectCamera()==true);
83 }
84
86 {
87 m_PlayerDevice->ConnectCamera();
88 m_PlayerDevice->StartCamera();
89 CPPUNIT_ASSERT_MESSAGE("After starting the device, the device should be active.", m_PlayerDevice->IsCameraActive()==true);
90 }
91
93 {
94 try
95 {
96 m_PlayerDevice->ConnectCamera();
97 m_PlayerDevice->StartCamera();
98 unsigned int dimension[2];
99 dimension[0] = m_PlayerDevice->GetCaptureWidth();
100 dimension[1] = m_PlayerDevice->GetCaptureHeight();
101 int imageSequence = 0;
102
103 //fill the array with the device output
104 m_PlayerDevice->GetDistances(m_DistanceArray,imageSequence);
105
106 //initialize an image and fill it with the array
107 mitk::Image::Pointer resultDepthImage = mitk::Image::New();
108 resultDepthImage->Initialize(mitk::PixelType(mitk::MakeScalarPixelType<float>()), 2, dimension,1);
109 resultDepthImage->SetSlice(m_DistanceArray);
110
111 mitk::Image::Pointer expectedDepthImage = mitk::IOUtil::Load<mitk::Image>(m_PathToDepthData);
112
113 MITK_ASSERT_EQUAL( expectedDepthImage, resultDepthImage,
114 "Image from the player should be the same as loaded from the harddisk, because we just load one slice.");
115 }
116 catch(std::exception &e)
117 {
118 MITK_ERROR << "Unknown exception occured: " << e.what();
119 }
120 }
121
123 {
124 try
125 {
126 int imageSequence = 0;
127 m_PlayerDevice->ConnectCamera();
128 m_PlayerDevice->StartCamera();
129 m_PlayerDevice->GetDistances(m_DistanceArray,imageSequence);
130 m_PlayerDevice->StopCamera();
131 CPPUNIT_ASSERT_MESSAGE("After stopping the device, the device should be inactive.", m_PlayerDevice->IsCameraActive()==false);
132 CPPUNIT_ASSERT_MESSAGE("DisconnectCamera() should return true in case of success.", m_PlayerDevice->DisconnectCamera()==true);
133 }
134 catch(std::exception &e)
135 {
136 MITK_ERROR << "Unknown exception occured: " << e.what();
137 }
138 }
139};
140
141MITK_TEST_SUITE_REGISTRATION(mitkToFCameraMITKPlayerDevice)
Virtual interface and base class for all Time-of-Flight devices.
virtual bool ConnectCamera()
ConnectCamera Internally calls OnConnectCamera() of the respective device implementation.
virtual bool IsCameraActive()
returns true if the camera is connected and started
virtual bool IsCameraConnected()
returns true if the camera is connected
virtual void StopCamera()
stops the continuous updating of the camera
Device class representing a player for MITK-ToF images.
void SetProperty(const char *propertyKey, BaseProperty *propertyValue) override
set a BaseProperty
void GetDistances(float *distanceArray, int &imageSequence) override
gets the distance data from the ToF camera measuring the distance between the camera and the differen...
void StartCamera() override
starts the continuous updating of the camera. A separate thread updates the source data,...
bool DisconnectCamera() override
closes the connection to the camera