Skip to content

Commit 44fc029

Browse files
committed
fix(create-itk-wasm): support dispatch on parameters
And also multi-component inputs. Test command: rm -rf /tmp/create-test/* && node dist/create-itk-wasm.js -o /tmp/create-test -n "parameters-dispatch-test" -d "Test dispatch on parameters" --pipeline-name "parameter-dispatch" --pipeline-description "I dispatch on a parameter instead of an input." --pipeline-dispatch Image --pipeline-parameters "input-images:Image:Input images::true:1:-1" --pipeline-outputs "output-image:Image:Output image." --build-and-test --no-input
1 parent dbb30c3 commit 44fc029

File tree

1 file changed

+48
-10
lines changed
  • packages/core/typescript/create-itk-wasm/src/generate

1 file changed

+48
-10
lines changed

packages/core/typescript/create-itk-wasm/src/generate/pipeline.ts

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,31 @@ import Dispatch from '../dispatch.js'
1111
import die from '../die.js'
1212
import camelCase from '../camel-case.js'
1313

14-
function findDispatchInput(pipeline: PipelineSpec): OptionSpec {
15-
return pipeline.inputs.find(
14+
interface DispatchInput {
15+
kind: string
16+
spec: OptionSpec
17+
}
18+
19+
function findDispatchInput(pipeline: PipelineSpec): DispatchInput {
20+
const foundInput = pipeline.inputs.find(
1621
(input) => (input.type as unknown as Dispatch) === pipeline.dispatch
1722
)
23+
if (typeof foundInput !== 'undefined') {
24+
return { kind: 'input', spec: foundInput }
25+
}
26+
27+
const foundParameter = pipeline.parameters.find(
28+
(param) => (param.type as unknown as Dispatch) === pipeline.dispatch
29+
)
30+
if (typeof foundParameter !== 'undefined') {
31+
return { kind: 'parameter', spec: foundParameter }
32+
}
33+
34+
die(
35+
`Pipeline ${pipeline.name} dispatch ${pipeline.dispatch} not found in inputs or parameters.`
36+
)
37+
// tslint:disable-next-line: no-unreachable
38+
throw new Error('unreachable')
1839
}
1940

2041
function pipelineFunction(
@@ -25,7 +46,7 @@ function pipelineFunction(
2546
return ''
2647
}
2748
const dispatch = pipeline.dispatch
28-
const dispatchInput = findDispatchInput(pipeline)
49+
const { kind, spec: dispatchInput } = findDispatchInput(pipeline)
2950
const camelDispatchInput = camelCase(dispatchInput.name)
3051

3152
let dimensionTrait = 'Dimension'
@@ -52,19 +73,21 @@ function pipelineFunction(
5273

5374
const firstTypeSizeName = optionTypeSizeName(
5475
dispatchInput.type,
55-
'input',
76+
kind,
5677
dispatchInput.itemsExpectedMin,
5778
dispatchInput.itemsExpectedMax
5879
)
80+
const argt = dispatchInput.itemsExpectedMax === 1 ? `T${dispatch} * ` : `std::vector<itk::wasm::Input${dispatch}<T${dispatch}>> & `
81+
const flag = kind === 'parameter' ? '--' : ''
5982
let result = ''
6083
result += `template <typename T${dispatch}>
61-
int ${camelCase(pipeline.name)}(itk::wasm::Pipeline &pipeline, const T${dispatch} *${camelDispatchInput})
84+
int ${camelCase(pipeline.name)}(itk::wasm::Pipeline &pipeline, const ${argt}${camelDispatchInput})
6285
{
6386
using ${dispatch}Type = T${dispatch};
6487
constexpr unsigned int Dimension = ${dispatch}Type::${dimensionTrait};
6588
using PixelType = typename ${dispatch}Type::${pixelTypeTrait};${defaultImageType}${defaultMeshType}${defaultPolyDataType}
6689
67-
pipeline.get_option("${dispatchInput.name}")->required()${firstTypeSizeName};
90+
pipeline.get_option("${flag}${dispatchInput.name}")->required()${firstTypeSizeName};
6891
6992
`
7093

@@ -75,6 +98,9 @@ int ${camelCase(pipeline.name)}(itk::wasm::Pipeline &pipeline, const T${dispatch
7598
result += optionCxx(indent, 'input', input, false)
7699
})
77100
pipeline.parameters.forEach((parameter) => {
101+
if (parameter === dispatchInput) {
102+
return
103+
}
78104
result += optionCxx(indent, 'parameter', parameter, false)
79105
})
80106
pipeline.outputs.forEach((output) => {
@@ -98,7 +124,7 @@ function pipelineFunctor(pipeline: PipelineSpec): string {
98124
return ''
99125
}
100126
const dispatch = pipeline.dispatch
101-
const dispatchInput = findDispatchInput(pipeline)
127+
const { kind, spec: dispatchInput } = findDispatchInput(pipeline)
102128
const camelDispatchInput = camelCase(dispatchInput.name)
103129

104130
let result = `template <typename T${dispatch}>
@@ -111,16 +137,27 @@ public:
111137
112138
`
113139

114-
result += optionCxx(' ', 'input', dispatchInput, true)
140+
result += optionCxx(' ', kind, dispatchInput, true)
115141

116142
result += ` ITK_WASM_PRE_PARSE(pipeline);
143+
`
117144

145+
if (dispatchInput.itemsExpectedMax === 1) {
146+
result += `
118147
typename ${dispatch}Type::ConstPointer ${camelDispatchInput}Ref = ${camelDispatchInput}.Get();
119148
return ${camelCase(pipeline.name)}<${dispatch}Type>(pipeline, ${camelDispatchInput}Ref);
120149
}
121150
};
122151
123152
`
153+
} else {
154+
result += `
155+
return ${camelCase(pipeline.name)}<${dispatch}Type>(pipeline, ${camelDispatchInput});
156+
}
157+
};
158+
159+
`
160+
}
124161
return result
125162
}
126163

@@ -185,7 +222,7 @@ function pipelineMain(
185222
}
186223
`
187224
} else {
188-
const dispatchInput = findDispatchInput(pipeline)
225+
const { kind, spec: dispatchInput } = findDispatchInput(pipeline)
189226
const dimensionString = pipeline.dispatchDimensions
190227
.map((dimension) => {
191228
return `${dimension}U`
@@ -195,8 +232,9 @@ function pipelineMain(
195232
pipeline.dispatch === 'PolyData'
196233
? `>::PixelTypes<${pipeline.dispatchPixels.join(', ')}>`
197234
: `,\n ${pipeline.dispatchPixels.join(', ')}>\n ::Dimensions<${dimensionString}>`
235+
const flags = kind === 'parameter' ? '--' : ''
198236
result += `
199-
return itk::wasm::SupportInput${pipeline.dispatch}Types<PipelineFunctor${dispatchArgs}("${dispatchInput.name}", pipeline);
237+
return itk::wasm::SupportInput${pipeline.dispatch}Types<PipelineFunctor${dispatchArgs}("${flags}${dispatchInput.name}", pipeline);
200238
}
201239
`
202240
}

0 commit comments

Comments
 (0)