Exemple #1
0
        public void ReadTextLinesTest()
        {
            string fname    = "test123.txt";
            string filePath = Path.Combine(_testDirectory, fname);
            File   target   = new File(filePath);

            string[] stlines  = { "This is line1", "This is line2", "This is line3" };
            Encoding encoding = Encoding.UTF32;

            System.IO.File.WriteAllLines(filePath, stlines, encoding);

            List <string> expected = new List <string>();

            expected.AddRange(stlines);

            List <string> actual;

            actual = target.ReadTextLines(encoding);

            if (expected.Count != actual.Count)
            {
                Assert.Fail("No of Lines not equal");
            }

            int i = 0;

            foreach (string l in expected)
            {
                if (l != actual[i++])
                {
                    Assert.Fail("String line not equal");
                }
            }
        }
Exemple #2
0
        public void ReadTextLinesTestWhenFileBeingAccessedByAnotherProcess()
        {
            string fname    = "ReadTextLinesTest.txt";
            string filePath = Path.Combine(_testDirectory, fname);
            File   target   = new File(filePath);

            var task = new Task(() => UseFileStream3Second(filePath));

            task.Start();
            Delay(200).Wait(); // Let the task run and acquire the file lock
            Assert.DoesNotThrow(() => target.ReadTextLines());
        }
Exemple #3
0
        /// <summary>
        /// Gets rotation and transformation matrix from a MATRIX file.
        /// </summary>
        /// <param name="matrixFile">The MATRIX file.</param>
        /// <returns>The transformation matrix.</returns>
        private Tuple <double[, ], double[]> GetRotationMatrixAndTranslationFromMatrixFile(File matrixFile)
        {
            int NUMBER_OF_ROWS_IN_TRANSFORMATION_MATRIX    = 4;
            int NUMBER_OF_COLUMNS_IN_TRANSFORMATION_MATRIX = 4;
            int NUMBER_OF_ROWS_IN_ROTATION_MATRIX          = 3;
            int NUMBER_OF_COLUMNS_IN_ROTATION_MATRIX       = 3;
            int NUMBER_OF_ELEMENTS_IN_TRANSLATION_VECTOR   = 3;

            var rows = matrixFile.ReadTextLines();

            if (rows.Count() != NUMBER_OF_ROWS_IN_TRANSFORMATION_MATRIX)
            {
                throw new ArgumentException(
                          $"Failed to load 'matrix' file. The file should have {NUMBER_OF_ROWS_IN_TRANSFORMATION_MATRIX} lines. The first line should be xAxis.i|yAxis.i|zAxis.i|x. The second line should be xAxis.j|yAxis.j|zAxis.j|y. The third line should be xAxis.k|yAxis.k|zAxis.k|z");
            }

            double[,] rotationMatrix = new double[NUMBER_OF_ROWS_IN_ROTATION_MATRIX, NUMBER_OF_COLUMNS_IN_ROTATION_MATRIX];
            double[] translation = new double[NUMBER_OF_ELEMENTS_IN_TRANSLATION_VECTOR];

            // The expected format of the transformation matrix is the following:
            //xAxis.i|yAxis.i|zAxis.i|x
            //xAxis.j|yAxis.j|zAxis.j|y
            //xAxis.k|yAxis.k|zAxis.k|z
            //0|0|0|1

            // The 1st column is the new x vector relative to World
            // The 2nd column is the new y vector relative to World
            // The 3rd column is the new z vector relative to World
            // The 4th column is the new origin position relative to World
            for (int l = 0; l < NUMBER_OF_ROWS_IN_ROTATION_MATRIX; l++)
            {
                var rowInvariant = rows[l].ToLowerInvariant();
                var rowItems     = rowInvariant.Split('|');

                if (rowItems.Count() != NUMBER_OF_COLUMNS_IN_TRANSFORMATION_MATRIX)
                {
                    throw new ArgumentException(
                              $"Failed to load 'matrix' file. The file should have per line {NUMBER_OF_COLUMNS_IN_TRANSFORMATION_MATRIX} numeric values separated by '|'.");
                }

                for (int c = 0; c < NUMBER_OF_COLUMNS_IN_TRANSFORMATION_MATRIX; c++)
                {
                    double rowValue;
                    try
                    {
                        rowValue = Convert.ToDouble(rowItems[c]);
                        if (c == 3)
                        {
                            // In the 4th column are the translation values
                            translation[l] = rowValue;
                        }
                        else
                        {
                            rotationMatrix[l, c] = rowValue;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new ArgumentException($"Invalid MATRIX file. No numeric value found in : {rowItems[c]}", ex);
                    }
                }
            }

            return(new Tuple <double[, ], double[]>(rotationMatrix, translation));
        }
Exemple #4
0
        /// <summary>
        /// Creates a transformation matrix from a TRX file.
        /// </summary>
        /// <param name="trxFile">The TRX file.</param>
        /// <returns>The transformation matrix.</returns>
        private Tuple <double[, ], double[]> GetRotationMatrixAndTranslationFromTrxFile(File trxFile)
        {
            var lines = trxFile.ReadTextLines();

            double xRotation    = 0;
            double yRotation    = 0;
            double zRotation    = 0;
            double xTranslation = 0;
            double yTranslation = 0;
            double zTranslation = 0;

            foreach (var line in lines)
            {
                var      lineInvariant = line.ToLowerInvariant();
                string[] lineSplit     = lineInvariant.Split(':');
                double   lineValue;
                try
                {
                    lineValue = Convert.ToDouble(lineSplit.Last());
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(
                              String.Format("Invalid TRX file. No numeric value found in this TRX line: {0}", line),
                              ex);
                    throw;
                }

                var lineIdentifier = lineSplit.First();
                if (lineIdentifier.Contains("rot_x"))
                {
                    xRotation = lineValue;
                }
                else if (lineIdentifier.Contains("rot_y"))
                {
                    yRotation = lineValue;
                }
                else if (lineIdentifier.Contains("rot_z"))
                {
                    zRotation = lineValue;
                }
                else if (lineIdentifier.Contains("shift_x"))
                {
                    xTranslation = lineValue;
                }
                else if (lineIdentifier.Contains("shift_y"))
                {
                    yTranslation = lineValue;
                }
                else if (lineIdentifier.Contains("shift_z"))
                {
                    zTranslation = lineValue;
                }
                else
                {
                    throw new ArgumentException(
                              String.Format("Invalid TRX File. TRX line should be rotation or shift in X,Y or Z: {0}", line));
                }
            }

            double[,] rotationmatrix = new Angles(xRotation * Math.PI / 180.0,
                                                  yRotation * Math.PI / 180.0,
                                                  zRotation * Math.PI / 180.0,
                                                  Conventions.XYZ).Matrix;
            double[] translation = { xTranslation, yTranslation, zTranslation };
            return(new Tuple <double[, ], double[]>(rotationmatrix, translation));
        }