summaryrefslogtreecommitdiff
path: root/java/tests/VrDemo/src/com/example/android/rs/vr/engine/Volume.java
blob: 0de55447856d914ab0f8e4e8a292f9699cf57bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.rs.vr.engine;

import android.renderscript.Allocation;
import android.util.Log;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Defines a simple volume to be used in the volume renderer
 */
public class Volume {
    private static final String LOGTAG = "Volume";
    public short[][] mData;
    public Allocation mVolumeAllocation; // one big volume
    public int mDimz = -1;
    public int mDimy = -1;
    public int mDimx = -1;
    public float[] mVoxelDim = new float[]{1f, 1f, 1f};
    private HashMap<String, Look> mLooks = new HashMap<String, Look>();

    @Override
    public String toString() {
        String ret = "Volume[" + mDimx + "," + mDimy + "," + mDimz + "]";
        ret += "(" + mVoxelDim[0] + ", " + mVoxelDim[1] + ", " + mVoxelDim[2] + ")";

        return ret;
    }

    public String[] getLookNames() {
        return mLooks.keySet().toArray(new String[mLooks.size()]);
    }

    public int[][] getLookColor(String name) {
        return mLooks.get(name).mColor;
    }

    public int[][] getLookOpactiy(String name) {
        return mLooks.get(name).mOpacity;
    }

    public void addLook(String name, int[][] color,  int[][] opacity) {
        mLooks.put(name, new Look(name, color, opacity));
    }

    public void addLook(String name, String color_string, String opacity_string) {
        mLooks.put(name, new Look(name, color_string, opacity_string));
        Look l = mLooks.get(name);
        Log.v(LOGTAG, " ========================== " + name + " =============================");
        Log.v(LOGTAG, "mColor "+l.dblArrayToString(l.mColor));
        Log.v(LOGTAG, "mOpacity "+l.dblArrayToString(l.mOpacity));
    }

    class Look {
        int[][] mColor;
        int[][] mOpacity;
        String mName;

        public Look(String name, String color_string, String opacity_string) {
            mName = name;
            String[] colorSplit = color_string.split("\\}\\s*\\,\\s*\\{");
            String[] opacitySplit = opacity_string.split("\\}\\s*\\,\\s*\\{");
            mColor = new int[colorSplit.length][];
            for (int i = 0; i < colorSplit.length; i++) {

                mColor[i] = readNumbers(colorSplit[i]);
            }
            mOpacity = new int[opacitySplit.length][];
            for (int i = 0; i < opacitySplit.length; i++) {

                mOpacity[i] = readNumbers(opacitySplit[i]);
            }
        }

        public Look(String name, int[][] color, int[][] opacity) {
            mColor = color;
            mOpacity = opacity;
            mName =name;
        }

        private int[] readNumbers(String numList) {
            numList = numList.replace('{', ' ');
            numList = numList.replace('}', ' ');
            numList = numList.replace(';', ' ');
            String[] split = numList.split(",");
            int[] ret = new int[split.length];
            for (int i = 0; i < ret.length; i++) {
                ret[i] = Integer.decode(split[i].trim());
            }
            return ret;
        }

        private String dblArrayToString(int[][] v) {
            String s = "";
            for (int i = 0; i < v.length; i++) {
                if (i > 0) {
                    s += ",";
                }
                s += Arrays.toString(v[i]);
            }
            return s;
        }

        public String toString() {
            return "mColor=" + dblArrayToString(mColor) + "\nmOpacity=" + dblArrayToString(mOpacity);
        }
    }
}