Skip to content

Commit

Permalink
STYLE: Add m_RandomCoordinates to ImageRandomCoordinateSampler
Browse files Browse the repository at this point in the history
It appears easier for ImageRandomCoordinateSampler to have its own dedicated `m_RandomCoordinates`, instead of using m_RandomNumberList from its Superclass.
  • Loading branch information
N-Dekker committed Nov 15, 2023
1 parent 1bc791e commit eb2bd38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
21 changes: 11 additions & 10 deletions Common/ImageSamplers/itkImageRandomCoordinateSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,24 @@ class ITK_TEMPLATE_EXPORT ImageRandomCoordinateSampler : public ImageRandomSampl
{
ITK_DISALLOW_COPY_AND_MOVE(UserData);

UserData(const std::vector<double> & randomNumberList,
const InputImageType & inputImage,
const InterpolatorType & interpolator,
std::vector<ImageSampleType> & samples)
: m_RandomNumberList(randomNumberList)
UserData(const std::vector<InputImageContinuousIndexType> & randomCoordinates,
const InputImageType & inputImage,
const InterpolatorType & interpolator,
std::vector<ImageSampleType> & samples)
: m_RandomCoordinates(randomCoordinates)
, m_InputImage(inputImage)
, m_Interpolator(interpolator)
, m_Samples(samples)
{}

const std::vector<double> & m_RandomNumberList;
const InputImageType & m_InputImage;
const InterpolatorType & m_Interpolator;
std::vector<ImageSampleType> & m_Samples;
const std::vector<InputImageContinuousIndexType> & m_RandomCoordinates;
const InputImageType & m_InputImage;
const InterpolatorType & m_Interpolator;
std::vector<ImageSampleType> & m_Samples;
};

std::optional<UserData> m_OptionalUserData{};
std::vector<InputImageContinuousIndexType> m_RandomCoordinates{};
std::optional<UserData> m_OptionalUserData{};

static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
ThreaderCallback(void * arg);
Expand Down
27 changes: 9 additions & 18 deletions Common/ImageSamplers/itkImageRandomCoordinateSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,19 @@ ImageRandomCoordinateSampler<TInputImage>::GenerateData()
samples.resize(this->Superclass::m_NumberOfSamples);

/** Clear the random number list. */
this->m_RandomNumberList.clear();
this->m_RandomNumberList.reserve(this->m_NumberOfSamples * InputImageDimension);
m_RandomCoordinates.clear();
m_RandomCoordinates.reserve(this->m_NumberOfSamples);

/** Fill the list with random numbers. */
for (unsigned long i = 0; i < this->m_NumberOfSamples; ++i)
{
InputImageContinuousIndexType randomCIndex;

this->GenerateRandomCoordinate(smallestContIndex, largestContIndex, randomCIndex);
for (unsigned int j = 0; j < InputImageDimension; ++j)
{
this->m_RandomNumberList.push_back(randomCIndex[j]);
}
m_RandomCoordinates.push_back(randomCIndex);
}

m_OptionalUserData.emplace(this->Superclass::m_RandomNumberList, *inputImage, *interpolator, samples);
m_OptionalUserData.emplace(m_RandomCoordinates, *inputImage, *interpolator, samples);

MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
multiThreader.SetSingleMethod(&Self::ThreaderCallback, &*m_OptionalUserData);
Expand Down Expand Up @@ -175,19 +172,19 @@ ImageRandomCoordinateSampler<TInputImage>::ThreaderCallback(void * const arg)
assert(info.UserData);
auto & userData = *static_cast<UserData *>(info.UserData);

const auto & randomNumberList = userData.m_RandomNumberList;
const auto & randomCoordinates = userData.m_RandomCoordinates;
auto & samples = userData.m_Samples;
const auto & interpolator = userData.m_Interpolator;

const auto totalNumberOfSamples = samples.size();
assert((totalNumberOfSamples * InputImageDimension) == randomNumberList.size());
assert(totalNumberOfSamples == randomCoordinates.size());

const auto numberOfSamplesPerWorkUnit = totalNumberOfSamples / info.NumberOfWorkUnits;
const auto remainderNumberOfSamples = totalNumberOfSamples % info.NumberOfWorkUnits;

const auto offset =
info.WorkUnitID * numberOfSamplesPerWorkUnit + std::min<size_t>(info.WorkUnitID, remainderNumberOfSamples);
const auto beginOfRandomNumbers = randomNumberList.data() + InputImageDimension * offset;
const auto beginOfRandomCoordinates = randomCoordinates.data() + offset;
const auto beginOfSamples = samples.data() + offset;

const auto & inputImage = userData.m_InputImage;
Expand All @@ -196,14 +193,8 @@ ImageRandomCoordinateSampler<TInputImage>::ThreaderCallback(void * const arg)

for (size_t i = 0; i < n; ++i)
{
auto & sample = beginOfSamples[i];
InputImageContinuousIndexType sampleCIndex;

/** Create a random point out of InputImageDimension random numbers. */
for (unsigned int j = 0; j < InputImageDimension; ++j)
{
sampleCIndex[j] = beginOfRandomNumbers[InputImageDimension * i + j];
}
auto & sample = beginOfSamples[i];
const InputImageContinuousIndexType sampleCIndex = beginOfRandomCoordinates[i];

/** Convert to point */
inputImage.TransformContinuousIndexToPhysicalPoint(sampleCIndex, sample.m_ImageCoordinates);
Expand Down

0 comments on commit eb2bd38

Please sign in to comment.