/// <summary>
 /// Reverts the last switch.
 /// </summary>
 /// <param name="sender">undoToolStripMenuItem or undoButton</param>
 /// <param name="e">Standard EventArgs</param>
 private void Undo(object sender, EventArgs e)
 {
     if (history.Count >= 1)
     {
         currentSelection = history[history.Count - 1];
         if (currentSelection[0] == -1)
         {
             parser.NormalArray = SwitchersHelpers.SwitchAll(parser.NormalArray);
             normalArray        = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);
         }
         else
         {
             parser.NormalArray = SwitchersHelpers.SwitchSelected(parser.NormalArray, currentSelection);
             normalArray        = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);
         }
         history.RemoveAt(history.Count - 1);
     }
     if (history.Count >= 1)
     {
         currentSelection = history[history.Count - 1];
     }
     else if (history.Count == 0)
     {
         currentSelection.Clear();
         undoButton.Enabled = false;
     }
     FillListView();
     visualization.Refresh();
 }
        /// <summary>
        /// Initializes the NormalSwitcherForm and opens the file given by <paramref name="file"/>
        /// </summary>
        /// <param name="file">Path of the file to be displayed</param>
        public NormalSwitcherForm(string file)
        {
            InitializeComponent();

            undoButton.EnabledChanged += new EventHandler(Undo_EnabledChanged);
            allButton.EnabledChanged  += new EventHandler(FileCondition_EnabledChanged);

            StreamReader reader = new StreamReader(file);

            try {
                parser.Parse(reader);
                normalArray   = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);
                vertexArray   = SwitchersHelpers.NormalizeVertexArray(parser.VertexArray, parser.Min, parser.Scale);
                backupNormals = new float[parser.NormalArray.Length];
                parser.NormalArray.CopyTo(backupNormals, 0);
                originTrackBar.Minimum     = -(int)(parser.Scale / 2);
                originTrackBar.Maximum     = (int)(parser.Scale / 2);
                originTrackBar.Value       = origin = 0;
                rotationOriginTextBox.Text = origin.ToString();
                originTrackBar.Visible     = true;
                InitVisualization();
                visualization.SetColorArray();

                currentFile       = file;
                allButton.Enabled = true;

                FillListView();
            } catch (Exception exception) {
                MessageBox.Show(exception.Message, "Error");
            } finally {
                reader.Close();
            }
        }
Example #3
0
 /// <summary>
 /// Saves the STL-data in the chosen file and the chosen format (ASCII or binary).
 /// </summary>
 /// <param name="sender">saveAsToolStripMenuItem</param>
 /// <param name="e">Standard EventArgs</param>
 private void SaveAs(object sender, EventArgs e)
 {
     if (triangleList.Count == 0)
     {
         MessageBox.Show("You need at least one triangle to save!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
     else
     {
         SaveFileDialog sfd = new SaveFileDialog();
         sfd.AddExtension    = true;
         sfd.DefaultExt      = "stl";
         sfd.Filter          = "ASCII STL File (*.stl)|*.stl|Binary STL File (*.stl)|*.stl";
         sfd.OverwritePrompt = true;
         if (sfd.ShowDialog() == DialogResult.OK)
         {
             if (sfd.FilterIndex == 1)
             {
                 SwitchersHelpers.WriteToASCII(sfd.FileName, triangleList);
             }
             else
             {
                 SwitchersHelpers.WriteToBinary(sfd.FileName, triangleList);
             }
             changed = false;
         }
     }
 }
        /// <summary>
        /// Sets all normal vectors back to the values from the STL-file.
        /// </summary>
        /// <param name="sender">resetToolStripMenuItem or resetButton</param>
        /// <param name="e">Standard EventArgs</param>
        private void Reset(object sender, EventArgs e)
        {
            backupNormals.CopyTo(parser.NormalArray, 0);
            normalArray = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);
            currentSelection.Clear();
            history.Clear();

            undoButton.Enabled = false;
            FillListView();
            visualization.Refresh();
        }
 /// <summary>
 /// Overwrites the opened file with the STL-data in the same format (ASCII or binary).
 /// </summary>
 /// <param name="sender">saveToolStripMenuItem</param>
 /// <param name="e">Standard EventArgs</param>
 private void SaveFile(object sender, EventArgs e)
 {
     if (parser.ASCII)
     {
         SwitchersHelpers.WriteToASCII(this.currentFile, parser.NormalArray, parser.VertexArray);
     }
     else
     {
         SwitchersHelpers.WriteToBinary(this.currentFile, parser.NormalArray, parser.VertexArray);
     }
 }
Example #6
0
        /// <summary>
        /// Sets a different color for each triangle for picking.
        /// </summary>
        private void SetPickingColors()
        {
            RGB color;

            this.pickingColors = new float[owner.VertexArray.Length];

            for (int i = 0; i < owner.VertexArray.Length / 9; i++)
            {
                color = SwitchersHelpers.GetRGBFromInt(i * colorDist);
                pickingColors[i * 9]     = pickingColors[i * 9 + 3] = pickingColors[i * 9 + 6] = color.R;
                pickingColors[i * 9 + 1] = pickingColors[i * 9 + 4] = pickingColors[i * 9 + 7] = color.G;
                pickingColors[i * 9 + 2] = pickingColors[i * 9 + 5] = pickingColors[i * 9 + 8] = color.B;
            }
        }
        /// <summary>
        /// Switches the selected normal vectors.
        /// </summary>
        /// <param name="sender">switchSelectedToolStripMenuItem or selectedButton</param>
        /// <param name="e">Standard EventArgs</param>
        private void SwitchSelected(object sender, EventArgs e)
        {
            if (currentSelection.Count > 0)
            {
                parser.NormalArray = SwitchersHelpers.SwitchSelected(parser.NormalArray, currentSelection);
                normalArray        = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);

                MakeHistory();

                undoButton.Enabled = true;
                UpdateListView();
                visualization.Refresh();
            }
        }
        /// <summary>
        /// Switches all normal vectors.
        /// </summary>
        /// <param name="sender">switchAllToolStripMenuItem or allButton</param>
        /// <param name="e">Standard EventArgs</param>
        private void SwitchAll(object sender, EventArgs e)
        {
            parser.NormalArray = SwitchersHelpers.SwitchAll(parser.NormalArray);
            normalArray        = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);

            currentSelection = new List <int>(new int[1] {
                -1
            });
            history.Add(currentSelection);

            undoButton.Enabled = true;
            FillListView();
            visualization.Refresh();
        }
        /// <summary>
        /// Sets a different color for each triangle for picking.
        /// </summary>
        internal void SetPickingColors()
        {
            RGB color;

            this.colorDist     = 256 * 256 * 256 / (owner.TriangleList.Count + 2);
            this.pickingColors = new float[owner.TriangleList.Count * 9];

            for (int i = 0; i < owner.TriangleList.Count; i++)
            {
                color = SwitchersHelpers.GetRGBFromInt(i * colorDist);
                pickingColors[i * 9]     = pickingColors[i * 9 + 3] = pickingColors[i * 9 + 6] = color.R;
                pickingColors[i * 9 + 1] = pickingColors[i * 9 + 4] = pickingColors[i * 9 + 7] = color.G;
                pickingColors[i * 9 + 2] = pickingColors[i * 9 + 5] = pickingColors[i * 9 + 8] = color.B;
            }
        }
