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 
17 package com.example.android.rs.vr.engine;
18 
19 import android.util.Log;
20 
21 import java.io.FileReader;
22 import java.io.LineNumberReader;
23 import java.text.DecimalFormat;
24 import java.util.Arrays;
25 
26 /**
27  * Simple representation triangulated surface
28  */
29 public class TriData {
30     private static final String LOGTAG = "TriData";
31     protected float[] mVert;
32     protected int[] mIndex;
33 
TriData()34     public TriData() {
35      }
36 
print()37     public void print() {
38         class Fmt extends DecimalFormat {
39             public Fmt() {
40                 super("      ##0.000");
41             }
42 
43             public String f(double number) {
44                 String ret = "           "+super.format(number);
45                 return ret.substring(ret.length() - 7);
46             }
47         }
48         Fmt df = new Fmt();
49         for (int i = 0; i < mVert.length; i += 3) {
50 
51             String s = (i / 3 + "[ " + df.f(mVert[i]));
52             s += (", " + df.f(mVert[i + 1]));
53             Log.v(LOGTAG, s + ", " + df.f(mVert[i + 2]) + "]");
54         }
55     }
56 
TriData(TriData clone)57     public TriData(TriData clone) {
58 
59         mVert = Arrays.copyOf(clone.mVert, clone.mVert.length);
60         mIndex = Arrays.copyOf(clone.mIndex, clone.mIndex.length);
61     }
62 
scale(float[] s)63     public void scale(float[] s) {
64         for (int i = 0; i < mVert.length; i += 3) {
65             mVert[i] *= s[0];
66             mVert[i + 1] *= s[1];
67             mVert[i + 2] *= s[2];
68         }
69     }
70 
scale(double[] s)71     public void scale(double[] s) {
72         for (int i = 0; i < mVert.length; i += 3) {
73             mVert[i] *= s[0];
74             mVert[i + 1] *= s[1];
75             mVert[i + 2] *= s[2];
76         }
77     }
78 
transform(Matrix m)79     public void transform(Matrix m) {
80         for (int i = 0; i < mVert.length; i += 3) {
81             m.mult3(mVert, i, mVert, i);
82         }
83     }
84 
transform(Matrix m, TriData out)85     public void transform(Matrix m, TriData out) {
86 
87         for (int i = 0; i < mVert.length; i += 3) {
88             m.mult3(mVert, i, out.mVert, i);
89         }
90     }
91 
92     /**
93      * Read some simple triangle format used in testing
94      * @param fileName
95      */
read(String fileName)96     public void read(String fileName) {
97         try {
98             FileReader fr = new FileReader(fileName);
99             LineNumberReader lnr = new LineNumberReader(fr);
100             int num_verts = Integer.parseInt(lnr.readLine());
101             Log.v(LOGTAG, "verts =" + num_verts);
102             mVert = new float[num_verts * 3];
103             int k = 0;
104             for (int i = 0; i < num_verts; i++) {
105                 String[] s = lnr.readLine().split("\\s");
106 
107                 for (int j = 0; j < s.length; j++) {
108                     mVert[k++] = Float.parseFloat(s[j]);
109                 }
110             }
111             int num_tri = Integer.parseInt(lnr.readLine());
112             Log.v(LOGTAG, "tri =" + num_tri);
113             mIndex = new int[3 * num_tri];
114             k = 0;
115             for (int i = 0; i < num_tri; i++) {
116                 String[] s = lnr.readLine().split("\\s");
117                 for (int j = 0; j < s.length; j++) {
118                     mIndex[k++] = Integer.parseInt(s[j]);
119                 }
120             }
121         } catch (Exception e) {
122             e.printStackTrace();
123         }
124     }
125 }
126