|
| 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 | + * https://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 | +#ifndef itkSupportInputTransformTypes_h |
| 19 | +#define itkSupportInputTransformTypes_h |
| 20 | +#include "itkPipeline.h" |
| 21 | +#include "itkWasmPixelTypeFromIOPixelEnum.h" |
| 22 | +#include "itkWasmComponentTypeFromIOComponentEnum.h" |
| 23 | +#include "itkWasmMapComponentType.h" |
| 24 | + |
| 25 | +#include "itkTransform.h" |
| 26 | +#include "itkTransformIOBase.h" |
| 27 | +#include "itkTransformIOFactory.h" |
| 28 | +#include "WebAssemblyInterfaceExport.h" |
| 29 | + |
| 30 | +#include "itkTransformJSON.h" |
| 31 | + |
| 32 | +namespace itk |
| 33 | +{ |
| 34 | + |
| 35 | +WebAssemblyInterface_EXPORT bool |
| 36 | +lexical_cast(const std::string & input, TransformTypeJSON & transformType); |
| 37 | + |
| 38 | +namespace wasm |
| 39 | +{ |
| 40 | + |
| 41 | +/** \class SupportInputTransformTypes |
| 42 | + * |
| 43 | + * \brief Instantiatiate a Pipeline functor over multiple transform parameter types and dimensions and match to the input transform type. |
| 44 | + * |
| 45 | + * Instantiate the PipelineFunctor (function object) over multiple transform types. |
| 46 | + * If the input transform matches these parameter types and dimensions, use the compile-time optimized pipeline for that transform type. |
| 47 | + * Otherwise, exit the pipeline with an error identifying the unsupported transform type. |
| 48 | + * |
| 49 | + * Example usage: |
| 50 | + * |
| 51 | +``` |
| 52 | +template<typename TTransform> |
| 53 | +class PipelineFunctor |
| 54 | +{ |
| 55 | +public: |
| 56 | + int operator()(itk::wasm::Pipeline & pipeline) |
| 57 | + { |
| 58 | + using TransformType = TTransform; |
| 59 | +
|
| 60 | + using InputTransformType = itk::wasm::InputTransform<TransformType>; |
| 61 | + InputTransformType inputTransform; |
| 62 | + pipeline.add_option("input-transform", inputTransform, "The input polydata")->required(); |
| 63 | +``` |
| 64 | +[...] |
| 65 | +
|
| 66 | +``` |
| 67 | +int |
| 68 | +main(int argc, char * argv[]) |
| 69 | +{ |
| 70 | + itk::wasm::Pipeline pipeline("support-multiple", "Test supporting multiple input polydata types", argc, argv); |
| 71 | +
|
| 72 | + // Supports the parameters types float, double, and dimensions 2 and 3 |
| 73 | + return itk::wasm::SupportInputTransformTypes<PipelineFunctor, float, double> |
| 74 | + ::Dimensions<2U, 3U>("input-transform", pipeline); |
| 75 | +} |
| 76 | +``` |
| 77 | + * It is assumed that the input dimension and output dimension will be the same. |
| 78 | + * |
| 79 | + * \ingroup WebAssemblyInterface |
| 80 | + */ |
| 81 | +template <template <typename TTransform> class TPipelineFunctor, typename... TParameterValuesSupported> |
| 82 | +class SupportInputTransformTypes |
| 83 | +{ |
| 84 | +public: |
| 85 | + template <unsigned int... VDimensions> |
| 86 | + static int |
| 87 | + Dimensions(const std::string & inputTransformOptionName, Pipeline & pipeline) |
| 88 | + { |
| 89 | + TransformTypeJSON transformType; |
| 90 | + |
| 91 | + const auto iwpArgc = pipeline.get_argc(); |
| 92 | + const auto iwpArgv = pipeline.get_argv(); |
| 93 | + bool passThrough = false; |
| 94 | + for (int ii = 0; ii < iwpArgc; ++ii) |
| 95 | + { |
| 96 | + const std::string arg(iwpArgv[ii]); |
| 97 | + if (arg == "-h" || arg == "--help" || arg == "--interface-json" || arg == "--version") |
| 98 | + { |
| 99 | + passThrough = true; |
| 100 | + } |
| 101 | + } |
| 102 | + if (passThrough) |
| 103 | + { |
| 104 | + return IterateDimensions<VDimensions...>(pipeline, transformType, passThrough); |
| 105 | + } |
| 106 | + |
| 107 | + auto tempOption = pipeline.add_option(inputTransformOptionName, transformType, "Read Transform type."); |
| 108 | + |
| 109 | + ITK_WASM_PRE_PARSE(pipeline); |
| 110 | + |
| 111 | + pipeline.remove_option(tempOption); |
| 112 | + |
| 113 | + return IterateDimensions<VDimensions...>(pipeline, transformType); |
| 114 | + } |
| 115 | + |
| 116 | +private: |
| 117 | + template <unsigned int VDimension, typename TParameterValues, typename... TParameterValuesRest> |
| 118 | + static int |
| 119 | + IterateParameterValueTypes(Pipeline & pipeline, const TransformTypeJSON & transformType, bool passThrough = false) |
| 120 | + { |
| 121 | + using ParameterValueType = TParameterValues; |
| 122 | + |
| 123 | + if (passThrough || |
| 124 | + transformType.parametersValueType == MapComponentType<ParameterValueType>::JSONFloatTypeEnum) |
| 125 | + { |
| 126 | + using TransformType = Transform<ParameterValueType, VDimension, VDimension>; |
| 127 | + |
| 128 | + using PipelineType = TPipelineFunctor<TransformType>; |
| 129 | + return PipelineType()(pipeline); |
| 130 | + } |
| 131 | + |
| 132 | + if constexpr (sizeof...(TParameterValuesRest) > 0) |
| 133 | + { |
| 134 | + return IterateParameterValueTypes<VDimension, TParameterValuesRest...>(pipeline, transformType); |
| 135 | + } |
| 136 | + |
| 137 | + std::ostringstream ostrm; |
| 138 | + std::string transformTypeString = glz::write_json(transformType).value_or("error"); |
| 139 | + ostrm << "Unsupported transform type: " << transformTypeString << std::endl; |
| 140 | + CLI::Error err("Runtime error", ostrm.str(), 1); |
| 141 | + return pipeline.exit(err); |
| 142 | + } |
| 143 | + |
| 144 | + template <unsigned int VDimension, unsigned int... VDimensions> |
| 145 | + static int |
| 146 | + IterateDimensions(Pipeline & pipeline, const TransformTypeJSON & transformType, bool passThrough = false) |
| 147 | + { |
| 148 | + if (passThrough || VDimension == transformType.inputDimension) |
| 149 | + { |
| 150 | + return IterateParameterValueTypes<VDimension, TParameterValuesSupported...>(pipeline, transformType); |
| 151 | + } |
| 152 | + |
| 153 | + if constexpr (sizeof...(VDimensions) > 0) |
| 154 | + { |
| 155 | + return IterateDimensions<VDimensions...>(pipeline, transformType); |
| 156 | + } |
| 157 | + |
| 158 | + std::ostringstream ostrm; |
| 159 | + ostrm << "Unsupported transform dimension: " << transformType.inputDimension; |
| 160 | + CLI::Error err("Runtime error", ostrm.str(), 1); |
| 161 | + return pipeline.exit(err); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +} // end namespace wasm |
| 166 | +} // end namespace itk |
| 167 | + |
| 168 | +#endif |
0 commit comments