-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjFileObject.java
More file actions
207 lines (181 loc) · 5.31 KB
/
ObjFileObject.java
File metadata and controls
207 lines (181 loc) · 5.31 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
206
207
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* Class to define objects read from obj files
*
*/
public class ObjFileObject implements Object3D {
private ArrayList<Vertex> vertices;
private double xTranslation;
private double yTranslation;
private double zTranslation;
private Material material;
/**
* Constructor that takes in a file name and sets the translations to 0
*
* @param fileName
* - the file name of the obj file
*/
public ObjFileObject(String fileName) {
xTranslation = 0;
yTranslation = 0;
zTranslation = 0;
try {
parseObjFile(fileName);
} catch (IOException e) {
System.out.println("Unable to parse OBJ file: " + fileName);
e.printStackTrace();
}
}
/**
* Constructor that takes in a list of vertices and sets the translations to 0
*
* @param vertices
* - a list of vertices that make up the object
*/
public ObjFileObject(ArrayList<Vertex> vertices) {
xTranslation = 0;
yTranslation = 0;
zTranslation = 0;
this.vertices = vertices;
}
/**
* Returns list of vertices
*
* @return - return the list of vertices
*/
public ArrayList<Vertex> getVertices() {
return vertices;
}
/**
* Rotates vertices around vector a and by angle degrees
*
* @param a
* - the vector to rotate around
* @param andle
* - the degree amount to rotate by
*/
public void rotate(Vector a, double angle) {
double x;
double y;
double z;
double s;
double c;
for (Vertex vertex : vertices) {
// Translates vertex back to origin
vertex.translateXYZ(-xTranslation, -yTranslation, -zTranslation);
// Rotates vertex
c = Math.cos(Math.toRadians(angle));
s = Math.sin(Math.toRadians(angle));
x = vertex.getX() * (c + (1 - c) * Math.pow(a.getX(), 2))
+ vertex.getY() * ((1 - c) * a.getX() * a.getY() - s * a.getZ())
+ vertex.getZ() * ((1 - c) * a.getX() * a.getZ() + s * a.getY());
y = vertex.getX() * ((1 - c) * a.getX() * a.getY() + s * a.getZ())
+ vertex.getY() * (c + (1 - c) * Math.pow(a.getY(), 2))
+ vertex.getZ() * ((1 - c) * a.getY() * a.getZ() - s * a.getX());
z = vertex.getX() * ((1 - c) * a.getX() * a.getZ() - s * a.getY())
+ vertex.getY() * ((1 - c) * a.getY() * a.getZ() + s * a.getX())
+ vertex.getZ() * (c + (1 - c) * Math.pow(a.getZ(), 2));
vertex.setPosition(x, y, z);
// translates back to the original position.
vertex.translateXYZ(xTranslation, yTranslation, zTranslation);
}
}
/**
* Translates the object by the amount of the params
*
* @param xAmt
* - the x amount to translate this object by
* @param yAmt
* - the y amount to translate this object by
* @param zAmt
* - the z amount to translate this object by
*/
public void translateXYZ(double xAmt, double yAmt, double zAmt) {
for (Vertex vertex : vertices) {
vertex.translateXYZ(xAmt, yAmt, zAmt);
}
xTranslation += xAmt;
yTranslation += yAmt;
zTranslation += zAmt;
}
/**
* Parses the .obj file given to get the list of vertices for this object.
*
* @param fileName
* The filename of the file to parse
* @throws IOException
*/
private void parseObjFile(String fileName) throws IOException {
vertices = new ArrayList<Vertex>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
String line = br.readLine();
String formatted;
String[] coordinates;
while (line != null) {
if (line.substring(0, 2).equals("v ")) {
formatted = line.substring(2);
coordinates = formatted.split(" ");
vertices.add(new Vertex(Double.parseDouble(coordinates[0]), Double.parseDouble(coordinates[1]),
Double.parseDouble(coordinates[2])));
}
line = br.readLine();
}
} finally {
br.close();
}
}
/**
* Returns the distance from the object to the vertex
*
* @param vertex
* - the vertex to calculate distance from
* @return - the distance between this object and the vertex
*/
@Override
public double distanceFrom(Vertex vertex) {
double distance = Double.MAX_VALUE;
Vector vector;
for (int k = 0; k < vertices.size(); k++) {
vector = new Vector(vertices.get(k).getX() - vertex.getX(), vertices.get(k).getY() - vertex.getY(),
vertices.get(k).getZ() - vertex.getZ());
if (vector.getMagnitude() - 0.1 < distance) {
// vertices are spheres of radius 0.1
distance = vector.getMagnitude() - 0.1;
}
}
return distance;
}
/**
* Returns null since obj files were only used when color was dependent on
* distance.
*
* @param toColor
* - the point on the object to return the color of
* @param position
* - the position of the camera
* @param lightSource
* - the position of the lightsource
* @param objects
* - a list of all the objects in the scene
* @return - Returns null since obj files were only used when color was
* dependent on distance.
*/
public Color getColor(Vertex toColor, Vertex position, Vertex lightSource, ArrayList<Object3D> objects) {
return null;
}
/**
* Sets the material of the object
*
* @param material
* - the material to set
*/
public void setMaterial(Material material) {
this.material = material;
}
}