Exemplo n.º 1
0
        public void Parse()
        {
            Posture p = new Posture();

            bool IsFirstElement = true;

            StreamReader sread = new StreamReader(_path);

            while (!sread.EndOfStream)
            {
                string buffer = sread.ReadLine();
                buffer = buffer.TrimStart();

                if ('#' == buffer[0])    // 注释行
                {
                    continue;
                }

                if (':' == buffer[0])
                {
                    continue;
                }

                string[] splitbuff = buffer.Split(new Char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                Bone bone = new Bone();
                bone.Name = splitbuff[0];

                if (1 == splitbuff.Length)
                {
                    if (!IsFirstElement)
                    {
                        _postures.Add(p.Clone());
                        p.Bones.Clear();
                    }

                    if (IsFirstElement)
                    {
                        IsFirstElement = false;
                    }
                    continue;
                }

                List <string> dofss = new List <string>();
                for (int i = 1; i < splitbuff.Length; i++)
                {
                    dofss.Add(splitbuff[i]);
                }
                bone.Dofs = dofss.ToArray();
                p.Bones.Add(bone.Clone());
                if (sread.EndOfStream)
                {
                    _postures.Add(p.Clone());
                }
            }

            sread.Close();
        }
Exemplo n.º 2
0
        private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            Bone lvsDisp = (Bone)((ListBox)sender).SelectedItem;

            if (lvsDisp != null)
            {
                this.ucBoneEdit1.bone = (Bone)lvsDisp.Clone();
                this.ucBoneEdit1.RefreshEditor();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据文件建立骨架
        /// </summary>
        /// <param name="filename">asf文件的绝对路径</param>
        /// <returns>Skeleton 对象</returns>
        public static Skeleton CreateSkeletonFromAsfFile(string filename)
        {
            AsfParse gap = new AsfParse(filename);

            gap.Parse();

            Skeleton ske = new Skeleton();

            Bone root = new Bone();

            root.Name = "root";
            root.DofNames.Add("tx"); root.DofNames.Add("ty"); root.DofNames.Add("ty");
            root.DofNames.Add("rx"); root.DofNames.Add("ry"); root.DofNames.Add("rz");
            ske.Bones.Add(root.Name, root);

            // 关节信息
            int numbone = gap.Bones.Count;

            for (int indbone = 0; indbone < numbone; ++indbone)
            {
                Bone bone = new Bone();

                bone.Name   = gap.Bones[indbone].Name;
                bone.Length = float.Parse(gap.Bones[indbone].Length);

                bone.Dir.x = float.Parse(gap.Bones[indbone].Direction[0]);
                bone.Dir.y = float.Parse(gap.Bones[indbone].Direction[1]);
                bone.Dir.z = float.Parse(gap.Bones[indbone].Direction[2]);

                bone.Axis.x = float.Parse(gap.Bones[indbone].Axis[0]);
                bone.Axis.y = float.Parse(gap.Bones[indbone].Axis[1]);
                bone.Axis.z = float.Parse(gap.Bones[indbone].Axis[2]);


                int numdof = 0;
                if (gap.Bones[indbone].Dofs != null)
                {
                    numdof = gap.Bones[indbone].Dofs.Length;
                }

                for (int inddof = 0; inddof < numdof; ++inddof)
                {
                    string namedof = gap.Bones[indbone].Dofs[inddof];

                    bone.DofNames.Add(namedof);
                }
                ske.Bones.Add(bone.Name, bone.Clone());
            }

            // -- 关节关联
            int numhier = gap.HierarchyItems.Count;

            for (int indhier = 0; indhier < numhier; ++indhier)
            {
                string bonename    = gap.HierarchyItems[indhier].Parent;
                int    numchildren = gap.HierarchyItems[indhier].Children.Count;
                for (int indchild = 0; indchild < numchildren; ++indchild)
                {
                    string childname = gap.HierarchyItems[indhier].Children[indchild];

                    Skeleton.Bone parent = ske.Bones[bonename];
                    Skeleton.Bone child  = ske.Bones[childname];
                    if (parent.Children == null)
                    {
                        parent.Children = new List <Bone>();
                    }
                    parent.Children.Add(child);
                    child.Parent = parent;

                    Matrix3 childmx     = new Matrix3();
                    Matrix3 childmy     = new Matrix3();
                    Matrix3 childmz     = new Matrix3();
                    Matrix3 parentinvmx = new Matrix3();
                    Matrix3 parentinvmy = new Matrix3();
                    Matrix3 parentinvmz = new Matrix3();

                    childmx.FromAxisAngle(Vector3.UNIT_X, child.Axis.x);//子节点:从局部坐标系到全局坐标系
                    childmy.FromAxisAngle(Vector3.UNIT_Y, child.Axis.y);
                    childmz.FromAxisAngle(Vector3.UNIT_Z, child.Axis.z);
                    parentinvmx.FromAxisAngle(Vector3.UNIT_X, -parent.Axis.x);  //父节点:从全局坐标系到局部坐标系
                    parentinvmy.FromAxisAngle(Vector3.UNIT_Y, -parent.Axis.y);
                    parentinvmz.FromAxisAngle(Vector3.UNIT_Z, -parent.Axis.z);
                    child.ParentOrient.FromRotationMatrix(parentinvmx * parentinvmy * parentinvmz * childmz * childmy * childmx);


                    Matrix3 childinvmx = new Matrix3();
                    Matrix3 childinvmy = new Matrix3();
                    Matrix3 childinvmz = new Matrix3();

                    childinvmx.FromAxisAngle(Vector3.UNIT_X, -child.Axis.x);//子节点:从全局坐标系到局部坐标系
                    childinvmy.FromAxisAngle(Vector3.UNIT_Y, -child.Axis.y);
                    childinvmz.FromAxisAngle(Vector3.UNIT_Z, -child.Axis.z);
                    child.Dir = childinvmx * childinvmy * childinvmz * child.Dir;
                    child.Dir.Normalize();
                }
            }

            return(ske);
        }
Exemplo n.º 4
0
        public void Parse()
        {
            ReadStage stage = ReadStage.Header;

            Bone tempbone = new Bone();

            HierarchyItem hierarchyitem = new HierarchyItem();

            StreamReader sread = new StreamReader(this._path);

            while (!sread.EndOfStream)
            {
                string buffer = sread.ReadLine();
                buffer = buffer.TrimStart();
                buffer = buffer.TrimEnd();

                if ('#' == buffer[0])  // 注释
                {
                    continue;
                }

                string[] splitbuff = buffer.Split();
                switch (stage)
                {
                    #region Parse
                case ReadStage.Header:
                    switch (splitbuff[0])
                    {
                        #region Header
                    case ":version":
                        break;

                    case ":name":
                        break;

                    case ":units":
                        break;

                    case "mass":            // of :units
                        break;

                    case "length":          // of :units
                        goto case "mass";

                    case "angle":           // of :units
                        goto case "mass";

                    case ":documentation":
                        break;

                    case ":root":
                        stage = ReadStage.Root;
                        break;

                    default:
                        break;
                        #endregion
                    }
                    break;

                case ReadStage.Root:
                    switch (splitbuff[0])
                    {
                        #region Root
                    case "order":
                        break;

                    case "axis":
                        break;

                    case "position":
                        break;

                    case "orientation":
                        break;

                    case ":bonedata":
                        stage = ReadStage.Bonedata;
                        break;
                        #endregion
                    }
                    break;

                case ReadStage.Bonedata:
                    switch (splitbuff[0])
                    {
                        #region Bonedata
                    case "begin":
                        break;

                    case "id":
                        break;

                    case "name":
                        tempbone.Name = splitbuff[1];
                        break;

                    case "direction":
                        tempbone.Direction = new string[] {
                            splitbuff[1],
                            splitbuff[2],
                            splitbuff[3]
                        };
                        break;

                    case "length":
                        tempbone.Length = splitbuff[1];
                        break;

                    case "axis":
                        tempbone.Axis = new string[] {
                            splitbuff[1],
                            splitbuff[2],
                            splitbuff[3]
                        };
                        break;

                    case "dof":
                        List <string> dofnames = new List <string>();
                        for (int i = 1; i < splitbuff.Length; ++i)
                        {
                            dofnames.Add(splitbuff[i]);
                        }
                        tempbone.Dofs = dofnames.ToArray();
                        break;

                    case "limits":
                        break;

                    case "end":
                        _bones.Add(tempbone.Clone());
                        tempbone = new Bone();
                        break;

                    case ":hierarchy":
                        stage = ReadStage.Hierarchy;
                        break;

                    default:
                        break;
                        #endregion
                    }
                    break;

                case ReadStage.Hierarchy:
                    switch (splitbuff[0])
                    {
                        #region Hierarchy
                    case "begin":
                        break;

                    case "end":
                        break;

                    default:
                        hierarchyitem.Parent = splitbuff[0];

                        for (int i = 1; i < splitbuff.Length; ++i)
                        {
                            hierarchyitem.Children.Add(splitbuff[i]);
                        }

                        _hierarchyitems.Add(hierarchyitem.Clone());
                        hierarchyitem.Children.Clear();
                        break;
                        #endregion
                    }
                    break;

                default:
                    break;
                    #endregion
                }
            }

            sread.Close();
        }