From 51900645da2e77fffbea2413623941296950661b Mon Sep 17 00:00:00 2001 From: Josh Heinrichs Date: Thu, 28 Apr 2016 12:53:39 -0600 Subject: [PATCH] Clean up some function names and commented out code --- .../java/models/DelaunayTriangulation.java | 226 +++++------------- .../java/settings/DelaunayModeSetting.java | 37 --- .../DelaunayTriangulationUiAdapter.java | 16 -- src/main/java/uiAdapters/UiAdapter.java | 44 ++++ 4 files changed, 103 insertions(+), 220 deletions(-) delete mode 100644 src/main/java/settings/DelaunayModeSetting.java diff --git a/src/main/java/models/DelaunayTriangulation.java b/src/main/java/models/DelaunayTriangulation.java index 5aca3b7..302247e 100644 --- a/src/main/java/models/DelaunayTriangulation.java +++ b/src/main/java/models/DelaunayTriangulation.java @@ -6,61 +6,61 @@ import graph.Vertex; import graphAdapters.DelaunayTriangulationGraphAdapter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; -/** - * Created by joshheinrichs on 15-05-05. - */ public class DelaunayTriangulation extends Model { public static final double BOUNDS = 10000000.d; - ArrayList delaunayVertexes = new ArrayList<>(); - ArrayList delaunayEdges = new ArrayList<>(); + private ArrayList delaunayVertexes = new ArrayList<>(); + private ArrayList delaunayEdges = new ArrayList<>(); - ArrayList voronoiVertexes = new ArrayList<>(); - ArrayList voronoiEdges = new ArrayList<>(); + private ArrayList voronoiVertexes = new ArrayList<>(); + private ArrayList voronoiEdges = new ArrayList<>(); - ArrayList triangles = new ArrayList<>(); + private ArrayList triangles = new ArrayList<>(); - DelaunayTriangulationGraphAdapter graph = new DelaunayTriangulationGraphAdapter(this); + private DelaunayTriangulationGraphAdapter graph = new DelaunayTriangulationGraphAdapter(this); - boolean unique = true; - - Mode mode = Mode.FAST; + private boolean unique = true; + /** {@inheritDoc} */ @Override public Vertex getVertex(int index) { return delaunayVertexes.get(index); } + /** {@inheritDoc} */ @Override public void addVertex(Point location) { delaunayVertexes.add(new Vertex(location)); update(); } + /** {@inheritDoc} */ @Override public void addVertexes(List points) { delaunayVertexes.addAll(points.stream().map(point -> new Vertex(point)).collect(Collectors.toList())); update(); } + /** {@inheritDoc} */ @Override public void moveVertex(int index, Point location) { delaunayVertexes.get(index).setPoint(location); update(); } + /** {@inheritDoc} */ @Override public void removeVertex(int index) { delaunayVertexes.remove(index); update(); } + + /** {@inheritDoc} */ @Override public void moveVertexes(List indexes, double x, double y) { for (Integer index : indexes) { @@ -70,6 +70,7 @@ public void moveVertexes(List indexes, double x, double y) { update(); } + /** {@inheritDoc} */ @Override public void removeVertexes(List indexes) { for (Integer index : indexes) { @@ -79,22 +80,26 @@ public void removeVertexes(List indexes) { update(); } + /** {@inheritDoc} */ @Override public Edge getEdge(int index) { return delaunayEdges.get(index); } + /** {@inheritDoc} */ @Override public void clearVertexes() { delaunayVertexes.clear(); update(); } + /** {@inheritDoc} */ @Override public ArrayList getVertexes() { return delaunayVertexes; } + /** {@inheritDoc} */ @Override public ArrayList getVertexes(double startX, double startY, double endX, double endY) { double minX = Math.min(startX, endX); @@ -112,6 +117,7 @@ public ArrayList getVertexes(double startX, double startY, double endX, return indexList; } + /** {@inheritDoc} */ @Override public List getEdges() { return new ArrayList<>(delaunayEdges); @@ -119,7 +125,6 @@ public List getEdges() { /** * Returns a list containing all the points at which all Delaunay vertexes are located in the Delaunay triangulation. - * @return */ public List getPoints() { return delaunayVertexes.stream().map(vertex -> vertex.getPoint()).collect(Collectors.toList()); @@ -127,7 +132,6 @@ public List getPoints() { /** * Returns a list containing all of the Delaunay vertexes in the Delaunay triangulation . - * @return */ public List getDelaunayVertexes() { return delaunayVertexes; @@ -135,7 +139,6 @@ public List getDelaunayVertexes() { /** * Returns a list containing all of the Delaunay edges in the Delaunay triangulation. - * @return */ public List getDelaunayEdges() { return new ArrayList<>(delaunayEdges); @@ -143,7 +146,6 @@ public List getDelaunayEdges() { /** * Returns a list containing all of the Voronoi vertexes in the Delaunay triangulation. - * @return */ public List getVoronoiVertexes() { return voronoiVertexes; @@ -151,7 +153,6 @@ public List getVoronoiVertexes() { /** * Returns a list containing all of the Voronoi edges in the Delaunay triangulation. - * @return */ public List getVoronoiEdges() { return voronoiEdges; @@ -159,7 +160,6 @@ public List getVoronoiEdges() { /** * Returns a list containing all of the segments in the Delaunay triangulation. - * @return */ public List getSegments() { return delaunayEdges.stream().map(edge -> edge.getSegment()).collect(Collectors.toList()); @@ -167,7 +167,6 @@ public List getSegments() { /** * Returns a list containing all of the circumcircles in the Delaunay triangulation. - * @return */ public List getCircumcircles() { return triangles.stream().map(DelaunayTriangle::getCircumcircle).collect(Collectors.toList()); @@ -179,7 +178,6 @@ public List getGabrielCircles() { /** * Returns a list containing all of the triangles in the Delaunay triangulation. - * @return */ public List getDelaunayTriangles() { return this.triangles.stream().map(DelaunayTriangle::getTriangle).collect(Collectors.toList()); @@ -187,9 +185,8 @@ public List getDelaunayTriangles() { /** * Returns the Delaunay distance between the vertexes at the given indexes. - * @param a - * @param b - * @return + * @param a Index of the source vertex. + * @param b Index of the destination vertex. */ public double getDelaunayDistance(int a, int b) { return graph.getShortestPathDistance(delaunayVertexes.get(a), delaunayVertexes.get(b)); @@ -197,14 +194,12 @@ public double getDelaunayDistance(int a, int b) { /** * Returns the indexes of the edges along the shortest path from the vertex at index a, to the vertex at index b. - * @param a - * @param b - * @return + * @param a Index of the source vertex. + * @param b Index of the destination vertex. */ public ArrayList getDelaunayPath(int a, int b) { List path = graph.getShortestPath(delaunayVertexes.get(a), delaunayVertexes.get(b)); ArrayList indexes = new ArrayList<>(); - for (Edge edge : path) { for (int i = 0; i < delaunayEdges.size(); i++) { if (edge == delaunayEdges.get(i)) { @@ -212,15 +207,13 @@ public ArrayList getDelaunayPath(int a, int b) { } } } - return indexes; } /** * Returns the straightline distance between the vertexes at the given indexes. - * @param a - * @param b - * @return + * @param a Index of the first vertex + * @param b Index of the second vertex */ public double getStraightDistance(int a, int b) { return delaunayVertexes.get(a).getPoint().distance(delaunayVertexes.get(b).getPoint()); @@ -229,7 +222,6 @@ public double getStraightDistance(int a, int b) { /** * Returns true if the Delauany triangulation is unique (i.e. no sets of 4 or more cocircular points), false * otherwise. - * @return */ public boolean isUnique() { return this.unique; @@ -238,16 +230,16 @@ public boolean isUnique() { /** * Computes the Delaunay triangulation and voronoi diagram. */ - void update() { - generate_delaunay_n4(); - generate_voronoi(); + private void update() { + generateDelaunayN4(); + generateVoronoi(); graph.update(); } /** * Computes the delaunay triangulation in O(n^4) time. */ - void generate_delaunay_n4() { + private void generateDelaunayN4() { delaunayEdges.clear(); triangles.clear(); unique = true; @@ -294,82 +286,23 @@ void generate_delaunay_n4() { } } - if (mode == Mode.FAST) { - if (!this.isUnique()) { - //checks for intersecting edges in the case of > 3 cocircular vertexes. - for (int i = 0; i < this.delaunayEdges.size(); i++) { - DelaunayEdge edge1 = this.delaunayEdges.get(i); - for (int j = i + 1; j < this.delaunayEdges.size(); j++) { - DelaunayEdge edge2 = this.delaunayEdges.get(j); - if (edge1.getSegment().intersects(edge2.getSegment())) { - this.delaunayEdges.remove(edge2); - ArrayList triangles = new ArrayList<>(edge2.getTriangles()); - for (DelaunayTriangle triangle : triangles) { - triangle.delete(); - this.triangles.remove(triangle); - } + if (!this.isUnique()) { + //checks for intersecting edges in the case of > 3 cocircular vertexes. + for (int i = 0; i < this.delaunayEdges.size(); i++) { + DelaunayEdge edge1 = this.delaunayEdges.get(i); + for (int j = i + 1; j < this.delaunayEdges.size(); j++) { + DelaunayEdge edge2 = this.delaunayEdges.get(j); + if (edge1.getSegment().intersects(edge2.getSegment())) { + this.delaunayEdges.remove(edge2); + ArrayList triangles = new ArrayList<>(edge2.getTriangles()); + for (DelaunayTriangle triangle : triangles) { + triangle.delete(); + this.triangles.remove(triangle); } } } } } -// } else if (mode == Mode.MAX_DISTANCE_RATIO) { -// //generate all triangle set combinations (O(2^(n^4)) -// ArrayList dtList; //TODO -// -// //discard triangle sets in which edges intersect (O(n^2) each) -// for (DelaunayTriangulation dt : dtList) { -// for (DelaunayEdge edge1 : dt.delaunayEdges) { -// for (DelaunayEdge edge2 : dt.delaunayEdges) { -// if (edge1.getSegment().intersects(edge2.getSegment())) { -// //TODO delete triangulation -// } -// } -// } -// } -// -// //discard triangle sets in which edges are missing (O(n^2) each) -// for (DelaunayTriangulation dt : dtList) { -// for (Vertex vertex1 : dt.delaunayVertexes) { -// for (Vertex vertex2 : dt.delaunayVertexes) { -// Circle circumcircle = new Circle(vertex1.getPoint(), vertex2.getPoint()); -// boolean intersected = false; -// for (Vertex vertex3 : dt.delaunayVertexes) { -// if (circumcircle.contains(vertex3.getPoint(), false)) { -// intersected = true; -// break; -// } -// } -// if (!intersected) { -// if (!vertex1.isConnected(vertex2)) { -// //TODO delete triangulation -// } -// } -// } -// } -// } -// -// //compute max ratio from remaining triangulations -// double maxRatio = 0.d; -// DelaunayTriangulation maxTriangulation = null; -// for (DelaunayTriangulation dt : dtList) { -// DelaunayTriangulationGraphAdapter graph = new DelaunayTriangulationGraphAdapter(dt); -// for (int i = 0; i < dt.delaunayVertexes.size(); i++) { -// for (int j = i+1; j < dt.delaunayVertexes.size(); j++) { -// double delaunayDistance = graph.getShortestPathDistance(dt.getVertex(i), dt.getVertex(j)); -// double ratio = dt.getVertex(i).getPoint().distance(dt.getVertex(j).getPoint()) / delaunayDistance; -// if (ratio > maxRatio) { -// maxRatio = ratio; -// maxTriangulation = dt; -// } -// } -// } -// } -// -// this.delaunayVertexes = maxTriangulation.delaunayVertexes; -// this.delaunayEdges = maxTriangulation.delaunayEdges; -// this.triangles = maxTriangulation.triangles; -// } } } @@ -378,15 +311,15 @@ void generate_delaunay_n4() { * Generates the Voronoi diagram from the Delaunay triangulation, and computes alpha stability values for Delaunay * edges. */ - void generate_voronoi() { + private void generateVoronoi() { voronoiEdges.clear(); voronoiVertexes.clear(); for (DelaunayEdge edge : delaunayEdges) { - //assert(triangles.size() > 0); if(edge.isHull()) { - + // Handle the special case where the voronoi edge extends to infinity + // Cannot compute the intersection of perpendicular lines to find center of triangle that doesn't exist DelaunayTriangle dt = edge.getTriangles().get(0); Triangle triangle = dt.getTriangle(); Point start = triangle.getCircumcircle().center; @@ -459,13 +392,13 @@ void generate_voronoi() { double angle2 = Math.toRadians(Angle.getAngle(a.getPoint(), edge.getSegment().end, b.getPoint())); if (Double.isNaN(angle1) && Double.isNaN(angle2)) { - edge.setAlphaStable(0.0); + edge.setAlphaStability(0.0); } else if (Double.isNaN(angle1)) { - edge.setAlphaStable(angle2); + edge.setAlphaStability(angle2); } else if (Double.isNaN(angle2)) { - edge.setAlphaStable(angle1); + edge.setAlphaStability(angle1); } else { - edge.setAlphaStable(Math.min(angle1, angle2)); + edge.setAlphaStability(Math.min(angle1, angle2)); } voronoiVertexes.add(a); @@ -475,32 +408,16 @@ void generate_voronoi() { } } - /** - * Returns the triangulation mode currently being used. - */ - public Mode getMode() { - return this.mode; - } - - /** - * Sets the triangulation mode to be used. - * @param mode Mode to which the triangulation mode will be set. - */ - public void setMode(Mode mode) { - this.mode = mode; - update(); - } - /** * A specific formation used in the construction of the Delaunay triangulation */ - class DelaunayTriangle { + public class DelaunayTriangle { - final Vertex a, b, c; - final DelaunayEdge ab, bc, ca; + private final Vertex a, b, c; + private final DelaunayEdge ab, bc, ca; /** - * Constructs a new DelaunayTriangle from the given delaunayEdges. Edge order or direction does not matter. + * Constructs a new DelaunayTriangle from the given delaunayEdges. Edge order and direction does not matter. * @param ab * @param bc * @param ca @@ -525,7 +442,6 @@ class DelaunayTriangle { /** * Returns all adjacent {@link DelaunayTriangle LinkedTriangles} - * @return */ ArrayList getAdjacentTriangles() { ArrayList triangles = new ArrayList<>(3); @@ -551,17 +467,15 @@ ArrayList getAdjacentTriangles() { /** * Returns true if the triangle has a hull edge, false otherwise - * @return */ boolean isHull() { return ab.isHull() || bc.isHull() || ca.isHull(); } /** - * Returns true if the given vertex lies within this {@link DelaunayTriangle}. + * Returns true if the given vertex lies within this {@link DelaunayTriangle}. * @param vertex * @param inclusive - * @return */ boolean contains(Vertex vertex, boolean inclusive) { return new Triangle(a.getPoint(), b.getPoint(), c.getPoint()).contains(vertex.getPoint(), inclusive); @@ -569,7 +483,6 @@ boolean contains(Vertex vertex, boolean inclusive) { /** * Returns this {@link DelaunayTriangle}'s equivelant {@link Triangle}. - * @return */ Triangle getTriangle() { return new Triangle(a.getPoint(), b.getPoint(), c.getPoint()); @@ -577,7 +490,6 @@ Triangle getTriangle() { /** * Returns a circumcircle which passes through this triangles vertexes. - * @return */ Circle getCircumcircle() { return getTriangle().getCircumcircle(); @@ -603,8 +515,8 @@ public String toString() { */ public class DelaunayEdge extends Edge { - ArrayList triangles = new ArrayList<>(2); - double alphaStability = Double.NaN; + private ArrayList triangles = new ArrayList<>(2); + private double alphaStability = Double.NaN; DelaunayEdge(Vertex a, Vertex b) { super(a, b); @@ -619,7 +531,6 @@ ArrayList getTriangles() { * @param triangle */ void addTriangle(DelaunayTriangle triangle) { - //assert(triangles.size() < 2); triangles.add(triangle); } @@ -649,31 +560,12 @@ public double getAlphaStability() { /** * Sets the alpha stability of this edge to the given value. - * @param alphaStability + * @param alphaStability Value to witch the edge's alpha stability will be set. * @see Stable Delaunay Graphs */ - public void setAlphaStable(double alphaStability) { + public void setAlphaStability(double alphaStability) { this.alphaStability = alphaStability; } } - - /** - * Triangulation Mode. Effects which edges are added in situations where the Delaunay triangulation is not unqiue. - */ - public enum Mode { - FAST("Fast"), - MAX_DISTANCE_RATIO("Maximize Distance Ratio"); - - private final String text; - - Mode(final String text) { - this.text = text; - } - - @Override - public String toString() { - return text; - } - } } \ No newline at end of file diff --git a/src/main/java/settings/DelaunayModeSetting.java b/src/main/java/settings/DelaunayModeSetting.java deleted file mode 100644 index fa02247..0000000 --- a/src/main/java/settings/DelaunayModeSetting.java +++ /dev/null @@ -1,37 +0,0 @@ -package settings; - -import javafx.geometry.Insets; -import javafx.geometry.Pos; -import javafx.scene.control.RadioButton; -import javafx.scene.control.ToggleGroup; -import javafx.scene.layout.HBox; -import javafx.scene.text.Text; -import models.DelaunayTriangulation; -import uiAdapters.DelaunayTriangulationUiAdapter; - -/** - * Sets the triangulation mode used, which effects the that are added in a non-unique Delaunay triangulation. - */ -public class DelaunayModeSetting extends DelaunayTriangulationSetting { - - public DelaunayModeSetting(DelaunayTriangulationUiAdapter adapter) { - super(adapter); - - HBox hBox = new HBox(5); - final ToggleGroup toggleGroup = new ToggleGroup(); - hBox.setPadding(new Insets(5)); - hBox.setAlignment(Pos.CENTER_LEFT); - hBox.getChildren().add(new Text("Triangulation Mode:")); - - - for (final DelaunayTriangulation.Mode mode : DelaunayTriangulation.Mode.values()) { - RadioButton radioButton = new RadioButton(mode.toString()); - radioButton.setOnAction(event -> dt.setMode(mode)); - radioButton.setSelected(dt.getMode() == mode); - radioButton.setToggleGroup(toggleGroup); - hBox.getChildren().add(radioButton); - } - - root.getChildren().add(hBox); - } -} diff --git a/src/main/java/uiAdapters/DelaunayTriangulationUiAdapter.java b/src/main/java/uiAdapters/DelaunayTriangulationUiAdapter.java index 0bb7ab1..eb928b0 100644 --- a/src/main/java/uiAdapters/DelaunayTriangulationUiAdapter.java +++ b/src/main/java/uiAdapters/DelaunayTriangulationUiAdapter.java @@ -427,22 +427,6 @@ public void draw() { } } - /** - * Returns the triangulation mode currently being used. - */ - public DelaunayTriangulation.Mode getMode() { - return ((DelaunayTriangulation) model).getMode(); - } - - /** - * Sets the triangulation mode of the delaunay triangulation. - * @param mode Mode to which it will be set. - */ - public void setMode(DelaunayTriangulation.Mode mode) { - ((DelaunayTriangulation) model).setMode(mode); - draw(); - } - /** * Returns true if vertex labels are being displayed, false otherwise. */ diff --git a/src/main/java/uiAdapters/UiAdapter.java b/src/main/java/uiAdapters/UiAdapter.java index f6f351b..d15a8c9 100644 --- a/src/main/java/uiAdapters/UiAdapter.java +++ b/src/main/java/uiAdapters/UiAdapter.java @@ -148,6 +148,10 @@ public void handle(MouseEvent t) { } }; + /** + * Constructs a new UiAdapter. + * @param app App in which the UiAdapter is to be displayed. + */ public UiAdapter(App app) { this.app = app; @@ -401,6 +405,9 @@ public void appendToOutput(String string) { this.app.console.appendText(string); } + /** + * Undoes the last action. + */ public void undo() { redo.push(state); state = undo.pop(); @@ -410,6 +417,9 @@ public void undo() { undoMenuItem.setDisable(!canUndo()); } + /** + * Redoes the last undid action. + */ public void redo() { undo.push(state); state = redo.pop(); @@ -419,14 +429,24 @@ public void redo() { undoMenuItem.setDisable(!canUndo()); } + /** + * Returns true if there is a state in the undo stack, allowing for an undo action to occur. + */ public boolean canUndo() { return !undo.isEmpty(); } + /** + * Returns true if there is a state in the redo stack, allowing for a redo action to occur. + * @return + */ public boolean canRedo() { return !redo.isEmpty(); } + /** + * Saves the previous state onto the undo stack, stores this state as the current state, and clears the redo stack. + */ void saveState() { redo.clear(); undo.push(state); @@ -436,6 +456,11 @@ void saveState() { tempState = false; } + /** + * Saves a temporary state. This is needed to record and potentially throw out states that are created from + * dragging. When a user undoes an action, they don't want to have to undo every state that occurred when moving + * the point, and so these points exist to be overwritten by the next state. + */ void saveTempState() { if (tempState) { state = toJson(); @@ -447,6 +472,11 @@ void saveTempState() { undoMenuItem.setDisable(!canUndo()); } + /** + * Loads vertexes into the model from the given file. + * @param file + * @throws FileNotFoundException + */ public void loadVertexes(File file) throws FileNotFoundException { model.clearVertexes(); JsonReader jsonReader = new JsonReader(new FileReader(file.getPath())); @@ -458,6 +488,10 @@ public void loadVertexes(File file) throws FileNotFoundException { saveState(); } + /** + * Saves vertexes in the model to the given file. + * @param file + */ public void saveVertexes(File file) { try { if (!FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("json")) { @@ -474,6 +508,9 @@ public void saveVertexes(File file) { } } + /** + * Converts the model to JSON. + */ String toJson() { List vertexes = model.getVertexes(); List points = vertexes.stream().map(Vertex::getPoint).collect(Collectors.toList()); @@ -481,12 +518,19 @@ String toJson() { return gson.toJson(points); } + /** + * Converts the given JSON into a point set. + * @param json JSON to be read, should contain a point array. + */ ArrayList fromJson(String json) { Gson gson = new Gson(); Point[] points = gson.fromJson(json, Point[].class); return new ArrayList<>(Arrays.asList(points)); } + /** + * + */ public abstract void draw(); public Group getRoot() {