/// <summary> /// The main thread method that will pop from the stack, then split the file /// based on the header defined in the passed in stack /// </summary> /// <param name="filename"></param> /// <param name="stack"></param> /// <param name="numOfFiles"></param> public void split(string filename, ConcurrentStack <String> stack, int numOfFiles) { while (!stack.IsEmpty) { //If we fail popping, we may be attempting to access //at the same time as someone else. This means the stack //might have emptied since we last checked so we check again if (!stack.TryPop(out string header)) { continue; } //If we got this far, then we have a header FieldSplitter splitter = new FieldSplitter(); DebugLog.logConsole("Splitting " + filename + " file based on " + header); splitter.splitFile(filename, header, numOfFiles); //remove our reference to splitter for the next iteration splitter = null; double perc = ((double)(numOfFiles - stack.Count) / (double)numOfFiles) * 100; PercentageClass.UpdatePercentage("Splitting " + header, perc); DebugLog.logConsole("File splitting: " + perc.ToString("#.##") + "% total"); } //Since the stack is empty, we are done }
/// <summary> /// Takes a file path. If the file already exists in the cache, /// the file is deleted from the cache. The cache directory /// is then filled with the corresponding files /// </summary> /// <param name="filein"></param> public static void parseDataIntoCache(string filein) { long fileSize = new FileInfo(filein).Length; StreamReader fileIN = new StreamReader(filein); string line = ""; line = fileIN.ReadLine(); //These are the string headers string[] parsedPoints = line.Replace(" ", "").Split(','); FileStructure.removeFileFromCache(Path.GetFileNameWithoutExtension(filein)); //Creates cache directories for file FileStructure.addFileToDirectory(filein); //Writers for their own subjective FileStream[] streams = new FileStream[parsedPoints.Length - 1]; BinaryWriter[] writers = new BinaryWriter[parsedPoints.Length - 1]; //We're doing something a bit weird here, but it saves time in the //post-parsing stage if we do it now. //we're going to save the min and max of each header into its own file. //The min max array goes [header1Min, header1Max, header2min, ...] float[] minMaxArray = new float[2 * (parsedPoints.Length - 1)]; //here we set the min max to be -+inf for (int i = 0; i < minMaxArray.Length; i++) { if (i % 2 == 0) { minMaxArray[i] = float.MaxValue; } else { minMaxArray[i] = float.MinValue; } } //creating a string array so that we know where to write our min max arrays to at each end string[] minMaxDestinations = new string[parsedPoints.Length - 1]; //for each header, we create a directory within it, then //a file named full.NBF in each which we will write to over //the course of the parsing //We don't care about the cell number as that is held natively for (int i = 1; i < parsedPoints.Length - 2; i++) { string headerPath = FileStructure.createHeaderDirectory(parsedPoints[i]); streams[i - 1] = File.Open(headerPath + "full.bnf", FileMode.Create); writers[i - 1] = new BinaryWriter(streams[i - 1]); //here we do the add the headerpath to our minMacDestinations minMaxDestinations[i - 1] = headerPath; } long bytes = 0; int count = 0; DebugLog.logConsole("Reading " + filein + "..."); //As long as the string is not null or empty while (!String.IsNullOrEmpty(line = fileIN.ReadLine())) { bytes += Encoding.ASCII.GetByteCount(line); parsedPoints = line.Replace(" ", "").Split(','); //For each point, save it to it's respective file //again we don't care about cell number for (int i = 1; i < parsedPoints.Length - 2; i++) { float point = float.Parse(parsedPoints[i]); writers[i - 1].Write(point); //checking our min max of each point if (point < minMaxArray[2 * (i - 1)]) { minMaxArray[2 * (i - 1)] = point; } if (point > minMaxArray[(2 * (i - 1)) + 1]) { minMaxArray[(2 * (i - 1)) + 1] = point; } } //For measuring progress if (count == 500000) { count = 0; double perc = ((double)bytes / (double)fileSize) * 100; PercentageClass.UpdatePercentage("Parsing", perc); DebugLog.logConsole("Parsing File :" + perc.ToString("#.##") + "%"); } count++; } //Closing the outputs foreach (BinaryWriter w in writers) { if (w != null) { w.Close(); } } foreach (FileStream s in streams) { if (s != null) { s.Close(); } } //We are done with the input fileIN.Close(); //Now we write the min max of each header to file for (int i = 0; i < minMaxDestinations.Length; i++) { if (minMaxDestinations[i] == null) { continue; } Stream stream = File.Create(minMaxDestinations[i] + "mmAray.bin"); BinaryWriter writer = new BinaryWriter(stream); DebugLog.logConsole(minMaxDestinations[i] + "mmAray.bin had minimum :\n\t" + minMaxArray[i * 2].ToString() + "\nand maximum :\n\t" + minMaxArray[(i * 2) + 1].ToString()); writer.Write(minMaxArray[i * 2]); writer.Write(minMaxArray[(i * 2) + 1]); writer.Close(); stream.Close(); } }
public void createMesh(ConcurrentStack <string> stack, int resolution, string[] axis, float[] minMaxArray, float isoLevel) { DebugLog.logConsole("Mesh creation thread started"); totalMeshes = stack.Count; while (!stack.IsEmpty) { //If we fail popping, we may be attempting to access //at the same time as someone else. This means the stack //might have emptied since we last checked so we check again if (!stack.TryPop(out string file)) { continue; } //If we got this far, then we have a file DebugLog.logConsole(file + " has been selected"); SingularMesh alg = alg = new SingularMesh(); VoxelArray array = null; try { array = alg.createPointCloud(file, resolution, axis[0], axis[1], axis[2], minMaxArray); } catch (Exception) { DebugLog.logConsole("Threading failed at array creation for " + file); } MarchingCubes test = new MarchingCubes(); test.SetTarget(isoLevel); IM intermediate = null; try { intermediate = test.CreateMesh(array.toFloatArray()); } catch (Exception) { DebugLog.logConsole("Threading failed at intermediate creation for " + file); } try { if (intermediate.verts.Count > 64999) { DebugLog.logConsole("Dividing " + file); IM[] divs = intermediate.divideIntoSmall(); for (int i = 0; i < divs.Length; i++) { divs[i].WriteIntermediateToFile(alg.outputPath + "_" + i + ".IMF"); } } else { intermediate.WriteIntermediateToFile(alg.outputPath + ".IMF"); } DebugLog.logConsole(alg.outputPath + ".IMF"); } catch (Exception e) { DebugLog.logConsole(e.Message + "\n" + e.StackTrace + "\n" + file + "\n--------------------------------------------"); } array = null; alg = null; intermediate = null; try { double perc = ((double)(totalMeshes - stack.Count) / (double)totalMeshes) * 100; PercentageClass.UpdatePercentage("Creating Mesh", perc); DebugLog.logConsole("Mesh Creation: " + perc.ToString("#.##") + "% total"); } catch (Exception) { DebugLog.logConsole("Threading failed at percentage writing for " + file); } } }