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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 itkImageSink_hxx
#define itkImageSink_hxx
#include "itkProgressTransformer.h"
#include "itkInputDataObjectConstIterator.h"
#include "itkMultiThreaderBase.h"
namespace itk
{
template <typename TInputImage>
ImageSink<TInputImage>::ImageSink()
{
// create default region splitter
m_RegionSplitter = ImageRegionSplitterSlowDimension::New();
// Modify superclass default values, can be overridden by subclasses
this->SetNumberOfRequiredInputs(1);
}
template <typename TInputImage>
void
ImageSink<TInputImage>::SetInput(const InputImageType * input)
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(0, const_cast<InputImageType *>(input));
}
template <typename TInputImage>
auto
ImageSink<TInputImage>::GetInput() const -> const InputImageType *
{
return itkDynamicCastInDebugMode<const TInputImage *>(this->ProcessObject::GetPrimaryInput());
}
template <typename TInputImage>
auto
ImageSink<TInputImage>::GetInput(unsigned int idx) const -> const InputImageType *
{
const auto * in = dynamic_cast<const TInputImage *>(this->ProcessObject::GetInput(idx));
if (in == nullptr && this->ProcessObject::GetInput(idx) != nullptr)
{
itkWarningMacro("Unable to convert input number " << idx << " to type " << typeid(InputImageType).name());
}
return in;
}
template <typename TInputImage>
auto
ImageSink<TInputImage>::GetInput(const DataObjectIdentifierType & key) const -> const InputImageType *
{
const auto * in = dynamic_cast<const TInputImage *>(this->ProcessObject::GetInput(key));
if (in == nullptr && this->ProcessObject::GetInput(key) != nullptr)
{
itkWarningMacro("Unable to convert input \"" << key << "\" to type " << typeid(InputImageType).name());
}
return in;
}
template <typename TInputImage>
void
ImageSink<TInputImage>::UpdateLargestPossibleRegion()
{
this->Update();
}
template <typename TInputImage>
void
ImageSink<TInputImage>::Update()
{
this->UpdateOutputInformation();
// if output 1, the just call it
// this->PropagateRequestedRegion( nullptr );
this->UpdateOutputData(nullptr);
}
template <typename TInputImage>
void
ImageSink<TInputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "NumberOfStreamDivisions: " << this->m_NumberOfStreamDivisions << std::endl;
os << indent << "RegionSplitter: " << this->m_RegionSplitter << std::endl;
os << indent << "CoordinateTolerance: " << this->m_CoordinateTolerance << std::endl;
os << indent << "DirectionTolerance: " << this->m_DirectionTolerance << std::endl;
}
template <typename TInputImage>
unsigned int
ImageSink<TInputImage>::GetNumberOfInputRequestedRegions()
{
const InputImageType * inputPtr = const_cast<InputImageType *>(this->GetInput());
InputImageRegionType inputImageRegion = inputPtr->GetLargestPossibleRegion();
return this->GetRegionSplitter()->GetNumberOfSplits(inputImageRegion, this->m_NumberOfStreamDivisions);
}
template <typename TInputImage>
void
ImageSink<TInputImage>::GenerateNthInputRequestedRegion(unsigned int inputRequestedRegionNumber)
{
Superclass::GenerateInputRequestedRegion();
auto * inputPtr = const_cast<InputImageType *>(this->GetInput());
InputImageRegionType inputImageRegion = inputPtr->GetLargestPossibleRegion();
this->GetRegionSplitter()->GetSplit(
inputRequestedRegionNumber, this->GetNumberOfInputRequestedRegions(), inputImageRegion);
m_CurrentInputRegion = inputImageRegion;
itkDebugMacro("Generating " << inputRequestedRegionNumber << " chunk as " << m_CurrentInputRegion);
for (auto & inputName : this->GetInputNames())
{
if (this->ProcessObject::GetInput(inputName))
{
// Check whether the input is an image of the appropriate
// dimension (use ProcessObject's version of the GetInput()
// method since it returns the input as a pointer to a
// DataObject as opposed to the subclass version which
// static_casts the input to an TInputImage).
using ImageBaseType = ImageBase<InputImageDimension>;
auto * input = dynamic_cast<ImageBaseType *>(this->ProcessObject::GetInput(inputName));
// If not an image, skip it. A subclass can override this method
// for particular input types.
if (input == nullptr)
{
continue;
}
// copy the requested region of the first input to the others
input->SetRequestedRegion(m_CurrentInputRegion);
}
}
}
template <typename TInputImage>
void
ImageSink<TInputImage>::VerifyInputInformation() ITKv5_CONST
{
using ImageBaseType = const ImageBase<InputImageDimension>;
ImageBaseType * inputPtr1 = nullptr;
InputDataObjectConstIterator it(this);
for (; !it.IsAtEnd(); ++it)
{
// Check whether the output is an image of the appropriate
// dimension (use ProcessObject's version of the GetInput()
// method since it returns the input as a pointer to a
// DataObject as opposed to the subclass version which
// static_casts the input to an TInputImage).
inputPtr1 = dynamic_cast<ImageBaseType *>(it.GetInput());
if (inputPtr1)
{
break;
}
}
for (; !it.IsAtEnd(); ++it)
{
auto * inputPtrN = dynamic_cast<ImageBaseType *>(it.GetInput());
// Physical space computation only matters if we're using two
// images, and not an image and a constant.
if (inputPtrN)
{
// check that the image occupy the same physical space, and that
// each index is at the same physical location
// tolerance for origin and spacing depends on the size of pixel
// tolerance for directions a fraction of the unit cube.
const SpacePrecisionType coordinateTol =
itk::Math::abs(this->m_CoordinateTolerance * inputPtr1->GetSpacing()[0]); // use first dimension spacing
if (!inputPtr1->GetOrigin().GetVnlVector().is_equal(inputPtrN->GetOrigin().GetVnlVector(), coordinateTol) ||
!inputPtr1->GetSpacing().GetVnlVector().is_equal(inputPtrN->GetSpacing().GetVnlVector(), coordinateTol) ||
!inputPtr1->GetDirection().GetVnlMatrix().is_equal(inputPtrN->GetDirection().GetVnlMatrix(),
this->m_DirectionTolerance))
{
std::ostringstream originString, spacingString, directionString;
if (!inputPtr1->GetOrigin().GetVnlVector().is_equal(inputPtrN->GetOrigin().GetVnlVector(), coordinateTol))
{
originString.setf(std::ios::scientific);
originString.precision(7);
originString << "InputImage Origin: " << inputPtr1->GetOrigin() << ", InputImage" << it.GetName()
<< " Origin: " << inputPtrN->GetOrigin() << std::endl;
originString << "\tTolerance: " << coordinateTol << std::endl;
}
if (!inputPtr1->GetSpacing().GetVnlVector().is_equal(inputPtrN->GetSpacing().GetVnlVector(), coordinateTol))
{
spacingString.setf(std::ios::scientific);
spacingString.precision(7);
spacingString << "InputImage Spacing: " << inputPtr1->GetSpacing() << ", InputImage" << it.GetName()
<< " Spacing: " << inputPtrN->GetSpacing() << std::endl;
spacingString << "\tTolerance: " << coordinateTol << std::endl;
}
if (!inputPtr1->GetDirection().GetVnlMatrix().is_equal(inputPtrN->GetDirection().GetVnlMatrix(),
this->m_DirectionTolerance))
{
directionString.setf(std::ios::scientific);
directionString.precision(7);
directionString << "InputImage Direction: " << inputPtr1->GetDirection() << ", InputImage" << it.GetName()
<< " Direction: " << inputPtrN->GetDirection() << std::endl;
directionString << "\tTolerance: " << this->m_DirectionTolerance << std::endl;
}
itkExceptionMacro("Inputs do not occupy the same physical space! " << std::endl
<< originString.str() << spacingString.str()
<< directionString.str());
}
}
}
}
template <typename TInputImage>
void
ImageSink<TInputImage>::StreamedGenerateData(unsigned int inputRequestedRegionNumber)
{
this->GetMultiThreader()->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
// calculate the progress range for this streamed chunk
const ThreadIdType total = this->GetNumberOfInputRequestedRegions();
const float oldProgress = static_cast<float>(inputRequestedRegionNumber) / (total);
const float newProgress = static_cast<float>(inputRequestedRegionNumber + 1) / (total);
ProgressTransformer pt(oldProgress, newProgress, this);
this->GetMultiThreader()->template ParallelizeImageRegion<InputImageDimension>(
this->m_CurrentInputRegion,
[this](const InputImageRegionType & inputRegionForThread) {
this->ThreadedStreamedGenerateData(inputRegionForThread);
},
pt.GetProcessObject());
}
} // namespace itk
#endif // itkImageSink_hxx
|