Skip to content

Commit 5187709

Browse files
committed
fix(run-pipeline-emscripten): fix reading composite transforms
The first item in a composite transform list does not have input arrays.
1 parent fc8a8f3 commit 5187709

4 files changed

Lines changed: 99 additions & 30 deletions

File tree

packages/core/typescript/itk-wasm/src/pipeline/internal/run-pipeline-emscripten.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const haveSharedArrayBuffer = typeof globalThis.SharedArrayBuffer === 'function'
2222
const encoder = new TextEncoder()
2323
const decoder = new TextDecoder('utf-8')
2424

25-
function readFileSharedArray(
25+
function readFileSharedArray (
2626
emscriptenModule: PipelineEmscriptenModule,
2727
path: string
2828
): Uint8Array {
@@ -42,7 +42,7 @@ function readFileSharedArray(
4242
return array
4343
}
4444

45-
function memoryUint8SharedArray(
45+
function memoryUint8SharedArray (
4646
emscriptenModule: PipelineEmscriptenModule,
4747
byteOffset: number,
4848
length: number
@@ -63,7 +63,7 @@ function memoryUint8SharedArray(
6363
return array
6464
}
6565

66-
function setPipelineModuleInputArray(
66+
function setPipelineModuleInputArray (
6767
emscriptenModule: PipelineEmscriptenModule,
6868
dataArray: TypedArray | null,
6969
inputIndex: number,
@@ -82,7 +82,7 @@ function setPipelineModuleInputArray(
8282
return dataPtr
8383
}
8484

85-
function setPipelineModuleInputJSON(
85+
function setPipelineModuleInputJSON (
8686
emscriptenModule: PipelineEmscriptenModule,
8787
dataObject: object,
8888
inputIndex: number
@@ -98,13 +98,13 @@ function setPipelineModuleInputJSON(
9898
emscriptenModule.stringToUTF8(dataJSON, jsonPtr, length)
9999
}
100100

101-
function getPipelineModuleOutputArray(
101+
function getPipelineModuleOutputArray (
102102
emscriptenModule: PipelineEmscriptenModule,
103103
outputIndex: number,
104104
subIndex: number,
105105
componentType:
106-
| (typeof IntTypes)[keyof typeof IntTypes]
107-
| (typeof FloatTypes)[keyof typeof FloatTypes]
106+
| (typeof IntTypes)[keyof typeof IntTypes]
107+
| (typeof FloatTypes)[keyof typeof FloatTypes]
108108
): TypedArray | Float32Array | Uint32Array | null {
109109
const dataPtr = emscriptenModule.ccall(
110110
'itk_wasm_output_array_address',
@@ -123,7 +123,7 @@ function getPipelineModuleOutputArray(
123123
return data
124124
}
125125

126-
function getPipelineModuleOutputJSON(
126+
function getPipelineModuleOutputJSON (
127127
emscriptenModule: PipelineEmscriptenModule,
128128
outputIndex: number
129129
): object {
@@ -138,7 +138,7 @@ function getPipelineModuleOutputJSON(
138138
return dataObject
139139
}
140140

141-
function runPipelineEmscripten(
141+
function runPipelineEmscripten (
142142
pipelineModule: PipelineEmscriptenModule,
143143
args: string[],
144144
outputs: PipelineOutput[] | null,
@@ -313,21 +313,34 @@ function runPipelineEmscripten(
313313
case InterfaceTypes.TransformList: {
314314
const transformList = input.data as TransformList
315315
const transformListJSON: any = []
316-
transformList.forEach((transform, transformIndex) => {
317-
const fixedParameterPtr = setPipelineModuleInputArray(
318-
pipelineModule,
319-
transform.fixedParameters,
320-
index,
321-
transformIndex * 2
322-
)
323-
const fixedParameters = `data:application/vnd.itk.address,0:${fixedParameterPtr}`
324-
const parameterPtr = setPipelineModuleInputArray(
325-
pipelineModule,
326-
transform.parameters,
327-
index,
328-
transformIndex * 2 + 1
329-
)
330-
const parameters = `data:application/vnd.itk.address,0:${parameterPtr}`
316+
let inputArrayIndex = 0
317+
transformList.forEach((transform) => {
318+
let fixedParameters = ''
319+
let parameters = ''
320+
321+
// Skip setting input arrays for Composite transforms as they don't have array data
322+
if (
323+
transform.transformType.transformParameterization !== 'Composite'
324+
) {
325+
const fixedParameterPtr = setPipelineModuleInputArray(
326+
pipelineModule,
327+
transform.fixedParameters,
328+
index,
329+
inputArrayIndex
330+
)
331+
fixedParameters = `data:application/vnd.itk.address,0:${fixedParameterPtr}`
332+
inputArrayIndex += 1
333+
334+
const parameterPtr = setPipelineModuleInputArray(
335+
pipelineModule,
336+
transform.parameters,
337+
index,
338+
inputArrayIndex
339+
)
340+
parameters = `data:application/vnd.itk.address,0:${parameterPtr}`
341+
inputArrayIndex += 1
342+
}
343+
331344
const transformJSON = {
332345
transformType: transform.transformType,
333346
numberOfFixedParameters: transform.numberOfFixedParameters,

packages/core/typescript/itk-wasm/test/node/pipeline/run-pipeline-node-test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ function readCompositeTransform() {
145145

146146
// Skip the composite transform (index 0) as it doesn't have separate data files
147147
if (transform.transformType.transformParameterization === 'Composite') {
148+
// transform.fixedParameters = new Float64Array(0)
149+
// transform.parameters = new Float32Array(0)
150+
transform.fixedParameters = null
151+
transform.parameters = null
148152
continue
149153
}
150154

@@ -164,7 +168,7 @@ function readCompositeTransform() {
164168
),
165169
null
166170
)
167-
const fixedParameters = new Float32Array(
171+
const fixedParameters = new Float64Array(
168172
fixedParametersBuffer.buffer.slice(
169173
fixedParametersBuffer.byteOffset,
170174
fixedParametersBuffer.byteOffset + fixedParametersBuffer.byteLength
@@ -606,16 +610,16 @@ test.only('runPipelineNode writes and reads a CompositeTransform itk.TransformLi
606610
'Rigid2D parameters should be Float32Array'
607611
)
608612
t.true(
609-
rigid2DTransform.fixedParameters instanceof Float32Array,
610-
'Rigid2D fixedParameters should be Float32Array'
613+
rigid2DTransform.fixedParameters instanceof Float64Array,
614+
'Rigid2D fixedParameters should be Float64Array'
611615
)
612616
t.true(
613617
affineTransform.parameters instanceof Float32Array,
614618
'Affine parameters should be Float32Array'
615619
)
616620
t.true(
617-
affineTransform.fixedParameters instanceof Float32Array,
618-
'Affine fixedParameters should be Float32Array'
621+
affineTransform.fixedParameters instanceof Float64Array,
622+
'Affine fixedParameters should be Float64Array'
619623
)
620624
}
621625

@@ -625,7 +629,7 @@ test.only('runPipelineNode writes and reads a CompositeTransform itk.TransformLi
625629
'pipelines',
626630
'emscripten-build',
627631
'transform-read-write-pipeline',
628-
'transform-read-write-test'
632+
'transform-read-write-composite-test'
629633
)
630634
const args = ['0', '0', '--memory-io']
631635
const desiredOutputs = [{ type: InterfaceTypes.TransformList }]

packages/core/typescript/itk-wasm/test/pipelines/transform-read-write-pipeline/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ include(${ITK_USE_FILE})
1717
add_executable(transform-read-write-test transform-read-write-test.cxx)
1818
target_link_libraries(transform-read-write-test PUBLIC ${ITK_LIBRARIES})
1919

20+
add_executable(transform-read-write-composite-test transform-read-write-composite-test.cxx)
21+
target_link_libraries(transform-read-write-composite-test PUBLIC ${ITK_LIBRARIES})
22+
2023
enable_testing()
2124
add_test(NAME transform-read-write-test
2225
COMMAND transform-read-write-test ${CMAKE_CURRENT_SOURCE_DIR}/LinearTransform.h5
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#include "itkCompositeTransform.h"
19+
#include "itkInputTransform.h"
20+
#include "itkOutputTransform.h"
21+
#include "itkPipeline.h"
22+
23+
int
24+
main(int argc, char * argv[])
25+
{
26+
itk::wasm::Pipeline pipeline("transform-read-write-composite-test", "A test for reading and writing composite transforms", argc, argv);
27+
28+
using ParametersValueType = float;
29+
constexpr unsigned int Dimension = 2;
30+
using TransformType = itk::CompositeTransform<ParametersValueType, Dimension>;
31+
32+
using InputTransformType = itk::wasm::InputTransform<TransformType>;
33+
InputTransformType inputTransform;
34+
pipeline.add_option("input-transform", inputTransform, "The input transform")
35+
->required()
36+
->type_name("INPUT_TRANSFORM");
37+
38+
using OutputTransformType = itk::wasm::OutputTransform<TransformType>;
39+
OutputTransformType outputTransform;
40+
pipeline.add_option("output-transform", outputTransform, "The output transform")
41+
->required()
42+
->type_name("OUTPUT_TRANSFORM");
43+
44+
ITK_WASM_PARSE(pipeline);
45+
46+
outputTransform.Set(inputTransform.Get());
47+
48+
return EXIT_SUCCESS;
49+
}

0 commit comments

Comments
 (0)