-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectReader.java
More file actions
205 lines (172 loc) · 4.84 KB
/
ObjectReader.java
File metadata and controls
205 lines (172 loc) · 4.84 KB
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
//for reading the faces of a triangle mesh in 3d space
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.util.ArrayList;
import java.io.IOException;
import java.lang.Math;
class ObjectReader {
private Vertex readVertex(String coordinate){
//returns a vertex with coordinates specified in the input line
int l = coordinate.length();
double[] values = new double[3]; //array of coordinates
int axis=0; //index for each axis
for(int i=0; i<l; i++){
//filling the coordinates specified in the line into array
int temp = (int) coordinate.charAt(i);
if(temp>47&&temp<58){
String number = "";
if(coordinate.charAt(i-1)=='-') number=number+'-';
while(temp>47&&temp<58||temp==46){
number=number+coordinate.charAt(i);
i++;
if(i<l) temp = (int) coordinate.charAt(i);
else temp=0;
}
values[axis]=Double.parseDouble(number);
axis++;
}
}
return new Vertex(values);
}
private Triangle readFace(String line){
//returns a Triangle with index of vertex specified in the input line
int l = line.length();
int[] values = new int[3]; //array of indices
int x=0; //iterator for each vertex
for(int i=0; i<l; i++){
//filling the vertices specified in the line into array
int temp = (int) line.charAt(i);
if(temp>47&&temp<58){
String number = "";
while(temp>47&&temp<58){
number=number+line.charAt(i);
i++;
if(i<l) temp = (int) line.charAt(i);
else temp=0;
}
values[x]=Integer.parseInt(number);
x++;
while(i<l&&line.charAt(i)!=' ') i++;
}
}
return new Triangle(values);
}
private boolean checkObj(File file) {
//checks for .obj file
String fileName = file.getName().toLowerCase();
return fileName.endsWith(".obj");
}
public ArrayList<Triangle> readFaceData(File f) throws Exception{
//returns a list of triangles present in input file(expected to be .obj file)
if(!checkObj(f)) throw new Exception("obj file expected");
FileReader fr = null;
BufferedReader reader = null;
ArrayList<Triangle> faces = null;
try{
fr = new FileReader(f);
reader = new BufferedReader(fr);
faces = new ArrayList<Triangle>();
faces.add(null);//list of faces
int lineBreaks = 0;
while(lineBreaks<10){
String line = reader.readLine();
while(line!=null&& !line.isEmpty()){
lineBreaks=0;
//faces are specified in .obj file with a starting character 'f'
if(line.charAt(0)=='f'&&line.charAt(1)==' '){
faces.add(readFace(line));
}
line=reader.readLine();
}
lineBreaks++;
}
}
catch(IOException e){
e.printStackTrace();
}
finally{
try {
if (reader != null)
reader.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return faces;
}
public ArrayList<Vertex> readVertexData(File f) throws Exception{
//returns a list of vertices present in input file(expected to be .obj file)
if(!checkObj(f)) throw new Exception("obj file expected");
FileReader fr = null;
BufferedReader reader = null;
ArrayList<Vertex> vertices = null;
try{
fr = new FileReader(f);
reader = new BufferedReader(fr);
vertices = new ArrayList<Vertex>();
vertices.add(null);//list of vertices
int lineBreaks=0;
while(lineBreaks<10){
String line = reader.readLine();
while(line!=null&& !line.isEmpty()){
lineBreaks=0;
//vertices are specified in .obj file with a starting character 'v'
if(line.charAt(0)=='v'&&line.charAt(1)==' '){
vertices.add(readVertex(line));
}
line=reader.readLine();
}
lineBreaks++;
}
}
catch(IOException e){
e.printStackTrace();
}
finally{
try {
if (reader != null)
reader.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return vertices;
}
/*public static void main(String[] args) throws Exception{
//driver program
//vertex reader testing
ObjectReader dr = new ObjectReader();
ArrayList<Vertex> vertexList = dr.readVertexData(new File("example.obj"));
int count = 1;
for(Vertex v: vertexList){
if(v!=null){
System.out.print(count+" ");
double[] coordinates=v.getCoordinates();
for(double d: coordinates){
System.out.print(d+" ");
}
System.out.println("");
count++;
}
}
//face reader testing
ArrayList<Triangle> list = dr.readFaceData(new File("example.obj"));
count = 1;
for(Triangle t: list){
if(t!=null){
System.out.print(count+" ");
int[] vertices=t.getVertices();
for(int i: vertices){
System.out.print(i+" ");
}
System.out.println("");
count++;
}
}
}*/
}