1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.rs.vr.engine;
18
19import android.renderscript.Allocation;
20import android.util.Log;
21
22import java.util.Arrays;
23import java.util.HashMap;
24
25/**
26 * Defines a simple volume to be used in the volume renderer
27 */
28public class Volume {
29    private static final String LOGTAG = "Volume";
30    public short[][] mData;
31    public Allocation mVolumeAllocation; // one big volume
32    public int mDimz = -1;
33    public int mDimy = -1;
34    public int mDimx = -1;
35    public float[] mVoxelDim = new float[]{1f, 1f, 1f};
36    private HashMap<String, Look> mLooks = new HashMap<String, Look>();
37
38    @Override
39    public String toString() {
40        String ret = "Volume[" + mDimx + "," + mDimy + "," + mDimz + "]";
41        ret += "(" + mVoxelDim[0] + ", " + mVoxelDim[1] + ", " + mVoxelDim[2] + ")";
42
43        return ret;
44    }
45
46    public String[] getLookNames() {
47        return mLooks.keySet().toArray(new String[mLooks.size()]);
48    }
49
50    public int[][] getLookColor(String name) {
51        return mLooks.get(name).mColor;
52    }
53
54    public int[][] getLookOpactiy(String name) {
55        return mLooks.get(name).mOpacity;
56    }
57
58    public void addLook(String name, int[][] color,  int[][] opacity) {
59        mLooks.put(name, new Look(name, color, opacity));
60    }
61
62    public void addLook(String name, String color_string, String opacity_string) {
63        mLooks.put(name, new Look(name, color_string, opacity_string));
64        Look l = mLooks.get(name);
65        Log.v(LOGTAG, " ========================== " + name + " =============================");
66        Log.v(LOGTAG, "mColor "+l.dblArrayToString(l.mColor));
67        Log.v(LOGTAG, "mOpacity "+l.dblArrayToString(l.mOpacity));
68    }
69
70    class Look {
71        int[][] mColor;
72        int[][] mOpacity;
73        String mName;
74
75        public Look(String name, String color_string, String opacity_string) {
76            mName = name;
77            String[] colorSplit = color_string.split("\\}\\s*\\,\\s*\\{");
78            String[] opacitySplit = opacity_string.split("\\}\\s*\\,\\s*\\{");
79            mColor = new int[colorSplit.length][];
80            for (int i = 0; i < colorSplit.length; i++) {
81
82                mColor[i] = readNumbers(colorSplit[i]);
83            }
84            mOpacity = new int[opacitySplit.length][];
85            for (int i = 0; i < opacitySplit.length; i++) {
86
87                mOpacity[i] = readNumbers(opacitySplit[i]);
88            }
89        }
90
91        public Look(String name, int[][] color, int[][] opacity) {
92            mColor = color;
93            mOpacity = opacity;
94            mName =name;
95        }
96
97        private int[] readNumbers(String numList) {
98            numList = numList.replace('{', ' ');
99            numList = numList.replace('}', ' ');
100            numList = numList.replace(';', ' ');
101            String[] split = numList.split(",");
102            int[] ret = new int[split.length];
103            for (int i = 0; i < ret.length; i++) {
104                ret[i] = Integer.decode(split[i].trim());
105            }
106            return ret;
107        }
108
109        private String dblArrayToString(int[][] v) {
110            String s = "";
111            for (int i = 0; i < v.length; i++) {
112                if (i > 0) {
113                    s += ",";
114                }
115                s += Arrays.toString(v[i]);
116            }
117            return s;
118        }
119
120        public String toString() {
121            return "mColor=" + dblArrayToString(mColor) + "\nmOpacity=" + dblArrayToString(mOpacity);
122        }
123    }
124}
125