1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkImageSamplerBase_hxx
#define itkImageSamplerBase_hxx
#include "itkImageSamplerBase.h"
#include <itkDeref.h>
#include <itkMultiThreaderBase.h>
#include <cassert>
#include <numeric> // For accumulate.
namespace itk
{
/**
* ******************* SetMask *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::SetMask(const MaskType * _arg, unsigned int pos)
{
if (m_MaskVector.size() < pos + 1)
{
m_MaskVector.resize(pos + 1);
m_NumberOfMasks = pos + 1;
}
if (pos == 0)
{
m_Mask = _arg;
}
if (m_MaskVector[pos] != _arg)
{
m_MaskVector[pos] = _arg;
/** The following line is not necessary, since the local
* bounding box is already computed when SetImage() is called
* in the elxRegistrationBase (when the mask spatial object
* is constructed).
*/
// m_Mask->ComputeLocalBoundingBox();
this->Modified();
}
} // SetMask()
/**
* ******************* GetMask *******************
*/
template <class TInputImage>
auto
ImageSamplerBase<TInputImage>::GetMask(unsigned int pos) const -> const MaskType *
{
if (m_MaskVector.size() < pos + 1)
{
return nullptr;
}
return m_MaskVector[pos];
} // end GetMask()
/**
* ******************* SetNumberOfMasks *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::SetNumberOfMasks(const unsigned int _arg)
{
if (m_NumberOfMasks != _arg)
{
m_MaskVector.resize(_arg);
m_NumberOfMasks = _arg;
this->Modified();
}
} // end SetNumberOfMasks()
/**
* ******************* SetInputImageRegion *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::SetInputImageRegion(const InputImageRegionType _arg, unsigned int pos)
{
if (m_InputImageRegionVector.size() < pos + 1)
{
m_InputImageRegionVector.resize(pos + 1);
m_NumberOfInputImageRegions = pos + 1;
}
if (pos == 0)
{
m_InputImageRegion = _arg;
}
if (m_InputImageRegionVector[pos] != _arg)
{
m_InputImageRegionVector[pos] = _arg;
this->Modified();
}
} // SetInputImageRegion()
/**
* ******************* GetInputImageRegion *******************
*/
template <class TInputImage>
auto
ImageSamplerBase<TInputImage>::GetInputImageRegion(unsigned int pos) const -> const InputImageRegionType &
{
if (m_InputImageRegionVector.size() < pos + 1)
{
return m_DummyInputImageRegion;
}
return m_InputImageRegionVector[pos];
} // end GetInputImageRegion()
/**
* ******************* SetNumberOfInputImageRegions *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::SetNumberOfInputImageRegions(const unsigned int _arg)
{
if (m_NumberOfInputImageRegions != _arg)
{
m_InputImageRegionVector.resize(_arg);
m_NumberOfInputImageRegions = _arg;
this->Modified();
}
} // end SetNumberOfInputImageRegions()
/**
* ******************* GenerateInputRequestedRegion *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::GenerateInputRequestedRegion()
{
/** Check if input image was set. */
if (this->GetNumberOfInputs() == 0)
{
itkExceptionMacro("ERROR: Input image not set");
}
/** Get a non-const reference to the input image. */
auto & inputImage = const_cast<InputImageType &>(Deref(this->GetInput()));
/** Get and set the region. */
if (this->GetInputImageRegion().GetNumberOfPixels() != 0)
{
InputImageRegionType inputRequestedRegion = this->GetInputImageRegion();
/** Crop the input requested region at the input's largest possible region. */
if (inputRequestedRegion.Crop(inputImage.GetLargestPossibleRegion()))
{
inputImage.SetRequestedRegion(inputRequestedRegion);
}
else
{
/** Couldn't crop the region (requested region is outside the largest
* possible region). Throw an exception.
*/
/** Store what we tried to request (prior to trying to crop). */
inputImage.SetRequestedRegion(inputRequestedRegion);
/** Build an exception. */
InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
e.SetDataObject(&inputImage);
throw e;
}
}
else
{
inputImage.SetRequestedRegion(inputImage.GetLargestPossibleRegion());
this->SetInputImageRegion(inputImage.GetLargestPossibleRegion());
}
/** Crop the region of the inputImage to the bounding box of the mask. */
this->CropInputImageRegion();
inputImage.SetRequestedRegion(m_CroppedInputImageRegion);
} // end GenerateInputRequestedRegion()
/**
* ******************* SelectNewSamplesOnUpdate *******************
*/
template <class TInputImage>
bool
ImageSamplerBase<TInputImage>::SelectNewSamplesOnUpdate()
{
/** Set the Modified flag, such that on calling Update(),
* the GenerateData method is executed again.
* Return true to indicate that indeed new samples will be selected.
* Inheriting subclasses may just return false and do nothing.
*/
this->Modified();
return true;
} // end SelectNewSamplesOnUpdate()
/**
* ******************* IsInsideAllMasks *******************
*/
template <class TInputImage>
bool
ImageSamplerBase<TInputImage>::IsInsideAllMasks(const InputImagePointType & point) const
{
bool ret = true;
for (unsigned int i = 0; i < m_NumberOfMasks; ++i)
{
ret &= this->GetMask(i)->IsInsideInWorldSpace(point);
}
return ret;
} // end IsInsideAllMasks()
/**
* ******************* UpdateAllMasks *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::UpdateAllMasks()
{
/** If the masks are generated by a filter, then make sure they are updated. */
for (unsigned int i = 0; i < m_NumberOfMasks; ++i)
{
this->GetMask(i)->UpdateSource();
}
} // end UpdateAllMasks()
/**
* ******************* CheckInputImageRegions *******************
*/
template <class TInputImage>
bool
ImageSamplerBase<TInputImage>::CheckInputImageRegions()
{
bool ret = true;
for (unsigned int i = 0; i < this->GetNumberOfInputImageRegions(); ++i)
{
ret &= this->GetInput(i)->GetLargestPossibleRegion().IsInside(this->GetInputImageRegion(i));
}
return ret;
} // end CheckInputImageRegions()
/**
* ******************* CropInputImageRegion *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::CropInputImageRegion()
{
/** Since we expect to be called from GenerateInputRequestedRegion(),
* we can safely assume that m_InputImageRegion is either
* the LargestPossibleRegion of InputImage or a valid subregion of it.
*
* If a mask was set, then compute the intersection of the
* InputImageRegion and the BoundingBoxRegion.
*/
m_CroppedInputImageRegion = m_InputImageRegion;
if (!m_Mask.IsNull())
{
/** Get a handle to the input image. */
InputImageConstPointer inputImage = this->GetInput();
if (!inputImage)
{
return;
}
this->UpdateAllMasks();
/** Get the indices of the bounding box extremes, based on the first mask.
* Note that the bounding box is defined in terms of the mask
* spacing and origin, and that we need a region in terms
* of the inputImage indices.
*/
using BoundingBoxType = typename MaskType::BoundingBoxType;
using PointsContainerType = typename BoundingBoxType::PointsContainer;
typename BoundingBoxType::ConstPointer bb = m_Mask->GetMyBoundingBoxInWorldSpace();
auto bbIndex = BoundingBoxType::New();
const PointsContainerType * cornersWorld = bb->GetPoints();
auto cornersIndex = PointsContainerType::New();
cornersIndex->Reserve(cornersWorld->Size());
typename PointsContainerType::const_iterator itCW = cornersWorld->begin();
typename PointsContainerType::iterator itCI = cornersIndex->begin();
while (itCW != cornersWorld->end())
{
*itCI = inputImage->template TransformPhysicalPointToContinuousIndex<InputImagePointValueType>(*itCW);
++itCI;
++itCW;
}
bbIndex->SetPoints(cornersIndex);
bbIndex->ComputeBoundingBox();
/** Create a bounding box region. */
InputImageIndexType minIndex, maxIndex;
InputImageSizeType size;
InputImageRegionType boundingBoxRegion;
for (unsigned int i = 0; i < InputImageDimension; ++i)
{
/** apply ceil/floor for max/min resp. to be sure that
* the bounding box is not too small */
maxIndex[i] = static_cast<IndexValueType>(std::ceil(bbIndex->GetMaximum()[i]));
minIndex[i] = static_cast<IndexValueType>(std::floor(bbIndex->GetMinimum()[i]));
size[i] = maxIndex[i] - minIndex[i] + 1;
}
boundingBoxRegion.SetIndex(minIndex);
boundingBoxRegion.SetSize(size);
/** Compute the intersection. */
bool cropped = m_CroppedInputImageRegion.Crop(boundingBoxRegion);
/** If the cropping return false, then the intersection is empty.
* In this case m_CroppedInputImageRegion is unchanged,
* but we would like to throw an exception.
*/
if (!cropped)
{
itkExceptionMacro("ERROR: the bounding box of the mask lies entirely out of the InputImageRegion!");
}
}
} // end CropInputImageRegion()
/**
* ******************* PrintSelf *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "NumberOfMasks" << m_NumberOfMasks << std::endl;
os << indent << "Mask: " << m_Mask.GetPointer() << std::endl;
os << indent << "MaskVector:" << std::endl;
for (unsigned int i = 0; i < m_NumberOfMasks; ++i)
{
os << indent.GetNextIndent() << m_MaskVector[i].GetPointer() << std::endl;
}
os << indent << "NumberOfInputImageRegions" << m_NumberOfInputImageRegions << std::endl;
os << indent << "InputImageRegion: " << m_InputImageRegion << std::endl;
os << indent << "InputImageRegionVector:" << std::endl;
for (unsigned int i = 0; i < m_NumberOfInputImageRegions; ++i)
{
os << indent.GetNextIndent() << m_InputImageRegionVector[i] << std::endl;
}
os << indent << "CroppedInputImageRegion" << m_CroppedInputImageRegion << std::endl;
} // end PrintSelf()
/**
* ******************* Constructor *******************
*/
template <class TInputImage>
ImageSamplerBase<TInputImage>::ImageSamplerBase()
{
this->ProcessObject::SetNumberOfRequiredInputs(1);
this->ProcessObject::SetNumberOfRequiredOutputs(1);
this->ProcessObject::SetNthOutput(0, OutputVectorContainerType::New().GetPointer());
} // end Constructor
/**
* ******************* MakeOutput *******************
*/
template <class TInputImage>
DataObject::Pointer
ImageSamplerBase<TInputImage>::MakeOutput(ProcessObject::DataObjectPointerArraySizeType itkNotUsed(idx))
{
OutputVectorContainerPointer outputVectorContainer = OutputVectorContainerType::New();
return outputVectorContainer.GetPointer();
} // end MakeOutput()
/**
* ******************* SetInput *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::SetInput(unsigned int idx, const InputImageType * input)
{
// process object is not const-correct, the const_cast
// is required here.
this->ProcessObject::SetNthInput(idx, const_cast<InputImageType *>(input));
} // end SetInput()
/**
* ******************* SetInput *******************
*/
template <class TInputImage>
void
ImageSamplerBase<TInputImage>::SetInput(const InputImageType * input)
{
this->ProcessObject::SetNthInput(0, const_cast<InputImageType *>(input));
} // end SetInput()
/**
* ******************* GetInput *******************
*/
template <class TInputImage>
auto
ImageSamplerBase<TInputImage>::GetInput() -> const InputImageType *
{
return dynamic_cast<const InputImageType *>(this->ProcessObject::GetInput(0));
} // end GetInput()
/**
* ******************* GetInput *******************
*/
template <class TInputImage>
auto
ImageSamplerBase<TInputImage>::GetInput(unsigned int idx) -> const InputImageType *
{
return dynamic_cast<const InputImageType *>(this->ProcessObject::GetInput(idx));
} // end GetInput()
/**
* ******************* GetOutput *******************
*/
template <class TInputImage>
auto
ImageSamplerBase<TInputImage>::GetOutput() -> OutputVectorContainerType *
{
return dynamic_cast<OutputVectorContainerType *>(this->ProcessObject::GetOutput(0));
} // end GetOutput()
template <class TInputImage>
auto
ImageSamplerBase<TInputImage>::SplitRegion(const InputImageRegionType & inputRegion,
const size_t requestedNumberOfSubregions)
-> std::vector<InputImageRegionType>
{
if (requestedNumberOfSubregions == 0)
{
assert(!"The requested number of subregions must be greater than zero!");
return {};
}
constexpr unsigned int ImageDimension{ TInputImage::ImageDimension };
const Index<ImageDimension> & inputRegionIndex = inputRegion.GetIndex();
const Size<ImageDimension> & inputRegionSize = inputRegion.GetSize();
static_assert(TInputImage::ImageDimension > 0);
// split on the outermost dimension available
unsigned int splitAxis{ ImageDimension - 1 };
while (inputRegionSize[splitAxis] <= 1)
{
if (splitAxis == 0)
{
// cannot split
return { inputRegion };
}
--splitAxis;
}
// determine the actual number of pieces that will be generated
const SizeValueType inputSizeValue = inputRegionSize[splitAxis];
const auto numberOfValues = static_cast<unsigned int>(((inputSizeValue - 1) / requestedNumberOfSubregions) + 1);
const auto n = static_cast<unsigned int>((inputSizeValue - 1) / numberOfValues);
std::vector<InputImageRegionType> subregions{};
subregions.reserve(n + 1);
for (size_t i{}; i < n; ++i)
{
auto index = inputRegionIndex;
auto size = inputRegionSize;
index[splitAxis] += i * numberOfValues;
size[splitAxis] = numberOfValues;
subregions.push_back({ index, size });
}
auto index = inputRegionIndex;
auto size = inputRegionSize;
index[splitAxis] += n * numberOfValues;
// last thread needs to process the "rest" dimension being split
size[splitAxis] -= n * numberOfValues;
subregions.push_back(InputImageRegionType{ index, size });
assert(subregions.size() == n + 1);
return subregions;
}
} // end namespace itk
#endif // end #ifndef itkImageSamplerBase_hxx
|