27
27
import net .imglib2 .Cursor ;
28
28
import net .imglib2 .RandomAccessibleInterval ;
29
29
import net .imglib2 .img .Img ;
30
- import net .imglib2 .type .Type ;
30
+ import net .imglib2 .type .NativeType ;
31
+ import net .imglib2 .type .numeric .RealType ;
31
32
import net .imglib2 .type .numeric .integer .ByteType ;
32
33
import net .imglib2 .type .numeric .integer .IntType ;
33
34
import net .imglib2 .type .numeric .real .DoubleType ;
34
35
import net .imglib2 .type .numeric .real .FloatType ;
36
+ import net .imglib2 .util .Cast ;
35
37
import net .imglib2 .util .Util ;
36
38
import net .imglib2 .view .Views ;
37
39
@@ -60,26 +62,9 @@ public class NDArrayBuilder {
60
62
* @return The {@link NDArray} built from the {@link Tensor}.
61
63
* @throws IllegalArgumentException If the tensor type is not supported.
62
64
*/
63
- public static NDArray build (Tensor tensor , NDManager manager )
64
- throws IllegalArgumentException
65
- {
66
- // Create an Icy sequence of the same type of the tensor
67
- if (Util .getTypeFromInterval (tensor .getData ()) instanceof ByteType ) {
68
- return buildFromTensorByte (tensor .getData (), manager );
69
- }
70
- else if (Util .getTypeFromInterval (tensor .getData ()) instanceof IntType ) {
71
- return buildFromTensorInt (tensor .getData (), manager );
72
- }
73
- else if (Util .getTypeFromInterval (tensor .getData ()) instanceof FloatType ) {
74
- return buildFromTensorFloat (tensor .getData (), manager );
75
- }
76
- else if (Util .getTypeFromInterval (tensor .getData ()) instanceof DoubleType ) {
77
- return buildFromTensorDouble (tensor .getData (), manager );
78
- }
79
- else {
80
- throw new IllegalArgumentException ("Unsupported tensor type: " + tensor
81
- .getDataType ());
82
- }
65
+ public static <T extends RealType <T > & NativeType <T >>
66
+ NDArray build (Tensor <T > tensor , NDManager manager ) throws IllegalArgumentException {
67
+ return build (tensor .getData (), manager );
83
68
}
84
69
85
70
/**
@@ -94,42 +79,28 @@ else if (Util.getTypeFromInterval(tensor.getData()) instanceof DoubleType) {
94
79
* @return The {@link NDArray} built from the {@link RandomAccessibleInterval}.
95
80
* @throws IllegalArgumentException if the {@link RandomAccessibleInterval} is not supported
96
81
*/
97
- public static <T extends Type <T >> NDArray build (
98
- RandomAccessibleInterval <T > tensor , NDManager manager )
82
+ public static <T extends RealType <T > & NativeType < T >>
83
+ NDArray build ( RandomAccessibleInterval <T > tensor , NDManager manager )
99
84
throws IllegalArgumentException
100
85
{
101
86
if (Util .getTypeFromInterval (tensor ) instanceof ByteType ) {
102
- return buildFromTensorByte ((RandomAccessibleInterval <ByteType >) tensor ,
103
- manager );
87
+ return buildFromTensorByte (Cast .unchecked (tensor ), manager );
104
88
}
105
89
else if (Util .getTypeFromInterval (tensor ) instanceof IntType ) {
106
- return buildFromTensorInt ((RandomAccessibleInterval <IntType >) tensor ,
107
- manager );
90
+ return buildFromTensorInt (Cast .unchecked (tensor ), manager );
108
91
}
109
92
else if (Util .getTypeFromInterval (tensor ) instanceof FloatType ) {
110
- return buildFromTensorFloat ((RandomAccessibleInterval <FloatType >) tensor ,
111
- manager );
93
+ return buildFromTensorFloat (Cast .unchecked (tensor ), manager );
112
94
}
113
95
else if (Util .getTypeFromInterval (tensor ) instanceof DoubleType ) {
114
- return buildFromTensorDouble (
115
- (RandomAccessibleInterval <DoubleType >) tensor , manager );
96
+ return buildFromTensorDouble (Cast .unchecked (tensor ), manager );
116
97
}
117
98
else {
118
99
throw new IllegalArgumentException ("Unsupported tensor type: " + Util
119
100
.getTypeFromInterval (tensor ).getClass ().toString ());
120
101
}
121
102
}
122
103
123
- /**
124
- * Builds a {@link NDArray} from a signed byte-typed
125
- * {@link RandomAccessibleInterval}.
126
- *
127
- * @param tensor
128
- * the {@link RandomAccessibleInterval} that will be copied into an {@link NDArray}
129
- * @param manager
130
- * {@link NDManager} needed to create a {@link NDArray}
131
- * @return The {@link NDArray} built from the tensor of type {@link ByteType}.
132
- */
133
104
private static NDArray buildFromTensorByte (
134
105
RandomAccessibleInterval <ByteType > tensor , NDManager manager )
135
106
{
@@ -156,16 +127,6 @@ private static NDArray buildFromTensorByte(
156
127
return ndarray ;
157
128
}
158
129
159
- /**
160
- * Builds a {@link NDArray} from a signed integer-typed
161
- * {@link RandomAccessibleInterval}.
162
- *
163
- * @param tensor
164
- * the {@link RandomAccessibleInterval} that will be copied into an {@link NDArray}
165
- * @param manager
166
- * {@link NDManager} needed to create a {@link NDArray}
167
- * @return The {@link NDArray} built from the tensor of type {@link IntType}.
168
- */
169
130
private static NDArray buildFromTensorInt (
170
131
RandomAccessibleInterval <IntType > tensor , NDManager manager )
171
132
{
@@ -192,16 +153,6 @@ private static NDArray buildFromTensorInt(
192
153
return ndarray ;
193
154
}
194
155
195
- /**
196
- * Builds a {@link NDArray} from a signed float-typed
197
- * {@link RandomAccessibleInterval}.
198
- *
199
- * @param tensor
200
- * the {@link RandomAccessibleInterval} that will be copied into an {@link NDArray}
201
- * @param manager
202
- * {@link NDManager} needed to create a {@link NDArray}
203
- * @return The {@link NDArray} built from the tensor of type {@link FloatType}.
204
- */
205
156
private static NDArray buildFromTensorFloat (
206
157
RandomAccessibleInterval <FloatType > tensor , NDManager manager )
207
158
{
@@ -228,16 +179,6 @@ private static NDArray buildFromTensorFloat(
228
179
return ndarray ;
229
180
}
230
181
231
- /**
232
- * Builds a {@link NDArray} from a signed double-typed
233
- * {@link RandomAccessibleInterval}.
234
- *
235
- * @param tensor
236
- * the {@link RandomAccessibleInterval} that will be copied into an {@link NDArray}
237
- * @param manager
238
- * {@link NDManager} needed to create a {@link NDArray}
239
- * @return The {@link NDArray} built from the tensor of type {@link DoubleType}.
240
- */
241
182
private static NDArray buildFromTensorDouble (
242
183
RandomAccessibleInterval <DoubleType > tensor , NDManager manager )
243
184
{
0 commit comments