Skip to content

Commit

Permalink
Add stl files cache persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRisenPhoenix committed Mar 13, 2024
1 parent 5d61bc7 commit 7fc1530
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/main/java/algorithm/VisualizationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ public List<File> loadStlModel() {
public void loadLastSTLModels() {
StlMeshImporter importer = new StlMeshImporter();
try {
var jsonString = Files.readString(new File("src/main/resources/json/stlFiles.json").toPath());
JSONObject jsonSTLModels = new JSONObject(jsonString);
JSONObject jsonSTLModels = util.Persistence.readStlSaveFile();
stlModels = new ArrayList<>();
for (int i = 0; i < jsonSTLModels.length(); i++) {
JSONObject jsonSTLModel = jsonSTLModels.getJSONObject("STL " + i);
Expand Down Expand Up @@ -260,7 +259,7 @@ private String getSTLName(File file) {
public void addPathVisualisation() {
FileChooser fc = new FileChooser();
fc.setTitle("Load Path Visualisation");
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("STL Files", "*.mps"));
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("MPS Files", "*.mps"));
File file = fc.showOpenDialog(new Stage());
if (file != null) {
// Read xml files:
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/controller/VisualizationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javafx.scene.shape.DrawMode;
import org.json.JSONObject;
import shapes.STLModel;
import util.Persistence;

import java.io.File;
import java.io.FileWriter;
Expand Down Expand Up @@ -147,8 +148,7 @@ public void addSTLFile() {
visualizationManager.showFigure();
if (fileNames != null) {
try {
var jsonString = Files.readString(new File("src/main/resources/json/stlFiles.json").toPath());
JSONObject jsonSTLModels = new JSONObject(jsonString);
var jsonSTLModels = Persistence.readStlSaveFile();
addSTLToJSON(fileNames, jsonSTLModels);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -180,9 +180,7 @@ private void addSTLToJSON(List<File> fileNames, JSONObject jsonSTLModels) {
stlBranch.getChildren().add(stlFile);
}
try {
FileWriter file = new FileWriter("src/main/resources/json/stlFiles.json");
file.write(jsonSTLModels.toString());
file.close();
Persistence.writeStlSaveFile(jsonSTLModels);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -399,8 +397,7 @@ private void setSTLVisibility() {
*/
private void changeAttributeOfSTL(int index, String attribute, String value) {
try {
var jsonString = Files.readString(new File("src/main/resources/json/stlFiles.json").toPath());
JSONObject jsonSTLModels = new JSONObject(jsonString);
var jsonSTLModels = Persistence.readStlSaveFile();
JSONObject updatedModels = new JSONObject();

for (int i = 0; i < jsonSTLModels.length(); i++) {
Expand All @@ -411,9 +408,7 @@ private void changeAttributeOfSTL(int index, String attribute, String value) {
updatedModels.put("STL " + i, jsonSTLModel);
}
try {
FileWriter file = new FileWriter("src/main/resources/json/stlFiles.json");
file.write(jsonSTLModels.toString());
file.close();
Persistence.writeStlSaveFile(updatedModels);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -430,8 +425,7 @@ private void changeAttributeOfSTL(int index, String attribute, String value) {
*/
private JSONObject getSelectedJSON(int index) {
try {
var jsonString = Files.readString(new File("src/main/resources/json/stlFiles.json").toPath());
JSONObject jsonSTLModels = new JSONObject(jsonString);
var jsonSTLModels = Persistence.readStlSaveFile();
return jsonSTLModels.getJSONObject("STL " + index);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/util/Matrix3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public Matrix3D() {
makeIdentityMatrix();
}

/**
* Static method for clearly creating an identity matrix
* @return identity matrix
*/
public static Matrix3D identity() {
return new Matrix3D();
}

/**
* Instantiates a matrix with specified elements.
*
Expand Down Expand Up @@ -186,7 +194,6 @@ public Vector3D mult(Vector3D vec) {
* @param scalar the number with which all values should be multiplied
*/
public void mult(double scalar) {
Matrix3D store = new Matrix3D();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
this.matrix[i][j] *= scalar;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/util/Persistence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package util;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class Persistence {
private static final String saveFileName = "stlFiles.json";

private static void checkFileExists() throws IOException {
var file = new File(saveFileName);
if (!file.exists()) {
file.createNewFile();
Files.writeString(file.toPath(), "{}");
}
}

public static JSONObject readStlSaveFile() throws IOException {
checkFileExists();
var jsonString = Files.readString(new File(saveFileName).toPath());
return new JSONObject(jsonString);
}

public static void writeStlSaveFile(JSONObject jsonObject) throws IOException {
checkFileExists();
Files.writeString(new File(saveFileName).toPath(), jsonObject.toString());
}
}
8 changes: 8 additions & 0 deletions src/main/java/util/Vector3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public Vector3D(double[] arr) {
}
}

/**
* Returns a zero vector (identity operation)
* @return the zero vector
*/
public static Vector3D zero() {
return new Vector3D(0, 0, 0);
}

/**
* Method to set the vectors components
*
Expand Down

0 comments on commit 7fc1530

Please sign in to comment.