private void ButtonPointGroupsImport_OnClick(object sender, RoutedEventArgs e) { if (Options.Membrane.SurfaceMesh == null || Options.Membrane.SurfaceMesh.Triangles.Count == 0) { MessageBox.Show("This will not work without a mesh."); return; } System.Windows.Forms.OpenFileDialog Dialog = new System.Windows.Forms.OpenFileDialog(); Dialog.Filter = "Text File|*.txt|Session File|*.xml"; System.Windows.Forms.DialogResult Result = Dialog.ShowDialog(); if (Result.ToString() == "OK") { FileInfo Info = new FileInfo(Dialog.FileName); if (Info.Extension.ToLower().Replace(".", "") == "txt") { PointGroup NewGroup = new PointGroup { Name = Dialog.SafeFileName.Substring(0, Dialog.SafeFileName.LastIndexOf(".txt")), Size = 10, Color = ColorHelper.SpectrumColor(Options.Membrane.PointGroups.Count, 0.3f) }; NewGroup.PointCloud.GLContext = Options.Viewport.GetControl(); CultureInfo IC = CultureInfo.InvariantCulture; using (TextReader Reader = new StreamReader(File.OpenRead(Dialog.FileName))) { string Line; while ((Line = Reader.ReadLine()) != null) { if (Line[0] == '#') continue; string[] Parts = Line.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); if (Parts.Length < 3) continue; try { Vector3 Position = new Vector3(float.Parse(Parts[0], IC), float.Parse(Parts[1], IC), float.Parse(Parts[2], IC)); Triangle ClosestTri = Options.Membrane.SurfaceMesh.Triangles[0]; float ClosestDist = float.MaxValue; foreach (var tri in Options.Membrane.SurfaceMesh.Triangles) { float Dist = (tri.V0.Position - Position).Length; if (Dist < ClosestDist) { ClosestDist = Dist; ClosestTri = tri; } } OpenTK.Matrix3 Orientation = ClosestTri.GetPlaneMatrix3(); if (Parts.Length >= 3 + 9) { int Offset = Parts.Length - 9; Vector3 C1 = new Vector3(float.Parse(Parts[Offset + 0], IC), float.Parse(Parts[Offset + 1], IC), float.Parse(Parts[Offset + 2], IC)); Vector3 C2 = new Vector3(float.Parse(Parts[Offset + 3], IC), float.Parse(Parts[Offset + 4], IC), float.Parse(Parts[Offset + 5], IC)); Vector3 C3 = new Vector3(float.Parse(Parts[Offset + 6], IC), float.Parse(Parts[Offset + 7], IC), float.Parse(Parts[Offset + 8], IC)); Orientation = new OpenTK.Matrix3(C1.X, C2.X, C3.X, C1.Y, C2.Y, C3.Y, C1.Z, C2.Z, C3.Z); } SurfacePoint NewPoint = new SurfacePoint(Position, ClosestTri, Vector3.Zero, 0, Orientation); NewGroup.Points.Add(NewPoint); } catch { MessageBox.Show("Could not import:\n" + Line); } } } if (NewGroup.Points.Count > 0) { Options.Membrane.PointGroups.Add(NewGroup); Options.Viewport.Redraw(); } } else if (Info.Extension.ToLower().Replace(".", "") == "xml") { List<string> GroupNames = new List<string>(); using (Stream SessionStream = File.OpenRead(Dialog.FileName)) { XPathDocument Doc = new XPathDocument(SessionStream); XPathNavigator Reader = Doc.CreateNavigator(); Reader.MoveToRoot(); foreach (XPathNavigator groupNav in Reader.Select("//PointGroups/Group")) GroupNames.Add(XMLHelper.LoadAttribute(groupNav, "Name", "Group " + (GroupNames.Count + 1))); } if (GroupNames.Count == 0) return; PointGroupImportDialog ImportDialog = new PointGroupImportDialog(); foreach (var groupName in GroupNames) ImportDialog.AvailableGroups.Add(groupName); ImportDialog.SessionPath = Dialog.FileName; ImportDialog.ShowDialog(); } } }
public static float[] ToFloatArray(OpenTK.Matrix3 m) { return(new float[] { m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33 }); }