static public bool LoadTopologyFromFile(string filename, out VascularNet r_vnet) { try { var text = File.ReadAllText(filename); return(LoadTopologyFromString(text, out r_vnet)); } catch { throw new TopReadingException("The topology file " + filename + " doesn't exist or can't be opened!\n"); } }
static public bool LoadTopologyFromString(string text, out VascularNet r_vnet) { ILoadVascularNet vnet = new VascularNet(); System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); string[] readText = text.Split(new[] { '\r', '\n' }); Regex regex = new Regex(@"^name:\s*(\w+)$", RegexOptions.IgnoreCase); int i = 0; while (!regex.IsMatch(readText[i])) { i++; if (i >= readText.Length) { throw new TopReadingException("No correct name of vascular system in found\n"); } } Match name_match = regex.Match(readText[i]); vnet.name = name_match.Groups[1].Value; Regex regex_1 = new Regex(@"^Coordinates:\s*$", RegexOptions.IgnoreCase); Regex regex_2 = new Regex(@"^Bonds:\s*$", RegexOptions.IgnoreCase); List <List <int> > bonds_index = new List <List <int> >(); int node_count = 0; int bond_string_count = 0; while (true) { i++; if (regex_1.IsMatch(readText[i])) { while (true) { i++; if (i >= readText.Length) { break; } //regex = new Regex(@"^\s*(\d+)\s+X:(-*\d+.\d+)\s+Y:(-*\d+.\d+)\s+Z:(-*\d+.\d+)\s+R:(-*\d+.\d+)\s+C:(\d+.\d+)$", RegexOptions.IgnoreCase); regex = new Regex(@"^\s*(\d+)\s+L:(-*\d+.\d+)\s+R:(-*\d+.\d+)\s+E:(-*\d+.\d+)\s+T:(-*\d+)$", RegexOptions.IgnoreCase); Match node_match = regex.Match(readText[i]); if (node_match.Groups.Count < 4) { //regex = new Regex(@"^\s*(\d+)\s+X:(-*\d+.\d+)\s+Y:(-*\d+.\d+)\s+Z:(-*\d+.\d+)\s+R:(-*\d+.\d+)$", RegexOptions.IgnoreCase); regex = new Regex(@"^\s*(\d+)\s+L:(-*\d+.\d+)\s+R:(-*\d+.\d+)$", RegexOptions.IgnoreCase); node_match = regex.Match(readText[i]); if (node_match.Groups.Count < 4) { break; } } int id = int.Parse(node_match.Groups[1].Value); //convert to meter from millimeter Vector1 position = new Vector1(double.Parse(node_match.Groups[2].Value) * 1e-3); double rad = double.Parse(node_match.Groups[3].Value); double elasticity = double.Parse(node_match.Groups[4].Value) * 1e6; int nodetype = int.Parse(node_match.Groups[5].Value); vnet.vascular_system.Add(new VascularNode(id, position, rad * 1e-3, elasticity, nodetype)); node_count++; } } else if (regex_2.IsMatch(readText[i])) { while (true) { i++; if (i >= readText.Length) { break; } regex = new Regex(@"(\d+)\s*", RegexOptions.IgnoreCase); MatchCollection node_match = regex.Matches(readText[i]); if (node_match.Count < 2) { break; } int id = int.Parse(node_match[0].Value); bonds_index.Add(new List <int>()); bonds_index[bonds_index.Count - 1].Add(id); for (int n = 1; n < node_match.Count; n++) { bonds_index[bonds_index.Count - 1].Add(int.Parse(node_match[n].Value)); } bond_string_count++; } } if (i >= readText.Length - 1) { foreach (var str in bonds_index) { VascularNode nd = vnet.vascular_system.Find(x => x.id == str[0]); for (int s = 1; s < str.Count; s++) { nd.addNeighbours(new VascularNode[] { vnet.vascular_system.Find(x => x.id == str[s]) }); } } break; } } r_vnet = (VascularNet)vnet; return(false); }