/// <summary> /// Called when an item is saved. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">Event arguments.</param> public void OnSaveClicked(object sender, EventArgs e) { Model3DFile file = this.listModel3DPanel.GetSelectedItem <Model3DFile>(); if (file != null) { this.saveFileDialog.InitialDirectory = Path.GetDirectoryName(file.OriginalPath); this.saveFileDialog.FileName = file.Name; DialogResult dialogResult = this.saveFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { this.statusLabel.Text = "Writing " + file.Name + " to " + this.saveFileDialog.FileName + "."; FileReader fileReader = new FileReader(); Model3DData model3D = fileReader.ReadModel(file.EditPath); try { ObjFileWriter fileWriter = new ObjFileWriter(this.saveFileDialog.FileName); fileWriter.WriteModel(model3D.Model); fileWriter.Close(); this.statusLabel.Text = "Writing was finished."; file.Exported = true; this.listModel3DPanel.Refresh(); } catch { MessageBox.Show(this, "An error occurred when writing to a file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show(this, "Please select a model that should be saved.", "Model not selected", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Starts a nonrigid registration. /// </summary> /// <param name="sender">The sender of an event.</param> /// <param name="e">Event arguments.</param> private void toolsRegistrationNonrigidMenuItem_Click(object sender, EventArgs e) { NonrigidSettingsForm nonrigidSettingsForm = new NonrigidSettingsForm(this.model3DBean.ModelListBean); nonrigidSettingsForm.ShowDialog(this); if (nonrigidSettingsForm.DialogResult == DialogResult.OK) { this.statusProgressBar.Visible = true; this.statusLabel.Text = "Registration task is in progress."; for (int i = 0; i < nonrigidSettingsForm.SourceModels.Count; i++) { for (int j = 0; j < nonrigidSettingsForm.TargetModels.Count; j++) { BackgroundWorker worker = new BackgroundWorker(); worker.RunWorkerCompleted += this.ReportTaskCompleted; worker.DoWork += (obj, args) => { NonrigidArgs nonrigidArgs = (NonrigidArgs)args.Argument; FileReader fileReader = new FileReader(); Model3DData sourceModel = fileReader.ReadModel(nonrigidArgs.SourceModel.EditPath); Model3DData targetModel = fileReader.ReadModel(nonrigidArgs.TargetModel.EditPath); List <Vector <float> > sourceVertexBuffer = sourceModel.GetVertices(); List <Vector <float> > targetVertexBuffer = targetModel.GetVertices(); RigidRegistration rigidRegistration = new RigidRegistration(new KdTreeMapping(), nonrigidArgs.NumberOfIterations); NonrigidRegistration nonrigidRegistration = new NonrigidRegistration(rigidRegistration); sourceVertexBuffer = nonrigidRegistration.ComputeRegistration(sourceVertexBuffer, targetVertexBuffer); sourceModel.SetVertices(sourceVertexBuffer); ObjFileWriter fileWriter = new ObjFileWriter(nonrigidArgs.SourceModel.EditPath); fileWriter.WriteModel(sourceModel.Model); fileWriter.Close(); nonrigidArgs.SourceModel.Changed = true; nonrigidArgs.SourceModel.Exported = false; }; worker.RunWorkerAsync(new NonrigidArgs(nonrigidSettingsForm.SourceModels[i], nonrigidSettingsForm.TargetModels[j], nonrigidSettingsForm.MaxIterations)); } } } }
/// <summary> /// Writes the structure geometry to an .OBJ file for 3D applications /// </summary> /// <param name="s">the structure</param> /// <param name="objPath">the path to create the .OBJ file</param> public static void ToObjFile(this Structure s, string objPath) { ObjFileWriter.Write(s.MeshGeometry, objPath); }
void OnGUI() { float y = 0f; GUI.Label(new Rect(0f, y, 100f, 30f), ((int)div).ToString()); div = GUI.HorizontalSlider(new Rect(100f, y, 180f, 30f), div, 0f, 10.5f); GUI.Label(new Rect(300f, y, 200f, 30f), "V: " + mesh.vertexCount); y += 30f; if (GUI.Button(new Rect(0f, y, 100f, 50f), "Sphere")) { if (MeshGenerator.GenerateSphere(mesh, (int)div, separateFaces)) { mesh.name = string.Format("sphere_{0}", (int)div); } } y += 50f; if (GUI.Button(new Rect(0f, y, 100f, 50f), "Cylinder")) { if (MeshGenerator.GenerateCylinderSide(mesh, 1f, 0.5f, (int)div, separateFaces)) { mesh.name = string.Format("cylinder_{0}", (int)div); } } y += 50f; if (GUI.Button(new Rect(0f, y, 100f, 50f), "Wall")) { var d = 2 << (int)div; var radMax = Mathf.PI * 2f * 4f; var positions = new Vector2[d + 1]; for (int i = 0; i <= d; i++) { var t = 1f - (float)i / (float)d; var rad = radMax * t; positions[i] = new Vector2( Mathf.Cos(rad) * t * 0.5f, Mathf.Sin(rad) * t * 0.5f); } if (MeshGenerator.GenerateWall(mesh, positions, 0.1f, 0.05f, looped: false, separateFaces, 2)) { mesh.name = string.Format("wall_{0}", (int)div); } } y += 50f; if (GUI.Button(new Rect(0f, y, 100f, 50f), "ConvexPolygon")) { int d = 3 << (int)div; var positions = new Vector2[d]; for (int i = 0; i < positions.Length; i++) { var rad = 2f * Mathf.PI * (float)i / (float)positions.Length; positions[i] = new Vector2( Mathf.Sin(rad), Mathf.Cos(rad)) * 0.5f; } if (MeshGenerator.GenerateConvexPolygon(mesh, positions, 0.1f, separateFaces, 0.1f, 1)) { mesh.name = string.Format("convex_{0}", (int)div); } } y += 50f; #if UNITY_EDITOR if (GUI.Button(new Rect(0f, y, 100f, 50f), "Save")) { ObjFileWriter.Write("Assets/GeneratedMeshes", mesh, importImmediately: true); } y += 50f; #endif staticCamera.enabled = GUI.Toggle(new Rect(0f, y, 200f, 30f), staticCamera.enabled, "ShowStaticSample"); y += 30f; separateFaces = GUI.Toggle(new Rect(0f, y, 200f, 30f), separateFaces, "SeparateFaces"); }