예제 #1
0
        /// <summary>
        /// Creates a transformation matrix from a MAT file.
        /// </summary>
        /// <param name="matFile">The MAT file.</param>
        /// <returns>The transformation matrix.</returns>
        private Tuple <double[, ], double[]> GetRotationMatrixAndTranslationFromMatFile(File matFile)
        {
            StreamReader myFile      = new StreamReader(matFile.Path);
            double       xRotation   = 0;
            double       yRotation   = 0;
            double       zRotation   = 0;
            var          translation = new double[3];
            bool         invert      = false;

            double rotationInDegrees;

            while (myFile.EndOfStream == false)
            {
                string   myLine     = myFile.ReadLine();
                string[] splitLine  = myLine.Split();
                var      lineValues = splitLine.Where(x => !string.IsNullOrEmpty(x)).ToList();

                // PowerInspect writes the MAT files using InvariantCulture

                if (lineValues.Any())
                {
                    switch (lineValues[0])
                    {
                    case "R":
                        switch (lineValues[1])
                        {
                        case "X":
                            rotationInDegrees = Convert.ToDouble(lineValues[2], CultureInfo.InvariantCulture);
                            xRotation         = rotationInDegrees * Math.PI / 180;

                            break;

                        case "Y":
                            rotationInDegrees = Convert.ToDouble(lineValues[2], CultureInfo.InvariantCulture);
                            yRotation         = rotationInDegrees * Math.PI / 180;

                            break;

                        case "Z":
                            rotationInDegrees = Convert.ToDouble(lineValues[2], CultureInfo.InvariantCulture);
                            zRotation         = rotationInDegrees * Math.PI / 180;

                            break;
                        }
                        break;

                    case "T":
                        translation[0] = Convert.ToDouble(lineValues[1], CultureInfo.InvariantCulture);
                        translation[1] = Convert.ToDouble(lineValues[2], CultureInfo.InvariantCulture);
                        translation[2] = Convert.ToDouble(lineValues[3], CultureInfo.InvariantCulture);
                        break;

                    case "I":
                        invert = true;

                        break;
                    }
                }
            }
            myFile.Close();

            // PowerInspect uses the convention XYZ extrinsic (which means 1st rotate around x, then y, then z regarding to the world)
            double[,] rotationMatrix = new Angles(xRotation, yRotation, zRotation, Conventions.XYZ).Matrix;

            if (invert)
            {
                //Invert the Rotation matrix and invert the Translation vector.
                //This is equivalent to transpose the Rotation Matrix(Rt) and for the translation vector is equivalent to multiply it by -Rt
                //(http://www.cg.info.hiroshima-cu.ac.jp/~miyazaki/knowledge/teche53.html)
                rotationMatrix = rotationMatrix.Transpose();
                var negativeRotationMatrix = rotationMatrix.Multiply(-1);
                translation = negativeRotationMatrix.Multiply(translation);

                translation[0] = translation[0];
                translation[1] = translation[1];
                translation[2] = translation[2];
            }

            return(new Tuple <double[, ], double[]>(rotationMatrix, translation));
        }