@@ -166,3 +166,27 @@ def test_bspline_to_x5(tmp_path):
166166 assert np .allclose (xfm ._coeffs , xfm2 ._coeffs )
167167 assert xfm2 .reference .shape == ref .shape
168168 assert np .allclose (xfm2 .reference .affine , ref .affine )
169+
170+
171+ @pytest .mark .parametrize ("is_deltas" , [True , False ])
172+ def test_densefield_oob_resampling (is_deltas ):
173+ """Ensure mapping outside the field returns input coordinates."""
174+ ref = nb .Nifti1Image (np .zeros ((2 , 2 , 2 ), dtype = "uint8" ), np .eye (4 ))
175+
176+ if is_deltas :
177+ field = nb .Nifti1Image (np .ones ((2 , 2 , 2 , 3 ), dtype = "float32" ), np .eye (4 ))
178+ else :
179+ grid = np .stack (
180+ np .meshgrid (* [np .arange (2 ) for _ in range (3 )], indexing = "ij" ),
181+ axis = - 1 ,
182+ ).astype ("float32" )
183+ field = nb .Nifti1Image (grid + 1.0 , np .eye (4 ))
184+
185+ xfm = DenseFieldTransform (field , is_deltas = is_deltas , reference = ref )
186+
187+ points = np .array ([[- 1.0 , - 1.0 , - 1.0 ], [0.5 , 0.5 , 0.5 ], [3.0 , 3.0 , 3.0 ]])
188+ mapped = xfm .map (points )
189+
190+ assert np .allclose (mapped [0 ], points [0 ])
191+ assert np .allclose (mapped [2 ], points [2 ])
192+ assert np .allclose (mapped [1 ], points [1 ] + 1 )
0 commit comments