Loading src/main/java/Graph/readFile.java +99 −15 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ import java.io.*; import java.util.*; import java.util.Random; /** * readFile class utilized to parse through commands to obtain the information of the figure */ public class readFile { public static String[] path = new String[3]; final private static double[] speed = new double[3]; Loading @@ -17,10 +19,20 @@ public class readFile { private static ArrayList<Integer> faces = new ArrayList<>(); public static AdjacencyMatrix adjMatrix; public static void parseCommand(String[] args) { // parses command of up to 3 shapes, each with own path, init speed, and init dir // will just match these attributes up in the order that they appear /** * Parses an array of command-line arguments to extract image paths, speeds, and directional vectors. * This method expects arguments in specific formats: * * <ul> * <li>{@code --image} followed by a string representing the path to an image.</li> * <li>{@code --speed} followed by a double value representing the speed.</li> * <li>{@code --dir} followed by three comma-separated double values representing a directional vector.</li> * </ul> * * @param args args the command-line arguments to be parsed. */ public static void parseCommand(String[] args) { for (String arg : args) { if (Objects.equals(arg, "--image")) { Loading Loading @@ -49,6 +61,15 @@ public class readFile { } } /** * Reads vertex data from a specified file and populates global hash structures * to map each vertex to its adjacent vertices and indices. The file should contain * descriptions of triangles, with each line representing a triangle. Vertices are * expected to be in the format "[x,y,z]", separated by spaces. * * @param currPath the path to the file containing the triangles' vertex data. * @throws FileNotFoundException if the file at {@code currPath} does not exist or cannot be read. */ public static void makeVertexHash(String currPath) { try { Loading Loading @@ -90,12 +111,21 @@ public class readFile { myReader.close(); } catch (FileNotFoundException exception) { catch (FileNotFoundException exception) { } } /** * Constructs an adjacency matrix for a graph using pre-existing vertex and adjacency information. * This method initializes a new {@link AdjacencyMatrix} instance and populates it with edges based * on the adjacency information stored in a global structure. * <p> * It iterates over all vertices, represented by their indices, and for each vertex, it retrieves its * corresponding {@link VertexKey}. It then iterates over all vertices adjacent to the current vertex, * adding an edge to the adjacency matrix for each adjacency. This process constructs a complete * representation of the graph's connectivity in matrix form. */ public static void makeMatrix() { adjMatrix = new AdjacencyMatrix(vertexCount); for (int i = 0; i < vertexCount; i++) { Loading @@ -105,6 +135,20 @@ public class readFile { } } } /** * Retrieves all vertices from the graph, scaled by a factor of 3, and returns them as a flat array. * <p> * This method constructs an array of floating-point numbers, where each set of three consecutive * entries represents the x, y, and z coordinates of a vertex, respectively, scaled by a factor of 3. * The vertices are retrieved in the order determined by their indices in the {@code indexToVertex} mapping. * <p> * Note: The method assumes that each {@link VertexKey} object's {@code get()} method returns an * array of three floats representing the x, y, and z coordinates of the vertex. * * @return A flat array of floats representing the scaled coordinates of all vertices in the graph. * Each vertex's coordinates occupy three consecutive positions in the array (x, y, z), * scaled by a factor of 3. */ public static float[] getVertices() { float[] vertices = new float[vertexCount * 3]; VertexKey tmp; Loading @@ -118,7 +162,22 @@ public class readFile { } return vertices; } /** * Generates an array of face indices with corresponding texture indices for a graphical object. * <p> * This method constructs an array where each pair of elements corresponds to a face index * followed by a texture index. Given that the texture index is not specified or used (set to 0), * this method effectively doubles the size of the input list, with every second element in the * output array being 0. * <p> * The method iterates over a global {@code faces} collection, where each element represents a * single face index. For each face index from this collection, two entries are added to the result * array: the face index itself and a 0 indicating the default or placeholder texture index. * * @return An array of integers where for every face index from the global faces collection, * two consecutive entries are created in the output array: the face index and a 0 as a * placeholder for the texture index. */ public static int[] getFaces() { int[] facesWithTexture = new int[faces.size()*2]; Loading @@ -131,6 +190,20 @@ public class readFile { return facesWithTexture; } /** * Generates a file with a specified filename, populated with 15,000 randomly generated lines of data. * Each line is created by the {@code makeLine} method, formatting random coordinate triples to simulate * vertices or points in a 3D space. * <p> * This method creates or overwrites a file named according to the {@code filename} parameter. It uses * a {@link BufferedWriter} to efficiently write lines to the file. Each line represents a collection of * three coordinate points, each point consisting of x, y, and z coordinates generated randomly within a * range of 0 to 30 (exclusive). * * @param filename The name of the file to be generated or overwritten. If the file already exists, * its contents will be replaced with the new, randomly generated data. * @throws IOException If an I/O error occurs during writing to the file. */ public static void randFileGenerator(String filename) throws IOException { Random random = new Random(); try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) { Loading @@ -142,6 +215,17 @@ public class readFile { } } /** * Generates a string representing three 3D points, each with x, y, and z coordinates. * The coordinates are randomly generated within the range [0, 30) and formatted into a string. * <p> * This method is utilized by {@code randFileGenerator} to create each line of the output file, simulating * vertices of triangles in a 3D space. The randomness introduces variability in the data. * * @param random A {@link Random} instance used to generate the random floats for the coordinates. * @return A string formatted as "[x1,y1,z1] [x2,y2,z2] [x3,y3,z3]", where x, y, and z are coordinate * values for three points, generated randomly. */ private static String makeLine(Random random) { return String.format("[%f,%f,%f] [%f,%f,%f] [%f,%f,%f]", random.nextFloat() * 30, random.nextFloat() * 30, random.nextFloat() * 30, Loading Loading
src/main/java/Graph/readFile.java +99 −15 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ import java.io.*; import java.util.*; import java.util.Random; /** * readFile class utilized to parse through commands to obtain the information of the figure */ public class readFile { public static String[] path = new String[3]; final private static double[] speed = new double[3]; Loading @@ -17,10 +19,20 @@ public class readFile { private static ArrayList<Integer> faces = new ArrayList<>(); public static AdjacencyMatrix adjMatrix; public static void parseCommand(String[] args) { // parses command of up to 3 shapes, each with own path, init speed, and init dir // will just match these attributes up in the order that they appear /** * Parses an array of command-line arguments to extract image paths, speeds, and directional vectors. * This method expects arguments in specific formats: * * <ul> * <li>{@code --image} followed by a string representing the path to an image.</li> * <li>{@code --speed} followed by a double value representing the speed.</li> * <li>{@code --dir} followed by three comma-separated double values representing a directional vector.</li> * </ul> * * @param args args the command-line arguments to be parsed. */ public static void parseCommand(String[] args) { for (String arg : args) { if (Objects.equals(arg, "--image")) { Loading Loading @@ -49,6 +61,15 @@ public class readFile { } } /** * Reads vertex data from a specified file and populates global hash structures * to map each vertex to its adjacent vertices and indices. The file should contain * descriptions of triangles, with each line representing a triangle. Vertices are * expected to be in the format "[x,y,z]", separated by spaces. * * @param currPath the path to the file containing the triangles' vertex data. * @throws FileNotFoundException if the file at {@code currPath} does not exist or cannot be read. */ public static void makeVertexHash(String currPath) { try { Loading Loading @@ -90,12 +111,21 @@ public class readFile { myReader.close(); } catch (FileNotFoundException exception) { catch (FileNotFoundException exception) { } } /** * Constructs an adjacency matrix for a graph using pre-existing vertex and adjacency information. * This method initializes a new {@link AdjacencyMatrix} instance and populates it with edges based * on the adjacency information stored in a global structure. * <p> * It iterates over all vertices, represented by their indices, and for each vertex, it retrieves its * corresponding {@link VertexKey}. It then iterates over all vertices adjacent to the current vertex, * adding an edge to the adjacency matrix for each adjacency. This process constructs a complete * representation of the graph's connectivity in matrix form. */ public static void makeMatrix() { adjMatrix = new AdjacencyMatrix(vertexCount); for (int i = 0; i < vertexCount; i++) { Loading @@ -105,6 +135,20 @@ public class readFile { } } } /** * Retrieves all vertices from the graph, scaled by a factor of 3, and returns them as a flat array. * <p> * This method constructs an array of floating-point numbers, where each set of three consecutive * entries represents the x, y, and z coordinates of a vertex, respectively, scaled by a factor of 3. * The vertices are retrieved in the order determined by their indices in the {@code indexToVertex} mapping. * <p> * Note: The method assumes that each {@link VertexKey} object's {@code get()} method returns an * array of three floats representing the x, y, and z coordinates of the vertex. * * @return A flat array of floats representing the scaled coordinates of all vertices in the graph. * Each vertex's coordinates occupy three consecutive positions in the array (x, y, z), * scaled by a factor of 3. */ public static float[] getVertices() { float[] vertices = new float[vertexCount * 3]; VertexKey tmp; Loading @@ -118,7 +162,22 @@ public class readFile { } return vertices; } /** * Generates an array of face indices with corresponding texture indices for a graphical object. * <p> * This method constructs an array where each pair of elements corresponds to a face index * followed by a texture index. Given that the texture index is not specified or used (set to 0), * this method effectively doubles the size of the input list, with every second element in the * output array being 0. * <p> * The method iterates over a global {@code faces} collection, where each element represents a * single face index. For each face index from this collection, two entries are added to the result * array: the face index itself and a 0 indicating the default or placeholder texture index. * * @return An array of integers where for every face index from the global faces collection, * two consecutive entries are created in the output array: the face index and a 0 as a * placeholder for the texture index. */ public static int[] getFaces() { int[] facesWithTexture = new int[faces.size()*2]; Loading @@ -131,6 +190,20 @@ public class readFile { return facesWithTexture; } /** * Generates a file with a specified filename, populated with 15,000 randomly generated lines of data. * Each line is created by the {@code makeLine} method, formatting random coordinate triples to simulate * vertices or points in a 3D space. * <p> * This method creates or overwrites a file named according to the {@code filename} parameter. It uses * a {@link BufferedWriter} to efficiently write lines to the file. Each line represents a collection of * three coordinate points, each point consisting of x, y, and z coordinates generated randomly within a * range of 0 to 30 (exclusive). * * @param filename The name of the file to be generated or overwritten. If the file already exists, * its contents will be replaced with the new, randomly generated data. * @throws IOException If an I/O error occurs during writing to the file. */ public static void randFileGenerator(String filename) throws IOException { Random random = new Random(); try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) { Loading @@ -142,6 +215,17 @@ public class readFile { } } /** * Generates a string representing three 3D points, each with x, y, and z coordinates. * The coordinates are randomly generated within the range [0, 30) and formatted into a string. * <p> * This method is utilized by {@code randFileGenerator} to create each line of the output file, simulating * vertices of triangles in a 3D space. The randomness introduces variability in the data. * * @param random A {@link Random} instance used to generate the random floats for the coordinates. * @return A string formatted as "[x1,y1,z1] [x2,y2,z2] [x3,y3,z3]", where x, y, and z are coordinate * values for three points, generated randomly. */ private static String makeLine(Random random) { return String.format("[%f,%f,%f] [%f,%f,%f] [%f,%f,%f]", random.nextFloat() * 30, random.nextFloat() * 30, random.nextFloat() * 30, Loading