diff --git a/Modules/IGT/DataManagement/mitkNavigationDataSource.cpp b/Modules/IGT/DataManagement/mitkNavigationDataSource.cpp index 615175b1d6..381329b9a3 100644 --- a/Modules/IGT/DataManagement/mitkNavigationDataSource.cpp +++ b/Modules/IGT/DataManagement/mitkNavigationDataSource.cpp @@ -1,156 +1,153 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkNavigationDataSource.h" #include "mitkUIDGenerator.h" //Microservices #include #include #include #include const std::string mitk::NavigationDataSource::US_INTERFACE_NAME = "org.mitk.services.NavigationDataSource"; const std::string mitk::NavigationDataSource::US_PROPKEY_DEVICENAME = US_INTERFACE_NAME + ".devicename"; const std::string mitk::NavigationDataSource::US_PROPKEY_ID = US_INTERFACE_NAME + ".id"; const std::string mitk::NavigationDataSource::US_PROPKEY_ISACTIVE = US_INTERFACE_NAME + ".isActive"; mitk::NavigationDataSource::NavigationDataSource() : itk::ProcessObject(), m_Name("NavigationDataSource (no defined type)") { } mitk::NavigationDataSource::~NavigationDataSource() { } mitk::NavigationData* mitk::NavigationDataSource::GetOutput() { if (this->GetNumberOfIndexedOutputs() < 1) return NULL; return static_cast(this->ProcessObject::GetPrimaryOutput()); } mitk::NavigationData* mitk::NavigationDataSource::GetOutput(DataObjectPointerArraySizeType idx) { NavigationData* out = dynamic_cast( this->ProcessObject::GetOutput(idx) ); if ( out == NULL && this->ProcessObject::GetOutput(idx) != NULL ) { itkWarningMacro (<< "Unable to convert output number " << idx << " to type " << typeid( NavigationData ).name () ); } return out; } mitk::NavigationData* mitk::NavigationDataSource::GetOutput(const std::string& navDataName) { - /* DataObjectPointerArray outputs = this->GetOutputs(); for (DataObjectPointerArray::iterator it = outputs.begin(); it != outputs.end(); ++it) if (navDataName == (static_cast(it->GetPointer()))->GetName()) return static_cast(it->GetPointer()); return NULL; - */ - return static_cast(this->ProcessObject::GetOutput(navDataName)); } itk::ProcessObject::DataObjectPointerArraySizeType mitk::NavigationDataSource::GetOutputIndex( std::string navDataName ) { DataObjectPointerArray outputs = this->GetOutputs(); for (DataObjectPointerArray::size_type i = 0; i < outputs.size(); ++i) if (navDataName == (static_cast(outputs.at(i).GetPointer()))->GetName()) return i; throw std::invalid_argument("output name does not exist"); } void mitk::NavigationDataSource::RegisterAsMicroservice(){ // Get Context us::ModuleContext* context = us::GetModuleContext(); // Define ServiceProps us::ServiceProperties props; mitk::UIDGenerator uidGen = mitk::UIDGenerator ("org.mitk.services.NavigationDataSource.id_", 16); props[ US_PROPKEY_ID ] = uidGen.GetUID(); props[ US_PROPKEY_DEVICENAME ] = m_Name; m_ServiceRegistration = context->RegisterService(this, props); } void mitk::NavigationDataSource::UnRegisterMicroservice(){ m_ServiceRegistration.Unregister(); m_ServiceRegistration = 0; } std::string mitk::NavigationDataSource::GetMicroserviceID(){ return this->m_ServiceRegistration.GetReference().GetProperty(US_PROPKEY_ID).ToString(); } void mitk::NavigationDataSource::GraftOutput(itk::DataObject *graft) { this->GraftNthOutput(0, graft); } void mitk::NavigationDataSource::GraftNthOutput(unsigned int idx, itk::DataObject *graft) { if ( idx >= this->GetNumberOfIndexedOutputs() ) { itkExceptionMacro(<<"Requested to graft output " << idx << " but this filter only has " << this->GetNumberOfIndexedOutputs() << " Outputs."); } if ( !graft ) { itkExceptionMacro(<<"Requested to graft output with a NULL pointer object" ); } itk::DataObject* output = this->GetOutput(idx); if ( !output ) { itkExceptionMacro(<<"Requested to graft output that is a NULL pointer" ); } // Call Graft on NavigationData to copy member data output->Graft( graft ); } itk::DataObject::Pointer mitk::NavigationDataSource::MakeOutput ( DataObjectPointerArraySizeType /*idx*/ ) { return mitk::NavigationData::New().GetPointer(); } itk::DataObject::Pointer mitk::NavigationDataSource::MakeOutput( const DataObjectIdentifierType & name ) { itkDebugMacro("MakeOutput(" << name << ")"); if( this->IsIndexedOutputName(name) ) { return this->MakeOutput( this->MakeIndexFromOutputName(name) ); } return static_cast(mitk::NavigationData::New().GetPointer()); } mitk::PropertyList::ConstPointer mitk::NavigationDataSource::GetParameters() const { mitk::PropertyList::Pointer p = mitk::PropertyList::New(); // add properties to p like this: //p->SetProperty("MyFilter_MyParameter", mitk::PropertyDataType::New(m_MyParameter)); return mitk::PropertyList::ConstPointer(p); } diff --git a/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp b/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp index 697d1f85cc..5a22ebcc7b 100644 --- a/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp +++ b/Modules/IGT/Testing/mitkTrackingDeviceSourceTest.cpp @@ -1,137 +1,137 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTrackingDeviceSource.h" #include "mitkVirtualTrackingDevice.h" #include "mitkTestingMacros.h" #include #include "itksys/SystemTools.hxx" /**Documentation * test for the class "NavigationDataSource". */ int mitkTrackingDeviceSourceTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("TrackingDeviceSource"); // let's create an object of our class mitk::TrackingDeviceSource::Pointer mySource = mitk::TrackingDeviceSource::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(mySource.IsNotNull(), "Testing instantiation"); mySource->SetTrackingDevice(NULL); MITK_TEST_CONDITION(mySource->GetTrackingDevice() == NULL, "Testing Set/GetTrackingDevice(NULL)"); MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 0, "Testing GetNumberOfOutputs with NULL td"); MITK_TEST_FOR_EXCEPTION(std::invalid_argument, mySource->Connect()); MITK_TEST_FOR_EXCEPTION(std::invalid_argument, mySource->StartTracking()); mitk::VirtualTrackingDevice::Pointer tracker = mitk::VirtualTrackingDevice::New(); tracker->SetRefreshRate(10); mySource->SetTrackingDevice(tracker); MITK_TEST_CONDITION(mySource->GetTrackingDevice() == tracker.GetPointer(), "Testing Set/GetTrackingDevice(tracker)"); MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 0, "Testing GetNumberOfOutputs with no tool tracker"); tracker = mitk::VirtualTrackingDevice::New(); mitk::ReferenceCountWatcher::Pointer watch = new mitk::ReferenceCountWatcher(tracker); tracker->AddTool("T0"); tracker->AddTool("T1"); mySource->SetTrackingDevice(tracker); MITK_TEST_CONDITION(mySource->GetTrackingDevice() == tracker.GetPointer(), "Testing Set/GetTrackingDevice(tracker2)"); MITK_TEST_CONDITION(mySource->GetNumberOfOutputs() == 2, "Testing GetNumberOfOutputs with 2 tools tracker"); MITK_TEST_CONDITION(mySource->IsConnected() == false, "Testing IsConnected()"); mySource->Connect(); MITK_TEST_CONDITION(mySource->IsConnected() == true, "Testing Connect()/IsConnected()"); MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Ready, "Testing Connect()/IsConnected() 2"); mySource->Disconnect(); MITK_TEST_CONDITION(mySource->IsConnected() == false, "Testing Disconnect()/IsConnected()"); MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Setup, "Testing Disconnect()/IsConnected() 2"); mySource->Connect(); mySource->StartTracking(); MITK_TEST_CONDITION(mySource->IsConnected() == true, "Testing StartTracking()/IsConnected()"); MITK_TEST_CONDITION(mySource->IsTracking() == true, "Testing StartTracking()/IsTracking()"); MITK_TEST_CONDITION(tracker->GetState() == mitk::TrackingDevice::Tracking, "Testing StartTracking()/IsTracking() 2"); unsigned long modTime = mySource->GetMTime(); mySource->UpdateOutputInformation(); MITK_TEST_CONDITION(mySource->GetMTime() != modTime, "Testing if UpdateOutputInformation() modifies the object"); //test getOutput() mitk::NavigationData* nd0 = mySource->GetOutput(); - MITK_TEST_CONDITION(nd0!=NULL,"Testing GetOutput() [1]"); + MITK_TEST_CONDITION_REQUIRED(nd0!=NULL,"Testing GetOutput() [1]"); nd0 = mySource->GetOutput(nd0->GetName()); - MITK_TEST_CONDITION(nd0!=NULL,"Testing GetOutput() [2]"); + MITK_TEST_CONDITION_REQUIRED(nd0!=NULL,"Testing GetOutput() [2]"); //test getOutputIndex() MITK_TEST_CONDITION(mySource->GetOutputIndex(nd0->GetName())==0,"Testing GetOutputIndex()"); //test GraftNthOutput() mitk::NavigationData::Pointer ndCopy = mitk::NavigationData::New(); mySource->GraftNthOutput(1,nd0); ndCopy = mySource->GetOutput(1); MITK_TEST_CONDITION(std::string(ndCopy->GetName())==std::string(nd0->GetName()),"Testing GraftNthOutput()"); //test GetParameters() mitk::PropertyList::ConstPointer p = mySource->GetParameters(); MITK_TEST_CONDITION(p.IsNotNull(),"Testing GetParameters()"); nd0->Update(); mitk::NavigationData::PositionType pos = nd0->GetPosition(); unsigned long tmpMTime0 = nd0->GetMTime(); itksys::SystemTools::Delay(500); // allow the tracking thread to advance the tool position nd0->Update(); mitk::NavigationData::PositionType newPos = nd0->GetPosition(); if(nd0->GetMTime() == tmpMTime0) //tool not modified yet { MITK_TEST_CONDITION(mitk::Equal(newPos, pos) == true, "Testing if output changes on each update"); } else { MITK_TEST_CONDITION(mitk::Equal(newPos, pos) == false, "Testing if output changes on each update"); } mySource->StopTracking(); mySource->Disconnect(); tracker = mitk::VirtualTrackingDevice::New(); mySource->SetTrackingDevice(tracker); MITK_TEST_CONDITION(watch->GetReferenceCount() == 0, "Testing if reference to previous tracker object is released"); watch = NULL; MITK_TEST_FOR_EXCEPTION(std::runtime_error, mySource->StartTracking()); // new tracker, needs Connect() before StartTracking() mySource->Connect(); mySource->StartTracking(); // itksys::SystemTools::Delay(800); // wait for tracking thread to start properly //DEBUG ONLY --> race condition. will the thread start before the object is destroyed? --> maybe hold a smartpointer? try { mySource = NULL; // delete source tracker = NULL; // delete tracker --> both should not result in any exceptions or deadlocks } catch (...) { MITK_TEST_FAILED_MSG(<< "exception during destruction of source or tracker!"); } // always end with this! MITK_TEST_END(); }