Skip to content

Commit db6365a

Browse files
authored
Merge pull request #12 from Brookoko/polygon
Polygon
2 parents 4f42114 + bba3911 commit db6365a

18 files changed

Lines changed: 319 additions & 337 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package labs.introtoprogramming.lab5.geometry;
2+
3+
import labs.introtoprogramming.lab5.io.obj.Polygon;
4+
import labs.introtoprogramming.lab5.io.obj.Vertex;
5+
import labs.introtoprogramming.lab5.object.Triangle;
6+
import labs.introtoprogramming.lab5.scene.Transform;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class Triangulation {
12+
13+
public List<Triangle> splitPolygons(List<Polygon> polygons) {
14+
List<Triangle> triangles = new ArrayList<>();
15+
for (Polygon pol : polygons) {
16+
List<Vertex> ver = pol.getVertices();
17+
Vector3 pillar = ver.get(0).geometry;
18+
for (int i = 1; i < ver.size() - 1; i++) {
19+
triangles.add(new Triangle(new Transform(),
20+
pillar, ver.get(i).geometry, ver.get(i + 1).geometry));
21+
}
22+
}
23+
System.out.println(triangles.size());
24+
return triangles;
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
package labs.introtoprogramming.lab5.io.obj;
22

3+
import labs.introtoprogramming.lab5.object.Triangle;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.List;
8+
39
public interface ObjReader {
10+
void load(InputStream is) throws IOException;
11+
List<Triangle> getPolygons();
412
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package labs.introtoprogramming.lab5.io.obj;
2+
3+
import labs.introtoprogramming.lab5.geometry.Triangulation;
4+
import labs.introtoprogramming.lab5.geometry.Vector3;
5+
import labs.introtoprogramming.lab5.object.PolygonObject;
6+
import labs.introtoprogramming.lab5.object.Triangle;
7+
import labs.introtoprogramming.lab5.scene.SceneObject;
8+
import labs.introtoprogramming.lab5.scene.Transform;
9+
10+
import java.io.*;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public class ParseObjFile implements ObjReader {
16+
/**
17+
* String indicators of parameter in .obj file.
18+
*/
19+
private final static String OBJ_VERTEX_NORMAL = "vn";
20+
private final static String OBJ_VERTEX = "v";
21+
private final static String OBJ_FACE = "f";
22+
private List<Vector3> verticesNormals = new ArrayList<>();
23+
private List<Vector3> verticesGeometry = new ArrayList<>();
24+
private List<Polygon> polygons = new ArrayList<>();
25+
private Triangulation triangulation = new Triangulation();
26+
27+
/**
28+
* This is part to parse object file
29+
* @param is
30+
* @throws FileNotFoundException
31+
* @throws IOException
32+
*/
33+
public void load(InputStream is) throws IOException {
34+
InputStreamReader fileReader;
35+
BufferedReader bufferedReader;
36+
fileReader = new InputStreamReader(is, StandardCharsets.UTF_8);
37+
bufferedReader = new BufferedReader(fileReader);
38+
String line;
39+
40+
// read .obj file
41+
while ((line = bufferedReader.readLine()) != null) {
42+
line = line.trim();
43+
44+
if (line.isEmpty()) {
45+
continue;
46+
}
47+
48+
// cases to process
49+
if (line.startsWith(OBJ_VERTEX_NORMAL)) {
50+
processVertexNormal(line);
51+
// add vertex with some coordinates
52+
} else if (line.startsWith(OBJ_VERTEX)) {
53+
processVertex(line);
54+
} else if (line.startsWith(OBJ_FACE)) {
55+
processFace(line);
56+
}
57+
}
58+
bufferedReader.close();
59+
}
60+
61+
public List<Triangle> getPolygons() {
62+
return triangulation.splitPolygons(polygons);
63+
}
64+
/**
65+
* Create vertex( just coordinates of each one) from data of file.
66+
* @param line
67+
*/
68+
private void processVertex(String line) {
69+
String[] values = line.replaceAll("\\s+", " ").split(" ");
70+
double[] result = new double[3];
71+
for (int i = 1; i < values.length; i++) {
72+
result[i - 1] = Double.parseDouble(values[i]);
73+
}
74+
verticesGeometry.add(new Vector3(result[0], result[1], result[2]));
75+
}
76+
/**
77+
* Create normal-data of vertex
78+
* @param line
79+
*/
80+
private void processVertexNormal(String line) {
81+
String[] values = line.replaceAll("\\s+", " ").split(" ");
82+
double[] result = new double[3];
83+
for (int i = 1; i < values.length; i++) {
84+
result[i - 1] = Double.parseDouble(values[i]);
85+
}
86+
verticesNormals.add(new Vector3(result[0], result[1], result[2]));
87+
}
88+
89+
90+
private void processFace(String line) {
91+
line = line.substring(OBJ_FACE.length()).trim();
92+
polygons.add(parseCodeLine(line));
93+
}
94+
95+
private Polygon parseCodeLine(String list) {
96+
if (list == null) {
97+
return null;
98+
}
99+
if (list.equals("")) {
100+
return null;
101+
}
102+
String[] vertexStrings = list.split(" ");
103+
List<Vertex> vertices = new ArrayList<>();
104+
105+
for (int i = 0; i < vertexStrings.length; i++) {
106+
String[] temp = vertexStrings[i].split("/");
107+
Vector3 geometry = verticesGeometry.get(Integer.parseInt(temp[0]) - 1);
108+
int index = -1;
109+
if (temp.length >= 3) {
110+
index = Integer.parseInt(temp[2]);
111+
}
112+
Vector3 normal = index == -1 ? Vector3.ZERO : verticesNormals.get(index - 1);
113+
vertices.add(new Vertex(geometry, normal));
114+
}
115+
return new Polygon(vertices);
116+
}
117+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package labs.introtoprogramming.lab5.io.obj;
2+
3+
import java.util.List;
4+
5+
public class Polygon {
6+
private List<Vertex> vertices;
7+
8+
public Polygon(List<Vertex> vertices) {
9+
this.vertices = vertices;
10+
}
11+
12+
public List<Vertex> getVertices() {
13+
return vertices;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package labs.introtoprogramming.lab5.io.obj;
2+
3+
import labs.introtoprogramming.lab5.geometry.Vector3;
4+
5+
public class Vertex {
6+
/**
7+
* Vertex parameters: geometry coordinates and coordinates of normals
8+
*/
9+
public Vector3 geometry;
10+
public Vector3 normal;
11+
12+
public Vertex(Vector3 geometry, Vector3 normal) {
13+
this.geometry = geometry;
14+
this.normal = normal;
15+
}
16+
}

src/main/java/labs/introtoprogramming/lab5/object/OptimizedObject.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class OptimizedObject extends SceneObject {
1212

1313
public OptimizedObject(Transform transform, List<SceneObject> objects) {
1414
super(transform);
15+
for (SceneObject obj : objects) {
16+
obj.getTransform().setParent(transform);
17+
}
1518
tree = new KDTree(objects);
1619
}
1720

src/main/java/labs/introtoprogramming/lab5/object/Parser/ParseObjFile.java

Lines changed: 0 additions & 135 deletions
This file was deleted.

src/main/java/labs/introtoprogramming/lab5/object/Parser/Polygon.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)