private static void WriteTesselatedObjectToIVFile(TesselatedObject vrmlData, string ivFilename) { using (StreamWriter writer = new StreamWriter(ivFilename)) { writer.WriteLine(IV_FILE_HEADER); for (int i = 0; i < vrmlData.Points.Length / 3; i++) { writer.WriteLine("\t\t\t{0} {1} {2},", vrmlData.Points[i, 0], vrmlData.Points[i, 1], vrmlData.Points[i, 2]); } writer.WriteLine("\t\t]"); writer.WriteLine("\t}"); writer.WriteLine("\tIndexedFaceSet {"); writer.WriteLine("\t\tcoordIndex ["); for (int i = 0; i < vrmlData.Connections.Length / 3; i++) { writer.WriteLine("\t\t\t{0}, {1}, {2}, -1,", vrmlData.Connections[i, 0], vrmlData.Connections[i, 1], vrmlData.Connections[i, 2]); } writer.WriteLine("\t\t]"); writer.WriteLine("\t}"); writer.WriteLine("}"); } }
public static TesselatedObject parseVRMLFile(string filename, bool autoFixMimics10Scale) { TesselatedObject vrmlData = new TesselatedObject(); string full; using (StreamReader r = new StreamReader(filename)) { full = r.ReadToEnd(); } Regex pointRegex = new Regex(@"point\s+\["); Regex closingRegex = new Regex("]"); Regex coordIndexRegex = new Regex(@"coordIndex\s+\["); /* if we are supposed to search for fixing Mimics Scale, * then we need to look before we strip the comments. The key * is a comment :) */ bool applyMimics10Scale = false; if (autoFixMimics10Scale) { //check for it, we will re-use this bool value as an indicator of if the scale needs to be changed if (full.Contains(MIMICS_10_SCALE_KEY) || full.Contains(MIMICS_13_SCALE_KEY)) { applyMimics10Scale = true; } } //Remove all comments from the file full = Regex.Replace(full, @"#.*$", "", RegexOptions.Multiline); Match pointMatch = pointRegex.Match(full); int s = pointMatch.Index; int e = closingRegex.Match(full, s + pointMatch.Length).Index; string ptsSection = full.Substring(s + pointMatch.Length, e - s - pointMatch.Length); Match coordMatch = coordIndexRegex.Match(full); s = coordMatch.Index; e = closingRegex.Match(full, s + coordMatch.Length).Index; string connSection = full.Substring(s + coordMatch.Length, e - s - coordMatch.Length); double[] scale = { 1.0, 1.0, 1.0 }; Match scaleMatch = Regex.Match(full, @"scale\s+([\d\.]+)\s+([\d\.]+)\s+([\d\.]+)"); if (scaleMatch.Success) { scale[0] = Double.Parse(scaleMatch.Groups[1].Value); scale[1] = Double.Parse(scaleMatch.Groups[1].Value); scale[2] = Double.Parse(scaleMatch.Groups[1].Value); } if (applyMimics10Scale) { scale[0] *= 1000; scale[1] *= 1000; scale[2] *= 1000; } vrmlData.Points = parsePts(ptsSection, scale); vrmlData.Connections = parseConn(connSection); return(vrmlData); }
public static void ConvertVRMLToIV(string vrmlFilename, string ivFilename, bool autoFixMimics10Scale) { TesselatedObject data = parseVRMLFile(vrmlFilename, autoFixMimics10Scale); WriteTesselatedObjectToIVFile(data, ivFilename); }
public VRMLParser(string fname) { _localObject = parseVRMLFile(fname); }