|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +#ifndef itkVkComplexToComplex1DFFTImageFilter_hxx |
| 20 | +#define itkVkComplexToComplex1DFFTImageFilter_hxx |
| 21 | + |
| 22 | +#include "itkVkComplexToComplex1DFFTImageFilter.h" |
| 23 | +#include "vkFFT.h" |
| 24 | +#include "itkImageRegionIterator.h" |
| 25 | +#include "itkIndent.h" |
| 26 | +#include "itkMetaDataObject.h" |
| 27 | +#include "itkProgressReporter.h" |
| 28 | + |
| 29 | +namespace itk |
| 30 | +{ |
| 31 | +template <typename TImage> |
| 32 | +void |
| 33 | +VkComplexToComplex1DFFTImageFilter<TImage>::GenerateData() |
| 34 | +{ |
| 35 | + // get pointers to the input and output |
| 36 | + const InputImageType * const input{ this->GetInput() }; |
| 37 | + OutputImageType * const output{ this->GetOutput() }; |
| 38 | + |
| 39 | + if (!input || !output) |
| 40 | + { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // we don't have a nice progress to report, but at least this simple line |
| 45 | + // reports the beginning and the end of the process |
| 46 | + const ProgressReporter progress(this, 0, 1); |
| 47 | + |
| 48 | + // allocate output buffer memory |
| 49 | + output->SetBufferedRegion(output->GetRequestedRegion()); |
| 50 | + output->Allocate(); |
| 51 | + |
| 52 | + const SizeType & inputSize{ input->GetLargestPossibleRegion().GetSize() }; |
| 53 | + |
| 54 | + const InputPixelType * const inputCPUBuffer{ input->GetBufferPointer() }; |
| 55 | + OutputPixelType * const outputCPUBuffer{ output->GetBufferPointer() }; |
| 56 | + itkAssertOrThrowMacro(inputCPUBuffer != nullptr, "No CPU input buffer"); |
| 57 | + itkAssertOrThrowMacro(outputCPUBuffer != nullptr, "No CPU output buffer"); |
| 58 | + const SizeValueType inBytes{ input->GetLargestPossibleRegion().GetNumberOfPixels() * sizeof(InputPixelType) }; |
| 59 | + const SizeValueType outBytes{ output->GetLargestPossibleRegion().GetNumberOfPixels() * sizeof(OutputPixelType) }; |
| 60 | + itkAssertOrThrowMacro(inBytes == outBytes, "CPU input and output buffers are of different sizes."); |
| 61 | + |
| 62 | + // Mostly use defaults for VkCommon::VkGPU |
| 63 | + typename VkCommon::VkGPU vkGPU; |
| 64 | + vkGPU.device_id = m_DeviceID; |
| 65 | + |
| 66 | + // Describe this filter in VkCommon::VkParameters |
| 67 | + typename VkCommon::VkParameters vkParameters; |
| 68 | + if (ImageDimension > 0) |
| 69 | + vkParameters.X = inputSize[0]; |
| 70 | + if (ImageDimension > 1) |
| 71 | + vkParameters.Y = inputSize[1]; |
| 72 | + if (ImageDimension > 2) |
| 73 | + vkParameters.Z = inputSize[2]; |
| 74 | + if (std::is_same<RealType, float>::value) |
| 75 | + vkParameters.P = VkCommon::PrecisionEnum::FLOAT; |
| 76 | + else if (std::is_same<RealType, double>::value) |
| 77 | + vkParameters.P = VkCommon::PrecisionEnum::DOUBLE; |
| 78 | + else |
| 79 | + itkAssertOrThrowMacro(false, "Unsupported type for real numbers."); |
| 80 | + vkParameters.fft = VkCommon::FFTEnum::C2C; |
| 81 | + vkParameters.PSize = sizeof(RealType); |
| 82 | + vkParameters.I = this->GetTransformDirection() == Superclass::TransformDirectionType::INVERSE |
| 83 | + ? VkCommon::DirectionEnum::INVERSE |
| 84 | + : VkCommon::DirectionEnum::FORWARD; |
| 85 | + vkParameters.normalized = vkParameters.I == VkCommon::DirectionEnum::INVERSE |
| 86 | + ? VkCommon::NormalizationEnum::NORMALIZED |
| 87 | + : VkCommon::NormalizationEnum::UNNORMALIZED; |
| 88 | + for (size_t dim = 0; dim < ImageDimension; ++dim) |
| 89 | + { |
| 90 | + if (this->GetDirection() != dim) |
| 91 | + { |
| 92 | + vkParameters.omitDimension[dim] = 1; // omit dimensions other than in the given direction. |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + vkParameters.inputCPUBuffer = inputCPUBuffer; |
| 97 | + vkParameters.inputBufferBytes = inBytes; |
| 98 | + vkParameters.outputCPUBuffer = outputCPUBuffer; |
| 99 | + vkParameters.outputBufferBytes = outBytes; |
| 100 | + |
| 101 | + const VkFFTResult resFFT{ m_VkCommon.Run(vkGPU, vkParameters) }; |
| 102 | + if (resFFT != VKFFT_SUCCESS) |
| 103 | + { |
| 104 | + std::ostringstream mesg; |
| 105 | + mesg << "VkFFT third-party library failed with error code " << resFFT << "."; |
| 106 | + itkAssertOrThrowMacro(false, mesg.str()); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +template <typename TImage> |
| 111 | +void |
| 112 | +VkComplexToComplex1DFFTImageFilter<TImage>::PrintSelf(std::ostream & os, Indent indent) const |
| 113 | +{ |
| 114 | + Superclass::PrintSelf(os, indent); |
| 115 | + os << indent << "DeviceID: " << m_DeviceID << std::endl; |
| 116 | +} |
| 117 | + |
| 118 | +template <typename TImage> |
| 119 | +typename VkComplexToComplex1DFFTImageFilter<TImage>::SizeValueType |
| 120 | +VkComplexToComplex1DFFTImageFilter<TImage>::GetSizeGreatestPrimeFactor() const |
| 121 | +{ |
| 122 | + return m_VkCommon.GetGreatestPrimeFactor(); |
| 123 | +} |
| 124 | + |
| 125 | +} // end namespace itk |
| 126 | + |
| 127 | +#endif // _itkVkComplexToComplex1DFFTImageFilter_hxx |
0 commit comments