diff --git a/pom.xml b/pom.xml index 04fd0958..90cee26f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ sh.ball osci-render - 1.17.2 + 1.17.3 osci-render diff --git a/src/main/java/sh/ball/engine/Camera.java b/src/main/java/sh/ball/engine/Camera.java index c274e68e..201eb2fe 100644 --- a/src/main/java/sh/ball/engine/Camera.java +++ b/src/main/java/sh/ball/engine/Camera.java @@ -21,6 +21,7 @@ public class Camera { private static final double VERTEX_VALUE_THRESHOLD = 1; private static final double CAMERA_MOVE_INCREMENT = -0.1; private static final int SAMPLE_RENDER_SAMPLES = 50; + private static final int VERTEX_SAMPLES = 1000; private double focalLength; private Vector3 pos; @@ -35,17 +36,7 @@ public class Camera { } public List draw(WorldObject worldObject) { - return getFrame(getProjectedVertices(worldObject), worldObject.getVertexPath()); - } - - public Map getProjectedVertices(WorldObject worldObject) { - Map projectionMap = new HashMap<>(); - - for (Vector3 vertex : worldObject.getVertices()) { - projectionMap.put(vertex, project(vertex)); - } - - return projectionMap; + return getFrame(worldObject.getVertexPath()); } // Automatically finds the correct Z position to use to view the world object properly. @@ -70,7 +61,9 @@ public class Camera { List vertices = new ArrayList<>(); for (int i = 0; i < SAMPLE_RENDER_SAMPLES - 1; i++) { - vertices.addAll(getProjectedVertices(clone).values()); + for (int j = 0; j < Math.min(VERTEX_SAMPLES, clone.numVertices()); j++) { + vertices.add(project(clone.getVertex(j))); + } clone.rotate(rotation); } @@ -106,13 +99,13 @@ public class Camera { ); } - public List getFrame(Map projectionMap, List vertexPath) { + public List getFrame(List vertexPath) { List lines = new ArrayList<>(); for (int i = 0; i < vertexPath.size(); i += 2) { lines.add(new Line( - projectionMap.get(vertexPath.get(i)), - projectionMap.get(vertexPath.get(i + 1)) + project(vertexPath.get(i)), + project(vertexPath.get(i + 1)) )); } diff --git a/src/main/java/sh/ball/engine/Vector3.java b/src/main/java/sh/ball/engine/Vector3.java index 01c217cf..8ad0ae4c 100644 --- a/src/main/java/sh/ball/engine/Vector3.java +++ b/src/main/java/sh/ball/engine/Vector3.java @@ -35,53 +35,61 @@ public final class Vector3 { public Vector3 add(Vector3 other) { return new Vector3( - getX() + other.getX(), - getY() + other.getY(), - getZ() + other.getZ() + x + other.x, + y + other.y, + z + other.z ); } public Vector3 scale(double factor) { return new Vector3( - getX() * factor, - getY() * factor, - getZ() * factor + x * factor, + y * factor, + z * factor ); } public Vector3 rotate(Vector3 rotation) { - return rotateX(rotation.getX()) - .rotateY(rotation.getY()) - .rotateZ(rotation.getZ()); + return rotateX(rotation.x) + .rotateY(rotation.y) + .rotateZ(rotation.z); } public Vector3 rotateX(double theta) { + double cos = Math.cos(theta); + double sin = Math.sin(theta); return new Vector3( - getX(), - Math.cos(theta) * getY() - Math.sin(theta) * getZ(), - Math.sin(theta) * getY() + Math.cos(theta) * getZ() + x, + cos * y - sin * z, + sin * y + cos * z ); } public Vector3 rotateY(double theta) { + double cos = Math.cos(theta); + double sin = Math.sin(theta); return new Vector3( - Math.cos(theta) * getX() + Math.sin(theta) * getZ(), - getY(), - -Math.sin(theta) * getX() + Math.cos(theta) * getZ() + cos * x + sin * z, + y, + -sin * x + cos * z ); } public Vector3 rotateZ(double theta) { + double cos = Math.cos(theta); + double sin = Math.sin(theta); return new Vector3( - Math.cos(theta) * getX() - Math.sin(theta) * getY(), - Math.sin(theta) * getX() + Math.cos(theta) * getY(), - getZ() + cos * x - sin * y, + sin * x + cos * y, + z ); } - // TODO: Is this correctly used?! public double distance(Vector3 vector) { - return Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2) + Math.pow(vector.z, 2)); + double xDiff = x - vector.x; + double yDiff = y - vector.y; + double zDiff = z - vector.z; + return Math.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff); } public static Vector3 meanPoint(List points) { diff --git a/src/main/java/sh/ball/engine/WorldObject.java b/src/main/java/sh/ball/engine/WorldObject.java index e341db79..8a5c2c4b 100644 --- a/src/main/java/sh/ball/engine/WorldObject.java +++ b/src/main/java/sh/ball/engine/WorldObject.java @@ -76,6 +76,7 @@ public class WorldObject { // Chinese Postman can only be performed on connected graphs, so iterate over all connected // sub-graphs. + // TODO: parallelize? for (Set vertices : inspector.connectedSets()) { AsSubgraph subgraph = new AsSubgraph<>(graph, vertices); ChinesePostman cp = new ChinesePostman<>(); @@ -108,14 +109,12 @@ public class WorldObject { position = new Vector3(); } - public List getVertices() { - List newVertices = new ArrayList<>(); + public Vector3 getVertex(int i) { + return objVertices.get(i).rotate(rotation).add(position); + } - for (Vector3 vertex : objVertices) { - newVertices.add(vertex.rotate(rotation).add(position)); - } - - return newVertices; + public int numVertices() { + return objVertices.size(); } private Set loadFromInput(InputStream input) {