Added a saving and loading system for graph maps

This commit is contained in:
Quinn
2023-05-02 12:10:58 -05:00
parent 38847abead
commit 2f46605837
4 changed files with 212 additions and 98 deletions

View File

@@ -123,91 +123,6 @@ public class PointGraph extends Graph {
super.removeVertex(v);
}
/**
* @return a bundle with all of the graphs vertex and edge information saved into it
*/
public void save() throws IOException {
FileWriter file = new FileWriter("map.txt");
file.write("numVerts," + super.numVertices());
file.write("\nnumEdges," + super.numEdges());
// turn the hash map into something linear
ArrayList<PointVertex> verts = new ArrayList<>();
ArrayList<Edge> edges = new ArrayList<>();
for(Vertex v : super.adjList.keySet()){
verts.add((PointVertex) v);
for(Edge e : super.adjList.get(v)){
edges.add(e);
}
}
// save the vertexes
int countVerts = 0;
for(PointVertex v : verts){
String countVertsString = "\nvert"+String.valueOf(countVerts);
// save the vertex position
file.write(countVertsString+"Pos,"+v.getPos().x+","+v.getPos().y);
countVerts++;
}
// save the edges
int countEdges = 0;
for(Edge e : edges){
int idx = 0;
String countEdgesString = "\nedge" + String.valueOf(countEdges);
for(PointVertex v : verts){
if(e.getStartVertex() == (Vertex)v){
file.write(countEdgesString+"Start,"+ idx);
}
else if(e.getEndVertex() == (Vertex)v){
file.write(countEdgesString+"End," + idx);
}
idx++;
}
countEdges++;
}
file.close();
}
/**
* @ brief add all graph information in ta bundle to the graph
* @param bundle the bundle to add to the graph
*/
/*
public void addBundleToGraph(Bundle bundle){
int numVerts = bundle.getInt("numVerts", 0);
int numEdges = bundle.getInt("numEdges", 0);
ArrayList<PointVertex> verts = new ArrayList<>();
// add all of the vertexes from the bundle
for(int i = 0; i < numVerts; i++){
String countVertsString = "vert"+String.valueOf(i);
float[] pos = bundle.getFloatArray(countVertsString+"Pos");
String label = bundle.getString(countVertsString+"Label");
boolean isSelected = bundle.getBoolean(countVertsString+"isSelected");
PointVertex v = new PointVertex(pos[0], pos[1], label);
addVertex(v);
verts.add(v);
if(isSelected){
setSelectedVertex(v);
}
}
// add all of the edges from the bundle
for(int i = 0; i < numEdges; i++){
String countEdgesString = "edge" + String.valueOf(i);
int startVertIndex = bundle.getInt(countEdgesString+"Start");
int endVertIndex = bundle.getInt(countEdgesString+"End");
PointVertex vStart = verts.get(startVertIndex);
PointVertex vEnd = verts.get(endVertIndex);
addEdge(vStart, vEnd);
}
}
*/
/**
* @return all edges in the graph

View File

@@ -0,0 +1,115 @@
package Graph;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class PointGraphWriter {
public void save(String filename, PointGraph g) throws IOException {
FileWriter file = new FileWriter(filename);
file.write("numVerts," + g.numVertices());
file.write("\nnumEdges," + g.numEdges());
// turn the hash map into something linear
ArrayList<PointVertex> verts = g.getAllVertexes();
ArrayList<LineEdge> edges = g.getAllEdges();
// save the vertexes
int countVerts = 0;
for(PointVertex v : verts){
// save the vertex position
file.write("\nvert,"+v.getPos().x+","+v.getPos().y);
countVerts++;
}
// save the edges
for(Edge e : edges){
int idx = 0;
file.write("\nedge,");
boolean otherIsWritten = false;
for(PointVertex v : verts){
if(e.getStartVertex() == (Vertex)v){
file.write("start,"+ idx);
if(!otherIsWritten){
file.write(",");
otherIsWritten = true;
}
}
else if(e.getEndVertex() == (Vertex)v){
file.write("end," + idx);
if(!otherIsWritten){
file.write(",");
otherIsWritten = true;
}
}
idx++;
}
}
file.close();
}
public PointGraph loadFile(String filename) throws FileNotFoundException, NumberFormatException {
PointGraph g = new PointGraph();
File file = new File(filename);
Scanner reader = new Scanner(file);
ArrayList<PointVertex> vertices = new ArrayList<>();
while(reader.hasNextLine()){
String line = reader.nextLine();
ArrayList<String> args = parseLine(line);
String key = args.get(0);
switch (key) {
case "numVerts" -> System.out.println("Number of Vertexes: " + Integer.parseInt(args.get(1)));
case "numEdges" -> System.out.println("Number of Edges: " + Integer.parseInt(args.get(1)));
case "vert" -> {
float x = Float.parseFloat(args.get(1));
float y = Float.parseFloat(args.get(2));
PointVertex v = new PointVertex(x, y);
g.addVertex(v);
vertices.add(v);
}
case "edge" -> {
int startIdx;
int endIdx;
if (args.get(1).contains("start")) {
startIdx = Integer.parseInt(args.get(2));
endIdx = Integer.parseInt(args.get(4));
}
else{
startIdx = Integer.parseInt(args.get(4));
endIdx = Integer.parseInt(args.get(2));
}
g.addEdge(vertices.get(startIdx), vertices.get(endIdx));
}
default -> System.out.println("Unrecognized Line: " + line);
}
}
return g;
}
private ArrayList<String> parseLine(String line){
ArrayList<String> args = new ArrayList<>();
StringBuilder arg = new StringBuilder();
for(char letter : line.toCharArray()){
if(letter == ','){
args.add(arg.toString());
arg = new StringBuilder();
continue;
}
if(letter == '\n'){
args.add(arg.toString());
break;
}
arg.append(letter);
}
if(!args.get(args.size()-1).contains(arg.toString())){
args.add(arg.toString());
}
return args;
}
}

View File

@@ -2,6 +2,7 @@ import Graph.*;
import Vector.Vector;
import processing.core.PApplet;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -66,12 +67,28 @@ public class Processing extends PApplet {
if(key == ESC){
System.out.println("Attempting to save map to file.");
try{
map.save();
PointGraphWriter writer = new PointGraphWriter();
writer.save("map.txt", map);
}
catch(IOException e){
e.printStackTrace();
}
}
if(key == 'l'){
System.out.println("Attempting to load a map from file");
try{
PointGraphWriter writer = new PointGraphWriter();
map = writer.loadFile("map.txt");
}
catch (FileNotFoundException e){
System.out.println("File not found");
e.printStackTrace();
}
catch (NumberFormatException e){
System.out.println("Number format incorrect");
e.printStackTrace();
}
}
}
public void mousePressed(){