Skip to content

Commit 37176aa

Browse files
committed
Add UnaryBlockOperator.numDimensions()
1 parent 8aad7f2 commit 37176aa

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

src/main/java/net/imglib2/algorithm/blocks/ConcatenatedBlockSupplier.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ public < S extends NativeType< S > > ConcatenatedBlockSupplier(
5858
this.p0 = srcSupplier;
5959
this.p1 = operator.blockProcessor();
6060
this.type = operator.getTargetType();
61-
this.numDimensions = srcSupplier.numDimensions(); // TODO: REVISE. The operator should determine the number of (target) dimensions. This is only to make it compile.
61+
if ( operator.numSourceDimensions() > 0 )
62+
{
63+
if ( srcSupplier.numDimensions() != operator.numSourceDimensions() )
64+
throw new IllegalArgumentException( "UnaryBlockOperator cannot be concatenated: number of dimensions mismatch." );
65+
this.numDimensions = operator.numTargetDimensions();
66+
}
67+
else
68+
{
69+
this.numDimensions = srcSupplier.numDimensions();
70+
}
6271
}
6372

6473
private ConcatenatedBlockSupplier( final ConcatenatedBlockSupplier< T > s )

src/main/java/net/imglib2/algorithm/blocks/DefaultUnaryBlockOperator.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ public class DefaultUnaryBlockOperator< S extends NativeType< S >, T extends Nat
4242
{
4343
private final S sourceType;
4444
private final T targetType;
45+
private final int numSourceDimensions;
46+
private final int numTargetDimensions;
4547
private final BlockProcessor< ?, ? > blockProcessor;
4648

47-
public DefaultUnaryBlockOperator( S sourceType, T targetType, BlockProcessor< ?, ? > blockProcessor )
49+
public DefaultUnaryBlockOperator( S sourceType, T targetType, int numSourceDimensions, int numTargetDimensions, BlockProcessor< ?, ? > blockProcessor )
4850
{
4951
this.sourceType = sourceType;
5052
this.targetType = targetType;
53+
this.numSourceDimensions = numSourceDimensions;
54+
this.numTargetDimensions = numTargetDimensions;
5155
this.blockProcessor = blockProcessor;
5256
}
5357

@@ -69,10 +73,22 @@ public T getTargetType()
6973
return targetType;
7074
}
7175

76+
@Override
77+
public int numSourceDimensions()
78+
{
79+
return numSourceDimensions;
80+
}
81+
82+
@Override
83+
public int numTargetDimensions()
84+
{
85+
return numTargetDimensions;
86+
}
87+
7288
@Override
7389
public UnaryBlockOperator< S, T > independentCopy()
7490
{
75-
return new DefaultUnaryBlockOperator<>( sourceType, targetType, blockProcessor.independentCopy() );
91+
return new DefaultUnaryBlockOperator<>( sourceType, targetType, numSourceDimensions, numTargetDimensions, blockProcessor.independentCopy() );
7692
}
7793

7894
private Supplier< UnaryBlockOperator< S, T > > threadSafeSupplier;
@@ -102,6 +118,18 @@ public T getTargetType()
102118
return targetType;
103119
}
104120

121+
@Override
122+
public int numSourceDimensions()
123+
{
124+
return numSourceDimensions;
125+
}
126+
127+
@Override
128+
public int numTargetDimensions()
129+
{
130+
return numTargetDimensions;
131+
}
132+
105133
@Override
106134
public UnaryBlockOperator< S, T > threadSafe()
107135
{

src/main/java/net/imglib2/algorithm/blocks/UnaryBlockOperator.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ public interface UnaryBlockOperator< S extends NativeType< S >, T extends Native
7777

7878
T getTargetType();
7979

80+
/**
81+
* Number of source dimensions.
82+
* <p>
83+
* Some operators can be applied to arbitrary dimensions, e.g., converters.
84+
* In this case, they should return {@code numSourceDimensions() <= 0}. It
85+
* is expected that these operators do not modify the number dimensions,
86+
* that is, when such an operator is applied to a 3D block, the result is
87+
* also a 3D block.
88+
*
89+
* @return the number of source dimensions
90+
*/
91+
int numSourceDimensions();
92+
93+
/**
94+
* Number of target dimensions.
95+
* <p>
96+
* Some operators can be applied to arbitrary dimensions, e.g., converters.
97+
* In this case, they should return {@code numTargetDimensions() <= 0}. It
98+
* is expected that these operators do not modify the number dimensions,
99+
* that is, when such an operator is applied to a 3D block, the result is
100+
* also a 3D block.
101+
*
102+
* @return the number of source dimensions
103+
*/
104+
int numTargetDimensions();
105+
80106
/**
81107
* Get a thread-safe version of this {@code UnaryBlockOperator}.
82108
* (Implemented as a wrapper that makes {@link ThreadLocal} copies).
@@ -96,9 +122,18 @@ public interface UnaryBlockOperator< S extends NativeType< S >, T extends Native
96122
*/
97123
default < U extends NativeType< U > > UnaryBlockOperator< S, U > andThen( UnaryBlockOperator< T, U > op )
98124
{
125+
final boolean thisHasDimensions = numSourceDimensions() > 0;
126+
final boolean opHasDimensions = op.numSourceDimensions() > 0;
127+
if ( opHasDimensions && thisHasDimensions && numTargetDimensions() != op.numSourceDimensions() ) {
128+
throw new IllegalArgumentException( "UnaryBlockOperator cannot be concatenated: number of dimensions mismatch." );
129+
}
130+
final int numSourceDimensions = thisHasDimensions ? numSourceDimensions() : op.numSourceDimensions();
131+
final int numTargetDimensions = opHasDimensions ? op.numTargetDimensions() : numTargetDimensions();
99132
return new DefaultUnaryBlockOperator<>(
100133
getSourceType(),
101134
op.getTargetType(),
135+
numSourceDimensions,
136+
numTargetDimensions,
102137
blockProcessor().andThen( op.blockProcessor() ) );
103138
}
104139

src/main/java/net/imglib2/algorithm/blocks/convert/Convert.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType )
7878
public static < S extends NativeType< S >, T extends NativeType< T > >
7979
UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType, final ClampType clamp )
8080
{
81-
return new DefaultUnaryBlockOperator<>( sourceType, targetType, new ConvertBlockProcessor<>( sourceType, targetType, clamp ) );
81+
return new DefaultUnaryBlockOperator<>(
82+
sourceType, targetType, 0, 0,
83+
new ConvertBlockProcessor<>( sourceType, targetType, clamp ) );
8284
}
8385