Example #10
0
 /// <summary>
 /// When a button of the mouse is pressed, the values for the rotation are initialized.
 /// </summary>
 /// <param name="sender"> The mouse </param>
 /// <param name="ev"> Standard MouseEventArgs </param>
 private void MouseDownEvent(object sender, MouseEventArgs ev)
 {
     if (ev.Button == MouseButtons.Left)
     {
         int[] pickingRectangle = SwitchersHelpers.GetPickingRectangle(ev.X, this.Height - ev.Y, ev.X, this.Height - ev.Y);
         if (Control.ModifierKeys == Keys.Control)
         {
             this.PickTriangle(pickingRectangle, true);
         }
         else
         {
             this.PickTriangle(pickingRectangle, false);
         }
     }
     this.xDiff = ev.X - this.yRot;
     this.yDiff = ev.Y + this.xRot;
 }
        /// <summary>
        /// Reads the color of the pixel at the given position and picks the corresponding triangle
        /// </summary>
        /// <param name="rect">Contains, in this order,
        /// the X- and Y-coordinates of the lower left corner of the picking rectangle
        /// and its width and height.</param>
        /// <param name="additive">True, if the selected Triangle is to be added to the selection</param>
        private void PickTriangle(int[] rect, bool additive)
        {
            Gl.glDisable(Gl.GL_LIGHTING);
            Gl.glDisable(Gl.GL_LIGHT0);
            this.picking = true;

            float[] color = new float[3 * rect[2] * rect[3]];

            RenderScene();
            Gl.glReadBuffer(Gl.GL_BACK);
            Gl.glReadPixels(rect[0], rect[1], rect[2], rect[3], Gl.GL_RGB, Gl.GL_FLOAT, color);

            Gl.glEnable(Gl.GL_LIGHTING);
            Gl.glEnable(Gl.GL_LIGHT0);
            this.picking = false;

            owner.PickTriangle(SwitchersHelpers.UniqueSelection(color, colorDist, owner.TriangleList.Count), additive);
        }
