summaryrefslogtreecommitdiff
path: root/java/tests/VrDemo/src/com/example/android/rs/vr/engine/Transform.java
blob: 5dc5fc3d1989feae7d1bac405b182819d70ce792 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * 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.util.Log;

import java.text.DecimalFormat;

/**
 * code to manage transformations between world screen and volume space.
 */
public class Transform {
    public final static char WORLD_SPACE = 0;
    public final static char SCREEN_SPACE = 1;
    public final static char VOLUME_SPACE = 2;
    private static final String LOGTAG = "Transform";

    Matrix[][] mAllMat = new Matrix[3][3];
    public ViewMatrix mViewMatrix = new ViewMatrix();
    float[] mVoxelDim = new float[3];

    public void clone(Transform src) {
        System.arraycopy(src.mVoxelDim, 0, mVoxelDim, 0, mVoxelDim.length);
        mViewMatrix.clone(src.mViewMatrix);
        updateAllMatrix();
    }

    public void updateAllMatrix() {
        mViewMatrix.calcMatrix();
        Matrix m = new Matrix();
        m.setToUnit();
        mAllMat[0][0] = m;
        mAllMat[1][1] = m;
        mAllMat[2][2] = m;
        mAllMat[SCREEN_SPACE][WORLD_SPACE] = new Matrix(mViewMatrix);
        mAllMat[WORLD_SPACE][SCREEN_SPACE] = mViewMatrix.invers();
        m = new Matrix();
        m.setToUnit();
        if (mVoxelDim[0] > 0) {
            int min = 0;

            m.m[0] = 1 / mVoxelDim[0];
            m.m[5] = 1 / mVoxelDim[1];
            m.m[10] = 1 / mVoxelDim[2];
        }
        mAllMat[WORLD_SPACE][VOLUME_SPACE] = m;
        mAllMat[VOLUME_SPACE][WORLD_SPACE] = m.invers();
        mAllMat[SCREEN_SPACE][VOLUME_SPACE] = m = m.premult(mViewMatrix);
        mAllMat[VOLUME_SPACE][SCREEN_SPACE] = m.invers();

    }

    public void setVoxelDim(float[] volDim) {
        mVoxelDim[0] = volDim[0];
        mVoxelDim[1] = volDim[1];
        mVoxelDim[2] = volDim[2];
    }

    public Matrix getMatrix(char from, char to) {
        return mAllMat[from][to];
    }

    public void setScreenDim(int x, int y) {
        mViewMatrix.setScreenDim(x, y);
        updateAllMatrix();
    }

    public double[] getLookPoint() {
        return mViewMatrix.getLookPoint();
    }

    public void setLookPoint(double[] mLookPoint) {
        mViewMatrix.setLookPoint(mLookPoint);
        updateAllMatrix();
    }

    public double[] getEyePoint() {
        return mViewMatrix.getEyePoint();
    }

    public void setEyePoint(double[] mEyePoint) {
        mViewMatrix.setEyePoint(mEyePoint);
        updateAllMatrix();
    }

    public double[] getUpVector() {
        return mViewMatrix.getUpVector();
    }

    public void setUpVector(double[] mUpVector) {
        mViewMatrix.setUpVector(mUpVector);
        updateAllMatrix();
    }

    public double getScreenWidth() {
        return mViewMatrix.getScreenWidth();
    }

    public void setScreenWidth(double screenWidth) {
        mViewMatrix.setScreenWidth(screenWidth);
        updateAllMatrix();
    }

    public void lookAt(TriData tri, int w, int h) {
        mViewMatrix.lookAt(tri, mVoxelDim, w, h);
        updateAllMatrix();
    }

    public void look(char dir, TriData tri, int w, int h) {
        mViewMatrix.look(dir, tri, mVoxelDim, w, h);
        updateAllMatrix();
    }

    public void trackBallUp(float x, float y) {
        mViewMatrix.trackBallUP(x, y);
        updateAllMatrix();
    }

    public void trackBallDown(float x, float y) {
        mViewMatrix.trackBallDown(x, y);
    }

    public void trackBallMove(float x, float y) {
        mViewMatrix.trackBallMove(x, y);
        updateAllMatrix();
    }

    static DecimalFormat df = new DecimalFormat("      ##0.000");

    private static String trim(double d) {
        String s = df.format(d);
        return s.substring(s.length() - 6);
    }

    public static void print(float[] d) {
        String s = "";
        for (int i = 0; i < d.length; i++) {
            s += (((i == 0) ? "[ " : " , ") + trim(d[i]));
        }
        Log.v(LOGTAG, s + "]");
    }

    public static void main(String[] args) {
        int[] voldim = {50, 50, 100};
        double[] mEyePoint = {voldim[0] / 2., -voldim[1] / 2., voldim[2] / 2.};
        double[] mLookPoint = {voldim[0] / 2., voldim[1] / 2., voldim[2] / 2.};
        double[] mUpVector = {0., 0., 1.};

        Transform t = new Transform();
        t.mVoxelDim[0] = 1;
        t.setEyePoint(mEyePoint);
        t.setLookPoint(mLookPoint);
        t.setUpVector(mUpVector);
        t.setScreenDim(256, 256);
        t.setScreenWidth(128);
        t.updateAllMatrix();

        Matrix m = t.getMatrix(SCREEN_SPACE, VOLUME_SPACE);
        float[] orig = {.5f, .5f, 0};
        float[] ret = new float[3];

        m.mult3(orig, ret);
        print(ret);
        float[] look = {0, 0, 1};
        m.mult3v(look, ret);
        print(ret);
        float[] up = {1, 0, 0};
        m.mult3v(up, ret);
        print(ret);
        float[] right = {0, 1, 0};
        m.mult3v(right, ret);
        print(ret);

    }

    public void print() {
        Log.v(LOGTAG, "==== =========== VIEW ========== ======");

        mViewMatrix.print();
        Log.v(LOGTAG, "==== SCREEN_SPACE to WORLD_SPACE ======");
        mAllMat[SCREEN_SPACE][WORLD_SPACE].print();
        Log.v(LOGTAG, "==== SCREEN_SPACE to VOLUME_SPACE ======");
        mAllMat[SCREEN_SPACE][VOLUME_SPACE].print();
        Log.v(LOGTAG, "==== WORLD_SPACE to VOLUME_SPACE ======");
        mAllMat[WORLD_SPACE][VOLUME_SPACE].print();
        Log.v(LOGTAG, "==== WORLD_SPACE to SCREEN_SPACE ======");
        mAllMat[WORLD_SPACE][SCREEN_SPACE].print();
        Log.v(LOGTAG, "==== VOLUME_SPACE to SCREEN_SPACE ======");
        mAllMat[VOLUME_SPACE][SCREEN_SPACE].print();
        Log.v(LOGTAG, "==== VOLUME_SPACE to WORLD_SPACE ======");
        mAllMat[VOLUME_SPACE][WORLD_SPACE].print();
        Log.v(LOGTAG, "=======================================");
    }
}