8486
/**
@@ -88,6 +90,8 @@ UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType, fina
8890
public static < S extends NativeType< S >, T extends NativeType< T > >
8991
UnaryBlockOperator< S, T > convert( final S sourceType, final T targetType, Supplier< Converter< ? super S, T > > converterSupplier )
9092
{
91-
return new DefaultUnaryBlockOperator<>( sourceType, targetType, new ConverterBlockProcessor<>( sourceType, targetType, converterSupplier ) );
93+
return new DefaultUnaryBlockOperator<>(
94+
sourceType, targetType, 0, 0,
95+
new ConverterBlockProcessor<>( sourceType, targetType, converterSupplier ) );
9296
}
9397
}

src/main/java/net/imglib2/algorithm/blocks/downsample/Downsample.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,21 @@ UnaryBlockOperator< T, T > downsample( final T type, final int numDimensions )
280280

281281

282282

283-
private static UnaryBlockOperator< FloatType, FloatType > downsampleFloat( Offset offset, final boolean[] downsampleInDim )
283+
private static UnaryBlockOperator< FloatType, FloatType > downsampleFloat( final Offset offset, final boolean[] downsampleInDim )
284284
{
285285
final FloatType type = new FloatType();
286-
return new DefaultUnaryBlockOperator<>( type, type,
286+
final int n = downsampleInDim.length;
287+
return new DefaultUnaryBlockOperator<>( type, type, n, n,
287288
offset == Offset.HALF_PIXEL
288289
? new HalfPixelFloat( downsampleInDim )
289290
: new CenterFloat( downsampleInDim ) );
290291
}
291292

292-
private static UnaryBlockOperator< DoubleType, DoubleType > downsampleDouble( Offset offset, final boolean[] downsampleInDim )
293+
private static UnaryBlockOperator< DoubleType, DoubleType > downsampleDouble( final Offset offset, final boolean[] downsampleInDim )
293294
{
294295
final DoubleType type = new DoubleType();
295-
return new DefaultUnaryBlockOperator<>( type, type,
296+
final int n = downsampleInDim.length;
297+
return new DefaultUnaryBlockOperator<>( type, type, n, n,
296298
offset == Offset.HALF_PIXEL
297299
? new HalfPixelDouble( downsampleInDim )
298300
: new CenterDouble( downsampleInDim ) );

src/main/java/net/imglib2/algorithm/blocks/transform/Transform.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ UnaryBlockOperator< T, T > affine( final T type, final AffineGet transformFromSo
155155

156156
private static < T extends NativeType< T > > UnaryBlockOperator< T, T > _affine( final AffineGet transform, final Interpolation interpolation, final T type )
157157
{
158-
return new DefaultUnaryBlockOperator<>( type, type,
159-
transform.numDimensions() == 2
158+
final int n = transform.numDimensions();
159+
if ( n < 2 || n > 3 )
160+
throw new IllegalArgumentException( "Only 2D and 3D affine transforms are supported currently" );
161+
return new DefaultUnaryBlockOperator<>( type, type, n, n,
162+
n == 2
160163
? new Affine2DProcessor<>( ( AffineTransform2D ) transform, interpolation, type.getNativeTypeFactory().getPrimitiveType() )
161164
: new Affine3DProcessor<>( ( AffineTransform3D ) transform, interpolation, type.getNativeTypeFactory().getPrimitiveType() ) );
162165
}

0 commit comments

Comments
 (0)