-
Notifications
You must be signed in to change notification settings - Fork 0
Loading obj #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading obj #6
Changes from all commits
8b15296
ce13b51
6e9a32e
ca6d511
b534f22
bdf1406
dd94b1b
fef4e13
de2b0a6
218e69b
f1f2747
1bd4568
d8ec335
4d61c0e
939b627
c3d68f2
a1fc82c
8077705
c36d3b3
e4d8ecd
70e15b6
8f12919
b8a83b2
2925c07
6643c14
25af686
51d8654
fc72c0e
64033c4
0e828a2
fda2ca9
3178b35
66861e8
61859ca
4ffe1d3
f0b1352
1bc691a
e3c2274
a006efa
5e51f6f
992b205
89ad6eb
3740f6f
3df79dd
54b064e
ab29af5
7291b07
b23c40c
d902577
12a7b47
7996e6d
07e0edf
39140c2
e6ecab3
62752d8
cca92ff
900db34
9aff9a2
65d2d43
41ee8cd
8ff6050
ca39cbe
a027cd7
eb6a4a6
c2fbbb6
9771dae
99bbd54
7fce926
02d1146
01ef578
740d222
a00fe4b
a272d22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
|
|
||
| import java.io.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import static java.util.logging.Level.INFO; | ||
| import static java.util.logging.Level.WARNING; | ||
|
|
||
| public class ParseObjFile { | ||
| private Logger log = Logger.getLogger(ParseObjFile.class.getName()); | ||
| /** | ||
| * String indicators of parameter in .obj file. | ||
| */ | ||
| private final static String OBJ_VERTEX_NORMAL = "vn"; | ||
| private final static String OBJ_VERTEX = "v"; | ||
| private final static String OBJ_FACE = "f"; | ||
| private List<Vector3> verticesNormals = new ArrayList<>(); | ||
| private List<Vector3> verticesGeometry = new ArrayList<>(); | ||
| private PolygonObject polygonObject = null; | ||
|
|
||
| public ParseObjFile(InputStream is) throws FileNotFoundException, IOException { | ||
| polygonObject = new PolygonObject(); | ||
| parseObjFile(is); | ||
| } | ||
| /** | ||
| * This is part to parse object file | ||
| * @param is | ||
| * @throws FileNotFoundException | ||
| * @throws IOException | ||
| */ | ||
| private void parseObjFile(InputStream is) throws IOException { | ||
| int lineCount = 0; | ||
| InputStreamReader fileReader; | ||
| BufferedReader bufferedReader; | ||
| fileReader = new InputStreamReader(is, "UTF-8"); | ||
| bufferedReader = new BufferedReader(fileReader); | ||
| String line; | ||
|
|
||
| // read .obj file | ||
| while ((line = bufferedReader.readLine()) != null) { | ||
| line = line.trim(); | ||
|
|
||
| if (line.length() == 0) { | ||
| continue; | ||
| } | ||
|
|
||
| // cases to process | ||
| if (line.startsWith("#")) { | ||
| continue; | ||
| } else if (line.startsWith(OBJ_VERTEX_NORMAL)) { | ||
| processVertexNormal(line); | ||
| // add vertex with some coordinates | ||
| } else if (line.startsWith(OBJ_VERTEX)) { | ||
| processVertex(line); | ||
| } else if (line.startsWith(OBJ_FACE)) { | ||
| processFace(line); | ||
| } else { | ||
| log.log(WARNING, "line " + lineCount + " unknown line |" + line + "|"); | ||
| } | ||
| lineCount++; | ||
| } | ||
| bufferedReader.close(); | ||
|
|
||
| log.log(INFO, "Loaded " + lineCount + " lines"); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this empty line |
||
| public PolygonObject getPolygonObject() { | ||
| return polygonObject; | ||
| } | ||
| /** | ||
| * Create vertex( just coordinates of each one) from data of file. | ||
| * @param line | ||
| */ | ||
| private void processVertex(String line) { | ||
| String[] values = line.split(" "); | ||
| float[] result = new float[3]; | ||
| for (int i = 2; i < values.length; i++) { | ||
| result[i - 2] = Float.parseFloat(values[i]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking into account Vector3 uses Double, maybe change to Double here? |
||
| } | ||
| verticesGeometry.add(new Vector3(result[0], result[1], result[2])); | ||
| } | ||
| /** | ||
| * Create normal-data of vertex | ||
| * @param line | ||
| */ | ||
| private void processVertexNormal(String line) { | ||
| String[] values = line.split(" "); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code block repeats. Maybe create |
||
| float[] result = new float[3]; | ||
| for (int i = 2; i < values.length; i++) { | ||
| result[i - 2] = Float.parseFloat(values[i]); | ||
| } | ||
| verticesNormals.add(new Vector3(result[0], result[1], result[2])); | ||
| } | ||
|
|
||
|
|
||
| private void processFace(String line) { | ||
| line = line.substring(OBJ_FACE.length()).trim(); | ||
| Integer[] vertexIndexAry = StringUtils.parseCodeLine(line); | ||
| addPolygon(vertexIndexAry); | ||
| } | ||
|
|
||
| public void addPolygon(Integer[] vertexIndices) { | ||
| Polygon polygon = new Polygon(); | ||
|
|
||
| int indexOfStructure = 0; | ||
| Vertex fv; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable can be moved inside |
||
|
|
||
| while (indexOfStructure + 2 < vertexIndices.length) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A short explanation would be nice here |
||
|
|
||
| fv = new Vertex(); | ||
| fv.setVertexG(verticesGeometry.get(vertexIndices[indexOfStructure] - 1)); | ||
| if (vertexIndices[indexOfStructure + 2] != null) { | ||
| indexOfStructure++; | ||
| } | ||
| fv.setVertexN(verticesNormals.get(vertexIndices[++indexOfStructure] - 1)); | ||
| polygonObject.addVertex(fv); | ||
| polygon.addVertex(fv); | ||
| indexOfStructure += 2; | ||
| } | ||
| polygon.calculatePolygonNormalTriangle(); | ||
| polygonObject.addPolygon(polygon); | ||
| } | ||
|
|
||
| public List<Vector3> getVerticesNormals() { | ||
| return verticesNormals; | ||
| } | ||
|
|
||
| public List<Vector3> getVerticesGeometry() { | ||
| return verticesGeometry; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Polygon { | ||
| private List<Vertex> vertices = new ArrayList<>(); | ||
| private Vector3 polygonNormal; | ||
| public List<Vertex> getVertices() { | ||
| return vertices; | ||
| } | ||
|
|
||
| public void setVertecies(ArrayList<Vertex> vertices) { | ||
| this.vertices = vertices; | ||
| } | ||
|
|
||
| public void addVertex(Vertex vertex) {vertices.add(vertex);} | ||
|
|
||
| public void setVertices(ArrayList<Vertex> vertices) { | ||
| this.vertices = vertices; | ||
| } | ||
|
|
||
| public Vector3 getPolygonNormal() { | ||
| return polygonNormal; | ||
| } | ||
|
|
||
| public void calculatePolygonNormalTriangle() { | ||
| if (vertices.size() == 3) { | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line |
||
| Vector3 fromOneToTwo = vertices.get(0).getVertexG().subtract(vertices.get(1).getVertexG()); | ||
| Vector3 fromOneToThree = vertices.get(0).getVertexG().subtract(vertices.get(2).getVertexG()); | ||
| polygonNormal = fromOneToTwo.crossProduct(fromOneToThree); | ||
| } | ||
| } | ||
|
|
||
| public void setPolygonNormal(Vector3 polygonNormal) { | ||
| this.polygonNormal = polygonNormal; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import labs.introtoprogramming.lab5.scene.SceneObject; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PolygonObject extends SceneObject { | ||
| private List<Vertex> vertices = new ArrayList<>(); | ||
| private List<Polygon> polygons = new ArrayList<>(); | ||
| public List<Vertex> getVertices() { | ||
| return vertices; | ||
| } | ||
| public List<Polygon> getPolygons() { | ||
| return polygons; | ||
| } | ||
|
|
||
| public void addVertex(Vertex vertex) { | ||
| vertices.add(vertex); | ||
| } | ||
|
|
||
| public void addPolygon(Polygon polygon) { | ||
| polygons.add(polygon); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import java.util.logging.Logger; | ||
|
|
||
| public class StringUtils { | ||
| private static Logger log = Logger.getLogger(StringUtils.class.getName()); | ||
|
|
||
| public static Integer[] parseCodeLine(String list) { | ||
| if (list == null) { | ||
| return null; | ||
| } | ||
| if (list.equals("")) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isEmpty() |
||
| return null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use Optional? Returning null does not seem like a good idea to me. |
||
| } | ||
| String[] temp; | ||
| String[] vertexStrings = list.split(" "); | ||
| temp = vertexStrings[0].split("/"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String[] temp = ... |
||
| boolean mark = true; | ||
| if (!temp[1].equals("")) { | ||
Ekatereana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mark = false; | ||
| } | ||
| Integer[] parsed = new Integer[vertexStrings.length * 3]; | ||
|
|
||
| int j = 0; | ||
| for (int i = 0; i < vertexStrings.length; i++) { | ||
| if (mark) { | ||
| temp = vertexStrings[i].split("//"); | ||
| parsed[j] = Integer.parseInt(temp[0]); | ||
| parsed[++j] = Integer.parseInt(temp[1]); | ||
| } else { | ||
| temp = vertexStrings[i].split("/"); | ||
| parsed[j] = Integer.parseInt(temp[0]); | ||
| parsed[++j] = Integer.parseInt(temp[2]); | ||
| } | ||
| parsed[++j] = null; | ||
| j++; | ||
| } | ||
| return parsed; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
|
|
||
| public class Vertex { | ||
| /** | ||
| * Vertex parameters: geometry coordinates and coordinates of normals | ||
| */ | ||
| private Vector3 vertexG = null; | ||
| private Vector3 vertexN = null; | ||
|
|
||
| public void setVertexG(Vector3 vertexG) { | ||
Ekatereana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.vertexG = vertexG; | ||
| } | ||
|
|
||
| public void setVertexN(Vector3 vertexN) { | ||
| this.vertexN = vertexN; | ||
| } | ||
|
|
||
| public Vector3 getVertexG() { | ||
| return vertexG; | ||
| } | ||
|
|
||
| public Vector3 getVertexN() { | ||
| return vertexN; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return vertexG + "|" + vertexN; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class ParseObjFileTest { | ||
| private static final double DELTA = 1e-10; | ||
|
|
||
| @Test | ||
| public void ParseTest() throws IOException { | ||
Ekatereana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| //4//4 5//5 18//18 17//17 | ||
| ParseObjFile obj = new ParseObjFile( | ||
| ParseObjFileTest.class.getResourceAsStream("/LibertStatue.obj")); | ||
| Vector3 geometry = obj.getVerticesGeometry().get(4); | ||
| Polygon polygon = obj.getPolygonObject().getPolygons().get(4); | ||
| Vector3 findGeometry = polygon.getVertices().get(0).getVertexG(); | ||
| assertEquals(geometry.x, findGeometry.x, DELTA); | ||
| obj.getPolygonObject().toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class StringUtilsTest { | ||
| private static final double DELTA = 1e-10; | ||
|
|
||
| @Test | ||
| public void testStringUtil() { | ||
| String firstLine = "1//2 1//3 1//5"; | ||
| String secondLine = "1/4/2 1/7/3 1/2/5"; | ||
| Integer[] firstArr = StringUtils.parseCodeLine(firstLine); | ||
| Integer[] secondArr = StringUtils.parseCodeLine(secondLine); | ||
| assertEquals(1, secondArr[0], DELTA); | ||
| assertEquals(1, secondArr[0], DELTA); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package labs.introtoprogramming.lab5.object.Parser; | ||
|
|
||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class VertexTest { | ||
| private static final double DELTA = 1e-10; | ||
|
|
||
| @Test | ||
| public void testVertex() { | ||
| Vertex vertex = new Vertex(); | ||
| vertex.setVertexN(new Vector3(0, 0, 0)); | ||
| vertex.setVertexG(new Vector3(1, 1, 1)); | ||
| assertEquals(1, vertex.getVertexG().x, DELTA); | ||
| assertEquals(0, vertex.getVertexN().x, DELTA); | ||
| vertex.toString(); | ||
Ekatereana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.