Exemplo n.º 1
0
        /// <summary>
        /// Figures out what kind of data the file contains and passes it to parser.
        /// </summary>
        /// <param name="myStream">the file to parse</param>
        private void readFileData(Stream myStream)
        {
            // Make a new StreamReader to read the text input file.
            StreamReader reader = new StreamReader(myStream);

            // Check the first line of the file for its file type identifier.
            string fileType = reader.ReadLine().Trim();

            // The new file data object will be stored here if successful.
            FileDataAbstract fileData;

            // Try to read the file data.
            if (fileType == "I0") { // TODO: Finish other data file types.
                fileData = new FileDataI0(reader.ReadToEnd(), this.currentTimeStep); }
            // Throw an exception if the format is not recognized.
            else {
                throw new Exception("The file format was not recognized; only files in 'I0' format work at this time."); }

            // If there are already other data files, make sure they are compatible.
            if (this.fileDataList.Count > 0) {
                FileDataAbstract tempData = this.fileDataList[0];
                if (tempData.Xdim != fileData.Xdim || tempData.Ydim != fileData.Ydim || tempData.Zdim != fileData.Zdim) {
                    throw new Exception("The file you are attempting to add has different dimensions than the previously added file(s). "
                        + "If you would like to start over with new data, please clear the current data first."); } }

            // If we've gotten this far without Exception, we can
            // assume the file data will be added successfully.
            this.currentTimeStep++;
            this.fileDataList.Add(fileData);

            // Take the file data and turn it into a list of grid points.
            List<GridPoint> pointsList = fileData.GetSortedGridPointList();

            // If there are already other file(s) in the program, clear out
            // the current trees and add their data to the points list.
            if (this.fileDataList.Count > 1) {
                // Clear the trees.
                this.joinTreeView.ClearTrees();
                this.splitTreeView.ClearTrees();
                this.contourTreeView.ClearTrees();
                // Add the data.
                foreach (FileDataAbstract data in this.fileDataList) {
                    pointsList = pointsList.Union(data.GetSortedGridPointList()).ToList();
                    pointsList.Sort(new Comparison<GridPoint>(GridPoint.Compare)); } }

            // Create the join tree and split tree from the file data.
             JoinTree  joinTree = new  JoinTree(pointsList[0].value, pointsList.Last().value, pointsList);
            SplitTree splitTree = new SplitTree(pointsList[0].value, pointsList.Last().value, pointsList);

            // Simplify the trees, except for the others' critical points.
             joinTree.SimplifyExceptFor(splitTree.GetCritical());
            splitTree.SimplifyExceptFor( joinTree.GetCritical());

            // Make the contour tree.
            ContourTree contourTree = new ContourTree(joinTree, splitTree);

            this.contourTreeView.AddTree(contourTree);
            this.topology.SetTree(contourTree);
            this.topology.AddTimeStep();

            // Simplify trees the rest of the way.
             joinTree.Simplify();
            splitTree.Simplify();

            // Add trees to their views.
            this.joinTreeView.AddTree(joinTree);
            this.splitTreeView.AddTree(splitTree);

            // Dispose of unneeded resources.
            reader.Dispose();
            myStream.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add a new data file with formatting like the data file from Nathaniel.
        /// </summary>
        /// <param name="uselessParam1">required by C#</param>
        /// <param name="uselessParam2">required by C#</param>
        private void addNewNathanielDataFile(object uselessParam1, EventArgs uselessParam2)
        {
            // If opened properly, the file data will be stored here.
            Stream myStream = null;
            // This is a popup window that lets you pick a file to open.
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "C:\\";
            openFileDialog.Filter = "txt files (*.txt)|*.txt"; // Only look at text files.

            // If the file doesn't get opened / read / added properly, display an error message.
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                try {
                    if ((myStream = openFileDialog.OpenFile()) != null) {
                        // Make a new StreamReader to read the text input file.
                        StreamReader reader = new StreamReader(myStream);

                        // The new file data object will be stored here if successful.
                        FileDataAbstract fileData;
                        fileData = new FileDataNathaniel(reader.ReadToEnd(), this.currentTimeStep);

                        // If there are already other data files, make sure they are compatible.
                        if (this.fileDataList.Count > 0)
                        {
                            FileDataAbstract tempData = this.fileDataList[0];
                            if (tempData.Xdim != fileData.Xdim || tempData.Ydim != fileData.Ydim || tempData.Zdim != fileData.Zdim)
                            {
                                throw new Exception("The file you are attempting to add has different dimensions than the previously added file(s). "
                                    + "If you would like to start over with new data, please clear the current data first.");
                            }
                        }

                        // If we've gotten this far without Exception, we can
                        // assume the file data will be added successfully.
                        this.currentTimeStep++;
                        this.fileDataList.Add(fileData);

                        // Take the file data and turn it into a list of grid points.
                        List<GridPoint> pointsList = fileData.GetSortedGridPointList();

                        // If there are already other file(s) in the program, clear out
                        // the current trees and add their data to the points list.
                        if (this.fileDataList.Count > 1)
                        {
                            // Clear the trees.
                            this.joinTreeView.ClearTrees();
                            this.splitTreeView.ClearTrees();
                            this.contourTreeView.ClearTrees();
                            // Add the data.
                            foreach (FileDataAbstract data in this.fileDataList)
                            {
                                pointsList = pointsList.Union(data.GetSortedGridPointList()).ToList();
                                pointsList.Sort(new Comparison<GridPoint>(GridPoint.Compare));
                            }
                        }

                        // Create the join tree and split tree from the file data.
                        JoinTree joinTree = new JoinTree(pointsList[0].value, pointsList.Last().value, pointsList);
                        SplitTree splitTree = new SplitTree(pointsList[0].value, pointsList.Last().value, pointsList);

                        // Simplify the trees, except for the others' critical points.
                        joinTree.SimplifyExceptFor(splitTree.GetCritical());
                        splitTree.SimplifyExceptFor(joinTree.GetCritical());

                        // Make the contour tree.
                        ContourTree contourTree = new ContourTree(joinTree, splitTree);

                        this.contourTreeView.AddTree(contourTree);
                        this.topology.SetTree(contourTree);
                        this.topology.AddTimeStep();

                        // Simplify trees the rest of the way.
                        joinTree.Simplify();
                        splitTree.Simplify();

                        // Add trees to their views.
                        this.joinTreeView.AddTree(joinTree);
                        this.splitTreeView.AddTree(splitTree);

                        // Dispose of unneeded resources.
                        reader.Dispose();
                        myStream.Close();
                    }
                }
                catch (Exception ex) {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); }
            }
        }