Example #12
0
 /// <summary>
 /// Overwrites the opened file with the STL-data in the same format (ASCII or binary).
 /// </summary>
 /// <param name="sender">saveToolStripMenuItem</param>
 /// <param name="e">Standard EventArgs</param>
 private void SaveFile(object sender, EventArgs e)
 {
     if (triangleList.Count == 0)
     {
         MessageBox.Show("You need at least one triangle to save!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
     else
     {
         if (parser.ASCII)
         {
             SwitchersHelpers.WriteToASCII(this.currentFile, triangleList);
         }
         else
         {
             SwitchersHelpers.WriteToBinary(this.currentFile, triangleList);
         }
         changed = false;
     }
 }
Example #13
0
        /// <summary>
        /// Fills the Neighbors ComboBoxes with <paramref name="number"/> neighbors.
        /// </summary>
        /// <param name="number">Number of Neighbors</param>
        private void CalculateNeighbors(int number)
        {
            aNeighbors.DataSource = bNeighbors.DataSource = cNeighbors.DataSource = null;
            neighborsOfA.Clear();
            neighborsOfB.Clear();
            neighborsOfC.Clear();

            SortedList <string, Vertex> tempA = new SortedList <string, Vertex>();
            SortedList <string, Vertex> tempB = new SortedList <string, Vertex>();
            SortedList <string, Vertex> tempC = new SortedList <string, Vertex>();
            double dist;

            for (int i = 0; i < owner.TriangleList.Vertices.Length; i++)
            {
                dist = owner.CurrentSelection[0][0].DistanceFrom(owner.TriangleList.Vertices[i]);
                tempA.Add(SwitchersHelpers.GenerateKey(dist, i, owner.TriangleList.Vertices.Length), owner.TriangleList.Vertices[i]);
                dist = owner.CurrentSelection[0][1].DistanceFrom(owner.TriangleList.Vertices[i]);
                tempB.Add(SwitchersHelpers.GenerateKey(dist, i, owner.TriangleList.Vertices.Length), owner.TriangleList.Vertices[i]);
                dist = owner.CurrentSelection[0][2].DistanceFrom(owner.TriangleList.Vertices[i]);
                tempC.Add(SwitchersHelpers.GenerateKey(dist, i, owner.TriangleList.Vertices.Length), owner.TriangleList.Vertices[i]);
            }

            for (int k = 0; k < number; k++)
            {
                neighborsOfA.Add(tempA[tempA.Keys[k]]);
                neighborsOfB.Add(tempB[tempB.Keys[k]]);
                neighborsOfC.Add(tempC[tempC.Keys[k]]);
            }

            aNeighbors.DataSource    = neighborsOfA;
            aNeighbors.DisplayMember = "AsString";
            aNeighbors.SelectedIndex = 0;
            bNeighbors.DataSource    = neighborsOfB;
            bNeighbors.DisplayMember = "AsString";
            bNeighbors.SelectedIndex = 0;
            cNeighbors.DataSource    = neighborsOfC;
            cNeighbors.DisplayMember = "AsString";
            cNeighbors.SelectedIndex = 0;
            copyA.Enabled            = true;

            owner.Visualization.Fresh = true;
        }
        /// <summary>
        /// Saves the STL-data in the chosen file and the chosen format (ASCII or binary).
        /// </summary>
        /// <param name="sender">saveAsToolStripMenuItem</param>
        /// <param name="e">Standard EventArgs</param>
        private void SaveAs(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.AddExtension    = true;
            sfd.DefaultExt      = "stl";
            sfd.Filter          = "ASCII STL File (*.stl)|*.stl|Binary STL File (*.stl)|*.stl";
            sfd.OverwritePrompt = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (sfd.FilterIndex == 1)
                {
                    SwitchersHelpers.WriteToASCII(sfd.FileName, parser.NormalArray, parser.VertexArray);
                }
                else
                {
                    SwitchersHelpers.WriteToBinary(sfd.FileName, parser.NormalArray, parser.VertexArray);
                }
            }
        }
        /// <summary>
        /// Closes a previously opened file and opens a new one.
        /// Parses the file and initializes the arrays and GUI-elements.
        /// </summary>
        /// <param name="sender">openToolStripMenuItem</param>
        /// <param name="e">Standard EventArgs</param>
        private void OpenFile(object sender, EventArgs e)
        {
            CloseFile(sender, e);

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.CheckFileExists = true;
            ofd.DefaultExt      = "stl";
            ofd.Filter          = "STL Files (*.stl)|*.stl";
            ofd.Multiselect     = false;
            ofd.Title           = "Open STL-file";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StreamReader reader = new StreamReader(ofd.FileName);
                try {
                    parser.Parse(reader);
                    normalArray   = SwitchersHelpers.ExpandNormalArray(parser.NormalArray);
                    vertexArray   = SwitchersHelpers.NormalizeVertexArray(parser.VertexArray, parser.Min, parser.Scale);
                    backupNormals = new float[parser.NormalArray.Length];
                    parser.NormalArray.CopyTo(backupNormals, 0);
                    originTrackBar.Minimum     = -(int)(parser.Scale / 2);
                    originTrackBar.Maximum     = (int)(parser.Scale / 2);
                    originTrackBar.Value       = origin = 0;
                    rotationOriginTextBox.Text = origin.ToString();
                    originTrackBar.Visible     = true;
                    InitVisualization();
                    visualization.SetColorArray();

                    currentFile       = ofd.FileName;
                    allButton.Enabled = true;

                    FillListView();
                } catch (Exception exception) {
                    MessageBox.Show(exception.Message, "Error");
                } finally {
                    reader.Close();
                }
            }
            ofd.Dispose();
        }