Skip to content

Commit

Permalink
STYLE: Use std::optional for ImageRandomSampler user data
Browse files Browse the repository at this point in the history
Declared data members of `UserData` as reference, instead of pointer, because they cannot be null, once the user data is "emplaced".
  • Loading branch information
N-Dekker committed Oct 26, 2023
1 parent 9b039b7 commit 9a59a85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
20 changes: 16 additions & 4 deletions Common/ImageSamplers/itkImageRandomSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkImageRandomSampler_h

#include "itkImageRandomSamplerBase.h"
#include <optional>

namespace itk
{
Expand Down Expand Up @@ -91,14 +92,25 @@ class ITK_TEMPLATE_EXPORT ImageRandomSampler : public ImageRandomSamplerBase<TIn
{
ITK_DISALLOW_COPY_AND_ASSIGN(UserData);

const std::vector<double> * m_RandomNumberList{};
std::vector<ImageSampleType> * m_Samples{};
const InputImageType * m_InputImage{};
UserData(const std::vector<double> & randomNumberList,
std::vector<ImageSampleType> & samples,
const InputImageType & inputImage,
const InputImageRegionType & region)
: m_RandomNumberList(randomNumberList)
, m_Samples(samples)
, m_InputImage(inputImage)
, m_RegionIndex(region.GetIndex())
, m_RegionSize(region.GetSize())
{}

const std::vector<double> & m_RandomNumberList;
std::vector<ImageSampleType> & m_Samples;
const InputImageType & m_InputImage;
InputImageIndexType m_RegionIndex{};
InputImageSizeType m_RegionSize{};
};

UserData m_UserData{};
std::optional<UserData> m_OptionalUserData{};

static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
ThreaderCallback(void * arg);
Expand Down
21 changes: 6 additions & 15 deletions Common/ImageSamplers/itkImageRandomSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,14 @@ ImageRandomSampler<TInputImage>::GenerateData()
auto & samples = elastix::Deref(sampleContainer).CastToSTLContainer();
samples.resize(randomNumberList.size());

m_UserData.m_RandomNumberList = &randomNumberList;
m_UserData.m_Samples = &samples;
m_UserData.m_InputImage = inputImage;

const InputImageRegionType region = this->GetCroppedInputImageRegion();

m_UserData.m_RegionIndex = region.GetIndex();
m_UserData.m_RegionSize = region.GetSize();
m_OptionalUserData.emplace(
randomNumberList, samples, elastix::Deref(inputImage), this->GetCroppedInputImageRegion());

auto & randomVariateGenerator = elastix::Deref(Statistics::MersenneTwisterRandomVariateGenerator::GetInstance());
randomVariateGenerator.SetSeed(randomVariateGenerator.GetNextSeed());

MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &m_UserData);
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &*m_OptionalUserData);
multiThreader.SingleMethodExecute();
return;
}
Expand Down Expand Up @@ -163,11 +157,8 @@ ImageRandomSampler<TInputImage>::ThreaderCallback(void * const arg)
assert(info.UserData);
auto & userData = *static_cast<UserData *>(info.UserData);

assert(userData.m_RandomNumberList);
const auto & randomNumberList = *(userData.m_RandomNumberList);

assert(userData.m_Samples);
auto & samples = *(userData.m_Samples);
const auto & randomNumberList = userData.m_RandomNumberList;
auto & samples = userData.m_Samples;

const auto totalNumberOfSamples = samples.size();
assert(totalNumberOfSamples == randomNumberList.size());
Expand All @@ -179,7 +170,7 @@ ImageRandomSampler<TInputImage>::ThreaderCallback(void * const arg)
info.WorkUnitID * numberOfSamplesPerWorkUnit + std::min<size_t>(info.WorkUnitID, remainderNumberOfSamples);
const auto beginOfRandomNumbers = randomNumberList.data() + offset;
const auto beginOfSamples = samples.data() + offset;
const auto & inputImage = *(userData.m_InputImage);
const auto & inputImage = userData.m_InputImage;

const InputImageSizeType regionSize = userData.m_RegionSize;
const InputImageIndexType regionIndex = userData.m_RegionIndex;
Expand Down

0 comments on commit 9a59a85

Please sign in to comment.