/// <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();
            }
        }
        /// <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>
        /// 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>
        /// 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();
        }