public ptsDTMtriangleLine(ptsDTMpoint pt1, ptsDTMpoint pt2, ptsDTMtriangle tngle) { firstPoint = pt1; secondPoint = pt2; oneTriangle = tngle; theOtherTriangle = null; }
private void LoadTINfromVRML(string fileName) { string line; long lineCount = 0; if (!(String.Compare(Path.GetExtension(fileName), ".wrl", true) == 0)) { throw new ArgumentException("Filename must have wrl extension."); } System.IO.StreamReader file = new System.IO.StreamReader(fileName); try { while ((line = file.ReadLine()) != null) { if (false == validateVRMLfileHeader(line)) throw new System.IO.InvalidDataException("File not in VRML2 format."); break; } lineCount++; while ((line = file.ReadLine()) != null) { lineCount++; if(line.Equals("IndexedFaceSet")) break; } while ((line = file.ReadLine()) != null) { lineCount++; if (line.Equals("point")) { line = file.ReadLine(); // eat the open brace, [ break; } } ulong ptIndex=0; while ((line = file.ReadLine()) != null) { lineCount++; // Read until the close brace, [ if(line.Equals("]")) break; scratchPoint = convertLineOfDataToPoint(line, ptIndex); if (allPoints == null) { createAllpointsCollection(); myBoundingBox = new ptsBoundingBox2d(scratchPoint.x, scratchPoint.y,scratchPoint.x, scratchPoint.y); } allPoints.Add(ptIndex, scratchPoint); ptIndex++; myBoundingBox.expandByPoint(scratchPoint.x, scratchPoint.y, scratchPoint.z); } while ((line = file.ReadLine()) != null) { lineCount++; if (line.Equals("coordIndex")) { line = file.ReadLine(); // eat the open brace, [ break; } } var allTriangles_ = new List<ptsDTMtriangle>(); while ((line = file.ReadLine()) != null) { lineCount++; // Read until the close brace, [ if (line.Equals("]")) break; scratchTriangle = convertLineOfDataToTriangle(line); allTriangles_.Add(scratchTriangle); } allTriangles_.Sort(); allTriangles = allTriangles_.ToList(); } finally { file.Close(); } }
private ptsDTMtriangle convertLineOfDataToTriangle(string line) { UInt32 ptIndex1, ptIndex2, ptIndex3; string[] parsed = line.Split(','); int correction = parsed.Length - 4; ptIndex1 = Convert.ToUInt32(parsed[0 + correction]); ptIndex2 = Convert.ToUInt32(parsed[1 + correction]); ptIndex3 = Convert.ToUInt32(parsed[2 + correction]); ptsDTMtriangle triangle = new ptsDTMtriangle(allPoints, ptIndex1, ptIndex2, ptIndex3); return triangle; }
private bool addTriangleLine(UInt64 ndx1, UInt64 ndx2, ptsDTMtriangle aTriangle) { if (ndx1 == 0 || ndx2 == 0 || aTriangle == null) return false; if (ndx1 < ndx2) { scratchUIntPair.num1 = ndx1; scratchUIntPair.num2 = ndx2; } else { scratchUIntPair.num1 = ndx2; scratchUIntPair.num2 = ndx1; } if (triangleLines == null) { triangleLines = new Dictionary<UInt64pair, ptsDTMtriangleLine>(); scratchTriangleLine = new ptsDTMtriangleLine(allPoints[ndx1], allPoints[ndx2], aTriangle); triangleLines.Add(scratchUIntPair, scratchTriangleLine); return true; } bool tryGetSucces = triangleLines.TryGetValue(scratchUIntPair, out scratchTriangleLine); if (tryGetSucces == false) // we must add this line to the collection { scratchTriangleLine = new ptsDTMtriangleLine(allPoints[ndx1], allPoints[ndx2], aTriangle); triangleLines.Add(scratchUIntPair, scratchTriangleLine); return true; } else { if (scratchTriangleLine.theOtherTriangle == null) { scratchTriangleLine.theOtherTriangle = aTriangle; return true; } else { return false; } } }
// Constructor to construct from an xml file public ptsDTM(string fileName) { if (!(String.Compare(Path.GetExtension(fileName), "xml", true) == 0)) { //throw new notAnXMLfileException(); } setupStopWatches(); System.Console.WriteLine(""); System.Console.WriteLine("Load XML document"); XDocument tinXMLdoc = XDocument.Load(fileName); System.Console.WriteLine("Seeking Pnts collection"); XElement root = tinXMLdoc.Elements().ToList<XElement>()[0]; List<XElement> allXelements = root.Elements().ToList<XElement>(); foreach (XElement anElement in allXelements) { if (String.Compare(anElement.Name.ToString(), "Surfaces") == 0) { System.Console.WriteLine("Found Surfaces Collection"); XElement surfacesXE = anElement; XElement srface = surfacesXE.Elements().ToList<XElement>()[0]; List<XElement> srfcElements = srface.Elements().ToList<XElement>(); foreach (XElement aSubElement in srfcElements) { if (String.Compare(aSubElement.Name.ToString(), "Definition") == 0) { List<XElement> dfinitionElements = aSubElement.Elements().ToList<XElement>(); foreach (XElement subDef in dfinitionElements) { System.Console.WriteLine(subDef.Name); if (String.Compare(subDef.Name.ToString(), "Pnts") == 0) { System.Console.WriteLine("Found Pnts collection"); stpWatches["Process Points"].Start(); List<XElement> pnts = subDef.Elements().ToList<XElement>(); foreach (XElement pnt in pnts) { scratchPoint = new ptsDTMpoint(pnt.Value); UInt64 index; UInt64.TryParse(pnt.FirstAttribute.Value, out index); if (null == allPoints) allPoints = new Dictionary<UInt64, ptsDTMpoint>(); allPoints.Add(index, scratchPoint); } pnts = null; GC.Collect(); GC.WaitForPendingFinalizers(); stpWatches["Process Points"].Stop(); System.Console.WriteLine("Processing Points took:"); consoleOutStopwatch(stpWatches["Process Points"]); } if (String.Compare(subDef.Name.ToString(), "Faces") == 0) { ulong count; count = 0; stpWatches["Process Triangles"].Start(); List<XElement> faces = subDef.Elements().ToList<XElement>(); foreach (XElement aFace in faces) { count++; scratchTriangle = new ptsDTMtriangle(allPoints, aFace.Value.ToString()); if (null == allTriangles_genList) allTriangles_genList = new List<ptsDTMtriangle>(); allTriangles_genList.Add(scratchTriangle); if ((count % 100000) == 0) System.Console.WriteLine("{0} Triangles so far.", count); //To Do: start here: Add function to get Indexes of points // and assign to each triangle // or just hold the indices for now -- not sure -- sleep on it } System.Console.WriteLine("Total Triangles = {0}", count); System.Console.WriteLine("Freeing Faces collection"); faces = null; GC.Collect(); GC.WaitForPendingFinalizers(); System.Console.WriteLine("Copying triangle list to array"); allTriangles = allTriangles_genList.ToArray<ptsDTMtriangle>(); System.Console.WriteLine("Freeing triangle list"); allTriangles_genList = null; GC.Collect(); GC.WaitForPendingFinalizers(); System.Console.WriteLine("Sorting Triangle Array in x"); Array.Sort(allTriangles); System.Console.WriteLine("Sorting Triangle Array in y"); //not yet implemented stpWatches["Process Triangles"].Stop(); System.Console.WriteLine("Processing Triangles took:"); consoleOutStopwatch(stpWatches["Process Triangles"]); } // all processing is complete by this point } } } } } tinXMLdoc = null; GC.Collect(); GC.WaitForPendingFinalizers(); }