String数据转Matrix矩阵

String数据转Matrix矩阵

private Matrix String_To_Matrix(string str)

{

            int[] Remove_Num = new int[10];

            int Remove_Int = 0;

            string Str_One = str;

            //{ {M11:1 M12:0 M13:0 M14:0} {M21:0 M22:1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:0 M42:0 M43:0 M44:1} }

            //string Str_One = "{ {M11:1 M12:0 M13:0 M14:0} {M21:0 M22:1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:0 M42:0 M43:0 M44:1} }";

            for (int i = 0; i < Str_One.Length; i++)

            {

                //string str = Str_One[i].ToString ();

                if (Str_One[i].Equals('{') || Str_One[i].Equals('}'))

                {

                    Remove_Num[Remove_Int] = i;

                    Remove_Int += 1;

                }

            }

            Remove_Int = 0;

            int Remove_num_int = 0;

            string Str_Two = "";// M11:1 M12:0 M13:0 M14:0 M21:0 M22:1 M23:0 M24:0 M31:0 M32:0 M33:1 M34:0 M41:0 M42:0 M43:0 M44:1

            foreach (var item in Remove_Num)

            {

                //string str = item.ToString();

                Remove_num_int = item - Remove_Int;

                Str_Two = Str_One.Remove(Remove_num_int, 1);

                Str_One = Str_Two;

                Remove_Int += 1;

            }

            string[] M_int = new string[16];// 1 0 0 0 ,0 1 0 0 ,0 0 1 0 ,0 0 0 1

            for (int i = 0; i < 16; i++)

            {

                M_int[i] = (Str_Two.Split(' ')[i + 1]).Split(':')[1];

            }

            //Matrix aaaa = new Matrix(0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1);

            Matrix new_Matrix = new Matrix(float.Parse(M_int[0]), float.Parse(M_int[1]), float.Parse(M_int[2]), float.Parse(M_int[3]), float.Parse(M_int[4]), float.Parse(M_int[5]), float.Parse(M_int[6]), float.Parse(M_int[7]), float.Parse(M_int[8]), float.Parse(M_int[9]), float.Parse(M_int[10]), float.Parse(M_int[11]), float.Parse(M_int[12]), float.Parse(M_int[13]), float.Parse(M_int[14]), float.Parse(M_int[15]));

            return new_Matrix;

}

注:取Matrix格式string字符串,循环判断添加索引int[]数组,去除‘{}',再根据’  ‘进行对整体string分割取string[],之后依据':'分割读取float于float[]数组,对新建Matrix依次赋值,达到string转matrix效果.....

猜你喜欢

转载自www.cnblogs.com/XiaoLang0/p/10214056.html