diff --git a/Modules/PhotoacousticsLib/src/Utils/Thread/mitkPAMonteCarloThreadHandler.cpp b/Modules/PhotoacousticsLib/src/Utils/Thread/mitkPAMonteCarloThreadHandler.cpp index 2919ff5401..f8e98d8ae5 100644 --- a/Modules/PhotoacousticsLib/src/Utils/Thread/mitkPAMonteCarloThreadHandler.cpp +++ b/Modules/PhotoacousticsLib/src/Utils/Thread/mitkPAMonteCarloThreadHandler.cpp @@ -1,143 +1,140 @@ /*=================================================================== 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 "mitkPAMonteCarloThreadHandler.h" #include "mitkCommon.h" mitk::pa::MonteCarloThreadHandler::MonteCarloThreadHandler(long timInMillisecondsOrNumberofPhotons, bool simulateOnTimeBasis) : - MonteCarloThreadHandler(timInMillisecondsOrNumberofPhotons, simulateOnTimeBasis, true) -{ -} + MonteCarloThreadHandler(timInMillisecondsOrNumberofPhotons, simulateOnTimeBasis, true){} mitk::pa::MonteCarloThreadHandler::MonteCarloThreadHandler(long timInMillisecondsOrNumberofPhotons, bool simulateOnTimeBasis, bool verbose) { m_Verbose = verbose; m_SimulateOnTimeBasis = simulateOnTimeBasis; m_WorkPackageSize = 10000L; m_SimulationTime = 0; m_Time = 0; m_NumberPhotonsToSimulate = 0; m_NumberPhotonsRemaining = 0; if (m_SimulateOnTimeBasis) { m_SimulationTime = timInMillisecondsOrNumberofPhotons; m_Time = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); } else { m_NumberPhotonsToSimulate = timInMillisecondsOrNumberofPhotons; m_NumberPhotonsRemaining = timInMillisecondsOrNumberofPhotons; } - MITK_INFO << "Created!"; } mitk::pa::MonteCarloThreadHandler::~MonteCarloThreadHandler() { } long mitk::pa::MonteCarloThreadHandler::GetNextWorkPackage() { long workPackageSize = 0; if (m_SimulateOnTimeBasis) { long now = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); if (now - m_Time <= m_SimulationTime) { workPackageSize = m_WorkPackageSize; if (m_Verbose) { std::cout << "" << std::endl; } } } else { m_MutexRemainingPhotonsManipulation.lock(); if (m_NumberPhotonsRemaining < m_WorkPackageSize) { workPackageSize = m_NumberPhotonsRemaining; } else { workPackageSize = m_WorkPackageSize; } m_NumberPhotonsRemaining -= workPackageSize; m_MutexRemainingPhotonsManipulation.unlock(); if (m_Verbose) { std::cout << "" << std::endl; } } return workPackageSize; } void mitk::pa::MonteCarloThreadHandler::SetPackageSize(long sizeInMilliseconsOrNumberOfPhotons) { m_WorkPackageSize = sizeInMilliseconsOrNumberOfPhotons; } bool mitk::pa::Equal(const MonteCarloThreadHandler::Pointer leftHandSide, const MonteCarloThreadHandler::Pointer rightHandSide, double /*eps*/, bool verbose) { if (rightHandSide->GetNumberPhotonsRemaining() != leftHandSide->GetNumberPhotonsRemaining()) { MITK_INFO(verbose) << "Number of Photons remaining wasnt equal: lhs=" << leftHandSide->GetNumberPhotonsRemaining() << " rhs=" << rightHandSide->GetNumberPhotonsRemaining(); return false; } if (rightHandSide->GetNumberPhotonsToSimulate() != leftHandSide->GetNumberPhotonsToSimulate()) { MITK_INFO(verbose) << "Number of Photons to simulate wasnt equal: lhs=" << leftHandSide->GetNumberPhotonsToSimulate() << " rhs=" << rightHandSide->GetNumberPhotonsToSimulate(); return false; } if (rightHandSide->GetWorkPackageSize() != leftHandSide->GetWorkPackageSize()) { MITK_INFO(verbose) << "WorkPackageSize wasnt equal: lhs=" << leftHandSide->GetWorkPackageSize() << " rhs=" << rightHandSide->GetWorkPackageSize(); return false; } if (rightHandSide->GetSimulationTime() != leftHandSide->GetSimulationTime()) { MITK_INFO(verbose) << "Simulationtime wasnt equal: lhs=" << leftHandSide->GetSimulationTime() << " rhs=" << rightHandSide->GetSimulationTime(); return false; } if (rightHandSide->GetSimulateOnTimeBasis() != leftHandSide->GetSimulateOnTimeBasis()) { MITK_INFO(verbose) << "simulation on time basis wasnt equal: lhs=" << leftHandSide->GetSimulateOnTimeBasis() << " rhs=" << rightHandSide->GetSimulateOnTimeBasis(); return false; } if (rightHandSide->GetVerbose() != leftHandSide->GetVerbose()) { MITK_INFO(verbose) << "Verbose wasnt equal: lhs=" << leftHandSide->GetVerbose() << " rhs=" << rightHandSide->GetVerbose(); return false; } return true; } diff --git a/Modules/PhotoacousticsLib/test/mitkMCThreadHandlerTest.cpp b/Modules/PhotoacousticsLib/test/mitkMCThreadHandlerTest.cpp index c9a91ee76a..83ae8b69c7 100644 --- a/Modules/PhotoacousticsLib/test/mitkMCThreadHandlerTest.cpp +++ b/Modules/PhotoacousticsLib/test/mitkMCThreadHandlerTest.cpp @@ -1,124 +1,124 @@ /*=================================================================== 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 #include #include // us #include #include #include #include #include #include #include class mitkMCThreadHandlerTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkMCThreadHandlerTestSuite); MITK_TEST(testConstructorBehavior); MITK_TEST(testCorrectNumberOfPhotons); MITK_TEST(testCorrectNumberOfPhotonsWithUnevenPackageSize); MITK_TEST(testCorrectNumberOfPhotonsWithTooLargePackageSize); MITK_TEST(testCorrectTimeMeasure); CPPUNIT_TEST_SUITE_END(); private: mitk::pa::MonteCarloThreadHandler::Pointer m_MonteCarloThreadHandler; long m_NumberOrTime = 500; public: void setUp() { } void testConstructorBehavior() { auto threadHandler1 = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, true, true); auto threadHandler2 = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, true); CPPUNIT_ASSERT(mitk::pa::Equal(threadHandler1, threadHandler2, 1e-6, true)); } void testCorrectTimeMeasure() { for (int i = 0; i < 10; i++) { m_MonteCarloThreadHandler = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, true, false); auto timeBefore = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); long nextWorkPackage = 0; while ((nextWorkPackage = m_MonteCarloThreadHandler->GetNextWorkPackage()) > 0) {//Do nothing } auto timeAfter = std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); //Assert that the time error is less than 10% in a 500ms sample size //This test might not be stable when on different machines. CPPUNIT_ASSERT(abs((timeAfter - timeBefore) - m_NumberOrTime) <= 50); } } void testCorrectNumberOfPhotons() { m_MonteCarloThreadHandler = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, false, false); m_MonteCarloThreadHandler->SetPackageSize(100); long numberOfPhotonsSimulated = 0; long nextWorkPackage = 0; while ((nextWorkPackage = m_MonteCarloThreadHandler->GetNextWorkPackage()) > 0) { numberOfPhotonsSimulated += nextWorkPackage; } CPPUNIT_ASSERT(numberOfPhotonsSimulated == m_NumberOrTime); } void testCorrectNumberOfPhotonsWithUnevenPackageSize() { m_MonteCarloThreadHandler = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, false, false); m_MonteCarloThreadHandler->SetPackageSize(77); long numberOfPhotonsSimulated = 0; long nextWorkPackage = 0; while ((nextWorkPackage = m_MonteCarloThreadHandler->GetNextWorkPackage()) > 0) { numberOfPhotonsSimulated += nextWorkPackage; } CPPUNIT_ASSERT(numberOfPhotonsSimulated == m_NumberOrTime); } void testCorrectNumberOfPhotonsWithTooLargePackageSize() { - m_MonteCarloThreadHandler = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, false); + m_MonteCarloThreadHandler = mitk::pa::MonteCarloThreadHandler::New(m_NumberOrTime, false, false); m_MonteCarloThreadHandler->SetPackageSize(10000); long numberOfPhotonsSimulated = 0; long nextWorkPackage = 0; while ((nextWorkPackage = m_MonteCarloThreadHandler->GetNextWorkPackage()) > 0) { numberOfPhotonsSimulated += nextWorkPackage; } CPPUNIT_ASSERT(numberOfPhotonsSimulated == m_NumberOrTime); } void tearDown() { m_MonteCarloThreadHandler = nullptr; } }; MITK_TEST_SUITE_REGISTRATION(mitkMCThreadHandler)