001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.type.collection.array;
020
021import icy.type.DataType;
022
023/**
024 * @author Stephane
025 */
026public class Array3DUtil
027{
028
029    /**
030     * Return the total number of element of the specified array
031     */
032    public static int getTotalLength(byte[][][] array)
033    {
034        int result = 0;
035
036        if (array != null)
037        {
038            final int len = array.length;
039
040            for (int i = 0; i < len; i++)
041                result += Array2DUtil.getTotalLength(array[i]);
042        }
043
044        return result;
045    }
046
047    /**
048     * Return the total number of element of the specified array
049     */
050    public static int getTotalLength(short[][][] array)
051    {
052        int result = 0;
053
054        if (array != null)
055        {
056            final int len = array.length;
057
058            for (int i = 0; i < len; i++)
059                result += Array2DUtil.getTotalLength(array[i]);
060        }
061
062        return result;
063    }
064
065    /**
066     * Return the total number of element of the specified array
067     */
068    public static int getTotalLength(int[][][] array)
069    {
070        int result = 0;
071
072        if (array != null)
073        {
074            final int len = array.length;
075
076            for (int i = 0; i < len; i++)
077                result += Array2DUtil.getTotalLength(array[i]);
078        }
079
080        return result;
081    }
082
083    /**
084     * Return the total number of element of the specified array
085     */
086    public static int getTotalLength(long[][][] array)
087    {
088        int result = 0;
089
090        if (array != null)
091        {
092            final int len = array.length;
093
094            for (int i = 0; i < len; i++)
095                result += Array2DUtil.getTotalLength(array[i]);
096        }
097
098        return result;
099    }
100
101    /**
102     * Return the total number of element of the specified array
103     */
104    public static int getTotalLength(float[][][] array)
105    {
106        int result = 0;
107
108        if (array != null)
109        {
110            final int len = array.length;
111
112            for (int i = 0; i < len; i++)
113                result += Array2DUtil.getTotalLength(array[i]);
114        }
115
116        return result;
117    }
118
119    /**
120     * Return the total number of element of the specified array
121     */
122    public static int getTotalLength(double[][][] array)
123    {
124        int result = 0;
125
126        if (array != null)
127        {
128            final int len = array.length;
129
130            for (int i = 0; i < len; i++)
131                result += Array2DUtil.getTotalLength(array[i]);
132        }
133
134        return result;
135    }
136
137    /**
138     * Create a new 3D array with specified data type and length
139     */
140    public static Object[][] createArray(DataType dataType, int len)
141    {
142        switch (dataType.getJavaType())
143        {
144            case BYTE:
145                return new byte[len][][];
146            case SHORT:
147                return new short[len][][];
148            case INT:
149                return new int[len][][];
150            case LONG:
151                return new int[len][][];
152            case FLOAT:
153                return new float[len][][];
154            case DOUBLE:
155                return new double[len][][];
156            default:
157                return null;
158        }
159    }
160
161    /**
162     * @deprecated
163     */
164    @Deprecated
165    public static Object[][] createArray(int dataType, int len)
166    {
167        return createArray(DataType.getDataType(dataType), len);
168    }
169
170    /**
171     * Return the multi dimension 'in' array as a single dimension byte array.
172     */
173    public static byte[] toByteArray1D(byte[][][] in)
174    {
175        return toByteArray1D(in, null, 0);
176    }
177
178    /**
179     * Return the multi dimension 'in' array as a single dimension short array.
180     */
181    public static short[] toShortArray1D(short[][][] in)
182    {
183        return toShortArray1D(in, null, 0);
184    }
185
186    /**
187     * Return the multi dimension 'in' array as a single dimension int array.
188     */
189    public static int[] toIntArray1D(int[][][] in)
190    {
191        return toIntArray1D(in, null, 0);
192    }
193
194    /**
195     * Return the multi dimension 'in' array as a single dimension float array.
196     */
197    public static float[] toFloatArray1D(float[][][] in)
198    {
199        return toFloatArray1D(in, null, 0);
200    }
201
202    /**
203     * Return the multi dimension 'in' array as a single dimension double array.
204     */
205    public static double[] toDoubleArray1D(double[][][] in)
206    {
207        return toDoubleArray1D(in, null, 0);
208    }
209
210    /**
211     * Return the 3 dimensions 'in' array as a single dimension array.<br>
212     * The resulting array is returned in 'out' and from the specified if any.<br>
213     * If (out == null) a new array is allocated.
214     */
215    public static byte[] toByteArray1D(byte[][][] in, byte[] out, int offset)
216    {
217        final byte[] result = Array1DUtil.allocIfNull(out, offset + getTotalLength(in));
218
219        if (in != null)
220        {
221            final int len = in.length;
222
223            int off = offset;
224            for (int i = 0; i < len; i++)
225            {
226                final byte[][] s_in = in[i];
227
228                if (s_in != null)
229                {
230                    Array2DUtil.toByteArray1D(s_in, result, off);
231                    off += s_in.length;
232                }
233            }
234        }
235
236        return result;
237    }
238
239    /**
240     * Return the 3 dimensions 'in' array as a single dimension array.<br>
241     * The resulting array is returned in 'out' and from the specified if any.<br>
242     * If (out == null) a new array is allocated.
243     */
244    public static short[] toShortArray1D(short[][][] in, short[] out, int offset)
245    {
246        final short[] result = Array1DUtil.allocIfNull(out, offset + getTotalLength(in));
247
248        if (in != null)
249        {
250            final int len = in.length;
251
252            int off = offset;
253            for (int i = 0; i < len; i++)
254            {
255                final short[][] s_in = in[i];
256
257                if (s_in != null)
258                {
259                    Array2DUtil.toShortArray1D(s_in, result, off);
260                    off += s_in.length;
261                }
262            }
263        }
264
265        return result;
266    }
267
268    /**
269     * Return the 3 dimensions 'in' array as a single dimension array.<br>
270     * The resulting array is returned in 'out' and from the specified if any.<br>
271     * If (out == null) a new array is allocated.
272     */
273    public static int[] toIntArray1D(int[][][] in, int[] out, int offset)
274    {
275        final int[] result = Array1DUtil.allocIfNull(out, offset + getTotalLength(in));
276
277        if (in != null)
278        {
279            final int len = in.length;
280
281            int off = offset;
282            for (int i = 0; i < len; i++)
283            {
284                final int[][] s_in = in[i];
285
286                if (s_in != null)
287                {
288                    Array2DUtil.toIntArray1D(s_in, result, off);
289                    off += s_in.length;
290                }
291            }
292        }
293
294        return result;
295    }
296
297    /**
298     * Return the 3 dimensions 'in' array as a single dimension array.<br>
299     * The resulting array is returned in 'out' and from the specified if any.<br>
300     * If (out == null) a new array is allocated.
301     */
302    public static long[] toLongArray1D(long[][][] in, long[] out, int offset)
303    {
304        final long[] result = Array1DUtil.allocIfNull(out, offset + getTotalLength(in));
305
306        if (in != null)
307        {
308            final int len = in.length;
309
310            int off = offset;
311            for (int i = 0; i < len; i++)
312            {
313                final long[][] s_in = in[i];
314
315                if (s_in != null)
316                {
317                    Array2DUtil.toLongArray1D(s_in, result, off);
318                    off += s_in.length;
319                }
320            }
321        }
322
323        return result;
324    }
325
326    /**
327     * Return the 3 dimensions 'in' array as a single dimension array.<br>
328     * The resulting array is returned in 'out' and from the specified if any.<br>
329     * If (out == null) a new array is allocated.
330     */
331    public static float[] toFloatArray1D(float[][][] in, float[] out, int offset)
332    {
333        final float[] result = Array1DUtil.allocIfNull(out, offset + getTotalLength(in));
334
335        if (in != null)
336        {
337            final int len = in.length;
338
339            int off = offset;
340            for (int i = 0; i < len; i++)
341            {
342                final float[][] s_in = in[i];
343
344                if (s_in != null)
345                {
346                    Array2DUtil.toFloatArray1D(s_in, result, off);
347                    off += s_in.length;
348                }
349            }
350        }
351
352        return result;
353    }
354
355    /**
356     * Return the 3 dimensions 'in' array as a single dimension array.<br>
357     * The resulting array is returned in 'out' and from the specified if any.<br>
358     * If (out == null) a new array is allocated.
359     */
360    public static double[] toDoubleArray1D(double[][][] in, double[] out, int offset)
361    {
362        final double[] result = Array1DUtil.allocIfNull(out, offset + getTotalLength(in));
363
364        if (in != null)
365        {
366            final int len = in.length;
367
368            int off = offset;
369            for (int i = 0; i < len; i++)
370            {
371                final double[][] s_in = in[i];
372
373                if (s_in != null)
374                {
375                    Array2DUtil.toDoubleArray1D(s_in, result, off);
376                    off += s_in.length;
377                }
378            }
379        }
380
381        return result;
382    }
383
384}